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