]>
Dogcows Code - chaz/openbox/blob - src/python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
11 typedef std::vector
<PyObject
*> FunctionList
;
13 static FunctionList callbacks
[OBActions::NUM_ACTIONS
];
14 static FunctionList keyfuncs
;
15 static FunctionList mousefuncs
;
17 bool python_register(int action
, PyObject
*callback
)
19 if (action
< 0 || action
>= OBActions::NUM_ACTIONS
) {
20 PyErr_SetString(PyExc_AssertionError
, "Invalid action type.");
23 if (!PyCallable_Check(callback
)) {
24 PyErr_SetString(PyExc_AssertionError
, "Invalid callback function.");
28 FunctionList::iterator it
= std::find(callbacks
[action
].begin(),
29 callbacks
[action
].end(),
31 if (it
== callbacks
[action
].end()) { // not already in there
32 Py_XINCREF(callback
); // Add a reference to new callback
33 callbacks
[action
].push_back(callback
);
38 bool python_preregister(int action
, PyObject
*callback
)
40 if (action
< 0 || action
>= OBActions::NUM_ACTIONS
) {
41 PyErr_SetString(PyExc_AssertionError
, "Invalid action type.");
44 if (!PyCallable_Check(callback
)) {
45 PyErr_SetString(PyExc_AssertionError
, "Invalid callback function.");
49 FunctionList::iterator it
= std::find(callbacks
[action
].begin(),
50 callbacks
[action
].end(),
52 if (it
== callbacks
[action
].end()) { // not already in there
53 Py_XINCREF(callback
); // Add a reference to new callback
54 callbacks
[action
].insert(callbacks
[action
].begin(), callback
);
59 bool python_unregister(int action
, PyObject
*callback
)
61 if (action
< 0 || action
>= OBActions::NUM_ACTIONS
) {
62 PyErr_SetString(PyExc_AssertionError
, "Invalid action type.");
65 if (!PyCallable_Check(callback
)) {
66 PyErr_SetString(PyExc_AssertionError
, "Invalid callback function.");
70 FunctionList::iterator it
= std::find(callbacks
[action
].begin(),
71 callbacks
[action
].end(),
73 if (it
!= callbacks
[action
].end()) { // its been registered before
74 Py_XDECREF(*it
); // Dispose of previous callback
75 callbacks
[action
].erase(it
);
80 bool python_unregister_all(int action
)
82 if (action
< 0 || action
>= OBActions::NUM_ACTIONS
) {
83 PyErr_SetString(PyExc_AssertionError
, "Invalid action type.");
87 while (!callbacks
[action
].empty()) {
88 Py_XDECREF(callbacks
[action
].back());
89 callbacks
[action
].pop_back();
94 void python_callback(OBActions::ActionType action
, Window window
,
95 OBWidget::WidgetType type
, unsigned int state
,
96 long d1
, long d2
, long d3
, long d4
)
101 assert(action
>= 0 && action
< OBActions::NUM_ACTIONS
);
104 arglist
= Py_BuildValue("iliillll", action
, window
, type
, state
,
106 else if (d3
!= LONG_MIN
)
107 arglist
= Py_BuildValue("iliilll", action
, window
, type
, state
,
109 else if (d2
!= LONG_MIN
)
110 arglist
= Py_BuildValue("iliill", action
, window
, type
, state
, d1
, d2
);
111 else if (d1
!= LONG_MIN
)
112 arglist
= Py_BuildValue("iliil", action
, window
, type
, state
, d1
);
114 arglist
= Py_BuildValue("ilii", action
, window
, type
, state
);
116 FunctionList::iterator it
, end
= callbacks
[action
].end();
117 for (it
= callbacks
[action
].begin(); it
!= end
; ++it
) {
119 result
= PyEval_CallObject(*it
, arglist
);
123 // an exception occured in the script, display it
136 bool python_bind_key(PyObject
*keylist
, PyObject
*callback
)
138 if (!PyList_Check(keylist
)) {
139 PyErr_SetString(PyExc_AssertionError
, "Invalid keylist. Not a list.");
142 if (!PyCallable_Check(callback
)) {
143 PyErr_SetString(PyExc_AssertionError
, "Invalid callback function.");
147 OBBindings::StringVect vectkeylist
;
148 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
149 PyObject
*str
= PyList_GetItem(keylist
, i
);
150 if (!PyString_Check(str
)) {
151 PyErr_SetString(PyExc_AssertionError
,
152 "Invalid keylist. It must contain only strings.");
155 vectkeylist
.push_back(PyString_AsString(str
));
158 // the id is what the binding class can call back with so it doesnt have to
159 // worry about the python function pointer
160 int id
= keyfuncs
.size();
161 if (Openbox::instance
->bindings()->add_key(vectkeylist
, id
)) {
162 Py_XINCREF(callback
); // Add a reference to new callback
163 keyfuncs
.push_back(callback
);
166 PyErr_SetString(PyExc_AssertionError
,"Unable to create binding. Invalid.");
171 bool python_unbind_key(PyObject
*keylist
)
173 if (!PyList_Check(keylist
)) {
174 PyErr_SetString(PyExc_AssertionError
, "Invalid keylist. Not a list.");
178 OBBindings::StringVect vectkeylist
;
179 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
180 PyObject
*str
= PyList_GetItem(keylist
, i
);
181 if (!PyString_Check(str
)) {
182 PyErr_SetString(PyExc_AssertionError
,
183 "Invalid keylist. It must contain only strings.");
186 vectkeylist
.push_back(PyString_AsString(str
));
191 Openbox::instance
->bindings()->remove_key(vectkeylist
)) >= 0) {
192 assert(keyfuncs
[id
]); // shouldn't be able to remove it twice
193 Py_XDECREF(keyfuncs
[id
]); // Dispose of previous callback
194 // important note: we don't erase the item from the list cuz that would
195 // ruin all the id's that are in use. simply nullify it.
203 bool python_bind_mouse(const std::string
&button
, PyObject
*callback
)
205 if (!PyCallable_Check(callback
)) {
206 PyErr_SetString(PyExc_AssertionError
, "Invalid callback function.");
210 // the id is what the binding class can call back with so it doesnt have to
211 // worry about the python function pointer
212 int id
= mousefuncs
.size();
213 if (Openbox::instance
->bindings()->add_mouse(button
, id
)) {
214 Py_XINCREF(callback
); // Add a reference to new callback
215 mousefuncs
.push_back(callback
);
218 PyErr_SetString(PyExc_AssertionError
,"Unable to create binding. Invalid.");
223 bool python_unbind_mouse(const std::string
&button
)
227 Openbox::instance
->bindings()->remove_mouse(button
)) >= 0) {
228 assert(mousefuncs
[id
]); // shouldn't be able to remove it twice
229 Py_XDECREF(mousefuncs
[id
]); // Dispose of previous callback
230 // important note: we don't erase the item from the list cuz that would
231 // ruin all the id's that are in use. simply nullify it.
239 bool python_unbind_all()
241 Openbox::instance
->bindings()->remove_all();
This page took 0.051984 seconds and 5 git commands to generate.