2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 #ifdef SDL_CDROM_FREEBSD
26 /* Functions for system-level CD-ROM audio control */
28 #include <sys/types.h>
35 #include "SDL_cdrom.h"
36 #include "../SDL_syscdrom.h"
39 /* The maximum number of CD-ROM drives we'll detect */
42 /* A list of available CD-ROM drives */
43 static char *SDL_cdlist[MAX_DRIVES];
44 static dev_t SDL_cdmode[MAX_DRIVES];
46 /* The system-dependent CD control functions */
47 static const char *SDL_SYS_CDName(int drive);
48 static int SDL_SYS_CDOpen(int drive);
49 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
50 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
51 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
52 static int SDL_SYS_CDPause(SDL_CD *cdrom);
53 static int SDL_SYS_CDResume(SDL_CD *cdrom);
54 static int SDL_SYS_CDStop(SDL_CD *cdrom);
55 static int SDL_SYS_CDEject(SDL_CD *cdrom);
56 static void SDL_SYS_CDClose(SDL_CD *cdrom);
58 /* Some ioctl() errno values which occur when the tray is empty */
59 #define ERRNO_TRAYEMPTY(errno) \
60 ((errno == EIO) || (errno == ENOENT) || (errno == EINVAL))
62 /* Check a drive to see if it is a CD-ROM */
63 static int CheckDrive(char *drive, struct stat *stbuf)
66 struct ioc_read_subchannel info;
68 /* If it doesn't exist, return -1 */
69 if ( stat(drive, stbuf) < 0 ) {
73 /* If it does exist, verify that it's an available CD-ROM */
75 if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) {
76 cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0);
78 info.address_format = CD_MSF_FORMAT;
79 info.data_format = CD_CURRENT_POSITION;
82 /* Under Linux, EIO occurs when a disk is not present.
83 This isn't 100% reliable, so we use the USE_MNTENT
86 if ( (ioctl(cdfd, CDIOCREADSUBCHANNEL, &info) == 0) ||
87 ERRNO_TRAYEMPTY(errno) ) {
96 /* Add a CD-ROM drive to our list of valid drives */
97 static void AddDrive(char *drive, struct stat *stbuf)
101 if ( SDL_numcds < MAX_DRIVES ) {
102 /* Check to make sure it's not already in our list.
103 This can happen when we see a drive via symbolic link.
105 for ( i=0; i<SDL_numcds; ++i ) {
106 if ( stbuf->st_rdev == SDL_cdmode[i] ) {
108 fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]);
114 /* Add this drive to our list */
116 SDL_cdlist[i] = SDL_strdup(drive);
117 if ( SDL_cdlist[i] == NULL ) {
121 SDL_cdmode[i] = stbuf->st_rdev;
124 fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
129 int SDL_SYS_CDInit(void)
131 /* checklist: /dev/cdrom,/dev/cd?c /dev/acd?c
132 /dev/matcd?c /dev/mcd?c /dev/scd?c */
133 static char *checklist[] = {
134 "cdrom", "?0 cd?", "?0 acd?", "?0 matcd?", "?0 mcd?", "?0 scd?",NULL
141 /* Fill in our driver capabilities */
142 SDL_CDcaps.Name = SDL_SYS_CDName;
143 SDL_CDcaps.Open = SDL_SYS_CDOpen;
144 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
145 SDL_CDcaps.Status = SDL_SYS_CDStatus;
146 SDL_CDcaps.Play = SDL_SYS_CDPlay;
147 SDL_CDcaps.Pause = SDL_SYS_CDPause;
148 SDL_CDcaps.Resume = SDL_SYS_CDResume;
149 SDL_CDcaps.Stop = SDL_SYS_CDStop;
150 SDL_CDcaps.Eject = SDL_SYS_CDEject;
151 SDL_CDcaps.Close = SDL_SYS_CDClose;
153 /* Look in the environment for our CD-ROM drive list */
154 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */
155 if ( SDLcdrom != NULL ) {
156 char *cdpath, *delim;
157 size_t len = SDL_strlen(SDLcdrom)+1;
158 cdpath = SDL_stack_alloc(char, len);
159 if ( cdpath != NULL ) {
160 SDL_strlcpy(cdpath, SDLcdrom, len);
163 delim = SDL_strchr(SDLcdrom, ':');
167 if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) {
168 AddDrive(SDLcdrom, &stbuf);
175 } while ( SDLcdrom );
176 SDL_stack_free(cdpath);
179 /* If we found our drives, there's nothing left to do */
180 if ( SDL_numcds > 0 ) {
185 /* Scan the system for CD-ROM drives */
186 for ( i=0; checklist[i]; ++i ) {
187 if ( checklist[i][0] == '?' ) {
190 for ( j=checklist[i][1]; exists; ++j ) {
191 SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%sc", &checklist[i][3]);
192 insert = SDL_strchr(drive, '?');
193 if ( insert != NULL ) {
196 switch (CheckDrive(drive, &stbuf)) {
197 /* Drive exists and is a CD-ROM */
199 AddDrive(drive, &stbuf);
201 /* Drive exists, but isn't a CD-ROM */
204 /* Drive doesn't exist */
211 SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s", checklist[i]);
212 if ( CheckDrive(drive, &stbuf) > 0 ) {
213 AddDrive(drive, &stbuf);
220 /* General ioctl() CD-ROM command function */
221 static int SDL_SYS_CDioctl(int id, int command, void *arg)
225 retval = ioctl(id, command, arg);
227 SDL_SetError("ioctl() error: %s", strerror(errno));
232 static const char *SDL_SYS_CDName(int drive)
234 return(SDL_cdlist[drive]);
237 static int SDL_SYS_CDOpen(int drive)
239 return(open(SDL_cdlist[drive], (O_RDONLY|O_EXCL|O_NONBLOCK), 0));
242 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
244 struct ioc_toc_header toc;
246 struct ioc_read_toc_entry entry;
247 struct cd_toc_entry data;
250 if ( SDL_SYS_CDioctl(cdrom->id, CDIOREADTOCHEADER, &toc) == 0 ) {
251 cdrom->numtracks = toc.ending_track-toc.starting_track+1;
252 if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
253 cdrom->numtracks = SDL_MAX_TRACKS;
255 /* Read all the track TOC entries */
256 for ( i=0; i<=cdrom->numtracks; ++i ) {
257 if ( i == cdrom->numtracks ) {
258 cdrom->track[i].id = 0xAA; /* CDROM_LEADOUT */
260 cdrom->track[i].id = toc.starting_track+i;
262 entry.starting_track = cdrom->track[i].id;
263 entry.address_format = CD_MSF_FORMAT;
264 entry.data_len = sizeof(data);
266 if ( SDL_SYS_CDioctl(cdrom->id, CDIOREADTOCENTRYS,
270 cdrom->track[i].type = data.control;
271 cdrom->track[i].offset = MSF_TO_FRAMES(
272 data.addr.msf.minute,
273 data.addr.msf.second,
274 data.addr.msf.frame);
275 cdrom->track[i].length = 0;
277 cdrom->track[i-1].length =
278 cdrom->track[i].offset-
279 cdrom->track[i-1].offset;
283 if ( i == (cdrom->numtracks+1) ) {
287 return(okay ? 0 : -1);
290 /* Get CD-ROM status */
291 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
294 struct ioc_toc_header toc;
295 struct ioc_read_subchannel info;
296 struct cd_sub_channel_info data;
298 info.address_format = CD_MSF_FORMAT;
299 info.data_format = CD_CURRENT_POSITION;
301 info.data_len = sizeof(data);
303 if ( ioctl(cdrom->id, CDIOCREADSUBCHANNEL, &info) < 0 ) {
304 if ( ERRNO_TRAYEMPTY(errno) ) {
305 status = CD_TRAYEMPTY;
310 switch (data.header.audio_status) {
311 case CD_AS_AUDIO_INVALID:
312 case CD_AS_NO_STATUS:
313 /* Try to determine if there's a CD available */
314 if (ioctl(cdrom->id,CDIOREADTOCHEADER,&toc)==0)
317 status = CD_TRAYEMPTY;
319 case CD_AS_PLAY_COMPLETED:
322 case CD_AS_PLAY_IN_PROGRESS:
325 case CD_AS_PLAY_PAUSED:
334 if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
335 *position = MSF_TO_FRAMES(
336 data.what.position.absaddr.msf.minute,
337 data.what.position.absaddr.msf.second,
338 data.what.position.absaddr.msf.frame);
347 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
349 struct ioc_play_msf playtime;
352 &playtime.start_m, &playtime.start_s, &playtime.start_f);
353 FRAMES_TO_MSF(start+length,
354 &playtime.end_m, &playtime.end_s, &playtime.end_f);
356 fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
357 playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
358 playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
360 ioctl(cdrom->id, CDIOCSTART, 0);
361 return(SDL_SYS_CDioctl(cdrom->id, CDIOCPLAYMSF, &playtime));
365 static int SDL_SYS_CDPause(SDL_CD *cdrom)
367 return(SDL_SYS_CDioctl(cdrom->id, CDIOCPAUSE, 0));
371 static int SDL_SYS_CDResume(SDL_CD *cdrom)
373 return(SDL_SYS_CDioctl(cdrom->id, CDIOCRESUME, 0));
377 static int SDL_SYS_CDStop(SDL_CD *cdrom)
379 return(SDL_SYS_CDioctl(cdrom->id, CDIOCSTOP, 0));
382 /* Eject the CD-ROM */
383 static int SDL_SYS_CDEject(SDL_CD *cdrom)
385 return(SDL_SYS_CDioctl(cdrom->id, CDIOCEJECT, 0));
388 /* Close the CD-ROM handle */
389 static void SDL_SYS_CDClose(SDL_CD *cdrom)
394 void SDL_SYS_CDQuit(void)
398 if ( SDL_numcds > 0 ) {
399 for ( i=0; i<SDL_numcds; ++i ) {
400 SDL_free(SDL_cdlist[i]);
406 #endif /* SDL_CDROM_FREEBSD */