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