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