pygame / src / cdrom.c

  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
/*
    PyGame - Python Game Library
    Copyright (C) 2000  Pete Shinners

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the Free
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    Pete Shinners
    pete@shinners.org
*/

#define PYGAMEAPI_CDROM_INTERNAL
#include "pygame.h"




staticforward PyTypeObject PyCD_Type;
static PyObject* PyCD_New(SDL_CD* cdrom);
#define PyCD_Check(x) ((x)->ob_type == &PyCD_Type)



static void cdrom_autoquit()
{
	if(SDL_WasInit(SDL_INIT_CDROM))
		SDL_QuitSubSystem(SDL_INIT_CDROM);
}

static PyObject* cdrom_autoinit(PyObject* self, PyObject* arg)
{
	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	if(!SDL_WasInit(SDL_INIT_CDROM))
	{
		if(SDL_InitSubSystem(SDL_INIT_CDROM))
			return PyInt_FromLong(0);
		PyGame_RegisterQuit(cdrom_autoquit);
	}
	return PyInt_FromLong(1);
}


    /*DOC*/ static char doc_cdrom_quit[] =
    /*DOC*/    "pygame.cdrom.quit() -> None\n"
    /*DOC*/    "uninitialize the cdrom subsystem\n"
    /*DOC*/    "\n"
    /*DOC*/    "Uninitialize the CDROM module manually\n"
    /*DOC*/ ;

static PyObject* cdrom_quit(PyObject* self, PyObject* arg)
{
	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	cdrom_autoquit();

	RETURN_NONE
}


    /*DOC*/ static char doc_cdrom_init[] =
    /*DOC*/    "pygame.cdrom.init() -> None\n"
    /*DOC*/    "initialize the cdrom subsystem\n"
    /*DOC*/    "\n"
    /*DOC*/    "Initialize the CDROM module manually\n"
    /*DOC*/ ;

static PyObject* cdrom_init(PyObject* self, PyObject* arg)
{
	PyObject* result;
	int istrue;

	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	result = cdrom_autoinit(self, arg);
	istrue = PyObject_IsTrue(result);
	Py_DECREF(result);
	if(!istrue)
		return RAISE(PyExc_SDLError, SDL_GetError());

	RETURN_NONE
}



    /*DOC*/ static char doc_get_init[] =
    /*DOC*/    "pygame.cdrom.get_init() -> bool\n"
    /*DOC*/    "query init of cdrom module\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns true of the cdrom module is initialized\n"
    /*DOC*/ ;

static PyObject* get_init(PyObject* self, PyObject* arg)
{
	if(!PyArg_ParseTuple(arg, ""))
		return NULL;

	return PyInt_FromLong(SDL_WasInit(SDL_INIT_CDROM)!=0);
}


static void cd_dealloc(PyObject* self)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

	if(SDL_WasInit(SDL_INIT_CDROM))
		SDL_CDClose(cd_ref->cd);
	PyMem_DEL(self);	
}



    /*DOC*/ static char doc_cdrom_open[] =
    /*DOC*/    "pygame.cdrom.open(id) -> CD\n"
    /*DOC*/    "open cd device\n"
    /*DOC*/    "\n"
    /*DOC*/    "Creates a new CD object for the given CDROM id.\n"
    /*DOC*/ ;

static PyObject* cdrom_open(PyObject* self, PyObject* args)
{
	int id;
	
	if(!PyArg_ParseTuple(args, "i", &id))
		return NULL;

	CDROM_INIT_CHECK();

	return PyCD_New(SDL_CDOpen(id));
}



    /*DOC*/ static char doc_cdrom_count[] =
    /*DOC*/    "pygame.cdrom.count() -> int\n"
    /*DOC*/    "query number of cdroms on system\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the number of CDROM drives available on\n"
    /*DOC*/    "the system\n"
    /*DOC*/ ;

static PyObject* cdrom_count(PyObject* self, PyObject* args)
{
	if(!PyArg_ParseTuple(args, ""))
		return NULL;

	CDROM_INIT_CHECK();

	return PyInt_FromLong(SDL_CDNumDrives());
}



    /*DOC*/ static char doc_cdrom_name[] =
    /*DOC*/    "pygame.cdrom.name(id) -> string\n"
    /*DOC*/    "query name of cdrom drive\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns the name of the CDROM device, given by the\n"
    /*DOC*/    "system.\n"
    /*DOC*/ ;

