]> icculus.org git repositories - mikachu/openbox.git/blob - src/python.cc
moving a window is possible once again
[mikachu/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 *KeyData_key(KeyData *self, PyObject *args)
175 {
176   if(!PyArg_ParseTuple(args,":key")) return NULL;
177   return PyString_FromString(
178     XKeysymToString(XKeycodeToKeysym(otk::OBDisplay::display, self->key, 0)));
179
180 }
181
182 static PyMethodDef KeyData_methods[] = {
183   {"window", (PyCFunction)MotionData_window, METH_VARARGS,
184    "Return the client window id."},
185   {"modifiers", (PyCFunction)MotionData_modifiers, METH_VARARGS,
186    "Return the modifier keys state."},
187   {"key", (PyCFunction)KeyData_key, METH_VARARGS,
188    "Return the name of the pressed key."},
189   {"time", (PyCFunction)MotionData_time, METH_VARARGS,
190    "Return the time at which the event occured."},
191   {NULL, NULL, 0, NULL}
192 };
193
194 static PyObject *MotionDataGetAttr(PyObject *obj, char *name)
195 {
196   return Py_FindMethod(MotionData_methods, obj, name);
197 }
198
199 static PyObject *ButtonDataGetAttr(PyObject *obj, char *name)
200 {
201   return Py_FindMethod(ButtonData_methods, obj, name);
202 }
203
204 static PyObject *KeyDataGetAttr(PyObject *obj, char *name)
205 {
206   return Py_FindMethod(KeyData_methods, obj, name);
207 }
208
209 static PyTypeObject MotionData_Type = {
210   PyObject_HEAD_INIT(NULL)
211   0,
212   "MotionData",
213   sizeof(MotionData),
214   0,
215   dealloc,
216   0,
217   (getattrfunc)MotionDataGetAttr,
218   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
219 };
220
221 static PyTypeObject ButtonData_Type = {
222   PyObject_HEAD_INIT(NULL)
223   0,
224   "ButtonData",
225   sizeof(ButtonData),
226   0,
227   dealloc,
228   0,
229   (getattrfunc)ButtonDataGetAttr,
230   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
231 };
232
233 static PyTypeObject KeyData_Type = {
234   PyObject_HEAD_INIT(NULL)
235   0,
236   "KeyData",
237   sizeof(KeyData),
238   0,
239   dealloc,
240   0,
241   (getattrfunc)KeyDataGetAttr,
242   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
243 };
244
245 MotionData *new_motion_data(Window window, Time time, unsigned int state,
246                           unsigned int button, MouseContext context,
247                           MouseAction action, int xroot, int yroot,
248                           const otk::Point &initpos, const otk::Rect &initarea)
249 {
250   MotionData *data = PyObject_New(MotionData, &MotionData_Type);
251   data->window = window;
252   data->time   = time;
253   data->state  = state;
254   data->button = button;
255   data->context= context;
256   data->action = action;
257   data->xroot  = xroot;
258   data->yroot  = yroot;
259   data->pressx = initpos.x();
260   data->pressy = initpos.y();
261   data->press_clientx      = initarea.x();
262   data->press_clienty      = initarea.y();
263   data->press_clientwidth  = initarea.width();
264   data->press_clientheight = initarea.height();
265   return data;
266 }
267
268 ButtonData *new_button_data(Window window, Time time, unsigned int state,
269                           unsigned int button, MouseContext context,
270                           MouseAction action)
271 {
272   ButtonData *data = PyObject_New(ButtonData, &ButtonData_Type);
273   data->window = window;
274   data->time   = time;
275   data->state  = state;
276   data->button = button;
277   data->context= context;
278   data->action = action;
279   return data;
280 }
281
282 KeyData *new_key_data(Window window, Time time, unsigned int state,
283                        unsigned int key)
284 {
285   KeyData *data = PyObject_New(KeyData, &KeyData_Type);
286   data->window = window;
287   data->time   = time;
288   data->state  = state;
289   data->key    = key;
290   return data;
291 }
292
293 // **************** //
294 // End custom types //
295 // **************** //
296
297 void python_init(char *argv0)
298 {
299   Py_SetProgramName(argv0);
300   Py_Initialize();
301   init_otk();
302   init_openbox();
303   PyRun_SimpleString("from _otk import *; from _openbox import *;");
304   PyRun_SimpleString("openbox = Openbox_instance()");
305
306   // set up access to the python global variables
307   PyObject *obmodule = PyImport_AddModule("__main__");
308   obdict = PyModule_GetDict(obmodule);
309
310   // set up the custom types
311   MotionData_Type.ob_type = &PyType_Type;
312   ButtonData_Type.ob_type = &PyType_Type;
313   KeyData_Type.ob_type = &PyType_Type;
314 }
315
316 void python_destroy()
317 {
318   Py_DECREF(obdict);
319 }
320
321 bool python_exec(const std::string &path)
322 {
323   FILE *rcpyfd = fopen(path.c_str(), "r");
324   if (!rcpyfd) {
325     printf("failed to load python file %s\n", path.c_str());
326     return false;
327   }
328   PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
329   fclose(rcpyfd);
330   return true;
331 }
332
333 void python_callback(PyObject *func, PyObject *data)
334 {
335   PyObject *arglist;
336   PyObject *result;
337
338   arglist = Py_BuildValue("(O)", data);
339   
340   // call the callback
341   result = PyEval_CallObject(func, arglist);
342   if (!result) {
343     // an exception occured in the script, display it
344     PyErr_Print();
345   }
346
347   Py_XDECREF(result);
348   Py_DECREF(arglist);
349 }
350
351 bool python_get_string(const char *name, std::string *value)
352 {
353   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
354   if (!(val && PyString_Check(val))) return false;
355   
356   *value = PyString_AsString(val);
357   return true;
358 }
359
360 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
361 {
362   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
363   if (!(val && PyList_Check(val))) return false;
364
365   for (int i = 0, end = PyList_Size(val); i < end; ++i) {
366     PyObject *str = PyList_GetItem(val, i);
367     if (PyString_Check(str))
368       value->push_back(PyString_AsString(str));
369   }
370   return true;
371 }
372
373 // ************************************* //
374 // Stuff for calling from Python scripts //
375 // ************************************* //
376
377 /*
378 PyObject * python_register(int action, PyObject *func, bool infront = false)
379 {
380   if (!PyCallable_Check(func)) {
381     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
382     return NULL;
383   }
384
385   if (!ob::Openbox::instance->actions()->registerCallback(
386         (ob::OBActions::ActionType)action, func, infront)) {
387     PyErr_SetString(PyExc_RuntimeError, "Unable to register action callback.");
388     return NULL;
389   }
390   Py_INCREF(Py_None); return Py_None;
391 }
392
393 PyObject *unregister(int action, PyObject *func)
394 {
395   if (!PyCallable_Check(func)) {
396     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
397     return NULL;
398   }
399
400   if (!ob::Openbox::instance->actions()->unregisterCallback(
401         (ob::OBActions::ActionType)action, func)) {
402     PyErr_SetString(PyExc_RuntimeError, "Unable to unregister action callback.");
403     return NULL;
404   }
405   Py_INCREF(Py_None); return Py_None;
406 }
407
408 PyObject *unregister_all(int action)
409 {
410   if (!ob::Openbox::instance->actions()->unregisterAllCallbacks(
411         (ob::OBActions::ActionType)action)) {
412     PyErr_SetString(PyExc_RuntimeError,
413                     "Unable to unregister action callbacks.");
414     return NULL;
415   }
416   Py_INCREF(Py_None); return Py_None;
417 }
418 */
419 PyObject * mbind(const std::string &button, ob::MouseContext context,
420                  ob::MouseAction action, PyObject *func)
421 {
422   if (!PyCallable_Check(func)) {
423     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
424     return NULL;
425   }
426   
427   if (!ob::Openbox::instance->bindings()->addButton(button, context,
428                                                     action, func)) {
429     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
430     return NULL;
431   }
432   Py_INCREF(Py_None); return Py_None;
433 }
434
435 PyObject * kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
436 {
437   if (!PyCallable_Check(func)) {
438     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
439     return NULL;
440   }
441   if (!PyList_Check(keylist)) {
442     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
443     return NULL;
444   }
445
446   ob::OBBindings::StringVect vectkeylist;
447   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
448     PyObject *str = PyList_GetItem(keylist, i);
449     if (!PyString_Check(str)) {
450       PyErr_SetString(PyExc_TypeError,
451                      "Invalid keylist. It must contain only strings.");
452       return NULL;
453     }
454     vectkeylist.push_back(PyString_AsString(str));
455   }
456
457   if (!ob::Openbox::instance->bindings()->add(vectkeylist, func)) {
458     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
459     return NULL;
460   }
461   Py_INCREF(Py_None); return Py_None;
462 }
463
464 PyObject * kunbind(PyObject *keylist)
465 {
466   if (!PyList_Check(keylist)) {
467     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
468     return NULL;
469   }
470
471   ob::OBBindings::StringVect vectkeylist;
472   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
473     PyObject *str = PyList_GetItem(keylist, i);
474     if (!PyString_Check(str)) {
475       PyErr_SetString(PyExc_TypeError,
476                      "Invalid keylist. It must contain only strings.");
477       return NULL;
478     }
479     vectkeylist.push_back(PyString_AsString(str));
480   }
481
482   ob::Openbox::instance->bindings()->remove(vectkeylist);
483   Py_INCREF(Py_None); return Py_None;
484 }
485
486 void kunbind_all()
487 {
488   ob::Openbox::instance->bindings()->removeAll();
489 }
490
491 void set_reset_key(const std::string &key)
492 {
493   ob::Openbox::instance->bindings()->setResetKey(key);
494 }
495
496 }