]> icculus.org git repositories - dana/openbox.git/blob - src/python.cc
some intermediate stage. stacked focus cycling is very broken. dont try it. going...
[dana/openbox.git] / src / python.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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"
10
11 extern "C" {
12 // The initializer in openbox_wrap.cc
13 extern void init_openbox(void);
14 }
15
16 namespace ob {
17
18 static PyObject *obdict = NULL;
19
20 void python_init(char *argv0)
21 {
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()");
38
39   // set up access to the python global variables
40   PyObject *obmodule = PyImport_AddModule("__main__");
41   obdict = PyModule_GetDict(obmodule);
42 }
43
44 void python_destroy()
45 {
46   Py_DECREF(obdict);
47 }
48
49 bool python_exec(const std::string &path)
50 {
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;
55   }
56   PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
57   fclose(rcpyfd);
58   return true;
59 }
60
61 bool python_get_long(const char *name, long *value)
62 {
63   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
64   if (!(val && PyInt_Check(val))) return false;
65   
66   *value = PyInt_AsLong(val);
67   return true;
68 }
69
70 bool python_get_string(const char *name, otk::ustring *value)
71 {
72   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
73   if (!(val && PyString_Check(val))) return false;
74   
75   *value = PyString_AsString(val);
76   return true;
77 }
78
79 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
80 {
81   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
82   if (!(val && PyList_Check(val))) return false;
83
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));
88   }
89   return true;
90 }
91
92 // ************************************* //
93 // Stuff for calling from Python scripts //
94 // ************************************* //
95
96 PyObject *mbind(const std::string &button, ob::MouseContext context,
97                 ob::MouseAction action, PyObject *func)
98 {
99   if (!PyCallable_Check(func)) {
100     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
101     return NULL;
102   }
103   
104   if (!ob::openbox->bindings()->addButton(button, context,
105                                                     action, func)) {
106     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
107     return NULL;
108   }
109   Py_INCREF(Py_None); return Py_None;
110 }
111
112 PyObject *ebind(ob::EventAction action, PyObject *func)
113 {
114   if (!PyCallable_Check(func)) {
115     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
116     return NULL;
117   }
118   
119   if (!ob::openbox->bindings()->addEvent(action, func)) {
120     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
121     return NULL;
122   }
123   Py_INCREF(Py_None); return Py_None;
124 }
125
126 PyObject *kgrab(int screen, PyObject *func)
127 {
128   if (!PyCallable_Check(func)) {
129     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
130     return NULL;
131   }
132
133   if (!ob::openbox->bindings()->grabKeyboard(screen, func)) {
134     PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
135     return NULL;
136   }
137   Py_INCREF(Py_None); return Py_None;
138 }
139
140 PyObject *kungrab()
141 {
142   ob::openbox->bindings()->ungrabKeyboard();
143   Py_INCREF(Py_None); return Py_None;
144 }
145
146 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
147 {
148   if (!PyCallable_Check(func)) {
149     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
150     return NULL;
151   }
152   if (!PyList_Check(keylist)) {
153     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
154     return NULL;
155   }
156
157   ob::Bindings::StringVect vectkeylist;
158   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
159     PyObject *str = PyList_GetItem(keylist, i);
160     if (!PyString_Check(str)) {
161       PyErr_SetString(PyExc_TypeError,
162                      "Invalid keylist. It must contain only strings.");
163       return NULL;
164     }
165     vectkeylist.push_back(PyString_AsString(str));
166   }
167
168   (void)context; // XXX use this sometime!
169   if (!ob::openbox->bindings()->addKey(vectkeylist, func)) {
170     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
171     return NULL;
172   }
173   Py_INCREF(Py_None); return Py_None;
174 }
175
176 PyObject *kunbind(PyObject *keylist, PyObject *func)
177 {
178   if (!PyList_Check(keylist)) {
179     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
180     return NULL;
181   }
182   if (!PyCallable_Check(func)) {
183     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
184     return NULL;
185   }
186   
187   ob::Bindings::StringVect vectkeylist;
188   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
189     PyObject *str = PyList_GetItem(keylist, i);
190     if (!PyString_Check(str)) {
191       PyErr_SetString(PyExc_TypeError,
192                      "Invalid keylist. It must contain only strings.");
193       return NULL;
194     }
195     vectkeylist.push_back(PyString_AsString(str));
196   }
197
198   if (!ob::openbox->bindings()->removeKey(vectkeylist, func)) {
199       PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
200       return NULL;
201   }
202   Py_INCREF(Py_None); return Py_None;
203 }
204
205 void kunbind_all()
206 {
207   ob::openbox->bindings()->removeAllKeys();
208 }
209
210 void set_reset_key(const std::string &key)
211 {
212   ob::openbox->bindings()->setResetKey(key);
213 }
214
215 PyObject *send_client_msg(Window target, Atom type, Window about,
216                           long data, long data1, long data2,
217                           long data3, long data4)
218 {
219   XEvent e;
220   e.xclient.type = ClientMessage;
221   e.xclient.format = 32;
222   e.xclient.message_type = type;
223   e.xclient.window = about;
224   e.xclient.data.l[0] = data;
225   e.xclient.data.l[1] = data1;
226   e.xclient.data.l[2] = data2;
227   e.xclient.data.l[3] = data3;
228   e.xclient.data.l[4] = data4;
229
230   XSendEvent(**otk::display, target, false,
231              SubstructureRedirectMask | SubstructureNotifyMask,
232              &e);
233   Py_INCREF(Py_None); return Py_None;
234 }
235
236 }