static PyObject* cdrom_name(PyObject* self, PyObject* args)
{
	int drive;
	
	if(!PyArg_ParseTuple(args, "i", &drive))
		return NULL;

	CDROM_INIT_CHECK();

	return PyString_FromString(SDL_CDName(drive));
}



    /*DOC*/ static char doc_cd_play_tracks[] =
    /*DOC*/    "CD.play_tracks(start_track, start_frame, ntracks, nframes) -> int\n"
    /*DOC*/    "play music from cdrom\n"
    /*DOC*/    "\n"
    /*DOC*/    "Start playing from start_track, for ntracks and\n"
    /*DOC*/    "nframes. If ntracks and nframes are 0, it will\n"
    /*DOC*/    "play until the end of the cdrom\n"
    /*DOC*/ ;

static PyObject* cd_play_tracks(PyObject* self, PyObject* args)
{
	int start_track, start_frame, ntracks, nframes;
	PyCDObject* cd_ref = (PyCDObject*)self;

	if(!PyArg_ParseTuple(args, "iiii", &start_track, &start_frame, &ntracks,
		&nframes	))
		return NULL;
		
	SDL_CDStatus(cd_ref->cd);
		
	return PyInt_FromLong(
		SDL_CDPlayTracks(cd_ref->cd, start_track, start_frame, ntracks, nframes));
}



    /*DOC*/ static char doc_cd_play[] =
    /*DOC*/    "CD.play(start_frame, nframes) -> int\n"
    /*DOC*/    "play music from cdrom\n"
    /*DOC*/    "\n"
    /*DOC*/    "Start playing from start_frame, for nframes. If\n"
    /*DOC*/    "nframes is 0, it will play until the end of the\n"
    /*DOC*/    "cdrom\n"
    /*DOC*/ ;

static PyObject* cd_play(PyObject* self, PyObject* args)
{
	int start_frame, nframes;
	PyCDObject* cd_ref = (PyCDObject*)self;
	
	if(!PyArg_ParseTuple(args, "ii", &start_frame, &nframes))
		return NULL;

	SDL_CDStatus(cd_ref->cd);

	return PyInt_FromLong(SDL_CDPlay(cd_ref->cd, start_frame, nframes));
}



    /*DOC*/ static char doc_cd_pause[] =
    /*DOC*/    "CD.pause() -> int\n"
    /*DOC*/    "pause playing cdrom\n"
    /*DOC*/    "\n"
    /*DOC*/    "Pauses the playing CD.\n"
    /*DOC*/ ;

static PyObject* cd_pause(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

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

	if(SDL_CDStatus(cd_ref->cd) != CD_PLAYING)
		return PyInt_FromLong(-1);

	return PyInt_FromLong(SDL_CDPause(cd_ref->cd));
}



    /*DOC*/ static char doc_cd_resume[] =
    /*DOC*/    "CD.resume() -> int\n"
    /*DOC*/    "resume paused cdrom\n"
    /*DOC*/    "\n"
    /*DOC*/    "Resumes playback of a paused CD.\n"
    /*DOC*/ ;

static PyObject* cd_resume(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

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

	if(SDL_CDStatus(cd_ref->cd) != CD_PAUSED)
		return PyInt_FromLong(-1);

	return PyInt_FromLong(SDL_CDResume(cd_ref->cd));
}



    /*DOC*/ static char doc_cd_stop[] =
    /*DOC*/    "CD.stop() -> int\n"
    /*DOC*/    "stops playing cdrom\n"
    /*DOC*/    "\n"
    /*DOC*/    "Stops the playing CD.\n"
    /*DOC*/ ;

static PyObject* cd_stop(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

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

	if(SDL_CDStatus(cd_ref->cd) < CD_PLAYING)
		return PyInt_FromLong(-1);

	return PyInt_FromLong(SDL_CDStop(cd_ref->cd));
}



    /*DOC*/ static char doc_cd_eject[] =
    /*DOC*/    "CD.eject() -> int\n"
    /*DOC*/    "ejects cdrom drive\n"
    /*DOC*/    "\n"
    /*DOC*/    "Ejects the media from the CDROM drive.\n"
    /*DOC*/ ;

