Skip to contentSkip to conversation

ui/sdl2 : initial port to SDL 2.0

PATCH In review Dave Airlie 3 participants 6 messages Nov 10, 2013 – Nov 11, 2013

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ui/sdl2 : initial port to SDL 2.0

From: Dave Airlie <[email protected]>

I've ported the SDL1.2 code over, and rewritten it to use the SDL2 interface.

The biggest changes were in the input handling, where SDL2 has done a major overhaul, and I've had to include a generated translation file to get from SDL2 codes back to qemu compatible ones. I'm still not sure how the keyboard layout code works in qemu, so there may be further work if someone can point me a test case that works with SDL1.2 and doesn't with SDL2.

Some SDL env vars we used to set are no longer used by SDL2, Windows, OSX support is untested,

I don't think we can link to SDL1.2 and SDL2 at the same time, so I felt using --with-sdlabi=2.0 to select the new code should be fine, like how gtk does it.

---
 configure                    |  23 +-
 ui/Makefile.objs             |   4 +-
 ui/sdl.c                     |   3 +
 ui/sdl2.c                    | 889 +++++++++++++++++++++++++++++++++++++++++++
 ui/sdl2_scancode_translate.h | 260 +++++++++++++
 ui/sdl_keysym.h              |   3 +-
 6 files changed, 1175 insertions(+), 7 deletions(-)
 create mode 100644 ui/sdl2.c
 create mode 100644 ui/sdl2_scancode_translate.h
configure +19 -4
index 9addff1..bf3be37 100755
@@ -158,6 +158,7 @@ docs=""
 fdt=""
 pixman=""
 sdl=""
+sdlabi="1.2"
 virtfs=""
 vnc="yes"
 sparse="no"
@@ -310,6 +311,7 @@ query_pkg_config() {
 }
 pkg_config=query_pkg_config
 sdl_config="${SDL_CONFIG-${cross_prefix}sdl-config}"
+sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 
 # default flags for all hosts
 QEMU_CFLAGS="-fno-strict-aliasing $QEMU_CFLAGS"
@@ -710,6 +712,8 @@ for opt do
   ;;
   --enable-sdl) sdl="yes"
   ;;
+  --with-sdlabi=*) sdlabi="$optarg"
+  ;;
   --disable-qom-cast-debug) qom_cast_debug="no"
   ;;
   --enable-qom-cast-debug) qom_cast_debug="yes"
@@ -1092,6 +1096,7 @@ echo "  --disable-strip          disable stripping binaries"
 echo "  --disable-werror         disable compilation abort on warning"
 echo "  --disable-sdl            disable SDL"
 echo "  --enable-sdl             enable SDL"
+echo "  --with-sdlabi            select preferred SDL ABI 1.2 or 2.0"
 echo "  --disable-gtk            disable gtk UI"
 echo "  --enable-gtk             enable gtk UI"
 echo "  --disable-virtfs         disable VirtFS"
@@ -1751,12 +1756,22 @@ fi
 
 # Look for sdl configuration program (pkg-config or sdl-config).  Try
 # sdl-config even without cross prefix, and favour pkg-config over sdl-config.
-if test "`basename $sdl_config`" != sdl-config && ! has ${sdl_config}; then
-  sdl_config=sdl-config
+
+if test $sdlabi == "2.0"; then
+    sdl_config=$sdl2_config
+    sdlname=sdl2
+    sdlconfigname=sdl2_config
+else
+    sdlname=sdl
+    sdlconfigname=sdl_config
+fi
+
+if test "`basename $sdl_config`" != $sdlconfigname && ! has ${sdl_config}; then
+  sdl_config=$sdlconfigname
 fi
 
-if $pkg_config sdl --exists; then
-  sdlconfig="$pkg_config sdl"
+if $pkg_config $sdlname --exists; then
+  sdlconfig="$pkg_config $sdlname"
   _sdlversion=`$sdlconfig --modversion 2>/dev/null | sed 's/[^0-9]//g'`
 elif has ${sdl_config}; then
   sdlconfig="$sdl_config"
ui/Makefile.objs +2 -2
index f33be47..721ad37 100644
@@ -9,12 +9,12 @@ vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o console.o cursor.o input.o qemu-pixman.o
 common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o spice-display.o
-common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o
+common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o sdl2.o
 common-obj-$(CONFIG_COCOA) += cocoa.o
 common-obj-$(CONFIG_CURSES) += curses.o
 common-obj-$(CONFIG_VNC) += $(vnc-obj-y)
 common-obj-$(CONFIG_GTK) += gtk.o x_keymap.o
 
-$(obj)/sdl.o $(obj)/sdl_zoom.o: QEMU_CFLAGS += $(SDL_CFLAGS) 
+$(obj)/sdl.o $(obj)/sdl_zoom.o $(obj)/sdl2.o: QEMU_CFLAGS += $(SDL_CFLAGS) 
 
 $(obj)/gtk.o: QEMU_CFLAGS += $(GTK_CFLAGS) $(VTE_CFLAGS)
ui/sdl.c +3 -0
index 9d8583c..736bb95 100644
@@ -26,6 +26,8 @@
 #undef WIN32_LEAN_AND_MEAN
 
 #include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 1
 #include <SDL_syswm.h>
 
 #include "qemu-common.h"
@@ -966,3 +968,4 @@ void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
 
     atexit(sdl_cleanup);
 }
+#endif
ui/sdl2.c new file +889 -0

889 lines not on this page · raw message

ui/sdl2_scancode_translate.h new file +260 -0

260 lines not on this page · raw message

ui/sdl_keysym.h +2 -1
index ee90480..599d9fc 100644
@@ -200,6 +200,7 @@ static const name2keysym_t name2keysym[]={
 { "yacute",               0x0fd},
 { "thorn",                0x0fe},
 { "ydiaeresis",           0x0ff},
+#if SDL_MAJOR_VERSION == 1
 {"EuroSign", SDLK_EURO},
 
     /* modifiers */
@@ -272,6 +273,6 @@ static const name2keysym_t name2keysym[]={
 {"Num_Lock", SDLK_NUMLOCK},
 {"Pause", SDLK_PAUSE},
 {"Escape", SDLK_ESCAPE},
-
+#endif
 {NULL, 0},
 };
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 17 lines · Dave Airlie

From: Dave Airlie <[email protected]>

I've ported the SDL1.2 code over, and rewritten it to use the SDL2 interface.

The biggest changes were in the input handling, where SDL2 has done a major overhaul, and I've had to include a generated translation file to get from SDL2 codes back to qemu compatible ones. I'm still not sure how the keyboard layout code works in qemu, so there may be further work if someone can point me a test case that works with SDL1.2 and doesn't with SDL2.

Some SDL env vars we used to set are no longer used by SDL2, Windows, OSX support is untested,

I don't think we can link to SDL1.2 and SDL2 at the same time, so I felt using --with-sdlabi=2.0 to select the new code should be fine, like how gtk does it.

Quoted 6 lines
---
 configure                    |  23 +-
 ui/Makefile.objs             |   4 +-
 ui/sdl.c                     |   3 +
 ui/sdl2.c                    | 889 +++++++++++++++++++++++++++++++++++++++++++

Can we refactor this to not duplicate everything and instead have function hooks or even #ifdefs for the things that are different? We try to guess the right SDL to use in configure. See how we handle GTK2 vs. GTK3.

It's very hard to review ATM due to the split.

Regarding the keycodes, danpb has a great write up on his blog:

https://www.berrange.com/posts/2010/07/04/a-summary-of-scan-code-key-codes-sets-used-in-the-pc-virtualization-stack/

Internally, we use a variant of raw XT scancodes. We have a keymapping routine that translates from a symbolic key (i.e. "capital A") to the appropriate XT scancode.

SDL 1.x at least lets you get at raw X11 scancodes which are either evdev or PS/2 codes depending on the version of X11. So for SDL 1.x we have two translations mechanisms 1) X11 scancodes to XT scancodes and 2) SDL keysyms to internal QEMU keysyms.

Quoted 1 line

From what I can tell, SDL2 has moved from just passing through raw X11

scancodes (which like I said, can be different depending on your X configuration) to having an intermediate translation layer. See comments inline:

Quoted 282 lines
 ui/sdl2_scancode_translate.h | 260 +++++++++++++
 ui/sdl_keysym.h              |   3 +-
 6 files changed, 1175 insertions(+), 7 deletions(-)
 create mode 100644 ui/sdl2.c
 create mode 100644 ui/sdl2_scancode_translate.h
configure +19 -4
index 9addff1..bf3be37 100755
@@ -158,6 +158,7 @@ docs=""
 fdt=""
 pixman=""
 sdl=""
+sdlabi="1.2"
 virtfs=""
 vnc="yes"
 sparse="no"
@@ -310,6 +311,7 @@ query_pkg_config() {
 }
 pkg_config=query_pkg_config
 sdl_config="${SDL_CONFIG-${cross_prefix}sdl-config}"
+sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 
 # default flags for all hosts
 QEMU_CFLAGS="-fno-strict-aliasing $QEMU_CFLAGS"
@@ -710,6 +712,8 @@ for opt do
   ;;
   --enable-sdl) sdl="yes"
   ;;
+  --with-sdlabi=*) sdlabi="$optarg"
+  ;;
   --disable-qom-cast-debug) qom_cast_debug="no"
   ;;
   --enable-qom-cast-debug) qom_cast_debug="yes"
@@ -1092,6 +1096,7 @@ echo "  --disable-strip          disable stripping binaries"
 echo "  --disable-werror         disable compilation abort on warning"
 echo "  --disable-sdl            disable SDL"
 echo "  --enable-sdl             enable SDL"
+echo "  --with-sdlabi            select preferred SDL ABI 1.2 or 2.0"
 echo "  --disable-gtk            disable gtk UI"
 echo "  --enable-gtk             enable gtk UI"
 echo "  --disable-virtfs         disable VirtFS"
