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); \
77 #define DEFINE_TYPE(str, Type) \
84 static PyTypeObject PyType##Type = \
86 PyObject_HEAD_INIT(NULL) \
88 .tp_name = "screen." #Type, \
89 .tp_basicsize = sizeof(Py##Type), \
90 .tp_flags = Py_TPFLAGS_DEFAULT, \
91 .tp_doc = #Type " object", \
97 PyObject_From##Type(str *_obj) \
99 Py##Type *obj = PyType##Type.tp_alloc(&PyType##Type, 0); \
101 return (PyObject *)obj; \
105 PyString_FromStringSafe(const char *str
)
108 return PyString_FromString(str
);
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
),
128 REGISTER_TYPE(window
, Window
, wclosures
)
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
;
147 cb
= PyString_FromStringSafe
;
150 case T_STRING_INPLACE
:
151 cb
= PyString_FromStringSafe
;
164 SPy_Set(PyObject
*obj
, PyObject
*value
, void *closure
)
170 screen_windows(PyObject
*self
)
172 struct win
*w
= windows
;
174 for (; w
; w
= w
->w_next
)
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
));
184 const PyMethodDef py_methods
[] = {
185 {"windows", (PyCFunction
)screen_windows
, METH_NOARGS
, NULL
},
186 {NULL
, NULL
, 0, NULL
}
196 m
= Py_InitModule3 ("screen", py_methods
, NULL
);
210 SPySource(const char *file
, int async
)
212 FILE *f
= fopen(file
, "rb");
213 int ret
= PyRun_SimpleFile(f
, file
);
217 return 1; /* Success */
219 if (PyErr_Occurred())
228 struct binding py_binding
=