]>
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);
14 // The initializer in otk_wrap.cc
15 extern void init_otk(void);
20 static PyObject
*obdict
= NULL
;
22 void python_init(char *argv0
)
24 // start the python engine
25 Py_SetProgramName(argv0
);
27 // initialize the C python modules
30 // include the openbox directories for python scripts in the sys path
31 PyRun_SimpleString("import sys");
32 PyRun_SimpleString("sys.path.append('" SCRIPTDIR
"')");
33 PyRun_SimpleString(const_cast<char*>(((std::string
)"sys.path.append('" +
34 otk::expandTilde("~/.openbox/python") +
36 // import the otk and openbox modules into the main namespace
37 PyRun_SimpleString("from openbox import *;");
38 // set up convenience global variables
39 PyRun_SimpleString("openbox = Openbox_instance()");
40 PyRun_SimpleString("display = OBDisplay_display()");
42 // set up access to the python global variables
43 PyObject
*obmodule
= PyImport_AddModule("__main__");
44 obdict
= PyModule_GetDict(obmodule
);
52 bool python_exec(const std::string
&path
)
54 FILE *rcpyfd
= fopen(path
.c_str(), "r");
56 printf("failed to load python file %s\n", path
.c_str());
59 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
64 bool python_get_long(const char *name
, long *value
)
66 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
67 if (!(val
&& PyLong_Check(val
))) return false;
69 *value
= PyLong_AsLong(val
);
73 bool python_get_string(const char *name
, std::string
*value
)
75 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
76 if (!(val
&& PyString_Check(val
))) return false;
78 *value
= PyString_AsString(val
);
82 bool python_get_stringlist(const char *name
, std::vector
<std::string
> *value
)
84 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
85 if (!(val
&& PyList_Check(val
))) return false;
87 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
88 PyObject
*str
= PyList_GetItem(val
, i
);
89 if (PyString_Check(str
))
90 value
->push_back(PyString_AsString(str
));
95 // ************************************* //
96 // Stuff for calling from Python scripts //
97 // ************************************* //
99 PyObject
*mbind(const std::string
&button
, ob::MouseContext context
,
100 ob::MouseAction action
, PyObject
*func
)
102 if (!PyCallable_Check(func
)) {
103 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
107 if (!ob::Openbox::instance
->bindings()->addButton(button
, context
,
109 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
112 Py_INCREF(Py_None
); return Py_None
;
115 PyObject
*ebind(ob::EventAction action
, PyObject
*func
)
117 if (!PyCallable_Check(func
)) {
118 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
122 if (!ob::Openbox::instance
->bindings()->addEvent(action
, func
)) {
123 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
126 Py_INCREF(Py_None
); return Py_None
;
129 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext context
, PyObject
*func
)
131 if (!PyCallable_Check(func
)) {
132 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
135 if (!PyList_Check(keylist
)) {
136 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
140 ob::OBBindings::StringVect vectkeylist
;
141 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
142 PyObject
*str
= PyList_GetItem(keylist
, i
);
143 if (!PyString_Check(str
)) {
144 PyErr_SetString(PyExc_TypeError
,
145 "Invalid keylist. It must contain only strings.");
148 vectkeylist
.push_back(PyString_AsString(str
));
151 (void)context
; // XXX use this sometime!
152 if (!ob::Openbox::instance
->bindings()->addKey(vectkeylist
, func
)) {
153 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
156 Py_INCREF(Py_None
); return Py_None
;
159 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
161 if (!PyList_Check(keylist
)) {
162 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
165 if (!PyCallable_Check(func
)) {
166 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
170 ob::OBBindings::StringVect vectkeylist
;
171 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
172 PyObject
*str
= PyList_GetItem(keylist
, i
);
173 if (!PyString_Check(str
)) {
174 PyErr_SetString(PyExc_TypeError
,
175 "Invalid keylist. It must contain only strings.");
178 vectkeylist
.push_back(PyString_AsString(str
));
181 if (!ob::Openbox::instance
->bindings()->removeKey(vectkeylist
, func
)) {
182 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
185 Py_INCREF(Py_None
); return Py_None
;
190 ob::Openbox::instance
->bindings()->removeAllKeys();
193 void set_reset_key(const std::string
&key
)
195 ob::Openbox::instance
->bindings()->setResetKey(key
);
198 PyObject
*send_client_msg(Window target
, int type
, Window about
,
199 long data
, long data1
, long data2
,
200 long data3
, long data4
)
202 if (type
< 0 || type
>= otk::OBProperty::NUM_ATOMS
) {
203 PyErr_SetString(PyExc_TypeError
,
204 "Invalid atom type. Must be from otk::OBProperty::Atoms");
209 e
.xclient
.type
= ClientMessage
;
210 e
.xclient
.format
= 32;
211 e
.xclient
.message_type
=
212 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)type
);
213 e
.xclient
.window
= about
;
214 e
.xclient
.data
.l
[0] = data
;
215 e
.xclient
.data
.l
[1] = data1
;
216 e
.xclient
.data
.l
[2] = data2
;
217 e
.xclient
.data
.l
[3] = data3
;
218 e
.xclient
.data
.l
[4] = data4
;
220 XSendEvent(otk::OBDisplay::display
, target
, false,
221 SubstructureRedirectMask
| SubstructureNotifyMask
,
223 Py_INCREF(Py_None
); return Py_None
;
This page took 0.045085 seconds and 5 git commands to generate.