@@ -1751,12 +1756,22 @@ fi
 
 # Look for sdl configuration program (pkg-config or sdl-config).  Try
 # sdl-config even without cross prefix, and favour pkg-config over sdl-config.
-if test "`basename $sdl_config`" != sdl-config && ! has ${sdl_config}; then
-  sdl_config=sdl-config
+
+if test $sdlabi == "2.0"; then
+    sdl_config=$sdl2_config
+    sdlname=sdl2
+    sdlconfigname=sdl2_config
+else
+    sdlname=sdl
+    sdlconfigname=sdl_config
+fi
+
+if test "`basename $sdl_config`" != $sdlconfigname && ! has ${sdl_config}; then
+  sdl_config=$sdlconfigname
 fi
 
-if $pkg_config sdl --exists; then
-  sdlconfig="$pkg_config sdl"
+if $pkg_config $sdlname --exists; then
+  sdlconfig="$pkg_config $sdlname"
   _sdlversion=`$sdlconfig --modversion 2>/dev/null | sed 's/[^0-9]//g'`
 elif has ${sdl_config}; then
   sdlconfig="$sdl_config"
ui/Makefile.objs +2 -2
index f33be47..721ad37 100644
@@ -9,12 +9,12 @@ vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o console.o cursor.o input.o qemu-pixman.o
 common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o spice-display.o
-common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o
+common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o sdl2.o
 common-obj-$(CONFIG_COCOA) += cocoa.o
 common-obj-$(CONFIG_CURSES) += curses.o
 common-obj-$(CONFIG_VNC) += $(vnc-obj-y)
 common-obj-$(CONFIG_GTK) += gtk.o x_keymap.o
 
-$(obj)/sdl.o $(obj)/sdl_zoom.o: QEMU_CFLAGS += $(SDL_CFLAGS)
+$(obj)/sdl.o $(obj)/sdl_zoom.o $(obj)/sdl2.o: QEMU_CFLAGS += $(SDL_CFLAGS)
 
 $(obj)/gtk.o: QEMU_CFLAGS += $(GTK_CFLAGS) $(VTE_CFLAGS)
ui/sdl.c +3 -0
index 9d8583c..736bb95 100644
@@ -26,6 +26,8 @@
 #undef WIN32_LEAN_AND_MEAN
 
 #include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 1
 #include <SDL_syswm.h>
 
 #include "qemu-common.h"
@@ -966,3 +968,4 @@ void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
 
     atexit(sdl_cleanup);
 }
+#endif
ui/sdl2.c new file +169 -0
new file mode 100644
index 0000000..1a71f59
@@ -0,0 +1,889 @@
+/*
+ * QEMU SDL display driver
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+/* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
+
+/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
+#undef WIN32_LEAN_AND_MEAN
+
+#include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 2
+#include <SDL_syswm.h>
+
+#include "qemu-common.h"
+#include "ui/console.h"
+#include "sysemu/sysemu.h"
+#include "x_keymap.h"
+#include "sdl_zoom.h"
+
+#include "sdl2_scancode_translate.h"
+
+static DisplayChangeListener *dcl;
+static DisplaySurface *surface;
+static SDL_Window *real_window;
+static SDL_Renderer *real_renderer;
+static SDL_Texture *guest_texture;
+
+static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
+static int last_vm_running;
+static bool gui_saved_scaling;
+static int gui_saved_width;
+static int gui_saved_height;
+static int gui_saved_grab;
+static int gui_fullscreen;
+static int gui_noframe;
+static int gui_key_modifier_pressed;
+static int gui_keysym;
+static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
+static uint8_t modifiers_state[256];
+static SDL_Cursor *sdl_cursor_normal;
+static SDL_Cursor *sdl_cursor_hidden;
+static int absolute_enabled = 0;
+static int guest_cursor = 0;
+static int guest_x, guest_y;
+static SDL_Cursor *guest_sprite = NULL;
+static int scaling_active = 0;
+static Notifier mouse_mode_notifier;
+
+static void sdl_update_caption(void);
+
+static void sdl_update(DisplayChangeListener *dcl,
+                       int x, int y, int w, int h)
+{
+    if (!surface)
+        return;
+
+    SDL_UpdateTexture(guest_texture, NULL, surface_data(surface),
+                      surface_stride(surface));
+    SDL_RenderCopy(real_renderer, guest_texture, NULL, NULL);
+    SDL_RenderPresent(real_renderer);
+}
+
+static void do_sdl_resize(int width, int height, int bpp)
+{
+    int flags;
+
+    if (real_window) {
+        SDL_SetWindowSize(real_window, width, height);
+        SDL_RenderSetLogicalSize(real_renderer, width, height);
+    } else {
+        flags = 0;
+        if (gui_fullscreen)
+            flags |= SDL_WINDOW_FULLSCREEN;
+        else
+            flags |= SDL_WINDOW_RESIZABLE;
+
+        real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
+                                       SDL_WINDOWPOS_UNDEFINED,
+                                       width, height, flags);
+        real_renderer = SDL_CreateRenderer(real_window, -1, 0);
+        sdl_update_caption();
+    }
+}
+
+static void sdl_switch(DisplayChangeListener *dcl,
+                       DisplaySurface *new_surface)
+{
+    int format = 0;
+    /* temporary hack: allows to call sdl_switch to handle scaling changes */
+    if (new_surface) {
+        surface = new_surface;
+    }
+
+    if (!scaling_active) {
+        do_sdl_resize(surface_width(surface), surface_height(surface), 0);
+    } else
+        do_sdl_resize(surface_width(surface), surface_height(surface), 0);
+
+    if (guest_texture)
+        SDL_DestroyTexture(guest_texture);
+
+    if (surface_bits_per_pixel(surface) == 16)
+        format = SDL_PIXELFORMAT_RGB565;
+    else if (surface_bits_per_pixel(surface) == 32)
+        format = SDL_PIXELFORMAT_ARGB8888;
+
+    if (!format)
+        exit(1);
+    guest_texture = SDL_CreateTexture(real_renderer, format,
+                                      SDL_TEXTUREACCESS_STREAMING,
+                                      surface_width(surface), surface_height(surface));
+    SDL_RenderSetLogicalSize(real_renderer, surface_width(surface), surface_height(surface));
+}
+
+/* generic keyboard conversion */
+
+#include "sdl_keysym.h"
+
+static kbd_layout_t *kbd_layout = NULL;
+
+static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
+{
+    int keysym;
+    /* workaround for X11+SDL bug with AltGR */
+    keysym = ev->keysym.sym;
+    if (keysym == 0 && ev->keysym.scancode == 113)
+        keysym = SDLK_MODE;
+    /* For Japanese key '\' and '|' */
+    if (keysym == 92 && ev->keysym.scancode == 133) {
+        keysym = 0xa5;
+    }
+    return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
+}
+
+/* specific keyboard conversions from scan codes */
+
+#if defined(_WIN32)
+
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+    keycode = sdl2_scancode_to_keycode[keycode];
+    return keycode;
+}
+
+#else
+

ui/sdl2.c line 179

Quoted 10 lines
ui/sdl2.c new file +10 -0
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+
+    keycode = sdl2_scancode_to_keycode[keycode];
+    if (keycode >= 89 && keycode < 150) {
+        keycode = translate_evdev_keycode(keycode - 89);
+    }

This doesn't seem right to me. It just so happened that the low value scan codes are the same for both X11, evdev, and SDL 1.x. That's why we only used the evdev/x11 translation tables for higher codes. It was just a table size optimization.

Presumably, sdl2 doesn't do this though, or does it? If it does, don't we need to probe for evdev instead of assuming it's there?

Regards,

Anthony Liguori

