]> icculus.org git repositories - dana/openbox.git/blob - src/python.cc
add click_raise global var
[dana/openbox.git] / 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(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(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(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(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 void python_unbind_all()
212 {
213   Openbox::instance->bindings()->remove_all();
214 }
215
216
217 void python_callback_binding(int id, Window window, unsigned int state,
218                              unsigned int keybutton, Time time)
219 {
220   if (!bindfuncs[id]) return; // the key was unbound
221
222   PyObject *arglist;
223   PyObject *result;
224
225   arglist = Py_BuildValue("lisl", window, state,
226                           XKeysymToString(
227                             XKeycodeToKeysym(otk::OBDisplay::display,
228                                              keybutton, 0)),
229                           time);
230
231   // call the callback
232   result = PyEval_CallObject(bindfuncs[id], arglist);
233   if (result) {
234     Py_DECREF(result);
235   } else {
236     // an exception occured in the script, display it
237     PyErr_Print();
238   }
239
240   Py_DECREF(arglist);
241 }
242
243 }