sdl.c 28 KB

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