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