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);
18 static PyObject *obdict = NULL;
20 void python_init(char *argv0)
24 // start the python engine
25 //Py_SetProgramName(argv0);
27 // initialize the C python module
29 // include the openbox directories for python scripts in the sys path
30 // PyRun_SimpleString("import sys");
31 printf("SCRIPTDIR=%s\n", SCRIPTDIR);
32 printf("1 getenv(DISPLAY)=%s\n", getenv("DISPLAY"));
34 printf("2 getenv(DISPLAY)=%s\n", getenv("DISPLAY"));
35 path = "sys.path.append('";
36 printf("3 getenv(DISPLAY)=%s\n", getenv("DISPLAY"));
39 PyRun_SimpleString(const_cast<char*>(path.c_str()));
40 path = "sys.path.append('";
41 path += otk::expandTilde("~/.openbox/python");
43 PyRun_SimpleString(const_cast<char*>(path.c_str()));
44 // import the otk and openbox modules into the main namespace
45 PyRun_SimpleString("from openbox import *;");
46 // set up convenience global variables
47 PyRun_SimpleString("openbox = Openbox_instance()");
49 // set up access to the python global variables
50 PyObject *obmodule = PyImport_AddModule("__main__");
51 obdict = PyModule_GetDict(obmodule);
59 bool python_exec(const std::string &path)
61 FILE *rcpyfd = fopen(path.c_str(), "r");
63 printf("failed to load python file %s\n", path.c_str());
66 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
71 bool python_get_long(const char *name, long *value)
73 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
74 if (!(val && PyLong_Check(val))) return false;
76 *value = PyLong_AsLong(val);
80 bool python_get_string(const char *name, std::string *value)
82 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
83 if (!(val && PyString_Check(val))) return false;
85 *value = PyString_AsString(val);
89 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
91 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
92 if (!(val && PyList_Check(val))) return false;
94 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
95 PyObject *str = PyList_GetItem(val, i);
96 if (PyString_Check(str))
97 value->push_back(PyString_AsString(str));
102 // ************************************* //
103 // Stuff for calling from Python scripts //
104 // ************************************* //
106 PyObject *mbind(const std::string &button, ob::MouseContext context,
107 ob::MouseAction action, PyObject *func)
109 if (!PyCallable_Check(func)) {
110 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
114 if (!ob::Openbox::instance->bindings()->addButton(button, context,
116 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
119 Py_INCREF(Py_None); return Py_None;
122 PyObject *ebind(ob::EventAction action, PyObject *func)
124 if (!PyCallable_Check(func)) {
125 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
129 if (!ob::Openbox::instance->bindings()->addEvent(action, func)) {
130 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
133 Py_INCREF(Py_None); return Py_None;
136 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
138 if (!PyCallable_Check(func)) {
139 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
142 if (!PyList_Check(keylist)) {
143 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
147 ob::OBBindings::StringVect vectkeylist;
148 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
149 PyObject *str = PyList_GetItem(keylist, i);
150 if (!PyString_Check(str)) {
151 PyErr_SetString(PyExc_TypeError,
152 "Invalid keylist. It must contain only strings.");
155 vectkeylist.push_back(PyString_AsString(str));
158 (void)context; // XXX use this sometime!
159 if (!ob::Openbox::instance->bindings()->addKey(vectkeylist, func)) {
160 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
163 Py_INCREF(Py_None); return Py_None;
166 PyObject *kunbind(PyObject *keylist, PyObject *func)
168 if (!PyList_Check(keylist)) {
169 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
172 if (!PyCallable_Check(func)) {
173 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
177 ob::OBBindings::StringVect vectkeylist;
178 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
179 PyObject *str = PyList_GetItem(keylist, i);
180 if (!PyString_Check(str)) {
181 PyErr_SetString(PyExc_TypeError,
182 "Invalid keylist. It must contain only strings.");
185 vectkeylist.push_back(PyString_AsString(str));
188 if (!ob::Openbox::instance->bindings()->removeKey(vectkeylist, func)) {
189 PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
192 Py_INCREF(Py_None); return Py_None;
197 ob::Openbox::instance->bindings()->removeAllKeys();
200 void set_reset_key(const std::string &key)
202 ob::Openbox::instance->bindings()->setResetKey(key);
205 PyObject *send_client_msg(Window target, int type, Window about,
206 long data, long data1, long data2,
207 long data3, long data4)
209 if (type < 0 || type >= otk::OBProperty::NUM_ATOMS) {
210 PyErr_SetString(PyExc_TypeError,
211 "Invalid atom type. Must be from otk::OBProperty::Atoms");
216 e.xclient.type = ClientMessage;
217 e.xclient.format = 32;
218 e.xclient.message_type =
219 Openbox::instance->property()->atom((otk::OBProperty::Atoms)type);
220 e.xclient.window = about;
221 e.xclient.data.l[0] = data;
222 e.xclient.data.l[1] = data1;
223 e.xclient.data.l[2] = data2;
224 e.xclient.data.l[3] = data3;
225 e.xclient.data.l[4] = data4;
227 XSendEvent(otk::OBDisplay::display, target, false,
228 SubstructureRedirectMask | SubstructureNotifyMask,
230 Py_INCREF(Py_None); return Py_None;