Add a way to get the current modifier state

Xkb makes this available to us, and it is useful if you want
to do easter eggs that are triggered by Alt.
This commit is contained in:
Matthias Clasen 2012-02-21 00:30:44 +01:00
parent 8ec0cfd571
commit 9a92a1da89
7 changed files with 51 additions and 6 deletions

View File

@ -631,6 +631,7 @@ gdk_keymap_get_direction
gdk_keymap_have_bidi_layouts
gdk_keymap_get_caps_lock_state
gdk_keymap_get_num_lock_state
gdk_keymap_get_modifier_state
gdk_keymap_add_virtual_modifiers
gdk_keymap_map_virtual_modifiers
gdk_keymap_get_modifier_mask

View File

@ -209,6 +209,7 @@ gdk_keymap_get_entries_for_keycode
gdk_keymap_get_entries_for_keyval
gdk_keymap_get_for_display
gdk_keymap_get_modifier_mask
gdk_keymap_get_modifier_state
gdk_keymap_get_num_lock_state
gdk_keymap_get_type
gdk_keymap_have_bidi_layouts

View File

@ -378,6 +378,27 @@ gdk_keymap_get_num_lock_state (GdkKeymap *keymap)
return GDK_KEYMAP_GET_CLASS (keymap)->get_num_lock_state (keymap);
}
/**
* gdk_keymap_get_modifier_state:
* @keymap: a #GdkKeymap
*
* Returns the current modifier state.
*
* Returns: the current modifier state.
*
* Since: 3.2
*/
guint
gdk_keymap_get_modifier_state (GdkKeymap *keymap)
{
g_return_val_if_fail (GDK_IS_KEYMAP (keymap), FALSE);
if (GDK_KEYMAP_GET_CLASS (keymap)->get_modifier_state)
return GDK_KEYMAP_GET_CLASS (keymap)->get_modifier_state (keymap);
return 0;
}
/**
* gdk_keymap_get_entries_for_keyval:
* @keymap: a #GdkKeymap

View File

@ -110,6 +110,7 @@ PangoDirection gdk_keymap_get_direction (GdkKeymap *keymap)
gboolean gdk_keymap_have_bidi_layouts (GdkKeymap *keymap);
gboolean gdk_keymap_get_caps_lock_state (GdkKeymap *keymap);
gboolean gdk_keymap_get_num_lock_state (GdkKeymap *keymap);
guint gdk_keymap_get_modifier_state (GdkKeymap *keymap);
void gdk_keymap_add_virtual_modifiers (GdkKeymap *keymap,
GdkModifierType *state);
gboolean gdk_keymap_map_virtual_modifiers (GdkKeymap *keymap,

View File

@ -63,6 +63,7 @@ struct _GdkKeymapClass
GdkModifierType *state);
GdkModifierType (*get_modifier_mask) (GdkKeymap *keymap,
GdkModifierIntent intent);
guint (* get_modifier_state) (GdkKeymap *keymap);
/* Signals */

View File

@ -1403,7 +1403,7 @@ _gdk_x11_display_open (const gchar *display_name)
XkbSelectEventDetails (display_x11->xdisplay,
XkbUseCoreKbd, XkbStateNotify,
XkbAllStateComponentsMask,
XkbGroupLockMask|XkbModifierLockMask);
XkbModifierStateMask|XkbGroupStateMask);
XkbSetDetectableAutoRepeat (display_x11->xdisplay,
True,

View File

@ -77,7 +77,8 @@ struct _GdkX11Keymap
guint have_direction : 1;
guint have_lock_state : 1;
guint caps_lock_state : 1;
guint num_lock_state : 1;
guint num_lock_state : 1;
guint modifier_state;
guint current_serial;
#ifdef HAVE_XKB
@ -600,12 +601,14 @@ update_direction (GdkX11Keymap *keymap_x11,
static gboolean
update_lock_state (GdkX11Keymap *keymap_x11,
gint locked_mods)
gint locked_mods,
gint effective_mods)
{
XkbDescPtr xkb G_GNUC_UNUSED;
gboolean have_lock_state;
gboolean caps_lock_state;
gboolean num_lock_state;
guint modifier_state;
/* ensure keymap_x11->num_lock_mask is initialized */
xkb = get_xkb (keymap_x11);
@ -613,14 +616,18 @@ update_lock_state (GdkX11Keymap *keymap_x11,
have_lock_state = keymap_x11->have_lock_state;
caps_lock_state = keymap_x11->caps_lock_state;
num_lock_state = keymap_x11->num_lock_state;
modifier_state = keymap_x11->modifier_state;
keymap_x11->have_lock_state = TRUE;
keymap_x11->caps_lock_state = (locked_mods & GDK_LOCK_MASK) != 0;
keymap_x11->num_lock_state = (locked_mods & keymap_x11->num_lock_mask) != 0;
/* FIXME: sanitize this */
keymap_x11->modifier_state = (guint)effective_mods;
return !have_lock_state
|| (caps_lock_state != keymap_x11->caps_lock_state)
|| (num_lock_state != keymap_x11->num_lock_state);
|| (num_lock_state != keymap_x11->num_lock_state)
|| (modifier_state != keymap_x11->modifier_state);
}
/* keep this in sync with the XkbSelectEventDetails()
@ -640,7 +647,9 @@ _gdk_x11_keymap_state_changed (GdkDisplay *display,
if (update_direction (keymap_x11, XkbStateGroup (&xkb_event->state)))
g_signal_emit_by_name (keymap_x11, "direction-changed");
if (update_lock_state (keymap_x11, xkb_event->state.locked_mods))
if (update_lock_state (keymap_x11,
xkb_event->state.locked_mods,
xkb_event->state.mods))
g_signal_emit_by_name (keymap_x11, "state-changed");
}
}
@ -661,7 +670,7 @@ ensure_lock_state (GdkKeymap *keymap)
XkbStateRec state_rec;
XkbGetState (GDK_DISPLAY_XDISPLAY (display), XkbUseCoreKbd, &state_rec);
update_lock_state (keymap_x11, state_rec.locked_mods);
update_lock_state (keymap_x11, state_rec.locked_mods, state_rec.mods);
}
}
#endif /* HAVE_XKB */
@ -752,6 +761,16 @@ gdk_x11_keymap_get_num_lock_state (GdkKeymap *keymap)
return keymap_x11->num_lock_state;
}
static guint
gdk_x11_keymap_get_modifier_state (GdkKeymap *keymap)
{
GdkX11Keymap *keymap_x11 = GDK_X11_KEYMAP (keymap);
ensure_lock_state (keymap);
return keymap_x11->modifier_state;
}
static gboolean
gdk_x11_keymap_get_entries_for_keyval (GdkKeymap *keymap,
guint keyval,
@ -1580,6 +1599,7 @@ gdk_x11_keymap_class_init (GdkX11KeymapClass *klass)
keymap_class->have_bidi_layouts = gdk_x11_keymap_have_bidi_layouts;
keymap_class->get_caps_lock_state = gdk_x11_keymap_get_caps_lock_state;
keymap_class->get_num_lock_state = gdk_x11_keymap_get_num_lock_state;
keymap_class->get_modifier_state = gdk_x11_keymap_get_modifier_state;
keymap_class->get_entries_for_keyval = gdk_x11_keymap_get_entries_for_keyval;
keymap_class->get_entries_for_keycode = gdk_x11_keymap_get_entries_for_keycode;
keymap_class->lookup_key = gdk_x11_keymap_lookup_key;