]>
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*>(((std::string
)"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()");
38 // set up access to the python global variables
39 PyObject
*obmodule
= PyImport_AddModule("__main__");
40 obdict
= PyModule_GetDict(obmodule
);
48 bool python_exec(const std::string
&path
)
50 FILE *rcpyfd
= fopen(path
.c_str(), "r");
52 printf("failed to load python file %s\n", path
.c_str());
55 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
60 bool python_get_long(const char *name
, long *value
)
62 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
63 if (!(val
&& PyLong_Check(val
))) return false;
65 *value
= PyLong_AsLong(val
);
69 bool python_get_string(const char *name
, std::string
*value
)
71 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
72 if (!(val
&& PyString_Check(val
))) return false;
74 *value
= PyString_AsString(val
);
78 bool python_get_stringlist(const char *name
, std::vector
<std::string
> *value
)
80 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
81 if (!(val
&& PyList_Check(val
))) return false;
83 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
84 PyObject
*str
= PyList_GetItem(val
, i
);
85 if (PyString_Check(str
))
86 value
->push_back(PyString_AsString(str
));
91 // ************************************* //
92 // Stuff for calling from Python scripts //
93 // ************************************* //
95 PyObject
*mbind(const std::string
&button
, ob::MouseContext context
,
96 ob::MouseAction action
, PyObject
*func
)
98 if (!PyCallable_Check(func
)) {
99 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
103 if (!ob::Openbox::instance
->bindings()->addButton(button
, context
,
105 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
108 Py_INCREF(Py_None
); return Py_None
;
111 PyObject
*ebind(ob::EventAction action
, PyObject
*func
)
113 if (!PyCallable_Check(func
)) {
114 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
118 if (!ob::Openbox::instance
->bindings()->addEvent(action
, func
)) {
119 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
122 Py_INCREF(Py_None
); return Py_None
;
125 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext context
, PyObject
*func
)
127 if (!PyCallable_Check(func
)) {
128 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
131 if (!PyList_Check(keylist
)) {
132 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
136 ob::OBBindings::StringVect vectkeylist
;
137 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
138 PyObject
*str
= PyList_GetItem(keylist
, i
);
139 if (!PyString_Check(str
)) {
140 PyErr_SetString(PyExc_TypeError
,
141 "Invalid keylist. It must contain only strings.");
144 vectkeylist
.push_back(PyString_AsString(str
));
147 (void)context
; // XXX use this sometime!
148 if (!ob::Openbox::instance
->bindings()->addKey(vectkeylist
, func
)) {
149 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
152 Py_INCREF(Py_None
); return Py_None
;
155 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
157 if (!PyList_Check(keylist
)) {
158 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
161 if (!PyCallable_Check(func
)) {
162 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
166 ob::OBBindings::StringVect vectkeylist
;
167 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
168 PyObject
*str
= PyList_GetItem(keylist
, i
);
169 if (!PyString_Check(str
)) {
170 PyErr_SetString(PyExc_TypeError
,
171 "Invalid keylist. It must contain only strings.");
174 vectkeylist
.push_back(PyString_AsString(str
));
177 if (!ob::Openbox::instance
->bindings()->removeKey(vectkeylist
, func
)) {
178 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
181 Py_INCREF(Py_None
); return Py_None
;
186 ob::Openbox::instance
->bindings()->removeAllKeys();
189 void set_reset_key(const std::string
&key
)
191 ob::Openbox::instance
->bindings()->setResetKey(key
);
194 PyObject
*send_client_msg(Window target
, int type
, Window about
,
195 long data
, long data1
, long data2
,
196 long data3
, long data4
)
198 if (type
< 0 || type
>= otk::OBProperty::NUM_ATOMS
) {
199 PyErr_SetString(PyExc_TypeError
,
200 "Invalid atom type. Must be from otk::OBProperty::Atoms");
205 e
.xclient
.type
= ClientMessage
;
206 e
.xclient
.format
= 32;
207 e
.xclient
.message_type
=
208 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)type
);
209 e
.xclient
.window
= about
;
210 e
.xclient
.data
.l
[0] = data
;
211 e
.xclient
.data
.l
[1] = data1
;
212 e
.xclient
.data
.l
[2] = data2
;
213 e
.xclient
.data
.l
[3] = data3
;
214 e
.xclient
.data
.l
[4] = data4
;
216 XSendEvent(otk::OBDisplay::display
, target
, false,
217 SubstructureRedirectMask
| SubstructureNotifyMask
,
219 Py_INCREF(Py_None
); return Py_None
;
This page took 0.055567 seconds and 5 git commands to generate.