0 /*
1 * grab.c - key/button grabbing
2 *
3 * The routines in this files are equivalent to their X counterparts,
4 * except that they also grab/ungrab all combinations of Num Lock,
5 * Caps Lock and Scroll Lock.
6 */
7
8 /*
9 * Copyright 2006 Johan Veenhuizen
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a
12 * copy of this software and associated documentation files (the "Software"),
13 * to deal in the Software without restriction, including without limitation
14 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 * and/or sell copies of the Software, and to permit persons to whom the
16 * Software is furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included
19 * in all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 * DEALINGS IN THE SOFTWARE.
28 */
29
30 #include <X11/keysym.h>
31 #include <X11/Xlib.h>
32 #include <X11/Xutil.h>
33
34 #include "lib.h"
35
36 /*
37 * All possible combinations of the lock masks.
38 */
39 static unsigned lockmasks[] = {
40 0,
41 LockMask, /* Caps Lock */
42 Mod2Mask, /* Num Lock */
43 Mod5Mask, /* Scroll Lock */
44 LockMask | Mod2Mask,
45 LockMask | Mod5Mask,
46 Mod2Mask | Mod5Mask,
47 LockMask | Mod2Mask | Mod5Mask
48 };
49
50 /*
51 * XGrabKey()
52 */
53 void grabkey(Display *display, int keycode, unsigned modifiers,
54 Window grab_window, Bool owner_events, int pointer_mode, int keyboard_mode)
55 {
56 int i;
57
58 if (modifiers == AnyModifier)
59 XGrabKey(display, keycode, AnyModifier,
60 grab_window, owner_events, pointer_mode, keyboard_mode);
61 else
62 for (i = 0; i < NELEM(lockmasks); i++)
63 XGrabKey(display, keycode, lockmasks[i] | modifiers,
64 grab_window, owner_events, pointer_mode,
65 keyboard_mode);
66 }
67
68 /*
69 * XUngrabKey()
70 */
71 void ungrabkey(Display *display, int keycode, unsigned modifiers,
72 Window grab_window)
73 {
74 int i;
75
76 if (modifiers == AnyModifier)
77 XUngrabKey(display, keycode, AnyModifier, grab_window);
78 else
79 for (i = 0; i < NELEM(lockmasks); i++)
80 XUngrabKey(display, keycode, lockmasks[i] | modifiers,
81 grab_window);
82 }
83
84 /*
85 * XGrabButton()
86 */
87 void grabbutton(Display *display, unsigned button, unsigned modifiers,
88 Window grab_window, Bool owner_events, unsigned event_mask,
89 int pointer_mode, int keyboard_mode, Window confine_to, Cursor cursor)
90 {
91 int i;
92
93 if (modifiers == AnyModifier)
94 XGrabButton(display, button, AnyModifier,
95 grab_window, owner_events, event_mask, pointer_mode,
96 keyboard_mode, confine_to, cursor);
97 else
98 for (i = 0; i < NELEM(lockmasks); i++)
99 XGrabButton(display, button, lockmasks[i] | modifiers,
100 grab_window, owner_events, event_mask,
101 pointer_mode, keyboard_mode, confine_to, cursor);
102 }
103
104 /*
105 * XUngrabButton()
106 */
107 void ungrabbutton(Display *display, unsigned button, unsigned modifiers,
108 Window grab_window)
109 {
110 int i;
111
112 if (modifiers == AnyModifier)
113 XUngrabButton(display, button, AnyModifier, grab_window);
114 else
115 for (i = 0; i < NELEM(lockmasks); i++)
116 XUngrabButton(display, button,
117 lockmasks[i] | modifiers, grab_window);
118 }
119