Quoted 1,000 lines
ui/sdl2.c new file +710 -0
+    return keycode;
+}
+
+#endif
+
+static void reset_keys(void)
+{
+    int i;
+    for(i = 0; i < 256; i++) {
+        if (modifiers_state[i]) {
+            if (i & SCANCODE_GREY)
+                kbd_put_keycode(SCANCODE_EMUL0);
+            kbd_put_keycode(i | SCANCODE_UP);
+            modifiers_state[i] = 0;
+        }
+    }
+}
+
+static void sdl_process_key(SDL_KeyboardEvent *ev)
+{
+    int keycode, v;
+
+    if (ev->keysym.sym == SDLK_PAUSE) {
+        /* specific case */
+        v = 0;
+        if (ev->type == SDL_KEYUP)
+            v |= SCANCODE_UP;
+        kbd_put_keycode(0xe1);
+        kbd_put_keycode(0x1d | v);
+        kbd_put_keycode(0x45 | v);
+        return;
+    }
+
+    if (kbd_layout) {
+        keycode = sdl_keyevent_to_keycode_generic(ev);
+    } else {
+        keycode = sdl_keyevent_to_keycode(ev);
+    }
+
+    switch(keycode) {
+    case 0x00:
+        /* sent when leaving window: reset the modifiers state */
+        reset_keys();
+        return;
+    case 0x2a:                          /* Left Shift */
+    case 0x36:                          /* Right Shift */
+    case 0x1d:                          /* Left CTRL */
+    case 0x9d:                          /* Right CTRL */
+    case 0x38:                          /* Left ALT */
+    case 0xb8:                         /* Right ALT */
+        if (ev->type == SDL_KEYUP)
+            modifiers_state[keycode] = 0;
+        else
+            modifiers_state[keycode] = 1;
+        break;
+#define QEMU_SDL_VERSION ((SDL_MAJOR_VERSION << 8) + SDL_MINOR_VERSION)
+#if QEMU_SDL_VERSION < 0x102 || QEMU_SDL_VERSION == 0x102 && SDL_PATCHLEVEL < 14
+        /* SDL versions before 1.2.14 don't support key up for caps/num lock. */
+    case 0x45: /* num lock */
+    case 0x3a: /* caps lock */
+        /* SDL does not send the key up event, so we generate it */
+        kbd_put_keycode(keycode);
+        kbd_put_keycode(keycode | SCANCODE_UP);
+        return;
+#endif
+    }
+
+    /* now send the key code */
+    if (keycode & SCANCODE_GREY)
+        kbd_put_keycode(SCANCODE_EMUL0);
+    if (ev->type == SDL_KEYUP)
+        kbd_put_keycode(keycode | SCANCODE_UP);
+    else
+        kbd_put_keycode(keycode & SCANCODE_KEYCODEMASK);
+}
+
+static void sdl_update_caption(void)
+{
+    char win_title[1024];
+    char icon_title[1024];
+    const char *status = "";
+
+    if (!runstate_is_running())
+        status = " [Stopped]";
+    else if (gui_grab) {
+        if (alt_grab)
+            status = " - Press Ctrl-Alt-Shift to exit mouse grab";
+        else if (ctrl_grab)
+            status = " - Press Right-Ctrl to exit mouse grab";
+        else
+            status = " - Press Ctrl-Alt to exit mouse grab";
+    }
+
+    if (qemu_name) {
+        snprintf(win_title, sizeof(win_title), "QEMU (%s)%s", qemu_name, status);
+        snprintf(icon_title, sizeof(icon_title), "QEMU (%s)", qemu_name);
+    } else {
+        snprintf(win_title, sizeof(win_title), "QEMU%s", status);
+        snprintf(icon_title, sizeof(icon_title), "QEMU");
+    }
+
+    if (real_window)
+      SDL_SetWindowTitle(real_window, win_title);
+}
+
+static void sdl_hide_cursor(void)
+{
+    if (!cursor_hide)
+        return;
+
+    if (kbd_mouse_is_absolute()) {
+        SDL_ShowCursor(1);
+        SDL_SetCursor(sdl_cursor_hidden);
+    } else {
+        SDL_ShowCursor(0);
+    }
+}
+
+static void sdl_show_cursor(void)
+{
+    if (!cursor_hide)
+        return;
+
+    if (!kbd_mouse_is_absolute() || !qemu_console_is_graphic(NULL)) {
+        SDL_ShowCursor(1);
+        if (guest_cursor &&
+                (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
+            SDL_SetCursor(guest_sprite);
+        else
+            SDL_SetCursor(sdl_cursor_normal);
+    }
+}
+
+static void sdl_grab_start(void)
+{
+    /*
+     * If the application is not active, do not try to enter grab state. This
+     * prevents 'SDL_WM_GrabInput(SDL_GRAB_ON)' from blocking all the
+     * application (SDL bug).
+     */
+    if (!(SDL_GetWindowFlags(real_window) & SDL_WINDOW_INPUT_FOCUS)) {
+        return;
+    }
+    if (guest_cursor) {
+        SDL_SetCursor(guest_sprite);
+        if (!kbd_mouse_is_absolute() && !absolute_enabled) {
+            SDL_WarpMouseInWindow(real_window, guest_x, guest_y);
+        }
+    } else
+        sdl_hide_cursor();
+    SDL_SetWindowGrab(real_window, SDL_TRUE);
+    gui_grab = 1;
+    sdl_update_caption();
+}
+
+static void sdl_grab_end(void)
+{
+    SDL_SetWindowGrab(real_window, SDL_FALSE);
+    gui_grab = 0;
+    sdl_show_cursor();
+    sdl_update_caption();
+}
+
+static void absolute_mouse_grab(void)
+{
+    int mouse_x, mouse_y;
+    int scr_w, scr_h;
+    SDL_GetMouseState(&mouse_x, &mouse_y);
+    SDL_GetWindowSize(real_window, &scr_w, &scr_h);
+    if (mouse_x > 0 && mouse_x < scr_w - 1 &&
+        mouse_y > 0 && mouse_y < scr_h - 1) {
+        sdl_grab_start();
+    }
+}
+
+static void sdl_mouse_mode_change(Notifier *notify, void *data)
+{
+    if (kbd_mouse_is_absolute()) {
+        if (!absolute_enabled) {
+            absolute_enabled = 1;
+            if (qemu_console_is_graphic(NULL)) {
+                absolute_mouse_grab();
+            }
+        }
+    } else if (absolute_enabled) {
+        if (!gui_fullscreen) {
+            sdl_grab_end();
+        }
+        absolute_enabled = 0;
+    }
+}
+
+static void sdl_send_mouse_event(int dx, int dy, int dz, int x, int y, int state)
+{
+    int buttons = 0;
+
+    if (state & SDL_BUTTON(SDL_BUTTON_LEFT)) {
+        buttons |= MOUSE_EVENT_LBUTTON;
+    }
+    if (state & SDL_BUTTON(SDL_BUTTON_RIGHT)) {
+        buttons |= MOUSE_EVENT_RBUTTON;
+    }
+    if (state & SDL_BUTTON(SDL_BUTTON_MIDDLE)) {
+        buttons |= MOUSE_EVENT_MBUTTON;
+    }
+
+    if (kbd_mouse_is_absolute()) {
+        int scr_w, scr_h;
+        SDL_GetWindowSize(real_window, &scr_w, &scr_h);
+        dx = x * 0x7FFF / (scr_w - 1);
+        dy = y * 0x7FFF / (scr_h - 1);
+    } else if (guest_cursor) {
+        x -= guest_x;
+        y -= guest_y;
+        guest_x += x;
+        guest_y += y;
+        dx = x;
+        dy = y;
+    }
+
+    kbd_mouse_event(dx, dy, dz, buttons);
+}
+
+static void sdl_scale(int width, int height)
+{
+    int bpp = 0;
+    do_sdl_resize(width, height, bpp);
+    scaling_active = 1;
+}
+
+static void toggle_full_screen(void)
+{
+    int width = surface_width(surface);
+    int height = surface_height(surface);
+    int bpp = surface_bits_per_pixel(surface);
+
+    gui_fullscreen = !gui_fullscreen;
+    if (gui_fullscreen) {
+        SDL_GetWindowSize(real_window, &gui_saved_width, &gui_saved_height);
+        gui_saved_scaling = scaling_active;
+
+        do_sdl_resize(width, height, bpp);
+        scaling_active = 0;
+
+        gui_saved_grab = gui_grab;
+        sdl_grab_start();
+    } else {
+        if (gui_saved_scaling) {
+            sdl_scale(gui_saved_width, gui_saved_height);
+        } else {
+            do_sdl_resize(width, height, 0);
+        }
+        if (!gui_saved_grab || !qemu_console_is_graphic(NULL)) {
+            sdl_grab_end();
+        }
+    }
+    graphic_hw_invalidate(NULL);
+    graphic_hw_update(NULL);
+}
+
+static void handle_keydown(SDL_Event *ev)
+{
+    int mod_state;
+    int keycode;
+
+    if (alt_grab) {
+        mod_state = (SDL_GetModState() & (gui_grab_code | KMOD_LSHIFT)) ==
+                    (gui_grab_code | KMOD_LSHIFT);
+    } else if (ctrl_grab) {
+        mod_state = (SDL_GetModState() & KMOD_RCTRL) == KMOD_RCTRL;
+    } else {
+        mod_state = (SDL_GetModState() & gui_grab_code) == gui_grab_code;
+    }
+    gui_key_modifier_pressed = mod_state;
+
+    if (gui_key_modifier_pressed) {
+        keycode = sdl_keyevent_to_keycode(&ev->key);
+        switch (keycode) {
+        case 0x21: /* 'f' key on US keyboard */
+            toggle_full_screen();
+            gui_keysym = 1;
+            break;
+        case 0x16: /* 'u' key on US keyboard */
+            if (scaling_active) {
+                scaling_active = 0;
+                sdl_switch(dcl, NULL);
+                graphic_hw_invalidate(NULL);
+                graphic_hw_update(NULL);
+            }
+            gui_keysym = 1;
+            break;
+        case 0x02 ... 0x0a: /* '1' to '9' keys */
+            /* Reset the modifiers sent to the current console */
+            reset_keys();
+            console_select(keycode - 0x02);
+            gui_keysym = 1;
+            if (gui_fullscreen) {
+                break;
+            }
+            if (!qemu_console_is_graphic(NULL)) {
+                /* release grab if going to a text console */
+                if (gui_grab) {
+                    sdl_grab_end();
+                } else if (absolute_enabled) {
+                    sdl_show_cursor();
+                }
+            } else if (absolute_enabled) {
+                sdl_hide_cursor();
+                absolute_mouse_grab();
+            }
+            break;
+        case 0x1b: /* '+' */
+        case 0x35: /* '-' */
+            if (!gui_fullscreen) {
+                int scr_w, scr_h;
+                int width, height;
+                SDL_GetWindowSize(real_window, &scr_w, &scr_h);
+
+                width = MAX(scr_w + (keycode == 0x1b ? 50 : -50),
+                                160);
+                height = (surface_height(surface) * width) /
+                    surface_width(surface);
+
+                sdl_scale(width, height);
+                graphic_hw_invalidate(NULL);
+                graphic_hw_update(NULL);
+                gui_keysym = 1;
+            }
+        default:
+            break;
+        }
+    } else if (!qemu_console_is_graphic(NULL)) {
+        int keysym = 0;
+
+        if (ev->key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
+            switch (ev->key.keysym.sym) {
+            case SDLK_UP:
+                keysym = QEMU_KEY_CTRL_UP;
+                break;
+            case SDLK_DOWN:
+                keysym = QEMU_KEY_CTRL_DOWN;
+                break;
+            case SDLK_LEFT:
+                keysym = QEMU_KEY_CTRL_LEFT;
+                break;
+            case SDLK_RIGHT:
+                keysym = QEMU_KEY_CTRL_RIGHT;
+                break;
+            case SDLK_HOME:
+                keysym = QEMU_KEY_CTRL_HOME;
+                break;
+            case SDLK_END:
+                keysym = QEMU_KEY_CTRL_END;
+                break;
+            case SDLK_PAGEUP:
+                keysym = QEMU_KEY_CTRL_PAGEUP;
+                break;
+            case SDLK_PAGEDOWN:
+                keysym = QEMU_KEY_CTRL_PAGEDOWN;
+                break;
+            default:
+                break;
+            }
+        } else {
+            switch (ev->key.keysym.sym) {
+            case SDLK_UP:
+                keysym = QEMU_KEY_UP;
+                break;
+            case SDLK_DOWN:
+                keysym = QEMU_KEY_DOWN;
+                break;
+            case SDLK_LEFT:
+                keysym = QEMU_KEY_LEFT;
+                break;
+            case SDLK_RIGHT:
+                keysym = QEMU_KEY_RIGHT;
+                break;
+            case SDLK_HOME:
+                keysym = QEMU_KEY_HOME;
+                break;
+            case SDLK_END:
+                keysym = QEMU_KEY_END;
+                break;
+            case SDLK_PAGEUP:
+                keysym = QEMU_KEY_PAGEUP;
+                break;
+            case SDLK_PAGEDOWN:
+                keysym = QEMU_KEY_PAGEDOWN;
+                break;
+            case SDLK_BACKSPACE:
+                keysym = QEMU_KEY_BACKSPACE;
+                break;
+            case SDLK_DELETE:
+                keysym = QEMU_KEY_DELETE;
+                break;
+            default:
+                break;
+            }
+        }
+        if (keysym) {
+            kbd_put_keysym(keysym);
+        }
+    }
+    if (qemu_console_is_graphic(NULL) && !gui_keysym) {
+        sdl_process_key(&ev->key);
+    }
+}
+
+static void handle_keyup(SDL_Event *ev)
+{
+    int mod_state;
+
+    if (!alt_grab) {
+        mod_state = (ev->key.keysym.mod & gui_grab_code);
+    } else {
+        mod_state = (ev->key.keysym.mod & (gui_grab_code | KMOD_LSHIFT));
+    }
+    if (!mod_state && gui_key_modifier_pressed) {
+        gui_key_modifier_pressed = 0;
+        if (gui_keysym == 0) {
+            /* exit/enter grab if pressing Ctrl-Alt */
+            if (!gui_grab) {
+                if (qemu_console_is_graphic(NULL)) {
+                    sdl_grab_start();
+                }
+            } else if (!gui_fullscreen) {
+                sdl_grab_end();
+            }
+            /* SDL does not send back all the modifiers key, so we must
+             * correct it. */
+            reset_keys();
+            return;
+        }
+        gui_keysym = 0;
+    }
+    if (qemu_console_is_graphic(NULL) && !gui_keysym) {
+        sdl_process_key(&ev->key);
+    }
+}
+
+static void handle_mousemotion(SDL_Event *ev)
+{
+    int max_x, max_y;
+
+    if (qemu_console_is_graphic(NULL) &&
+        (kbd_mouse_is_absolute() || absolute_enabled)) {
+        int scr_w, scr_h;
+        SDL_GetWindowSize(real_window, &scr_w, &scr_h);
+        max_x = scr_w - 1;
+        max_y = scr_h - 1;
+        if (gui_grab && (ev->motion.x == 0 || ev->motion.y == 0 ||
+            ev->motion.x == max_x || ev->motion.y == max_y)) {
+            sdl_grab_end();
+        }
+        if (!gui_grab &&
+            (ev->motion.x > 0 && ev->motion.x < max_x &&
+            ev->motion.y > 0 && ev->motion.y < max_y)) {
+            sdl_grab_start();
+        }
+    }
+    if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
+        sdl_send_mouse_event(ev->motion.xrel, ev->motion.yrel, 0,
+                             ev->motion.x, ev->motion.y, ev->motion.state);
+    }
+}
+
+static void handle_mousebutton(SDL_Event *ev)
+{
+    int buttonstate = SDL_GetMouseState(NULL, NULL);
+    SDL_MouseButtonEvent *bev;
+    int dz;
+
+    if (!qemu_console_is_graphic(NULL)) {
+        return;
+    }
+
+    bev = &ev->button;
+    if (!gui_grab && !kbd_mouse_is_absolute()) {
+        if (ev->type == SDL_MOUSEBUTTONUP && bev->button == SDL_BUTTON_LEFT) {
+            /* start grabbing all events */
+            sdl_grab_start();
+        }
+    } else {
+        dz = 0;
+        if (ev->type == SDL_MOUSEBUTTONDOWN) {
+            buttonstate |= SDL_BUTTON(bev->button);
+        } else {
+            buttonstate &= ~SDL_BUTTON(bev->button);
+        }
+#ifdef SDL_BUTTON_WHEELUP
+        if (bev->button == SDL_BUTTON_WHEELUP &&
+            ev->type == SDL_MOUSEBUTTONDOWN) {
+            dz = -1;
+        } else if (bev->button == SDL_BUTTON_WHEELDOWN &&
+                   ev->type == SDL_MOUSEBUTTONDOWN) {
+            dz = 1;
+        }
+#endif
+        sdl_send_mouse_event(0, 0, dz, bev->x, bev->y, buttonstate);
+    }
+}
+
+static void handle_windowevent(DisplayChangeListener *dcl, SDL_Event *ev)
+{
+    int w, h;
+
+    switch (ev->window.event) {
+    case SDL_WINDOWEVENT_RESIZED:
+        sdl_scale(ev->window.data1, ev->window.data2);
+        graphic_hw_invalidate(NULL);
+        graphic_hw_update(NULL);
+        break;
+    case SDL_WINDOWEVENT_EXPOSED:
+        SDL_GetWindowSize(SDL_GetWindowFromID(ev->window.windowID), &w, &h);
+        sdl_update(dcl, 0, 0, w, h);
+        break;
+    case SDL_WINDOWEVENT_FOCUS_GAINED:
+    case SDL_WINDOWEVENT_ENTER:
+        if (!gui_grab && qemu_console_is_graphic(NULL) &&
+            (kbd_mouse_is_absolute() || absolute_enabled)) {
+            absolute_mouse_grab();
+        }
+        break;
+    case SDL_WINDOWEVENT_FOCUS_LOST:
+        if (gui_grab && !gui_fullscreen)
+            sdl_grab_end();
+        break;
+    case SDL_WINDOWEVENT_RESTORED:
+        update_displaychangelistener(dcl, GUI_REFRESH_INTERVAL_DEFAULT);
+        break;
+    case SDL_WINDOWEVENT_MINIMIZED:
+        update_displaychangelistener(dcl, 500);
+        break;
+    case SDL_WINDOWEVENT_CLOSE:
+        qemu_system_shutdown_request();
+        break;
+    }
+}
+
+static void sdl_refresh(DisplayChangeListener *dcl)
+{
+    SDL_Event ev1, *ev = &ev1;
+
+    if (last_vm_running != runstate_is_running()) {
+        last_vm_running = runstate_is_running();
+        sdl_update_caption();
+    }
+
+    graphic_hw_update(NULL);
+
+    while (SDL_PollEvent(ev)) {
+        switch (ev->type) {
+         case SDL_KEYDOWN:
+            handle_keydown(ev);
+            break;
+        case SDL_KEYUP:
+            handle_keyup(ev);
+            break;
+        case SDL_QUIT:
+            if (!no_quit) {
+                no_shutdown = 0;
+                qemu_system_shutdown_request();
+            }
+            break;
+        case SDL_MOUSEMOTION:
+            handle_mousemotion(ev);
+            break;
+        case SDL_MOUSEBUTTONDOWN:
+        case SDL_MOUSEBUTTONUP:
+            handle_mousebutton(ev);
+            break;
+        case SDL_WINDOWEVENT:
+            handle_windowevent(dcl, ev);
+            break;
+        default:
+            break;
+        }
+    }
+}
+
+static void sdl_mouse_warp(DisplayChangeListener *dcl,
+                           int x, int y, int on)
+{
+    if (on) {
+        if (!guest_cursor)
+            sdl_show_cursor();
+        if (gui_grab || kbd_mouse_is_absolute() || absolute_enabled) {
+            SDL_SetCursor(guest_sprite);
+            if (!kbd_mouse_is_absolute() && !absolute_enabled) {
+                SDL_WarpMouseInWindow(real_window, x, y);
+            }
+        }
+    } else if (gui_grab)
+        sdl_hide_cursor();
+    guest_cursor = on;
+    guest_x = x, guest_y = y;
+}
+
+static void sdl_mouse_define(DisplayChangeListener *dcl,
+                             QEMUCursor *c)
+{
+    uint8_t *image, *mask;
+    int bpl;
+
+    if (guest_sprite)
+        SDL_FreeCursor(guest_sprite);
+
+    bpl = cursor_get_mono_bpl(c);
+    image = g_malloc0(bpl * c->height);
+    mask  = g_malloc0(bpl * c->height);
+    cursor_get_mono_image(c, 0x000000, image);
+    cursor_get_mono_mask(c, 0, mask);
+    guest_sprite = SDL_CreateCursor(image, mask, c->width, c->height,
+                                    c->hot_x, c->hot_y);
+    g_free(image);
+    g_free(mask);
+
+    if (guest_cursor &&
+            (gui_grab || kbd_mouse_is_absolute() || absolute_enabled))
+        SDL_SetCursor(guest_sprite);
+}
+
+static void sdl_cleanup(void)
+{
+    if (guest_sprite)
+        SDL_FreeCursor(guest_sprite);
+    SDL_QuitSubSystem(SDL_INIT_VIDEO);
+}
+
+static const DisplayChangeListenerOps dcl_ops = {
+    .dpy_name          = "sdl",
+    .dpy_gfx_update    = sdl_update,
+    .dpy_gfx_switch    = sdl_switch,
+    .dpy_refresh       = sdl_refresh,
+    .dpy_mouse_set     = sdl_mouse_warp,
+    .dpy_cursor_define = sdl_mouse_define,
+};
+
+void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
+{
+    int flags;
+    uint8_t data = 0;
+    char *filename;
+
+#if defined(__APPLE__)
+    /* always use generic keymaps */
+    if (!keyboard_layout)
+        keyboard_layout = "en-us";
+#endif
+    if(keyboard_layout) {
+        kbd_layout = init_keyboard_layout(name2keysym, keyboard_layout);
+        if (!kbd_layout)
+            exit(1);
+    }
+
+    if (no_frame)
+        gui_noframe = 1;
+
+#ifdef __linux__
+    /* on Linux, SDL may use fbcon|directfb|svgalib when run without
+     * accessible $DISPLAY to open X11 window.  This is often the case
+     * when qemu is run using sudo.  But in this case, and when actually
+     * run in X11 environment, SDL fights with X11 for the video card,
+     * making current display unavailable, often until reboot.
+     * So make x11 the default SDL video driver if this variable is unset.
+     * This is a bit hackish but saves us from bigger problem.
+     * Maybe it's a good idea to fix this in SDL instead.
+     */
+    setenv("SDL_VIDEODRIVER", "x11", 0);
+#endif
+
+    flags = SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE;
+    if (SDL_Init (flags)) {
+        fprintf(stderr, "Could not initialize SDL(%s) - exiting\n",
+                SDL_GetError());
+        exit(1);
+    }
+    /* Load a 32x32x4 image. White pixels are transparent. */
+    filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, "qemu-icon.bmp");
+    if (filename) {
+        SDL_Surface *image = SDL_LoadBMP(filename);
+        if (image) {
+            uint32_t colorkey = SDL_MapRGB(image->format, 255, 255, 255);
+            SDL_SetColorKey(image, SDL_TRUE, colorkey);
+            SDL_SetWindowIcon(real_window, image);
+        }
+        g_free(filename);
+    }
+
+    if (full_screen) {
+        gui_fullscreen = 1;
+        sdl_grab_start();
+    }
+
+    dcl = g_malloc0(sizeof(DisplayChangeListener));
+    dcl->ops = &dcl_ops;
+    register_displaychangelistener(dcl);
+
+    mouse_mode_notifier.notify = sdl_mouse_mode_change;
+    qemu_add_mouse_mode_change_notifier(&mouse_mode_notifier);
+
+    sdl_update_caption();
+    gui_grab = 0;
+
+    sdl_cursor_hidden = SDL_CreateCursor(&data, &data, 8, 1, 0, 0);
+    sdl_cursor_normal = SDL_GetCursor();
+
+    atexit(sdl_cleanup);
+}
+#endif
ui/sdl2_scancode_translate.h new file +260 -0
new file mode 100644
index 0000000..1bd4953
@@ -0,0 +1,260 @@
+/* generated from a back translation of the SDL2 linux_scancodes file.
+   this translates SDL2 scancodes into Linux evdev keycodes,
+   these need to be translated again to go into qemu keys. */
+static uint8_t const sdl2_scancode_to_keycode[] = {
+235,
+0,
+0,
+0,
+30,
+48,
+46,
+32,
+18,
+33,
+34,
+35,
+23,
+36,
+37,
+38,
+50,
+49,
+24,
+25,
+16,
+19,
+31,
+20,
+22,
+47,
+17,
+45,
+21,
+44,
+2,
+3,
+4,
+5,
+6,
+7,
+8,
+9,
+10,
+11,
+28,
+1,
+14,
+15,
+57,
+12,
+13,
+26,
+27,
+43,
+0,
+39,
+40,
+41,
+51,
+52,
+53,
+58,
+59,
+60,
+61,
+62,
+63,
+64,
+65,
+66,
+67,
+68,
+87,
+88,
+0,
+70,
+119,
+110,
+102,
+104,
+111,
+107,
+109,
+106,
+105,
+108,
+103,
+69,
+98,
+55,
+74,
+78,
+96,
+79,
+80,
+81,
+75,
+76,
+77,
+71,
+72,
+73,
+82,
+83,
+86,
+0,
+116,
+117,
+183,
+184,
+185,
+186,
+187,
+188,
+189,
+190,
+191,
+192,
+193,
+194,
+0,
+138,
+139,
+0,
+128,
+129,
+131,
+137,
+133,
+135,
+136,
+113,
+115,
+114,
+0,
+0,
+0,
+121,
+0,
+89,
+93,
+124,
+92,
+95,
+0,
+0,
+0,
+0,
+122,
+123,
+90,
+91,
+0,
+0,
+0,
+0,
+0,
+221,
+99,
+222,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+179,
+180,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+118,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+29,
+42,
+56,
+125,
+97,
+54,
+100,
+126,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+0,
+};
ui/sdl_keysym.h +2 -2
index ee90480..599d9fc 100644
@@ -200,6 +200,7 @@ static const name2keysym_t name2keysym[]={
 { "yacute",               0x0fd},
 { "thorn",                0x0fe},
 { "ydiaeresis",           0x0ff},
+#if SDL_MAJOR_VERSION == 1
 {"EuroSign", SDLK_EURO},
 
     /* modifiers */
@@ -272,6 +273,6 @@ static const name2keysym_t name2keysym[]={
 {"Num_Lock", SDLK_NUMLOCK},
 {"Pause", SDLK_PAUSE},
 {"Escape", SDLK_ESCAPE},
-
+#endif
 {NULL, 0},
 };
--

1.8.3.1

#2 lore raw
Quoted 25 lines · Anthony Liguori
Quoted 17 lines · Dave Airlie

From: Dave Airlie <[email protected]>

I've ported the SDL1.2 code over, and rewritten it to use the SDL2 interface.

The biggest changes were in the input handling, where SDL2 has done a major overhaul, and I've had to include a generated translation file to get from SDL2 codes back to qemu compatible ones. I'm still not sure how the keyboard layout code works in qemu, so there may be further work if someone can point me a test case that works with SDL1.2 and doesn't with SDL2.

Some SDL env vars we used to set are no longer used by SDL2, Windows, OSX support is untested,

I don't think we can link to SDL1.2 and SDL2 at the same time, so I felt using --with-sdlabi=2.0 to select the new code should be fine, like how gtk does it.

Quoted 6 lines
---
 configure                    |  23 +-
 ui/Makefile.objs             |   4 +-
 ui/sdl.c                     |   3 +
 ui/sdl2.c                    | 889 +++++++++++++++++++++++++++++++++++++++++++
Quoted 6 lines

Can we refactor this to not duplicate everything and instead have function hooks or even #ifdefs for the things that are different? We try to guess the right SDL to use in configure. See how we handle GTK2 vs. GTK3.

It's very hard to review ATM due to the split.

No I talked to enough people at kvmforum and everyone said I should split this into a separate file, please don't make me undo that now, I originally did it with ifdefs and just spent a few days redoing it the other way!

Quoted 4 lines

Regarding the keycodes, danpb has a great write up on his blog:

https://www.berrange.com/posts/2010/07/04/a-summary-of-scan-code-key-codes-sets-used-in-the-pc-virtualization-stack/

Okay I'll read that later,

Quoted 313 lines

Internally, we use a variant of raw XT scancodes. We have a keymapping routine that translates from a symbolic key (i.e. "capital A") to the appropriate XT scancode.

SDL 1.x at least lets you get at raw X11 scancodes which are either evdev or PS/2 codes depending on the version of X11. So for SDL 1.x we have two translations mechanisms 1) X11 scancodes to XT scancodes and 2) SDL keysyms to internal QEMU keysyms.

