]>
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
);
50 bool python_exec(const std::string
&path
)
52 FILE *rcpyfd
= fopen(path
.c_str(), "r");
54 printf("failed to load python file %s\n", path
.c_str());
57 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
62 bool python_get_long(const char *name
, long *value
)
64 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
65 if (!(val
&& PyInt_Check(val
))) return false;
67 *value
= PyInt_AsLong(val
);
71 bool python_get_string(const char *name
, otk::ustring
*value
)
73 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
74 if (!(val
&& PyString_Check(val
))) return false;
76 *value
= PyString_AsString(val
);
80 bool python_get_stringlist(const char *name
, std::vector
<otk::ustring
> *value
)
82 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
83 if (!(val
&& PyList_Check(val
))) return false;
85 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
86 PyObject
*str
= PyList_GetItem(val
, i
);
87 if (PyString_Check(str
))
88 value
->push_back(PyString_AsString(str
));
93 // ************************************* //
94 // Stuff for calling from Python scripts //
95 // ************************************* //
97 PyObject
*mbind(const std::string
&button
, ob::MouseContext::MC context
,
98 ob::MouseAction::MA action
, PyObject
*func
)
100 if (!PyCallable_Check(func
)) {
101 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
105 if (!ob::openbox
->bindings()->addButton(button
, context
,
107 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
110 Py_INCREF(Py_None
); return Py_None
;
113 PyObject
*ebind(ob::EventAction::EA action
, PyObject
*func
)
115 if (!PyCallable_Check(func
)) {
116 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
120 if (!ob::openbox
->bindings()->addEvent(action
, func
)) {
121 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
124 Py_INCREF(Py_None
); return Py_None
;
127 PyObject
*kgrab(int screen
, PyObject
*func
)
129 if (!PyCallable_Check(func
)) {
130 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
134 if (!ob::openbox
->bindings()->grabKeyboard(screen
, func
)) {
135 PyErr_SetString(PyExc_RuntimeError
,"Unable to grab keybaord.");
138 Py_INCREF(Py_None
); return Py_None
;
143 ob::openbox
->bindings()->ungrabKeyboard();
144 Py_INCREF(Py_None
); return Py_None
;
147 PyObject
*mgrab(int screen
)
149 if (!ob::openbox
->bindings()->grabPointer(screen
)) {
150 PyErr_SetString(PyExc_RuntimeError
,"Unable to grab pointer.");
153 Py_INCREF(Py_None
); return Py_None
;
158 ob::openbox
->bindings()->ungrabPointer();
159 Py_INCREF(Py_None
); return Py_None
;
162 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext::KC context
, PyObject
*func
)
164 if (!PyCallable_Check(func
)) {
165 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
168 if (!PyList_Check(keylist
)) {
169 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
173 ob::Bindings::StringVect vectkeylist
;
174 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
175 PyObject
*str
= PyList_GetItem(keylist
, i
);
176 if (!PyString_Check(str
)) {
177 PyErr_SetString(PyExc_TypeError
,
178 "Invalid keylist. It must contain only strings.");
181 vectkeylist
.push_back(PyString_AsString(str
));
184 (void)context
; // XXX use this sometime!
185 if (!ob::openbox
->bindings()->addKey(vectkeylist
, func
)) {
186 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
189 Py_INCREF(Py_None
); return Py_None
;
192 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
194 if (!PyList_Check(keylist
)) {
195 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
198 if (!PyCallable_Check(func
)) {
199 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
203 ob::Bindings::StringVect vectkeylist
;
204 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
205 PyObject
*str
= PyList_GetItem(keylist
, i
);
206 if (!PyString_Check(str
)) {
207 PyErr_SetString(PyExc_TypeError
,
208 "Invalid keylist. It must contain only strings.");
211 vectkeylist
.push_back(PyString_AsString(str
));
214 if (!ob::openbox
->bindings()->removeKey(vectkeylist
, func
)) {
215 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
218 Py_INCREF(Py_None
); return Py_None
;
223 ob::openbox
->bindings()->removeAllKeys();
226 void set_reset_key(const std::string
&key
)
228 ob::openbox
->bindings()->setResetKey(key
);
231 PyObject
*send_client_msg(Window target
, Atom type
, Window about
,
232 long data
, long data1
, long data2
,
233 long data3
, long data4
)
236 e
.xclient
.type
= ClientMessage
;
237 e
.xclient
.format
= 32;
238 e
.xclient
.message_type
= type
;
239 e
.xclient
.window
= about
;
240 e
.xclient
.data
.l
[0] = data
;
241 e
.xclient
.data
.l
[1] = data1
;
242 e
.xclient
.data
.l
[2] = data2
;
243 e
.xclient
.data
.l
[3] = data3
;
244 e
.xclient
.data
.l
[4] = data4
;
246 XSendEvent(**otk::display
, target
, false,
247 SubstructureRedirectMask
| SubstructureNotifyMask
,
249 Py_INCREF(Py_None
); return Py_None
;
252 void execute(const std::string
&bin
, int screen
)
254 if (screen
>= ScreenCount(**otk::display
))
256 otk::bexec(bin
, otk::display
->screenInfo(screen
)->displayString());
This page took 0.045597 seconds and 5 git commands to generate.