1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
10 typedef std::vector<PyObject*> FunctionList;
12 static FunctionList callbacks[OBActions::NUM_ACTIONS];
14 bool python_register(int action, PyObject *callback)
16 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
17 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
20 if (!PyCallable_Check(callback)) {
21 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
25 FunctionList::iterator it = std::find(callbacks[action].begin(),
26 callbacks[action].end(),
28 if (it == callbacks[action].end()) { // not already in there
29 Py_XINCREF(callback); // Add a reference to new callback
30 callbacks[action].push_back(callback);
35 bool python_preregister(int action, PyObject *callback)
37 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
38 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
41 if (!PyCallable_Check(callback)) {
42 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
46 FunctionList::iterator it = std::find(callbacks[action].begin(),
47 callbacks[action].end(),
49 if (it == callbacks[action].end()) { // not already in there
50 Py_XINCREF(callback); // Add a reference to new callback
51 callbacks[action].insert(callbacks[action].begin(), callback);
56 bool python_unregister(int action, PyObject *callback)
58 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
59 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
62 if (!PyCallable_Check(callback)) {
63 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
67 FunctionList::iterator it = std::find(callbacks[action].begin(),
68 callbacks[action].end(),
70 if (it != callbacks[action].end()) { // its been registered before
71 Py_XDECREF(*it); // Dispose of previous callback
72 callbacks[action].erase(it);
77 void python_callback(OBActions::ActionType action, Window window,
78 OBWidget::WidgetType type, unsigned int state,
79 long d1, long d2, long d3, long d4)
84 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
87 arglist = Py_BuildValue("iliillll", action, window, type, state,
89 else if (d3 != LONG_MIN)
90 arglist = Py_BuildValue("iliilll", action, window, type, state,
92 else if (d2 != LONG_MIN)
93 arglist = Py_BuildValue("iliill", action, window, type, state, d1, d2);
94 else if (d1 != LONG_MIN)
95 arglist = Py_BuildValue("iliil", action, window, type, state, d1);
97 arglist = Py_BuildValue("ilii", action, window, type, state);
99 FunctionList::iterator it, end = callbacks[action].end();
100 for (it = callbacks[action].begin(); it != end; ++it) {
102 result = PyEval_CallObject(*it, arglist);
106 // an exception occured in the script, display it