]> Dogcows Code - chaz/openbox/blob - src/python.cc
initialize _modal, its used before the window's type is checked
[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_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
105 if (!ob::openbox->bindings()->addButton(button, context,
106 action, func)) {
107 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
108 return NULL;
109 }
110 Py_INCREF(Py_None); return Py_None;
111 }
112
113 PyObject *ebind(ob::EventAction::EA action, PyObject *func)
114 {
115 if (!PyCallable_Check(func)) {
116 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
117 return NULL;
118 }
119
120 if (!ob::openbox->bindings()->addEvent(action, func)) {
121 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
122 return NULL;
123 }
124 Py_INCREF(Py_None); return Py_None;
125 }
126
127 PyObject *kgrab(int screen, PyObject *func)
128 {
129 if (!PyCallable_Check(func)) {
130 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
131 return NULL;
132 }
133
134 if (!ob::openbox->bindings()->grabKeyboard(screen, func)) {
135 PyErr_SetString(PyExc_RuntimeError,"Unable to grab keybaord.");
136 return NULL;
137 }
138 Py_INCREF(Py_None); return Py_None;
139 }
140
141 PyObject *kungrab()
142 {
143 ob::openbox->bindings()->ungrabKeyboard();
144 Py_INCREF(Py_None); return Py_None;
145 }
146
147 PyObject *mgrab(int screen)
148 {
149 if (!ob::openbox->bindings()->grabPointer(screen)) {
150 PyErr_SetString(PyExc_RuntimeError,"Unable to grab pointer.");
151 return NULL;
152 }
153 Py_INCREF(Py_None); return Py_None;
154 }
155
156 PyObject *mungrab()
157 {
158 ob::openbox->bindings()->ungrabPointer();
159 Py_INCREF(Py_None); return Py_None;
160 }
161
162 PyObject *kbind(PyObject *keylist, ob::KeyContext::KC context, PyObject *func)
163 {
164 if (!PyCallable_Check(func)) {
165 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
166 return NULL;
167 }
168 if (!PyList_Check(keylist)) {
169 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
170 return NULL;
171 }
172
173 ob::Bindings::StringVect vectkeylist;
174 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
175 PyObject *str = PyList_GetItem(keylist, i);
176 if (!PyString_Check(str)) {
177 PyErr_SetString(PyExc_TypeError,
178 "Invalid keylist. It must contain only strings.");
179 return NULL;
180 }
181 vectkeylist.push_back(PyString_AsString(str));
182 }
183
184 (void)context; // XXX use this sometime!
185 if (!ob::openbox->bindings()->addKey(vectkeylist, func)) {
186 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
187 return NULL;
188 }
189 Py_INCREF(Py_None); return Py_None;
190 }
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
223 void kunbind_all()
224 {
225 ob::openbox->bindings()->removeAllKeys();
226 }
227
228 void set_reset_key(const std::string &key)
229 {
230 ob::openbox->bindings()->setResetKey(key);
231 }
232
233 PyObject *send_client_msg(Window target, Atom type, Window about,
234 long data, long data1, long data2,
235 long data3, long data4)
236 {
237 XEvent e;
238 e.xclient.type = ClientMessage;
239 e.xclient.format = 32;
240 e.xclient.message_type = type;
241 e.xclient.window = about;
242 e.xclient.data.l[0] = data;
243 e.xclient.data.l[1] = data1;
244 e.xclient.data.l[2] = data2;
245 e.xclient.data.l[3] = data3;
246 e.xclient.data.l[4] = data4;
247
248 XSendEvent(**otk::display, target, false,
249 SubstructureRedirectMask | SubstructureNotifyMask,
250 &e);
251 Py_INCREF(Py_None); return Py_None;
252 }
253
254 void execute(const std::string &bin, int screen)
255 {
256 if (screen >= ScreenCount(**otk::display))
257 screen = 0;
258 otk::bexec(bin, otk::display->screenInfo(screen)->displayString());
259 }
260
261 }
This page took 0.05099 seconds and 4 git commands to generate.