]> icculus.org git repositories - mikachu/openbox.git/blob - src/python.cc
add the global display variable
[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
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 *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
127 {
128   if (!PyCallable_Check(func)) {
129     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
130     return NULL;
131   }
132   if (!PyList_Check(keylist)) {
133     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
134     return NULL;
135   }
136
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;
144     }
145     vectkeylist.push_back(PyString_AsString(str));
146   }
147
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;
152   }
153   Py_INCREF(Py_None); return Py_None;
154 }
155
156 PyObject *kunbind(PyObject *keylist, PyObject *func)
157 {
158   if (!PyList_Check(keylist)) {
159     PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
160     return NULL;
161   }
162   if (!PyCallable_Check(func)) {
163     PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
164     return NULL;
165   }
166   
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;
174     }
175     vectkeylist.push_back(PyString_AsString(str));
176   }
177
178   if (!ob::openbox->bindings()->removeKey(vectkeylist, func)) {
179       PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
180       return NULL;
181   }
182   Py_INCREF(Py_None); return Py_None;
183 }
184
185 void kunbind_all()
186 {
187   ob::openbox->bindings()->removeAllKeys();
188 }
189
190 void set_reset_key(const std::string &key)
191 {
192   ob::openbox->bindings()->setResetKey(key);
193 }
194
195 PyObject *send_client_msg(Window target, int type, Window about,
196                           long data, long data1, long data2,
197                           long data3, long data4)
198 {
199   if (type < 0 || type >= otk::Property::NUM_ATOMS) {
200       PyErr_SetString(PyExc_TypeError,
201                      "Invalid atom type. Must be from otk::Property::Atoms");
202       return NULL;
203   }
204   
205   XEvent e;
206   e.xclient.type = ClientMessage;
207   e.xclient.format = 32;
208   e.xclient.message_type =
209     openbox->property()->atom((otk::Property::Atoms)type);
210   e.xclient.window = about;
211   e.xclient.data.l[0] = data;
212   e.xclient.data.l[1] = data1;
213   e.xclient.data.l[2] = data2;
214   e.xclient.data.l[3] = data3;
215   e.xclient.data.l[4] = data4;
216
217   XSendEvent(**otk::display, target, false,
218              SubstructureRedirectMask | SubstructureNotifyMask,
219              &e);
220   Py_INCREF(Py_None); return Py_None;
221 }
222
223 }