]> Dogcows Code - chaz/openbox/blob - src/python.cc
so close to keybindings. wont link for now.
[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
6 #include <vector>
7 #include <algorithm>
8
9 namespace ob {
10
11 typedef std::vector<PyObject*> FunctionList;
12
13 static FunctionList callbacks[OBActions::NUM_ACTIONS];
14 static FunctionList keyfuncs;
15 static FunctionList mousefuncs;
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 = keyfuncs.size();
164 if (Openbox::instance->bindings()->add_key(vectkeylist, id)) {
165 Py_XINCREF(callback); // Add a reference to new callback
166 keyfuncs.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(keyfuncs[id]); // shouldn't be able to remove it twice
196 Py_XDECREF(keyfuncs[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 keyfuncs[id] = 0;
200 return true;
201 }
202
203 return false;
204 }
205
206 bool python_bind_mouse(const std::string &button, PyObject *callback)
207 {
208 if (!PyCallable_Check(callback)) {
209 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
210 return false;
211 }
212
213 // the id is what the binding class can call back with so it doesnt have to
214 // worry about the python function pointer
215 int id = mousefuncs.size();
216 if (Openbox::instance->bindings()->add_mouse(button, id)) {
217 Py_XINCREF(callback); // Add a reference to new callback
218 mousefuncs.push_back(callback);
219 return true;
220 } else {
221 PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid.");
222 return false;
223 }
224 }
225
226 bool python_unbind_mouse(const std::string &button)
227 {
228 int id;
229 if ((id =
230 Openbox::instance->bindings()->remove_mouse(button)) >= 0) {
231 assert(mousefuncs[id]); // shouldn't be able to remove it twice
232 Py_XDECREF(mousefuncs[id]); // Dispose of previous callback
233 // important note: we don't erase the item from the list cuz that would
234 // ruin all the id's that are in use. simply nullify it.
235 mousefuncs[id] = 0;
236 return true;
237 }
238
239 return false;
240 }
241
242 bool python_unbind_all()
243 {
244 Openbox::instance->bindings()->remove_all();
245 return true;
246 }
247
248
249 void python_callback_binding(int id, OBActions::ActionType action,
250 Window window, unsigned int state,
251 unsigned int keybutton, Time time)
252 {
253 PyObject *func;
254
255 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
256
257 if (action == OBActions::Action_KeyPress)
258 func = keyfuncs[id];
259 else
260 func = mousefuncs[id];
261
262 if (!func) return;
263
264 PyObject *arglist;
265 PyObject *result;
266
267 arglist = Py_BuildValue("iliil", action, window, state, keybutton, time);
268
269 // call the callback
270 result = PyEval_CallObject(func, arglist);
271 if (result) {
272 Py_DECREF(result);
273 } else {
274 // an exception occured in the script, display it
275 PyErr_Print();
276 }
277
278 Py_DECREF(arglist);
279 }
280
281 }
This page took 0.043457 seconds and 4 git commands to generate.