1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
3 %include "std_string.i"
7 Calls a python callback for the MouseCallback function type
9 static void PythonMouseCallback(ob::MouseData *data, void *pyfunc)
11 PyObject *arglist, *pdata, *result;
13 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__MouseData, 0);
14 arglist = Py_BuildValue("(O)", pdata);
18 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
19 if (!result || PyErr_Occurred()) {
20 // an exception occured in the script, display it
29 Calls a python callback for the KeyCallback function type
31 static void PythonKeyCallback(ob::KeyData *data, void *pyfunc)
33 PyObject *arglist, *pdata, *result;
35 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__KeyData, 0);
36 arglist = Py_BuildValue("(O)", pdata);
40 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
41 if (!result || PyErr_Occurred()) {
42 // an exception occured in the script, display it
51 Calls a python callback for the EventCallback function type
53 static void PythonEventCallback(ob::EventData *data, void *pyfunc)
55 PyObject *arglist, *pdata, *result;
57 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__EventData, 0);
58 arglist = Py_BuildValue("(O)", pdata);
62 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
63 if (!result || PyErr_Occurred()) {
64 // an exception occured in the script, display it
73 // for all of these, PyErr_SetString is called before they return a false!
76 if (!result) return NULL;
80 if (!result) return NULL;
84 if (!result) return NULL;
88 if (!result) return NULL;
92 if (!result) return NULL;
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.");
105 #include "bindings.hh"
107 bool mbind(const std::string &button, ob::MouseContext::MC context,
108 ob::MouseAction::MA action, PyObject *func)
110 if(context < 0 || context >= ob::MouseContext::NUM_MOUSE_CONTEXT) {
111 PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
114 if(action < 0 || action >= ob::MouseAction::NUM_MOUSE_ACTION) {
115 PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
119 if (!ob::openbox->bindings()->addButton(button, context,
120 action, PythonMouseCallback, func)) {
121 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
128 bool ebind(ob::EventAction::EA action, PyObject *func)
130 if(action < 0 || action >= ob::EventAction::NUM_EVENT_ACTION) {
131 PyErr_SetString(PyExc_ValueError, "Invalid EventAction");
135 if (!ob::openbox->bindings()->addEvent(action, PythonEventCallback, func)) {
136 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
143 bool kgrab(int screen, PyObject *func)
145 if (!ob::openbox->bindings()->grabKeyboard(screen,
146 PythonKeyCallback, func)) {
147 PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
156 ob::openbox->bindings()->ungrabKeyboard();
159 bool mgrab(int screen)
161 if (!ob::openbox->bindings()->grabPointer(screen)) {
162 PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
170 ob::openbox->bindings()->ungrabPointer();
173 bool kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
175 if (!PyList_Check(keylist)) {
176 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
179 if(context < 0 || context >= ob::KeyContext::NUM_KEY_CONTEXT) {
180 PyErr_SetString(PyExc_ValueError, "Invalid KeyContext");
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.");
192 vectkeylist.push_back(PyString_AsString(str));
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.");