static PyObject* cd_eject(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

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

	if(SDL_CDStatus(cd_ref->cd) <= 0 )
		return PyInt_FromLong(-1);

	return PyInt_FromLong(SDL_CDEject(cd_ref->cd));
}



    /*DOC*/ static char doc_cd_status[] =
    /*DOC*/    "CD.get_status() -> int\n"
    /*DOC*/    "query drive status\n"
    /*DOC*/    "\n"
    /*DOC*/    "Get the status of the CDROM drive.\n"
    /*DOC*/ ;

static PyObject* cd_status(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

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

	return PyInt_FromLong(SDL_CDStatus(cd_ref->cd));
}



    /*DOC*/ static char doc_cd_cur_track[] =
    /*DOC*/    "CD.get_cur_track() -> int\n"
    /*DOC*/    "query current track\n"
    /*DOC*/    "\n"
    /*DOC*/    "Get the current track of a playing CD.\n"
    /*DOC*/ ;

static PyObject* cd_cur_track(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;
	SDL_CD* s_cd_ref = cd_ref->cd;

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

	if(SDL_CDStatus(s_cd_ref) <= 1)
		return PyInt_FromLong(-1);

	return PyInt_FromLong(s_cd_ref->cur_track);
}



    /*DOC*/ static char doc_cd_cur_frame[] =
    /*DOC*/    "CD.get_cur_frame() -> int\n"
    /*DOC*/    "query current frame\n"
    /*DOC*/    "\n"
    /*DOC*/    "Get the current frame of a playing CD.\n"
    /*DOC*/ ;

static PyObject* cd_cur_frame(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;
	SDL_CD* s_cd_ref = cd_ref->cd;

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

	if(SDL_CDStatus(s_cd_ref) <= 1)
		return PyInt_FromLong(-1);

	return PyInt_FromLong(s_cd_ref->cur_frame);
}



    /*DOC*/ static char doc_cd_get_empty[] =
    /*DOC*/    "CD.get_empty() -> bool\n"
    /*DOC*/    "query status of drive\n"
    /*DOC*/    "\n"
    /*DOC*/    "Returns true when there is no disk in the CD\n"
    /*DOC*/    "drive.\n"
    /*DOC*/ ;

static PyObject* cd_get_empty(PyObject* self, PyObject* args)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

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

	return PyInt_FromLong(CD_INDRIVE(SDL_CDStatus(cd_ref->cd)));
}





static int cd_seq_len(PyObject* self)
{
	PyCDObject* cd_ref = (PyCDObject*)self;

	if(!SDL_WasInit(SDL_INIT_CDROM))
	{
		RAISE(PyExc_RuntimeError, "SDL has shut down.");
		return -1;
	}
	
	if(!CD_INDRIVE(SDL_CDStatus(cd_ref->cd)))
	{
		RAISE(PyExc_RuntimeError, "No CD present");
		return -1;
	}

	return cd_ref->cd->numtracks;
}



static PyObject* cd_seq_get(PyObject* self, int index)
{
	PyCDObject* cd_ref = (PyCDObject*)self;
	SDL_CD* s_cd_ref = cd_ref->cd;
	SDL_CDtrack* s_track_ref;

	if(!SDL_WasInit(SDL_INIT_CDROM))
		return RAISE(PyExc_RuntimeError, "SDL has shut down.");

	if(!CD_INDRIVE(SDL_CDStatus(s_cd_ref)) || index >= s_cd_ref->numtracks
		|| index < 0)
	{
		return PyInt_FromLong(-1);
	}

	s_track_ref = &s_cd_ref->track[index];
																			 
	return Py_BuildValue("(iii)", s_track_ref->type, s_track_ref->length,
		s_track_ref->offset);	
}



static PyObject* cd_seq_get_slice(PyObject* self, int start, int end)
{
	PyCDObject* cd_ref = (PyCDObject*)self;
	SDL_CD* s_cd_ref = cd_ref->cd;
	PyObject* track_tuple;
	int count;
	int i;

	if(!SDL_WasInit(SDL_INIT_CDROM))
		return RAISE(PyExc_RuntimeError, "SDL has shut down.");
	
	i = min(start,end);
	end = max(start,end);
	start = i;

	if(start < 0)
		return RAISE(PyExc_IndexError, "Index out of range.");
	
	if(CD_INDRIVE(SDL_CDStatus(s_cd_ref)) <= 0)
		return RAISE(PyExc_RuntimeError, "CD unavailable");

	/* If end is greater than the real number of tracks, trunctate it.
	 * This is done because of how Python handles slicing
	*/
	end = min(end, s_cd_ref->numtracks);
	count = end - start;

	track_tuple = PyTuple_New(count);
	if(!track_tuple)
		return NULL;

	for(i = 0;i < count;i++)
	{
		PyObject* elem_tuple;
		SDL_CDtrack* s_track_ref = &s_cd_ref->track[i + start];

		elem_tuple = Py_BuildValue("(iii)",
			s_track_ref->type, s_track_ref->length, s_track_ref->offset);
		if(!elem_tuple)
		{
			Py_DECREF(track_tuple);
			return NULL;
		}

		PyTuple_SET_ITEM(track_tuple, i, elem_tuple);
	}
	return track_tuple; 	
}



