]> icculus.org git repositories - mikachu/openbox.git/blob - src/python.cc
require automake 1.7.1 for py-compile
[mikachu/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 / otk_wrap.cc
13 extern void init_ob(void);
14 extern void init_otk(void);
15 }
16
17 namespace ob {
18
19 static PyObject *obdict = NULL;
20
21 void python_init(char *argv0)
22 {
23   // start the python engine
24   Py_SetProgramName(argv0);
25   Py_Initialize();
26   // initialize the C python module
27   init_otk();
28   init_ob();
29   // prepend the openbox directories for python scripts to the sys path
30   PyRun_SimpleString("import sys");
31   PyRun_SimpleString("sys.path.insert(0, '" SCRIPTDIR "')");
32   PyRun_SimpleString(const_cast<char*>(("sys.path.insert(0, '" +
33                                         otk::expandTilde("~/.openbox/python") +
34                                         "')").c_str()));
35   PyRun_SimpleString("import ob; import otk; import config;");
36   // set up convenience global variables
37   PyRun_SimpleString("ob.openbox = ob.Openbox_instance()");
38   PyRun_SimpleString("otk.display = otk.Display_instance()");
39
40   // set up access to the python global variables
41   PyObject *obmodule = PyImport_AddModule("config");
42   obdict = PyModule_GetDict(obmodule);
43 }
44
45 void python_destroy()
46 {
47   Py_Finalize();
48 }
49
50 bool python_exec(const std::string &path)
51 {
52   FILE *rcpyfd = fopen(path.c_str(), "r");
53   if (!rcpyfd) {
54     printf("Failed to load python file %s\n", path.c_str());
55     return false;
56   }
57   PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
58   fclose(rcpyfd);
59   return true;
60 }
61
62 bool python_get_long(const char *name, long *value)
63 {
64   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
65   if (!(val && PyInt_Check(val))) return false;
66   
67   *value = PyInt_AsLong(val);
68   return true;
69 }
70
71 bool python_get_string(const char *name, otk::ustring *value)
72 {
73   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
74   if (!(val && PyString_Check(val))) return false;
75   
76   *value = PyString_AsString(val);
77   return true;
78 }
79
80 bool python_get_stringlist(const char *name, std::vector<otk::ustring> *value)
81 {
82   PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
83   if (!(val && PyList_Check(val))) return false;
84
85   for (int i = 0, end = PyList_Size(val); i < end; ++i) {
86     PyObject *str = PyList_GetItem(val, i);
87     if (PyString_Check(str))
88       value->push_back(PyString_AsString(str));
89   }
90   return true;
91 }
92
93 // ************************************* //
94 // Stuff for calling from Python scripts //
95 // ************************************* //
96
97 PyObject *mbind(const std::string &button, ob::MouseContext::MC context,
98                 ob::MouseAction::MA action, PyObject *func)
99 {
100   if (!PyCallable_Check(func)) {
101     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
102     return NULL;
103   }
104   if(context < 0 || context >= MouseContext::NUM_MOUSE_CONTEXT) {
105     PyErr_SetString(PyExc_ValueError, "Invalid MouseContext");
106     return NULL;
107   }
108   if(action < 0 || action >= MouseAction::NUM_MOUSE_ACTION) {
109     PyErr_SetString(PyExc_ValueError, "Invalid MouseAction");
110     return NULL;
111   }
112   
113   if (!ob::openbox->bindings()->addButton(button, context,
114                                           action, func)) {
115     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
116     return NULL;
117   }
118   Py_INCREF(Py_None); return Py_None;
119 }
120
121 PyObject *ebind(ob::EventAction::EA action, PyObject *func)
122 {
123   if (!PyCallable_Check(func)) {
124     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
125     return NULL;
126   }
127   
128   if (!ob::openbox->bindings()->addEvent(action, func)) {
129     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
130     return NULL;
131   }
132   Py_INCREF(Py_None); return Py_None;
133 }
134
135 PyObject *kgrab(int screen, PyObject *func)
136 {
137   if (!PyCallable_Check(func)) {
138     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
139     return NULL;
140   }
141
142   if (!ob::openbox->bindings()->grabKeyboard(screen, func)) {
143     PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
144     return NULL;
145   }
146   Py_INCREF(Py_None); return Py_None;
147 }
148
149 PyObject *kungrab()
150 {
151   ob::openbox->bindings()->ungrabKeyboard();
152   Py_INCREF(Py_None); return Py_None;
153 }
154
155 PyObject *mgrab(int screen)
156 {
157   if (!ob::openbox->bindings()->grabPointer(screen)) {
158     PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
159     return NULL;
160   }
161   Py_INCREF(Py_None); return Py_None;
162 }
163
164 PyObject *mungrab()
165 {
166   ob::openbox->bindings()->ungrabPointer();
167   Py_INCREF(Py_None); return Py_None;
168 }
169
170 PyObject *kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
171 {
172   if (!PyCallable_Check(func)) {
173     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
174     return NULL;
175   }
176   if (!PyList_Check(keylist)) {
177     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
178     return NULL;
179   }
180
181   ob::Bindings::StringVect vectkeylist;
182   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
183     PyObject *str = PyList_GetItem(keylist, i);
184     if (!PyString_Check(str)) {
185       PyErr_SetString(PyExc_TypeError,
186                      "Invalid keylist. It must contain only strings.");
187       return NULL;
188     }
189     vectkeylist.push_back(PyString_AsString(str));
190   }
191
192   (void)context; // XXX use this sometime!
193   if (!ob::openbox->bindings()->addKey(vectkeylist, func)) {
194     PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
195     return NULL;
196   }
197   Py_INCREF(Py_None); return Py_None;
198 }
199
200 /*
201 PyObject *kunbind(PyObject *keylist, PyObject *func)
202 {
203   if (!PyList_Check(keylist)) {
204     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
205     return NULL;
206   }
207   if (!PyCallable_Check(func)) {
208     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
209     return NULL;
210   }
211   
212   ob::Bindings::StringVect vectkeylist;
213   for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
214     PyObject *str = PyList_GetItem(keylist, i);
215     if (!PyString_Check(str)) {
216       PyErr_SetString(PyExc_TypeError,
217                      "Invalid keylist. It must contain only strings.");
218       return NULL;
219     }
220     vectkeylist.push_back(PyString_AsString(str));
221   }
222
223   if (!ob::openbox->bindings()->removeKey(vectkeylist, func)) {
224       PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
225       return NULL;
226   }
227   Py_INCREF(Py_None); return Py_None;
228 }
229 */
230
231 void kunbind_all()
232 {
233   ob::openbox->bindings()->removeAllKeys();
234 }
235
236 void set_reset_key(const std::string &key)
237 {
238   ob::openbox->bindings()->setResetKey(key);
239 }
240
241 PyObject *send_client_msg(Window target, Atom type, Window about,
242                           long data, long data1, long data2,
243                           long data3, long data4)
244 {
245   XEvent e;
246   e.xclient.type = ClientMessage;
247   e.xclient.format = 32;
248   e.xclient.message_type = type;
249   e.xclient.window = about;
250   e.xclient.data.l[0] = data;
251   e.xclient.data.l[1] = data1;
252   e.xclient.data.l[2] = data2;
253   e.xclient.data.l[3] = data3;
254   e.xclient.data.l[4] = data4;
255
256   XSendEvent(**otk::display, target, false,
257              SubstructureRedirectMask | SubstructureNotifyMask,
258              &e);
259   Py_INCREF(Py_None); return Py_None;
260 }
261
262 void execute(const std::string &bin, int screen)
263 {
264   if (screen >= ScreenCount(**otk::display))
265     screen = 0;
266   otk::bexec(bin, otk::display->screenInfo(screen)->displayString());
267 }
268
269 }