Kaydet (Commit) 8496d394 authored tarafından Barry Warsaw's avatar Barry Warsaw

Renamed, however there is no test case. I did test some of the module

out manually, and it does compile, but I'm not sure how to write a
useful portable test case.  Maybe later...
üst 0caa7ec2
...@@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* Sad objects */ /* Sad objects */
#include "allobjects.h" #include "Python.h"
#include "modsupport.h"
#include "structmember.h" #include "structmember.h"
#ifdef HAVE_SYS_AUDIOIO_H #ifdef HAVE_SYS_AUDIOIO_H
...@@ -58,7 +57,7 @@ PERFORMANCE OF THIS SOFTWARE. ...@@ -58,7 +57,7 @@ PERFORMANCE OF THIS SOFTWARE.
/* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */ /* #define offsetof(str,mem) ((int)(((str *)0)->mem)) */
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
int x_fd; /* The open file */ int x_fd; /* The open file */
int x_icount; /* # samples read */ int x_icount; /* # samples read */
int x_ocount; /* # samples written */ int x_ocount; /* # samples written */
...@@ -67,22 +66,22 @@ typedef struct { ...@@ -67,22 +66,22 @@ typedef struct {
} sadobject; } sadobject;
typedef struct { typedef struct {
OB_HEAD PyObject_HEAD
audio_info_t ai; audio_info_t ai;
} sadstatusobject; } sadstatusobject;
staticforward typeobject Sadtype; staticforward PyTypeObject Sadtype;
staticforward typeobject Sadstatustype; staticforward PyTypeObject Sadstatustype;
static sadstatusobject *sads_alloc(); /* Forward */ static sadstatusobject *sads_alloc(); /* Forward */
static object *SunAudioError; static PyObject *SunAudioError;
#define is_sadobject(v) ((v)->ob_type == &Sadtype) #define is_sadobject(v) ((v)->ob_type == &Sadtype)
#define is_sadstatusobject(v) ((v)->ob_type == &Sadstatustype) #define is_sadstatusobject(v) ((v)->ob_type == &Sadstatustype)
static sadobject * static sadobject *
newsadobject(arg) newsadobject(arg)
object *arg; PyObject *arg;
{ {
sadobject *xp; sadobject *xp;
int fd; int fd;
...@@ -90,34 +89,35 @@ newsadobject(arg) ...@@ -90,34 +89,35 @@ newsadobject(arg)
int imode; int imode;
/* Check arg for r/w/rw */ /* Check arg for r/w/rw */
if ( !getargs(arg, "s", &mode) ) if (!PyArg_Parse(arg, "s", &mode))
return 0; return NULL;
if ( strcmp(mode, "r") == 0 ) if (strcmp(mode, "r") == 0)
imode = 0; imode = 0;
else if ( strcmp(mode, "w") == 0 ) else if (strcmp(mode, "w") == 0)
imode = 1; imode = 1;
else if ( strcmp(mode, "rw") == 0 ) else if (strcmp(mode, "rw") == 0)
imode = 2; imode = 2;
else if ( strcmp(mode, "control") == 0 ) else if (strcmp(mode, "control") == 0)
imode = -1; imode = -1;
else { else {
err_setstr(SunAudioError, PyErr_SetString(SunAudioError,
"Mode should be one of 'r', 'w', 'rw' or 'control'"); "Mode should be one of 'r', 'w', 'rw' or 'control'");
return 0; return NULL;
} }
/* Open the correct device */ /* Open the correct device */
if ( imode < 0 ) if (imode < 0)
fd = open("/dev/audioctl", 2); /* XXXX Chaeck that this works */ /* XXXX Check that this works */
fd = open("/dev/audioctl", 2);
else else
fd = open("/dev/audio", imode); fd = open("/dev/audio", imode);
if ( fd < 0 ) { if (fd < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
/* Create and initialize the object */ /* Create and initialize the object */
xp = NEWOBJ(sadobject, &Sadtype); xp = PyObject_NEW(sadobject, &Sadtype);
if (xp == NULL) if (xp == NULL)
return NULL; return NULL;
xp->x_fd = fd; xp->x_fd = fd;
...@@ -134,242 +134,249 @@ sad_dealloc(xp) ...@@ -134,242 +134,249 @@ sad_dealloc(xp)
sadobject *xp; sadobject *xp;
{ {
close(xp->x_fd); close(xp->x_fd);
DEL(xp); PyMem_DEL(xp);
} }
static object * static PyObject *
sad_read(self, args) sad_read(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
int size, count; int size, count;
char *cp; char *cp;
object *rv; PyObject *rv;
if ( !getargs(args, "i", &size) ) if (!PyArg_Parse(args, "i", &size))
return 0; return NULL;
rv = newsizedstringobject(NULL, size); rv = PyString_FromStringAndSize(NULL, size);
if ( rv == NULL ) if (rv == NULL)
return 0; return NULL;
cp = getstringvalue(rv); if (!(cp = PyString_AsString(rv)))
goto finally;
count = read(self->x_fd, cp, size); count = read(self->x_fd, cp, size);
if ( count < 0 ) { if (count < 0) {
DECREF(rv); PyErr_SetFromErrno(SunAudioError);
err_errno(SunAudioError); goto finally;
return NULL;
} }
if ( count != size ) #if 0
printf("sunaudio: funny read rv %d wtd %d\n", count, size); /* TBD: why print this message if you can handle the condition?
assume it's debugging info which we can just as well get rid
of. in any case this message should *not* be using printf!
*/
if (count != size)
printf("sunaudio: funny read rv %d wtd %d\n", count, size);
#endif
self->x_icount += count; self->x_icount += count;
return rv; return rv;
finally:
Py_DECREF(rv);
return NULL;
} }
static object * static PyObject *
sad_write(self, args) sad_write(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
char *cp; char *cp;
int count, size; int count, size;
if ( !getargs(args, "s#", &cp, &size) ) if (!PyArg_Parse(args, "s#", &cp, &size))
return 0; return NULL;
count = write(self->x_fd, cp, size); count = write(self->x_fd, cp, size);
if ( count < 0 ) { if (count < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
if ( count != size ) #if 0
printf("sunaudio: funny write rv %d wanted %d\n", count, size); if (count != size)
printf("sunaudio: funny write rv %d wanted %d\n", count, size);
#endif
self->x_ocount += count; self->x_ocount += count;
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sad_getinfo(self, args) sad_getinfo(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
sadstatusobject *rv; sadstatusobject *rv;
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return NULL; return NULL;
rv = sads_alloc(); rv = sads_alloc();
if ( ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0 ) { if (ioctl(self->x_fd, AUDIO_GETINFO, &rv->ai) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
DECREF(rv); Py_DECREF(rv);
return NULL; return NULL;
} }
return (object *)rv; return (PyObject *)rv;
} }
static object * static PyObject *
sad_setinfo(self, arg) sad_setinfo(self, arg)
sadobject *self; sadobject *self;
sadstatusobject *arg; sadstatusobject *arg;
{ {
if ( !is_sadstatusobject(arg) ) { if (!is_sadstatusobject(arg)) {
err_setstr(TypeError, "Must be sun audio status object"); PyErr_SetString(PyExc_TypeError,
"Must be sun audio status object");
return NULL; return NULL;
} }
if ( ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0 ) { if (ioctl(self->x_fd, AUDIO_SETINFO, &arg->ai) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sad_ibufcount(self, args) sad_ibufcount(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
audio_info_t ai; audio_info_t ai;
object *rv;
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return 0; return NULL;
if ( ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0 ) { if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
rv = newintobject(ai.record.samples - self->x_icount); return PyInt_FromLong(ai.record.samples - self->x_icount);
return rv;
} }
static object * static PyObject *
sad_obufcount(self, args) sad_obufcount(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
audio_info_t ai; audio_info_t ai;
object *rv;
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return 0; return NULL;
if ( ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0 ) { if (ioctl(self->x_fd, AUDIO_GETINFO, &ai) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
rv = newintobject(self->x_ocount - ai.play.samples); return PyInt_FromLong(self->x_ocount - ai.play.samples);
return rv;
} }
static object * static PyObject *
sad_drain(self, args) sad_drain(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return 0; return NULL;
if ( ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0 ) { if (ioctl(self->x_fd, AUDIO_DRAIN, 0) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
#ifdef SOLARIS #ifdef SOLARIS
static object * static PyObject *
sad_getdev(self, args) sad_getdev(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
struct audio_device ad; struct audio_device ad;
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return 0; return NULL;
if ( ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0 ) { if (ioctl(self->x_fd, AUDIO_GETDEV, &ad) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
return mkvalue("(sss)", ad.name, ad.version, ad.config); return Py_BuildValue("(sss)", ad.name, ad.version, ad.config);
} }
#endif #endif
static object * static PyObject *
sad_flush(self, args) sad_flush(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return 0; return NULL;
if ( ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0 ) { if (ioctl(self->x_fd, I_FLUSH, FLUSHW) < 0) {
err_errno(SunAudioError); PyErr_SetFromErrno(SunAudioError);
return NULL; return NULL;
} }
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static object * static PyObject *
sad_close(self, args) sad_close(self, args)
sadobject *self; sadobject *self;
object *args; PyObject *args;
{ {
if ( !getargs(args, "") ) if (!PyArg_Parse(args, ""))
return 0; return NULL;
if ( self->x_fd >= 0 ) { if (self->x_fd >= 0) {
close(self->x_fd); close(self->x_fd);
self->x_fd = -1; self->x_fd = -1;
} }
INCREF(None); Py_INCREF(Py_None);
return None; return Py_None;
} }
static struct methodlist sad_methods[] = { static PyMethodDef sad_methods[] = {
{ "read", (method)sad_read }, { "read", (PyCFunction)sad_read },
{ "write", (method)sad_write }, { "write", (PyCFunction)sad_write },
{ "ibufcount", (method)sad_ibufcount }, { "ibufcount", (PyCFunction)sad_ibufcount },
{ "obufcount", (method)sad_obufcount }, { "obufcount", (PyCFunction)sad_obufcount },
#define CTL_METHODS 4 #define CTL_METHODS 4
{ "getinfo", (method)sad_getinfo }, { "getinfo", (PyCFunction)sad_getinfo },
{ "setinfo", (method)sad_setinfo }, { "setinfo", (PyCFunction)sad_setinfo },
{ "drain", (method)sad_drain }, { "drain", (PyCFunction)sad_drain },
{ "flush", (method)sad_flush }, { "flush", (PyCFunction)sad_flush },
#ifdef SOLARIS #ifdef SOLARIS
{ "getdev", (method)sad_getdev }, { "getdev", (PyCFunction)sad_getdev },
#endif #endif
{ "close", (method)sad_close }, { "close", (PyCFunction)sad_close },
{NULL, NULL} /* sentinel */ {NULL, NULL} /* sentinel */
}; };
static object * static PyObject *
sad_getattr(xp, name) sad_getattr(xp, name)
sadobject *xp; sadobject *xp;
char *name; char *name;
{ {
if ( xp->x_isctl ) if (xp->x_isctl)
return findmethod(sad_methods+CTL_METHODS, (object *)xp, name); return Py_FindMethod(sad_methods+CTL_METHODS,
(PyObject *)xp, name);
else else
return findmethod(sad_methods, (object *)xp, name); return Py_FindMethod(sad_methods, (PyObject *)xp, name);
} }
/* ----------------------------------------------------------------- */ /* ----------------------------------------------------------------- */
static sadstatusobject * static sadstatusobject *
sads_alloc() { sads_alloc() {
sadstatusobject *rv; return PyObject_NEW(sadstatusobject, &Sadstatustype);
rv = NEWOBJ(sadstatusobject, &Sadstatustype);
return rv;
} }
static void static void
sads_dealloc(xp) sads_dealloc(xp)
sadstatusobject *xp; sadstatusobject *xp;
{ {
DEL(xp); PyMem_DEL(xp);
} }
#define OFF(x) offsetof(audio_info_t,x) #define OFF(x) offsetof(audio_info_t,x)
...@@ -416,34 +423,34 @@ static struct memberlist sads_ml[] = { ...@@ -416,34 +423,34 @@ static struct memberlist sads_ml[] = {
{ NULL, 0, 0}, { NULL, 0, 0},
}; };
static object * static PyObject *
sads_getattr(xp, name) sads_getattr(xp, name)
sadstatusobject *xp; sadstatusobject *xp;
char *name; char *name;
{ {
return getmember((char *)&xp->ai, sads_ml, name); return PyMember_Get((char *)&xp->ai, sads_ml, name);
} }
static int static int
sads_setattr(xp, name, v) sads_setattr(xp, name, v)
sadstatusobject *xp; sadstatusobject *xp;
char *name; char *name;
object *v; PyObject *v;
{ {
if (v == NULL) { if (v == NULL) {
err_setstr(TypeError, PyErr_SetString(PyExc_TypeError,
"can't delete sun audio status attributes"); "can't delete sun audio status attributes");
return -1; return -1;
} }
return setmember((char *)&xp->ai, sads_ml, name, v); return PyMember_Set((char *)&xp->ai, sads_ml, name, v);
} }
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
static typeobject Sadtype = { static PyTypeObject Sadtype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"sun_audio_device", /*tp_name*/ "sun_audio_device", /*tp_name*/
sizeof(sadobject), /*tp_size*/ sizeof(sadobject), /*tp_size*/
...@@ -457,8 +464,8 @@ static typeobject Sadtype = { ...@@ -457,8 +464,8 @@ static typeobject Sadtype = {
0, /*tp_repr*/ 0, /*tp_repr*/
}; };
static typeobject Sadstatustype = { static PyTypeObject Sadstatustype = {
OB_HEAD_INIT(&Typetype) PyObject_HEAD_INIT(&PyType_Type)
0, /*ob_size*/ 0, /*ob_size*/
"sun_audio_device_status", /*tp_name*/ "sun_audio_device_status", /*tp_name*/
sizeof(sadstatusobject), /*tp_size*/ sizeof(sadstatusobject), /*tp_size*/
...@@ -473,29 +480,28 @@ static typeobject Sadstatustype = { ...@@ -473,29 +480,28 @@ static typeobject Sadstatustype = {
}; };
/* ------------------------------------------------------------------- */ /* ------------------------------------------------------------------- */
static object * static PyObject *
sadopen(self, args) sadopen(self, args)
object *self; PyObject *self;
object *args; PyObject *args;
{ {
object *rv; return (PyObject *)newsadobject(args);
rv = (object *)newsadobject(args);
return rv;
} }
static struct methodlist sunaudiodev_methods[] = { static PyMethodDef sunaudiodev_methods[] = {
{ "open", sadopen }, { "open", sadopen },
{ 0, 0 }, { 0, 0 },
}; };
void void
initsunaudiodev() { initsunaudiodev()
object *m, *d; {
PyObject *m, *d;
m = initmodule("sunaudiodev", sunaudiodev_methods);
d = getmoduledict(m); m = Py_InitModule("sunaudiodev", sunaudiodev_methods);
SunAudioError = newstringobject("sunaudiodev.error"); d = PyModule_GetDict(m);
if ( SunAudioError == NULL || dictinsert(d, "error", SunAudioError) ) SunAudioError = PyString_FromString("sunaudiodev.error");
fatal("can't define sunaudiodev.error"); if ( SunAudioError == NULL ||
PyDict_SetItemString(d, "error", SunAudioError) )
Py_FatalError("can't define sunaudiodev.error");
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment