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