From what I can tell, SDL2 has moved from just passing through raw X11 scancodes (which like I said, can be different depending on your X configuration) to having an intermediate translation layer. See comments inline:

Quoted 282 lines
 ui/sdl2_scancode_translate.h | 260 +++++++++++++
 ui/sdl_keysym.h              |   3 +-
 6 files changed, 1175 insertions(+), 7 deletions(-)
 create mode 100644 ui/sdl2.c
 create mode 100644 ui/sdl2_scancode_translate.h
configure +19 -4
index 9addff1..bf3be37 100755
@@ -158,6 +158,7 @@ docs=""
 fdt=""
 pixman=""
 sdl=""
+sdlabi="1.2"
 virtfs=""
 vnc="yes"
 sparse="no"
@@ -310,6 +311,7 @@ query_pkg_config() {
 }
 pkg_config=query_pkg_config
 sdl_config="${SDL_CONFIG-${cross_prefix}sdl-config}"
+sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 
 # default flags for all hosts
 QEMU_CFLAGS="-fno-strict-aliasing $QEMU_CFLAGS"
@@ -710,6 +712,8 @@ for opt do
   ;;
   --enable-sdl) sdl="yes"
   ;;
+  --with-sdlabi=*) sdlabi="$optarg"
+  ;;
   --disable-qom-cast-debug) qom_cast_debug="no"
   ;;
   --enable-qom-cast-debug) qom_cast_debug="yes"
