]>
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 Py_SetProgramName(argv0
);
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 // PyRun_SimpleString("from _otk import *; from _openbox import *;");
34 PyRun_SimpleString("from openbox import *;");
35 PyRun_SimpleString("openbox = Openbox_instance()");
36 PyRun_SimpleString("display = OBDisplay_display()");
39 sys.path.append('stuff')
40 install the .py wrappers, and include their path with this, then import em
41 and ~/.openbox/python/ !!
44 // set up access to the python global variables
45 PyObject
*obmodule
= PyImport_AddModule("__main__");
46 obdict
= PyModule_GetDict(obmodule
);
54 bool python_exec(const std::string
&path
)
56 FILE *rcpyfd
= fopen(path
.c_str(), "r");
58 printf("failed to load python file %s\n", path
.c_str());
61 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
66 bool python_get_long(const char *name
, long *value
)
68 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
69 if (!(val
&& PyLong_Check(val
))) return false;
71 *value
= PyLong_AsLong(val
);
75 bool python_get_string(const char *name
, std::string
*value
)
77 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
78 if (!(val
&& PyString_Check(val
))) return false;
80 *value
= PyString_AsString(val
);
84 bool python_get_stringlist(const char *name
, std::vector
<std::string
> *value
)
86 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
87 if (!(val
&& PyList_Check(val
))) return false;
89 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
90 PyObject
*str
= PyList_GetItem(val
, i
);
91 if (PyString_Check(str
))
92 value
->push_back(PyString_AsString(str
));
97 // ************************************* //
98 // Stuff for calling from Python scripts //
99 // ************************************* //
101 PyObject
*mbind(const std::string
&button
, ob::MouseContext context
,
102 ob::MouseAction action
, PyObject
*func
)
104 if (!PyCallable_Check(func
)) {
105 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
109 if (!ob::Openbox::instance
->bindings()->addButton(button
, context
,
111 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
114 Py_INCREF(Py_None
); return Py_None
;
117 PyObject
*ebind(ob::EventAction action
, PyObject
*func
)
119 if (!PyCallable_Check(func
)) {
120 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
124 if (!ob::Openbox::instance
->bindings()->addEvent(action
, func
)) {
125 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
128 Py_INCREF(Py_None
); return Py_None
;
131 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext context
, PyObject
*func
)
133 if (!PyCallable_Check(func
)) {
134 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
137 if (!PyList_Check(keylist
)) {
138 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
142 ob::OBBindings::StringVect vectkeylist
;
143 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
144 PyObject
*str
= PyList_GetItem(keylist
, i
);
145 if (!PyString_Check(str
)) {
146 PyErr_SetString(PyExc_TypeError
,
147 "Invalid keylist. It must contain only strings.");
150 vectkeylist
.push_back(PyString_AsString(str
));
153 (void)context
; // XXX use this sometime!
154 if (!ob::Openbox::instance
->bindings()->addKey(vectkeylist
, func
)) {
155 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
158 Py_INCREF(Py_None
); return Py_None
;
161 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
163 if (!PyList_Check(keylist
)) {
164 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
167 if (!PyCallable_Check(func
)) {
168 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
172 ob::OBBindings::StringVect vectkeylist
;
173 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
174 PyObject
*str
= PyList_GetItem(keylist
, i
);
175 if (!PyString_Check(str
)) {
176 PyErr_SetString(PyExc_TypeError
,
177 "Invalid keylist. It must contain only strings.");
180 vectkeylist
.push_back(PyString_AsString(str
));
183 if (!ob::Openbox::instance
->bindings()->removeKey(vectkeylist
, func
)) {
184 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
187 Py_INCREF(Py_None
); return Py_None
;
192 ob::Openbox::instance
->bindings()->removeAllKeys();
195 void set_reset_key(const std::string
&key
)
197 ob::Openbox::instance
->bindings()->setResetKey(key
);
200 PyObject
*send_client_msg(Window target
, int type
, Window about
,
201 long data
, long data1
, long data2
,
202 long data3
, long data4
)
204 if (type
< 0 || type
>= otk::OBProperty::NUM_ATOMS
) {
205 PyErr_SetString(PyExc_TypeError
,
206 "Invalid atom type. Must be from otk::OBProperty::Atoms");
211 e
.xclient
.type
= ClientMessage
;
212 e
.xclient
.format
= 32;
213 e
.xclient
.message_type
=
214 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)type
);
215 e
.xclient
.window
= about
;
216 e
.xclient
.data
.l
[0] = data
;
217 e
.xclient
.data
.l
[1] = data1
;
218 e
.xclient
.data
.l
[2] = data2
;
219 e
.xclient
.data
.l
[3] = data3
;
220 e
.xclient
.data
.l
[4] = data4
;
222 XSendEvent(otk::OBDisplay::display
, target
, false,
223 SubstructureRedirectMask
| SubstructureNotifyMask
,
225 Py_INCREF(Py_None
); return Py_None
;
This page took 0.04556 seconds and 5 git commands to generate.