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
30 #define COM_NO_WINDOWS_H
32 //#include "winbase.h"
39 //#include "winternl.h"
40 #include <wine/debug.h>
41 #include <wine/unicode.h>
44 WINE_DEFAULT_DEBUG_CHANNEL(mcicda
);
46 #define CDFRAMES_PERSEC 75
47 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
48 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
49 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
51 /* Defined by red-book standard; do not change! */
52 #define RAW_SECTOR_SIZE (2352)
54 /* Must be >= RAW_SECTOR_SIZE */
55 #define CDDA_FRAG_SIZE (32768)
57 #define CDDA_FRAG_COUNT (3)
61 int nUseCount
; /* Incremented for each shared open */
62 BOOL fShareable
; /* TRUE if first open was shareable */
63 MCIDEVICEID wNotifyDeviceID
; /* MCI device ID with a pending notification */
64 HANDLE hCallback
; /* Callback handle for pending notification */
68 /* The following are used for digital playback only */
74 IDirectSoundBuffer
*dsBuf
;
79 /*-----------------------------------------------------------------------*/
81 typedef HRESULT(WINAPI
*LPDIRECTSOUNDCREATE
)(LPCGUID
,LPDIRECTSOUND
*,LPUNKNOWN
);
82 static LPDIRECTSOUNDCREATE pDirectSoundCreate
;
84 static BOOL
device_io(HANDLE dev
, DWORD code
, void *inbuffer
, DWORD insize
, void *outbuffer
, DWORD outsize
, DWORD
*retsize
, OVERLAPPED
*overlapped
)
87 BOOL ret
= DeviceIoControl(dev
, code
, inbuffer
, insize
, outbuffer
, outsize
, retsize
, overlapped
);
89 #define XX(x) case (x): str = #x; break
92 XX(IOCTL_CDROM_RAW_READ
);
93 XX(IOCTL_CDROM_READ_TOC
);
94 XX(IOCTL_CDROM_READ_Q_CHANNEL
);
95 XX(IOCTL_CDROM_SEEK_AUDIO_MSF
);
96 XX(IOCTL_CDROM_PLAY_AUDIO_MSF
);
97 XX(IOCTL_CDROM_STOP_AUDIO
);
98 XX(IOCTL_CDROM_PAUSE_AUDIO
);
99 XX(IOCTL_CDROM_RESUME_AUDIO
);
100 XX(IOCTL_STORAGE_EJECT_MEDIA
);
101 XX(IOCTL_STORAGE_LOAD_MEDIA
);
102 default: str
= wine_dbg_sprintf("UNKNOWN (0x%x)", code
);
105 TRACE("Device %p, Code %s -> Return %d, Bytes %u\n", dev
, str
, ret
, *retsize
);
109 static DWORD CALLBACK
MCICDA_playLoop(void *ptr
)
111 WINE_MCICDAUDIO
*wmcda
= (WINE_MCICDAUDIO
*)ptr
;
112 DWORD lastPos
, curPos
, endPos
, br
;
114 DWORD lockLen
, fragLen
;
116 RAW_READ_INFO rdInfo
;
119 memset(&caps
, 0, sizeof(caps
));
120 caps
.dwSize
= sizeof(caps
);
121 hr
= IDirectSoundBuffer_GetCaps(wmcda
->dsBuf
, &caps
);
123 fragLen
= caps
.dwBufferBytes
/CDDA_FRAG_COUNT
;
124 curPos
= lastPos
= 0;
126 while (SUCCEEDED(hr
) && endPos
!= lastPos
&&
127 WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
) {
128 hr
= IDirectSoundBuffer_GetCurrentPosition(wmcda
->dsBuf
, &curPos
, NULL
);
129 if ((curPos
-lastPos
+caps
.dwBufferBytes
)%caps
.dwBufferBytes
< fragLen
) {
134 EnterCriticalSection(&wmcda
->cs
);
135 rdInfo
.DiskOffset
.QuadPart
= wmcda
->start
<<11;
136 rdInfo
.SectorCount
= min(fragLen
/RAW_SECTOR_SIZE
, wmcda
->end
-wmcda
->start
);
137 rdInfo
.TrackMode
= CDDA
;
139 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, lastPos
, fragLen
, &cdData
, &lockLen
, NULL
, NULL
, 0);
140 if (hr
== DSERR_BUFFERLOST
) {
141 if(FAILED(IDirectSoundBuffer_Restore(wmcda
->dsBuf
)) ||
142 FAILED(IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
))) {
143 LeaveCriticalSection(&wmcda
->cs
);
146 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, lastPos
, fragLen
, &cdData
, &lockLen
, NULL
, NULL
, 0);
150 if (rdInfo
.SectorCount
> 0) {
151 if (!device_io(wmcda
->handle
, IOCTL_CDROM_RAW_READ
, &rdInfo
, sizeof(rdInfo
), cdData
, lockLen
, &br
, NULL
))
152 WARN("CD read failed at sector %d: 0x%x\n", wmcda
->start
, GetLastError());
154 if (rdInfo
.SectorCount
*RAW_SECTOR_SIZE
< lockLen
) {
155 if(endPos
== ~0u) endPos
= lastPos
;
156 memset((BYTE
*)cdData
+ rdInfo
.SectorCount
*RAW_SECTOR_SIZE
, 0,
157 lockLen
- rdInfo
.SectorCount
*RAW_SECTOR_SIZE
);
159 hr
= IDirectSoundBuffer_Unlock(wmcda
->dsBuf
, cdData
, lockLen
, NULL
, 0);
163 lastPos
%= caps
.dwBufferBytes
;
164 wmcda
->start
+= rdInfo
.SectorCount
;
166 LeaveCriticalSection(&wmcda
->cs
);
168 IDirectSoundBuffer_Stop(wmcda
->dsBuf
);
169 SetEvent(wmcda
->stopEvent
);
171 /* A design bug in native: the independent CD player called by the
172 * MCI has no means to signal end of playing, therefore the MCI
173 * notification is left hanging. MCI_NOTIFY_SUPERSEDED will be
174 * signaled by the next command that has MCI_NOTIFY set (or
175 * MCI_NOTIFY_ABORTED for MCI_PLAY). */
182 /**************************************************************************
183 * MCICDA_drvOpen [internal]
185 static DWORD
MCICDA_drvOpen(LPCWSTR str
, LPMCI_OPEN_DRIVER_PARMSW modp
)
187 static HMODULE dsHandle
;
188 WINE_MCICDAUDIO
* wmcda
;
190 if (!modp
) return 0xFFFFFFFF;
191 /* FIXME: MCIERR_CANNOT_LOAD_DRIVER if there's no drive of type CD-ROM */
193 wmcda
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WINE_MCICDAUDIO
));
199 dsHandle
= LoadLibraryA("dsound.dll");
201 pDirectSoundCreate
= (LPDIRECTSOUNDCREATE
)GetProcAddress(dsHandle
, "DirectSoundCreate");
204 wmcda
->wDevID
= modp
->wDeviceID
;
205 mciSetDriverData(wmcda
->wDevID
, (DWORD_PTR
)wmcda
);
206 modp
->wCustomCommandTable
= MCI_NO_COMMAND_TABLE
;
207 modp
->wType
= MCI_DEVTYPE_CD_AUDIO
;
208 InitializeCriticalSection(&wmcda
->cs
);
209 wmcda
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": WINE_MCICDAUDIO.cs");
210 return modp
->wDeviceID
;
213 /**************************************************************************
214 * MCICDA_drvClose [internal]
216 static DWORD
MCICDA_drvClose(DWORD dwDevID
)
218 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(dwDevID
);
221 wmcda
->cs
.DebugInfo
->Spare
[0] = 0;
222 DeleteCriticalSection(&wmcda
->cs
);
223 HeapFree(GetProcessHeap(), 0, wmcda
);
224 mciSetDriverData(dwDevID
, 0);
226 return (dwDevID
== 0xFFFFFFFF) ? 1 : 0;
229 /**************************************************************************
230 * MCICDA_GetOpenDrv [internal]
232 static WINE_MCICDAUDIO
* MCICDA_GetOpenDrv(UINT wDevID
)
234 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
236 if (wmcda
== NULL
|| wmcda
->nUseCount
== 0) {
237 WARN("Invalid wDevID=%u\n", wDevID
);
243 /**************************************************************************
244 * MCICDA_mciNotify [internal]
246 * Notifications in MCI work like a 1-element queue.
247 * Each new notification request supersedes the previous one.
249 static void MCICDA_Notify(DWORD_PTR hWndCallBack
, WINE_MCICDAUDIO
* wmcda
, UINT wStatus
)
251 MCIDEVICEID wDevID
= wmcda
->wNotifyDeviceID
;
252 HANDLE old
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
253 if (old
) mciDriverNotify(old
, wDevID
, MCI_NOTIFY_SUPERSEDED
);
254 mciDriverNotify(HWND_32(LOWORD(hWndCallBack
)), wDevID
, wStatus
);
257 /**************************************************************************
258 * MCICDA_ReadTOC [internal]
260 static BOOL
MCICDA_ReadTOC(WINE_MCICDAUDIO
* wmcda
, CDROM_TOC
*toc
, DWORD
*br
)
262 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0, toc
, sizeof(*toc
), br
, NULL
)) {
263 WARN("error reading TOC !\n");
269 /**************************************************************************
270 * MCICDA_GetStatus [internal]
272 static DWORD
MCICDA_GetStatus(WINE_MCICDAUDIO
* wmcda
)
274 CDROM_SUB_Q_DATA_FORMAT fmt
;
275 SUB_Q_CHANNEL_DATA data
;
277 DWORD mode
= MCI_MODE_NOT_READY
;
279 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
280 if(wmcda
->hThread
!= 0) {
284 hr
= IDirectSoundBuffer_GetStatus(wmcda
->dsBuf
, &status
);
286 if(!(status
&DSBSTATUS_PLAYING
)) {
287 if(WaitForSingleObject(wmcda
->stopEvent
, 0) == WAIT_OBJECT_0
)
288 mode
= MCI_MODE_STOP
;
290 mode
= MCI_MODE_PAUSE
;
293 mode
= MCI_MODE_PLAY
;
296 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
297 &data
, sizeof(data
), &br
, NULL
)) {
298 if (GetLastError() == ERROR_NOT_READY
) mode
= MCI_MODE_OPEN
;
300 switch (data
.CurrentPosition
.Header
.AudioStatus
)
302 case AUDIO_STATUS_IN_PROGRESS
: mode
= MCI_MODE_PLAY
; break;
303 case AUDIO_STATUS_PAUSED
: mode
= MCI_MODE_PAUSE
; break;
304 case AUDIO_STATUS_NO_STATUS
:
305 case AUDIO_STATUS_PLAY_COMPLETE
: mode
= MCI_MODE_STOP
; break;
306 case AUDIO_STATUS_PLAY_ERROR
:
307 case AUDIO_STATUS_NOT_SUPPORTED
:
315 /**************************************************************************
316 * MCICDA_GetError [internal]
318 static int MCICDA_GetError(WINE_MCICDAUDIO
* wmcda
)
320 switch (GetLastError())
322 case ERROR_NOT_READY
: return MCIERR_DEVICE_NOT_READY
;
323 case ERROR_NOT_SUPPORTED
:
324 case ERROR_IO_DEVICE
: return MCIERR_HARDWARE
;
326 FIXME("Unknown mode %u\n", GetLastError());
328 return MCIERR_DRIVER_INTERNAL
;
331 /**************************************************************************
332 * MCICDA_CalcFrame [internal]
334 static DWORD
MCICDA_CalcFrame(WINE_MCICDAUDIO
* wmcda
, DWORD dwTime
)
342 TRACE("(%p, %08X, %u);\n", wmcda
, wmcda
->dwTimeFormat
, dwTime
);
344 switch (wmcda
->dwTimeFormat
) {
345 case MCI_FORMAT_MILLISECONDS
:
346 dwFrame
= ((dwTime
- 1) * CDFRAMES_PERSEC
+ 500) / 1000;
347 TRACE("MILLISECONDS %u\n", dwFrame
);
350 TRACE("MSF %02u:%02u:%02u\n",
351 MCI_MSF_MINUTE(dwTime
), MCI_MSF_SECOND(dwTime
), MCI_MSF_FRAME(dwTime
));
352 dwFrame
+= CDFRAMES_PERMIN
* MCI_MSF_MINUTE(dwTime
);
353 dwFrame
+= CDFRAMES_PERSEC
* MCI_MSF_SECOND(dwTime
);
354 dwFrame
+= MCI_MSF_FRAME(dwTime
);
356 case MCI_FORMAT_TMSF
:
357 default: /* unknown format ! force TMSF ! ... */
358 wTrack
= MCI_TMSF_TRACK(dwTime
);
359 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
360 &toc
, sizeof(toc
), &br
, NULL
))
362 if (wTrack
< toc
.FirstTrack
|| wTrack
> toc
.LastTrack
)
364 TRACE("MSF %02u-%02u:%02u:%02u\n",
365 MCI_TMSF_TRACK(dwTime
), MCI_TMSF_MINUTE(dwTime
),
366 MCI_TMSF_SECOND(dwTime
), MCI_TMSF_FRAME(dwTime
));
367 addr
= toc
.TrackData
[wTrack
- toc
.FirstTrack
].Address
;
368 TRACE("TMSF trackpos[%u]=%d:%d:%d\n",
369 wTrack
, addr
[1], addr
[2], addr
[3]);
370 dwFrame
= CDFRAMES_PERMIN
* (addr
[1] + MCI_TMSF_MINUTE(dwTime
)) +
371 CDFRAMES_PERSEC
* (addr
[2] + MCI_TMSF_SECOND(dwTime
)) +
372 addr
[3] + MCI_TMSF_FRAME(dwTime
);
378 /**************************************************************************
379 * MCICDA_CalcTime [internal]
381 static DWORD
MCICDA_CalcTime(WINE_MCICDAUDIO
* wmcda
, DWORD tf
, DWORD dwFrame
, LPDWORD lpRet
)
391 TRACE("(%p, %08X, %u);\n", wmcda
, tf
, dwFrame
);
394 case MCI_FORMAT_MILLISECONDS
:
395 dwTime
= (dwFrame
* 1000) / CDFRAMES_PERSEC
+ 1;
396 TRACE("MILLISECONDS %u\n", dwTime
);
400 wMinutes
= dwFrame
/ CDFRAMES_PERMIN
;
401 wSeconds
= (dwFrame
- CDFRAMES_PERMIN
* wMinutes
) / CDFRAMES_PERSEC
;
402 wFrames
= dwFrame
- CDFRAMES_PERMIN
* wMinutes
- CDFRAMES_PERSEC
* wSeconds
;
403 dwTime
= MCI_MAKE_MSF(wMinutes
, wSeconds
, wFrames
);
404 TRACE("MSF %02u:%02u:%02u -> dwTime=%u\n",
405 wMinutes
, wSeconds
, wFrames
, dwTime
);
406 *lpRet
= MCI_COLONIZED3_RETURN
;
408 case MCI_FORMAT_TMSF
:
409 default: /* unknown format ! force TMSF ! ... */
410 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
411 &toc
, sizeof(toc
), &br
, NULL
))
413 if (dwFrame
< FRAME_OF_TOC(toc
, toc
.FirstTrack
) ||
414 dwFrame
> FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1)) {
415 ERR("Out of range value %u [%u,%u]\n",
416 dwFrame
, FRAME_OF_TOC(toc
, toc
.FirstTrack
),
417 FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1));
421 for (wTrack
= toc
.FirstTrack
; wTrack
<= toc
.LastTrack
; wTrack
++) {
422 if (FRAME_OF_TOC(toc
, wTrack
) > dwFrame
)
426 dwFrame
-= FRAME_OF_TOC(toc
, wTrack
);
427 wMinutes
= dwFrame
/ CDFRAMES_PERMIN
;
428 wSeconds
= (dwFrame
- CDFRAMES_PERMIN
* wMinutes
) / CDFRAMES_PERSEC
;
429 wFrames
= dwFrame
- CDFRAMES_PERMIN
* wMinutes
- CDFRAMES_PERSEC
* wSeconds
;
430 dwTime
= MCI_MAKE_TMSF(wTrack
, wMinutes
, wSeconds
, wFrames
);
431 TRACE("%02u-%02u:%02u:%02u\n", wTrack
, wMinutes
, wSeconds
, wFrames
);
432 *lpRet
= MCI_COLONIZED4_RETURN
;
438 static DWORD
MCICDA_Stop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
);
440 /**************************************************************************
441 * MCICDA_Open [internal]
443 static DWORD
MCICDA_Open(UINT wDevID
, DWORD dwFlags
, LPMCI_OPEN_PARMSW lpOpenParms
)
445 MCIDEVICEID dwDeviceID
;
447 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
448 WCHAR root
[7], drive
= 0;
450 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpOpenParms
);
452 if (lpOpenParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
453 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
455 dwDeviceID
= lpOpenParms
->wDeviceID
;
457 if (wmcda
->nUseCount
> 0) {
458 /* The driver is already open on this channel */
459 /* If the driver was opened shareable before and this open specifies */
460 /* shareable then increment the use count */
461 if (wmcda
->fShareable
&& (dwFlags
& MCI_OPEN_SHAREABLE
))
464 return MCIERR_MUST_USE_SHAREABLE
;
466 wmcda
->nUseCount
= 1;
467 wmcda
->fShareable
= dwFlags
& MCI_OPEN_SHAREABLE
;
469 if (dwFlags
& MCI_OPEN_ELEMENT
) {
470 if (dwFlags
& MCI_OPEN_ELEMENT_ID
) {
471 WARN("MCI_OPEN_ELEMENT_ID %p! Abort\n", lpOpenParms
->lpstrElementName
);
472 ret
= MCIERR_FLAGS_NOT_COMPATIBLE
;
475 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms
->lpstrElementName
));
476 /* Only the first letter counts since w2k
477 * Win9x-NT accept only d: and w98SE accepts d:\foobar as well.
478 * Play d:\Track03.cda plays from the first track, not #3. */
479 if (!isalpha(lpOpenParms
->lpstrElementName
[0]))
481 ret
= MCIERR_INVALID_FILE
;
484 drive
= toupper(lpOpenParms
->lpstrElementName
[0]);
485 root
[0] = drive
; root
[1] = ':'; root
[2] = '\\'; root
[3] = '\0';
486 if (GetDriveTypeW(root
) != DRIVE_CDROM
)
488 ret
= MCIERR_INVALID_FILE
;
494 root
[0] = 'A'; root
[1] = ':'; root
[2] = '\\'; root
[3] = '\0';
495 for ( ; root
[0] <= 'Z'; root
[0]++)
497 if (GetDriveTypeW(root
) == DRIVE_CDROM
)
505 ret
= MCIERR_CANNOT_LOAD_DRIVER
; /* drvOpen should return this */
510 wmcda
->wNotifyDeviceID
= dwDeviceID
;
511 wmcda
->dwTimeFormat
= MCI_FORMAT_MSF
;
513 /* now, open the handle */
514 root
[0] = root
[1] = '\\'; root
[2] = '.'; root
[3] = '\\'; root
[4] = drive
; root
[5] = ':'; root
[6] = '\0';
515 wmcda
->handle
= CreateFileW(root
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, 0);
516 if (wmcda
->handle
== INVALID_HANDLE_VALUE
)
518 ret
= MCIERR_MUST_USE_SHAREABLE
;
522 if (dwFlags
& MCI_NOTIFY
) {
523 mciDriverNotify(HWND_32(LOWORD(lpOpenParms
->dwCallback
)),
524 dwDeviceID
, MCI_NOTIFY_SUCCESSFUL
);
533 /**************************************************************************
534 * MCICDA_Close [internal]
536 static DWORD
MCICDA_Close(UINT wDevID
, DWORD dwParam
, LPMCI_GENERIC_PARMS lpParms
)
538 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
540 TRACE("(%04X, %08X, %p);\n", wDevID
, dwParam
, lpParms
);
542 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
544 MCICDA_Stop(wDevID
, MCI_WAIT
, NULL
);
546 if (--wmcda
->nUseCount
== 0) {
547 CloseHandle(wmcda
->handle
);
549 if ((dwParam
& MCI_NOTIFY
) && lpParms
)
550 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
554 /**************************************************************************
555 * MCICDA_GetDevCaps [internal]
557 static DWORD
MCICDA_GetDevCaps(UINT wDevID
, DWORD dwFlags
,
558 LPMCI_GETDEVCAPS_PARMS lpParms
)
560 WINE_MCICDAUDIO
* wmcda
= (WINE_MCICDAUDIO
*)mciGetDriverData(wDevID
);
563 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
565 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
566 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
568 if (dwFlags
& MCI_GETDEVCAPS_ITEM
) {
569 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms
->dwItem
);
571 switch (lpParms
->dwItem
) {
572 case MCI_GETDEVCAPS_CAN_RECORD
:
573 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
574 ret
= MCI_RESOURCE_RETURNED
;
576 case MCI_GETDEVCAPS_HAS_AUDIO
:
577 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
578 ret
= MCI_RESOURCE_RETURNED
;
580 case MCI_GETDEVCAPS_HAS_VIDEO
:
581 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
582 ret
= MCI_RESOURCE_RETURNED
;
584 case MCI_GETDEVCAPS_DEVICE_TYPE
:
585 lpParms
->dwReturn
= MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO
, MCI_DEVTYPE_CD_AUDIO
);
586 ret
= MCI_RESOURCE_RETURNED
;
588 case MCI_GETDEVCAPS_USES_FILES
:
589 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
590 ret
= MCI_RESOURCE_RETURNED
;
592 case MCI_GETDEVCAPS_COMPOUND_DEVICE
:
593 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
594 ret
= MCI_RESOURCE_RETURNED
;
596 case MCI_GETDEVCAPS_CAN_EJECT
:
597 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
598 ret
= MCI_RESOURCE_RETURNED
;
600 case MCI_GETDEVCAPS_CAN_PLAY
:
601 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
602 ret
= MCI_RESOURCE_RETURNED
;
604 case MCI_GETDEVCAPS_CAN_SAVE
:
605 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
606 ret
= MCI_RESOURCE_RETURNED
;
609 WARN("Unsupported %x devCaps item\n", lpParms
->dwItem
);
610 return MCIERR_UNSUPPORTED_FUNCTION
;
613 TRACE("No GetDevCaps-Item !\n");
614 return MCIERR_MISSING_PARAMETER
;
616 TRACE("lpParms->dwReturn=%08X;\n", lpParms
->dwReturn
);
617 if (dwFlags
& MCI_NOTIFY
) {
618 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
623 static DWORD
CDROM_Audio_GetSerial(CDROM_TOC
* toc
)
628 DWORD dwStart
, dwEnd
;
631 * wMagic collects the wFrames from track 1
632 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
634 * There it is collected for correcting the serial when there are less than
637 wMagic
= toc
->TrackData
[0].Address
[3];
638 dwStart
= FRAME_OF_TOC(*toc
, toc
->FirstTrack
);
640 for (i
= 0; i
<= toc
->LastTrack
- toc
->FirstTrack
; i
++) {
641 serial
+= (toc
->TrackData
[i
].Address
[1] << 16) |
642 (toc
->TrackData
[i
].Address
[2] << 8) | toc
->TrackData
[i
].Address
[3];
644 dwEnd
= FRAME_OF_TOC(*toc
, toc
->LastTrack
+ 1);
646 if (toc
->LastTrack
- toc
->FirstTrack
+ 1 < 3)
647 serial
+= wMagic
+ (dwEnd
- dwStart
);
653 /**************************************************************************
654 * MCICDA_Info [internal]
656 static DWORD
MCICDA_Info(UINT wDevID
, DWORD dwFlags
, LPMCI_INFO_PARMSW lpParms
)
659 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
663 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
665 if (lpParms
== NULL
|| lpParms
->lpstrReturn
== NULL
)
666 return MCIERR_NULL_PARAMETER_BLOCK
;
667 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
669 TRACE("buf=%p, len=%u\n", lpParms
->lpstrReturn
, lpParms
->dwRetSize
);
671 if (dwFlags
& MCI_INFO_PRODUCT
) {
672 static const WCHAR wszAudioCd
[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
674 } else if (dwFlags
& MCI_INFO_MEDIA_UPC
) {
675 ret
= MCIERR_NO_IDENTITY
;
676 } else if (dwFlags
& MCI_INFO_MEDIA_IDENTITY
) {
680 static const WCHAR wszLu
[] = {'%','l','u',0};
682 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_TOC
, NULL
, 0,
683 &toc
, sizeof(toc
), &br
, NULL
)) {
684 return MCICDA_GetError(wmcda
);
687 res
= CDROM_Audio_GetSerial(&toc
);
688 sprintfW(buffer
, wszLu
, res
);
691 WARN("Don't know this info command (%u)\n", dwFlags
);
692 ret
= MCIERR_MISSING_PARAMETER
;
695 TRACE("=> %s\n", debugstr_w(str
));
696 if (lpParms
->dwRetSize
) {
697 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
698 * to the number of characters written, excluding \0. */
699 lstrcpynW(lpParms
->lpstrReturn
, str
, lpParms
->dwRetSize
);
700 } else ret
= MCIERR_PARAM_OVERFLOW
;
702 if (MMSYSERR_NOERROR
==ret
&& (dwFlags
& MCI_NOTIFY
))
703 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
707 /**************************************************************************
708 * MCICDA_Status [internal]
710 static DWORD
MCICDA_Status(UINT wDevID
, DWORD dwFlags
, LPMCI_STATUS_PARMS lpParms
)
712 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
714 CDROM_SUB_Q_DATA_FORMAT fmt
;
715 SUB_Q_CHANNEL_DATA data
;
719 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
721 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
722 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
724 if (dwFlags
& MCI_STATUS_ITEM
) {
725 TRACE("dwItem = %x\n", lpParms
->dwItem
);
726 switch (lpParms
->dwItem
) {
727 case MCI_STATUS_CURRENT_TRACK
:
728 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
729 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
730 &data
, sizeof(data
), &br
, NULL
))
732 return MCICDA_GetError(wmcda
);
733 /* alt. data.CurrentPosition.TrackNumber = 1; -- what native yields */
735 lpParms
->dwReturn
= data
.CurrentPosition
.TrackNumber
;
736 TRACE("CURRENT_TRACK=%lu\n", lpParms
->dwReturn
);
738 case MCI_STATUS_LENGTH
:
739 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
740 return MCICDA_GetError(wmcda
);
742 if (dwFlags
& MCI_TRACK
) {
743 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms
->dwTrack
);
744 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
745 return MCIERR_OUTOFRANGE
;
746 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, lpParms
->dwTrack
+ 1) -
747 FRAME_OF_TOC(toc
, lpParms
->dwTrack
);
748 /* Windows returns one frame less than the total track length for the
749 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
750 if (lpParms
->dwTrack
== toc
.LastTrack
)
753 /* Sum of the lengths of all of the tracks. Inherits the
754 'off by one frame' behavior from the length of the last track.
755 See above comment. */
756 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) -
757 FRAME_OF_TOC(toc
, toc
.FirstTrack
) - 1;
759 lpParms
->dwReturn
= MCICDA_CalcTime(wmcda
,
760 (wmcda
->dwTimeFormat
== MCI_FORMAT_TMSF
)
761 ? MCI_FORMAT_MSF
: wmcda
->dwTimeFormat
,
764 TRACE("LENGTH=%lu\n", lpParms
->dwReturn
);
766 case MCI_STATUS_MODE
:
767 lpParms
->dwReturn
= MCICDA_GetStatus(wmcda
);
768 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms
->dwReturn
);
769 lpParms
->dwReturn
= MAKEMCIRESOURCE(lpParms
->dwReturn
, lpParms
->dwReturn
);
770 ret
= MCI_RESOURCE_RETURNED
;
772 case MCI_STATUS_MEDIA_PRESENT
:
773 lpParms
->dwReturn
= (MCICDA_GetStatus(wmcda
) == MCI_MODE_OPEN
) ?
774 MAKEMCIRESOURCE(FALSE
, MCI_FALSE
) : MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
775 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms
->dwReturn
) ? 'Y' : 'N');
776 ret
= MCI_RESOURCE_RETURNED
;
778 case MCI_STATUS_NUMBER_OF_TRACKS
:
779 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
780 return MCICDA_GetError(wmcda
);
782 lpParms
->dwReturn
= toc
.LastTrack
- toc
.FirstTrack
+ 1;
783 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms
->dwReturn
);
784 if (lpParms
->dwReturn
== (WORD
)-1)
785 return MCICDA_GetError(wmcda
);
787 case MCI_STATUS_POSITION
:
788 switch (dwFlags
& (MCI_STATUS_START
| MCI_TRACK
)) {
789 case MCI_STATUS_START
:
790 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
791 return MCICDA_GetError(wmcda
);
793 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, toc
.FirstTrack
);
794 TRACE("get MCI_STATUS_START !\n");
797 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
798 return MCICDA_GetError(wmcda
);
800 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
801 return MCIERR_OUTOFRANGE
;
802 lpParms
->dwReturn
= FRAME_OF_TOC(toc
, lpParms
->dwTrack
);
803 TRACE("get MCI_TRACK #%u !\n", lpParms
->dwTrack
);
806 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
807 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
808 &data
, sizeof(data
), &br
, NULL
)) {
809 return MCICDA_GetError(wmcda
);
811 lpParms
->dwReturn
= FRAME_OF_ADDR(data
.CurrentPosition
.AbsoluteAddress
);
814 return MCIERR_FLAGS_NOT_COMPATIBLE
;
816 lpParms
->dwReturn
= MCICDA_CalcTime(wmcda
, wmcda
->dwTimeFormat
, lpParms
->dwReturn
, &ret
);
817 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms
->dwReturn
);
819 case MCI_STATUS_READY
:
820 TRACE("MCI_STATUS_READY !\n");
821 switch (MCICDA_GetStatus(wmcda
))
823 case MCI_MODE_NOT_READY
:
825 lpParms
->dwReturn
= MAKEMCIRESOURCE(FALSE
, MCI_FALSE
);
828 lpParms
->dwReturn
= MAKEMCIRESOURCE(TRUE
, MCI_TRUE
);
831 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms
->dwReturn
));
832 ret
= MCI_RESOURCE_RETURNED
;
834 case MCI_STATUS_TIME_FORMAT
:
835 lpParms
->dwReturn
= MAKEMCIRESOURCE(wmcda
->dwTimeFormat
, MCI_FORMAT_RETURN_BASE
+ wmcda
->dwTimeFormat
);
836 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms
->dwReturn
));
837 ret
= MCI_RESOURCE_RETURNED
;
839 case 4001: /* FIXME: for bogus FullCD */
840 case MCI_CDA_STATUS_TYPE_TRACK
:
841 if (!(dwFlags
& MCI_TRACK
))
842 ret
= MCIERR_MISSING_PARAMETER
;
844 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
845 return MCICDA_GetError(wmcda
);
847 if (lpParms
->dwTrack
< toc
.FirstTrack
|| lpParms
->dwTrack
> toc
.LastTrack
)
848 ret
= MCIERR_OUTOFRANGE
;
850 lpParms
->dwReturn
= (toc
.TrackData
[lpParms
->dwTrack
- toc
.FirstTrack
].Control
& 0x04) ?
851 MCI_CDA_TRACK_OTHER
: MCI_CDA_TRACK_AUDIO
;
852 /* FIXME: MAKEMCIRESOURCE "audio" | "other", localised */
854 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms
->dwTrack
, lpParms
->dwReturn
);
857 FIXME("unknown command %08X !\n", lpParms
->dwItem
);
858 return MCIERR_UNSUPPORTED_FUNCTION
;
860 } else return MCIERR_MISSING_PARAMETER
;
861 if ((dwFlags
& MCI_NOTIFY
) && HRESULT_CODE(ret
)==0)
862 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
866 /**************************************************************************
867 * MCICDA_SkipDataTracks [internal]
869 static DWORD
MCICDA_SkipDataTracks(WINE_MCICDAUDIO
* wmcda
,DWORD
*frame
)
874 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
875 return MCICDA_GetError(wmcda
);
877 if (*frame
< FRAME_OF_TOC(toc
,toc
.FirstTrack
) ||
878 *frame
>= FRAME_OF_TOC(toc
,toc
.LastTrack
+1)) /* lead-out */
879 return MCIERR_OUTOFRANGE
;
880 for(i
=toc
.LastTrack
+1;i
>toc
.FirstTrack
;i
--)
881 if ( FRAME_OF_TOC(toc
, i
) <= *frame
) break;
882 /* i points to last track whose start address is not greater than frame.
883 * Now skip non-audio tracks */
884 for(;i
<=toc
.LastTrack
;i
++)
885 if ( ! (toc
.TrackData
[i
-toc
.FirstTrack
].Control
& 4) )
887 /* The frame will be an address in the next audio track or
888 * address of lead-out. */
889 if ( FRAME_OF_TOC(toc
, i
) > *frame
)
890 *frame
= FRAME_OF_TOC(toc
, i
);
891 /* Lead-out is an invalid seek position (on Linux as well). */
892 if (*frame
== FRAME_OF_TOC(toc
,toc
.LastTrack
+1))
897 /**************************************************************************
898 * MCICDA_Play [internal]
900 static DWORD
MCICDA_Play(UINT wDevID
, DWORD dwFlags
, LPMCI_PLAY_PARMS lpParms
)
902 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
903 DWORD ret
= 0, start
, end
;
906 CDROM_PLAY_AUDIO_MSF play
;
907 CDROM_SUB_Q_DATA_FORMAT fmt
;
908 SUB_Q_CHANNEL_DATA data
;
911 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
914 return MCIERR_NULL_PARAMETER_BLOCK
;
917 return MCIERR_INVALID_DEVICE_ID
;
919 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
920 return MCICDA_GetError(wmcda
);
922 if (dwFlags
& MCI_FROM
) {
923 start
= MCICDA_CalcFrame(wmcda
, lpParms
->dwFrom
);
924 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &start
)) )
926 TRACE("MCI_FROM=%08X -> %u\n", lpParms
->dwFrom
, start
);
928 fmt
.Format
= IOCTL_CDROM_CURRENT_POSITION
;
929 if (!device_io(wmcda
->handle
, IOCTL_CDROM_READ_Q_CHANNEL
, &fmt
, sizeof(fmt
),
930 &data
, sizeof(data
), &br
, NULL
)) {
931 return MCICDA_GetError(wmcda
);
933 start
= FRAME_OF_ADDR(data
.CurrentPosition
.AbsoluteAddress
);
934 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &start
)) )
937 if (dwFlags
& MCI_TO
) {
938 end
= MCICDA_CalcFrame(wmcda
, lpParms
->dwTo
);
939 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &end
)) )
941 TRACE("MCI_TO=%08X -> %u\n", lpParms
->dwTo
, end
);
943 end
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) - 1;
945 if (end
< start
) return MCIERR_OUTOFRANGE
;
946 TRACE("Playing from %u to %u\n", start
, end
);
948 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
,
949 (dwFlags
& MCI_NOTIFY
) ? HWND_32(LOWORD(lpParms
->dwCallback
)) : NULL
);
950 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
952 if (start
== end
|| start
== FRAME_OF_TOC(toc
,toc
.LastTrack
+1)-1) {
953 if (dwFlags
& MCI_NOTIFY
) {
954 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
955 if (oldcb
) mciDriverNotify(oldcb
, wDevID
, MCI_NOTIFY_SUCCESSFUL
);
957 return MMSYSERR_NOERROR
;
960 if (wmcda
->hThread
!= 0) {
961 SetEvent(wmcda
->stopEvent
);
962 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
964 CloseHandle(wmcda
->hThread
);
966 CloseHandle(wmcda
->stopEvent
);
967 wmcda
->stopEvent
= 0;
969 IDirectSoundBuffer_Stop(wmcda
->dsBuf
);
970 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
972 IDirectSound_Release(wmcda
->dsObj
);
976 if (pDirectSoundCreate
) {
983 hr
= pDirectSoundCreate(NULL
, &wmcda
->dsObj
, NULL
);
985 IDirectSound_SetCooperativeLevel(wmcda
->dsObj
, GetDesktopWindow(), DSSCL_PRIORITY
);
987 /* The "raw" frame is relative to the start of the first track */
988 wmcda
->start
= start
- FRAME_OF_TOC(toc
, toc
.FirstTrack
);
989 wmcda
->end
= end
- FRAME_OF_TOC(toc
, toc
.FirstTrack
);
991 memset(&format
, 0, sizeof(format
));
992 format
.wFormatTag
= WAVE_FORMAT_PCM
;
993 format
.nChannels
= 2;
994 format
.nSamplesPerSec
= 44100;
995 format
.wBitsPerSample
= 16;
996 format
.nBlockAlign
= format
.nChannels
* format
.wBitsPerSample
/ 8;
997 format
.nAvgBytesPerSec
= format
.nSamplesPerSec
* format
.nBlockAlign
;
1000 memset(&desc
, 0, sizeof(desc
));
1001 desc
.dwSize
= sizeof(desc
);
1002 desc
.dwFlags
= DSBCAPS_GETCURRENTPOSITION2
| DSBCAPS_GLOBALFOCUS
;
1003 desc
.dwBufferBytes
= (CDDA_FRAG_SIZE
- (CDDA_FRAG_SIZE
%RAW_SECTOR_SIZE
)) * CDDA_FRAG_COUNT
;
1004 desc
.lpwfxFormat
= &format
;
1006 hr
= IDirectSound_CreateSoundBuffer(wmcda
->dsObj
, &desc
, &wmcda
->dsBuf
, NULL
);
1008 if (SUCCEEDED(hr
)) {
1009 hr
= IDirectSoundBuffer_Lock(wmcda
->dsBuf
, 0, 0, &cdData
, &lockLen
,
1010 NULL
, NULL
, DSBLOCK_ENTIREBUFFER
);
1012 if (SUCCEEDED(hr
)) {
1013 RAW_READ_INFO rdInfo
;
1016 rdInfo
.DiskOffset
.QuadPart
= wmcda
->start
<<11;
1017 rdInfo
.SectorCount
= min(desc
.dwBufferBytes
/RAW_SECTOR_SIZE
,
1018 wmcda
->end
-wmcda
->start
);
1019 rdInfo
.TrackMode
= CDDA
;
1021 readok
= device_io(wmcda
->handle
, IOCTL_CDROM_RAW_READ
,
1022 &rdInfo
, sizeof(rdInfo
), cdData
, lockLen
,
1024 IDirectSoundBuffer_Unlock(wmcda
->dsBuf
, cdData
, lockLen
, NULL
, 0);
1027 wmcda
->start
+= rdInfo
.SectorCount
;
1028 wmcda
->stopEvent
= CreateEventA(NULL
, TRUE
, FALSE
, NULL
);
1030 if (wmcda
->stopEvent
!= 0)
1031 wmcda
->hThread
= CreateThread(NULL
, 0, MCICDA_playLoop
, wmcda
, 0, &br
);
1032 if (wmcda
->hThread
!= 0) {
1033 hr
= IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
);
1034 if (SUCCEEDED(hr
)) {
1035 /* FIXME: implement MCI_WAIT and send notification only in that case */
1037 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1038 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
,
1039 FAILED(hr
) ? MCI_NOTIFY_FAILURE
: MCI_NOTIFY_SUCCESSFUL
);
1044 SetEvent(wmcda
->stopEvent
);
1045 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
1046 CloseHandle(wmcda
->hThread
);
1051 if (wmcda
->stopEvent
!= 0) {
1052 CloseHandle(wmcda
->stopEvent
);
1053 wmcda
->stopEvent
= 0;
1056 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
1057 wmcda
->dsBuf
= NULL
;
1060 IDirectSound_Release(wmcda
->dsObj
);
1061 wmcda
->dsObj
= NULL
;
1065 play
.StartingM
= start
/ CDFRAMES_PERMIN
;
1066 play
.StartingS
= (start
/ CDFRAMES_PERSEC
) % 60;
1067 play
.StartingF
= start
% CDFRAMES_PERSEC
;
1068 play
.EndingM
= end
/ CDFRAMES_PERMIN
;
1069 play
.EndingS
= (end
/ CDFRAMES_PERSEC
) % 60;
1070 play
.EndingF
= end
% CDFRAMES_PERSEC
;
1071 if (!device_io(wmcda
->handle
, IOCTL_CDROM_PLAY_AUDIO_MSF
, &play
, sizeof(play
),
1072 NULL
, 0, &br
, NULL
)) {
1073 wmcda
->hCallback
= NULL
;
1074 ret
= MCIERR_HARDWARE
;
1076 /* The independent CD player has no means to signal MCI_NOTIFY when it's done.
1077 * Native sends a notification with MCI_WAIT only. */
1081 /**************************************************************************
1082 * MCICDA_Stop [internal]
1084 static DWORD
MCICDA_Stop(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1086 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1090 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1092 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1094 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1095 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
1097 if (wmcda
->hThread
!= 0) {
1098 SetEvent(wmcda
->stopEvent
);
1099 WaitForSingleObject(wmcda
->hThread
, INFINITE
);
1101 CloseHandle(wmcda
->hThread
);
1103 CloseHandle(wmcda
->stopEvent
);
1104 wmcda
->stopEvent
= 0;
1106 IDirectSoundBuffer_Release(wmcda
->dsBuf
);
1107 wmcda
->dsBuf
= NULL
;
1108 IDirectSound_Release(wmcda
->dsObj
);
1109 wmcda
->dsObj
= NULL
;
1111 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_STOP_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1112 return MCIERR_HARDWARE
;
1114 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1115 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1119 /**************************************************************************
1120 * MCICDA_Pause [internal]
1122 static DWORD
MCICDA_Pause(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1124 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1128 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1130 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1132 oldcb
= InterlockedExchangePointer(&wmcda
->hCallback
, NULL
);
1133 if (oldcb
) mciDriverNotify(oldcb
, wmcda
->wNotifyDeviceID
, MCI_NOTIFY_ABORTED
);
1135 if (wmcda
->hThread
!= 0) {
1136 /* Don't bother calling stop if the playLoop thread has already stopped */
1137 if(WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
&&
1138 FAILED(IDirectSoundBuffer_Stop(wmcda
->dsBuf
)))
1139 return MCIERR_HARDWARE
;
1141 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_PAUSE_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1142 return MCIERR_HARDWARE
;
1144 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1145 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1149 /**************************************************************************
1150 * MCICDA_Resume [internal]
1152 static DWORD
MCICDA_Resume(UINT wDevID
, DWORD dwFlags
, LPMCI_GENERIC_PARMS lpParms
)
1154 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1157 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1159 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1161 if (wmcda
->hThread
!= 0) {
1162 /* Don't restart if the playLoop thread has already stopped */
1163 if(WaitForSingleObject(wmcda
->stopEvent
, 0) != WAIT_OBJECT_0
&&
1164 FAILED(IDirectSoundBuffer_Play(wmcda
->dsBuf
, 0, 0, DSBPLAY_LOOPING
)))
1165 return MCIERR_HARDWARE
;
1167 else if (!device_io(wmcda
->handle
, IOCTL_CDROM_RESUME_AUDIO
, NULL
, 0, NULL
, 0, &br
, NULL
))
1168 return MCIERR_HARDWARE
;
1170 if ((dwFlags
& MCI_NOTIFY
) && lpParms
)
1171 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1175 /**************************************************************************
1176 * MCICDA_Seek [internal]
1178 static DWORD
MCICDA_Seek(UINT wDevID
, DWORD dwFlags
, LPMCI_SEEK_PARMS lpParms
)
1181 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1182 CDROM_SEEK_AUDIO_MSF seek
;
1183 DWORD br
, position
, ret
;
1186 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1188 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1189 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1191 position
= dwFlags
& (MCI_SEEK_TO_START
|MCI_SEEK_TO_END
|MCI_TO
);
1192 if (!position
) return MCIERR_MISSING_PARAMETER
;
1193 if (position
&(position
-1)) return MCIERR_FLAGS_NOT_COMPATIBLE
;
1195 /* Stop sends MCI_NOTIFY_ABORTED when needed.
1196 * Tests show that native first sends ABORTED and reads the TOC,
1197 * then only checks the position flags, then stops and seeks. */
1198 MCICDA_Stop(wDevID
, MCI_WAIT
, 0);
1200 if (!MCICDA_ReadTOC(wmcda
, &toc
, &br
))
1201 return MCICDA_GetError(wmcda
);
1204 case MCI_SEEK_TO_START
:
1205 TRACE("Seeking to start\n");
1206 at
= FRAME_OF_TOC(toc
,toc
.FirstTrack
);
1207 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1210 case MCI_SEEK_TO_END
:
1211 TRACE("Seeking to end\n");
1212 /* End is prior to lead-out
1213 * yet Win9X seeks to even one frame less than that. */
1214 at
= FRAME_OF_TOC(toc
, toc
.LastTrack
+ 1) - 1;
1215 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1219 TRACE("Seeking to %u\n", lpParms
->dwTo
);
1220 at
= MCICDA_CalcFrame(wmcda
, lpParms
->dwTo
);
1221 if ( (ret
=MCICDA_SkipDataTracks(wmcda
, &at
)) )
1225 return MCIERR_FLAGS_NOT_COMPATIBLE
;
1229 seek
.M
= at
/ CDFRAMES_PERMIN
;
1230 seek
.S
= (at
/ CDFRAMES_PERSEC
) % 60;
1231 seek
.F
= at
% CDFRAMES_PERSEC
;
1232 if (!device_io(wmcda
->handle
, IOCTL_CDROM_SEEK_AUDIO_MSF
, &seek
, sizeof(seek
),
1233 NULL
, 0, &br
, NULL
))
1234 return MCIERR_HARDWARE
;
1237 if (dwFlags
& MCI_NOTIFY
)
1238 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1242 /**************************************************************************
1243 * MCICDA_SetDoor [internal]
1245 static DWORD
MCICDA_SetDoor(UINT wDevID
, BOOL open
)
1247 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1250 TRACE("(%04x, %s) !\n", wDevID
, (open
) ? "OPEN" : "CLOSE");
1252 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1254 if (!device_io(wmcda
->handle
,
1255 (open
) ? IOCTL_STORAGE_EJECT_MEDIA
: IOCTL_STORAGE_LOAD_MEDIA
,
1256 NULL
, 0, NULL
, 0, &br
, NULL
))
1257 return MCIERR_HARDWARE
;
1262 /**************************************************************************
1263 * MCICDA_Set [internal]
1265 static DWORD
MCICDA_Set(UINT wDevID
, DWORD dwFlags
, LPMCI_SET_PARMS lpParms
)
1267 WINE_MCICDAUDIO
* wmcda
= MCICDA_GetOpenDrv(wDevID
);
1269 TRACE("(%04X, %08X, %p);\n", wDevID
, dwFlags
, lpParms
);
1271 if (wmcda
== NULL
) return MCIERR_INVALID_DEVICE_ID
;
1273 if (dwFlags
& MCI_SET_DOOR_OPEN
) {
1274 MCICDA_SetDoor(wDevID
, TRUE
);
1276 if (dwFlags
& MCI_SET_DOOR_CLOSED
) {
1277 MCICDA_SetDoor(wDevID
, FALSE
);
1280 /* only functions which require valid lpParms below this line ! */
1281 if (lpParms
== NULL
) return MCIERR_NULL_PARAMETER_BLOCK
;
1283 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1285 if (dwFlags
& MCI_SET_TIME_FORMAT
) {
1286 switch (lpParms
->dwTimeFormat
) {
1287 case MCI_FORMAT_MILLISECONDS
:
1288 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1290 case MCI_FORMAT_MSF
:
1291 TRACE("MCI_FORMAT_MSF !\n");
1293 case MCI_FORMAT_TMSF
:
1294 TRACE("MCI_FORMAT_TMSF !\n");
1297 return MCIERR_BAD_TIME_FORMAT
;
1299 wmcda
->dwTimeFormat
= lpParms
->dwTimeFormat
;
1301 if (dwFlags
& MCI_SET_AUDIO
) /* one xp machine ignored it */
1302 TRACE("SET_AUDIO %X %x\n", dwFlags
, lpParms
->dwAudio
);
1304 if (dwFlags
& MCI_NOTIFY
)
1305 MCICDA_Notify(lpParms
->dwCallback
, wmcda
, MCI_NOTIFY_SUCCESSFUL
);
1309 /**************************************************************************
1310 * DriverProc (MCICDA.@)
1312 LRESULT CALLBACK
MCICDA_DriverProc(DWORD_PTR dwDevID
, HDRVR hDriv
, UINT wMsg
,
1313 LPARAM dwParam1
, LPARAM dwParam2
)
1316 case DRV_LOAD
: return 1;
1317 case DRV_FREE
: return 1;
1318 case DRV_OPEN
: return MCICDA_drvOpen((LPCWSTR
)dwParam1
, (LPMCI_OPEN_DRIVER_PARMSW
)dwParam2
);
1319 case DRV_CLOSE
: return MCICDA_drvClose(dwDevID
);
1320 case DRV_ENABLE
: return 1;
1321 case DRV_DISABLE
: return 1;
1322 case DRV_QUERYCONFIGURE
: return 1;
1323 case DRV_CONFIGURE
: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK
); return 1;
1324 case DRV_INSTALL
: return DRVCNF_RESTART
;
1325 case DRV_REMOVE
: return DRVCNF_RESTART
;
1328 if (dwDevID
== 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION
;
1331 case MCI_OPEN_DRIVER
: return MCICDA_Open(dwDevID
, dwParam1
, (LPMCI_OPEN_PARMSW
)dwParam2
);
1332 case MCI_CLOSE_DRIVER
: return MCICDA_Close(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1333 case MCI_GETDEVCAPS
: return MCICDA_GetDevCaps(dwDevID
, dwParam1
, (LPMCI_GETDEVCAPS_PARMS
)dwParam2
);
1334 case MCI_INFO
: return MCICDA_Info(dwDevID
, dwParam1
, (LPMCI_INFO_PARMSW
)dwParam2
);
1335 case MCI_STATUS
: return MCICDA_Status(dwDevID
, dwParam1
, (LPMCI_STATUS_PARMS
)dwParam2
);
1336 case MCI_SET
: return MCICDA_Set(dwDevID
, dwParam1
, (LPMCI_SET_PARMS
)dwParam2
);
1337 case MCI_PLAY
: return MCICDA_Play(dwDevID
, dwParam1
, (LPMCI_PLAY_PARMS
)dwParam2
);
1338 case MCI_STOP
: return MCICDA_Stop(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1339 case MCI_PAUSE
: return MCICDA_Pause(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1340 case MCI_RESUME
: return MCICDA_Resume(dwDevID
, dwParam1
, (LPMCI_GENERIC_PARMS
)dwParam2
);
1341 case MCI_SEEK
: return MCICDA_Seek(dwDevID
, dwParam1
, (LPMCI_SEEK_PARMS
)dwParam2
);
1342 /* commands that should report an error as they are not supported in
1343 * the native version */
1347 return MCIERR_UNSUPPORTED_FUNCTION
;
1363 TRACE("Unsupported command [0x%x]\n", wMsg
);
1367 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1370 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg
);
1371 return DefDriverProc(dwDevID
, hDriv
, wMsg
, dwParam1
, dwParam2
);
1373 return MCIERR_UNRECOGNIZED_COMMAND
;
1376 /*-----------------------------------------------------------------------*/