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 #define RETURN_NONE do { Py_INCREF(Py_None); return Py_None; } while (0)
42 extern struct win
*windows
;
43 extern struct display
*display
, *displays
;
44 extern struct layer
*flayer
;
46 static PyObject
* SPy_Get(PyObject
*obj
, void *closure
);
47 static PyObject
* SPy_Set(PyObject
*obj
, PyObject
*value
, void *closure
);
48 static int PyDispatch(void *handler
, const char *params
, va_list va
);
53 struct listener
*listener
;
64 PyObject
* (*conv
)(void *);
68 #define compare_callback NULL
69 #define repr_callback NULL
71 #define REGISTER_TYPE(type, Type, closures, methods) \
73 register_##type(PyObject *module) \
75 static PyGetSetDef getsets[sizeof(closures)]; \
76 int i, count = sizeof(closures); \
77 for (i = 0; i < count; i++) \
79 getsets[i].name = closures[i].name; \
80 getsets[i].doc = closures[i].doc; \
81 getsets[i].closure = &closures[i]; \
82 getsets[i].get = SPy_Get; \
83 getsets[i].set = SPy_Set; \
85 PyType##Type.tp_getset = getsets; \
86 PyType##Type.tp_methods = methods; \
87 PyType##Type.tp_compare = compare_##type; \
88 PyType##Type.tp_repr = repr_##type; \
89 PyType_Ready(&PyType##Type); \
90 Py_INCREF(&PyType##Type); \
91 PyModule_AddObject(module, #Type, (PyObject *)&PyType##Type); \
95 #define DEFINE_TYPE(str, Type) \
102 static PyTypeObject PyType##Type = \
104 PyObject_HEAD_INIT(NULL) \
106 .tp_name = "screen." #Type, \
107 .tp_basicsize = sizeof(Py##Type), \
108 .tp_flags = Py_TPFLAGS_DEFAULT, \
109 .tp_doc = #Type " object", \
110 .tp_methods = NULL, \
115 PyObject_From##Type(str *_obj) \
117 Py##Type *obj = PyType##Type.tp_alloc(&PyType##Type, 0); \
119 return (PyObject *)obj; \
123 PyString_FromStringSafe(const char *str
)
126 return PyString_FromString(str
);
131 DEFINE_TYPE(struct win
, Window
)
133 #define SPY_CLOSURE(name, doc, type, member, func) \
134 {name, doc, type, offsetof(PyWindow, _obj), offsetof(struct win, member), func}
135 static SPyClosure wclosures
[] =
137 SPY_CLOSURE("title", "Window title", T_STRING
, w_title
, NULL
),
138 SPY_CLOSURE("number", "Window number", T_INT
, w_number
, NULL
),
139 SPY_CLOSURE("dir", "Window directory", T_STRING
, w_dir
, NULL
),
140 SPY_CLOSURE("tty", "TTY belonging to the window", T_STRING_INPLACE
, w_tty
, NULL
),
141 SPY_CLOSURE("group", "The group the window belongs to", T_OBJECT_EX
, w_group
, PyObject_FromWindow
),
142 SPY_CLOSURE("pid", "Window pid", T_INT
, w_pid
, NULL
),
147 window_select(PyObject
*self
)
149 PyWindow
*win
= self
;
150 struct win
*w
= win
->_obj
;
151 SwitchWindow(w
->w_number
);
155 static PyMethodDef wmethods
[] = {
156 {"select", (PyCFunction
)window_select
, METH_NOARGS
, "Select the window."},
161 compare_window(PyWindow
*one
, PyWindow
*two
)
163 struct win
*wone
= one
->_obj
;
164 struct win
*wtwo
= two
->_obj
;
166 return wtwo
->w_number
- wone
->w_number
;
170 repr_window(PyObject
*obj
)
173 struct win
*win
= w
->_obj
;
174 return PyString_FromFormat("window (title: %s, number: %d)", win
->w_title
, win
->w_number
);
177 REGISTER_TYPE(window
, Window
, wclosures
, wmethods
)
182 DEFINE_TYPE(struct display
, Display
)
184 #define SPY_CLOSURE(name, doc, type, member, func) \
185 {name, doc, type, offsetof(PyDisplay, _obj), offsetof(struct display, member), func}
186 static SPyClosure dclosures
[] =
188 SPY_CLOSURE("tty", "Display TTY", T_STRING_INPLACE
, d_usertty
, NULL
),
189 SPY_CLOSURE("term", "Display Term", T_STRING_INPLACE
, d_termname
, NULL
),
190 SPY_CLOSURE("fore", "Foreground window of the display", T_OBJECT_EX
, d_fore
, PyObject_FromWindow
),
191 SPY_CLOSURE("width", "Display width", T_INT
, d_width
, NULL
),
192 SPY_CLOSURE("height", "Display height", T_INT
, d_height
, NULL
),
196 static PyMethodDef dmethods
[] =
202 compare_display(PyDisplay
*one
, PyDisplay
*two
)
204 struct display
*done
= one
->_obj
;
205 struct display
*dtwo
= two
->_obj
;
206 struct display
*iter
;
208 if (done
->d_userpid
== dtwo
->d_userpid
)
211 for (iter
= displays
; iter
; iter
= iter
->d_next
)
214 else if (iter
== dtwo
)
221 repr_display(PyObject
*obj
)
224 struct display
*disp
= d
->_obj
;
225 return PyString_FromFormat("display (term: %s, tty: %s)", disp
->d_termname
, disp
->d_usertty
);
228 REGISTER_TYPE(display
, Display
, dclosures
, dmethods
)
233 DEFINE_TYPE(SPyCallback
, Callback
)
234 static SPyClosure cclosures
[] = {{NULL
}};
237 FreeCallback(SPyCallback
*scallback
)
239 Py_XDECREF(scallback
->callback
);
244 callback_unhook(PyObject
*obj
)
246 PyCallback
*cb
= obj
;
247 SPyCallback
*scallback
= cb
->_obj
;
250 unregister_listener(scallback
->listener
);
251 FreeCallback(scallback
);
256 static PyMethodDef cmethods
[] = {
257 {"unhook", (PyCFunction
)callback_unhook
, METH_NOARGS
, "Unhook this event callback."},
260 REGISTER_TYPE(callback
, Callback
, cclosures
, cmethods
)
264 SPy_Get(PyObject
*obj
, void *closure
)
266 SPyClosure
*sc
= closure
;
267 char **first
= (char *)obj
+ sc
->offset1
;
268 char **second
= (char *)*first
+ sc
->offset2
;
269 PyObject
*(*cb
)(void *) = sc
->conv
;
270 void *data
= *second
;
277 cb
= PyString_FromStringSafe
;
280 case T_STRING_INPLACE
:
281 cb
= PyString_FromStringSafe
;
294 SPy_Set(PyObject
*obj
, PyObject
*value
, void *closure
)
300 PyDispatch(void *handler
, const char *params
, va_list va
)
302 PyCallback
*callback
= handler
;
303 PyObject
*args
, *ret
;
306 SPyCallback
*scallback
= callback
->_obj
;
308 for (count
= 0, p
= params
; *p
; p
++, count
++)
311 args
= PyTuple_New(count
);
315 for (count
= 0, p
= params
; *p
; p
++, count
++)
317 PyObject
*item
= NULL
;
321 item
= PyString_FromStringSafe(va_arg(va
, char *));
325 char **ls
= va_arg(va
, char **), **iter
;
327 for (iter
= ls
; iter
&& *iter
; iter
++, c
++)
331 item
= PyTuple_New(c
);
332 for (c
= 0, iter
= ls
; iter
&& *iter
; iter
++, c
++)
333 PyTuple_SetItem(item
, c
, PyString_FromStringSafe(*iter
));
337 item
= PyInt_FromLong(va_arg(va
, int));
340 item
= PyObject_FromDisplay(va_arg(va
, struct display
*));
349 PyTuple_SetItem(args
, count
, item
);
352 ret
= PyObject_CallObject(scallback
->callback
, args
);
357 retval
= (int)PyInt_AsLong(ret
);
364 screen_display(PyObject
*self
)
370 return PyObject_FromDisplay(display
);
374 screen_displays(PyObject
*self
)
376 struct display
*d
= displays
;
378 for (; d
; d
= d
->d_next
)
380 PyObject
*tuple
= PyTuple_New(count
);
382 for (d
= displays
, count
= 0; d
; d
= d
->d_next
, ++count
)
383 PyTuple_SetItem(tuple
, count
, PyObject_FromDisplay(d
));
389 screen_windows(PyObject
*self
)
391 struct win
*w
= windows
;
393 for (; w
; w
= w
->w_next
)
395 PyObject
*tuple
= PyTuple_New(count
);
397 for (w
= windows
, count
= 0; w
; w
= w
->w_next
, ++count
)
398 PyTuple_SetItem(tuple
, count
, PyObject_FromWindow(w
));
404 hook_event(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
406 static char *kwlist
[] = {"event", "callback", NULL
};
410 struct script_event
*sev
;
412 SPyCallback
*scallback
;
414 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "sO:screen.hook", kwlist
, &name
, &callback
))
415 return NULL
; /* Return Py_None instead? */
417 if (!PyCallable_Check(callback
))
419 PyErr_SetString(PyExc_TypeError
, "The event-callback functions must be callable.");
420 LMsg(0, "The event-callback functions must be callable.");
424 sev
= object_get_event(NULL
, name
);
427 LMsg(0, "No event named '%s'", name
);
431 l
= malloc(sizeof(struct listener
));
433 scallback
= malloc(sizeof(SPyCallback
));
434 scallback
->callback
= callback
;
435 scallback
->listener
= l
;
436 Py_INCREF(scallback
->callback
);
438 l
->handler
= PyObject_FromCallback(scallback
);
440 l
->dispatcher
= PyDispatch
;
441 if (register_listener(sev
, l
))
443 Py_DECREF((PyObject
*)l
->handler
);
444 FreeCallback(scallback
);
447 LMsg(0, "Hook could not be registered.");
452 Py_INCREF((PyObject
*)l
->handler
);
457 screen_input_cb(char *buf
, int len
, char *p
)
459 PyObject
*callback
= p
;
460 PyObject
*str
= PyTuple_New(1);
461 PyTuple_SetItem(str
, 0, PyString_FromStringSafe(buf
));
462 PyObject_CallObject(callback
, str
);
468 screen_input(PyObject
*self
, PyObject
*args
, PyObject
*kw
)
470 static char *kwlist
[] = {"prompt", "callback", "value (optional)", NULL
};
471 char *prompt
, *pre
= NULL
;
474 if (!PyArg_ParseTupleAndKeywords(args
, kw
, "sO|s:screen.input", kwlist
, &prompt
, &callback
, &pre
))
476 LMsg(0, "Could not parse all the parameters to screen.input call.");
480 if (!PyCallable_Check(callback
))
482 LMsg(0, "Input callback must be a callable object.");
487 Input(prompt
, 100 /* huh? */,
488 INP_COOKED
, screen_input_cb
, callback
, 0);
492 int len
= strlen(pre
);
493 LayProcess(&pre
, &len
);
499 const PyMethodDef py_methods
[] = {
500 {"display", (PyCFunction
)screen_display
, METH_NOARGS
, "Get the current display."},
501 {"displays", (PyCFunction
)screen_displays
, METH_NOARGS
, "Get the list of displays."},
502 {"hook", (PyCFunction
)hook_event
, METH_VARARGS
|METH_KEYWORDS
, "Hook a callback to an event."},
503 {"input", (PyCFunction
)screen_input
, METH_VARARGS
|METH_KEYWORDS
, "Read user input interactively."},
504 {"windows", (PyCFunction
)screen_windows
, METH_NOARGS
, "Get the list of windows."},
505 {NULL
, NULL
, 0, NULL
}
516 m
= Py_InitModule3 ("screen", py_methods
, NULL
);
519 register_callback(m
);
532 SPySource(const char *file
, int async
)
534 FILE *f
= fopen(file
, "rb");
535 int ret
= PyRun_SimpleFile(f
, file
);
539 return 1; /* Success */
541 if (PyErr_Occurred())
550 struct binding py_binding
=