]> Dogcows Code - chaz/openbox/blob - src/python.cc
deal with when nothing has focus anymore
[chaz/openbox] / src / python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #include "python.hh"
4
5 #include <vector>
6 #include <algorithm>
7
8 namespace ob {
9
10 typedef std::vector<PyObject*> FunctionList;
11
12 static FunctionList callbacks[OBActions::NUM_ACTIONS];
13
14 bool python_register(int action, PyObject *callback)
15 {
16 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
17 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
18 return false;
19 }
20 if (!PyCallable_Check(callback)) {
21 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
22 return false;
23 }
24
25 FunctionList::iterator it = std::find(callbacks[action].begin(),
26 callbacks[action].end(),
27 callback);
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);
31 }
32 return true;
33 }
34
35 bool python_preregister(int action, PyObject *callback)
36 {
37 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
38 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
39 return false;
40 }
41 if (!PyCallable_Check(callback)) {
42 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
43 return false;
44 }
45
46 FunctionList::iterator it = std::find(callbacks[action].begin(),
47 callbacks[action].end(),
48 callback);
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);
52 }
53 return true;
54 }
55
56 bool python_unregister(int action, PyObject *callback)
57 {
58 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
59 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
60 return false;
61 }
62 if (!PyCallable_Check(callback)) {
63 PyErr_SetString(PyExc_AssertionError, "Invalid callback function.");
64 return false;
65 }
66
67 FunctionList::iterator it = std::find(callbacks[action].begin(),
68 callbacks[action].end(),
69 callback);
70 if (it != callbacks[action].end()) { // its been registered before
71 Py_XDECREF(*it); // Dispose of previous callback
72 callbacks[action].erase(it);
73 }
74 return true;
75 }
76
77 bool python_unregister_all(int action)
78 {
79 if (action < 0 || action >= OBActions::NUM_ACTIONS) {
80 PyErr_SetString(PyExc_AssertionError, "Invalid action type.");
81 return false;
82 }
83
84 while (!callbacks[action].empty()) {
85 Py_XDECREF(callbacks[action].back());
86 callbacks[action].pop_back();
87 }
88 return true;
89 }
90
91 void python_callback(OBActions::ActionType action, Window window,
92 OBWidget::WidgetType type, unsigned int state,
93 long d1, long d2, long d3, long d4)
94 {
95 PyObject *arglist;
96 PyObject *result;
97
98 assert(action >= 0 && action < OBActions::NUM_ACTIONS);
99
100 if (d4 != LONG_MIN)
101 arglist = Py_BuildValue("iliillll", action, window, type, state,
102 d1, d2, d3, d4);
103 else if (d3 != LONG_MIN)
104 arglist = Py_BuildValue("iliilll", action, window, type, state,
105 d1, d2, d3);
106 else if (d2 != LONG_MIN)
107 arglist = Py_BuildValue("iliill", action, window, type, state, d1, d2);
108 else if (d1 != LONG_MIN)
109 arglist = Py_BuildValue("iliil", action, window, type, state, d1);
110 else
111 arglist = Py_BuildValue("ilii", action, window, type, state);
112
113 FunctionList::iterator it, end = callbacks[action].end();
114 for (it = callbacks[action].begin(); it != end; ++it) {
115 // call the callback
116 result = PyEval_CallObject(*it, arglist);
117 if (result) {
118 Py_DECREF(result);
119 } else {
120 // an exception occured in the script, display it
121 PyErr_Print();
122 }
123 }
124
125 Py_DECREF(arglist);
126 }
127
128 }
This page took 0.041423 seconds and 4 git commands to generate.