From: Sadrul Habib Chowdhury Date: Thu, 11 Jun 2009 20:58:18 +0000 (-0400) Subject: Expose some of the 'window' structure. X-Git-Url: https://repo.or.cz/w/screen-lua.git/commitdiff_plain/679f3bfbe0df80a2495a57f5315b3f0a7146efa2 Expose some of the 'window' structure. For now, the 'number' and 'title' attributes of the 'window' structure are exposed. --- diff --git a/src/python.c b/src/python.c index 3cd58fe..de8aec9 100644 --- a/src/python.c +++ b/src/python.c @@ -35,9 +35,46 @@ #include "logfile.h" #include +#include extern struct win *windows; +/** Window {{{ */ +typedef struct +{ + PyObject_HEAD + struct win w; +} PyWindow; + +static PyMemberDef py_window_members[] = +{ + {"title", T_STRING, offsetof(PyWindow, w) + offsetof(struct win, w_title), 0, "Title of the window"}, + {"number", T_INT, offsetof(PyWindow, w) + offsetof(struct win, w_number), 0, "The window number"}, + {NULL} +}; + +static PyTypeObject PyWindowType = +{ + PyObject_HEAD_INIT(NULL) + .ob_size = 0, + .tp_name = "screen.window", + .tp_basicsize = sizeof(PyWindow), + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_doc = "Window object", + .tp_methods = NULL, + .tp_members = py_window_members +}; + +static PyObject * +PyWindow_FromWindow(struct win *w) +{ + PyWindow *obj = PyWindowType.tp_alloc(&PyWindowType, 0); + obj->w = *w; + return (PyObject *)obj; +} + +/** }}} */ + static PyObject * screen_windows(PyObject *self) { @@ -49,8 +86,7 @@ screen_windows(PyObject *self) for (w = windows, count = 0; w; w = w->w_next, ++count) { - PyObject *name = PyString_FromString(w->w_title); - PyTuple_SetItem(tuple, count, name); + PyTuple_SetItem(tuple, count, PyWindow_FromWindow(w)); } return tuple; @@ -64,15 +100,23 @@ const PyMethodDef py_methods[] = { static int SPyInit(void) { + PyObject *m; + Py_Initialize(); - Py_InitModule3 ("screen", py_methods, NULL); + + PyType_Ready(&PyWindowType); + + m = Py_InitModule3 ("screen", py_methods, NULL); + + Py_INCREF(&PyWindowType); + PyModule_AddObject(m, "Window", (PyObject *)&PyWindowType); return 0; } static int SPyFinit(void) { - Py_Finalize(); + /*Py_Finalize(); // Crashes -- why? */ return 0; }