sdl.c 28 KB

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