]>
Dogcows Code - chaz/openbox/blob - src/python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
8 #include "otk/display.hh"
11 // The initializer in openbox_wrap.cc
12 extern void init_openbox(void);
13 // The initializer in otk_wrap.cc
14 extern void init_otk(void);
19 static PyObject
*obdict
= NULL
;
21 void python_init(char *argv0
)
23 Py_SetProgramName(argv0
);
27 PyRun_SimpleString("from _otk import *; from _openbox import *;");
28 PyRun_SimpleString("openbox = Openbox_instance()");
29 PyRun_SimpleString("display = OBDisplay_display()");
32 sys.path.append('stuff')
33 install the .py wrappers, and include their path with this, then import em
34 and ~/.openbox/python/ !!
37 // set up access to the python global variables
38 PyObject
*obmodule
= PyImport_AddModule("__main__");
39 obdict
= PyModule_GetDict(obmodule
);
47 bool python_exec(const std::string
&path
)
49 FILE *rcpyfd
= fopen(path
.c_str(), "r");
51 printf("failed to load python file %s\n", path
.c_str());
54 PyRun_SimpleFile(rcpyfd
, const_cast<char*>(path
.c_str()));
59 bool python_get_long(const char *name
, long *value
)
61 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
62 if (!(val
&& PyLong_Check(val
))) return false;
64 *value
= PyLong_AsLong(val
);
68 bool python_get_string(const char *name
, std::string
*value
)
70 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
71 if (!(val
&& PyString_Check(val
))) return false;
73 *value
= PyString_AsString(val
);
77 bool python_get_stringlist(const char *name
, std::vector
<std::string
> *value
)
79 PyObject
*val
= PyDict_GetItemString(obdict
, const_cast<char*>(name
));
80 if (!(val
&& PyList_Check(val
))) return false;
82 for (int i
= 0, end
= PyList_Size(val
); i
< end
; ++i
) {
83 PyObject
*str
= PyList_GetItem(val
, i
);
84 if (PyString_Check(str
))
85 value
->push_back(PyString_AsString(str
));
90 // ************************************* //
91 // Stuff for calling from Python scripts //
92 // ************************************* //
94 PyObject
*mbind(const std::string
&button
, ob::MouseContext context
,
95 ob::MouseAction action
, PyObject
*func
)
97 if (!PyCallable_Check(func
)) {
98 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
102 if (!ob::Openbox::instance
->bindings()->addButton(button
, context
,
104 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
107 Py_INCREF(Py_None
); return Py_None
;
110 PyObject
*ebind(ob::EventAction action
, PyObject
*func
)
112 if (!PyCallable_Check(func
)) {
113 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
117 if (!ob::Openbox::instance
->bindings()->addEvent(action
, func
)) {
118 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
121 Py_INCREF(Py_None
); return Py_None
;
124 PyObject
*kbind(PyObject
*keylist
, ob::KeyContext context
, PyObject
*func
)
126 if (!PyCallable_Check(func
)) {
127 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
130 if (!PyList_Check(keylist
)) {
131 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
135 ob::OBBindings::StringVect vectkeylist
;
136 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
137 PyObject
*str
= PyList_GetItem(keylist
, i
);
138 if (!PyString_Check(str
)) {
139 PyErr_SetString(PyExc_TypeError
,
140 "Invalid keylist. It must contain only strings.");
143 vectkeylist
.push_back(PyString_AsString(str
));
146 if (!ob::Openbox::instance
->bindings()->addKey(vectkeylist
, func
)) {
147 PyErr_SetString(PyExc_RuntimeError
,"Unable to add binding.");
150 Py_INCREF(Py_None
); return Py_None
;
153 PyObject
*kunbind(PyObject
*keylist
, PyObject
*func
)
155 if (!PyList_Check(keylist
)) {
156 PyErr_SetString(PyExc_TypeError
, "Invalid keylist. Not a list.");
159 if (!PyCallable_Check(func
)) {
160 PyErr_SetString(PyExc_TypeError
, "Invalid callback function.");
164 ob::OBBindings::StringVect vectkeylist
;
165 for (int i
= 0, end
= PyList_Size(keylist
); i
< end
; ++i
) {
166 PyObject
*str
= PyList_GetItem(keylist
, i
);
167 if (!PyString_Check(str
)) {
168 PyErr_SetString(PyExc_TypeError
,
169 "Invalid keylist. It must contain only strings.");
172 vectkeylist
.push_back(PyString_AsString(str
));
175 if (!ob::Openbox::instance
->bindings()->removeKey(vectkeylist
, func
)) {
176 PyErr_SetString(PyExc_RuntimeError
, "Could not remove callback.");
179 Py_INCREF(Py_None
); return Py_None
;
184 ob::Openbox::instance
->bindings()->removeAllKeys();
187 void set_reset_key(const std::string
&key
)
189 ob::Openbox::instance
->bindings()->setResetKey(key
);
192 PyObject
*send_client_msg(Window target
, int type
, Window about
,
193 long data
, long data1
, long data2
,
194 long data3
, long data4
)
196 if (type
< 0 || type
>= otk::OBProperty::NUM_ATOMS
) {
197 PyErr_SetString(PyExc_TypeError
,
198 "Invalid atom type. Must be from otk::OBProperty::Atoms");
203 e
.xclient
.type
= ClientMessage
;
204 e
.xclient
.format
= 32;
205 e
.xclient
.message_type
=
206 Openbox::instance
->property()->atom((otk::OBProperty::Atoms
)type
);
207 e
.xclient
.window
= about
;
208 e
.xclient
.data
.l
[0] = data
;
209 e
.xclient
.data
.l
[1] = data1
;
210 e
.xclient
.data
.l
[2] = data2
;
211 e
.xclient
.data
.l
[3] = data3
;
212 e
.xclient
.data
.l
[4] = data4
;
214 XSendEvent(otk::OBDisplay::display
, target
, false,
215 SubstructureRedirectMask
| SubstructureNotifyMask
,
217 Py_INCREF(Py_None
); return Py_None
;
This page took 0.047529 seconds and 5 git commands to generate.