1 /*
2 * This file is part of WorkMan, the civilized CD player library
3 * Copyright (C) 1991-1997 by Steven Grimm <koreth@midwinter.com>
4 * Copyright (C) by Dirk Försterling <milliByte@DeathsDoor.com>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 *
21 * IRIX specific.
22 *
23 * Taken from the kscd distribution
24 *
25 * Paul Kendall
26 * paul@orion.co.nz, or
27 * paul@kcbbs.gen.nz
28 */
29
30 #if defined(sgi) || defined(__sgi)
31
32 #include "include/wm_config.h"
33
34 /*
35 * Yes, it was designed for WorkMan 1.4b3
36 * Because I did start over from 1.3a, I disable it here.
37 * There is no guarantee of getting working code by defining
38 * CDDA yourself.
39 *
40 */
41 #undef CDDA
42 /*#define CDDA*/
43
44 #include <sys/types.h>
45 #include <sys/time.h>
46 #include <stdio.h>
47 #include <ctype.h>
48 #include <string.h>
49 #include <fcntl.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <signal.h>
53 #include <sigfpe.h>
54 #include <dmedia/cdaudio.h>
55 #include <dmedia/audio.h>
56 #include <errno.h>
57
58 #include "include/wm_struct.h"
59 #include "include/wm_cdtext.h"
60
61 #define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
62
63 void *malloc();
64 char *strchr();
65
66 int min_volume = 0;
67 int max_volume = 255;
68
69 #ifdef CDDA
70 static int playing = STOPPED;
71 static CDPLAYER *icd;
72 static CDPARSER *icdp;
73 static CDFRAME cdbuf[12];
74 static ALport audioport;
75 static ALconfig aconfig;
76 static struct itimerval audiotimer = { {0,0}, {0,25000} };
77 static int cdtrack=0;
78 static int cdframe=0;
79 static int cdstopframe=0;
80
81 /*
82 * Platform specific internal functions for CDDA
83 */
84 void
cbprognum(void * arg,CDDATATYPES type,CDPROGNUM * prognum)85 cbprognum(void *arg, CDDATATYPES type, CDPROGNUM* prognum)
86 {
87 cdtrack = prognum->value;
88 } /* cbprognum() */
89
90 void
cbabstime(void * arg,CDDATATYPES type,struct cdtimecode * atime)91 cbabstime(void *arg, CDDATATYPES type, struct cdtimecode* atime)
92 {
93 cdframe = CDtctoframe(atime);
94 if( cdframe == cdstopframe )
95 playing = STOPPED;
96 } /* cbabstime() */
97
98 void
cbplayaudio(void * arg,CDDATATYPES type,short * audio)99 cbplayaudio(void *arg, CDDATATYPES type, short* audio)
100 {
101 if(playing != PLAYING) return;
102 ALwritesamps(audioport, audio, CDDA_NUMSAMPLES);
103 } /* cbplayaudio() */
104
105 static void
alarmsignal()106 alarmsignal()
107 {
108 int n, i;
109 if(playing != PLAYING) return;
110 if( ALgetfilled(audioport) < CDDA_NUMSAMPLES*8 )
111 {
112 /* Only get more samples and play them if we're getting low
113 * this ensures that the CD stays close to the sound
114 */
115 n = CDreadda(icd, cdbuf, 12);
116 if( n == 0 ) return;
117 for( i=0 ; i<12 ; i++ )
118 CDparseframe(icdp, &cdbuf[i]);
119 }
120 signal(SIGALRM, alarmsignal);
121 setitimer(ITIMER_REAL, &audiotimer, NULL);
122 } /* alarmsignal() */
123 #endif
124
125 /*--------------------------------------------------------*
126 * Initialize the drive. A no-op for the generic driver.
127 *--------------------------------------------------------*/
128 int
gen_init(struct wm_drive * d)129 gen_init( struct wm_drive *d )
130 {
131 #ifdef CDDA
132 long Param[4];
133 /* Set the audio rate to 44100Hz 16bit 2s-comp stereo */
134 aconfig = ALnewconfig();
135 ALsetwidth(aconfig, AL_SAMPLE_16);
136 ALsetsampfmt(aconfig, AL_SAMPFMT_TWOSCOMP);
137 ALsetchannels(aconfig, 2);
138 Param[0] = AL_OUTPUT_RATE; Param[1] = AL_RATE_44100;
139 Param[2] = AL_CHANNEL_MODE; Param[3] = AL_STEREO;
140 ALsetparams(AL_DEFAULT_DEVICE, Param, 4);
141 audioport = ALopenport("KDE KSCD Audio", "w", aconfig);
142
143 /* setup cdparser */
144 icdp = CDcreateparser();
145 CDaddcallback(icdp, cd_audio, (CDCALLBACKFUNC)cbplayaudio, 0);
146 CDaddcallback(icdp, cd_pnum, (CDCALLBACKFUNC)cbprognum, 0);
147 CDaddcallback(icdp, cd_atime, (CDCALLBACKFUNC)cbabstime, 0);
148
149 /* Lets handle those floating point exceptions expeditiously. */
150 sigfpe_[_UNDERFL].repls = _ZERO;
151 handle_sigfpes(_ON, _EN_UNDERFL, NULL, _ABORT_ON_ERROR, NULL);
152 #endif
153 return 0;
154 } /* gen_init() */
155
156 /*-------------------------------------------------------------*
157 * Open the CD and figure out which kind of drive is attached.
158 *-------------------------------------------------------------*/
159 int
gen_open(struct wm_drive * d)160 gen_open( struct wm_drive *d )
161 {
162 CDSTATUS s;
163
164 if (d->fd < 0) /* Device already open? */
165 {
166 d->daux = CDopen(d->cd_device, "r");
167 if (d->daux == 0) {
168 return -6;
169 }
170
171 d->fd = 1;
172 #ifdef CDDA
173 icd = d->daux;
174 #endif
175 } else {
176 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "gen_open(): [device is open (fd=%d)]\n", d->fd);
177 }
178
179 CDgetstatus(d->daux, &s);
180 if( s.state == CD_NODISC || s.state == CD_ERROR )
181 return 1;
182
183 return 0;
184 } /* gen_open() */
185
186 /*----------------------------------*
187 * Send a SCSI command out the bus.
188 *----------------------------------*/
189 int
gen_scsi(struct wm_drive * d,unsigned char * xcdb,int cdblen,char * retbuf,int retbuflen,int getreply)190 gen_scsi( struct wm_drive *d, unsigned char *xcdb, int cdblen,
191 char *retbuf, int retbuflen, int getreply)
192 {
193 return -1;
194 } /* gen_scsi() */
195
196 int
gen_close(struct wm_drive * d)197 gen_close( struct wm_drive *d )
198 {
199 if(d->fd != -1) {
200 wm_lib_message(WM_MSG_LEVEL_DEBUG|WM_MSG_CLASS, "closing the device\n");
201 close(d->fd);
202 d->fd = -1;
203 }
204 return 0;
205 }
206
207 /*--------------------------------------------------------------------------*
208 * Get the current status of the drive: the current play mode, the absolute
209 * position from start of disc (in frames), and the current track and index
210 * numbers if the CD is playing or paused.
211 *--------------------------------------------------------------------------*/
212 int
gen_get_drive_status(struct wm_drive * d,int oldmode,int * mode,int * pos,int * track,int * index)213 gen_get_drive_status( struct wm_drive *d, int oldmode,
214 int *mode, int *pos, int *track,
215 int *index )
216 {
217 #ifdef CDDA
218 *mode = playing;
219 *track = cdtrack;
220 *pos = cdframe;
221 *index = 0;
222 #else
223 CDSTATUS s;
224 if( CDgetstatus(d->daux, &s)==0 )
225 return -1;
226 *pos = CDmsftoframe(s.min,s.sec,s.frame);
227 *track = s.track;
228 *index = 0;
229 switch( s.state )
230 {
231 case CD_READY: *mode = WM_CDM_STOPPED;
232 break;
233 case CD_STILL:
234 case CD_PAUSED: *mode = WM_CDM_PAUSED;
235 break;
236 case CD_PLAYING: *mode = WM_CDM_PLAYING;
237 break;
238 default: *mode = WM_CDM_UNKNOWN;
239 }
240 #endif
241 return 0;
242 } /* gen_get_drive_status() */
243
244 /*-------------------------------------*
245 * Get the number of tracks on the CD.
246 *-------------------------------------*/
247 int
gen_get_trackcount(struct wm_drive * d,int * tracks)248 gen_get_trackcount( struct wm_drive *d, int *tracks )
249 {
250 CDSTATUS s;
251 if( CDgetstatus(d->daux, &s)==0 )
252 return -1;
253 *tracks = s.last;
254 return 0;
255 } /* gen_get_trackcount() */
256
257 /*---------------------------------------------------------*
258 * Get the start time and mode (data or audio) of a track.
259 *---------------------------------------------------------*/
260 int
gen_get_trackinfo(struct wm_drive * d,int track,int * data,int * startframe)261 gen_get_trackinfo( struct wm_drive *d, int track, int *data, int *startframe)
262 {
263 CDTRACKINFO i;
264 int ret = CDgettrackinfo(d->daux, track, &i);
265 if( ret == 0 )
266 return -1;
267 *data = 0;
268 *startframe = CDmsftoframe(i.start_min,i.start_sec,i.start_frame);
269 return 0;
270 } /* gen_get_trackinfo() */
271
272 /*-------------------------------------*
273 * Get the number of frames on the CD.
274 *-------------------------------------*/
275 int
gen_get_cdlen(struct wm_drive * d,int * frames)276 gen_get_cdlen( struct wm_drive *d, int *frames )
277 {
278 CDSTATUS s;
279 if( CDgetstatus(d->daux, &s)==0 )
280 return -1;
281 *frames = CDmsftoframe(s.total_min,s.total_sec,s.total_frame);
282 return 0;
283 } /* gen_get_cdlen() */
284
285 /*------------------------------------------------------------*
286 * Play the CD from one position to another (both in frames.)
287 *------------------------------------------------------------*/
288 int
gen_play(struct wm_drive * d,int start,int end)289 gen_play( struct wm_drive *d, int start, int end )
290 {
291 #ifdef CDDA
292 int m, s, f;
293 CDframetomsf(start, &m, &s, &f);
294 CDseek(icd, m, s, f);
295 cdstopframe = end;
296 playing = PLAYING;
297 signal(SIGALRM, alarmsignal);
298 setitimer(ITIMER_REAL, &audiotimer, NULL);
299 #else
300 int m, s, f;
301 CDframetomsf(start, &m, &s, &f);
302 CDplayabs(d->daux, m, s, f, 1);
303 #endif
304 return 0;
305 } /* gen_play() */
306
307 /*---------------*
308 * Pause the CD.
309 *---------------*/
310 int
gen_pause(struct wm_drive * d)311 gen_pause( struct wm_drive *d )
312 {
313 #ifdef CDDA
314 playing = WM_CDM_PAUSED;
315 #else
316 CDSTATUS s;
317 if( CDgetstatus(d->daux, &s)==0 )
318 return -1;
319 if(s.state == CD_PLAYING)
320 CDtogglepause(d->daux);
321 #endif
322 return 0;
323 } /* gen_pause() */
324
325 /*-------------------------------------------------*
326 * Resume playing the CD (assuming it was paused.)
327 *-------------------------------------------------*/
328 int
gen_resume(struct wm_drive * d)329 gen_resume( struct wm_drive *d )
330 {
331 #ifdef CDDA
332 playing = WM_CDM_PLAYING;
333 signal(SIGALRM, alarmsignal);
334 setitimer(ITIMER_REAL, &audiotimer, NULL);
335 #else
336 CDSTATUS s;
337 if( CDgetstatus(d->daux, &s)==0 )
338 return -1;
339 if(s.state == CD_PAUSED)
340 CDtogglepause(d->daux);
341 #endif
342 return 0;
343 } /* gen_resume() */
344
345 /*--------------*
346 * Stop the CD.
347 *--------------*/
348 int
gen_stop(struct wm_drive * d)349 gen_stop( struct wm_drive *d )
350 {
351 #ifdef CDDA
352 playing = WM_CDM_STOPPED;
353 #else
354 CDstop(d->daux);
355 #endif
356 return 0;
357 } /* gen_stop() */
358
359 /*----------------------------------------*
360 * Eject the current CD, if there is one.
361 *----------------------------------------*/
362 int
gen_eject(struct wm_drive * d)363 gen_eject( struct wm_drive *d )
364 {
365 #ifdef CDDA
366 playing = WM_CDM_STOPPED;
367 #endif
368 CDeject(d->daux);
369 return 0;
370 } /* gen_eject() */
371
372 /*----------------------------------------*
373 * Close the CD tray
374 *
375 * Please edit and send changes to
376 * milliByte@Deathsdoor.com
377 *----------------------------------------*/
378
379 int
gen_closetray(struct wm_drive * d)380 gen_closetray(struct wm_drive *d)
381 {
382 return -1;
383 } /* gen_closetray() */
384
385 /*---------------------------------------------------------------------*
386 * Set the volume level for the left and right channels. Their values
387 * range from 0 to 100.
388 *---------------------------------------------------------------------*/
389 int
gen_set_volume(struct wm_drive * d,int left,int right)390 gen_set_volume( struct wm_drive *d, int left, int right )
391 {
392 long Param[4];
393 Param[0] = AL_LEFT_SPEAKER_GAIN; Param[1] = left*255/100;
394 Param[2] = AL_RIGHT_SPEAKER_GAIN; Param[3] = right*255/100;
395 ALsetparams(AL_DEFAULT_DEVICE, Param, 4);
396 return 0;
397 } /* gen_set_volume() */
398
399 /*---------------------------------------------------------------------*
400 * Read the initial volume from the drive, if available. Each channel
401 * ranges from 0 to 100, with -1 indicating data not available.
402 *---------------------------------------------------------------------*/
403 int
gen_get_volume(struct wm_drive * d,int * left,int * right)404 gen_get_volume( struct wm_drive *d, int *left, int *right )
405 {
406 long Param[4];
407 Param[0] = AL_LEFT_SPEAKER_GAIN; Param[1] = 0;
408 Param[2] = AL_RIGHT_SPEAKER_GAIN; Param[3] = 0;
409 ALgetparams(AL_DEFAULT_DEVICE, Param, 4);
410 *left = Param[1] * 100 / 255;
411 *right = Param[3] * 100 / 255;
412 return 0;
413 } /* gen_get_volume() */
414
415 #endif
416
417