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