all repos — openbox @ f8a47de5ec444c452093371e3db16857eb39a490

openbox fork - make it a bit more like ryudo

c/hooks.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "hooks.h"
#include <Python.h>
#include <glib.h>

/* the 'hooks' module and its dictionary */
static PyObject *hooks = NULL, *hooksdict = NULL;

/*
 *
 * Define the type 'Hook'
 *
 */
#define IS_HOOK(v)  ((v)->ob_type == &HookType)

staticforward PyTypeObject HookType;

typedef struct {
    PyObject_HEAD
    GSList *funcs;
} HookObject;

static PyObject *create_Hook(PyObject *self, PyObject *args)
{
    HookObject *hook;
    char *name;
    int ret;

    (void) self;

    if (!PyArg_ParseTuple(args, "s:Hook", &name))
	return NULL;

    hook = PyObject_New(HookObject, &HookType);
    hook->funcs = NULL;

    /* add it to the hooks module */
    ret = PyDict_SetItemString(hooksdict, name, (PyObject*) hook);
    Py_DECREF(hook);

    if (ret == -1) {
	char *s = g_strdup_printf(
	    "Failed to add the hook '%s' to the 'hooks' module", name);
	PyErr_SetString(PyExc_RuntimeError, s);
	g_free(s);
	return NULL;
    }
     
    Py_INCREF(Py_None);
    return Py_None;
}

static void hook_dealloc(HookObject *self)
{
    GSList *it;

    for (it = self->funcs; it != NULL; it = it->next)
	Py_DECREF((PyObject*) it->data);
     
    PyObject_Del((PyObject*) self);
}

