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"
26 /* Functions for system-level CD-ROM audio control on BeOS
27 (not completely implemented yet)
30 #include <sys/types.h>
35 #include <Directory.h>
39 #include "SDL_cdrom.h"
41 #include "../SDL_syscdrom.h"
44 /* Constants to help us get at the SCSI table-of-contents info */
45 #define CD_NUMTRACKS(toc) toc.toc_data[3]
46 #define CD_TRACK(toc, track) (&toc.toc_data[6+(track)*8])
47 #define CD_TRACK_N(toc, track) CD_TRACK(toc, track)[0]
48 #define CD_TRACK_M(toc, track) CD_TRACK(toc, track)[3]
49 #define CD_TRACK_S(toc, track) CD_TRACK(toc, track)[4]
50 #define CD_TRACK_F(toc, track) CD_TRACK(toc, track)[5]
52 /* Constants to help us get at the SCSI position info */
53 #define POS_TRACK(pos) pos.position[6]
54 #define POS_ABS_M(pos) pos.position[9]
55 #define POS_ABS_S(pos) pos.position[10]
56 #define POS_ABS_F(pos) pos.position[11]
57 #define POS_REL_M(pos) pos.position[13]
58 #define POS_REL_S(pos) pos.position[14]
59 #define POS_REL_F(pos) pos.position[15]
61 /* The maximum number of CD-ROM drives we'll detect */
64 /* A list of available CD-ROM drives */
65 static char *SDL_cdlist[MAX_DRIVES];
67 /* The system-dependent CD control functions */
68 static const char *SDL_SYS_CDName(int drive);
69 static int SDL_SYS_CDOpen(int drive);
70 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
71 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
72 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
73 static int SDL_SYS_CDPause(SDL_CD *cdrom);
74 static int SDL_SYS_CDResume(SDL_CD *cdrom);
75 static int SDL_SYS_CDStop(SDL_CD *cdrom);
76 static int SDL_SYS_CDEject(SDL_CD *cdrom);
77 static void SDL_SYS_CDClose(SDL_CD *cdrom);
78 int try_dir(const char *directory);
81 /* Check a drive to see if it is a CD-ROM */
82 static int CheckDrive(char *drive)
88 /* If it doesn't exist, return -1 */
89 if ( stat(drive, &stbuf) < 0 ) {
93 /* If it does exist, verify that it's an available CD-ROM */
95 cdfd = open(drive, 0);
97 if ( ioctl(cdfd, B_GET_GEOMETRY, &info) == B_NO_ERROR ) {
98 if ( info.device_type == B_CD ) {
104 /* This can happen when the drive is open .. (?) */;
110 /* Add a CD-ROM drive to our list of valid drives */
111 static void AddDrive(char *drive)
116 if ( SDL_numcds < MAX_DRIVES ) {
117 /* Add this drive to our list */
119 len = SDL_strlen(drive)+1;
120 SDL_cdlist[i] = (char *)SDL_malloc(len);
121 if ( SDL_cdlist[i] == NULL ) {
125 SDL_strlcpy(SDL_cdlist[i], drive, len);
128 fprintf(stderr, "Added CD-ROM drive: %s\n", drive);
133 /* IDE bus scanning magic */
135 IDE_GET_DEVICES_INFO = B_DEVICE_OP_CODES_END + 50,
137 struct ide_ctrl_info {
139 bool ide_0_master_present;
140 bool ide_0_slave_present;
141 int ide_0_master_type;
142 int ide_0_slave_type;
144 bool ide_1_master_present;
145 bool ide_1_slave_present;
146 int ide_1_master_type;
147 int ide_1_slave_type;
150 int SDL_SYS_CDInit(void)
154 struct ide_ctrl_info info;
156 /* Fill in our driver capabilities */
157 SDL_CDcaps.Name = SDL_SYS_CDName;
158 SDL_CDcaps.Open = SDL_SYS_CDOpen;
159 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
160 SDL_CDcaps.Status = SDL_SYS_CDStatus;
161 SDL_CDcaps.Play = SDL_SYS_CDPlay;
162 SDL_CDcaps.Pause = SDL_SYS_CDPause;
163 SDL_CDcaps.Resume = SDL_SYS_CDResume;
164 SDL_CDcaps.Stop = SDL_SYS_CDStop;
165 SDL_CDcaps.Eject = SDL_SYS_CDEject;
166 SDL_CDcaps.Close = SDL_SYS_CDClose;
168 /* Look in the environment for our CD-ROM drive list */
169 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */
170 if ( SDLcdrom != NULL ) {
171 char *cdpath, *delim;
172 size_t len = SDL_strlen(SDLcdrom)+1;
173 cdpath = SDL_stack_alloc(char, len);
174 if ( cdpath != NULL ) {
175 SDL_strlcpy(cdpath, SDLcdrom, len);
178 delim = SDL_strchr(SDLcdrom, ':');
182 if ( CheckDrive(SDLcdrom) > 0 ) {
190 } while ( SDLcdrom );
191 SDL_stack_free(cdpath);
194 /* If we found our drives, there's nothing left to do */
195 if ( SDL_numcds > 0 ) {
200 /* Scan the system for CD-ROM drives */
201 try_dir("/dev/disk");
206 int try_dir(const char *directory)
209 dir.SetTo(directory);
210 if(dir.InitCheck() != B_NO_ERROR) {
215 while(dir.GetNextEntry(&entry) >= 0) {
220 if(entry.GetPath(&path) != B_NO_ERROR)
224 if(entry.GetRef(&e) != B_NO_ERROR)
227 if(entry.IsDirectory()) {
228 if(SDL_strcmp(e.name, "floppy") == 0)
229 continue; /* ignore floppy (it is not silent) */
230 int devfd = try_dir(name);
238 if(SDL_strcmp(e.name, "raw") != 0)
239 continue; /* ignore partitions */
241 devfd = open(name, O_RDONLY);
245 if(ioctl(devfd, B_GET_GEOMETRY, &g, sizeof(g)) >= 0) {
246 if(g.device_type == B_CD)
248 AddDrive(strdup(name));
258 /* General ioctl() CD-ROM command function */
259 static int SDL_SYS_CDioctl(int index, int command, void *arg)
265 fd = open(SDL_cdlist[index], 0);
267 if ( ioctl(fd, command, arg) == B_NO_ERROR ) {
272 return(okay ? 0 : -1);
275 static const char *SDL_SYS_CDName(int drive)
277 return(SDL_cdlist[drive]);
280 static int SDL_SYS_CDOpen(int drive)
285 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
290 if ( SDL_SYS_CDioctl(cdrom->id, B_SCSI_GET_TOC, &toc) == 0 ) {
291 cdrom->numtracks = CD_NUMTRACKS(toc);
292 if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
293 cdrom->numtracks = SDL_MAX_TRACKS;
295 for ( i=0; i<=cdrom->numtracks; ++i ) {
296 cdrom->track[i].id = CD_TRACK_N(toc, i);
297 /* FIXME: How do we tell on BeOS? */
298 cdrom->track[i].type = SDL_AUDIO_TRACK;
299 cdrom->track[i].offset = MSF_TO_FRAMES(
303 cdrom->track[i].length = 0;
305 cdrom->track[i-1].length =
306 cdrom->track[i].offset-
307 cdrom->track[i-1].offset;
316 /* Get CD-ROM status */
317 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
324 fd = open(SDL_cdlist[cdrom->id], 0);
327 if ( ioctl(fd, B_SCSI_GET_POSITION, &pos) == B_NO_ERROR ) {
328 cur_frame = MSF_TO_FRAMES(
329 POS_ABS_M(pos), POS_ABS_S(pos), POS_ABS_F(pos));
331 if ( ! pos.position[1] || (pos.position[1] >= 0x13) ||
332 ((pos.position[1] == 0x12) && (!pos.position[6])) ) {
335 if ( pos.position[1] == 0x11 ) {
342 status = CD_TRAYEMPTY;
345 *position = cur_frame;
351 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
355 scsi_play_position pos;
358 fd = open(SDL_cdlist[cdrom->id], 0);
360 FRAMES_TO_MSF(start, &pos.start_m, &pos.start_s, &pos.start_f);
361 FRAMES_TO_MSF(start+length, &pos.end_m, &pos.end_s, &pos.end_f);
362 if ( ioctl(fd, B_SCSI_PLAY_POSITION, &pos) == B_NO_ERROR ) {
367 return(okay ? 0 : -1);
371 static int SDL_SYS_CDPause(SDL_CD *cdrom)
373 return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_PAUSE_AUDIO, 0));
377 static int SDL_SYS_CDResume(SDL_CD *cdrom)
379 return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_RESUME_AUDIO, 0));
383 static int SDL_SYS_CDStop(SDL_CD *cdrom)
385 return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_STOP_AUDIO, 0));
388 /* Eject the CD-ROM */
389 static int SDL_SYS_CDEject(SDL_CD *cdrom)
391 return(SDL_SYS_CDioctl(cdrom->id, B_SCSI_EJECT, 0));
394 /* Close the CD-ROM handle */
395 static void SDL_SYS_CDClose(SDL_CD *cdrom)
400 void SDL_SYS_CDQuit(void)
404 if ( SDL_numcds > 0 ) {
405 for ( i=0; i<SDL_numcds; ++i ) {
406 SDL_free(SDL_cdlist[i]);
412 #endif /* SDL_CDROM_BEOS */