1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
11 typedef std::vector<PyObject*> FunctionList;
13 static FunctionList callbacks[OBActions::NUM_ACTIONS];
14 static FunctionList keyfuncs;
15 static FunctionList mousefuncs;
17 bool python_register(int action, PyObject *callback)
19 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
20 action == OBActions::Action_KeyPress) {
21 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
24 if (!PyCallable_Check(callback)) {
25 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
29 FunctionList::iterator it = std::find(callbacks[action].begin(),
30 callbacks[action].end(),
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);
39 bool python_preregister(int action, PyObject *callback)
41 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
42 action == OBActions::Action_KeyPress) {
43 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
46 if (!PyCallable_Check(callback)) {
47 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
51 FunctionList::iterator it = std::find(callbacks[action].begin(),
52 callbacks[action].end(),
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);
61 bool python_unregister(int action, PyObject *callback)
63 if (action < 0 || action >= OBActions::NUM_ACTIONS ||
64 action == OBActions::Action_KeyPress) {
65 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
68 if (!PyCallable_Check(callback)) {
69 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
73 FunctionList::iterator it = std::find(callbacks[action].begin(),
74 callbacks[action].end(),
76 if (it != callbacks[action].end()) { // its been registered before
77 Py_XDECREF(*it); // Dispose of previous callback
78 callbacks[action].erase(it);
83 bool python_unregister_all(int action)
85 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
86 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
90 while (!callbacks[action].empty()) {
91 Py_XDECREF(callbacks[action].back());
92 callbacks[action].pop_back();
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)
104 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
107 arglist = Py_BuildValue("iliillll", action, window, type, state,
109 else if (d3 != LONG_MIN)
110 arglist = Py_BuildValue("iliilll", action, window, type, state,
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);
117 arglist = Py_BuildValue("ilii", action, window, type, state);
119 FunctionList::iterator it, end = callbacks[action].end();
120 for (it = callbacks[action].begin(); it != end; ++it) {
122 result = PyEval_CallObject(*it, arglist);
126 // an exception occured in the script, display it
139 bool python_bind_key(PyObject *keylist, PyObject *callback)
141 if (!PyList_Check(keylist)) {
142 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
145 if (!PyCallable_Check(callback)) {
146 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
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.");
158 vectkeylist.push_back(PyString_AsString(str));
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);
169 PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid.");
174 bool python_unbind_key(PyObject *keylist)
176 if (!PyList_Check(keylist)) {
177 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
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.");
189 vectkeylist.push_back(PyString_AsString(str));
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.
206 bool python_bind_mouse(const std::string &button, PyObject *callback)
208 if (!PyCallable_Check(callback)) {
209 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
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);
221 PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid.");
226 bool python_unbind_mouse(const std::string &button)
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.
242 bool python_unbind_all()
244 Openbox::instance->bindings()->remove_all();
249 void python_callback_binding(int id, OBActions::ActionType action,
250 Window window, unsigned int state,
251 unsigned int keybutton, Time time)
255 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
257 if (action == OBActions::Action_KeyPress)
260 func = mousefuncs[id];
267 arglist = Py_BuildValue("iliil", action, window, state, keybutton, time);
270 result = PyEval_CallObject(func, arglist);
274 // an exception occured in the script, display it