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