@@ -1092,6 +1096,7 @@ echo "  --disable-strip          disable stripping binaries"
 echo "  --disable-werror         disable compilation abort on warning"
 echo "  --disable-sdl            disable SDL"
 echo "  --enable-sdl             enable SDL"
+echo "  --with-sdlabi            select preferred SDL ABI 1.2 or 2.0"
 echo "  --disable-gtk            disable gtk UI"
 echo "  --enable-gtk             enable gtk UI"
 echo "  --disable-virtfs         disable VirtFS"
@@ -1751,12 +1756,22 @@ fi
 
 # Look for sdl configuration program (pkg-config or sdl-config).  Try
 # sdl-config even without cross prefix, and favour pkg-config over sdl-config.
-if test "`basename $sdl_config`" != sdl-config && ! has ${sdl_config}; then
-  sdl_config=sdl-config
+
+if test $sdlabi == "2.0"; then
+    sdl_config=$sdl2_config
+    sdlname=sdl2
+    sdlconfigname=sdl2_config
+else
+    sdlname=sdl
+    sdlconfigname=sdl_config
+fi
+
+if test "`basename $sdl_config`" != $sdlconfigname && ! has ${sdl_config}; then
+  sdl_config=$sdlconfigname
 fi
 
-if $pkg_config sdl --exists; then
-  sdlconfig="$pkg_config sdl"
+if $pkg_config $sdlname --exists; then
+  sdlconfig="$pkg_config $sdlname"
   _sdlversion=`$sdlconfig --modversion 2>/dev/null | sed 's/[^0-9]//g'`
 elif has ${sdl_config}; then
   sdlconfig="$sdl_config"
