Patchwork gtk: Release modifier when graphic console loses keyboard focus

login
register
mail settings
Submitter Jan Kiszka
Date Feb. 24, 2013, 10:46 a.m.
Message ID <5129EF92.4010904@web.de>
Download mbox | patch
Permalink /patch/222764/
State New
Headers show

Comments

Jan Kiszka - Feb. 24, 2013, 10:46 a.m.
From: Jan Kiszka <jan.kiszka@siemens.com>

This solves, e.g., sticky ALT when selecting a GTK menu, switching to a
different window or selecting a different virtual console.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
 ui/gtk.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 50 insertions(+), 2 deletions(-)


@@ -553,8 +581,10 @@ static gboolean gd_button_event(GtkWidget *widget,
GdkEventButton *button,

 static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void
*opaque)
 {
+    GtkDisplayState *s = opaque;
     int gdk_keycode;
     int qemu_keycode;
+    int i;

     gdk_keycode = key->hardware_keycode;

@@ -576,6 +606,12 @@ static gboolean gd_key_event(GtkWidget *widget,
GdkEventKey *key, void *opaque)
             gdk_keycode, qemu_keycode,
             (key->type == GDK_KEY_PRESS) ? "down" : "up");

+    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
+        if (qemu_keycode == modifier_keycode[i]) {
+            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
+        }
+    }
+
     if (qemu_keycode & SCANCODE_GREY) {
         kbd_put_keycode(SCANCODE_EMUL0);
     }
