~ubuntu-branches/ubuntu/hoary/python-osd/hoary

« back to all changes in this revision

Viewing changes to _pyosd.c

  • Committer: Bazaar Package Importer
  • Author(s): Fabio M. Di Nitto
  • Date: 2004-07-01 11:56:49 UTC
  • Revision ID: james.westby@ubuntu.com-20040701115649-i14ewykgd0vmt4ap
Tags: upstream-0.2.6
ImportĀ upstreamĀ versionĀ 0.2.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
 $Id: _pyosd.c,v 1.1.1.1 2002/06/01 13:43:13 resolve Exp $
 
4
  
 
5
 pyosd - a wrapper of libxosd which allows the displaying of "on screen display"
 
6
         messages.
 
7
 
 
8
 Started 7/12/01.
 
9
 Copyright (C) 2001, Damien Elmes <resolve@repose.cx>.
 
10
 
 
11
 This file is licensed under the GPL. Please see the file COPYING
 
12
 for more details.
 
13
 
 
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
 
16
 distribution.
 
17
 
 
18
*/
 
19
 
 
20
#include <signal.h>
 
21
#include <Python.h>
 
22
#include <xosd.h>
 
23
 
 
24
// raised if there's an error in the underlying library (such as X not running)
 
25
static PyObject *pyosd_error;
 
26
 
 
27
// lazy prototypes
 
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);
 
39
 
 
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},
 
52
    {NULL,  NULL}
 
53
};
 
54
 
 
55
void
 
56
init_pyosd(void)
 
57
{
 
58
  PyObject *self;
 
59
  PyObject *dict;
 
60
 
 
61
  // create the module and add the functions
 
62
  self = Py_InitModule("_pyosd", pyosd_methods);
 
63
 
 
64
  // init custom exception
 
65
  dict = PyModule_GetDict(self);
 
66
 
 
67
  pyosd_error = PyErr_NewException("pyosd.error", NULL, NULL);
 
68
  PyDict_SetItemString(dict, "error", pyosd_error);
 
69
}
 
70
 
 
71
////////////////////////////////////////////////////////////////////////
 
72
 
 
73
 
 
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)
 
77
{
 
78
  if (!osd) {
 
79
    PyErr_SetString(pyosd_error, error);
 
80
    return 0;
 
81
  }
 
82
 
 
83
  return 1;
 
84
}
 
85
    
 
86
// pyosd_init(fontdesc, colour, timeout, xosd_pos, offset, shadow)
 
87
// initialise the osd interface
 
88
static PyObject *
 
89
pyosd_init(PyObject *self, PyObject *args)
 
90
{
 
91
  char *fontdesc, *colour;
 
92
  int timeout, offset, shadow, pos, lines;
 
93
  xosd_pos osd_pos;
 
94
  PyObject *pyc_osd;
 
95
  xosd *osd = NULL;
 
96
 
 
97
  sigset_t newset;
 
98
 
 
99
  if(!PyArg_ParseTuple(args, "ssiiiii", &fontdesc, &colour,
 
100
                         &timeout, &pos, &offset, &shadow, &lines))
 
101
    return NULL;
 
102
 
 
103
  if (pos==0)
 
104
    osd_pos = XOSD_top;
 
105
  else if (pos==1)
 
106
    osd_pos = XOSD_bottom;
 
107
  else {
 
108
    PyErr_SetString(PyExc_ValueError, "OSD position not in range");
 
109
    return NULL;
 
110
  }
 
111
 
 
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
 
115
 
 
116
  if (osd)
 
117
    pyosd_deinit(NULL, NULL);
 
118
 
 
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
 
122
 
 
123
  sigemptyset(&newset); 
 
124
  sigaddset(&newset, SIGINT);
 
125
 
 
126
  sigprocmask(SIG_BLOCK, &newset, NULL);
 
127
 
 
128
  osd = xosd_init(fontdesc, colour, timeout, osd_pos, offset, shadow, lines);
 
129
 
 
130
  // turn SIGINT back on for the main app
 
131
  sigprocmask(SIG_UNBLOCK, &newset, NULL);
 
132
 
 
133
  if(!osd) {
 
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);
 
137
    return NULL;
 
138
  }
 
