cpython/Modules/cdmodule.c

801 lines
22 KiB
C
Raw Normal View History

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
1992-04-13 15:22:53 -03:00
typedef struct {
PyObject_HEAD
CDPLAYER *ob_cdplayer;
1992-04-13 15:22:53 -03:00
} cdplayerobject;
static PyObject *CdError; /* exception cd.error */
1997-01-03 19:00:51 -04:00
static PyObject *
CD_allowremoval(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":allowremoval"))
return NULL;
1992-04-13 15:22:53 -03:00
CDallowremoval(self->ob_cdplayer);
1992-04-13 15:22:53 -03: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 *
CD_preventremoval(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":preventremoval"))
return NULL;
1992-04-13 15:22:53 -03:00
CDpreventremoval(self->ob_cdplayer);
1992-04-13 15:22:53 -03: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 *
CD_bestreadsize(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":bestreadsize"))
return NULL;
1992-04-13 15:22:53 -03: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 *
CD_close(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":close"))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDclose(self->ob_cdplayer)) {
PyErr_SetFromErrno(CdError); /* XXX - ??? */
return NULL;
}
self->ob_cdplayer = NULL;
1992-04-13 15:22:53 -03: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 *
CD_eject(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":eject"))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDeject(self->ob_cdplayer)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "eject failed");
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_getstatus(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, ":getstatus"))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDgetstatus(self->ob_cdplayer, &status)) {
PyErr_SetFromErrno(CdError); /* XXX - ??? */
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_gettrackinfo(cdplayerobject *self, 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))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDgettrackinfo(self->ob_cdplayer, track, &info)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "gettrackinfo failed");
return NULL;
}
1992-04-13 15:22:53 -03:00
return Py_BuildValue("((iii)(iii))",
info.start_min, info.start_sec, info.start_frame,
info.total_min, info.total_sec, info.total_frame);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_msftoblock(cdplayerobject *self, PyObject *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:msftoblock", &min, &sec, &frame))
return NULL;
1992-04-13 15:22:53 -03: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 *
CD_play(cdplayerobject *self, 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))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDplay(self->ob_cdplayer, start, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "play failed");
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_playabs(cdplayerobject *self, 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))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDplayabs(self->ob_cdplayer, min, sec, frame, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "playabs failed");
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_playtrack(cdplayerobject *self, 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))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDplaytrack(self->ob_cdplayer, start, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "playtrack failed");
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_playtrackabs(cdplayerobject *self, 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,
&frame, &play))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDplaytrackabs(self->ob_cdplayer, track, min, sec, frame, play)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "playtrackabs failed");
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_readda(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
int numframes, n;
PyObject *result;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "i:readda", &numframes))
return NULL;
1992-04-13 15:22:53 -03:00
result = PyString_FromStringAndSize(NULL, numframes * sizeof(CDFRAME));
if (result == NULL)
return NULL;
1992-04-13 15:22:53 -03:00
n = CDreadda(self->ob_cdplayer,
(CDFRAME *) PyString_AsString(result), numframes);
if (n == -1) {
Py_DECREF(result);
PyErr_SetFromErrno(CdError);
return NULL;
}
if (n < numframes)
_PyString_Resize(&result, n * sizeof(CDFRAME));
1992-04-13 15:22:53 -03:00
return result;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_seek(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
int min, sec, frame;
long PyTryBlock;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "iii:seek", &min, &sec, &frame))
return NULL;
1992-04-13 15:22:53 -03:00
PyTryBlock = CDseek(self->ob_cdplayer, min, sec, frame);
if (PyTryBlock == -1) {
PyErr_SetFromErrno(CdError);
return NULL;
}
1992-04-13 15:22:53 -03:00
return PyInt_FromLong(PyTryBlock);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_seektrack(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
int track;
long PyTryBlock;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "i:seektrack", &track))
return NULL;
1992-04-13 15:22:53 -03:00
PyTryBlock = CDseektrack(self->ob_cdplayer, track);
if (PyTryBlock == -1) {
PyErr_SetFromErrno(CdError);
return NULL;
}
1992-04-13 15:22:53 -03: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(cdplayerobject *self, PyObject *args)
{
unsigned long PyTryBlock;
if (!PyArg_ParseTuple(args, "l:seekblock", &PyTryBlock))
return NULL;
PyTryBlock = CDseekblock(self->ob_cdplayer, PyTryBlock);
if (PyTryBlock == (unsigned long) -1) {
PyErr_SetFromErrno(CdError);
return NULL;
}
return PyInt_FromLong(PyTryBlock);
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_stop(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":stop"))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDstop(self->ob_cdplayer)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "stop failed");
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_togglepause(cdplayerobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
CDSTATUS status;
if (!PyArg_ParseTuple(args, ":togglepause"))
return NULL;
1992-04-13 15:22:53 -03:00
if (!CDtogglepause(self->ob_cdplayer)) {
if (CDgetstatus(self->ob_cdplayer, &status) &&
status.state == CD_NODISC)
PyErr_SetString(CdError, "no disc in player");
else
PyErr_SetString(CdError, "togglepause failed");
return NULL;
}
1992-04-13 15:22:53 -03: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, METH_VARARGS},
{"bestreadsize", (PyCFunction)CD_bestreadsize, METH_VARARGS},
{"close", (PyCFunction)CD_close, METH_VARARGS},
{"eject", (PyCFunction)CD_eject, METH_VARARGS},
{"getstatus", (PyCFunction)CD_getstatus, METH_VARARGS},
{"gettrackinfo", (PyCFunction)CD_gettrackinfo, METH_VARARGS},
{"msftoblock", (PyCFunction)CD_msftoblock, METH_VARARGS},
{"play", (PyCFunction)CD_play, METH_VARARGS},
{"playabs", (PyCFunction)CD_playabs, METH_VARARGS},
{"playtrack", (PyCFunction)CD_playtrack, METH_VARARGS},
{"playtrackabs", (PyCFunction)CD_playtrackabs, METH_VARARGS},
{"preventremoval", (PyCFunction)CD_preventremoval, METH_VARARGS},
{"readda", (PyCFunction)CD_readda, METH_VARARGS},
{"seek", (PyCFunction)CD_seek, METH_VARARGS},
{"seekblock", (PyCFunction)CD_seekblock, METH_VARARGS},
{"seektrack", (PyCFunction)CD_seektrack, METH_VARARGS},
{"stop", (PyCFunction)CD_stop, METH_VARARGS},
{"togglepause", (PyCFunction)CD_togglepause, METH_VARARGS},
{NULL, NULL} /* sentinel */
1992-04-13 15:22:53 -03:00
};
static void
cdplayer_dealloc(cdplayerobject *self)
1992-04-13 15:22:53 -03:00
{
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(cdplayerobject *self, char *name)
1992-04-13 15:22:53 -03:00
{
if (self->ob_cdplayer == NULL) {
PyErr_SetString(PyExc_RuntimeError, "no player active");
return NULL;
}
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)
0, /*ob_size*/
"cd.cdplayer", /*tp_name*/
sizeof(cdplayerobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdplayer_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)cdplayer_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
1992-04-13 15:22:53 -03:00
};
1997-01-03 19:00:51 -04:00
static PyObject *
newcdplayerobject(CDPLAYER *cdp)
1992-04-13 15:22:53 -03:00
{
cdplayerobject *p;
1992-04-13 15:22:53 -03:00
p = PyObject_New(cdplayerobject, &CdPlayertype);
if (p == NULL)
return NULL;
p->ob_cdplayer = cdp;
return (PyObject *) p;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_open(PyObject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
char *dev, *direction;
CDPLAYER *cdp;
1992-04-13 15:22:53 -03:00
/*
* 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) {
PyErr_SetFromErrno(CdError);
return NULL;
}
1992-04-13 15:22:53 -03:00
return newcdplayerobject(cdp);
1992-04-13 15:22:53 -03:00
}
typedef struct {
PyObject_HEAD
CDPARSER *ob_cdparser;
struct {
PyObject *ob_cdcallback;
PyObject *ob_cdcallbackarg;
} ob_cdcallbacks[NCALLBACKS];
1992-04-13 15:22:53 -03:00
} cdparserobject;
static void
CD_callback(void *arg, CDDATATYPES type, void *data)
1992-04-13 15:22:53 -03:00
{
PyObject *result, *args, *v = NULL;
char *p;
int i;
cdparserobject *self;
self = (cdparserobject *) arg;
args = PyTuple_New(3);
if (args == NULL)
return;
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));
switch (type) {
case cd_audio:
v = PyString_FromStringAndSize(data, CDDA_DATASIZE);
break;
case cd_pnum:
case cd_index:
v = PyInt_FromLong(((CDPROGNUM *) data)->value);
break;
case cd_ptime:
case cd_atime:
#define ptr ((struct cdtimecode *) data)
v = Py_BuildValue("(iii)",
ptr->mhi * 10 + ptr->mlo,
ptr->shi * 10 + ptr->slo,
ptr->fhi * 10 + ptr->flo);
#undef ptr
break;
case cd_catalog:
v = PyString_FromStringAndSize(NULL, 13);
p = PyString_AsString(v);
for (i = 0; i < 13; i++)
*p++ = ((char *) data)[i] + '0';
break;
case cd_ident:
#define ptr ((struct cdident *) data)
v = PyString_FromStringAndSize(NULL, 12);
p = PyString_AsString(v);
CDsbtoa(p, ptr->country, 2);
p += 2;
CDsbtoa(p, ptr->owner, 3);
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
break;
case cd_control:
v = PyInt_FromLong((long) *((unchar *) data));
break;
}
PyTuple_SetItem(args, 2, v);
if (PyErr_Occurred()) {
Py_DECREF(args);
return;
}
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 *
CD_deleteparser(cdparserobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
int i;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, ":deleteparser"))
return NULL;
1992-04-13 15:22:53 -03:00
CDdeleteparser(self->ob_cdparser);
self->ob_cdparser = NULL;
1992-04-13 15:22:53 -03:00
/* no sense in keeping the callbacks, so remove them */
for (i = 0; i < NCALLBACKS; i++) {
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_parseframe(cdparserobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
char *cdfp;
int length;
CDFRAME *p;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "s#:parseframe", &cdfp, &length))
return NULL;
1992-04-13 15:22:53 -03:00
if (length % sizeof(CDFRAME) != 0) {
PyErr_SetString(PyExc_TypeError, "bad length");
return NULL;
}
1992-04-13 15:22:53 -03:00
p = (CDFRAME *) cdfp;
while (length > 0) {
CDparseframe(self->ob_cdparser, p);
length -= sizeof(CDFRAME);
p++;
if (PyErr_Occurred())
return NULL;
}
1992-04-13 15:22:53 -03: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 *
CD_removecallback(cdparserobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
int type;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, "i:removecallback", &type))
return NULL;
1992-04-13 15:22:53 -03:00
if (type < 0 || type >= NCALLBACKS) {
PyErr_SetString(PyExc_TypeError, "bad type");
return NULL;
}
1992-08-03 16:06:59 -03:00
CDremovecallback(self->ob_cdparser, (CDDATATYPES) type);
1992-04-13 15:22:53 -03:00
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
self->ob_cdcallbacks[type].ob_cdcallback = NULL;
1992-04-13 15:22:53 -03:00
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL;
1992-04-13 15:22:53 -03: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 *
CD_resetparser(cdparserobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
if (!PyArg_ParseTuple(args, ":resetparser"))
return NULL;
1992-04-13 15:22:53 -03:00
CDresetparser(self->ob_cdparser);
1992-04-13 15:22:53 -03: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 *
CD_addcallback(cdparserobject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
int type;
PyObject *func, *funcarg;
1992-04-13 15:22:53 -03:00
/* XXX - more work here */
if (!PyArg_ParseTuple(args, "iOO:addcallback", &type, &func, &funcarg))
return NULL;
1992-04-13 15:22:53 -03:00
if (type < 0 || type >= NCALLBACKS) {
PyErr_SetString(PyExc_TypeError, "argument out of range");
return NULL;
}
1992-04-13 15:22:53 -03:00
#ifdef CDsetcallback
CDaddcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
(void *) self);
1992-08-03 16:06:59 -03:00
#else
CDsetcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
(void *) self);
1992-08-03 16:06:59 -03:00
#endif
Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
Py_INCREF(func);
self->ob_cdcallbacks[type].ob_cdcallback = func;
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;
handle_sigfpes(_ON, _EN_UNDERFL, NULL,
_ABORT_ON_ERROR, NULL);
}
*/
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, METH_VARARGS},
{"deleteparser", (PyCFunction)CD_deleteparser, METH_VARARGS},
{"parseframe", (PyCFunction)CD_parseframe, METH_VARARGS},
{"removecallback", (PyCFunction)CD_removecallback, METH_VARARGS},
{"resetparser", (PyCFunction)CD_resetparser, METH_VARARGS},
/* backward compatibility */
{"setcallback", (PyCFunction)CD_addcallback, METH_VARARGS},
{NULL, NULL} /* sentinel */
1992-04-13 15:22:53 -03:00
};
static void
cdparser_dealloc(cdparserobject *self)
1992-04-13 15:22:53 -03:00
{
int i;
1992-04-13 15:22:53 -03:00
for (i = 0; i < NCALLBACKS; i++) {
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
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(cdparserobject *self, char *name)
1992-04-13 15:22:53 -03:00
{
if (self->ob_cdparser == NULL) {
PyErr_SetString(PyExc_RuntimeError, "no parser active");
return NULL;
}
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)
0, /*ob_size*/
"cd.cdparser", /*tp_name*/
sizeof(cdparserobject), /*tp_size*/
0, /*tp_itemsize*/
/* methods */
(destructor)cdparser_dealloc, /*tp_dealloc*/
0, /*tp_print*/
(getattrfunc)cdparser_getattr, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
1992-04-13 15:22:53 -03:00
};
1997-01-03 19:00:51 -04:00
static PyObject *
newcdparserobject(CDPARSER *cdp)
1992-04-13 15:22:53 -03:00
{
cdparserobject *p;
int i;
1992-04-13 15:22:53 -03:00
p = PyObject_New(cdparserobject, &CdParsertype);
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;
}
return (PyObject *) p;
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_createparser(PyObject *self, PyObject *args)
1992-04-13 15:22:53 -03:00
{
CDPARSER *cdp;
1992-04-13 15:22:53 -03:00
if (!PyArg_ParseTuple(args, ":createparser"))
return NULL;
cdp = CDcreateparser();
if (cdp == NULL) {
PyErr_SetString(CdError, "createparser failed");
return NULL;
}
1992-04-13 15:22:53 -03:00
return newcdparserobject(cdp);
1992-04-13 15:22:53 -03:00
}
1997-01-03 19:00:51 -04:00
static PyObject *
CD_msftoframe(PyObject *self, PyObject *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))
return NULL;
1992-04-13 15:22:53 -03: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, METH_VARARGS},
{"createparser", (PyCFunction)CD_createparser, METH_VARARGS},
{"msftoframe", (PyCFunction)CD_msftoframe, METH_VARARGS},
{NULL, NULL} /* Sentinel */
1992-04-13 15:22:53 -03:00
};
void
initcd(void)
1992-04-13 15:22:53 -03:00
{
PyObject *m, *d;
if (PyErr_WarnPy3k("the cd module has been removed in "
"Python 3.0", 2) < 0)
return;
m = Py_InitModule("cd", CD_methods);
if (m == NULL)
return;
d = PyModule_GetDict(m);
CdError = PyErr_NewException("cd.error", NULL, NULL);
PyDict_SetItemString(d, "error", CdError);
/* Identifiers for the different types of callbacks from the parser */
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 */
PyDict_SetItemString(d, "DATASIZE",
PyInt_FromLong((long) CDDA_DATASIZE));
PyDict_SetItemString(d, "BLOCKSIZE",
PyInt_FromLong((long) CDDA_BLOCKSIZE));
/* Possible states for the cd player */
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 */
PyDict_SetItemString(d, "CDROM", PyInt_FromLong((long) CD_CDROM));
#endif
1992-04-13 15:22:53 -03:00
}