[gtk/wip/otte/win32-cleanup: 7/19] win32: Clean up keymap code




commit 7fc6c2ece7bf4c70240c3a74185fcc10d2e837d3
Author: Benjamin Otte <otte redhat com>
Date:   Wed Jul 21 05:23:15 2021 +0200

    win32: Clean up keymap code
    
    1. Store the keymap in the display
    2. Don't pretend that a different keymap might be passed
    3. Remove static global default_keymap variable

 gdk/win32/gdkdisplay-win32.c |  17 +++++--
 gdk/win32/gdkdisplay-win32.h |   1 +
 gdk/win32/gdkevents-win32.c  |  10 ++--
 gdk/win32/gdkkeys-win32.c    | 113 +++++++++++--------------------------------
 gdk/win32/gdkprivate-win32.h |   3 --
 5 files changed, 46 insertions(+), 98 deletions(-)
---
diff --git a/gdk/win32/gdkdisplay-win32.c b/gdk/win32/gdkdisplay-win32.c
index 40a24b0111..a0ae3684ed 100644
--- a/gdk/win32/gdkdisplay-win32.c
+++ b/gdk/win32/gdkdisplay-win32.c
@@ -519,7 +519,6 @@ _gdk_win32_display_open (const char *display_name)
 
   gdk_win32_display_init_monitors (win32_display);
   
-  _gdk_win32_keymap_set_active_layout (GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (display)), 
_gdk_input_locale);
   _gdk_events_init (display);
 
   _gdk_input_ignore_core = 0;
@@ -529,7 +528,9 @@ _gdk_win32_display_open (const char *display_name)
   _gdk_win32_lang_notification_init ();
   _gdk_drag_init ();
   _gdk_drop_init ();
-
+  
+  win32_display->keymap = g_object_new (GDK_TYPE_WIN32_KEYMAP, NULL);
+  _gdk_win32_keymap_set_active_layout (win32_display->keymap, _gdk_input_locale);
   display->clipboard = gdk_win32_clipboard_new (display);
   display->primary_clipboard = gdk_clipboard_new (display);
 
@@ -680,6 +681,7 @@ gdk_win32_display_finalize (GObject *object)
 
   g_list_store_remove_all (G_LIST_STORE (display_win32->monitors));
   g_object_unref (display_win32->monitors);
+  g_object_unref (display_win32->keymap);
 
   while (display_win32->filters)
     _gdk_win32_message_filter_unref (display_win32, display_win32->filters->data);
@@ -1054,6 +1056,14 @@ gdk_win32_display_get_monitors (GdkDisplay *display)
   return self->monitors;
 }
 
