sdl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * QEMU SDL display driver
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. /* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
  25. #undef WIN32_LEAN_AND_MEAN
  26. #include "qemu/osdep.h"
  27. #include <SDL.h>
  28. #include <SDL_syswm.h>
  29. #include "qemu-common.h"
  30. #include "qemu/cutils.h"
  31. #include "ui/console.h"
  32. #include "ui/input.h"
  33. #include "sysemu/sysemu.h"
  34. #ifndef WIN32
  35. #include "x_keymap.h"
  36. #endif
  37. #include "sdl_zoom.h"
  38. static DisplayChangeListener *dcl;
  39. static DisplaySurface *surface;
  40. static SDL_Surface *real_screen;
  41. static SDL_Surface *guest_screen = NULL;
  42. static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
  43. static int last_vm_running;
  44. static bool gui_saved_scaling;
  45. static int gui_saved_width;
  46. static int gui_saved_height;
  47. static int gui_saved_grab;
  48. static int gui_fullscreen;
  49. static int gui_noframe;
  50. static int gui_key_modifier_pressed;
  51. static int gui_keysym;
  52. static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
  53. static uint8_t modifiers_state[256];
  54. static SDL_Cursor *sdl_cursor_normal;
  55. static SDL_Cursor *sdl_cursor_hidden;
  56. static int absolute_enabled = 0;
  57. static int guest_cursor = 0;
  58. static int guest_x, guest_y;
  59. static SDL_Cursor *guest_sprite = NULL;
  60. static SDL_PixelFormat host_format;
  61. static int scaling_active = 0;
  62. static Notifier mouse_mode_notifier;
  63. static int idle_counter;
  64. static const guint16 *keycode_map;
  65. static size_t keycode_maplen;
  66. #define SDL_REFRESH_INTERVAL_BUSY 10
  67. #define SDL_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
  68. / SDL_REFRESH_INTERVAL_BUSY + 1)
  69. #if 0
  70. #define DEBUG_SDL
  71. #endif
  72. static void sdl_update(DisplayChangeListener *dcl,
  73. int x, int y, int w, int h)
  74. {
  75. SDL_Rect rec;
  76. rec.x = x;
  77. rec.y = y;
  78. rec.w = w;
  79. rec.h = h;
  80. #ifdef DEBUG_SDL
  81. printf("SDL: Updating x=%d y=%d w=%d h=%d (scaling: %d)\n",
  82. x, y, w, h, scaling_active);
  83. #endif
  84. if (guest_screen) {
  85. if (!scaling_active) {
  86. SDL_BlitSurface(guest_screen, &rec, real_screen, &rec);
  87. } else {
  88. if (sdl_zoom_blit(guest_screen, real_screen, SMOOTHING_ON, &rec) < 0) {
  89. fprintf(stderr, "Zoom blit failed\n");
  90. exit(1);
  91. }
  92. }
  93. }
  94. SDL_UpdateRect(real_screen, rec.x, rec.y, rec.w, rec.h);
  95. }
  96. static void do_sdl_resize(int width, int height, int bpp)
  97. {
  98. int flags;
  99. SDL_Surface *tmp_screen;
  100. #ifdef DEBUG_SDL
  101. printf("SDL: Resizing to %dx%d bpp %d\n", width, height, bpp);
  102. #endif
  103. flags = SDL_HWSURFACE | SDL_ASYNCBLIT | SDL_HWACCEL;
  104. if (gui_fullscreen) {
  105. flags |= SDL_FULLSCREEN;
  106. } else {
  107. flags |= SDL_RESIZABLE;
  108. }
  109. if (gui_noframe)
  110. flags |= SDL_NOFRAME;
  111. tmp_screen = SDL_SetVideoMode(width, height, bpp, flags);
  112. if (!real_screen) {
  113. if (!tmp_screen) {
  114. fprintf(stderr, "Could not open SDL display (%dx%dx%d): %s\n",
  115. width, height, bpp, SDL_GetError());
  116. exit(1);
  117. }
  118. } else {
  119. /*
  120. * Revert to the previous video mode if the change of resizing or
  121. * resolution failed.
  122. */
  123. if (!tmp_screen) {
  124. fprintf(stderr, "Failed to set SDL display (%dx%dx%d): %s\n",
  125. width, height, bpp, SDL_GetError());
  126. return;
  127. }
  128. }
  129. real_screen = tmp_screen;
  130. }
  131. static void sdl_switch(DisplayChangeListener *dcl,
  132. DisplaySurface *new_surface)
  133. {
  134. PixelFormat pf;
  135. /* temporary hack: allows to call sdl_switch to handle scaling changes */
  136. if (new_surface) {
  137. surface = new_surface;
  138. }
  139. pf = qemu_pixelformat_from_pixman(surface->format);
  140. if (!scaling_active) {
  141. do_sdl_resize(surface_width(surface), surface_height(surface), 0);
  142. } else if (real_screen->format->BitsPerPixel !=
  143. surface_bits_per_pixel(surface)) {
  144. do_sdl_resize(real_screen->w, real_screen->h,
  145. surface_bits_per_pixel(surface));
  146. }
  147. if (guest_screen != NULL) {
  148. SDL_FreeSurface(guest_screen);
  149. }
  150. #ifdef DEBUG_SDL
  151. printf("SDL: Creating surface with masks: %08x %08x %08x %08x\n",
  152. pf.rmask, pf.gmask, pf.bmask, pf.amask);
  153. #endif
  154. guest_screen = SDL_CreateRGBSurfaceFrom
  155. (surface_data(surface),
  156. surface_width(surface), surface_height(surface),
  157. surface_bits_per_pixel(surface), surface_stride(surface),
  158. pf.rmask, pf.gmask,
  159. pf.bmask, pf.amask);
  160. }
  161. static bool sdl_check_format(DisplayChangeListener *dcl,
  162. pixman_format_code_t format)
  163. {
  164. /*
  165. * We let SDL convert for us a few more formats than,
  166. * the native ones. Thes are the ones I have tested.
  167. */
  168. return (format == PIXMAN_x8r8g8b8 ||
  169. format == PIXMAN_b8g8r8x8 ||
  170. format == PIXMAN_x1r5g5b5 ||
  171. format == PIXMAN_r5g6b5);
  172. }
  173. /* generic keyboard conversion */
  174. #include "sdl_keysym.h"
  175. static kbd_layout_t *kbd_layout = NULL;
  176. static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
  177. {
  178. int keysym;
  179. /* workaround for X11+SDL bug with AltGR */
  180. keysym = ev->keysym.sym;
  181. if (keysym == 0 && ev->keysym.scancode == 113)
  182. keysym = SDLK_MODE;
  183. /* For Japanese key '\' and '|' */
  184. if (keysym == 92 && ev->keysym.scancode == 133) {
  185. keysym = 0xa5;
  186. }
  187. return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
  188. }
  189. static const guint16 *sdl_get_keymap(size_t *maplen)
  190. {
  191. #if defined(WIN32)
  192. *maplen = qemu_input_map_atset1_to_qcode_len;
  193. return qemu_input_map_atset1_to_qcode;
  194. #else
  195. #if defined(SDL_VIDEO_DRIVER_X11)
  196. SDL_SysWMinfo info;
  197. SDL_VERSION(&info.version);
  198. if (SDL_GetWMInfo(&info) > 0) {
  199. return qemu_xkeymap_mapping_table(
  200. info.info.x11.display, maplen);
  201. }
  202. #endif
  203. g_warning("Unsupported SDL video driver / platform.\n"
  204. "Assuming Linux KBD scancodes, but probably wrong.\n"
  205. "Please report to qemu-devel@nongnu.org\n"
  206. "including the following information:\n"
  207. "\n"
  208. " - Operating system\n"
  209. " - SDL video driver\n");
  210. *maplen = qemu_input_map_xorgkbd_to_qcode_len;
  211. return qemu_input_map_xorgkbd_to_qcode;
  212. #endif
  213. }
  214. static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
  215. {
  216. if (!keycode_map) {
  217. return 0;
  218. }
  219. if (ev->keysym.scancode > keycode_maplen) {
  220. return 0;
  221. }
  222. return keycode_map[ev->keysym.scancode];
  223. }
  224. static void reset_keys(void)
  225. {
  226. int i;
  227. for(i = 0; i < 256; i++) {
  228. if (modifiers_state[i]) {
  229. qemu_input_event_send_key_number(dcl->con, i, false);
  230. modifiers_state[i] = 0;
  231. }
  232. }
  233. }
  234. static void sdl_process_key(SDL_KeyboardEvent *ev)
  235. {
  236. int keycode;
  237. if (ev->keysym.sym == SDLK_PAUSE) {
  238. /* specific case */
  239. qemu_input_event_send_key_qcode(dcl->con, Q_KEY_CODE_PAUSE,
  240. ev->type == SDL_KEYDOWN);
  241. return;
  242. }
  243. if (kbd_layout) {
  244. keycode = sdl_keyevent_to_keycode_generic(ev);
  245. } else {
  246. keycode = sdl_keyevent_to_keycode(ev);
  247. }
  248. switch(keycode) {
  249. case 0x00:
  250. /* sent when leaving window: reset the modifiers state */
  251. reset_keys();
  252. return;
  253. case 0x2a: /* Left Shift */
  254. case 0x36: /* Right Shift */
  255. case 0x1d: /* Left CTRL */
  256. case 0x9d: /* Right CTRL */
  257. case 0x38: /* Left ALT */
  258. case 0xb8: /* Right ALT */
  259. if (ev->type == SDL_KEYUP)
  260. modifiers_state[keycode] = 0;
  261. else
  262. modifiers_state[keycode] = 1;
  263. break;
  264. #define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
  265. #if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
  266. /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
  267. case 0x45: /* num lock */
  268. case 0x3a: /* caps lock */
  269. /* SDL does not send the key up event, so we generate it */
  270. qemu_input_event_send_key_number(dcl->con, keycode, true);
  271. qemu_input_event_send_key_number(dcl->con, keycode, false);
  272. return;
  273. #endif
  274. }
  275. /* now send the key code */
  276. qemu_input_event_send_key_number(dcl->con, keycode,
  277. ev->type == SDL_KEYDOWN);
  278. }
  279. static void sdl_update_caption(void)
  280. {
  281. char win_title[1024];
  282. char icon_title[1024];
  283. const char *status = "";
  284. if (!runstate_is_running())
  285. status = " [Stopped]";
  286. else if (gui_grab) {
  287. if (alt_grab)
  288. status = " - Press Ctrl-Alt-Shift to exit mouse grab";
  289. else if (ctrl_grab)
  290. status = " - Press Right-Ctrl to exit mouse grab";
  291. else
  292. status = " - Press Ctrl-Alt to exit mouse grab";
  293. }
  294. if (qemu_name) {
  295. snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
  296. snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
  297. } else {
  298. snprintf(win_title, sizeof(win_title), "QEMU%s", status);
  299. snprintf(icon_title, sizeof(icon_title), "QEMU");
  300. }
  301. SDL_WM_SetCaption(win_title, icon_title);
  302. }
  303. static void sdl_hide_cursor(void)
  304. {
  305. if (!cursor_hide)
  306. return;
  307. if (qemu_input_is_absolute()) {
  308. SDL_ShowCursor(1);
  309. SDL_SetCursor(sdl_cursor_hidden);
  310. } else {
  311. SDL_ShowCursor(0);
  312. }
  313. }
  314. static void sdl_show_cursor(void)
  315. {
  316. if (!cursor_hide)
  317. return;
  318. if (!qemu_input_is_absolute() || !qemu_console_is_graphic(NULL)) {
  319. SDL_ShowCursor(1);
  320. if (guest_cursor &&
  321. (gui_grab || qemu_input_is_absolute() || absolute_enabled))
  322. SDL_SetCursor(guest_sprite);
  323. else
  324. SDL_SetCursor(sdl_cursor_normal);
  325. }
  326. }
  327. static void sdl_grab_start(void)
  328. {
  329. /*
  330. * If the application is not active, do not try to enter grab state. This
  331. * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
  332. * application (SDL bug).
  333. */
  334. if (!(SDL_GetAppState() & SDL_APPINPUTFOCUS)) {
  335. return;
  336. }
  337. if (guest_cursor) {
  338. SDL_SetCursor(guest_sprite);
  339. if (!qemu_input_is_absolute() && !absolute_enabled) {
  340. SDL_WarpMouse(guest_x, guest_y);
  341. }
  342. } else
  343. sdl_hide_cursor();
  344. SDL_WM_GrabInput(SDL_GRAB_ON);
  345. gui_grab = 1;
  346. sdl_update_caption();
  347. }
  348. static void sdl_grab_end(void)
  349. {
  350. SDL_WM_GrabInput(SDL_GRAB_OFF);
  351. gui_grab = 0;
  352. sdl_show_cursor();
  353. sdl_update_caption();
  354. }
  355. static void absolute_mouse_grab(void)
  356. {
  357. int mouse_x, mouse_y;
  358. SDL_GetMouseState(&mouse_x, &mouse_y);
  359. if (mouse_x > 0 && mouse_x < real_screen->w - 1 &&
  360. mouse_y > 0 && mouse_y < real_screen->h - 1) {
  361. sdl_grab_start();
  362. }
  363. }
  364. static void sdl_mouse_mode_change(Notifier *notify, void *data)
  365. {
  366. if (qemu_input_is_absolute()) {
  367. if (!absolute_enabled) {
  368. absolute_enabled = 1;
  369. if (qemu_console_is_graphic(NULL)) {
  370. absolute_mouse_grab();
  371. }
  372. }
  373. } else if (absolute_enabled) {
  374. if (!gui_fullscreen) {
  375. sdl_grab_end();
  376. }
  377. absolute_enabled = 0;
  378. }
  379. }
  380. static void sdl_send_mouse_event(int dx, int dy, int x, int y, int state)
  381. {
  382. static uint32_t bmap[INPUT_BUTTON__MAX] = {
  383. [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
  384. [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
  385. [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
  386. [INPUT_BUTTON_WHEEL_UP] = SDL_BUTTON(SDL_BUTTON_WHEELUP),
  387. [INPUT_BUTTON_WHEEL_DOWN] = SDL_BUTTON(SDL_BUTTON_WHEELDOWN),
  388. };
  389. static uint32_t prev_state;
  390. if (prev_state != state) {
  391. qemu_input_update_buttons(dcl->con, bmap, prev_state, state);
  392. prev_state = state;
  393. }
  394. if (qemu_input_is_absolute()) {
  395. qemu_input_queue_abs(dcl->con, INPUT_AXIS_X, x,
  396. 0, real_screen->w);
  397. qemu_input_queue_abs(dcl->con, INPUT_AXIS_Y, y,
  398. 0, real_screen->h);
  399. } else {
  400. if (guest_cursor) {
  401. x -= guest_x;
  402. y -= guest_y;
  403. guest_x += x;
  404. guest_y += y;
  405. dx = x;
  406. dy = y;
  407. }
  408. qemu_input_queue_rel(dcl->con, INPUT_AXIS_X, dx);
  409. qemu_input_queue_rel(dcl->con, INPUT_AXIS_Y, dy);
  410. }
  411. qemu_input_event_sync();
  412. }
  413. static void sdl_scale(int width, int height)
  414. {
  415. int bpp = real_screen->format->BitsPerPixel;
  416. #ifdef DEBUG_SDL
  417. printf("SDL: Scaling to %dx%d bpp %d\n", width, height, bpp);
  418. #endif
  419. if (bpp != 16 && bpp != 32) {
  420. bpp = 32;
  421. }
  422. do_sdl_resize(width, height, bpp);
  423. scaling_active = 1;
  424. }
  425. static void toggle_full_screen(void)
  426. {
  427. int width = surface_width(surface);
  428. int height = surface_height(surface);
  429. int bpp = surface_bits_per_pixel(surface);
  430. gui_fullscreen = !gui_fullscreen;
  431. if (gui_fullscreen) {
  432. gui_saved_width = real_screen->w;
  433. gui_saved_height = real_screen->h;
  434. gui_saved_scaling = scaling_active;
  435. do_sdl_resize(width, height, bpp);
  436. scaling_active = 0;
  437. gui_saved_grab = gui_grab;
  438. sdl_grab_start();
  439. } else {
  440. if (gui_saved_scaling) {
  441. sdl_scale(gui_saved_width, gui_saved_height);
  442. } else {
  443. do_sdl_resize(width, height, 0);
  444. }
  445. if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
  446. sdl_grab_end();
  447. }
  448. }
  449. graphic_hw_invalidate(NULL);
  450. graphic_hw_update(NULL);
  451. }
  452. static void handle_keydown(SDL_Event *ev)
  453. {
  454. int mod_state;
  455. int keycode;
  456. if (alt_grab) {
  457. mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
  458. (gui_grab_code | KMOD_LSHIFT);
  459. } else if (ctrl_grab) {
  460. mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
  461. } else {
  462. mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
  463. }
  464. gui_key_modifier_pressed = mod_state;
  465. if (gui_key_modifier_pressed) {
  466. keycode = sdl_keyevent_to_keycode(&ev->key);
  467. switch (keycode) {
  468. case 0x21: /* 'f' key on US keyboard */
  469. toggle_full_screen();
  470. gui_keysym = 1;
  471. break;
  472. case 0x16: /* 'u' key on US keyboard */
  473. if (scaling_active) {
  474. scaling_active = 0;
  475. sdl_switch(dcl, NULL);
  476. graphic_hw_invalidate(NULL);
  477. graphic_hw_update(NULL);
  478. }
  479. gui_keysym = 1;
  480. break;
  481. case 0x02 ... 0x0a: /* '1' to '9' keys */
  482. /* Reset the modifiers sent to the current console */
  483. reset_keys();
  484. console_select(keycode - 0x02);
  485. gui_keysym = 1;
  486. if (gui_fullscreen) {
  487. break;
  488. }
  489. if (!qemu_console_is_graphic(NULL)) {
  490. /* release grab if going to a text console */
  491. if (gui_grab) {
  492. sdl_grab_end();
  493. } else if (absolute_enabled) {
  494. sdl_show_cursor();
  495. }
  496. } else if (absolute_enabled) {
  497. sdl_hide_cursor();
  498. absolute_mouse_grab();
  499. }
  500. break;
  501. case 0x1b: /* '+' */
  502. case 0x35: /* '-' */
  503. if (!gui_fullscreen) {
  504. int width = MAX(real_screen->w + (keycode == 0x1b ? 50 : -50),
  505. 160);
  506. int height = (surface_height(surface) * width) /
  507. surface_width(surface);
  508. sdl_scale(width, height);
  509. graphic_hw_invalidate(NULL);
  510. graphic_hw_update(NULL);
  511. gui_keysym = 1;
  512. }
  513. default:
  514. break;
  515. }
  516. } else if (!qemu_console_is_graphic(NULL)) {
  517. int keysym = 0;
  518. if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
  519. switch (ev->key.keysym.sym) {
  520. case SDLK_UP:
  521. keysym = QEMU_KEY_CTRL_UP;
  522. break;
  523. case SDLK_DOWN:
  524. keysym = QEMU_KEY_CTRL_DOWN;
  525. break;
  526. case SDLK_LEFT:
  527. keysym = QEMU_KEY_CTRL_LEFT;
  528. break;
  529. case SDLK_RIGHT:
  530. keysym = QEMU_KEY_CTRL_RIGHT;
  531. break;
  532. case SDLK_HOME:
  533. keysym = QEMU_KEY_CTRL_HOME;
  534. break;
  535. case SDLK_END:
  536. keysym = QEMU_KEY_CTRL_END;
  537. break;
  538. case SDLK_PAGEUP:
  539. keysym = QEMU_KEY_CTRL_PAGEUP;
  540. break;
  541. case SDLK_PAGEDOWN:
  542. keysym = QEMU_KEY_CTRL_PAGEDOWN;
  543. break;
  544. default:
  545. break;
  546. }
  547. } else {
  548. switch (ev->key.keysym.sym) {
  549. case SDLK_UP:
  550. keysym = QEMU_KEY_UP;
  551. break;
  552. case SDLK_DOWN:
  553. keysym = QEMU_KEY_DOWN;
  554. break;
  555. case SDLK_LEFT:
  556. keysym = QEMU_KEY_LEFT;
  557. break;
  558. case SDLK_RIGHT:
  559. keysym = QEMU_KEY_RIGHT;
  560. break;
  561. case SDLK_HOME:
  562. keysym = QEMU_KEY_HOME;
  563. break;
  564. case SDLK_END:
  565. keysym = QEMU_KEY_END;
  566. break;
  567. case SDLK_PAGEUP:
  568. keysym = QEMU_KEY_PAGEUP;
  569. break;
  570. case SDLK_PAGEDOWN:
  571. keysym = QEMU_KEY_PAGEDOWN;
  572. break;
  573. case SDLK_BACKSPACE:
  574. keysym = QEMU_KEY_BACKSPACE;
  575. break;
  576. case SDLK_DELETE:
  577. keysym = QEMU_KEY_DELETE;
  578. break;
  579. default:
  580. break;
  581. }
  582. }
  583. if (keysym) {
  584. kbd_put_keysym(keysym);
  585. } else if (ev->key.keysym.unicode != 0) {
  586. kbd_put_keysym(ev->key.keysym.unicode);
  587. }
  588. }
  589. if (qemu_console_is_graphic(NULL) && !gui_keysym) {
  590. sdl_process_key(&ev->key);
  591. }
  592. }
  593. static void handle_keyup(SDL_Event *ev)
  594. {
  595. int mod_state;
  596. if (!alt_grab) {
  597. mod_state = (ev->key.keysym.mod & gui_grab_code);
  598. } else {
  599. mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
  600. }
  601. if (!mod_state && gui_key_modifier_pressed) {
  602. gui_key_modifier_pressed = 0;
  603. if (gui_keysym == 0) {
  604. /* exit/enter grab if pressing Ctrl-Alt */
  605. if (!gui_grab) {
  606. if (qemu_console_is_graphic(NULL)) {
  607. sdl_grab_start();
  608. }
  609. } else if (!gui_fullscreen) {
  610. sdl_grab_end();
  611. }
  612. /* SDL does not send back all the modifiers key, so we must
  613. * correct it. */
  614. reset_keys();
  615. return;
  616. }
  617. gui_keysym = 0;
  618. }
  619. if (qemu_console_is_graphic(NULL) && !gui_keysym) {
  620. sdl_process_key(&ev->key);
  621. }
  622. }
  623. static void handle_mousemotion(SDL_Event *ev)
  624. {
  625. int max_x, max_y;
  626. if (qemu_console_is_graphic(NULL) &&
  627. (qemu_input_is_absolute() || absolute_enabled)) {
  628. max_x = real_screen->w - 1;
  629. max_y = real_screen->h - 1;
  630. if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
  631. ev->motion.x == max_x || ev->motion.y == max_y)) {
  632. sdl_grab_end();
  633. }
  634. if (!gui_grab &&
  635. (ev->motion.x > 0 && ev->motion.x < max_x &&
  636. ev->motion.y > 0 && ev->motion.y < max_y)) {
  637. sdl_grab_start();
  638. }
  639. }
  640. if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
  641. sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel,
  642. ev->motion.x, ev->motion.y, ev->motion.state);
  643. }
  644. }
  645. static void handle_mousebutton(SDL_Event *ev)
  646. {
  647. int buttonstate = SDL_GetMouseState(NULL, NULL);
  648. SDL_MouseButtonEvent *bev;
  649. if (!qemu_console_is_graphic(NULL)) {
  650. return;
  651. }
  652. bev = &ev->button;
  653. if (!gui_grab && !qemu_input_is_absolute()) {
  654. if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
  655. /* start grabbing all events */
  656. sdl_grab_start();
  657. }
  658. } else {
  659. if (ev->type == SDL_MOUSEBUTTONDOWN) {
  660. buttonstate |= SDL_BUTTON(bev->button);
  661. } else {
  662. buttonstate &= ~SDL_BUTTON(bev->button);
  663. }
  664. sdl_send_mouse_event(0, 0, bev->x, bev->y, buttonstate);
  665. }
  666. }
  667. static void handle_activation(SDL_Event *ev)
  668. {
  669. #ifdef _WIN32
  670. /* Disable grab if the window no longer has the focus
  671. * (Windows-only workaround) */
  672. if (gui_grab && ev->active.state == SDL_APPINPUTFOCUS &&
  673. !ev->active.gain && !gui_fullscreen) {
  674. sdl_grab_end();
  675. }
  676. #endif
  677. if (!gui_grab && ev->active.gain && qemu_console_is_graphic(NULL) &&
  678. (qemu_input_is_absolute() || absolute_enabled)) {
  679. absolute_mouse_grab();
  680. }
  681. if (ev->active.state & SDL_APPACTIVE) {
  682. if (ev->active.gain) {
  683. /* Back to default interval */
  684. update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
  685. } else {
  686. /* Sleeping interval. Not using the long default here as
  687. * sdl_refresh does not only update the guest screen, but
  688. * also checks for gui events. */
  689. update_displaychangelistener(dcl, 500);
  690. }
  691. }
  692. }
  693. static void sdl_refresh(DisplayChangeListener *dcl)
  694. {
  695. SDL_Event ev1, *ev = &ev1;
  696. int idle = 1;
  697. if (last_vm_running != runstate_is_running()) {
  698. last_vm_running = runstate_is_running();
  699. sdl_update_caption();
  700. }
  701. graphic_hw_update(NULL);
  702. SDL_EnableUNICODE(!qemu_console_is_graphic(NULL));
  703. while (SDL_PollEvent(ev)) {
  704. switch (ev->type) {
  705. case SDL_VIDEOEXPOSE:
  706. sdl_update(dcl, 0, 0, real_screen->w, real_screen->h);
  707. break;
  708. case SDL_KEYDOWN:
  709. idle = 0;
  710. handle_keydown(ev);
  711. break;
  712. case SDL_KEYUP:
  713. idle = 0;
  714. handle_keyup(ev);
  715. break;
  716. case SDL_QUIT:
  717. if (!no_quit) {
  718. no_shutdown = 0;
  719. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
  720. }
  721. break;
  722. case SDL_MOUSEMOTION:
  723. idle = 0;
  724. handle_mousemotion(ev);
  725. break;
  726. case SDL_MOUSEBUTTONDOWN:
  727. case SDL_MOUSEBUTTONUP:
  728. idle = 0;
  729. handle_mousebutton(ev);
  730. break;
  731. case SDL_ACTIVEEVENT:
  732. handle_activation(ev);
  733. break;
  734. case SDL_VIDEORESIZE:
  735. sdl_scale(ev->resize.w, ev->resize.h);
  736. graphic_hw_invalidate(NULL);
  737. graphic_hw_update(NULL);
  738. break;
  739. default:
  740. break;
  741. }
  742. }
  743. if (idle) {
  744. if (idle_counter < SDL_MAX_IDLE_COUNT) {
  745. idle_counter++;
  746. if (idle_counter >= SDL_MAX_IDLE_COUNT) {
  747. dcl->update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
  748. }
  749. }
  750. } else {
  751. idle_counter = 0;
  752. dcl->update_interval = SDL_REFRESH_INTERVAL_BUSY;
  753. }
  754. }
  755. static void sdl_mouse_warp(DisplayChangeListener *dcl,
  756. int x, int y, int on)
  757. {
  758. if (on) {
  759. if (!guest_cursor)
  760. sdl_show_cursor();
  761. if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
  762. SDL_SetCursor(guest_sprite);
  763. if (!qemu_input_is_absolute() && !absolute_enabled) {
  764. SDL_WarpMouse(x, y);
  765. }
  766. }
  767. } else if (gui_grab)
  768. sdl_hide_cursor();
  769. guest_cursor = on;
  770. guest_x = x, guest_y = y;
  771. }
  772. static void sdl_mouse_define(DisplayChangeListener *dcl,
  773. QEMUCursor *c)
  774. {
  775. uint8_t *image, *mask;
  776. int bpl;
  777. if (guest_sprite)
  778. SDL_FreeCursor(guest_sprite);
  779. bpl = cursor_get_mono_bpl(c);
  780. image = g_malloc0(bpl * c->height);
  781. mask = g_malloc0(bpl * c->height);
  782. cursor_get_mono_image(c, 0x000000, image);
  783. cursor_get_mono_mask(c, 0, mask);
  784. guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
  785. c->hot_x, c->hot_y);
  786. g_free(image);
  787. g_free(mask);
  788. if (guest_cursor &&
  789. (gui_grab || qemu_input_is_absolute() || absolute_enabled))
  790. SDL_SetCursor(guest_sprite);
  791. }
  792. static void sdl_cleanup(void)
  793. {
  794. if (guest_sprite)
  795. SDL_FreeCursor(guest_sprite);
  796. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  797. }
  798. static const DisplayChangeListenerOps dcl_ops = {
  799. .dpy_name = "sdl",
  800. .dpy_gfx_update = sdl_update,
  801. .dpy_gfx_switch = sdl_switch,
  802. .dpy_gfx_check_format = sdl_check_format,
  803. .dpy_refresh = sdl_refresh,
  804. .dpy_mouse_set = sdl_mouse_warp,
  805. .dpy_cursor_define = sdl_mouse_define,
  806. };
  807. void sdl_display_early_init(int opengl)
  808. {
  809. if (opengl == 1 /* on */) {
  810. fprintf(stderr,
  811. "SDL1 display code has no opengl support.\n"
  812. "Please recompile qemu with SDL2, using\n"
  813. "./configure --enable-sdl --with-sdlabi=2.0\n");
  814. }
  815. }
  816. void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
  817. {
  818. int flags;
  819. uint8_t data = 0;
  820. const SDL_VideoInfo *vi;
  821. SDL_SysWMinfo info;
  822. char *filename;
  823. #if defined(__APPLE__)
  824. /* always use generic keymaps */
  825. if (!keyboard_layout)
  826. keyboard_layout = "en-us";
  827. #endif
  828. if(keyboard_layout) {
  829. kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
  830. if (!kbd_layout)
  831. exit(1);
  832. }
  833. if (no_frame)
  834. gui_noframe = 1;
  835. if (!full_screen) {
  836. setenv("SDL_VIDEO_ALLOW_SCREENSAVER", "1", 0);
  837. }
  838. #ifdef __linux__
  839. /* on Linux, SDL may use fbcon|directfb|svgalib when run without
  840. * accessible $DISPLAY to open X11 window. This is often the case
  841. * when qemu is run using sudo. But in this case, and when actually
  842. * run in X11 environment, SDL fights with X11 for the video card,
  843. * making current display unavailable, often until reboot.
  844. * So make x11 the default SDL video driver if this variable is unset.
  845. * This is a bit hackish but saves us from bigger problem.
  846. * Maybe it's a good idea to fix this in SDL instead.
  847. */
  848. setenv("SDL_VIDEODRIVER", "x11", 0);
  849. #endif
  850. /* Enable normal up/down events for Caps-Lock and Num-Lock keys.
  851. * This requires SDL >= 1.2.14. */
  852. setenv("SDL_DISABLE_LOCK_KEYS", "1", 1);
  853. flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
  854. if (SDL_Init (flags)) {
  855. fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
  856. SDL_GetError());
  857. exit(1);
  858. }
  859. vi = SDL_GetVideoInfo();
  860. host_format = *(vi->vfmt);
  861. keycode_map = sdl_get_keymap(&keycode_maplen);
  862. /* Load a 32x32x4 image. White pixels are transparent. */
  863. filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
  864. if (filename) {
  865. SDL_Surface *image = SDL_LoadBMP(filename);
  866. if (image) {
  867. uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
  868. SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
  869. SDL_WM_SetIcon(image, NULL);
  870. }
  871. g_free(filename);
  872. }
  873. if (full_screen) {
  874. gui_fullscreen = 1;
  875. sdl_grab_start();
  876. }
  877. dcl = g_new0(DisplayChangeListener, 1);
  878. dcl->ops = &dcl_ops;
  879. register_displaychangelistener(dcl);
  880. mouse_mode_notifier.notify = sdl_mouse_mode_change;
  881. qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
  882. sdl_update_caption();
  883. SDL_EnableKeyRepeat(250, 50);
  884. gui_grab = 0;
  885. sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
  886. sdl_cursor_normal = SDL_GetCursor();
  887. memset(&info, 0, sizeof(info));
  888. SDL_VERSION(&info.version);
  889. if (SDL_GetWMInfo(&info)) {
  890. int i;
  891. for (i = 0; ; i++) {
  892. /* All consoles share the same window */
  893. QemuConsole *con = qemu_console_lookup_by_index(i);
  894. if (con) {
  895. #if defined(SDL_VIDEO_DRIVER_X11)
  896. qemu_console_set_window_id(con, info.info.x11.wmwindow);
  897. #elif defined(SDL_VIDEO_DRIVER_NANOX) || \
  898. defined(SDL_VIDEO_DRIVER_WINDIB) || defined(SDL_VIDEO_DRIVER_DDRAW) || \
  899. defined(SDL_VIDEO_DRIVER_GAPI) || \
  900. defined(SDL_VIDEO_DRIVER_RISCOS)
  901. qemu_console_set_window_id(con, (int) (uintptr_t) info.window);
  902. #else
  903. qemu_console_set_window_id(con, info.data);
  904. #endif
  905. } else {
  906. break;
  907. }
  908. }
  909. }
  910. atexit(sdl_cleanup);
  911. }