]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
count the EnterNotify events and tell openbox to ignore them when ungrabbing the...
[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 else
58 ret = TRUE;
59 } else if (kgrabs > 0) {
60 if (--kgrabs == 0)
61 XUngrabKeyboard(ob_display, event_lasttime);
62 ret = TRUE;
63 }
64
65 return ret;
66 }
67
68 gboolean grab_pointer(gboolean grab, ObCursor cur)
69 {
70 gboolean ret = FALSE;
71
72 if (grab) {
73 if (pgrabs++ == 0)
74 ret = XGrabPointer(ob_display, screen_support_win,
75 False, GRAB_PTR_MASK, GrabModeAsync,
76 GrabModeAsync, FALSE,
77 ob_cursor(cur), event_lasttime) == Success;
78 else
79 ret = TRUE;
80 } else if (pgrabs > 0) {
81 if (--pgrabs == 0) {
82 XUngrabPointer(ob_display, event_lasttime);
83
84 /* ignore all enter events caused by ungrabbing the pointer */
85 {
86 GSList *saved = NULL, *it;
87 XEvent *e;
88 guint n = 0;
89
90 XSync(ob_display, FALSE);
91
92 /* count the events */
93 while (TRUE) {
94 e = g_new(XEvent, 1);
95 if (XCheckTypedEvent(ob_display, EnterNotify, e)) {
96 saved = g_slist_append(saved, e);
97 ++n;
98 } else {
99 g_free(e);
100 break;
101 }
102 }
103 /* put the events back */
104 for (it = saved; it; it = g_slist_next(it)) {
105 XPutBackEvent(ob_display, it->data);
106 g_free(it->data);
107 }
108 g_slist_free(saved);
109 /* ignore the events */
110 event_ignore_enter_focus(n);
111 }
112 }
113 ret = TRUE;
114 }
115 return ret;
116 }
117
118 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
119 {
120 gboolean ret = FALSE;
121
122 if (grab) {
123 if (pgrabs++ == 0)
124 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
125 GrabModeAsync, GrabModeAsync, TRUE,
126 ob_cursor(cur),
127 event_lasttime) == Success;
128 else
129 ret = TRUE;
130 } else if (pgrabs > 0) {
131 if (--pgrabs == 0)
132 XUngrabPointer(ob_display, event_lasttime);
133 ret = TRUE;
134 }
135 return ret;
136 }
137
138 gint grab_server(gboolean grab)
139 {
140 static guint sgrabs = 0;
141 if (grab) {
142 if (sgrabs++ == 0) {
143 XGrabServer(ob_display);
144 XSync(ob_display, FALSE);
145 }
146 } else if (sgrabs > 0) {
147 if (--sgrabs == 0) {
148 XUngrabServer(ob_display);
149 XFlush(ob_display);
150 }
151 }
152 return sgrabs;
153 }
154
155 void grab_startup(gboolean reconfig)
156 {
157 guint i = 0;
158
159 if (reconfig) return;
160
161 mask_list[i++] = 0;
162 mask_list[i++] = LockMask;
163 mask_list[i++] = NumLockMask;
164 mask_list[i++] = LockMask | NumLockMask;
165 mask_list[i++] = ScrollLockMask;
166 mask_list[i++] = ScrollLockMask | LockMask;
167 mask_list[i++] = ScrollLockMask | NumLockMask;
168 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
169 g_assert(i == MASK_LIST_SIZE);
170 }
171
172 void grab_shutdown(gboolean reconfig)
173 {
174 if (reconfig) return;
175
176 while (grab_keyboard(FALSE));
177 while (grab_pointer(FALSE, OB_CURSOR_NONE));
178 while (grab_pointer_window(FALSE, OB_CURSOR_NONE, None));
179 while (grab_server(FALSE));
180 }
181
182 void grab_button_full(guint button, guint state, Window win, guint mask,
183 int pointer_mode, ObCursor cur)
184 {
185 guint i;
186
187 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
188 xerror_occured = FALSE;
189 for (i = 0; i < MASK_LIST_SIZE; ++i)
190 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
191 pointer_mode, GrabModeSync, None, ob_cursor(cur));
192 xerror_set_ignore(FALSE);
193 if (xerror_occured)
194 g_warning("failed to grab button %d modifiers %d", button, state);
195 }
196
197 void grab_button(guint button, guint state, Window win, guint mask)
198 {
199 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
200 }
201
202 void ungrab_button(guint button, guint state, Window win)
203 {
204 guint i;
205
206 for (i = 0; i < MASK_LIST_SIZE; ++i)
207 XUngrabButton(ob_display, button, state | mask_list[i], win);
208 }
209
210 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
211 {
212 guint i;
213
214 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
215 xerror_occured = FALSE;
216 for (i = 0; i < MASK_LIST_SIZE; ++i)
217 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
218 GrabModeAsync, keyboard_mode);
219 xerror_set_ignore(FALSE);
220 if (xerror_occured)
221 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
222 }
223
224 void ungrab_all_keys(Window win)
225 {
226 XUngrabKey(ob_display, AnyKey, AnyModifier, win);
227 }
This page took 0.046877 seconds and 5 git commands to generate.