all repos — openbox @ 9185ca5c1a7e7fb492ef829449f9a089f1d858ee

openbox fork - make it a bit more like ryudo

openbox/dispatch.c (raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include "dispatch.h"
#include "extensions.h"

#include <glib.h>

typedef struct {
    EventHandler h;
    void *data;
} Func;

static GSList **funcs;

void dispatch_startup()
{
    guint i;
    EventType j;

    i = 0;
    j = EVENT_RANGE;
    while (j > 1) {
        j >>= 1;
        ++i;
    }
    funcs = g_new(GSList*, i);

    for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1)
        funcs[i] = NULL;
}

void dispatch_shutdown()
{
    guint i;
    EventType j;
    GSList *it;

    for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1) {
        for (it = funcs[i]; it != NULL; it = it->next)
            g_free(it->data);
        g_slist_free(funcs[i]);
        funcs[i] = NULL;
    }

    g_free(funcs);
}

void dispatch_register(EventMask mask, EventHandler h, void *data)
{
    guint i;
    EventType j;
    GSList *it, *next;
    EventMask m;
    Func *f;

    /* add to masks it needs to be registered for */
    m = mask;
    while (m) {
        for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1)
            if (m & j) {
                for (it = funcs[i]; it != NULL; it = it->next) {
                    f = it->data;
                    if (f->h == h && f->data == data)
                        break;
                }
                if (it == NULL) { /* wasn't already regged */
                    f = g_new(Func, 1);
                    f->h = h;
                    f->data = data;
                    funcs[i] = g_slist_append(funcs[i], f);
                }
                m ^= j; /* remove from the mask */
            }
        g_assert(j >= EVENT_RANGE); /* an invalid event is in the mask */
    }

    /* remove from masks its not registered for anymore */
    for (i = 0, j = 1; j < EVENT_RANGE; ++i, j <<= 1) {
        if (!(j & mask))
            for (it = funcs[i]; it != NULL; it = next) {
                next = it->next;
                f = it->data;
                if (f->h == h && f->data == data) {
                    g_free(f);
                    funcs[i] = g_slist_delete_link(funcs[i], it);
                }
            }
    }
}

void dispatch_x(XEvent *xe, Client *c)
{
    EventType e;
    guint i;
    GSList *it;
    ObEvent obe;

    switch (xe->type) {
    case EnterNotify:
        e = Event_X_EnterNotify;
        break;
    case LeaveNotify:
        e = Event_X_LeaveNotify;
        break;
    case KeyPress:
        e = Event_X_KeyPress;
        break;
    case KeyRelease:
        e = Event_X_KeyRelease;
        break;
    case ButtonPress:
        e = Event_X_ButtonPress;
        break;
    case ButtonRelease:
        e = Event_X_ButtonRelease;
        break;
    case MotionNotify:
        e = Event_X_MotionNotify;
        break;
    default:
	/* XKB events */
	if (xe->type == extensions_xkb_event_basep) {
	    switch (((XkbAnyEvent*)&e)->xkb_type) {
	    case XkbBellNotify:
		e = Event_X_Bell;
		break;
	    }
	}
        return;
    }

    obe.type = e;
    obe.data.x.e = xe;
    obe.data.x.client = c;

    i = 0;
    while (e > 1) {
        e >>= 1;
        ++i;
    }

    for (it = funcs[i]; it != NULL; it = it->next) {
        Func *f = it->data;
        f->h(&obe, f->data);
    }
}

void dispatch_client(EventType e, Client *c)
{
    guint i;
    GSList *it;
    ObEvent obe;

    g_assert(c != NULL);

    obe.type = e;
    obe.data.client = c;

    i = 0;
    while (e > 1) {
        e >>= 1;
        ++i;
    }

    for (it = funcs[i]; it != NULL; it = it->next) {
        Func *f = it->data;
        f->h(&obe, f->data);
    }
}

void dispatch_ob(EventType e)
{
    guint i;
    GSList *it;
    ObEvent obe;

    obe.type = e;

    i = 0;
    while (e > 1) {
        e >>= 1;
        ++i;
    }

    for (it = funcs[i]; it != NULL; it = it->next) {
        Func *f = it->data;
        f->h(&obe, f->data);
    }
}

void dispatch_signal(int signal)
{
    guint i;
    EventType e = Event_Signal;
    GSList *it;
    ObEvent obe;

    obe.type = e;
    obe.data.signal = signal;

    i = 0;
    while (e > 1) {
        e >>= 1;
        ++i;
    }

    for (it = funcs[i]; it != NULL; it = it->next) {
        Func *f = it->data;
        f->h(&obe, f->data);
    }
}