Add minimalistic python scripting support.
[screen-lua.git] / src / python.c
blob3cd58fe690334bb2e22a7367380d8cd9fcc65674
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>
39 extern struct win *windows;
41 static PyObject *
42 screen_windows(PyObject *self)
44 struct win *w = windows;
45 int count = 0;
46 for (; w; w = w->w_next)
47 ++count;
48 PyObject *tuple = PyTuple_New(count);
50 for (w = windows, count = 0; w; w = w->w_next, ++count)
52 PyObject *name = PyString_FromString(w->w_title);
53 PyTuple_SetItem(tuple, count, name);
56 return tuple;
59 const PyMethodDef py_methods[] = {
60 {"windows", (PyCFunction)screen_windows, METH_NOARGS, NULL},
61 {NULL, NULL, 0, NULL}
64 static int
65 SPyInit(void)
67 Py_Initialize();
68 Py_InitModule3 ("screen", py_methods, NULL);
69 return 0;
72 static int
73 SPyFinit(void)
75 Py_Finalize();
76 return 0;
79 static int
80 SPySource(const char *file, int async)
82 FILE *f = fopen(file, "rb");
83 int ret = PyRun_SimpleFile(f, file);
84 fclose(f);
86 if (ret == 0)
87 return 1; /* Success */
89 if (PyErr_Occurred())
91 PyErr_Print();
92 return 0;
95 return 1;
98 struct binding py_binding =
100 "python",
103 SPyInit,
104 SPyFinit,
106 SPySource,