added "pixel32" typedef for 32bit rgb data
[openbox.git] / src / python.cc
blob9905dec59b2dd93211793b9b451c6bec3d773d3b
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
3 #include "python.hh"
4 #include "openbox.hh"
5 #include "actions.hh"
6 #include "python.hh"
7 #include "bindings.hh"
8 #include "otk/display.hh"
9 #include "otk/util.hh"
11 extern "C" {
12 // The initializer in openbox_wrap.cc
13 extern void init_openbox(void);
16 namespace ob {
18 static PyObject *obdict = NULL;
20 void python_init(char *argv0)
22 // start the python engine
23 Py_SetProgramName(argv0);
24 Py_Initialize();
25 // initialize the C python module
26 init_openbox();
27 // include the openbox directories for python scripts in the sys path
28 PyRun_SimpleString("import sys");
29 PyRun_SimpleString("sys.path.append('" SCRIPTDIR "')");
30 PyRun_SimpleString(const_cast<char*>(("sys.path.append('" +
31 otk::expandTilde("~/.openbox/python") +
32 "')").c_str()));
33 // import the otk and openbox modules into the main namespace
34 PyRun_SimpleString("from openbox import *;");
35 // set up convenience global variables
36 PyRun_SimpleString("openbox = Openbox_instance()");
37 PyRun_SimpleString("display = Display_instance()");
39 // set up access to the python global variables
40 PyObject *obmodule = PyImport_AddModule("__main__");
41 obdict = PyModule_GetDict(obmodule);
44 void python_destroy()
46 Py_DECREF(obdict);
49 bool python_exec(const std::string &path)
51 FILE *rcpyfd = fopen(path.c_str(), "r");
52 if (!rcpyfd) {
53 printf("failed to load python file %s\n", path.c_str());
54 return false;
56 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
57 fclose(rcpyfd);
58 return true;
61 bool python_get_long(const char *name, long *value)
63 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
64 if (!(val && PyInt_Check(val))) return false;
66 *value = PyInt_AsLong(val);
67 return true;
70 bool python_get_string(const char *name, otk::ustring *value)
72 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
73 if (!(val && PyString_Check(val))) return false;
75 *value = PyString_AsString(val);
76 return true;
79 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
81 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
82 if (!(val && PyList_Check(val))) return false;
84 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
85 PyObject *str = PyList_GetItem(val, i);
86 if (PyString_Check(str))
87 value->push_back(PyString_AsString(str));
89 return true;
92 // ************************************* //
93 // Stuff for calling from Python scripts //
94 // ************************************* //
96 PyObject *mbind(const std::string &button, ob::MouseContext context,
97 ob::MouseAction action, PyObject *func)
99 if (!PyCallable_Check(func)) {
100 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
101 return NULL;
104 if (!ob::openbox->bindings()->addButton(button, context,
105 action, func)) {
106 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
107 return NULL;
109 Py_INCREF(Py_None); return Py_None;
112 PyObject *ebind(ob::EventAction action, PyObject *func)
114 if (!PyCallable_Check(func)) {
115 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
116 return NULL;
119 if (!ob::openbox->bindings()->addEvent(action, func)) {
120 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
121 return NULL;
123 Py_INCREF(Py_None); return Py_None;
126 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
128 if (!PyCallable_Check(func)) {
129 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
130 return NULL;
132 if (!PyList_Check(keylist)) {
133 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
134 return NULL;
137 ob::Bindings::StringVect vectkeylist;
138 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
139 PyObject *str = PyList_GetItem(keylist, i);
140 if (!PyString_Check(str)) {
141 PyErr_SetString(PyExc_TypeError,
142 "Invalid keylist. It must contain only strings.");
143 return NULL;
145 vectkeylist.push_back(PyString_AsString(str));
148 (void)context; // XXX use this sometime!
149 if (!ob::openbox->bindings()->addKey(vectkeylist, func)) {
150 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
151 return NULL;
153 Py_INCREF(Py_None); return Py_None;
156 PyObject *kunbind(PyObject *keylist, PyObject *func)
158 if (!PyList_Check(keylist)) {
159 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
160 return NULL;
162 if (!PyCallable_Check(func)) {
163 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
164 return NULL;
167 ob::Bindings::StringVect vectkeylist;
168 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
169 PyObject *str = PyList_GetItem(keylist, i);
170 if (!PyString_Check(str)) {
171 PyErr_SetString(PyExc_TypeError,
172 "Invalid keylist. It must contain only strings.");
173 return NULL;
175 vectkeylist.push_back(PyString_AsString(str));
178 if (!ob::openbox->bindings()->removeKey(vectkeylist, func)) {
179 PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
180 return NULL;
182 Py_INCREF(Py_None); return Py_None;
185 void kunbind_all()
187 ob::openbox->bindings()->removeAllKeys();
190 void set_reset_key(const std::string &key)
192 ob::openbox->bindings()->setResetKey(key);
195 PyObject *send_client_msg(Window target, Atom type, Window about,
196 long data, long data1, long data2,
197 long data3, long data4)
199 XEvent e;
200 e.xclient.type = ClientMessage;
201 e.xclient.format = 32;
202 e.xclient.message_type = type;
203 e.xclient.window = about;
204 e.xclient.data.l[0] = data;
205 e.xclient.data.l[1] = data1;
206 e.xclient.data.l[2] = data2;
207 e.xclient.data.l[3] = data3;
208 e.xclient.data.l[4] = data4;
210 XSendEvent(**otk::display, target, false,
211 SubstructureRedirectMask | SubstructureNotifyMask,
212 &e);
213 Py_INCREF(Py_None); return Py_None;