]> icculus.org git repositories - dana/openbox.git/blob - src/python.cc
put event bindings in OBBindings too
[dana/openbox.git] / src / python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "python.hh"
4 #include "openbox.hh"
5 #include "actions.hh"
6 #include "python.hh"
7 #include "bindings.hh"
8 #include "otk/display.hh"
9
10 extern "C" {
11 // The initializer in openbox_wrap.cc
12 extern void init_openbox(void);
13 // The initializer in otk_wrap.cc
14 extern void init_otk(void);
15 }
16
17 namespace ob {
18
19 static PyObject *obdict = NULL;
20
21 // ************************************************************* //
22 // Define some custom types which are passed to python callbacks //
23 // ************************************************************* //
24
25 static void dealloc(PyObject *self)
26 {
27   PyObject_Del(self);
28 }
29
30 PyObject *MotionData_window(MotionData *self, PyObject *args)
31 {
32   if(!PyArg_ParseTuple(args,":window")) return NULL;
33   return PyLong_FromLong(self->window);
34 }
35
36 PyObject *MotionData_context(MotionData *self, PyObject *args)
37 {
38   if(!PyArg_ParseTuple(args,":context")) return NULL;
39   return PyLong_FromLong((int)self->context);
40 }
41
42 PyObject *MotionData_action(MotionData *self, PyObject *args)
43 {
44   if(!PyArg_ParseTuple(args,":action")) return NULL;
45   return PyLong_FromLong((int)self->action);
46 }
47
48 PyObject *MotionData_modifiers(MotionData *self, PyObject *args)
49 {
50   if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
51   return PyLong_FromUnsignedLong(self->state);
52 }
53
54 PyObject *MotionData_button(MotionData *self, PyObject *args)
55 {
56   if(!PyArg_ParseTuple(args,":button")) return NULL;
57   int b = 0;
58   switch (self->button) {
59   case Button5: b++;
60   case Button4: b++;
61   case Button3: b++;
62   case Button2: b++;
63   case Button1: b++;
64   default: ;
65   }
66   return PyLong_FromLong(b);
67 }
68
69 PyObject *MotionData_xroot(MotionData *self, PyObject *args)
70 {
71   if(!PyArg_ParseTuple(args,":xroot")) return NULL;
72   return PyLong_FromLong(self->xroot);
73 }
74
75 PyObject *MotionData_yroot(MotionData *self, PyObject *args)
76 {
77   if(!PyArg_ParseTuple(args,":yroot")) return NULL;
78   return PyLong_FromLong(self->yroot);
79 }
80
81 PyObject *MotionData_pressx(MotionData *self, PyObject *args)
82 {
83   if(!PyArg_ParseTuple(args,":pressx")) return NULL;
84   return PyLong_FromLong(self->pressx);
85 }
86
87 PyObject *MotionData_pressy(MotionData *self, PyObject *args)
88 {
89   if(!PyArg_ParseTuple(args,":pressy")) return NULL;
90   return PyLong_FromLong(self->pressy);
91 }
92
93
94 PyObject *MotionData_press_clientx(MotionData *self, PyObject *args)
95 {
96   if(!PyArg_ParseTuple(args,":press_clientx")) return NULL;
97   return PyLong_FromLong(self->press_clientx);
98 }
99
100 PyObject *MotionData_press_clienty(MotionData *self, PyObject *args)
101 {
102   if(!PyArg_ParseTuple(args,":press_clienty")) return NULL;
103   return PyLong_FromLong(self->press_clienty);
104 }
105
106 PyObject *MotionData_press_clientwidth(MotionData *self, PyObject *args)
107 {
108   if(!PyArg_ParseTuple(args,":press_clientwidth")) return NULL;
109   return PyLong_FromLong(self->press_clientwidth);
110 }
111
112 PyObject *MotionData_press_clientheight(MotionData *self, PyObject *args)
113 {
114   if(!PyArg_ParseTuple(args,":press_clientheight")) return NULL;
115   return PyLong_FromLong(self->press_clientheight);
116 }
117
118 PyObject *MotionData_time(MotionData *self, PyObject *args)
119 {
120   if(!PyArg_ParseTuple(args,":time")) return NULL;
121   return PyLong_FromLong(self->time);
122 }
123
124 static PyMethodDef MotionData_methods[] = {
125   {"action", (PyCFunction)MotionData_action, METH_VARARGS,
126    "Return the action being executed."},
127   {"window", (PyCFunction)MotionData_window, METH_VARARGS,
128    "Return the client window id."},
129   {"context", (PyCFunction)MotionData_context, METH_VARARGS,
130    "Return the context that the action is occuring in."},
131   {"modifiers", (PyCFunction)MotionData_modifiers, METH_VARARGS,
132    "Return the modifier keys state."},
133   {"button", (PyCFunction)MotionData_button, METH_VARARGS,
134    "Return the number of the pressed button (1-5)."},
135   {"xroot", (PyCFunction)MotionData_xroot, METH_VARARGS,
136    "Return the X-position of the mouse cursor on the root window."},
137   {"yroot", (PyCFunction)MotionData_yroot, METH_VARARGS,
138    "Return the Y-position of the mouse cursor on the root window."},
139   {"pressx", (PyCFunction)MotionData_pressx, METH_VARARGS,
140    "Return the X-position of the mouse cursor at the start of the drag."},
141   {"pressy", (PyCFunction)MotionData_pressy, METH_VARARGS,
142    "Return the Y-position of the mouse cursor at the start of the drag."},
143   {"press_clientx", (PyCFunction)MotionData_press_clientx, METH_VARARGS,
144    "Return the X-position of the client at the start of the drag."},
145   {"press_clienty", (PyCFunction)MotionData_press_clienty, METH_VARARGS,
146    "Return the Y-position of the client at the start of the drag."},
147   {"press_clientwidth", (PyCFunction)MotionData_press_clientwidth,
148    METH_VARARGS,
149    "Return the width of the client at the start of the drag."},
150   {"press_clientheight", (PyCFunction)MotionData_press_clientheight,
151    METH_VARARGS,
152    "Return the height of the client at the start of the drag."},
153   {"time", (PyCFunction)MotionData_time, METH_VARARGS,
154    "Return the time at which the event occured."},
155   {NULL, NULL, 0, NULL}
156 };
157
158 static PyMethodDef ButtonData_methods[] = {
159   {"action", (PyCFunction)MotionData_action, METH_VARARGS,
160    "Return the action being executed."},
161   {"context", (PyCFunction)MotionData_context, METH_VARARGS,
162    "Return the context that the action is occuring in."},
163   {"window", (PyCFunction)MotionData_window, METH_VARARGS,
164    "Return the client window id."},
165   {"modifiers", (PyCFunction)MotionData_modifiers, METH_VARARGS,
166    "Return the modifier keys state."},
167   {"button", (PyCFunction)MotionData_button, METH_VARARGS,
168    "Return the number of the pressed button (1-5)."},
169   {"time", (PyCFunction)MotionData_time, METH_VARARGS,
170    "Return the time at which the event occured."},
171   {NULL, NULL, 0, NULL}
172 };
173
174 PyObject *EventData_action(EventData *self, PyObject *args)
175 {
176   if(!PyArg_ParseTuple(args,":action")) return NULL;
177   return PyLong_FromLong((int)self->action);
178 }
179
180 PyObject *EventData_modifiers(EventData *self, PyObject *args)
181 {
182   if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
183   return PyLong_FromUnsignedLong(self->state);
184 }
185
186 static PyMethodDef EventData_methods[] = {
187   {"window", (PyCFunction)MotionData_window, METH_VARARGS,
188    "Return the client window id."},
189   {"action", (PyCFunction)EventData_action, METH_VARARGS,
190    "Return the action being executed."},
191   {"modifiers", (PyCFunction)EventData_modifiers, METH_VARARGS,
192    "Return the modifier keys state."},
193   {NULL, NULL, 0, NULL}
194 };
195
196 PyObject *KeyData_key(KeyData *self, PyObject *args)
197 {
198   if(!PyArg_ParseTuple(args,":key")) return NULL;
199   return PyString_FromString(
200     XKeysymToString(XKeycodeToKeysym(otk::OBDisplay::display, self->key, 0)));
201
202 }
203
204 static PyMethodDef KeyData_methods[] = {
205   {"window", (PyCFunction)MotionData_window, METH_VARARGS,
206    "Return the client window id."},
207   {"modifiers", (PyCFunction)MotionData_modifiers, METH_VARARGS,
208    "Return the modifier keys state."},
209   {"key", (PyCFunction)KeyData_key, METH_VARARGS,
210    "Return the name of the pressed key."},
211   {"time", (PyCFunction)MotionData_time, METH_VARARGS,
212    "Return the time at which the event occured."},
213   {NULL, NULL, 0, NULL}
214 };
215
216 static PyObject *MotionDataGetAttr(PyObject *obj, char *name)
217 {
218   return Py_FindMethod(MotionData_methods, obj, name);
219 }
220
221 static PyObject *ButtonDataGetAttr(PyObject *obj, char *name)
222 {
223   return Py_FindMethod(ButtonData_methods, obj, name);
224 }
225
226 static PyObject *EventDataGetAttr(PyObject *obj, char *name)
227 {
228   return Py_FindMethod(EventData_methods, obj, name);
229 }
230
231 static PyObject *KeyDataGetAttr(PyObject *obj, char *name)
232 {
233   return Py_FindMethod(KeyData_methods, obj, name);
234 }
235
236 static PyTypeObject MotionData_Type = {
237   PyObject_HEAD_INIT(NULL)
238   0,
239   "MotionData",
240   sizeof(MotionData),
241   0,
242   dealloc,
243   0,
244   (getattrfunc)MotionDataGetAttr,
245   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
246 };
247
248 static PyTypeObject ButtonData_Type = {
249   PyObject_HEAD_INIT(NULL)
250   0,
251   "ButtonData",
252   sizeof(ButtonData),
253   0,
254   dealloc,
255   0,
256   (getattrfunc)ButtonDataGetAttr,
257   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
258 };
259
260 static PyTypeObject EventData_Type = {
261   PyObject_HEAD_INIT(NULL)
262   0,
263   "EventData",
264   sizeof(EventData),
265   0,
266   dealloc,
267   0,
268   (getattrfunc)EventDataGetAttr,
269   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
270 };
271
272 static PyTypeObject KeyData_Type = {
273   PyObject_HEAD_INIT(NULL)
274   0,
275   "KeyData",
276   sizeof(KeyData),
277   0,
278   dealloc,
279   0,
280   (getattrfunc)KeyDataGetAttr,
281   0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
282 };
283
284 MotionData *new_motion_data(Window window, Time time, unsigned int state,
285                           unsigned int button, MouseContext context,
286                           MouseAction action, int xroot, int yroot,
287                           const otk::Point &initpos, const otk::Rect &initarea)
288 {
289   MotionData *data = PyObject_New(MotionData, &MotionData_Type);
290   data->window = window;
291   data->time   = time;
292   data->state  = state;
293   data->button = button;
294   data->context= context;
295   data->action = action;
296   data->xroot  = xroot;
297   data->yroot  = yroot;
298   data->pressx = initpos.x();
299   data->pressy = initpos.y();
300   data->press_clientx      = initarea.x();
301   data->press_clienty      = initarea.y();
302   data->press_clientwidth  = initarea.width();
303   data->press_clientheight = initarea.height();
304   return data;
305 }
306
307 ButtonData *new_button_data(Window window, Time time, unsigned int state,
308                           unsigned int button, MouseContext context,
309                           MouseAction action)
310 {
311   ButtonData *data = PyObject_New(ButtonData, &ButtonData_Type);
312   data->window = window;
313   data->time   = time;
314   data->state  = state;
315   data->button = button;
316   data->context= context;
317   data->action = action;
318   return data;
319 }
320
321 EventData *new_event_data(Window window, EventAction action,
322                           unsigned int state)
323 {
324   EventData *data = PyObject_New(EventData, &EventData_Type);
325   data->window = window;
326   data->action = action;
327   data->state  = state;
328   return data;
329 }
330
331 KeyData *new_key_data(Window window, Time time, unsigned int state,
332                        unsigned int key)
333 {
334   KeyData *data = PyObject_New(KeyData, &KeyData_Type);
335   data->window = window;
336   data->time   = time;
337   data->state  = state;
338   data->key    = key;
339   return data;
340 }
341
342 // **************** //
343 // End custom types //
344 // **************** //
345
346 void python_init(char *argv0)
347 {
348   Py_SetProgramName(argv0);
349   Py_Initialize();
350   init_otk();
351   init_openbox();
352   PyRun_SimpleString("from _otk import *; from _openbox import *;");
353   PyRun_SimpleString("openbox = Openbox_instance()");
354
355   // set up access to the python global variables
356   PyObject *obmodule = PyImport_AddModule("__main__");
357   obdict = PyModule_GetDict(obmodule);
358
359   // set up the custom types
360   MotionData_Type.ob_type = &PyType_Type;
361   ButtonData_Type.ob_type = &PyType_Type;
362   KeyData_Type.ob_type = &PyType_Type;
363 }
364
365 void python_destroy()
366 {
367   Py_DECREF(obdict);
368 }
369
370 bool python_exec(const std::string &path)
371 {
372   FILE *rcpyfd = fopen(path.c_str(), "r");
373   if (!rcpyfd) {
374     printf("failed to load python file %s\n", path.c_str());
375     return false;
376   }
377   PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
378   fclose(rcpyfd);
379   return true;
380 }
381
382 void python_callback(PyObject *func, PyObject *data)
383 {
384   PyObject *arglist;
385   PyObject *result;
386
387   arglist = Py_BuildValue("(O)", data);
388   
389   // call the callback
390   result = PyEval_CallObject(func, arglist);
391   if (!result) {
392     // an exception occured in the script, display it
393     PyErr_Print();
394   }
395
396   Py_XDECREF(result);
397   Py_DECREF(arglist);
398 }
399
400 bool python_get_long(const char *name, long *value)
401 {
402   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
403   if (!(val && PyLong_Check(val))) return false;
404   
405   *value = PyLong_AsLong(val);
406   return true;
407 }
408
409 bool python_get_string(const char *name, std::string *value)
410 {
411   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
412   if (!(val && PyString_Check(val))) return false;
413   
414   *value = PyString_AsString(val);
415   return true;
416 }
417
418 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
419 {
420   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
421   if (!(val && PyList_Check(val))) return false;
422
423   for (int i = 0, end = PyList_Size(val); i < end; ++i) {
424     PyObject *str = PyList_GetItem(val, i);
425     if (PyString_Check(str))
426       value->push_back(PyString_AsString(str));
427   }
428   return true;
429 }
430
431 // ************************************* //
432 // Stuff for calling from Python scripts //
433 // ************************************* //
434
435 PyObject *mbind(const std::string &button, ob::MouseContext context,
436                 ob::MouseAction action, PyObject *func)
437 {
438   if (!PyCallable_Check(func)) {
439     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
440     return NULL;
441   }
442   
443   if (!ob::Openbox::instance->bindings()->addButton(button, context,
444                                                     action, func)) {
445     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
446     return NULL;
447   }
448   Py_INCREF(Py_None); return Py_None;
449 }
450
451 PyObject *ebind(ob::EventAction action, PyObject *func)
452 {
453   if (!PyCallable_Check(func)) {
454     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
455     return NULL;
456   }
457   
458   if (!ob::Openbox::instance->bindings()->addEvent(action, func)) {
459     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
460     return NULL;
461   }
462   Py_INCREF(Py_None); return Py_None;
463 }
464
465 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
466 {
467   if (!PyCallable_Check(func)) {
468     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
469     return NULL;
470   }
471   if (!PyList_Check(keylist)) {
472     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
473     return NULL;
474   }
475
476   ob::OBBindings::StringVect vectkeylist;
477   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
478     PyObject *str = PyList_GetItem(keylist, i);
479     if (!PyString_Check(str)) {
480       PyErr_SetString(PyExc_TypeError,
481                      "Invalid keylist. It must contain only strings.");
482       return NULL;
483     }
484     vectkeylist.push_back(PyString_AsString(str));
485   }
486
487   if (!ob::Openbox::instance->bindings()->addKey(vectkeylist, func)) {
488     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
489     return NULL;
490   }
491   Py_INCREF(Py_None); return Py_None;
492 }
493
494 PyObject *kunbind(PyObject *keylist)
495 {
496   if (!PyList_Check(keylist)) {
497     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
498     return NULL;
499   }
500
501   ob::OBBindings::StringVect vectkeylist;
502   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
503     PyObject *str = PyList_GetItem(keylist, i);
504     if (!PyString_Check(str)) {
505       PyErr_SetString(PyExc_TypeError,
506                      "Invalid keylist. It must contain only strings.");
507       return NULL;
508     }
509     vectkeylist.push_back(PyString_AsString(str));
510   }
511
512   ob::Openbox::instance->bindings()->removeKey(vectkeylist);
513   Py_INCREF(Py_None); return Py_None;
514 }
515
516 void kunbind_all()
517 {
518   ob::Openbox::instance->bindings()->removeAllKeys();
519 }
520
521 void set_reset_key(const std::string &key)
522 {
523   ob::Openbox::instance->bindings()->setResetKey(key);
524 }
525
526 }