1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
8 #include "otk/display.hh"
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);
19 static PyObject
*obdict
= NULL
;
21 // ************************************************************* //
22 // Define some custom types which are passed to python callbacks //
23 // ************************************************************* //
27 OBActions::ActionType action
;
29 OBWidget::WidgetType type
;
45 static void ActionDataDealloc(ActionData
*self
)
47 PyObject_Del((PyObject
*)self
);
50 static void BindingDataDealloc(BindingData
*self
)
52 PyObject_Del((PyObject
*)self
);
55 PyObject
*ActionData_action(ActionData
*self
, PyObject
*args
)
57 if(!PyArg_ParseTuple(args
,":action")) return NULL
;
58 return PyLong_FromLong((int)self
->action
);
61 PyObject
*ActionData_window(ActionData
*self
, PyObject
*args
)
63 if(!PyArg_ParseTuple(args
,":window")) return NULL
;
64 return PyLong_FromLong(self
->window
);
67 PyObject
*ActionData_target(ActionData
*self
, PyObject
*args
)
69 if(!PyArg_ParseTuple(args
,":target")) return NULL
;
70 return PyLong_FromLong((int)self
->type
);
73 PyObject
*ActionData_modifiers(ActionData
*self
, PyObject
*args
)
75 if(!PyArg_ParseTuple(args
,":modifiers")) return NULL
;
76 return PyLong_FromUnsignedLong(self
->state
);
79 PyObject
*ActionData_button(ActionData
*self
, PyObject
*args
)
81 if(!PyArg_ParseTuple(args
,":button")) return NULL
;
83 switch (self
->button
) {
91 return PyLong_FromLong(b
);
94 PyObject
*ActionData_xroot(ActionData
*self
, PyObject
*args
)
96 if(!PyArg_ParseTuple(args
,":xroot")) return NULL
;
97 return PyLong_FromLong(self
->xroot
);
100 PyObject
*ActionData_yroot(ActionData
*self
, PyObject
*args
)
102 if(!PyArg_ParseTuple(args
,":yroot")) return NULL
;
103 return PyLong_FromLong(self
->yroot
);
106 PyObject
*ActionData_time(ActionData
*self
, PyObject
*args
)
108 if(!PyArg_ParseTuple(args
,":time")) return NULL
;
109 return PyLong_FromLong(self
->time
);
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 {"target", (PyCFunction
)ActionData_target
, METH_VARARGS
,
118 "Return the target type that the action is occuring on."},
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
}
132 PyObject
*BindingData_window(BindingData
*self
, PyObject
*args
)
134 if(!PyArg_ParseTuple(args
,":window")) return NULL
;
135 return PyLong_FromLong(self
->window
);
138 PyObject
*BindingData_modifiers(BindingData
*self
, PyObject
*args
)
140 if(!PyArg_ParseTuple(args
,":modifiers")) return NULL
;
141 return PyLong_FromUnsignedLong(self
->state
);
144 PyObject
*BindingData_key(BindingData
*self
, PyObject
*args
)
146 if(!PyArg_ParseTuple(args
,":key")) return NULL
;
147 return PyString_FromString(
148 XKeysymToString(XKeycodeToKeysym(otk::OBDisplay::display
, self
->key
, 0)));
152 PyObject
*BindingData_time(BindingData
*self
, PyObject
*args
)
154 if(!PyArg_ParseTuple(args
,":time")) return NULL
;
155 return PyLong_FromLong(self
->time
);
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
}
170 static PyObject
*ActionDataGetAttr(PyObject
*obj
, char *name
)
172 return Py_FindMethod(ActionData_methods
, obj
, name
);
175 static PyObject
*BindingDataGetAttr(PyObject
*obj
, char *name
)
177 return Py_FindMethod(BindingData_methods
, obj
, name
);
180 static PyTypeObject ActionData_Type
= {
181 PyObject_HEAD_INIT(NULL
)
186 (destructor
)ActionDataDealloc
,
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
192 static PyTypeObject BindingData_Type
= {
193 PyObject_HEAD_INIT(NULL
)
198 (destructor
)BindingDataDealloc
,
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
204 // **************** //
205 // End custom types //
206 // **************** //
208 void python_init(char *argv0
)
210 Py_SetProgramName(argv0
);
214 PyRun_SimpleString("from _otk import *; from _openbox import *;");
215 PyRun_SimpleString("openbox = Openbox_instance()");
217 // set up access to the python global variables
218 PyObject
*obmodule
= PyImport_AddModule("__main__");
219 obdict
= PyModule_GetDict(obmodule
);
221 // set up the custom types
222 ActionData_Type
.ob_type
= &PyType_Type
;
223 BindingData_Type
.ob_type
= &PyType_Type
;
226 void python_destroy()
231 bool python_exec(const std::string
&path
)
233 FILE *rcpyfd
= fopen(path
.c_str(), "r");
235 printf("failed to load python file %s\n", path
.c_str());
238 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
243 static void call(PyObject
*func
, PyObject
*data
)
248 arglist
= Py_BuildValue("(O)", data
);
251 result
= PyEval_CallObject(func
, arglist
);
253 // an exception occured in the script, display it
261 void python_callback(PyObject
*func
, OBActions::ActionType action
,
262 Window window
, OBWidget::WidgetType type
,
263 unsigned int state
, unsigned int button
,
264 int xroot
, int yroot
, Time time
)
268 ActionData
*data
= PyObject_New(ActionData
, &ActionData_Type
);
269 data
->action
= action
;
270 data
->window
= window
;
273 data
->button
= button
;
278 call(func
, (PyObject
*)data
);
282 void python_callback(PyObject
*func
, Window window
, unsigned int state
,
283 unsigned int key
, Time time
)
287 BindingData
*data
= PyObject_New(BindingData
, &BindingData_Type
);
288 data
->window
= window
;
293 call(func
, (PyObject
*)data
);
297 bool python_get_string(const char *name
, std::string
*value
)
299 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
300 if (!(val
&& PyString_Check(val
))) return false;
302 *value
= PyString_AsString(val
);
306 bool python_get_stringlist(const char *name
, std::vector
<std::string
> *value
)
308 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
309 if (!(val
&& PyList_Check(val
))) return false;
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
));