Fix calling functions with wrong number of arguments
[mikachu/openbox.git] / openbox / modkeys.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    modkeys.c for the Openbox window manager
4    Copyright (c) 2007        Dana Jansens
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10
11    This program 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
14    GNU General Public License for more details.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "modkeys.h"
20 #include "openbox.h"
21
22 #include <X11/Xlib.h>
23 #include <X11/keysym.h>
24
25 /* These masks are constants and the modifier keys are bound to them as
26    anyone sees fit:
27         ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
28         Mod2Mask (1<<4), Mod3Mask (1<<5), Mod4Mask (1<<6), Mod5Mask (1<<7)
29 */
30 #define NUM_MASKS 8
31 #define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
32
33 /* Get the bitflag for the n'th modifier mask */
34 #define nth_mask(n) (1 << n)
35
36 static void set_modkey_mask(guchar mask, KeySym sym);
37
38 /* This is 8 lists of keycodes that are bound to the given mod mask.
39    If contains more than the one given to us by X cuz XKB is weird apparently.
40    We will look up all keycodes for a given keysym that is bound to the mask,
41    and add them all here.
42
43    With XKB, you can have a keycode bound to a modifier that isn't in the
44    modifier map somehow.  So this means that when we try translate from the
45    KeyRelease to a mod mask, we are unable to.  So this array stores *all*
46    the KeyCodes for each KeySym for each KeyCode bound to a mod mask.
47    Confused? Haha...
48
49    ModMask -> n KeyCodes -> n*m KeySyms (up to m for each KeyCode) ->
50      n*m*p KeyCodes (up to p for each KeySym)
51 */
52 static GArray *modmap[NUM_MASKS];
53 static KeySym *keymap;
54 static gint min_keycode, max_keycode, keysyms_per_keycode;
55 /* This is a bitmask of the different masks for each modifier key */
56 static guchar modkeys_keys[OB_MODKEY_NUM_KEYS];
57
58 static gboolean alt_l = FALSE;
59 static gboolean meta_l = FALSE;
60 static gboolean super_l = FALSE;
61 static gboolean hyper_l = FALSE;
62
63 void modkeys_startup(gboolean reconfigure)
64 {
65     static XModifierKeymap *xmodmap;
66     gint i, j, k, l, m;
67
68     /* reset the keys to not be bound to any masks */
69     for (i = 0; i < OB_MODKEY_NUM_KEYS; ++i)
70         modkeys_keys[i] = 0;
71
72     xmodmap = XGetModifierMapping(ob_display);
73     g_assert(xmodmap->max_keypermod > 0);
74
75     XDisplayKeycodes(ob_display, &min_keycode, &max_keycode);
76     keymap = XGetKeyboardMapping(ob_display, min_keycode,
77                                  max_keycode - min_keycode + 1,
78                                  &keysyms_per_keycode);
79
80     alt_l = meta_l = super_l = hyper_l = FALSE;
81
82     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
83     for (i = 0; i < NUM_MASKS; ++i) {
84         /* reset the modmap list */
85         modmap[i] = g_array_new(FALSE, FALSE, sizeof(KeyCode));
86
87         /* go through each keycode that is bound to the mask */
88         for (j = 0; j < xmodmap->max_keypermod; ++j) {
89             KeySym sym;
90             /* get a keycode that is bound to the mask (i) */
91             KeyCode keycode = xmodmap->modifiermap[i*xmodmap->max_keypermod+j];
92             if (keycode) {
93                 /* go through each keysym bound to the given keycode */
94                 for (k = 0; k < keysyms_per_keycode; ++k) {
95                     sym = keymap[(keycode-min_keycode) * keysyms_per_keycode +
96                                  k];
97                     if (sym != NoSymbol) {
98                         /* find all keycodes for the given keysym */
99                         for (l = min_keycode; l <= max_keycode; ++l)
100                             for (m = 0; m < keysyms_per_keycode; ++m)
101                                 if (keymap[(l-min_keycode) *
102                                            keysyms_per_keycode + m] == sym)
103                                 {
104                                     /* add all keycodes for the keysym to our
105                                        modmap */
106                                     g_array_append_val(modmap[i], l);
107                                 }
108
109                         /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
110                         set_modkey_mask(nth_mask(i), sym);
111                     }
112                 }
113             }
114         }
115     }
116
117     /* CapsLock, Shift, and Control are special and hard-coded */
118     modkeys_keys[OB_MODKEY_KEY_CAPSLOCK] = LockMask;
119     modkeys_keys[OB_MODKEY_KEY_SHIFT] = ShiftMask;
120     modkeys_keys[OB_MODKEY_KEY_CONTROL] = ControlMask;
121
122     XFreeModifiermap(xmodmap);
123 }
124
125 void modkeys_shutdown(gboolean reconfigure)
126 {
127     guint i;
128
129     for (i = 0; i < NUM_MASKS; ++i)
130         g_array_free(modmap[i], TRUE);
131     XFree(keymap);
132 }
133
134 guint modkeys_keycode_to_mask(guint keycode)
135 {
136     guint i, j;
137     guint mask = 0;
138
139     if (keycode == NoSymbol) return 0;
140
141     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
142     for (i = 0; i < NUM_MASKS; ++i) {
143         /* go through each keycode that is bound to the mask */
144         for (j = 0; j < modmap[i]->len; ++j) {
145             /* compare with a keycode that is bound to the mask (i) */
146             if (g_array_index(modmap[i], KeyCode, j) == keycode)
147                 mask |= nth_mask(i);
148         }
149     }
150     return mask;
151 }
152
153 guint modkeys_only_modifier_masks(guint mask)
154 {
155     mask &= ALL_MASKS;
156     /* strip off these lock keys. they shouldn't affect key bindings */
157     mask &= ~LockMask; /* use the LockMask, not what capslock is bound to,
158                           because you could bind it to something else and it
159                           should work as that modifier then. i think capslock
160                           is weird in xkb. */
161     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
162     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
163     return mask;
164 }
165
166 guint modkeys_key_to_mask(ObModkeysKey key)
167 {
168     return modkeys_keys[key];
169 }
170
171 static void set_modkey_mask(guchar mask, KeySym sym)
172 {
173     /* find what key this is, and bind it to the mask */
174
175     if (sym == XK_Num_Lock)
176         modkeys_keys[OB_MODKEY_KEY_NUMLOCK] |= mask;
177     else if (sym == XK_Scroll_Lock)
178         modkeys_keys[OB_MODKEY_KEY_SCROLLLOCK] |= mask;
179
180     else if (sym == XK_Super_L && super_l)
181         modkeys_keys[OB_MODKEY_KEY_SUPER] |= mask;
182     else if (sym == XK_Super_L && !super_l)
183         /* left takes precident over right, so erase any masks the right
184            key may have set */
185         modkeys_keys[OB_MODKEY_KEY_SUPER] = mask, super_l = TRUE;
186     else if (sym == XK_Super_R && !super_l)
187         modkeys_keys[OB_MODKEY_KEY_SUPER] |= mask;
188
189     else if (sym == XK_Hyper_L && hyper_l)
190         modkeys_keys[OB_MODKEY_KEY_HYPER] |= mask;
191     else if (sym == XK_Hyper_L && !hyper_l)
192         modkeys_keys[OB_MODKEY_KEY_HYPER] = mask, hyper_l = TRUE;
193     else if (sym == XK_Hyper_R && !hyper_l)
194         modkeys_keys[OB_MODKEY_KEY_HYPER] |= mask;
195
196     else if (sym == XK_Alt_L && alt_l)
197         modkeys_keys[OB_MODKEY_KEY_ALT] |= mask;
198     else if (sym == XK_Alt_L && !alt_l)
199         modkeys_keys[OB_MODKEY_KEY_ALT] = mask, alt_l = TRUE;
200     else if (sym == XK_Alt_R && !alt_l)
201         modkeys_keys[OB_MODKEY_KEY_ALT] |= mask;
202
203     else if (sym == XK_Meta_L && meta_l)
204         modkeys_keys[OB_MODKEY_KEY_META] |= mask;
205     else if (sym == XK_Meta_L && !meta_l)
206         modkeys_keys[OB_MODKEY_KEY_META] = mask, meta_l = TRUE;
207     else if (sym == XK_Meta_R && !meta_l)
208         modkeys_keys[OB_MODKEY_KEY_META] |= mask;
209
210     /* CapsLock, Shift, and Control are special and hard-coded */
211 }
212
213 KeyCode modkeys_sym_to_code(KeySym sym)
214 {
215     gint i, j;
216
217     /* go through each keycode and look for the keysym */
218     for (i = min_keycode; i <= max_keycode; ++i)
219         for (j = 0; j < keysyms_per_keycode; ++j)
220             if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j])
221                 return i;
222     return 0;
223 }
224