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
*kbind(PyObject
*keylist
, ob::KeyContext context
, PyObject
*func
)
128 if (!PyCallable_Check(func
)) {
129 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
132 if (!PyList_Check(keylist
)) {
133 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
137 ob::Bindings::StringVect vectkeylist
;
138 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
139 PyObject
*str
= PyList_GetItem(keylist
, i
);
140 if (!PyString_Check(str
)) {
141 PyErr_SetString(PyExc_TypeError
,
142 "Invalid keylist. It must contain only strings.");
145 vectkeylist
.push_back(PyString_AsString(str
));
148 (void)context
; // XXX use this sometime!
149 if (!ob::openbox
->bindings()->addKey(vectkeylist
, func
)) {
150 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
153 Py_INCREF(Py_None
); return Py_None
;
156 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
158 if (!PyList_Check(keylist
)) {
159 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
162 if (!PyCallable_Check(func
)) {
163 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
167 ob::Bindings::StringVect vectkeylist
;
168 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
169 PyObject
*str
= PyList_GetItem(keylist
, i
);
170 if (!PyString_Check(str
)) {
171 PyErr_SetString(PyExc_TypeError
,
172 "Invalid keylist. It must contain only strings.");
175 vectkeylist
.push_back(PyString_AsString(str
));
178 if (!ob::openbox
->bindings()->removeKey(vectkeylist
, func
)) {
179 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
182 Py_INCREF(Py_None
); return Py_None
;
187 ob::openbox
->bindings()->removeAllKeys();
190 void set_reset_key(const std::string
&key
)
192 ob::openbox
->bindings()->setResetKey(key
);
195 PyObject
*send_client_msg(Window target
, Atom type
, Window about
,
196 long data
, long data1
, long data2
,
197 long data3
, long data4
)
200 e
.xclient
.type
= ClientMessage
;
201 e
.xclient
.format
= 32;
202 e
.xclient
.message_type
= type
;
203 e
.xclient
.window
= about
;
204 e
.xclient
.data
.l
[0] = data
;
205 e
.xclient
.data
.l
[1] = data1
;
206 e
.xclient
.data
.l
[2] = data2
;
207 e
.xclient
.data
.l
[3] = data3
;
208 e
.xclient
.data
.l
[4] = data4
;
210 XSendEvent(**otk::display
, target
, false,
211 SubstructureRedirectMask
| SubstructureNotifyMask
,
213 Py_INCREF(Py_None
); return Py_None
;