]> Pileus Git - ~andy/gtk/blob - gtk/gtkkeyhash.c
Partial fix for problem where keypad keys acted as shift-arrows in an
[~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 (new_keys != old_keys)
228             {
229               if (new_keys)
230                 g_hash_table_insert (key_hash->keycode_hash,
231                                      GUINT_TO_POINTER (entry->keys[i].keycode),
232                                      new_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     }
243 }
244
245 static gint
246 lookup_result_compare (gconstpointer a,
247                        gconstpointer b)
248 {
249   const GtkKeyHashEntry *entry_a = a;
250   const GtkKeyHashEntry *entry_b = b;
251   guint modifiers;
252
253   gint n_bits_a = 0;
254   gint n_bits_b = 0;
255
256   modifiers = entry_a->modifiers;
257   while (modifiers)
258     {
259       if (modifiers & 1)
260         n_bits_a++;
261       modifiers >>= 1;
262     }
263
264   modifiers = entry_b->modifiers;
265   while (modifiers)
266     {
267       if (modifiers & 1)
268         n_bits_b++;
269       modifiers >>= 1;
270     }
271
272   return n_bits_a < n_bits_b ? -1 : (n_bits_a == n_bits_b ? 0 : 1);
273   
274 }
275
276 /* Sort a list of results so that matches with less modifiers come
277  * before matches with more modifiers
278  */
279 static GSList *
280 sort_lookup_results (GSList *slist)
281 {
282   return g_slist_sort (slist, lookup_result_compare);
283 }
284
285 /**
286  * _gtk_key_hash_lookup:
287  * @key_hash: a #GtkKeyHash
288  * @hardware_keycode: hardware keycode field from a #GdkEventKey
289  * @state: state field from a #GdkEventKey
290  * @group: group field from a #GdkEventKey
291  * 
292  * Looks up the best matching entry or entries in the hash table for
293  * a given event. The results are sorted so that entries with less
294  * modifiers come before entries with more modifiers.
295  * 
296  * Return value: A #GSList of all matching entries. If there were exact
297  *  matches, they are returned, otherwise all fuzzy matches are
298  *  returned. (A fuzzy match is a match in keycode and level, but not
299  *  in group.)
300  **/
301 GSList *
302 _gtk_key_hash_lookup (GtkKeyHash      *key_hash,
303                       guint16          hardware_keycode,
304                       GdkModifierType  state,
305                       gint             group)
306 {
307   GSList *keys = g_hash_table_lookup (key_hash->keycode_hash, GUINT_TO_POINTER ((guint)hardware_keycode));
308   GSList *results = NULL;
309   gboolean have_exact = FALSE;
310   guint keyval;
311   gint effective_group;
312   gint level;
313   GdkModifierType consumed_modifiers;
314
315   gdk_keymap_translate_keyboard_state (key_hash->keymap,
316                                        hardware_keycode, state, group,
317                                        &keyval, &effective_group, &level, &consumed_modifiers);
318
319   GTK_NOTE (KEYBINDINGS,
320             g_message ("Looking up keycode = %u, modifiers = 0x%04x,\n"
321                        "    keyval = %u, group = %d, level = %d, consumed_modifiers = 0x%04x",
322                        hardware_keycode, state, keyval, effective_group, level, consumed_modifiers));
323
324   if (keys)
325     {
326       GSList *tmp_list = keys;
327       while (tmp_list)
328         {
329           GtkKeyHashEntry *entry = tmp_list->data;
330
331           if ((entry->modifiers & ~consumed_modifiers) == (state & ~consumed_modifiers))
332             {
333               gint i;
334
335               if (keyval == entry->keyval) /* Exact match */
336                 {
337                   GTK_NOTE (KEYBINDINGS,
338                             g_message ("  found exact match, keyval = %u, modifiers = 0x%04x",
339                                        entry->keyval, entry->modifiers));
340                   
341                   if (!have_exact)
342                     {
343                       g_slist_free (results);
344                       results = NULL;
345                     }
346
347                   have_exact = TRUE;
348                   results = g_slist_prepend (results, entry->value);
349                 }
350
351               if (!have_exact)
352                 {
353                   for (i = 0; i < entry->n_keys; i++)
354                     {
355                       if (entry->keys[i].keycode == hardware_keycode &&
356                           entry->keys[i].level == level) /* Match for all but group */
357                         {
358                           GTK_NOTE (KEYBINDINGS,
359                                     g_message ("  found group = %d, level = %d",
360                                                entry->keys[i].group, entry->keys[i].level));
361                           results = g_slist_prepend (results, entry->value);
362                           break;
363                         }
364                     }
365                 }
366             }
367
368           tmp_list = tmp_list->next;
369         }
370     }
371
372   return sort_lookup_results (results);
373 }
374
375 /**
376  * _gtk_key_hash_lookup_keyval:
377  * @key_hash: a #GtkKeyHash
378  * @event: a #GtkEvent
379  * 
380  * Looks up the best matching entry or entries in the hash table for a
381  * given keyval/modifiers pair. It's better to use
382  * _gtk_key_hash_lookup() if you have the original #GdkEventKey
383  * available.  The results are sorted so that entries with less
384  * modifiers come before entries with more modifiers.
385  * 
386  * Return value: A #GSList of all matching entries.
387  **/
388 GSList *
389 _gtk_key_hash_lookup_keyval (GtkKeyHash     *key_hash,
390                              guint           keyval,
391                              GdkModifierType modifiers)
392 {
393   GdkKeymapKey *keys;
394   gint n_keys;
395   GSList *results = NULL;
396
397   if (!keyval)                  /* Key without symbol */
398     return NULL;
399
400   /* Find some random keycode for this keycode
401    */
402   gdk_keymap_get_entries_for_keyval (key_hash->keymap, keyval,
403                                      &keys, &n_keys);
404
405   if (n_keys)
406     {
407       GSList *entries = g_hash_table_lookup (key_hash->keycode_hash, GUINT_TO_POINTER (keys[0].keycode));
408
409       while (entries)
410         {
411           GtkKeyHashEntry *entry = entries->data;
412
413           if (entry->keyval == keyval && entry->modifiers == modifiers)
414             results = g_slist_prepend (results, entry->value);
415
416           entries = entries->next;
417         }
418     }
419
420   g_free (keys);
421           
422   return sort_lookup_results (results);
423 }