]> Dogcows Code - chaz/openbox/blob - src/python.cc
new mouse button code is seeming to work. you can move windows
[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 typedef struct {
26 PyObject_HEAD;
27 MouseAction action;
28 Window window;
29 MouseContext context;
30 unsigned int state;
31 unsigned int button;
32 int xroot;
33 int yroot;
34 Time time;
35 } ActionData;
36
37 typedef struct {
38 PyObject_HEAD;
39 Window window;
40 unsigned int state;
41 unsigned int key;
42 Time time;
43 } BindingData;
44
45 static void ActionDataDealloc(ActionData *self)
46 {
47 PyObject_Del((PyObject*)self);
48 }
49
50 static void BindingDataDealloc(BindingData *self)
51 {
52 PyObject_Del((PyObject*)self);
53 }
54
55 PyObject *ActionData_action(ActionData *self, PyObject *args)
56 {
57 if(!PyArg_ParseTuple(args,":action")) return NULL;
58 return PyLong_FromLong((int)self->action);
59 }
60
61 PyObject *ActionData_window(ActionData *self, PyObject *args)
62 {
63 if(!PyArg_ParseTuple(args,":window")) return NULL;
64 return PyLong_FromLong(self->window);
65 }
66
67 PyObject *ActionData_context(ActionData *self, PyObject *args)
68 {
69 if(!PyArg_ParseTuple(args,":context")) return NULL;
70 return PyLong_FromLong((int)self->context);
71 }
72
73 PyObject *ActionData_modifiers(ActionData *self, PyObject *args)
74 {
75 if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
76 return PyLong_FromUnsignedLong(self->state);
77 }
78
79 PyObject *ActionData_button(ActionData *self, PyObject *args)
80 {
81 if(!PyArg_ParseTuple(args,":button")) return NULL;
82 int b = 0;
83 switch (self->button) {
84 case Button5: b++;
85 case Button4: b++;
86 case Button3: b++;
87 case Button2: b++;
88 case Button1: b++;
89 default: ;
90 }
91 return PyLong_FromLong(b);
92 }
93
94 PyObject *ActionData_xroot(ActionData *self, PyObject *args)
95 {
96 if(!PyArg_ParseTuple(args,":xroot")) return NULL;
97 return PyLong_FromLong(self->xroot);
98 }
99
100 PyObject *ActionData_yroot(ActionData *self, PyObject *args)
101 {
102 if(!PyArg_ParseTuple(args,":yroot")) return NULL;
103 return PyLong_FromLong(self->yroot);
104 }
105
106 PyObject *ActionData_time(ActionData *self, PyObject *args)
107 {
108 if(!PyArg_ParseTuple(args,":time")) return NULL;
109 return PyLong_FromLong(self->time);
110 }
111
112 static PyMethodDef ActionData_methods[] = {
113 {"action", (PyCFunction)ActionData_action, METH_VARARGS,
114 "Return the action being executed."},
115 {"window", (PyCFunction)ActionData_window, METH_VARARGS,
116 "Return the client window id."},
117 {"context", (PyCFunction)ActionData_context, METH_VARARGS,
118 "Return the context that the action is occuring in."},
119 {"modifiers", (PyCFunction)ActionData_modifiers, METH_VARARGS,
120 "Return the modifier keys state."},
121 {"button", (PyCFunction)ActionData_button, METH_VARARGS,
122 "Return the number of the pressed button (1-5)."},
123 {"xroot", (PyCFunction)ActionData_xroot, METH_VARARGS,
124 "Return the X-position of the mouse cursor on the root window."},
125 {"yroot", (PyCFunction)ActionData_yroot, METH_VARARGS,
126 "Return the Y-position of the mouse cursor on the root window."},
127 {"time", (PyCFunction)ActionData_time, METH_VARARGS,
128 "Return the time at which the event occured."},
129 {NULL, NULL, 0, NULL}
130 };
131
132 PyObject *BindingData_window(BindingData *self, PyObject *args)
133 {
134 if(!PyArg_ParseTuple(args,":window")) return NULL;
135 return PyLong_FromLong(self->window);
136 }
137
138 PyObject *BindingData_modifiers(BindingData *self, PyObject *args)
139 {
140 if(!PyArg_ParseTuple(args,":modifiers")) return NULL;
141 return PyLong_FromUnsignedLong(self->state);
142 }
143
144 PyObject *BindingData_key(BindingData *self, PyObject *args)
145 {
146 if(!PyArg_ParseTuple(args,":key")) return NULL;
147 return PyString_FromString(
148 XKeysymToString(XKeycodeToKeysym(otk::OBDisplay::display, self->key, 0)));
149
150 }
151
152 PyObject *BindingData_time(BindingData *self, PyObject *args)
153 {
154 if(!PyArg_ParseTuple(args,":time")) return NULL;
155 return PyLong_FromLong(self->time);
156 }
157
158 static PyMethodDef BindingData_methods[] = {
159 {"window", (PyCFunction)BindingData_window, METH_VARARGS,
160 "Return the client window id."},
161 {"modifiers", (PyCFunction)BindingData_modifiers, METH_VARARGS,
162 "Return the modifier keys state."},
163 {"key", (PyCFunction)BindingData_key, METH_VARARGS,
164 "Return the name of the pressed key."},
165 {"time", (PyCFunction)BindingData_time, METH_VARARGS,
166 "Return the time at which the event occured."},
167 {NULL, NULL, 0, NULL}
168 };
169
170 static PyObject *ActionDataGetAttr(PyObject *obj, char *name)
171 {
172 return Py_FindMethod(ActionData_methods, obj, name);
173 }
174
175 static PyObject *BindingDataGetAttr(PyObject *obj, char *name)
176 {
177 return Py_FindMethod(BindingData_methods, obj, name);
178 }
179
180 static PyTypeObject ActionData_Type = {
181 PyObject_HEAD_INIT(NULL)
182 0,
183 "ActionData",
184 sizeof(ActionData),
185 0,
186 (destructor)ActionDataDealloc,
187 0,
188 (getattrfunc)ActionDataGetAttr,
189 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
190 };
191
192 static PyTypeObject BindingData_Type = {
193 PyObject_HEAD_INIT(NULL)
194 0,
195 "BindingData",
196 sizeof(BindingData),
197 0,
198 (destructor)BindingDataDealloc,
199 0,
200 (getattrfunc)BindingDataGetAttr,
201 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
202 };
203
204 // **************** //
205 // End custom types //
206 // **************** //
207
208 void python_init(char *argv0)
209 {
210 Py_SetProgramName(argv0);
211 Py_Initialize();
212 init_otk();
213 init_openbox();
214 PyRun_SimpleString("from _otk import *; from _openbox import *;");
215 PyRun_SimpleString("openbox = Openbox_instance()");
216
217 // set up access to the python global variables
218 PyObject *obmodule = PyImport_AddModule("__main__");
219 obdict = PyModule_GetDict(obmodule);
220
221 // set up the custom types
222 ActionData_Type.ob_type = &PyType_Type;
223 BindingData_Type.ob_type = &PyType_Type;
224 }
225
226 void python_destroy()
227 {
228 Py_DECREF(obdict);
229 }
230
231 bool python_exec(const std::string &path)
232 {
233 FILE *rcpyfd = fopen(path.c_str(), "r");
234 if (!rcpyfd) {
235 printf("failed to load python file %s\n", path.c_str());
236 return false;
237 }
238 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
239 fclose(rcpyfd);
240 return true;
241 }
242
243 static void call(PyObject *func, PyObject *data)
244 {
245 PyObject *arglist;
246 PyObject *result;
247
248 arglist = Py_BuildValue("(O)", data);
249
250 // call the callback
251 result = PyEval_CallObject(func, arglist);
252 if (!result) {
253 // an exception occured in the script, display it
254 PyErr_Print();
255 }
256
257 Py_XDECREF(result);
258 Py_DECREF(arglist);
259 }
260
261 void python_callback(PyObject *func, MouseAction action,
262 Window window, MouseContext context,
263 unsigned int state, unsigned int button,
264 int xroot, int yroot, Time time)
265 {
266 assert(func);
267
268 ActionData *data = PyObject_New(ActionData, &ActionData_Type);
269 data->action = action;
270 data->window = window;
271 data->context= context;
272 data->state = state;
273 data->button = button;
274 data->xroot = xroot;
275 data->yroot = yroot;
276 data->time = time;
277
278 call(func, (PyObject*)data);
279 Py_DECREF(data);
280 }
281
282 void python_callback(PyObject *func, Window window, unsigned int state,
283 unsigned int key, Time time)
284 {
285 if (!func) return;
286
287 BindingData *data = PyObject_New(BindingData, &BindingData_Type);
288 data->window = window;
289 data->state = state;
290 data->key = key;
291 data->time = time;
292
293 call(func, (PyObject*)data);
294 Py_DECREF(data);
295 }
296
297 bool python_get_string(const char *name, std::string *value)
298 {
299 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
300 if (!(val && PyString_Check(val))) return false;
301
302 *value = PyString_AsString(val);
303 return true;
304 }
305
306 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
307 {
308 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
309 if (!(val && PyList_Check(val))) return false;
310
311 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
312 PyObject *str = PyList_GetItem(val, i);
313 if (PyString_Check(str))
314 value->push_back(PyString_AsString(str));
315 }
316 return true;
317 }
318
319 // ************************************* //
320 // Stuff for calling from Python scripts //
321 // ************************************* //
322
323 /*
324 PyObject * python_register(int action, PyObject *func, bool infront = false)
325 {
326 if (!PyCallable_Check(func)) {
327 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
328 return NULL;
329 }
330
331 if (!ob::Openbox::instance->actions()->registerCallback(
332 (ob::OBActions::ActionType)action, func, infront)) {
333 PyErr_SetString(PyExc_RuntimeError, "Unable to register action callback.");
334 return NULL;
335 }
336 Py_INCREF(Py_None); return Py_None;
337 }
338
339 PyObject *unregister(int action, PyObject *func)
340 {
341 if (!PyCallable_Check(func)) {
342 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
343 return NULL;
344 }
345
346 if (!ob::Openbox::instance->actions()->unregisterCallback(
347 (ob::OBActions::ActionType)action, func)) {
348 PyErr_SetString(PyExc_RuntimeError, "Unable to unregister action callback.");
349 return NULL;
350 }
351 Py_INCREF(Py_None); return Py_None;
352 }
353
354 PyObject *unregister_all(int action)
355 {
356 if (!ob::Openbox::instance->actions()->unregisterAllCallbacks(
357 (ob::OBActions::ActionType)action)) {
358 PyErr_SetString(PyExc_RuntimeError,
359 "Unable to unregister action callbacks.");
360 return NULL;
361 }
362 Py_INCREF(Py_None); return Py_None;
363 }
364 */
365 PyObject * mbind(const std::string &button, ob::MouseContext context,
366 ob::MouseAction action, PyObject *func)
367 {
368 if (!PyCallable_Check(func)) {
369 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
370 return NULL;
371 }
372
373 if (!ob::Openbox::instance->bindings()->addButton(button, context,
374 action, func)) {
375 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
376 return NULL;
377 }
378 Py_INCREF(Py_None); return Py_None;
379 }
380
381 PyObject * kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
382 {
383 if (!PyCallable_Check(func)) {
384 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
385 return NULL;
386 }
387 if (!PyList_Check(keylist)) {
388 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
389 return NULL;
390 }
391
392 ob::OBBindings::StringVect vectkeylist;
393 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
394 PyObject *str = PyList_GetItem(keylist, i);
395 if (!PyString_Check(str)) {
396 PyErr_SetString(PyExc_TypeError,
397 "Invalid keylist. It must contain only strings.");
398 return NULL;
399 }
400 vectkeylist.push_back(PyString_AsString(str));
401 }
402
403 if (!ob::Openbox::instance->bindings()->add(vectkeylist, func)) {
404 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
405 return NULL;
406 }
407 Py_INCREF(Py_None); return Py_None;
408 }
409
410 PyObject * kunbind(PyObject *keylist)
411 {
412 if (!PyList_Check(keylist)) {
413 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
414 return NULL;
415 }
416
417 ob::OBBindings::StringVect vectkeylist;
418 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
419 PyObject *str = PyList_GetItem(keylist, i);
420 if (!PyString_Check(str)) {
421 PyErr_SetString(PyExc_TypeError,
422 "Invalid keylist. It must contain only strings.");
423 return NULL;
424 }
425 vectkeylist.push_back(PyString_AsString(str));
426 }
427
428 ob::Openbox::instance->bindings()->remove(vectkeylist);
429 Py_INCREF(Py_None); return Py_None;
430 }
431
432 void kunbind_all()
433 {
434 ob::Openbox::instance->bindings()->removeAll();
435 }
436
437 void set_reset_key(const std::string &key)
438 {
439 ob::Openbox::instance->bindings()->setResetKey(key);
440 }
441 }
This page took 0.062829 seconds and 5 git commands to generate.