sdl.c 30 KB

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