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
28 #define WIN32_NO_STATUS
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(mcicda
);
44 #define CDFRAMES_PERSEC 75
45 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
46 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
47 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
49 /* Defined by red-book standard; do not change! */
50 #define RAW_SECTOR_SIZE (2352)
52 /* Must be >= RAW_SECTOR_SIZE */
53 #define CDDA_FRAG_SIZE (32768)
55 #define CDDA_FRAG_COUNT (3)
59 int nUseCount
; /* Incremented for each shared open */
60 BOOL fShareable
; /* TRUE if first open was shareable */
61 MCIDEVICEID wNotifyDeviceID
; /* MCI device ID with a pending notification */
62 HANDLE hCallback
; /* Callback handle for pending notification */
66 /* The following are used for digital playback only */
72 IDirectSoundBuffer
*dsBuf
;
77 /*-----------------------------------------------------------------------*/
79 typedef HRESULT(WINAPI
*LPDIRECTSOUNDCREATE
)(LPCGUID
,LPDIRECTSOUND
*,LPUNKNOWN
);
80 static LPDIRECTSOUNDCREATE pDirectSoundCreate
;
82 static DWORD CALLBACK
MCICDA_playLoop(void *ptr
)
84 WINE_MCICDAUDIO
*wmcda
= (WINE_MCICDAUDIO
*)ptr
;
85 DWORD lastPos
, curPos
, endPos
, br
;
87 DWORD lockLen
, fragLen
;
92 memset(&caps
, 0, sizeof(caps
));
93 caps
.dwSize
= sizeof(caps
);
94 hr
= IDirectSoundBuffer_GetCaps(wmcda
->dsBuf
, &caps
);
96 fragLen
= caps
.dwBufferBytes
/CDDA_FRAG_COUNT
;
99 while (SUCCEEDED(hr
) && endPos
!= lastPos
&&
100 WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
) {
101 hr
= IDirectSoundBuffer_GetCurrentPosition(wmcda
->dsBuf
, &curPos
, NULL
);
102 if ((curPos
-lastPos
+caps
.dwBufferBytes
)%caps
.dwBufferBytes
< fragLen
) {
107 EnterCriticalSection(&wmcda
->cs
);
108 rdInfo
.DiskOffset
.QuadPart
= wmcda
->start
<<11;
109 rdInfo
.SectorCount
= min(fragLen
/RAW_SECTOR_SIZE
, wmcda
->end
-wmcda
->start
);
110 rdInfo
.TrackMode
= CDDA
;
112 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, lastPos
, fragLen
, &cdData
, &lockLen
, NULL
, NULL
, 0);
113 if (hr
== DSERR_BUFFERLOST
) {
114 if(FAILED(IDirectSoundBuffer_Restore(wmcda
->dsBuf
)) ||
115 FAILED(IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
))) {
116 LeaveCriticalSection(&wmcda
->cs
);
119 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, lastPos
, fragLen
, &cdData
, &lockLen
, NULL
, NULL
, 0);
123 if (rdInfo
.SectorCount
> 0) {
124 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_RAW_READ
, &rdInfo
, sizeof(rdInfo
), cdData
, lockLen
, &br
, NULL
))
125 WARN("CD read failed at sector %d: 0x%x\n", wmcda
->start
, GetLastError());
127 if (rdInfo
.SectorCount
*RAW_SECTOR_SIZE
< lockLen
) {
128 if(endPos
== ~0u) endPos
= lastPos
;
129 memset((BYTE
*)cdData
+ rdInfo
.SectorCount
*RAW_SECTOR_SIZE
, 0,
130 lockLen
- rdInfo
.SectorCount
*RAW_SECTOR_SIZE
);
132 hr
= IDirectSoundBuffer_Unlock(wmcda
->dsBuf
, cdData
, lockLen
, NULL
, 0);
136 lastPos
%= caps
.dwBufferBytes
;
137 wmcda
->start
+= rdInfo
.SectorCount
;
139 LeaveCriticalSection(&wmcda
->cs
);
141 IDirectSoundBuffer_Stop(wmcda
->dsBuf
);
142 SetEvent(wmcda
->stopEvent
);
144 /* A design bug in native: the independent CD player called by the
145 * MCI has no means to signal end of playing, therefore the MCI
146 * notification is left hanging. MCI_NOTIFY_SUPERSEDED will be
147 * signaled by the next command that has MCI_NOTIFY set (or
148 * MCI_NOTIFY_ABORTED for MCI_PLAY). */
155 /**************************************************************************
156 * MCICDA_drvOpen [internal]
158 static DWORD
MCICDA_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
160 static HMODULE dsHandle
;
161 WINE_MCICDAUDIO
* wmcda
;
163 if (!modp
) return 0xFFFFFFFF;
164 /* FIXME: MCIERR_CANNOT_LOAD_DRIVER if there's no drive of type CD-ROM */
166 wmcda
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCICDAUDIO
));
172 dsHandle
= LoadLibraryA("dsound.dll");
174 pDirectSoundCreate
= (LPDIRECTSOUNDCREATE
)GetProcAddress(dsHandle
, "DirectSoundCreate");
177 wmcda
->wDevID
= modp
->wDeviceID
;
178 mciSetDriverData(wmcda
->wDevID
, (DWORD_PTR
)wmcda
);
179 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
180 modp
->wType
= MCI_DEVTYPE_CD_AUDIO
;
181 InitializeCriticalSection(&wmcda
->cs
);
182 return modp
->wDeviceID
;
185 /**************************************************************************
186 * MCICDA_drvClose [internal]
188 static DWORD
MCICDA_drvClose(DWORD dwDevID
)
190 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(dwDevID
);
193 DeleteCriticalSection(&wmcda
->cs
);
194 HeapFree(GetProcessHeap(), 0, wmcda
);
195 mciSetDriverData(dwDevID
, 0);
197 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
200 /**************************************************************************
201 * MCICDA_GetOpenDrv [internal]
203 static WINE_MCICDAUDIO
* MCICDA_GetOpenDrv(UINT wDevID
)
205 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
207 if (wmcda
== NULL
|| wmcda
->nUseCount
== 0) {
208 WARN("Invalid wDevID=%u\n", wDevID
);
214 /**************************************************************************
215 * MCICDA_mciNotify [internal]
217 * Notifications in MCI work like a 1-element queue.
218 * Each new notification request supersedes the previous one.
220 static void MCICDA_Notify(DWORD_PTR hWndCallBack
, WINE_MCICDAUDIO
* wmcda
, UINT wStatus
)
222 MCIDEVICEID wDevID
= wmcda
->wNotifyDeviceID
;
223 HANDLE old
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
224 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
225 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
228 /**************************************************************************
229 * MCICDA_GetStatus [internal]
231 static DWORD
MCICDA_GetStatus(WINE_MCICDAUDIO
* wmcda
)
233 CDROM_SUB_Q_DATA_FORMAT fmt
;
234 SUB_Q_CHANNEL_DATA data
;
236 DWORD mode
= MCI_MODE_NOT_READY
;
238 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
239 if(wmcda
->hThread
!= 0) {
243 hr
= IDirectSoundBuffer_GetStatus(wmcda
->dsBuf
, &status
);
245 if(!(status
&DSBSTATUS_PLAYING
)) {
246 if(WaitForSingleObject(wmcda
->stopEvent
, 0) == WAIT_OBJECT_0
)
247 mode
= MCI_MODE_STOP
;
249 mode
= MCI_MODE_PAUSE
;
252 mode
= MCI_MODE_PLAY
;
255 else if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
256 &data
, sizeof(data
), &br
, NULL
)) {
257 if (GetLastError() == ERROR_NOT_READY
) mode
= MCI_MODE_OPEN
;
259 switch (data
.CurrentPosition
.Header
.AudioStatus
)
261 case AUDIO_STATUS_IN_PROGRESS
: mode
= MCI_MODE_PLAY
; break;
262 case AUDIO_STATUS_PAUSED
: mode
= MCI_MODE_PAUSE
; break;
263 case AUDIO_STATUS_NO_STATUS
:
264 case AUDIO_STATUS_PLAY_COMPLETE
: mode
= MCI_MODE_STOP
; break;
265 case AUDIO_STATUS_PLAY_ERROR
:
266 case AUDIO_STATUS_NOT_SUPPORTED
:
274 /**************************************************************************
275 * MCICDA_GetError [internal]
277 static int MCICDA_GetError(WINE_MCICDAUDIO
* wmcda
)
279 switch (GetLastError())
281 case ERROR_NOT_READY
: return MCIERR_DEVICE_NOT_READY
;
282 case ERROR_NOT_SUPPORTED
:
283 case ERROR_IO_DEVICE
: return MCIERR_HARDWARE
;
285 FIXME("Unknown mode %u\n", GetLastError());
287 return MCIERR_DRIVER_INTERNAL
;
290 /**************************************************************************
291 * MCICDA_CalcFrame [internal]
293 static DWORD
MCICDA_CalcFrame(WINE_MCICDAUDIO
* wmcda
, DWORD dwTime
)
301 TRACE("(%p, %08X, %u);\n", wmcda
, wmcda
->dwTimeFormat
, dwTime
);
303 switch (wmcda
->dwTimeFormat
) {
304 case MCI_FORMAT_MILLISECONDS
:
305 dwFrame
= ((dwTime
- 1) * CDFRAMES_PERSEC
+ 500) / 1000;
306 TRACE("MILLISECONDS %u\n", dwFrame
);
309 TRACE("MSF %02u:%02u:%02u\n",
310 MCI_MSF_MINUTE(dwTime
), MCI_MSF_SECOND(dwTime
), MCI_MSF_FRAME(dwTime
));
311 dwFrame
+= CDFRAMES_PERMIN
* MCI_MSF_MINUTE(dwTime
);
312 dwFrame
+= CDFRAMES_PERSEC
* MCI_MSF_SECOND(dwTime
);
313 dwFrame
+= MCI_MSF_FRAME(dwTime
);
315 case MCI_FORMAT_TMSF
:
316 default: /* unknown format ! force TMSF ! ... */
317 wTrack
= MCI_TMSF_TRACK(dwTime
);
318 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
319 &toc
, sizeof(toc
), &br
, NULL
))
321 if (wTrack
< toc
.FirstTrack
|| wTrack
> toc
.LastTrack
)
323 TRACE("MSF %02u-%02u:%02u:%02u\n",
324 MCI_TMSF_TRACK(dwTime
), MCI_TMSF_MINUTE(dwTime
),
325 MCI_TMSF_SECOND(dwTime
), MCI_TMSF_FRAME(dwTime
));
326 addr
= toc
.TrackData
[wTrack
- toc
.FirstTrack
].Address
;
327 TRACE("TMSF trackpos[%u]=%d:%d:%d\n",
328 wTrack
, addr
[1], addr
[2], addr
[3]);
329 dwFrame
= CDFRAMES_PERMIN
* (addr
[1] + MCI_TMSF_MINUTE(dwTime
)) +
330 CDFRAMES_PERSEC
* (addr
[2] + MCI_TMSF_SECOND(dwTime
)) +
331 addr
[3] + MCI_TMSF_FRAME(dwTime
);
337 /**************************************************************************
338 * MCICDA_CalcTime [internal]
340 static DWORD
MCICDA_CalcTime(WINE_MCICDAUDIO
* wmcda
, DWORD tf
, DWORD dwFrame
, LPDWORD lpRet
)
350 TRACE("(%p, %08X, %u);\n", wmcda
, tf
, dwFrame
);
353 case MCI_FORMAT_MILLISECONDS
:
354 dwTime
= (dwFrame
* 1000) / CDFRAMES_PERSEC
+ 1;
355 TRACE("MILLISECONDS %u\n", dwTime
);
359 wMinutes
= dwFrame
/ CDFRAMES_PERMIN
;
360 wSeconds
= (dwFrame
- CDFRAMES_PERMIN
* wMinutes
) / CDFRAMES_PERSEC
;
361 wFrames
= dwFrame
- CDFRAMES_PERMIN
* wMinutes
- CDFRAMES_PERSEC
* wSeconds
;
362 dwTime
= MCI_MAKE_MSF(wMinutes
, wSeconds
, wFrames
);
363 TRACE("MSF %02u:%02u:%02u -> dwTime=%u\n",
364 wMinutes
, wSeconds
, wFrames
, dwTime
);
365 *lpRet
= MCI_COLONIZED3_RETURN
;
367 case MCI_FORMAT_TMSF
:
368 default: /* unknown format ! force TMSF ! ... */
369 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
370 &toc
, sizeof(toc
), &br
, NULL
))
372 if (dwFrame
< FRAME_OF_TOC(toc
, toc
.FirstTrack
) ||
373 dwFrame
> FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1)) {
374 ERR("Out of range value %u [%u,%u]\n",
375 dwFrame
, FRAME_OF_TOC(toc
, toc
.FirstTrack
),
376 FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1));
380 for (wTrack
= toc
.FirstTrack
; wTrack
<= toc
.LastTrack
; wTrack
++) {
381 if (FRAME_OF_TOC(toc
, wTrack
) > dwFrame
)
385 dwFrame
-= FRAME_OF_TOC(toc
, wTrack
);
386 wMinutes
= dwFrame
/ CDFRAMES_PERMIN
;
387 wSeconds
= (dwFrame
- CDFRAMES_PERMIN
* wMinutes
) / CDFRAMES_PERSEC
;
388 wFrames
= dwFrame
- CDFRAMES_PERMIN
* wMinutes
- CDFRAMES_PERSEC
* wSeconds
;
389 dwTime
= MCI_MAKE_TMSF(wTrack
, wMinutes
, wSeconds
, wFrames
);
390 TRACE("%02u-%02u:%02u:%02u\n", wTrack
, wMinutes
, wSeconds
, wFrames
);
391 *lpRet
= MCI_COLONIZED4_RETURN
;
397 static DWORD
MCICDA_Stop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
399 /**************************************************************************
400 * MCICDA_Open [internal]
402 static DWORD
MCICDA_Open(UINT wDevID
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpOpenParms
)
404 MCIDEVICEID dwDeviceID
;
406 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
407 WCHAR root
[7], drive
= 0;
409 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpOpenParms
);
411 if (lpOpenParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
412 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
414 dwDeviceID
= lpOpenParms
->wDeviceID
;
416 if (wmcda
->nUseCount
> 0) {
417 /* The driver is already open on this channel */
418 /* If the driver was opened shareable before and this open specifies */
419 /* shareable then increment the use count */
420 if (wmcda
->fShareable
&& (dwFlags
& MCI_OPEN_SHAREABLE
))
423 return MCIERR_MUST_USE_SHAREABLE
;
425 wmcda
->nUseCount
= 1;
426 wmcda
->fShareable
= dwFlags
& MCI_OPEN_SHAREABLE
;
428 if (dwFlags
& MCI_OPEN_ELEMENT
) {
429 if (dwFlags
& MCI_OPEN_ELEMENT_ID
) {
430 WARN("MCI_OPEN_ELEMENT_ID %p! Abort\n", lpOpenParms
->lpstrElementName
);
431 ret
= MCIERR_FLAGS_NOT_COMPATIBLE
;
434 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms
->lpstrElementName
));
435 /* Only the first letter counts since w2k
436 * Win9x-NT accept only d: and w98SE accepts d:\foobar as well.
437 * Play d:\Track03.cda plays from the first track, not #3. */
438 if (!isalpha(lpOpenParms
->lpstrElementName
[0]))
440 ret
= MCIERR_INVALID_FILE
;
443 drive
= toupper(lpOpenParms
->lpstrElementName
[0]);
444 root
[0] = drive
; root
[1] = ':'; root
[2] = '\\'; root
[3] = '\0';
445 if (GetDriveTypeW(root
) != DRIVE_CDROM
)
447 ret
= MCIERR_INVALID_FILE
;
453 root
[0] = 'A'; root
[1] = ':'; root
[2] = '\\'; root
[3] = '\0';
454 for ( ; root
[0] <= 'Z'; root
[0]++)
456 if (GetDriveTypeW(root
) == DRIVE_CDROM
)
464 ret
= MCIERR_CANNOT_LOAD_DRIVER
; /* drvOpen should return this */
469 wmcda
->wNotifyDeviceID
= dwDeviceID
;
470 wmcda
->dwTimeFormat
= MCI_FORMAT_MSF
;
472 /* now, open the handle */
473 root
[0] = root
[1] = '\\'; root
[2] = '.'; root
[3] = '\\'; root
[4] = drive
; root
[5] = ':'; root
[6] = '\0';
474 wmcda
->handle
= CreateFileW(root
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
475 if (wmcda
->handle
== INVALID_HANDLE_VALUE
)
477 ret
= MCIERR_MUST_USE_SHAREABLE
;
481 if (dwFlags
& MCI_NOTIFY
) {
482 mciDriverNotify(HWND_32(LOWORD(lpOpenParms
->dwCallback
)),
483 dwDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
492 /**************************************************************************
493 * MCICDA_Close [internal]
495 static DWORD
MCICDA_Close(UINT wDevID
, DWORD dwParam
, LPMCI_GENERIC_PARMS lpParms
)
497 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
499 TRACE("(%04X, %08X, %p);\n", wDevID
, dwParam
, lpParms
);
501 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
503 MCICDA_Stop(wDevID
, MCI_WAIT
, NULL
);
505 if (--wmcda
->nUseCount
== 0) {
506 CloseHandle(wmcda
->handle
);
508 if ((dwParam
& MCI_NOTIFY
) && lpParms
)
509 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
513 /**************************************************************************
514 * MCICDA_GetDevCaps [internal]
516 static DWORD
MCICDA_GetDevCaps(UINT wDevID
, DWORD dwFlags
,
517 LPMCI_GETDEVCAPS_PARMS lpParms
)
519 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
522 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
524 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
525 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
527 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
528 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms
->dwItem
);
530 switch (lpParms
->dwItem
) {
531 case MCI_GETDEVCAPS_CAN_RECORD
:
532 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
533 ret
= MCI_RESOURCE_RETURNED
;
535 case MCI_GETDEVCAPS_HAS_AUDIO
:
536 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
537 ret
= MCI_RESOURCE_RETURNED
;
539 case MCI_GETDEVCAPS_HAS_VIDEO
:
540 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
541 ret
= MCI_RESOURCE_RETURNED
;
543 case MCI_GETDEVCAPS_DEVICE_TYPE
:
544 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO
, MCI_DEVTYPE_CD_AUDIO
);
545 ret
= MCI_RESOURCE_RETURNED
;
547 case MCI_GETDEVCAPS_USES_FILES
:
548 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
549 ret
= MCI_RESOURCE_RETURNED
;
551 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
552 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
553 ret
= MCI_RESOURCE_RETURNED
;
555 case MCI_GETDEVCAPS_CAN_EJECT
:
556 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
557 ret
= MCI_RESOURCE_RETURNED
;
559 case MCI_GETDEVCAPS_CAN_PLAY
:
560 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
561 ret
= MCI_RESOURCE_RETURNED
;
563 case MCI_GETDEVCAPS_CAN_SAVE
:
564 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
565 ret
= MCI_RESOURCE_RETURNED
;
568 WARN("Unsupported %x devCaps item\n", lpParms
->dwItem
);
569 return MCIERR_UNSUPPORTED_FUNCTION
;
572 TRACE("No GetDevCaps-Item !\n");
573 return MCIERR_MISSING_PARAMETER
;
575 TRACE("lpParms->dwReturn=%08X;\n", lpParms
->dwReturn
);
576 if (dwFlags
& MCI_NOTIFY
) {
577 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
582 static DWORD
CDROM_Audio_GetSerial(CDROM_TOC
* toc
)
587 DWORD dwStart
, dwEnd
;
590 * wMagic collects the wFrames from track 1
591 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
593 * There it is collected for correcting the serial when there are less than
596 wMagic
= toc
->TrackData
[0].Address
[3];
597 dwStart
= FRAME_OF_TOC(*toc
, toc
->FirstTrack
);
599 for (i
= 0; i
<= toc
->LastTrack
- toc
->FirstTrack
; i
++) {
600 serial
+= (toc
->TrackData
[i
].Address
[1] << 16) |
601 (toc
->TrackData
[i
].Address
[2] << 8) | toc
->TrackData
[i
].Address
[3];
603 dwEnd
= FRAME_OF_TOC(*toc
, toc
->LastTrack
+ 1);
605 if (toc
->LastTrack
- toc
->FirstTrack
+ 1 < 3)
606 serial
+= wMagic
+ (dwEnd
- dwStart
);
612 /**************************************************************************
613 * MCICDA_Info [internal]
615 static DWORD
MCICDA_Info(UINT wDevID
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
618 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
622 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
624 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
625 return MCIERR_NULL_PARAMETER_BLOCK
;
626 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
628 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
630 if (dwFlags
& MCI_INFO_PRODUCT
) {
631 static const WCHAR wszAudioCd
[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
633 } else if (dwFlags
& MCI_INFO_MEDIA_UPC
) {
634 ret
= MCIERR_NO_IDENTITY
;
635 } else if (dwFlags
& MCI_INFO_MEDIA_IDENTITY
) {
639 static const WCHAR wszLu
[] = {'%','l','u',0};
641 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
642 &toc
, sizeof(toc
), &br
, NULL
)) {
643 return MCICDA_GetError(wmcda
);
646 res
= CDROM_Audio_GetSerial(&toc
);
647 sprintfW(buffer
, wszLu
, res
);
650 WARN("Don't know this info command (%u)\n", dwFlags
);
651 ret
= MCIERR_MISSING_PARAMETER
;
654 if (lpParms
->dwRetSize
<= strlenW(str
)) {
655 lstrcpynW(lpParms
->lpstrReturn
, str
, lpParms
->dwRetSize
- 1);
656 ret
= MCIERR_PARAM_OVERFLOW
;
658 strcpyW(lpParms
->lpstrReturn
, str
);
661 *lpParms
->lpstrReturn
= 0;
663 TRACE("=> %s (%d)\n", debugstr_w(lpParms
->lpstrReturn
), ret
);
665 if (MMSYSERR_NOERROR
==ret
&& (dwFlags
& MCI_NOTIFY
))
666 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
670 /**************************************************************************
671 * MCICDA_Status [internal]
673 static DWORD
MCICDA_Status(UINT wDevID
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
675 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
677 CDROM_SUB_Q_DATA_FORMAT fmt
;
678 SUB_Q_CHANNEL_DATA data
;
682 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
684 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
685 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
687 if (dwFlags
& MCI_STATUS_ITEM
) {
688 TRACE("dwItem = %x\n", lpParms
->dwItem
);
689 switch (lpParms
->dwItem
) {
690 case MCI_STATUS_CURRENT_TRACK
:
691 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
692 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
693 &data
, sizeof(data
), &br
, NULL
))
695 return MCICDA_GetError(wmcda
);
696 /* alt. data.CurrentPosition.TrackNumber = 1; -- what native yields */
698 lpParms
->dwReturn
= data
.CurrentPosition
.TrackNumber
;
699 TRACE("CURRENT_TRACK=%lu\n", lpParms
->dwReturn
);
701 case MCI_STATUS_LENGTH
:
702 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
703 &toc
, sizeof(toc
), &br
, NULL
)) {
704 WARN("error reading TOC !\n");
705 return MCICDA_GetError(wmcda
);
707 if (dwFlags
& MCI_TRACK
) {
708 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms
->dwTrack
);
709 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
710 return MCIERR_OUTOFRANGE
;
711 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, lpParms
->dwTrack
+ 1) -
712 FRAME_OF_TOC(toc
, lpParms
->dwTrack
);
713 /* Windows returns one frame less than the total track length for the
714 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
715 if (lpParms
->dwTrack
== toc
.LastTrack
)
718 /* Sum of the lengths of all of the tracks. Inherits the
719 'off by one frame' behavior from the length of the last track.
720 See above comment. */
721 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) -
722 FRAME_OF_TOC(toc
, toc
.FirstTrack
) - 1;
724 lpParms
->dwReturn
= MCICDA_CalcTime(wmcda
,
725 (wmcda
->dwTimeFormat
== MCI_FORMAT_TMSF
)
726 ? MCI_FORMAT_MSF
: wmcda
->dwTimeFormat
,
729 TRACE("LENGTH=%lu\n", lpParms
->dwReturn
);
731 case MCI_STATUS_MODE
:
732 lpParms
->dwReturn
= MCICDA_GetStatus(wmcda
);
733 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms
->dwReturn
);
734 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
, lpParms
->dwReturn
);
735 ret
= MCI_RESOURCE_RETURNED
;
737 case MCI_STATUS_MEDIA_PRESENT
:
738 lpParms
->dwReturn
= (MCICDA_GetStatus(wmcda
) == MCI_MODE_OPEN
) ?
739 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
740 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms
->dwReturn
) ? 'Y' : 'N');
741 ret
= MCI_RESOURCE_RETURNED
;
743 case MCI_STATUS_NUMBER_OF_TRACKS
:
744 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
745 &toc
, sizeof(toc
), &br
, NULL
)) {
746 WARN("error reading TOC !\n");
747 return MCICDA_GetError(wmcda
);
749 lpParms
->dwReturn
= toc
.LastTrack
- toc
.FirstTrack
+ 1;
750 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms
->dwReturn
);
751 if (lpParms
->dwReturn
== (WORD
)-1)
752 return MCICDA_GetError(wmcda
);
754 case MCI_STATUS_POSITION
:
755 switch (dwFlags
& (MCI_STATUS_START
| MCI_TRACK
)) {
756 case MCI_STATUS_START
:
757 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
758 &toc
, sizeof(toc
), &br
, NULL
)) {
759 WARN("error reading TOC !\n");
760 return MCICDA_GetError(wmcda
);
762 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, toc
.FirstTrack
);
763 TRACE("get MCI_STATUS_START !\n");
766 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
767 &toc
, sizeof(toc
), &br
, NULL
)) {
768 WARN("error reading TOC !\n");
769 return MCICDA_GetError(wmcda
);
771 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
772 return MCIERR_OUTOFRANGE
;
773 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, lpParms
->dwTrack
);
774 TRACE("get MCI_TRACK #%u !\n", lpParms
->dwTrack
);
777 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
778 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
779 &data
, sizeof(data
), &br
, NULL
)) {
780 return MCICDA_GetError(wmcda
);
782 lpParms
->dwReturn
= FRAME_OF_ADDR(data
.CurrentPosition
.AbsoluteAddress
);
785 return MCIERR_FLAGS_NOT_COMPATIBLE
;
787 lpParms
->dwReturn
= MCICDA_CalcTime(wmcda
, wmcda
->dwTimeFormat
, lpParms
->dwReturn
, &ret
);
788 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms
->dwReturn
);
790 case MCI_STATUS_READY
:
791 TRACE("MCI_STATUS_READY !\n");
792 switch (MCICDA_GetStatus(wmcda
))
794 case MCI_MODE_NOT_READY
:
796 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
799 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
802 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms
->dwReturn
));
803 ret
= MCI_RESOURCE_RETURNED
;
805 case MCI_STATUS_TIME_FORMAT
:
806 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmcda
->dwTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmcda
->dwTimeFormat
);
807 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms
->dwReturn
));
808 ret
= MCI_RESOURCE_RETURNED
;
810 case 4001: /* FIXME: for bogus FullCD */
811 case MCI_CDA_STATUS_TYPE_TRACK
:
812 if (!(dwFlags
& MCI_TRACK
))
813 ret
= MCIERR_MISSING_PARAMETER
;
815 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
816 &toc
, sizeof(toc
), &br
, NULL
)) {
817 WARN("error reading TOC !\n");
818 return MCICDA_GetError(wmcda
);
820 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
821 ret
= MCIERR_OUTOFRANGE
;
823 lpParms
->dwReturn
= (toc
.TrackData
[lpParms
->dwTrack
- toc
.FirstTrack
].Control
& 0x04) ?
824 MCI_CDA_TRACK_OTHER
: MCI_CDA_TRACK_AUDIO
;
825 /* FIXME: MAKEMCIRESOURCE "audio" | "other", localised */
827 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms
->dwTrack
, lpParms
->dwReturn
);
830 FIXME("unknown command %08X !\n", lpParms
->dwItem
);
831 return MCIERR_UNSUPPORTED_FUNCTION
;
833 } else return MCIERR_MISSING_PARAMETER
;
834 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
835 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
839 /**************************************************************************
840 * MCICDA_SkipDataTracks [internal]
842 static DWORD
MCICDA_SkipDataTracks(WINE_MCICDAUDIO
* wmcda
,DWORD
*frame
)
847 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
848 &toc
, sizeof(toc
), &br
, NULL
)) {
849 WARN("error reading TOC !\n");
850 return MCICDA_GetError(wmcda
);
852 if (*frame
< FRAME_OF_TOC(toc
,toc
.FirstTrack
) ||
853 *frame
>= FRAME_OF_TOC(toc
,toc
.LastTrack
+1)) /* lead-out */
854 return MCIERR_OUTOFRANGE
;
855 for(i
=toc
.LastTrack
+1;i
>toc
.FirstTrack
;i
--)
856 if ( FRAME_OF_TOC(toc
, i
) <= *frame
) break;
857 /* i points to last track whose start address is not greater than frame.
858 * Now skip non-audio tracks */
859 for(;i
<=toc
.LastTrack
;i
++)
860 if ( ! (toc
.TrackData
[i
-toc
.FirstTrack
].Control
& 4) )
862 /* The frame will be an address in the next audio track or
863 * address of lead-out. */
864 if ( FRAME_OF_TOC(toc
, i
) > *frame
)
865 *frame
= FRAME_OF_TOC(toc
, i
);
866 /* Lead-out is an invalid seek position (on Linux as well). */
867 if (*frame
== FRAME_OF_TOC(toc
,toc
.LastTrack
+1))
872 /**************************************************************************
873 * MCICDA_Play [internal]
875 static DWORD
MCICDA_Play(UINT wDevID
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
877 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
878 DWORD ret
= 0, start
, end
;
881 CDROM_PLAY_AUDIO_MSF play
;
882 CDROM_SUB_Q_DATA_FORMAT fmt
;
883 SUB_Q_CHANNEL_DATA data
;
886 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
889 return MCIERR_NULL_PARAMETER_BLOCK
;
892 return MCIERR_INVALID_DEVICE_ID
;
894 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
895 &toc
, sizeof(toc
), &br
, NULL
)) {
896 WARN("error reading TOC !\n");
897 return MCICDA_GetError(wmcda
);
900 if (dwFlags
& MCI_FROM
) {
901 start
= MCICDA_CalcFrame(wmcda
, lpParms
->dwFrom
);
902 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &start
)) )
904 TRACE("MCI_FROM=%08X -> %u\n", lpParms
->dwFrom
, start
);
906 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
907 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
908 &data
, sizeof(data
), &br
, NULL
)) {
909 return MCICDA_GetError(wmcda
);
911 start
= FRAME_OF_ADDR(data
.CurrentPosition
.AbsoluteAddress
);
912 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &start
)) )
915 if (dwFlags
& MCI_TO
) {
916 end
= MCICDA_CalcFrame(wmcda
, lpParms
->dwTo
);
917 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &end
)) )
919 TRACE("MCI_TO=%08X -> %u\n", lpParms
->dwTo
, end
);
921 end
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) - 1;
923 if (end
< start
) return MCIERR_OUTOFRANGE
;
924 TRACE("Playing from %u to %u\n", start
, end
);
926 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
,
927 (dwFlags
& MCI_NOTIFY
) ? HWND_32(LOWORD(lpParms
->dwCallback
)) : NULL
);
928 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
930 if (start
== end
|| start
== FRAME_OF_TOC(toc
,toc
.LastTrack
+1)-1) {
931 if (dwFlags
& MCI_NOTIFY
) {
932 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
933 if (oldcb
) mciDriverNotify(oldcb
, wDevID
, MCI_NOTIFY_SUCCESSFUL
);
935 return MMSYSERR_NOERROR
;
938 if (wmcda
->hThread
!= 0) {
939 SetEvent(wmcda
->stopEvent
);
940 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
942 CloseHandle(wmcda
->hThread
);
944 CloseHandle(wmcda
->stopEvent
);
945 wmcda
->stopEvent
= 0;
947 IDirectSoundBuffer_Stop(wmcda
->dsBuf
);
948 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
950 IDirectSound_Release(wmcda
->dsObj
);
954 if (pDirectSoundCreate
) {
961 hr
= pDirectSoundCreate(NULL
, &wmcda
->dsObj
, NULL
);
963 IDirectSound_SetCooperativeLevel(wmcda
->dsObj
, GetDesktopWindow(), DSSCL_PRIORITY
);
965 /* The "raw" frame is relative to the start of the first track */
966 wmcda
->start
= start
- FRAME_OF_TOC(toc
, toc
.FirstTrack
);
967 wmcda
->end
= end
- FRAME_OF_TOC(toc
, toc
.FirstTrack
);
969 memset(&format
, 0, sizeof(format
));
970 format
.wFormatTag
= WAVE_FORMAT_PCM
;
971 format
.nChannels
= 2;
972 format
.nSamplesPerSec
= 44100;
973 format
.wBitsPerSample
= 16;
974 format
.nBlockAlign
= format
.nChannels
* format
.wBitsPerSample
/ 8;
975 format
.nAvgBytesPerSec
= format
.nSamplesPerSec
* format
.nBlockAlign
;
978 memset(&desc
, 0, sizeof(desc
));
979 desc
.dwSize
= sizeof(desc
);
980 desc
.dwFlags
= DSBCAPS_GETCURRENTPOSITION2
| DSBCAPS_GLOBALFOCUS
;
981 desc
.dwBufferBytes
= (CDDA_FRAG_SIZE
- (CDDA_FRAG_SIZE
%RAW_SECTOR_SIZE
)) * CDDA_FRAG_COUNT
;
982 desc
.lpwfxFormat
= &format
;
984 hr
= IDirectSound_CreateSoundBuffer(wmcda
->dsObj
, &desc
, &wmcda
->dsBuf
, NULL
);
987 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, 0, 0, &cdData
, &lockLen
,
988 NULL
, NULL
, DSBLOCK_ENTIREBUFFER
);
991 RAW_READ_INFO rdInfo
;
994 rdInfo
.DiskOffset
.QuadPart
= wmcda
->start
<<11;
995 rdInfo
.SectorCount
= min(desc
.dwBufferBytes
/RAW_SECTOR_SIZE
,
996 wmcda
->end
-wmcda
->start
);
997 rdInfo
.TrackMode
= CDDA
;
999 readok
= DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_RAW_READ
,
1000 &rdInfo
, sizeof(rdInfo
), cdData
, lockLen
,
1002 IDirectSoundBuffer_Unlock(wmcda
->dsBuf
, cdData
, lockLen
, NULL
, 0);
1005 wmcda
->start
+= rdInfo
.SectorCount
;
1006 wmcda
->stopEvent
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1008 if (wmcda
->stopEvent
!= 0)
1009 wmcda
->hThread
= CreateThread(NULL
, 0, MCICDA_playLoop
, wmcda
, 0, &br
);
1010 if (wmcda
->hThread
!= 0) {
1011 hr
= IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
);
1012 if (SUCCEEDED(hr
)) {
1013 /* FIXME: implement MCI_WAIT and send notification only in that case */
1015 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1016 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
,
1017 FAILED(hr
) ? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1022 SetEvent(wmcda
->stopEvent
);
1023 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
1024 CloseHandle(wmcda
->hThread
);
1029 if (wmcda
->stopEvent
!= 0) {
1030 CloseHandle(wmcda
->stopEvent
);
1031 wmcda
->stopEvent
= 0;
1034 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
1035 wmcda
->dsBuf
= NULL
;
1038 IDirectSound_Release(wmcda
->dsObj
);
1039 wmcda
->dsObj
= NULL
;
1043 play
.StartingM
= start
/ CDFRAMES_PERMIN
;
1044 play
.StartingS
= (start
/ CDFRAMES_PERSEC
) % 60;
1045 play
.StartingF
= start
% CDFRAMES_PERSEC
;
1046 play
.EndingM
= end
/ CDFRAMES_PERMIN
;
1047 play
.EndingS
= (end
/ CDFRAMES_PERSEC
) % 60;
1048 play
.EndingF
= end
% CDFRAMES_PERSEC
;
1049 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_PLAY_AUDIO_MSF
, &play
, sizeof(play
),
1050 NULL
, 0, &br
, NULL
)) {
1051 wmcda
->hCallback
= NULL
;
1052 ret
= MCIERR_HARDWARE
;
1054 /* The independent CD player has no means to signal MCI_NOTIFY when it's done.
1055 * Native sends a notification with MCI_WAIT only. */
1059 /**************************************************************************
1060 * MCICDA_Stop [internal]
1062 static DWORD
MCICDA_Stop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1064 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1068 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1070 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1072 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1073 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
1075 if (wmcda
->hThread
!= 0) {
1076 SetEvent(wmcda
->stopEvent
);
1077 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
1079 CloseHandle(wmcda
->hThread
);
1081 CloseHandle(wmcda
->stopEvent
);
1082 wmcda
->stopEvent
= 0;
1084 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
1085 wmcda
->dsBuf
= NULL
;
1086 IDirectSound_Release(wmcda
->dsObj
);
1087 wmcda
->dsObj
= NULL
;
1089 else if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_STOP_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1090 return MCIERR_HARDWARE
;
1092 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1093 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1097 /**************************************************************************
1098 * MCICDA_Pause [internal]
1100 static DWORD
MCICDA_Pause(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1102 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1106 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1108 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1110 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1111 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
1113 if (wmcda
->hThread
!= 0) {
1114 /* Don't bother calling stop if the playLoop thread has already stopped */
1115 if(WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
&&
1116 FAILED(IDirectSoundBuffer_Stop(wmcda
->dsBuf
)))
1117 return MCIERR_HARDWARE
;
1119 else if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_PAUSE_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1120 return MCIERR_HARDWARE
;
1122 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1123 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1127 /**************************************************************************
1128 * MCICDA_Resume [internal]
1130 static DWORD
MCICDA_Resume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1132 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1135 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1137 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1139 if (wmcda
->hThread
!= 0) {
1140 /* Don't restart if the playLoop thread has already stopped */
1141 if(WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
&&
1142 FAILED(IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
)))
1143 return MCIERR_HARDWARE
;
1145 else if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_RESUME_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1146 return MCIERR_HARDWARE
;
1148 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1149 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1153 /**************************************************************************
1154 * MCICDA_Seek [internal]
1156 static DWORD
MCICDA_Seek(UINT wDevID
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1159 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1160 CDROM_SEEK_AUDIO_MSF seek
;
1161 DWORD br
, position
, ret
;
1164 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1166 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1167 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1169 position
= dwFlags
& (MCI_SEEK_TO_START
|MCI_SEEK_TO_END
|MCI_TO
);
1170 if (!position
) return MCIERR_MISSING_PARAMETER
;
1171 if (position
&(position
-1)) return MCIERR_FLAGS_NOT_COMPATIBLE
;
1173 /* Stop sends MCI_NOTIFY_ABORTED when needed.
1174 * Tests show that native first sends ABORTED and reads the TOC,
1175 * then only checks the position flags, then stops and seeks. */
1176 MCICDA_Stop(wDevID
, MCI_WAIT
, 0);
1178 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
1179 &toc
, sizeof(toc
), &br
, NULL
)) {
1180 WARN("error reading TOC !\n");
1181 return MCICDA_GetError(wmcda
);
1184 case MCI_SEEK_TO_START
:
1185 TRACE("Seeking to start\n");
1186 at
= FRAME_OF_TOC(toc
,toc
.FirstTrack
);
1187 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1190 case MCI_SEEK_TO_END
:
1191 TRACE("Seeking to end\n");
1192 /* End is prior to lead-out
1193 * yet Win9X seeks to even one frame less than that. */
1194 at
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) - 1;
1195 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1199 TRACE("Seeking to %u\n", lpParms
->dwTo
);
1200 at
= MCICDA_CalcFrame(wmcda
, lpParms
->dwTo
);
1201 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1205 return MCIERR_FLAGS_NOT_COMPATIBLE
;
1209 seek
.M
= at
/ CDFRAMES_PERMIN
;
1210 seek
.S
= (at
/ CDFRAMES_PERSEC
) % 60;
1211 seek
.F
= at
% CDFRAMES_PERSEC
;
1212 if (!DeviceIoControl(wmcda
->handle
, IOCTL_CDROM_SEEK_AUDIO_MSF
, &seek
, sizeof(seek
),
1213 NULL
, 0, &br
, NULL
))
1214 return MCIERR_HARDWARE
;
1217 if (dwFlags
& MCI_NOTIFY
)
1218 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1222 /**************************************************************************
1223 * MCICDA_SetDoor [internal]
1225 static DWORD
MCICDA_SetDoor(UINT wDevID
, BOOL open
)
1227 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1230 TRACE("(%04x, %s) !\n", wDevID
, (open
) ? "OPEN" : "CLOSE");
1232 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1234 if (!DeviceIoControl(wmcda
->handle
,
1235 (open
) ? IOCTL_STORAGE_EJECT_MEDIA
: IOCTL_STORAGE_LOAD_MEDIA
,
1236 NULL
, 0, NULL
, 0, &br
, NULL
))
1237 return MCIERR_HARDWARE
;
1242 /**************************************************************************
1243 * MCICDA_Set [internal]
1245 static DWORD
MCICDA_Set(UINT wDevID
, DWORD dwFlags
, LPMCI_SET_PARMS lpParms
)
1247 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1249 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1251 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1253 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1254 MCICDA_SetDoor(wDevID
, TRUE
);
1256 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1257 MCICDA_SetDoor(wDevID
, FALSE
);
1260 /* only functions which require valid lpParms below this line ! */
1261 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1263 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1265 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1266 switch (lpParms
->dwTimeFormat
) {
1267 case MCI_FORMAT_MILLISECONDS
:
1268 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1270 case MCI_FORMAT_MSF
:
1271 TRACE("MCI_FORMAT_MSF !\n");
1273 case MCI_FORMAT_TMSF
:
1274 TRACE("MCI_FORMAT_TMSF !\n");
1277 return MCIERR_BAD_TIME_FORMAT
;
1279 wmcda
->dwTimeFormat
= lpParms
->dwTimeFormat
;
1281 if (dwFlags
& MCI_SET_AUDIO
) /* one xp machine ignored it */
1282 TRACE("SET_AUDIO %X %x\n", dwFlags
, lpParms
->dwAudio
);
1284 if (dwFlags
& MCI_NOTIFY
)
1285 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1289 /**************************************************************************
1290 * DriverProc (MCICDA.@)
1292 LRESULT CALLBACK
MCICDA_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1293 LPARAM dwParam1
, LPARAM dwParam2
)
1296 case DRV_LOAD
: return 1;
1297 case DRV_FREE
: return 1;
1298 case DRV_OPEN
: return MCICDA_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1299 case DRV_CLOSE
: return MCICDA_drvClose(dwDevID
);
1300 case DRV_ENABLE
: return 1;
1301 case DRV_DISABLE
: return 1;
1302 case DRV_QUERYCONFIGURE
: return 1;
1303 case DRV_CONFIGURE
: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK
); return 1;
1304 case DRV_INSTALL
: return DRVCNF_RESTART
;
1305 case DRV_REMOVE
: return DRVCNF_RESTART
;
1308 if (dwDevID
== 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION
;
1311 case MCI_OPEN_DRIVER
: return MCICDA_Open(dwDevID
, dwParam1
, (LPMCI_OPEN_PARMSW
)dwParam2
);
1312 case MCI_CLOSE_DRIVER
: return MCICDA_Close(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1313 case MCI_GETDEVCAPS
: return MCICDA_GetDevCaps(dwDevID
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1314 case MCI_INFO
: return MCICDA_Info(dwDevID
, dwParam1
, (LPMCI_INFO_PARMSW
)dwParam2
);
1315 case MCI_STATUS
: return MCICDA_Status(dwDevID
, dwParam1
, (LPMCI_STATUS_PARMS
)dwParam2
);
1316 case MCI_SET
: return MCICDA_Set(dwDevID
, dwParam1
, (LPMCI_SET_PARMS
)dwParam2
);
1317 case MCI_PLAY
: return MCICDA_Play(dwDevID
, dwParam1
, (LPMCI_PLAY_PARMS
)dwParam2
);
1318 case MCI_STOP
: return MCICDA_Stop(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1319 case MCI_PAUSE
: return MCICDA_Pause(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1320 case MCI_RESUME
: return MCICDA_Resume(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1321 case MCI_SEEK
: return MCICDA_Seek(dwDevID
, dwParam1
, (LPMCI_SEEK_PARMS
)dwParam2
);
1322 /* commands that should report an error as they are not supported in
1323 * the native version */
1327 return MCIERR_UNSUPPORTED_FUNCTION
;
1343 TRACE("Unsupported command [0x%x]\n", wMsg
);
1347 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1350 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg
);
1351 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1353 return MCIERR_UNRECOGNIZED_COMMAND
;
1356 /*-----------------------------------------------------------------------*/