2
0

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