1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 Calls a python callback for the MouseCallback function type
7 static void PythonMouseCallback(ob::MouseData *data, void *pyfunc)
9 PyObject *arglist, *pdata, *result;
11 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__MouseData, 0);
12 arglist = Py_BuildValue("(O)", pdata);
16 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
17 if (!result || PyErr_Occurred()) {
18 // an exception occured in the script, display it
27 Calls a python callback for the KeyCallback function type
29 static void PythonKeyCallback(ob::KeyData *data, void *pyfunc)
31 PyObject *arglist, *pdata, *result;
33 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__KeyData, 0);
34 arglist = Py_BuildValue("(O)", pdata);
38 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
39 if (!result || PyErr_Occurred()) {
40 // an exception occured in the script, display it
49 Calls a python callback for the EventCallback function type
51 static void PythonEventCallback(ob::EventData *data, void *pyfunc)
53 PyObject *arglist, *pdata, *result;
55 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__EventData, 0);
56 arglist = Py_BuildValue("(O)", pdata);
60 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
61 if (!result || PyErr_Occurred()) {
62 // an exception occured in the script, display it
71 // for all of these, PyErr_SetString is called before they return a false!
74 if (!result) return NULL;
78 if (!result) return NULL;
82 if (!result) return NULL;
86 if (!result) return NULL;
90 if (!result) return NULL;
93 // Grab a Python function object as a Python object.
94 %typemap(python,in) PyObject *func {
95 if (!PyCallable_Check($input)) {
96 PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
103 bool mbind(const std::string &button, ob::MouseContext::MC context,
104 ob::MouseAction::MA action, PyObject *func)
106 if(context < 0 || context >= ob::MouseContext::NUM_MOUSE_CONTEXT) {
107 PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
110 if(action < 0 || action >= ob::MouseAction::NUM_MOUSE_ACTION) {
111 PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
115 if (!ob::openbox->bindings()->addButton(button, context,
116 action, PythonMouseCallback, func)) {
117 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
124 bool ebind(ob::EventAction::EA action, PyObject *func)
126 if(action < 0 || action >= ob::EventAction::NUM_EVENT_ACTION) {
127 PyErr_SetString(PyExc_ValueError, "Invalid EventAction");
131 if (!ob::openbox->bindings()->addEvent(action, PythonEventCallback, func)) {
132 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
139 bool kgrab(int screen, PyObject *func)
141 if (!ob::openbox->bindings()->grabKeyboard(screen,
142 PythonKeyCallback, func)) {
143 PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
152 ob::openbox->bindings()->ungrabKeyboard();
155 bool mgrab(int screen)
157 if (!ob::openbox->bindings()->grabPointer(screen)) {
158 PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
166 ob::openbox->bindings()->ungrabPointer();
169 bool kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
171 if (!PyList_Check(keylist)) {
172 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
175 if(context < 0 || context >= ob::KeyContext::NUM_KEY_CONTEXT) {
176 PyErr_SetString(PyExc_ValueError, "Invalid KeyContext");
180 ob::Bindings::StringVect vectkeylist;
181 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
182 PyObject *str = PyList_GetItem(keylist, i);
183 if (!PyString_Check(str)) {
184 PyErr_SetString(PyExc_TypeError,
185 "Invalid keylist. It must contain only strings.");
188 vectkeylist.push_back(PyString_AsString(str));
191 (void)context; // XXX use this sometime!
192 if (!ob::openbox->bindings()->addKey(vectkeylist, PythonKeyCallback, func)) {
193 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");