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