Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b0d1b06

Browse files
committed
audio(7I) suggests that applications do the following to get the
device and control pseudo-device: - first look for the device filename in the environment variable AUDIODEV. - if not found, use /dev/audio - calculate the control device by tacking "ctl" onto the base device name. We now do this. Also, if the open fails, we call PyErr_SetFromErrnoWithFilename() to give a more informative error message. Added a fileno() method to the audio object returned from open(). This returns the file descriptor which can be used by applications to set up SIGPOLL notification, as per the manpage.
1 parent 2c8b35b commit b0d1b06

1 file changed

Lines changed: 42 additions & 7 deletions

File tree

Modules/sunaudiodev.c

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static PyObject *SunAudioError;
7979
#define is_sadobject(v) ((v)->ob_type == &Sadtype)
8080
#define is_sadstatusobject(v) ((v)->ob_type == &Sadstatustype)
8181

82+
8283
static sadobject *
8384
newsadobject(arg)
8485
PyObject *arg;
@@ -87,6 +88,9 @@ newsadobject(arg)
8788
int fd;
8889
char *mode;
8990
int imode;
91+
char* basedev;
92+
char* ctldev;
93+
char* opendev;
9094

9195
/* Check arg for r/w/rw */
9296
if (!PyArg_Parse(arg, "s", &mode))
@@ -105,16 +109,34 @@ newsadobject(arg)
105109
return NULL;
106110
}
107111

108-
/* Open the correct device */
109-
if (imode < 0)
110-
/* XXXX Check that this works */
111-
fd = open("/dev/audioctl", 2);
112-
else
113-
fd = open("/dev/audio", imode);
112+
/* Open the correct device. The base device name comes from the
113+
* AUDIODEV environment variable first, then /dev/audio. The
114+
* control device tacks "ctl" onto the base device name.
115+
*/
116+
basedev = getenv("AUDIODEV");
117+
if (!basedev)
118+
basedev = "/dev/audio";
119+
ctldev = PyMem_NEW(char, strlen(basedev) + 4);
120+
if (!ctldev) {
121+
PyErr_NoMemory();
122+
return NULL;
123+
}
124+
strcpy(ctldev, basedev);
125+
strcat(ctldev, "ctl");
126+
127+
if (imode < 0) {
128+
opendev = ctldev;
129+
fd = open(ctldev, 2);
130+
}
131+
else {
132+
opendev = basedev;
133+
fd = open(basedev, imode);
134+
}
114135
if (fd < 0) {
115-
PyErr_SetFromErrno(SunAudioError);
136+
PyErr_SetFromErrnoWithFilename(SunAudioError, opendev);
116137
return NULL;
117138
}
139+
PyMem_DEL(ctldev);
118140

119141
/* Create and initialize the object */
120142
xp = PyObject_NEW(sadobject, &Sadtype);
@@ -344,6 +366,18 @@ sad_close(self, args)
344366
return Py_None;
345367
}
346368

369+
static PyObject *
370+
sad_fileno(self, args)
371+
sadobject *self;
372+
PyObject *args;
373+
{
374+
if (!PyArg_Parse(args, ""))
375+
return NULL;
376+
377+
return PyInt_FromLong(self->x_fd);
378+
}
379+
380+
347381
static PyMethodDef sad_methods[] = {
348382
{ "read", (PyCFunction)sad_read },
349383
{ "write", (PyCFunction)sad_write },
@@ -358,6 +392,7 @@ static PyMethodDef sad_methods[] = {
358392
{ "getdev", (PyCFunction)sad_getdev },
359393
#endif
360394
{ "close", (PyCFunction)sad_close },
395+
{ "fileno", (PyCFunction)sad_fileno },
361396
{NULL, NULL} /* sentinel */
362397
};
363398

0 commit comments

Comments
 (0)