]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
same shit different pile
[chaz/openbox] / openbox / grab.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 grab.c for the Openbox window manager
4 Copyright (c) 2003 Ben 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 "grab.h"
20 #include "openbox.h"
21 #include "event.h"
22 #include "xerror.h"
23 #include "screen.h"
24
25 #include <glib.h>
26 #include <X11/Xlib.h>
27
28 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
29 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
30
31 #define MASK_LIST_SIZE 8
32
33 /*! A list of all possible combinations of keyboard lock masks */
34 static unsigned int mask_list[MASK_LIST_SIZE];
35 static guint kgrabs = 0;
36 static guint pgrabs = 0;
37
38 gboolean grab_on_keyboard()
39 {
40 return kgrabs > 0;
41 }
42
43 gboolean grab_on_pointer()
44 {
45 return pgrabs > 0;
46 }
47
48 gboolean grab_keyboard(gboolean grab)
49 {
50 gboolean ret = FALSE;
51
52 if (grab) {
53 if (kgrabs++ == 0) {
54 ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
55 FALSE, GrabModeAsync, GrabModeAsync,
56 event_lasttime) == Success;
57 if (!ret)
58 --kgrabs;
59 } else
60 ret = TRUE;
61 } else if (kgrabs > 0) {
62 if (--kgrabs == 0)
63 XUngrabKeyboard(ob_display, event_lasttime);
64 ret = TRUE;
65 }
66
67 return ret;
68 }
69
70 gboolean grab_pointer(gboolean grab, ObCursor cur)
71 {
72 gboolean ret = FALSE;
73
74 if (grab) {
75 if (pgrabs++ == 0) {
76 ret = XGrabPointer(ob_display, screen_support_win,
77 False, GRAB_PTR_MASK, GrabModeAsync,
78 GrabModeAsync, FALSE,
79 ob_cursor(cur), event_lasttime) == Success;
80 if (!ret)
81 --pgrabs;
82 } else
83 ret = TRUE;
84 } else if (pgrabs > 0) {
85 if (--pgrabs == 0) {
86 XUngrabPointer(ob_display, event_lasttime);
87 }
88 ret = TRUE;
89 }
90 return ret;
91 }
92
93 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
94 {
95 gboolean ret = FALSE;
96
97 if (grab) {
98 if (pgrabs++ == 0) {
99 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
100 GrabModeAsync, GrabModeAsync, TRUE,
101 ob_cursor(cur),
102 event_lasttime) == Success;
103 if (!ret)
104 --pgrabs;
105 } else
106 ret = TRUE;
107 } else if (pgrabs > 0) {
108 if (--pgrabs == 0) {
109 XUngrabPointer(ob_display, event_lasttime);
110 }
111 ret = TRUE;
112 }
113 return ret;
114 }
115
116 gint grab_server(gboolean grab)
117 {
118 static guint sgrabs = 0;
119 if (grab) {
120 if (sgrabs++ == 0) {
121 XGrabServer(ob_display);
122 XSync(ob_display, FALSE);
123 }
124 } else if (sgrabs > 0) {
125 if (--sgrabs == 0) {
126 XUngrabServer(ob_display);
127 XFlush(ob_display);
128 }
129 }
130 return sgrabs;
131 }
132
133 void grab_startup(gboolean reconfig)
134 {
135 guint i = 0;
136
137 if (reconfig) return;
138
139 mask_list[i++] = 0;
140 mask_list[i++] = LockMask;
141 mask_list[i++] = NumLockMask;
142 mask_list[i++] = LockMask | NumLockMask;
143 mask_list[i++] = ScrollLockMask;
144 mask_list[i++] = ScrollLockMask | LockMask;
145 mask_list[i++] = ScrollLockMask | NumLockMask;
146 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
147 g_assert(i == MASK_LIST_SIZE);
148 }
149
150 void grab_shutdown(gboolean reconfig)
151 {
152 if (reconfig) return;
153
154 while (grab_keyboard(FALSE));
155 while (grab_pointer(FALSE, OB_CURSOR_NONE));
156 while (grab_pointer_window(FALSE, OB_CURSOR_NONE, None));
157 while (grab_server(FALSE));
158 }
159
160 void grab_button_full(guint button, guint state, Window win, guint mask,
161 int pointer_mode, ObCursor cur)
162 {
163 guint i;
164
165 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
166 xerror_occured = FALSE;
167 for (i = 0; i < MASK_LIST_SIZE; ++i)
168 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
169 pointer_mode, GrabModeSync, None, ob_cursor(cur));
170 xerror_set_ignore(FALSE);
171 if (xerror_occured)
172 g_warning("failed to grab button %d modifiers %d", button, state);
173 }
174
175 void grab_button(guint button, guint state, Window win, guint mask)
176 {
177 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
178 }
179
180 void ungrab_button(guint button, guint state, Window win)
181 {
182 guint i;
183
184 for (i = 0; i < MASK_LIST_SIZE; ++i)
185 XUngrabButton(ob_display, button, state | mask_list[i], win);
186 }
187
188 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
189 {
190 guint i;
191
192 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
193 xerror_occured = FALSE;
194 for (i = 0; i < MASK_LIST_SIZE; ++i)
195 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
196 GrabModeAsync, keyboard_mode);
197 xerror_set_ignore(FALSE);
198 if (xerror_occured)
199 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
200 }
201
202 void ungrab_all_keys(Window win)
203 {
204 XUngrabKey(ob_display, AnyKey, AnyModifier, win);
205 }
This page took 0.047257 seconds and 4 git commands to generate.