ui/Makefile.objs +2 -2
index f33be47..721ad37 100644
@@ -9,12 +9,12 @@ vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o console.o cursor.o input.o qemu-pixman.o
 common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o spice-display.o
-common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o
+common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o sdl2.o
 common-obj-$(CONFIG_COCOA) += cocoa.o
 common-obj-$(CONFIG_CURSES) += curses.o
 common-obj-$(CONFIG_VNC) += $(vnc-obj-y)
 common-obj-$(CONFIG_GTK) += gtk.o x_keymap.o
 
-$(obj)/sdl.o $(obj)/sdl_zoom.o: QEMU_CFLAGS += $(SDL_CFLAGS)
+$(obj)/sdl.o $(obj)/sdl_zoom.o $(obj)/sdl2.o: QEMU_CFLAGS += $(SDL_CFLAGS)
 
 $(obj)/gtk.o: QEMU_CFLAGS += $(GTK_CFLAGS) $(VTE_CFLAGS)
ui/sdl.c +3 -0
index 9d8583c..736bb95 100644
@@ -26,6 +26,8 @@
 #undef WIN32_LEAN_AND_MEAN
 
 #include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 1
 #include <SDL_syswm.h>
 
 #include "qemu-common.h"
@@ -966,3 +968,4 @@ void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
 
     atexit(sdl_cleanup);
 }
+#endif
ui/sdl2.c new file +169 -0
new file mode 100644
index 0000000..1a71f59
@@ -0,0 +1,889 @@
+/*
+ * QEMU SDL display driver
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+/* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
+
+/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
+#undef WIN32_LEAN_AND_MEAN
+
+#include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 2
+#include <SDL_syswm.h>
+
+#include "qemu-common.h"
+#include "ui/console.h"
+#include "sysemu/sysemu.h"
+#include "x_keymap.h"
+#include "sdl_zoom.h"
+
+#include "sdl2_scancode_translate.h"
+
+static DisplayChangeListener *dcl;
+static DisplaySurface *surface;
+static SDL_Window *real_window;
+static SDL_Renderer *real_renderer;
+static SDL_Texture *guest_texture;
+
+static int gui_grab; /* if true, all keyboard/mouse events are grabbed */
+static int last_vm_running;
+static bool gui_saved_scaling;
+static int gui_saved_width;
+static int gui_saved_height;
+static int gui_saved_grab;
+static int gui_fullscreen;
+static int gui_noframe;
+static int gui_key_modifier_pressed;
+static int gui_keysym;
+static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
+static uint8_t modifiers_state[256];
+static SDL_Cursor *sdl_cursor_normal;
+static SDL_Cursor *sdl_cursor_hidden;
+static int absolute_enabled = 0;
+static int guest_cursor = 0;
+static int guest_x, guest_y;
+static SDL_Cursor *guest_sprite = NULL;
+static int scaling_active = 0;
+static Notifier mouse_mode_notifier;
+
+static void sdl_update_caption(void);
+
+static void sdl_update(DisplayChangeListener *dcl,
+                       int x, int y, int w, int h)
+{
+    if (!surface)
+        return;
+
+    SDL_UpdateTexture(guest_texture, NULL, surface_data(surface),
+                      surface_stride(surface));
+    SDL_RenderCopy(real_renderer, guest_texture, NULL, NULL);
+    SDL_RenderPresent(real_renderer);
+}
+
+static void do_sdl_resize(int width, int height, int bpp)
+{
+    int flags;
+
+    if (real_window) {
+        SDL_SetWindowSize(real_window, width, height);
+        SDL_RenderSetLogicalSize(real_renderer, width, height);
+    } else {
+        flags = 0;
+        if (gui_fullscreen)
+            flags |= SDL_WINDOW_FULLSCREEN;
+        else
+            flags |= SDL_WINDOW_RESIZABLE;
+
+        real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
+                                       SDL_WINDOWPOS_UNDEFINED,
+                                       width, height, flags);
+        real_renderer = SDL_CreateRenderer(real_window, -1, 0);
+        sdl_update_caption();
+    }
+}
+
+static void sdl_switch(DisplayChangeListener *dcl,
+                       DisplaySurface *new_surface)
+{
+    int format = 0;
+    /* temporary hack: allows to call sdl_switch to handle scaling changes */
+    if (new_surface) {
+        surface = new_surface;
+    }
+
+    if (!scaling_active) {
+        do_sdl_resize(surface_width(surface), surface_height(surface), 0);
+    } else
+        do_sdl_resize(surface_width(surface), surface_height(surface), 0);
+
+    if (guest_texture)
+        SDL_DestroyTexture(guest_texture);
+
+    if (surface_bits_per_pixel(surface) == 16)
+        format = SDL_PIXELFORMAT_RGB565;
+    else if (surface_bits_per_pixel(surface) == 32)
+        format = SDL_PIXELFORMAT_ARGB8888;
+
+    if (!format)
+        exit(1);
+    guest_texture = SDL_CreateTexture(real_renderer, format,
+                                      SDL_TEXTUREACCESS_STREAMING,
+                                      surface_width(surface), surface_height(surface));
+    SDL_RenderSetLogicalSize(real_renderer, surface_width(surface), surface_height(surface));
+}
+
+/* generic keyboard conversion */
+
+#include "sdl_keysym.h"
+
+static kbd_layout_t *kbd_layout = NULL;
+
+static uint8_t sdl_keyevent_to_keycode_generic(const SDL_KeyboardEvent *ev)
+{
+    int keysym;
+    /* workaround for X11+SDL bug with AltGR */
+    keysym = ev->keysym.sym;
+    if (keysym == 0 && ev->keysym.scancode == 113)
+        keysym = SDLK_MODE;
+    /* For Japanese key '\' and '|' */
+    if (keysym == 92 && ev->keysym.scancode == 133) {
+        keysym = 0xa5;
+    }
+    return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
+}
+
+/* specific keyboard conversions from scan codes */
+
+#if defined(_WIN32)
+
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+    keycode = sdl2_scancode_to_keycode[keycode];
+    return keycode;
+}
+
+#else
+

ui/sdl2.c line 179

Quoted 10 lines
ui/sdl2.c new file +10 -0
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+
+    keycode = sdl2_scancode_to_keycode[keycode];
+    if (keycode >= 89 && keycode < 150) {
+        keycode = translate_evdev_keycode(keycode - 89);
+    }

This doesn't seem right to me. It just so happened that the low value scan codes are the same for both X11, evdev, and SDL 1.x. That's why we only used the evdev/x11 translation tables for higher codes. It was just a table size optimization.

Quoted 2 lines

Presumably, sdl2 doesn't do this though, or does it? If it does, don't we need to probe for evdev instead of assuming it's there?

The table I wrote translates to evdev codes, then we use the evdev translator to translate to qemu ones, so its perfectly fine as is, SDL2 hides everything in its own table.

Dave.

#3 lore raw

Il 11/11/2013 10:10, Dave Airlie ha scritto:

Quoted 6 lines
Quoted 1 line
Quoted 1 line

It's very hard to review ATM due to the split.

No I talked to enough people at kvmforum and everyone said I should split this into a separate file, please don't make me undo that now, I originally did it with ifdefs and just spent a few days redoing it the other way!

You can try using options for git format-patch like -C10 --find-copies-harder.

Paolo

#4 lore raw
In reply to Dave Airlie
Quoted 2 lines · Dave Airlie

On Mon, Nov 11, 2013 at 2:02 PM, Anthony Liguori <[email protected]>

Quoted 4 lines · wrote:
Quoted 4 lines · Dave Airlie
Quoted 3 lines

From: Dave Airlie <[email protected]>

I've ported the SDL1.2 code over, and rewritten it to use the SDL2

interface.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

The biggest changes were in the input handling, where SDL2 has done a

major

Quoted 1 line
Quoted 1 line
Quoted 1 line

overhaul, and I've had to include a generated translation file to get

from

Quoted 1 line
Quoted 1 line
Quoted 1 line

SDL2 codes back to qemu compatible ones. I'm still not sure how the

keyboard

Quoted 1 line
Quoted 1 line
Quoted 1 line

layout code works in qemu, so there may be further work if someone can

point

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines

me a test case that works with SDL1.2 and doesn't with SDL2.

Some SDL env vars we used to set are no longer used by SDL2, Windows, OSX support is untested,

I don't think we can link to SDL1.2 and SDL2 at the same time, so I

felt

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines

using --with-sdlabi=2.0 to select the new code should be fine, like how gtk does it.

---
 configure                    |  23 +-
 ui/Makefile.objs             |   4 +-
 ui/sdl.c                     |   3 +
 ui/sdl2.c                    | 889

+++++++++++++++++++++++++++++++++++++++++++

Quoted 6 lines
Quoted 6 lines

