ANSI function declarations, ie () -> (void)
[mikachu/openbox.git] / 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-2007   Dana 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 "modkeys.h"
22 #include "openbox.h"
23 #include "event.h"
24 #include "xerror.h"
25 #include "screen.h"
26 #include "debug.h"
27
28 #include <glib.h>
29 #include <X11/Xlib.h>
30
31 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
32 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
33
34 #define MASK_LIST_SIZE 8
35
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
44 static Time ungrab_time(void)
45 {
46     Time t = event_curtime;
47     if (grab_time == CurrentTime ||
48         !(t == CurrentTime || event_time_after(t, grab_time)))
49         /* When the time moves backward on the server, then we can't use
50            the grab time because that will be in the future. So instead we
51            have to use CurrentTime.
52
53            "XUngrabPointer does not release the pointer if the specified time
54            is earlier than the last-pointer-grab time or is later than the
55            current X server time."
56         */
57         t = CurrentTime; /*grab_time;*/
58     return t;
59 }
60
61 gboolean grab_on_keyboard(void)
62 {
63     return kgrabs > 0;
64 }
65
66 gboolean grab_on_pointer(void)
67 {
68     return pgrabs > 0;
69 }
70
71 gboolean grab_keyboard_full(gboolean grab)
72 {
73     gboolean ret = FALSE;
74
75     if (grab) {
76         if (kgrabs++ == 0) {
77             ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
78                                 False, GrabModeAsync, GrabModeAsync,
79                                 event_curtime) == Success;
80             if (!ret)
81                 --kgrabs;
82             else {
83                 passive_count = 0;
84                 grab_time = event_curtime;
85             }
86         } else
87             ret = TRUE;
88     } else if (kgrabs > 0) {
89         if (--kgrabs == 0) {
90             XUngrabKeyboard(ob_display, ungrab_time());
91         }
92         ret = TRUE;
93     }
94
95     return ret;
96 }
97
98 gboolean grab_pointer_full(gboolean grab, gboolean owner_events,
99                            gboolean confine, ObCursor cur)
100 {
101     gboolean ret = FALSE;
102
103     if (grab) {
104         if (pgrabs++ == 0) {
105             ret = XGrabPointer(ob_display, screen_support_win, owner_events,
106                                GRAB_PTR_MASK,
107                                GrabModeAsync, GrabModeAsync,
108                                (confine ? RootWindow(ob_display, ob_screen) :
109                                 None),
110                                ob_cursor(cur), event_curtime) == Success;
111             if (!ret)
112                 --pgrabs;
113             else
114                 grab_time = event_curtime;
115         } else
116             ret = TRUE;
117     } else if (pgrabs > 0) {
118         if (--pgrabs == 0) {
119             XUngrabPointer(ob_display, ungrab_time());
120         }
121         ret = TRUE;
122     }
123     return ret;
124 }
125
126 gint grab_server(gboolean grab)
127 {
128     static guint sgrabs = 0;
129     if (grab) {
130         if (sgrabs++ == 0) {
131             XGrabServer(ob_display);
132             XSync(ob_display, FALSE);
133         }
134     } else if (sgrabs > 0) {
135         if (--sgrabs == 0) {
136             XUngrabServer(ob_display);
137             XFlush(ob_display);
138         }
139     }
140     return sgrabs;
141 }
142
143 void grab_startup(gboolean reconfig)
144 {
145     guint i = 0;
146     guint num, caps, scroll;
147
148     num = modkeys_key_to_mask(OB_MODKEY_KEY_NUMLOCK);
149     caps = modkeys_key_to_mask(OB_MODKEY_KEY_CAPSLOCK);
150     scroll = modkeys_key_to_mask(OB_MODKEY_KEY_SCROLLLOCK);
151
152     mask_list[i++] = 0;
153     mask_list[i++] = num;
154     mask_list[i++] = caps;
155     mask_list[i++] = scroll;
156     mask_list[i++] = num | caps;
157     mask_list[i++] = num | scroll;
158     mask_list[i++] = caps | scroll;
159     mask_list[i++] = num | caps | scroll;
160     g_assert(i == MASK_LIST_SIZE);
161 }
162
163 void grab_shutdown(gboolean reconfig)
164 {
165     if (reconfig) return;
166
167     while (ungrab_keyboard());
168     while (ungrab_pointer());
169     while (grab_server(FALSE));
170 }
171
172 void grab_button_full(guint button, guint state, Window win, guint mask,
173                       gint pointer_mode, ObCursor cur)
174 {
175     guint i;
176
177     xerror_set_ignore(TRUE); /* can get BadAccess from these */
178     xerror_occured = FALSE;
179     for (i = 0; i < MASK_LIST_SIZE; ++i)
180         XGrabButton(ob_display, button, state | mask_list[i], win, False, mask,
181                     pointer_mode, GrabModeAsync, None, ob_cursor(cur));
182     xerror_set_ignore(FALSE);
183     if (xerror_occured)
184         ob_debug("Failed to grab button %d modifiers %d", button, state);
185 }
186
187 void ungrab_button(guint button, guint state, Window win)
188 {
189     guint i;
190
191     for (i = 0; i < MASK_LIST_SIZE; ++i)
192         XUngrabButton(ob_display, button, state | mask_list[i], win);
193 }
194
195 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
196 {
197     guint i;
198
199     xerror_set_ignore(TRUE); /* can get BadAccess' from these */
200     xerror_occured = FALSE;
201     for (i = 0; i < MASK_LIST_SIZE; ++i)
202         XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
203                  GrabModeAsync, keyboard_mode);
204     xerror_set_ignore(FALSE);
205     if (xerror_occured)
206         ob_debug("Failed to grab keycode %d modifiers %d", keycode, state);
207 }
208
209 void ungrab_all_keys(Window win)
210 {
211     XUngrabKey(ob_display, AnyKey, AnyModifier, win);
212 }
213
214 void grab_key_passive_count(int change)
215 {
216     if (grab_on_keyboard()) return;
217     passive_count += change;
218     if (passive_count < 0) passive_count = 0;
219 }
220
221 void ungrab_passive_key(void)
222 {
223     /*ob_debug("ungrabbing %d passive grabs\n", passive_count);*/
224     if (passive_count) {
225         /* kill out passive grab */
226         XUngrabKeyboard(ob_display, event_curtime);
227         passive_count = 0;
228     }
229 }