]> Dogcows Code - chaz/openbox/blob - src/python.cc
keybindings underway. dont work yet
[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 bindfuncs;
15
16 bool python_register(int action, PyObject *callback)
17 {
18 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
19 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
20 return false;
21 }
22 if (!PyCallable_Check(callback)) {
23 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
24 return false;
25 }
26
27 FunctionList::iterator it = std::find(callbacks[action].begin(),
28 callbacks[action].end(),
29 callback);
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);
33 }
34 return true;
35 }
36
37 bool python_preregister(int action, PyObject *callback)
38 {
39 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
40 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
41 return false;
42 }
43 if (!PyCallable_Check(callback)) {
44 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
45 return false;
46 }
47
48 FunctionList::iterator it = std::find(callbacks[action].begin(),
49 callbacks[action].end(),
50 callback);
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);
54 }
55 return true;
56 }
57
58 bool python_unregister(int action, PyObject *callback)
59 {
60 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
61 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
62 return false;
63 }
64 if (!PyCallable_Check(callback)) {
65 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
66 return false;
67 }
68
69 FunctionList::iterator it = std::find(callbacks[action].begin(),
70 callbacks[action].end(),
71 callback);
72 if (it != callbacks[action].end()) { // its been registered before
73 Py_XDECREF(*it); // Dispose of previous callback
74 callbacks[action].erase(it);
75 }
76 return true;
77 }
78
79 bool python_unregister_all(int action)
80 {
81 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
82 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
83 return false;
84 }
85
86 while (!callbacks[action].empty()) {
87 Py_XDECREF(callbacks[action].back());
88 callbacks[action].pop_back();
89 }
90 return true;
91 }
92
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)
96 {
97 PyObject *arglist;
98 PyObject *result;
99
100 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
101
102 if (d4 != LONG_MIN)
103 arglist = Py_BuildValue("iliillll", action, window, type, state,
104 d1, d2, d3, d4);
105 else if (d3 != LONG_MIN)
106 arglist = Py_BuildValue("iliilll", action, window, type, state,
107 d1, d2, d3);
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);
112 else
113 arglist = Py_BuildValue("ilii", action, window, type, state);
114
115 FunctionList::iterator it, end = callbacks[action].end();
116 for (it = callbacks[action].begin(); it != end; ++it) {
117 // call the callback
118 result = PyEval_CallObject(*it, arglist);
119 if (result) {
120 Py_DECREF(result);
121 } else {
122 // an exception occured in the script, display it
123 PyErr_Print();
124 }
125 }
126
127 Py_DECREF(arglist);
128 }
129
130
131
132
133
134
135 bool python_bind(PyObject *keylist, PyObject *callback)
136 {
137 if (!PyList_Check(keylist)) {
138 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
139 return false;
140 }
141 if (!PyCallable_Check(callback)) {
142 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
143 return false;
144 }
145
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.");
152 return false;
153 }
154 vectkeylist.push_back(PyString_AsString(str));
155 }
156
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);
163 return true;
164 } else {
165 PyErr_SetString(PyExc_AssertionError,"Unable to create binding. Invalid.");
166 return false;
167 }
168 }
169
170 bool python_unbind(PyObject *keylist)
171 {
172 if (!PyList_Check(keylist)) {
173 PyErr_SetString(PyExc_AssertionError, "Invalid keylist. Not a list.");
174 return false;
175 }
176
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.");
183 return false;
184 }
185 vectkeylist.push_back(PyString_AsString(str));
186 }
187
188 int id;
189 if ((id =
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.
195 bindfuncs[id] = 0;
196 return true;
197 }
198
199 return false;
200 }
201
202 bool python_unbind_all()
203 {
204 Openbox::instance->bindings()->remove_all();
205 return true;
206 }
207
208
209 }
This page took 0.049102 seconds and 4 git commands to generate.