summaryrefslogtreecommitdiffstats
path: root/Modules/cdmodule.c
blob: 1b414c10c2ea1142a57d50d8513626de1b5e8702 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/**********************************************************
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI or Corporation for National Research Initiatives or
CNRI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior
permission.

While CWI is the initial source for this software, a modified version
is made available by the Corporation for National Research Initiatives
(CNRI) at the Internet address ftp://ftp.python.org.

STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* CD module -- interface to Mark Callow's and Roger Chickering's */
 /* CD Audio Library (CD). */

#include <sys/types.h>
#include <cdaudio.h>
#include "Python.h"

#define NCALLBACKS	8

typedef struct {
	PyObject_HEAD
	CDPLAYER *ob_cdplayer;
} cdplayerobject;

static PyObject *CdError;		/* exception cd.error */

static PyObject *
CD_allowremoval(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	if (!PyArg_ParseTuple(args, ":allowremoval"))
		return NULL;

	CDallowremoval(self->ob_cdplayer);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_preventremoval(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	if (!PyArg_ParseTuple(args, ":preventremoval"))
		return NULL;

	CDpreventremoval(self->ob_cdplayer);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_bestreadsize(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	if (!PyArg_ParseTuple(args, ":bestreadsize"))
		return NULL;

	return PyInt_FromLong((long) CDbestreadsize(self->ob_cdplayer));
}

static PyObject *
CD_close(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	if (!PyArg_ParseTuple(args, ":close"))
		return NULL;

	if (!CDclose(self->ob_cdplayer)) {
		PyErr_SetFromErrno(CdError); /* XXX - ??? */
		return NULL;
	}
	self->ob_cdplayer = NULL;

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_eject(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, ":eject"))
		return NULL;

	if (!CDeject(self->ob_cdplayer)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "eject failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyObject *
CD_getstatus(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, ":getstatus"))
		return NULL;

	if (!CDgetstatus(self->ob_cdplayer, &status)) {
		PyErr_SetFromErrno(CdError); /* XXX - ??? */
		return NULL;
	}

	return Py_BuildValue("(ii(iii)(iii)(iii)iiii)", status.state,
		       status.track, status.min, status.sec, status.frame,
		       status.abs_min, status.abs_sec, status.abs_frame,
		       status.total_min, status.total_sec, status.total_frame,
		       status.first, status.last, status.scsi_audio,
		       status.cur_block);
}
	
static PyObject *
CD_gettrackinfo(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int track;
	CDTRACKINFO info;
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, "i:gettrackinfo", &track))
		return NULL;

	if (!CDgettrackinfo(self->ob_cdplayer, track, &info)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "gettrackinfo failed");
		return NULL;
	}

	return Py_BuildValue("((iii)(iii))",
		       info.start_min, info.start_sec, info.start_frame,
		       info.total_min, info.total_sec, info.total_frame);
}
	
static PyObject *
CD_msftoblock(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int min, sec, frame;

	if (!PyArg_ParseTuple(args, "iii:msftoblock", &min, &sec, &frame))
		return NULL;

	return PyInt_FromLong((long) CDmsftoblock(self->ob_cdplayer,
						min, sec, frame));
}
	
