1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 grab.c for the Openbox window manager
4 Copyright (c) 2003 Ben Jansens
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.
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.
16 See the COPYING file for a copy of the GNU General Public License.
28 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
29 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
31 #define MASK_LIST_SIZE 8
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;
38 gboolean grab_on_keyboard()
43 gboolean grab_on_pointer()
48 gboolean grab_keyboard(gboolean grab)
54 ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
55 FALSE, GrabModeAsync, GrabModeAsync,
56 event_lasttime) == Success;
61 } else if (kgrabs > 0) {
63 XUngrabKeyboard(ob_display, event_lasttime);
70 gboolean grab_pointer(gboolean grab, ObCursor cur)
76 ret = XGrabPointer(ob_display, screen_support_win,
77 False, GRAB_PTR_MASK, GrabModeAsync,
79 ob_cursor(cur), event_lasttime) == Success;
84 } else if (pgrabs > 0) {
86 XUngrabPointer(ob_display, event_lasttime);
93 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
99 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
100 GrabModeAsync, GrabModeAsync, TRUE,
102 event_lasttime) == Success;
107 } else if (pgrabs > 0) {
109 XUngrabPointer(ob_display, event_lasttime);
116 gint grab_server(gboolean grab)
118 static guint sgrabs = 0;
121 XGrabServer(ob_display);
122 XSync(ob_display, FALSE);
124 } else if (sgrabs > 0) {
126 XUngrabServer(ob_display);
133 void grab_startup(gboolean reconfig)
137 if (reconfig) return;
140 mask_list[i++] = LockMask;
141 mask_list[i++] = NumLockMask;
142 mask_list[i++] = LockMask | NumLockMask;
143 mask_list[i++] = ScrollLockMask;
144 mask_list[i++] = ScrollLockMask | LockMask;
145 mask_list[i++] = ScrollLockMask | NumLockMask;
146 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
147 g_assert(i == MASK_LIST_SIZE);
150 void grab_shutdown(gboolean reconfig)
152 if (reconfig) return;
154 while (grab_keyboard(FALSE));
155 while (grab_pointer(FALSE, OB_CURSOR_NONE));
156 while (grab_pointer_window(FALSE, OB_CURSOR_NONE, None));
157 while (grab_server(FALSE));
160 void grab_button_full(guint button, guint state, Window win, guint mask,
161 int pointer_mode, ObCursor cur)
165 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
166 xerror_occured = FALSE;
167 for (i = 0; i < MASK_LIST_SIZE; ++i)
168 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
169 pointer_mode, GrabModeSync, None, ob_cursor(cur));
170 xerror_set_ignore(FALSE);
172 g_warning("failed to grab button %d modifiers %d", button, state);
175 void grab_button(guint button, guint state, Window win, guint mask)
177 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
180 void ungrab_button(guint button, guint state, Window win)
184 for (i = 0; i < MASK_LIST_SIZE; ++i)
185 XUngrabButton(ob_display, button, state | mask_list[i], win);
188 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
192 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
193 xerror_occured = FALSE;
194 for (i = 0; i < MASK_LIST_SIZE; ++i)
195 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
196 GrabModeAsync, keyboard_mode);
197 xerror_set_ignore(FALSE);
199 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
202 void ungrab_all_keys(Window win)
204 XUngrabKey(ob_display, AnyKey, AnyModifier, win);