add copyright headers, adjust --version output to include copyright, and --help outpu...
[dana/openbox.git] / openbox / grab.c
1 /* -*- indent-tabs-mode: t; tab-width: 4; c-basic-offset: 4; -*-
2
3    grab.c for the Openbox window manager
4    Copyright (c) 2003        Ben Jansens
5
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.
10
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.
15
16    See the COPYING file for a copy of the GNU General Public License.
17 */
18
19 #include "grab.h"
20 #include "openbox.h"
21 #include "event.h"
22 #include "xerror.h"
23 #include "screen.h"
24
25 #include <glib.h>
26 #include <X11/Xlib.h>
27
28 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
29 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
30
31 #define MASK_LIST_SIZE 8
32
33 /*! A list of all possible combinations of keyboard lock masks */
34 static unsigned int mask_list[MASK_LIST_SIZE];
35
36 gboolean grab_keyboard(gboolean grab)
37 {
38     static guint kgrabs = 0;
39     gboolean ret = FALSE;
40
41     if (grab) {
42         if (kgrabs++ == 0)
43             ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
44                                 FALSE, GrabModeAsync, GrabModeAsync,
45                                 event_lasttime) == Success;
46         else
47             ret = TRUE;
48     } else if (kgrabs > 0) {
49         if (--kgrabs == 0)
50             XUngrabKeyboard(ob_display, event_lasttime);
51         ret = TRUE;
52     }
53
54     return ret;
55 }
56
57 gboolean grab_pointer(gboolean grab, ObCursor cur)
58 {
59     static guint pgrabs = 0;
60     gboolean ret = FALSE;
61
62     if (grab) {
63         if (pgrabs++ == 0)
64             ret = XGrabPointer(ob_display, screen_support_win,
65                                False, GRAB_PTR_MASK, GrabModeAsync,
66                                GrabModeAsync, FALSE,
67                                ob_cursor(cur), event_lasttime) == Success;
68         else
69             ret = TRUE;
70     } else if (pgrabs > 0) {
71         if (--pgrabs == 0)
72             XUngrabPointer(ob_display, event_lasttime);
73         ret = TRUE;
74     }
75     return ret;
76 }
77
78 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
79 {
80     static guint pgrabs = 0;
81     gboolean ret = FALSE;
82
83     if (grab) {
84         if (pgrabs++ == 0)
85             ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
86                                GrabModeAsync, GrabModeAsync, TRUE,
87                                ob_cursor(cur),
88                                event_lasttime) == Success;
89         else
90             ret = TRUE;
91     } else if (pgrabs > 0) {
92         if (--pgrabs == 0)
93             XUngrabPointer(ob_display, event_lasttime);
94         ret = TRUE;
95     }
96     return ret;
97 }
98
99 gint grab_server(gboolean grab)
100 {
101     static guint sgrabs = 0;
102     if (grab) {
103         if (sgrabs++ == 0) {
104             XGrabServer(ob_display);
105             XSync(ob_display, FALSE);
106         }
107     } else if (sgrabs > 0) {
108         if (--sgrabs == 0) {
109             XUngrabServer(ob_display);
110             XFlush(ob_display);
111         }
112     }
113     return sgrabs;
114 }
115
116 void grab_startup(gboolean reconfig)
117 {
118     guint i = 0;
119
120     if (reconfig) return;
121
122     mask_list[i++] = 0;
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);
131 }
132
133 void grab_shutdown(gboolean reconfig)
134 {
135     if (reconfig) return;
136
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));
141 }
142
143 void grab_button_full(guint button, guint state, Window win, guint mask,
144                       int pointer_mode, ObCursor cur)
145 {
146     guint i;
147
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);
154     if (xerror_occured)
155         g_warning("failed to grab button %d modifiers %d", button, state);
156 }
157
158 void grab_button(guint button, guint state, Window win, guint mask)
159 {
160     grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
161 }
162
163 void ungrab_button(guint button, guint state, Window win)
164 {
165     guint i;
166
167     for (i = 0; i < MASK_LIST_SIZE; ++i)
168         XUngrabButton(ob_display, button, state | mask_list[i], win);
169 }
170
171 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
172 {
173     guint i;
174
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);
181     if (xerror_occured)
182         g_warning("failed to grab keycode %d modifiers %d", keycode, state);
183 }
184
185 void ungrab_all_keys(Window win)
186 {
187     XUngrabKey(ob_display, AnyKey, AnyModifier, win);
188 }