]> Pileus Git - ~andy/gtk/blob - gtk/gtkkeyhash.c
Implement "fuzzy" key binding lookups; allow matches on key and level but
[~andy/gtk] / gtk / gtkkeyhash.c
1 /* gtkkeyhash.c: Keymap aware matching of key bindings
2  *
3  * GTK - The GIMP Toolkit
4  * Copyright (C) 2002, Red Hat Inc.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 #include "gtkdebug.h"
22 #include "gtkkeyhash.h"
23
24 /* We need to add a ::changed signal to GdkKeyMap to properly deal
25  * with changes to the key map while we are running.
26  */
27 #undef HAVE_CHANGED_SIGNAL
28
29 typedef struct _GtkKeyHashEntry GtkKeyHashEntry;
30
31 struct _GtkKeyHashEntry
32 {
33   guint keyval;
34   GdkModifierType modifiers;
35   GdkKeymapKey *keys;
36   gint n_keys;
37   gpointer value;
38 };
39
40 struct _GtkKeyHash
41 {
42   GdkKeymap *keymap;
43   GHashTable *keycode_hash;
44   GHashTable *reverse_hash;
45   GDestroyNotify destroy_notify;
46 };
47
48 static void
49 key_hash_clear_keycode (gpointer key,
50                         gpointer value,
51                         gpointer data)
52 {
53   GSList *keys = value;
54   g_slist_free (keys);
55 }
56
57 static void
58 key_hash_insert_entry (GtkKeyHash      *key_hash,
59                        GtkKeyHashEntry *entry)
60 {
61   gint i;
62
63   gdk_keymap_get_entries_for_keyval (key_hash->keymap,
64                                      entry->keyval,
65                                      &entry->keys, &entry->n_keys);
66   
67   for (i = 0; i < entry->n_keys; i++)
68     {
69       GSList *old_keys = g_hash_table_lookup (key_hash->keycode_hash,
70                                               GUINT_TO_POINTER (entry->keys[i].keycode));
71       old_keys = g_slist_prepend (old_keys, entry);
72       g_hash_table_insert (key_hash->keycode_hash,
73                            GUINT_TO_POINTER (entry->keys[i].keycode),
74                            old_keys);
75     }
76 }
77
78 #ifdef HAVE_CHANGED_SIGNAL
79 static void
80 key_hash_reinsert_entry (gpointer key,
81                          gpointer value,
82                          gpointer data)
83 {
84   GtkKeyHashEntry *entry = value;
85   GtkKeyHash *key_hash = data;
86
87   g_free (entry->keys);
88   key_hash_insert_entry (key_hash, entry);
89 }
90
91 static void
92 key_hash_keymap_changed (GdkKeymap  *keymap,
93                          GtkKeyHash *key_hash)
94 {
95   /* The keymap changed, so we have to clear and reinsert all our entries
96    */
97   g_hash_table_foreach (key_hash->keycode_hash, key_hash_clear_keycode, NULL);
98
99   /* FIXME: Here we reinsert in random order, but I think we actually have to
100    * insert in the same order as the original order to keep GtkBindingSet happy.
101    */
102   g_hash_table_foreach (key_hash->reverse_hash, key_hash_reinsert_entry, key_hash);
103 }
104 #endif /* HAVE_CHANGED_SIGNAL */
105
106 /**
107  * _gtk_key_hash_new:
108  * @keymap: a #GdkKeymap
109  * @item_destroy_notify: function to be called when items are removed
110  *   from the hash or %NULL.
111  * 
112  * Create a new key hash object for doing binding resolution. 
113  * 
114  * Return value: the newly created object. Free with _gtk_key_hash_free().
115  **/
116 GtkKeyHash *
117 _gtk_key_hash_new (GdkKeymap      *keymap,
118                    GDestroyNotify  item_destroy_notify)
119 {
120   GtkKeyHash *key_hash = g_new (GtkKeyHash, 1);
121
122   key_hash->keymap = keymap;
123 #ifdef HAVE_CHANGED_SIGNAL
124   g_signal_connect (keymap, "changed",
125                     G_CALLBACK (key_hash_keymap_changed), key_hash);
126 #endif  
127
128   key_hash->keycode_hash = g_hash_table_new (g_direct_hash, NULL);
129   key_hash->reverse_hash = g_hash_table_new (g_direct_hash, NULL);
130   key_hash->destroy_notify = item_destroy_notify;
131
132   return key_hash;
133 }
134
135 static void
136 key_hash_free_entry (GtkKeyHash      *key_hash,
137                      GtkKeyHashEntry *entry)
138 {
139   if (key_hash->destroy_notify)
140     (*key_hash->destroy_notify) (entry->value);
141   
142   g_free (entry->keys);
143   g_free (entry);
144 }
145
146 static void
147 key_hash_free_entry_foreach (gpointer key,
148                              gpointer value,
149                              gpointer data)
150 {
151   GtkKeyHashEntry *entry = value;
152   GtkKeyHash *key_hash = data;
153
154   key_hash_free_entry (key_hash, entry);
155 }
156
157 /**
158  * gtk_key_hash_free:
159  * @key_hash: a #GtkKeyHash
160  * 
161  * Destroys a key hash created with gtk_key_hash_new()
162  **/
163 void
164 _gtk_key_hash_free (GtkKeyHash *key_hash)
165 {
166 #if HAVE_CHANGED_SIGNAL  
167   g_signal_handlers_disconnect_by_func (key_hash->keymap,
168                                         G_CALLBACK (key_hash_keymap_changed), key_hash);
169 #endif
170
171   g_hash_table_foreach (key_hash->keycode_hash, key_hash_clear_keycode, NULL);
172   g_hash_table_foreach (key_hash->reverse_hash, key_hash_free_entry_foreach, key_hash);
173   g_hash_table_destroy (key_hash->keycode_hash);
174   g_hash_table_destroy (key_hash->reverse_hash);
175
176   g_free (key_hash);
177 }
178
179 /**
180  * _gtk_key_hash_add_entry:
181  * @key_hash: a #GtkKeyHash
182  * @keyval: key symbol for this binding
183  * @modifiers: modifiers for this binding
184  * @value: value to insert in the key hash
185  * 
186  * Inserts a pair of key symbol and modifier mask into the key hash. 
187  **/
188 void
189 _gtk_key_hash_add_entry (GtkKeyHash      *key_hash,
190                          guint            keyval,
191                          GdkModifierType  modifiers,
192                          gpointer         value)
193 {
194   GtkKeyHashEntry *entry = g_new (GtkKeyHashEntry, 1);
195
196   entry->value = value;
197   entry->keyval = keyval;
198   entry->modifiers = modifiers;
199
200   g_hash_table_insert (key_hash->reverse_hash, value, entry);
201   key_hash_insert_entry (key_hash, entry);
202 }
203
204 /**
205  * _gtk_key_hash_remove_entry:
206  * @key_hash: a #GtkKeyHash
207  * @value: value previously added with _gtk_key_hash_add_entry()
208  * 
209  * Removes a value previously added to the key hash with
210  * _gtk_key_hash_add_entry().
211  **/
212 void
213 _gtk_key_hash_remove_entry (GtkKeyHash *key_hash,
214                             gpointer    value)
215 {
216   GtkKeyHashEntry *entry = g_hash_table_lookup (key_hash->reverse_hash, value);
217   if (entry)
218     {
219       gint i;
220
221       for (i = 0; i < entry->n_keys; i++)
222         {
223           GSList *old_keys = g_hash_table_lookup (key_hash->keycode_hash,
224                                                   GUINT_TO_POINTER (entry->keys[i].keycode));
225
226           GSList *new_keys = g_slist_remove (old_keys, entry);
227           if (old_keys != new_keys)
228             {
229               if (old_keys)
230                 g_hash_table_insert (key_hash->keycode_hash,
231                                      GUINT_TO_POINTER (entry->keys[i].keycode),
232                                      old_keys);
233               else
234                 g_hash_table_remove (key_hash->keycode_hash,
235                                      GUINT_TO_POINTER (entry->keys[i].keycode));
236             }
237         }
238       
239       g_hash_table_remove (key_hash->reverse_hash, value);
240
241       key_hash_free_entry (key_hash, entry);
242       g_free (entry);
243     }
244 }
245
246 /**
247  * _gtk_key_hash_lookup:
248  * @key_hash: a #GtkKeyHash
249  * @hardware_keycode: hardware keycode field from a #GdkEventKey
250  * @state: state field from a #GdkEventKey
251  * @group: group field from a #GdkEventKey
252  * 
253  * Looks up the best matching entry or entries in the hash table for
254  * a given event.
255  * 
256  * Return value: A #GSList of all matching entries. If there were exact
257  *  matches, they are returned, otherwise all fuzzy matches are
258  *  returned. (A fuzzy match is a match in keycode and level, but not
259  *  in group.)
260  **/
261 GSList *
262 _gtk_key_hash_lookup (GtkKeyHash      *key_hash,
263                       guint16          hardware_keycode,
264                       GdkModifierType  state,
265                       gint             group)
266 {
267   GSList *keys = g_hash_table_lookup (key_hash->keycode_hash, GUINT_TO_POINTER ((guint)hardware_keycode));
268   GSList *results = NULL;
269   gboolean have_exact = FALSE;
270   guint keyval;
271   gint effective_group;
272   gint level;
273   GdkModifierType consumed_modifiers;
274
275   gdk_keymap_translate_keyboard_state (key_hash->keymap,
276                                        hardware_keycode, state, group,
277                                        &keyval, &effective_group, &level, &consumed_modifiers);
278
279   GTK_NOTE (KEYBINDINGS,
280             g_message ("Looking up keycode = %u, modifiers = 0x%04x,\n"
281                        "    keyval = %u, group = %d, level = %d, consumed_modifiers = 0x%04x",
282                        hardware_keycode, state, keyval, effective_group, level, consumed_modifiers));
283
284   if (keys)
285     {
286       GSList *tmp_list = keys;
287       while (tmp_list)
288         {
289           GtkKeyHashEntry *entry = tmp_list->data;
290
291           if ((entry->modifiers & ~consumed_modifiers) == (state & ~consumed_modifiers))
292             {
293               gint i;
294
295               if (keyval == entry->keyval) /* Exact match */
296                 {
297                   GTK_NOTE (KEYBINDINGS,
298                             g_message ("  found exact match, keyval = %u, modifiers = 0x%04x",
299                                        entry->keyval, entry->modifiers));
300                   
301                   if (!have_exact)
302                     {
303                       g_slist_free (results);
304                       results = NULL;
305                     }
306
307                   have_exact = TRUE;
308                   results = g_slist_prepend (results, entry->value);
309                 }
310
311               if (!have_exact)
312                 {
313                   for (i = 0; i < entry->n_keys; i++)
314                     {
315                       if (entry->keys[i].keycode == hardware_keycode &&
316                           entry->keys[i].level == level) /* Match for all but group */
317                         {
318                           GTK_NOTE (KEYBINDINGS,
319                                     g_message ("  found group = %d, level = %d",
320                                                entry->keys[i].group, entry->keys[i].level));
321                           results = g_slist_prepend (results, entry->value);
322                           break;
323                         }
324                     }
325                 }
326             }
327
328           tmp_list = tmp_list->next;
329         }
330     }
331
332   return results;
333 }
334
335 /**
336  * _gtk_key_hash_lookup_keyval:
337  * @key_hash: a #GtkKeyHash
338  * @event: a #GtkEvent
339  * 
340  * Looks up the best matching entry or entries in the hash table for a
341  * given keyval/modifiers pair. It's better to use
342  * _gtk_key_hash_lookup() if you have the original #GdkEventKey
343  * available.
344  * 
345  * Return value: A #GSList of all matching entries.
346  **/
347 GSList *
348 _gtk_key_hash_lookup_keyval (GtkKeyHash     *key_hash,
349                              guint           keyval,
350                              GdkModifierType modifiers)
351 {
352   GdkKeymapKey *keys;
353   gint n_keys;
354   GSList *results = NULL;
355   
356   /* Find some random keycode for this keycode
357    */
358   gdk_keymap_get_entries_for_keyval (key_hash->keymap, keyval,
359                                      &keys, &n_keys);
360
361   if (n_keys)
362     {
363       GSList *entries = g_hash_table_lookup (key_hash->keycode_hash, GUINT_TO_POINTER (keys[0].keycode));
364
365       while (entries)
366         {
367           GtkKeyHashEntry *entry = entries->data;
368
369           if (entry->keyval == keyval && entry->modifiers == modifiers)
370             results = g_slist_prepend (results, entry->value);
371
372           entries = entries->next;
373         }
374     }
375
376   g_free (keys);
377           
378   return results;
379 }