Blame gtk/gtkkeyhash.c

Packit Service fb6fa5
/* gtkkeyhash.c: Keymap aware matching of key bindings
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * GTK - The GIMP Toolkit
Packit Service fb6fa5
 * Copyright (C) 2002, Red Hat Inc.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * This library is free software; you can redistribute it and/or
Packit Service fb6fa5
 * modify it under the terms of the GNU Lesser General Public
Packit Service fb6fa5
 * License as published by the Free Software Foundation; either
Packit Service fb6fa5
 * version 2 of the License, or (at your option) any later version.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * This library is distributed in the hope that it will be useful,
Packit Service fb6fa5
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service fb6fa5
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service fb6fa5
 * Lesser General Public License for more details.
Packit Service fb6fa5
 *
Packit Service fb6fa5
 * You should have received a copy of the GNU Lesser General Public
Packit Service fb6fa5
 * License along with this library; if not, write to the
Packit Service fb6fa5
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Packit Service fb6fa5
 * Boston, MA 02111-1307, USA.
Packit Service fb6fa5
 */
Packit Service fb6fa5
Packit Service fb6fa5
#include "config.h"
Packit Service fb6fa5
Packit Service fb6fa5
#include "gtkdebug.h"
Packit Service fb6fa5
#include "gtkkeyhash.h"
Packit Service fb6fa5
#include "gtkprivate.h"
Packit Service fb6fa5
#include "gtkalias.h"
Packit Service fb6fa5
Packit Service fb6fa5
typedef struct _GtkKeyHashEntry GtkKeyHashEntry;
Packit Service fb6fa5
Packit Service fb6fa5
struct _GtkKeyHashEntry
Packit Service fb6fa5
{
Packit Service fb6fa5
  guint keyval;
Packit Service fb6fa5
  GdkModifierType modifiers;
Packit Service fb6fa5
  gpointer value;
Packit Service fb6fa5
Packit Service fb6fa5
  /* Set as a side effect of generating key_hash->keycode_hash
Packit Service fb6fa5
   */
Packit Service fb6fa5
  GdkKeymapKey *keys;		
Packit Service fb6fa5
  gint n_keys;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
struct _GtkKeyHash
Packit Service fb6fa5
{
Packit Service fb6fa5
  GdkKeymap *keymap;
Packit Service fb6fa5
  GHashTable *keycode_hash;
Packit Service fb6fa5
  GHashTable *reverse_hash;
Packit Service fb6fa5
  GList *entries_list;
Packit Service fb6fa5
  GDestroyNotify destroy_notify;
Packit Service fb6fa5
};
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
key_hash_clear_keycode (gpointer key,
Packit Service fb6fa5
			gpointer value,
Packit Service fb6fa5
			gpointer data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GSList *keys = value;
Packit Service fb6fa5
  g_slist_free (keys);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
key_hash_insert_entry (GtkKeyHash      *key_hash,
Packit Service fb6fa5
		       GtkKeyHashEntry *entry)
Packit Service fb6fa5
{
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (entry->keys);
Packit Service fb6fa5
  gdk_keymap_get_entries_for_keyval (key_hash->keymap,
Packit Service fb6fa5
				     entry->keyval,
Packit Service fb6fa5
				     &entry->keys, &entry->n_keys);
Packit Service fb6fa5
  
Packit Service fb6fa5
  for (i = 0; i < entry->n_keys; i++)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GSList *old_keys = g_hash_table_lookup (key_hash->keycode_hash,
Packit Service fb6fa5
					      GUINT_TO_POINTER (entry->keys[i].keycode));
Packit Service fb6fa5
      old_keys = g_slist_prepend (old_keys, entry);
Packit Service fb6fa5
      g_hash_table_insert (key_hash->keycode_hash,
Packit Service fb6fa5
			   GUINT_TO_POINTER (entry->keys[i].keycode),
Packit Service fb6fa5
			   old_keys);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static GHashTable *
Packit Service fb6fa5
key_hash_get_keycode_hash (GtkKeyHash *key_hash)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (!key_hash->keycode_hash)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GList *tmp_list;
Packit Service fb6fa5
  
Packit Service fb6fa5
      key_hash->keycode_hash = g_hash_table_new (g_direct_hash, NULL);
Packit Service fb6fa5
      
Packit Service fb6fa5
      /* Preserve the original insertion order
Packit Service fb6fa5
       */
Packit Service fb6fa5
      for (tmp_list = g_list_last (key_hash->entries_list);
Packit Service fb6fa5
	   tmp_list;
Packit Service fb6fa5
	   tmp_list = tmp_list->prev)
Packit Service fb6fa5
	key_hash_insert_entry (key_hash, tmp_list->data);
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return key_hash->keycode_hash;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
key_hash_keys_changed (GdkKeymap  *keymap,
Packit Service fb6fa5
		       GtkKeyHash *key_hash)
Packit Service fb6fa5
{
Packit Service fb6fa5
  /* The keymap changed, so we have to regenerate the keycode hash
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (key_hash->keycode_hash)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_hash_table_foreach (key_hash->keycode_hash, key_hash_clear_keycode, NULL);
Packit Service fb6fa5
      g_hash_table_destroy (key_hash->keycode_hash);
Packit Service fb6fa5
      key_hash->keycode_hash = NULL;
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * _gtk_key_hash_new:
Packit Service fb6fa5
 * @keymap: a #GdkKeymap
Packit Service fb6fa5
 * @item_destroy_notify: function to be called when items are removed
Packit Service fb6fa5
 *   from the hash or %NULL.
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Create a new key hash object for doing binding resolution. 
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Return value: the newly created object. Free with _gtk_key_hash_free().
Packit Service fb6fa5
 **/
Packit Service fb6fa5
GtkKeyHash *
Packit Service fb6fa5
_gtk_key_hash_new (GdkKeymap      *keymap,
Packit Service fb6fa5
		   GDestroyNotify  item_destroy_notify)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkKeyHash *key_hash = g_new (GtkKeyHash, 1);
Packit Service fb6fa5
Packit Service fb6fa5
  key_hash->keymap = keymap;
Packit Service fb6fa5
  g_signal_connect (keymap, "keys-changed",
Packit Service fb6fa5
		    G_CALLBACK (key_hash_keys_changed), key_hash);
Packit Service fb6fa5
Packit Service fb6fa5
  key_hash->entries_list = NULL;
Packit Service fb6fa5
  key_hash->keycode_hash = NULL;
Packit Service fb6fa5
  key_hash->reverse_hash = g_hash_table_new (g_direct_hash, NULL);
Packit Service fb6fa5
  key_hash->destroy_notify = item_destroy_notify;
Packit Service fb6fa5
Packit Service fb6fa5
  return key_hash;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
key_hash_free_entry (GtkKeyHash      *key_hash,
Packit Service fb6fa5
		     GtkKeyHashEntry *entry)
Packit Service fb6fa5
{
Packit Service fb6fa5
  if (key_hash->destroy_notify)
Packit Service fb6fa5
    (*key_hash->destroy_notify) (entry->value);
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_free (entry->keys);
Packit Service fb6fa5
  g_slice_free (GtkKeyHashEntry, entry);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static void
Packit Service fb6fa5
key_hash_free_entry_foreach (gpointer value,
Packit Service fb6fa5
			     gpointer data)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkKeyHashEntry *entry = value;
Packit Service fb6fa5
  GtkKeyHash *key_hash = data;
Packit Service fb6fa5
Packit Service fb6fa5
  key_hash_free_entry (key_hash, entry);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * gtk_key_hash_free:
Packit Service fb6fa5
 * @key_hash: a #GtkKeyHash
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Destroys a key hash created with gtk_key_hash_new()
Packit Service fb6fa5
 **/
Packit Service fb6fa5
void
Packit Service fb6fa5
_gtk_key_hash_free (GtkKeyHash *key_hash)
Packit Service fb6fa5
{
Packit Service fb6fa5
  g_signal_handlers_disconnect_by_func (key_hash->keymap,
Packit Service fb6fa5
					key_hash_keys_changed,
Packit Service fb6fa5
					key_hash);
Packit Service fb6fa5
Packit Service fb6fa5
  if (key_hash->keycode_hash)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      g_hash_table_foreach (key_hash->keycode_hash, key_hash_clear_keycode, NULL);
Packit Service fb6fa5
      g_hash_table_destroy (key_hash->keycode_hash);
Packit Service fb6fa5
    }
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_hash_table_destroy (key_hash->reverse_hash);
Packit Service fb6fa5
Packit Service fb6fa5
  g_list_foreach (key_hash->entries_list, key_hash_free_entry_foreach, key_hash);
Packit Service fb6fa5
  g_list_free (key_hash->entries_list);
Packit Service fb6fa5
  
Packit Service fb6fa5
  g_free (key_hash);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * _gtk_key_hash_add_entry:
Packit Service fb6fa5
 * @key_hash: a #GtkKeyHash
Packit Service fb6fa5
 * @keyval: key symbol for this binding
Packit Service fb6fa5
 * @modifiers: modifiers for this binding
Packit Service fb6fa5
 * @value: value to insert in the key hash
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Inserts a pair of key symbol and modifier mask into the key hash. 
Packit Service fb6fa5
 **/
Packit Service fb6fa5
void
Packit Service fb6fa5
_gtk_key_hash_add_entry (GtkKeyHash      *key_hash,
Packit Service fb6fa5
			 guint            keyval,
Packit Service fb6fa5
			 GdkModifierType  modifiers,
Packit Service fb6fa5
			 gpointer         value)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GtkKeyHashEntry *entry = g_slice_new (GtkKeyHashEntry);
Packit Service fb6fa5
Packit Service fb6fa5
  entry->value = value;
Packit Service fb6fa5
  entry->keyval = keyval;
Packit Service fb6fa5
  entry->modifiers = modifiers;
Packit Service fb6fa5
  entry->keys = NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  key_hash->entries_list = g_list_prepend (key_hash->entries_list, entry);
Packit Service fb6fa5
  g_hash_table_insert (key_hash->reverse_hash, value, key_hash->entries_list);
Packit Service fb6fa5
Packit Service fb6fa5
  if (key_hash->keycode_hash)
Packit Service fb6fa5
    key_hash_insert_entry (key_hash, entry);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * _gtk_key_hash_remove_entry:
Packit Service fb6fa5
 * @key_hash: a #GtkKeyHash
Packit Service fb6fa5
 * @value: value previously added with _gtk_key_hash_add_entry()
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Removes a value previously added to the key hash with
Packit Service fb6fa5
 * _gtk_key_hash_add_entry().
Packit Service fb6fa5
 **/
Packit Service fb6fa5
void
Packit Service fb6fa5
_gtk_key_hash_remove_entry (GtkKeyHash *key_hash,
Packit Service fb6fa5
			    gpointer    value)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GList *entry_node = g_hash_table_lookup (key_hash->reverse_hash, value);
Packit Service fb6fa5
  
Packit Service fb6fa5
  if (entry_node)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GtkKeyHashEntry *entry = entry_node->data;
Packit Service fb6fa5
Packit Service fb6fa5
      if (key_hash->keycode_hash)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  gint i;
Packit Service fb6fa5
	  
Packit Service fb6fa5
	  for (i = 0; i < entry->n_keys; i++)
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      GSList *old_keys = g_hash_table_lookup (key_hash->keycode_hash,
Packit Service fb6fa5
						      GUINT_TO_POINTER (entry->keys[i].keycode));
Packit Service fb6fa5
	      
Packit Service fb6fa5
	      GSList *new_keys = g_slist_remove (old_keys, entry);
Packit Service fb6fa5
	      if (new_keys != old_keys)
Packit Service fb6fa5
		{
Packit Service fb6fa5
		  if (new_keys)
Packit Service fb6fa5
		    g_hash_table_insert (key_hash->keycode_hash,
Packit Service fb6fa5
					 GUINT_TO_POINTER (entry->keys[i].keycode),
Packit Service fb6fa5
					 new_keys);
Packit Service fb6fa5
		  else
Packit Service fb6fa5
		    g_hash_table_remove (key_hash->keycode_hash,
Packit Service fb6fa5
					 GUINT_TO_POINTER (entry->keys[i].keycode));
Packit Service fb6fa5
		}
Packit Service fb6fa5
	    }
Packit Service fb6fa5
	}
Packit Service fb6fa5
	  
Packit Service fb6fa5
      g_hash_table_remove (key_hash->reverse_hash, entry_node);
Packit Service fb6fa5
      key_hash->entries_list = g_list_delete_link (key_hash->entries_list, entry_node);
Packit Service fb6fa5
Packit Service fb6fa5
      key_hash_free_entry (key_hash, entry);
Packit Service fb6fa5
    }
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gint
Packit Service fb6fa5
lookup_result_compare (gconstpointer a,
Packit Service fb6fa5
		       gconstpointer b)
Packit Service fb6fa5
{
Packit Service fb6fa5
  const GtkKeyHashEntry *entry_a = a;
Packit Service fb6fa5
  const GtkKeyHashEntry *entry_b = b;
Packit Service fb6fa5
  guint modifiers;
Packit Service fb6fa5
Packit Service fb6fa5
  gint n_bits_a = 0;
Packit Service fb6fa5
  gint n_bits_b = 0;
Packit Service fb6fa5
Packit Service fb6fa5
  modifiers = entry_a->modifiers;
Packit Service fb6fa5
  while (modifiers)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (modifiers & 1)
Packit Service fb6fa5
	n_bits_a++;
Packit Service fb6fa5
      modifiers >>= 1;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  modifiers = entry_b->modifiers;
Packit Service fb6fa5
  while (modifiers)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (modifiers & 1)
Packit Service fb6fa5
	n_bits_b++;
Packit Service fb6fa5
      modifiers >>= 1;
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  return n_bits_a < n_bits_b ? -1 : (n_bits_a == n_bits_b ? 0 : 1);
Packit Service fb6fa5
  
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Sort a list of results so that matches with less modifiers come
Packit Service fb6fa5
 * before matches with more modifiers
Packit Service fb6fa5
 */
Packit Service fb6fa5
static GSList *
Packit Service fb6fa5
sort_lookup_results (GSList *slist)
Packit Service fb6fa5
{
Packit Service fb6fa5
  return g_slist_sort (slist, lookup_result_compare);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static gint
Packit Service fb6fa5
lookup_result_compare_by_keyval (gconstpointer a,
Packit Service fb6fa5
		                 gconstpointer b)
Packit Service fb6fa5
{
Packit Service fb6fa5
  const GtkKeyHashEntry *entry_a = a;
Packit Service fb6fa5
  const GtkKeyHashEntry *entry_b = b;
Packit Service fb6fa5
Packit Service fb6fa5
  if (entry_a->keyval < entry_b->keyval)
Packit Service fb6fa5
	return -1;
Packit Service fb6fa5
  else if (entry_a->keyval > entry_b->keyval)
Packit Service fb6fa5
	return 1;
Packit Service fb6fa5
  else
Packit Service fb6fa5
	return 0;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
static GSList *
Packit Service fb6fa5
sort_lookup_results_by_keyval (GSList *slist)
Packit Service fb6fa5
{
Packit Service fb6fa5
  return g_slist_sort (slist, lookup_result_compare_by_keyval);
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/* Return true if keyval is defined in keyboard group
Packit Service fb6fa5
 */
Packit Service fb6fa5
static gboolean 
Packit Service fb6fa5
keyval_in_group (GdkKeymap  *keymap,
Packit Service fb6fa5
                 guint      keyval,
Packit Service fb6fa5
                 gint       group)
Packit Service fb6fa5
{                 
Packit Service fb6fa5
  GtkKeyHashEntry entry;
Packit Service fb6fa5
  gint i;
Packit Service fb6fa5
Packit Service fb6fa5
  gdk_keymap_get_entries_for_keyval (keymap,
Packit Service fb6fa5
				     keyval,
Packit Service fb6fa5
				     &entry.keys, &entry.n_keys);
Packit Service fb6fa5
Packit Service fb6fa5
  for (i = 0; i < entry.n_keys; i++)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      if (entry.keys[i].group == group)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          g_free (entry.keys);
Packit Service fb6fa5
          return TRUE;
Packit Service fb6fa5
        }
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (entry.keys);
Packit Service fb6fa5
  return FALSE;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * _gtk_key_hash_lookup:
Packit Service fb6fa5
 * @key_hash: a #GtkKeyHash
Packit Service fb6fa5
 * @hardware_keycode: hardware keycode field from a #GdkEventKey
Packit Service fb6fa5
 * @state: state field from a #GdkEventKey
Packit Service fb6fa5
 * @mask: mask of modifiers to consider when matching against the
Packit Service fb6fa5
 *        modifiers in entries.
Packit Service fb6fa5
 * @group: group field from a #GdkEventKey
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Looks up the best matching entry or entries in the hash table for
Packit Service fb6fa5
 * a given event. The results are sorted so that entries with less
Packit Service fb6fa5
 * modifiers come before entries with more modifiers.
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * The matches returned by this function can be exact (i.e. keycode, level
Packit Service fb6fa5
 * and group all match) or fuzzy (i.e. keycode and level match, but group
Packit Service fb6fa5
 * does not). As long there are any exact matches, only exact matches
Packit Service fb6fa5
 * are returned. If there are no exact matches, fuzzy matches will be
Packit Service fb6fa5
 * returned, as long as they are not shadowing a possible exact match.
Packit Service fb6fa5
 * This means that fuzzy matches won't be considered if their keyval is 
Packit Service fb6fa5
 * present in the current group.
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Return value: A #GSList of matching entries.
Packit Service fb6fa5
 **/
Packit Service fb6fa5
GSList *
Packit Service fb6fa5
_gtk_key_hash_lookup (GtkKeyHash      *key_hash,
Packit Service fb6fa5
		      guint16          hardware_keycode,
Packit Service fb6fa5
		      GdkModifierType  state,
Packit Service fb6fa5
		      GdkModifierType  mask,
Packit Service fb6fa5
		      gint             group)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GHashTable *keycode_hash = key_hash_get_keycode_hash (key_hash);
Packit Service fb6fa5
  GSList *keys = g_hash_table_lookup (keycode_hash, GUINT_TO_POINTER ((guint)hardware_keycode));
Packit Service fb6fa5
  GSList *results = NULL;
Packit Service fb6fa5
  GSList *l;
Packit Service fb6fa5
  gboolean have_exact = FALSE;
Packit Service fb6fa5
  guint keyval;
Packit Service fb6fa5
  gint effective_group;
Packit Service fb6fa5
  gint level;
Packit Service fb6fa5
  GdkModifierType modifiers;
Packit Service fb6fa5
  GdkModifierType consumed_modifiers;
Packit Service fb6fa5
  gboolean group_mod_is_accel_mod = FALSE;
Packit Service fb6fa5
  const GdkModifierType xmods = GDK_MOD2_MASK|GDK_MOD3_MASK|GDK_MOD4_MASK|GDK_MOD5_MASK;
Packit Service fb6fa5
  const GdkModifierType vmods = GDK_SUPER_MASK|GDK_HYPER_MASK|GDK_META_MASK;
Packit Service fb6fa5
Packit Service fb6fa5
  /* We don't want Caps_Lock to affect keybinding lookups.
Packit Service fb6fa5
   */
Packit Service fb6fa5
  state &= ~GDK_LOCK_MASK;
Packit Service fb6fa5
Packit Service fb6fa5
  _gtk_translate_keyboard_accel_state (key_hash->keymap,
Packit Service fb6fa5
                                       hardware_keycode, state, mask, group,
Packit Service fb6fa5
                                       &keyval,
Packit Service fb6fa5
                                       &effective_group, &level, &consumed_modifiers);
Packit Service fb6fa5
Packit Service fb6fa5
  /* if the group-toggling modifier is part of the default accel mod
Packit Service fb6fa5
   * mask, and it is active, disable it for matching
Packit Service fb6fa5
   */
Packit Service fb6fa5
  if (mask & GTK_TOGGLE_GROUP_MOD_MASK)
Packit Service fb6fa5
    group_mod_is_accel_mod = TRUE;
Packit Service fb6fa5
Packit Service fb6fa5
  gdk_keymap_map_virtual_modifiers (key_hash->keymap, &mask);
Packit Service fb6fa5
  gdk_keymap_add_virtual_modifiers (key_hash->keymap, &state);
Packit Service fb6fa5
Packit Service fb6fa5
  GTK_NOTE (KEYBINDINGS,
Packit Service fb6fa5
	    g_message ("Looking up keycode = %u, modifiers = 0x%04x,\n"
Packit Service fb6fa5
		       "    keyval = %u, group = %d, level = %d, consumed_modifiers = 0x%04x",
Packit Service fb6fa5
		       hardware_keycode, state, keyval, effective_group, level, consumed_modifiers));
Packit Service fb6fa5
Packit Service fb6fa5
  if (keys)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GSList *tmp_list = keys;
Packit Service fb6fa5
      while (tmp_list)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  GtkKeyHashEntry *entry = tmp_list->data;
Packit Service fb6fa5
Packit Service fb6fa5
	  /* If the virtual Super, Hyper or Meta modifiers are present,
Packit Service fb6fa5
	   * they will also be mapped to some of the Mod2 - Mod5 modifiers,
Packit Service fb6fa5
	   * so we compare them twice, ignoring either set.
Packit Service fb6fa5
	   * We accept combinations involving virtual modifiers only if they
Packit Service fb6fa5
	   * are mapped to separate modifiers; i.e. if Super and Hyper are
Packit Service fb6fa5
	   * both mapped to Mod4, then pressing a key that is mapped to Mod4
Packit Service fb6fa5
	   * will not match a Super+Hyper entry.
Packit Service fb6fa5
	   */
Packit Service fb6fa5
          modifiers = entry->modifiers;
Packit Service fb6fa5
          if (gdk_keymap_map_virtual_modifiers (key_hash->keymap, &modifiers) &&
Packit Service fb6fa5
	      ((modifiers & ~consumed_modifiers & mask & ~vmods) == (state & ~consumed_modifiers & mask & ~vmods) ||
Packit Service fb6fa5
	       (modifiers & ~consumed_modifiers & mask & ~xmods) == (state & ~consumed_modifiers & mask & ~xmods)))
Packit Service fb6fa5
	    {
Packit Service fb6fa5
	      gint i;
Packit Service fb6fa5
Packit Service fb6fa5
	      if (keyval == entry->keyval && /* Exact match */
Packit Service fb6fa5
                  /* but also match for group if it is an accel mod, because
Packit Service fb6fa5
                   * otherwise we can get multiple exact matches, some being
Packit Service fb6fa5
                   * bogus */
Packit Service fb6fa5
                  (!group_mod_is_accel_mod ||
Packit Service fb6fa5
                   (state & GTK_TOGGLE_GROUP_MOD_MASK) ==
Packit Service fb6fa5
                   (entry->modifiers & GTK_TOGGLE_GROUP_MOD_MASK)))
Packit Service fb6fa5
Packit Service fb6fa5
		{
Packit Service fb6fa5
		  GTK_NOTE (KEYBINDINGS,
Packit Service fb6fa5
			    g_message ("  found exact match, keyval = %u, modifiers = 0x%04x",
Packit Service fb6fa5
				       entry->keyval, entry->modifiers));
Packit Service fb6fa5
Packit Service fb6fa5
		  if (!have_exact)
Packit Service fb6fa5
		    {
Packit Service fb6fa5
		      g_slist_free (results);
Packit Service fb6fa5
		      results = NULL;
Packit Service fb6fa5
		    }
Packit Service fb6fa5
Packit Service fb6fa5
		  have_exact = TRUE;
Packit Service fb6fa5
		  results = g_slist_prepend (results, entry);
Packit Service fb6fa5
		}
Packit Service fb6fa5
Packit Service fb6fa5
	      if (!have_exact)
Packit Service fb6fa5
		{
Packit Service fb6fa5
		  for (i = 0; i < entry->n_keys; i++)
Packit Service fb6fa5
		    {
Packit Service fb6fa5
                      if (entry->keys[i].keycode == hardware_keycode &&
Packit Service fb6fa5
                          entry->keys[i].level == level &&
Packit Service fb6fa5
                           /* Only match for group if it's an accel mod */
Packit Service fb6fa5
                          (!group_mod_is_accel_mod ||
Packit Service fb6fa5
                           entry->keys[i].group == effective_group))
Packit Service fb6fa5
			{
Packit Service fb6fa5
			  GTK_NOTE (KEYBINDINGS,
Packit Service fb6fa5
				    g_message ("  found group = %d, level = %d",
Packit Service fb6fa5
					       entry->keys[i].group, entry->keys[i].level));
Packit Service fb6fa5
			  results = g_slist_prepend (results, entry);
Packit Service fb6fa5
			  break;
Packit Service fb6fa5
			}
Packit Service fb6fa5
		    }
Packit Service fb6fa5
		}
Packit Service fb6fa5
	    }
Packit Service fb6fa5
Packit Service fb6fa5
	  tmp_list = tmp_list->next;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  if (!have_exact && results) 
Packit Service fb6fa5
    {
Packit Service fb6fa5
      /* If there are fuzzy matches, check that the current group doesn't also 
Packit Service fb6fa5
       * define these keyvals; if yes, discard results because a widget up in 
Packit Service fb6fa5
       * the stack may have an exact match and we don't want to 'steal' it.
Packit Service fb6fa5
       */
Packit Service fb6fa5
      guint oldkeyval = 0;
Packit Service fb6fa5
      GtkKeyHashEntry *keyhashentry;
Packit Service fb6fa5
Packit Service fb6fa5
      results = sort_lookup_results_by_keyval (results);
Packit Service fb6fa5
      for (l = results; l; l = l->next)
Packit Service fb6fa5
        {
Packit Service fb6fa5
          keyhashentry = l->data;
Packit Service fb6fa5
          if (l == results || oldkeyval != keyhashentry->keyval)
Packit Service fb6fa5
            {
Packit Service fb6fa5
              oldkeyval = keyhashentry->keyval;
Packit Service fb6fa5
              if (keyval_in_group (key_hash->keymap, oldkeyval, group))
Packit Service fb6fa5
                {
Packit Service fb6fa5
       	          g_slist_free (results);
Packit Service fb6fa5
       	          return NULL;
Packit Service fb6fa5
                }
Packit Service fb6fa5
            }
Packit Service fb6fa5
        }
Packit Service fb6fa5
    }
Packit Service fb6fa5
    
Packit Service fb6fa5
  results = sort_lookup_results (results);
Packit Service fb6fa5
  for (l = results; l; l = l->next)
Packit Service fb6fa5
    l->data = ((GtkKeyHashEntry *)l->data)->value;
Packit Service fb6fa5
Packit Service fb6fa5
  return results;
Packit Service fb6fa5
}
Packit Service fb6fa5
Packit Service fb6fa5
/**
Packit Service fb6fa5
 * _gtk_key_hash_lookup_keyval:
Packit Service fb6fa5
 * @key_hash: a #GtkKeyHash
Packit Service fb6fa5
 * @event: a #GtkEvent
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Looks up the best matching entry or entries in the hash table for a
Packit Service fb6fa5
 * given keyval/modifiers pair. It's better to use
Packit Service fb6fa5
 * _gtk_key_hash_lookup() if you have the original #GdkEventKey
Packit Service fb6fa5
 * available.  The results are sorted so that entries with less
Packit Service fb6fa5
 * modifiers come before entries with more modifiers.
Packit Service fb6fa5
 * 
Packit Service fb6fa5
 * Return value: A #GSList of all matching entries.
Packit Service fb6fa5
 **/
Packit Service fb6fa5
GSList *
Packit Service fb6fa5
_gtk_key_hash_lookup_keyval (GtkKeyHash     *key_hash,
Packit Service fb6fa5
			     guint           keyval,
Packit Service fb6fa5
			     GdkModifierType modifiers)
Packit Service fb6fa5
{
Packit Service fb6fa5
  GdkKeymapKey *keys;
Packit Service fb6fa5
  gint n_keys;
Packit Service fb6fa5
  GSList *results = NULL;
Packit Service fb6fa5
  GSList *l;
Packit Service fb6fa5
Packit Service fb6fa5
  if (!keyval)			/* Key without symbol */
Packit Service fb6fa5
    return NULL;
Packit Service fb6fa5
Packit Service fb6fa5
  /* Find some random keycode for this keyval
Packit Service fb6fa5
   */
Packit Service fb6fa5
  gdk_keymap_get_entries_for_keyval (key_hash->keymap, keyval,
Packit Service fb6fa5
				     &keys, &n_keys);
Packit Service fb6fa5
Packit Service fb6fa5
  if (n_keys)
Packit Service fb6fa5
    {
Packit Service fb6fa5
      GHashTable *keycode_hash = key_hash_get_keycode_hash (key_hash);
Packit Service fb6fa5
      GSList *entries = g_hash_table_lookup (keycode_hash, GUINT_TO_POINTER (keys[0].keycode));
Packit Service fb6fa5
Packit Service fb6fa5
      while (entries)
Packit Service fb6fa5
	{
Packit Service fb6fa5
	  GtkKeyHashEntry *entry = entries->data;
Packit Service fb6fa5
Packit Service fb6fa5
	  if (entry->keyval == keyval && entry->modifiers == modifiers)
Packit Service fb6fa5
	    results = g_slist_prepend (results, entry);
Packit Service fb6fa5
Packit Service fb6fa5
	  entries = entries->next;
Packit Service fb6fa5
	}
Packit Service fb6fa5
    }
Packit Service fb6fa5
Packit Service fb6fa5
  g_free (keys);
Packit Service fb6fa5
	  
Packit Service fb6fa5
  results = sort_lookup_results (results);
Packit Service fb6fa5
  for (l = results; l; l = l->next)
Packit Service fb6fa5
    l->data = ((GtkKeyHashEntry *)l->data)->value;
Packit Service fb6fa5
Packit Service fb6fa5
  return results;
Packit Service fb6fa5
}