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