2
0

sdl.c 30 KB

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