1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
5 %include "std_string.i"
9 Calls a python callback for the MouseCallback function type
11 static void PythonMouseCallback(ob::MouseData *data, void *pyfunc)
13 PyObject *arglist, *pdata, *result;
15 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__MouseData, 0);
16 arglist = Py_BuildValue("(O)", pdata);
20 result = PyEval_CallObject((PyObject*)pyfunc, 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 *arglist, *pdata, *result;
37 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__KeyData, 0);
38 arglist = Py_BuildValue("(O)", pdata);
42 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
43 if (!result || PyErr_Occurred()) {
44 // an exception occured in the script, display it
53 Calls a python callback for the EventCallback function type
55 static void PythonEventCallback(ob::EventData *data, void *pyfunc)
57 PyObject *arglist, *pdata, *result;
59 pdata = SWIG_NewPointerObj((void *) data, SWIGTYPE_p_ob__EventData, 0);
60 arglist = Py_BuildValue("(O)", pdata);
64 result = PyEval_CallObject((PyObject*)pyfunc, arglist);
65 if (!result || PyErr_Occurred()) {
66 // an exception occured in the script, display it
75 // for all of these, PyErr_SetString is called before they return a false!
78 if (!result) return NULL;
82 if (!result) return NULL;
86 if (!result) return NULL;
90 if (!result) return NULL;
94 if (!result) return NULL;
97 // Grab a Python function object as a Python object.
98 %typemap(python,in) PyObject *func {
99 if (!PyCallable_Check($input)) {
100 PyErr_SetString(PyExc_TypeError, "Excepting a callable object.");
107 #include "bindings.hh"
109 bool mbind(const std::string &button, ob::MouseContext::MC context,
110 ob::MouseAction::MA action, PyObject *func)
112 if(context < 0 || context >= ob::MouseContext::NUM_MOUSE_CONTEXT) {
113 PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
116 if(action < 0 || action >= ob::MouseAction::NUM_MOUSE_ACTION) {
117 PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
121 if (!ob::openbox->bindings()->addButton(button, context,
122 action, PythonMouseCallback, func)) {
123 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
126 Py_INCREF(func); // the func is never decreffed... XXX
130 bool ebind(ob::EventAction::EA action, PyObject *func)
132 if(action < 0 || action >= ob::EventAction::NUM_EVENT_ACTION) {
133 PyErr_SetString(PyExc_ValueError, "Invalid EventAction");
137 if (!ob::openbox->bindings()->addEvent(action, PythonEventCallback, func)) {
138 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
141 Py_INCREF(func); // the func is never decreffed... XXX
145 bool kgrab(int screen, PyObject *func)
147 if (!ob::openbox->bindings()->grabKeyboard(screen,
148 PythonKeyCallback, func)) {
149 PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
152 Py_INCREF(func); // the func is never decreffed... XXX
158 ob::openbox->bindings()->ungrabKeyboard();
161 bool mgrab(int screen)
163 if (!ob::openbox->bindings()->grabPointer(screen)) {
164 PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
172 ob::openbox->bindings()->ungrabPointer();
175 bool kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
177 if (!PyList_Check(keylist)) {
178 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
181 if(context < 0 || context >= ob::KeyContext::NUM_KEY_CONTEXT) {
182 PyErr_SetString(PyExc_ValueError, "Invalid KeyContext");
186 ob::Bindings::StringVect vectkeylist;
187 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
188 PyObject *str = PyList_GetItem(keylist, i);
189 if (!PyString_Check(str)) {
190 PyErr_SetString(PyExc_TypeError,
191 "Invalid keylist. It must contain only strings.");
194 vectkeylist.push_back(PyString_AsString(str));
197 (void)context; // XXX use this sometime!
198 if (!ob::openbox->bindings()->addKey(vectkeylist, PythonKeyCallback, func)) {
199 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
202 Py_INCREF(func); // the func is never decreffed... XXX