sdl2.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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. /* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
  25. #include "qemu/osdep.h"
  26. #include "qemu/module.h"
  27. #include "qemu/cutils.h"
  28. #include "ui/console.h"
  29. #include "ui/input.h"
  30. #include "ui/sdl2.h"
  31. #include "sysemu/runstate.h"
  32. #include "sysemu/runstate-action.h"
  33. #include "sysemu/sysemu.h"
  34. #include "ui/win32-kbd-hook.h"
  35. #include "qemu/log.h"
  36. static int sdl2_num_outputs;
  37. static struct sdl2_console *sdl2_console;
  38. static SDL_Surface *guest_sprite_surface;
  39. static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
  40. static bool alt_grab;
  41. static bool ctrl_grab;
  42. static int gui_saved_grab;
  43. static int gui_fullscreen;
  44. static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
  45. static SDL_Cursor *sdl_cursor_normal;
  46. static SDL_Cursor *sdl_cursor_hidden;
  47. static int absolute_enabled;
  48. static int guest_cursor;
  49. static int guest_x, guest_y;
  50. static SDL_Cursor *guest_sprite;
  51. static Notifier mouse_mode_notifier;
  52. #define SDL2_REFRESH_INTERVAL_BUSY 10
  53. #define SDL2_MAX_IDLE_COUNT (2 * GUI_REFRESH_INTERVAL_DEFAULT \
  54. / SDL2_REFRESH_INTERVAL_BUSY + 1)
  55. /* introduced in SDL 2.0.10 */
  56. #ifndef SDL_HINT_RENDER_BATCHING
  57. #define SDL_HINT_RENDER_BATCHING "SDL_RENDER_BATCHING"
  58. #endif
  59. static void sdl_update_caption(struct sdl2_console *scon);
  60. static struct sdl2_console *get_scon_from_window(uint32_t window_id)
  61. {
  62. int i;
  63. for (i = 0; i < sdl2_num_outputs; i++) {
  64. if (sdl2_console[i].real_window == SDL_GetWindowFromID(window_id)) {
  65. return &sdl2_console[i];
  66. }
  67. }
  68. return NULL;
  69. }
  70. void sdl2_window_create(struct sdl2_console *scon)
  71. {
  72. int flags = 0;
  73. if (!scon->surface) {
  74. return;
  75. }
  76. assert(!scon->real_window);
  77. if (gui_fullscreen) {
  78. flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
  79. } else {
  80. flags |= SDL_WINDOW_RESIZABLE;
  81. }
  82. if (scon->hidden) {
  83. flags |= SDL_WINDOW_HIDDEN;
  84. }
  85. #ifdef CONFIG_OPENGL
  86. if (scon->opengl) {
  87. flags |= SDL_WINDOW_OPENGL;
  88. }
  89. #endif
  90. scon->real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
  91. SDL_WINDOWPOS_UNDEFINED,
  92. surface_width(scon->surface),
  93. surface_height(scon->surface),
  94. flags);
  95. if (scon->opengl) {
  96. const char *driver = "opengl";
  97. if (scon->opts->gl == DISPLAYGL_MODE_ES) {
  98. driver = "opengles2";
  99. }
  100. SDL_SetHint(SDL_HINT_RENDER_DRIVER, driver);
  101. SDL_SetHint(SDL_HINT_RENDER_BATCHING, "1");
  102. scon->winctx = SDL_GL_CreateContext(scon->real_window);
  103. } else {
  104. /* The SDL renderer is only used by sdl2-2D, when OpenGL is disabled */
  105. scon->real_renderer = SDL_CreateRenderer(scon->real_window, -1, 0);
  106. }
  107. sdl_update_caption(scon);
  108. }
  109. void sdl2_window_destroy(struct sdl2_console *scon)
  110. {
  111. if (!scon->real_window) {
  112. return;
  113. }
  114. if (scon->winctx) {
  115. SDL_GL_DeleteContext(scon->winctx);
  116. scon->winctx = NULL;
  117. }
  118. if (scon->real_renderer) {
  119. SDL_DestroyRenderer(scon->real_renderer);
  120. scon->real_renderer = NULL;
  121. }
  122. SDL_DestroyWindow(scon->real_window);
  123. scon->real_window = NULL;
  124. }
  125. void sdl2_window_resize(struct sdl2_console *scon)
  126. {
  127. if (!scon->real_window) {
  128. return;
  129. }
  130. SDL_SetWindowSize(scon->real_window,
  131. surface_width(scon->surface),
  132. surface_height(scon->surface));
  133. }
  134. static void sdl2_redraw(struct sdl2_console *scon)
  135. {
  136. if (scon->opengl) {
  137. #ifdef CONFIG_OPENGL
  138. sdl2_gl_redraw(scon);
  139. #endif
  140. } else {
  141. sdl2_2d_redraw(scon);
  142. }
  143. }
  144. static void sdl_update_caption(struct sdl2_console *scon)
  145. {
  146. char win_title[1024];
  147. char icon_title[1024];
  148. const char *status = "";
  149. if (!runstate_is_running()) {
  150. status = " [Stopped]";
  151. } else if (gui_grab) {
  152. if (alt_grab) {
  153. status = " - Press Ctrl-Alt-Shift-G to exit grab";
  154. } else if (ctrl_grab) {
  155. status = " - Press Right-Ctrl-G to exit grab";
  156. } else {
  157. status = " - Press Ctrl-Alt-G to exit grab";
  158. }
  159. }
  160. if (qemu_name) {
  161. snprintf(win_title, sizeof(win_title), "QEMU (%s-%d)%s", qemu_name,
  162. scon->idx, status);
  163. snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
  164. } else {
  165. snprintf(win_title, sizeof(win_title), "QEMU%s", status);
  166. snprintf(icon_title, sizeof(icon_title), "QEMU");
  167. }
  168. if (scon->real_window) {
  169. SDL_SetWindowTitle(scon->real_window, win_title);
  170. }
  171. }
  172. static void sdl_hide_cursor(struct sdl2_console *scon)
  173. {
  174. if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
  175. return;
  176. }
  177. SDL_ShowCursor(SDL_DISABLE);
  178. SDL_SetCursor(sdl_cursor_hidden);
  179. if (!qemu_input_is_absolute()) {
  180. SDL_SetRelativeMouseMode(SDL_TRUE);
  181. }
  182. }
  183. static void sdl_show_cursor(struct sdl2_console *scon)
  184. {
  185. if (scon->opts->has_show_cursor && scon->opts->show_cursor) {
  186. return;
  187. }
  188. if (!qemu_input_is_absolute()) {
  189. SDL_SetRelativeMouseMode(SDL_FALSE);
  190. }
  191. if (guest_cursor &&
  192. (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
  193. SDL_SetCursor(guest_sprite);
  194. } else {
  195. SDL_SetCursor(sdl_cursor_normal);
  196. }
  197. SDL_ShowCursor(SDL_ENABLE);
  198. }
  199. static void sdl_grab_start(struct sdl2_console *scon)
  200. {
  201. QemuConsole *con = scon ? scon->dcl.con : NULL;
  202. if (!con || !qemu_console_is_graphic(con)) {
  203. return;
  204. }
  205. /*
  206. * If the application is not active, do not try to enter grab state. This
  207. * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
  208. * application (SDL bug).
  209. */
  210. if (!(SDL_GetWindowFlags(scon->real_window) & SDL_WINDOW_INPUT_FOCUS)) {
  211. return;
  212. }
  213. if (guest_cursor) {
  214. SDL_SetCursor(guest_sprite);
  215. if (!qemu_input_is_absolute() && !absolute_enabled) {
  216. SDL_WarpMouseInWindow(scon->real_window, guest_x, guest_y);
  217. }
  218. } else {
  219. sdl_hide_cursor(scon);
  220. }
  221. SDL_SetWindowGrab(scon->real_window, SDL_TRUE);
  222. gui_grab = 1;
  223. win32_kbd_set_grab(true);
  224. sdl_update_caption(scon);
  225. }
  226. static void sdl_grab_end(struct sdl2_console *scon)
  227. {
  228. SDL_SetWindowGrab(scon->real_window, SDL_FALSE);
  229. gui_grab = 0;
  230. win32_kbd_set_grab(false);
  231. sdl_show_cursor(scon);
  232. sdl_update_caption(scon);
  233. }
  234. static void absolute_mouse_grab(struct sdl2_console *scon)
  235. {
  236. int mouse_x, mouse_y;
  237. int scr_w, scr_h;
  238. SDL_GetMouseState(&mouse_x, &mouse_y);
  239. SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
  240. if (mouse_x > 0 && mouse_x < scr_w - 1 &&
  241. mouse_y > 0 && mouse_y < scr_h - 1) {
  242. sdl_grab_start(scon);
  243. }
  244. }
  245. static void sdl_mouse_mode_change(Notifier *notify, void *data)
  246. {
  247. if (qemu_input_is_absolute()) {
  248. if (!absolute_enabled) {
  249. absolute_enabled = 1;
  250. SDL_SetRelativeMouseMode(SDL_FALSE);
  251. absolute_mouse_grab(&sdl2_console[0]);
  252. }
  253. } else if (absolute_enabled) {
  254. if (!gui_fullscreen) {
  255. sdl_grab_end(&sdl2_console[0]);
  256. }
  257. absolute_enabled = 0;
  258. }
  259. }
  260. static void sdl_send_mouse_event(struct sdl2_console *scon, int dx, int dy,
  261. int x, int y, int state)
  262. {
  263. static uint32_t bmap[INPUT_BUTTON__MAX] = {
  264. [INPUT_BUTTON_LEFT] = SDL_BUTTON(SDL_BUTTON_LEFT),
  265. [INPUT_BUTTON_MIDDLE] = SDL_BUTTON(SDL_BUTTON_MIDDLE),
  266. [INPUT_BUTTON_RIGHT] = SDL_BUTTON(SDL_BUTTON_RIGHT),
  267. [INPUT_BUTTON_SIDE] = SDL_BUTTON(SDL_BUTTON_X1),
  268. [INPUT_BUTTON_EXTRA] = SDL_BUTTON(SDL_BUTTON_X2)
  269. };
  270. static uint32_t prev_state;
  271. if (prev_state != state) {
  272. qemu_input_update_buttons(scon->dcl.con, bmap, prev_state, state);
  273. prev_state = state;
  274. }
  275. if (qemu_input_is_absolute()) {
  276. qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_X,
  277. x, 0, surface_width(scon->surface));
  278. qemu_input_queue_abs(scon->dcl.con, INPUT_AXIS_Y,
  279. y, 0, surface_height(scon->surface));
  280. } else {
  281. if (guest_cursor) {
  282. x -= guest_x;
  283. y -= guest_y;
  284. guest_x += x;
  285. guest_y += y;
  286. dx = x;
  287. dy = y;
  288. }
  289. qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_X, dx);
  290. qemu_input_queue_rel(scon->dcl.con, INPUT_AXIS_Y, dy);
  291. }
  292. qemu_input_event_sync();
  293. }
  294. static void toggle_full_screen(struct sdl2_console *scon)
  295. {
  296. gui_fullscreen = !gui_fullscreen;
  297. if (gui_fullscreen) {
  298. SDL_SetWindowFullscreen(scon->real_window,
  299. SDL_WINDOW_FULLSCREEN_DESKTOP);
  300. gui_saved_grab = gui_grab;
  301. sdl_grab_start(scon);
  302. } else {
  303. if (!gui_saved_grab) {
  304. sdl_grab_end(scon);
  305. }
  306. SDL_SetWindowFullscreen(scon->real_window, 0);
  307. }
  308. sdl2_redraw(scon);
  309. }
  310. static int get_mod_state(void)
  311. {
  312. SDL_Keymod mod = SDL_GetModState();
  313. if (alt_grab) {
  314. return (mod & (gui_grab_code | KMOD_LSHIFT)) ==
  315. (gui_grab_code | KMOD_LSHIFT);
  316. } else if (ctrl_grab) {
  317. return (mod & KMOD_RCTRL) == KMOD_RCTRL;
  318. } else {
  319. return (mod & gui_grab_code) == gui_grab_code;
  320. }
  321. }
  322. static void *sdl2_win32_get_hwnd(struct sdl2_console *scon)
  323. {
  324. #ifdef CONFIG_WIN32
  325. SDL_SysWMinfo info;
  326. SDL_VERSION(&info.version);
  327. if (SDL_GetWindowWMInfo(scon->real_window, &info)) {
  328. return info.info.win.window;
  329. }
  330. #endif
  331. return NULL;
  332. }
  333. static void handle_keydown(SDL_Event *ev)
  334. {
  335. int win;
  336. struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
  337. int gui_key_modifier_pressed = get_mod_state();
  338. int gui_keysym = 0;
  339. if (!scon) {
  340. return;
  341. }
  342. if (!scon->ignore_hotkeys && gui_key_modifier_pressed && !ev->key.repeat) {
  343. switch (ev->key.keysym.scancode) {
  344. case SDL_SCANCODE_2:
  345. case SDL_SCANCODE_3:
  346. case SDL_SCANCODE_4:
  347. case SDL_SCANCODE_5:
  348. case SDL_SCANCODE_6:
  349. case SDL_SCANCODE_7:
  350. case SDL_SCANCODE_8:
  351. case SDL_SCANCODE_9:
  352. if (gui_grab) {
  353. sdl_grab_end(scon);
  354. }
  355. win = ev->key.keysym.scancode - SDL_SCANCODE_1;
  356. if (win < sdl2_num_outputs) {
  357. sdl2_console[win].hidden = !sdl2_console[win].hidden;
  358. if (sdl2_console[win].real_window) {
  359. if (sdl2_console[win].hidden) {
  360. SDL_HideWindow(sdl2_console[win].real_window);
  361. } else {
  362. SDL_ShowWindow(sdl2_console[win].real_window);
  363. }
  364. }
  365. gui_keysym = 1;
  366. }
  367. break;
  368. case SDL_SCANCODE_F:
  369. toggle_full_screen(scon);
  370. gui_keysym = 1;
  371. break;
  372. case SDL_SCANCODE_G:
  373. gui_keysym = 1;
  374. if (!gui_grab) {
  375. sdl_grab_start(scon);
  376. } else if (!gui_fullscreen) {
  377. sdl_grab_end(scon);
  378. }
  379. break;
  380. case SDL_SCANCODE_U:
  381. sdl2_window_resize(scon);
  382. if (!scon->opengl) {
  383. /* re-create scon->texture */
  384. sdl2_2d_switch(&scon->dcl, scon->surface);
  385. }
  386. gui_keysym = 1;
  387. break;
  388. #if 0
  389. case SDL_SCANCODE_KP_PLUS:
  390. case SDL_SCANCODE_KP_MINUS:
  391. if (!gui_fullscreen) {
  392. int scr_w, scr_h;
  393. int width, height;
  394. SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
  395. width = MAX(scr_w + (ev->key.keysym.scancode ==
  396. SDL_SCANCODE_KP_PLUS ? 50 : -50),
  397. 160);
  398. height = (surface_height(scon->surface) * width) /
  399. surface_width(scon->surface);
  400. fprintf(stderr, "%s: scale to %dx%d\n",
  401. __func__, width, height);
  402. sdl_scale(scon, width, height);
  403. sdl2_redraw(scon);
  404. gui_keysym = 1;
  405. }
  406. #endif
  407. default:
  408. break;
  409. }
  410. }
  411. if (!gui_keysym) {
  412. sdl2_process_key(scon, &ev->key);
  413. }
  414. }
  415. static void handle_keyup(SDL_Event *ev)
  416. {
  417. struct sdl2_console *scon = get_scon_from_window(ev->key.windowID);
  418. if (!scon) {
  419. return;
  420. }
  421. scon->ignore_hotkeys = false;
  422. sdl2_process_key(scon, &ev->key);
  423. }
  424. static void handle_textinput(SDL_Event *ev)
  425. {
  426. struct sdl2_console *scon = get_scon_from_window(ev->text.windowID);
  427. QemuConsole *con = scon ? scon->dcl.con : NULL;
  428. if (!con) {
  429. return;
  430. }
  431. if (qemu_console_is_graphic(con)) {
  432. return;
  433. }
  434. kbd_put_string_console(con, ev->text.text, strlen(ev->text.text));
  435. }
  436. static void handle_mousemotion(SDL_Event *ev)
  437. {
  438. int max_x, max_y;
  439. struct sdl2_console *scon = get_scon_from_window(ev->motion.windowID);
  440. if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
  441. return;
  442. }
  443. if (qemu_input_is_absolute() || absolute_enabled) {
  444. int scr_w, scr_h;
  445. SDL_GetWindowSize(scon->real_window, &scr_w, &scr_h);
  446. max_x = scr_w - 1;
  447. max_y = scr_h - 1;
  448. if (gui_grab && !gui_fullscreen
  449. && (ev->motion.x == 0 || ev->motion.y == 0 ||
  450. ev->motion.x == max_x || ev->motion.y == max_y)) {
  451. sdl_grab_end(scon);
  452. }
  453. if (!gui_grab &&
  454. (ev->motion.x > 0 && ev->motion.x < max_x &&
  455. ev->motion.y > 0 && ev->motion.y < max_y)) {
  456. sdl_grab_start(scon);
  457. }
  458. }
  459. if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
  460. sdl_send_mouse_event(scon, ev->motion.xrel, ev->motion.yrel,
  461. ev->motion.x, ev->motion.y, ev->motion.state);
  462. }
  463. }
  464. static void handle_mousebutton(SDL_Event *ev)
  465. {
  466. int buttonstate = SDL_GetMouseState(NULL, NULL);
  467. SDL_MouseButtonEvent *bev;
  468. struct sdl2_console *scon = get_scon_from_window(ev->button.windowID);
  469. if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
  470. return;
  471. }
  472. bev = &ev->button;
  473. if (!gui_grab && !qemu_input_is_absolute()) {
  474. if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
  475. /* start grabbing all events */
  476. sdl_grab_start(scon);
  477. }
  478. } else {
  479. if (ev->type == SDL_MOUSEBUTTONDOWN) {
  480. buttonstate |= SDL_BUTTON(bev->button);
  481. } else {
  482. buttonstate &= ~SDL_BUTTON(bev->button);
  483. }
  484. sdl_send_mouse_event(scon, 0, 0, bev->x, bev->y, buttonstate);
  485. }
  486. }
  487. static void handle_mousewheel(SDL_Event *ev)
  488. {
  489. struct sdl2_console *scon = get_scon_from_window(ev->wheel.windowID);
  490. SDL_MouseWheelEvent *wev = &ev->wheel;
  491. InputButton btn;
  492. if (!scon || !qemu_console_is_graphic(scon->dcl.con)) {
  493. return;
  494. }
  495. if (wev->y > 0) {
  496. btn = INPUT_BUTTON_WHEEL_UP;
  497. } else if (wev->y < 0) {
  498. btn = INPUT_BUTTON_WHEEL_DOWN;
  499. } else if (wev->x < 0) {
  500. btn = INPUT_BUTTON_WHEEL_RIGHT;
  501. } else if (wev->x > 0) {
  502. btn = INPUT_BUTTON_WHEEL_LEFT;
  503. } else {
  504. return;
  505. }
  506. qemu_input_queue_btn(scon->dcl.con, btn, true);
  507. qemu_input_event_sync();
  508. qemu_input_queue_btn(scon->dcl.con, btn, false);
  509. qemu_input_event_sync();
  510. }
  511. static void handle_windowevent(SDL_Event *ev)
  512. {
  513. struct sdl2_console *scon = get_scon_from_window(ev->window.windowID);
  514. bool allow_close = true;
  515. if (!scon) {
  516. return;
  517. }
  518. switch (ev->window.event) {
  519. case SDL_WINDOWEVENT_RESIZED:
  520. {
  521. QemuUIInfo info;
  522. memset(&info, 0, sizeof(info));
  523. info.width = ev->window.data1;
  524. info.height = ev->window.data2;
  525. dpy_set_ui_info(scon->dcl.con, &info, true);
  526. }
  527. sdl2_redraw(scon);
  528. break;
  529. case SDL_WINDOWEVENT_EXPOSED:
  530. sdl2_redraw(scon);
  531. break;
  532. case SDL_WINDOWEVENT_FOCUS_GAINED:
  533. win32_kbd_set_grab(gui_grab);
  534. if (qemu_console_is_graphic(scon->dcl.con)) {
  535. win32_kbd_set_window(sdl2_win32_get_hwnd(scon));
  536. }
  537. /* fall through */
  538. case SDL_WINDOWEVENT_ENTER:
  539. if (!gui_grab && (qemu_input_is_absolute() || absolute_enabled)) {
  540. absolute_mouse_grab(scon);
  541. }
  542. /* If a new console window opened using a hotkey receives the
  543. * focus, SDL sends another KEYDOWN event to the new window,
  544. * closing the console window immediately after.
  545. *
  546. * Work around this by ignoring further hotkey events until a
  547. * key is released.
  548. */
  549. scon->ignore_hotkeys = get_mod_state();
  550. break;
  551. case SDL_WINDOWEVENT_FOCUS_LOST:
  552. if (qemu_console_is_graphic(scon->dcl.con)) {
  553. win32_kbd_set_window(NULL);
  554. }
  555. if (gui_grab && !gui_fullscreen) {
  556. sdl_grab_end(scon);
  557. }
  558. break;
  559. case SDL_WINDOWEVENT_RESTORED:
  560. update_displaychangelistener(&scon->dcl, GUI_REFRESH_INTERVAL_DEFAULT);
  561. break;
  562. case SDL_WINDOWEVENT_MINIMIZED:
  563. update_displaychangelistener(&scon->dcl, 500);
  564. break;
  565. case SDL_WINDOWEVENT_CLOSE:
  566. if (qemu_console_is_graphic(scon->dcl.con)) {
  567. if (scon->opts->has_window_close && !scon->opts->window_close) {
  568. allow_close = false;
  569. }
  570. if (allow_close) {
  571. shutdown_action = SHUTDOWN_ACTION_POWEROFF;
  572. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
  573. }
  574. } else {
  575. SDL_HideWindow(scon->real_window);
  576. scon->hidden = true;
  577. }
  578. break;
  579. case SDL_WINDOWEVENT_SHOWN:
  580. scon->hidden = false;
  581. break;
  582. case SDL_WINDOWEVENT_HIDDEN:
  583. scon->hidden = true;
  584. break;
  585. }
  586. }
  587. void sdl2_poll_events(struct sdl2_console *scon)
  588. {
  589. SDL_Event ev1, *ev = &ev1;
  590. bool allow_close = true;
  591. int idle = 1;
  592. if (scon->last_vm_running != runstate_is_running()) {
  593. scon->last_vm_running = runstate_is_running();
  594. sdl_update_caption(scon);
  595. }
  596. while (SDL_PollEvent(ev)) {
  597. switch (ev->type) {
  598. case SDL_KEYDOWN:
  599. idle = 0;
  600. handle_keydown(ev);
  601. break;
  602. case SDL_KEYUP:
  603. idle = 0;
  604. handle_keyup(ev);
  605. break;
  606. case SDL_TEXTINPUT:
  607. idle = 0;
  608. handle_textinput(ev);
  609. break;
  610. case SDL_QUIT:
  611. if (scon->opts->has_window_close && !scon->opts->window_close) {
  612. allow_close = false;
  613. }
  614. if (allow_close) {
  615. shutdown_action = SHUTDOWN_ACTION_POWEROFF;
  616. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_UI);
  617. }
  618. break;
  619. case SDL_MOUSEMOTION:
  620. idle = 0;
  621. handle_mousemotion(ev);
  622. break;
  623. case SDL_MOUSEBUTTONDOWN:
  624. case SDL_MOUSEBUTTONUP:
  625. idle = 0;
  626. handle_mousebutton(ev);
  627. break;
  628. case SDL_MOUSEWHEEL:
  629. idle = 0;
  630. handle_mousewheel(ev);
  631. break;
  632. case SDL_WINDOWEVENT:
  633. handle_windowevent(ev);
  634. break;
  635. default:
  636. break;
  637. }
  638. }
  639. if (idle) {
  640. if (scon->idle_counter < SDL2_MAX_IDLE_COUNT) {
  641. scon->idle_counter++;
  642. if (scon->idle_counter >= SDL2_MAX_IDLE_COUNT) {
  643. scon->dcl.update_interval = GUI_REFRESH_INTERVAL_DEFAULT;
  644. }
  645. }
  646. } else {
  647. scon->idle_counter = 0;
  648. scon->dcl.update_interval = SDL2_REFRESH_INTERVAL_BUSY;
  649. }
  650. }
  651. static void sdl_mouse_warp(DisplayChangeListener *dcl,
  652. int x, int y, int on)
  653. {
  654. struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
  655. if (!qemu_console_is_graphic(scon->dcl.con)) {
  656. return;
  657. }
  658. if (on) {
  659. if (!guest_cursor) {
  660. sdl_show_cursor(scon);
  661. }
  662. if (gui_grab || qemu_input_is_absolute() || absolute_enabled) {
  663. SDL_SetCursor(guest_sprite);
  664. if (!qemu_input_is_absolute() && !absolute_enabled) {
  665. SDL_WarpMouseInWindow(scon->real_window, x, y);
  666. }
  667. }
  668. } else if (gui_grab) {
  669. sdl_hide_cursor(scon);
  670. }
  671. guest_cursor = on;
  672. guest_x = x, guest_y = y;
  673. }
  674. static void sdl_mouse_define(DisplayChangeListener *dcl,
  675. QEMUCursor *c)
  676. {
  677. if (guest_sprite) {
  678. SDL_FreeCursor(guest_sprite);
  679. }
  680. if (guest_sprite_surface) {
  681. SDL_FreeSurface(guest_sprite_surface);
  682. }
  683. guest_sprite_surface =
  684. SDL_CreateRGBSurfaceFrom(c->data, c->width, c->height, 32, c->width * 4,
  685. 0xff0000, 0x00ff00, 0xff, 0xff000000);
  686. if (!guest_sprite_surface) {
  687. fprintf(stderr, "Failed to make rgb surface from %p\n", c);
  688. return;
  689. }
  690. guest_sprite = SDL_CreateColorCursor(guest_sprite_surface,
  691. c->hot_x, c->hot_y);
  692. if (!guest_sprite) {
  693. fprintf(stderr, "Failed to make color cursor from %p\n", c);
  694. return;
  695. }
  696. if (guest_cursor &&
  697. (gui_grab || qemu_input_is_absolute() || absolute_enabled)) {
  698. SDL_SetCursor(guest_sprite);
  699. }
  700. }
  701. static void sdl_cleanup(void)
  702. {
  703. if (guest_sprite) {
  704. SDL_FreeCursor(guest_sprite);
  705. }
  706. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  707. }
  708. static const DisplayChangeListenerOps dcl_2d_ops = {
  709. .dpy_name = "sdl2-2d",
  710. .dpy_gfx_update = sdl2_2d_update,
  711. .dpy_gfx_switch = sdl2_2d_switch,
  712. .dpy_gfx_check_format = sdl2_2d_check_format,
  713. .dpy_refresh = sdl2_2d_refresh,
  714. .dpy_mouse_set = sdl_mouse_warp,
  715. .dpy_cursor_define = sdl_mouse_define,
  716. };
  717. #ifdef CONFIG_OPENGL
  718. static const DisplayChangeListenerOps dcl_gl_ops = {
  719. .dpy_name = "sdl2-gl",
  720. .dpy_gfx_update = sdl2_gl_update,
  721. .dpy_gfx_switch = sdl2_gl_switch,
  722. .dpy_gfx_check_format = console_gl_check_format,
  723. .dpy_refresh = sdl2_gl_refresh,
  724. .dpy_mouse_set = sdl_mouse_warp,
  725. .dpy_cursor_define = sdl_mouse_define,
  726. .dpy_gl_scanout_disable = sdl2_gl_scanout_disable,
  727. .dpy_gl_scanout_texture = sdl2_gl_scanout_texture,
  728. .dpy_gl_update = sdl2_gl_scanout_flush,
  729. };
  730. static bool
  731. sdl2_gl_is_compatible_dcl(DisplayGLCtx *dgc,
  732. DisplayChangeListener *dcl)
  733. {
  734. return dcl->ops == &dcl_gl_ops;
  735. }
  736. static const DisplayGLCtxOps gl_ctx_ops = {
  737. .dpy_gl_ctx_is_compatible_dcl = sdl2_gl_is_compatible_dcl,
  738. .dpy_gl_ctx_create = sdl2_gl_create_context,
  739. .dpy_gl_ctx_destroy = sdl2_gl_destroy_context,
  740. .dpy_gl_ctx_make_current = sdl2_gl_make_context_current,
  741. };
  742. #endif
  743. static void sdl2_display_early_init(DisplayOptions *o)
  744. {
  745. assert(o->type == DISPLAY_TYPE_SDL);
  746. if (o->has_gl && o->gl) {
  747. #ifdef CONFIG_OPENGL
  748. display_opengl = 1;
  749. #endif
  750. }
  751. }
  752. static void sdl2_display_init(DisplayState *ds, DisplayOptions *o)
  753. {
  754. uint8_t data = 0;
  755. int i;
  756. SDL_SysWMinfo info;
  757. SDL_Surface *icon = NULL;
  758. char *dir;
  759. assert(o->type == DISPLAY_TYPE_SDL);
  760. if (SDL_GetHintBoolean("QEMU_ENABLE_SDL_LOGGING", SDL_FALSE)) {
  761. SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
  762. }
  763. if (SDL_Init(SDL_INIT_VIDEO)) {
  764. fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
  765. SDL_GetError());
  766. exit(1);
  767. }
  768. #ifdef SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR /* only available since SDL 2.0.8 */
  769. SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
  770. #endif
  771. #ifndef CONFIG_WIN32
  772. /* QEMU uses its own low level keyboard hook procedure on Windows */
  773. SDL_SetHint(SDL_HINT_GRAB_KEYBOARD, "1");
  774. #endif
  775. #ifdef SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED
  776. SDL_SetHint(SDL_HINT_ALLOW_ALT_TAB_WHILE_GRABBED, "0");
  777. #endif
  778. SDL_SetHint(SDL_HINT_WINDOWS_NO_CLOSE_ON_ALT_F4, "1");
  779. memset(&info, 0, sizeof(info));
  780. SDL_VERSION(&info.version);
  781. gui_fullscreen = o->has_full_screen && o->full_screen;
  782. if (o->u.sdl.has_grab_mod) {
  783. if (o->u.sdl.grab_mod == HOT_KEY_MOD_LSHIFT_LCTRL_LALT) {
  784. alt_grab = true;
  785. } else if (o->u.sdl.grab_mod == HOT_KEY_MOD_RCTRL) {
  786. ctrl_grab = true;
  787. }
  788. }
  789. for (i = 0;; i++) {
  790. QemuConsole *con = qemu_console_lookup_by_index(i);
  791. if (!con) {
  792. break;
  793. }
  794. }
  795. sdl2_num_outputs = i;
  796. if (sdl2_num_outputs == 0) {
  797. return;
  798. }
  799. sdl2_console = g_new0(struct sdl2_console, sdl2_num_outputs);
  800. for (i = 0; i < sdl2_num_outputs; i++) {
  801. QemuConsole *con = qemu_console_lookup_by_index(i);
  802. assert(con != NULL);
  803. if (!qemu_console_is_graphic(con) &&
  804. qemu_console_get_index(con) != 0) {
  805. sdl2_console[i].hidden = true;
  806. }
  807. sdl2_console[i].idx = i;
  808. sdl2_console[i].opts = o;
  809. #ifdef CONFIG_OPENGL
  810. sdl2_console[i].opengl = display_opengl;
  811. sdl2_console[i].dcl.ops = display_opengl ? &dcl_gl_ops : &dcl_2d_ops;
  812. sdl2_console[i].dgc.ops = display_opengl ? &gl_ctx_ops : NULL;
  813. #else
  814. sdl2_console[i].opengl = 0;
  815. sdl2_console[i].dcl.ops = &dcl_2d_ops;
  816. #endif
  817. sdl2_console[i].dcl.con = con;
  818. sdl2_console[i].kbd = qkbd_state_init(con);
  819. if (display_opengl) {
  820. qemu_console_set_display_gl_ctx(con, &sdl2_console[i].dgc);
  821. }
  822. register_displaychangelistener(&sdl2_console[i].dcl);
  823. #if defined(SDL_VIDEO_DRIVER_WINDOWS) || defined(SDL_VIDEO_DRIVER_X11)
  824. if (SDL_GetWindowWMInfo(sdl2_console[i].real_window, &info)) {
  825. #if defined(SDL_VIDEO_DRIVER_WINDOWS)
  826. qemu_console_set_window_id(con, (uintptr_t)info.info.win.window);
  827. #elif defined(SDL_VIDEO_DRIVER_X11)
  828. qemu_console_set_window_id(con, info.info.x11.window);
  829. #endif
  830. }
  831. #endif
  832. }
  833. #ifdef CONFIG_SDL_IMAGE
  834. dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/128x128/apps/qemu.png");
  835. icon = IMG_Load(dir);
  836. #else
  837. /* Load a 32x32x4 image. White pixels are transparent. */
  838. dir = get_relocated_path(CONFIG_QEMU_ICONDIR "/hicolor/32x32/apps/qemu.bmp");
  839. icon = SDL_LoadBMP(dir);
  840. if (icon) {
  841. uint32_t colorkey = SDL_MapRGB(icon->format, 255, 255, 255);
  842. SDL_SetColorKey(icon, SDL_TRUE, colorkey);
  843. }
  844. #endif
  845. g_free(dir);
  846. if (icon) {
  847. SDL_SetWindowIcon(sdl2_console[0].real_window, icon);
  848. }
  849. mouse_mode_notifier.notify = sdl_mouse_mode_change;
  850. qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
  851. sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
  852. sdl_cursor_normal = SDL_GetCursor();
  853. if (gui_fullscreen) {
  854. sdl_grab_start(&sdl2_console[0]);
  855. }
  856. atexit(sdl_cleanup);
  857. }
  858. static QemuDisplay qemu_display_sdl2 = {
  859. .type = DISPLAY_TYPE_SDL,
  860. .early_init = sdl2_display_early_init,
  861. .init = sdl2_display_init,
  862. };
  863. static void register_sdl1(void)
  864. {
  865. qemu_display_register(&qemu_display_sdl2);
  866. }
  867. type_init(register_sdl1);
  868. #ifdef CONFIG_OPENGL
  869. module_dep("ui-opengl");
  870. #endif