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 *func, *arglist, *pdata;
13 func = (PyObject*) pyfunc;
15 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__MouseData, 0);
16 arglist = Py_BuildValue("(O)", pdata);
20 result = PyEval_CallObject(func, arglist);
21 if (!result || PyErr_Occurred()) {
22 // an exception occured in the script, display it
31 Calls a python callback for the KeyCallback function type
33 static void PythonKeyCallback(ob::KeyData *data, void *pyfunc)
35 PyObject *func, *arglist, *pdata;
39 func = (PyObject*) pyfunc;
41 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__KeyData, 0);
42 arglist = Py_BuildValue("(O)", pdata);
46 result = PyEval_CallObject(func, arglist);
47 if (!result || PyErr_Occurred()) {
48 // an exception occured in the script, display it
57 Calls a python callback for the EventCallback function type
59 static void PythonEventCallback(ob::EventData *data, void *pyfunc)
61 PyObject *func, *arglist, *pdata;
65 func = (PyObject*) pyfunc;
67 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__EventData, 0);
68 arglist = Py_BuildValue("(O)", pdata);
72 result = PyEval_CallObject(func, arglist);
73 if (!result || PyErr_Occurred()) {
74 // an exception occured in the script, display it
83 // for all of these, PyErr_SetString is called before they return a false!
86 if (!result) return NULL;
90 if (!result) return NULL;
94 if (!result) return NULL;
98 if (!result) return NULL;
102 if (!result) return NULL;
105 // Grab a Python function object as a Python object.
106 %typemap(python,in) PyObject *func {
107 if (!PyCallable_Check($input)) {
108 PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
115 bool mbind(const std::string &button, ob::MouseContext::MC context,
116 ob::MouseAction::MA action, PyObject *func)
118 if(context < 0 || context >= ob::MouseContext::NUM_MOUSE_CONTEXT) {
119 PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
122 if(action < 0 || action >= ob::MouseAction::NUM_MOUSE_ACTION) {
123 PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
127 if (!ob::openbox->bindings()->addButton(button, context,
128 action, PythonMouseCallback, func)) {
129 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
135 bool ebind(ob::EventAction::EA action, PyObject *func)
137 if(action < 0 || action >= ob::EventAction::NUM_EVENT_ACTION) {
138 PyErr_SetString(PyExc_ValueError, "Invalid EventAction");
142 if (!ob::openbox->bindings()->addEvent(action, PythonEventCallback, func)) {
143 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
149 bool kgrab(int screen, PyObject *func)
151 if (!ob::openbox->bindings()->grabKeyboard(screen,
152 PythonKeyCallback, func)) {
153 PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
161 ob::openbox->bindings()->ungrabKeyboard();
164 bool mgrab(int screen)
166 if (!ob::openbox->bindings()->grabPointer(screen)) {
167 PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
175 ob::openbox->bindings()->ungrabPointer();
178 bool kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
180 if (!PyList_Check(keylist)) {
181 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
184 if(context < 0 || context >= ob::KeyContext::NUM_KEY_CONTEXT) {
185 PyErr_SetString(PyExc_ValueError, "Invalid KeyContext");
189 ob::Bindings::StringVect vectkeylist;
190 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
191 PyObject *str = PyList_GetItem(keylist, i);
192 if (!PyString_Check(str)) {
193 PyErr_SetString(PyExc_TypeError,
194 "Invalid keylist. It must contain only strings.");
197 vectkeylist.push_back(PyString_AsString(str));
200 (void)context; // XXX use this sometime!
201 if (!ob::openbox->bindings()->addKey(vectkeylist, PythonKeyCallback, func)) {
202 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");