]> icculus.org git repositories - mikachu/openbox.git/blob - src/python.cc
ignore the right about of unmap notifies
[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_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   PyRun_SimpleString("display = OBDisplay_display()");
374
375   /* XXX
376      sys.path.append('stuff')
377      install the .py wrappers, and include their path with this, then import em
378   */
379   
380   // set up access to the python global variables
381   PyObject *obmodule = PyImport_AddModule("__main__");
382   obdict = PyModule_GetDict(obmodule);
383
384   // set up the custom types
385   MotionData_Type.ob_type = &PyType_Type;
386   ButtonData_Type.ob_type = &PyType_Type;
387   KeyData_Type.ob_type = &PyType_Type;
388 }
389
390 void python_destroy()
391 {
392   Py_DECREF(obdict);
393 }
394
395 bool python_exec(const std::string &path)
396 {
397   FILE *rcpyfd = fopen(path.c_str(), "r");
398   if (!rcpyfd) {
399     printf("failed to load python file %s\n", path.c_str());
400     return false;
401   }
402   PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
403   fclose(rcpyfd);
404   return true;
405 }
406
407 void python_callback(PyObject *func, PyObject *data)
408 {
409   PyObject *arglist;
410   PyObject *result;
411
412   arglist = Py_BuildValue("(O)", data);
413   
414   // call the callback
415   result = PyEval_CallObject(func, arglist);
416   if (!result) {
417     // an exception occured in the script, display it
418     PyErr_Print();
419   }
420
421   Py_XDECREF(result);
422   Py_DECREF(arglist);
423 }
424
425 bool python_get_long(const char *name, long *value)
426 {
427   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
428   if (!(val && PyLong_Check(val))) return false;
429   
430   *value = PyLong_AsLong(val);
431   return true;
432 }
433
434 bool python_get_string(const char *name, std::string *value)
435 {
436   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
437   if (!(val && PyString_Check(val))) return false;
438   
439   *value = PyString_AsString(val);
440   return true;
441 }
442
443 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
444 {
445   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
446   if (!(val && PyList_Check(val))) return false;
447
448   for (int i = 0, end = PyList_Size(val); i < end; ++i) {
449     PyObject *str = PyList_GetItem(val, i);
450     if (PyString_Check(str))
451       value->push_back(PyString_AsString(str));
452   }
453   return true;
454 }
455
456 // ************************************* //
457 // Stuff for calling from Python scripts //
458 // ************************************* //
459
460 PyObject *mbind(const std::string &button, ob::MouseContext context,
461                 ob::MouseAction action, PyObject *func)
462 {
463   if (!PyCallable_Check(func)) {
464     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
465     return NULL;
466   }
467   
468   if (!ob::Openbox::instance->bindings()->addButton(button, context,
469                                                     action, func)) {
470     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
471     return NULL;
472   }
473   Py_INCREF(Py_None); return Py_None;
474 }
475
476 PyObject *ebind(ob::EventAction action, PyObject *func)
477 {
478   if (!PyCallable_Check(func)) {
479     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
480     return NULL;
481   }
482   
483   if (!ob::Openbox::instance->bindings()->addEvent(action, func)) {
484     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
485     return NULL;
486   }
487   Py_INCREF(Py_None); return Py_None;
488 }
489
490 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
491 {
492   if (!PyCallable_Check(func)) {
493     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
494     return NULL;
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   if (!ob::Openbox::instance->bindings()->addKey(vectkeylist, func)) {
513     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
514     return NULL;
515   }
516   Py_INCREF(Py_None); return Py_None;
517 }
518
519 PyObject *kunbind(PyObject *keylist, PyObject *func)
520 {
521   if (!PyList_Check(keylist)) {
522     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
523     return NULL;
524   }
525   if (!PyCallable_Check(func)) {
526     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
527     return NULL;
528   }
529   
530   ob::OBBindings::StringVect vectkeylist;
531   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
532     PyObject *str = PyList_GetItem(keylist, i);
533     if (!PyString_Check(str)) {
534       PyErr_SetString(PyExc_TypeError,
535                      "Invalid keylist. It must contain only strings.");
536       return NULL;
537     }
538     vectkeylist.push_back(PyString_AsString(str));
539   }
540
541   if (!ob::Openbox::instance->bindings()->removeKey(vectkeylist, func)) {
542       PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
543       return NULL;
544   }
545   Py_INCREF(Py_None); return Py_None;
546 }
547
548 void kunbind_all()
549 {
550   ob::Openbox::instance->bindings()->removeAllKeys();
551 }
552
553 void set_reset_key(const std::string &key)
554 {
555   ob::Openbox::instance->bindings()->setResetKey(key);
556 }
557
558 PyObject *send_client_msg(Window target, int type, Window about,
559                           long data, long data1, long data2,
560                           long data3, long data4)
561 {
562   if (type < 0 || type >= otk::OBProperty::NUM_ATOMS) {
563       PyErr_SetString(PyExc_TypeError,
564                      "Invalid atom type. Must be from otk::OBProperty::Atoms");
565       return NULL;
566   }
567   
568   XEvent e;
569   e.xclient.type = ClientMessage;
570   e.xclient.format = 32;
571   e.xclient.message_type =
572     Openbox::instance->property()->atom((otk::OBProperty::Atoms)type);
573   e.xclient.window = about;
574   e.xclient.data.l[0] = data;
575   e.xclient.data.l[1] = data1;
576   e.xclient.data.l[2] = data2;
577   e.xclient.data.l[3] = data3;
578   e.xclient.data.l[4] = data4;
579
580   XSendEvent(otk::OBDisplay::display, target, false,
581              SubstructureRedirectMask | SubstructureNotifyMask,
582              &e);
583   Py_INCREF(Py_None); return Py_None;
584 }
585
586 }