139
 
 
140
  // we've now got a osd reference, which we need to package up and return
 
141
  // to the surrounding python code
 
142
 
 
143
  pyc_osd = PyCObject_FromVoidPtr((void *)osd, NULL);
 
144
 
 
145
  return pyc_osd;
 
146
}
 
147
 
 
148
static PyObject *
 
149
pyosd_deinit(PyObject *self, PyObject *args)
 
150
{
 
151
  PyObject *pyc_osd;
 
152
  xosd *osd;
 
153
 
 
154
  if(!PyArg_ParseTuple(args, "O", &pyc_osd))
 
155
    return NULL;
 
156
 
 
157
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
158
 
 
159
  if(osd==NULL) {
 
160
    PyErr_SetString(pyosd_error, "Already deinitialised");
 
161
    return NULL;
 
162
  }
 
163
 
 
164
  // deinit - the wrapping python should clear out the
 
165
  // cobject as well, as it's no longer valid.
 
166
  xosd_uninit(osd);
 
167
  osd = NULL;
 
168
 
 
169
  Py_INCREF(Py_None);
 
170
  return Py_None;
 
171
}
 
172
 
 
173
 
 
174
// pyosd_display_string(line, string)
 
175
static PyObject *
 
176
pyosd_display_string(PyObject *self, PyObject *args)
 
177
{
 
178
  int line;
 
179
  char *str;
 
180
  PyObject *pyc_osd;
 
181
  xosd *osd;
 
182
 
 
183
  if(!PyArg_ParseTuple(args, "Ois", &pyc_osd, &line, &str))
 
184
    return NULL;
 
185
 
 
186
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
187
 
 
188
  if(!assert_osd(osd, "Run init() first!"))
 
189
    return NULL;
 
190
 
 
191
  xosd_display(osd, line, XOSD_string, str);
 
192
 
 
193
  Py_INCREF(Py_None);
 
194
  return Py_None;
 
195
}
 
196
 
 
197
// FIXME
 
198
// pyosd_display_perc(line, percentage)
 
199
static PyObject *
 
200
pyosd_display_perc(PyObject *self, PyObject *args)
 
201
{
 
202
  int line;
 
203
  int perc;
 
204
  PyObject *pyc_osd;
 
205
  xosd *osd;  
 
206
 
 
207
  if(!PyArg_ParseTuple(args, "Oii", &pyc_osd, &line, &perc))
 
208
    return NULL;
 
209
 
 
210
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
211
 
 
212
  if(!assert_osd(osd, "Run init() first!"))
 
213
    return NULL;
 
214
 
 
215
  xosd_display(osd, line, XOSD_percentage, perc);
 
216
 
 
217
  Py_INCREF(Py_None);
 
218
  return Py_None;
 
219
}
 
220
 
 
221
// pyosd_display_slider(line, slider)
 
222
static PyObject *
 
223
pyosd_display_slider(PyObject *self, PyObject *args)
 
224
{
 
225
  int line;
 
226
  int slider;
 
227
  PyObject *pyc_osd;
 
228
  xosd *osd;  
 
229
 
 
230
  if(!PyArg_ParseTuple(args, "Oii", &pyc_osd, &line, &slider))
 
231
    return NULL;
 
232
 
 
233
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
234
 
 
235
  if(!assert_osd(osd, "Run init() first!"))
 
236
    return NULL;
 
237
 
 
238
  xosd_display(osd, line, XOSD_slider, slider);
 
239
 
 
240
  Py_INCREF(Py_None);
 
241
  return Py_None;
 
242
}
 
243
 
 
244
static PyObject *
 
245
pyosd_set_font(PyObject *self, PyObject *args)
 
246
{
 
247
  char *font;
 
248
  int res;
 
249
  PyObject *pyc_osd;
 
250
  xosd *osd;  
 
251
 
 
252
  if(!PyArg_ParseTuple(args, "Os", &pyc_osd, &font))
 
253
    return NULL;
 
254
 
 
255
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
256
 
 
257
  if(!assert_osd(osd, "Run init() first!"))
 
258
    return NULL;
 
259
 
 
260
  res = xosd_set_font(osd, font);
 
261
 
 
262
  if(res==-1) {
 
263
    PyErr_SetString(pyosd_error, xosd_error);
 
264
    return NULL;
 
265
  }
 
266
 
 
267
  Py_INCREF(Py_None);
 
268
  return Py_None;
 
269
}
 
