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 bindfuncs;
16 bool python_register(int action, PyObject *callback)
18 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
19 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
22 if (!PyCallable_Check(callback)) {
23 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
27 FunctionList::iterator it = std::find(callbacks[action].begin(),
28 callbacks[action].end(),
30 if (it == callbacks[action].end()) { // not already in there
31 Py_XINCREF(callback); // Add a reference to new callback
32 callbacks[action].push_back(callback);
37 bool python_preregister(int action, PyObject *callback)
39 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
40 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
43 if (!PyCallable_Check(callback)) {
44 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
48 FunctionList::iterator it = std::find(callbacks[action].begin(),
49 callbacks[action].end(),
51 if (it == callbacks[action].end()) { // not already in there
52 Py_XINCREF(callback); // Add a reference to new callback
53 callbacks[action].insert(callbacks[action].begin(), callback);
58 bool python_unregister(int action, PyObject *callback)
60 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
61 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
64 if (!PyCallable_Check(callback)) {
65 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
69 FunctionList::iterator it = std::find(callbacks[action].begin(),
70 callbacks[action].end(),
72 if (it != callbacks[action].end()) { // its been registered before
73 Py_XDECREF(*it); // Dispose of previous callback
74 callbacks[action].erase(it);
79 bool python_unregister_all(int action)
81 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
82 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
86 while (!callbacks[action].empty()) {
87 Py_XDECREF(callbacks[action].back());
88 callbacks[action].pop_back();
93 void python_callback(OBActions::ActionType action, Window window,
94 OBWidget::WidgetType type, unsigned int state,
95 long d1, long d2, long d3, long d4)
100 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
103 arglist = Py_BuildValue("iliillll", action, window, type, state,
105 else if (d3 != LONG_MIN)
106 arglist = Py_BuildValue("iliilll", action, window, type, state,
108 else if (d2 != LONG_MIN)
109 arglist = Py_BuildValue("iliill", action, window, type, state, d1, d2);
110 else if (d1 != LONG_MIN)
111 arglist = Py_BuildValue("iliil", action, window, type, state, d1);
113 arglist = Py_BuildValue("ilii", action, window, type, state);
115 FunctionList::iterator it, end = callbacks[action].end();
116 for (it = callbacks[action].begin(); it != end; ++it) {
118 result = PyEval_CallObject(*it, arglist);
122 // an exception occured in the script, display it
135 bool python_bind(PyObject *keylist, PyObject *callback)
137 if (!PyList_Check(keylist)) {
138 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
141 if (!PyCallable_Check(callback)) {
142 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
146 OBBindings::StringVect vectkeylist;
147 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
148 PyObject *str = PyList_GetItem(keylist, i);
149 if (!PyString_Check(str)) {
150 PyErr_SetString(PyExc_AssertionError,
151 "Invalid keylist. It must contain only strings.");
154 vectkeylist.push_back(PyString_AsString(str));
157 // the id is what the binding class can call back with so it doesnt have to
158 // worry about the python function pointer
159 int id = bindfuncs.size();
160 if (Openbox::instance->bindings()->add(vectkeylist, id)) {
161 Py_XINCREF(callback); // Add a reference to new callback
162 bindfuncs.push_back(callback);
165 PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid.");
170 bool python_unbind(PyObject *keylist)
172 if (!PyList_Check(keylist)) {
173 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
177 OBBindings::StringVect vectkeylist;
178 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
179 PyObject *str = PyList_GetItem(keylist, i);
180 if (!PyString_Check(str)) {
181 PyErr_SetString(PyExc_AssertionError,
182 "Invalid keylist. It must contain only strings.");
185 vectkeylist.push_back(PyString_AsString(str));
190 Openbox::instance->bindings()->remove(vectkeylist)) >= 0) {
191 assert(bindfuncs[id]); // shouldn't be able to remove it twice
192 Py_XDECREF(bindfuncs[id]); // Dispose of previous callback
193 // important note: we don't erase the item from the list cuz that would
194 // ruin all the id's that are in use. simply nullify it.
202 bool python_unbind_all()
204 Openbox::instance->bindings()->remove_all();