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