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