]> Dogcows Code - chaz/openbox/blob - openbox/grab.c
dont need client.h in this header
[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, ob_root, FALSE, GrabModeAsync,
24 GrabModeAsync, event_lasttime) == Success;
25 else
26 ret = TRUE;
27 } else if (kgrabs > 0) {
28 if (--kgrabs == 0)
29 XUngrabKeyboard(ob_display, event_lasttime);
30 ret = TRUE;
31 }
32 return ret;
33 }
34
35 gboolean grab_pointer(gboolean grab, ObCursor cur)
36 {
37 static guint pgrabs = 0;
38 gboolean ret = FALSE;
39
40 if (grab) {
41 if (pgrabs++ == 0)
42 ret = XGrabPointer(ob_display, ob_root, False, GRAB_PTR_MASK,
43 GrabModeAsync, GrabModeAsync, FALSE,
44 ob_cursor(cur),
45 event_lasttime) == Success;
46 else
47 ret = TRUE;
48 } else if (pgrabs > 0) {
49 if (--pgrabs == 0)
50 XUngrabPointer(ob_display, event_lasttime);
51 ret = TRUE;
52 }
53 return ret;
54 }
55
56 gboolean grab_pointer_window(gboolean grab, ObCursor cur, Window win)
57 {
58 static guint pgrabs = 0;
59 gboolean ret = FALSE;
60
61 if (grab) {
62 if (pgrabs++ == 0)
63 ret = XGrabPointer(ob_display, win, False, GRAB_PTR_MASK,
64 GrabModeAsync, GrabModeAsync, TRUE,
65 ob_cursor(cur),
66 event_lasttime) == Success;
67 else
68 ret = TRUE;
69 } else if (pgrabs > 0) {
70 if (--pgrabs == 0)
71 XUngrabPointer(ob_display, event_lasttime);
72 ret = TRUE;
73 }
74 return ret;
75 }
76
77 int grab_server(gboolean grab)
78 {
79 static guint sgrabs = 0;
80 if (grab) {
81 if (sgrabs++ == 0) {
82 XGrabServer(ob_display);
83 XSync(ob_display, FALSE);
84 }
85 } else if (sgrabs > 0) {
86 if (--sgrabs == 0) {
87 XUngrabServer(ob_display);
88 XFlush(ob_display);
89 }
90 }
91 return sgrabs;
92 }
93
94 void grab_startup()
95 {
96 guint i = 0;
97
98 mask_list[i++] = 0;
99 mask_list[i++] = LockMask;
100 mask_list[i++] = NumLockMask;
101 mask_list[i++] = LockMask | NumLockMask;
102 mask_list[i++] = ScrollLockMask;
103 mask_list[i++] = ScrollLockMask | LockMask;
104 mask_list[i++] = ScrollLockMask | NumLockMask;
105 mask_list[i++] = ScrollLockMask | LockMask | NumLockMask;
106 g_assert(i == MASK_LIST_SIZE);
107 }
108
109 void grab_shutdown()
110 {
111 while (grab_keyboard(FALSE));
112 while (grab_pointer(FALSE, None));
113 while (grab_pointer_window(FALSE, None, None));
114 while (grab_server(FALSE));
115 }
116
117 void grab_button_full(guint button, guint state, Window win, guint mask,
118 int pointer_mode, ObCursor cur)
119 {
120 guint i;
121
122 for (i = 0; i < MASK_LIST_SIZE; ++i)
123 XGrabButton(ob_display, button, state | mask_list[i], win, FALSE, mask,
124 pointer_mode, GrabModeSync, None, ob_cursor(cur));
125 }
126
127 void grab_button(guint button, guint state, Window win, guint mask)
128 {
129 grab_button_full(button, state, win, mask, GrabModeAsync, None);
130 }
131
132 void ungrab_button(guint button, guint state, Window win)
133 {
134 guint i;
135
136 for (i = 0; i < MASK_LIST_SIZE; ++i)
137 XUngrabButton(ob_display, button, state | mask_list[i], win);
138 }
139
140 void grab_key(guint keycode, guint state, Window win, int keyboard_mode)
141 {
142 guint i;
143
144 xerror_set_ignore(TRUE); /* can get BadAccess' from these */
145 xerror_occured = FALSE;
146 for (i = 0; i < MASK_LIST_SIZE; ++i)
147 XGrabKey(ob_display, keycode, state | mask_list[i], win, FALSE,
148 GrabModeSync, keyboard_mode);
149 xerror_set_ignore(FALSE);
150 if (xerror_occured)
151 g_warning("failed to grab keycode %d modifiers %d", keycode, state);
152 }
153
154 void ungrab_all_keys(Window win)
155 {
156 XUngrabKey(ob_display, AnyKey, AnyModifier, win);
157 }
This page took 0.043144 seconds and 4 git commands to generate.