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