Fix compile failure when X_HAVE_UTF8_STRING is not defined
[openbox.git] / openbox / grab.c
blob4518c4339890dc435e744b3d8c996e842a5dce61
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 grab.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
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"
28 #include <glib.h>
29 #include <X11/Xlib.h>
31 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
32 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
34 #define MASK_LIST_SIZE 8
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;
45 static Time ungrab_time(void)
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.
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."
58 t = CurrentTime; /*grab_time;*/
59 return t;
62 static Window grab_window(void)
64 return screen_support_win;
67 gboolean grab_on_keyboard(void)
69 return kgrabs > 0;
72 gboolean grab_on_pointer(void)
74 return pgrabs > 0;
77 ObtIC *grab_input_context(void)
79 return ic;
82 gboolean grab_keyboard_full(gboolean grab)
84 gboolean ret = FALSE;
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();
97 } else
98 ret = TRUE;
99 } else if (kgrabs > 0) {
100 if (--kgrabs == 0) {
101 XUngrabKeyboard(obt_display, ungrab_time());
103 ret = TRUE;
106 return ret;
109 gboolean grab_pointer_full(gboolean grab, gboolean owner_events,
110 gboolean confine, ObCursor cur)
112 gboolean ret = FALSE;
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());
131 ret = TRUE;
133 return ret;
136 gint grab_server(gboolean grab)
138 static guint sgrabs = 0;
139 if (grab) {
140 if (sgrabs++ == 0) {
141 XGrabServer(obt_display);
142 XSync(obt_display, FALSE);
144 } else if (sgrabs > 0) {
145 if (--sgrabs == 0) {
146 XUngrabServer(obt_display);
147 XFlush(obt_display);
150 return sgrabs;
153 void grab_startup(gboolean reconfig)
155 guint i = 0;
156 guint num, caps, scroll;
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);
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);
172 ic = obt_keyboard_context_new(obt_root(ob_screen), grab_window());
175 void grab_shutdown(gboolean reconfig)
177 obt_keyboard_context_unref(ic);
178 ic = NULL;
180 if (reconfig) return;
182 while (ungrab_keyboard());
183 while (ungrab_pointer());
184 while (grab_server(FALSE));
187 void grab_button_full(guint button, guint state, Window win, guint mask,
188 gint pointer_mode, ObCursor cur)
190 guint i;
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);
202 void ungrab_button(guint button, guint state, Window win)
204 guint i;
206 for (i = 0; i < MASK_LIST_SIZE; ++i)
207 XUngrabButton(obt_display, button, state | mask_list[i], win);
210 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
212 guint i;
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);
224 void ungrab_all_keys(Window win)
226 XUngrabKey(obt_display, AnyKey, AnyModifier, win);
229 void grab_key_passive_count(int change)
231 if (grab_on_keyboard()) return;
232 passive_count += change;
233 if (passive_count < 0) passive_count = 0;
236 void ungrab_passive_key(void)
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;