blob: 7bf867bcb2b9c5e49fe4e7a20b553eb543ba59ae [file] [log] [blame]
Guido van Rossum9c52e0f1992-06-03 17:06:14 +00001
2/* Sad objects */
3
Barry Warsaw8496d391996-12-13 00:11:04 +00004#include "Python.h"
Guido van Rossum9c52e0f1992-06-03 17:06:14 +00005#include "structmember.h"
6
Guido van Rossumb6775db1994-08-01 11:34:53 +00007#ifdef HAVE_SYS_AUDIOIO_H
8#define SOLARIS
9#endif
10
Guido van Rossume29ed8f1996-12-10 15:21:30 +000011#ifdef HAVE_UNISTD_H
12#include <unistd.h>
13#endif
14
15#ifdef HAVE_FCNTL_H
16#include <fcntl.h>
17#endif
18
Guido van Rossum234f9421993-06-17 12:35:49 +000019#include <stropts.h>
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000020#include <sys/ioctl.h>
Sjoerd Mullender66bca321993-12-03 16:54:45 +000021#ifdef SOLARIS
Guido van Rossumb6775db1994-08-01 11:34:53 +000022#include <sys/audioio.h>
Sjoerd Mullender66bca321993-12-03 16:54:45 +000023#else
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000024#include <sun/audioio.h>
Sjoerd Mullender66bca321993-12-03 16:54:45 +000025#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000026
27/* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */
28
29typedef struct {
Barry Warsaw8496d391996-12-13 00:11:04 +000030 PyObject_HEAD
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000031 int x_fd; /* The open file */
32 int x_icount; /* # samples read */
33 int x_ocount; /* # samples written */
34 int x_isctl; /* True if control device */
35
36} sadobject;
37
38typedef struct {
Barry Warsaw8496d391996-12-13 00:11:04 +000039 PyObject_HEAD
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000040 audio_info_t ai;
41} sadstatusobject;
42
Barry Warsaw8496d391996-12-13 00:11:04 +000043staticforward PyTypeObject Sadtype;
44staticforward PyTypeObject Sadstatustype;
Thomas Wouters58d05102000-07-24 14:43:35 +000045static sadstatusobject *sads_alloc(void); /* Forward */
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000046
Barry Warsaw8496d391996-12-13 00:11:04 +000047static PyObject *SunAudioError;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000048
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000049#define is_sadobject(v) ((v)->ob_type == &Sadtype)
50#define is_sadstatusobject(v) ((v)->ob_type == &Sadstatustype)
51
Barry Warsawb0d1b061998-10-31 22:52:54 +000052
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000053static sadobject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +000054newsadobject(PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000055{
56 sadobject *xp;
57 int fd;
58 char *mode;
59 int imode;
Barry Warsawb0d1b061998-10-31 22:52:54 +000060 char* basedev;
61 char* ctldev;
62 char* opendev;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000063
64 /* Check arg for r/w/rw */
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +000065 if (!PyArg_Parse(args, "s", &mode))
Barry Warsaw8496d391996-12-13 00:11:04 +000066 return NULL;
67 if (strcmp(mode, "r") == 0)
68 imode = 0;
69 else if (strcmp(mode, "w") == 0)
70 imode = 1;
71 else if (strcmp(mode, "rw") == 0)
72 imode = 2;
73 else if (strcmp(mode, "control") == 0)
74 imode = -1;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000075 else {
Barry Warsaw8496d391996-12-13 00:11:04 +000076 PyErr_SetString(SunAudioError,
77 "Mode should be one of 'r', 'w', 'rw' or 'control'");
78 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000079 }
80
Barry Warsawb0d1b061998-10-31 22:52:54 +000081 /* Open the correct device. The base device name comes from the
82 * AUDIODEV environment variable first, then /dev/audio. The
83 * control device tacks "ctl" onto the base device name.
84 */
85 basedev = getenv("AUDIODEV");
86 if (!basedev)
87 basedev = "/dev/audio";
88 ctldev = PyMem_NEW(char, strlen(basedev) + 4);
89 if (!ctldev) {
90 PyErr_NoMemory();
Barry Warsaw8496d391996-12-13 00:11:04 +000091 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +000092 }
Barry Warsawb0d1b061998-10-31 22:52:54 +000093 strcpy(ctldev, basedev);
94 strcat(ctldev, "ctl");
95
96 if (imode < 0) {
97 opendev = ctldev;
98 fd = open(ctldev, 2);
99 }
100 else {
101 opendev = basedev;
102 fd = open(basedev, imode);
103 }
104 if (fd < 0) {
105 PyErr_SetFromErrnoWithFilename(SunAudioError, opendev);
106 return NULL;
107 }
108 PyMem_DEL(ctldev);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000109
110 /* Create and initialize the object */
Guido van Rossumb18618d2000-05-03 23:44:39 +0000111 xp = PyObject_New(sadobject, &Sadtype);
Barry Warsaw42185a21997-01-09 22:29:57 +0000112 if (xp == NULL) {
113 close(fd);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000114 return NULL;
Barry Warsaw42185a21997-01-09 22:29:57 +0000115 }
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000116 xp->x_fd = fd;
117 xp->x_icount = xp->x_ocount = 0;
118 xp->x_isctl = (imode < 0);
119
120 return xp;
121}
122
123/* Sad methods */
124
125static void
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000126sad_dealloc(sadobject *xp)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000127{
128 close(xp->x_fd);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000129 PyObject_Del(xp);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000130}
131
Barry Warsaw8496d391996-12-13 00:11:04 +0000132static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000133sad_read(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000134{
135 int size, count;
136 char *cp;
Barry Warsaw8496d391996-12-13 00:11:04 +0000137 PyObject *rv;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000138
Barry Warsaw8496d391996-12-13 00:11:04 +0000139 if (!PyArg_Parse(args, "i", &size))
140 return NULL;
141 rv = PyString_FromStringAndSize(NULL, size);
142 if (rv == NULL)
143 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000144
Barry Warsaw8496d391996-12-13 00:11:04 +0000145 if (!(cp = PyString_AsString(rv)))
146 goto finally;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000147
148 count = read(self->x_fd, cp, size);
Barry Warsaw8496d391996-12-13 00:11:04 +0000149 if (count < 0) {
150 PyErr_SetFromErrno(SunAudioError);
151 goto finally;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000152 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000153#if 0
154 /* TBD: why print this message if you can handle the condition?
Barry Warsaw42185a21997-01-09 22:29:57 +0000155 * assume it's debugging info which we can just as well get rid
156 * of. in any case this message should *not* be using printf!
157 */
Barry Warsaw8496d391996-12-13 00:11:04 +0000158 if (count != size)
159 printf("sunaudio: funny read rv %d wtd %d\n", count, size);
160#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000161 self->x_icount += count;
162 return rv;
Barry Warsaw8496d391996-12-13 00:11:04 +0000163
164 finally:
165 Py_DECREF(rv);
166 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000167}
168
Barry Warsaw8496d391996-12-13 00:11:04 +0000169static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000170sad_write(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000171{
172 char *cp;
173 int count, size;
174
Barry Warsaw8496d391996-12-13 00:11:04 +0000175 if (!PyArg_Parse(args, "s#", &cp, &size))
176 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000177
178 count = write(self->x_fd, cp, size);
Barry Warsaw8496d391996-12-13 00:11:04 +0000179 if (count < 0) {
180 PyErr_SetFromErrno(SunAudioError);
181 return NULL;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000182 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000183#if 0
184 if (count != size)
185 printf("sunaudio: funny write rv %d wanted %d\n", count, size);
186#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000187 self->x_ocount += count;
188
Barry Warsaw8496d391996-12-13 00:11:04 +0000189 Py_INCREF(Py_None);
190 return Py_None;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000191}
192
Barry Warsaw8496d391996-12-13 00:11:04 +0000193static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000194sad_getinfo(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000195{
196 sadstatusobject *rv;
197
Barry Warsaw8496d391996-12-13 00:11:04 +0000198 if (!PyArg_Parse(args, ""))
199 return NULL;
Barry Warsaw42185a21997-01-09 22:29:57 +0000200 if (!(rv = sads_alloc()))
201 return NULL;
202
Barry Warsaw8496d391996-12-13 00:11:04 +0000203 if (ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0) {
204 PyErr_SetFromErrno(SunAudioError);
205 Py_DECREF(rv);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000206 return NULL;
207 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000208 return (PyObject *)rv;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000209}
210
Barry Warsaw8496d391996-12-13 00:11:04 +0000211static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000212sad_setinfo(sadobject *self, sadstatusobject *arg)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000213{
Barry Warsaw8496d391996-12-13 00:11:04 +0000214 if (!is_sadstatusobject(arg)) {
215 PyErr_SetString(PyExc_TypeError,
216 "Must be sun audio status object");
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000217 return NULL;
218 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000219 if (ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0) {
220 PyErr_SetFromErrno(SunAudioError);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000221 return NULL;
222 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000223 Py_INCREF(Py_None);
224 return Py_None;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000225}
226
Barry Warsaw8496d391996-12-13 00:11:04 +0000227static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000228sad_ibufcount(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000229{
Barry Warsaw8496d391996-12-13 00:11:04 +0000230 audio_info_t ai;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000231
Barry Warsaw8496d391996-12-13 00:11:04 +0000232 if (!PyArg_Parse(args, ""))
233 return NULL;
234 if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
235 PyErr_SetFromErrno(SunAudioError);
236 return NULL;
237 }
238 return PyInt_FromLong(ai.record.samples - self->x_icount);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000239}
240
Barry Warsaw8496d391996-12-13 00:11:04 +0000241static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000242sad_obufcount(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000243{
Barry Warsaw8496d391996-12-13 00:11:04 +0000244 audio_info_t ai;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000245
Barry Warsaw8496d391996-12-13 00:11:04 +0000246 if (!PyArg_Parse(args, ""))
247 return NULL;
248 if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
249 PyErr_SetFromErrno(SunAudioError);
250 return NULL;
251 }
Thomas Wouters7e474022000-07-16 12:04:32 +0000252 /* x_ocount is in bytes, whereas play.samples is in frames */
Guido van Rossum1e0b19e1997-04-11 15:23:52 +0000253 /* we want frames */
254 return PyInt_FromLong(self->x_ocount / (ai.play.channels *
255 ai.play.precision / 8) -
256 ai.play.samples);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000257}
258
Barry Warsaw8496d391996-12-13 00:11:04 +0000259static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000260sad_drain(sadobject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000261{
262
Barry Warsaw8496d391996-12-13 00:11:04 +0000263 if (!PyArg_Parse(args, ""))
264 return NULL;
265 if (ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0) {
266 PyErr_SetFromErrno(SunAudioError);
267 return NULL;
268 }
269 Py_INCREF(Py_None);
270 return Py_None;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000271}
272
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000273#ifdef SOLARIS
Barry Warsaw8496d391996-12-13 00:11:04 +0000274static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000275sad_getdev(sadobject *self, PyObject *args)
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000276{
Barry Warsaw8496d391996-12-13 00:11:04 +0000277 struct audio_device ad;
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000278
Barry Warsaw8496d391996-12-13 00:11:04 +0000279 if (!PyArg_Parse(args, ""))
280 return NULL;
281 if (ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0) {
282 PyErr_SetFromErrno(SunAudioError);
283 return NULL;
284 }
285 return Py_BuildValue("(sss)", ad.name, ad.version, ad.config);
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000286}
287#endif
288
Barry Warsaw8496d391996-12-13 00:11:04 +0000289static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000290sad_flush(sadobject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000291{
292
Barry Warsaw8496d391996-12-13 00:11:04 +0000293 if (!PyArg_Parse(args, ""))
294 return NULL;
295 if (ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0) {
296 PyErr_SetFromErrno(SunAudioError);
297 return NULL;
298 }
299 Py_INCREF(Py_None);
300 return Py_None;
Guido van Rossum234f9421993-06-17 12:35:49 +0000301}
302
Barry Warsaw8496d391996-12-13 00:11:04 +0000303static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000304sad_close(sadobject *self, PyObject *args)
Guido van Rossum234f9421993-06-17 12:35:49 +0000305{
306
Barry Warsaw8496d391996-12-13 00:11:04 +0000307 if (!PyArg_Parse(args, ""))
308 return NULL;
309 if (self->x_fd >= 0) {
310 close(self->x_fd);
311 self->x_fd = -1;
312 }
313 Py_INCREF(Py_None);
314 return Py_None;
Guido van Rossum234f9421993-06-17 12:35:49 +0000315}
316
Barry Warsawb0d1b061998-10-31 22:52:54 +0000317static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000318sad_fileno(sadobject *self, PyObject *args)
Barry Warsawb0d1b061998-10-31 22:52:54 +0000319{
320 if (!PyArg_Parse(args, ""))
321 return NULL;
322
323 return PyInt_FromLong(self->x_fd);
324}
325
326
Barry Warsaw8496d391996-12-13 00:11:04 +0000327static PyMethodDef sad_methods[] = {
328 { "read", (PyCFunction)sad_read },
329 { "write", (PyCFunction)sad_write },
330 { "ibufcount", (PyCFunction)sad_ibufcount },
331 { "obufcount", (PyCFunction)sad_obufcount },
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000332#define CTL_METHODS 4
Barry Warsaw8496d391996-12-13 00:11:04 +0000333 { "getinfo", (PyCFunction)sad_getinfo },
334 { "setinfo", (PyCFunction)sad_setinfo },
335 { "drain", (PyCFunction)sad_drain },
336 { "flush", (PyCFunction)sad_flush },
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000337#ifdef SOLARIS
Barry Warsaw8496d391996-12-13 00:11:04 +0000338 { "getdev", (PyCFunction)sad_getdev },
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000339#endif
Barry Warsaw8496d391996-12-13 00:11:04 +0000340 { "close", (PyCFunction)sad_close },
Barry Warsawb0d1b061998-10-31 22:52:54 +0000341 { "fileno", (PyCFunction)sad_fileno },
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000342 {NULL, NULL} /* sentinel */
343};
344
Barry Warsaw8496d391996-12-13 00:11:04 +0000345static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000346sad_getattr(sadobject *xp, char *name)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000347{
Barry Warsaw8496d391996-12-13 00:11:04 +0000348 if (xp->x_isctl)
349 return Py_FindMethod(sad_methods+CTL_METHODS,
350 (PyObject *)xp, name);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000351 else
Barry Warsaw8496d391996-12-13 00:11:04 +0000352 return Py_FindMethod(sad_methods, (PyObject *)xp, name);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000353}
354
355/* ----------------------------------------------------------------- */
356
357static sadstatusobject *
Thomas Wouters58d05102000-07-24 14:43:35 +0000358sads_alloc(void) {
Guido van Rossumb18618d2000-05-03 23:44:39 +0000359 return PyObject_New(sadstatusobject, &Sadstatustype);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000360}
361
362static void
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000363sads_dealloc(sadstatusobject *xp)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000364{
Barry Warsaw8496d391996-12-13 00:11:04 +0000365 PyMem_DEL(xp);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000366}
367
368#define OFF(x) offsetof(audio_info_t,x)
369static struct memberlist sads_ml[] = {
370 { "i_sample_rate", T_UINT, OFF(record.sample_rate) },
371 { "i_channels", T_UINT, OFF(record.channels) },
372 { "i_precision", T_UINT, OFF(record.precision) },
373 { "i_encoding", T_UINT, OFF(record.encoding) },
374 { "i_gain", T_UINT, OFF(record.gain) },
375 { "i_port", T_UINT, OFF(record.port) },
376 { "i_samples", T_UINT, OFF(record.samples) },
377 { "i_eof", T_UINT, OFF(record.eof) },
378 { "i_pause", T_UBYTE, OFF(record.pause) },
379 { "i_error", T_UBYTE, OFF(record.error) },
380 { "i_waiting", T_UBYTE, OFF(record.waiting) },
381 { "i_open", T_UBYTE, OFF(record.open) , RO},
382 { "i_active", T_UBYTE, OFF(record.active) , RO},
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000383#ifdef SOLARIS
384 { "i_buffer_size", T_UINT, OFF(record.buffer_size) },
385 { "i_balance", T_UBYTE, OFF(record.balance) },
386 { "i_avail_ports", T_UINT, OFF(record.avail_ports) },
387#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000388
389 { "o_sample_rate", T_UINT, OFF(play.sample_rate) },
390 { "o_channels", T_UINT, OFF(play.channels) },
391 { "o_precision", T_UINT, OFF(play.precision) },
392 { "o_encoding", T_UINT, OFF(play.encoding) },
393 { "o_gain", T_UINT, OFF(play.gain) },
394 { "o_port", T_UINT, OFF(play.port) },
395 { "o_samples", T_UINT, OFF(play.samples) },
396 { "o_eof", T_UINT, OFF(play.eof) },
397 { "o_pause", T_UBYTE, OFF(play.pause) },
398 { "o_error", T_UBYTE, OFF(play.error) },
399 { "o_waiting", T_UBYTE, OFF(play.waiting) },
400 { "o_open", T_UBYTE, OFF(play.open) , RO},
401 { "o_active", T_UBYTE, OFF(play.active) , RO},
Sjoerd Mullender66bca321993-12-03 16:54:45 +0000402#ifdef SOLARIS
403 { "o_buffer_size", T_UINT, OFF(play.buffer_size) },
404 { "o_balance", T_UBYTE, OFF(play.balance) },
405 { "o_avail_ports", T_UINT, OFF(play.avail_ports) },
406#endif
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000407
408 { "monitor_gain", T_UINT, OFF(monitor_gain) },
409 { NULL, 0, 0},
410};
411
Barry Warsaw8496d391996-12-13 00:11:04 +0000412static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000413sads_getattr(sadstatusobject *xp, char *name)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000414{
Barry Warsaw8496d391996-12-13 00:11:04 +0000415 return PyMember_Get((char *)&xp->ai, sads_ml, name);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000416}
417
418static int
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000419sads_setattr(sadstatusobject *xp, char *name, PyObject *v)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000420{
Guido van Rossum94472a01992-09-04 09:45:18 +0000421
422 if (v == NULL) {
Barry Warsaw8496d391996-12-13 00:11:04 +0000423 PyErr_SetString(PyExc_TypeError,
424 "can't delete sun audio status attributes");
Guido van Rossumf156f501996-09-11 23:31:23 +0000425 return -1;
Guido van Rossum94472a01992-09-04 09:45:18 +0000426 }
Barry Warsaw8496d391996-12-13 00:11:04 +0000427 return PyMember_Set((char *)&xp->ai, sads_ml, name, v);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000428}
429
430/* ------------------------------------------------------------------- */
431
432
Barry Warsaw8496d391996-12-13 00:11:04 +0000433static PyTypeObject Sadtype = {
434 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossumb6775db1994-08-01 11:34:53 +0000435 0, /*ob_size*/
436 "sun_audio_device", /*tp_name*/
437 sizeof(sadobject), /*tp_size*/
438 0, /*tp_itemsize*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000439 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000440 (destructor)sad_dealloc, /*tp_dealloc*/
441 0, /*tp_print*/
442 (getattrfunc)sad_getattr, /*tp_getattr*/
443 0, /*tp_setattr*/
444 0, /*tp_compare*/
445 0, /*tp_repr*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000446};
447
Barry Warsaw8496d391996-12-13 00:11:04 +0000448static PyTypeObject Sadstatustype = {
449 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000450 0, /*ob_size*/
451 "sun_audio_device_status", /*tp_name*/
452 sizeof(sadstatusobject), /*tp_size*/
453 0, /*tp_itemsize*/
454 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000455 (destructor)sads_dealloc, /*tp_dealloc*/
456 0, /*tp_print*/
457 (getattrfunc)sads_getattr, /*tp_getattr*/
458 (setattrfunc)sads_setattr, /*tp_setattr*/
459 0, /*tp_compare*/
460 0, /*tp_repr*/
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000461};
462/* ------------------------------------------------------------------- */
463
Barry Warsaw8496d391996-12-13 00:11:04 +0000464static PyObject *
Peter Schneider-Kamp3e3175b2000-07-10 17:25:37 +0000465sadopen(PyObject *self, PyObject *args)
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000466{
Barry Warsaw8496d391996-12-13 00:11:04 +0000467 return (PyObject *)newsadobject(args);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000468}
469
Barry Warsaw8496d391996-12-13 00:11:04 +0000470static PyMethodDef sunaudiodev_methods[] = {
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000471 { "open", sadopen },
472 { 0, 0 },
473};
474
475void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000476initsunaudiodev(void)
Barry Warsaw8496d391996-12-13 00:11:04 +0000477{
478 PyObject *m, *d;
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000479
Barry Warsaw8496d391996-12-13 00:11:04 +0000480 m = Py_InitModule("sunaudiodev", sunaudiodev_methods);
481 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000482 SunAudioError = PyErr_NewException("sunaudiodev.error", NULL, NULL);
Barry Warsaw42185a21997-01-09 22:29:57 +0000483 if (SunAudioError)
484 PyDict_SetItemString(d, "error", SunAudioError);
Guido van Rossum9c52e0f1992-06-03 17:06:14 +0000485}