cpython/Modules/almodule.c

792 lines
15 KiB
C
Raw Normal View History

1991-09-09 23:33:34 +00:00
/**********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
1991-09-09 23:33:34 +00:00
All Rights Reserved
1996-10-25 14:44:06 +00:00
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
1991-09-09 23:33:34 +00:00
provided that the above copyright notice appear in all copies and that
1996-10-25 14:44:06 +00:00
both that copyright notice and this permission notice appear in
1991-09-09 23:33:34 +00:00
supporting documentation, and that the names of Stichting Mathematisch
1996-10-25 14:44:06 +00:00
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.
While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.
1991-09-09 23:33:34 +00:00
******************************************************************/
1992-05-15 11:06:29 +00:00
/* AL module -- interface to Mark Callow's Audio Library (AL). */
1991-09-09 23:33:34 +00:00
#include <audio.h>
1991-09-09 23:33:34 +00:00
/* Check which version audio library we have: */
#ifdef AL_ERROR_NUMBER
#define AL_405
/* XXXX 4.0.5 libaudio also allows us to provide better error
** handling (with ALseterrorhandler). We should implement that
** sometime.
*/
#endif
1997-01-03 22:40:34 +00:00
#include "Python.h"
1991-09-09 23:33:34 +00:00
/* Config objects */
typedef struct {
1997-01-03 22:40:34 +00:00
PyObject_HEAD
1991-09-09 23:33:34 +00:00
ALconfig ob_config;
} configobject;
1997-01-03 22:40:34 +00:00
staticforward PyTypeObject Configtype;
1991-09-09 23:33:34 +00:00
#define is_configobject(v) ((v)->ob_type == &Configtype)
1992-01-27 16:45:55 +00:00
/* Forward */
1997-01-03 22:40:34 +00:00
static int getconfigarg Py_PROTO((PyObject *, ALconfig *));
static int getstrstrconfigarg Py_PROTO((PyObject *, char **, char **,
ALconfig *));
1992-01-27 16:45:55 +00:00
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
setConfig (self, args, func)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
void (*func)(ALconfig, long);
{
long par;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse (args, "l", &par)) return NULL;
1991-09-09 23:33:34 +00:00
(*func) (self-> ob_config, par);
1997-01-03 22:40:34 +00:00
Py_INCREF (Py_None);
return Py_None;
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
getConfig (self, args, func)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
long (*func)(ALconfig);
{
long par;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
par = (*func) (self-> ob_config);
1997-01-03 22:40:34 +00:00
return PyInt_FromLong (par);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_setqueuesize (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
return (setConfig (self, args, ALsetqueuesize));
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getqueuesize (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
return (getConfig (self, args, ALgetqueuesize));
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_setwidth (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
return (setConfig (self, args, ALsetwidth));
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getwidth (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
return (getConfig (self, args, ALgetwidth));
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getchannels (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
return (getConfig (self, args, ALgetchannels));
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_setchannels (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
return (setConfig (self, args, ALsetchannels));
}
#ifdef AL_405
1997-01-03 22:40:34 +00:00
static PyObject *
al_getsampfmt (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
{
return (getConfig (self, args, ALgetsampfmt));
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_setsampfmt (self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
{
return (setConfig (self, args, ALsetsampfmt));
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_getfloatmax(self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
{
double arg;
1997-01-03 22:40:34 +00:00
if ( !PyArg_NoArgs(args) )
return 0;
arg = ALgetfloatmax(self->ob_config);
1997-01-03 22:40:34 +00:00
return PyFloat_FromDouble(arg);
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_setfloatmax(self, args)
configobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
{
double arg;
1997-01-03 22:40:34 +00:00
if ( !PyArg_Parse(args, "d", &arg) )
return 0;
ALsetfloatmax(self->ob_config, arg);
1997-01-03 22:40:34 +00:00
Py_INCREF(Py_None);
return Py_None;
}
#endif /* AL_405 */
1997-01-03 22:40:34 +00:00
static PyMethodDef config_methods[] = {
{"getqueuesize", (PyCFunction)al_getqueuesize},
{"setqueuesize", (PyCFunction)al_setqueuesize},
{"getwidth", (PyCFunction)al_getwidth},
{"setwidth", (PyCFunction)al_setwidth},
{"getchannels", (PyCFunction)al_getchannels},
{"setchannels", (PyCFunction)al_setchannels},
#ifdef AL_405
1997-01-03 22:40:34 +00:00
{"getsampfmt", (PyCFunction)al_getsampfmt},
{"setsampfmt", (PyCFunction)al_setsampfmt},
{"getfloatmax", (PyCFunction)al_getfloatmax},
{"setfloatmax", (PyCFunction)al_setfloatmax},
#endif /* AL_405 */
1991-09-09 23:33:34 +00:00
{NULL, NULL} /* sentinel */
};
static void
config_dealloc(self)
configobject *self;
{
ALfreeconfig(self->ob_config);
1997-01-03 22:40:34 +00:00
PyMem_DEL(self);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
config_getattr(self, name)
configobject *self;
char *name;
{
1997-01-03 22:40:34 +00:00
return Py_FindMethod(config_methods, (PyObject *)self, name);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyTypeObject Configtype = {
PyObject_HEAD_INIT(&PyType_Type)
1991-09-09 23:33:34 +00:00
0, /*ob_size*/
"config", /*tp_name*/
sizeof(configobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)config_dealloc, /*tp_dealloc*/
1991-09-09 23:33:34 +00:00
0, /*tp_print*/
(getattrfunc)config_getattr, /*tp_getattr*/
1991-09-09 23:33:34 +00:00
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
};
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
newconfigobject(config)
ALconfig config;
{
configobject *p;
1997-01-03 22:40:34 +00:00
p = PyObject_NEW(configobject, &Configtype);
1991-09-09 23:33:34 +00:00
if (p == NULL)
return NULL;
p->ob_config = config;
1997-01-03 22:40:34 +00:00
return (PyObject *)p;
1991-09-09 23:33:34 +00:00
}
/* Port objects */
typedef struct {
1997-01-03 22:40:34 +00:00
PyObject_HEAD
1991-09-09 23:33:34 +00:00
ALport ob_port;
} portobject;
1997-01-03 22:40:34 +00:00
staticforward PyTypeObject Porttype;
1991-09-09 23:33:34 +00:00
#define is_portobject(v) ((v)->ob_type == &Porttype)
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_closeport (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
if (self->ob_port != NULL) {
ALcloseport (self-> ob_port);
self->ob_port = NULL;
/* XXX Using a closed port may dump core! */
}
1997-01-03 22:40:34 +00:00
Py_INCREF (Py_None);
return Py_None;
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getfd (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
int fd;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
fd = ALgetfd (self-> ob_port);
1997-01-03 22:40:34 +00:00
return PyInt_FromLong (fd);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getfilled (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
long count;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
count = ALgetfilled (self-> ob_port);
1997-01-03 22:40:34 +00:00
return PyInt_FromLong (count);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getfillable (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
long count;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
count = ALgetfillable (self-> ob_port);
1997-01-03 22:40:34 +00:00
return PyInt_FromLong (count);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_readsamps (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
long count;
1997-01-03 22:40:34 +00:00
PyObject *v;
ALconfig c;
1991-09-09 23:33:34 +00:00
int width;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse (args, "l", &count)) return NULL;
1991-09-09 23:33:34 +00:00
if (count <= 0)
{
1997-01-03 22:40:34 +00:00
PyErr_SetString (PyExc_RuntimeError,
"al.readsamps : arg <= 0");
1991-09-09 23:33:34 +00:00
return NULL;
}
c = ALgetconfig(self->ob_port);
#ifdef AL_405
width = ALgetsampfmt(c);
if ( width == AL_SAMPFMT_FLOAT )
width = sizeof(float);
else if ( width == AL_SAMPFMT_DOUBLE )
width = sizeof(double);
else
width = ALgetwidth(c);
#else
width = ALgetwidth(c);
#endif /* AL_405 */
ALfreeconfig(c);
1997-01-03 22:40:34 +00:00
v = PyString_FromStringAndSize ((char *)NULL, width * count);
1991-09-09 23:33:34 +00:00
if (v == NULL) return NULL;
1997-01-03 22:40:34 +00:00
Py_BEGIN_ALLOW_THREADS
ALreadsamps (self-> ob_port, (void *) PyString_AsString(v), count);
Py_END_ALLOW_THREADS
1991-09-09 23:33:34 +00:00
return (v);
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_writesamps (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
1992-01-27 16:45:55 +00:00
char *buf;
int size, width;
ALconfig c;
1991-09-09 23:33:34 +00:00
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse (args, "s#", &buf, &size)) return NULL;
1991-09-09 23:33:34 +00:00
c = ALgetconfig(self->ob_port);
#ifdef AL_405
width = ALgetsampfmt(c);
if ( width == AL_SAMPFMT_FLOAT )
width = sizeof(float);
else if ( width == AL_SAMPFMT_DOUBLE )
width = sizeof(double);
else
width = ALgetwidth(c);
#else
width = ALgetwidth(c);
#endif /* AL_405 */
ALfreeconfig(c);
1997-01-03 22:40:34 +00:00
Py_BEGIN_ALLOW_THREADS
1992-01-27 16:45:55 +00:00
ALwritesamps (self-> ob_port, (void *) buf, (long) size / width);
1997-01-03 22:40:34 +00:00
Py_END_ALLOW_THREADS
1991-09-09 23:33:34 +00:00
1997-01-03 22:40:34 +00:00
Py_INCREF (Py_None);
return Py_None;
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getfillpoint (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
long count;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
count = ALgetfillpoint (self-> ob_port);
1997-01-03 22:40:34 +00:00
return PyInt_FromLong (count);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_setfillpoint (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
long count;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse (args, "l", &count)) return NULL;
1991-09-09 23:33:34 +00:00
ALsetfillpoint (self-> ob_port, count);
1997-01-03 22:40:34 +00:00
Py_INCREF (Py_None);
return (Py_None);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_setconfig (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
ALconfig config;
1992-01-27 16:45:55 +00:00
if (!getconfigarg (args, &config)) return NULL;
1991-09-09 23:33:34 +00:00
ALsetconfig (self-> ob_port, config);
1997-01-03 22:40:34 +00:00
Py_INCREF (Py_None);
return (Py_None);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_getconfig (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
1991-09-09 23:33:34 +00:00
{
ALconfig config;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
config = ALgetconfig (self-> ob_port);
return newconfigobject (config);
}
#ifdef AL_405
1997-01-03 22:40:34 +00:00
static PyObject *
al_getstatus (self, args)
portobject *self;
1997-01-03 22:40:34 +00:00
PyObject *args;
{
1997-01-03 22:40:34 +00:00
PyObject *list, *v;
long *PVbuffer;
long length;
int i;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse(args, "O", &list))
return NULL;
1997-01-03 22:40:34 +00:00
if (!PyList_Check(list)) {
PyErr_BadArgument();
return NULL;
}
1997-01-03 22:40:34 +00:00
length = PyList_Size(list);
PVbuffer = PyMem_NEW(long, length);
if (PVbuffer == NULL)
1997-01-03 22:40:34 +00:00
return PyErr_NoMemory();
for (i = 0; i < length; i++) {
1997-01-03 22:40:34 +00:00
v = PyList_GetItem(list, i);
if (!PyInt_Check(v)) {
PyMem_DEL(PVbuffer);
PyErr_BadArgument();
return NULL;
}
1997-01-03 22:40:34 +00:00
PVbuffer[i] = PyInt_AsLong(v);
}
ALgetstatus(self->ob_port, PVbuffer, length);
for (i = 0; i < length; i++)
1997-01-03 22:40:34 +00:00
PyList_SetItem(list, i, PyInt_FromLong(PVbuffer[i]));
1997-01-03 22:40:34 +00:00
PyMem_DEL(PVbuffer);
1997-01-03 22:40:34 +00:00
Py_INCREF(Py_None);
return Py_None;
}
#endif /* AL_405 */
1997-01-03 22:40:34 +00:00
static PyMethodDef port_methods[] = {
{"closeport", (PyCFunction)al_closeport},
{"getfd", (PyCFunction)al_getfd},
{"fileno", (PyCFunction)al_getfd},
{"getfilled", (PyCFunction)al_getfilled},
{"getfillable", (PyCFunction)al_getfillable},
{"readsamps", (PyCFunction)al_readsamps},
{"writesamps", (PyCFunction)al_writesamps},
{"setfillpoint", (PyCFunction)al_setfillpoint},
{"getfillpoint", (PyCFunction)al_getfillpoint},
{"setconfig", (PyCFunction)al_setconfig},
{"getconfig", (PyCFunction)al_getconfig},
#ifdef AL_405
1997-01-03 22:40:34 +00:00
{"getstatus", (PyCFunction)al_getstatus},
#endif /* AL_405 */
1991-09-09 23:33:34 +00:00
{NULL, NULL} /* sentinel */
};
static void
port_dealloc(p)
portobject *p;
{
if (p->ob_port != NULL)
ALcloseport(p->ob_port);
1997-01-03 22:40:34 +00:00
PyMem_DEL(p);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
port_getattr(p, name)
portobject *p;
char *name;
{
1997-01-03 22:40:34 +00:00
return Py_FindMethod(port_methods, (PyObject *)p, name);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00
static PyTypeObject Porttype = {
PyObject_HEAD_INIT(&PyType_Type)
1991-09-09 23:33:34 +00:00
0, /*ob_size*/
"port", /*tp_name*/
sizeof(portobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)port_dealloc, /*tp_dealloc*/
1991-09-09 23:33:34 +00:00
0, /*tp_print*/
(getattrfunc)port_getattr, /*tp_getattr*/
1991-09-09 23:33:34 +00:00
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
};
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
newportobject(port)
ALport port;
{
portobject *p;
1997-01-03 22:40:34 +00:00
p = PyObject_NEW(portobject, &Porttype);
1991-09-09 23:33:34 +00:00
if (p == NULL)
return NULL;
p->ob_port = port;
1997-01-03 22:40:34 +00:00
return (PyObject *)p;
1991-09-09 23:33:34 +00:00
}
/* the module al */
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_openport (self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
1991-09-09 23:33:34 +00:00
{
1992-01-27 16:45:55 +00:00
char *name, *dir;
1991-09-09 23:33:34 +00:00
ALport port;
ALconfig config = NULL;
1991-10-20 20:10:46 +00:00
int size;
1991-09-09 23:33:34 +00:00
1997-01-03 22:40:34 +00:00
if (args == NULL || !PyTuple_Check(args)) {
PyErr_BadArgument ();
1991-10-20 20:10:46 +00:00
return NULL;
}
1997-01-03 22:40:34 +00:00
size = PyTuple_Size(args);
1991-09-09 23:33:34 +00:00
if (size == 2) {
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse (args, "(ss)", &name, &dir))
1991-09-09 23:33:34 +00:00
return NULL;
}
else if (size == 3) {
if (!getstrstrconfigarg (args, &name, &dir, &config))
return NULL;
}
else {
1997-01-03 22:40:34 +00:00
PyErr_BadArgument ();
1991-09-09 23:33:34 +00:00
return NULL;
}
1992-01-27 16:45:55 +00:00
port = ALopenport(name, dir, config);
1991-09-09 23:33:34 +00:00
1991-10-20 20:10:46 +00:00
if (port == NULL) {
1997-01-03 22:40:34 +00:00
PyErr_SetFromErrno(PyExc_RuntimeError);
1991-10-20 20:10:46 +00:00
return NULL;
}
1991-09-09 23:33:34 +00:00
return newportobject (port);
}
1997-01-03 22:40:34 +00:00
static PyObject *
1991-09-09 23:33:34 +00:00
al_newconfig (self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
1991-09-09 23:33:34 +00:00
{
ALconfig config;
1997-01-03 22:40:34 +00:00
if (!PyArg_NoArgs (args)) return NULL;
1991-09-09 23:33:34 +00:00
config = ALnewconfig ();
1991-10-20 20:10:46 +00:00
if (config == NULL) {
1997-01-03 22:40:34 +00:00
PyErr_SetFromErrno(PyExc_RuntimeError);
1991-10-20 20:10:46 +00:00
return NULL;
}
1991-09-09 23:33:34 +00:00
return newconfigobject (config);
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_queryparams(self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
{
long device;
long length;
long *PVbuffer;
long PVdummy[2];
1997-01-03 22:40:34 +00:00
PyObject *v;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse (args, "l", &device))
return NULL;
length = ALqueryparams(device, PVdummy, 2L);
1997-01-03 22:40:34 +00:00
PVbuffer = PyMem_NEW(long, length);
if (PVbuffer == NULL)
1997-01-03 22:40:34 +00:00
return PyErr_NoMemory();
(void) ALqueryparams(device, PVbuffer, length);
1997-01-03 22:40:34 +00:00
v = PyList_New((int)length);
if (v != NULL) {
int i;
for (i = 0; i < length; i++)
1997-01-03 22:40:34 +00:00
PyList_SetItem(v, i, PyInt_FromLong(PVbuffer[i]));
}
1997-01-03 22:40:34 +00:00
PyMem_DEL(PVbuffer);
return v;
}
1997-01-03 22:40:34 +00:00
static PyObject *
doParams(args, func, modified)
1997-01-03 22:40:34 +00:00
PyObject *args;
void (*func)(long, long *, long);
int modified;
{
long device;
1997-01-03 22:40:34 +00:00
PyObject *list, *v;
long *PVbuffer;
long length;
int i;
1991-09-09 23:33:34 +00:00
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse(args, "(lO)", &device, &list))
return NULL;
1997-01-03 22:40:34 +00:00
if (!PyList_Check(list)) {
PyErr_BadArgument();
return NULL;
}
1997-01-03 22:40:34 +00:00
length = PyList_Size(list);
PVbuffer = PyMem_NEW(long, length);
if (PVbuffer == NULL)
1997-01-03 22:40:34 +00:00
return PyErr_NoMemory();
for (i = 0; i < length; i++) {
1997-01-03 22:40:34 +00:00
v = PyList_GetItem(list, i);
if (!PyInt_Check(v)) {
PyMem_DEL(PVbuffer);
PyErr_BadArgument();
return NULL;
}
1997-01-03 22:40:34 +00:00
PVbuffer[i] = PyInt_AsLong(v);
}
1991-09-13 15:31:47 +00:00
(*func)(device, PVbuffer, length);
if (modified) {
for (i = 0; i < length; i++)
1997-01-03 22:40:34 +00:00
PyList_SetItem(list, i, PyInt_FromLong(PVbuffer[i]));
}
1997-01-03 22:40:34 +00:00
PyMem_DEL(PVbuffer);
1991-10-20 20:10:46 +00:00
1997-01-03 22:40:34 +00:00
Py_INCREF(Py_None);
return Py_None;
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_getparams(self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
{
return doParams(args, ALgetparams, 1);
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_setparams(self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
{
return doParams(args, ALsetparams, 0);
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_getname(self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
{
long device, descriptor;
char *name;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse(args, "(ll)", &device, &descriptor))
return NULL;
name = ALgetname(device, descriptor);
if (name == NULL) {
1997-01-03 22:40:34 +00:00
PyErr_SetString(PyExc_ValueError,
"al.getname: bad descriptor");
return NULL;
}
1997-01-03 22:40:34 +00:00
return PyString_FromString(name);
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_getdefault(self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
{
long device, descriptor, value;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse(args, "(ll)", &device, &descriptor))
return NULL;
value = ALgetdefault(device, descriptor);
1997-01-03 22:40:34 +00:00
return PyLong_FromLong(value);
}
1997-01-03 22:40:34 +00:00
static PyObject *
al_getminmax(self, args)
1997-01-03 22:40:34 +00:00
PyObject *self, *args;
{
long device, descriptor, min, max;
1997-01-03 22:40:34 +00:00
if (!PyArg_Parse(args, "(ll)", &device, &descriptor))
return NULL;
min = -1;
max = -1;
ALgetminmax(device, descriptor, &min, &max);
1997-01-03 22:40:34 +00:00
return Py_BuildValue("ll", min, max);
}
static PyMethodDef al_methods[] = {
{"openport", (PyCFunction)al_openport},
{"newconfig", (PyCFunction)al_newconfig},
{"queryparams", (PyCFunction)al_queryparams},
{"getparams", (PyCFunction)al_getparams},
{"setparams", (PyCFunction)al_setparams},
{"getname", (PyCFunction)al_getname},
{"getdefault", (PyCFunction)al_getdefault},
{"getminmax", (PyCFunction)al_getminmax},
1991-09-09 23:33:34 +00:00
{NULL, NULL} /* sentinel */
};
void
inital()
{
1997-01-03 22:40:34 +00:00
Py_InitModule("al", al_methods);
1991-09-09 23:33:34 +00:00
}
1992-01-27 16:45:55 +00:00
static int
getconfigarg(o, conf)
1997-01-03 22:40:34 +00:00
PyObject *o;
1991-09-09 23:33:34 +00:00
ALconfig *conf;
{
if (o == NULL || !is_configobject(o))
1997-01-03 22:40:34 +00:00
return PyErr_BadArgument ();
1991-09-09 23:33:34 +00:00
1992-01-27 16:45:55 +00:00
*conf = ((configobject *) o) -> ob_config;
1991-09-09 23:33:34 +00:00
return 1;
}
1992-01-27 16:45:55 +00:00
static int
1991-09-09 23:33:34 +00:00
getstrstrconfigarg(v, a, b, c)
1997-01-03 22:40:34 +00:00
PyObject *v;
1992-01-27 16:45:55 +00:00
char **a;
char **b;
1991-09-09 23:33:34 +00:00
ALconfig *c;
{
1997-01-03 22:40:34 +00:00
PyObject *o;
return PyArg_Parse(v, "(ssO)", a, b, &o) && getconfigarg(o, c);
1991-09-09 23:33:34 +00:00
}
1997-01-03 22:40:34 +00:00