]> Dogcows Code - chaz/openbox/blob - src/python.cc
load config options from the python environment
[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 "otk/display.hh"
6
7 #include <vector>
8 #include <algorithm>
9
10 extern "C" {
11 #include <Python.h>
12
13 // The initializer in openbox_wrap.cc
14 extern void init_openbox(void);
15 // The initializer in otk_wrap.cc
16 extern void init_otk(void);
17 }
18
19 namespace ob {
20
21 typedef std::vector<PyObject*> FunctionList;
22
23 static FunctionList callbacks[OBActions::NUM_ACTIONS];
24 static FunctionList bindfuncs;
25
26 static PyObject *obdict;
27
28 void python_init(char *argv0)
29 {
30 Py_SetProgramName(argv0);
31 Py_Initialize();
32 init_otk();
33 init_openbox();
34 PyRun_SimpleString("from _otk import *; from _openbox import *;");
35
36 // set up access to the python global variables
37 PyObject *obmodule = PyImport_AddModule("__main__");
38 obdict = PyModule_GetDict(obmodule);
39 }
40
41 bool python_exec(const char *file) {
42 FILE *rcpyfd = fopen(file, "r");
43 if (!rcpyfd) {
44 printf("failed to load python file %s\n", file);
45 return false;
46 }
47 PyRun_SimpleFile(rcpyfd, const_cast<char*>(file));
48 fclose(rcpyfd);
49 return true;
50 }
51
52 bool python_get_string(const char *name, std::string *value)
53 {
54 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
55 if (!val) return false;
56
57 *value = PyString_AsString(val);
58 return true;
59 }
60
61
62 bool python_register(int action, PyObject *callback)
63 {
64 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
65 action == OBActions::Action_KeyPress) {
66 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
67 return false;
68 }
69 if (!PyCallable_Check(callback)) {
70 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
71 return false;
72 }
73
74 FunctionList::iterator it = std::find(callbacks[action].begin(),
75 callbacks[action].end(),
76 callback);
77 if (it == callbacks[action].end()) { // not already in there
78 Py_XINCREF(callback); // Add a reference to new callback
79 callbacks[action].push_back(callback);
80 }
81 return true;
82 }
83
84 bool python_preregister(int action, PyObject *callback)
85 {
86 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
87 action == OBActions::Action_KeyPress) {
88 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
89 return false;
90 }
91 if (!PyCallable_Check(callback)) {
92 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
93 return false;
94 }
95
96 FunctionList::iterator it = std::find(callbacks[action].begin(),
97 callbacks[action].end(),
98 callback);
99 if (it == callbacks[action].end()) { // not already in there
100 Py_XINCREF(callback); // Add a reference to new callback
101 callbacks[action].insert(callbacks[action].begin(), callback);
102 }
103 return true;
104 }
105
106 bool python_unregister(int action, PyObject *callback)
107 {
108 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
109 action == OBActions::Action_KeyPress) {
110 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
111 return false;
112 }
113 if (!PyCallable_Check(callback)) {
114 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
115 return false;
116 }
117
118 FunctionList::iterator it = std::find(callbacks[action].begin(),
119 callbacks[action].end(),
120 callback);
121 if (it != callbacks[action].end()) { // its been registered before
122 Py_XDECREF(*it); // Dispose of previous callback
123 callbacks[action].erase(it);
124 }
125 return true;
126 }
127
128 bool python_unregister_all(int action)
129 {
130 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
131 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
132 return false;
133 }
134
135 while (!callbacks[action].empty()) {
136 Py_XDECREF(callbacks[action].back());
137 callbacks[action].pop_back();
138 }
139 return true;
140 }
141
142 void python_callback(OBActions::ActionType action, Window window,
143 OBWidget::WidgetType type, unsigned int state,
144 long d1, long d2, long d3, long d4)
145 {
146 PyObject *arglist;
147 PyObject *result;
148
149 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
150
151 if (d4 != LONG_MIN)
152 arglist = Py_BuildValue("iliillll", action, window, type, state,
153 d1, d2, d3, d4);
154 else if (d3 != LONG_MIN)
155 arglist = Py_BuildValue("iliilll", action, window, type, state,
156 d1, d2, d3);
157 else if (d2 != LONG_MIN)
158 arglist = Py_BuildValue("iliill", action, window, type, state, d1, d2);
159 else if (d1 != LONG_MIN)
160 arglist = Py_BuildValue("iliil", action, window, type, state, d1);
161 else
162 arglist = Py_BuildValue("ilii", action, window, type, state);
163
164 FunctionList::iterator it, end = callbacks[action].end();
165 for (it = callbacks[action].begin(); it != end; ++it) {
166 // call the callback
167 result = PyEval_CallObject(*it, arglist);
168 if (result) {
169 Py_DECREF(result);
170 } else {
171 // an exception occured in the script, display it
172 PyErr_Print();
173 }
174 }
175
176 Py_DECREF(arglist);
177 }
178
179
180
181
182
183
184 bool python_bind(PyObject *keylist, PyObject *callback)
185 {
186 if (!PyList_Check(keylist)) {
187 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
188 return false;
189 }
190 if (!PyCallable_Check(callback)) {
191 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
192 return false;
193 }
194
195 OBBindings::StringVect vectkeylist;
196 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
197 PyObject *str = PyList_GetItem(keylist, i);
198 if (!PyString_Check(str)) {
199 PyErr_SetString(PyExc_AssertionError,
200 "Invalid keylist. It must contain only strings.");
201 return false;
202 }
203 vectkeylist.push_back(PyString_AsString(str));
204 }
205
206 // the id is what the binding class can call back with so it doesnt have to
207 // worry about the python function pointer
208 int id = bindfuncs.size();
209 if (Openbox::instance->bindings()->add(vectkeylist, id)) {
210 Py_XINCREF(callback); // Add a reference to new callback
211 bindfuncs.push_back(callback);
212 return true;
213 } else {
214 PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid.");
215 return false;
216 }
217 }
218
219 bool python_unbind(PyObject *keylist)
220 {
221 if (!PyList_Check(keylist)) {
222 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
223 return false;
224 }
225
226 OBBindings::StringVect vectkeylist;
227 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
228 PyObject *str = PyList_GetItem(keylist, i);
229 if (!PyString_Check(str)) {
230 PyErr_SetString(PyExc_AssertionError,
231 "Invalid keylist. It must contain only strings.");
232 return false;
233 }
234 vectkeylist.push_back(PyString_AsString(str));
235 }
236
237 int id;
238 if ((id =
239 Openbox::instance->bindings()->remove(vectkeylist)) >= 0) {
240 assert(bindfuncs[id]); // shouldn't be able to remove it twice
241 Py_XDECREF(bindfuncs[id]); // Dispose of previous callback
242 // important note: we don't erase the item from the list cuz that would
243 // ruin all the id's that are in use. simply nullify it.
244 bindfuncs[id] = 0;
245 return true;
246 }
247
248 return false;
249 }
250
251 void python_set_reset_key(const std::string &key)
252 {
253 Openbox::instance->bindings()->setResetKey(key);
254 }
255
256 void python_unbind_all()
257 {
258 Openbox::instance->bindings()->remove_all();
259 }
260
261
262 void python_callback_binding(int id, Window window, unsigned int state,
263 unsigned int keybutton, Time time)
264 {
265 if (!bindfuncs[id]) return; // the key was unbound
266
267 PyObject *arglist;
268 PyObject *result;
269
270 arglist = Py_BuildValue("lisl", window, state,
271 XKeysymToString(
272 XKeycodeToKeysym(otk::OBDisplay::display,
273 keybutton, 0)),
274 time);
275
276 // call the callback
277 result = PyEval_CallObject(bindfuncs[id], arglist);
278 if (result) {
279 Py_DECREF(result);
280 } else {
281 // an exception occured in the script, display it
282 PyErr_Print();
283 }
284
285 Py_DECREF(arglist);
286 }
287
288 }
This page took 0.052211 seconds and 5 git commands to generate.