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];
36 gboolean grab_keyboard(gboolean grab)
38 static guint kgrabs = 0;
43 ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
44 FALSE, GrabModeAsync, GrabModeAsync,
45 event_lasttime) == Success;
48 } else if (kgrabs > 0) {
50 XUngrabKeyboard(ob_display, event_lasttime);
57 gboolean grab_pointer(gboolean grab, ObCursor cur)
59 static guint pgrabs = 0;
64 ret = XGrabPointer(ob_display, screen_support_win,
65 False, GRAB_PTR_MASK, GrabModeAsync,
67 ob_cursor(cur), event_lasttime) == Success;
70 } else if (pgrabs > 0) {
72 XUngrabPointer(ob_display, event_lasttime);
78 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
80 static guint pgrabs = 0;
85 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
86 GrabModeAsync, GrabModeAsync, TRUE,
88 event_lasttime) == Success;
91 } else if (pgrabs > 0) {
93 XUngrabPointer(ob_display, event_lasttime);
99 gint grab_server(gboolean grab)
101 static guint sgrabs = 0;
104 XGrabServer(ob_display);
105 XSync(ob_display, FALSE);
107 } else if (sgrabs > 0) {
109 XUngrabServer(ob_display);
116 void grab_startup(gboolean reconfig)
120 if (reconfig) return;
123 mask_list[i++] = LockMask;
124 mask_list[i++] = NumLockMask;
125 mask_list[i++] = LockMask | NumLockMask;
126 mask_list[i++] = ScrollLockMask;
127 mask_list[i++] = ScrollLockMask | LockMask;
128 mask_list[i++] = ScrollLockMask | NumLockMask;
129 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
130 g_assert(i == MASK_LIST_SIZE);
133 void grab_shutdown(gboolean reconfig)
135 if (reconfig) return;
137 while (grab_keyboard(FALSE));
138 while (grab_pointer(FALSE, OB_CURSOR_NONE));
139 while (grab_pointer_window(FALSE, OB_CURSOR_NONE, None));
140 while (grab_server(FALSE));
143 void grab_button_full(guint button, guint state, Window win, guint mask,
144 int pointer_mode, ObCursor cur)
148 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
149 xerror_occured = FALSE;
150 for (i = 0; i < MASK_LIST_SIZE; ++i)
151 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
152 pointer_mode, GrabModeSync, None, ob_cursor(cur));
153 xerror_set_ignore(FALSE);
155 g_warning("failed to grab button %d modifiers %d", button, state);
158 void grab_button(guint button, guint state, Window win, guint mask)
160 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
163 void ungrab_button(guint button, guint state, Window win)
167 for (i = 0; i < MASK_LIST_SIZE; ++i)
168 XUngrabButton(ob_display, button, state | mask_list[i], win);
171 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
175 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
176 xerror_occured = FALSE;
177 for (i = 0; i < MASK_LIST_SIZE; ++i)
178 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
179 GrabModeAsync, keyboard_mode);
180 xerror_set_ignore(FALSE);
182 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
185 void ungrab_all_keys(Window win)
187 XUngrabKey(ob_display, AnyKey, AnyModifier, win);