Mercurial > sdl-ios-xcode
annotate src/cdrom/linux/SDL_syscdrom.c @ 1367:e440d5c488c1
Fixes for BeOS and Solaris builds
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 17 Feb 2006 08:43:23 +0000 |
parents | 19418e4422cb |
children | c0a74f199ecf |
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:
1062
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:
1062
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:
1062
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:
1062
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:
1062
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:
1062
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:
1062
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:
247
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* Functions for system-level CD-ROM audio control */ | |
24 | |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
25 #include <string.h> /* For strerror() */ |
0 | 26 #include <sys/types.h> |
27 #include <sys/stat.h> | |
28 #include <sys/ioctl.h> | |
29 #include <fcntl.h> | |
30 #include <errno.h> | |
31 #include <unistd.h> | |
32 #ifdef __linux__ | |
1062
3e637850c02b
Date: Tue, 22 Mar 2005 23:50:20 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1051
diff
changeset
|
33 #ifdef HAVE_LINUX_VERSION_H |
998
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
34 /* linux 2.6.9 workaround */ |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
35 #include <linux/version.h> |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
36 #if LINUX_VERSION_CODE == KERNEL_VERSION(2,6,9) |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
37 #include <asm/types.h> |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
38 #define __le64 __u64 |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
39 #define __le32 __u32 |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
40 #define __le16 __u16 |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
41 #define __be64 __u64 |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
42 #define __be32 __u32 |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
43 #define __be16 __u16 |
0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
44 #endif /* linux 2.6.9 workaround */ |
1062
3e637850c02b
Date: Tue, 22 Mar 2005 23:50:20 +0100
Sam Lantinga <slouken@libsdl.org>
parents:
1051
diff
changeset
|
45 #endif /* HAVE_LINUX_VERSION_H */ |
0 | 46 #include <linux/cdrom.h> |
47 #endif | |
48 #ifdef __SVR4 | |
49 #include <sys/cdio.h> | |
50 #endif | |
51 | |
52 /* Define this to use the alternative getmntent() code */ | |
53 #ifndef __SVR4 | |
54 #define USE_MNTENT | |
55 #endif | |
56 | |
57 #ifdef USE_MNTENT | |
58 #if defined(__USLC__) | |
59 #include <sys/mntent.h> | |
60 #else | |
61 #include <mntent.h> | |
62 #endif | |
63 | |
64 #ifndef _PATH_MNTTAB | |
65 #ifdef MNTTAB | |
66 #define _PATH_MNTTAB MNTTAB | |
67 #else | |
68 #define _PATH_MNTTAB "/etc/fstab" | |
69 #endif | |
70 #endif /* !_PATH_MNTTAB */ | |
71 | |
72 #ifndef _PATH_MOUNTED | |
73 #define _PATH_MOUNTED "/etc/mtab" | |
74 #endif /* !_PATH_MOUNTED */ | |
75 | |
76 #ifndef MNTTYPE_CDROM | |
77 #define MNTTYPE_CDROM "iso9660" | |
78 #endif | |
79 #ifndef MNTTYPE_SUPER | |
80 #define MNTTYPE_SUPER "supermount" | |
81 #endif | |
82 #endif /* USE_MNTENT */ | |
83 | |
84 #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
|
85 #include "../SDL_syscdrom.h" |
0 | 86 |
87 | |
88 /* The maximum number of CD-ROM drives we'll detect */ | |
89 #define MAX_DRIVES 16 | |
90 | |
91 /* A list of available CD-ROM drives */ | |
92 static char *SDL_cdlist[MAX_DRIVES]; | |
93 static dev_t SDL_cdmode[MAX_DRIVES]; | |
94 | |
95 /* The system-dependent CD control functions */ | |
96 static const char *SDL_SYS_CDName(int drive); | |
97 static int SDL_SYS_CDOpen(int drive); | |
98 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom); | |
99 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position); | |
100 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length); | |
101 static int SDL_SYS_CDPause(SDL_CD *cdrom); | |
102 static int SDL_SYS_CDResume(SDL_CD *cdrom); | |
103 static int SDL_SYS_CDStop(SDL_CD *cdrom); | |
104 static int SDL_SYS_CDEject(SDL_CD *cdrom); | |
105 static void SDL_SYS_CDClose(SDL_CD *cdrom); | |
106 | |
107 /* Some ioctl() errno values which occur when the tray is empty */ | |
396
11d6eed68dba
Added check for ENOMEDIUM to the Linux CDROM code
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
108 #ifndef ENOMEDIUM |
11d6eed68dba
Added check for ENOMEDIUM to the Linux CDROM code
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
109 #define ENOMEDIUM ENOENT |
11d6eed68dba
Added check for ENOMEDIUM to the Linux CDROM code
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
110 #endif |
0 | 111 #define ERRNO_TRAYEMPTY(errno) \ |
396
11d6eed68dba
Added check for ENOMEDIUM to the Linux CDROM code
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
112 ((errno == EIO) || (errno == ENOENT) || \ |
11d6eed68dba
Added check for ENOMEDIUM to the Linux CDROM code
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
113 (errno == EINVAL) || (errno == ENOMEDIUM)) |
0 | 114 |
115 /* Check a drive to see if it is a CD-ROM */ | |
116 static int CheckDrive(char *drive, char *mnttype, struct stat *stbuf) | |
117 { | |
118 int is_cd, cdfd; | |
119 struct cdrom_subchnl info; | |
120 | |
121 /* If it doesn't exist, return -1 */ | |
122 if ( stat(drive, stbuf) < 0 ) { | |
123 return(-1); | |
124 } | |
125 | |
126 /* If it does exist, verify that it's an available CD-ROM */ | |
127 is_cd = 0; | |
128 if ( S_ISCHR(stbuf->st_mode) || S_ISBLK(stbuf->st_mode) ) { | |
1051
091350827e08
SDL_CDOpen() fails on Linux if data track on disc is mounted in the
Ryan C. Gordon <icculus@icculus.org>
parents:
998
diff
changeset
|
129 cdfd = open(drive, (O_RDONLY|O_NONBLOCK), 0); |
0 | 130 if ( cdfd >= 0 ) { |
131 info.cdsc_format = CDROM_MSF; | |
132 /* Under Linux, EIO occurs when a disk is not present. | |
133 */ | |
134 if ( (ioctl(cdfd, CDROMSUBCHNL, &info) == 0) || | |
135 ERRNO_TRAYEMPTY(errno) ) { | |
136 is_cd = 1; | |
137 } | |
138 close(cdfd); | |
139 } | |
140 #ifdef USE_MNTENT | |
141 /* Even if we can't read it, it might be mounted */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
142 else if ( mnttype && (SDL_strcmp(mnttype, MNTTYPE_CDROM) == 0) ) { |
0 | 143 is_cd = 1; |
144 } | |
145 #endif | |
146 } | |
147 return(is_cd); | |
148 } | |
149 | |
150 /* Add a CD-ROM drive to our list of valid drives */ | |
151 static void AddDrive(char *drive, struct stat *stbuf) | |
152 { | |
153 int i; | |
154 | |
155 if ( SDL_numcds < MAX_DRIVES ) { | |
156 /* Check to make sure it's not already in our list. | |
157 This can happen when we see a drive via symbolic link. | |
158 */ | |
159 for ( i=0; i<SDL_numcds; ++i ) { | |
160 if ( stbuf->st_rdev == SDL_cdmode[i] ) { | |
161 #ifdef DEBUG_CDROM | |
162 fprintf(stderr, "Duplicate drive detected: %s == %s\n", drive, SDL_cdlist[i]); | |
163 #endif | |
164 return; | |
165 } | |
166 } | |
167 | |
168 /* Add this drive to our list */ | |
169 i = SDL_numcds; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
170 SDL_cdlist[i] = (char *)SDL_malloc(SDL_strlen(drive)+1); |
0 | 171 if ( SDL_cdlist[i] == NULL ) { |
172 SDL_OutOfMemory(); | |
173 return; | |
174 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
175 SDL_strcpy(SDL_cdlist[i], drive); |
0 | 176 SDL_cdmode[i] = stbuf->st_rdev; |
177 ++SDL_numcds; | |
178 #ifdef DEBUG_CDROM | |
179 fprintf(stderr, "Added CD-ROM drive: %s\n", drive); | |
180 #endif | |
181 } | |
182 } | |
183 | |
184 #ifdef USE_MNTENT | |
185 static void CheckMounts(const char *mtab) | |
186 { | |
187 FILE *mntfp; | |
188 struct mntent *mntent; | |
189 struct stat stbuf; | |
190 | |
191 mntfp = setmntent(mtab, "r"); | |
192 if ( mntfp != NULL ) { | |
139
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
193 char *tmp; |
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
194 char *mnt_type; |
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
195 char *mnt_dev; |
0 | 196 |
197 while ( (mntent=getmntent(mntfp)) != NULL ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
198 mnt_type = SDL_malloc(SDL_strlen(mntent->mnt_type) + 1); |
139
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
199 if (mnt_type == NULL) |
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
200 continue; /* maybe you'll get lucky next time. */ |
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
201 |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
202 mnt_dev = SDL_malloc(SDL_strlen(mntent->mnt_fsname) + 1); |
139
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
203 if (mnt_dev == NULL) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
204 SDL_free(mnt_type); |
139
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
205 continue; |
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
206 } |
ef23a1bf1244
Fixed potential buffer overflow in Linux CD code (thanks Ryan!)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
207 |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
208 SDL_strcpy(mnt_type, mntent->mnt_type); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
209 SDL_strcpy(mnt_dev, mntent->mnt_fsname); |
0 | 210 |
211 /* Handle "supermount" filesystem mounts */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
212 if ( SDL_strcmp(mnt_type, MNTTYPE_SUPER) == 0 ) { |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
213 tmp = SDL_strstr(mntent->mnt_opts, "fs="); |
0 | 214 if ( tmp ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
215 SDL_free(mnt_type); |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
216 mnt_type = SDL_strdup(tmp + SDL_strlen("fs=")); |
247
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
217 if ( mnt_type ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
218 tmp = SDL_strchr(mnt_type, ','); |
247
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
219 if ( tmp ) { |
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
220 *tmp = '\0'; |
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
221 } |
0 | 222 } |
223 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
224 tmp = SDL_strstr(mntent->mnt_opts, "dev="); |
0 | 225 if ( tmp ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
226 SDL_free(mnt_dev); |
1341
d02b552e5304
Configure dynamically generates SDL_config.h
Sam Lantinga <slouken@libsdl.org>
parents:
1338
diff
changeset
|
227 mnt_dev = SDL_strdup(tmp + SDL_strlen("dev=")); |
247
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
228 if ( mnt_dev ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
229 tmp = SDL_strchr(mnt_dev, ','); |
247
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
230 if ( tmp ) { |
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
231 *tmp = '\0'; |
b0f09f86378d
Fix crash with Linux supermount fstab entries (thanks Erno!)
Sam Lantinga <slouken@libsdl.org>
parents:
139
diff
changeset
|
232 } |
0 | 233 } |
234 } | |
235 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
236 if ( SDL_strcmp(mnt_type, MNTTYPE_CDROM) == 0 ) { |
0 | 237 #ifdef DEBUG_CDROM |
238 fprintf(stderr, "Checking mount path from %s: %s mounted on %s of %s\n", | |
239 mtab, mnt_dev, mntent->mnt_dir, mnt_type); | |
240 #endif | |
241 if (CheckDrive(mnt_dev, mnt_type, &stbuf) > 0) { | |
242 AddDrive(mnt_dev, &stbuf); | |
243 } | |
244 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
245 SDL_free(mnt_dev); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
246 SDL_free(mnt_type); |
0 | 247 } |
248 endmntent(mntfp); | |
249 } | |
250 } | |
251 #endif /* USE_MNTENT */ | |
252 | |
253 int SDL_SYS_CDInit(void) | |
254 { | |
255 /* checklist: /dev/cdrom, /dev/hd?, /dev/scd? /dev/sr? */ | |
256 static char *checklist[] = { | |
257 "cdrom", "?a hd?", "?0 scd?", "?0 sr?", NULL | |
258 }; | |
259 char *SDLcdrom; | |
260 int i, j, exists; | |
261 char drive[32]; | |
262 struct stat stbuf; | |
263 | |
264 /* Fill in our driver capabilities */ | |
265 SDL_CDcaps.Name = SDL_SYS_CDName; | |
266 SDL_CDcaps.Open = SDL_SYS_CDOpen; | |
267 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC; | |
268 SDL_CDcaps.Status = SDL_SYS_CDStatus; | |
269 SDL_CDcaps.Play = SDL_SYS_CDPlay; | |
270 SDL_CDcaps.Pause = SDL_SYS_CDPause; | |
271 SDL_CDcaps.Resume = SDL_SYS_CDResume; | |
272 SDL_CDcaps.Stop = SDL_SYS_CDStop; | |
273 SDL_CDcaps.Eject = SDL_SYS_CDEject; | |
274 SDL_CDcaps.Close = SDL_SYS_CDClose; | |
275 | |
276 /* 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
|
277 SDLcdrom = SDL_getenv("SDL_CDROM"); /* ':' separated list of devices */ |
0 | 278 if ( SDLcdrom != NULL ) { |
279 char *cdpath, *delim; | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
280 cdpath = SDL_malloc(SDL_strlen(SDLcdrom)+1); |
0 | 281 if ( cdpath != NULL ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
282 SDL_strcpy(cdpath, SDLcdrom); |
0 | 283 SDLcdrom = cdpath; |
284 do { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
285 delim = SDL_strchr(SDLcdrom, ':'); |
0 | 286 if ( delim ) { |
287 *delim++ = '\0'; | |
288 } | |
289 #ifdef DEBUG_CDROM | |
290 fprintf(stderr, "Checking CD-ROM drive from SDL_CDROM: %s\n", SDLcdrom); | |
291 #endif | |
292 if ( CheckDrive(SDLcdrom, NULL, &stbuf) > 0 ) { | |
293 AddDrive(SDLcdrom, &stbuf); | |
294 } | |
295 if ( delim ) { | |
296 SDLcdrom = delim; | |
297 } else { | |
298 SDLcdrom = NULL; | |
299 } | |
300 } while ( SDLcdrom ); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
301 SDL_free(cdpath); |
0 | 302 } |
303 | |
304 /* If we found our drives, there's nothing left to do */ | |
305 if ( SDL_numcds > 0 ) { | |
306 return(0); | |
307 } | |
308 } | |
309 | |
310 #ifdef USE_MNTENT | |
311 /* Check /dev/cdrom first :-) */ | |
312 if (CheckDrive("/dev/cdrom", NULL, &stbuf) > 0) { | |
313 AddDrive("/dev/cdrom", &stbuf); | |
314 } | |
315 | |
316 /* Now check the currently mounted CD drives */ | |
317 CheckMounts(_PATH_MOUNTED); | |
318 | |
319 /* Finally check possible mountable drives in /etc/fstab */ | |
320 CheckMounts(_PATH_MNTTAB); | |
321 | |
322 /* If we found our drives, there's nothing left to do */ | |
323 if ( SDL_numcds > 0 ) { | |
324 return(0); | |
325 } | |
326 #endif /* USE_MNTENT */ | |
327 | |
328 /* Scan the system for CD-ROM drives. | |
329 Not always 100% reliable, so use the USE_MNTENT code above first. | |
330 */ | |
331 for ( i=0; checklist[i]; ++i ) { | |
332 if ( checklist[i][0] == '?' ) { | |
333 char *insert; | |
334 exists = 1; | |
335 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
|
336 SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s", &checklist[i][3]); |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
337 insert = SDL_strchr(drive, '?'); |
0 | 338 if ( insert != NULL ) { |
339 *insert = j; | |
340 } | |
341 #ifdef DEBUG_CDROM | |
342 fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive); | |
343 #endif | |
344 switch (CheckDrive(drive, NULL, &stbuf)) { | |
345 /* Drive exists and is a CD-ROM */ | |
346 case 1: | |
347 AddDrive(drive, &stbuf); | |
348 break; | |
349 /* Drive exists, but isn't a CD-ROM */ | |
350 case 0: | |
351 break; | |
352 /* Drive doesn't exist */ | |
353 case -1: | |
354 exists = 0; | |
355 break; | |
356 } | |
357 } | |
358 } else { | |
1338
604d73db6802
Removed uses of stdlib.h and string.h
Sam Lantinga <slouken@libsdl.org>
parents:
1336
diff
changeset
|
359 SDL_snprintf(drive, SDL_arraysize(drive), "/dev/%s", checklist[i]); |
0 | 360 #ifdef DEBUG_CDROM |
361 fprintf(stderr, "Checking possible CD-ROM drive: %s\n", drive); | |
362 #endif | |
363 if ( CheckDrive(drive, NULL, &stbuf) > 0 ) { | |
364 AddDrive(drive, &stbuf); | |
365 } | |
366 } | |
367 } | |
368 return(0); | |
369 } | |
370 | |
371 /* General ioctl() CD-ROM command function */ | |
372 static int SDL_SYS_CDioctl(int id, int command, void *arg) | |
373 { | |
374 int retval; | |
375 | |
376 retval = ioctl(id, command, arg); | |
377 if ( retval < 0 ) { | |
378 SDL_SetError("ioctl() error: %s", strerror(errno)); | |
379 } | |
380 return(retval); | |
381 } | |
382 | |
383 static const char *SDL_SYS_CDName(int drive) | |
384 { | |
385 return(SDL_cdlist[drive]); | |
386 } | |
387 | |
388 static int SDL_SYS_CDOpen(int drive) | |
389 { | |
1051
091350827e08
SDL_CDOpen() fails on Linux if data track on disc is mounted in the
Ryan C. Gordon <icculus@icculus.org>
parents:
998
diff
changeset
|
390 return(open(SDL_cdlist[drive], (O_RDONLY|O_NONBLOCK), 0)); |
0 | 391 } |
392 | |
393 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom) | |
394 { | |
395 struct cdrom_tochdr toc; | |
396 int i, okay; | |
397 struct cdrom_tocentry entry; | |
398 | |
399 okay = 0; | |
400 if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCHDR, &toc) == 0 ) { | |
401 cdrom->numtracks = toc.cdth_trk1-toc.cdth_trk0+1; | |
402 if ( cdrom->numtracks > SDL_MAX_TRACKS ) { | |
403 cdrom->numtracks = SDL_MAX_TRACKS; | |
404 } | |
405 /* Read all the track TOC entries */ | |
406 for ( i=0; i<=cdrom->numtracks; ++i ) { | |
407 if ( i == cdrom->numtracks ) { | |
408 cdrom->track[i].id = CDROM_LEADOUT; | |
409 } else { | |
410 cdrom->track[i].id = toc.cdth_trk0+i; | |
411 } | |
412 entry.cdte_track = cdrom->track[i].id; | |
413 entry.cdte_format = CDROM_MSF; | |
414 if ( SDL_SYS_CDioctl(cdrom->id, CDROMREADTOCENTRY, | |
415 &entry) < 0 ) { | |
416 break; | |
417 } else { | |
418 if ( entry.cdte_ctrl & CDROM_DATA_TRACK ) { | |
419 cdrom->track[i].type = SDL_DATA_TRACK; | |
420 } else { | |
421 cdrom->track[i].type = SDL_AUDIO_TRACK; | |
422 } | |
423 cdrom->track[i].offset = MSF_TO_FRAMES( | |
424 entry.cdte_addr.msf.minute, | |
425 entry.cdte_addr.msf.second, | |
426 entry.cdte_addr.msf.frame); | |
427 cdrom->track[i].length = 0; | |
428 if ( i > 0 ) { | |
429 cdrom->track[i-1].length = | |
430 cdrom->track[i].offset- | |
431 cdrom->track[i-1].offset; | |
432 } | |
433 } | |
434 } | |
435 if ( i == (cdrom->numtracks+1) ) { | |
436 okay = 1; | |
437 } | |
438 } | |
439 return(okay ? 0 : -1); | |
440 } | |
441 | |
442 /* Get CD-ROM status */ | |
443 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position) | |
444 { | |
445 CDstatus status; | |
446 struct cdrom_tochdr toc; | |
447 struct cdrom_subchnl info; | |
448 | |
449 info.cdsc_format = CDROM_MSF; | |
450 if ( ioctl(cdrom->id, CDROMSUBCHNL, &info) < 0 ) { | |
451 if ( ERRNO_TRAYEMPTY(errno) ) { | |
452 status = CD_TRAYEMPTY; | |
453 } else { | |
454 status = CD_ERROR; | |
455 } | |
456 } else { | |
457 switch (info.cdsc_audiostatus) { | |
458 case CDROM_AUDIO_INVALID: | |
459 case CDROM_AUDIO_NO_STATUS: | |
460 /* Try to determine if there's a CD available */ | |
461 if (ioctl(cdrom->id, CDROMREADTOCHDR, &toc)==0) | |
462 status = CD_STOPPED; | |
463 else | |
464 status = CD_TRAYEMPTY; | |
465 break; | |
466 case CDROM_AUDIO_COMPLETED: | |
467 status = CD_STOPPED; | |
468 break; | |
469 case CDROM_AUDIO_PLAY: | |
470 status = CD_PLAYING; | |
471 break; | |
472 case CDROM_AUDIO_PAUSED: | |
473 /* Workaround buggy CD-ROM drive */ | |
474 if ( info.cdsc_trk == CDROM_LEADOUT ) { | |
475 status = CD_STOPPED; | |
476 } else { | |
477 status = CD_PAUSED; | |
478 } | |
479 break; | |
480 default: | |
481 status = CD_ERROR; | |
482 break; | |
483 } | |
484 } | |
485 if ( position ) { | |
486 if ( status == CD_PLAYING || (status == CD_PAUSED) ) { | |
487 *position = MSF_TO_FRAMES( | |
488 info.cdsc_absaddr.msf.minute, | |
489 info.cdsc_absaddr.msf.second, | |
490 info.cdsc_absaddr.msf.frame); | |
491 } else { | |
492 *position = 0; | |
493 } | |
494 } | |
495 return(status); | |
496 } | |
497 | |
498 /* Start play */ | |
499 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length) | |
500 { | |
501 struct cdrom_msf playtime; | |
502 | |
503 FRAMES_TO_MSF(start, | |
504 &playtime.cdmsf_min0, &playtime.cdmsf_sec0, &playtime.cdmsf_frame0); | |
505 FRAMES_TO_MSF(start+length, | |
506 &playtime.cdmsf_min1, &playtime.cdmsf_sec1, &playtime.cdmsf_frame1); | |
507 #ifdef DEBUG_CDROM | |
508 fprintf(stderr, "Trying to play from %d:%d:%d to %d:%d:%d\n", | |
509 playtime.cdmsf_min0, playtime.cdmsf_sec0, playtime.cdmsf_frame0, | |
510 playtime.cdmsf_min1, playtime.cdmsf_sec1, playtime.cdmsf_frame1); | |
511 #endif | |
512 return(SDL_SYS_CDioctl(cdrom->id, CDROMPLAYMSF, &playtime)); | |
513 } | |
514 | |
515 /* Pause play */ | |
516 static int SDL_SYS_CDPause(SDL_CD *cdrom) | |
517 { | |
518 return(SDL_SYS_CDioctl(cdrom->id, CDROMPAUSE, 0)); | |
519 } | |
520 | |
521 /* Resume play */ | |
522 static int SDL_SYS_CDResume(SDL_CD *cdrom) | |
523 { | |
524 return(SDL_SYS_CDioctl(cdrom->id, CDROMRESUME, 0)); | |
525 } | |
526 | |
527 /* Stop play */ | |
528 static int SDL_SYS_CDStop(SDL_CD *cdrom) | |
529 { | |
530 return(SDL_SYS_CDioctl(cdrom->id, CDROMSTOP, 0)); | |
531 } | |
532 | |
533 /* Eject the CD-ROM */ | |
534 static int SDL_SYS_CDEject(SDL_CD *cdrom) | |
535 { | |
536 return(SDL_SYS_CDioctl(cdrom->id, CDROMEJECT, 0)); | |
537 } | |
538 | |
539 /* Close the CD-ROM handle */ | |
540 static void SDL_SYS_CDClose(SDL_CD *cdrom) | |
541 { | |
542 close(cdrom->id); | |
543 } | |
544 | |
545 void SDL_SYS_CDQuit(void) | |
546 { | |
547 int i; | |
548 | |
549 if ( SDL_numcds > 0 ) { | |
550 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
|
551 SDL_free(SDL_cdlist[i]); |
0 | 552 } |
553 SDL_numcds = 0; | |
554 } | |
555 } | |
556 |