]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
dont ungrab/grab all the mouse bindings every time a new one is added. just do it...
[chaz/openbox] / openbox / grab.c
1 #include "openbox.h"
2 #include "event.h"
3 #include "xerror.h"
4
5 #include <glib.h>
6 #include <X11/Xlib.h>
7
8 #define GRAB_PTR_MASK (ButtonPressMask | ButtonReleaseMask | PointerMotionMask)
9 #define GRAB_KEY_MASK (KeyPressMask | KeyReleaseMask)
10
11 #define MASK_LIST_SIZE 8
12
13 /*! A list of all possible combinations of keyboard lock masks */
14 static unsigned int mask_list[MASK_LIST_SIZE];
15
16 gboolean grab_keyboard(gboolean grab)
17 {
18 static guint kgrabs = 0;
19 gboolean ret = FALSE;
20
21 if (grab) {
22 if (kgrabs++ == 0)
23 ret = XGrabKeyboard(ob_display, RootWindow(ob_display, ob_screen),
24 FALSE, GrabModeAsync, GrabModeAsync,
25 event_lasttime) == Success;
26 else
27 ret = TRUE;
28 } else if (kgrabs > 0) {
29 if (--kgrabs == 0)
30 XUngrabKeyboard(ob_display, event_lasttime);
31 ret = TRUE;
32 }
33
34 return ret;
35 }
36
37 gboolean grab_pointer(gboolean grab, ObCursor cur)
38 {
39 static guint pgrabs = 0;
40 gboolean ret = FALSE;
41
42 if (grab) {
43 if (pgrabs++ == 0)
44 ret = XGrabPointer(ob_display, RootWindow(ob_display, ob_screen),
45 False, GRAB_PTR_MASK, GrabModeAsync,
46 GrabModeAsync, FALSE,
47 ob_cursor(cur), event_lasttime) == Success;
48 else
49 ret = TRUE;
50 } else if (pgrabs > 0) {
51 if (--pgrabs == 0)
52 XUngrabPointer(ob_display, event_lasttime);
53 ret = TRUE;
54 }
55 return ret;
56 }
57
58 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
59 {
60 static guint pgrabs = 0;
61 gboolean ret = FALSE;
62
63 if (grab) {
64 if (pgrabs++ == 0)
65 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
66 GrabModeAsync, GrabModeAsync, TRUE,
67 ob_cursor(cur),
68 event_lasttime) == Success;
69 else
70 ret = TRUE;
71 } else if (pgrabs > 0) {
72 if (--pgrabs == 0)
73 XUngrabPointer(ob_display, event_lasttime);
74 ret = TRUE;
75 }
76 return ret;
77 }
78
79 int grab_server(gboolean grab)
80 {
81 static guint sgrabs = 0;
82 if (grab) {
83 if (sgrabs++ == 0) {
84 XGrabServer(ob_display);
85 XSync(ob_display, FALSE);
86 }
87 } else if (sgrabs > 0) {
88 if (--sgrabs == 0) {
89 XUngrabServer(ob_display);
90 XFlush(ob_display);
91 }
92 }
93 return sgrabs;
94 }
95
96 void grab_startup()
97 {
98 guint i = 0;
99
100 mask_list[i++] = 0;
101 mask_list[i++] = LockMask;
102 mask_list[i++] = NumLockMask;
103 mask_list[i++] = LockMask | NumLockMask;
104 mask_list[i++] = ScrollLockMask;
105 mask_list[i++] = ScrollLockMask | LockMask;
106 mask_list[i++] = ScrollLockMask | NumLockMask;
107 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
108 g_assert(i == MASK_LIST_SIZE);
109 }
110
111 void grab_shutdown()
112 {
113 while (grab_keyboard(FALSE));
114 while (grab_pointer(FALSE, OB_CURSOR_NONE));
115 while (grab_pointer_window(FALSE, OB_CURSOR_NONE, None));
116 while (grab_server(FALSE));
117 }
118
119 void grab_button_full(guint button, guint state, Window win, guint mask,
120 int pointer_mode, ObCursor cur)
121 {
122 guint i;
123
124 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
125 xerror_occured = FALSE;
126 for (i = 0; i < MASK_LIST_SIZE; ++i)
127 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
128 pointer_mode, GrabModeSync, None, ob_cursor(cur));
129 xerror_set_ignore(FALSE);
130 if (xerror_occured)
131 g_warning("failed to grab button %d modifiers %d", button, state);
132 }
133
134 void grab_button(guint button, guint state, Window win, guint mask)
135 {
136 grab_button_full(button, state, win, mask, GrabModeAsync, OB_CURSOR_NONE);
137 }
138
139 void ungrab_button(guint button, guint state, Window win)
140 {
141 guint i;
142
143 for (i = 0; i < MASK_LIST_SIZE; ++i)
144 XUngrabButton(ob_display, button, state | mask_list[i], win);
145 }
146
147 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
148 {
149 guint i;
150
151 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
152 xerror_occured = FALSE;
153 for (i = 0; i < MASK_LIST_SIZE; ++i)
154 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
155 GrabModeAsync, keyboard_mode);
156 xerror_set_ignore(FALSE);
157 if (xerror_occured)
158 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
159 }
160
161 void ungrab_all_keys(Window win)
162 {
163 XUngrabKey(ob_display, AnyKey, AnyModifier, win);
164 }
This page took 0.045121 seconds and 4 git commands to generate.