mfplat: Read queue subscriber within the critical section.
[wine/zf.git] / dlls / mcicda / mcicda.c
blobea7e2a6261d571f94e913467967b5221daba04e3
1 /*
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
23 #include <stdarg.h>
24 #include <stdio.h>
25 #include <string.h>
27 #define WIN32_NO_STATUS
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "winuser.h"
32 #include "wownt32.h"
33 #include "mmddk.h"
34 #include "winioctl.h"
35 #include "ntddcdrm.h"
36 #include "winternl.h"
37 #include "wine/debug.h"
38 #include "dsound.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)
52 /* Must be >= 2 */
53 #define CDDA_FRAG_COUNT (3)
55 typedef struct {
56 UINT wDevID;
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 */
61 DWORD dwTimeFormat;
62 HANDLE handle;
64 /* The following are used for digital playback only */
65 HANDLE hThread;
66 HANDLE stopEvent;
67 DWORD start, end;
69 IDirectSound *dsObj;
70 IDirectSoundBuffer *dsBuf;
72 CRITICAL_SECTION cs;
73 } WINE_MCICDAUDIO;
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)
82 const char *str;
83 BOOL ret = DeviceIoControl(dev, code, inbuffer, insize, outbuffer, outsize, retsize, overlapped);
85 #define XX(x) case (x): str = #x; break
86 switch (code)
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);
100 #undef XX
101 TRACE("Device %p, Code %s -> Return %d, Bytes %u\n", dev, str, ret, *retsize);
102 return ret;
105 static DWORD CALLBACK MCICDA_playLoop(void *ptr)
107 WINE_MCICDAUDIO *wmcda = (WINE_MCICDAUDIO*)ptr;
108 DWORD lastPos, curPos, endPos, br;
109 void *cdData;
110 DWORD lockLen, fragLen;
111 DSBCAPS caps;
112 RAW_READ_INFO rdInfo;
113 HRESULT hr = DS_OK;
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;
121 endPos = ~0u;
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) {
126 Sleep(1);
127 continue;
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);
140 break;
142 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, lastPos, fragLen, &cdData, &lockLen, NULL, NULL, 0);
145 if (SUCCEEDED(hr)) {
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);
158 lastPos += fragLen;
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). */
173 return 0;
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));
191 if (!wmcda)
192 return 0;
194 if (!dsHandle) {
195 dsHandle = LoadLibraryA("dsound.dll");
196 if(dsHandle)
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);
216 if (wmcda) {
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);
234 return 0;
236 return wmcda;
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");
260 return FALSE;
262 return TRUE;
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;
272 DWORD br;
273 DWORD mode = MCI_MODE_NOT_READY;
275 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
276 if(wmcda->hThread != 0) {
277 DWORD status;
278 HRESULT hr;
280 hr = IDirectSoundBuffer_GetStatus(wmcda->dsBuf, &status);
281 if(SUCCEEDED(hr)) {
282 if(!(status&DSBSTATUS_PLAYING)) {
283 if(WaitForSingleObject(wmcda->stopEvent, 0) == WAIT_OBJECT_0)
284 mode = MCI_MODE_STOP;
285 else
286 mode = MCI_MODE_PAUSE;
288 else
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;
295 } else {
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:
304 default:
305 break;
308 return mode;
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;
321 default:
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)
332 DWORD dwFrame = 0;
333 UINT wTrack;
334 CDROM_TOC toc;
335 DWORD br;
336 BYTE* addr;
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);
344 break;
345 case MCI_FORMAT_MSF:
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);
351 break;
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))
357 return 0;
358 if (wTrack < toc.FirstTrack || wTrack > toc.LastTrack)
359 return 0;
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);
369 break;
371 return dwFrame;
374 /**************************************************************************
375 * MCICDA_CalcTime [internal]
377 static DWORD MCICDA_CalcTime(WINE_MCICDAUDIO* wmcda, DWORD tf, DWORD dwFrame, LPDWORD lpRet)
379 DWORD dwTime = 0;
380 UINT wTrack;
381 UINT wMinutes;
382 UINT wSeconds;
383 UINT wFrames;
384 CDROM_TOC toc;
385 DWORD br;
387 TRACE("(%p, %08X, %u);\n", wmcda, tf, dwFrame);
389 switch (tf) {
390 case MCI_FORMAT_MILLISECONDS:
391 dwTime = (dwFrame * 1000) / CDFRAMES_PERSEC + 1;
392 TRACE("MILLISECONDS %u\n", dwTime);
393 *lpRet = 0;
394 break;
395 case MCI_FORMAT_MSF:
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;
403 break;
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))
408 return 0;
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));
414 *lpRet = 0;
415 return 0;
417 for (wTrack = toc.FirstTrack; wTrack <= toc.LastTrack; wTrack++) {
418 if (FRAME_OF_TOC(toc, wTrack) > dwFrame)
419 break;
421 wTrack--;
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;
429 break;
431 return dwTime;
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;
442 DWORD ret;
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))
458 ++wmcda->nUseCount;
459 else
460 return MCIERR_MUST_USE_SHAREABLE;
461 } else {
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;
469 goto the_error;
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;
478 goto the_error;
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;
485 goto the_error;
488 else
490 root[0] = 'A'; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
491 for ( ; root[0] <= 'Z'; root[0]++)
493 if (GetDriveTypeW(root) == DRIVE_CDROM)
495 drive = root[0];
496 break;
499 if (!drive)
501 ret = MCIERR_CANNOT_LOAD_DRIVER; /* drvOpen should return this */
502 goto the_error;
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;
515 goto the_error;
518 if (dwFlags & MCI_NOTIFY) {
519 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
520 dwDeviceID, MCI_NOTIFY_SUCCESSFUL);
522 return 0;
524 the_error:
525 --wmcda->nUseCount;
526 return ret;
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);
547 return 0;
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);
557 DWORD ret = 0;
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;
571 break;
572 case MCI_GETDEVCAPS_HAS_AUDIO:
573 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
574 ret = MCI_RESOURCE_RETURNED;
575 break;
576 case MCI_GETDEVCAPS_HAS_VIDEO:
577 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
578 ret = MCI_RESOURCE_RETURNED;
579 break;
580 case MCI_GETDEVCAPS_DEVICE_TYPE:
581 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO, MCI_DEVTYPE_CD_AUDIO);
582 ret = MCI_RESOURCE_RETURNED;
583 break;
584 case MCI_GETDEVCAPS_USES_FILES:
585 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
586 ret = MCI_RESOURCE_RETURNED;
587 break;
588 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
589 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
590 ret = MCI_RESOURCE_RETURNED;
591 break;
592 case MCI_GETDEVCAPS_CAN_EJECT:
593 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
594 ret = MCI_RESOURCE_RETURNED;
595 break;
596 case MCI_GETDEVCAPS_CAN_PLAY:
597 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
598 ret = MCI_RESOURCE_RETURNED;
599 break;
600 case MCI_GETDEVCAPS_CAN_SAVE:
601 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
602 ret = MCI_RESOURCE_RETURNED;
603 break;
604 default:
605 WARN("Unsupported %x devCaps item\n", lpParms->dwItem);
606 return MCIERR_UNSUPPORTED_FUNCTION;
608 } else {
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);
616 return ret;
619 static DWORD CDROM_Audio_GetSerial(CDROM_TOC* toc)
621 DWORD serial = 0;
622 int i;
623 WORD wMagic;
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
629 * frames.
630 * There it is collected for correcting the serial when there are less than
631 * 3 tracks.
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);
645 return serial;
649 /**************************************************************************
650 * MCICDA_Info [internal]
652 static DWORD MCICDA_Info(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
654 LPCWSTR str = NULL;
655 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
656 DWORD ret = 0;
657 WCHAR buffer[16];
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) {
672 DWORD res = 0;
673 CDROM_TOC toc;
674 DWORD br;
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);
683 str = buffer;
684 } else {
685 WARN("Don't know this info command (%u)\n", dwFlags);
686 ret = MCIERR_MISSING_PARAMETER;
688 if (!ret) {
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);
698 return ret;
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);
707 DWORD ret = 0;
708 CDROM_SUB_Q_DATA_FORMAT fmt;
709 SUB_Q_CHANNEL_DATA data;
710 CDROM_TOC toc;
711 DWORD br;
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);
731 break;
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)
745 lpParms->dwReturn--;
746 } else {
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,
756 lpParms->dwReturn,
757 &ret);
758 TRACE("LENGTH=%lu\n", lpParms->dwReturn);
759 break;
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;
765 break;
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;
771 break;
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);
780 break;
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");
789 break;
790 case MCI_TRACK:
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);
798 break;
799 case 0:
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);
806 break;
807 default:
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);
812 break;
813 case MCI_STATUS_READY:
814 TRACE("MCI_STATUS_READY !\n");
815 switch (MCICDA_GetStatus(wmcda))
817 case MCI_MODE_NOT_READY:
818 case MCI_MODE_OPEN:
819 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
820 break;
821 default:
822 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
823 break;
825 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms->dwReturn));
826 ret = MCI_RESOURCE_RETURNED;
827 break;
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;
832 break;
833 case 4001: /* FIXME: for bogus FullCD */
834 case MCI_CDA_STATUS_TYPE_TRACK:
835 if (!(dwFlags & MCI_TRACK))
836 ret = MCIERR_MISSING_PARAMETER;
837 else {
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;
843 else
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);
849 break;
850 default:
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);
857 return ret;
860 /**************************************************************************
861 * MCICDA_SkipDataTracks [internal]
863 static DWORD MCICDA_SkipDataTracks(WINE_MCICDAUDIO* wmcda,DWORD *frame)
865 int i;
866 DWORD br;
867 CDROM_TOC toc;
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) )
880 break;
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))
887 (*frame)--;
888 return 0;
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;
898 HANDLE oldcb;
899 DWORD br;
900 CDROM_PLAY_AUDIO_MSF play;
901 CDROM_SUB_Q_DATA_FORMAT fmt;
902 SUB_Q_CHANNEL_DATA data;
903 CDROM_TOC toc;
905 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
907 if (lpParms == NULL)
908 return MCIERR_NULL_PARAMETER_BLOCK;
910 if (wmcda == NULL)
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)) )
919 return ret;
920 TRACE("MCI_FROM=%08X -> %u\n", lpParms->dwFrom, start);
921 } else {
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)) )
929 return ret;
931 if (dwFlags & MCI_TO) {
932 end = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
933 if ( (ret=MCICDA_SkipDataTracks(wmcda, &end)) )
934 return ret;
935 TRACE("MCI_TO=%08X -> %u\n", lpParms->dwTo, end);
936 } else {
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);
959 wmcda->hThread = 0;
960 CloseHandle(wmcda->stopEvent);
961 wmcda->stopEvent = 0;
963 IDirectSoundBuffer_Stop(wmcda->dsBuf);
964 IDirectSoundBuffer_Release(wmcda->dsBuf);
965 wmcda->dsBuf = NULL;
966 IDirectSound_Release(wmcda->dsObj);
967 wmcda->dsObj = NULL;
970 if (pDirectSoundCreate) {
971 WAVEFORMATEX format;
972 DSBUFFERDESC desc;
973 DWORD lockLen;
974 void *cdData;
975 HRESULT hr;
977 hr = pDirectSoundCreate(NULL, &wmcda->dsObj, NULL);
978 if (SUCCEEDED(hr)) {
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;
992 format.cbSize = 0;
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;
1008 int readok;
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,
1017 &br, NULL);
1018 IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
1020 if (readok) {
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 */
1030 if (0) {
1031 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1032 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID,
1033 FAILED(hr) ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1035 return ret;
1038 SetEvent(wmcda->stopEvent);
1039 WaitForSingleObject(wmcda->hThread, INFINITE);
1040 CloseHandle(wmcda->hThread);
1041 wmcda->hThread = 0;
1045 if (wmcda->stopEvent != 0) {
1046 CloseHandle(wmcda->stopEvent);
1047 wmcda->stopEvent = 0;
1049 if (wmcda->dsBuf) {
1050 IDirectSoundBuffer_Release(wmcda->dsBuf);
1051 wmcda->dsBuf = NULL;
1053 if (wmcda->dsObj) {
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. */
1072 return ret;
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);
1081 HANDLE oldcb;
1082 DWORD br;
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);
1096 wmcda->hThread = 0;
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);
1110 return 0;
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);
1119 HANDLE oldcb;
1120 DWORD br;
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);
1140 return 0;
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);
1149 DWORD br;
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);
1166 return 0;
1169 /**************************************************************************
1170 * MCICDA_Seek [internal]
1172 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1174 DWORD at;
1175 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1176 CDROM_SEEK_AUDIO_MSF seek;
1177 DWORD br, position, ret;
1178 CDROM_TOC toc;
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);
1197 switch (position) {
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)) )
1202 return ret;
1203 break;
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)) )
1210 return ret;
1211 break;
1212 case MCI_TO:
1213 TRACE("Seeking to %u\n", lpParms->dwTo);
1214 at = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
1215 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1216 return ret;
1217 break;
1218 default:
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);
1233 return 0;
1236 /**************************************************************************
1237 * MCICDA_SetDoor [internal]
1239 static DWORD MCICDA_SetDoor(UINT wDevID, BOOL open)
1241 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1242 DWORD br;
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;
1253 return 0;
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");
1283 break;
1284 case MCI_FORMAT_MSF:
1285 TRACE("MCI_FORMAT_MSF !\n");
1286 break;
1287 case MCI_FORMAT_TMSF:
1288 TRACE("MCI_FORMAT_TMSF !\n");
1289 break;
1290 default:
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);
1300 return 0;
1303 /**************************************************************************
1304 * DriverProc (MCICDA.@)
1306 LRESULT CALLBACK MCICDA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1307 LPARAM dwParam1, LPARAM dwParam2)
1309 switch(wMsg) {
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;
1324 switch (wMsg) {
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 */
1338 case MCI_RECORD:
1339 case MCI_LOAD:
1340 case MCI_SAVE:
1341 return MCIERR_UNSUPPORTED_FUNCTION;
1342 case MCI_BREAK:
1343 case MCI_FREEZE:
1344 case MCI_PUT:
1345 case MCI_REALIZE:
1346 case MCI_UNFREEZE:
1347 case MCI_UPDATE:
1348 case MCI_WHERE:
1349 case MCI_STEP:
1350 case MCI_SPIN:
1351 case MCI_ESCAPE:
1352 case MCI_COPY:
1353 case MCI_CUT:
1354 case MCI_DELETE:
1355 case MCI_PASTE:
1356 case MCI_WINDOW:
1357 TRACE("Unsupported command [0x%x]\n", wMsg);
1358 break;
1359 case MCI_OPEN:
1360 case MCI_CLOSE:
1361 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1362 break;
1363 default:
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 /*-----------------------------------------------------------------------*/