]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
only pass thru events when the menu is open, don't for other stuff
[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) 2006 Mikael Magnusson
5 Copyright (c) 2003 Ben Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "grab.h"
21 #include "openbox.h"
22 #include "event.h"
23 #include "xerror.h"
24 #include "screen.h"
25
26 #include <glib.h>
27 #include <X11/Xlib.h>
28
29 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
30 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
31
32 #define MASK_LIST_SIZE 8
33
34 /*! A list of all possible combinations of keyboard lock masks */
35 static guint mask_list[MASK_LIST_SIZE];
36 static guint kgrabs = 0;
37 static guint pgrabs = 0;
38 /*! The time at which the last grab was made */
39 static Time grab_time = CurrentTime;
40
41 static Time ungrab_time()
42 {
43 Time t = event_curtime;
44 if (!(t == CurrentTime || event_time_after(t, grab_time)))
45 /* When the time moves backward on the server, then we can't use
46 the grab time because that will be in the future. So instead we
47 have to use CurrentTime.
48
49 "XUngrabPointer does not release the pointer if the specified time
50 is earlier than the last-pointer-grab time or is later than the
51 current X server time."
52 */
53 t = CurrentTime; /*grab_time;*/
54 return t;
55 }
56
57 gboolean grab_on_keyboard()
58 {
59 return kgrabs > 0;
60 }
61
62 gboolean grab_on_pointer()
63 {
64 return pgrabs > 0;
65 }
66
67 gboolean grab_keyboard(gboolean grab)
68 {
69 gboolean ret = FALSE;
70
71 if (grab) {
72 if (kgrabs++ == 0) {
73 ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
74 False, GrabModeAsync, GrabModeAsync,
75 event_curtime) == Success;
76 if (!ret)
77 --kgrabs;
78 else
79 grab_time = event_curtime;
80 } else
81 ret = TRUE;
82 } else if (kgrabs > 0) {
83 if (--kgrabs == 0) {
84 XUngrabKeyboard(ob_display, ungrab_time());
85 }
86 ret = TRUE;
87 }
88
89 return ret;
90 }
91
92 gboolean grab_pointer(gboolean grab, gboolean owner_events, ObCursor cur)
93 {
94 gboolean ret = FALSE;
95
96 if (grab) {
97 if (pgrabs++ == 0) {
98 ret = XGrabPointer(ob_display, screen_support_win, owner_events,
99 GRAB_PTR_MASK,
100 GrabModeAsync, GrabModeAsync, None,
101 ob_cursor(cur), event_curtime) == Success;
102 if (!ret)
103 --pgrabs;
104 else
105 grab_time = event_curtime;
106 } else
107 ret = TRUE;
108 } else if (pgrabs > 0) {
109 if (--pgrabs == 0) {
110 XUngrabPointer(ob_display, ungrab_time());
111 }
112 ret = TRUE;
113 }
114 return ret;
115 }
116
117 gboolean grab_pointer_window(gboolean grab, gboolean owner_events,
118 ObCursor cur, Window win)
119 {
120 gboolean ret = FALSE;
121
122 if (grab) {
123 if (pgrabs++ == 0) {
124 ret = XGrabPointer(ob_display, win, owner_events,
125 GRAB_PTR_MASK,
126 GrabModeAsync, GrabModeAsync, None,
127 ob_cursor(cur),
128 event_curtime) == Success;
129 if (!ret)
130 --pgrabs;
131 else
132 grab_time = event_curtime;
133 } else
134 ret = TRUE;
135 } else if (pgrabs > 0) {
136 if (--pgrabs == 0) {
137 XUngrabPointer(ob_display, ungrab_time());
138 }
139 ret = TRUE;
140 }
141 return ret;
142 }
143
144 gint grab_server(gboolean grab)
145 {
146 static guint sgrabs = 0;
147 if (grab) {
148 if (sgrabs++ == 0) {
149 XGrabServer(ob_display);
150 XSync(ob_display, FALSE);
151 }
152 } else if (sgrabs > 0) {
153 if (--sgrabs == 0) {
154 XUngrabServer(ob_display);
155 XFlush(ob_display);
156 }
157 }
158 return sgrabs;
159 }
160
161 void grab_startup(gboolean reconfig)
162 {
163 guint i = 0;
164
165 if (reconfig) return;
166
167 mask_list[i++] = 0;
168 mask_list[i++] = LockMask;
169 mask_list[i++] = NumLockMask;
170 mask_list[i++] = LockMask | NumLockMask;
171 mask_list[i++] = ScrollLockMask;
172 mask_list[i++] = ScrollLockMask | LockMask;
173 mask_list[i++] = ScrollLockMask | NumLockMask;
174 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
175 g_assert(i == MASK_LIST_SIZE);
176 }
177
178 void grab_shutdown(gboolean reconfig)
179 {
180 if (reconfig) return;
181
182 while (grab_keyboard(FALSE));
183 while (grab_pointer(FALSE, FALSE, OB_CURSOR_NONE));
184 while (grab_pointer_window(FALSE, FALSE, OB_CURSOR_NONE, None));
185 while (grab_server(FALSE));
186 }
187
188 void grab_button_full(guint button, guint state, Window win, guint mask,
189 gint pointer_mode, ObCursor cur)
190 {
191 guint i;
192
193 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
194 xerror_occured = FALSE;
195 for (i = 0; i < MASK_LIST_SIZE; ++i)
196 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
197 pointer_mode, GrabModeSync, None, ob_cursor(cur));
198 xerror_set_ignore(FALSE);
199 if (xerror_occured)
200 g_warning("failed to grab button %d modifiers %d", button, state);
201 }
202
203 void grab_button(guint button, guint state, Window win, guint mask)
204 {
205 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
206 }
207
208 void ungrab_button(guint button, guint state, Window win)
209 {
210 guint i;
211
212 for (i = 0; i < MASK_LIST_SIZE; ++i)
213 XUngrabButton(ob_display, button, state | mask_list[i], win);
214 }
215
216 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
217 {
218 guint i;
219
220 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
221 xerror_occured = FALSE;
222 for (i = 0; i < MASK_LIST_SIZE; ++i)
223 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
224 GrabModeAsync, keyboard_mode);
225 xerror_set_ignore(FALSE);
226 if (xerror_occured)
227 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
228 }
229
230 void ungrab_all_keys(Window win)
231 {
232 XUngrabKey(ob_display, AnyKey, AnyModifier, win);
233 }
This page took 0.048277 seconds and 5 git commands to generate.