blob: f0dd599c03702a5fe0bafadd79c6a12d1c98e671 [file] [log] [blame]
Guido van Rossum16b8f301992-04-13 18:22:53 +00001/* CD module -- interface to Mark Callow's and Roger Chickering's */
2 /* CD Audio Library (CD). */
3
4#include <sys/types.h>
5#include <cdaudio.h>
Roger E. Massebf0c3ca1997-01-03 23:00:51 +00006#include "Python.h"
Guido van Rossum16b8f301992-04-13 18:22:53 +00007
8#define NCALLBACKS 8
9
10typedef struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000011 PyObject_HEAD
Guido van Rossum16b8f301992-04-13 18:22:53 +000012 CDPLAYER *ob_cdplayer;
13} cdplayerobject;
14
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000015static PyObject *CdError; /* exception cd.error */
Sjoerd Mullender46927ba1992-09-24 10:48:40 +000016
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000017static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000018CD_allowremoval(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +000019{
Guido van Rossum43713e52000-02-29 13:59:29 +000020 if (!PyArg_ParseTuple(args, ":allowremoval"))
Guido van Rossum16b8f301992-04-13 18:22:53 +000021 return NULL;
22
23 CDallowremoval(self->ob_cdplayer);
24
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000025 Py_INCREF(Py_None);
26 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +000027}
28
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000029static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000030CD_preventremoval(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +000031{
Guido van Rossum43713e52000-02-29 13:59:29 +000032 if (!PyArg_ParseTuple(args, ":preventremoval"))
Guido van Rossum16b8f301992-04-13 18:22:53 +000033 return NULL;
34
35 CDpreventremoval(self->ob_cdplayer);
36
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000037 Py_INCREF(Py_None);
38 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +000039}
40
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000041static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000042CD_bestreadsize(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +000043{
Guido van Rossum43713e52000-02-29 13:59:29 +000044 if (!PyArg_ParseTuple(args, ":bestreadsize"))
Guido van Rossum16b8f301992-04-13 18:22:53 +000045 return NULL;
46
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000047 return PyInt_FromLong((long) CDbestreadsize(self->ob_cdplayer));
Guido van Rossum16b8f301992-04-13 18:22:53 +000048}
49
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000050static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000051CD_close(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +000052{
Guido van Rossum43713e52000-02-29 13:59:29 +000053 if (!PyArg_ParseTuple(args, ":close"))
Guido van Rossum16b8f301992-04-13 18:22:53 +000054 return NULL;
55
56 if (!CDclose(self->ob_cdplayer)) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000057 PyErr_SetFromErrno(CdError); /* XXX - ??? */
Guido van Rossum16b8f301992-04-13 18:22:53 +000058 return NULL;
59 }
60 self->ob_cdplayer = NULL;
61
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000062 Py_INCREF(Py_None);
63 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +000064}
65
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000066static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000067CD_eject(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +000068{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +000069 CDSTATUS status;
70
Guido van Rossum43713e52000-02-29 13:59:29 +000071 if (!PyArg_ParseTuple(args, ":eject"))
Guido van Rossum16b8f301992-04-13 18:22:53 +000072 return NULL;
73
74 if (!CDeject(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +000075 if (CDgetstatus(self->ob_cdplayer, &status) &&
76 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000077 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +000078 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000079 PyErr_SetString(CdError, "eject failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +000080 return NULL;
81 }
82
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000083 Py_INCREF(Py_None);
84 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +000085}
86
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000087static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +000088CD_getstatus(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +000089{
90 CDSTATUS status;
91
Guido van Rossum43713e52000-02-29 13:59:29 +000092 if (!PyArg_ParseTuple(args, ":getstatus"))
Guido van Rossum16b8f301992-04-13 18:22:53 +000093 return NULL;
94
95 if (!CDgetstatus(self->ob_cdplayer, &status)) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000096 PyErr_SetFromErrno(CdError); /* XXX - ??? */
Guido van Rossum16b8f301992-04-13 18:22:53 +000097 return NULL;
98 }
99
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000100 return Py_BuildValue("(ii(iii)(iii)(iii)iiii)", status.state,
Guido van Rossumece6efe1992-04-15 15:56:11 +0000101 status.track, status.min, status.sec, status.frame,
102 status.abs_min, status.abs_sec, status.abs_frame,
103 status.total_min, status.total_sec, status.total_frame,
104 status.first, status.last, status.scsi_audio,
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000105 status.cur_block);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000106}
107
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000108static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000109CD_gettrackinfo(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000110{
111 int track;
112 CDTRACKINFO info;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000113 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000114
Guido van Rossum43713e52000-02-29 13:59:29 +0000115 if (!PyArg_ParseTuple(args, "i:gettrackinfo", &track))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000116 return NULL;
117
118 if (!CDgettrackinfo(self->ob_cdplayer, track, &info)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000119 if (CDgetstatus(self->ob_cdplayer, &status) &&
120 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000121 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000122 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000123 PyErr_SetString(CdError, "gettrackinfo failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000124 return NULL;
125 }
126
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000127 return Py_BuildValue("((iii)(iii))",
Guido van Rossum16b8f301992-04-13 18:22:53 +0000128 info.start_min, info.start_sec, info.start_frame,
129 info.total_min, info.total_sec, info.total_frame);
130}
131
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000132static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000133CD_msftoblock(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000134{
135 int min, sec, frame;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000136
Guido van Rossum43713e52000-02-29 13:59:29 +0000137 if (!PyArg_ParseTuple(args, "iii:msftoblock", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000138 return NULL;
139
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000140 return PyInt_FromLong((long) CDmsftoblock(self->ob_cdplayer,
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000141 min, sec, frame));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000142}
143
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000144static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000145CD_play(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000146{
147 int start, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000148 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000149
Guido van Rossum43713e52000-02-29 13:59:29 +0000150 if (!PyArg_ParseTuple(args, "ii:play", &start, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000151 return NULL;
152
153 if (!CDplay(self->ob_cdplayer, start, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000154 if (CDgetstatus(self->ob_cdplayer, &status) &&
155 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000156 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000157 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000158 PyErr_SetString(CdError, "play failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000159 return NULL;
160 }
161
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000162 Py_INCREF(Py_None);
163 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000164}
165
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000166static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000167CD_playabs(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000168{
169 int min, sec, frame, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000170 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000171
Guido van Rossum43713e52000-02-29 13:59:29 +0000172 if (!PyArg_ParseTuple(args, "iiii:playabs", &min, &sec, &frame, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000173 return NULL;
174
175 if (!CDplayabs(self->ob_cdplayer, min, sec, frame, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000176 if (CDgetstatus(self->ob_cdplayer, &status) &&
177 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000178 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000179 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000180 PyErr_SetString(CdError, "playabs failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000181 return NULL;
182 }
183
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000184 Py_INCREF(Py_None);
185 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000186}
187
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000188static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000189CD_playtrack(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000190{
191 int start, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000192 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000193
Guido van Rossum43713e52000-02-29 13:59:29 +0000194 if (!PyArg_ParseTuple(args, "ii:playtrack", &start, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000195 return NULL;
196
197 if (!CDplaytrack(self->ob_cdplayer, start, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000198 if (CDgetstatus(self->ob_cdplayer, &status) &&
199 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000200 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000201 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000202 PyErr_SetString(CdError, "playtrack failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000203 return NULL;
204 }
205
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000206 Py_INCREF(Py_None);
207 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000208}
209
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000210static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000211CD_playtrackabs(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000212{
213 int track, min, sec, frame, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000214 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000215
Guido van Rossum43713e52000-02-29 13:59:29 +0000216 if (!PyArg_ParseTuple(args, "iiiii:playtrackabs", &track, &min, &sec,
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000217 &frame, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000218 return NULL;
219
220 if (!CDplaytrackabs(self->ob_cdplayer, track, min, sec, frame, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000221 if (CDgetstatus(self->ob_cdplayer, &status) &&
222 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000223 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000224 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000225 PyErr_SetString(CdError, "playtrackabs failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000226 return NULL;
227 }
228
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000229 Py_INCREF(Py_None);
230 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000231}
232
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000233static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000234CD_readda(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000235{
236 int numframes, n;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000237 PyObject *result;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000238
Guido van Rossum43713e52000-02-29 13:59:29 +0000239 if (!PyArg_ParseTuple(args, "i:readda", &numframes))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000240 return NULL;
241
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000242 result = PyString_FromStringAndSize(NULL, numframes * sizeof(CDFRAME));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000243 if (result == NULL)
244 return NULL;
245
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000246 n = CDreadda(self->ob_cdplayer,
247 (CDFRAME *) PyString_AsString(result), numframes);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000248 if (n == -1) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000249 Py_DECREF(result);
250 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000251 return NULL;
252 }
253 if (n < numframes)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000254 if (_PyString_Resize(&result, n * sizeof(CDFRAME)))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000255 return NULL;
256
257 return result;
258}
259
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000260static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000261CD_seek(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000262{
263 int min, sec, frame;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000264 long PyTryBlock;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000265
Guido van Rossum43713e52000-02-29 13:59:29 +0000266 if (!PyArg_ParseTuple(args, "iii:seek", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000267 return NULL;
268
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000269 PyTryBlock = CDseek(self->ob_cdplayer, min, sec, frame);
270 if (PyTryBlock == -1) {
271 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000272 return NULL;
273 }
274
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000275 return PyInt_FromLong(PyTryBlock);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000276}
277
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000278static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000279CD_seektrack(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000280{
281 int track;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000282 long PyTryBlock;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000283
Guido van Rossum43713e52000-02-29 13:59:29 +0000284 if (!PyArg_ParseTuple(args, "i:seektrack", &track))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000285 return NULL;
286
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000287 PyTryBlock = CDseektrack(self->ob_cdplayer, track);
288 if (PyTryBlock == -1) {
289 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000290 return NULL;
291 }
292
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000293 return PyInt_FromLong(PyTryBlock);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000294}
295
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000296static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000297CD_seekblock(cdplayerobject *self, PyObject *args)
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000298{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000299 unsigned long PyTryBlock;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000300
Guido van Rossum43713e52000-02-29 13:59:29 +0000301 if (!PyArg_ParseTuple(args, "l:seekblock", &PyTryBlock))
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000302 return NULL;
303
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000304 PyTryBlock = CDseekblock(self->ob_cdplayer, PyTryBlock);
305 if (PyTryBlock == (unsigned long) -1) {
306 PyErr_SetFromErrno(CdError);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000307 return NULL;
308 }
309
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000310 return PyInt_FromLong(PyTryBlock);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000311}
312
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000313static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000314CD_stop(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000315{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000316 CDSTATUS status;
317
Guido van Rossum43713e52000-02-29 13:59:29 +0000318 if (!PyArg_ParseTuple(args, ":stop"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000319 return NULL;
320
321 if (!CDstop(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000322 if (CDgetstatus(self->ob_cdplayer, &status) &&
323 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000324 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000325 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000326 PyErr_SetString(CdError, "stop failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000327 return NULL;
328 }
329
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000330 Py_INCREF(Py_None);
331 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000332}
333
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000334static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000335CD_togglepause(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000336{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000337 CDSTATUS status;
338
Guido van Rossum43713e52000-02-29 13:59:29 +0000339 if (!PyArg_ParseTuple(args, ":togglepause"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000340 return NULL;
341
342 if (!CDtogglepause(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000343 if (CDgetstatus(self->ob_cdplayer, &status) &&
344 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000345 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000346 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000347 PyErr_SetString(CdError, "togglepause failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000348 return NULL;
349 }
350
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000351 Py_INCREF(Py_None);
352 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000353}
354
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000355static PyMethodDef cdplayer_methods[] = {
356 {"allowremoval", (PyCFunction)CD_allowremoval, 1},
357 {"bestreadsize", (PyCFunction)CD_bestreadsize, 1},
358 {"close", (PyCFunction)CD_close, 1},
359 {"eject", (PyCFunction)CD_eject, 1},
360 {"getstatus", (PyCFunction)CD_getstatus, 1},
361 {"gettrackinfo", (PyCFunction)CD_gettrackinfo, 1},
362 {"msftoblock", (PyCFunction)CD_msftoblock, 1},
363 {"play", (PyCFunction)CD_play, 1},
364 {"playabs", (PyCFunction)CD_playabs, 1},
365 {"playtrack", (PyCFunction)CD_playtrack, 1},
366 {"playtrackabs", (PyCFunction)CD_playtrackabs, 1},
367 {"preventremoval", (PyCFunction)CD_preventremoval, 1},
368 {"readda", (PyCFunction)CD_readda, 1},
369 {"seek", (PyCFunction)CD_seek, 1},
370 {"seekblock", (PyCFunction)CD_seekblock, 1},
371 {"seektrack", (PyCFunction)CD_seektrack, 1},
372 {"stop", (PyCFunction)CD_stop, 1},
373 {"togglepause", (PyCFunction)CD_togglepause, 1},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000374 {NULL, NULL} /* sentinel */
375};
376
377static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000378cdplayer_dealloc(cdplayerobject *self)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000379{
380 if (self->ob_cdplayer != NULL)
381 CDclose(self->ob_cdplayer);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000382 PyObject_Del(self);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000383}
384
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000385static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000386cdplayer_getattr(cdplayerobject *self, char *name)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000387{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000388 if (self->ob_cdplayer == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000389 PyErr_SetString(PyExc_RuntimeError, "no player active");
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000390 return NULL;
391 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000392 return Py_FindMethod(cdplayer_methods, (PyObject *)self, name);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000393}
394
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000395PyTypeObject CdPlayertype = {
396 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000397 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000398 "cd.cdplayer", /*tp_name*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000399 sizeof(cdplayerobject), /*tp_size*/
400 0, /*tp_itemsize*/
401 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000402 (destructor)cdplayer_dealloc, /*tp_dealloc*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000403 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000404 (getattrfunc)cdplayer_getattr, /*tp_getattr*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000405 0, /*tp_setattr*/
406 0, /*tp_compare*/
407 0, /*tp_repr*/
408};
409
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000410static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000411newcdplayerobject(CDPLAYER *cdp)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000412{
413 cdplayerobject *p;
414
Guido van Rossumb18618d2000-05-03 23:44:39 +0000415 p = PyObject_New(cdplayerobject, &CdPlayertype);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000416 if (p == NULL)
417 return NULL;
418 p->ob_cdplayer = cdp;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000419 return (PyObject *) p;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000420}
421
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000422static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000423CD_open(PyObject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000424{
425 char *dev, *direction;
426 CDPLAYER *cdp;
427
428 /*
429 * Variable number of args.
430 * First defaults to "None", second defaults to "r".
431 */
432 dev = NULL;
433 direction = "r";
Guido van Rossum43713e52000-02-29 13:59:29 +0000434 if (!PyArg_ParseTuple(args, "|zs:open", &dev, &direction))
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000435 return NULL;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000436
437 cdp = CDopen(dev, direction);
438 if (cdp == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000439 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000440 return NULL;
441 }
442
443 return newcdplayerobject(cdp);
444}
445
446typedef struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000447 PyObject_HEAD
Guido van Rossum16b8f301992-04-13 18:22:53 +0000448 CDPARSER *ob_cdparser;
449 struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000450 PyObject *ob_cdcallback;
451 PyObject *ob_cdcallbackarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000452 } ob_cdcallbacks[NCALLBACKS];
453} cdparserobject;
454
455static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000456CD_callback(void *arg, CDDATATYPES type, void *data)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000457{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000458 PyObject *result, *args, *v = NULL;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000459 char *p;
460 int i;
461 cdparserobject *self;
462
463 self = (cdparserobject *) arg;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000464 args = PyTuple_New(3);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000465 if (args == NULL)
466 return;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000467 Py_INCREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
468 PyTuple_SetItem(args, 0, self->ob_cdcallbacks[type].ob_cdcallbackarg);
469 PyTuple_SetItem(args, 1, PyInt_FromLong((long) type));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000470 switch (type) {
471 case cd_audio:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000472 v = PyString_FromStringAndSize(data, CDDA_DATASIZE);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000473 break;
474 case cd_pnum:
475 case cd_index:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000476 v = PyInt_FromLong(((CDPROGNUM *) data)->value);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000477 break;
478 case cd_ptime:
479 case cd_atime:
Guido van Rossumece6efe1992-04-15 15:56:11 +0000480#define ptr ((struct cdtimecode *) data)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000481 v = Py_BuildValue("(iii)",
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000482 ptr->mhi * 10 + ptr->mlo,
Guido van Rossumece6efe1992-04-15 15:56:11 +0000483 ptr->shi * 10 + ptr->slo,
484 ptr->fhi * 10 + ptr->flo);
485#undef ptr
Guido van Rossum16b8f301992-04-13 18:22:53 +0000486 break;
487 case cd_catalog:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000488 v = PyString_FromStringAndSize(NULL, 13);
489 p = PyString_AsString(v);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000490 for (i = 0; i < 13; i++)
491 *p++ = ((char *) data)[i] + '0';
492 break;
493 case cd_ident:
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000494#define ptr ((struct cdident *) data)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000495 v = PyString_FromStringAndSize(NULL, 12);
496 p = PyString_AsString(v);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000497 CDsbtoa(p, ptr->country, 2);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000498 p += 2;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000499 CDsbtoa(p, ptr->owner, 3);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000500 p += 3;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000501 *p++ = ptr->year[0] + '0';
502 *p++ = ptr->year[1] + '0';
503 *p++ = ptr->serial[0] + '0';
504 *p++ = ptr->serial[1] + '0';
505 *p++ = ptr->serial[2] + '0';
506 *p++ = ptr->serial[3] + '0';
507 *p++ = ptr->serial[4] + '0';
508#undef ptr
Guido van Rossum16b8f301992-04-13 18:22:53 +0000509 break;
510 case cd_control:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000511 v = PyInt_FromLong((long) *((unchar *) data));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000512 break;
513 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000514 PyTuple_SetItem(args, 2, v);
515 if (PyErr_Occurred()) {
516 Py_DECREF(args);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000517 return;
518 }
519
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000520 result = PyEval_CallObject(self->ob_cdcallbacks[type].ob_cdcallback,
521 args);
522 Py_DECREF(args);
523 Py_XDECREF(result);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000524}
525
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000526static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000527CD_deleteparser(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000528{
529 int i;
530
Guido van Rossum43713e52000-02-29 13:59:29 +0000531 if (!PyArg_ParseTuple(args, ":deleteparser"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000532 return NULL;
533
534 CDdeleteparser(self->ob_cdparser);
535 self->ob_cdparser = NULL;
536
537 /* no sense in keeping the callbacks, so remove them */
538 for (i = 0; i < NCALLBACKS; i++) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000539 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000540 self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000541 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000542 self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
543 }
544
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000545 Py_INCREF(Py_None);
546 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000547}
548
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000549static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000550CD_parseframe(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000551{
552 char *cdfp;
553 int length;
554 CDFRAME *p;
555
Guido van Rossum43713e52000-02-29 13:59:29 +0000556 if (!PyArg_ParseTuple(args, "s#:parseframe", &cdfp, &length))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000557 return NULL;
558
559 if (length % sizeof(CDFRAME) != 0) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000560 PyErr_SetString(PyExc_TypeError, "bad length");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000561 return NULL;
562 }
563
564 p = (CDFRAME *) cdfp;
565 while (length > 0) {
566 CDparseframe(self->ob_cdparser, p);
567 length -= sizeof(CDFRAME);
568 p++;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000569 if (PyErr_Occurred())
Guido van Rossum16b8f301992-04-13 18:22:53 +0000570 return NULL;
571 }
572
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000573 Py_INCREF(Py_None);
574 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000575}
576
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000577static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000578CD_removecallback(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000579{
580 int type;
581
Guido van Rossum43713e52000-02-29 13:59:29 +0000582 if (!PyArg_ParseTuple(args, "i:removecallback", &type))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000583 return NULL;
584
Guido van Rossumf16eda51992-08-03 19:06:59 +0000585 if (type < 0 || type >= NCALLBACKS) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000586 PyErr_SetString(PyExc_TypeError, "bad type");
Guido van Rossumf16eda51992-08-03 19:06:59 +0000587 return NULL;
588 }
589
Guido van Rossum16b8f301992-04-13 18:22:53 +0000590 CDremovecallback(self->ob_cdparser, (CDDATATYPES) type);
591
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000592 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000593 self->ob_cdcallbacks[type].ob_cdcallback = NULL;
594
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000595 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000596 self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL;
597
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000598 Py_INCREF(Py_None);
599 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000600}
601
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000602static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000603CD_resetparser(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000604{
Guido van Rossum43713e52000-02-29 13:59:29 +0000605 if (!PyArg_ParseTuple(args, ":resetparser"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000606 return NULL;
607
608 CDresetparser(self->ob_cdparser);
609
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000610 Py_INCREF(Py_None);
611 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000612}
613
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000614static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000615CD_addcallback(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000616{
617 int type;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000618 PyObject *func, *funcarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000619
Guido van Rossum16b8f301992-04-13 18:22:53 +0000620 /* XXX - more work here */
Guido van Rossum43713e52000-02-29 13:59:29 +0000621 if (!PyArg_ParseTuple(args, "iOO:addcallback", &type, &func, &funcarg))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000622 return NULL;
623
624 if (type < 0 || type >= NCALLBACKS) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000625 PyErr_SetString(PyExc_TypeError, "argument out of range");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000626 return NULL;
627 }
628
Sjoerd Mullender7c4eb401992-09-25 11:15:58 +0000629#ifdef CDsetcallback
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000630 CDaddcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
631 (void *) self);
Guido van Rossumf16eda51992-08-03 19:06:59 +0000632#else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000633 CDsetcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
634 (void *) self);
Guido van Rossumf16eda51992-08-03 19:06:59 +0000635#endif
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000636 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
637 Py_INCREF(func);
Guido van Rossum234f9421993-06-17 12:35:49 +0000638 self->ob_cdcallbacks[type].ob_cdcallback = func;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000639 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
640 Py_INCREF(funcarg);
Guido van Rossum234f9421993-06-17 12:35:49 +0000641 self->ob_cdcallbacks[type].ob_cdcallbackarg = funcarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000642
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000643/*
644 if (type == cd_audio) {
645 sigfpe_[_UNDERFL].repls = _ZERO;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000646 handle_sigfpes(_ON, _EN_UNDERFL, NULL,
647 _ABORT_ON_ERROR, NULL);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000648 }
649*/
650
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000651 Py_INCREF(Py_None);
652 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000653}
654
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000655static PyMethodDef cdparser_methods[] = {
656 {"addcallback", (PyCFunction)CD_addcallback, 1},
657 {"deleteparser", (PyCFunction)CD_deleteparser, 1},
658 {"parseframe", (PyCFunction)CD_parseframe, 1},
659 {"removecallback", (PyCFunction)CD_removecallback, 1},
660 {"resetparser", (PyCFunction)CD_resetparser, 1},
661 /* backward compatibility */
662 {"setcallback", (PyCFunction)CD_addcallback, 1},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000663 {NULL, NULL} /* sentinel */
664};
665
666static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000667cdparser_dealloc(cdparserobject *self)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000668{
669 int i;
670
671 for (i = 0; i < NCALLBACKS; i++) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000672 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000673 self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000674 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000675 self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
676 }
677 CDdeleteparser(self->ob_cdparser);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000678 PyObject_Del(self);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000679}
680
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000681static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000682cdparser_getattr(cdparserobject *self, char *name)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000683{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000684 if (self->ob_cdparser == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000685 PyErr_SetString(PyExc_RuntimeError, "no parser active");
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000686 return NULL;
687 }
688
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000689 return Py_FindMethod(cdparser_methods, (PyObject *)self, name);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000690}
691
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000692PyTypeObject CdParsertype = {
693 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000694 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000695 "cd.cdparser", /*tp_name*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000696 sizeof(cdparserobject), /*tp_size*/
697 0, /*tp_itemsize*/
698 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000699 (destructor)cdparser_dealloc, /*tp_dealloc*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000700 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000701 (getattrfunc)cdparser_getattr, /*tp_getattr*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000702 0, /*tp_setattr*/
703 0, /*tp_compare*/
704 0, /*tp_repr*/
705};
706
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000707static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000708newcdparserobject(CDPARSER *cdp)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000709{
710 cdparserobject *p;
711 int i;
712
Guido van Rossumb18618d2000-05-03 23:44:39 +0000713 p = PyObject_New(cdparserobject, &CdParsertype);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000714 if (p == NULL)
715 return NULL;
716 p->ob_cdparser = cdp;
717 for (i = 0; i < NCALLBACKS; i++) {
718 p->ob_cdcallbacks[i].ob_cdcallback = NULL;
719 p->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
720 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000721 return (PyObject *) p;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000722}
723
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000724static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000725CD_createparser(PyObject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000726{
727 CDPARSER *cdp;
728
Guido van Rossum43713e52000-02-29 13:59:29 +0000729 if (!PyArg_ParseTuple(args, ":createparser"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000730 return NULL;
731 cdp = CDcreateparser();
732 if (cdp == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000733 PyErr_SetString(CdError, "createparser failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000734 return NULL;
735 }
736
737 return newcdparserobject(cdp);
738}
739
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000740static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000741CD_msftoframe(PyObject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000742{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000743 int min, sec, frame;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000744
Guido van Rossum43713e52000-02-29 13:59:29 +0000745 if (!PyArg_ParseTuple(args, "iii:msftoframe", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000746 return NULL;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000747
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000748 return PyInt_FromLong((long) CDmsftoframe(min, sec, frame));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000749}
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000750
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000751static PyMethodDef CD_methods[] = {
752 {"open", (PyCFunction)CD_open, 1},
753 {"createparser", (PyCFunction)CD_createparser, 1},
754 {"msftoframe", (PyCFunction)CD_msftoframe, 1},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000755 {NULL, NULL} /* Sentinel */
756};
757
758void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000759initcd(void)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000760{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000761 PyObject *m, *d;
Sjoerd Mullender46927ba1992-09-24 10:48:40 +0000762
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000763 m = Py_InitModule("cd", CD_methods);
764 d = PyModule_GetDict(m);
Sjoerd Mullender46927ba1992-09-24 10:48:40 +0000765
Guido van Rossum88d8bee1997-10-01 04:43:39 +0000766 CdError = PyErr_NewException("cd.error", NULL, NULL);
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000767 PyDict_SetItemString(d, "error", CdError);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000768
769 /* Identifiers for the different types of callbacks from the parser */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000770 PyDict_SetItemString(d, "audio", PyInt_FromLong((long) cd_audio));
771 PyDict_SetItemString(d, "pnum", PyInt_FromLong((long) cd_pnum));
772 PyDict_SetItemString(d, "index", PyInt_FromLong((long) cd_index));
773 PyDict_SetItemString(d, "ptime", PyInt_FromLong((long) cd_ptime));
774 PyDict_SetItemString(d, "atime", PyInt_FromLong((long) cd_atime));
775 PyDict_SetItemString(d, "catalog", PyInt_FromLong((long) cd_catalog));
776 PyDict_SetItemString(d, "ident", PyInt_FromLong((long) cd_ident));
777 PyDict_SetItemString(d, "control", PyInt_FromLong((long) cd_control));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000778
779 /* Block size information for digital audio data */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000780 PyDict_SetItemString(d, "DATASIZE",
781 PyInt_FromLong((long) CDDA_DATASIZE));
782 PyDict_SetItemString(d, "BLOCKSIZE",
783 PyInt_FromLong((long) CDDA_BLOCKSIZE));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000784
785 /* Possible states for the cd player */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000786 PyDict_SetItemString(d, "ERROR", PyInt_FromLong((long) CD_ERROR));
787 PyDict_SetItemString(d, "NODISC", PyInt_FromLong((long) CD_NODISC));
788 PyDict_SetItemString(d, "READY", PyInt_FromLong((long) CD_READY));
789 PyDict_SetItemString(d, "PLAYING", PyInt_FromLong((long) CD_PLAYING));
790 PyDict_SetItemString(d, "PAUSED", PyInt_FromLong((long) CD_PAUSED));
791 PyDict_SetItemString(d, "STILL", PyInt_FromLong((long) CD_STILL));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000792#ifdef CD_CDROM /* only newer versions of the library */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000793 PyDict_SetItemString(d, "CDROM", PyInt_FromLong((long) CD_CDROM));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000794#endif
Guido van Rossum16b8f301992-04-13 18:22:53 +0000795}