From 33f1706634905e7bfb86d7c5afbf3ccf480e983c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Thu, 17 Aug 2017 17:01:50 +0800 Subject: [PATCH] backend: Add API to get layout group Add API to get the layout group (layout index) currently active. In the native backend this is done by fetching the state directly from the evdev backend; on X11 this works by listening for XkbStateNotify events, caching the layout group value. https://bugzilla.gnome.org/show_bug.cgi?id=786408 --- src/backends/meta-backend-private.h | 4 +++ src/backends/meta-backend.c | 6 ++++ src/backends/native/meta-backend-native.c | 9 +++++ src/backends/x11/meta-backend-x11.c | 40 ++++++++++++++++++++++- 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/src/backends/meta-backend-private.h b/src/backends/meta-backend-private.h index 33b9e69a1..bf6d0c329 100644 --- a/src/backends/meta-backend-private.h +++ b/src/backends/meta-backend-private.h @@ -83,6 +83,8 @@ struct _MetaBackendClass struct xkb_keymap * (* get_keymap) (MetaBackend *backend); + xkb_layout_index_t (* get_keymap_layout_group) (MetaBackend *backend); + void (* lock_layout_group) (MetaBackend *backend, guint idx); @@ -133,6 +135,8 @@ MetaLogicalMonitor * meta_backend_get_current_logical_monitor (MetaBackend *back struct xkb_keymap * meta_backend_get_keymap (MetaBackend *backend); +xkb_layout_index_t meta_backend_get_keymap_layout_group (MetaBackend *backend); + void meta_backend_update_last_device (MetaBackend *backend, int device_id); diff --git a/src/backends/meta-backend.c b/src/backends/meta-backend.c index 85baf1c3d..66a731318 100644 --- a/src/backends/meta-backend.c +++ b/src/backends/meta-backend.c @@ -699,6 +699,12 @@ meta_backend_get_keymap (MetaBackend *backend) return META_BACKEND_GET_CLASS (backend)->get_keymap (backend); } +xkb_layout_index_t +meta_backend_get_keymap_layout_group (MetaBackend *backend) +{ + return META_BACKEND_GET_CLASS (backend)->get_keymap_layout_group (backend); +} + void meta_backend_lock_layout_group (MetaBackend *backend, guint idx) diff --git a/src/backends/native/meta-backend-native.c b/src/backends/native/meta-backend-native.c index 547bdde50..5ebe79189 100644 --- a/src/backends/native/meta-backend-native.c +++ b/src/backends/native/meta-backend-native.c @@ -507,6 +507,14 @@ meta_backend_native_get_keymap (MetaBackend *backend) return clutter_evdev_get_keyboard_map (manager); } +static xkb_layout_index_t +meta_backend_native_get_keymap_layout_group (MetaBackend *backend) +{ + ClutterDeviceManager *manager = clutter_device_manager_get_default (); + + return clutter_evdev_get_keyboard_layout_index (manager); +} + static void meta_backend_native_lock_layout_group (MetaBackend *backend, guint idx) @@ -579,6 +587,7 @@ meta_backend_native_class_init (MetaBackendNativeClass *klass) backend_class->set_keymap = meta_backend_native_set_keymap; backend_class->get_keymap = meta_backend_native_get_keymap; + backend_class->get_keymap_layout_group = meta_backend_native_get_keymap_layout_group; backend_class->lock_layout_group = meta_backend_native_lock_layout_group; backend_class->get_relative_motion_deltas = meta_backend_native_get_relative_motion_deltas; backend_class->update_screen_size = meta_backend_native_update_screen_size; diff --git a/src/backends/x11/meta-backend-x11.c b/src/backends/x11/meta-backend-x11.c index c299d263f..863749e5e 100644 --- a/src/backends/x11/meta-backend-x11.c +++ b/src/backends/x11/meta-backend-x11.c @@ -67,6 +67,7 @@ struct _MetaBackendX11Private uint8_t xkb_error_base; struct xkb_keymap *keymap; + xkb_layout_index_t keymap_layout_group; MetaLogicalMonitor *cached_current_logical_monitor; }; @@ -282,12 +283,18 @@ handle_host_xevent (MetaBackend *backend, if (xkb_ev->any.device == META_VIRTUAL_CORE_KEYBOARD_ID) { + int layout_group; + switch (xkb_ev->any.xkb_type) { case XkbNewKeyboardNotify: case XkbMapNotify: keymap_changed (backend); break; + case XkbStateNotify: + layout_group = xkb_ev->state.locked_group; + priv->keymap_layout_group = layout_group; + break; default: break; } @@ -424,7 +431,6 @@ meta_backend_x11_post_init (MetaBackend *backend) if (!has_xi) meta_fatal ("X server doesn't have the XInput extension, version 2.2 or newer\n"); - priv->xcb = XGetXCBConnection (priv->xdisplay); if (!xkb_x11_setup_xkb_extension (priv->xcb, XKB_X11_MIN_MAJOR_XKB_VERSION, XKB_X11_MIN_MINOR_XKB_VERSION, @@ -568,6 +574,15 @@ meta_backend_x11_get_keymap (MetaBackend *backend) return priv->keymap; } +static xkb_layout_index_t +meta_backend_x11_get_keymap_layout_group (MetaBackend *backend) +{ + MetaBackendX11 *x11 = META_BACKEND_X11 (backend); + MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11); + + return priv->keymap_layout_group; +} + static void meta_backend_x11_set_numlock (MetaBackend *backend, gboolean numlock_state) @@ -592,6 +607,25 @@ meta_backend_x11_get_xkb_event_base (MetaBackendX11 *x11) return priv->xkb_event_base; } +static void +init_xkb_state (MetaBackendX11 *x11) +{ + MetaBackendX11Private *priv = meta_backend_x11_get_instance_private (x11); + struct xkb_keymap *keymap; + int32_t device_id; + struct xkb_state *state; + + keymap = meta_backend_get_keymap (META_BACKEND (x11)); + + device_id = xkb_x11_get_core_keyboard_device_id (priv->xcb); + state = xkb_x11_state_new_from_device (keymap, priv->xcb, device_id); + + priv->keymap_layout_group = + xkb_state_serialize_layout (state, XKB_STATE_LAYOUT_LOCKED); + + xkb_state_unref (state); +} + static gboolean meta_backend_x11_initable_init (GInitable *initable, GCancellable *cancellable, @@ -619,8 +653,11 @@ meta_backend_x11_initable_init (GInitable *initable, } priv->xdisplay = xdisplay; + priv->xcb = XGetXCBConnection (priv->xdisplay); clutter_x11_set_display (xdisplay); + init_xkb_state (x11); + return initable_parent_iface->init (initable, cancellable, error); } @@ -645,6 +682,7 @@ meta_backend_x11_class_init (MetaBackendX11Class *klass) backend_class->warp_pointer = meta_backend_x11_warp_pointer; backend_class->get_current_logical_monitor = meta_backend_x11_get_current_logical_monitor; backend_class->get_keymap = meta_backend_x11_get_keymap; + backend_class->get_keymap_layout_group = meta_backend_x11_get_keymap_layout_group; backend_class->set_numlock = meta_backend_x11_set_numlock; }