static PyObject *
CD_play(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int start, play;
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, "ii:play", &start, &play))
		return NULL;

	if (!CDplay(self->ob_cdplayer, start, play)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "play failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyObject *
CD_playabs(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int min, sec, frame, play;
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, "iiii:playabs", &min, &sec, &frame, &play))
		return NULL;

	if (!CDplayabs(self->ob_cdplayer, min, sec, frame, play)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "playabs failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyObject *
CD_playtrack(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int start, play;
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, "ii:playtrack", &start, &play))
		return NULL;

	if (!CDplaytrack(self->ob_cdplayer, start, play)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "playtrack failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyObject *
CD_playtrackabs(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int track, min, sec, frame, play;
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, "iiiii:playtrackabs", &track, &min, &sec,
			      &frame, &play))
		return NULL;

	if (!CDplaytrackabs(self->ob_cdplayer, track, min, sec, frame, play)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "playtrackabs failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyObject *
CD_readda(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int numframes, n;
	PyObject *result;

	if (!PyArg_ParseTuple(args, "i:readda", &numframes))
		return NULL;

	result = PyString_FromStringAndSize(NULL, numframes * sizeof(CDFRAME));
	if (result == NULL)
		return NULL;

	n = CDreadda(self->ob_cdplayer,
		       (CDFRAME *) PyString_AsString(result), numframes);
	if (n == -1) {
		Py_DECREF(result);
		PyErr_SetFromErrno(CdError);
		return NULL;
	}
	if (n < numframes)
		if (_PyString_Resize(&result, n * sizeof(CDFRAME)))
			return NULL;

	return result;
}

static PyObject *
CD_seek(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int min, sec, frame;
	long PyTryBlock;

	if (!PyArg_ParseTuple(args, "iii:seek", &min, &sec, &frame))
		return NULL;

	PyTryBlock = CDseek(self->ob_cdplayer, min, sec, frame);
	if (PyTryBlock == -1) {
		PyErr_SetFromErrno(CdError);
		return NULL;
	}

	return PyInt_FromLong(PyTryBlock);
}
	
static PyObject *
CD_seektrack(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	int track;
	long PyTryBlock;

	if (!PyArg_ParseTuple(args, "i:seektrack", &track))
		return NULL;

	PyTryBlock = CDseektrack(self->ob_cdplayer, track);
	if (PyTryBlock == -1) {
		PyErr_SetFromErrno(CdError);
		return NULL;
	}

	return PyInt_FromLong(PyTryBlock);
}
	
static PyObject *
CD_seekblock(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	unsigned long PyTryBlock;

	if (!PyArg_ParseTuple(args, "l:seekblock", &PyTryBlock))
		return NULL;

	PyTryBlock = CDseekblock(self->ob_cdplayer, PyTryBlock);
	if (PyTryBlock == (unsigned long) -1) {
		PyErr_SetFromErrno(CdError);
		return NULL;
	}

	return PyInt_FromLong(PyTryBlock);
}
	
static PyObject *
CD_stop(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, ":stop"))
		return NULL;

	if (!CDstop(self->ob_cdplayer)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "stop failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyObject *
CD_togglepause(self, args)
	cdplayerobject *self;
	PyObject *args;
{
	CDSTATUS status;

	if (!PyArg_ParseTuple(args, ":togglepause"))
		return NULL;

	if (!CDtogglepause(self->ob_cdplayer)) {
		if (CDgetstatus(self->ob_cdplayer, &status) &&
		    status.state == CD_NODISC)
			PyErr_SetString(CdError, "no disc in player");
		else
			PyErr_SetString(CdError, "togglepause failed");
		return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}
	
static PyMethodDef cdplayer_methods[] = {
	{"allowremoval",	(PyCFunction)CD_allowremoval,	1},
	{"bestreadsize",	(PyCFunction)CD_bestreadsize,	1},
	{"close",		(PyCFunction)CD_close,		1},
	{"eject",		(PyCFunction)CD_eject,		1},
	{"getstatus",		(PyCFunction)CD_getstatus,		1},
	{"gettrackinfo",	(PyCFunction)CD_gettrackinfo,	1},
	{"msftoblock",		(PyCFunction)CD_msftoblock,		1},
	{"play",		(PyCFunction)CD_play,		1},
	{"playabs",		(PyCFunction)CD_playabs,		1},
	{"playtrack",		(PyCFunction)CD_playtrack,		1},
	{"playtrackabs",	(PyCFunction)CD_playtrackabs,	1},
	{"preventremoval",	(PyCFunction)CD_preventremoval,	1},
	{"readda",		(PyCFunction)CD_readda,		1},
	{"seek",		(PyCFunction)CD_seek,		1},
	{"seekblock",		(PyCFunction)CD_seekblock,		1},
	{"seektrack",		(PyCFunction)CD_seektrack,		1},
	{"stop",		(PyCFunction)CD_stop,		1},
	{"togglepause",		(PyCFunction)CD_togglepause,   	1},
	{NULL,			NULL} 		/* sentinel */
};

static void
cdplayer_dealloc(self)
	cdplayerobject *self;
{
	if (self->ob_cdplayer != NULL)
		CDclose(self->ob_cdplayer);
	PyMem_DEL(self);
}

static PyObject *
cdplayer_getattr(self, name)
	cdplayerobject *self;
	char *name;
{
	if (self->ob_cdplayer == NULL) {
		PyErr_SetString(PyExc_RuntimeError, "no player active");
		return NULL;
	}
	return Py_FindMethod(cdplayer_methods, (PyObject *)self, name);
}

PyTypeObject CdPlayertype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,			/*ob_size*/
	"cdplayer",		/*tp_name*/
	sizeof(cdplayerobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)cdplayer_dealloc, /*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)cdplayer_getattr, /*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
};

static PyObject *
newcdplayerobject(cdp)
	CDPLAYER *cdp;
{
	cdplayerobject *p;

	p = PyObject_NEW(cdplayerobject, &CdPlayertype);
	if (p == NULL)
		return NULL;
	p->ob_cdplayer = cdp;
	return (PyObject *) p;
}

static PyObject *
CD_open(self, args)
	PyObject *self, *args;
{
	char *dev, *direction;
	CDPLAYER *cdp;

	/*
	 * Variable number of args.
	 * First defaults to "None", second defaults to "r".
	 */
	dev = NULL;
	direction = "r";
	if (!PyArg_ParseTuple(args, "|zs:open", &dev, &direction))
		return NULL;

	cdp = CDopen(dev, direction);
	if (cdp == NULL) {
		PyErr_SetFromErrno(CdError);
		return NULL;
	}

	return newcdplayerobject(cdp);
}

typedef struct {
	PyObject_HEAD
	CDPARSER *ob_cdparser;
	struct {
		PyObject *ob_cdcallback;
		PyObject *ob_cdcallbackarg;
	} ob_cdcallbacks[NCALLBACKS];
} cdparserobject;

static void
CD_callback(arg, type, data)
	void *arg;
	CDDATATYPES type;
	void *data;
{
	PyObject *result, *args, *v = NULL;
	char *p;
	int i;
	cdparserobject *self;

	self = (cdparserobject *) arg;
	args = PyTuple_New(3);
	if (args == NULL)
		return;
	Py_INCREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
	PyTuple_SetItem(args, 0, self->ob_cdcallbacks[type].ob_cdcallbackarg);
	PyTuple_SetItem(args, 1, PyInt_FromLong((long) type));
	switch (type) {
	case cd_audio:
		v = PyString_FromStringAndSize(data, CDDA_DATASIZE);
		break;
	case cd_pnum:
	case cd_index:
		v = PyInt_FromLong(((CDPROGNUM *) data)->value);
		break;
	case cd_ptime:
	case cd_atime:
#define ptr ((struct cdtimecode *) data)
		v = Py_BuildValue("(iii)",
			    ptr->mhi * 10 + ptr->mlo,
			    ptr->shi * 10 + ptr->slo,
			    ptr->fhi * 10 + ptr->flo);
#undef ptr
		break;
	case cd_catalog:
		v = PyString_FromStringAndSize(NULL, 13);
		p = PyString_AsString(v);
		for (i = 0; i < 13; i++)
			*p++ = ((char *) data)[i] + '0';
		break;
	case cd_ident:
#define ptr ((struct cdident *) data)
		v = PyString_FromStringAndSize(NULL, 12);
		p = PyString_AsString(v);
		CDsbtoa(p, ptr->country, 2);
		p += 2;
		CDsbtoa(p, ptr->owner, 3);
		p += 3;
		*p++ = ptr->year[0] + '0';
		*p++ = ptr->year[1] + '0';
		*p++ = ptr->serial[0] + '0';
		*p++ = ptr->serial[1] + '0';
		*p++ = ptr->serial[2] + '0';
		*p++ = ptr->serial[3] + '0';
		*p++ = ptr->serial[4] + '0';
#undef ptr
		break;
	case cd_control:
		v = PyInt_FromLong((long) *((unchar *) data));
		break;
	}
	PyTuple_SetItem(args, 2, v);
	if (PyErr_Occurred()) {
		Py_DECREF(args);
		return;
	}
	
	result = PyEval_CallObject(self->ob_cdcallbacks[type].ob_cdcallback,
				   args);
	Py_DECREF(args);
	Py_XDECREF(result);
}

static PyObject *
CD_deleteparser(self, args)
	cdparserobject *self;
	PyObject *args;
{
	int i;

	if (!PyArg_ParseTuple(args, ":deleteparser"))
		return NULL;

	CDdeleteparser(self->ob_cdparser);
	self->ob_cdparser = NULL;

	/* no sense in keeping the callbacks, so remove them */
	for (i = 0; i < NCALLBACKS; i++) {
		Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
		self->ob_cdcallbacks[i].ob_cdcallback = NULL;
		Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
		self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_parseframe(self, args)
	cdparserobject *self;
	PyObject *args;
{
	char *cdfp;
	int length;
	CDFRAME *p;

	if (!PyArg_ParseTuple(args, "s#:parseframe", &cdfp, &length))
		return NULL;

	if (length % sizeof(CDFRAME) != 0) {
		PyErr_SetString(PyExc_TypeError, "bad length");
		return NULL;
	}

	p = (CDFRAME *) cdfp;
	while (length > 0) {
		CDparseframe(self->ob_cdparser, p);
		length -= sizeof(CDFRAME);
		p++;
		if (PyErr_Occurred())
			return NULL;
	}

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_removecallback(self, args)
	cdparserobject *self;
	PyObject *args;
{
	int type;

	if (!PyArg_ParseTuple(args, "i:removecallback", &type))
		return NULL;

	if (type < 0 || type >= NCALLBACKS) {
		PyErr_SetString(PyExc_TypeError, "bad type");
		return NULL;
	}

	CDremovecallback(self->ob_cdparser, (CDDATATYPES) type);

	Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
	self->ob_cdcallbacks[type].ob_cdcallback = NULL;

	Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
	self->ob_cdcallbacks[type].ob_cdcallbackarg = NULL;

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_resetparser(self, args)
	cdparserobject *self;
	PyObject *args;
{
	if (!PyArg_ParseTuple(args, ":resetparser"))
		return NULL;

	CDresetparser(self->ob_cdparser);

	Py_INCREF(Py_None);
	return Py_None;
}

static PyObject *
CD_addcallback(self, args)
	cdparserobject *self;
	PyObject *args;
{
	int type;
	PyObject *func, *funcarg;

	/* XXX - more work here */
	if (!PyArg_ParseTuple(args, "iOO:addcallback", &type, &func, &funcarg))
		return NULL;

	if (type < 0 || type >= NCALLBACKS) {
		PyErr_SetString(PyExc_TypeError, "argument out of range");
		return NULL;
	}

#ifdef CDsetcallback
	CDaddcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
		      (void *) self);
#else
	CDsetcallback(self->ob_cdparser, (CDDATATYPES) type, CD_callback,
		      (void *) self);
#endif
	Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallback);
	Py_INCREF(func);
	self->ob_cdcallbacks[type].ob_cdcallback = func;
	Py_XDECREF(self->ob_cdcallbacks[type].ob_cdcallbackarg);
	Py_INCREF(funcarg);
	self->ob_cdcallbacks[type].ob_cdcallbackarg = funcarg;

/*
	if (type == cd_audio) {
		sigfpe_[_UNDERFL].repls = _ZERO;
		handle_sigfpes(_ON, _EN_UNDERFL, NULL,
		                        _ABORT_ON_ERROR, NULL);
	}
*/

	Py_INCREF(Py_None);
	return Py_None;
}

static PyMethodDef cdparser_methods[] = {
	{"addcallback",		(PyCFunction)CD_addcallback,   	1},
	{"deleteparser",	(PyCFunction)CD_deleteparser,	1},
	{"parseframe",		(PyCFunction)CD_parseframe,	1},
	{"removecallback",	(PyCFunction)CD_removecallback,	1},
	{"resetparser",		(PyCFunction)CD_resetparser,	1},
		                                /* backward compatibility */
	{"setcallback",		(PyCFunction)CD_addcallback,   	1},
	{NULL,			NULL} 		/* sentinel */
};

static void
cdparser_dealloc(self)
	cdparserobject *self;
{
	int i;

	for (i = 0; i < NCALLBACKS; i++) {
		Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallback);
		self->ob_cdcallbacks[i].ob_cdcallback = NULL;
		Py_XDECREF(self->ob_cdcallbacks[i].ob_cdcallbackarg);
		self->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
	}
	CDdeleteparser(self->ob_cdparser);
	PyMem_DEL(self);
}

static PyObject *
cdparser_getattr(self, name)
	cdparserobject *self;
	char *name;
{
	if (self->ob_cdparser == NULL) {
		PyErr_SetString(PyExc_RuntimeError, "no parser active");
		return NULL;
	}

	return Py_FindMethod(cdparser_methods, (PyObject *)self, name);
}

PyTypeObject CdParsertype = {
	PyObject_HEAD_INIT(&PyType_Type)
	0,			/*ob_size*/
	"cdparser",		/*tp_name*/
	sizeof(cdparserobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)cdparser_dealloc, /*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)cdparser_getattr, /*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
};

static PyObject *
newcdparserobject(cdp)
	CDPARSER *cdp;
{
	cdparserobject *p;
	int i;

	p = PyObject_NEW(cdparserobject, &CdParsertype);
	if (p == NULL)
		return NULL;
	p->ob_cdparser = cdp;
	for (i = 0; i < NCALLBACKS; i++) {
		p->ob_cdcallbacks[i].ob_cdcallback = NULL;
		p->ob_cdcallbacks[i].ob_cdcallbackarg = NULL;
	}
	return (PyObject *) p;
}

static PyObject *
CD_createparser(self, args)
	PyObject *self, *args;
{
	CDPARSER *cdp;

	if (!PyArg_ParseTuple(args, ":createparser"))
		return NULL;
	cdp = CDcreateparser();
	if (cdp == NULL) {
		PyErr_SetString(CdError, "createparser failed");
		return NULL;
	}

	return newcdparserobject(cdp);
}

static PyObject *
CD_msftoframe(self, args)
	PyObject *self, *args;
{
	int min, sec, frame;

	if (!PyArg_ParseTuple(args, "iii:msftoframe", &min, &sec, &frame))
		return NULL;

	return PyInt_FromLong((long) CDmsftoframe(min, sec, frame));
}
	
static PyMethodDef CD_methods[] = {
	{"open",		(PyCFunction)CD_open,		1},
	{"createparser",	(PyCFunction)CD_createparser,	1},
	{"msftoframe",		(PyCFunction)CD_msftoframe,	1},
	{NULL,		NULL}	/* Sentinel */
};

void
initcd()
{
	PyObject *m, *d;

	m = Py_InitModule("cd", CD_methods);
	d = PyModule_GetDict(m);

	CdError = PyErr_NewException("cd.error", NULL, NULL);
	PyDict_SetItemString(d, "error", CdError);

	/* Identifiers for the different types of callbacks from the parser */
	PyDict_SetItemString(d, "audio", PyInt_FromLong((long) cd_audio));
	PyDict_SetItemString(d, "pnum", PyInt_FromLong((long) cd_pnum));
	PyDict_SetItemString(d, "index", PyInt_FromLong((long) cd_index));
	PyDict_SetItemString(d, "ptime", PyInt_FromLong((long) cd_ptime));
	PyDict_SetItemString(d, "atime", PyInt_FromLong((long) cd_atime));
	PyDict_SetItemString(d, "catalog", PyInt_FromLong((long) cd_catalog));
	PyDict_SetItemString(d, "ident", PyInt_FromLong((long) cd_ident));
	PyDict_SetItemString(d, "control", PyInt_FromLong((long) cd_control));

	/* Block size information for digital audio data */
	PyDict_SetItemString(d, "DATASIZE",
			   PyInt_FromLong((long) CDDA_DATASIZE));
	PyDict_SetItemString(d, "BLOCKSIZE",
			   PyInt_FromLong((long) CDDA_BLOCKSIZE));

	/* Possible states for the cd player */
	PyDict_SetItemString(d, "ERROR", PyInt_FromLong((long) CD_ERROR));
	PyDict_SetItemString(d, "NODISC", PyInt_FromLong((long) CD_NODISC));
	PyDict_SetItemString(d, "READY", PyInt_FromLong((long) CD_READY));
	PyDict_SetItemString(d, "PLAYING", PyInt_FromLong((long) CD_PLAYING));
	PyDict_SetItemString(d, "PAUSED", PyInt_FromLong((long) CD_PAUSED));
	PyDict_SetItemString(d, "STILL", PyInt_FromLong((long) CD_STILL));
#ifdef CD_CDROM			/* only newer versions of the library */
	PyDict_SetItemString(d, "CDROM", PyInt_FromLong((long) CD_CDROM));
#endif

	if (PyErr_Occurred())
		Py_FatalError("can't initialize module cd");
}