sdl2.c 28 KB

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