]>
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 / otk_wrap.cc
13 extern void init_ob(void);
14 extern void init_otk(void);
19 static PyObject
*obdict
= NULL
;
21 void python_init(char *argv0
)
23 // start the python engine
24 Py_SetProgramName(argv0
);
26 // initialize the C python module
29 // prepend the openbox directories for python scripts to the sys path
30 PyRun_SimpleString("import sys");
31 PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR
"')");
32 PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
33 otk::expandTilde("~/.openbox/python") +
35 PyRun_SimpleString("import ob; import otk; import config;");
36 // set up convenience global variables
37 PyRun_SimpleString("ob.openbox = ob.Openbox_instance()");
38 PyRun_SimpleString("otk.display = otk.Display_instance()");
40 // set up access to the python global variables
41 PyObject
*obmodule
= PyImport_AddModule("config");
42 obdict
= PyModule_GetDict(obmodule
);
51 bool python_exec(const std::string
&path
)
53 FILE *rcpyfd
= fopen(path
.c_str(), "r");
55 printf("Failed to load python file %s\n", path
.c_str());
58 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
63 bool python_get_long(const char *name
, long *value
)
65 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
66 if (!(val
&& PyInt_Check(val
))) return false;
68 *value
= PyInt_AsLong(val
);
72 bool python_get_string(const char *name
, otk::ustring
*value
)
74 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
75 if (!(val
&& PyString_Check(val
))) return false;
77 *value
= PyString_AsString(val
);
81 bool python_get_stringlist(const char *name
, std::vector
<otk::ustring
> *value
)
83 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
84 if (!(val
&& PyList_Check(val
))) return false;
86 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
87 PyObject
*str
= PyList_GetItem(val
, i
);
88 if (PyString_Check(str
))
89 value
->push_back(PyString_AsString(str
));
94 // ************************************* //
95 // Stuff for calling from Python scripts //
96 // ************************************* //
98 PyObject
*mbind(const std::string
&button
, ob::MouseContext::MC context
,
99 ob::MouseAction::MA action
, PyObject
*func
)
101 if (!PyCallable_Check(func
)) {
102 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
106 if (!ob::openbox
->bindings()->addButton(button
, context
,
108 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
111 Py_INCREF(Py_None
); return Py_None
;
114 PyObject
*ebind(ob::EventAction::EA action
, PyObject
*func
)
116 if (!PyCallable_Check(func
)) {
117 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
121 if (!ob::openbox
->bindings()->addEvent(action
, func
)) {
122 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
125 Py_INCREF(Py_None
); return Py_None
;
128 PyObject
*kgrab(int screen
, PyObject
*func
)
130 if (!PyCallable_Check(func
)) {
131 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
135 if (!ob::openbox
->bindings()->grabKeyboard(screen
, func
)) {
136 PyErr_SetString(PyExc_RuntimeError
,"Unable to grab keybaord.");
139 Py_INCREF(Py_None
); return Py_None
;
144 ob::openbox
->bindings()->ungrabKeyboard();
145 Py_INCREF(Py_None
); return Py_None
;
148 PyObject
*mgrab(int screen
)
150 if (!ob::openbox
->bindings()->grabPointer(screen
)) {
151 PyErr_SetString(PyExc_RuntimeError
,"Unable to grab pointer.");
154 Py_INCREF(Py_None
); return Py_None
;
159 ob::openbox
->bindings()->ungrabPointer();
160 Py_INCREF(Py_None
); return Py_None
;
163 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext::KC context
, PyObject
*func
)
165 if (!PyCallable_Check(func
)) {
166 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
169 if (!PyList_Check(keylist
)) {
170 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
174 ob::Bindings::StringVect vectkeylist
;
175 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
176 PyObject
*str
= PyList_GetItem(keylist
, i
);
177 if (!PyString_Check(str
)) {
178 PyErr_SetString(PyExc_TypeError
,
179 "Invalid keylist. It must contain only strings.");
182 vectkeylist
.push_back(PyString_AsString(str
));
185 (void)context
; // XXX use this sometime!
186 if (!ob::openbox
->bindings()->addKey(vectkeylist
, func
)) {
187 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
190 Py_INCREF(Py_None
); return Py_None
;
193 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
195 if (!PyList_Check(keylist
)) {
196 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
199 if (!PyCallable_Check(func
)) {
200 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
204 ob::Bindings::StringVect vectkeylist
;
205 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
206 PyObject
*str
= PyList_GetItem(keylist
, i
);
207 if (!PyString_Check(str
)) {
208 PyErr_SetString(PyExc_TypeError
,
209 "Invalid keylist. It must contain only strings.");
212 vectkeylist
.push_back(PyString_AsString(str
));
215 if (!ob::openbox
->bindings()->removeKey(vectkeylist
, func
)) {
216 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
219 Py_INCREF(Py_None
); return Py_None
;
224 ob::openbox
->bindings()->removeAllKeys();
227 void set_reset_key(const std::string
&key
)
229 ob::openbox
->bindings()->setResetKey(key
);
232 PyObject
*send_client_msg(Window target
, Atom type
, Window about
,
233 long data
, long data1
, long data2
,
234 long data3
, long data4
)
237 e
.xclient
.type
= ClientMessage
;
238 e
.xclient
.format
= 32;
239 e
.xclient
.message_type
= type
;
240 e
.xclient
.window
= about
;
241 e
.xclient
.data
.l
[0] = data
;
242 e
.xclient
.data
.l
[1] = data1
;
243 e
.xclient
.data
.l
[2] = data2
;
244 e
.xclient
.data
.l
[3] = data3
;
245 e
.xclient
.data
.l
[4] = data4
;
247 XSendEvent(**otk::display
, target
, false,
248 SubstructureRedirectMask
| SubstructureNotifyMask
,
250 Py_INCREF(Py_None
); return Py_None
;
253 void execute(const std::string
&bin
, int screen
)
255 if (screen
>= ScreenCount(**otk::display
))
257 otk::bexec(bin
, otk::display
->screenInfo(screen
)->displayString());
This page took 0.051578 seconds and 4 git commands to generate.