]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/modkeys.c
it looks better GNOME/Openbox etc rather than vice versa.
[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
24 /* These masks are constants and the modifier keys are bound to them as
25    anyone sees fit:
26         ShiftMask (1<<0), LockMask (1<<1), ControlMask (1<<2), Mod1Mask (1<<3),
27         Mod2Mask (1<<4), Mod3Mask (1<<5), Mod4Mask (1<<6), Mod5Mask (1<<7)
28 */
29 #define NUM_MASKS 8
30 #define ALL_MASKS 0xff /* an or'ing of all 8 keyboard masks */
31
32 /* Get the bitflag for the n'th modifier mask */
33 #define nth_mask(n) (1 << n)
34
35 static void set_modkey_mask(guchar mask, KeySym sym);
36
37 static XModifierKeymap *modmap;
38 /* This is a bitmask of the different masks for each modifier key */
39 static guchar modkeys_keys[OB_MODKEY_NUM_KEYS];
40
41 void modkeys_startup(gboolean reconfigure)
42 {
43     KeySym *keymap;
44     gint i, j, k;
45     gint min_keycode, max_keycode, keysyms_per_keycode;
46
47     /* reset the keys to not be bound to any masks */
48     for (i = 0; i < OB_MODKEY_NUM_KEYS; ++i)
49         modkeys_keys[i] = 0;
50
51     modmap = XGetModifierMapping(ob_display);
52     g_assert(modmap->max_keypermod > 0);
53
54     XDisplayKeycodes(ob_display, &min_keycode, &max_keycode);
55     keymap = XGetKeyboardMapping(ob_display, min_keycode,
56                                  max_keycode - min_keycode + 1,
57                                  &keysyms_per_keycode);
58
59     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
60     for (i = 0; i < NUM_MASKS; ++i) {
61         /* go through each keycode that is bound to the mask */
62         for (j = 0; j < modmap->max_keypermod; ++j) {
63             KeySym sym;
64             /* get a keycode that is bound to the mask (i) */
65             KeyCode keycode = modmap->modifiermap[i*modmap->max_keypermod + j];
66             /* go through each keysym bound to the given keycode */
67             for (k = 0; k < keysyms_per_keycode; ++k) {
68                 sym = keymap[(keycode-min_keycode) * keysyms_per_keycode + k];
69                 if (sym != NoSymbol) {
70                     /* bind the key to the mask (e.g. Alt_L => Mod1Mask) */
71                     set_modkey_mask(nth_mask(i), sym);
72                 }
73             }
74         }
75     }
76     XFree(keymap);
77 }
78
79 void modkeys_shutdown(gboolean reconfigure)
80 {
81     XFreeModifiermap(modmap);
82 }
83
84 guint modkeys_keycode_to_mask(guint keycode)
85 {
86     gint i, j;
87     guint mask = 0;
88
89     if (keycode == NoSymbol) return 0;
90
91     /* go through each of the modifier masks (eg ShiftMask, CapsMask...) */
92     for (i = 0; i < NUM_MASKS; ++i) {
93         /* go through each keycode that is bound to the mask */
94         for (j = 0; j < modmap->max_keypermod; ++j) {
95             /* compare with a keycode that is bound to the mask (i) */
96             if (modmap->modifiermap[i*modmap->max_keypermod + j] == keycode)
97                 mask |= nth_mask(i);
98         }
99     }
100     return mask;
101 }
102
103 guint modkeys_only_modifier_masks(guint mask)
104 {
105     mask &= ALL_MASKS;
106     /* strip off these lock keys. they shouldn't affect key bindings */
107     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_CAPSLOCK);
108     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
109     mask &= ~modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
110     return mask;
111 }
112
113 guint modkeys_key_to_mask(ObModkeysKey key)
114 {
115     return modkeys_keys[key];
116 }
117
118 static void set_modkey_mask(guchar mask, KeySym sym)
119 {
120     /* find what key this is, and bind it to the mask */
121
122     if (sym == XK_Num_Lock)
123         modkeys_keys[OB_MODKEY_KEY_NUMLOCK] |= mask;
124     else if (sym == XK_Scroll_Lock)
125         modkeys_keys[OB_MODKEY_KEY_SCROLLLOCK] |= mask;
126     else if (sym == XK_Caps_Lock)
127         modkeys_keys[OB_MODKEY_KEY_CAPSLOCK] |= mask;
128     else if (sym == XK_Shift_L || sym == XK_Shift_R)
129         modkeys_keys[OB_MODKEY_KEY_SHIFT] |= mask;
130     else if (sym == XK_Control_L || sym == XK_Control_R)
131         modkeys_keys[OB_MODKEY_KEY_CONTROL] |= mask;
132     else if (sym == XK_Super_L || sym == XK_Super_R)
133         modkeys_keys[OB_MODKEY_KEY_SUPER] |= mask;
134     else if (sym == XK_Hyper_L || sym == XK_Hyper_R)
135         modkeys_keys[OB_MODKEY_KEY_HYPER] |= mask;
136     else if (sym == XK_Alt_L || sym == XK_Alt_R)
137         modkeys_keys[OB_MODKEY_KEY_ALT] |= mask;
138     else if (sym == XK_Meta_L || sym == XK_Meta_R)
139         modkeys_keys[OB_MODKEY_KEY_META] |= mask;
140 }