[Window] Fix automatic pulldown code
[tilda-gobject.git] / eggaccelerators.c
1 /* eggaccelerators.c
2  * Copyright (C) 2002  Red Hat, Inc.; Copyright 1998, 2001 Tim Janik
3  * Developed by Havoc Pennington, Tim Janik
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "eggaccelerators.h"
22
23 #include <string.h>
24 #include <gdk/gdkx.h>
25 #include <gdk/gdkkeysyms.h>
26
27 enum
28 {
29   EGG_MODMAP_ENTRY_SHIFT   = 0,
30   EGG_MODMAP_ENTRY_LOCK    = 1,
31   EGG_MODMAP_ENTRY_CONTROL = 2,
32   EGG_MODMAP_ENTRY_MOD1    = 3,
33   EGG_MODMAP_ENTRY_MOD2    = 4,
34   EGG_MODMAP_ENTRY_MOD3    = 5,
35   EGG_MODMAP_ENTRY_MOD4    = 6,
36   EGG_MODMAP_ENTRY_MOD5    = 7,
37   EGG_MODMAP_ENTRY_LAST    = 8
38 };
39
40 #define MODMAP_ENTRY_TO_MODIFIER(x) (1 << (x))
41
42 typedef struct
43 {
44   EggVirtualModifierType mapping[EGG_MODMAP_ENTRY_LAST];
45
46 } EggModmap;
47
48 const EggModmap* egg_keymap_get_modmap (GdkKeymap *keymap);
49
50 #if 0
51 static inline gboolean
52 is_alt (const gchar *string)
53 {
54   return ((string[0] == '<') &&
55           (string[1] == 'a' || string[1] == 'A') &&
56           (string[2] == 'l' || string[2] == 'L') &&
57           (string[3] == 't' || string[3] == 'T') &&
58           (string[4] == '>'));
59 }
60
61 static inline gboolean
62 is_ctl (const gchar *string)
63 {
64   return ((string[0] == '<') &&
65           (string[1] == 'c' || string[1] == 'C') &&
66           (string[2] == 't' || string[2] == 'T') &&
67           (string[3] == 'l' || string[3] == 'L') &&
68           (string[4] == '>'));
69 }
70
71 static inline gboolean
72 is_modx (const gchar *string)
73 {
74   return ((string[0] == '<') &&
75           (string[1] == 'm' || string[1] == 'M') &&
76           (string[2] == 'o' || string[2] == 'O') &&
77           (string[3] == 'd' || string[3] == 'D') &&
78           (string[4] >= '1' && string[4] <= '5') &&
79           (string[5] == '>'));
80 }
81
82 static inline gboolean
83 is_ctrl (const gchar *string)
84 {
85   return ((string[0] == '<') &&
86           (string[1] == 'c' || string[1] == 'C') &&
87           (string[2] == 't' || string[2] == 'T') &&
88           (string[3] == 'r' || string[3] == 'R') &&
89           (string[4] == 'l' || string[4] == 'L') &&
90           (string[5] == '>'));
91 }
92
93 static inline gboolean
94 is_shft (const gchar *string)
95 {
96   return ((string[0] == '<') &&
97           (string[1] == 's' || string[1] == 'S') &&
98           (string[2] == 'h' || string[2] == 'H') &&
99           (string[3] == 'f' || string[3] == 'F') &&
100           (string[4] == 't' || string[4] == 'T') &&
101           (string[5] == '>'));
102 }
103
104 static inline gboolean
105 is_shift (const gchar *string)
106 {
107   return ((string[0] == '<') &&
108           (string[1] == 's' || string[1] == 'S') &&
109           (string[2] == 'h' || string[2] == 'H') &&
110           (string[3] == 'i' || string[3] == 'I') &&
111           (string[4] == 'f' || string[4] == 'F') &&
112           (string[5] == 't' || string[5] == 'T') &&
113           (string[6] == '>'));
114 }
115
116 static inline gboolean
117 is_control (const gchar *string)
118 {
119   return ((string[0] == '<') &&
120           (string[1] == 'c' || string[1] == 'C') &&
121           (string[2] == 'o' || string[2] == 'O') &&
122           (string[3] == 'n' || string[3] == 'N') &&
123           (string[4] == 't' || string[4] == 'T') &&
124           (string[5] == 'r' || string[5] == 'R') &&
125           (string[6] == 'o' || string[6] == 'O') &&
126           (string[7] == 'l' || string[7] == 'L') &&
127           (string[8] == '>'));
128 }
129
130 static inline gboolean
131 is_release (const gchar *string)
132 {
133   return ((string[0] == '<') &&
134           (string[1] == 'r' || string[1] == 'R') &&
135           (string[2] == 'e' || string[2] == 'E') &&
136           (string[3] == 'l' || string[3] == 'L') &&
137           (string[4] == 'e' || string[4] == 'E') &&
138           (string[5] == 'a' || string[5] == 'A') &&
139           (string[6] == 's' || string[6] == 'S') &&
140           (string[7] == 'e' || string[7] == 'E') &&
141           (string[8] == '>'));
142 }
143
144 static inline gboolean
145 is_meta (const gchar *string)
146 {
147   return ((string[0] == '<') &&
148           (string[1] == 'm' || string[1] == 'M') &&
149           (string[2] == 'e' || string[2] == 'E') &&
150           (string[3] == 't' || string[3] == 'T') &&
151           (string[4] == 'a' || string[4] == 'A') &&
152           (string[5] == '>'));
153 }
154
155 static inline gboolean
156 is_super (const gchar *string)
157 {
158   return ((string[0] == '<') &&
159           (string[1] == 's' || string[1] == 'S') &&
160           (string[2] == 'u' || string[2] == 'U') &&
161           (string[3] == 'p' || string[3] == 'P') &&
162           (string[4] == 'e' || string[4] == 'E') &&
163           (string[5] == 'r' || string[5] == 'R') &&
164           (string[6] == '>'));
165 }
166
167 static inline gboolean
168 is_hyper (const gchar *string)
169 {
170   return ((string[0] == '<') &&
171           (string[1] == 'h' || string[1] == 'H') &&
172           (string[2] == 'y' || string[2] == 'Y') &&
173           (string[3] == 'p' || string[3] == 'P') &&
174           (string[4] == 'e' || string[4] == 'E') &&
175           (string[5] == 'r' || string[5] == 'R') &&
176           (string[6] == '>'));
177 }
178
179 /**
180  * egg_accelerator_parse_virtual:
181  * @accelerator:      string representing an accelerator
182  * @accelerator_key:  return location for accelerator keyval
183  * @accelerator_mods: return location for accelerator modifier mask
184  *
185  * Parses a string representing a virtual accelerator. The format
186  * looks like "&lt;Control&gt;a" or "&lt;Shift&gt;&lt;Alt&gt;F1" or
187  * "&lt;Release&gt;z" (the last one is for key release).  The parser
188  * is fairly liberal and allows lower or upper case, and also
189  * abbreviations such as "&lt;Ctl&gt;" and "&lt;Ctrl&gt;".
190  *
191  * If the parse fails, @accelerator_key and @accelerator_mods will
192  * be set to 0 (zero) and %FALSE will be returned. If the string contains
193  * only modifiers, @accelerator_key will be set to 0 but %TRUE will be
194  * returned.
195  *
196  * The virtual vs. concrete accelerator distinction is a relic of
197  * how the X Window System works; there are modifiers Mod2-Mod5 that
198  * can represent various keyboard keys (numlock, meta, hyper, etc.),
199  * the virtual modifier represents the keyboard key, the concrete
200  * modifier the actual Mod2-Mod5 bits in the key press event.
201  * 
202  * Returns: %TRUE on success.
203  */
204 gboolean
205 egg_accelerator_parse_virtual (const gchar            *accelerator,
206                                guint                  *accelerator_key,
207                                EggVirtualModifierType *accelerator_mods)
208 {
209   guint keyval;
210   GdkModifierType mods;
211   gint len;
212   gboolean bad_keyval;
213   
214   if (accelerator_key)
215     *accelerator_key = 0;
216   if (accelerator_mods)
217     *accelerator_mods = 0;
218
219   g_return_val_if_fail (accelerator != NULL, FALSE);
220
221   bad_keyval = FALSE;
222   
223   keyval = 0;
224   mods = 0;
225   len = strlen (accelerator);
226   while (len)
227     {
228       if (*accelerator == '<')
229         {
230           if (len >= 9 && is_release (accelerator))
231             {
232               accelerator += 9;
233               len -= 9;
234               mods |= EGG_VIRTUAL_RELEASE_MASK;
235             }
236           else if (len >= 9 && is_control (accelerator))
237             {
238               accelerator += 9;
239               len -= 9;
240               mods |= EGG_VIRTUAL_CONTROL_MASK;
241             }
242           else if (len >= 7 && is_shift (accelerator))
243             {
244               accelerator += 7;
245               len -= 7;
246               mods |= EGG_VIRTUAL_SHIFT_MASK;
247             }
248           else if (len >= 6 && is_shft (accelerator))
249             {
250               accelerator += 6;
251               len -= 6;
252               mods |= EGG_VIRTUAL_SHIFT_MASK;
253             }
254           else if (len >= 6 && is_ctrl (accelerator))
255             {
256               accelerator += 6;
257               len -= 6;
258               mods |= EGG_VIRTUAL_CONTROL_MASK;
259             }
260           else if (len >= 6 && is_modx (accelerator))
261             {
262               static const guint mod_vals[] = {
263                 EGG_VIRTUAL_ALT_MASK, EGG_VIRTUAL_MOD2_MASK, EGG_VIRTUAL_MOD3_MASK,
264                 EGG_VIRTUAL_MOD4_MASK, EGG_VIRTUAL_MOD5_MASK
265               };
266
267               len -= 6;
268               accelerator += 4;
269               mods |= mod_vals[*accelerator - '1'];
270               accelerator += 2;
271             }
272           else if (len >= 5 && is_ctl (accelerator))
273             {
274               accelerator += 5;
275               len -= 5;
276               mods |= EGG_VIRTUAL_CONTROL_MASK;
277             }
278           else if (len >= 5 && is_alt (accelerator))
279             {
280               accelerator += 5;
281               len -= 5;
282               mods |= EGG_VIRTUAL_ALT_MASK;
283             }
284           else if (len >= 6 && is_meta (accelerator))
285             {
286               accelerator += 6;
287               len -= 6;
288               mods |= EGG_VIRTUAL_META_MASK;
289             }
290           else if (len >= 7 && is_hyper (accelerator))
291             {
292               accelerator += 7;
293               len -= 7;
294               mods |= EGG_VIRTUAL_HYPER_MASK;
295             }
296           else if (len >= 7 && is_super (accelerator))
297             {
298               accelerator += 7;
299               len -= 7;
300               mods |= EGG_VIRTUAL_SUPER_MASK;
301             }
302           else
303             {
304               gchar last_ch;
305               
306               last_ch = *accelerator;
307               while (last_ch && last_ch != '>')
308                 {
309                   last_ch = *accelerator;
310                   accelerator += 1;
311                   len -= 1;
312                 }
313             }
314         }
315       else
316         {
317           keyval = gdk_keyval_from_name (accelerator);
318           
319           if (keyval == 0)
320             bad_keyval = TRUE;
321           
322           accelerator += len;
323           len -= len;              
324         }
325     }
326   
327   if (accelerator_key)
328     *accelerator_key = gdk_keyval_to_lower (keyval);
329   if (accelerator_mods)
330     *accelerator_mods = mods;
331
332   return !bad_keyval;
333 }
334
335
336 /**
337  * egg_virtual_accelerator_name:
338  * @accelerator_key:  accelerator keyval
339  * @accelerator_mods: accelerator modifier mask
340  * @returns:          a newly-allocated accelerator name
341  * 
342  * Converts an accelerator keyval and modifier mask
343  * into a string parseable by egg_accelerator_parse_virtual().
344  * For example, if you pass in #GDK_q and #EGG_VIRTUAL_CONTROL_MASK,
345  * this function returns "&lt;Control&gt;q".
346  *
347  * The caller of this function must free the returned string.
348  */
349 gchar*
350 egg_virtual_accelerator_name (guint                  accelerator_key,
351                               EggVirtualModifierType accelerator_mods)
352 {
353   static const gchar text_release[] = "<Release>";
354   static const gchar text_shift[] = "<Shift>";
355   static const gchar text_control[] = "<Control>";
356   static const gchar text_mod1[] = "<Alt>";
357   static const gchar text_mod2[] = "<Mod2>";
358   static const gchar text_mod3[] = "<Mod3>";
359   static const gchar text_mod4[] = "<Mod4>";
360   static const gchar text_mod5[] = "<Mod5>";
361   static const gchar text_meta[] = "<Meta>";
362   static const gchar text_super[] = "<Super>";
363   static const gchar text_hyper[] = "<Hyper>";
364   guint l;
365   gchar *keyval_name;
366   gchar *accelerator;
367
368   accelerator_mods &= EGG_VIRTUAL_MODIFIER_MASK;
369
370   keyval_name = gdk_keyval_name (gdk_keyval_to_lower (accelerator_key));
371   if (!keyval_name)
372     keyval_name = "";
373
374   l = 0;
375   if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
376     l += sizeof (text_release) - 1;
377   if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
378     l += sizeof (text_shift) - 1;
379   if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
380     l += sizeof (text_control) - 1;
381   if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
382     l += sizeof (text_mod1) - 1;
383   if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
384     l += sizeof (text_mod2) - 1;
385   if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
386     l += sizeof (text_mod3) - 1;
387   if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
388     l += sizeof (text_mod4) - 1;
389   if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
390     l += sizeof (text_mod5) - 1;
391   if (accelerator_mods & EGG_VIRTUAL_META_MASK)
392     l += sizeof (text_meta) - 1;
393   if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
394     l += sizeof (text_hyper) - 1;
395   if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
396     l += sizeof (text_super) - 1;
397   l += strlen (keyval_name);
398
399   accelerator = g_new (gchar, l + 1);
400
401   l = 0;
402   accelerator[l] = 0;
403   if (accelerator_mods & EGG_VIRTUAL_RELEASE_MASK)
404     {
405       strcpy (accelerator + l, text_release);
406       l += sizeof (text_release) - 1;
407     }
408   if (accelerator_mods & EGG_VIRTUAL_SHIFT_MASK)
409     {
410       strcpy (accelerator + l, text_shift);
411       l += sizeof (text_shift) - 1;
412     }
413   if (accelerator_mods & EGG_VIRTUAL_CONTROL_MASK)
414     {
415       strcpy (accelerator + l, text_control);
416       l += sizeof (text_control) - 1;
417     }
418   if (accelerator_mods & EGG_VIRTUAL_ALT_MASK)
419     {
420       strcpy (accelerator + l, text_mod1);
421       l += sizeof (text_mod1) - 1;
422     }
423   if (accelerator_mods & EGG_VIRTUAL_MOD2_MASK)
424     {
425       strcpy (accelerator + l, text_mod2);
426       l += sizeof (text_mod2) - 1;
427     }
428   if (accelerator_mods & EGG_VIRTUAL_MOD3_MASK)
429     {
430       strcpy (accelerator + l, text_mod3);
431       l += sizeof (text_mod3) - 1;
432     }
433   if (accelerator_mods & EGG_VIRTUAL_MOD4_MASK)
434     {
435       strcpy (accelerator + l, text_mod4);
436       l += sizeof (text_mod4) - 1;
437     }
438   if (accelerator_mods & EGG_VIRTUAL_MOD5_MASK)
439     {
440       strcpy (accelerator + l, text_mod5);
441       l += sizeof (text_mod5) - 1;
442     }
443   if (accelerator_mods & EGG_VIRTUAL_META_MASK)
444     {
445       strcpy (accelerator + l, text_meta);
446       l += sizeof (text_meta) - 1;
447     }
448   if (accelerator_mods & EGG_VIRTUAL_HYPER_MASK)
449     {
450       strcpy (accelerator + l, text_hyper);
451       l += sizeof (text_hyper) - 1;
452     }
453   if (accelerator_mods & EGG_VIRTUAL_SUPER_MASK)
454     {
455       strcpy (accelerator + l, text_super);
456       l += sizeof (text_super) - 1;
457     }
458   
459   strcpy (accelerator + l, keyval_name);
460
461   return accelerator;
462 }
463 #endif
464
465 void
466 egg_keymap_resolve_virtual_modifiers (GdkKeymap              *keymap,
467                                       GdkModifierType         virtual_mods,
468                                       GdkModifierType        *concrete_mods)
469 {
470   GdkModifierType concrete;
471   int i;
472   const EggModmap *modmap;
473
474   g_return_if_fail (GDK_IS_KEYMAP (keymap));
475   g_return_if_fail (concrete_mods != NULL);
476   
477   modmap = egg_keymap_get_modmap (keymap);
478   
479   /* Not so sure about this algorithm. */
480   
481   concrete = 0;
482   i = 0;
483   while (i < EGG_MODMAP_ENTRY_LAST)
484     {
485       if (modmap->mapping[i] & virtual_mods)
486         concrete |= (1 << i);
487
488       ++i;
489     }
490
491   *concrete_mods = concrete;
492 }
493
494 #if 0
495 void
496 egg_keymap_virtualize_modifiers (GdkKeymap              *keymap,
497                                  GdkModifierType         concrete_mods,
498                                  EggVirtualModifierType *virtual_mods)
499 {
500   GdkModifierType virtual;
501   int i;
502   const EggModmap *modmap;
503   
504   g_return_if_fail (GDK_IS_KEYMAP (keymap));
505   g_return_if_fail (virtual_mods != NULL);
506
507   modmap = egg_keymap_get_modmap (keymap);
508   
509   /* Not so sure about this algorithm. */
510   
511   virtual = 0;
512   i = 0;
513   while (i < EGG_MODMAP_ENTRY_LAST)
514     {
515       if ((1 << i) & concrete_mods)
516         {
517           EggVirtualModifierType cleaned;
518           
519           cleaned = modmap->mapping[i] & ~(EGG_VIRTUAL_MOD2_MASK |
520                                            EGG_VIRTUAL_MOD3_MASK |
521                                            EGG_VIRTUAL_MOD4_MASK |
522                                            EGG_VIRTUAL_MOD5_MASK);
523           
524           if (cleaned != 0)
525             {
526               virtual |= cleaned;
527             }
528           else
529             {
530               /* Rather than dropping mod2->mod5 if not bound,
531                * go ahead and use the concrete names
532                */
533               virtual |= modmap->mapping[i];
534             }
535         }
536       
537       ++i;
538     }
539   
540   *virtual_mods = virtual;
541 }
542 #endif
543
544 static void
545 reload_modmap (GdkKeymap *keymap,
546                EggModmap *modmap)
547 {
548   XModifierKeymap *xmodmap;
549   int map_size;
550   int i;
551
552   /* FIXME multihead */
553   xmodmap = XGetModifierMapping (gdk_x11_get_default_xdisplay ());
554
555   memset (modmap->mapping, 0, sizeof (modmap->mapping));
556   
557   /* there are 8 modifiers, and the first 3 are shift, shift lock,
558    * and control
559    */
560   map_size = 8 * xmodmap->max_keypermod;
561   i = 3 * xmodmap->max_keypermod;
562   while (i < map_size)
563     {
564       /* get the key code at this point in the map,
565        * see if its keysym is one we're interested in
566        */
567       int keycode = xmodmap->modifiermap[i];
568       GdkKeymapKey *keys;
569       guint *keyvals;
570       int n_entries;
571       int j;
572       EggVirtualModifierType mask;
573       
574       keys = NULL;
575       keyvals = NULL;
576       n_entries = 0;
577
578       gdk_keymap_get_entries_for_keycode (keymap,
579                                           keycode,
580                                           &keys, &keyvals, &n_entries);
581       
582       mask = 0;
583       j = 0;
584       while (j < n_entries)
585         {          
586           if (keyvals[j] == GDK_Num_Lock)
587             mask |= EGG_VIRTUAL_NUM_LOCK_MASK;
588           else if (keyvals[j] == GDK_Scroll_Lock)
589             mask |= EGG_VIRTUAL_SCROLL_LOCK_MASK;
590           else if (keyvals[j] == GDK_Meta_L ||
591                    keyvals[j] == GDK_Meta_R)
592             mask |= EGG_VIRTUAL_META_MASK;
593           else if (keyvals[j] == GDK_Hyper_L ||
594                    keyvals[j] == GDK_Hyper_R)
595             mask |= EGG_VIRTUAL_HYPER_MASK;
596           else if (keyvals[j] == GDK_Super_L ||
597                    keyvals[j] == GDK_Super_R)
598             mask |= EGG_VIRTUAL_SUPER_MASK;
599           else if (keyvals[j] == GDK_Mode_switch)
600             mask |= EGG_VIRTUAL_MODE_SWITCH_MASK;
601           
602           ++j;
603         }
604
605       /* Mod1Mask is 1 << 3 for example, i.e. the
606        * fourth modifier, i / keyspermod is the modifier
607        * index
608        */      
609       modmap->mapping[i/xmodmap->max_keypermod] |= mask;
610       
611       g_free (keyvals);
612       g_free (keys);      
613       
614       ++i;
615     }
616
617   /* Add in the not-really-virtual fixed entries */
618   modmap->mapping[EGG_MODMAP_ENTRY_SHIFT] |= EGG_VIRTUAL_SHIFT_MASK;
619   modmap->mapping[EGG_MODMAP_ENTRY_CONTROL] |= EGG_VIRTUAL_CONTROL_MASK;
620   modmap->mapping[EGG_MODMAP_ENTRY_LOCK] |= EGG_VIRTUAL_LOCK_MASK;
621   modmap->mapping[EGG_MODMAP_ENTRY_MOD1] |= EGG_VIRTUAL_ALT_MASK;
622   modmap->mapping[EGG_MODMAP_ENTRY_MOD2] |= EGG_VIRTUAL_MOD2_MASK;
623   modmap->mapping[EGG_MODMAP_ENTRY_MOD3] |= EGG_VIRTUAL_MOD3_MASK;
624   modmap->mapping[EGG_MODMAP_ENTRY_MOD4] |= EGG_VIRTUAL_MOD4_MASK;
625   modmap->mapping[EGG_MODMAP_ENTRY_MOD5] |= EGG_VIRTUAL_MOD5_MASK;
626   
627   XFreeModifiermap (xmodmap);
628 }
629
630 const EggModmap*
631 egg_keymap_get_modmap (GdkKeymap *keymap)
632 {
633   EggModmap *modmap;
634
635   /* This is all a hack, much simpler when we can just
636    * modify GDK directly.
637    */
638   
639   modmap = g_object_get_data (G_OBJECT (keymap),
640                               "egg-modmap");
641
642   if (modmap == NULL)
643     {
644       modmap = g_new0 (EggModmap, 1);
645
646       /* FIXME modify keymap change events with an event filter
647        * and force a reload if we get one
648        */
649       
650       reload_modmap (keymap, modmap);
651       
652       g_object_set_data_full (G_OBJECT (keymap),
653                               "egg-modmap",
654                               modmap,
655                               g_free);
656     }
657
658   g_assert (modmap != NULL);
659   
660   return modmap;
661 }