]> icculus.org git repositories - dana/openbox.git/blob - openbox/modkeys.c
ignore control_l/r shift_l/r and caps_lock bindings. Control, Shift and NumLock are...
[dana/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 static XModifierKeymap *modmap;
39 static KeySym *keymap;
40 static gint min_keycode, max_keycode, keysyms_per_keycode;
41 /* This is a bitmask of the different masks for each modifier key */
42 static guchar modkeys_keys[OB_MODKEY_NUM_KEYS];
43
44 void modkeys_startup(gboolean reconfigure)
45 {
46     gint i, j, k;
47
48     /* reset the keys to not be bound to any masks */
49     for (i = 0; i < OB_MODKEY_NUM_KEYS; ++i)
50         modkeys_keys[i] = 0;
51
52     modmap = XGetModifierMapping(ob_display);
53     g_assert(modmap->max_keypermod > 0);
54
55     XDisplayKeycodes(ob_display, &min_keycode, &max_keycode);
56     keymap = XGetKeyboardMapping(ob_display, min_keycode,
57                                  max_keycode - min_keycode + 1,
58                                  &keysyms_per_keycode);
59
60     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
61     for (i = 0; i < NUM_MASKS; ++i) {
62         /* go through each keycode that is bound to the mask */
63         for (j = 0; j < modmap->max_keypermod; ++j) {
64             KeySym sym;
65             /* get a keycode that is bound to the mask (i) */
66             KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
67             if (keycode) {
68                 /* go through each keysym bound to the given keycode */
69                 for (k = 0; k < keysyms_per_keycode; ++k) {
70                     sym = keymap[(keycode-min_keycode) * keysyms_per_keycode +
71                                  k];
72                     if (sym != NoSymbol) {
73                         /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
74                         set_modkey_mask(nth_mask(i), sym);
75                     }
76                 }
77             }
78         }
79     }
80
81     /* CapsLock, Shift, and Control are special and hard-coded */
82     modkeys_keys[OB_MODKEY_KEY_CAPSLOCK] = LockMask;
83     modkeys_keys[OB_MODKEY_KEY_SHIFT] = ShiftMask;
84     modkeys_keys[OB_MODKEY_KEY_CONTROL] = ControlMask;
85 }
86
87 void modkeys_shutdown(gboolean reconfigure)
88 {
89     XFreeModifiermap(modmap);
90     XFree(keymap);
91 }
92
93 guint modkeys_keycode_to_mask(guint keycode)
94 {
95     gint i, j;
96     guint mask = 0;
97
98     if (keycode == NoSymbol) return 0;
99
100     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
101     for (i = 0; i < NUM_MASKS; ++i) {
102         /* go through each keycode that is bound to the mask */
103         for (j = 0; j < modmap->max_keypermod; ++j) {
104             /* compare with a keycode that is bound to the mask (i) */
105             if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
106                 mask |= nth_mask(i);
107         }
108     }
109     return mask;
110 }
111
112 guint modkeys_only_modifier_masks(guint mask)
113 {
114     mask &= ALL_MASKS;
115     /* strip off these lock keys. they shouldn't affect key bindings */
116     mask &= ~LockMask; /* use the LockMask, not what capslock is bound to,
117                           because you could bind it to something else and it
118                           should work as that modifier then. i think capslock
119                           is weird in xkb. */
120     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
121     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
122     return mask;
123 }
124
125 guint modkeys_key_to_mask(ObModkeysKey key)
126 {
127     return modkeys_keys[key];
128 }
129
130 static void set_modkey_mask(guchar mask, KeySym sym)
131 {
132     /* find what key this is, and bind it to the mask */
133
134     if (sym == XK_Num_Lock)
135         modkeys_keys[OB_MODKEY_KEY_NUMLOCK] |= mask;
136     else if (sym == XK_Scroll_Lock)
137         modkeys_keys[OB_MODKEY_KEY_SCROLLLOCK] |= mask;
138     else if (sym == XK_Super_L || sym == XK_Super_R)
139         modkeys_keys[OB_MODKEY_KEY_SUPER] |= mask;
140     else if (sym == XK_Hyper_L || sym == XK_Hyper_R)
141         modkeys_keys[OB_MODKEY_KEY_HYPER] |= mask;
142     else if (sym == XK_Alt_L || sym == XK_Alt_R)
143         modkeys_keys[OB_MODKEY_KEY_ALT] |= mask;
144     else if (sym == XK_Meta_L || sym == XK_Meta_R)
145         modkeys_keys[OB_MODKEY_KEY_META] |= mask;
146     /* CapsLock, Shift, and Control are special and hard-coded */
147 }
148
149 KeyCode modkeys_sym_to_code(KeySym sym)
150 {
151     gint i, j;
152
153     /* go through each keycode and look for the keysym */
154     for (i = min_keycode; i <= max_keycode; ++i)
155         for (j = 0; j < keysyms_per_keycode; ++j)
156             if (sym == keymap[(i-min_keycode) * keysyms_per_keycode + j])
157                 return i;
158     return 0;
159 }
160