blob: 25add3e1b61337e9d1fa4f13100e5b1a3237ce18 [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)
Tim Peters5de98422002-04-27 18:44:32 +0000254 _PyString_Resize(&result, n * sizeof(CDFRAME));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000255
256 return result;
257}
258
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000259static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000260CD_seek(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000261{
262 int min, sec, frame;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000263 long PyTryBlock;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000264
Guido van Rossum43713e52000-02-29 13:59:29 +0000265 if (!PyArg_ParseTuple(args, "iii:seek", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000266 return NULL;
267
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000268 PyTryBlock = CDseek(self->ob_cdplayer, min, sec, frame);
269 if (PyTryBlock == -1) {
270 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000271 return NULL;
272 }
273
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000274 return PyInt_FromLong(PyTryBlock);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000275}
276
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000277static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000278CD_seektrack(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000279{
280 int track;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000281 long PyTryBlock;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000282
Guido van Rossum43713e52000-02-29 13:59:29 +0000283 if (!PyArg_ParseTuple(args, "i:seektrack", &track))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000284 return NULL;
285
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000286 PyTryBlock = CDseektrack(self->ob_cdplayer, track);
287 if (PyTryBlock == -1) {
288 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000289 return NULL;
290 }
291
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000292 return PyInt_FromLong(PyTryBlock);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000293}
294
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000295static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000296CD_seekblock(cdplayerobject *self, PyObject *args)
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000297{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000298 unsigned long PyTryBlock;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000299
Guido van Rossum43713e52000-02-29 13:59:29 +0000300 if (!PyArg_ParseTuple(args, "l:seekblock", &PyTryBlock))
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000301 return NULL;
302
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000303 PyTryBlock = CDseekblock(self->ob_cdplayer, PyTryBlock);
304 if (PyTryBlock == (unsigned long) -1) {
305 PyErr_SetFromErrno(CdError);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000306 return NULL;
307 }
308
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000309 return PyInt_FromLong(PyTryBlock);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000310}
311
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000312static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000313CD_stop(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000314{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000315 CDSTATUS status;
316
Guido van Rossum43713e52000-02-29 13:59:29 +0000317 if (!PyArg_ParseTuple(args, ":stop"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000318 return NULL;
319
320 if (!CDstop(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000321 if (CDgetstatus(self->ob_cdplayer, &status) &&
322 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000323 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000324 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000325 PyErr_SetString(CdError, "stop failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000326 return NULL;
327 }
328
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000329 Py_INCREF(Py_None);
330 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000331}
332
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000333static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000334CD_togglepause(cdplayerobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000335{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000336 CDSTATUS status;
337
Guido van Rossum43713e52000-02-29 13:59:29 +0000338 if (!PyArg_ParseTuple(args, ":togglepause"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000339 return NULL;
340
341 if (!CDtogglepause(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000342 if (CDgetstatus(self->ob_cdplayer, &status) &&
343 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000344 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000345 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000346 PyErr_SetString(CdError, "togglepause failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000347 return NULL;
348 }
349
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000350 Py_INCREF(Py_None);
351 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000352}
353
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000354static PyMethodDef cdplayer_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000355 {"allowremoval", (PyCFunction)CD_allowremoval, METH_VARARGS},
356 {"bestreadsize", (PyCFunction)CD_bestreadsize, METH_VARARGS},
357 {"close", (PyCFunction)CD_close, METH_VARARGS},
358 {"eject", (PyCFunction)CD_eject, METH_VARARGS},
359 {"getstatus", (PyCFunction)CD_getstatus, METH_VARARGS},
360 {"gettrackinfo", (PyCFunction)CD_gettrackinfo, METH_VARARGS},
361 {"msftoblock", (PyCFunction)CD_msftoblock, METH_VARARGS},
362 {"play", (PyCFunction)CD_play, METH_VARARGS},
363 {"playabs", (PyCFunction)CD_playabs, METH_VARARGS},
364 {"playtrack", (PyCFunction)CD_playtrack, METH_VARARGS},
365 {"playtrackabs", (PyCFunction)CD_playtrackabs, METH_VARARGS},
366 {"preventremoval", (PyCFunction)CD_preventremoval, METH_VARARGS},
367 {"readda", (PyCFunction)CD_readda, METH_VARARGS},
368 {"seek", (PyCFunction)CD_seek, METH_VARARGS},
369 {"seekblock", (PyCFunction)CD_seekblock, METH_VARARGS},
370 {"seektrack", (PyCFunction)CD_seektrack, METH_VARARGS},
371 {"stop", (PyCFunction)CD_stop, METH_VARARGS},
372 {"togglepause", (PyCFunction)CD_togglepause, METH_VARARGS},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000373 {NULL, NULL} /* sentinel */
374};
375
376static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000377cdplayer_dealloc(cdplayerobject *self)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000378{
379 if (self->ob_cdplayer != NULL)
380 CDclose(self->ob_cdplayer);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000381 PyObject_Del(self);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000382}
383
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000384static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000385cdplayer_getattr(cdplayerobject *self, char *name)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000386{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000387 if (self->ob_cdplayer == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000388 PyErr_SetString(PyExc_RuntimeError, "no player active");
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000389 return NULL;
390 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000391 return Py_FindMethod(cdplayer_methods, (PyObject *)self, name);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000392}
393
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000394PyTypeObject CdPlayertype = {
395 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000396 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000397 "cd.cdplayer", /*tp_name*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000398 sizeof(cdplayerobject), /*tp_size*/
399 0, /*tp_itemsize*/
400 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000401 (destructor)cdplayer_dealloc, /*tp_dealloc*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000402 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000403 (getattrfunc)cdplayer_getattr, /*tp_getattr*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000404 0, /*tp_setattr*/
405 0, /*tp_compare*/
406 0, /*tp_repr*/
407};
408
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000409static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000410newcdplayerobject(CDPLAYER *cdp)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000411{
412 cdplayerobject *p;
413
Guido van Rossumb18618d2000-05-03 23:44:39 +0000414 p = PyObject_New(cdplayerobject, &CdPlayertype);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000415 if (p == NULL)
416 return NULL;
417 p->ob_cdplayer = cdp;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000418 return (PyObject *) p;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000419}
420
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000421static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000422CD_open(PyObject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000423{
424 char *dev, *direction;
425 CDPLAYER *cdp;
426
427 /*
428 * Variable number of args.
429 * First defaults to "None", second defaults to "r".
430 */
431 dev = NULL;
432 direction = "r";
Guido van Rossum43713e52000-02-29 13:59:29 +0000433 if (!PyArg_ParseTuple(args, "|zs:open", &dev, &direction))
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000434 return NULL;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000435
436 cdp = CDopen(dev, direction);
437 if (cdp == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000438 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000439 return NULL;
440 }
441
442 return newcdplayerobject(cdp);
443}
444
445typedef struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000446 PyObject_HEAD
Guido van Rossum16b8f301992-04-13 18:22:53 +0000447 CDPARSER *ob_cdparser;
448 struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000449 PyObject *ob_cdcallback;
450 PyObject *ob_cdcallbackarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000451 } ob_cdcallbacks[NCALLBACKS];
452} cdparserobject;
453
454static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000455CD_callback(void *arg, CDDATATYPES type, void *data)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000456{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000457 PyObject *result, *args, *v = NULL;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000458 char *p;
459 int i;
460 cdparserobject *self;
461
462 self = (cdparserobject *) arg;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000463 args = PyTuple_New(3);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000464 if (args == NULL)
465 return;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000466 Py_INCREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
467 PyTuple_SetItem(args, 0, self->ob_cdcallbacks[type].ob_cdcallbackarg);
468 PyTuple_SetItem(args, 1, PyInt_FromLong((long) type));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000469 switch (type) {
470 case cd_audio:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000471 v = PyString_FromStringAndSize(data, CDDA_DATASIZE);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000472 break;
473 case cd_pnum:
474 case cd_index:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000475 v = PyInt_FromLong(((CDPROGNUM *) data)->value);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000476 break;
477 case cd_ptime:
478 case cd_atime:
Guido van Rossumece6efe1992-04-15 15:56:11 +0000479#define ptr ((struct cdtimecode *) data)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000480 v = Py_BuildValue("(iii)",
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000481 ptr->mhi * 10 + ptr->mlo,
Guido van Rossumece6efe1992-04-15 15:56:11 +0000482 ptr->shi * 10 + ptr->slo,
483 ptr->fhi * 10 + ptr->flo);
484#undef ptr
Guido van Rossum16b8f301992-04-13 18:22:53 +0000485 break;
486 case cd_catalog:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000487 v = PyString_FromStringAndSize(NULL, 13);
488 p = PyString_AsString(v);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000489 for (i = 0; i < 13; i++)
490 *p++ = ((char *) data)[i] + '0';
491 break;
492 case cd_ident:
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000493#define ptr ((struct cdident *) data)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000494 v = PyString_FromStringAndSize(NULL, 12);
495 p = PyString_AsString(v);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000496 CDsbtoa(p, ptr->country, 2);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000497 p += 2;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000498 CDsbtoa(p, ptr->owner, 3);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000499 p += 3;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000500 *p++ = ptr->year[0] + '0';
501 *p++ = ptr->year[1] + '0';
502 *p++ = ptr->serial[0] + '0';
503 *p++ = ptr->serial[1] + '0';
504 *p++ = ptr->serial[2] + '0';
505 *p++ = ptr->serial[3] + '0';
506 *p++ = ptr->serial[4] + '0';
507#undef ptr
Guido van Rossum16b8f301992-04-13 18:22:53 +0000508 break;
509 case cd_control:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000510 v = PyInt_FromLong((long) *((unchar *) data));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000511 break;
512 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000513 PyTuple_SetItem(args, 2, v);
514 if (PyErr_Occurred()) {
515 Py_DECREF(args);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000516 return;
517 }
518
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000519 result = PyEval_CallObject(self->ob_cdcallbacks[type].ob_cdcallback,
520 args);
521 Py_DECREF(args);
522 Py_XDECREF(result);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000523}
524
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000525static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000526CD_deleteparser(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000527{
528 int i;
529
Guido van Rossum43713e52000-02-29 13:59:29 +0000530 if (!PyArg_ParseTuple(args, ":deleteparser"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000531 return NULL;
532
533 CDdeleteparser(self->ob_cdparser);
534 self->ob_cdparser = NULL;
535
536 /* no sense in keeping the callbacks, so remove them */
537 for (i = 0; i < NCALLBACKS; i++) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000538 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000539 self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000540 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000541 self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
542 }
543
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000544 Py_INCREF(Py_None);
545 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000546}
547
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000548static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000549CD_parseframe(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000550{
551 char *cdfp;
552 int length;
553 CDFRAME *p;
554
Guido van Rossum43713e52000-02-29 13:59:29 +0000555 if (!PyArg_ParseTuple(args, "s#:parseframe", &cdfp, &length))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000556 return NULL;
557
558 if (length % sizeof(CDFRAME) != 0) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000559 PyErr_SetString(PyExc_TypeError, "bad length");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000560 return NULL;
561 }
562
563 p = (CDFRAME *) cdfp;
564 while (length > 0) {
565 CDparseframe(self->ob_cdparser, p);
566 length -= sizeof(CDFRAME);
567 p++;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000568 if (PyErr_Occurred())
Guido van Rossum16b8f301992-04-13 18:22:53 +0000569 return NULL;
570 }
571
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000572 Py_INCREF(Py_None);
573 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000574}
575
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000576static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000577CD_removecallback(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000578{
579 int type;
580
Guido van Rossum43713e52000-02-29 13:59:29 +0000581 if (!PyArg_ParseTuple(args, "i:removecallback", &type))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000582 return NULL;
583
Guido van Rossumf16eda51992-08-03 19:06:59 +0000584 if (type < 0 || type >= NCALLBACKS) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000585 PyErr_SetString(PyExc_TypeError, "bad type");
Guido van Rossumf16eda51992-08-03 19:06:59 +0000586 return NULL;
587 }
588
Guido van Rossum16b8f301992-04-13 18:22:53 +0000589 CDremovecallback(self->ob_cdparser, (CDDATATYPES) type);
590
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000591 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000592 self->ob_cdcallbacks[type].ob_cdcallback = NULL;
593
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000594 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000595 self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL;
596
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000597 Py_INCREF(Py_None);
598 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000599}
600
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000601static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000602CD_resetparser(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000603{
Guido van Rossum43713e52000-02-29 13:59:29 +0000604 if (!PyArg_ParseTuple(args, ":resetparser"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000605 return NULL;
606
607 CDresetparser(self->ob_cdparser);
608
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000609 Py_INCREF(Py_None);
610 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000611}
612
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000613static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000614CD_addcallback(cdparserobject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000615{
616 int type;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000617 PyObject *func, *funcarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000618
Guido van Rossum16b8f301992-04-13 18:22:53 +0000619 /* XXX - more work here */
Guido van Rossum43713e52000-02-29 13:59:29 +0000620 if (!PyArg_ParseTuple(args, "iOO:addcallback", &type, &func, &funcarg))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000621 return NULL;
622
623 if (type < 0 || type >= NCALLBACKS) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000624 PyErr_SetString(PyExc_TypeError, "argument out of range");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000625 return NULL;
626 }
627
Sjoerd Mullender7c4eb401992-09-25 11:15:58 +0000628#ifdef CDsetcallback
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000629 CDaddcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
630 (void *) self);
Guido van Rossumf16eda51992-08-03 19:06:59 +0000631#else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000632 CDsetcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
633 (void *) self);
Guido van Rossumf16eda51992-08-03 19:06:59 +0000634#endif
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000635 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
636 Py_INCREF(func);
Guido van Rossum234f9421993-06-17 12:35:49 +0000637 self->ob_cdcallbacks[type].ob_cdcallback = func;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000638 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
639 Py_INCREF(funcarg);
Guido van Rossum234f9421993-06-17 12:35:49 +0000640 self->ob_cdcallbacks[type].ob_cdcallbackarg = funcarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000641
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000642/*
643 if (type == cd_audio) {
644 sigfpe_[_UNDERFL].repls = _ZERO;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000645 handle_sigfpes(_ON, _EN_UNDERFL, NULL,
646 _ABORT_ON_ERROR, NULL);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000647 }
648*/
649
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000650 Py_INCREF(Py_None);
651 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000652}
653
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000654static PyMethodDef cdparser_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000655 {"addcallback", (PyCFunction)CD_addcallback, METH_VARARGS},
656 {"deleteparser", (PyCFunction)CD_deleteparser, METH_VARARGS},
657 {"parseframe", (PyCFunction)CD_parseframe, METH_VARARGS},
658 {"removecallback", (PyCFunction)CD_removecallback, METH_VARARGS},
659 {"resetparser", (PyCFunction)CD_resetparser, METH_VARARGS},
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000660 /* backward compatibility */
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000661 {"setcallback", (PyCFunction)CD_addcallback, METH_VARARGS},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000662 {NULL, NULL} /* sentinel */
663};
664
665static void
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000666cdparser_dealloc(cdparserobject *self)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000667{
668 int i;
669
670 for (i = 0; i < NCALLBACKS; i++) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000671 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000672 self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000673 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000674 self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
675 }
676 CDdeleteparser(self->ob_cdparser);
Guido van Rossumb18618d2000-05-03 23:44:39 +0000677 PyObject_Del(self);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000678}
679
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000680static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000681cdparser_getattr(cdparserobject *self, char *name)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000682{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000683 if (self->ob_cdparser == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000684 PyErr_SetString(PyExc_RuntimeError, "no parser active");
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000685 return NULL;
686 }
687
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000688 return Py_FindMethod(cdparser_methods, (PyObject *)self, name);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000689}
690
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000691PyTypeObject CdParsertype = {
692 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000693 0, /*ob_size*/
Guido van Rossum14648392001-12-08 18:02:58 +0000694 "cd.cdparser", /*tp_name*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000695 sizeof(cdparserobject), /*tp_size*/
696 0, /*tp_itemsize*/
697 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000698 (destructor)cdparser_dealloc, /*tp_dealloc*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000699 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000700 (getattrfunc)cdparser_getattr, /*tp_getattr*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000701 0, /*tp_setattr*/
702 0, /*tp_compare*/
703 0, /*tp_repr*/
704};
705
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000706static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000707newcdparserobject(CDPARSER *cdp)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000708{
709 cdparserobject *p;
710 int i;
711
Guido van Rossumb18618d2000-05-03 23:44:39 +0000712 p = PyObject_New(cdparserobject, &CdParsertype);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000713 if (p == NULL)
714 return NULL;
715 p->ob_cdparser = cdp;
716 for (i = 0; i < NCALLBACKS; i++) {
717 p->ob_cdcallbacks[i].ob_cdcallback = NULL;
718 p->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
719 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000720 return (PyObject *) p;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000721}
722
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000723static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000724CD_createparser(PyObject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000725{
726 CDPARSER *cdp;
727
Guido van Rossum43713e52000-02-29 13:59:29 +0000728 if (!PyArg_ParseTuple(args, ":createparser"))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000729 return NULL;
730 cdp = CDcreateparser();
731 if (cdp == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000732 PyErr_SetString(CdError, "createparser failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000733 return NULL;
734 }
735
736 return newcdparserobject(cdp);
737}
738
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000739static PyObject *
Peter Schneider-Kamp6a850272000-07-10 17:04:33 +0000740CD_msftoframe(PyObject *self, PyObject *args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000741{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000742 int min, sec, frame;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000743
Guido van Rossum43713e52000-02-29 13:59:29 +0000744 if (!PyArg_ParseTuple(args, "iii:msftoframe", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000745 return NULL;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000746
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000747 return PyInt_FromLong((long) CDmsftoframe(min, sec, frame));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000748}
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000749
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000750static PyMethodDef CD_methods[] = {
Martin v. Löwis43b936d2002-01-17 23:15:58 +0000751 {"open", (PyCFunction)CD_open, METH_VARARGS},
752 {"createparser", (PyCFunction)CD_createparser, METH_VARARGS},
753 {"msftoframe", (PyCFunction)CD_msftoframe, METH_VARARGS},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000754 {NULL, NULL} /* Sentinel */
755};
756
757void
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000758initcd(void)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000759{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000760 PyObject *m, *d;
Sjoerd Mullender46927ba1992-09-24 10:48:40 +0000761
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000762 m = Py_InitModule("cd", CD_methods);
763 d = PyModule_GetDict(m);
Sjoerd Mullender46927ba1992-09-24 10:48:40 +0000764
Guido van Rossum88d8bee1997-10-01 04:43:39 +0000765 CdError = PyErr_NewException("cd.error", NULL, NULL);
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000766 PyDict_SetItemString(d, "error", CdError);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000767
768 /* Identifiers for the different types of callbacks from the parser */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000769 PyDict_SetItemString(d, "audio", PyInt_FromLong((long) cd_audio));
770 PyDict_SetItemString(d, "pnum", PyInt_FromLong((long) cd_pnum));
771 PyDict_SetItemString(d, "index", PyInt_FromLong((long) cd_index));
772 PyDict_SetItemString(d, "ptime", PyInt_FromLong((long) cd_ptime));
773 PyDict_SetItemString(d, "atime", PyInt_FromLong((long) cd_atime));
774 PyDict_SetItemString(d, "catalog", PyInt_FromLong((long) cd_catalog));
775 PyDict_SetItemString(d, "ident", PyInt_FromLong((long) cd_ident));
776 PyDict_SetItemString(d, "control", PyInt_FromLong((long) cd_control));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000777
778 /* Block size information for digital audio data */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000779 PyDict_SetItemString(d, "DATASIZE",
780 PyInt_FromLong((long) CDDA_DATASIZE));
781 PyDict_SetItemString(d, "BLOCKSIZE",
782 PyInt_FromLong((long) CDDA_BLOCKSIZE));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000783
784 /* Possible states for the cd player */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000785 PyDict_SetItemString(d, "ERROR", PyInt_FromLong((long) CD_ERROR));
786 PyDict_SetItemString(d, "NODISC", PyInt_FromLong((long) CD_NODISC));
787 PyDict_SetItemString(d, "READY", PyInt_FromLong((long) CD_READY));
788 PyDict_SetItemString(d, "PLAYING", PyInt_FromLong((long) CD_PLAYING));
789 PyDict_SetItemString(d, "PAUSED", PyInt_FromLong((long) CD_PAUSED));
790 PyDict_SetItemString(d, "STILL", PyInt_FromLong((long) CD_STILL));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000791#ifdef CD_CDROM /* only newer versions of the library */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000792 PyDict_SetItemString(d, "CDROM", PyInt_FromLong((long) CD_CDROM));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000793#endif
Guido van Rossum16b8f301992-04-13 18:22:53 +0000794}