]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
s/ob_display/obt_display/ and remove ob_display
[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 "modkeys.h"
22 #include "openbox.h"
23 #include "event.h"
24 #include "screen.h"
25 #include "debug.h"
26 #include "obt/display.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
44 static Time ungrab_time(void)
45 {
46 Time t = event_curtime;
47 if (grab_time == CurrentTime ||
48 !(t == CurrentTime || event_time_after(t, grab_time)))
49 /* When the time moves backward on the server, then we can't use
50 the grab time because that will be in the future. So instead we
51 have to use CurrentTime.
52
53 "XUngrabPointer does not release the pointer if the specified time
54 is earlier than the last-pointer-grab time or is later than the
55 current X server time."
56 */
57 t = CurrentTime; /*grab_time;*/
58 return t;
59 }
60
61 gboolean grab_on_keyboard(void)
62 {
63 return kgrabs > 0;
64 }
65
66 gboolean grab_on_pointer(void)
67 {
68 return pgrabs > 0;
69 }
70
71 gboolean grab_keyboard_full(gboolean grab)
72 {
73 gboolean ret = FALSE;
74
75 if (grab) {
76 if (kgrabs++ == 0) {
77 ret = XGrabKeyboard(obt_display,
78 RootWindow(obt_display, ob_screen),
79 False, GrabModeAsync, GrabModeAsync,
80 event_curtime) == Success;
81 if (!ret)
82 --kgrabs;
83 else {
84 passive_count = 0;
85 grab_time = event_curtime;
86 }
87 } else
88 ret = TRUE;
89 } else if (kgrabs > 0) {
90 if (--kgrabs == 0) {
91 XUngrabKeyboard(obt_display, ungrab_time());
92 }
93 ret = TRUE;
94 }
95
96 return ret;
97 }
98
99 gboolean grab_pointer_full(gboolean grab, gboolean owner_events,
100 gboolean confine, ObCursor cur)
101 {
102 gboolean ret = FALSE;
103
104 if (grab) {
105 if (pgrabs++ == 0) {
106 ret = XGrabPointer(obt_display, screen_support_win, owner_events,
107 GRAB_PTR_MASK,
108 GrabModeAsync, GrabModeAsync,
109 (confine ? RootWindow(obt_display, ob_screen) :
110 None),
111 ob_cursor(cur), event_curtime) == Success;
112 if (!ret)
113 --pgrabs;
114 else
115 grab_time = event_curtime;
116 } else
117 ret = TRUE;
118 } else if (pgrabs > 0) {
119 if (--pgrabs == 0) {
120 XUngrabPointer(obt_display, ungrab_time());
121 }
122 ret = TRUE;
123 }
124 return ret;
125 }
126
127 gint grab_server(gboolean grab)
128 {
129 static guint sgrabs = 0;
130 if (grab) {
131 if (sgrabs++ == 0) {
132 XGrabServer(obt_display);
133 XSync(obt_display, FALSE);
134 }
135 } else if (sgrabs > 0) {
136 if (--sgrabs == 0) {
137 XUngrabServer(obt_display);
138 XFlush(obt_display);
139 }
140 }
141 return sgrabs;
142 }
143
144 void grab_startup(gboolean reconfig)
145 {
146 guint i = 0;
147 guint num, caps, scroll;
148
149 num = modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
150 caps = modkeys_key_to_mask(OB_MODKEY_KEY_CAPSLOCK);
151 scroll = modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
152
153 mask_list[i++] = 0;
154 mask_list[i++] = num;
155 mask_list[i++] = caps;
156 mask_list[i++] = scroll;
157 mask_list[i++] = num | caps;
158 mask_list[i++] = num | scroll;
159 mask_list[i++] = caps | scroll;
160 mask_list[i++] = num | caps | scroll;
161 g_assert(i == MASK_LIST_SIZE);
162 }
163
164 void grab_shutdown(gboolean reconfig)
165 {
166 if (reconfig) return;
167
168 while (ungrab_keyboard());
169 while (ungrab_pointer());
170 while (grab_server(FALSE));
171 }
172
173 void grab_button_full(guint button, guint state, Window win, guint mask,
174 gint pointer_mode, ObCursor cur)
175 {
176 guint i;
177
178 /* can get BadAccess from these */
179 obt_display_ignore_errors(TRUE);
180 for (i = 0; i < MASK_LIST_SIZE; ++i)
181 XGrabButton(obt_display, button, state | mask_list[i], win, False,
182 mask, pointer_mode, GrabModeAsync, None, ob_cursor(cur));
183 obt_display_ignore_errors(FALSE);
184 if (obt_display_error_occured)
185 ob_debug("Failed to grab button %d modifiers %d", button, state);
186 }
187
188 void ungrab_button(guint button, guint state, Window win)
189 {
190 guint i;
191
192 for (i = 0; i < MASK_LIST_SIZE; ++i)
193 XUngrabButton(obt_display, button, state | mask_list[i], win);
194 }
195
196 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
197 {
198 guint i;
199
200 /* can get BadAccess' from these */
201 obt_display_ignore_errors(TRUE);
202 for (i = 0; i < MASK_LIST_SIZE; ++i)
203 XGrabKey(obt_display, keycode, state | mask_list[i], win, FALSE,
204 GrabModeAsync, keyboard_mode);
205 obt_display_ignore_errors(FALSE);
206 if (obt_display_error_occured)
207 ob_debug("Failed to grab keycode %d modifiers %d", keycode, state);
208 }
209
210 void ungrab_all_keys(Window win)
211 {
212 XUngrabKey(obt_display, AnyKey, AnyModifier, win);
213 }
214
215 void grab_key_passive_count(int change)
216 {
217 if (grab_on_keyboard()) return;
218 passive_count += change;
219 if (passive_count < 0) passive_count = 0;
220 }
221
222 void ungrab_passive_key(void)
223 {
224 /*ob_debug("ungrabbing %d passive grabs\n", passive_count);*/
225 if (passive_count) {
226 /* kill out passive grab */
227 XUngrabKeyboard(obt_display, event_curtime);
228 passive_count = 0;
229 }
230 }
This page took 0.043126 seconds and 5 git commands to generate.