]> icculus.org git repositories - dana/openbox.git/blob - openbox/grab.c
add the PointerMotionHintMask everywhere, we dont need every mouse event
[dana/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 "openbox.h"
22 #include "event.h"
23 #include "xerror.h"
24 #include "screen.h"
25 #include "debug.h"
26
27 #include <glib.h>
28 #include <X11/Xlib.h>
29
30 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | \
31                        PointerMotionMask | PointerMotionHintMask)
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
43 static Time ungrab_time()
44 {
45     Time t = event_curtime;
46     if (!(t == CurrentTime || event_time_after(t, grab_time)))
47         /* When the time moves backward on the server, then we can't use
48            the grab time because that will be in the future. So instead we
49            have to use CurrentTime.
50
51            "XUngrabPointer does not release the pointer if the specified time
52            is earlier than the last-pointer-grab time or is later than the
53            current X server time."
54         */
55         t = CurrentTime; /*grab_time;*/
56     return t;
57 }
58
59 gboolean grab_on_keyboard()
60 {
61     return kgrabs > 0;
62 }
63
64 gboolean grab_on_pointer()
65 {
66     return pgrabs > 0;
67 }
68
69 gboolean grab_keyboard(gboolean grab)
70 {
71     gboolean ret = FALSE;
72
73     if (grab) {
74         if (kgrabs++ == 0) {
75             ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
76                                 False, GrabModeAsync, GrabModeAsync,
77                                 event_curtime) == Success;
78             if (!ret)
79                 --kgrabs;
80             else
81                 grab_time = event_curtime;
82         } else
83             ret = TRUE;
84     } else if (kgrabs > 0) {
85         if (--kgrabs == 0) {
86             XUngrabKeyboard(ob_display, ungrab_time());
87         }
88         ret = TRUE;
89     }
90
91     return ret;
92 }
93
94 gboolean grab_pointer(gboolean grab, gboolean owner_events, ObCursor cur)
95 {
96     gboolean ret = FALSE;
97
98     if (grab) {
99         if (pgrabs++ == 0) {
100             ret = XGrabPointer(ob_display, screen_support_win, owner_events,
101                                GRAB_PTR_MASK,
102                                GrabModeAsync, GrabModeAsync, None,
103                                ob_cursor(cur), event_curtime) == Success;
104             if (!ret)
105                 --pgrabs;
106             else
107                 grab_time = event_curtime;
108         } else
109             ret = TRUE;
110     } else if (pgrabs > 0) {
111         if (--pgrabs == 0) {
112             XUngrabPointer(ob_display, ungrab_time());
113         }
114         ret = TRUE;
115     }
116     return ret;
117 }
118
119 gint grab_server(gboolean grab)
120 {
121     static guint sgrabs = 0;
122     if (grab) {
123         if (sgrabs++ == 0) {
124             XGrabServer(ob_display);
125             XSync(ob_display, FALSE);
126         }
127     } else if (sgrabs > 0) {
128         if (--sgrabs == 0) {
129             XUngrabServer(ob_display);
130             XFlush(ob_display);
131         }
132     }
133     return sgrabs;
134 }
135
136 void grab_startup(gboolean reconfig)
137 {
138     guint i = 0;
139
140     if (reconfig) return;
141
142     mask_list[i++] = 0;
143     mask_list[i++] = LockMask;
144     mask_list[i++] = NumLockMask;
145     mask_list[i++] = LockMask | NumLockMask;
146     mask_list[i++] = ScrollLockMask;
147     mask_list[i++] = ScrollLockMask | LockMask;
148     mask_list[i++] = ScrollLockMask | NumLockMask;
149     mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
150     g_assert(i == MASK_LIST_SIZE);
151 }
152
153 void grab_shutdown(gboolean reconfig)
154 {
155     if (reconfig) return;
156
157     while (grab_keyboard(FALSE));
158     while (grab_pointer(FALSE, FALSE, OB_CURSOR_NONE));
159     while (grab_server(FALSE));
160 }
161
162 void grab_button_full(guint button, guint state, Window win, guint mask,
163                       gint pointer_mode, ObCursor cur)
164 {
165     guint i;
166
167     xerror_set_ignore(TRUE); /* can get BadAccess from these */
168     xerror_occured = FALSE;
169     for (i = 0; i < MASK_LIST_SIZE; ++i)
170         XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
171                     pointer_mode, GrabModeSync, None, ob_cursor(cur));
172     xerror_set_ignore(FALSE);
173     if (xerror_occured)
174         ob_debug("Failed to grab button %d modifiers %d", button, state);
175 }
176
177 void grab_button(guint button, guint state, Window win, guint mask)
178 {
179     grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
180 }
181
182 void ungrab_button(guint button, guint state, Window win)
183 {
184     guint i;
185
186     for (i = 0; i < MASK_LIST_SIZE; ++i)
187         XUngrabButton(ob_display, button, state | mask_list[i], win);
188 }
189
190 void grab_key(guint keycode, guint state, Window win, gint keyboard_mode)
191 {
192     guint i;
193
194     xerror_set_ignore(TRUE); /* can get BadAccess' from these */
195     xerror_occured = FALSE;
196     for (i = 0; i < MASK_LIST_SIZE; ++i)
197         XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
198                  GrabModeAsync, keyboard_mode);
199     xerror_set_ignore(FALSE);
200     if (xerror_occured)
201         ob_debug("Failed to grab keycode %d modifiers %d", keycode, state);
202 }
203
204 void ungrab_all_keys(Window win)
205 {
206     XUngrabKey(ob_display, AnyKey, AnyModifier, win);
207 }