270
 
 
271
static PyObject *
 
272
pyosd_set_colour(PyObject *self, PyObject *args)
 
273
{
 
274
  char *colour;
 
275
  PyObject *pyc_osd;
 
276
  xosd *osd;  
 
277
 
 
278
  if(!PyArg_ParseTuple(args, "Os", &pyc_osd, &colour))
 
279
    return NULL;
 
280
 
 
281
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
282
 
 
283
  if(!assert_osd(osd, "Run init() first!"))
 
284
    return NULL;
 
285
 
 
286
  xosd_set_colour(osd, colour);
 
287
 
 
288
  Py_INCREF(Py_None);
 
289
  return Py_None;
 
290
}
 
291
 
 
292
static PyObject *
 
293
pyosd_set_timeout(PyObject *self, PyObject *args)
 
294
{
 
295
  int timeout;
 
296
  PyObject *pyc_osd;
 
297
  xosd *osd;  
 
298
 
 
299
  if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &timeout))
 
300
    return NULL;
 
301
 
 
302
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
303
 
 
304
  if(!assert_osd(osd, "Run init() first!"))
 
305
    return NULL;
 
306
 
 
307
  xosd_set_timeout(osd, timeout);
 
308
 
 
309
  Py_INCREF(Py_None);
 
310
  return Py_None;
 
311
}
 
312
 
 
313
static PyObject *
 
314
pyosd_set_pos(PyObject *self, PyObject *args)
 
315
{
 
316
  int pos;
 
317
  PyObject *pyc_osd;
 
318
  xosd *osd;  
 
319
 
 
320
  xosd_pos osd_pos;
 
321
 
 
322
  if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &pos))
 
323
    return NULL;
 
324
 
 
325
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
326
 
 
327
  if(!assert_osd(osd, "Run init() first!"))
 
328
    return NULL;
 
329
 
 
330
  if (pos==0)
 
331
    osd_pos = XOSD_top;
 
332
  else if (pos==1)
 
333
    osd_pos = XOSD_bottom;
 
334
  else {
 
335
    PyErr_SetString(PyExc_ValueError, "OSD position not in range");
 
336
    return NULL;
 
337
  }
 
338
 
 
339
  xosd_set_pos(osd, pos);
 
340
 
 
341
  Py_INCREF(Py_None);
 
342
  return Py_None;
 
343
}
 
344
 
 
345
static PyObject *
 
346
pyosd_set_offset(PyObject *self, PyObject *args)
 
347
{
 
348
  int offset;
 
349
  PyObject *pyc_osd;
 
350
  xosd *osd;  
 
351
 
 
352
  if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &offset))
 
353
    return NULL;
 
354
 
 
355
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
356
 
 
357
  if(!assert_osd(osd, "Run init() first!"))
 
358
    return NULL;
 
359
 
 
360
  xosd_set_vertical_offset(osd, offset);
 
361
 
 
362
  Py_INCREF(Py_None);
 
363
  return Py_None;
 
364
}
 
365
 
 
366
static PyObject *
 
367
pyosd_set_shadow_offset(PyObject *self, PyObject *args)
 
368
{
 
369
  int offset;
 
370
  PyObject *pyc_osd;
 
371
  xosd *osd;  
 
372
 
 
373
  if(!PyArg_ParseTuple(args, "Oi", &pyc_osd, &offset))
 
374
    return NULL;
 
375
 
 
376
  osd = (xosd *)PyCObject_AsVoidPtr(pyc_osd);
 
377
 
 
378
  if(!assert_osd(osd, "Run init() first!"))
 
379
    return NULL;
 
380
 
 
381
  xosd_set_shadow_offset(osd, offset);
 
382
 
 
383
  Py_INCREF(Py_None);
 
384
  return Py_None;
 
385
}
 
386