9f9eb77ea991e48eea8b8dc9ed426d66f177bedc
[dana/openbox.git] / wrap / otk_timer.i
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 %module otk_timer
4
5 %{
6 #include "config.h"
7 #include "timer.hh"
8 %}
9
10 %{
11   struct PythonCallbackData {
12     PyObject *pyfunc;
13     void *data;
14   };
15  
16   /*
17     Calls a python callback for the TimeoutHandler function type
18   */
19   static void PythonCallback(PythonCallbackData *calldata) {
20     PyObject *arglist, *result;
21       
22     arglist = Py_BuildValue("(O)", calldata->data);
23       
24     // call the callback
25     result = PyEval_CallObject((PyObject*)calldata->pyfunc, arglist);
26     if (!result || PyErr_Occurred()) {
27       // an exception occured in the script, display it
28       PyErr_Print();
29     }
30       
31     Py_XDECREF(result);
32     Py_DECREF(arglist);
33   }
34 %}
35
36 // Grab a Python function object as a Python object.
37 %typemap(python,in) PyObject *func {
38   if (!PyCallable_Check($input)) {
39     PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
40     return NULL;
41   }
42   $1 = $input;
43 }
44
45 namespace otk {
46
47 %ignore Timer::Timer(long, TimeoutHandler, void*);
48 %ignore Timer::operator delete(void*);
49 %ignore Timer::initialize();
50 %ignore Timer::destroy();
51 %ignore Timer::dispatchTimers(bool);
52 %ignore Timer::nearestTimeout(struct timeval&);
53
54 %extend Timer {
55   Timer(long, PyObject*, PyObject*);
56
57   // if you don't call stop() before the object disappears, the timer will
58   // keep firing forever
59   void stop() {
60     delete self;
61   }
62 }
63
64 }
65
66 %{
67   static otk::Timer *new_otk_Timer(long delay,
68                                    PyObject *func, PyObject *data) {
69     PythonCallbackData *d = new PythonCallbackData;
70     d->pyfunc = func;
71     d->data = data;
72     return new otk::Timer(delay,
73                           (otk::Timer::TimeoutHandler)&PythonCallback, d);
74     // the PythonCallbackData is leaked.. XXX
75   }
76 %}
77
78 %include "timer.hh"