cpython/Modules/cdmodule.c

890 lines
20 KiB
C
Raw Normal View History

1992-04-13 15:22:53 -03:00
/**********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.
1992-04-13 15:22:53 -03:00
All Rights Reserved
1996-10-25 11:44:06 -03:00
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
1992-04-13 15:22:53 -03:00
provided that the above copyright notice appear in all copies and that
1996-10-25 11:44:06 -03:00
both that copyright notice and this permission notice appear in
1992-04-13 15:22:53 -03:00
supporting documentation, and that the names of Stichting Mathematisch
1996-10-25 11:44:06 -03: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.
1992-04-13 15:22:53 -03:00
******************************************************************/
/* CD module -- interface to Mark Callow's and Roger Chickering's */
/* CD Audio Library (CD). */
#include <sys/types.h>
#include <cdaudio.h>
1997-01-03 19:00:51 -04:00
#include "Python.h"
1992-04-13 15:22:53 -03:00
#define NCALLBACKS 8
typedef struct {
1997-01-03 19:00:51 -04:00
PyObject_HEAD
1992-04-13 15:22:53 -03:00
CDPLAYER *ob_cdplayer;
} cdplayerobject;
1997-01-03 19:00:51 -04:00
static PyObject *CdError; /* exception cd.error */
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_allowremoval(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":allowremoval"))
1992-04-13 15:22:53 -03:00
return NULL;
CDallowremoval(self->ob_cdplayer);
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_preventremoval(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":preventremoval"))
1992-04-13 15:22:53 -03:00
return NULL;
CDpreventremoval(self->ob_cdplayer);
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_bestreadsize(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":bestreadsize"))
1992-04-13 15:22:53 -03:00
return NULL;
1997-01-03 19:00:51 -04:00
return PyInt_FromLong((long) CDbestreadsize(self->ob_cdplayer));
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_close(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":close"))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDclose(self->ob_cdplayer)) {
1997-01-03 19:00:51 -04:00
PyErr_SetFromErrno(CdError); /* XXX - ??? */
1992-04-13 15:22:53 -03:00
return NULL;
}
self->ob_cdplayer = NULL;
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_eject(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":eject"))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDeject(self->ob_cdplayer)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "eject failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_getstatus(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":getstatus"))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDgetstatus(self->ob_cdplayer, &status)) {
1997-01-03 19:00:51 -04:00
PyErr_SetFromErrno(CdError); /* XXX - ??? */
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
return Py_BuildValue("(ii(iii)(iii)(iii)iiii)", status.state,
status.track, status.min, status.sec, status.frame,
status.abs_min, status.abs_sec, status.abs_frame,
status.total_min, status.total_sec, status.total_frame,
status.first, status.last, status.scsi_audio,
status.cur_block);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_gettrackinfo(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int track;
CDTRACKINFO info;
CDSTATUS status;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "i:gettrackinfo", &track))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDgettrackinfo(self->ob_cdplayer, track, &info)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "gettrackinfo failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
return Py_BuildValue("((iii)(iii))",
1992-04-13 15:22:53 -03:00
info.start_min, info.start_sec, info.start_frame,
info.total_min, info.total_sec, info.total_frame);
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_msftoblock(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int min, sec, frame;
if (!PyArg_ParseTuple(args, "iii:msftoblock", &min, &sec, &frame))
1992-04-13 15:22:53 -03:00
return NULL;
1997-01-03 19:00:51 -04:00
return PyInt_FromLong((long) CDmsftoblock(self->ob_cdplayer,
min, sec, frame));
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_play(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int start, play;
CDSTATUS status;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "ii:play", &start, &play))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDplay(self->ob_cdplayer, start, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "play failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_playabs(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int min, sec, frame, play;
CDSTATUS status;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "iiii:playabs", &min, &sec, &frame, &play))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDplayabs(self->ob_cdplayer, min, sec, frame, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "playabs failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_playtrack(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int start, play;
CDSTATUS status;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "ii:playtrack", &start, &play))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDplaytrack(self->ob_cdplayer, start, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "playtrack failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_playtrackabs(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int track, min, sec, frame, play;
CDSTATUS status;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "iiiii:playtrackabs", &track, &min, &sec,
1997-01-03 19:00:51 -04:00
&frame, &play))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDplaytrackabs(self->ob_cdplayer, track, min, sec, frame, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "playtrackabs failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_readda(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int numframes, n;
1997-01-03 19:00:51 -04:00
PyObject *result;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "i:readda", &numframes))
1992-04-13 15:22:53 -03:00
return NULL;
1997-01-03 19:00:51 -04:00
result = PyString_FromStringAndSize(NULL, numframes * sizeof(CDFRAME));
1992-04-13 15:22:53 -03:00
if (result == NULL)
return NULL;
1997-01-03 19:00:51 -04:00
n = CDreadda(self->ob_cdplayer,
(CDFRAME *) PyString_AsString(result), numframes);
1992-04-13 15:22:53 -03:00
if (n == -1) {
1997-01-03 19:00:51 -04:00
Py_DECREF(result);
PyErr_SetFromErrno(CdError);
1992-04-13 15:22:53 -03:00
return NULL;
}
if (n < numframes)
1997-01-03 19:00:51 -04:00
if (_PyString_Resize(&result, n * sizeof(CDFRAME)))
1992-04-13 15:22:53 -03:00
return NULL;
return result;
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_seek(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int min, sec, frame;
1997-01-03 19:00:51 -04:00
long PyTryBlock;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "iii:seek", &min, &sec, &frame))
1992-04-13 15:22:53 -03:00
return NULL;
1997-01-03 19:00:51 -04:00
PyTryBlock = CDseek(self->ob_cdplayer, min, sec, frame);
if (PyTryBlock == -1) {
PyErr_SetFromErrno(CdError);
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
return PyInt_FromLong(PyTryBlock);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_seektrack(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int track;
1997-01-03 19:00:51 -04:00
long PyTryBlock;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "i:seektrack", &track))
1992-04-13 15:22:53 -03:00
return NULL;
1997-01-03 19:00:51 -04:00
PyTryBlock = CDseektrack(self->ob_cdplayer, track);
if (PyTryBlock == -1) {
PyErr_SetFromErrno(CdError);
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
return PyInt_FromLong(PyTryBlock);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_seekblock(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
{
1997-01-03 19:00:51 -04:00
unsigned long PyTryBlock;
if (!PyArg_ParseTuple(args, "l:seekblock", &PyTryBlock))
return NULL;
1997-01-03 19:00:51 -04:00
PyTryBlock = CDseekblock(self->ob_cdplayer, PyTryBlock);
if (PyTryBlock == (unsigned long) -1) {
PyErr_SetFromErrno(CdError);
return NULL;
}
1997-01-03 19:00:51 -04:00
return PyInt_FromLong(PyTryBlock);
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_stop(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":stop"))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDstop(self->ob_cdplayer)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "stop failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_togglepause(self, args)
cdplayerobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":togglepause"))
1992-04-13 15:22:53 -03:00
return NULL;
if (!CDtogglepause(self->ob_cdplayer)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "no disc in player");
else
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "togglepause failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyMethodDef cdplayer_methods[] = {
{"allowremoval", (PyCFunction)CD_allowremoval, 1},
{"bestreadsize", (PyCFunction)CD_bestreadsize, 1},
{"close", (PyCFunction)CD_close, 1},
{"eject", (PyCFunction)CD_eject, 1},
{"getstatus", (PyCFunction)CD_getstatus, 1},
{"gettrackinfo", (PyCFunction)CD_gettrackinfo, 1},
{"msftoblock", (PyCFunction)CD_msftoblock, 1},
{"play", (PyCFunction)CD_play, 1},
{"playabs", (PyCFunction)CD_playabs, 1},
{"playtrack", (PyCFunction)CD_playtrack, 1},
{"playtrackabs", (PyCFunction)CD_playtrackabs, 1},
{"preventremoval", (PyCFunction)CD_preventremoval, 1},
{"readda", (PyCFunction)CD_readda, 1},
{"seek", (PyCFunction)CD_seek, 1},
{"seekblock", (PyCFunction)CD_seekblock, 1},
{"seektrack", (PyCFunction)CD_seektrack, 1},
{"stop", (PyCFunction)CD_stop, 1},
{"togglepause", (PyCFunction)CD_togglepause, 1},
1992-04-13 15:22:53 -03:00
{NULL, NULL} /* sentinel */
};
static void
cdplayer_dealloc(self)
cdplayerobject *self;
{
if (self->ob_cdplayer != NULL)
CDclose(self->ob_cdplayer);
PyObject_Del(self);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
cdplayer_getattr(self, name)
cdplayerobject *self;
1992-04-13 15:22:53 -03:00
char *name;
{
if (self->ob_cdplayer == NULL) {
1997-01-03 19:00:51 -04:00
PyErr_SetString(PyExc_RuntimeError, "no player active");
return NULL;
}
1997-01-03 19:00:51 -04:00
return Py_FindMethod(cdplayer_methods, (PyObject *)self, name);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
PyTypeObject CdPlayertype = {
PyObject_HEAD_INIT(&PyType_Type)
1992-04-13 15:22:53 -03:00
0, /*ob_size*/
"cdplayer", /*tp_name*/
sizeof(cdplayerobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdplayer_dealloc, /*tp_dealloc*/
1992-04-13 15:22:53 -03:00
0, /*tp_print*/
(getattrfunc)cdplayer_getattr, /*tp_getattr*/
1992-04-13 15:22:53 -03:00
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
};
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
newcdplayerobject(cdp)
CDPLAYER *cdp;
{
cdplayerobject *p;
p = PyObject_New(cdplayerobject, &CdPlayertype);
1992-04-13 15:22:53 -03:00
if (p == NULL)
return NULL;
p->ob_cdplayer = cdp;
1997-01-03 19:00:51 -04:00
return (PyObject *) p;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_open(self, args)
1997-01-03 19:00:51 -04:00
PyObject *self, *args;
1992-04-13 15:22:53 -03:00
{
char *dev, *direction;
CDPLAYER *cdp;
/*
* Variable number of args.
* First defaults to "None", second defaults to "r".
*/
dev = NULL;
direction = "r";
if (!PyArg_ParseTuple(args, "|zs:open", &dev, &direction))
return NULL;
1992-04-13 15:22:53 -03:00
cdp = CDopen(dev, direction);
if (cdp == NULL) {
1997-01-03 19:00:51 -04:00
PyErr_SetFromErrno(CdError);
1992-04-13 15:22:53 -03:00
return NULL;
}
return newcdplayerobject(cdp);
}
typedef struct {
1997-01-03 19:00:51 -04:00
PyObject_HEAD
1992-04-13 15:22:53 -03:00
CDPARSER *ob_cdparser;
struct {
1997-01-03 19:00:51 -04:00
PyObject *ob_cdcallback;
PyObject *ob_cdcallbackarg;
1992-04-13 15:22:53 -03:00
} ob_cdcallbacks[NCALLBACKS];
} cdparserobject;
static void
CD_callback(arg, type, data)
void *arg;
CDDATATYPES type;
void *data;
{
1997-01-03 19:00:51 -04:00
PyObject *result, *args, *v = NULL;
1992-04-13 15:22:53 -03:00
char *p;
int i;
cdparserobject *self;
self = (cdparserobject *) arg;
1997-01-03 19:00:51 -04:00
args = PyTuple_New(3);
1992-04-13 15:22:53 -03:00
if (args == NULL)
return;
1997-01-03 19:00:51 -04:00
Py_INCREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
PyTuple_SetItem(args, 0, self->ob_cdcallbacks[type].ob_cdcallbackarg);
PyTuple_SetItem(args, 1, PyInt_FromLong((long) type));
1992-04-13 15:22:53 -03:00
switch (type) {
case cd_audio:
1997-01-03 19:00:51 -04:00
v = PyString_FromStringAndSize(data, CDDA_DATASIZE);
1992-04-13 15:22:53 -03:00
break;
case cd_pnum:
case cd_index:
1997-01-03 19:00:51 -04:00
v = PyInt_FromLong(((CDPROGNUM *) data)->value);
1992-04-13 15:22:53 -03:00
break;
case cd_ptime:
case cd_atime:
#define ptr ((struct cdtimecode *) data)
1997-01-03 19:00:51 -04:00
v = Py_BuildValue("(iii)",
ptr->mhi * 10 + ptr->mlo,
ptr->shi * 10 + ptr->slo,
ptr->fhi * 10 + ptr->flo);
#undef ptr
1992-04-13 15:22:53 -03:00
break;
case cd_catalog:
1997-01-03 19:00:51 -04:00
v = PyString_FromStringAndSize(NULL, 13);
p = PyString_AsString(v);
1992-04-13 15:22:53 -03:00
for (i = 0; i < 13; i++)
*p++ = ((char *) data)[i] + '0';
break;
case cd_ident:
#define ptr ((struct cdident *) data)
1997-01-03 19:00:51 -04:00
v = PyString_FromStringAndSize(NULL, 12);
p = PyString_AsString(v);
CDsbtoa(p, ptr->country, 2);
1992-04-13 15:22:53 -03:00
p += 2;
CDsbtoa(p, ptr->owner, 3);
1992-04-13 15:22:53 -03:00
p += 3;
*p++ = ptr->year[0] + '0';
*p++ = ptr->year[1] + '0';
*p++ = ptr->serial[0] + '0';
*p++ = ptr->serial[1] + '0';
*p++ = ptr->serial[2] + '0';
*p++ = ptr->serial[3] + '0';
*p++ = ptr->serial[4] + '0';
#undef ptr
1992-04-13 15:22:53 -03:00
break;
case cd_control:
1997-01-03 19:00:51 -04:00
v = PyInt_FromLong((long) *((unchar *) data));
1992-04-13 15:22:53 -03:00
break;
}
1997-01-03 19:00:51 -04:00
PyTuple_SetItem(args, 2, v);
if (PyErr_Occurred()) {
Py_DECREF(args);
1992-04-13 15:22:53 -03:00
return;
}
1997-01-03 19:00:51 -04:00
result = PyEval_CallObject(self->ob_cdcallbacks[type].ob_cdcallback,
args);
Py_DECREF(args);
Py_XDECREF(result);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_deleteparser(self, args)
cdparserobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int i;
if (!PyArg_ParseTuple(args, ":deleteparser"))
1992-04-13 15:22:53 -03:00
return NULL;
CDdeleteparser(self->ob_cdparser);
self->ob_cdparser = NULL;
/* no sense in keeping the callbacks, so remove them */
for (i = 0; i < NCALLBACKS; i++) {
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
1992-04-13 15:22:53 -03:00
self->ob_cdcallbacks[i].ob_cdcallback = NULL;
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
1992-04-13 15:22:53 -03:00
self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_parseframe(self, args)
cdparserobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
char *cdfp;
int length;
CDFRAME *p;
if (!PyArg_ParseTuple(args, "s#:parseframe", &cdfp, &length))
1992-04-13 15:22:53 -03:00
return NULL;
if (length % sizeof(CDFRAME) != 0) {
1997-01-03 19:00:51 -04:00
PyErr_SetString(PyExc_TypeError, "bad length");
1992-04-13 15:22:53 -03:00
return NULL;
}
p = (CDFRAME *) cdfp;
while (length > 0) {
CDparseframe(self->ob_cdparser, p);
length -= sizeof(CDFRAME);
p++;
1997-01-03 19:00:51 -04:00
if (PyErr_Occurred())
1992-04-13 15:22:53 -03:00
return NULL;
}
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_removecallback(self, args)
cdparserobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int type;
if (!PyArg_ParseTuple(args, "i:removecallback", &type))
1992-04-13 15:22:53 -03:00
return NULL;
1992-08-03 16:06:59 -03:00
if (type < 0 || type >= NCALLBACKS) {
1997-01-03 19:00:51 -04:00
PyErr_SetString(PyExc_TypeError, "bad type");
1992-08-03 16:06:59 -03:00
return NULL;
}
1992-04-13 15:22:53 -03:00
CDremovecallback(self->ob_cdparser, (CDDATATYPES) type);
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
1992-04-13 15:22:53 -03:00
self->ob_cdcallbacks[type].ob_cdcallback = NULL;
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
1992-04-13 15:22:53 -03:00
self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL;
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_resetparser(self, args)
cdparserobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":resetparser"))
1992-04-13 15:22:53 -03:00
return NULL;
CDresetparser(self->ob_cdparser);
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-08-03 16:06:59 -03:00
CD_addcallback(self, args)
1992-04-13 15:22:53 -03:00
cdparserobject *self;
1997-01-03 19:00:51 -04:00
PyObject *args;
1992-04-13 15:22:53 -03:00
{
int type;
1997-01-03 19:00:51 -04:00
PyObject *func, *funcarg;
1992-04-13 15:22:53 -03:00
/* XXX - more work here */
if (!PyArg_ParseTuple(args, "iOO:addcallback", &type, &func, &funcarg))
1992-04-13 15:22:53 -03:00
return NULL;
if (type < 0 || type >= NCALLBACKS) {
1997-01-03 19:00:51 -04:00
PyErr_SetString(PyExc_TypeError, "argument out of range");
1992-04-13 15:22:53 -03:00
return NULL;
}
#ifdef CDsetcallback
1997-01-03 19:00:51 -04:00
CDaddcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
(void *) self);
1992-08-03 16:06:59 -03:00
#else
1997-01-03 19:00:51 -04:00
CDsetcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
(void *) self);
1992-08-03 16:06:59 -03:00
#endif
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
Py_INCREF(func);
self->ob_cdcallbacks[type].ob_cdcallback = func;
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
Py_INCREF(funcarg);
self->ob_cdcallbacks[type].ob_cdcallbackarg = funcarg;
1992-04-13 15:22:53 -03:00
/*
if (type == cd_audio) {
sigfpe_[_UNDERFL].repls = _ZERO;
1997-01-03 19:00:51 -04:00
handle_sigfpes(_ON, _EN_UNDERFL, NULL,
_ABORT_ON_ERROR, NULL);
}
*/
1997-01-03 19:00:51 -04:00
Py_INCREF(Py_None);
return Py_None;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyMethodDef cdparser_methods[] = {
{"addcallback", (PyCFunction)CD_addcallback, 1},
{"deleteparser", (PyCFunction)CD_deleteparser, 1},
{"parseframe", (PyCFunction)CD_parseframe, 1},
{"removecallback", (PyCFunction)CD_removecallback, 1},
{"resetparser", (PyCFunction)CD_resetparser, 1},
/* backward compatibility */
{"setcallback", (PyCFunction)CD_addcallback, 1},
1992-04-13 15:22:53 -03:00
{NULL, NULL} /* sentinel */
};
static void
cdparser_dealloc(self)
cdparserobject *self;
{
int i;
for (i = 0; i < NCALLBACKS; i++) {
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
1992-04-13 15:22:53 -03:00
self->ob_cdcallbacks[i].ob_cdcallback = NULL;
1997-01-03 19:00:51 -04:00
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
1992-04-13 15:22:53 -03:00
self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
}
CDdeleteparser(self->ob_cdparser);
PyObject_Del(self);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
cdparser_getattr(self, name)
cdparserobject *self;
1992-04-13 15:22:53 -03:00
char *name;
{
if (self->ob_cdparser == NULL) {
1997-01-03 19:00:51 -04:00
PyErr_SetString(PyExc_RuntimeError, "no parser active");
return NULL;
}
1997-01-03 19:00:51 -04:00
return Py_FindMethod(cdparser_methods, (PyObject *)self, name);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
PyTypeObject CdParsertype = {
PyObject_HEAD_INIT(&PyType_Type)
1992-04-13 15:22:53 -03:00
0, /*ob_size*/
"cdparser", /*tp_name*/
sizeof(cdparserobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdparser_dealloc, /*tp_dealloc*/
1992-04-13 15:22:53 -03:00
0, /*tp_print*/
(getattrfunc)cdparser_getattr, /*tp_getattr*/
1992-04-13 15:22:53 -03:00
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
};
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
newcdparserobject(cdp)
CDPARSER *cdp;
{
cdparserobject *p;
int i;
p = PyObject_New(cdparserobject, &CdParsertype);
1992-04-13 15:22:53 -03:00
if (p == NULL)
return NULL;
p->ob_cdparser = cdp;
for (i = 0; i < NCALLBACKS; i++) {
p->ob_cdcallbacks[i].ob_cdcallback = NULL;
p->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
}
1997-01-03 19:00:51 -04:00
return (PyObject *) p;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
1992-04-13 15:22:53 -03:00
CD_createparser(self, args)
1997-01-03 19:00:51 -04:00
PyObject *self, *args;
1992-04-13 15:22:53 -03:00
{
CDPARSER *cdp;
if (!PyArg_ParseTuple(args, ":createparser"))
1992-04-13 15:22:53 -03:00
return NULL;
cdp = CDcreateparser();
if (cdp == NULL) {
1997-01-03 19:00:51 -04:00
PyErr_SetString(CdError, "createparser failed");
1992-04-13 15:22:53 -03:00
return NULL;
}
return newcdparserobject(cdp);
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_msftoframe(self, args)
1997-01-03 19:00:51 -04:00
PyObject *self, *args;
1992-04-13 15:22:53 -03:00
{
int min, sec, frame;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "iii:msftoframe", &min, &sec, &frame))
1992-04-13 15:22:53 -03:00
return NULL;
1997-01-03 19:00:51 -04:00
return PyInt_FromLong((long) CDmsftoframe(min, sec, frame));
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyMethodDef CD_methods[] = {
{"open", (PyCFunction)CD_open, 1},
{"createparser", (PyCFunction)CD_createparser, 1},
{"msftoframe", (PyCFunction)CD_msftoframe, 1},
1992-04-13 15:22:53 -03:00
{NULL, NULL} /* Sentinel */
};
void
initcd()
{
1997-01-03 19:00:51 -04:00
PyObject *m, *d;
1997-01-03 19:00:51 -04:00
m = Py_InitModule("cd", CD_methods);
d = PyModule_GetDict(m);
1997-10-01 01:43:39 -03:00
CdError = PyErr_NewException("cd.error", NULL, NULL);
1997-01-03 19:00:51 -04:00
PyDict_SetItemString(d, "error", CdError);
/* Identifiers for the different types of callbacks from the parser */
1997-01-03 19:00:51 -04:00
PyDict_SetItemString(d, "audio", PyInt_FromLong((long) cd_audio));
PyDict_SetItemString(d, "pnum", PyInt_FromLong((long) cd_pnum));
PyDict_SetItemString(d, "index", PyInt_FromLong((long) cd_index));
PyDict_SetItemString(d, "ptime", PyInt_FromLong((long) cd_ptime));
PyDict_SetItemString(d, "atime", PyInt_FromLong((long) cd_atime));
PyDict_SetItemString(d, "catalog", PyInt_FromLong((long) cd_catalog));
PyDict_SetItemString(d, "ident", PyInt_FromLong((long) cd_ident));
PyDict_SetItemString(d, "control", PyInt_FromLong((long) cd_control));
/* Block size information for digital audio data */
1997-01-03 19:00:51 -04:00
PyDict_SetItemString(d, "DATASIZE",
PyInt_FromLong((long) CDDA_DATASIZE));
PyDict_SetItemString(d, "BLOCKSIZE",
PyInt_FromLong((long) CDDA_BLOCKSIZE));
/* Possible states for the cd player */
1997-01-03 19:00:51 -04:00
PyDict_SetItemString(d, "ERROR", PyInt_FromLong((long) CD_ERROR));
PyDict_SetItemString(d, "NODISC", PyInt_FromLong((long) CD_NODISC));
PyDict_SetItemString(d, "READY", PyInt_FromLong((long) CD_READY));
PyDict_SetItemString(d, "PLAYING", PyInt_FromLong((long) CD_PLAYING));
PyDict_SetItemString(d, "PAUSED", PyInt_FromLong((long) CD_PAUSED));
PyDict_SetItemString(d, "STILL", PyInt_FromLong((long) CD_STILL));
#ifdef CD_CDROM /* only newer versions of the library */
1997-01-03 19:00:51 -04:00
PyDict_SetItemString(d, "CDROM", PyInt_FromLong((long) CD_CDROM));
#endif
1997-01-03 19:00:51 -04:00
if (PyErr_Occurred())
Py_FatalError("can't initialize module cd");
1992-04-13 15:22:53 -03:00
}