]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
update copyright step 2
[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 Ben 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 "openbox.h"
22 #include "event.h"
23 #include "xerror.h"
24 #include "screen.h"
25
26 #include <glib.h>
27 #include <X11/Xlib.h>
28
29 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
30 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
31
32 #define MASK_LIST_SIZE 8
33
34 /*! A list of all possible combinations of keyboard lock masks */
35 static guint mask_list[MASK_LIST_SIZE];
36 static guint kgrabs = 0;
37 static guint pgrabs = 0;
38
39 gboolean grab_on_keyboard()
40 {
41 return kgrabs > 0;
42 }
43
44 gboolean grab_on_pointer()
45 {
46 return pgrabs > 0;
47 }
48
49 gboolean grab_keyboard(gboolean grab)
50 {
51 gboolean ret = FALSE;
52
53 if (grab) {
54 if (kgrabs++ == 0) {
55 ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
56 FALSE, GrabModeAsync, GrabModeAsync,
57 event_lasttime) == Success;
58 if (!ret)
59 --kgrabs;
60 } else
61 ret = TRUE;
62 } else if (kgrabs > 0) {
63 if (--kgrabs == 0)
64 XUngrabKeyboard(ob_display, event_lasttime);
65 ret = TRUE;
66 }
67
68 return ret;
69 }
70
71 gboolean grab_pointer(gboolean grab, ObCursor cur)
72 {
73 gboolean ret = FALSE;
74
75 if (grab) {
76 if (pgrabs++ == 0) {
77 ret = XGrabPointer(ob_display, screen_support_win,
78 False, GRAB_PTR_MASK, GrabModeAsync,
79 GrabModeAsync, None,
80 ob_cursor(cur), event_lasttime) == Success;
81 if (!ret)
82 --pgrabs;
83 } else
84 ret = TRUE;
85 } else if (pgrabs > 0) {
86 if (--pgrabs == 0) {
87 XUngrabPointer(ob_display, event_lasttime);
88 }
89 ret = TRUE;
90 }
91 return ret;
92 }
93
94 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
95 {
96 gboolean ret = FALSE;
97
98 if (grab) {
99 if (pgrabs++ == 0) {
100 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
101 GrabModeAsync, GrabModeAsync, None,
102 ob_cursor(cur),
103 event_lasttime) == Success;
104 if (!ret)
105 --pgrabs;
106 } else
107 ret = TRUE;
108 } else if (pgrabs > 0) {
109 if (--pgrabs == 0) {
110 XUngrabPointer(ob_display, event_lasttime);
111 }
112 ret = TRUE;
113 }
114 return ret;
115 }
116
117 gint grab_server(gboolean grab)
118 {
119 static guint sgrabs = 0;
120 if (grab) {
121 if (sgrabs++ == 0) {
122 XGrabServer(ob_display);
123 XSync(ob_display, FALSE);
124 }
125 } else if (sgrabs > 0) {
126 if (--sgrabs == 0) {
127 XUngrabServer(ob_display);
128 XFlush(ob_display);
129 }
130 }
131 return sgrabs;
132 }
133
134 void grab_startup(gboolean reconfig)
135 {
136 guint i = 0;
137
138 if (reconfig) return;
139
140 mask_list[i++] = 0;
141 mask_list[i++] = LockMask;
142 mask_list[i++] = NumLockMask;
143 mask_list[i++] = LockMask | NumLockMask;
144 mask_list[i++] = ScrollLockMask;
145 mask_list[i++] = ScrollLockMask | LockMask;
146 mask_list[i++] = ScrollLockMask | NumLockMask;
147 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
148 g_assert(i == MASK_LIST_SIZE);
149 }
150
151 void grab_shutdown(gboolean reconfig)
152 {
153 if (reconfig) return;
154
155 while (grab_keyboard(FALSE));
156 while (grab_pointer(FALSE, OB_CURSOR_NONE));
157 while (grab_pointer_window(FALSE, OB_CURSOR_NONE, None));
158 while (grab_server(FALSE));
159 }
160
161 void grab_button_full(guint button, guint state, Window win, guint mask,
162 gint pointer_mode, ObCursor cur)
163 {
164 guint i;
165
166 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
167 xerror_occured = FALSE;
168 for (i = 0; i < MASK_LIST_SIZE; ++i)
169 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
170 pointer_mode, GrabModeSync, None, ob_cursor(cur));
171 xerror_set_ignore(FALSE);
172 if (xerror_occured)
173 g_warning("failed to grab button %d modifiers %d", button, state);
174 }
175
176 void grab_button(guint button, guint state, Window win, guint mask)
177 {
178 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
179 }
180
181 void ungrab_button(guint button, guint state, Window win)
182 {
183 guint i;
184
185 for (i = 0; i < MASK_LIST_SIZE; ++i)
186 XUngrabButton(ob_display, button, state | mask_list[i], win);
187 }
188
189 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
190 {
191 guint i;
192
193 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
194 xerror_occured = FALSE;
195 for (i = 0; i < MASK_LIST_SIZE; ++i)
196 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
197 GrabModeAsync, keyboard_mode);
198 xerror_set_ignore(FALSE);
199 if (xerror_occured)
200 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
201 }
202
203 void ungrab_all_keys(Window win)
204 {
205 XUngrabKey(ob_display, AnyKey, AnyModifier, win);
206 }
This page took 0.049955 seconds and 5 git commands to generate.