3
$Id: _pyosd.c,v 1.1.1.1 2002/06/01 13:43:13 resolve Exp $
5
pyosd - a wrapper of libxosd which allows the displaying of "on screen display"
9
Copyright (C) 2001, Damien Elmes <resolve@repose.cx>.
11
This file is licensed under the GPL. Please see the file COPYING
14
To compile this file, you will need libxosd. I'm waiting for a response from
15
the author about some patches, so for now use the version included with this
24
// raised if there's an error in the underlying library (such as X not running)
25
static PyObject *pyosd_error;
28
static PyObject *pyosd_init(PyObject *self, PyObject *args);
29
static PyObject *pyosd_deinit(PyObject *self, PyObject *args);
30
static PyObject *pyosd_display_string(PyObject *self, PyObject *args);
31
static PyObject *pyosd_display_perc(PyObject *self, PyObject *args);
32
static PyObject *pyosd_display_slider(PyObject *self, PyObject *args);
33
static PyObject *pyosd_set_colour(PyObject *self, PyObject *args);
34
static PyObject *pyosd_set_font(PyObject *self, PyObject *args);
35
static PyObject *pyosd_set_timeout(PyObject *self, PyObject *args);
36
static PyObject *pyosd_set_pos(PyObject *self, PyObject *args);
37
static PyObject *pyosd_set_offset(PyObject *self, PyObject *args);
38
static PyObject *pyosd_set_shadow_offset(PyObject *self, PyObject *args);
40
static PyMethodDef pyosd_methods[] = {
41
{"init", pyosd_init, METH_VARARGS},
42
{"deinit", pyosd_deinit, METH_VARARGS},
43
{"display_string", pyosd_display_string, METH_VARARGS},
44
{"display_perc", pyosd_display_perc, METH_VARARGS},
45
{"display_slider", pyosd_display_slider, METH_VARARGS},
46
{"set_font", pyosd_set_font, METH_VARARGS},
47
{"set_colour", pyosd_set_colour, METH_VARARGS},
48
{"set_timeout", pyosd_set_timeout, METH_VARARGS},
49
{"set_pos", pyosd_set_pos, METH_VARARGS},
50
{"set_offset", pyosd_set_offset, METH_VARARGS},
51
{"set_shadow_offset", pyosd_set_shadow_offset, METH_VARARGS},
61
// create the module and add the functions
62
self = Py_InitModule("_pyosd", pyosd_methods);
64
// init custom exception
65
dict = PyModule_GetDict(self);
67
pyosd_error = PyErr_NewException("pyosd.error", NULL, NULL);
68
PyDict_SetItemString(dict, "error", pyosd_error);
71
////////////////////////////////////////////////////////////////////////
74
// check to see that osd is a valid pointer. if it's not, raise an error
75
// and return return 0
76
static int assert_osd(xosd *osd, char *error)
79
PyErr_SetString(pyosd_error, error);
86
// pyosd_init(fontdesc, colour, timeout, xosd_pos, offset, shadow)
87
// initialise the osd interface
89
pyosd_init(PyObject *self, PyObject *args)
91
char *fontdesc, *colour;
92
int timeout, offset, shadow, pos, lines;
99
if(!PyArg_ParseTuple(args, "ssiiiii", &fontdesc, &colour,
100
&timeout, &pos, &offset, &shadow, &lines))
106
osd_pos = XOSD_bottom;
108
PyErr_SetString(PyExc_ValueError, "OSD position not in range");
112
// right, everything appeared to be successful so we can continue
113
// and load xosd. xosd uses threads, though, which means we must make
114
// python thread safe
117
pyosd_deinit(NULL, NULL);
119
// due to an unfortunate interaction with readline, we have to ensure
120
// that signals are disabled while calling xosd_init - this stops the
121
// threads it spawns from accepting SIGINT, and tripping up python
123
sigemptyset(&newset);
124
sigaddset(&newset, SIGINT);
126
sigprocmask(SIG_BLOCK, &newset, NULL);
128
osd = xosd_init(fontdesc, colour, timeout, osd_pos, offset, shadow, lines);
130
// turn SIGINT back on for the main app
131
sigprocmask(SIG_UNBLOCK, &newset, NULL);
134
// we don't use assert_osd here, because we want to pass back the error
135
// from the underlying code
136
PyErr_SetString(pyosd_error, xosd_error);
140
// we've now got a osd reference, which we need to package up and return
141
// to the surrounding python code
143
pyc_osd = PyCObject_FromVoidPtr((void *)osd, NULL);
149
pyosd_deinit(PyObject *self, PyObject *args)
154
if(!PyArg_ParseTuple(args, "O", &pyc_osd))
157
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
160
PyErr_SetString(pyosd_error, "Already deinitialised");
164
// deinit - the wrapping python should clear out the
165
// cobject as well, as it's no longer valid.
174
// pyosd_display_string(line, string)
176
pyosd_display_string(PyObject *self, PyObject *args)
183
if(!PyArg_ParseTuple(args, "Ois", &pyc_osd, &line, &str))
186
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
188
if(!assert_osd(osd, "Run init() first!"))
191
xosd_display(osd, line, XOSD_string, str);
198
// pyosd_display_perc(line, percentage)
200
pyosd_display_perc(PyObject *self, PyObject *args)
207
if(!PyArg_ParseTuple(args, "Oii", &pyc_osd, &line, &perc))
210
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
212
if(!assert_osd(osd, "Run init() first!"))
215
xosd_display(osd, line, XOSD_percentage, perc);
221
// pyosd_display_slider(line, slider)
223
pyosd_display_slider(PyObject *self, PyObject *args)
230
if(!PyArg_ParseTuple(args, "Oii", &pyc_osd, &line, &slider))
233
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
235
if(!assert_osd(osd, "Run init() first!"))
238
xosd_display(osd, line, XOSD_slider, slider);
245
pyosd_set_font(PyObject *self, PyObject *args)
252
if(!PyArg_ParseTuple(args, "Os", &pyc_osd, &font))
255
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
257
if(!assert_osd(osd, "Run init() first!"))
260
res = xosd_set_font(osd, font);
263
PyErr_SetString(pyosd_error, xosd_error);
272
pyosd_set_colour(PyObject *self, PyObject *args)
278
if(!PyArg_ParseTuple(args, "Os", &pyc_osd, &colour))
281
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
283
if(!assert_osd(osd, "Run init() first!"))
286
xosd_set_colour(osd, colour);
293
pyosd_set_timeout(PyObject *self, PyObject *args)
299
if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &timeout))
302
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
304
if(!assert_osd(osd, "Run init() first!"))
307
xosd_set_timeout(osd, timeout);
314
pyosd_set_pos(PyObject *self, PyObject *args)
322
if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &pos))
325
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
327
if(!assert_osd(osd, "Run init() first!"))
333
osd_pos = XOSD_bottom;
335
PyErr_SetString(PyExc_ValueError, "OSD position not in range");
339
xosd_set_pos(osd, pos);
346
pyosd_set_offset(PyObject *self, PyObject *args)
352
if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &offset))
355
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
357
if(!assert_osd(osd, "Run init() first!"))
360
xosd_set_vertical_offset(osd, offset);
367
pyosd_set_shadow_offset(PyObject *self, PyObject *args)
373
if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &offset))
376
osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
378
if(!assert_osd(osd, "Run init() first!"))
381
xosd_set_shadow_offset(osd, offset);