fix draw order in x_paint
[openbox.git] / openbox / dispatch.c
blob08690a3715c9e74e8ed1842400f2572f3eab4817
1 #include "dispatch.h"
2 #include "extensions.h"
4 #include <glib.h>
6 typedef struct {
7 EventHandler h;
8 void *data;
9 } Func;
11 /* an array of GSList*s of Func*s */
12 static GSList **funcs;
14 void dispatch_startup()
16 guint i;
17 EventType j;
19 i = 0;
20 j = EVENT_RANGE;
21 while (j > 1) {
22 j >>= 1;
23 ++i;
25 funcs = g_new0(GSList*, i);
28 void dispatch_shutdown()
30 guint i;
31 EventType j;
32 GSList *it;
34 for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1) {
35 for (it = funcs[i]; it != NULL; it = it->next)
36 g_free(it->data);
37 g_slist_free(funcs[i]);
38 funcs[i] = NULL;
41 g_free(funcs);
44 void dispatch_register(EventMask mask, EventHandler h, void *data)
46 guint i;
47 EventType j;
48 GSList *it, *next;
49 EventMask m;
50 Func *f;
52 /* add to masks it needs to be registered for */
53 m = mask;
54 while (m) {
55 for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1)
56 if (m & j) {
57 for (it = funcs[i]; it != NULL; it = it->next) {
58 f = it->data;
59 if (f->h == h && f->data == data)
60 break;
62 if (it == NULL) { /* wasn't already regged */
63 f = g_new(Func, 1);
64 f->h = h;
65 f->data = data;
66 funcs[i] = g_slist_append(funcs[i], f);
68 m ^= j; /* remove from the mask */
70 g_assert(j >= EVENT_RANGE); /* an invalid event is in the mask */
73 /* remove from masks its not registered for anymore */
74 for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1) {
75 if (!(j & mask))
76 for (it = funcs[i]; it != NULL; it = next) {
77 next = it->next;
78 f = it->data;
79 if (f->h == h && f->data == data) {
80 g_free(f);
81 funcs[i] = g_slist_delete_link(funcs[i], it);
87 void dispatch_x(XEvent *xe, Client *c)
89 EventType e;
90 guint i;
91 GSList *it;
92 ObEvent obe;
94 switch (xe->type) {
95 case EnterNotify:
96 e = Event_X_EnterNotify;
97 break;
98 case LeaveNotify:
99 e = Event_X_LeaveNotify;
100 break;
101 case KeyPress:
102 e = Event_X_KeyPress;
103 break;
104 case KeyRelease:
105 e = Event_X_KeyRelease;
106 break;
107 case ButtonPress:
108 e = Event_X_ButtonPress;
109 break;
110 case ButtonRelease:
111 e = Event_X_ButtonRelease;
112 break;
113 case MotionNotify:
114 e = Event_X_MotionNotify;
115 break;
116 default:
117 /* XKB events */
118 if (xe->type == extensions_xkb_event_basep) {
119 switch (((XkbAnyEvent*)&e)->xkb_type) {
120 case XkbBellNotify:
121 e = Event_X_Bell;
122 break;
125 return;
128 obe.type = e;
129 obe.data.x.e = xe;
130 obe.data.x.client = c;
132 i = 0;
133 while (e > 1) {
134 e >>= 1;
135 ++i;
138 for (it = funcs[i]; it != NULL; it = it->next) {
139 Func *f = it->data;
140 f->h(&obe, f->data);
144 void dispatch_client(EventType e, Client *c, int num0, int num1)
146 guint i;
147 GSList *it;
148 ObEvent obe;
150 g_assert(c != NULL);
152 obe.type = e;
153 obe.data.c.client = c;
154 obe.data.c.num[0] = num0;
155 obe.data.c.num[1] = num1;
157 i = 0;
158 while (e > 1) {
159 e >>= 1;
160 ++i;
163 for (it = funcs[i]; it != NULL; it = it->next) {
164 Func *f = it->data;
165 f->h(&obe, f->data);
169 void dispatch_ob(EventType e, int num0, int num1)
171 guint i;
172 GSList *it;
173 ObEvent obe;
175 obe.type = e;
176 obe.data.o.num[0] = num0;
177 obe.data.o.num[1] = num1;
179 i = 0;
180 while (e > 1) {
181 e >>= 1;
182 ++i;
185 for (it = funcs[i]; it != NULL; it = it->next) {
186 Func *f = it->data;
187 f->h(&obe, f->data);
191 void dispatch_signal(int signal)
193 guint i;
194 EventType e = Event_Signal;
195 GSList *it;
196 ObEvent obe;
198 obe.type = e;
199 obe.data.s.signal = signal;
201 i = 0;
202 while (e > 1) {
203 e >>= 1;
204 ++i;
207 for (it = funcs[i]; it != NULL; it = it->next) {
208 Func *f = it->data;
209 f->h(&obe, f->data);
213 void dispatch_move(Client *c, int *x, int *y)
215 guint i;
216 EventType e = Event_Client_Moving;
217 GSList *it;
218 ObEvent obe;
220 obe.type = e;
221 obe.data.c.client = c;
222 obe.data.c.num[0] = *x;
223 obe.data.c.num[1] = *y;
225 i = 0;
226 while (e > 1) {
227 e >>= 1;
228 ++i;
231 for (it = funcs[i]; it != NULL; it = it->next) {
232 Func *f = it->data;
233 f->h(&obe, f->data);
236 *x = obe.data.c.num[0];
237 *y = obe.data.c.num[1];