5 /* the 'hooks' module and its dictionary */
6 static PyObject *hooks = NULL, *hooksdict = NULL;
10 * Define the type 'Hook'
13 #define IS_HOOK(v) ((v)->ob_type == &HookType)
15 staticforward PyTypeObject HookType;
22 static PyObject *create_Hook(PyObject *self, PyObject *args)
30 if (!PyArg_ParseTuple(args, "s:Hook", &name))
33 hook = PyObject_New(HookObject, &HookType);
36 /* add it to the hooks module */
37 ret = PyDict_SetItemString(hooksdict, name, (PyObject*) hook);
41 char *s = g_strdup_printf(
42 "Failed to add the hook '%s' to the 'hooks' module", name);
43 PyErr_SetString(PyExc_RuntimeError, s);
52 static void hook_dealloc(HookObject *self)
56 for (it = self->funcs; it != NULL; it = it->next)
57 Py_DECREF((PyObject*) it->data);
59 PyObject_Del((PyObject*) self);
62 static PyObject *hook_fire(HookObject *self, PyObject *args)
67 PyErr_SetString(PyExc_TypeError,
68 "descriptor 'fire' requires a 'Hook' object");
72 for (it = self->funcs; it != NULL; it = it->next) {
73 PyObject *ret = PyObject_CallObject(it->data, args);
83 static PyObject *hook_add(HookObject *self, PyObject *args)
88 PyErr_SetString(PyExc_TypeError,
89 "descriptor 'add' requires a 'Hook' object");
92 if (!PyArg_ParseTuple(args, "O:add", &func))
94 if (!PyCallable_Check(func)) {
95 PyErr_SetString(PyExc_TypeError,
96 "descriptor 'add' requires a callable argument");
99 self->funcs = g_slist_append(self->funcs, func);
106 static PyObject *hook_remove(HookObject *self, PyObject *args)
111 if (!IS_HOOK(self)) {
112 PyErr_SetString(PyExc_TypeError,
113 "descriptor 'remove' requires a 'Hook' object");
116 if (!PyArg_ParseTuple(args, "O:remove", &func))
118 if (!PyCallable_Check(func)) {
119 PyErr_SetString(PyExc_TypeError,
120 "descriptor 'remove' requires a callable argument");
123 it = g_slist_find(self->funcs, func);
125 self->funcs = g_slist_delete_link(self->funcs, it);
131 PyErr_SetString(PyExc_TypeError,
132 "given callable object was not found in Hook");
136 static PyObject *hook_count(HookObject *self, PyObject *args)
138 if (!IS_HOOK(self)) {
139 PyErr_SetString(PyExc_TypeError,
140 "descriptor 'fire' requires a 'Hook' object");
143 if (!PyArg_ParseTuple(args, ":count"))
146 return PyInt_FromLong(g_slist_length(self->funcs));
149 static PyTypeObject HookType = {
150 PyObject_HEAD_INIT(NULL)
155 (destructor) hook_dealloc, /*tp_dealloc*/
162 0, /*tp_as_sequence*/
167 static PyMethodDef HookMethods[] = {
168 {"fire", (PyCFunction)hook_fire, METH_VARARGS,
169 "hook.fire() -- Fire the added hook functions for the Hook."},
170 {"add", (PyCFunction)hook_add, METH_VARARGS,
171 "hook.add(func) -- Add a function to the hook." },
172 {"remove", (PyCFunction)hook_remove, METH_VARARGS,
173 "hook.remove(func) -- Remove a function from the hook." },
174 {"count", (PyCFunction)hook_count, METH_VARARGS,
175 "hook.count() -- Return the number of functions in the hook." },
177 { NULL, NULL, 0, NULL }
183 * Module initialization/finalization
187 /* the "events" hook */
188 static HookObject *events_hook = NULL, *keyboard_hook = NULL,
189 *pointer_hook = NULL;
191 static PyMethodDef HooksMethods[] = {
192 {"create", create_Hook, METH_VARARGS,
193 "hooks.create('name') -- Add a hook called 'name' to the hooks module."},
195 { NULL, NULL, 0, NULL }
202 HookType.ob_type = &PyType_Type;
203 HookType.tp_methods = HookMethods;
204 PyType_Ready(&HookType);
206 Py_InitModule("hooks", HooksMethods);
208 /* get the hooks module/dict */
209 hooks = PyImport_ImportModule("hooks"); /* new */
210 g_assert(hooks != NULL);
211 hooksdict = PyModule_GetDict(hooks); /* borrowed */
212 g_assert(hooksdict != NULL);
214 /* create the "events" hook */
215 events_hook = PyObject_New(HookObject, &HookType);
216 events_hook->funcs = NULL;
218 /* add it to the hooks module */
219 ret = PyDict_SetItemString(hooksdict, "events", (PyObject*) events_hook);
222 /* create the "keyboard" hook */
223 keyboard_hook = PyObject_New(HookObject, &HookType);
224 keyboard_hook->funcs = NULL;
226 /* add it to the hooks module */
227 ret = PyDict_SetItemString(hooksdict, "keyboard",
228 (PyObject*) keyboard_hook);
231 /* create the "pointer" hook */
232 pointer_hook = PyObject_New(HookObject, &HookType);
233 pointer_hook->funcs = NULL;
235 /* add it to the hooks module */
236 ret = PyDict_SetItemString(hooksdict, "pointer", (PyObject*) pointer_hook);
240 void hooks_shutdown()
242 Py_DECREF(pointer_hook);
243 Py_DECREF(keyboard_hook);
244 Py_DECREF(events_hook);
248 void hooks_fire(EventData *data)
250 PyObject *ret, *args;
252 g_assert(events_hook != NULL);
254 args = Py_BuildValue("(O)", data);
255 ret = hook_fire(events_hook, args);
261 void hooks_fire_keyboard(EventData *data)
263 PyObject *ret, *args;
265 g_assert(events_hook != NULL);
267 args = Py_BuildValue("(O)", data);
268 ret = hook_fire(keyboard_hook, args);
274 void hooks_fire_pointer(EventData *data)
276 PyObject *ret, *args;
278 g_assert(events_hook != NULL);
280 args = Py_BuildValue("(O)", data);
281 ret = hook_fire(pointer_hook, args);