Expose some of the 'window' structure.
[screen-lua.git] / src / python.c
blobde8aec9d9252b52234bc5495c06611b4c50a4133
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 /** Window {{{ */
43 typedef struct
45 PyObject_HEAD
46 struct win w;
47 } PyWindow;
49 static PyMemberDef py_window_members[] =
51 {"title", T_STRING, offsetof(PyWindow, w) + offsetof(struct win, w_title), 0, "Title of the window"},
52 {"number", T_INT, offsetof(PyWindow, w) + offsetof(struct win, w_number), 0, "The window number"},
53 {NULL}
56 static PyTypeObject PyWindowType =
58 PyObject_HEAD_INIT(NULL)
59 .ob_size = 0,
60 .tp_name = "screen.window",
61 .tp_basicsize = sizeof(PyWindow),
62 .tp_flags = Py_TPFLAGS_DEFAULT,
63 .tp_doc = "Window object",
64 .tp_methods = NULL,
65 .tp_members = py_window_members
68 static PyObject *
69 PyWindow_FromWindow(struct win *w)
71 PyWindow *obj = PyWindowType.tp_alloc(&PyWindowType, 0);
72 obj->w = *w;
73 return (PyObject *)obj;
76 /** }}} */
78 static PyObject *
79 screen_windows(PyObject *self)
81 struct win *w = windows;
82 int count = 0;
83 for (; w; w = w->w_next)
84 ++count;
85 PyObject *tuple = PyTuple_New(count);
87 for (w = windows, count = 0; w; w = w->w_next, ++count)
89 PyTuple_SetItem(tuple, count, PyWindow_FromWindow(w));
92 return tuple;
95 const PyMethodDef py_methods[] = {
96 {"windows", (PyCFunction)screen_windows, METH_NOARGS, NULL},
97 {NULL, NULL, 0, NULL}
100 static int
101 SPyInit(void)
103 PyObject *m;
105 Py_Initialize();
107 PyType_Ready(&PyWindowType);
109 m = Py_InitModule3 ("screen", py_methods, NULL);
111 Py_INCREF(&PyWindowType);
112 PyModule_AddObject(m, "Window", (PyObject *)&PyWindowType);
113 return 0;
116 static int
117 SPyFinit(void)
119 /*Py_Finalize(); // Crashes -- why? */
120 return 0;
123 static int
124 SPySource(const char *file, int async)
126 FILE *f = fopen(file, "rb");
127 int ret = PyRun_SimpleFile(f, file);
128 fclose(f);
130 if (ret == 0)
131 return 1; /* Success */
133 if (PyErr_Occurred())
135 PyErr_Print();
136 return 0;
139 return 1;
142 struct binding py_binding =
144 "python",
147 SPyInit,
148 SPyFinit,
150 SPySource,