2 * MCI driver for audio CD (MCICDA)
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998-99 Eric Pouech
6 * Copyright 2000 Andreas Mohr
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #define WIN32_NO_STATUS
37 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(mcicda
);
42 #define CDFRAMES_PERSEC 75
43 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
44 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
45 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
47 /* Defined by red-book standard; do not change! */
48 #define RAW_SECTOR_SIZE (2352)
50 /* Must be >= RAW_SECTOR_SIZE */
51 #define CDDA_FRAG_SIZE (32768)
53 #define CDDA_FRAG_COUNT (3)
57 int nUseCount
; /* Incremented for each shared open */
58 BOOL fShareable
; /* TRUE if first open was shareable */
59 MCIDEVICEID wNotifyDeviceID
; /* MCI device ID with a pending notification */
60 HANDLE hCallback
; /* Callback handle for pending notification */
64 /* The following are used for digital playback only */
70 IDirectSoundBuffer
*dsBuf
;
75 /*-----------------------------------------------------------------------*/
77 typedef HRESULT(WINAPI
*LPDIRECTSOUNDCREATE
)(LPCGUID
,LPDIRECTSOUND
*,LPUNKNOWN
);
78 static LPDIRECTSOUNDCREATE pDirectSoundCreate
;
80 static BOOL
device_io(HANDLE dev
, DWORD code
, void *inbuffer
, DWORD insize
, void *outbuffer
, DWORD outsize
, DWORD
*retsize
, OVERLAPPED
*overlapped
)
83 BOOL ret
= DeviceIoControl(dev
, code
, inbuffer
, insize
, outbuffer
, outsize
, retsize
, overlapped
);
85 #define XX(x) case (x): str = #x; break
88 XX(IOCTL_CDROM_RAW_READ
);
89 XX(IOCTL_CDROM_READ_TOC
);
90 XX(IOCTL_CDROM_READ_Q_CHANNEL
);
91 XX(IOCTL_CDROM_SEEK_AUDIO_MSF
);
92 XX(IOCTL_CDROM_PLAY_AUDIO_MSF
);
93 XX(IOCTL_CDROM_STOP_AUDIO
);
94 XX(IOCTL_CDROM_PAUSE_AUDIO
);
95 XX(IOCTL_CDROM_RESUME_AUDIO
);
96 XX(IOCTL_STORAGE_EJECT_MEDIA
);
97 XX(IOCTL_STORAGE_LOAD_MEDIA
);
98 default: str
= wine_dbg_sprintf("UNKNOWN (0x%x)", code
);
101 TRACE("Device %p, Code %s -> Return %d, Bytes %u\n", dev
, str
, ret
, *retsize
);
105 static DWORD CALLBACK
MCICDA_playLoop(void *ptr
)
107 WINE_MCICDAUDIO
*wmcda
= (WINE_MCICDAUDIO
*)ptr
;
108 DWORD lastPos
, curPos
, endPos
, br
;
110 DWORD lockLen
, fragLen
;
112 RAW_READ_INFO rdInfo
;
115 memset(&caps
, 0, sizeof(caps
));
116 caps
.dwSize
= sizeof(caps
);
117 hr
= IDirectSoundBuffer_GetCaps(wmcda
->dsBuf
, &caps
);
119 fragLen
= caps
.dwBufferBytes
/CDDA_FRAG_COUNT
;
120 curPos
= lastPos
= 0;
122 while (SUCCEEDED(hr
) && endPos
!= lastPos
&&
123 WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
) {
124 hr
= IDirectSoundBuffer_GetCurrentPosition(wmcda
->dsBuf
, &curPos
, NULL
);
125 if ((curPos
-lastPos
+caps
.dwBufferBytes
)%caps
.dwBufferBytes
< fragLen
) {
130 EnterCriticalSection(&wmcda
->cs
);
131 rdInfo
.DiskOffset
.QuadPart
= wmcda
->start
<<11;
132 rdInfo
.SectorCount
= min(fragLen
/RAW_SECTOR_SIZE
, wmcda
->end
-wmcda
->start
);
133 rdInfo
.TrackMode
= CDDA
;
135 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, lastPos
, fragLen
, &cdData
, &lockLen
, NULL
, NULL
, 0);
136 if (hr
== DSERR_BUFFERLOST
) {
137 if(FAILED(IDirectSoundBuffer_Restore(wmcda
->dsBuf
)) ||
138 FAILED(IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
))) {
139 LeaveCriticalSection(&wmcda
->cs
);
142 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, lastPos
, fragLen
, &cdData
, &lockLen
, NULL
, NULL
, 0);
146 if (rdInfo
.SectorCount
> 0) {
147 if (!device_io(wmcda
->handle
, IOCTL_CDROM_RAW_READ
, &rdInfo
, sizeof(rdInfo
), cdData
, lockLen
, &br
, NULL
))
148 WARN("CD read failed at sector %d: 0x%x\n", wmcda
->start
, GetLastError());
150 if (rdInfo
.SectorCount
*RAW_SECTOR_SIZE
< lockLen
) {
151 if(endPos
== ~0u) endPos
= lastPos
;
152 memset((BYTE
*)cdData
+ rdInfo
.SectorCount
*RAW_SECTOR_SIZE
, 0,
153 lockLen
- rdInfo
.SectorCount
*RAW_SECTOR_SIZE
);
155 hr
= IDirectSoundBuffer_Unlock(wmcda
->dsBuf
, cdData
, lockLen
, NULL
, 0);
159 lastPos
%= caps
.dwBufferBytes
;
160 wmcda
->start
+= rdInfo
.SectorCount
;
162 LeaveCriticalSection(&wmcda
->cs
);
164 IDirectSoundBuffer_Stop(wmcda
->dsBuf
);
165 SetEvent(wmcda
->stopEvent
);
167 /* A design bug in native: the independent CD player called by the
168 * MCI has no means to signal end of playing, therefore the MCI
169 * notification is left hanging. MCI_NOTIFY_SUPERSEDED will be
170 * signaled by the next command that has MCI_NOTIFY set (or
171 * MCI_NOTIFY_ABORTED for MCI_PLAY). */
178 /**************************************************************************
179 * MCICDA_drvOpen [internal]
181 static DWORD
MCICDA_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
183 static HMODULE dsHandle
;
184 WINE_MCICDAUDIO
* wmcda
;
186 if (!modp
) return 0xFFFFFFFF;
187 /* FIXME: MCIERR_CANNOT_LOAD_DRIVER if there's no drive of type CD-ROM */
189 wmcda
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCICDAUDIO
));
195 dsHandle
= LoadLibraryA("dsound.dll");
197 pDirectSoundCreate
= (LPDIRECTSOUNDCREATE
)GetProcAddress(dsHandle
, "DirectSoundCreate");
200 wmcda
->wDevID
= modp
->wDeviceID
;
201 mciSetDriverData(wmcda
->wDevID
, (DWORD_PTR
)wmcda
);
202 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
203 modp
->wType
= MCI_DEVTYPE_CD_AUDIO
;
204 InitializeCriticalSection(&wmcda
->cs
);
205 wmcda
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINE_MCICDAUDIO.cs");
206 return modp
->wDeviceID
;
209 /**************************************************************************
210 * MCICDA_drvClose [internal]
212 static DWORD
MCICDA_drvClose(DWORD dwDevID
)
214 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(dwDevID
);
217 wmcda
->cs
.DebugInfo
->Spare
[0] = 0;
218 DeleteCriticalSection(&wmcda
->cs
);
219 HeapFree(GetProcessHeap(), 0, wmcda
);
220 mciSetDriverData(dwDevID
, 0);
222 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
225 /**************************************************************************
226 * MCICDA_GetOpenDrv [internal]
228 static WINE_MCICDAUDIO
* MCICDA_GetOpenDrv(UINT wDevID
)
230 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
232 if (wmcda
== NULL
|| wmcda
->nUseCount
== 0) {
233 WARN("Invalid wDevID=%u\n", wDevID
);
239 /**************************************************************************
240 * MCICDA_mciNotify [internal]
242 * Notifications in MCI work like a 1-element queue.
243 * Each new notification request supersedes the previous one.
245 static void MCICDA_Notify(DWORD_PTR hWndCallBack
, WINE_MCICDAUDIO
* wmcda
, UINT wStatus
)
247 MCIDEVICEID wDevID
= wmcda
->wNotifyDeviceID
;
248 HANDLE old
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
249 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
250 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
253 /**************************************************************************
254 * MCICDA_ReadTOC [internal]
256 static BOOL
MCICDA_ReadTOC(WINE_MCICDAUDIO
* wmcda
, CDROM_TOC
*toc
, DWORD
*br
)
258 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0, toc
, sizeof(*toc
), br
, NULL
)) {
259 WARN("error reading TOC !\n");
265 /**************************************************************************
266 * MCICDA_GetStatus [internal]
268 static DWORD
MCICDA_GetStatus(WINE_MCICDAUDIO
* wmcda
)
270 CDROM_SUB_Q_DATA_FORMAT fmt
;
271 SUB_Q_CHANNEL_DATA data
;
273 DWORD mode
= MCI_MODE_NOT_READY
;
275 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
276 if(wmcda
->hThread
!= 0) {
280 hr
= IDirectSoundBuffer_GetStatus(wmcda
->dsBuf
, &status
);
282 if(!(status
&DSBSTATUS_PLAYING
)) {
283 if(WaitForSingleObject(wmcda
->stopEvent
, 0) == WAIT_OBJECT_0
)
284 mode
= MCI_MODE_STOP
;
286 mode
= MCI_MODE_PAUSE
;
289 mode
= MCI_MODE_PLAY
;
292 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
293 &data
, sizeof(data
), &br
, NULL
)) {
294 if (GetLastError() == ERROR_NOT_READY
) mode
= MCI_MODE_OPEN
;
296 switch (data
.CurrentPosition
.Header
.AudioStatus
)
298 case AUDIO_STATUS_IN_PROGRESS
: mode
= MCI_MODE_PLAY
; break;
299 case AUDIO_STATUS_PAUSED
: mode
= MCI_MODE_PAUSE
; break;
300 case AUDIO_STATUS_NO_STATUS
:
301 case AUDIO_STATUS_PLAY_COMPLETE
: mode
= MCI_MODE_STOP
; break;
302 case AUDIO_STATUS_PLAY_ERROR
:
303 case AUDIO_STATUS_NOT_SUPPORTED
:
311 /**************************************************************************
312 * MCICDA_GetError [internal]
314 static int MCICDA_GetError(WINE_MCICDAUDIO
* wmcda
)
316 switch (GetLastError())
318 case ERROR_NOT_READY
: return MCIERR_DEVICE_NOT_READY
;
319 case ERROR_NOT_SUPPORTED
:
320 case ERROR_IO_DEVICE
: return MCIERR_HARDWARE
;
322 FIXME("Unknown mode %u\n", GetLastError());
324 return MCIERR_DRIVER_INTERNAL
;
327 /**************************************************************************
328 * MCICDA_CalcFrame [internal]
330 static DWORD
MCICDA_CalcFrame(WINE_MCICDAUDIO
* wmcda
, DWORD dwTime
)
338 TRACE("(%p, %08X, %u);\n", wmcda
, wmcda
->dwTimeFormat
, dwTime
);
340 switch (wmcda
->dwTimeFormat
) {
341 case MCI_FORMAT_MILLISECONDS
:
342 dwFrame
= ((dwTime
- 1) * CDFRAMES_PERSEC
+ 500) / 1000;
343 TRACE("MILLISECONDS %u\n", dwFrame
);
346 TRACE("MSF %02u:%02u:%02u\n",
347 MCI_MSF_MINUTE(dwTime
), MCI_MSF_SECOND(dwTime
), MCI_MSF_FRAME(dwTime
));
348 dwFrame
+= CDFRAMES_PERMIN
* MCI_MSF_MINUTE(dwTime
);
349 dwFrame
+= CDFRAMES_PERSEC
* MCI_MSF_SECOND(dwTime
);
350 dwFrame
+= MCI_MSF_FRAME(dwTime
);
352 case MCI_FORMAT_TMSF
:
353 default: /* unknown format ! force TMSF ! ... */
354 wTrack
= MCI_TMSF_TRACK(dwTime
);
355 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
356 &toc
, sizeof(toc
), &br
, NULL
))
358 if (wTrack
< toc
.FirstTrack
|| wTrack
> toc
.LastTrack
)
360 TRACE("MSF %02u-%02u:%02u:%02u\n",
361 MCI_TMSF_TRACK(dwTime
), MCI_TMSF_MINUTE(dwTime
),
362 MCI_TMSF_SECOND(dwTime
), MCI_TMSF_FRAME(dwTime
));
363 addr
= toc
.TrackData
[wTrack
- toc
.FirstTrack
].Address
;
364 TRACE("TMSF trackpos[%u]=%d:%d:%d\n",
365 wTrack
, addr
[1], addr
[2], addr
[3]);
366 dwFrame
= CDFRAMES_PERMIN
* (addr
[1] + MCI_TMSF_MINUTE(dwTime
)) +
367 CDFRAMES_PERSEC
* (addr
[2] + MCI_TMSF_SECOND(dwTime
)) +
368 addr
[3] + MCI_TMSF_FRAME(dwTime
);
374 /**************************************************************************
375 * MCICDA_CalcTime [internal]
377 static DWORD
MCICDA_CalcTime(WINE_MCICDAUDIO
* wmcda
, DWORD tf
, DWORD dwFrame
, LPDWORD lpRet
)
387 TRACE("(%p, %08X, %u);\n", wmcda
, tf
, dwFrame
);
390 case MCI_FORMAT_MILLISECONDS
:
391 dwTime
= (dwFrame
* 1000) / CDFRAMES_PERSEC
+ 1;
392 TRACE("MILLISECONDS %u\n", dwTime
);
396 wMinutes
= dwFrame
/ CDFRAMES_PERMIN
;
397 wSeconds
= (dwFrame
- CDFRAMES_PERMIN
* wMinutes
) / CDFRAMES_PERSEC
;
398 wFrames
= dwFrame
- CDFRAMES_PERMIN
* wMinutes
- CDFRAMES_PERSEC
* wSeconds
;
399 dwTime
= MCI_MAKE_MSF(wMinutes
, wSeconds
, wFrames
);
400 TRACE("MSF %02u:%02u:%02u -> dwTime=%u\n",
401 wMinutes
, wSeconds
, wFrames
, dwTime
);
402 *lpRet
= MCI_COLONIZED3_RETURN
;
404 case MCI_FORMAT_TMSF
:
405 default: /* unknown format ! force TMSF ! ... */
406 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
407 &toc
, sizeof(toc
), &br
, NULL
))
409 if (dwFrame
< FRAME_OF_TOC(toc
, toc
.FirstTrack
) ||
410 dwFrame
> FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1)) {
411 ERR("Out of range value %u [%u,%u]\n",
412 dwFrame
, FRAME_OF_TOC(toc
, toc
.FirstTrack
),
413 FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1));
417 for (wTrack
= toc
.FirstTrack
; wTrack
<= toc
.LastTrack
; wTrack
++) {
418 if (FRAME_OF_TOC(toc
, wTrack
) > dwFrame
)
422 dwFrame
-= FRAME_OF_TOC(toc
, wTrack
);
423 wMinutes
= dwFrame
/ CDFRAMES_PERMIN
;
424 wSeconds
= (dwFrame
- CDFRAMES_PERMIN
* wMinutes
) / CDFRAMES_PERSEC
;
425 wFrames
= dwFrame
- CDFRAMES_PERMIN
* wMinutes
- CDFRAMES_PERSEC
* wSeconds
;
426 dwTime
= MCI_MAKE_TMSF(wTrack
, wMinutes
, wSeconds
, wFrames
);
427 TRACE("%02u-%02u:%02u:%02u\n", wTrack
, wMinutes
, wSeconds
, wFrames
);
428 *lpRet
= MCI_COLONIZED4_RETURN
;
434 static DWORD
MCICDA_Stop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
436 /**************************************************************************
437 * MCICDA_Open [internal]
439 static DWORD
MCICDA_Open(UINT wDevID
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpOpenParms
)
441 MCIDEVICEID dwDeviceID
;
443 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
444 WCHAR root
[7], drive
= 0;
446 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpOpenParms
);
448 if (lpOpenParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
449 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
451 dwDeviceID
= lpOpenParms
->wDeviceID
;
453 if (wmcda
->nUseCount
> 0) {
454 /* The driver is already open on this channel */
455 /* If the driver was opened shareable before and this open specifies */
456 /* shareable then increment the use count */
457 if (wmcda
->fShareable
&& (dwFlags
& MCI_OPEN_SHAREABLE
))
460 return MCIERR_MUST_USE_SHAREABLE
;
462 wmcda
->nUseCount
= 1;
463 wmcda
->fShareable
= dwFlags
& MCI_OPEN_SHAREABLE
;
465 if (dwFlags
& MCI_OPEN_ELEMENT
) {
466 if (dwFlags
& MCI_OPEN_ELEMENT_ID
) {
467 WARN("MCI_OPEN_ELEMENT_ID %p! Abort\n", lpOpenParms
->lpstrElementName
);
468 ret
= MCIERR_FLAGS_NOT_COMPATIBLE
;
471 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms
->lpstrElementName
));
472 /* Only the first letter counts since w2k
473 * Win9x-NT accept only d: and w98SE accepts d:\foobar as well.
474 * Play d:\Track03.cda plays from the first track, not #3. */
475 if (!isalpha(lpOpenParms
->lpstrElementName
[0]))
477 ret
= MCIERR_INVALID_FILE
;
480 drive
= toupper(lpOpenParms
->lpstrElementName
[0]);
481 root
[0] = drive
; root
[1] = ':'; root
[2] = '\\'; root
[3] = '\0';
482 if (GetDriveTypeW(root
) != DRIVE_CDROM
)
484 ret
= MCIERR_INVALID_FILE
;
490 root
[0] = 'A'; root
[1] = ':'; root
[2] = '\\'; root
[3] = '\0';
491 for ( ; root
[0] <= 'Z'; root
[0]++)
493 if (GetDriveTypeW(root
) == DRIVE_CDROM
)
501 ret
= MCIERR_CANNOT_LOAD_DRIVER
; /* drvOpen should return this */
506 wmcda
->wNotifyDeviceID
= dwDeviceID
;
507 wmcda
->dwTimeFormat
= MCI_FORMAT_MSF
;
509 /* now, open the handle */
510 root
[0] = root
[1] = '\\'; root
[2] = '.'; root
[3] = '\\'; root
[4] = drive
; root
[5] = ':'; root
[6] = '\0';
511 wmcda
->handle
= CreateFileW(root
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
512 if (wmcda
->handle
== INVALID_HANDLE_VALUE
)
514 ret
= MCIERR_MUST_USE_SHAREABLE
;
518 if (dwFlags
& MCI_NOTIFY
) {
519 mciDriverNotify(HWND_32(LOWORD(lpOpenParms
->dwCallback
)),
520 dwDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
529 /**************************************************************************
530 * MCICDA_Close [internal]
532 static DWORD
MCICDA_Close(UINT wDevID
, DWORD dwParam
, LPMCI_GENERIC_PARMS lpParms
)
534 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
536 TRACE("(%04X, %08X, %p);\n", wDevID
, dwParam
, lpParms
);
538 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
540 MCICDA_Stop(wDevID
, MCI_WAIT
, NULL
);
542 if (--wmcda
->nUseCount
== 0) {
543 CloseHandle(wmcda
->handle
);
545 if ((dwParam
& MCI_NOTIFY
) && lpParms
)
546 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
550 /**************************************************************************
551 * MCICDA_GetDevCaps [internal]
553 static DWORD
MCICDA_GetDevCaps(UINT wDevID
, DWORD dwFlags
,
554 LPMCI_GETDEVCAPS_PARMS lpParms
)
556 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
559 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
561 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
562 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
564 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
565 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms
->dwItem
);
567 switch (lpParms
->dwItem
) {
568 case MCI_GETDEVCAPS_CAN_RECORD
:
569 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
570 ret
= MCI_RESOURCE_RETURNED
;
572 case MCI_GETDEVCAPS_HAS_AUDIO
:
573 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
574 ret
= MCI_RESOURCE_RETURNED
;
576 case MCI_GETDEVCAPS_HAS_VIDEO
:
577 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
578 ret
= MCI_RESOURCE_RETURNED
;
580 case MCI_GETDEVCAPS_DEVICE_TYPE
:
581 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO
, MCI_DEVTYPE_CD_AUDIO
);
582 ret
= MCI_RESOURCE_RETURNED
;
584 case MCI_GETDEVCAPS_USES_FILES
:
585 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
586 ret
= MCI_RESOURCE_RETURNED
;
588 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
589 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
590 ret
= MCI_RESOURCE_RETURNED
;
592 case MCI_GETDEVCAPS_CAN_EJECT
:
593 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
594 ret
= MCI_RESOURCE_RETURNED
;
596 case MCI_GETDEVCAPS_CAN_PLAY
:
597 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
598 ret
= MCI_RESOURCE_RETURNED
;
600 case MCI_GETDEVCAPS_CAN_SAVE
:
601 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
602 ret
= MCI_RESOURCE_RETURNED
;
605 WARN("Unsupported %x devCaps item\n", lpParms
->dwItem
);
606 return MCIERR_UNSUPPORTED_FUNCTION
;
609 TRACE("No GetDevCaps-Item !\n");
610 return MCIERR_MISSING_PARAMETER
;
612 TRACE("lpParms->dwReturn=%08X;\n", lpParms
->dwReturn
);
613 if (dwFlags
& MCI_NOTIFY
) {
614 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
619 static DWORD
CDROM_Audio_GetSerial(CDROM_TOC
* toc
)
624 DWORD dwStart
, dwEnd
;
627 * wMagic collects the wFrames from track 1
628 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
630 * There it is collected for correcting the serial when there are less than
633 wMagic
= toc
->TrackData
[0].Address
[3];
634 dwStart
= FRAME_OF_TOC(*toc
, toc
->FirstTrack
);
636 for (i
= 0; i
<= toc
->LastTrack
- toc
->FirstTrack
; i
++) {
637 serial
+= (toc
->TrackData
[i
].Address
[1] << 16) |
638 (toc
->TrackData
[i
].Address
[2] << 8) | toc
->TrackData
[i
].Address
[3];
640 dwEnd
= FRAME_OF_TOC(*toc
, toc
->LastTrack
+ 1);
642 if (toc
->LastTrack
- toc
->FirstTrack
+ 1 < 3)
643 serial
+= wMagic
+ (dwEnd
- dwStart
);
649 /**************************************************************************
650 * MCICDA_Info [internal]
652 static DWORD
MCICDA_Info(UINT wDevID
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
655 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
659 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
661 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
662 return MCIERR_NULL_PARAMETER_BLOCK
;
663 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
665 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
667 if (dwFlags
& MCI_INFO_PRODUCT
) {
668 str
= L
"Wine's audio CD";
669 } else if (dwFlags
& MCI_INFO_MEDIA_UPC
) {
670 ret
= MCIERR_NO_IDENTITY
;
671 } else if (dwFlags
& MCI_INFO_MEDIA_IDENTITY
) {
676 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
677 &toc
, sizeof(toc
), &br
, NULL
)) {
678 return MCICDA_GetError(wmcda
);
681 res
= CDROM_Audio_GetSerial(&toc
);
682 swprintf(buffer
, ARRAY_SIZE(buffer
), L
"%lu", res
);
685 WARN("Don't know this info command (%u)\n", dwFlags
);
686 ret
= MCIERR_MISSING_PARAMETER
;
689 TRACE("=> %s\n", debugstr_w(str
));
690 if (lpParms
->dwRetSize
) {
691 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
692 * to the number of characters written, excluding \0. */
693 lstrcpynW(lpParms
->lpstrReturn
, str
, lpParms
->dwRetSize
);
694 } else ret
= MCIERR_PARAM_OVERFLOW
;
696 if (MMSYSERR_NOERROR
==ret
&& (dwFlags
& MCI_NOTIFY
))
697 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
701 /**************************************************************************
702 * MCICDA_Status [internal]
704 static DWORD
MCICDA_Status(UINT wDevID
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
706 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
708 CDROM_SUB_Q_DATA_FORMAT fmt
;
709 SUB_Q_CHANNEL_DATA data
;
713 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
715 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
716 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
718 if (dwFlags
& MCI_STATUS_ITEM
) {
719 TRACE("dwItem = %x\n", lpParms
->dwItem
);
720 switch (lpParms
->dwItem
) {
721 case MCI_STATUS_CURRENT_TRACK
:
722 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
723 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
724 &data
, sizeof(data
), &br
, NULL
))
726 return MCICDA_GetError(wmcda
);
727 /* alt. data.CurrentPosition.TrackNumber = 1; -- what native yields */
729 lpParms
->dwReturn
= data
.CurrentPosition
.TrackNumber
;
730 TRACE("CURRENT_TRACK=%lu\n", lpParms
->dwReturn
);
732 case MCI_STATUS_LENGTH
:
733 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
734 return MCICDA_GetError(wmcda
);
736 if (dwFlags
& MCI_TRACK
) {
737 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms
->dwTrack
);
738 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
739 return MCIERR_OUTOFRANGE
;
740 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, lpParms
->dwTrack
+ 1) -
741 FRAME_OF_TOC(toc
, lpParms
->dwTrack
);
742 /* Windows returns one frame less than the total track length for the
743 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
744 if (lpParms
->dwTrack
== toc
.LastTrack
)
747 /* Sum of the lengths of all of the tracks. Inherits the
748 'off by one frame' behavior from the length of the last track.
749 See above comment. */
750 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) -
751 FRAME_OF_TOC(toc
, toc
.FirstTrack
) - 1;
753 lpParms
->dwReturn
= MCICDA_CalcTime(wmcda
,
754 (wmcda
->dwTimeFormat
== MCI_FORMAT_TMSF
)
755 ? MCI_FORMAT_MSF
: wmcda
->dwTimeFormat
,
758 TRACE("LENGTH=%lu\n", lpParms
->dwReturn
);
760 case MCI_STATUS_MODE
:
761 lpParms
->dwReturn
= MCICDA_GetStatus(wmcda
);
762 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms
->dwReturn
);
763 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
, lpParms
->dwReturn
);
764 ret
= MCI_RESOURCE_RETURNED
;
766 case MCI_STATUS_MEDIA_PRESENT
:
767 lpParms
->dwReturn
= (MCICDA_GetStatus(wmcda
) == MCI_MODE_OPEN
) ?
768 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
769 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms
->dwReturn
) ? 'Y' : 'N');
770 ret
= MCI_RESOURCE_RETURNED
;
772 case MCI_STATUS_NUMBER_OF_TRACKS
:
773 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
774 return MCICDA_GetError(wmcda
);
776 lpParms
->dwReturn
= toc
.LastTrack
- toc
.FirstTrack
+ 1;
777 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms
->dwReturn
);
778 if (lpParms
->dwReturn
== (WORD
)-1)
779 return MCICDA_GetError(wmcda
);
781 case MCI_STATUS_POSITION
:
782 switch (dwFlags
& (MCI_STATUS_START
| MCI_TRACK
)) {
783 case MCI_STATUS_START
:
784 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
785 return MCICDA_GetError(wmcda
);
787 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, toc
.FirstTrack
);
788 TRACE("get MCI_STATUS_START !\n");
791 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
792 return MCICDA_GetError(wmcda
);
794 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
795 return MCIERR_OUTOFRANGE
;
796 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, lpParms
->dwTrack
);
797 TRACE("get MCI_TRACK #%u !\n", lpParms
->dwTrack
);
800 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
801 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
802 &data
, sizeof(data
), &br
, NULL
)) {
803 return MCICDA_GetError(wmcda
);
805 lpParms
->dwReturn
= FRAME_OF_ADDR(data
.CurrentPosition
.AbsoluteAddress
);
808 return MCIERR_FLAGS_NOT_COMPATIBLE
;
810 lpParms
->dwReturn
= MCICDA_CalcTime(wmcda
, wmcda
->dwTimeFormat
, lpParms
->dwReturn
, &ret
);
811 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms
->dwReturn
);
813 case MCI_STATUS_READY
:
814 TRACE("MCI_STATUS_READY !\n");
815 switch (MCICDA_GetStatus(wmcda
))
817 case MCI_MODE_NOT_READY
:
819 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
822 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
825 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms
->dwReturn
));
826 ret
= MCI_RESOURCE_RETURNED
;
828 case MCI_STATUS_TIME_FORMAT
:
829 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmcda
->dwTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmcda
->dwTimeFormat
);
830 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms
->dwReturn
));
831 ret
= MCI_RESOURCE_RETURNED
;
833 case 4001: /* FIXME: for bogus FullCD */
834 case MCI_CDA_STATUS_TYPE_TRACK
:
835 if (!(dwFlags
& MCI_TRACK
))
836 ret
= MCIERR_MISSING_PARAMETER
;
838 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
839 return MCICDA_GetError(wmcda
);
841 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
842 ret
= MCIERR_OUTOFRANGE
;
844 lpParms
->dwReturn
= (toc
.TrackData
[lpParms
->dwTrack
- toc
.FirstTrack
].Control
& 0x04) ?
845 MCI_CDA_TRACK_OTHER
: MCI_CDA_TRACK_AUDIO
;
846 /* FIXME: MAKEMCIRESOURCE "audio" | "other", localised */
848 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms
->dwTrack
, lpParms
->dwReturn
);
851 FIXME("unknown command %08X !\n", lpParms
->dwItem
);
852 return MCIERR_UNSUPPORTED_FUNCTION
;
854 } else return MCIERR_MISSING_PARAMETER
;
855 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
856 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
860 /**************************************************************************
861 * MCICDA_SkipDataTracks [internal]
863 static DWORD
MCICDA_SkipDataTracks(WINE_MCICDAUDIO
* wmcda
,DWORD
*frame
)
868 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
869 return MCICDA_GetError(wmcda
);
871 if (*frame
< FRAME_OF_TOC(toc
,toc
.FirstTrack
) ||
872 *frame
>= FRAME_OF_TOC(toc
,toc
.LastTrack
+1)) /* lead-out */
873 return MCIERR_OUTOFRANGE
;
874 for(i
=toc
.LastTrack
+1;i
>toc
.FirstTrack
;i
--)
875 if ( FRAME_OF_TOC(toc
, i
) <= *frame
) break;
876 /* i points to last track whose start address is not greater than frame.
877 * Now skip non-audio tracks */
878 for(;i
<=toc
.LastTrack
;i
++)
879 if ( ! (toc
.TrackData
[i
-toc
.FirstTrack
].Control
& 4) )
881 /* The frame will be an address in the next audio track or
882 * address of lead-out. */
883 if ( FRAME_OF_TOC(toc
, i
) > *frame
)
884 *frame
= FRAME_OF_TOC(toc
, i
);
885 /* Lead-out is an invalid seek position (on Linux as well). */
886 if (*frame
== FRAME_OF_TOC(toc
,toc
.LastTrack
+1))
891 /**************************************************************************
892 * MCICDA_Play [internal]
894 static DWORD
MCICDA_Play(UINT wDevID
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
896 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
897 DWORD ret
= 0, start
, end
;
900 CDROM_PLAY_AUDIO_MSF play
;
901 CDROM_SUB_Q_DATA_FORMAT fmt
;
902 SUB_Q_CHANNEL_DATA data
;
905 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
908 return MCIERR_NULL_PARAMETER_BLOCK
;
911 return MCIERR_INVALID_DEVICE_ID
;
913 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
914 return MCICDA_GetError(wmcda
);
916 if (dwFlags
& MCI_FROM
) {
917 start
= MCICDA_CalcFrame(wmcda
, lpParms
->dwFrom
);
918 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &start
)) )
920 TRACE("MCI_FROM=%08X -> %u\n", lpParms
->dwFrom
, start
);
922 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
923 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
924 &data
, sizeof(data
), &br
, NULL
)) {
925 return MCICDA_GetError(wmcda
);
927 start
= FRAME_OF_ADDR(data
.CurrentPosition
.AbsoluteAddress
);
928 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &start
)) )
931 if (dwFlags
& MCI_TO
) {
932 end
= MCICDA_CalcFrame(wmcda
, lpParms
->dwTo
);
933 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &end
)) )
935 TRACE("MCI_TO=%08X -> %u\n", lpParms
->dwTo
, end
);
937 end
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) - 1;
939 if (end
< start
) return MCIERR_OUTOFRANGE
;
940 TRACE("Playing from %u to %u\n", start
, end
);
942 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
,
943 (dwFlags
& MCI_NOTIFY
) ? HWND_32(LOWORD(lpParms
->dwCallback
)) : NULL
);
944 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
946 if (start
== end
|| start
== FRAME_OF_TOC(toc
,toc
.LastTrack
+1)-1) {
947 if (dwFlags
& MCI_NOTIFY
) {
948 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
949 if (oldcb
) mciDriverNotify(oldcb
, wDevID
, MCI_NOTIFY_SUCCESSFUL
);
951 return MMSYSERR_NOERROR
;
954 if (wmcda
->hThread
!= 0) {
955 SetEvent(wmcda
->stopEvent
);
956 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
958 CloseHandle(wmcda
->hThread
);
960 CloseHandle(wmcda
->stopEvent
);
961 wmcda
->stopEvent
= 0;
963 IDirectSoundBuffer_Stop(wmcda
->dsBuf
);
964 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
966 IDirectSound_Release(wmcda
->dsObj
);
970 if (pDirectSoundCreate
) {
977 hr
= pDirectSoundCreate(NULL
, &wmcda
->dsObj
, NULL
);
979 IDirectSound_SetCooperativeLevel(wmcda
->dsObj
, GetDesktopWindow(), DSSCL_PRIORITY
);
981 /* The "raw" frame is relative to the start of the first track */
982 wmcda
->start
= start
- FRAME_OF_TOC(toc
, toc
.FirstTrack
);
983 wmcda
->end
= end
- FRAME_OF_TOC(toc
, toc
.FirstTrack
);
985 memset(&format
, 0, sizeof(format
));
986 format
.wFormatTag
= WAVE_FORMAT_PCM
;
987 format
.nChannels
= 2;
988 format
.nSamplesPerSec
= 44100;
989 format
.wBitsPerSample
= 16;
990 format
.nBlockAlign
= format
.nChannels
* format
.wBitsPerSample
/ 8;
991 format
.nAvgBytesPerSec
= format
.nSamplesPerSec
* format
.nBlockAlign
;
994 memset(&desc
, 0, sizeof(desc
));
995 desc
.dwSize
= sizeof(desc
);
996 desc
.dwFlags
= DSBCAPS_GETCURRENTPOSITION2
| DSBCAPS_GLOBALFOCUS
;
997 desc
.dwBufferBytes
= (CDDA_FRAG_SIZE
- (CDDA_FRAG_SIZE
%RAW_SECTOR_SIZE
)) * CDDA_FRAG_COUNT
;
998 desc
.lpwfxFormat
= &format
;
1000 hr
= IDirectSound_CreateSoundBuffer(wmcda
->dsObj
, &desc
, &wmcda
->dsBuf
, NULL
);
1002 if (SUCCEEDED(hr
)) {
1003 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, 0, 0, &cdData
, &lockLen
,
1004 NULL
, NULL
, DSBLOCK_ENTIREBUFFER
);
1006 if (SUCCEEDED(hr
)) {
1007 RAW_READ_INFO rdInfo
;
1010 rdInfo
.DiskOffset
.QuadPart
= wmcda
->start
<<11;
1011 rdInfo
.SectorCount
= min(desc
.dwBufferBytes
/RAW_SECTOR_SIZE
,
1012 wmcda
->end
-wmcda
->start
);
1013 rdInfo
.TrackMode
= CDDA
;
1015 readok
= device_io(wmcda
->handle
, IOCTL_CDROM_RAW_READ
,
1016 &rdInfo
, sizeof(rdInfo
), cdData
, lockLen
,
1018 IDirectSoundBuffer_Unlock(wmcda
->dsBuf
, cdData
, lockLen
, NULL
, 0);
1021 wmcda
->start
+= rdInfo
.SectorCount
;
1022 wmcda
->stopEvent
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1024 if (wmcda
->stopEvent
!= 0)
1025 wmcda
->hThread
= CreateThread(NULL
, 0, MCICDA_playLoop
, wmcda
, 0, &br
);
1026 if (wmcda
->hThread
!= 0) {
1027 hr
= IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
);
1028 if (SUCCEEDED(hr
)) {
1029 /* FIXME: implement MCI_WAIT and send notification only in that case */
1031 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1032 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
,
1033 FAILED(hr
) ? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1038 SetEvent(wmcda
->stopEvent
);
1039 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
1040 CloseHandle(wmcda
->hThread
);
1045 if (wmcda
->stopEvent
!= 0) {
1046 CloseHandle(wmcda
->stopEvent
);
1047 wmcda
->stopEvent
= 0;
1050 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
1051 wmcda
->dsBuf
= NULL
;
1054 IDirectSound_Release(wmcda
->dsObj
);
1055 wmcda
->dsObj
= NULL
;
1059 play
.StartingM
= start
/ CDFRAMES_PERMIN
;
1060 play
.StartingS
= (start
/ CDFRAMES_PERSEC
) % 60;
1061 play
.StartingF
= start
% CDFRAMES_PERSEC
;
1062 play
.EndingM
= end
/ CDFRAMES_PERMIN
;
1063 play
.EndingS
= (end
/ CDFRAMES_PERSEC
) % 60;
1064 play
.EndingF
= end
% CDFRAMES_PERSEC
;
1065 if (!device_io(wmcda
->handle
, IOCTL_CDROM_PLAY_AUDIO_MSF
, &play
, sizeof(play
),
1066 NULL
, 0, &br
, NULL
)) {
1067 wmcda
->hCallback
= NULL
;
1068 ret
= MCIERR_HARDWARE
;
1070 /* The independent CD player has no means to signal MCI_NOTIFY when it's done.
1071 * Native sends a notification with MCI_WAIT only. */
1075 /**************************************************************************
1076 * MCICDA_Stop [internal]
1078 static DWORD
MCICDA_Stop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1080 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1084 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1086 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1088 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1089 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
1091 if (wmcda
->hThread
!= 0) {
1092 SetEvent(wmcda
->stopEvent
);
1093 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
1095 CloseHandle(wmcda
->hThread
);
1097 CloseHandle(wmcda
->stopEvent
);
1098 wmcda
->stopEvent
= 0;
1100 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
1101 wmcda
->dsBuf
= NULL
;
1102 IDirectSound_Release(wmcda
->dsObj
);
1103 wmcda
->dsObj
= NULL
;
1105 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_STOP_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1106 return MCIERR_HARDWARE
;
1108 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1109 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1113 /**************************************************************************
1114 * MCICDA_Pause [internal]
1116 static DWORD
MCICDA_Pause(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1118 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1122 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1124 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1126 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1127 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
1129 if (wmcda
->hThread
!= 0) {
1130 /* Don't bother calling stop if the playLoop thread has already stopped */
1131 if(WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
&&
1132 FAILED(IDirectSoundBuffer_Stop(wmcda
->dsBuf
)))
1133 return MCIERR_HARDWARE
;
1135 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_PAUSE_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1136 return MCIERR_HARDWARE
;
1138 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1139 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1143 /**************************************************************************
1144 * MCICDA_Resume [internal]
1146 static DWORD
MCICDA_Resume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1148 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1151 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1153 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1155 if (wmcda
->hThread
!= 0) {
1156 /* Don't restart if the playLoop thread has already stopped */
1157 if(WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
&&
1158 FAILED(IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
)))
1159 return MCIERR_HARDWARE
;
1161 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_RESUME_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1162 return MCIERR_HARDWARE
;
1164 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1165 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1169 /**************************************************************************
1170 * MCICDA_Seek [internal]
1172 static DWORD
MCICDA_Seek(UINT wDevID
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1175 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1176 CDROM_SEEK_AUDIO_MSF seek
;
1177 DWORD br
, position
, ret
;
1180 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1182 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1183 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1185 position
= dwFlags
& (MCI_SEEK_TO_START
|MCI_SEEK_TO_END
|MCI_TO
);
1186 if (!position
) return MCIERR_MISSING_PARAMETER
;
1187 if (position
&(position
-1)) return MCIERR_FLAGS_NOT_COMPATIBLE
;
1189 /* Stop sends MCI_NOTIFY_ABORTED when needed.
1190 * Tests show that native first sends ABORTED and reads the TOC,
1191 * then only checks the position flags, then stops and seeks. */
1192 MCICDA_Stop(wDevID
, MCI_WAIT
, 0);
1194 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
1195 return MCICDA_GetError(wmcda
);
1198 case MCI_SEEK_TO_START
:
1199 TRACE("Seeking to start\n");
1200 at
= FRAME_OF_TOC(toc
,toc
.FirstTrack
);
1201 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1204 case MCI_SEEK_TO_END
:
1205 TRACE("Seeking to end\n");
1206 /* End is prior to lead-out
1207 * yet Win9X seeks to even one frame less than that. */
1208 at
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) - 1;
1209 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1213 TRACE("Seeking to %u\n", lpParms
->dwTo
);
1214 at
= MCICDA_CalcFrame(wmcda
, lpParms
->dwTo
);
1215 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1219 return MCIERR_FLAGS_NOT_COMPATIBLE
;
1223 seek
.M
= at
/ CDFRAMES_PERMIN
;
1224 seek
.S
= (at
/ CDFRAMES_PERSEC
) % 60;
1225 seek
.F
= at
% CDFRAMES_PERSEC
;
1226 if (!device_io(wmcda
->handle
, IOCTL_CDROM_SEEK_AUDIO_MSF
, &seek
, sizeof(seek
),
1227 NULL
, 0, &br
, NULL
))
1228 return MCIERR_HARDWARE
;
1231 if (dwFlags
& MCI_NOTIFY
)
1232 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1236 /**************************************************************************
1237 * MCICDA_SetDoor [internal]
1239 static DWORD
MCICDA_SetDoor(UINT wDevID
, BOOL open
)
1241 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1244 TRACE("(%04x, %s) !\n", wDevID
, (open
) ? "OPEN" : "CLOSE");
1246 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1248 if (!device_io(wmcda
->handle
,
1249 (open
) ? IOCTL_STORAGE_EJECT_MEDIA
: IOCTL_STORAGE_LOAD_MEDIA
,
1250 NULL
, 0, NULL
, 0, &br
, NULL
))
1251 return MCIERR_HARDWARE
;
1256 /**************************************************************************
1257 * MCICDA_Set [internal]
1259 static DWORD
MCICDA_Set(UINT wDevID
, DWORD dwFlags
, LPMCI_SET_PARMS lpParms
)
1261 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1263 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1265 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1267 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1268 MCICDA_SetDoor(wDevID
, TRUE
);
1270 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1271 MCICDA_SetDoor(wDevID
, FALSE
);
1274 /* only functions which require valid lpParms below this line ! */
1275 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1277 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1279 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1280 switch (lpParms
->dwTimeFormat
) {
1281 case MCI_FORMAT_MILLISECONDS
:
1282 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1284 case MCI_FORMAT_MSF
:
1285 TRACE("MCI_FORMAT_MSF !\n");
1287 case MCI_FORMAT_TMSF
:
1288 TRACE("MCI_FORMAT_TMSF !\n");
1291 return MCIERR_BAD_TIME_FORMAT
;
1293 wmcda
->dwTimeFormat
= lpParms
->dwTimeFormat
;
1295 if (dwFlags
& MCI_SET_AUDIO
) /* one xp machine ignored it */
1296 TRACE("SET_AUDIO %X %x\n", dwFlags
, lpParms
->dwAudio
);
1298 if (dwFlags
& MCI_NOTIFY
)
1299 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1303 /**************************************************************************
1304 * DriverProc (MCICDA.@)
1306 LRESULT CALLBACK
MCICDA_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1307 LPARAM dwParam1
, LPARAM dwParam2
)
1310 case DRV_LOAD
: return 1;
1311 case DRV_FREE
: return 1;
1312 case DRV_OPEN
: return MCICDA_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1313 case DRV_CLOSE
: return MCICDA_drvClose(dwDevID
);
1314 case DRV_ENABLE
: return 1;
1315 case DRV_DISABLE
: return 1;
1316 case DRV_QUERYCONFIGURE
: return 1;
1317 case DRV_CONFIGURE
: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK
); return 1;
1318 case DRV_INSTALL
: return DRVCNF_RESTART
;
1319 case DRV_REMOVE
: return DRVCNF_RESTART
;
1322 if (dwDevID
== 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION
;
1325 case MCI_OPEN_DRIVER
: return MCICDA_Open(dwDevID
, dwParam1
, (LPMCI_OPEN_PARMSW
)dwParam2
);
1326 case MCI_CLOSE_DRIVER
: return MCICDA_Close(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1327 case MCI_GETDEVCAPS
: return MCICDA_GetDevCaps(dwDevID
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1328 case MCI_INFO
: return MCICDA_Info(dwDevID
, dwParam1
, (LPMCI_INFO_PARMSW
)dwParam2
);
1329 case MCI_STATUS
: return MCICDA_Status(dwDevID
, dwParam1
, (LPMCI_STATUS_PARMS
)dwParam2
);
1330 case MCI_SET
: return MCICDA_Set(dwDevID
, dwParam1
, (LPMCI_SET_PARMS
)dwParam2
);
1331 case MCI_PLAY
: return MCICDA_Play(dwDevID
, dwParam1
, (LPMCI_PLAY_PARMS
)dwParam2
);
1332 case MCI_STOP
: return MCICDA_Stop(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1333 case MCI_PAUSE
: return MCICDA_Pause(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1334 case MCI_RESUME
: return MCICDA_Resume(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1335 case MCI_SEEK
: return MCICDA_Seek(dwDevID
, dwParam1
, (LPMCI_SEEK_PARMS
)dwParam2
);
1336 /* commands that should report an error as they are not supported in
1337 * the native version */
1341 return MCIERR_UNSUPPORTED_FUNCTION
;
1357 TRACE("Unsupported command [0x%x]\n", wMsg
);
1361 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1364 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg
);
1365 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1367 return MCIERR_UNRECOGNIZED_COMMAND
;
1370 /*-----------------------------------------------------------------------*/