Can we refactor this to not duplicate everything and instead have function hooks or even #ifdefs for the things that are different? We try to guess the right SDL to use in configure. See how we handle GTK2 vs. GTK3.

Quoted 6 lines
Quoted 1 line

It's very hard to review ATM due to the split.

No I talked to enough people at kvmforum and everyone said I should split this into a separate file, please don't make me undo that now, I originally did it with ifdefs and just spent a few days redoing it the other way!

Perhaps whoever you spoke with should speal up then. Forking sdl.c seems like a pretty bad idea to me.

Quoted 5 lines
Quoted 5 lines

Regarding the keycodes, danpb has a great write up on his blog:

https://www.berrange.com/posts/2010/07/04/a-summary-of-scan-code-key-codes-sets-used-in-the-pc-virtualization-stack/

Quoted 42 lines

Okay I'll read that later,

Quoted 39 lines

Internally, we use a variant of raw XT scancodes. We have a keymapping routine that translates from a symbolic key (i.e. "capital A") to the appropriate XT scancode.

SDL 1.x at least lets you get at raw X11 scancodes which are either evdev or PS/2 codes depending on the version of X11. So for SDL 1.x we have two translations mechanisms 1) X11 scancodes to XT scancodes and 2) SDL keysyms to internal QEMU keysyms.

From what I can tell, SDL2 has moved from just passing through raw X11 scancodes (which like I said, can be different depending on your X configuration) to having an intermediate translation layer. See comments inline:

Quoted 24 lines
 ui/sdl2_scancode_translate.h | 260 +++++++++++++
 ui/sdl_keysym.h              |   3 +-
 6 files changed, 1175 insertions(+), 7 deletions(-)
 create mode 100644 ui/sdl2.c
 create mode 100644 ui/sdl2_scancode_translate.h
configure +2 -0
index 9addff1..bf3be37 100755
@@ -158,6 +158,7 @@ docs=""
 fdt=""
 pixman=""
 sdl=""
+sdlabi="1.2"
 virtfs=""
 vnc="yes"
 sparse="no"
@@ -310,6 +311,7 @@ query_pkg_config() {
 }
 pkg_config=query_pkg_config
 sdl_config="${SDL_CONFIG-${cross_prefix}sdl-config}"
+sdl2_config="${SDL2_CONFIG-${cross_prefix}sdl2-config}"
 
Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
configure +0 -0
 # default flags for all hosts
 QEMU_CFLAGS="-fno-strict-aliasing $QEMU_CFLAGS"
@@ -710,6 +712,8 @@ for opt do
   ;;
Quoted 8 lines

configure echo " --disable-strip disable

Quoted 8 lines
configure +2 -0
   --enable-sdl) sdl="yes"
   ;;
+  --with-sdlabi=*) sdlabi="$optarg"
+  ;;
   --disable-qom-cast-debug) qom_cast_debug="no"
   ;;
   --enable-qom-cast-debug) qom_cast_debug="yes"
@@ -1092,6 +1096,7 @@ echo "  --disable-strip          disable

stripping binaries"

Quoted 11 lines
Quoted 11 lines

configure fi · line 1758

Quoted 11 lines
configure +1 -0
 echo "  --disable-werror         disable compilation abort on warning"
 echo "  --disable-sdl            disable SDL"
 echo "  --enable-sdl             enable SDL"
+echo "  --with-sdlabi            select preferred SDL ABI 1.2 or 2.0"
 echo "  --disable-gtk            disable gtk UI"
 echo "  --enable-gtk             enable gtk UI"
 echo "  --disable-virtfs         disable VirtFS"
@@ -1751,12 +1756,22 @@ fi
 
 # Look for sdl configuration program (pkg-config or sdl-config).  Try
 # sdl-config even without cross prefix, and favour pkg-config over

sdl-config.

Quoted 1 line
Quoted 1 line

configure fi · line 1754

Quoted 1 line
configure +0 -1
-if test "`basename $sdl_config`" != sdl-config && ! has

${sdl_config}; then

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
configure +3 -1
-  sdl_config=sdl-config
+
+if test $sdlabi == "2.0"; then
+    sdl_config=$sdl2_config
Quoted 8 lines
Quoted 8 lines

configure fi

Quoted 8 lines
configure +8 -0
+    sdlname=sdl2
+    sdlconfigname=sdl2_config
+else
+    sdlname=sdl
+    sdlconfigname=sdl_config
+fi
+
+if test "`basename $sdl_config`" != $sdlconfigname && ! has

${sdl_config}; then

Quoted 8 lines
Quoted 8 lines

configure fi

Quoted 8 lines
configure +3 -2
+  sdl_config=$sdlconfigname
 fi
 
-if $pkg_config sdl --exists; then
-  sdlconfig="$pkg_config sdl"
+if $pkg_config $sdlname --exists; then
+  sdlconfig="$pkg_config $sdlname"
   _sdlversion=`$sdlconfig --modversion 2>/dev/null | sed

's/[^0-9]//g'`

Quoted 10 lines
Quoted 10 lines

ui/Makefile.objs vnc-obj-y += vnc-jobs.o · line 11

Quoted 10 lines
configure +0 -0
 elif has ${sdl_config}; then
   sdlconfig="$sdl_config"
ui/Makefile.objs +0 -0
index f33be47..721ad37 100644
@@ -9,12 +9,12 @@ vnc-obj-y += vnc-jobs.o
 
 common-obj-y += keymaps.o console.o cursor.o input.o qemu-pixman.o
 common-obj-$(CONFIG_SPICE) += spice-core.o spice-input.o

spice-display.o

Quoted 9 lines
Quoted 9 lines

ui/Makefile.objs vnc-obj-y += vnc-jobs.o

Quoted 9 lines
ui/Makefile.objs +2 -2
-common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o
+common-obj-$(CONFIG_SDL) += sdl.o sdl_zoom.o x_keymap.o sdl2.o
 common-obj-$(CONFIG_COCOA) += cocoa.o
 common-obj-$(CONFIG_CURSES) += curses.o
 common-obj-$(CONFIG_VNC) += $(vnc-obj-y)
 common-obj-$(CONFIG_GTK) += gtk.o x_keymap.o
 
-$(obj)/sdl.o $(obj)/sdl_zoom.o: QEMU_CFLAGS += $(SDL_CFLAGS)
+$(obj)/sdl.o $(obj)/sdl_zoom.o $(obj)/sdl2.o: QEMU_CFLAGS +=

$(SDL_CFLAGS)

Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
ui/Makefile.objs +0 -0
 $(obj)/gtk.o: QEMU_CFLAGS += $(GTK_CFLAGS) $(VTE_CFLAGS)
ui/sdl.c +0 -0
index 9d8583c..736bb95 100644
@@ -26,6 +26,8 @@
 #undef WIN32_LEAN_AND_MEAN
 
Quoted 7 lines
Quoted 7 lines

ui/sdl.c void sdl_display_init(DisplayState *ds, int

Quoted 7 lines
ui/sdl.c +2 -0
 #include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 1
 #include <SDL_syswm.h>
 
 #include "qemu-common.h"
@@ -966,3 +968,4 @@ void sdl_display_init(DisplayState *ds, int

full_screen, int no_frame)

Quoted 16 lines
Quoted 16 lines
Quoted 10 lines
ui/sdl.c +1 -0
     atexit(sdl_cleanup);
 }
+#endif
ui/sdl2.c new file +0 -0
new file mode 100644
index 0000000..1a71f59

ui/sdl2.c line 6

Quoted 6 lines
ui/sdl2.c new file +6 -0
@@ -0,0 +1,889 @@
+/*
+ * QEMU SDL display driver
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person

obtaining a copy

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 7

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * of this software and associated documentation files (the

"Software"), to deal

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 8

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * in the Software without restriction, including without limitation

the rights

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 9

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * to use, copy, modify, merge, publish, distribute, sublicense,

and/or sell

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 10

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * copies of the Software, and to permit persons to whom the Software

is

Quoted 3 lines
Quoted 3 lines

ui/sdl2.c

Quoted 3 lines
ui/sdl2.c new file +3 -0
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be

included in

Quoted 3 lines
Quoted 3 lines

ui/sdl2.c

Quoted 3 lines
ui/sdl2.c new file +3 -0
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,

EXPRESS OR

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 17

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF

MERCHANTABILITY,

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 18

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT

SHALL

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 19

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES

OR OTHER

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 20

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

ARISING FROM,

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 21

Quoted 1 line
ui/sdl2.c new file +1 -0
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER

DEALINGS IN

Quoted 5 lines
Quoted 5 lines

ui/sdl2.c

Quoted 5 lines
ui/sdl2.c new file +5 -0
+ * THE SOFTWARE.
+ */
+/* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
+
+/* Avoid compiler warning because macro is redefined in SDL_syswm.h.

*/

Quoted 14 lines
Quoted 14 lines
Quoted 14 lines
ui/sdl2.c new file +14 -0
+#undef WIN32_LEAN_AND_MEAN
+
+#include <SDL.h>
+
+#if SDL_MAJOR_VERSION == 2
+#include <SDL_syswm.h>
+
+#include "qemu-common.h"
+#include "ui/console.h"
+#include "sysemu/sysemu.h"
+#include "x_keymap.h"
+#include "sdl_zoom.h"
+
+#include "sdl2_scancode_translate.h"
Quoted 8 lines
Quoted 8 lines

ui/sdl2.c

