summaryrefslogtreecommitdiff
path: root/src/python.c
blob: 657a6b31880a63fddd0c8f3ccb106e9c5181a018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Python scripting support
 *
 * Copyright (c) 2009 Sadrul Habib Chowdhury (sadrul@users.sf.net)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (see the file COPYING); if not, write to the
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02111-1301  USA
 *
 ****************************************************************
 */
#include <sys/types.h>

#include "config.h"
#include "screen.h"
#include "script.h"
#include <sys/stat.h>
#include <unistd.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "extern.h"
#include "logfile.h"

#include <Python.h>
#include <structmember.h>

extern struct win *windows;

static PyObject * SPy_Get(PyObject *obj, void *closure);
static PyObject * SPy_Set(PyObject *obj, PyObject *value, void *closure);

typedef struct
{
  char *name;
  char *doc;

  int type;
  size_t offset1;
  size_t offset2;
  PyObject * (*conv)(void *);
} SPyClosure;

#define REGISTER_TYPE(type, Type, closures) \
static int \
register_##type(PyObject *module) \
{ \
  static PyGetSetDef getsets[sizeof(closures)]; \
  int i, count = sizeof(closures); \
  for (i = 0; i < count; i++) \
    { \
      getsets[i].name = closures[i].name; \
      getsets[i].doc = closures[i].doc; \
      getsets[i].closure = &closures[i]; \
      getsets[i].get = SPy_Get; \
      getsets[i].set = SPy_Set; \
    } \
  PyType##Type.tp_getset = getsets; \
  PyType_Ready(&PyType##Type); \
  Py_INCREF(&PyType##Type); \
  PyModule_AddObject(module, #Type, (PyObject *)&PyType##Type); \
  return 1; \
}

#define DEFINE_TYPE(str, Type) \
typedef struct \
{ \
  PyObject_HEAD \
  str *_obj; \
} Py##Type; \
\
static PyTypeObject PyType##Type = \
{ \
  PyObject_HEAD_INIT(NULL) \
  .ob_size = 0, \
  .tp_name = "screen." #Type, \
  .tp_basicsize = sizeof(Py##Type), \
  .tp_flags = Py_TPFLAGS_DEFAULT, \
  .tp_doc = #Type " object", \
  .tp_methods = NULL, \
  .tp_getset = NULL, \
}; \
\
static PyObject * \
PyObject_From##Type(str *_obj) \
{ \
  Py##Type *obj = PyType##Type.tp_alloc(&PyType##Type, 0); \
  obj->_obj = _obj; \
  return (PyObject *)obj; \
}

static PyObject *
PyString_FromStringSafe(const char *str)
{
  if (str)
    return PyString_FromString(str);
  Py_INCREF(Py_None);
  return Py_None;
}

/** Window {{{ */
DEFINE_TYPE(struct win, Window)

#define SPY_CLOSURE(name, doc, type, member, func) \
    {name, doc, type, offsetof(PyWindow, _obj), offsetof(struct win, member), func}
static SPyClosure wclosures[] =
{
  SPY_CLOSURE("title", "Window title", T_STRING, w_title, NULL),
  SPY_CLOSURE("number", "Window number", T_INT, w_number, NULL),
  SPY_CLOSURE("dir", "Window directory", T_STRING, w_dir, NULL),
  SPY_CLOSURE("tty", "TTY belonging to the window", T_STRING_INPLACE, w_tty, NULL),
  SPY_CLOSURE("group", "The group the window belongs to", T_OBJECT_EX, w_group, PyObject_FromWindow),
  SPY_CLOSURE("pid", "Window pid", T_INT, w_pid, NULL),
  {NULL}
};
REGISTER_TYPE(window, Window, wclosures)
#undef SPY_CLOSURE

/** }}} */

static PyObject *
SPy_Get(PyObject *obj, void *closure)
{
  SPyClosure *sc = closure;
  char **first = (char *)obj + sc->offset1;
  char **second = (char *)*first + sc->offset2;
  PyObject *(*cb)(void *) = sc->conv;
  void *data = *second;

  if (!cb)
    {
      switch (sc->type)
	{
	  case T_STRING:
	    cb = PyString_FromStringSafe;
	    data = *second;
	    break;
	  case T_STRING_INPLACE:
	    cb = PyString_FromStringSafe;
	    data = second;
	    break;
	  case T_INT:
	    cb = PyInt_FromLong;
	    data = *second;
	    break;
	}
    }
  return cb(data);
}

static PyObject *
SPy_Set(PyObject *obj, PyObject *value, void *closure)
{
  return NULL;
}

static PyObject *
screen_windows(PyObject *self)
{
  struct win *w = windows;
  int count = 0;
  for (; w; w = w->w_next)
    ++count;
  PyObject *tuple = PyTuple_New(count);

  for (w = windows, count = 0; w; w = w->w_next, ++count)
    PyTuple_SetItem(tuple, count, PyObject_FromWindow(w));

  return tuple;
}

const PyMethodDef py_methods[] = {
  {"windows", (PyCFunction)screen_windows, METH_NOARGS, NULL},
  {NULL, NULL, 0, NULL}
};

static int
SPyInit(void)
{
  PyObject *m;

  Py_Initialize();

  m = Py_InitModule3 ("screen", py_methods, NULL);
  register_window(m);

  return 0;
}

static int
SPyFinit(void)
{
  /*Py_Finalize(); // Crashes -- why? */
  return 0;
}

static int
SPySource(const char *file, int async)
{
  FILE *f = fopen(file, "rb");
  int ret = PyRun_SimpleFile(f, file);
  fclose(f);

  if (ret == 0)
      return 1; /* Success */

  if (PyErr_Occurred())
    {
      PyErr_Print();
      return 0;
    }

  return 1;
}

struct binding py_binding =
{
  "python",
  0,
  0,
  SPyInit,
  SPyFinit,
  0,
  SPySource,
  0,
  0
};