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