Mercurial > sdl-ios-xcode
annotate src/cdrom/freebsd/SDL_syscdrom.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | c9b51268668f |
children | 604d73db6802 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
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 | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Functions for system-level CD-ROM audio control */ | |
24 | |
25 #include <sys/types.h> | |
26 #include <stdlib.h> | |
27 #include <sys/stat.h> | |
28 #include <fcntl.h> | |
29 #include <stdio.h> | |
30 #include <string.h> | |
31 #include <errno.h> | |
32 #include <unistd.h> | |
33 #include <sys/cdio.h> | |
34 | |
35 #include "SDL_error.h" | |
36 #include "SDL_cdrom.h" | |
37 #include "SDL_syscdrom.h" | |
38 | |
39 | |
40 /* The maximum number of CD-ROM drives we'll detect */ | |
41 #define MAX_DRIVES 16 | |
42 | |
43 /* A list of available CD-ROM drives */ | |
44 static char *SDL_cdlist[MAX_DRIVES]; | |
45 static dev_t SDL_cdmode[MAX_DRIVES]; | |
46 | |
47 /* The system-dependent CD control functions */ | |
48 static const char *SDL_SYS_CDName(int drive); | |
49 static int SDL_SYS_CDOpen(int drive); | |
50 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom); | |
51 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position); | |
52 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length); | |
53 static int SDL_SYS_CDPause(SDL_CD *cdrom); | |
54 static int SDL_SYS_CDResume(SDL_CD *cdrom); | |
55 static int SDL_SYS_CDStop(SDL_CD *cdrom); | |
56 static int SDL_SYS_CDEject(SDL_CD *cdrom); | |
57 static void SDL_SYS_CDClose(SDL_CD *cdrom); | |
58 | |
59 /* Some ioctl() errno values which occur when the tray is empty */ | |
60 #define ERRNO_TRAYEMPTY(errno) \ | |
61 ((errno == EIO) || (errno == ENOENT) || (errno == EINVAL)) | |
62 | |
63 /* Check a drive to see if it is a CD-ROM */ | |
64 static int CheckDrive(char *drive, struct stat *stbuf) | |
65 { | |
66 int is_cd, cdfd; | |
67 struct ioc_read_subchannel info; | |
68 | |
69 /* If it doesn't exist, return -1 */ | |
70 if ( stat(drive, stbuf) < 0 ) { | |
71 return(-1); | |
72 } | |
73 | |
74 /* If it does exist, verify that it's an available CD-ROM */ | |
75 is_cd = 0; | |
76 if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) { | |
77 cdfd = open(drive, (O_RDONLY|O_EXCL|O_NONBLOCK), 0); | |
78 if ( cdfd >= 0 ) { | |
79 info.address_format = CD_MSF_FORMAT; | |
80 info.data_format = CD_CURRENT_POSITION; | |
81 info.data_len = 0; | |
82 info.data = NULL; | |
83 /* Under Linux, EIO occurs when a disk is not present. | |
84 This isn't 100% reliable, so we use the USE_MNTENT | |
85 code above instead. | |
86 */ | |
87 if ( (ioctl(cdfd, CDIOCREADSUBCHANNEL, &info) == 0) || | |
88 ERRNO_TRAYEMPTY(errno) ) { | |
89 is_cd = 1; | |
90 } | |
91 close(cdfd); | |
92 } | |
93 } | |
94 return(is_cd); | |
95 } | |
96 | |
97 /* Add a CD-ROM drive to our list of valid drives */ | |
98 static void AddDrive(char *drive, struct stat *stbuf) | |
99 { | |
100 int i; | |
101 | |
102 if ( SDL_numcds < MAX_DRIVES ) { | |
103 /* Check to make sure it's not already in our list. | |
104 This can happen when we see a drive via symbolic link. | |
105 */ | |
106 for ( i=0; i<SDL_numcds; ++i ) { | |
107 if ( stbuf->st_rdev == SDL_cdmode[i] ) { | |
108 #ifdef DEBUG_CDROM | |
109 fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]); | |
110 #endif | |
111 return; | |
112 } | |
113 } | |
114 | |
115 /* Add this drive to our list */ | |
116 i = SDL_numcds; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
117 SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1); |
0 | 118 if ( SDL_cdlist[i] == NULL ) { |
119 SDL_OutOfMemory(); | |
120 return; | |
121 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
122 SDL_strcpy(SDL_cdlist[i], drive); |
0 | 123 SDL_cdmode[i] = stbuf->st_rdev; |
124 ++SDL_numcds; | |
125 #ifdef DEBUG_CDROM | |
126 fprintf(stderr, "Added CD-ROM drive: %s\n", drive); | |
127 #endif | |
128 } | |
129 } | |
130 | |
131 int SDL_SYS_CDInit(void) | |
132 { | |
133 /* checklist: /dev/cdrom,/dev/cd?c /dev/acd?c | |
134 /dev/matcd?c /dev/mcd?c /dev/scd?c */ | |
135 static char *checklist[] = { | |
136 "cdrom", "?0 cd?", "?0 acd?", "?0 matcd?", "?0 mcd?", "?0 scd?",NULL | |
137 }; | |
138 char *SDLcdrom; | |
139 int i, j, exists; | |
140 char drive[32]; | |
141 struct stat stbuf; | |
142 | |
143 /* Fill in our driver capabilities */ | |
144 SDL_CDcaps.Name = SDL_SYS_CDName; | |
145 SDL_CDcaps.Open = SDL_SYS_CDOpen; | |
146 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC; | |
147 SDL_CDcaps.Status = SDL_SYS_CDStatus; | |
148 SDL_CDcaps.Play = SDL_SYS_CDPlay; | |
149 SDL_CDcaps.Pause = SDL_SYS_CDPause; | |
150 SDL_CDcaps.Resume = SDL_SYS_CDResume; | |
151 SDL_CDcaps.Stop = SDL_SYS_CDStop; | |
152 SDL_CDcaps.Eject = SDL_SYS_CDEject; | |
153 SDL_CDcaps.Close = SDL_SYS_CDClose; | |
154 | |
155 /* Look in the environment for our CD-ROM drive list */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
156 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */ |
0 | 157 if ( SDLcdrom != NULL ) { |
158 char *cdpath, *delim; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
159 cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1); |
0 | 160 if ( cdpath != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
161 SDL_strcpy(cdpath, SDLcdrom); |
0 | 162 SDLcdrom = cdpath; |
163 do { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
164 delim = SDL_strchr(SDLcdrom, ':'); |
0 | 165 if ( delim ) { |
166 *delim++ = '\0'; | |
167 } | |
168 if ( CheckDrive(SDLcdrom, &stbuf) > 0 ) { | |
169 AddDrive(SDLcdrom, &stbuf); | |
170 } | |
171 if ( delim ) { | |
172 SDLcdrom = delim; | |
173 } else { | |
174 SDLcdrom = NULL; | |
175 } | |
176 } while ( SDLcdrom ); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
177 SDL_free(cdpath); |
0 | 178 } |
179 | |
180 /* If we found our drives, there's nothing left to do */ | |
181 if ( SDL_numcds > 0 ) { | |
182 return(0); | |
183 } | |
184 } | |
185 | |
186 /* Scan the system for CD-ROM drives */ | |
187 for ( i=0; checklist[i]; ++i ) { | |
188 if ( checklist[i][0] == '?' ) { | |
189 char *insert; | |
190 exists = 1; | |
191 for ( j=checklist[i][1]; exists; ++j ) { | |
192 sprintf(drive, "/dev/%sc", &checklist[i][3]); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
193 insert = SDL_strchr(drive, '?'); |
0 | 194 if ( insert != NULL ) { |
195 *insert = j; | |
196 } | |
197 switch (CheckDrive(drive, &stbuf)) { | |
198 /* Drive exists and is a CD-ROM */ | |
199 case 1: | |
200 AddDrive(drive, &stbuf); | |
201 break; | |
202 /* Drive exists, but isn't a CD-ROM */ | |
203 case 0: | |
204 break; | |
205 /* Drive doesn't exist */ | |
206 case -1: | |
207 exists = 0; | |
208 break; | |
209 } | |
210 } | |
211 } else { | |
212 sprintf(drive, "/dev/%s", checklist[i]); | |
213 if ( CheckDrive(drive, &stbuf) > 0 ) { | |
214 AddDrive(drive, &stbuf); | |
215 } | |
216 } | |
217 } | |
218 return(0); | |
219 } | |
220 | |
221 /* General ioctl() CD-ROM command function */ | |
222 static int SDL_SYS_CDioctl(int id, int command, void *arg) | |
223 { | |
224 int retval; | |
225 | |
226 retval = ioctl(id, command, arg); | |
227 if ( retval < 0 ) { | |
228 SDL_SetError("ioctl() error: %s", strerror(errno)); | |
229 } | |
230 return(retval); | |
231 } | |
232 | |
233 static const char *SDL_SYS_CDName(int drive) | |
234 { | |
235 return(SDL_cdlist[drive]); | |
236 } | |
237 | |
238 static int SDL_SYS_CDOpen(int drive) | |
239 { | |
240 return(open(SDL_cdlist[drive], (O_RDONLY|O_EXCL|O_NONBLOCK), 0)); | |
241 } | |
242 | |
243 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom) | |
244 { | |
245 struct ioc_toc_header toc; | |
246 int i, okay; | |
247 struct ioc_read_toc_entry entry; | |
248 struct cd_toc_entry data; | |
249 | |
250 okay = 0; | |
251 if ( SDL_SYS_CDioctl(cdrom->id, CDIOREADTOCHEADER, &toc) == 0 ) { | |
252 cdrom->numtracks = toc.ending_track-toc.starting_track+1; | |
253 if ( cdrom->numtracks > SDL_MAX_TRACKS ) { | |
254 cdrom->numtracks = SDL_MAX_TRACKS; | |
255 } | |
256 /* Read all the track TOC entries */ | |
257 for ( i=0; i<=cdrom->numtracks; ++i ) { | |
258 if ( i == cdrom->numtracks ) { | |
259 cdrom->track[i].id = 0xAA; /* CDROM_LEADOUT */ | |
260 } else { | |
261 cdrom->track[i].id = toc.starting_track+i; | |
262 } | |
263 entry.starting_track = cdrom->track[i].id; | |
264 entry.address_format = CD_MSF_FORMAT; | |
265 entry.data_len = sizeof(data); | |
266 entry.data = &data; | |
267 if ( SDL_SYS_CDioctl(cdrom->id, CDIOREADTOCENTRYS, | |
268 &entry) < 0 ) { | |
269 break; | |
270 } else { | |
271 cdrom->track[i].type = data.control; | |
272 cdrom->track[i].offset = MSF_TO_FRAMES( | |
273 data.addr.msf.minute, | |
274 data.addr.msf.second, | |
275 data.addr.msf.frame); | |
276 cdrom->track[i].length = 0; | |
277 if ( i > 0 ) { | |
278 cdrom->track[i-1].length = | |
279 cdrom->track[i].offset- | |
280 cdrom->track[i-1].offset; | |
281 } | |
282 } | |
283 } | |
284 if ( i == (cdrom->numtracks+1) ) { | |
285 okay = 1; | |
286 } | |
287 } | |
288 return(okay ? 0 : -1); | |
289 } | |
290 | |
291 /* Get CD-ROM status */ | |
292 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position) | |
293 { | |
294 CDstatus status; | |
295 struct ioc_toc_header toc; | |
296 struct ioc_read_subchannel info; | |
297 struct cd_sub_channel_info data; | |
298 | |
299 info.address_format = CD_MSF_FORMAT; | |
300 info.data_format = CD_CURRENT_POSITION; | |
301 info.track = 0; | |
302 info.data_len = sizeof(data); | |
303 info.data = &data; | |
304 if ( ioctl(cdrom->id, CDIOCREADSUBCHANNEL, &info) < 0 ) { | |
305 if ( ERRNO_TRAYEMPTY(errno) ) { | |
306 status = CD_TRAYEMPTY; | |
307 } else { | |
308 status = CD_ERROR; | |
309 } | |
310 } else { | |
311 switch (data.header.audio_status) { | |
312 case CD_AS_AUDIO_INVALID: | |
313 case CD_AS_NO_STATUS: | |
314 /* Try to determine if there's a CD available */ | |
315 if (ioctl(cdrom->id,CDIOREADTOCHEADER,&toc)==0) | |
316 status = CD_STOPPED; | |
317 else | |
318 status = CD_TRAYEMPTY; | |
319 break; | |
320 case CD_AS_PLAY_COMPLETED: | |
321 status = CD_STOPPED; | |
322 break; | |
323 case CD_AS_PLAY_IN_PROGRESS: | |
324 status = CD_PLAYING; | |
325 break; | |
326 case CD_AS_PLAY_PAUSED: | |
327 status = CD_PAUSED; | |
328 break; | |
329 default: | |
330 status = CD_ERROR; | |
331 break; | |
332 } | |
333 } | |
334 if ( position ) { | |
335 if ( status == CD_PLAYING || (status == CD_PAUSED) ) { | |
336 *position = MSF_TO_FRAMES( | |
337 data.what.position.absaddr.msf.minute, | |
338 data.what.position.absaddr.msf.second, | |
339 data.what.position.absaddr.msf.frame); | |
340 } else { | |
341 *position = 0; | |
342 } | |
343 } | |
344 return(status); | |
345 } | |
346 | |
347 /* Start play */ | |
348 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length) | |
349 { | |
350 struct ioc_play_msf playtime; | |
351 | |
352 FRAMES_TO_MSF(start, | |
353 &playtime.start_m, &playtime.start_s, &playtime.start_f); | |
354 FRAMES_TO_MSF(start+length, | |
355 &playtime.end_m, &playtime.end_s, &playtime.end_f); | |
356 #ifdef DEBUG_CDROM | |
357 fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n", | |
358 playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0, | |
359 playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1); | |
360 #endif | |
361 ioctl(cdrom->id, CDIOCSTART, 0); | |
362 return(SDL_SYS_CDioctl(cdrom->id, CDIOCPLAYMSF, &playtime)); | |
363 } | |
364 | |
365 /* Pause play */ | |
366 static int SDL_SYS_CDPause(SDL_CD *cdrom) | |
367 { | |
368 return(SDL_SYS_CDioctl(cdrom->id, CDIOCPAUSE, 0)); | |
369 } | |
370 | |
371 /* Resume play */ | |
372 static int SDL_SYS_CDResume(SDL_CD *cdrom) | |
373 { | |
374 return(SDL_SYS_CDioctl(cdrom->id, CDIOCRESUME, 0)); | |
375 } | |
376 | |
377 /* Stop play */ | |
378 static int SDL_SYS_CDStop(SDL_CD *cdrom) | |
379 { | |
380 return(SDL_SYS_CDioctl(cdrom->id, CDIOCSTOP, 0)); | |
381 } | |
382 | |
383 /* Eject the CD-ROM */ | |
384 static int SDL_SYS_CDEject(SDL_CD *cdrom) | |
385 { | |
386 return(SDL_SYS_CDioctl(cdrom->id, CDIOCEJECT, 0)); | |
387 } | |
388 | |
389 /* Close the CD-ROM handle */ | |
390 static void SDL_SYS_CDClose(SDL_CD *cdrom) | |
391 { | |
392 close(cdrom->id); | |
393 } | |
394 | |
395 void SDL_SYS_CDQuit(void) | |
396 { | |
397 int i; | |
398 | |
399 if ( SDL_numcds > 0 ) { | |
400 for ( i=0; i<SDL_numcds; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
401 SDL_free(SDL_cdlist[i]); |
0 | 402 } |
403 SDL_numcds = 0; | |
404 } | |
405 } | |
406 |