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