static PyObject *hook_fire(HookObject *self, PyObject *args)
{
    GSList *it;

    if (!IS_HOOK(self)) {
	PyErr_SetString(PyExc_TypeError,
			"descriptor 'fire' requires a 'Hook' object");
	return NULL;
    }

    for (it = self->funcs; it != NULL; it = it->next) {
	PyObject *ret = PyObject_CallObject(it->data, args);
	if (ret == NULL)
	    return NULL;
	Py_DECREF(ret);
    }

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *hook_add(HookObject *self, PyObject *args)
{
    PyObject *func;
     
    if (!IS_HOOK(self)) {
	PyErr_SetString(PyExc_TypeError,
			"descriptor 'add' requires a 'Hook' object");
	return NULL;
    }
    if (!PyArg_ParseTuple(args, "O:add", &func))
	return NULL;
    if (!PyCallable_Check(func)) {
	PyErr_SetString(PyExc_TypeError,
			"descriptor 'add' requires a callable argument");
	return NULL;
    }
    self->funcs = g_slist_append(self->funcs, func);
    Py_INCREF(func);

    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *hook_remove(HookObject *self, PyObject *args)
{
    PyObject *func;
    GSList *it;
     
    if (!IS_HOOK(self)) {
	PyErr_SetString(PyExc_TypeError,
			"descriptor 'remove' requires a 'Hook' object");
	return NULL;
    }
    if (!PyArg_ParseTuple(args, "O:remove", &func))
	return NULL;
    if (!PyCallable_Check(func)) {
	PyErr_SetString(PyExc_TypeError,
			"descriptor 'remove' requires a callable argument");
	return NULL;
    }
    it = g_slist_find(self->funcs, func);
    if (it != NULL) {
	self->funcs = g_slist_delete_link(self->funcs, it);
	Py_DECREF(func);

	Py_INCREF(Py_None);
	return Py_None;
    }
    PyErr_SetString(PyExc_TypeError,
		    "given callable object was not found in Hook");
    return NULL;
}

static PyObject *hook_count(HookObject *self, PyObject *args)
{
    if (!IS_HOOK(self)) {
	PyErr_SetString(PyExc_TypeError,
			"descriptor 'fire' requires a 'Hook' object");
	return NULL;
    }
    if (!PyArg_ParseTuple(args, ":count"))
	return NULL;

    return PyInt_FromLong(g_slist_length(self->funcs));
}

static PyTypeObject HookType = {
    PyObject_HEAD_INIT(NULL)
    0,
    "Hook",
    sizeof(HookObject),
    0,
    (destructor) hook_dealloc, /*tp_dealloc*/
    0,                         /*tp_print*/
    0,                         /*tp_getattr*/
    0,                         /*tp_setattr*/
    0,                         /*tp_compare*/
    0,                         /*tp_repr*/
    0,                         /*tp_as_number*/
    0,                         /*tp_as_sequence*/
    0,                         /*tp_as_mapping*/
    0,                         /*tp_hash */
};

static PyMethodDef HookMethods[] = {
    {"fire", (PyCFunction)hook_fire, METH_VARARGS,
     "hook.fire() -- Fire the added hook functions for the Hook."},
    {"add", (PyCFunction)hook_add, METH_VARARGS,
     "hook.add(func) -- Add a function to the hook." },
    {"remove", (PyCFunction)hook_remove, METH_VARARGS,
     "hook.remove(func) -- Remove a function from the hook." },
    {"count", (PyCFunction)hook_count, METH_VARARGS,
     "hook.count() -- Return the number of functions in the hook." },
     
    { NULL, NULL, 0, NULL }
};


/*
 *
 * Module initialization/finalization
 *
 */

/* the "events" hook */
static HookObject *events_hook = NULL, *keyboard_hook = NULL,
    *pointer_hook = NULL;

static PyMethodDef HooksMethods[] = {
    {"create", create_Hook, METH_VARARGS,
     "hooks.create('name') -- Add a hook called 'name' to the hooks module."},
     
    { NULL, NULL, 0, NULL }
};

void hooks_startup()
{
    int ret;

    HookType.ob_type = &PyType_Type;
    HookType.tp_methods = HookMethods;
    PyType_Ready(&HookType);

    Py_InitModule("hooks", HooksMethods);

    /* get the hooks module/dict */
    hooks = PyImport_ImportModule("hooks"); /* new */
    g_assert(hooks != NULL);
    hooksdict = PyModule_GetDict(hooks); /* borrowed */
    g_assert(hooksdict != NULL);

    /* create the "events" hook */
    events_hook = PyObject_New(HookObject, &HookType);
    events_hook->funcs = NULL;

    /* add it to the hooks module */
    ret = PyDict_SetItemString(hooksdict, "events", (PyObject*) events_hook);
    g_assert(ret == 0);

    /* create the "keyboard" hook */
    keyboard_hook = PyObject_New(HookObject, &HookType);
    keyboard_hook->funcs = NULL;

    /* add it to the hooks module */
    ret = PyDict_SetItemString(hooksdict, "keyboard",
			       (PyObject*) keyboard_hook);
    g_assert(ret == 0);

    /* create the "pointer" hook */
    pointer_hook = PyObject_New(HookObject, &HookType);
    pointer_hook->funcs = NULL;

    /* add it to the hooks module */
    ret = PyDict_SetItemString(hooksdict, "pointer", (PyObject*) pointer_hook);
    g_assert(ret == 0);
}

void hooks_shutdown()
{
    Py_DECREF(pointer_hook);
    Py_DECREF(keyboard_hook);
    Py_DECREF(events_hook);
    Py_DECREF(hooks);
}

void hooks_fire(EventData *data)
{
    PyObject *ret, *args;

    g_assert(events_hook != NULL);

    args = Py_BuildValue("(O)", data);
    ret = hook_fire(events_hook, args);
    Py_DECREF(args);
    if (ret == NULL)
	PyErr_Print();
}

void hooks_fire_keyboard(EventData *data)
{
    PyObject *ret, *args;

    g_assert(events_hook != NULL);

    args = Py_BuildValue("(O)", data);
    ret = hook_fire(keyboard_hook, args);
    Py_DECREF(args);
    if (ret == NULL)
	PyErr_Print();
}

void hooks_fire_pointer(EventData *data)
{
    PyObject *ret, *args;

    g_assert(events_hook != NULL);

    args = Py_BuildValue("(O)", data);
    ret = hook_fire(pointer_hook, args);
    Py_DECREF(args);
    if (ret == NULL)
	PyErr_Print();
}