Quoted 8 lines
ui/sdl2.c new file +8 -0
+
+static DisplayChangeListener *dcl;
+static DisplaySurface *surface;
+static SDL_Window *real_window;
+static SDL_Renderer *real_renderer;
+static SDL_Texture *guest_texture;
+
+static int gui_grab; /* if true, all keyboard/mouse events are

grabbed */

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines
ui/sdl2.c new file +53 -0
+static int last_vm_running;
+static bool gui_saved_scaling;
+static int gui_saved_width;
+static int gui_saved_height;
+static int gui_saved_grab;
+static int gui_fullscreen;
+static int gui_noframe;
+static int gui_key_modifier_pressed;
+static int gui_keysym;
+static int gui_grab_code = KMOD_LALT | KMOD_LCTRL;
+static uint8_t modifiers_state[256];
+static SDL_Cursor *sdl_cursor_normal;
+static SDL_Cursor *sdl_cursor_hidden;
+static int absolute_enabled = 0;
+static int guest_cursor = 0;
+static int guest_x, guest_y;
+static SDL_Cursor *guest_sprite = NULL;
+static int scaling_active = 0;
+static Notifier mouse_mode_notifier;
+
+static void sdl_update_caption(void);
+
+static void sdl_update(DisplayChangeListener *dcl,
+                       int x, int y, int w, int h)
+{
+    if (!surface)
+        return;
+
+    SDL_UpdateTexture(guest_texture, NULL, surface_data(surface),
+                      surface_stride(surface));
+    SDL_RenderCopy(real_renderer, guest_texture, NULL, NULL);
+    SDL_RenderPresent(real_renderer);
+}
+
+static void do_sdl_resize(int width, int height, int bpp)
+{
+    int flags;
+
+    if (real_window) {
+        SDL_SetWindowSize(real_window, width, height);
+        SDL_RenderSetLogicalSize(real_renderer, width, height);
+    } else {
+        flags = 0;
+        if (gui_fullscreen)
+            flags |= SDL_WINDOW_FULLSCREEN;
+        else
+            flags |= SDL_WINDOW_RESIZABLE;
+
+        real_window = SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED,
+                                       SDL_WINDOWPOS_UNDEFINED,
+                                       width, height, flags);
+        real_renderer = SDL_CreateRenderer(real_window, -1, 0);
+        sdl_update_caption();
Quoted 8 lines
Quoted 8 lines

ui/sdl2.c

Quoted 8 lines
ui/sdl2.c new file +8 -0
+    }
+}
+
+static void sdl_switch(DisplayChangeListener *dcl,
+                       DisplaySurface *new_surface)
+{
+    int format = 0;
+    /* temporary hack: allows to call sdl_switch to handle scaling

changes */

Quoted 6 lines
Quoted 6 lines

ui/sdl2.c

Quoted 6 lines
ui/sdl2.c new file +6 -0
+    if (new_surface) {
+        surface = new_surface;
+    }
+
+    if (!scaling_active) {
+        do_sdl_resize(surface_width(surface),

surface_height(surface), 0);

Quoted 2 lines
Quoted 2 lines

ui/sdl2.c line 117

Quoted 2 lines
ui/sdl2.c new file +2 -0
+    } else
+        do_sdl_resize(surface_width(surface),

surface_height(surface), 0);

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
ui/sdl2.c new file +6 -0
+
+    if (guest_texture)
+        SDL_DestroyTexture(guest_texture);
+
+    if (surface_bits_per_pixel(surface) == 16)
+        format = SDL_PIXELFORMAT_RGB565;
Quoted 8 lines
Quoted 8 lines

ui/sdl2.c

Quoted 8 lines
ui/sdl2.c new file +8 -0
+    else if (surface_bits_per_pixel(surface) == 32)
+        format = SDL_PIXELFORMAT_ARGB8888;
+
+    if (!format)
+        exit(1);
+    guest_texture = SDL_CreateTexture(real_renderer, format,
+                                      SDL_TEXTUREACCESS_STREAMING,
+                                      surface_width(surface),

surface_height(surface));

Quoted 1 line
Quoted 1 line

ui/sdl2.c line 132

Quoted 1 line
ui/sdl2.c new file +1 -0
+    SDL_RenderSetLogicalSize(real_renderer, surface_width(surface),

surface_height(surface));

Quoted 9 lines
Quoted 9 lines

ui/sdl2.c

Quoted 9 lines
ui/sdl2.c new file +9 -0
+}
+
+/* generic keyboard conversion */
+
+#include "sdl_keysym.h"
+
+static kbd_layout_t *kbd_layout = NULL;
+
+static uint8_t sdl_keyevent_to_keycode_generic(const

SDL_KeyboardEvent *ev)

Quoted 44 lines
Quoted 44 lines
Quoted 28 lines
ui/sdl2.c new file +28 -0
+{
+    int keysym;
+    /* workaround for X11+SDL bug with AltGR */
+    keysym = ev->keysym.sym;
+    if (keysym == 0 && ev->keysym.scancode == 113)
+        keysym = SDLK_MODE;
+    /* For Japanese key '\' and '|' */
+    if (keysym == 92 && ev->keysym.scancode == 133) {
+        keysym = 0xa5;
+    }
+    return keysym2scancode(kbd_layout, keysym) & SCANCODE_KEYMASK;
+}
+
+/* specific keyboard conversions from scan codes */
+
+#if defined(_WIN32)
+
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+    keycode = sdl2_scancode_to_keycode[keycode];
+    return keycode;
+}
+
+#else
+

ui/sdl2.c line 179

Quoted 10 lines
ui/sdl2.c new file +10 -0
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+
+    keycode = sdl2_scancode_to_keycode[keycode];
+    if (keycode >= 89 && keycode < 150) {
+        keycode = translate_evdev_keycode(keycode - 89);
+    }

This doesn't seem right to me. It just so happened that the low value scan codes are the same for both X11, evdev, and SDL 1.x. That's why we only used the evdev/x11 translation tables for higher codes. It was just a table size optimization.

Quoted 6 lines
Quoted 2 lines

Presumably, sdl2 doesn't do this though, or does it? If it does, don't we need to probe for evdev instead of assuming it's there?

The table I wrote translates to evdev codes, then we use the evdev translator to translate to qemu ones, so its perfectly fine as is, SDL2 hides everything in its own table.

Why not translate to XT directly? The intermediate evdev step seems unnecessary.

Regards,

Anthony Liguori

Quoted 2 lines

Dave.

#5 lore raw
Quoted 44 lines · Anthony Liguori
Quoted 42 lines · Dave Airlie
Quoted 38 lines · Anthony Liguori
Quoted 23 lines · Dave Airlie

From: Dave Airlie <[email protected]>

I've ported the SDL1.2 code over, and rewritten it to use the SDL2 interface.

The biggest changes were in the input handling, where SDL2 has done a major
overhaul, and I've had to include a generated translation file to get from
SDL2 codes back to qemu compatible ones. I'm still not sure how the keyboard
layout code works in qemu, so there may be further work if someone can point
me a test case that works with SDL1.2 and doesn't with SDL2.

Some SDL env vars we used to set are no longer used by SDL2, Windows, OSX support is untested,

I don't think we can link to SDL1.2 and SDL2 at the same time, so I felt
using --with-sdlabi=2.0 to select the new code should be fine, like how gtk does it.

Quoted 7 lines
---
 configure                    |  23 +-
 ui/Makefile.objs             |   4 +-
 ui/sdl.c                     |   3 +
 ui/sdl2.c                    | 889

+++++++++++++++++++++++++++++++++++++++++++

Can we refactor this to not duplicate everything and instead have function hooks or even #ifdefs for the things that are different? We try to guess the right SDL to use in configure. See how we handle GTK2 vs. GTK3.

It's very hard to review ATM due to the split.

Quoted 7 lines
Quoted 4 lines

No I talked to enough people at kvmforum and everyone said I should split this into a separate file, please don't make me undo that now, I originally did it with ifdefs and just spent a few days redoing it the other way!

Perhaps whoever you spoke with should speal up then. Forking sdl.c seems like a pretty bad idea to me.

It does right now, but since I can't add multi-head support to SDL1.2 it'll rapidly start diverging in order to add features that SDL1.2 can't support.

btw here is my old sdl.c implementation its pretty ugly, later commit in that branch have the input bits.

http://cgit.freedesktop.org/~airlied/qemu/commit/?h=virtio-gpu&id=ee44399a3dbce8da810329230f0a439a3b88cd67

As I said I suspect this will just get uglier as time goes on, as we add SDL2 only features.

Quoted 29 lines
Quoted 29 lines
Quoted 7 lines

Regarding the keycodes, danpb has a great write up on his blog:

https://www.berrange.com/posts/2010/07/04/a-summary-of-scan-code-key-codes-sets-used-in-the-pc-virtualization-stack/

Okay I'll read that later,

Quoted 18 lines

ui/sdl2.c line 179

Quoted 10 lines
ui/sdl2.c new file +10 -0
+static uint8_t sdl_keyevent_to_keycode(const SDL_KeyboardEvent *ev)
+{
+    int keycode;
+
+    keycode = ev->keysym.scancode;
+
+    keycode = sdl2_scancode_to_keycode[keycode];
+    if (keycode >= 89 && keycode < 150) {
+        keycode = translate_evdev_keycode(keycode - 89);
+    }

This doesn't seem right to me. It just so happened that the low value scan codes are the same for both X11, evdev, and SDL 1.x. That's why we only used the evdev/x11 translation tables for higher codes. It was just a table size optimization.

Presumably, sdl2 doesn't do this though, or does it? If it does, don't we need to probe for evdev instead of assuming it's there?

Quoted 7 lines
Quoted 3 lines

The table I wrote translates to evdev codes, then we use the evdev translator to translate to qemu ones, so its perfectly fine as is, SDL2 hides everything in its own table.

Why not translate to XT directly? The intermediate evdev step seems unnecessary.

Correctness and future maintainability,
there exists a table in SDL2 source code going from evdev->SDL2, I wrote trivial code to reverse the contents of that table so I know its correct, if it changes in future I can just do the same simple reversal and import it. Though there is probably nothing stopping me adding the extra step at that stage I wasn't sure it was a good idea going forward.

Dave.