+static GdkKeymap *
+gdk_win32_display_get_keymap (GdkDisplay *display)
+{
+  GdkWin32Display *self = GDK_WIN32_DISPLAY (display);
+
+  return self->keymap;
+}
+
 guint
 gdk_win32_display_get_monitor_scale_factor (GdkWin32Display *display_win32,
                                             GdkSurface      *surface,
@@ -1240,8 +1250,7 @@ gdk_win32_display_class_init (GdkWin32DisplayClass *klass)
   display_class->notify_startup_complete = gdk_win32_display_notify_startup_complete;
   display_class->create_surface = _gdk_win32_display_create_surface;
 
-  display_class->get_keymap = _gdk_win32_display_get_keymap;
-
+  display_class->get_keymap = gdk_win32_display_get_keymap;
   display_class->get_monitors = gdk_win32_display_get_monitors;
 
 #ifdef GDK_RENDERING_VULKAN
diff --git a/gdk/win32/gdkdisplay-win32.h b/gdk/win32/gdkdisplay-win32.h
index fc6699fc00..3839dd0b7e 100644
--- a/gdk/win32/gdkdisplay-win32.h
+++ b/gdk/win32/gdkdisplay-win32.h
@@ -137,6 +137,7 @@ struct _GdkWin32Display
   HDC hdc_egl_temp;
 #endif
 
+  GdkKeymap *keymap;
   GListModel *monitors;
 
   guint hasWglARBCreateContext : 1;
diff --git a/gdk/win32/gdkevents-win32.c b/gdk/win32/gdkevents-win32.c
index 2316032663..118344e2e8 100644
--- a/gdk/win32/gdkevents-win32.c
+++ b/gdk/win32/gdkevents-win32.c
@@ -645,7 +645,7 @@ build_key_event_state (GdkDisplay *display,
   if (key_state[VK_XBUTTON2] & 0x80)
     state |= GDK_BUTTON5_MASK;
 
-  keymap = GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (display));
+  keymap = GDK_WIN32_KEYMAP (gdk_display_get_keymap (display));
 
   if (_gdk_win32_keymap_has_altgr (keymap) &&
       (key_state[VK_LCONTROL] & 0x80) &&
@@ -673,7 +673,7 @@ get_active_group (GdkDisplay *display)
 {
   GdkWin32Keymap *keymap;
 
-  keymap = GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (display));
+  keymap = GDK_WIN32_KEYMAP (gdk_display_get_keymap (display));
 
   return _gdk_win32_keymap_get_active_group (keymap);
 }
@@ -1829,7 +1829,7 @@ gdk_event_translate (MSG *msg,
     {
     case WM_INPUTLANGCHANGE:
       _gdk_input_locale = (HKL) msg->lParam;
-      _gdk_win32_keymap_set_active_layout (GDK_WIN32_KEYMAP (_gdk_win32_display_get_keymap (display)), 
_gdk_input_locale);
+      _gdk_win32_keymap_set_active_layout (GDK_WIN32_KEYMAP (gdk_display_get_keymap (display)), 
_gdk_input_locale);
       GetLocaleInfo (MAKELCID (LOWORD (_gdk_input_locale), SORT_DEFAULT),
                     LOCALE_IDEFAULTANSICODEPAGE,
                     buf, sizeof (buf));
@@ -1960,7 +1960,7 @@ gdk_event_translate (MSG *msg,
            }
        }
       else if (msg->wParam == VK_SHIFT &&
-              LOBYTE (HIWORD (msg->lParam)) == _gdk_win32_keymap_get_rshift_scancode (GDK_WIN32_KEYMAP 
(_gdk_win32_display_get_keymap (display))))
+              LOBYTE (HIWORD (msg->lParam)) == _gdk_win32_keymap_get_rshift_scancode (GDK_WIN32_KEYMAP 
(gdk_display_get_keymap (display))))
        keycode = VK_RSHIFT;
 
       is_modifier = (msg->wParam == VK_CONTROL ||
@@ -1995,7 +1995,7 @@ gdk_event_translate (MSG *msg,
        }
       else
        {
-         gdk_keymap_translate_keyboard_state (_gdk_win32_display_get_keymap (display),
+         gdk_keymap_translate_keyboard_state (gdk_display_get_keymap (display),
                                               keycode,
                                               state,
                                               group,
diff --git a/gdk/win32/gdkkeys-win32.c b/gdk/win32/gdkkeys-win32.c
index 981f52ab54..bc4573e03c 100644
--- a/gdk/win32/gdkkeys-win32.c
+++ b/gdk/win32/gdkkeys-win32.c
@@ -180,8 +180,6 @@ G_DEFINE_TYPE (GdkWin32Keymap, gdk_win32_keymap, GDK_TYPE_KEYMAP)
 
 guint _gdk_keymap_serial = 0;
 
-static GdkKeymap *default_keymap = NULL;
-
 #define KEY_STATE_SIZE 256
 
 static void update_keymap (GdkKeymap *gdk_keymap);
@@ -1178,17 +1176,6 @@ _gdk_win32_keymap_get_active_group (GdkWin32Keymap *keymap)
   return 0;
 }
 
-GdkKeymap*
-_gdk_win32_display_get_keymap (GdkDisplay *display)
-{
-  g_return_val_if_fail (display == gdk_display_get_default (), NULL);
-
-  if (default_keymap == NULL)
-    default_keymap = g_object_new (gdk_win32_keymap_get_type (), NULL);
-
-  return default_keymap;
-}
-
 static PangoDirection
 get_hkl_direction (HKL hkl)
 {
@@ -1211,14 +1198,8 @@ get_hkl_direction (HKL hkl)
 static PangoDirection
 gdk_win32_keymap_get_direction (GdkKeymap *gdk_keymap)
 {
+  GdkWin32Keymap *keymap = GDK_WIN32_KEYMAP (gdk_keymap);
   HKL active_hkl;
-  GdkWin32Keymap *keymap;
-  GdkKeymap *default_keymap = gdk_display_get_keymap (gdk_display_get_default ());
-
-  if (gdk_keymap == NULL || gdk_keymap != default_keymap)
-    keymap = GDK_WIN32_KEYMAP (default_keymap);
-  else
-    keymap = GDK_WIN32_KEYMAP (gdk_keymap);
 
   update_keymap (GDK_KEYMAP (keymap));
 
@@ -1233,16 +1214,10 @@ gdk_win32_keymap_get_direction (GdkKeymap *gdk_keymap)
 static gboolean
 gdk_win32_keymap_have_bidi_layouts (GdkKeymap *gdk_keymap)
 {
-  GdkWin32Keymap *keymap;
-  gboolean        have_rtl = FALSE;
+  GdkWin32Keymap *keymap = GDK_WIN32_KEYMAP (gdk_keymap);
+  gboolean have_rtl = FALSE;
   gboolean        have_ltr = FALSE;
   int             group;
-  GdkKeymap      *default_keymap = gdk_display_get_keymap (gdk_display_get_default ());
-
-  if (gdk_keymap == NULL || gdk_keymap != default_keymap)
-    keymap = GDK_WIN32_KEYMAP (default_keymap);
-  else
-    keymap = GDK_WIN32_KEYMAP (gdk_keymap);
 
   update_keymap (GDK_KEYMAP (keymap));
 
@@ -1286,48 +1261,34 @@ gdk_win32_keymap_get_entries_for_keyval (GdkKeymap     *gdk_keymap,
                                          guint          keyval,
                                          GArray        *retval)
 {
-  GdkKeymap *default_keymap = gdk_display_get_keymap (gdk_display_get_default ());
+  GdkWin32Keymap *keymap = GDK_WIN32_KEYMAP (gdk_keymap);
   guint len = retval->len;
+  int vk;
 
-  g_return_val_if_fail (gdk_keymap == NULL || GDK_IS_KEYMAP (gdk_keymap), FALSE);
-  g_return_val_if_fail (keyval != 0, FALSE);
+  update_keymap (gdk_keymap);
 
-  /* Accept only the default keymap */
-  if (gdk_keymap == NULL || gdk_keymap == default_keymap)
+  for (vk = 0; vk < KEY_STATE_SIZE; vk++)
     {
-      int vk;
-      GdkWin32Keymap *keymap;
-
-      if (gdk_keymap == NULL)
-        keymap = GDK_WIN32_KEYMAP (default_keymap);
-      else
-        keymap = GDK_WIN32_KEYMAP (gdk_keymap);
-
-      update_keymap (gdk_keymap);
+      int group;
 
-      for (vk = 0; vk < KEY_STATE_SIZE; vk++)
+      for (group = 0; group < keymap->layout_handles->len; group++)
         {
-          int group;
+          GdkWin32KeyLevelState level;
 
-          for (group = 0; group < keymap->layout_handles->len; group++)
+          for (level = GDK_WIN32_LEVEL_NONE; level < GDK_WIN32_LEVEL_COUNT; level++)
             {
-              GdkWin32KeyLevelState    level;
-
-              for (level = GDK_WIN32_LEVEL_NONE; level < GDK_WIN32_LEVEL_COUNT; level++)
-                {
-                  guint *keygroup;
+              guint *keygroup;
 
-                  keygroup = &keymap->keysym_tab[(vk * keymap->layout_handles->len + group) * 
GDK_WIN32_LEVEL_COUNT];
+              keygroup = &keymap->keysym_tab[(vk * keymap->layout_handles->len + group) * 
GDK_WIN32_LEVEL_COUNT];
 
-                  if (keygroup[level] == keyval)
-                    {
-                      GdkKeymapKey key;
+              if (keygroup[level] == keyval)
+                {
+                  GdkKeymapKey key;
 
-                      key.keycode = vk;
-                      key.group = group;
-                      key.level = level;
-                      g_array_append_val (retval, key);
-                    }
+                  key.keycode = vk;
+                  key.group = group;
+                  key.level = level;
+                  g_array_append_val (retval, key);
                 }
             }
         }
@@ -1359,19 +1320,14 @@ gdk_win32_keymap_get_entries_for_keycode (GdkKeymap     *gdk_keymap,
                                           guint        **keyvals,
                                           int           *n_entries)
 {
-  GArray         *key_array;
-  GArray         *keyval_array;
-  int             group;
-  GdkWin32Keymap *keymap;
-  GdkKeymap      *default_keymap = gdk_display_get_keymap (gdk_display_get_default ());
-
-  g_return_val_if_fail (gdk_keymap == NULL || GDK_IS_KEYMAP (gdk_keymap), FALSE);
-  g_return_val_if_fail (n_entries != NULL, FALSE);
+  GdkWin32Keymap *keymap = GDK_WIN32_KEYMAP (gdk_keymap);
+  GArray *key_array;
+  GArray *keyval_array;
+  int group;
 
   if (hardware_keycode <= 0 ||
       hardware_keycode >= KEY_STATE_SIZE ||
-      (keys == NULL && keyvals == NULL) ||
-      (gdk_keymap != NULL && gdk_keymap != default_keymap))
+      (keys == NULL && keyvals == NULL))
     {
       /* Wrong keycode or NULL output arrays or wrong keymap */
       if (keys)
@@ -1393,7 +1349,6 @@ gdk_win32_keymap_get_entries_for_keycode (GdkKeymap     *gdk_keymap,
   else
     keyval_array = NULL;
 
-  keymap = GDK_WIN32_KEYMAP (default_keymap);
   update_keymap (GDK_KEYMAP (keymap));
 
   for (group = 0; group < keymap->layout_handles->len; group++)
@@ -1453,18 +1408,12 @@ static guint
 gdk_win32_keymap_lookup_key (GdkKeymap          *gdk_keymap,
                              const GdkKeymapKey *key)
 {
+  GdkWin32Keymap *keymap = GDK_WIN32_KEYMAP (gdk_keymap);
   guint sym;
-  GdkWin32Keymap *keymap;
-  GdkKeymap      *default_keymap = gdk_display_get_keymap (gdk_display_get_default ());
 
   g_return_val_if_fail (gdk_keymap == NULL || GDK_IS_KEYMAP (gdk_keymap), 0);
   g_return_val_if_fail (key != NULL, 0);
 
-  /* Accept only the default keymap */
-  if (gdk_keymap != NULL && gdk_keymap != default_keymap)
-    return 0;
-
-  keymap = GDK_WIN32_KEYMAP (default_keymap);
   update_keymap (GDK_KEYMAP (keymap));
 
   if (key->keycode >= KEY_STATE_SIZE ||
@@ -1490,14 +1439,11 @@ gdk_win32_keymap_translate_keyboard_state (GdkKeymap       *gdk_keymap,
                                            int             *level,
                                            GdkModifierType *consumed_modifiers)
 {
-  GdkWin32Keymap *keymap;
+  GdkWin32Keymap *keymap = GDK_WIN32_KEYMAP (gdk_keymap);
   guint tmp_keyval;
   guint *keygroup;
   GdkWin32KeyLevelState shift_level;
   GdkModifierType modifiers = GDK_SHIFT_MASK | GDK_LOCK_MASK | GDK_MOD2_MASK;
-  GdkKeymap *default_keymap = gdk_display_get_keymap (gdk_display_get_default ());
-
-  g_return_val_if_fail (gdk_keymap == NULL || GDK_IS_KEYMAP (gdk_keymap), FALSE);
 
 #if 0
   GDK_NOTE (EVENTS, g_print ("gdk_keymap_translate_keyboard_state: keycode=%#x state=%#x group=%d\n",
@@ -1512,14 +1458,9 @@ gdk_win32_keymap_translate_keyboard_state (GdkKeymap       *gdk_keymap,
   if (consumed_modifiers)
     *consumed_modifiers = 0;
 
-  /* Accept only the default keymap */
-  if (gdk_keymap != NULL && gdk_keymap != default_keymap)
-    return FALSE;
-
   if (hardware_keycode >= KEY_STATE_SIZE)
     return FALSE;
 
-  keymap = GDK_WIN32_KEYMAP (default_keymap);
   update_keymap (GDK_KEYMAP (keymap));
 
   if (group < 0 || group >= keymap->layout_handles->len)
diff --git a/gdk/win32/gdkprivate-win32.h b/gdk/win32/gdkprivate-win32.h
index 60de7b17cf..f4ec9b3cd1 100644
--- a/gdk/win32/gdkprivate-win32.h
+++ b/gdk/win32/gdkprivate-win32.h
@@ -370,9 +370,6 @@ guint8     _gdk_win32_keymap_get_rshift_scancode (GdkWin32Keymap *keymap);
 void       _gdk_win32_keymap_set_active_layout   (GdkWin32Keymap *keymap,
                                                   HKL             hkl);
 
-
-GdkKeymap *_gdk_win32_display_get_keymap (GdkDisplay *display);
-
 GdkSurface *_gdk_win32_display_create_surface  (GdkDisplay    *display,
                                                 GdkSurfaceType surface_type,
                                                 GdkSurface     *parent,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]