]>
Dogcows Code - chaz/openbox/blob - src/python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
8 #include "otk/display.hh"
12 // The initializer in openbox_wrap.cc
13 extern void init_openbox(void);
18 static PyObject
*obdict
= NULL
;
20 void python_init(char *argv0
)
22 // start the python engine
23 Py_SetProgramName(argv0
);
25 // initialize the C python module
27 // include the openbox directories for python scripts in the sys path
28 PyRun_SimpleString("import sys");
29 PyRun_SimpleString("sys.path.append('" SCRIPTDIR
"')");
30 PyRun_SimpleString(const_cast<char*>(("sys.path.append('" +
31 otk::expandTilde("~/.openbox/python") +
33 // import the otk and openbox modules into the main namespace
34 PyRun_SimpleString("from openbox import *;");
35 // set up convenience global variables
36 PyRun_SimpleString("openbox = Openbox_instance()");
37 PyRun_SimpleString("display = Display_instance()");
39 // set up access to the python global variables
40 PyObject
*obmodule
= PyImport_AddModule("__main__");
41 obdict
= PyModule_GetDict(obmodule
);
49 bool python_exec(const std::string
&path
)
51 FILE *rcpyfd
= fopen(path
.c_str(), "r");
53 printf("failed to load python file %s\n", path
.c_str());
56 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
61 bool python_get_long(const char *name
, long *value
)
63 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
64 if (!(val
&& PyInt_Check(val
))) return false;
66 *value
= PyInt_AsLong(val
);
70 bool python_get_string(const char *name
, otk::ustring
*value
)
72 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
73 if (!(val
&& PyString_Check(val
))) return false;
75 *value
= PyString_AsString(val
);
79 bool python_get_stringlist(const char *name
, std::vector
<otk::ustring
> *value
)
81 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
82 if (!(val
&& PyList_Check(val
))) return false;
84 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
85 PyObject
*str
= PyList_GetItem(val
, i
);
86 if (PyString_Check(str
))
87 value
->push_back(PyString_AsString(str
));
92 // ************************************* //
93 // Stuff for calling from Python scripts //
94 // ************************************* //
96 PyObject
*mbind(const std::string
&button
, ob::MouseContext context
,
97 ob::MouseAction action
, PyObject
*func
)
99 if (!PyCallable_Check(func
)) {
100 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
104 if (!ob::openbox
->bindings()->addButton(button
, context
,
106 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
109 Py_INCREF(Py_None
); return Py_None
;
112 PyObject
*ebind(ob::EventAction action
, PyObject
*func
)
114 if (!PyCallable_Check(func
)) {
115 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
119 if (!ob::openbox
->bindings()->addEvent(action
, func
)) {
120 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
123 Py_INCREF(Py_None
); return Py_None
;
126 PyObject
*kgrab(PyObject
*func
)
128 if (!PyCallable_Check(func
)) {
129 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
133 if (!ob::openbox
->bindings()->grabKeyboard(func
)) {
134 PyErr_SetString(PyExc_RuntimeError
,"Unable to grab keybaord.");
137 Py_INCREF(Py_None
); return Py_None
;
142 ob::openbox
->bindings()->ungrabKeyboard();
143 Py_INCREF(Py_None
); return Py_None
;
146 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext context
, PyObject
*func
)
148 if (!PyCallable_Check(func
)) {
149 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
152 if (!PyList_Check(keylist
)) {
153 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
157 ob::Bindings::StringVect vectkeylist
;
158 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
159 PyObject
*str
= PyList_GetItem(keylist
, i
);
160 if (!PyString_Check(str
)) {
161 PyErr_SetString(PyExc_TypeError
,
162 "Invalid keylist. It must contain only strings.");
165 vectkeylist
.push_back(PyString_AsString(str
));
168 (void)context
; // XXX use this sometime!
169 if (!ob::openbox
->bindings()->addKey(vectkeylist
, func
)) {
170 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
173 Py_INCREF(Py_None
); return Py_None
;
176 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
178 if (!PyList_Check(keylist
)) {
179 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
182 if (!PyCallable_Check(func
)) {
183 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
187 ob::Bindings::StringVect vectkeylist
;
188 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
189 PyObject
*str
= PyList_GetItem(keylist
, i
);
190 if (!PyString_Check(str
)) {
191 PyErr_SetString(PyExc_TypeError
,
192 "Invalid keylist. It must contain only strings.");
195 vectkeylist
.push_back(PyString_AsString(str
));
198 if (!ob::openbox
->bindings()->removeKey(vectkeylist
, func
)) {
199 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
202 Py_INCREF(Py_None
); return Py_None
;
207 ob::openbox
->bindings()->removeAllKeys();
210 void set_reset_key(const std::string
&key
)
212 ob::openbox
->bindings()->setResetKey(key
);
215 PyObject
*send_client_msg(Window target
, Atom type
, Window about
,
216 long data
, long data1
, long data2
,
217 long data3
, long data4
)
220 e
.xclient
.type
= ClientMessage
;
221 e
.xclient
.format
= 32;
222 e
.xclient
.message_type
= type
;
223 e
.xclient
.window
= about
;
224 e
.xclient
.data
.l
[0] = data
;
225 e
.xclient
.data
.l
[1] = data1
;
226 e
.xclient
.data
.l
[2] = data2
;
227 e
.xclient
.data
.l
[3] = data3
;
228 e
.xclient
.data
.l
[4] = data4
;
230 XSendEvent(**otk::display
, target
, false,
231 SubstructureRedirectMask
| SubstructureNotifyMask
,
233 Py_INCREF(Py_None
); return Py_None
;
This page took 0.046375 seconds and 5 git commands to generate.