]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
Merge branch 'master' into chaz
[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-2007 Dana 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 "screen.h"
24 #include "debug.h"
25 #include "obt/display.h"
26 #include "obt/keyboard.h"
27
28 #include <glib.h>
29 #include <X11/Xlib.h>
30
31 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
32 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
33
34 #define MASK_LIST_SIZE 8
35
36 /*! A list of all possible combinations of keyboard lock masks */
37 static guint mask_list[MASK_LIST_SIZE];
38 static guint kgrabs = 0;
39 static guint pgrabs = 0;
40 /*! The time at which the last grab was made */
41 static Time grab_time = CurrentTime;
42 static gint passive_count = 0;
43 static ObtIC *ic = NULL;
44
45 static Time ungrab_time(void)
46 {
47 Time t = event_time();
48 if (grab_time == CurrentTime ||
49 !(t == CurrentTime || event_time_after(t, grab_time)))
50 /* When the time moves backward on the server, then we can't use
51 the grab time because that will be in the future. So instead we
52 have to use CurrentTime.
53
54 "XUngrabPointer does not release the pointer if the specified time
55 is earlier than the last-pointer-grab time or is later than the
56 current X server time."
57 */
58 t = CurrentTime; /*grab_time;*/
59 return t;
60 }
61
62 static Window grab_window(void)
63 {
64 return screen_support_win;
65 }
66
67 gboolean grab_on_keyboard(void)
68 {
69 return kgrabs > 0;
70 }
71
72 gboolean grab_on_pointer(void)
73 {
74 return pgrabs > 0;
75 }
76
77 ObtIC *grab_input_context(void)
78 {
79 return ic;
80 }
81
82 gboolean grab_keyboard_full(gboolean grab)
83 {
84 gboolean ret = FALSE;
85
86 if (grab) {
87 if (kgrabs++ == 0) {
88 ret = XGrabKeyboard(obt_display, grab_window(),
89 False, GrabModeAsync, GrabModeAsync,
90 event_time()) == Success;
91 if (!ret)
92 --kgrabs;
93 else {
94 passive_count = 0;
95 grab_time = event_time();
96 }
97 } else
98 ret = TRUE;
99 } else if (kgrabs > 0) {
100 if (--kgrabs == 0) {
101 XUngrabKeyboard(obt_display, ungrab_time());
102 }
103 ret = TRUE;
104 }
105
106 return ret;
107 }
108
109 gboolean grab_pointer_full(gboolean grab, gboolean owner_events,
110 gboolean confine, ObCursor cur)
111 {
112 gboolean ret = FALSE;
113
114 if (grab) {
115 if (pgrabs++ == 0) {
116 ret = XGrabPointer(obt_display, grab_window(), owner_events,
117 GRAB_PTR_MASK,
118 GrabModeAsync, GrabModeAsync,
119 (confine ? obt_root(ob_screen) : None),
120 ob_cursor(cur), event_time()) == Success;
121 if (!ret)
122 --pgrabs;
123 else
124 grab_time = event_time();
125 } else
126 ret = TRUE;
127 } else if (pgrabs > 0) {
128 if (--pgrabs == 0) {
129 XUngrabPointer(obt_display, ungrab_time());
130 }
131 ret = TRUE;
132 }
133 return ret;
134 }
135
136 gint grab_server(gboolean grab)
137 {
138 static guint sgrabs = 0;
139 if (grab) {
140 if (sgrabs++ == 0) {
141 XGrabServer(obt_display);
142 XSync(obt_display, FALSE);
143 }
144 } else if (sgrabs > 0) {
145 if (--sgrabs == 0) {
146 XUngrabServer(obt_display);
147 XFlush(obt_display);
148 }
149 }
150 return sgrabs;
151 }
152
153 void grab_startup(gboolean reconfig)
154 {
155 guint i = 0;
156 guint num, caps, scroll;
157
158 num = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK);
159 caps = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_CAPSLOCK);
160 scroll = obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK);
161
162 mask_list[i++] = 0;
163 mask_list[i++] = num;
164 mask_list[i++] = caps;
165 mask_list[i++] = scroll;
166 mask_list[i++] = num | caps;
167 mask_list[i++] = num | scroll;
168 mask_list[i++] = caps | scroll;
169 mask_list[i++] = num | caps | scroll;
170 g_assert(i == MASK_LIST_SIZE);
171
172 ic = obt_keyboard_context_new(obt_root(ob_screen), grab_window());
173 }
174
175 void grab_shutdown(gboolean reconfig)
176 {
177 obt_keyboard_context_unref(ic);
178 ic = NULL;
179
180 if (reconfig) return;
181
182 while (ungrab_keyboard());
183 while (ungrab_pointer());
184 while (grab_server(FALSE));
185 }
186
187 void grab_button_full(guint button, guint state, Window win, guint mask,
188 gint pointer_mode, ObCursor cur)
189 {
190 guint i;
191
192 /* can get BadAccess from these */
193 obt_display_ignore_errors(TRUE);
194 for (i = 0; i < MASK_LIST_SIZE; ++i)
195 XGrabButton(obt_display, button, state | mask_list[i], win, False,
196 mask, pointer_mode, GrabModeAsync, None, ob_cursor(cur));
197 obt_display_ignore_errors(FALSE);
198 if (obt_display_error_occured)
199 ob_debug("Failed to grab button %d modifiers %d", button, state);
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(obt_display, button, state | mask_list[i], win);
208 }
209
210 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
211 {
212 guint i;
213
214 /* can get BadAccess' from these */
215 obt_display_ignore_errors(TRUE);
216 for (i = 0; i < MASK_LIST_SIZE; ++i)
217 XGrabKey(obt_display, keycode, state | mask_list[i], win, FALSE,
218 GrabModeAsync, keyboard_mode);
219 obt_display_ignore_errors(FALSE);
220 if (obt_display_error_occured)
221 ob_debug("Failed to grab keycode %d modifiers %d", keycode, state);
222 }
223
224 void ungrab_all_keys(Window win)
225 {
226 XUngrabKey(obt_display, AnyKey, AnyModifier, win);
227 }
228
229 void grab_key_passive_count(int change)
230 {
231 if (grab_on_keyboard()) return;
232 passive_count += change;
233 if (passive_count < 0) passive_count = 0;
234 }
235
236 void ungrab_passive_key(void)
237 {
238 /*ob_debug("ungrabbing %d passive grabs\n", passive_count);*/
239 if (passive_count) {
240 /* kill our passive grab */
241 XUngrabKeyboard(obt_display, event_time());
242 passive_count = 0;
243 }
244 }
This page took 0.044032 seconds and 4 git commands to generate.