]> Dogcows Code - chaz/openbox/blob - wrap/callback.i
only return as many as was requested
[chaz/openbox] / wrap / callback.i
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 %include "std_string.i"
4
5 %{
6 /*
7 Calls a python callback for the MouseCallback function type
8 */
9 static void PythonMouseCallback(ob::MouseData *data, void *pyfunc)
10 {
11 PyObject *arglist, *pdata, *result;
12
13 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__MouseData, 0);
14 arglist = Py_BuildValue("(O)", pdata);
15 Py_DECREF(pdata);
16
17 // call the callback
18 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
19 if (!result || PyErr_Occurred()) {
20 // an exception occured in the script, display it
21 PyErr_Print();
22 }
23
24 Py_XDECREF(result);
25 Py_DECREF(arglist);
26 }
27
28 /*
29 Calls a python callback for the KeyCallback function type
30 */
31 static void PythonKeyCallback(ob::KeyData *data, void *pyfunc)
32 {
33 PyObject *arglist, *pdata, *result;
34
35 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__KeyData, 0);
36 arglist = Py_BuildValue("(O)", pdata);
37 Py_DECREF(pdata);
38
39 // call the callback
40 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
41 if (!result || PyErr_Occurred()) {
42 // an exception occured in the script, display it
43 PyErr_Print();
44 }
45
46 Py_XDECREF(result);
47 Py_DECREF(arglist);
48 }
49
50 /*
51 Calls a python callback for the EventCallback function type
52 */
53 static void PythonEventCallback(ob::EventData *data, void *pyfunc)
54 {
55 PyObject *arglist, *pdata, *result;
56
57 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__EventData, 0);
58 arglist = Py_BuildValue("(O)", pdata);
59 Py_DECREF(pdata);
60
61 // call the callback
62 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
63 if (!result || PyErr_Occurred()) {
64 // an exception occured in the script, display it
65 PyErr_Print();
66 }
67
68 Py_XDECREF(result);
69 Py_DECREF(arglist);
70 }
71 %}
72
73 // for all of these, PyErr_SetString is called before they return a false!
74 %exception mbind {
75 $action
76 if (!result) return NULL;
77 }
78 %exception kbind {
79 $action
80 if (!result) return NULL;
81 }
82 %exception ebind {
83 $action
84 if (!result) return NULL;
85 }
86 %exception kgrab {
87 $action
88 if (!result) return NULL;
89 }
90 %exception mgrab {
91 $action
92 if (!result) return NULL;
93 }
94
95 // Grab a Python function object as a Python object.
96 %typemap(python,in) PyObject *func {
97 if (!PyCallable_Check($input)) {
98 PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
99 return NULL;
100 }
101 $1 = $input;
102 }
103
104 %inline %{
105 #include "bindings.hh"
106
107 bool mbind(const std::string &button, ob::MouseContext::MC context,
108 ob::MouseAction::MA action, PyObject *func)
109 {
110 if(context < 0 || context >= ob::MouseContext::NUM_MOUSE_CONTEXT) {
111 PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
112 return false;
113 }
114 if(action < 0 || action >= ob::MouseAction::NUM_MOUSE_ACTION) {
115 PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
116 return false;
117 }
118
119 if (!ob::openbox->bindings()->addButton(button, context,
120 action, PythonMouseCallback, func)) {
121 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
122 return false;
123 }
124 Py_INCREF(func);
125 return true;
126 }
127
128 bool ebind(ob::EventAction::EA action, PyObject *func)
129 {
130 if(action < 0 || action >= ob::EventAction::NUM_EVENT_ACTION) {
131 PyErr_SetString(PyExc_ValueError, "Invalid EventAction");
132 return false;
133 }
134
135 if (!ob::openbox->bindings()->addEvent(action, PythonEventCallback, func)) {
136 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
137 return false;
138 }
139 Py_INCREF(func);
140 return true;
141 }
142
143 bool kgrab(int screen, PyObject *func)
144 {
145 if (!ob::openbox->bindings()->grabKeyboard(screen,
146 PythonKeyCallback, func)) {
147 PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
148 return false;
149 }
150 Py_INCREF(func);
151 return true;
152 }
153
154 void kungrab()
155 {
156 ob::openbox->bindings()->ungrabKeyboard();
157 }
158
159 bool mgrab(int screen)
160 {
161 if (!ob::openbox->bindings()->grabPointer(screen)) {
162 PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
163 return false;
164 }
165 return true;
166 }
167
168 void mungrab()
169 {
170 ob::openbox->bindings()->ungrabPointer();
171 }
172
173 bool kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
174 {
175 if (!PyList_Check(keylist)) {
176 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
177 return false;
178 }
179 if(context < 0 || context >= ob::KeyContext::NUM_KEY_CONTEXT) {
180 PyErr_SetString(PyExc_ValueError, "Invalid KeyContext");
181 return false;
182 }
183
184 ob::Bindings::StringVect vectkeylist;
185 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
186 PyObject *str = PyList_GetItem(keylist, i);
187 if (!PyString_Check(str)) {
188 PyErr_SetString(PyExc_TypeError,
189 "Invalid keylist. It must contain only strings.");
190 return false;
191 }
192 vectkeylist.push_back(PyString_AsString(str));
193 }
194
195 (void)context; // XXX use this sometime!
196 if (!ob::openbox->bindings()->addKey(vectkeylist, PythonKeyCallback, func)) {
197 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
198 return false;
199 }
200 Py_INCREF(func);
201 return true;
202 }
203
204 %};
This page took 0.04147 seconds and 4 git commands to generate.