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)
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>
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
);
53 PyObject
* (*conv
)(void *);
56 #define REGISTER_TYPE(type, Type, closures) \
58 register_##type(PyObject *module) \
60 static PyGetSetDef getsets[sizeof(closures)]; \
61 int i, count = sizeof(closures); \
62 for (i = 0; i < count; i++) \
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; \
70 PyType##Type.tp_getset = getsets; \
71 PyType_Ready(&PyType##Type); \
72 Py_INCREF(&PyType##Type); \
73 PyModule_AddObject(module, #Type, (PyObject *)&PyType##Type); \
84 static PyTypeObject PyTypeWindow
=
86 PyObject_HEAD_INIT(NULL
)
88 .tp_name
= "screen.window",
89 .tp_basicsize
= sizeof(PyWindow
),
90 .tp_flags
= Py_TPFLAGS_DEFAULT
,
91 .tp_doc
= "Window object",
96 #define SPY_CLOSURE(name, doc, type, member, func) \
97 {name, doc, type, offsetof(PyWindow, _obj), offsetof(struct win, member), func}
98 static SPyClosure wclosures
[] =
100 SPY_CLOSURE("title", "Window title", T_STRING
, w_title
, PyString_FromString
),
101 SPY_CLOSURE("number", "Window number", T_INT
, w_number
, PyInt_FromLong
),
104 REGISTER_TYPE(window
, Window
, wclosures
)
108 PyWindow_FromWindow(struct win
*w
)
110 PyWindow
*obj
= PyTypeWindow
.tp_alloc(&PyTypeWindow
, 0);
112 return (PyObject
*)obj
;
118 SPy_Get(PyObject
*obj
, void *closure
)
120 SPyClosure
*sc
= closure
;
121 char **first
= (char *)obj
+ sc
->offset1
;
122 char **second
= (char *)*first
+ sc
->offset2
;
123 return sc
->conv(*second
);
127 SPy_Set(PyObject
*obj
, PyObject
*value
, void *closure
)
133 screen_windows(PyObject
*self
)
135 struct win
*w
= windows
;
137 for (; w
; w
= w
->w_next
)
139 PyObject
*tuple
= PyTuple_New(count
);
141 for (w
= windows
, count
= 0; w
; w
= w
->w_next
, ++count
)
142 PyTuple_SetItem(tuple
, count
, PyWindow_FromWindow(w
));
147 const PyMethodDef py_methods
[] = {
148 {"windows", (PyCFunction
)screen_windows
, METH_NOARGS
, NULL
},
149 {NULL
, NULL
, 0, NULL
}
159 m
= Py_InitModule3 ("screen", py_methods
, NULL
);
168 /*Py_Finalize(); // Crashes -- why? */
173 SPySource(const char *file
, int async
)
175 FILE *f
= fopen(file
, "rb");
176 int ret
= PyRun_SimpleFile(f
, file
);
180 return 1; /* Success */
182 if (PyErr_Occurred())
191 struct binding py_binding
=