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