1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
3 grab.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
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.
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.
17 See the COPYING file for a copy of the GNU General Public License.
25 #include "obt/display.h"
26 #include "obt/keyboard.h"
31 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
32 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
34 #define MASK_LIST_SIZE 8
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;
44 static Time
ungrab_time(void)
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.
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."
57 t
= CurrentTime
; /*grab_time;*/
61 gboolean
grab_on_keyboard(void)
66 gboolean
grab_on_pointer(void)
71 gboolean
grab_keyboard_full(gboolean grab
)
77 ret
= XGrabKeyboard(obt_display
, obt_root(ob_screen
),
78 False
, GrabModeAsync
, GrabModeAsync
,
79 event_curtime
) == Success
;
84 grab_time
= event_curtime
;
88 } else if (kgrabs
> 0) {
90 XUngrabKeyboard(obt_display
, ungrab_time());
98 gboolean
grab_pointer_full(gboolean grab
, gboolean owner_events
,
99 gboolean confine
, ObCursor cur
)
101 gboolean ret
= FALSE
;
105 ret
= XGrabPointer(obt_display
, screen_support_win
, owner_events
,
107 GrabModeAsync
, GrabModeAsync
,
108 (confine
? obt_root(ob_screen
) : None
),
109 ob_cursor(cur
), event_curtime
) == Success
;
113 grab_time
= event_curtime
;
116 } else if (pgrabs
> 0) {
118 XUngrabPointer(obt_display
, ungrab_time());
125 gint
grab_server(gboolean grab
)
127 static guint sgrabs
= 0;
130 XGrabServer(obt_display
);
131 XSync(obt_display
, FALSE
);
133 } else if (sgrabs
> 0) {
135 XUngrabServer(obt_display
);
142 void grab_startup(gboolean reconfig
)
145 guint num
, caps
, scroll
;
147 num
= obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_NUMLOCK
);
148 caps
= obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_CAPSLOCK
);
149 scroll
= obt_keyboard_modkey_to_modmask(OBT_KEYBOARD_MODKEY_SCROLLLOCK
);
152 mask_list
[i
++] = num
;
153 mask_list
[i
++] = caps
;
154 mask_list
[i
++] = scroll
;
155 mask_list
[i
++] = num
| caps
;
156 mask_list
[i
++] = num
| scroll
;
157 mask_list
[i
++] = caps
| scroll
;
158 mask_list
[i
++] = num
| caps
| scroll
;
159 g_assert(i
== MASK_LIST_SIZE
);
162 void grab_shutdown(gboolean reconfig
)
164 if (reconfig
) return;
166 while (ungrab_keyboard());
167 while (ungrab_pointer());
168 while (grab_server(FALSE
));
171 void grab_button_full(guint button
, guint state
, Window win
, guint mask
,
172 gint pointer_mode
, ObCursor cur
)
176 /* can get BadAccess from these */
177 obt_display_ignore_errors(TRUE
);
178 for (i
= 0; i
< MASK_LIST_SIZE
; ++i
)
179 XGrabButton(obt_display
, button
, state
| mask_list
[i
], win
, False
,
180 mask
, pointer_mode
, GrabModeAsync
, None
, ob_cursor(cur
));
181 obt_display_ignore_errors(FALSE
);
182 if (obt_display_error_occured
)
183 ob_debug("Failed to grab button %d modifiers %d", button
, state
);
186 void ungrab_button(guint button
, guint state
, Window win
)
190 for (i
= 0; i
< MASK_LIST_SIZE
; ++i
)
191 XUngrabButton(obt_display
, button
, state
| mask_list
[i
], win
);
194 void grab_key(guint keycode
, guint state
, Window win
, gint keyboard_mode
)
198 /* can get BadAccess' from these */
199 obt_display_ignore_errors(TRUE
);
200 for (i
= 0; i
< MASK_LIST_SIZE
; ++i
)
201 XGrabKey(obt_display
, keycode
, state
| mask_list
[i
], win
, FALSE
,
202 GrabModeAsync
, keyboard_mode
);
203 obt_display_ignore_errors(FALSE
);
204 if (obt_display_error_occured
)
205 ob_debug("Failed to grab keycode %d modifiers %d", keycode
, state
);
208 void ungrab_all_keys(Window win
)
210 XUngrabKey(obt_display
, AnyKey
, AnyModifier
, win
);
213 void grab_key_passive_count(int change
)
215 if (grab_on_keyboard()) return;
216 passive_count
+= change
;
217 if (passive_count
< 0) passive_count
= 0;
220 void ungrab_passive_key(void)
222 /*ob_debug("ungrabbing %d passive grabs\n", passive_count);*/
224 /* kill our passive grab */
225 XUngrabKeyboard(obt_display
, event_curtime
);