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