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