@@ -46,11 +46,12 @@ typedef unsigned long uint32_t;
4646
4747typedef struct {
4848 PyObject_HEAD ;
49- int fd ; /* The open file */
50- int mode ; /* file mode */
51- int icount ; /* Input count */
52- int ocount ; /* Output count */
53- uint32_t afmts ; /* Audio formats supported by hardware */
49+ char * devicename ; /* name of the device file */
50+ int fd ; /* file descriptor */
51+ int mode ; /* file mode (O_RDONLY, etc.) */
52+ int icount ; /* input count */
53+ int ocount ; /* output count */
54+ uint32_t afmts ; /* audio formats supported by hardware */
5455} oss_audio_t ;
5556
5657typedef struct {
@@ -74,19 +75,19 @@ newossobject(PyObject *arg)
7475{
7576 oss_audio_t * self ;
7677 int fd , afmts , imode ;
77- char * basedev = NULL ;
78+ char * devicename = NULL ;
7879 char * mode = NULL ;
7980
8081 /* Two ways to call open():
8182 open(device, mode) (for consistency with builtin open())
8283 open(mode) (for backwards compatibility)
8384 because the *first* argument is optional, parsing args is
8485 a wee bit tricky. */
85- if (!PyArg_ParseTuple (arg , "s|s:open" , & basedev , & mode ))
86+ if (!PyArg_ParseTuple (arg , "s|s:open" , & devicename , & mode ))
8687 return NULL ;
8788 if (mode == NULL ) { /* only one arg supplied */
88- mode = basedev ;
89- basedev = NULL ;
89+ mode = devicename ;
90+ devicename = NULL ;
9091 }
9192
9293 if (strcmp (mode , "r" ) == 0 )
@@ -102,38 +103,39 @@ newossobject(PyObject *arg)
102103
103104 /* Open the correct device: either the 'device' argument,
104105 or the AUDIODEV environment variable, or "/dev/dsp". */
105- if (basedev == NULL ) { /* called with one arg */
106- basedev = getenv ("AUDIODEV" );
107- if (basedev == NULL ) /* $AUDIODEV not set */
108- basedev = "/dev/dsp" ;
106+ if (devicename == NULL ) { /* called with one arg */
107+ devicename = getenv ("AUDIODEV" );
108+ if (devicename == NULL ) /* $AUDIODEV not set */
109+ devicename = "/dev/dsp" ;
109110 }
110111
111112 /* Open with O_NONBLOCK to avoid hanging on devices that only allow
112113 one open at a time. This does *not* affect later I/O; OSS
113114 provides a special ioctl() for non-blocking read/write, which is
114115 exposed via oss_nonblock() below. */
115- if ((fd = open (basedev , imode |O_NONBLOCK )) == -1 ) {
116- PyErr_SetFromErrnoWithFilename (PyExc_IOError , basedev );
116+ if ((fd = open (devicename , imode |O_NONBLOCK )) == -1 ) {
117+ PyErr_SetFromErrnoWithFilename (PyExc_IOError , devicename );
117118 return NULL ;
118119 }
119120
120121 /* And (try to) put it back in blocking mode so we get the
121122 expected write() semantics. */
122123 if (fcntl (fd , F_SETFL , 0 ) == -1 ) {
123124 close (fd );
124- PyErr_SetFromErrnoWithFilename (PyExc_IOError , basedev );
125+ PyErr_SetFromErrnoWithFilename (PyExc_IOError , devicename );
125126 return NULL ;
126127 }
127128
128129 if (ioctl (fd , SNDCTL_DSP_GETFMTS , & afmts ) == -1 ) {
129- PyErr_SetFromErrnoWithFilename (PyExc_IOError , basedev );
130+ PyErr_SetFromErrnoWithFilename (PyExc_IOError , devicename );
130131 return NULL ;
131132 }
132133 /* Create and initialize the object */
133134 if ((self = PyObject_New (oss_audio_t , & OSSAudioType )) == NULL ) {
134135 close (fd );
135136 return NULL ;
136137 }
138+ self -> devicename = devicename ;
137139 self -> fd = fd ;
138140 self -> mode = imode ;
139141 self -> icount = self -> ocount = 0 ;
@@ -158,22 +160,22 @@ oss_dealloc(oss_audio_t *self)
158160static oss_mixer_t *
159161newossmixerobject (PyObject * arg )
160162{
161- char * basedev = NULL ;
163+ char * devicename = NULL ;
162164 int fd ;
163165 oss_mixer_t * self ;
164166
165- if (!PyArg_ParseTuple (arg , "|s" , & basedev )) {
167+ if (!PyArg_ParseTuple (arg , "|s" , & devicename )) {
166168 return NULL ;
167169 }
168170
169- if (basedev == NULL ) {
170- basedev = getenv ("MIXERDEV" );
171- if (basedev == NULL ) /* MIXERDEV not set */
172- basedev = "/dev/mixer" ;
171+ if (devicename == NULL ) {
172+ devicename = getenv ("MIXERDEV" );
173+ if (devicename == NULL ) /* MIXERDEV not set */
174+ devicename = "/dev/mixer" ;
173175 }
174176
175- if ((fd = open (basedev , O_RDWR )) == -1 ) {
176- PyErr_SetFromErrnoWithFilename (PyExc_IOError , basedev );
177+ if ((fd = open (devicename , O_RDWR )) == -1 ) {
178+ PyErr_SetFromErrnoWithFilename (PyExc_IOError , devicename );
177179 return NULL ;
178180 }
179181
@@ -827,7 +829,33 @@ static PyMethodDef oss_mixer_methods[] = {
827829static PyObject *
828830oss_getattr (oss_audio_t * self , char * name )
829831{
830- return Py_FindMethod (oss_methods , (PyObject * )self , name );
832+ PyObject * rval = NULL ;
833+ if (strcmp (name , "closed" ) == 0 ) {
834+ rval = (self -> fd == -1 ) ? Py_True : Py_False ;
835+ Py_INCREF (rval );
836+ }
837+ else if (strcmp (name , "name" ) == 0 ) {
838+ rval = PyString_FromString (self -> devicename );
839+ }
840+ else if (strcmp (name , "mode" ) == 0 ) {
841+ /* No need for a "default" in this switch: from newossobject(),
842+ self->mode can only be one of these three values. */
843+ switch (self -> mode ) {
844+ case O_RDONLY :
845+ rval = PyString_FromString ("r" );
846+ break ;
847+ case O_RDWR :
848+ rval = PyString_FromString ("rw" );
849+ break ;
850+ case O_WRONLY :
851+ rval = PyString_FromString ("w" );
852+ break ;
853+ }
854+ }
855+ else {
856+ rval = Py_FindMethod (oss_methods , (PyObject * )self , name );
857+ }
858+ return rval ;
831859}
832860
833861static PyObject *
0 commit comments