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 Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with this library; if not, write to the Free
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "SDL_config.h"
27 Atari MetaDOS CD-ROM functions
37 #include "SDL_cdrom.h"
38 #include "../SDL_syscdrom.h"
40 /* Some ioctl() errno values which occur when the tray is empty */
42 #define ENOMEDIUM ENOENT
44 #define ERRNO_TRAYEMPTY(errno) \
45 ((errno == EIO) || (errno == ENOENT) || \
46 (errno == EINVAL) || (errno == ENOMEDIUM))
48 /* The maximum number of CD-ROM drives we'll detect */
52 unsigned char device[3]; /* Physical device letter + ':' + '\0' */
53 metaopen_t metaopen; /* Infos on opened drive */
56 static metados_drive_t metados_drives[MAX_DRIVES];
58 /* The system-dependent CD control functions */
59 static const char *SDL_SYS_CDName(int drive);
60 static int SDL_SYS_CDOpen(int drive);
61 static void SDL_SYS_CDClose(SDL_CD *cdrom);
62 static int SDL_SYS_CDioctl(int id, int command, void *arg);
63 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom);
64 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position);
65 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length);
66 static int SDL_SYS_CDPause(SDL_CD *cdrom);
67 static int SDL_SYS_CDResume(SDL_CD *cdrom);
68 static int SDL_SYS_CDStop(SDL_CD *cdrom);
69 static int SDL_SYS_CDEject(SDL_CD *cdrom);
71 int SDL_SYS_CDInit(void)
73 metainit_t metainit={0,0,0,0};
76 struct cdrom_subchnl info;
79 if (metainit.version == NULL) {
81 fprintf(stderr, "MetaDOS not installed\n");
86 if (metainit.drives_map == 0) {
88 fprintf(stderr, "No MetaDOS devices present\n");
95 for (i='A'; i<='Z'; i++) {
96 metados_drives[SDL_numcds].device[0] = 0;
97 metados_drives[SDL_numcds].device[1] = ':';
98 metados_drives[SDL_numcds].device[2] = 0;
100 if (metainit.drives_map & (1<<(i-'A'))) {
101 handle = Metaopen(i, &metaopen);
104 info.cdsc_format = CDROM_MSF;
105 if ( (Metaioctl(i, METADOS_IOCTL_MAGIC, CDROMSUBCHNL, &info) == 0) || ERRNO_TRAYEMPTY(errno) ) {
106 metados_drives[SDL_numcds].device[0] = i;
115 /* Fill in our driver capabilities */
116 SDL_CDcaps.Name = SDL_SYS_CDName;
117 SDL_CDcaps.Open = SDL_SYS_CDOpen;
118 SDL_CDcaps.Close = SDL_SYS_CDClose;
120 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC;
121 SDL_CDcaps.Status = SDL_SYS_CDStatus;
122 SDL_CDcaps.Play = SDL_SYS_CDPlay;
123 SDL_CDcaps.Pause = SDL_SYS_CDPause;
124 SDL_CDcaps.Resume = SDL_SYS_CDResume;
125 SDL_CDcaps.Stop = SDL_SYS_CDStop;
126 SDL_CDcaps.Eject = SDL_SYS_CDEject;
131 void SDL_SYS_CDQuit(void)
136 static const char *SDL_SYS_CDName(int drive)
138 return(metados_drives[drive].device);
141 static int SDL_SYS_CDOpen(int drive)
145 handle = Metaopen(metados_drives[drive].device[0], &(metados_drives[drive].metaopen));
153 static void SDL_SYS_CDClose(SDL_CD *cdrom)
155 Metaclose(metados_drives[cdrom->id].device[0]);
158 static int SDL_SYS_CDioctl(int id, int command, void *arg)
162 retval = Metaioctl(metados_drives[id].device[0], METADOS_IOCTL_MAGIC, command, arg);
164 SDL_SetError("ioctl() error: %s", strerror(errno));
169 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom)
172 struct cdrom_tochdr toc;
173 struct cdrom_tocentry entry;
175 /* Use standard ioctl() */
176 if (SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc)<0) {
180 cdrom->numtracks = toc.cdth_trk1-toc.cdth_trk0+1;
181 if ( cdrom->numtracks > SDL_MAX_TRACKS ) {
182 cdrom->numtracks = SDL_MAX_TRACKS;
185 /* Read all the track TOC entries */
187 for ( i=0; i<=cdrom->numtracks; ++i ) {
188 if ( i == cdrom->numtracks ) {
189 cdrom->track[i].id = CDROM_LEADOUT;
191 cdrom->track[i].id = toc.cdth_trk0+i;
193 entry.cdte_track = cdrom->track[i].id;
194 entry.cdte_format = CDROM_MSF;
195 if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCENTRY, &entry) < 0 ) {
199 if ( entry.cdte_ctrl & CDROM_DATA_TRACK ) {
200 cdrom->track[i].type = SDL_DATA_TRACK;
202 cdrom->track[i].type = SDL_AUDIO_TRACK;
204 cdrom->track[i].offset = MSF_TO_FRAMES(
205 entry.cdte_addr.msf.minute,
206 entry.cdte_addr.msf.second,
207 entry.cdte_addr.msf.frame);
208 cdrom->track[i].length = 0;
210 cdrom->track[i-1].length = cdrom->track[i].offset-cdrom->track[i-1].offset;
215 return(okay ? 0 : -1);
218 /* Get CD-ROM status */
219 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position)
222 struct cdrom_tochdr toc;
223 struct cdrom_subchnl info;
225 info.cdsc_format = CDROM_MSF;
226 if ( SDL_SYS_CDioctl(cdrom->id, CDROMSUBCHNL, &info) < 0 ) {
227 if ( ERRNO_TRAYEMPTY(errno) ) {
228 status = CD_TRAYEMPTY;
233 switch (info.cdsc_audiostatus) {
234 case CDROM_AUDIO_INVALID:
235 case CDROM_AUDIO_NO_STATUS:
236 /* Try to determine if there's a CD available */
237 if (SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc)==0) {
240 status = CD_TRAYEMPTY;
243 case CDROM_AUDIO_COMPLETED:
246 case CDROM_AUDIO_PLAY:
249 case CDROM_AUDIO_PAUSED:
250 /* Workaround buggy CD-ROM drive */
251 if ( info.cdsc_trk == CDROM_LEADOUT ) {
263 if ( status == CD_PLAYING || (status == CD_PAUSED) ) {
264 *position = MSF_TO_FRAMES(
265 info.cdsc_absaddr.msf.minute,
266 info.cdsc_absaddr.msf.second,
267 info.cdsc_absaddr.msf.frame);
276 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length)
278 struct cdrom_msf playtime;
281 &playtime.cdmsf_min0, &playtime.cdmsf_sec0, &playtime.cdmsf_frame0);
282 FRAMES_TO_MSF(start+length,
283 &playtime.cdmsf_min1, &playtime.cdmsf_sec1, &playtime.cdmsf_frame1);
285 fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n",
286 playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0,
287 playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1);
290 return SDL_SYS_CDioctl(cdrom->id, CDROMPLAYMSF, &playtime);
294 static int SDL_SYS_CDPause(SDL_CD *cdrom)
296 return SDL_SYS_CDioctl(cdrom->id, CDROMPAUSE, 0);
300 static int SDL_SYS_CDResume(SDL_CD *cdrom)
302 return SDL_SYS_CDioctl(cdrom->id, CDROMRESUME, 0);
306 static int SDL_SYS_CDStop(SDL_CD *cdrom)
308 return SDL_SYS_CDioctl(cdrom->id, CDROMSTOP, 0);
311 /* Eject the CD-ROM */
312 static int SDL_SYS_CDEject(SDL_CD *cdrom)
314 return SDL_SYS_CDioctl(cdrom->id, CDROMEJECT, 0);
317 #endif /* SDL_CDROM_MINT */