@@ -631,6 +667,7 @@ static void gd_menu_switch_vc(GtkMenuItem *item,
void *opaque)
     } else {
         int i;

+        gtk_release_modifiers(s);
         for (i = 0; i < s->nb_vcs; i++) {
             if
(gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {

gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
@@ -840,6 +877,15 @@ static gboolean gd_leave_event(GtkWidget *widget,
GdkEventCrossing *crossing, gp
     return TRUE;
 }

+static gboolean gd_focus_out_event(GtkWidget *widget, GdkEventCrossing
*crossing, gpointer data)
+{
+    GtkDisplayState *s = data;
+
+    gtk_release_modifiers(s);
+
+    return TRUE;
+}
+
 /** Virtual Console Callbacks **/

 static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf,
int len)
@@ -1018,6 +1064,8 @@ static void gd_connect_signals(GtkDisplayState *s)
                      G_CALLBACK(gd_enter_event), s);
     g_signal_connect(s->drawing_area, "leave-notify-event",
                      G_CALLBACK(gd_leave_event), s);
+    g_signal_connect(s->drawing_area, "focus-out-event",
+                     G_CALLBACK(gd_focus_out_event), s);
 }

 static void gd_create_menus(GtkDisplayState *s)
Anthony Liguori - Feb. 26, 2013, 7:25 p.m.
Jan Kiszka <jan.kiszka@web.de> writes:

> From: Jan Kiszka <jan.kiszka@siemens.com>
>
> This solves, e.g., sticky ALT when selecting a GTK menu, switching to a
> different window or selecting a different virtual console.
>
> Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>

FYI, this patch is whitespace damaged.  Looks like your mailer line wrapped.

Regards,

Anthony Liguori

> ---
>  ui/gtk.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 files changed, 50 insertions(+), 2 deletions(-)
>
> diff --git a/ui/gtk.c b/ui/gtk.c
> index 7115cf6..daffc30 100644
> --- a/ui/gtk.c
> +++ b/ui/gtk.c
> @@ -74,6 +74,11 @@
>
>  #define MAX_VCS 10
>
> +static const int modifier_keycode[] = {
> +    /* shift, control, alt keys, meta keys, both left & right */
> +    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
> +};
> +
>  typedef struct VirtualConsole
>  {
>      GtkWidget *menu_item;
> @@ -133,6 +138,8 @@ typedef struct GtkDisplayState
>      gboolean free_scale;
>
>      bool external_pause_update;
> +
> +    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
>  } GtkDisplayState;
>
>  static GtkDisplayState *global_state;
> @@ -201,6 +208,26 @@ static void gd_update_caption(GtkDisplayState *s)
>      g_free(title);
>  }
>
> +static void gtk_release_modifiers(GtkDisplayState *s)
> +{
> +    int i, keycode;
> +
> +    if (!gd_on_vga(s)) {
> +        return;
> +    }
> +    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
> +        keycode = modifier_keycode[i];
> +        if (!s->modifier_pressed[i]) {
> +            continue;
> +        }
> +        if (keycode & SCANCODE_GREY) {
> +            kbd_put_keycode(SCANCODE_EMUL0);
> +        }
> +        kbd_put_keycode(keycode | SCANCODE_UP);
> +        s->modifier_pressed[i] = false;
> +    }
> +}
> +
>  /** DisplayState Callbacks **/
>
>  static void gd_update(DisplayState *ds, int x, int y, int w, int h)
> @@ -328,8 +355,9 @@ static gboolean gd_window_key_event(GtkWidget
> *widget, GdkEventKey *key, void *o
>          key->state == (GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
>          handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
>      }
> -
> -    if (!handled) {
> +    if (handled) {
> +        gtk_release_modifiers(s);
> +    } else {
>          handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
>      }
>
> @@ -553,8 +581,10 @@ static gboolean gd_button_event(GtkWidget *widget,
> GdkEventButton *button,
>
>  static gboolean gd_key_event(GtkWidget *widget, GdkEventKey *key, void
> *opaque)
>  {
> +    GtkDisplayState *s = opaque;
>      int gdk_keycode;
>      int qemu_keycode;
> +    int i;
>
>      gdk_keycode = key->hardware_keycode;
>
> @@ -576,6 +606,12 @@ static gboolean gd_key_event(GtkWidget *widget,
> GdkEventKey *key, void *opaque)
>              gdk_keycode, qemu_keycode,
>              (key->type == GDK_KEY_PRESS) ? "down" : "up");
>
> +    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
> +        if (qemu_keycode == modifier_keycode[i]) {
> +            s->modifier_pressed[i] = (key->type == GDK_KEY_PRESS);
> +        }
> +    }
> +
>      if (qemu_keycode & SCANCODE_GREY) {
>          kbd_put_keycode(SCANCODE_EMUL0);
>      }
> @@ -631,6 +667,7 @@ static void gd_menu_switch_vc(GtkMenuItem *item,
> void *opaque)
>      } else {
>          int i;
>
> +        gtk_release_modifiers(s);
>          for (i = 0; i < s->nb_vcs; i++) {
>              if
> (gtk_check_menu_item_get_active(GTK_CHECK_MENU_ITEM(s->vc[i].menu_item))) {
>
> gtk_notebook_set_current_page(GTK_NOTEBOOK(s->notebook), i + 1);
> @@ -840,6 +877,15 @@ static gboolean gd_leave_event(GtkWidget *widget,
> GdkEventCrossing *crossing, gp
>      return TRUE;
>  }
>
> +static gboolean gd_focus_out_event(GtkWidget *widget, GdkEventCrossing
> *crossing, gpointer data)
> +{
> +    GtkDisplayState *s = data;
> +
> +    gtk_release_modifiers(s);
> +
> +    return TRUE;
> +}
> +
>  /** Virtual Console Callbacks **/
>
>  static int gd_vc_chr_write(CharDriverState *chr, const uint8_t *buf,
> int len)
> @@ -1018,6 +1064,8 @@ static void gd_connect_signals(GtkDisplayState *s)
>                       G_CALLBACK(gd_enter_event), s);
>      g_signal_connect(s->drawing_area, "leave-notify-event",
>                       G_CALLBACK(gd_leave_event), s);
> +    g_signal_connect(s->drawing_area, "focus-out-event",
> +                     G_CALLBACK(gd_focus_out_event), s);
>  }
>
>  static void gd_create_menus(GtkDisplayState *s)
> -- 
> 1.7.3.4

Patch

diff --git a/ui/gtk.c b/ui/gtk.c
index 7115cf6..daffc30 100644
--- a/ui/gtk.c
+++ b/ui/gtk.c
@@ -74,6 +74,11 @@ 

 #define MAX_VCS 10

+static const int modifier_keycode[] = {
+    /* shift, control, alt keys, meta keys, both left & right */
+    0x2a, 0x36, 0x1d, 0x9d, 0x38, 0xb8, 0xdb, 0xdd,
+};
+
 typedef struct VirtualConsole
 {
     GtkWidget *menu_item;
@@ -133,6 +138,8 @@  typedef struct GtkDisplayState
     gboolean free_scale;

     bool external_pause_update;
+
+    bool modifier_pressed[ARRAY_SIZE(modifier_keycode)];
 } GtkDisplayState;

 static GtkDisplayState *global_state;
@@ -201,6 +208,26 @@  static void gd_update_caption(GtkDisplayState *s)
     g_free(title);
 }

+static void gtk_release_modifiers(GtkDisplayState *s)
+{
+    int i, keycode;
+
+    if (!gd_on_vga(s)) {
+        return;
+    }
+    for (i = 0; i < ARRAY_SIZE(modifier_keycode); i++) {
+        keycode = modifier_keycode[i];
+        if (!s->modifier_pressed[i]) {
+            continue;
+        }
+        if (keycode & SCANCODE_GREY) {
+            kbd_put_keycode(SCANCODE_EMUL0);
+        }
+        kbd_put_keycode(keycode | SCANCODE_UP);
+        s->modifier_pressed[i] = false;
+    }
+}
+
 /** DisplayState Callbacks **/

 static void gd_update(DisplayState *ds, int x, int y, int w, int h)
@@ -328,8 +355,9 @@  static gboolean gd_window_key_event(GtkWidget
*widget, GdkEventKey *key, void *o
         key->state == (GDK_CONTROL_MASK | GDK_MOD1_MASK)) {
         handled = gtk_window_activate_key(GTK_WINDOW(widget), key);
     }
-
-    if (!handled) {
+    if (handled) {
+        gtk_release_modifiers(s);
+    } else {
         handled = gtk_window_propagate_key_event(GTK_WINDOW(widget), key);
     }