static PyMethodDef cd_builtins[] =
{
	{ "play_tracks", cd_play_tracks, 1, doc_cd_play_tracks },
	{ "play", cd_play, 1, doc_cd_play },
	{ "pause", cd_pause, 1, doc_cd_pause },
	{ "resume", cd_resume, 1, doc_cd_resume },
	{ "stop", cd_stop, 1, doc_cd_stop },
	{ "eject", cd_eject, 1, doc_cd_eject },
	{ "get_status", cd_status, 1, doc_cd_status },
	{ "get_track", cd_cur_track, 1, doc_cd_cur_track },
	{ "get_frame", cd_cur_frame, 1, doc_cd_cur_frame },
	{ "get_empty", cd_get_empty, 1, doc_cd_get_empty },
	{ NULL, NULL }
};

static PyObject* cd_getattr(PyObject* self, char* attrname)
{
	if(SDL_WasInit(SDL_INIT_CDROM))
		return Py_FindMethod(cd_builtins, self, attrname);

	return RAISE(PyExc_NameError, attrname);
}



static PySequenceMethods cd_as_sequence = 
{
	cd_seq_len,
	0,
	0,
	cd_seq_get,
	cd_seq_get_slice,
	0,
	0
};


    /*DOC*/ static char doc_CD_MODULE[] =
    /*DOC*/    "thin wrapper around the SDL CDROM api, likely to\n"
    /*DOC*/    "change\n"
    /*DOC*/ ;


static PyTypeObject PyCD_Type =
{
	PyObject_HEAD_INIT(NULL)
	0,
	"CD",
	sizeof(PyCDObject),
	0,
	cd_dealloc,
	0,
	cd_getattr,
	0,
	0,
	0,
	0,
	&cd_as_sequence,
	0
};



static PyObject* PyCD_New(SDL_CD* cdrom)
{
	PyCDObject* cd;

	if(!cdrom)
		return RAISE(PyExc_SDLError, SDL_GetError());
	
	cd = PyObject_NEW(PyCDObject, &PyCD_Type);
	if(!cd) return NULL;

	cd->cd = cdrom;

	return (PyObject*)cd;
}





static PyMethodDef cdrom_builtins[] =
{
	{ "__PYGAMEinit__", cdrom_autoinit, 1, doc_cdrom_init },
	{ "init", cdrom_init, 1, doc_cdrom_init },
	{ "quit", cdrom_quit, 1, doc_cdrom_quit },
	{ "get_init", get_init, 1, doc_get_init },
	{ "get_count", cdrom_count, 1, doc_cdrom_count },
	{ "get_name", cdrom_name, 1, doc_cdrom_name },
	{ "open", cdrom_open, 1, doc_cdrom_open },
	{ NULL, NULL }
};



    /*DOC*/ static char doc_pygame_cdrom_MODULE[] =
    /*DOC*/    "thin wrapper around the SDL CDROM api, likely to\n"
    /*DOC*/    "change\n"
    /*DOC*/ ;


void initcdrom()
{
	PyObject *module, *dict, *apiobj;
	static void* c_api[PYGAMEAPI_CDROM_NUMSLOTS];

	PyType_Init(PyCD_Type);


    /* create the module */
	module = Py_InitModule3("cdrom", cdrom_builtins, doc_pygame_cdrom_MODULE);
	dict = PyModule_GetDict(module);

	/* export the c api */
	c_api[0] = &PyCD_Type;
	c_api[1] = PyCD_New;
	apiobj = PyCObject_FromVoidPtr(c_api, NULL);
	PyDict_SetItemString(dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);

	/*imported needed apis*/
	import_pygame_base();
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.