]>
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_ob(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(const_cast<char*>(("sys.path.append('" +
30 otk::expandTilde("~/.openbox/python") +
32 PyRun_SimpleString("sys.path.append('" SCRIPTDIR
"')");
33 PyRun_SimpleString("import ob;");
34 // set up convenience global variables
35 PyRun_SimpleString("ob.openbox = ob.Openbox_instance()");
36 PyRun_SimpleString("ob.display = ob.Display_instance()");
38 // set up access to the python global variables
39 PyObject
*obmodule
= PyImport_AddModule("config");
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
&& PyInt_Check(val
))) return false;
65 *value
= PyInt_AsLong(val
);
69 bool python_get_string(const char *name
, otk::ustring
*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
<otk::ustring
> *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::MC context
,
96 ob::MouseAction::MA action
, PyObject
*func
)
98 if (!PyCallable_Check(func
)) {
99 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
103 if (!ob::openbox
->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::EA action
, PyObject
*func
)
113 if (!PyCallable_Check(func
)) {
114 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
118 if (!ob::openbox
->bindings()->addEvent(action
, func
)) {
119 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
122 Py_INCREF(Py_None
); return Py_None
;
125 PyObject
*kgrab(int screen
, PyObject
*func
)
127 if (!PyCallable_Check(func
)) {
128 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
132 if (!ob::openbox
->bindings()->grabKeyboard(screen
, func
)) {
133 PyErr_SetString(PyExc_RuntimeError
,"Unable to grab keybaord.");
136 Py_INCREF(Py_None
); return Py_None
;
141 ob::openbox
->bindings()->ungrabKeyboard();
142 Py_INCREF(Py_None
); return Py_None
;
145 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext::KC context
, PyObject
*func
)
147 if (!PyCallable_Check(func
)) {
148 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
151 if (!PyList_Check(keylist
)) {
152 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
156 ob::Bindings::StringVect vectkeylist
;
157 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
158 PyObject
*str
= PyList_GetItem(keylist
, i
);
159 if (!PyString_Check(str
)) {
160 PyErr_SetString(PyExc_TypeError
,
161 "Invalid keylist. It must contain only strings.");
164 vectkeylist
.push_back(PyString_AsString(str
));
167 (void)context
; // XXX use this sometime!
168 if (!ob::openbox
->bindings()->addKey(vectkeylist
, func
)) {
169 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
172 Py_INCREF(Py_None
); return Py_None
;
175 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
177 if (!PyList_Check(keylist
)) {
178 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
181 if (!PyCallable_Check(func
)) {
182 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
186 ob::Bindings::StringVect vectkeylist
;
187 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
188 PyObject
*str
= PyList_GetItem(keylist
, i
);
189 if (!PyString_Check(str
)) {
190 PyErr_SetString(PyExc_TypeError
,
191 "Invalid keylist. It must contain only strings.");
194 vectkeylist
.push_back(PyString_AsString(str
));
197 if (!ob::openbox
->bindings()->removeKey(vectkeylist
, func
)) {
198 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
201 Py_INCREF(Py_None
); return Py_None
;
206 ob::openbox
->bindings()->removeAllKeys();
209 void set_reset_key(const std::string
&key
)
211 ob::openbox
->bindings()->setResetKey(key
);
214 PyObject
*send_client_msg(Window target
, Atom type
, Window about
,
215 long data
, long data1
, long data2
,
216 long data3
, long data4
)
219 e
.xclient
.type
= ClientMessage
;
220 e
.xclient
.format
= 32;
221 e
.xclient
.message_type
= type
;
222 e
.xclient
.window
= about
;
223 e
.xclient
.data
.l
[0] = data
;
224 e
.xclient
.data
.l
[1] = data1
;
225 e
.xclient
.data
.l
[2] = data2
;
226 e
.xclient
.data
.l
[3] = data3
;
227 e
.xclient
.data
.l
[4] = data4
;
229 XSendEvent(**otk::display
, target
, false,
230 SubstructureRedirectMask
| SubstructureNotifyMask
,
232 Py_INCREF(Py_None
); return Py_None
;
235 void execute(const std::string
&bin
, int screen
)
237 if (screen
>= ScreenCount(**otk::display
))
239 otk::bexec(bin
, otk::display
->screenInfo(screen
)->displayString());
This page took 0.044886 seconds and 5 git commands to generate.