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