Mercurial > sdl-ios-xcode
annotate src/cdrom/win32/SDL_syscdrom.c @ 1433:bb6839704ed6
SDL_windows.h is no longer necessary
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 25 Feb 2006 22:18:25 +0000 |
parents | d910939febfa |
children | 84de7511f79f |
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:
904
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:
904
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:
904
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:
904
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:
904
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:
904
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:
904
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:
201
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 | |
1433
bb6839704ed6
SDL_windows.h is no longer necessary
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
26 #define WIN32_LEAN_AND_MEAN |
bb6839704ed6
SDL_windows.h is no longer necessary
Sam Lantinga <slouken@libsdl.org>
parents:
1402
diff
changeset
|
27 #include <windows.h> |
0 | 28 #include <mmsystem.h> |
29 | |
30 #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
|
31 #include "../SDL_syscdrom.h" |
0 | 32 |
33 /* This really broken?? */ | |
34 #define BROKEN_MCI_PAUSE /* Pausing actually stops play -- Doh! */ | |
35 | |
36 /* The maximum number of CD-ROM drives we'll detect (Don't change!) */ | |
37 #define MAX_DRIVES 26 | |
38 | |
39 /* A list of available CD-ROM drives */ | |
40 static char *SDL_cdlist[MAX_DRIVES]; | |
41 static MCIDEVICEID SDL_mciID[MAX_DRIVES]; | |
42 #ifdef BROKEN_MCI_PAUSE | |
43 static int SDL_paused[MAX_DRIVES]; | |
44 #endif | |
904
34af7946130a
Date: Fri, 16 Jul 2004 17:25:45 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
45 static int SDL_CD_end_position; |
0 | 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 | |
60 /* Add a CD-ROM drive to our list of valid drives */ | |
61 static void AddDrive(char *drive) | |
62 { | |
63 int i; | |
64 | |
65 if ( SDL_numcds < MAX_DRIVES ) { | |
66 /* Add this drive to our list */ | |
67 i = SDL_numcds; | |
1379
c0a74f199ecf
Use only safe string functions
Sam Lantinga <slouken@libsdl.org>
parents:
1361
diff
changeset
|
68 SDL_cdlist[i] = SDL_strdup(drive); |
0 | 69 if ( SDL_cdlist[i] == NULL ) { |
70 SDL_OutOfMemory(); | |
71 return; | |
72 } | |
73 ++SDL_numcds; | |
74 #ifdef CDROM_DEBUG | |
75 fprintf(stderr, "Added CD-ROM drive: %s\n", drive); | |
76 #endif | |
77 } | |
78 } | |
79 | |
80 int SDL_SYS_CDInit(void) | |
81 { | |
82 /* checklist: Drive 'A' - 'Z' */ | |
83 int i; | |
84 char drive[4]; | |
85 | |
86 /* Fill in our driver capabilities */ | |
87 SDL_CDcaps.Name = SDL_SYS_CDName; | |
88 SDL_CDcaps.Open = SDL_SYS_CDOpen; | |
89 SDL_CDcaps.GetTOC = SDL_SYS_CDGetTOC; | |
90 SDL_CDcaps.Status = SDL_SYS_CDStatus; | |
91 SDL_CDcaps.Play = SDL_SYS_CDPlay; | |
92 SDL_CDcaps.Pause = SDL_SYS_CDPause; | |
93 SDL_CDcaps.Resume = SDL_SYS_CDResume; | |
94 SDL_CDcaps.Stop = SDL_SYS_CDStop; | |
95 SDL_CDcaps.Eject = SDL_SYS_CDEject; | |
96 SDL_CDcaps.Close = SDL_SYS_CDClose; | |
97 | |
98 /* Scan the system for CD-ROM drives */ | |
99 for ( i='A'; i<='Z'; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
100 SDL_snprintf(drive, SDL_arraysize(drive), "%c:\\", i); |
0 | 101 if ( GetDriveType(drive) == DRIVE_CDROM ) { |
102 AddDrive(drive); | |
103 } | |
104 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
105 SDL_memset(SDL_mciID, 0, sizeof(SDL_mciID)); |
0 | 106 return(0); |
107 } | |
108 | |
109 /* General ioctl() CD-ROM command function */ | |
110 static int SDL_SYS_CDioctl(int id, UINT msg, DWORD flags, void *arg) | |
111 { | |
112 MCIERROR mci_error; | |
113 | |
114 mci_error = mciSendCommand(SDL_mciID[id], msg, flags, (DWORD)arg); | |
115 if ( mci_error ) { | |
116 char error[256]; | |
117 | |
118 mciGetErrorString(mci_error, error, 256); | |
119 SDL_SetError("mciSendCommand() error: %s", error); | |
120 } | |
121 return(!mci_error ? 0 : -1); | |
122 } | |
123 | |
124 static const char *SDL_SYS_CDName(int drive) | |
125 { | |
126 return(SDL_cdlist[drive]); | |
127 } | |
128 | |
129 static int SDL_SYS_CDOpen(int drive) | |
130 { | |
131 MCI_OPEN_PARMS mci_open; | |
132 MCI_SET_PARMS mci_set; | |
133 char device[3]; | |
134 DWORD flags; | |
135 | |
136 /* Open the requested device */ | |
137 mci_open.lpstrDeviceType = (LPCSTR) MCI_DEVTYPE_CD_AUDIO; | |
138 device[0] = *SDL_cdlist[drive]; | |
139 device[1] = ':'; | |
140 device[2] = '\0'; | |
141 mci_open.lpstrElementName = device; | |
142 flags = | |
143 (MCI_OPEN_TYPE|MCI_OPEN_SHAREABLE|MCI_OPEN_TYPE_ID|MCI_OPEN_ELEMENT); | |
144 if ( SDL_SYS_CDioctl(0, MCI_OPEN, flags, &mci_open) < 0 ) { | |
145 flags &= ~MCI_OPEN_SHAREABLE; | |
146 if ( SDL_SYS_CDioctl(0, MCI_OPEN, flags, &mci_open) < 0 ) { | |
147 return(-1); | |
148 } | |
149 } | |
150 SDL_mciID[drive] = mci_open.wDeviceID; | |
151 | |
152 /* Set the minute-second-frame time format */ | |
153 mci_set.dwTimeFormat = MCI_FORMAT_MSF; | |
154 SDL_SYS_CDioctl(drive, MCI_SET, MCI_SET_TIME_FORMAT, &mci_set); | |
155 | |
156 #ifdef BROKEN_MCI_PAUSE | |
157 SDL_paused[drive] = 0; | |
158 #endif | |
159 return(drive); | |
160 } | |
161 | |
162 static int SDL_SYS_CDGetTOC(SDL_CD *cdrom) | |
163 { | |
164 MCI_STATUS_PARMS mci_status; | |
165 int i, okay; | |
166 DWORD flags; | |
167 | |
168 okay = 0; | |
169 mci_status.dwItem = MCI_STATUS_NUMBER_OF_TRACKS; | |
170 flags = MCI_STATUS_ITEM | MCI_WAIT; | |
171 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, &mci_status) == 0 ) { | |
172 cdrom->numtracks = mci_status.dwReturn; | |
173 if ( cdrom->numtracks > SDL_MAX_TRACKS ) { | |
174 cdrom->numtracks = SDL_MAX_TRACKS; | |
175 } | |
176 /* Read all the track TOC entries */ | |
177 flags = MCI_STATUS_ITEM | MCI_TRACK | MCI_WAIT; | |
178 for ( i=0; i<cdrom->numtracks; ++i ) { | |
179 cdrom->track[i].id = i+1; | |
180 mci_status.dwTrack = cdrom->track[i].id; | |
181 #ifdef MCI_CDA_STATUS_TYPE_TRACK | |
182 mci_status.dwItem = MCI_CDA_STATUS_TYPE_TRACK; | |
183 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, | |
184 &mci_status) < 0 ) { | |
185 break; | |
186 } | |
187 if ( mci_status.dwReturn == MCI_CDA_TRACK_AUDIO ) { | |
188 cdrom->track[i].type = SDL_AUDIO_TRACK; | |
189 } else { | |
190 cdrom->track[i].type = SDL_DATA_TRACK; | |
191 } | |
192 #else | |
193 cdrom->track[i].type = SDL_AUDIO_TRACK; | |
194 #endif | |
195 mci_status.dwItem = MCI_STATUS_POSITION; | |
196 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, | |
197 &mci_status) < 0 ) { | |
198 break; | |
199 } | |
200 cdrom->track[i].offset = MSF_TO_FRAMES( | |
201 MCI_MSF_MINUTE(mci_status.dwReturn), | |
202 MCI_MSF_SECOND(mci_status.dwReturn), | |
203 MCI_MSF_FRAME(mci_status.dwReturn)); | |
204 cdrom->track[i].length = 0; | |
205 if ( i > 0 ) { | |
206 cdrom->track[i-1].length = | |
207 cdrom->track[i].offset- | |
208 cdrom->track[i-1].offset; | |
209 } | |
210 } | |
211 if ( i == cdrom->numtracks ) { | |
201
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
212 mci_status.dwTrack = cdrom->track[i - 1].id; |
0 | 213 mci_status.dwItem = MCI_STATUS_LENGTH; |
214 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, | |
215 &mci_status) == 0 ) { | |
201
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
216 cdrom->track[i - 1].length = MSF_TO_FRAMES( |
0 | 217 MCI_MSF_MINUTE(mci_status.dwReturn), |
218 MCI_MSF_SECOND(mci_status.dwReturn), | |
201
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
219 MCI_MSF_FRAME(mci_status.dwReturn)) + 1; /* +1 to fix */ |
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
220 /* MCI last track length bug */ |
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
221 /* compute lead-out offset */ |
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
222 cdrom->track[i].offset = cdrom->track[i - 1].offset + |
ddee60057806
Fixed last track time calculation (thanks Tchi Southivong)
Sam Lantinga <slouken@libsdl.org>
parents:
192
diff
changeset
|
223 cdrom->track[i - 1].length; |
0 | 224 cdrom->track[i].length = 0; |
225 okay = 1; | |
226 } | |
227 } | |
228 } | |
229 return(okay ? 0 : -1); | |
230 } | |
231 | |
232 /* Get CD-ROM status */ | |
233 static CDstatus SDL_SYS_CDStatus(SDL_CD *cdrom, int *position) | |
234 { | |
235 CDstatus status; | |
236 MCI_STATUS_PARMS mci_status; | |
237 DWORD flags; | |
238 | |
239 flags = MCI_STATUS_ITEM | MCI_WAIT; | |
240 mci_status.dwItem = MCI_STATUS_MODE; | |
241 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, &mci_status) < 0 ) { | |
242 status = CD_ERROR; | |
243 } else { | |
244 switch (mci_status.dwReturn) { | |
245 case MCI_MODE_NOT_READY: | |
246 case MCI_MODE_OPEN: | |
247 status = CD_TRAYEMPTY; | |
248 break; | |
249 case MCI_MODE_STOP: | |
250 #ifdef BROKEN_MCI_PAUSE | |
251 if ( SDL_paused[cdrom->id] ) { | |
252 status = CD_PAUSED; | |
253 } else { | |
254 status = CD_STOPPED; | |
255 } | |
256 #else | |
257 status = CD_STOPPED; | |
258 #endif /* BROKEN_MCI_PAUSE */ | |
259 break; | |
260 case MCI_MODE_PLAY: | |
192
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
261 #ifdef BROKEN_MCI_PAUSE |
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
262 if ( SDL_paused[cdrom->id] ) { |
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
263 status = CD_PAUSED; |
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
264 } else { |
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
265 status = CD_PLAYING; |
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
266 } |
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
267 #else |
0 | 268 status = CD_PLAYING; |
192
e31f8d815aaa
Fixed resuming a paused CD on Win2K (thanks Aragorn)
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
269 #endif /* BROKEN_MCI_PAUSE */ |
0 | 270 break; |
271 case MCI_MODE_PAUSE: | |
272 status = CD_PAUSED; | |
273 break; | |
274 default: | |
275 status = CD_ERROR; | |
276 break; | |
277 } | |
278 } | |
279 if ( position ) { | |
280 if ( status == CD_PLAYING || (status == CD_PAUSED) ) { | |
281 mci_status.dwItem = MCI_STATUS_POSITION; | |
282 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, | |
283 &mci_status) == 0 ) { | |
284 *position = MSF_TO_FRAMES( | |
285 MCI_MSF_MINUTE(mci_status.dwReturn), | |
286 MCI_MSF_SECOND(mci_status.dwReturn), | |
287 MCI_MSF_FRAME(mci_status.dwReturn)); | |
288 } else { | |
289 *position = 0; | |
290 } | |
291 } else { | |
292 *position = 0; | |
293 } | |
294 } | |
295 return(status); | |
296 } | |
297 | |
298 /* Start play */ | |
299 static int SDL_SYS_CDPlay(SDL_CD *cdrom, int start, int length) | |
300 { | |
301 MCI_PLAY_PARMS mci_play; | |
302 int m, s, f; | |
303 DWORD flags; | |
304 | |
305 flags = MCI_FROM | MCI_TO | MCI_NOTIFY; | |
306 mci_play.dwCallback = 0; | |
307 FRAMES_TO_MSF(start, &m, &s, &f); | |
308 mci_play.dwFrom = MCI_MAKE_MSF(m, s, f); | |
309 FRAMES_TO_MSF(start+length, &m, &s, &f); | |
310 mci_play.dwTo = MCI_MAKE_MSF(m, s, f); | |
904
34af7946130a
Date: Fri, 16 Jul 2004 17:25:45 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
311 SDL_CD_end_position = mci_play.dwTo; |
0 | 312 return(SDL_SYS_CDioctl(cdrom->id, MCI_PLAY, flags, &mci_play)); |
313 } | |
314 | |
315 /* Pause play */ | |
316 static int SDL_SYS_CDPause(SDL_CD *cdrom) | |
317 { | |
318 #ifdef BROKEN_MCI_PAUSE | |
319 SDL_paused[cdrom->id] = 1; | |
320 #endif | |
321 return(SDL_SYS_CDioctl(cdrom->id, MCI_PAUSE, MCI_WAIT, NULL)); | |
322 } | |
323 | |
324 /* Resume play */ | |
325 static int SDL_SYS_CDResume(SDL_CD *cdrom) | |
326 { | |
327 #ifdef BROKEN_MCI_PAUSE | |
328 MCI_STATUS_PARMS mci_status; | |
329 int okay; | |
330 int flags; | |
331 | |
332 okay = 0; | |
904
34af7946130a
Date: Fri, 16 Jul 2004 17:25:45 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
333 /* Play from the current play position to the end position set earlier */ |
0 | 334 flags = MCI_STATUS_ITEM | MCI_WAIT; |
335 mci_status.dwItem = MCI_STATUS_POSITION; | |
336 if ( SDL_SYS_CDioctl(cdrom->id, MCI_STATUS, flags, &mci_status) == 0 ) { | |
337 MCI_PLAY_PARMS mci_play; | |
338 | |
904
34af7946130a
Date: Fri, 16 Jul 2004 17:25:45 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
339 flags = MCI_FROM | MCI_TO | MCI_NOTIFY; |
0 | 340 mci_play.dwCallback = 0; |
341 mci_play.dwFrom = mci_status.dwReturn; | |
904
34af7946130a
Date: Fri, 16 Jul 2004 17:25:45 +0200
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
342 mci_play.dwTo = SDL_CD_end_position; |
0 | 343 if (SDL_SYS_CDioctl(cdrom->id,MCI_PLAY,flags,&mci_play) == 0) { |
344 okay = 1; | |
345 SDL_paused[cdrom->id] = 0; | |
346 } | |
347 } | |
348 return(okay ? 0 : -1); | |
349 #else | |
350 return(SDL_SYS_CDioctl(cdrom->id, MCI_RESUME, MCI_WAIT, NULL)); | |
351 #endif /* BROKEN_MCI_PAUSE */ | |
352 } | |
353 | |
354 /* Stop play */ | |
355 static int SDL_SYS_CDStop(SDL_CD *cdrom) | |
356 { | |
357 return(SDL_SYS_CDioctl(cdrom->id, MCI_STOP, MCI_WAIT, NULL)); | |
358 } | |
359 | |
360 /* Eject the CD-ROM */ | |
361 static int SDL_SYS_CDEject(SDL_CD *cdrom) | |
362 { | |
363 return(SDL_SYS_CDioctl(cdrom->id, MCI_SET, MCI_SET_DOOR_OPEN, NULL)); | |
364 } | |
365 | |
366 /* Close the CD-ROM handle */ | |
367 static void SDL_SYS_CDClose(SDL_CD *cdrom) | |
368 { | |
369 SDL_SYS_CDioctl(cdrom->id, MCI_CLOSE, MCI_WAIT, NULL); | |
370 } | |
371 | |
372 void SDL_SYS_CDQuit(void) | |
373 { | |
374 int i; | |
375 | |
376 if ( SDL_numcds > 0 ) { | |
377 for ( i=0; i<SDL_numcds; ++i ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1330
diff
changeset
|
378 SDL_free(SDL_cdlist[i]); |
0 | 379 } |
380 SDL_numcds = 0; | |
381 } | |
382 } |