sdl2.c 25 KB

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