]> icculus.org git repositories - mikachu/openbox.git/blob - wrap/callback.i
put the desktop names in the right variable
[mikachu/openbox.git] / wrap / callback.i
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 %{
4 /*
5   Calls a python callback for the MouseCallback function type
6  */
7 static void PythonMouseCallback(ob::MouseData *data, void *pyfunc)
8 {
9   PyObject *arglist, *pdata, *result;
10
11   pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__MouseData, 0);
12   arglist = Py_BuildValue("(O)", pdata);
13   Py_DECREF(pdata);
14
15   // call the callback
16   result = PyEval_CallObject((PyObject*)pyfunc, arglist);
17   if (!result || PyErr_Occurred()) {
18     // an exception occured in the script, display it
19     PyErr_Print();
20   }
21
22   Py_XDECREF(result);
23   Py_DECREF(arglist);
24 }
25
26 /*
27   Calls a python callback for the KeyCallback function type
28  */
29 static void PythonKeyCallback(ob::KeyData *data, void *pyfunc)
30 {
31   PyObject *arglist, *pdata, *result;
32
33   pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__KeyData, 0);
34   arglist = Py_BuildValue("(O)", pdata);
35   Py_DECREF(pdata);
36
37   // call the callback
38   result = PyEval_CallObject((PyObject*)pyfunc, arglist);
39   if (!result || PyErr_Occurred()) {
40     // an exception occured in the script, display it
41     PyErr_Print();
42   }
43
44   Py_XDECREF(result);
45   Py_DECREF(arglist);
46 }
47
48 /*
49   Calls a python callback for the EventCallback function type
50  */
51 static void PythonEventCallback(ob::EventData *data, void *pyfunc)
52 {
53   PyObject *arglist, *pdata, *result;
54
55   pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__EventData, 0);
56   arglist = Py_BuildValue("(O)", pdata);
57   Py_DECREF(pdata);
58
59   // call the callback
60   result = PyEval_CallObject((PyObject*)pyfunc, arglist);
61   if (!result || PyErr_Occurred()) {
62     // an exception occured in the script, display it
63     PyErr_Print();
64   }
65
66   Py_XDECREF(result);
67   Py_DECREF(arglist);
68 }
69 %}
70
71 // for all of these, PyErr_SetString is called before they return a false!
72 %exception mbind {
73   $action
74   if (!result) return NULL;
75 }
76 %exception kbind {
77   $action
78   if (!result) return NULL;
79 }
80 %exception ebind {
81   $action
82   if (!result) return NULL;
83 }
84 %exception kgrab {
85   $action
86   if (!result) return NULL;
87 }
88 %exception mgrab {
89   $action
90   if (!result) return NULL;
91 }
92
93 // Grab a Python function object as a Python object.
94 %typemap(python,in) PyObject *func {
95   if (!PyCallable_Check($input)) {
96     PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
97     return NULL;
98   }
99   $1 = $input;
100 }
101
102 %inline %{
103 bool mbind(const std::string &button, ob::MouseContext::MC context,
104            ob::MouseAction::MA action, PyObject *func)
105 {
106   if(context < 0 || context >= ob::MouseContext::NUM_MOUSE_CONTEXT) {
107     PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
108     return false;
109   }
110   if(action < 0 || action >= ob::MouseAction::NUM_MOUSE_ACTION) {
111     PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
112     return false;
113   }
114   
115   if (!ob::openbox->bindings()->addButton(button, context,
116                                           action, PythonMouseCallback, func)) {
117     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
118     return false;
119   }
120   Py_INCREF(func);
121   return true;
122 }
123
124 bool ebind(ob::EventAction::EA action, PyObject *func)
125 {
126   if(action < 0 || action >= ob::EventAction::NUM_EVENT_ACTION) {
127     PyErr_SetString(PyExc_ValueError, "Invalid EventAction");
128     return false;
129   }
130
131   if (!ob::openbox->bindings()->addEvent(action, PythonEventCallback, func)) {
132     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
133     return false;
134   }
135   Py_INCREF(func);
136   return true;
137 }
138
139 bool kgrab(int screen, PyObject *func)
140 {
141   if (!ob::openbox->bindings()->grabKeyboard(screen,
142                                              PythonKeyCallback, func)) {
143     PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
144     return false;
145   }
146   Py_INCREF(func);
147   return true;
148 }
149
150 void kungrab()
151 {
152   ob::openbox->bindings()->ungrabKeyboard();
153 }
154
155 bool mgrab(int screen)
156 {
157   if (!ob::openbox->bindings()->grabPointer(screen)) {
158     PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
159     return false;
160   }
161   return true;
162 }
163
164 void mungrab()
165 {
166   ob::openbox->bindings()->ungrabPointer();
167 }
168
169 bool kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
170 {
171   if (!PyList_Check(keylist)) {
172     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
173     return false;
174   }
175   if(context < 0 || context >= ob::KeyContext::NUM_KEY_CONTEXT) {
176     PyErr_SetString(PyExc_ValueError, "Invalid KeyContext");
177     return false;
178   }
179
180   ob::Bindings::StringVect vectkeylist;
181   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
182     PyObject *str = PyList_GetItem(keylist, i);
183     if (!PyString_Check(str)) {
184       PyErr_SetString(PyExc_TypeError,
185                      "Invalid keylist. It must contain only strings.");
186       return false;
187     }
188     vectkeylist.push_back(PyString_AsString(str));
189   }
190
191   (void)context; // XXX use this sometime!
192   if (!ob::openbox->bindings()->addKey(vectkeylist, PythonKeyCallback, func)) {
193     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
194     return false;
195   }
196   Py_INCREF(func);
197   return true;
198 }
199
200 %};