wined3d: Initialize unwritten color varyings.
[wine/multimedia.git] / dlls / mcicda / mcicda.c
blobc7bacffcd16bc21acd68c226fc9cade5ea1f6dbb
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 "config.h"
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <string.h>
28 #define WIN32_NO_STATUS
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winuser.h"
33 #include "wownt32.h"
34 #include "mmddk.h"
35 #include "winioctl.h"
36 #include "ntddcdrm.h"
37 #include "winternl.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40 #include "dsound.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)
54 /* Must be >= 2 */
55 #define CDDA_FRAG_COUNT (3)
57 typedef struct {
58 UINT wDevID;
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 */
63 DWORD dwTimeFormat;
64 HANDLE handle;
66 /* The following are used for digital playback only */
67 HANDLE hThread;
68 HANDLE stopEvent;
69 DWORD start, end;
71 IDirectSound *dsObj;
72 IDirectSoundBuffer *dsBuf;
74 CRITICAL_SECTION cs;
75 } WINE_MCICDAUDIO;
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;
86 void *cdData;
87 DWORD lockLen, fragLen;
88 DSBCAPS caps;
89 RAW_READ_INFO rdInfo;
90 HRESULT hr = DS_OK;
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;
97 curPos = lastPos = 0;
98 endPos = ~0u;
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) {
103 Sleep(1);
104 continue;
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);
117 break;
119 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, lastPos, fragLen, &cdData, &lockLen, NULL, NULL, 0);
122 if (SUCCEEDED(hr)) {
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);
135 lastPos += fragLen;
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). */
150 return 0;
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));
168 if (!wmcda)
169 return 0;
171 if (!dsHandle) {
172 dsHandle = LoadLibraryA("dsound.dll");
173 if(dsHandle)
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 wmcda->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINE_MCICDAUDIO.cs");
183 return modp->wDeviceID;
186 /**************************************************************************
187 * MCICDA_drvClose [internal]
189 static DWORD MCICDA_drvClose(DWORD dwDevID)
191 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(dwDevID);
193 if (wmcda) {
194 wmcda->cs.DebugInfo->Spare[0] = 0;
195 DeleteCriticalSection(&wmcda->cs);
196 HeapFree(GetProcessHeap(), 0, wmcda);
197 mciSetDriverData(dwDevID, 0);
199 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
202 /**************************************************************************
203 * MCICDA_GetOpenDrv [internal]
205 static WINE_MCICDAUDIO* MCICDA_GetOpenDrv(UINT wDevID)
207 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
209 if (wmcda == NULL || wmcda->nUseCount == 0) {
210 WARN("Invalid wDevID=%u\n", wDevID);
211 return 0;
213 return wmcda;
216 /**************************************************************************
217 * MCICDA_mciNotify [internal]
219 * Notifications in MCI work like a 1-element queue.
220 * Each new notification request supersedes the previous one.
222 static void MCICDA_Notify(DWORD_PTR hWndCallBack, WINE_MCICDAUDIO* wmcda, UINT wStatus)
224 MCIDEVICEID wDevID = wmcda->wNotifyDeviceID;
225 HANDLE old = InterlockedExchangePointer(&wmcda->hCallback, NULL);
226 if (old) mciDriverNotify(old, wDevID, MCI_NOTIFY_SUPERSEDED);
227 mciDriverNotify(HWND_32(LOWORD(hWndCallBack)), wDevID, wStatus);
230 /**************************************************************************
231 * MCICDA_GetStatus [internal]
233 static DWORD MCICDA_GetStatus(WINE_MCICDAUDIO* wmcda)
235 CDROM_SUB_Q_DATA_FORMAT fmt;
236 SUB_Q_CHANNEL_DATA data;
237 DWORD br;
238 DWORD mode = MCI_MODE_NOT_READY;
240 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
241 if(wmcda->hThread != 0) {
242 DWORD status;
243 HRESULT hr;
245 hr = IDirectSoundBuffer_GetStatus(wmcda->dsBuf, &status);
246 if(SUCCEEDED(hr)) {
247 if(!(status&DSBSTATUS_PLAYING)) {
248 if(WaitForSingleObject(wmcda->stopEvent, 0) == WAIT_OBJECT_0)
249 mode = MCI_MODE_STOP;
250 else
251 mode = MCI_MODE_PAUSE;
253 else
254 mode = MCI_MODE_PLAY;
257 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
258 &data, sizeof(data), &br, NULL)) {
259 if (GetLastError() == ERROR_NOT_READY) mode = MCI_MODE_OPEN;
260 } else {
261 switch (data.CurrentPosition.Header.AudioStatus)
263 case AUDIO_STATUS_IN_PROGRESS: mode = MCI_MODE_PLAY; break;
264 case AUDIO_STATUS_PAUSED: mode = MCI_MODE_PAUSE; break;
265 case AUDIO_STATUS_NO_STATUS:
266 case AUDIO_STATUS_PLAY_COMPLETE: mode = MCI_MODE_STOP; break;
267 case AUDIO_STATUS_PLAY_ERROR:
268 case AUDIO_STATUS_NOT_SUPPORTED:
269 default:
270 break;
273 return mode;
276 /**************************************************************************
277 * MCICDA_GetError [internal]
279 static int MCICDA_GetError(WINE_MCICDAUDIO* wmcda)
281 switch (GetLastError())
283 case ERROR_NOT_READY: return MCIERR_DEVICE_NOT_READY;
284 case ERROR_NOT_SUPPORTED:
285 case ERROR_IO_DEVICE: return MCIERR_HARDWARE;
286 default:
287 FIXME("Unknown mode %u\n", GetLastError());
289 return MCIERR_DRIVER_INTERNAL;
292 /**************************************************************************
293 * MCICDA_CalcFrame [internal]
295 static DWORD MCICDA_CalcFrame(WINE_MCICDAUDIO* wmcda, DWORD dwTime)
297 DWORD dwFrame = 0;
298 UINT wTrack;
299 CDROM_TOC toc;
300 DWORD br;
301 BYTE* addr;
303 TRACE("(%p, %08X, %u);\n", wmcda, wmcda->dwTimeFormat, dwTime);
305 switch (wmcda->dwTimeFormat) {
306 case MCI_FORMAT_MILLISECONDS:
307 dwFrame = ((dwTime - 1) * CDFRAMES_PERSEC + 500) / 1000;
308 TRACE("MILLISECONDS %u\n", dwFrame);
309 break;
310 case MCI_FORMAT_MSF:
311 TRACE("MSF %02u:%02u:%02u\n",
312 MCI_MSF_MINUTE(dwTime), MCI_MSF_SECOND(dwTime), MCI_MSF_FRAME(dwTime));
313 dwFrame += CDFRAMES_PERMIN * MCI_MSF_MINUTE(dwTime);
314 dwFrame += CDFRAMES_PERSEC * MCI_MSF_SECOND(dwTime);
315 dwFrame += MCI_MSF_FRAME(dwTime);
316 break;
317 case MCI_FORMAT_TMSF:
318 default: /* unknown format ! force TMSF ! ... */
319 wTrack = MCI_TMSF_TRACK(dwTime);
320 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
321 &toc, sizeof(toc), &br, NULL))
322 return 0;
323 if (wTrack < toc.FirstTrack || wTrack > toc.LastTrack)
324 return 0;
325 TRACE("MSF %02u-%02u:%02u:%02u\n",
326 MCI_TMSF_TRACK(dwTime), MCI_TMSF_MINUTE(dwTime),
327 MCI_TMSF_SECOND(dwTime), MCI_TMSF_FRAME(dwTime));
328 addr = toc.TrackData[wTrack - toc.FirstTrack].Address;
329 TRACE("TMSF trackpos[%u]=%d:%d:%d\n",
330 wTrack, addr[1], addr[2], addr[3]);
331 dwFrame = CDFRAMES_PERMIN * (addr[1] + MCI_TMSF_MINUTE(dwTime)) +
332 CDFRAMES_PERSEC * (addr[2] + MCI_TMSF_SECOND(dwTime)) +
333 addr[3] + MCI_TMSF_FRAME(dwTime);
334 break;
336 return dwFrame;
339 /**************************************************************************
340 * MCICDA_CalcTime [internal]
342 static DWORD MCICDA_CalcTime(WINE_MCICDAUDIO* wmcda, DWORD tf, DWORD dwFrame, LPDWORD lpRet)
344 DWORD dwTime = 0;
345 UINT wTrack;
346 UINT wMinutes;
347 UINT wSeconds;
348 UINT wFrames;
349 CDROM_TOC toc;
350 DWORD br;
352 TRACE("(%p, %08X, %u);\n", wmcda, tf, dwFrame);
354 switch (tf) {
355 case MCI_FORMAT_MILLISECONDS:
356 dwTime = (dwFrame * 1000) / CDFRAMES_PERSEC + 1;
357 TRACE("MILLISECONDS %u\n", dwTime);
358 *lpRet = 0;
359 break;
360 case MCI_FORMAT_MSF:
361 wMinutes = dwFrame / CDFRAMES_PERMIN;
362 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
363 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - CDFRAMES_PERSEC * wSeconds;
364 dwTime = MCI_MAKE_MSF(wMinutes, wSeconds, wFrames);
365 TRACE("MSF %02u:%02u:%02u -> dwTime=%u\n",
366 wMinutes, wSeconds, wFrames, dwTime);
367 *lpRet = MCI_COLONIZED3_RETURN;
368 break;
369 case MCI_FORMAT_TMSF:
370 default: /* unknown format ! force TMSF ! ... */
371 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
372 &toc, sizeof(toc), &br, NULL))
373 return 0;
374 if (dwFrame < FRAME_OF_TOC(toc, toc.FirstTrack) ||
375 dwFrame > FRAME_OF_TOC(toc, toc.LastTrack + 1)) {
376 ERR("Out of range value %u [%u,%u]\n",
377 dwFrame, FRAME_OF_TOC(toc, toc.FirstTrack),
378 FRAME_OF_TOC(toc, toc.LastTrack + 1));
379 *lpRet = 0;
380 return 0;
382 for (wTrack = toc.FirstTrack; wTrack <= toc.LastTrack; wTrack++) {
383 if (FRAME_OF_TOC(toc, wTrack) > dwFrame)
384 break;
386 wTrack--;
387 dwFrame -= FRAME_OF_TOC(toc, wTrack);
388 wMinutes = dwFrame / CDFRAMES_PERMIN;
389 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
390 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - CDFRAMES_PERSEC * wSeconds;
391 dwTime = MCI_MAKE_TMSF(wTrack, wMinutes, wSeconds, wFrames);
392 TRACE("%02u-%02u:%02u:%02u\n", wTrack, wMinutes, wSeconds, wFrames);
393 *lpRet = MCI_COLONIZED4_RETURN;
394 break;
396 return dwTime;
399 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
401 /**************************************************************************
402 * MCICDA_Open [internal]
404 static DWORD MCICDA_Open(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpOpenParms)
406 MCIDEVICEID dwDeviceID;
407 DWORD ret;
408 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
409 WCHAR root[7], drive = 0;
411 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpOpenParms);
413 if (lpOpenParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
414 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
416 dwDeviceID = lpOpenParms->wDeviceID;
418 if (wmcda->nUseCount > 0) {
419 /* The driver is already open on this channel */
420 /* If the driver was opened shareable before and this open specifies */
421 /* shareable then increment the use count */
422 if (wmcda->fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
423 ++wmcda->nUseCount;
424 else
425 return MCIERR_MUST_USE_SHAREABLE;
426 } else {
427 wmcda->nUseCount = 1;
428 wmcda->fShareable = dwFlags & MCI_OPEN_SHAREABLE;
430 if (dwFlags & MCI_OPEN_ELEMENT) {
431 if (dwFlags & MCI_OPEN_ELEMENT_ID) {
432 WARN("MCI_OPEN_ELEMENT_ID %p! Abort\n", lpOpenParms->lpstrElementName);
433 ret = MCIERR_FLAGS_NOT_COMPATIBLE;
434 goto the_error;
436 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms->lpstrElementName));
437 /* Only the first letter counts since w2k
438 * Win9x-NT accept only d: and w98SE accepts d:\foobar as well.
439 * Play d:\Track03.cda plays from the first track, not #3. */
440 if (!isalpha(lpOpenParms->lpstrElementName[0]))
442 ret = MCIERR_INVALID_FILE;
443 goto the_error;
445 drive = toupper(lpOpenParms->lpstrElementName[0]);
446 root[0] = drive; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
447 if (GetDriveTypeW(root) != DRIVE_CDROM)
449 ret = MCIERR_INVALID_FILE;
450 goto the_error;
453 else
455 root[0] = 'A'; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
456 for ( ; root[0] <= 'Z'; root[0]++)
458 if (GetDriveTypeW(root) == DRIVE_CDROM)
460 drive = root[0];
461 break;
464 if (!drive)
466 ret = MCIERR_CANNOT_LOAD_DRIVER; /* drvOpen should return this */
467 goto the_error;
471 wmcda->wNotifyDeviceID = dwDeviceID;
472 wmcda->dwTimeFormat = MCI_FORMAT_MSF;
474 /* now, open the handle */
475 root[0] = root[1] = '\\'; root[2] = '.'; root[3] = '\\'; root[4] = drive; root[5] = ':'; root[6] = '\0';
476 wmcda->handle = CreateFileW(root, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
477 if (wmcda->handle == INVALID_HANDLE_VALUE)
479 ret = MCIERR_MUST_USE_SHAREABLE;
480 goto the_error;
483 if (dwFlags & MCI_NOTIFY) {
484 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
485 dwDeviceID, MCI_NOTIFY_SUCCESSFUL);
487 return 0;
489 the_error:
490 --wmcda->nUseCount;
491 return ret;
494 /**************************************************************************
495 * MCICDA_Close [internal]
497 static DWORD MCICDA_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
499 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
501 TRACE("(%04X, %08X, %p);\n", wDevID, dwParam, lpParms);
503 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
505 MCICDA_Stop(wDevID, MCI_WAIT, NULL);
507 if (--wmcda->nUseCount == 0) {
508 CloseHandle(wmcda->handle);
510 if ((dwParam & MCI_NOTIFY) && lpParms)
511 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
512 return 0;
515 /**************************************************************************
516 * MCICDA_GetDevCaps [internal]
518 static DWORD MCICDA_GetDevCaps(UINT wDevID, DWORD dwFlags,
519 LPMCI_GETDEVCAPS_PARMS lpParms)
521 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
522 DWORD ret = 0;
524 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
526 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
527 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
529 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
530 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms->dwItem);
532 switch (lpParms->dwItem) {
533 case MCI_GETDEVCAPS_CAN_RECORD:
534 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
535 ret = MCI_RESOURCE_RETURNED;
536 break;
537 case MCI_GETDEVCAPS_HAS_AUDIO:
538 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
539 ret = MCI_RESOURCE_RETURNED;
540 break;
541 case MCI_GETDEVCAPS_HAS_VIDEO:
542 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
543 ret = MCI_RESOURCE_RETURNED;
544 break;
545 case MCI_GETDEVCAPS_DEVICE_TYPE:
546 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO, MCI_DEVTYPE_CD_AUDIO);
547 ret = MCI_RESOURCE_RETURNED;
548 break;
549 case MCI_GETDEVCAPS_USES_FILES:
550 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
551 ret = MCI_RESOURCE_RETURNED;
552 break;
553 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
554 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
555 ret = MCI_RESOURCE_RETURNED;
556 break;
557 case MCI_GETDEVCAPS_CAN_EJECT:
558 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
559 ret = MCI_RESOURCE_RETURNED;
560 break;
561 case MCI_GETDEVCAPS_CAN_PLAY:
562 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
563 ret = MCI_RESOURCE_RETURNED;
564 break;
565 case MCI_GETDEVCAPS_CAN_SAVE:
566 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
567 ret = MCI_RESOURCE_RETURNED;
568 break;
569 default:
570 WARN("Unsupported %x devCaps item\n", lpParms->dwItem);
571 return MCIERR_UNSUPPORTED_FUNCTION;
573 } else {
574 TRACE("No GetDevCaps-Item !\n");
575 return MCIERR_MISSING_PARAMETER;
577 TRACE("lpParms->dwReturn=%08X;\n", lpParms->dwReturn);
578 if (dwFlags & MCI_NOTIFY) {
579 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
581 return ret;
584 static DWORD CDROM_Audio_GetSerial(CDROM_TOC* toc)
586 DWORD serial = 0;
587 int i;
588 WORD wMagic;
589 DWORD dwStart, dwEnd;
592 * wMagic collects the wFrames from track 1
593 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
594 * frames.
595 * There it is collected for correcting the serial when there are less than
596 * 3 tracks.
598 wMagic = toc->TrackData[0].Address[3];
599 dwStart = FRAME_OF_TOC(*toc, toc->FirstTrack);
601 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++) {
602 serial += (toc->TrackData[i].Address[1] << 16) |
603 (toc->TrackData[i].Address[2] << 8) | toc->TrackData[i].Address[3];
605 dwEnd = FRAME_OF_TOC(*toc, toc->LastTrack + 1);
607 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
608 serial += wMagic + (dwEnd - dwStart);
610 return serial;
614 /**************************************************************************
615 * MCICDA_Info [internal]
617 static DWORD MCICDA_Info(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
619 LPCWSTR str = NULL;
620 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
621 DWORD ret = 0;
622 WCHAR buffer[16];
624 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
626 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
627 return MCIERR_NULL_PARAMETER_BLOCK;
628 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
630 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
632 if (dwFlags & MCI_INFO_PRODUCT) {
633 static const WCHAR wszAudioCd[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
634 str = wszAudioCd;
635 } else if (dwFlags & MCI_INFO_MEDIA_UPC) {
636 ret = MCIERR_NO_IDENTITY;
637 } else if (dwFlags & MCI_INFO_MEDIA_IDENTITY) {
638 DWORD res = 0;
639 CDROM_TOC toc;
640 DWORD br;
641 static const WCHAR wszLu[] = {'%','l','u',0};
643 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
644 &toc, sizeof(toc), &br, NULL)) {
645 return MCICDA_GetError(wmcda);
648 res = CDROM_Audio_GetSerial(&toc);
649 sprintfW(buffer, wszLu, res);
650 str = buffer;
651 } else {
652 WARN("Don't know this info command (%u)\n", dwFlags);
653 ret = MCIERR_MISSING_PARAMETER;
655 if (!ret) {
656 TRACE("=> %s\n", debugstr_w(str));
657 if (lpParms->dwRetSize) {
658 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
659 * to the number of characters written, excluding \0. */
660 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize);
661 } else ret = MCIERR_PARAM_OVERFLOW;
663 if (MMSYSERR_NOERROR==ret && (dwFlags & MCI_NOTIFY))
664 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
665 return ret;
668 /**************************************************************************
669 * MCICDA_Status [internal]
671 static DWORD MCICDA_Status(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
673 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
674 DWORD ret = 0;
675 CDROM_SUB_Q_DATA_FORMAT fmt;
676 SUB_Q_CHANNEL_DATA data;
677 CDROM_TOC toc;
678 DWORD br;
680 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
682 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
683 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
685 if (dwFlags & MCI_STATUS_ITEM) {
686 TRACE("dwItem = %x\n", lpParms->dwItem);
687 switch (lpParms->dwItem) {
688 case MCI_STATUS_CURRENT_TRACK:
689 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
690 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
691 &data, sizeof(data), &br, NULL))
693 return MCICDA_GetError(wmcda);
694 /* alt. data.CurrentPosition.TrackNumber = 1; -- what native yields */
696 lpParms->dwReturn = data.CurrentPosition.TrackNumber;
697 TRACE("CURRENT_TRACK=%lu\n", lpParms->dwReturn);
698 break;
699 case MCI_STATUS_LENGTH:
700 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
701 &toc, sizeof(toc), &br, NULL)) {
702 WARN("error reading TOC !\n");
703 return MCICDA_GetError(wmcda);
705 if (dwFlags & MCI_TRACK) {
706 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms->dwTrack);
707 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
708 return MCIERR_OUTOFRANGE;
709 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack + 1) -
710 FRAME_OF_TOC(toc, lpParms->dwTrack);
711 /* Windows returns one frame less than the total track length for the
712 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
713 if (lpParms->dwTrack == toc.LastTrack)
714 lpParms->dwReturn--;
715 } else {
716 /* Sum of the lengths of all of the tracks. Inherits the
717 'off by one frame' behavior from the length of the last track.
718 See above comment. */
719 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.LastTrack + 1) -
720 FRAME_OF_TOC(toc, toc.FirstTrack) - 1;
722 lpParms->dwReturn = MCICDA_CalcTime(wmcda,
723 (wmcda->dwTimeFormat == MCI_FORMAT_TMSF)
724 ? MCI_FORMAT_MSF : wmcda->dwTimeFormat,
725 lpParms->dwReturn,
726 &ret);
727 TRACE("LENGTH=%lu\n", lpParms->dwReturn);
728 break;
729 case MCI_STATUS_MODE:
730 lpParms->dwReturn = MCICDA_GetStatus(wmcda);
731 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms->dwReturn);
732 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn, lpParms->dwReturn);
733 ret = MCI_RESOURCE_RETURNED;
734 break;
735 case MCI_STATUS_MEDIA_PRESENT:
736 lpParms->dwReturn = (MCICDA_GetStatus(wmcda) == MCI_MODE_OPEN) ?
737 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
738 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms->dwReturn) ? 'Y' : 'N');
739 ret = MCI_RESOURCE_RETURNED;
740 break;
741 case MCI_STATUS_NUMBER_OF_TRACKS:
742 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
743 &toc, sizeof(toc), &br, NULL)) {
744 WARN("error reading TOC !\n");
745 return MCICDA_GetError(wmcda);
747 lpParms->dwReturn = toc.LastTrack - toc.FirstTrack + 1;
748 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms->dwReturn);
749 if (lpParms->dwReturn == (WORD)-1)
750 return MCICDA_GetError(wmcda);
751 break;
752 case MCI_STATUS_POSITION:
753 switch (dwFlags & (MCI_STATUS_START | MCI_TRACK)) {
754 case MCI_STATUS_START:
755 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
756 &toc, sizeof(toc), &br, NULL)) {
757 WARN("error reading TOC !\n");
758 return MCICDA_GetError(wmcda);
760 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.FirstTrack);
761 TRACE("get MCI_STATUS_START !\n");
762 break;
763 case MCI_TRACK:
764 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
765 &toc, sizeof(toc), &br, NULL)) {
766 WARN("error reading TOC !\n");
767 return MCICDA_GetError(wmcda);
769 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
770 return MCIERR_OUTOFRANGE;
771 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack);
772 TRACE("get MCI_TRACK #%u !\n", lpParms->dwTrack);
773 break;
774 case 0:
775 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
776 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
777 &data, sizeof(data), &br, NULL)) {
778 return MCICDA_GetError(wmcda);
780 lpParms->dwReturn = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
781 break;
782 default:
783 return MCIERR_FLAGS_NOT_COMPATIBLE;
785 lpParms->dwReturn = MCICDA_CalcTime(wmcda, wmcda->dwTimeFormat, lpParms->dwReturn, &ret);
786 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms->dwReturn);
787 break;
788 case MCI_STATUS_READY:
789 TRACE("MCI_STATUS_READY !\n");
790 switch (MCICDA_GetStatus(wmcda))
792 case MCI_MODE_NOT_READY:
793 case MCI_MODE_OPEN:
794 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
795 break;
796 default:
797 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
798 break;
800 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms->dwReturn));
801 ret = MCI_RESOURCE_RETURNED;
802 break;
803 case MCI_STATUS_TIME_FORMAT:
804 lpParms->dwReturn = MAKEMCIRESOURCE(wmcda->dwTimeFormat, MCI_FORMAT_RETURN_BASE + wmcda->dwTimeFormat);
805 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms->dwReturn));
806 ret = MCI_RESOURCE_RETURNED;
807 break;
808 case 4001: /* FIXME: for bogus FullCD */
809 case MCI_CDA_STATUS_TYPE_TRACK:
810 if (!(dwFlags & MCI_TRACK))
811 ret = MCIERR_MISSING_PARAMETER;
812 else {
813 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
814 &toc, sizeof(toc), &br, NULL)) {
815 WARN("error reading TOC !\n");
816 return MCICDA_GetError(wmcda);
818 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
819 ret = MCIERR_OUTOFRANGE;
820 else
821 lpParms->dwReturn = (toc.TrackData[lpParms->dwTrack - toc.FirstTrack].Control & 0x04) ?
822 MCI_CDA_TRACK_OTHER : MCI_CDA_TRACK_AUDIO;
823 /* FIXME: MAKEMCIRESOURCE "audio" | "other", localised */
825 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms->dwTrack, lpParms->dwReturn);
826 break;
827 default:
828 FIXME("unknown command %08X !\n", lpParms->dwItem);
829 return MCIERR_UNSUPPORTED_FUNCTION;
831 } else return MCIERR_MISSING_PARAMETER;
832 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
833 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
834 return ret;
837 /**************************************************************************
838 * MCICDA_SkipDataTracks [internal]
840 static DWORD MCICDA_SkipDataTracks(WINE_MCICDAUDIO* wmcda,DWORD *frame)
842 int i;
843 DWORD br;
844 CDROM_TOC toc;
845 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
846 &toc, sizeof(toc), &br, NULL)) {
847 WARN("error reading TOC !\n");
848 return MCICDA_GetError(wmcda);
850 if (*frame < FRAME_OF_TOC(toc,toc.FirstTrack) ||
851 *frame >= FRAME_OF_TOC(toc,toc.LastTrack+1)) /* lead-out */
852 return MCIERR_OUTOFRANGE;
853 for(i=toc.LastTrack+1;i>toc.FirstTrack;i--)
854 if ( FRAME_OF_TOC(toc, i) <= *frame ) break;
855 /* i points to last track whose start address is not greater than frame.
856 * Now skip non-audio tracks */
857 for(;i<=toc.LastTrack;i++)
858 if ( ! (toc.TrackData[i-toc.FirstTrack].Control & 4) )
859 break;
860 /* The frame will be an address in the next audio track or
861 * address of lead-out. */
862 if ( FRAME_OF_TOC(toc, i) > *frame )
863 *frame = FRAME_OF_TOC(toc, i);
864 /* Lead-out is an invalid seek position (on Linux as well). */
865 if (*frame == FRAME_OF_TOC(toc,toc.LastTrack+1))
866 (*frame)--;
867 return 0;
870 /**************************************************************************
871 * MCICDA_Play [internal]
873 static DWORD MCICDA_Play(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
875 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
876 DWORD ret = 0, start, end;
877 HANDLE oldcb;
878 DWORD br;
879 CDROM_PLAY_AUDIO_MSF play;
880 CDROM_SUB_Q_DATA_FORMAT fmt;
881 SUB_Q_CHANNEL_DATA data;
882 CDROM_TOC toc;
884 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
886 if (lpParms == NULL)
887 return MCIERR_NULL_PARAMETER_BLOCK;
889 if (wmcda == NULL)
890 return MCIERR_INVALID_DEVICE_ID;
892 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
893 &toc, sizeof(toc), &br, NULL)) {
894 WARN("error reading TOC !\n");
895 return MCICDA_GetError(wmcda);
898 if (dwFlags & MCI_FROM) {
899 start = MCICDA_CalcFrame(wmcda, lpParms->dwFrom);
900 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
901 return ret;
902 TRACE("MCI_FROM=%08X -> %u\n", lpParms->dwFrom, start);
903 } else {
904 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
905 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
906 &data, sizeof(data), &br, NULL)) {
907 return MCICDA_GetError(wmcda);
909 start = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
910 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
911 return ret;
913 if (dwFlags & MCI_TO) {
914 end = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
915 if ( (ret=MCICDA_SkipDataTracks(wmcda, &end)) )
916 return ret;
917 TRACE("MCI_TO=%08X -> %u\n", lpParms->dwTo, end);
918 } else {
919 end = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
921 if (end < start) return MCIERR_OUTOFRANGE;
922 TRACE("Playing from %u to %u\n", start, end);
924 oldcb = InterlockedExchangePointer(&wmcda->hCallback,
925 (dwFlags & MCI_NOTIFY) ? HWND_32(LOWORD(lpParms->dwCallback)) : NULL);
926 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
928 if (start == end || start == FRAME_OF_TOC(toc,toc.LastTrack+1)-1) {
929 if (dwFlags & MCI_NOTIFY) {
930 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
931 if (oldcb) mciDriverNotify(oldcb, wDevID, MCI_NOTIFY_SUCCESSFUL);
933 return MMSYSERR_NOERROR;
936 if (wmcda->hThread != 0) {
937 SetEvent(wmcda->stopEvent);
938 WaitForSingleObject(wmcda->hThread, INFINITE);
940 CloseHandle(wmcda->hThread);
941 wmcda->hThread = 0;
942 CloseHandle(wmcda->stopEvent);
943 wmcda->stopEvent = 0;
945 IDirectSoundBuffer_Stop(wmcda->dsBuf);
946 IDirectSoundBuffer_Release(wmcda->dsBuf);
947 wmcda->dsBuf = NULL;
948 IDirectSound_Release(wmcda->dsObj);
949 wmcda->dsObj = NULL;
952 if (pDirectSoundCreate) {
953 WAVEFORMATEX format;
954 DSBUFFERDESC desc;
955 DWORD lockLen;
956 void *cdData;
957 HRESULT hr;
959 hr = pDirectSoundCreate(NULL, &wmcda->dsObj, NULL);
960 if (SUCCEEDED(hr)) {
961 IDirectSound_SetCooperativeLevel(wmcda->dsObj, GetDesktopWindow(), DSSCL_PRIORITY);
963 /* The "raw" frame is relative to the start of the first track */
964 wmcda->start = start - FRAME_OF_TOC(toc, toc.FirstTrack);
965 wmcda->end = end - FRAME_OF_TOC(toc, toc.FirstTrack);
967 memset(&format, 0, sizeof(format));
968 format.wFormatTag = WAVE_FORMAT_PCM;
969 format.nChannels = 2;
970 format.nSamplesPerSec = 44100;
971 format.wBitsPerSample = 16;
972 format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
973 format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
974 format.cbSize = 0;
976 memset(&desc, 0, sizeof(desc));
977 desc.dwSize = sizeof(desc);
978 desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
979 desc.dwBufferBytes = (CDDA_FRAG_SIZE - (CDDA_FRAG_SIZE%RAW_SECTOR_SIZE)) * CDDA_FRAG_COUNT;
980 desc.lpwfxFormat = &format;
982 hr = IDirectSound_CreateSoundBuffer(wmcda->dsObj, &desc, &wmcda->dsBuf, NULL);
984 if (SUCCEEDED(hr)) {
985 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, 0, 0, &cdData, &lockLen,
986 NULL, NULL, DSBLOCK_ENTIREBUFFER);
988 if (SUCCEEDED(hr)) {
989 RAW_READ_INFO rdInfo;
990 int readok;
992 rdInfo.DiskOffset.QuadPart = wmcda->start<<11;
993 rdInfo.SectorCount = min(desc.dwBufferBytes/RAW_SECTOR_SIZE,
994 wmcda->end-wmcda->start);
995 rdInfo.TrackMode = CDDA;
997 readok = DeviceIoControl(wmcda->handle, IOCTL_CDROM_RAW_READ,
998 &rdInfo, sizeof(rdInfo), cdData, lockLen,
999 &br, NULL);
1000 IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
1002 if (readok) {
1003 wmcda->start += rdInfo.SectorCount;
1004 wmcda->stopEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
1006 if (wmcda->stopEvent != 0)
1007 wmcda->hThread = CreateThread(NULL, 0, MCICDA_playLoop, wmcda, 0, &br);
1008 if (wmcda->hThread != 0) {
1009 hr = IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING);
1010 if (SUCCEEDED(hr)) {
1011 /* FIXME: implement MCI_WAIT and send notification only in that case */
1012 if (0) {
1013 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1014 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID,
1015 FAILED(hr) ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1017 return ret;
1020 SetEvent(wmcda->stopEvent);
1021 WaitForSingleObject(wmcda->hThread, INFINITE);
1022 CloseHandle(wmcda->hThread);
1023 wmcda->hThread = 0;
1027 if (wmcda->stopEvent != 0) {
1028 CloseHandle(wmcda->stopEvent);
1029 wmcda->stopEvent = 0;
1031 if (wmcda->dsBuf) {
1032 IDirectSoundBuffer_Release(wmcda->dsBuf);
1033 wmcda->dsBuf = NULL;
1035 if (wmcda->dsObj) {
1036 IDirectSound_Release(wmcda->dsObj);
1037 wmcda->dsObj = NULL;
1041 play.StartingM = start / CDFRAMES_PERMIN;
1042 play.StartingS = (start / CDFRAMES_PERSEC) % 60;
1043 play.StartingF = start % CDFRAMES_PERSEC;
1044 play.EndingM = end / CDFRAMES_PERMIN;
1045 play.EndingS = (end / CDFRAMES_PERSEC) % 60;
1046 play.EndingF = end % CDFRAMES_PERSEC;
1047 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PLAY_AUDIO_MSF, &play, sizeof(play),
1048 NULL, 0, &br, NULL)) {
1049 wmcda->hCallback = NULL;
1050 ret = MCIERR_HARDWARE;
1052 /* The independent CD player has no means to signal MCI_NOTIFY when it's done.
1053 * Native sends a notification with MCI_WAIT only. */
1054 return ret;
1057 /**************************************************************************
1058 * MCICDA_Stop [internal]
1060 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1062 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1063 HANDLE oldcb;
1064 DWORD br;
1066 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1068 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1070 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1071 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1073 if (wmcda->hThread != 0) {
1074 SetEvent(wmcda->stopEvent);
1075 WaitForSingleObject(wmcda->hThread, INFINITE);
1077 CloseHandle(wmcda->hThread);
1078 wmcda->hThread = 0;
1079 CloseHandle(wmcda->stopEvent);
1080 wmcda->stopEvent = 0;
1082 IDirectSoundBuffer_Release(wmcda->dsBuf);
1083 wmcda->dsBuf = NULL;
1084 IDirectSound_Release(wmcda->dsObj);
1085 wmcda->dsObj = NULL;
1087 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_STOP_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1088 return MCIERR_HARDWARE;
1090 if ((dwFlags & MCI_NOTIFY) && lpParms)
1091 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1092 return 0;
1095 /**************************************************************************
1096 * MCICDA_Pause [internal]
1098 static DWORD MCICDA_Pause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1100 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1101 HANDLE oldcb;
1102 DWORD br;
1104 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1106 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1108 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1109 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1111 if (wmcda->hThread != 0) {
1112 /* Don't bother calling stop if the playLoop thread has already stopped */
1113 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1114 FAILED(IDirectSoundBuffer_Stop(wmcda->dsBuf)))
1115 return MCIERR_HARDWARE;
1117 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1118 return MCIERR_HARDWARE;
1120 if ((dwFlags & MCI_NOTIFY) && lpParms)
1121 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1122 return 0;
1125 /**************************************************************************
1126 * MCICDA_Resume [internal]
1128 static DWORD MCICDA_Resume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1130 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1131 DWORD br;
1133 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1135 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1137 if (wmcda->hThread != 0) {
1138 /* Don't restart if the playLoop thread has already stopped */
1139 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1140 FAILED(IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING)))
1141 return MCIERR_HARDWARE;
1143 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_RESUME_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1144 return MCIERR_HARDWARE;
1146 if ((dwFlags & MCI_NOTIFY) && lpParms)
1147 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1148 return 0;
1151 /**************************************************************************
1152 * MCICDA_Seek [internal]
1154 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1156 DWORD at;
1157 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1158 CDROM_SEEK_AUDIO_MSF seek;
1159 DWORD br, position, ret;
1160 CDROM_TOC toc;
1162 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1164 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1165 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1167 position = dwFlags & (MCI_SEEK_TO_START|MCI_SEEK_TO_END|MCI_TO);
1168 if (!position) return MCIERR_MISSING_PARAMETER;
1169 if (position&(position-1)) return MCIERR_FLAGS_NOT_COMPATIBLE;
1171 /* Stop sends MCI_NOTIFY_ABORTED when needed.
1172 * Tests show that native first sends ABORTED and reads the TOC,
1173 * then only checks the position flags, then stops and seeks. */
1174 MCICDA_Stop(wDevID, MCI_WAIT, 0);
1176 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
1177 &toc, sizeof(toc), &br, NULL)) {
1178 WARN("error reading TOC !\n");
1179 return MCICDA_GetError(wmcda);
1181 switch (position) {
1182 case MCI_SEEK_TO_START:
1183 TRACE("Seeking to start\n");
1184 at = FRAME_OF_TOC(toc,toc.FirstTrack);
1185 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1186 return ret;
1187 break;
1188 case MCI_SEEK_TO_END:
1189 TRACE("Seeking to end\n");
1190 /* End is prior to lead-out
1191 * yet Win9X seeks to even one frame less than that. */
1192 at = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
1193 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1194 return ret;
1195 break;
1196 case MCI_TO:
1197 TRACE("Seeking to %u\n", lpParms->dwTo);
1198 at = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
1199 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1200 return ret;
1201 break;
1202 default:
1203 return MCIERR_FLAGS_NOT_COMPATIBLE;
1207 seek.M = at / CDFRAMES_PERMIN;
1208 seek.S = (at / CDFRAMES_PERSEC) % 60;
1209 seek.F = at % CDFRAMES_PERSEC;
1210 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_SEEK_AUDIO_MSF, &seek, sizeof(seek),
1211 NULL, 0, &br, NULL))
1212 return MCIERR_HARDWARE;
1215 if (dwFlags & MCI_NOTIFY)
1216 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1217 return 0;
1220 /**************************************************************************
1221 * MCICDA_SetDoor [internal]
1223 static DWORD MCICDA_SetDoor(UINT wDevID, BOOL open)
1225 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1226 DWORD br;
1228 TRACE("(%04x, %s) !\n", wDevID, (open) ? "OPEN" : "CLOSE");
1230 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1232 if (!DeviceIoControl(wmcda->handle,
1233 (open) ? IOCTL_STORAGE_EJECT_MEDIA : IOCTL_STORAGE_LOAD_MEDIA,
1234 NULL, 0, NULL, 0, &br, NULL))
1235 return MCIERR_HARDWARE;
1237 return 0;
1240 /**************************************************************************
1241 * MCICDA_Set [internal]
1243 static DWORD MCICDA_Set(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1245 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1247 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1249 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1251 if (dwFlags & MCI_SET_DOOR_OPEN) {
1252 MCICDA_SetDoor(wDevID, TRUE);
1254 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1255 MCICDA_SetDoor(wDevID, FALSE);
1258 /* only functions which require valid lpParms below this line ! */
1259 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1261 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1263 if (dwFlags & MCI_SET_TIME_FORMAT) {
1264 switch (lpParms->dwTimeFormat) {
1265 case MCI_FORMAT_MILLISECONDS:
1266 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1267 break;
1268 case MCI_FORMAT_MSF:
1269 TRACE("MCI_FORMAT_MSF !\n");
1270 break;
1271 case MCI_FORMAT_TMSF:
1272 TRACE("MCI_FORMAT_TMSF !\n");
1273 break;
1274 default:
1275 return MCIERR_BAD_TIME_FORMAT;
1277 wmcda->dwTimeFormat = lpParms->dwTimeFormat;
1279 if (dwFlags & MCI_SET_AUDIO) /* one xp machine ignored it */
1280 TRACE("SET_AUDIO %X %x\n", dwFlags, lpParms->dwAudio);
1282 if (dwFlags & MCI_NOTIFY)
1283 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1284 return 0;
1287 /**************************************************************************
1288 * DriverProc (MCICDA.@)
1290 LRESULT CALLBACK MCICDA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1291 LPARAM dwParam1, LPARAM dwParam2)
1293 switch(wMsg) {
1294 case DRV_LOAD: return 1;
1295 case DRV_FREE: return 1;
1296 case DRV_OPEN: return MCICDA_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1297 case DRV_CLOSE: return MCICDA_drvClose(dwDevID);
1298 case DRV_ENABLE: return 1;
1299 case DRV_DISABLE: return 1;
1300 case DRV_QUERYCONFIGURE: return 1;
1301 case DRV_CONFIGURE: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK); return 1;
1302 case DRV_INSTALL: return DRVCNF_RESTART;
1303 case DRV_REMOVE: return DRVCNF_RESTART;
1306 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1308 switch (wMsg) {
1309 case MCI_OPEN_DRIVER: return MCICDA_Open(dwDevID, dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1310 case MCI_CLOSE_DRIVER: return MCICDA_Close(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1311 case MCI_GETDEVCAPS: return MCICDA_GetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1312 case MCI_INFO: return MCICDA_Info(dwDevID, dwParam1, (LPMCI_INFO_PARMSW)dwParam2);
1313 case MCI_STATUS: return MCICDA_Status(dwDevID, dwParam1, (LPMCI_STATUS_PARMS)dwParam2);
1314 case MCI_SET: return MCICDA_Set(dwDevID, dwParam1, (LPMCI_SET_PARMS)dwParam2);
1315 case MCI_PLAY: return MCICDA_Play(dwDevID, dwParam1, (LPMCI_PLAY_PARMS)dwParam2);
1316 case MCI_STOP: return MCICDA_Stop(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1317 case MCI_PAUSE: return MCICDA_Pause(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1318 case MCI_RESUME: return MCICDA_Resume(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1319 case MCI_SEEK: return MCICDA_Seek(dwDevID, dwParam1, (LPMCI_SEEK_PARMS)dwParam2);
1320 /* commands that should report an error as they are not supported in
1321 * the native version */
1322 case MCI_RECORD:
1323 case MCI_LOAD:
1324 case MCI_SAVE:
1325 return MCIERR_UNSUPPORTED_FUNCTION;
1326 case MCI_BREAK:
1327 case MCI_FREEZE:
1328 case MCI_PUT:
1329 case MCI_REALIZE:
1330 case MCI_UNFREEZE:
1331 case MCI_UPDATE:
1332 case MCI_WHERE:
1333 case MCI_STEP:
1334 case MCI_SPIN:
1335 case MCI_ESCAPE:
1336 case MCI_COPY:
1337 case MCI_CUT:
1338 case MCI_DELETE:
1339 case MCI_PASTE:
1340 case MCI_WINDOW:
1341 TRACE("Unsupported command [0x%x]\n", wMsg);
1342 break;
1343 case MCI_OPEN:
1344 case MCI_CLOSE:
1345 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1346 break;
1347 default:
1348 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg);
1349 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1351 return MCIERR_UNRECOGNIZED_COMMAND;
1354 /*-----------------------------------------------------------------------*/