]> Dogcows Code - chaz/openbox/blob - src/python.cc
new python callbacks data, infrastructure. going to rework bindings code. cvs wont...
[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
10 extern "C" {
11 // The initializer in openbox_wrap.cc
12 extern void init_openbox(void);
13 // The initializer in otk_wrap.cc
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 Py_SetProgramName(argv0);
24 Py_Initialize();
25 init_otk();
26 init_openbox();
27 PyRun_SimpleString("from _otk import *; from _openbox import *;");
28 PyRun_SimpleString("openbox = Openbox_instance()");
29 PyRun_SimpleString("display = OBDisplay_display()");
30
31 /* XXX
32 sys.path.append('stuff')
33 install the .py wrappers, and include their path with this, then import em
34 and ~/.openbox/python/ !!
35 */
36
37 // set up access to the python global variables
38 PyObject *obmodule = PyImport_AddModule("__main__");
39 obdict = PyModule_GetDict(obmodule);
40 }
41
42 void python_destroy()
43 {
44 Py_DECREF(obdict);
45 }
46
47 bool python_exec(const std::string &path)
48 {
49 FILE *rcpyfd = fopen(path.c_str(), "r");
50 if (!rcpyfd) {
51 printf("failed to load python file %s\n", path.c_str());
52 return false;
53 }
54 PyRun_SimpleFile(rcpyfd, const_cast<char*>(path.c_str()));
55 fclose(rcpyfd);
56 return true;
57 }
58
59 bool python_get_long(const char *name, long *value)
60 {
61 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
62 if (!(val && PyLong_Check(val))) return false;
63
64 *value = PyLong_AsLong(val);
65 return true;
66 }
67
68 bool python_get_string(const char *name, std::string *value)
69 {
70 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
71 if (!(val && PyString_Check(val))) return false;
72
73 *value = PyString_AsString(val);
74 return true;
75 }
76
77 bool python_get_stringlist(const char *name, std::vector<std::string> *value)
78 {
79 PyObject *val = PyDict_GetItemString(obdict, const_cast<char*>(name));
80 if (!(val && PyList_Check(val))) return false;
81
82 for (int i = 0, end = PyList_Size(val); i < end; ++i) {
83 PyObject *str = PyList_GetItem(val, i);
84 if (PyString_Check(str))
85 value->push_back(PyString_AsString(str));
86 }
87 return true;
88 }
89
90 // ************************************* //
91 // Stuff for calling from Python scripts //
92 // ************************************* //
93
94 PyObject *mbind(const std::string &button, ob::MouseContext context,
95 ob::MouseAction action, PyObject *func)
96 {
97 if (!PyCallable_Check(func)) {
98 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
99 return NULL;
100 }
101
102 if (!ob::Openbox::instance->bindings()->addButton(button, context,
103 action, func)) {
104 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
105 return NULL;
106 }
107 Py_INCREF(Py_None); return Py_None;
108 }
109
110 PyObject *ebind(ob::EventAction action, PyObject *func)
111 {
112 if (!PyCallable_Check(func)) {
113 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
114 return NULL;
115 }
116
117 if (!ob::Openbox::instance->bindings()->addEvent(action, func)) {
118 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
119 return NULL;
120 }
121 Py_INCREF(Py_None); return Py_None;
122 }
123
124 PyObject *kbind(PyObject *keylist, ob::KeyContext context, PyObject *func)
125 {
126 if (!PyCallable_Check(func)) {
127 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
128 return NULL;
129 }
130 if (!PyList_Check(keylist)) {
131 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
132 return NULL;
133 }
134
135 ob::OBBindings::StringVect vectkeylist;
136 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
137 PyObject *str = PyList_GetItem(keylist, i);
138 if (!PyString_Check(str)) {
139 PyErr_SetString(PyExc_TypeError,
140 "Invalid keylist. It must contain only strings.");
141 return NULL;
142 }
143 vectkeylist.push_back(PyString_AsString(str));
144 }
145
146 if (!ob::Openbox::instance->bindings()->addKey(vectkeylist, func)) {
147 PyErr_SetString(PyExc_RuntimeError,"Unable to add binding.");
148 return NULL;
149 }
150 Py_INCREF(Py_None); return Py_None;
151 }
152
153 PyObject *kunbind(PyObject *keylist, PyObject *func)
154 {
155 if (!PyList_Check(keylist)) {
156 PyErr_SetString(PyExc_TypeError, "Invalid keylist. Not a list.");
157 return NULL;
158 }
159 if (!PyCallable_Check(func)) {
160 PyErr_SetString(PyExc_TypeError, "Invalid callback function.");
161 return NULL;
162 }
163
164 ob::OBBindings::StringVect vectkeylist;
165 for (int i = 0, end = PyList_Size(keylist); i < end; ++i) {
166 PyObject *str = PyList_GetItem(keylist, i);
167 if (!PyString_Check(str)) {
168 PyErr_SetString(PyExc_TypeError,
169 "Invalid keylist. It must contain only strings.");
170 return NULL;
171 }
172 vectkeylist.push_back(PyString_AsString(str));
173 }
174
175 if (!ob::Openbox::instance->bindings()->removeKey(vectkeylist, func)) {
176 PyErr_SetString(PyExc_RuntimeError, "Could not remove callback.");
177 return NULL;
178 }
179 Py_INCREF(Py_None); return Py_None;
180 }
181
182 void kunbind_all()
183 {
184 ob::Openbox::instance->bindings()->removeAllKeys();
185 }
186
187 void set_reset_key(const std::string &key)
188 {
189 ob::Openbox::instance->bindings()->setResetKey(key);
190 }
191
192 PyObject *send_client_msg(Window target, int type, Window about,
193 long data, long data1, long data2,
194 long data3, long data4)
195 {
196 if (type < 0 || type >= otk::OBProperty::NUM_ATOMS) {
197 PyErr_SetString(PyExc_TypeError,
198 "Invalid atom type. Must be from otk::OBProperty::Atoms");
199 return NULL;
200 }
201
202 XEvent e;
203 e.xclient.type = ClientMessage;
204 e.xclient.format = 32;
205 e.xclient.message_type =
206 Openbox::instance->property()->atom((otk::OBProperty::Atoms)type);
207 e.xclient.window = about;
208 e.xclient.data.l[0] = data;
209 e.xclient.data.l[1] = data1;
210 e.xclient.data.l[2] = data2;
211 e.xclient.data.l[3] = data3;
212 e.xclient.data.l[4] = data4;
213
214 XSendEvent(otk::OBDisplay::display, target, false,
215 SubstructureRedirectMask | SubstructureNotifyMask,
216 &e);
217 Py_INCREF(Py_None); return Py_None;
218 }
219
220 }
This page took 0.043935 seconds and 5 git commands to generate.