]> Dogcows Code - chaz/openbox/blob - src/python.cc
allow to bind multiple functions to everything
[chaz/openbox] / 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 /* XXX
356 sys.path.append('stuff')
357 install the .py wrappers, and include their path with this, then import em
358 */
359
360 // set up access to the python global variables
361 PyObject *obmodule = PyImport_AddModule("__main__");
362 obdict = PyModule_GetDict(obmodule);
363
364 // set up the custom types
365 MotionData_Type.ob_type = &PyType_Type;
366 ButtonData_Type.ob_type = &PyType_Type;
367 KeyData_Type.ob_type = &PyType_Type;
368 }
369
370 void python_destroy()
371 {
372 Py_DECREF(obdict);
373 }
374
375 bool python_exec(const std::string &path)
376 {
377 FILE *rcpyfd = fopen(path.c_str(), "r");
378 if (!rcpyfd) {
379 printf("failed to load python file %s\n", path.c_str());
380 return false;
381 }
382 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
383 fclose(rcpyfd);
384 return true;
385 }
386
387 void python_callback(PyObject *func, PyObject *data)
388 {
389 PyObject *arglist;
390 PyObject *result;
391
392 arglist = Py_BuildValue("(O)", data);
393
394 // call the callback
395 result = PyEval_CallObject(func, arglist);
396 if (!result) {
397 // an exception occured in the script, display it
398 PyErr_Print();
399 }
400
401 Py_XDECREF(result);
402 Py_DECREF(arglist);
403 }
404
405 bool python_get_long(const char *name, long *value)
406 {
407 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
408 if (!(val && PyLong_Check(val))) return false;
409
410 *value = PyLong_AsLong(val);
411 return true;
412 }
413
414 bool python_get_string(const char *name, std::string *value)
415 {
416 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
417 if (!(val && PyString_Check(val))) return false;
418
419 *value = PyString_AsString(val);
420 return true;
421 }
422
423 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
424 {
425 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
426 if (!(val && PyList_Check(val))) return false;
427
428 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
429 PyObject *str = PyList_GetItem(val, i);
430 if (PyString_Check(str))
431 value->push_back(PyString_AsString(str));
432 }
433 return true;
434 }
435
436 // ************************************* //
437 // Stuff for calling from Python scripts //
438 // ************************************* //
439
440 PyObject *mbind(const std::string &button, ob::MouseContext context,
441 ob::MouseAction action, PyObject *func)
442 {
443 if (!PyCallable_Check(func)) {
444 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
445 return NULL;
446 }
447
448 if (!ob::Openbox::instance->bindings()->addButton(button, context,
449 action, func)) {
450 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
451 return NULL;
452 }
453 Py_INCREF(Py_None); return Py_None;
454 }
455
456 PyObject *ebind(ob::EventAction action, PyObject *func)
457 {
458 if (!PyCallable_Check(func)) {
459 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
460 return NULL;
461 }
462
463 if (!ob::Openbox::instance->bindings()->addEvent(action, func)) {
464 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
465 return NULL;
466 }
467 Py_INCREF(Py_None); return Py_None;
468 }
469
470 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
471 {
472 if (!PyCallable_Check(func)) {
473 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
474 return NULL;
475 }
476 if (!PyList_Check(keylist)) {
477 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
478 return NULL;
479 }
480
481 ob::OBBindings::StringVect vectkeylist;
482 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
483 PyObject *str = PyList_GetItem(keylist, i);
484 if (!PyString_Check(str)) {
485 PyErr_SetString(PyExc_TypeError,
486 "Invalid keylist. It must contain only strings.");
487 return NULL;
488 }
489 vectkeylist.push_back(PyString_AsString(str));
490 }
491
492 if (!ob::Openbox::instance->bindings()->addKey(vectkeylist, func)) {
493 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
494 return NULL;
495 }
496 Py_INCREF(Py_None); return Py_None;
497 }
498
499 PyObject *kunbind(PyObject *keylist, PyObject *func)
500 {
501 if (!PyList_Check(keylist)) {
502 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
503 return NULL;
504 }
505 if (!PyCallable_Check(func)) {
506 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
507 return NULL;
508 }
509
510 ob::OBBindings::StringVect vectkeylist;
511 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
512 PyObject *str = PyList_GetItem(keylist, i);
513 if (!PyString_Check(str)) {
514 PyErr_SetString(PyExc_TypeError,
515 "Invalid keylist. It must contain only strings.");
516 return NULL;
517 }
518 vectkeylist.push_back(PyString_AsString(str));
519 }
520
521 if (!ob::Openbox::instance->bindings()->removeKey(vectkeylist, func)) {
522 PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
523 return NULL;
524 }
525 Py_INCREF(Py_None); return Py_None;
526 }
527
528 void kunbind_all()
529 {
530 ob::Openbox::instance->bindings()->removeAllKeys();
531 }
532
533 void set_reset_key(const std::string &key)
534 {
535 ob::Openbox::instance->bindings()->setResetKey(key);
536 }
537
538 }
This page took 0.058253 seconds and 5 git commands to generate.