No more crashy crashy\!
[screen-lua.git] / src / python.c
blob949847457258dd45224d534508ebae737f07d8ad
1 /* Python scripting support
3 * Copyright (c) 2009 Sadrul Habib Chowdhury (sadrul@users.sf.net)
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program (see the file COPYING); if not, write to the
17 * Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
20 ****************************************************************
22 #include <sys/types.h>
24 #include "config.h"
25 #include "screen.h"
26 #include "script.h"
27 #include <sys/stat.h>
28 #include <unistd.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <stdlib.h>
34 #include "extern.h"
35 #include "logfile.h"
37 #include <Python.h>
38 #include <structmember.h>
40 extern struct win *windows;
42 static PyObject * SPy_Get(PyObject *obj, void *closure);
43 static PyObject * SPy_Set(PyObject *obj, PyObject *value, void *closure);
45 typedef struct
47 char *name;
48 char *doc;
50 int type;
51 size_t offset1;
52 size_t offset2;
53 PyObject * (*conv)(void *);
54 } SPyClosure;
56 #define REGISTER_TYPE(type, Type, closures) \
57 static int \
58 register_##type(PyObject *module) \
59 { \
60 static PyGetSetDef getsets[sizeof(closures)]; \
61 int i, count = sizeof(closures); \
62 for (i = 0; i < count; i++) \
63 { \
64 getsets[i].name = closures[i].name; \
65 getsets[i].doc = closures[i].doc; \
66 getsets[i].closure = &closures[i]; \
67 getsets[i].get = SPy_Get; \
68 getsets[i].set = SPy_Set; \
69 } \
70 PyType##Type.tp_getset = getsets; \
71 PyType_Ready(&PyType##Type); \
72 Py_INCREF(&PyType##Type); \
73 PyModule_AddObject(module, #Type, (PyObject *)&PyType##Type); \
74 return 1; \
77 #define DEFINE_TYPE(str, Type) \
78 typedef struct \
79 { \
80 PyObject_HEAD \
81 str *_obj; \
82 } Py##Type; \
84 static PyTypeObject PyType##Type = \
85 { \
86 PyObject_HEAD_INIT(NULL) \
87 .ob_size = 0, \
88 .tp_name = "screen." #Type, \
89 .tp_basicsize = sizeof(Py##Type), \
90 .tp_flags = Py_TPFLAGS_DEFAULT, \
91 .tp_doc = #Type " object", \
92 .tp_methods = NULL, \
93 .tp_getset = NULL, \
94 }; \
96 static PyObject * \
97 PyObject_From##Type(str *_obj) \
98 { \
99 Py##Type *obj = PyType##Type.tp_alloc(&PyType##Type, 0); \
100 obj->_obj = _obj; \
101 return (PyObject *)obj; \
104 static PyObject *
105 PyString_FromStringSafe(const char *str)
107 if (str)
108 return PyString_FromString(str);
109 Py_INCREF(Py_None);
110 return Py_None;
113 /** Window {{{ */
114 DEFINE_TYPE(struct win, Window)
116 #define SPY_CLOSURE(name, doc, type, member, func) \
117 {name, doc, type, offsetof(PyWindow, _obj), offsetof(struct win, member), func}
118 static SPyClosure wclosures[] =
120 SPY_CLOSURE("title", "Window title", T_STRING, w_title, NULL),
121 SPY_CLOSURE("number", "Window number", T_INT, w_number, NULL),
122 SPY_CLOSURE("dir", "Window directory", T_STRING, w_dir, NULL),
123 SPY_CLOSURE("tty", "TTY belonging to the window", T_STRING_INPLACE, w_tty, NULL),
124 SPY_CLOSURE("group", "The group the window belongs to", T_OBJECT_EX, w_group, PyObject_FromWindow),
125 SPY_CLOSURE("pid", "Window pid", T_INT, w_pid, NULL),
126 {NULL}
128 REGISTER_TYPE(window, Window, wclosures)
129 #undef SPY_CLOSURE
131 /** }}} */
133 static PyObject *
134 SPy_Get(PyObject *obj, void *closure)
136 SPyClosure *sc = closure;
137 char **first = (char *)obj + sc->offset1;
138 char **second = (char *)*first + sc->offset2;
139 PyObject *(*cb)(void *) = sc->conv;
140 void *data = *second;
142 if (!cb)
144 switch (sc->type)
146 case T_STRING:
147 cb = PyString_FromStringSafe;
148 data = *second;
149 break;
150 case T_STRING_INPLACE:
151 cb = PyString_FromStringSafe;
152 data = second;
153 break;
154 case T_INT:
155 cb = PyInt_FromLong;
156 data = *second;
157 break;
160 return cb(data);
163 static PyObject *
164 SPy_Set(PyObject *obj, PyObject *value, void *closure)
166 return NULL;
169 static PyObject *
170 screen_windows(PyObject *self)
172 struct win *w = windows;
173 int count = 0;
174 for (; w; w = w->w_next)
175 ++count;
176 PyObject *tuple = PyTuple_New(count);
178 for (w = windows, count = 0; w; w = w->w_next, ++count)
179 PyTuple_SetItem(tuple, count, PyObject_FromWindow(w));
181 return tuple;
184 const PyMethodDef py_methods[] = {
185 {"windows", (PyCFunction)screen_windows, METH_NOARGS, NULL},
186 {NULL, NULL, 0, NULL}
189 static int
190 SPyInit(void)
192 PyObject *m;
194 Py_Initialize();
196 m = Py_InitModule3 ("screen", py_methods, NULL);
197 register_window(m);
199 return 0;
202 static int
203 SPyFinit(void)
205 Py_Finalize();
206 return 0;
209 static int
210 SPySource(const char *file, int async)
212 FILE *f = fopen(file, "rb");
213 int ret = PyRun_SimpleFile(f, file);
214 fclose(f);
216 if (ret == 0)
217 return 1; /* Success */
219 if (PyErr_Occurred())
221 PyErr_Print();
222 return 0;
225 return 1;
228 struct binding py_binding =
230 "python",
233 SPyInit,
234 SPyFinit,
236 SPySource,