blob: 71598d7cd7d15222b074c0902f1b9e660bd56298 [file] [log] [blame]
Guido van Rossum16b8f301992-04-13 18:22:53 +00001/**********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum16b8f301992-04-13 18:22:53 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum16b8f301992-04-13 18:22:53 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum16b8f301992-04-13 18:22:53 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum16b8f301992-04-13 18:22:53 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum16b8f301992-04-13 18:22:53 +000029
30******************************************************************/
31
32/* CD module -- interface to Mark Callow's and Roger Chickering's */
33 /* CD Audio Library (CD). */
34
35#include <sys/types.h>
36#include <cdaudio.h>
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000037#include "Python.h"
Guido van Rossum16b8f301992-04-13 18:22:53 +000038
39#define NCALLBACKS 8
40
41typedef struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000042 PyObject_HEAD
Guido van Rossum16b8f301992-04-13 18:22:53 +000043 CDPLAYER *ob_cdplayer;
44} cdplayerobject;
45
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000046static PyObject *CdError; /* exception cd.error */
Sjoerd Mullender46927ba1992-09-24 10:48:40 +000047
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000048static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +000049CD_allowremoval(self, args)
50 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000051 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +000052{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000053 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +000054 return NULL;
55
56 CDallowremoval(self->ob_cdplayer);
57
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000058 Py_INCREF(Py_None);
59 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +000060}
61
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000062static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +000063CD_preventremoval(self, args)
64 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000065 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +000066{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000067 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +000068 return NULL;
69
70 CDpreventremoval(self->ob_cdplayer);
71
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000072 Py_INCREF(Py_None);
73 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +000074}
75
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000076static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +000077CD_bestreadsize(self, args)
78 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000079 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +000080{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000081 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +000082 return NULL;
83
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000084 return PyInt_FromLong((long) CDbestreadsize(self->ob_cdplayer));
Guido van Rossum16b8f301992-04-13 18:22:53 +000085}
86
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000087static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +000088CD_close(self, args)
89 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000090 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +000091{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +000092 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +000093 return NULL;
94
95 if (!CDclose(self->ob_cdplayer)) {
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 self->ob_cdplayer = NULL;
100
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000101 Py_INCREF(Py_None);
102 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000103}
104
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000105static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000106CD_eject(self, args)
107 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000108 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000109{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000110 CDSTATUS status;
111
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000112 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000113 return NULL;
114
115 if (!CDeject(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000116 if (CDgetstatus(self->ob_cdplayer, &status) &&
117 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000118 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000119 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000120 PyErr_SetString(CdError, "eject failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000121 return NULL;
122 }
123
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000124 Py_INCREF(Py_None);
125 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000126}
127
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000128static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000129CD_getstatus(self, args)
130 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000131 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000132{
133 CDSTATUS status;
134
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000135 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000136 return NULL;
137
138 if (!CDgetstatus(self->ob_cdplayer, &status)) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000139 PyErr_SetFromErrno(CdError); /* XXX - ??? */
Guido van Rossum16b8f301992-04-13 18:22:53 +0000140 return NULL;
141 }
142
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000143 return Py_BuildValue("(ii(iii)(iii)(iii)iiii)", status.state,
Guido van Rossumece6efe1992-04-15 15:56:11 +0000144 status.track, status.min, status.sec, status.frame,
145 status.abs_min, status.abs_sec, status.abs_frame,
146 status.total_min, status.total_sec, status.total_frame,
147 status.first, status.last, status.scsi_audio,
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000148 status.cur_block);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000149}
150
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000151static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000152CD_gettrackinfo(self, args)
153 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000154 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000155{
156 int track;
157 CDTRACKINFO info;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000158 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000159
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000160 if (!PyArg_ParseTuple(args, "i", &track))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000161 return NULL;
162
163 if (!CDgettrackinfo(self->ob_cdplayer, track, &info)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000164 if (CDgetstatus(self->ob_cdplayer, &status) &&
165 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000166 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000167 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000168 PyErr_SetString(CdError, "gettrackinfo failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000169 return NULL;
170 }
171
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000172 return Py_BuildValue("((iii)(iii))",
Guido van Rossum16b8f301992-04-13 18:22:53 +0000173 info.start_min, info.start_sec, info.start_frame,
174 info.total_min, info.total_sec, info.total_frame);
175}
176
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000177static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000178CD_msftoblock(self, args)
179 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000180 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000181{
182 int min, sec, frame;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000183
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000184 if (!PyArg_ParseTuple(args, "iii", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000185 return NULL;
186
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000187 return PyInt_FromLong((long) CDmsftoblock(self->ob_cdplayer,
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000188 min, sec, frame));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000189}
190
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000191static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000192CD_play(self, args)
193 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000194 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000195{
196 int start, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000197 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000198
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000199 if (!PyArg_ParseTuple(args, "ii", &start, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000200 return NULL;
201
202 if (!CDplay(self->ob_cdplayer, start, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000203 if (CDgetstatus(self->ob_cdplayer, &status) &&
204 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000205 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000206 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000207 PyErr_SetString(CdError, "play failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000208 return NULL;
209 }
210
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000211 Py_INCREF(Py_None);
212 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000213}
214
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000215static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000216CD_playabs(self, args)
217 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000218 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000219{
220 int min, sec, frame, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000221 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000222
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000223 if (!PyArg_ParseTuple(args, "iiii", &min, &sec, &frame, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000224 return NULL;
225
226 if (!CDplayabs(self->ob_cdplayer, min, sec, frame, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000227 if (CDgetstatus(self->ob_cdplayer, &status) &&
228 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000229 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000230 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000231 PyErr_SetString(CdError, "playabs failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000232 return NULL;
233 }
234
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000235 Py_INCREF(Py_None);
236 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000237}
238
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000239static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000240CD_playtrack(self, args)
241 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000242 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000243{
244 int start, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000245 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000246
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000247 if (!PyArg_ParseTuple(args, "ii", &start, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000248 return NULL;
249
250 if (!CDplaytrack(self->ob_cdplayer, start, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000251 if (CDgetstatus(self->ob_cdplayer, &status) &&
252 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000253 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000254 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000255 PyErr_SetString(CdError, "playtrack failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000256 return NULL;
257 }
258
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000259 Py_INCREF(Py_None);
260 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000261}
262
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000263static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000264CD_playtrackabs(self, args)
265 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000266 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000267{
268 int track, min, sec, frame, play;
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000269 CDSTATUS status;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000270
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000271 if (!PyArg_ParseTuple(args, "iiiii", &track, &min, &sec,
272 &frame, &play))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000273 return NULL;
274
275 if (!CDplaytrackabs(self->ob_cdplayer, track, min, sec, frame, play)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000276 if (CDgetstatus(self->ob_cdplayer, &status) &&
277 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000278 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000279 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000280 PyErr_SetString(CdError, "playtrackabs failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000281 return NULL;
282 }
283
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000284 Py_INCREF(Py_None);
285 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000286}
287
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000288static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000289CD_readda(self, args)
290 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000291 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000292{
293 int numframes, n;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000294 PyObject *result;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000295
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000296 if (!PyArg_ParseTuple(args, "i", &numframes))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000297 return NULL;
298
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000299 result = PyString_FromStringAndSize(NULL, numframes * sizeof(CDFRAME));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000300 if (result == NULL)
301 return NULL;
302
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000303 n = CDreadda(self->ob_cdplayer,
304 (CDFRAME *) PyString_AsString(result), numframes);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000305 if (n == -1) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000306 Py_DECREF(result);
307 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000308 return NULL;
309 }
310 if (n < numframes)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000311 if (_PyString_Resize(&result, n * sizeof(CDFRAME)))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000312 return NULL;
313
314 return result;
315}
316
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000317static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000318CD_seek(self, args)
319 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000320 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000321{
322 int min, sec, frame;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000323 long PyTryBlock;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000324
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000325 if (!PyArg_ParseTuple(args, "iii", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000326 return NULL;
327
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000328 PyTryBlock = CDseek(self->ob_cdplayer, min, sec, frame);
329 if (PyTryBlock == -1) {
330 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000331 return NULL;
332 }
333
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000334 return PyInt_FromLong(PyTryBlock);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000335}
336
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000337static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000338CD_seektrack(self, args)
339 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000340 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000341{
342 int track;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000343 long PyTryBlock;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000344
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000345 if (!PyArg_ParseTuple(args, "i", &track))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000346 return NULL;
347
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000348 PyTryBlock = CDseektrack(self->ob_cdplayer, track);
349 if (PyTryBlock == -1) {
350 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000351 return NULL;
352 }
353
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000354 return PyInt_FromLong(PyTryBlock);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000355}
356
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000357static PyObject *
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000358CD_seekblock(self, args)
359 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000360 PyObject *args;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000361{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000362 unsigned long PyTryBlock;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000363
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000364 if (!PyArg_ParseTuple(args, "l", &PyTryBlock))
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000365 return NULL;
366
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000367 PyTryBlock = CDseekblock(self->ob_cdplayer, PyTryBlock);
368 if (PyTryBlock == (unsigned long) -1) {
369 PyErr_SetFromErrno(CdError);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000370 return NULL;
371 }
372
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000373 return PyInt_FromLong(PyTryBlock);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000374}
375
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000376static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000377CD_stop(self, args)
378 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000379 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000380{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000381 CDSTATUS status;
382
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000383 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000384 return NULL;
385
386 if (!CDstop(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000387 if (CDgetstatus(self->ob_cdplayer, &status) &&
388 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000389 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000390 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000391 PyErr_SetString(CdError, "stop failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000392 return NULL;
393 }
394
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000395 Py_INCREF(Py_None);
396 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000397}
398
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000399static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000400CD_togglepause(self, args)
401 cdplayerobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000402 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000403{
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000404 CDSTATUS status;
405
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000406 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000407 return NULL;
408
409 if (!CDtogglepause(self->ob_cdplayer)) {
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000410 if (CDgetstatus(self->ob_cdplayer, &status) &&
411 status.state == CD_NODISC)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000412 PyErr_SetString(CdError, "no disc in player");
Guido van Rossumc3c7ac81992-05-06 09:48:30 +0000413 else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000414 PyErr_SetString(CdError, "togglepause failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000415 return NULL;
416 }
417
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000418 Py_INCREF(Py_None);
419 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000420}
421
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000422static PyMethodDef cdplayer_methods[] = {
423 {"allowremoval", (PyCFunction)CD_allowremoval, 1},
424 {"bestreadsize", (PyCFunction)CD_bestreadsize, 1},
425 {"close", (PyCFunction)CD_close, 1},
426 {"eject", (PyCFunction)CD_eject, 1},
427 {"getstatus", (PyCFunction)CD_getstatus, 1},
428 {"gettrackinfo", (PyCFunction)CD_gettrackinfo, 1},
429 {"msftoblock", (PyCFunction)CD_msftoblock, 1},
430 {"play", (PyCFunction)CD_play, 1},
431 {"playabs", (PyCFunction)CD_playabs, 1},
432 {"playtrack", (PyCFunction)CD_playtrack, 1},
433 {"playtrackabs", (PyCFunction)CD_playtrackabs, 1},
434 {"preventremoval", (PyCFunction)CD_preventremoval, 1},
435 {"readda", (PyCFunction)CD_readda, 1},
436 {"seek", (PyCFunction)CD_seek, 1},
437 {"seekblock", (PyCFunction)CD_seekblock, 1},
438 {"seektrack", (PyCFunction)CD_seektrack, 1},
439 {"stop", (PyCFunction)CD_stop, 1},
440 {"togglepause", (PyCFunction)CD_togglepause, 1},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000441 {NULL, NULL} /* sentinel */
442};
443
444static void
445cdplayer_dealloc(self)
446 cdplayerobject *self;
447{
448 if (self->ob_cdplayer != NULL)
449 CDclose(self->ob_cdplayer);
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000450 PyMem_DEL(self);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000451}
452
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000453static PyObject *
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000454cdplayer_getattr(self, name)
455 cdplayerobject *self;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000456 char *name;
457{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000458 if (self->ob_cdplayer == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000459 PyErr_SetString(PyExc_RuntimeError, "no player active");
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000460 return NULL;
461 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000462 return Py_FindMethod(cdplayer_methods, (PyObject *)self, name);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000463}
464
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000465PyTypeObject CdPlayertype = {
466 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000467 0, /*ob_size*/
468 "cdplayer", /*tp_name*/
469 sizeof(cdplayerobject), /*tp_size*/
470 0, /*tp_itemsize*/
471 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000472 (destructor)cdplayer_dealloc, /*tp_dealloc*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000473 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000474 (getattrfunc)cdplayer_getattr, /*tp_getattr*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000475 0, /*tp_setattr*/
476 0, /*tp_compare*/
477 0, /*tp_repr*/
478};
479
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000480static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000481newcdplayerobject(cdp)
482 CDPLAYER *cdp;
483{
484 cdplayerobject *p;
485
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000486 p = PyObject_NEW(cdplayerobject, &CdPlayertype);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000487 if (p == NULL)
488 return NULL;
489 p->ob_cdplayer = cdp;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000490 return (PyObject *) p;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000491}
492
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000493static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000494CD_open(self, args)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000495 PyObject *self, *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000496{
497 char *dev, *direction;
498 CDPLAYER *cdp;
499
500 /*
501 * Variable number of args.
502 * First defaults to "None", second defaults to "r".
503 */
504 dev = NULL;
505 direction = "r";
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000506 if (!PyArg_ParseTuple(args, "|zs", &dev, &direction))
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000507 return NULL;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000508
509 cdp = CDopen(dev, direction);
510 if (cdp == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000511 PyErr_SetFromErrno(CdError);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000512 return NULL;
513 }
514
515 return newcdplayerobject(cdp);
516}
517
518typedef struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000519 PyObject_HEAD
Guido van Rossum16b8f301992-04-13 18:22:53 +0000520 CDPARSER *ob_cdparser;
521 struct {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000522 PyObject *ob_cdcallback;
523 PyObject *ob_cdcallbackarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000524 } ob_cdcallbacks[NCALLBACKS];
525} cdparserobject;
526
527static void
528CD_callback(arg, type, data)
529 void *arg;
530 CDDATATYPES type;
531 void *data;
532{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000533 PyObject *result, *args, *v = NULL;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000534 char *p;
535 int i;
536 cdparserobject *self;
537
538 self = (cdparserobject *) arg;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000539 args = PyTuple_New(3);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000540 if (args == NULL)
541 return;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000542 Py_INCREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
543 PyTuple_SetItem(args, 0, self->ob_cdcallbacks[type].ob_cdcallbackarg);
544 PyTuple_SetItem(args, 1, PyInt_FromLong((long) type));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000545 switch (type) {
546 case cd_audio:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000547 v = PyString_FromStringAndSize(data, CDDA_DATASIZE);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000548 break;
549 case cd_pnum:
550 case cd_index:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000551 v = PyInt_FromLong(((CDPROGNUM *) data)->value);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000552 break;
553 case cd_ptime:
554 case cd_atime:
Guido van Rossumece6efe1992-04-15 15:56:11 +0000555#define ptr ((struct cdtimecode *) data)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000556 v = Py_BuildValue("(iii)",
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000557 ptr->mhi * 10 + ptr->mlo,
Guido van Rossumece6efe1992-04-15 15:56:11 +0000558 ptr->shi * 10 + ptr->slo,
559 ptr->fhi * 10 + ptr->flo);
560#undef ptr
Guido van Rossum16b8f301992-04-13 18:22:53 +0000561 break;
562 case cd_catalog:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000563 v = PyString_FromStringAndSize(NULL, 13);
564 p = PyString_AsString(v);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000565 for (i = 0; i < 13; i++)
566 *p++ = ((char *) data)[i] + '0';
567 break;
568 case cd_ident:
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000569#define ptr ((struct cdident *) data)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000570 v = PyString_FromStringAndSize(NULL, 12);
571 p = PyString_AsString(v);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000572 CDsbtoa(p, ptr->country, 2);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000573 p += 2;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000574 CDsbtoa(p, ptr->owner, 3);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000575 p += 3;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000576 *p++ = ptr->year[0] + '0';
577 *p++ = ptr->year[1] + '0';
578 *p++ = ptr->serial[0] + '0';
579 *p++ = ptr->serial[1] + '0';
580 *p++ = ptr->serial[2] + '0';
581 *p++ = ptr->serial[3] + '0';
582 *p++ = ptr->serial[4] + '0';
583#undef ptr
Guido van Rossum16b8f301992-04-13 18:22:53 +0000584 break;
585 case cd_control:
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000586 v = PyInt_FromLong((long) *((unchar *) data));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000587 break;
588 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000589 PyTuple_SetItem(args, 2, v);
590 if (PyErr_Occurred()) {
591 Py_DECREF(args);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000592 return;
593 }
594
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000595 result = PyEval_CallObject(self->ob_cdcallbacks[type].ob_cdcallback,
596 args);
597 Py_DECREF(args);
598 Py_XDECREF(result);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000599}
600
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000601static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000602CD_deleteparser(self, args)
603 cdparserobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000604 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000605{
606 int i;
607
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000608 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000609 return NULL;
610
611 CDdeleteparser(self->ob_cdparser);
612 self->ob_cdparser = NULL;
613
614 /* no sense in keeping the callbacks, so remove them */
615 for (i = 0; i < NCALLBACKS; i++) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000616 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000617 self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000618 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000619 self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
620 }
621
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000622 Py_INCREF(Py_None);
623 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000624}
625
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000626static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000627CD_parseframe(self, args)
628 cdparserobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000629 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000630{
631 char *cdfp;
632 int length;
633 CDFRAME *p;
634
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000635 if (!PyArg_ParseTuple(args, "s#", &cdfp, &length))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000636 return NULL;
637
638 if (length % sizeof(CDFRAME) != 0) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000639 PyErr_SetString(PyExc_TypeError, "bad length");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000640 return NULL;
641 }
642
643 p = (CDFRAME *) cdfp;
644 while (length > 0) {
645 CDparseframe(self->ob_cdparser, p);
646 length -= sizeof(CDFRAME);
647 p++;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000648 if (PyErr_Occurred())
Guido van Rossum16b8f301992-04-13 18:22:53 +0000649 return NULL;
650 }
651
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000652 Py_INCREF(Py_None);
653 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000654}
655
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000656static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000657CD_removecallback(self, args)
658 cdparserobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000659 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000660{
661 int type;
662
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000663 if (!PyArg_ParseTuple(args, "i", &type))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000664 return NULL;
665
Guido van Rossumf16eda51992-08-03 19:06:59 +0000666 if (type < 0 || type >= NCALLBACKS) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000667 PyErr_SetString(PyExc_TypeError, "bad type");
Guido van Rossumf16eda51992-08-03 19:06:59 +0000668 return NULL;
669 }
670
Guido van Rossum16b8f301992-04-13 18:22:53 +0000671 CDremovecallback(self->ob_cdparser, (CDDATATYPES) type);
672
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000673 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000674 self->ob_cdcallbacks[type].ob_cdcallback = NULL;
675
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000676 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000677 self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL;
678
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000679 Py_INCREF(Py_None);
680 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000681}
682
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000683static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000684CD_resetparser(self, args)
685 cdparserobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000686 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000687{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000688 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000689 return NULL;
690
691 CDresetparser(self->ob_cdparser);
692
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000693 Py_INCREF(Py_None);
694 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000695}
696
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000697static PyObject *
Guido van Rossumf16eda51992-08-03 19:06:59 +0000698CD_addcallback(self, args)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000699 cdparserobject *self;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000700 PyObject *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000701{
702 int type;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000703 PyObject *func, *funcarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000704
Guido van Rossum16b8f301992-04-13 18:22:53 +0000705 /* XXX - more work here */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000706 if (!PyArg_ParseTuple(args, "iOO", &type, &func, &funcarg))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000707 return NULL;
708
709 if (type < 0 || type >= NCALLBACKS) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000710 PyErr_SetString(PyExc_TypeError, "argument out of range");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000711 return NULL;
712 }
713
Sjoerd Mullender7c4eb401992-09-25 11:15:58 +0000714#ifdef CDsetcallback
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000715 CDaddcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
716 (void *) self);
Guido van Rossumf16eda51992-08-03 19:06:59 +0000717#else
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000718 CDsetcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
719 (void *) self);
Guido van Rossumf16eda51992-08-03 19:06:59 +0000720#endif
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000721 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
722 Py_INCREF(func);
Guido van Rossum234f9421993-06-17 12:35:49 +0000723 self->ob_cdcallbacks[type].ob_cdcallback = func;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000724 Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
725 Py_INCREF(funcarg);
Guido van Rossum234f9421993-06-17 12:35:49 +0000726 self->ob_cdcallbacks[type].ob_cdcallbackarg = funcarg;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000727
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000728/*
729 if (type == cd_audio) {
730 sigfpe_[_UNDERFL].repls = _ZERO;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000731 handle_sigfpes(_ON, _EN_UNDERFL, NULL,
732 _ABORT_ON_ERROR, NULL);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000733 }
734*/
735
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000736 Py_INCREF(Py_None);
737 return Py_None;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000738}
739
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000740static PyMethodDef cdparser_methods[] = {
741 {"addcallback", (PyCFunction)CD_addcallback, 1},
742 {"deleteparser", (PyCFunction)CD_deleteparser, 1},
743 {"parseframe", (PyCFunction)CD_parseframe, 1},
744 {"removecallback", (PyCFunction)CD_removecallback, 1},
745 {"resetparser", (PyCFunction)CD_resetparser, 1},
746 /* backward compatibility */
747 {"setcallback", (PyCFunction)CD_addcallback, 1},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000748 {NULL, NULL} /* sentinel */
749};
750
751static void
752cdparser_dealloc(self)
753 cdparserobject *self;
754{
755 int i;
756
757 for (i = 0; i < NCALLBACKS; i++) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000758 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000759 self->ob_cdcallbacks[i].ob_cdcallback = NULL;
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000760 Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000761 self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
762 }
763 CDdeleteparser(self->ob_cdparser);
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000764 PyMem_DEL(self);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000765}
766
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000767static PyObject *
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000768cdparser_getattr(self, name)
769 cdparserobject *self;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000770 char *name;
771{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000772 if (self->ob_cdparser == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000773 PyErr_SetString(PyExc_RuntimeError, "no parser active");
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000774 return NULL;
775 }
776
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000777 return Py_FindMethod(cdparser_methods, (PyObject *)self, name);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000778}
779
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000780PyTypeObject CdParsertype = {
781 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum16b8f301992-04-13 18:22:53 +0000782 0, /*ob_size*/
783 "cdparser", /*tp_name*/
784 sizeof(cdparserobject), /*tp_size*/
785 0, /*tp_itemsize*/
786 /* methods */
Guido van Rossumb6775db1994-08-01 11:34:53 +0000787 (destructor)cdparser_dealloc, /*tp_dealloc*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000788 0, /*tp_print*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000789 (getattrfunc)cdparser_getattr, /*tp_getattr*/
Guido van Rossum16b8f301992-04-13 18:22:53 +0000790 0, /*tp_setattr*/
791 0, /*tp_compare*/
792 0, /*tp_repr*/
793};
794
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000795static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000796newcdparserobject(cdp)
797 CDPARSER *cdp;
798{
799 cdparserobject *p;
800 int i;
801
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000802 p = PyObject_NEW(cdparserobject, &CdParsertype);
Guido van Rossum16b8f301992-04-13 18:22:53 +0000803 if (p == NULL)
804 return NULL;
805 p->ob_cdparser = cdp;
806 for (i = 0; i < NCALLBACKS; i++) {
807 p->ob_cdcallbacks[i].ob_cdcallback = NULL;
808 p->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
809 }
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000810 return (PyObject *) p;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000811}
812
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000813static PyObject *
Guido van Rossum16b8f301992-04-13 18:22:53 +0000814CD_createparser(self, args)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000815 PyObject *self, *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000816{
817 CDPARSER *cdp;
818
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000819 if (!PyArg_ParseTuple(args, ""))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000820 return NULL;
821 cdp = CDcreateparser();
822 if (cdp == NULL) {
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000823 PyErr_SetString(CdError, "createparser failed");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000824 return NULL;
825 }
826
827 return newcdparserobject(cdp);
828}
829
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000830static PyObject *
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000831CD_msftoframe(self, args)
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000832 PyObject *self, *args;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000833{
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000834 int min, sec, frame;
Guido van Rossum16b8f301992-04-13 18:22:53 +0000835
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000836 if (!PyArg_ParseTuple(args, "iii", &min, &sec, &frame))
Guido van Rossum16b8f301992-04-13 18:22:53 +0000837 return NULL;
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000838
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000839 return PyInt_FromLong((long) CDmsftoframe(min, sec, frame));
Guido van Rossum16b8f301992-04-13 18:22:53 +0000840}
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000841
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000842static PyMethodDef CD_methods[] = {
843 {"open", (PyCFunction)CD_open, 1},
844 {"createparser", (PyCFunction)CD_createparser, 1},
845 {"msftoframe", (PyCFunction)CD_msftoframe, 1},
Guido van Rossum16b8f301992-04-13 18:22:53 +0000846 {NULL, NULL} /* Sentinel */
847};
848
849void
850initcd()
851{
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000852 PyObject *m, *d;
Sjoerd Mullender46927ba1992-09-24 10:48:40 +0000853
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000854 m = Py_InitModule("cd", CD_methods);
855 d = PyModule_GetDict(m);
Sjoerd Mullender46927ba1992-09-24 10:48:40 +0000856
Guido van Rossum88d8bee1997-10-01 04:43:39 +0000857 CdError = PyErr_NewException("cd.error", NULL, NULL);
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000858 PyDict_SetItemString(d, "error", CdError);
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000859
860 /* Identifiers for the different types of callbacks from the parser */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000861 PyDict_SetItemString(d, "audio", PyInt_FromLong((long) cd_audio));
862 PyDict_SetItemString(d, "pnum", PyInt_FromLong((long) cd_pnum));
863 PyDict_SetItemString(d, "index", PyInt_FromLong((long) cd_index));
864 PyDict_SetItemString(d, "ptime", PyInt_FromLong((long) cd_ptime));
865 PyDict_SetItemString(d, "atime", PyInt_FromLong((long) cd_atime));
866 PyDict_SetItemString(d, "catalog", PyInt_FromLong((long) cd_catalog));
867 PyDict_SetItemString(d, "ident", PyInt_FromLong((long) cd_ident));
868 PyDict_SetItemString(d, "control", PyInt_FromLong((long) cd_control));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000869
870 /* Block size information for digital audio data */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000871 PyDict_SetItemString(d, "DATASIZE",
872 PyInt_FromLong((long) CDDA_DATASIZE));
873 PyDict_SetItemString(d, "BLOCKSIZE",
874 PyInt_FromLong((long) CDDA_BLOCKSIZE));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000875
876 /* Possible states for the cd player */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000877 PyDict_SetItemString(d, "ERROR", PyInt_FromLong((long) CD_ERROR));
878 PyDict_SetItemString(d, "NODISC", PyInt_FromLong((long) CD_NODISC));
879 PyDict_SetItemString(d, "READY", PyInt_FromLong((long) CD_READY));
880 PyDict_SetItemString(d, "PLAYING", PyInt_FromLong((long) CD_PLAYING));
881 PyDict_SetItemString(d, "PAUSED", PyInt_FromLong((long) CD_PAUSED));
882 PyDict_SetItemString(d, "STILL", PyInt_FromLong((long) CD_STILL));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000883#ifdef CD_CDROM /* only newer versions of the library */
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000884 PyDict_SetItemString(d, "CDROM", PyInt_FromLong((long) CD_CDROM));
Sjoerd Mullender542659b1995-03-28 12:06:23 +0000885#endif
886
Roger E. Massebf0c3ca1997-01-03 23:00:51 +0000887 if (PyErr_Occurred())
888 Py_FatalError("can't initialize module cd");
Guido van Rossum16b8f301992-04-13 18:22:53 +0000889}