Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / mcicda / mcicda.c
blob92c943584e4f9a9aa413c82d82b7e38df2d563c6
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 WORD 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 EnterCriticalSection(&wmcda->cs);
145 if (wmcda->hCallback) {
146 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID,
147 FAILED(hr) ? MCI_NOTIFY_FAILURE :
148 ((endPos!=lastPos) ? MCI_NOTIFY_ABORTED :
149 MCI_NOTIFY_SUCCESSFUL));
150 wmcda->hCallback = NULL;
152 LeaveCriticalSection(&wmcda->cs);
154 ExitThread(0);
155 return 0;
160 /**************************************************************************
161 * MCICDA_drvOpen [internal]
163 static DWORD MCICDA_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
165 static HMODULE dsHandle;
166 WINE_MCICDAUDIO* wmcda;
168 if (!modp) return 0xFFFFFFFF;
170 wmcda = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCICDAUDIO));
172 if (!wmcda)
173 return 0;
175 if (!dsHandle) {
176 dsHandle = LoadLibraryA("dsound.dll");
177 if(dsHandle)
178 pDirectSoundCreate = (LPDIRECTSOUNDCREATE)GetProcAddress(dsHandle, "DirectSoundCreate");
181 wmcda->wDevID = modp->wDeviceID;
182 mciSetDriverData(wmcda->wDevID, (DWORD_PTR)wmcda);
183 modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
184 modp->wType = MCI_DEVTYPE_CD_AUDIO;
185 InitializeCriticalSection(&wmcda->cs);
186 return modp->wDeviceID;
189 /**************************************************************************
190 * MCICDA_drvClose [internal]
192 static DWORD MCICDA_drvClose(DWORD dwDevID)
194 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(dwDevID);
196 if (wmcda) {
197 DeleteCriticalSection(&wmcda->cs);
198 HeapFree(GetProcessHeap(), 0, wmcda);
199 mciSetDriverData(dwDevID, 0);
201 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
204 /**************************************************************************
205 * MCICDA_GetOpenDrv [internal]
207 static WINE_MCICDAUDIO* MCICDA_GetOpenDrv(UINT wDevID)
209 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
211 if (wmcda == NULL || wmcda->nUseCount == 0) {
212 WARN("Invalid wDevID=%u\n", wDevID);
213 return 0;
215 return wmcda;
218 /**************************************************************************
219 * MCICDA_GetStatus [internal]
221 static DWORD MCICDA_GetStatus(WINE_MCICDAUDIO* wmcda)
223 CDROM_SUB_Q_DATA_FORMAT fmt;
224 SUB_Q_CHANNEL_DATA data;
225 DWORD br;
226 DWORD mode = MCI_MODE_NOT_READY;
228 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
229 if(wmcda->hThread != 0) {
230 DWORD status;
231 HRESULT hr;
233 hr = IDirectSoundBuffer_GetStatus(wmcda->dsBuf, &status);
234 if(SUCCEEDED(hr)) {
235 if(!(status&DSBSTATUS_PLAYING)) {
236 if(WaitForSingleObject(wmcda->stopEvent, 0) == WAIT_OBJECT_0)
237 mode = MCI_MODE_STOP;
238 else
239 mode = MCI_MODE_PAUSE;
241 else
242 mode = MCI_MODE_PLAY;
245 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
246 &data, sizeof(data), &br, NULL)) {
247 if (GetLastError() == ERROR_NOT_READY) mode = MCI_MODE_OPEN;
248 } else {
249 switch (data.CurrentPosition.Header.AudioStatus)
251 case AUDIO_STATUS_IN_PROGRESS: mode = MCI_MODE_PLAY; break;
252 case AUDIO_STATUS_PAUSED: mode = MCI_MODE_PAUSE; break;
253 case AUDIO_STATUS_NO_STATUS:
254 case AUDIO_STATUS_PLAY_COMPLETE: mode = MCI_MODE_STOP; break;
255 case AUDIO_STATUS_PLAY_ERROR:
256 case AUDIO_STATUS_NOT_SUPPORTED:
257 default:
258 break;
261 return mode;
264 /**************************************************************************
265 * MCICDA_GetError [internal]
267 static int MCICDA_GetError(WINE_MCICDAUDIO* wmcda)
269 switch (GetLastError())
271 case ERROR_NOT_READY: return MCIERR_DEVICE_NOT_READY;
272 case ERROR_IO_DEVICE: return MCIERR_HARDWARE;
273 default:
274 FIXME("Unknown mode %u\n", GetLastError());
276 return MCIERR_DRIVER_INTERNAL;
279 /**************************************************************************
280 * MCICDA_CalcFrame [internal]
282 static DWORD MCICDA_CalcFrame(WINE_MCICDAUDIO* wmcda, DWORD dwTime)
284 DWORD dwFrame = 0;
285 UINT wTrack;
286 CDROM_TOC toc;
287 DWORD br;
288 BYTE* addr;
290 TRACE("(%p, %08X, %u);\n", wmcda, wmcda->dwTimeFormat, dwTime);
292 switch (wmcda->dwTimeFormat) {
293 case MCI_FORMAT_MILLISECONDS:
294 dwFrame = ((dwTime - 1) * CDFRAMES_PERSEC + 500) / 1000;
295 TRACE("MILLISECONDS %u\n", dwFrame);
296 break;
297 case MCI_FORMAT_MSF:
298 TRACE("MSF %02u:%02u:%02u\n",
299 MCI_MSF_MINUTE(dwTime), MCI_MSF_SECOND(dwTime), MCI_MSF_FRAME(dwTime));
300 dwFrame += CDFRAMES_PERMIN * MCI_MSF_MINUTE(dwTime);
301 dwFrame += CDFRAMES_PERSEC * MCI_MSF_SECOND(dwTime);
302 dwFrame += MCI_MSF_FRAME(dwTime);
303 break;
304 case MCI_FORMAT_TMSF:
305 default: /* unknown format ! force TMSF ! ... */
306 wTrack = MCI_TMSF_TRACK(dwTime);
307 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
308 &toc, sizeof(toc), &br, NULL))
309 return 0;
310 if (wTrack < toc.FirstTrack || wTrack > toc.LastTrack)
311 return 0;
312 TRACE("MSF %02u-%02u:%02u:%02u\n",
313 MCI_TMSF_TRACK(dwTime), MCI_TMSF_MINUTE(dwTime),
314 MCI_TMSF_SECOND(dwTime), MCI_TMSF_FRAME(dwTime));
315 addr = toc.TrackData[wTrack - toc.FirstTrack].Address;
316 TRACE("TMSF trackpos[%u]=%d:%d:%d\n",
317 wTrack, addr[1], addr[2], addr[3]);
318 dwFrame = CDFRAMES_PERMIN * (addr[1] + MCI_TMSF_MINUTE(dwTime)) +
319 CDFRAMES_PERSEC * (addr[2] + MCI_TMSF_SECOND(dwTime)) +
320 addr[3] + MCI_TMSF_FRAME(dwTime);
321 break;
323 return dwFrame;
326 /**************************************************************************
327 * MCICDA_CalcTime [internal]
329 static DWORD MCICDA_CalcTime(WINE_MCICDAUDIO* wmcda, DWORD tf, DWORD dwFrame, LPDWORD lpRet)
331 DWORD dwTime = 0;
332 UINT wTrack;
333 UINT wMinutes;
334 UINT wSeconds;
335 UINT wFrames;
336 CDROM_TOC toc;
337 DWORD br;
339 TRACE("(%p, %08X, %u);\n", wmcda, tf, dwFrame);
341 switch (tf) {
342 case MCI_FORMAT_MILLISECONDS:
343 dwTime = (dwFrame * 1000) / CDFRAMES_PERSEC + 1;
344 TRACE("MILLISECONDS %u\n", dwTime);
345 *lpRet = 0;
346 break;
347 case MCI_FORMAT_MSF:
348 wMinutes = dwFrame / CDFRAMES_PERMIN;
349 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
350 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - CDFRAMES_PERSEC * wSeconds;
351 dwTime = MCI_MAKE_MSF(wMinutes, wSeconds, wFrames);
352 TRACE("MSF %02u:%02u:%02u -> dwTime=%u\n",
353 wMinutes, wSeconds, wFrames, dwTime);
354 *lpRet = MCI_COLONIZED3_RETURN;
355 break;
356 case MCI_FORMAT_TMSF:
357 default: /* unknown format ! force TMSF ! ... */
358 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
359 &toc, sizeof(toc), &br, NULL))
360 return 0;
361 if (dwFrame < FRAME_OF_TOC(toc, toc.FirstTrack) ||
362 dwFrame > FRAME_OF_TOC(toc, toc.LastTrack + 1)) {
363 ERR("Out of range value %u [%u,%u]\n",
364 dwFrame, FRAME_OF_TOC(toc, toc.FirstTrack),
365 FRAME_OF_TOC(toc, toc.LastTrack + 1));
366 *lpRet = 0;
367 return 0;
369 for (wTrack = toc.FirstTrack; wTrack <= toc.LastTrack; wTrack++) {
370 if (FRAME_OF_TOC(toc, wTrack) > dwFrame)
371 break;
373 wTrack--;
374 dwFrame -= FRAME_OF_TOC(toc, wTrack);
375 wMinutes = dwFrame / CDFRAMES_PERMIN;
376 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
377 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - CDFRAMES_PERSEC * wSeconds;
378 dwTime = MCI_MAKE_TMSF(wTrack, wMinutes, wSeconds, wFrames);
379 TRACE("%02u-%02u:%02u:%02u\n", wTrack, wMinutes, wSeconds, wFrames);
380 *lpRet = MCI_COLONIZED4_RETURN;
381 break;
383 return dwTime;
386 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms);
387 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
389 /**************************************************************************
390 * MCICDA_Open [internal]
392 static DWORD MCICDA_Open(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpOpenParms)
394 DWORD dwDeviceID;
395 DWORD ret = MCIERR_HARDWARE;
396 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
397 WCHAR root[7], drive = 0;
398 unsigned int count;
400 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpOpenParms);
402 if (lpOpenParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
403 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
405 dwDeviceID = lpOpenParms->wDeviceID;
407 if (wmcda->nUseCount > 0) {
408 /* The driver is already open on this channel */
409 /* If the driver was opened shareable before and this open specifies */
410 /* shareable then increment the use count */
411 if (wmcda->fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
412 ++wmcda->nUseCount;
413 else
414 return MCIERR_MUST_USE_SHAREABLE;
415 } else {
416 wmcda->nUseCount = 1;
417 wmcda->fShareable = dwFlags & MCI_OPEN_SHAREABLE;
419 if (dwFlags & MCI_OPEN_ELEMENT) {
420 if (dwFlags & MCI_OPEN_ELEMENT_ID) {
421 WARN("MCI_OPEN_ELEMENT_ID %p! Abort\n", lpOpenParms->lpstrElementName);
422 ret = MCIERR_NO_ELEMENT_ALLOWED;
423 goto the_error;
425 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms->lpstrElementName));
426 if (!isalpha(lpOpenParms->lpstrElementName[0]) || lpOpenParms->lpstrElementName[1] != ':' ||
427 (lpOpenParms->lpstrElementName[2] && lpOpenParms->lpstrElementName[2] != '\\'))
429 WARN("MCI_OPEN_ELEMENT unsupported format: %s\n",
430 debugstr_w(lpOpenParms->lpstrElementName));
431 ret = MCIERR_NO_ELEMENT_ALLOWED;
432 goto the_error;
434 drive = toupper(lpOpenParms->lpstrElementName[0]);
435 root[0] = drive; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
436 if (GetDriveTypeW(root) != DRIVE_CDROM)
438 ret = MCIERR_INVALID_DEVICE_NAME;
439 goto the_error;
442 else
444 /* drive letter isn't passed... get the dwDeviceID'th cdrom in the system */
445 root[0] = 'A'; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
446 for (count = 0; root[0] <= 'Z'; root[0]++)
448 if (GetDriveTypeW(root) == DRIVE_CDROM && ++count >= dwDeviceID)
450 drive = root[0];
451 break;
454 if (!drive)
456 ret = MCIERR_INVALID_DEVICE_ID;
457 goto the_error;
461 wmcda->wNotifyDeviceID = dwDeviceID;
462 wmcda->dwTimeFormat = MCI_FORMAT_MSF;
464 /* now, open the handle */
465 root[0] = root[1] = '\\'; root[2] = '.'; root[3] = '\\'; root[4] = drive; root[5] = ':'; root[6] = '\0';
466 wmcda->handle = CreateFileW(root, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
467 if (wmcda->handle != INVALID_HANDLE_VALUE)
468 return 0;
470 the_error:
471 --wmcda->nUseCount;
472 return ret;
475 /**************************************************************************
476 * MCICDA_Close [internal]
478 static DWORD MCICDA_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
480 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
482 TRACE("(%04X, %08X, %p);\n", wDevID, dwParam, lpParms);
484 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
486 if (--wmcda->nUseCount == 0) {
487 CloseHandle(wmcda->handle);
489 return 0;
492 /**************************************************************************
493 * MCICDA_GetDevCaps [internal]
495 static DWORD MCICDA_GetDevCaps(UINT wDevID, DWORD dwFlags,
496 LPMCI_GETDEVCAPS_PARMS lpParms)
498 DWORD ret = 0;
500 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
502 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
504 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
505 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms->dwItem);
507 switch (lpParms->dwItem) {
508 case MCI_GETDEVCAPS_CAN_RECORD:
509 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
510 ret = MCI_RESOURCE_RETURNED;
511 break;
512 case MCI_GETDEVCAPS_HAS_AUDIO:
513 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
514 ret = MCI_RESOURCE_RETURNED;
515 break;
516 case MCI_GETDEVCAPS_HAS_VIDEO:
517 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
518 ret = MCI_RESOURCE_RETURNED;
519 break;
520 case MCI_GETDEVCAPS_DEVICE_TYPE:
521 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO, MCI_DEVTYPE_CD_AUDIO);
522 ret = MCI_RESOURCE_RETURNED;
523 break;
524 case MCI_GETDEVCAPS_USES_FILES:
525 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
526 ret = MCI_RESOURCE_RETURNED;
527 break;
528 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
529 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
530 ret = MCI_RESOURCE_RETURNED;
531 break;
532 case MCI_GETDEVCAPS_CAN_EJECT:
533 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
534 ret = MCI_RESOURCE_RETURNED;
535 break;
536 case MCI_GETDEVCAPS_CAN_PLAY:
537 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
538 ret = MCI_RESOURCE_RETURNED;
539 break;
540 case MCI_GETDEVCAPS_CAN_SAVE:
541 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
542 ret = MCI_RESOURCE_RETURNED;
543 break;
544 default:
545 ERR("Unsupported %x devCaps item\n", lpParms->dwItem);
546 return MCIERR_UNRECOGNIZED_COMMAND;
548 } else {
549 TRACE("No GetDevCaps-Item !\n");
550 return MCIERR_UNRECOGNIZED_COMMAND;
552 TRACE("lpParms->dwReturn=%08X;\n", lpParms->dwReturn);
553 return ret;
556 static DWORD CDROM_Audio_GetSerial(CDROM_TOC* toc)
558 unsigned long serial = 0;
559 int i;
560 WORD wMagic;
561 DWORD dwStart, dwEnd;
564 * wMagic collects the wFrames from track 1
565 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
566 * frames.
567 * There it is collected for correcting the serial when there are less than
568 * 3 tracks.
570 wMagic = toc->TrackData[0].Address[3];
571 dwStart = FRAME_OF_TOC(*toc, toc->FirstTrack);
573 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++) {
574 serial += (toc->TrackData[i].Address[1] << 16) |
575 (toc->TrackData[i].Address[2] << 8) | toc->TrackData[i].Address[3];
577 dwEnd = FRAME_OF_TOC(*toc, toc->LastTrack + 1);
579 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
580 serial += wMagic + (dwEnd - dwStart);
582 return serial;
586 /**************************************************************************
587 * MCICDA_Info [internal]
589 static DWORD MCICDA_Info(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
591 LPCWSTR str = NULL;
592 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
593 DWORD ret = 0;
594 WCHAR buffer[16];
596 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
598 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
599 return MCIERR_NULL_PARAMETER_BLOCK;
600 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
602 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
604 if (dwFlags & MCI_INFO_PRODUCT) {
605 static const WCHAR wszAudioCd[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
606 str = wszAudioCd;
607 } else if (dwFlags & MCI_INFO_MEDIA_UPC) {
608 ret = MCIERR_NO_IDENTITY;
609 } else if (dwFlags & MCI_INFO_MEDIA_IDENTITY) {
610 DWORD res = 0;
611 CDROM_TOC toc;
612 DWORD br;
613 static const WCHAR wszLu[] = {'%','l','u',0};
615 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
616 &toc, sizeof(toc), &br, NULL)) {
617 return MCICDA_GetError(wmcda);
620 res = CDROM_Audio_GetSerial(&toc);
621 sprintfW(buffer, wszLu, res);
622 str = buffer;
623 } else {
624 WARN("Don't know this info command (%u)\n", dwFlags);
625 ret = MCIERR_UNRECOGNIZED_COMMAND;
627 if (str) {
628 if (lpParms->dwRetSize <= strlenW(str)) {
629 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
630 ret = MCIERR_PARAM_OVERFLOW;
631 } else {
632 strcpyW(lpParms->lpstrReturn, str);
634 } else {
635 *lpParms->lpstrReturn = 0;
637 TRACE("=> %s (%d)\n", debugstr_w(lpParms->lpstrReturn), ret);
638 return ret;
641 /**************************************************************************
642 * MCICDA_Status [internal]
644 static DWORD MCICDA_Status(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
646 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
647 DWORD ret = 0;
648 CDROM_SUB_Q_DATA_FORMAT fmt;
649 SUB_Q_CHANNEL_DATA data;
650 CDROM_TOC toc;
651 DWORD br;
653 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
655 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
656 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
658 if (dwFlags & MCI_NOTIFY) {
659 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
660 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
661 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
663 if (dwFlags & MCI_STATUS_ITEM) {
664 TRACE("dwItem = %x\n", lpParms->dwItem);
665 switch (lpParms->dwItem) {
666 case MCI_STATUS_CURRENT_TRACK:
667 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
668 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
669 &data, sizeof(data), &br, NULL))
671 return MCICDA_GetError(wmcda);
673 lpParms->dwReturn = data.CurrentPosition.TrackNumber;
674 TRACE("CURRENT_TRACK=%lu\n", lpParms->dwReturn);
675 break;
676 case MCI_STATUS_LENGTH:
677 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
678 &toc, sizeof(toc), &br, NULL)) {
679 WARN("error reading TOC !\n");
680 return MCICDA_GetError(wmcda);
682 if (dwFlags & MCI_TRACK) {
683 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms->dwTrack);
684 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
685 return MCIERR_OUTOFRANGE;
686 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack + 1) -
687 FRAME_OF_TOC(toc, lpParms->dwTrack);
688 /* Windows returns one frame less than the total track length for the
689 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
690 if (lpParms->dwTrack == toc.LastTrack)
691 lpParms->dwReturn--;
692 } else {
693 /* Sum of the lengths of all of the tracks. Inherits the
694 'off by one frame' behavior from the length of the last track.
695 See above comment. */
696 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.LastTrack + 1) -
697 FRAME_OF_TOC(toc, toc.FirstTrack) - 1;
699 lpParms->dwReturn = MCICDA_CalcTime(wmcda,
700 (wmcda->dwTimeFormat == MCI_FORMAT_TMSF)
701 ? MCI_FORMAT_MSF : wmcda->dwTimeFormat,
702 lpParms->dwReturn,
703 &ret);
704 TRACE("LENGTH=%lu\n", lpParms->dwReturn);
705 break;
706 case MCI_STATUS_MODE:
707 lpParms->dwReturn = MCICDA_GetStatus(wmcda);
708 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms->dwReturn);
709 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn, lpParms->dwReturn);
710 ret = MCI_RESOURCE_RETURNED;
711 break;
712 case MCI_STATUS_MEDIA_PRESENT:
713 lpParms->dwReturn = (MCICDA_GetStatus(wmcda) == MCI_MODE_OPEN) ?
714 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
715 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms->dwReturn) ? 'Y' : 'N');
716 ret = MCI_RESOURCE_RETURNED;
717 break;
718 case MCI_STATUS_NUMBER_OF_TRACKS:
719 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
720 &toc, sizeof(toc), &br, NULL)) {
721 WARN("error reading TOC !\n");
722 return MCICDA_GetError(wmcda);
724 lpParms->dwReturn = toc.LastTrack - toc.FirstTrack + 1;
725 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms->dwReturn);
726 if (lpParms->dwReturn == (WORD)-1)
727 return MCICDA_GetError(wmcda);
728 break;
729 case MCI_STATUS_POSITION:
730 if (dwFlags & MCI_STATUS_START) {
731 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
732 &toc, sizeof(toc), &br, NULL)) {
733 WARN("error reading TOC !\n");
734 return MCICDA_GetError(wmcda);
736 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.FirstTrack);
737 TRACE("get MCI_STATUS_START !\n");
738 } else if (dwFlags & MCI_TRACK) {
739 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
740 &toc, sizeof(toc), &br, NULL)) {
741 WARN("error reading TOC !\n");
742 return MCICDA_GetError(wmcda);
744 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
745 return MCIERR_OUTOFRANGE;
746 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack);
747 TRACE("get MCI_TRACK #%u !\n", lpParms->dwTrack);
748 } else {
749 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
750 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
751 &data, sizeof(data), &br, NULL)) {
752 return MCICDA_GetError(wmcda);
754 lpParms->dwReturn = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
756 lpParms->dwReturn = MCICDA_CalcTime(wmcda, wmcda->dwTimeFormat, lpParms->dwReturn, &ret);
757 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms->dwReturn);
758 break;
759 case MCI_STATUS_READY:
760 TRACE("MCI_STATUS_READY !\n");
761 switch (MCICDA_GetStatus(wmcda))
763 case MCI_MODE_NOT_READY:
764 case MCI_MODE_OPEN:
765 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
766 break;
767 default:
768 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
769 break;
771 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms->dwReturn));
772 ret = MCI_RESOURCE_RETURNED;
773 break;
774 case MCI_STATUS_TIME_FORMAT:
775 lpParms->dwReturn = MAKEMCIRESOURCE(wmcda->dwTimeFormat, MCI_FORMAT_RETURN_BASE + wmcda->dwTimeFormat);
776 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms->dwReturn));
777 ret = MCI_RESOURCE_RETURNED;
778 break;
779 case 4001: /* FIXME: for bogus FullCD */
780 case MCI_CDA_STATUS_TYPE_TRACK:
781 if (!(dwFlags & MCI_TRACK))
782 ret = MCIERR_MISSING_PARAMETER;
783 else {
784 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
785 &toc, sizeof(toc), &br, NULL)) {
786 WARN("error reading TOC !\n");
787 return MCICDA_GetError(wmcda);
789 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
790 ret = MCIERR_OUTOFRANGE;
791 else
792 lpParms->dwReturn = (toc.TrackData[lpParms->dwTrack - toc.FirstTrack].Control & 0x04) ?
793 MCI_CDA_TRACK_OTHER : MCI_CDA_TRACK_AUDIO;
795 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms->dwTrack, lpParms->dwReturn);
796 break;
797 default:
798 FIXME("unknown command %08X !\n", lpParms->dwItem);
799 return MCIERR_UNRECOGNIZED_COMMAND;
801 } else {
802 WARN("not MCI_STATUS_ITEM !\n");
804 return ret;
807 /**************************************************************************
808 * MCICDA_SkipDataTracks [internal]
810 static DWORD MCICDA_SkipDataTracks(WINE_MCICDAUDIO* wmcda,DWORD *frame)
812 int i;
813 DWORD br;
814 CDROM_TOC toc;
815 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
816 &toc, sizeof(toc), &br, NULL)) {
817 WARN("error reading TOC !\n");
818 return MCICDA_GetError(wmcda);
820 /* Locate first track whose starting frame is bigger than frame */
821 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
822 if ( FRAME_OF_TOC(toc, i) > *frame ) break;
823 if (i <= toc.FirstTrack && i>toc.LastTrack+1) {
824 i = 0; /* requested address is out of range: go back to start */
825 *frame = FRAME_OF_TOC(toc,toc.FirstTrack);
827 else
828 i--;
829 /* i points to last track whose start address is not greater than frame.
830 * Now skip non-audio tracks */
831 for(;i<=toc.LastTrack+1;i++)
832 if ( ! (toc.TrackData[i-toc.FirstTrack].Control & 4) )
833 break;
834 /* The frame will be an address in the next audio track or
835 * address of lead-out. */
836 if ( FRAME_OF_TOC(toc, i) > *frame )
837 *frame = FRAME_OF_TOC(toc, i);
838 return 0;
841 /**************************************************************************
842 * MCICDA_Play [internal]
844 static DWORD MCICDA_Play(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
846 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
847 DWORD ret = 0, start, end;
848 DWORD br;
849 CDROM_PLAY_AUDIO_MSF play;
850 CDROM_SUB_Q_DATA_FORMAT fmt;
851 SUB_Q_CHANNEL_DATA data;
852 CDROM_TOC toc;
854 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
856 if (lpParms == NULL)
857 return MCIERR_NULL_PARAMETER_BLOCK;
859 if (wmcda == NULL)
860 return MCIERR_INVALID_DEVICE_ID;
862 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
863 &toc, sizeof(toc), &br, NULL)) {
864 WARN("error reading TOC !\n");
865 return MCICDA_GetError(wmcda);
868 if (dwFlags & MCI_FROM) {
869 start = MCICDA_CalcFrame(wmcda, lpParms->dwFrom);
870 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
871 return ret;
872 TRACE("MCI_FROM=%08X -> %u\n", lpParms->dwFrom, start);
873 } else {
874 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
875 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
876 &data, sizeof(data), &br, NULL)) {
877 return MCICDA_GetError(wmcda);
879 start = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
880 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
881 return ret;
883 if (dwFlags & MCI_TO) {
884 end = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
885 TRACE("MCI_TO=%08X -> %u\n", lpParms->dwTo, end);
886 } else {
887 end = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
889 TRACE("Playing from %u to %u\n", start, end);
891 if (wmcda->hThread != 0) {
892 SetEvent(wmcda->stopEvent);
893 WaitForSingleObject(wmcda->hThread, INFINITE);
895 CloseHandle(wmcda->hThread);
896 wmcda->hThread = 0;
897 CloseHandle(wmcda->stopEvent);
898 wmcda->stopEvent = 0;
900 IDirectSoundBuffer_Stop(wmcda->dsBuf);
901 IDirectSoundBuffer_Release(wmcda->dsBuf);
902 wmcda->dsBuf = NULL;
903 IDirectSound_Release(wmcda->dsObj);
904 wmcda->dsObj = NULL;
906 else if(wmcda->hCallback) {
907 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID,
908 MCI_NOTIFY_ABORTED);
909 wmcda->hCallback = NULL;
912 if ((dwFlags&MCI_NOTIFY))
913 wmcda->hCallback = HWND_32(LOWORD(lpParms->dwCallback));
915 if (pDirectSoundCreate) {
916 WAVEFORMATEX format;
917 DSBUFFERDESC desc;
918 DWORD lockLen;
919 void *cdData;
920 HRESULT hr;
922 hr = pDirectSoundCreate(NULL, &wmcda->dsObj, NULL);
923 if (SUCCEEDED(hr)) {
924 IDirectSound_SetCooperativeLevel(wmcda->dsObj, GetDesktopWindow(), DSSCL_PRIORITY);
926 /* The "raw" frame is relative to the start of the first track */
927 wmcda->start = start - FRAME_OF_TOC(toc, toc.FirstTrack);
928 wmcda->end = end - FRAME_OF_TOC(toc, toc.FirstTrack);
930 memset(&format, 0, sizeof(format));
931 format.wFormatTag = WAVE_FORMAT_PCM;
932 format.nChannels = 2;
933 format.nSamplesPerSec = 44100;
934 format.wBitsPerSample = 16;
935 format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
936 format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
937 format.cbSize = 0;
939 memset(&desc, 0, sizeof(desc));
940 desc.dwSize = sizeof(desc);
941 desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
942 desc.dwBufferBytes = (CDDA_FRAG_SIZE - (CDDA_FRAG_SIZE%RAW_SECTOR_SIZE)) * CDDA_FRAG_COUNT;
943 desc.lpwfxFormat = &format;
945 hr = IDirectSound_CreateSoundBuffer(wmcda->dsObj, &desc, &wmcda->dsBuf, NULL);
947 if (SUCCEEDED(hr)) {
948 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, 0, 0, &cdData, &lockLen,
949 NULL, NULL, DSBLOCK_ENTIREBUFFER);
951 if (SUCCEEDED(hr)) {
952 RAW_READ_INFO rdInfo;
953 int readok;
955 rdInfo.DiskOffset.QuadPart = wmcda->start<<11;
956 rdInfo.SectorCount = min(desc.dwBufferBytes/RAW_SECTOR_SIZE,
957 wmcda->end-wmcda->start);
958 rdInfo.TrackMode = CDDA;
960 readok = DeviceIoControl(wmcda->handle, IOCTL_CDROM_RAW_READ,
961 &rdInfo, sizeof(rdInfo), cdData, lockLen,
962 &br, NULL);
963 IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
965 if (readok) {
966 wmcda->start += rdInfo.SectorCount;
967 wmcda->stopEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
969 if (wmcda->stopEvent != 0)
970 wmcda->hThread = CreateThread(NULL, 0, MCICDA_playLoop, wmcda, 0, &br);
971 if (wmcda->hThread != 0) {
972 hr = IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING);
973 if (SUCCEEDED(hr))
974 return ret;
976 SetEvent(wmcda->stopEvent);
977 WaitForSingleObject(wmcda->hThread, INFINITE);
978 CloseHandle(wmcda->hThread);
979 wmcda->hThread = 0;
983 if (wmcda->stopEvent != 0) {
984 CloseHandle(wmcda->stopEvent);
985 wmcda->stopEvent = 0;
987 if (wmcda->dsBuf) {
988 IDirectSoundBuffer_Release(wmcda->dsBuf);
989 wmcda->dsBuf = NULL;
991 if (wmcda->dsObj) {
992 IDirectSound_Release(wmcda->dsObj);
993 wmcda->dsObj = NULL;
997 play.StartingM = start / CDFRAMES_PERMIN;
998 play.StartingS = (start / CDFRAMES_PERSEC) % 60;
999 play.StartingF = start % CDFRAMES_PERSEC;
1000 play.EndingM = end / CDFRAMES_PERMIN;
1001 play.EndingS = (end / CDFRAMES_PERSEC) % 60;
1002 play.EndingF = end % CDFRAMES_PERSEC;
1003 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PLAY_AUDIO_MSF, &play, sizeof(play),
1004 NULL, 0, &br, NULL)) {
1005 wmcda->hCallback = NULL;
1006 ret = MCIERR_HARDWARE;
1007 } else if (dwFlags & MCI_NOTIFY) {
1008 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1010 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1011 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1014 return ret;
1017 /**************************************************************************
1018 * MCICDA_Stop [internal]
1020 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1022 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1023 DWORD br;
1025 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1027 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1029 if (wmcda->hThread != 0) {
1030 SetEvent(wmcda->stopEvent);
1031 WaitForSingleObject(wmcda->hThread, INFINITE);
1033 CloseHandle(wmcda->hThread);
1034 wmcda->hThread = 0;
1035 CloseHandle(wmcda->stopEvent);
1036 wmcda->stopEvent = 0;
1038 IDirectSoundBuffer_Release(wmcda->dsBuf);
1039 wmcda->dsBuf = NULL;
1040 IDirectSound_Release(wmcda->dsObj);
1041 wmcda->dsObj = NULL;
1043 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_STOP_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1044 return MCIERR_HARDWARE;
1046 if (wmcda->hCallback) {
1047 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1048 wmcda->hCallback = NULL;
1051 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1052 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1053 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1054 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1056 return 0;
1059 /**************************************************************************
1060 * MCICDA_Pause [internal]
1062 static DWORD MCICDA_Pause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1064 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1065 DWORD br;
1067 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1069 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1071 if (wmcda->hThread != 0) {
1072 /* Don't bother calling stop if the playLoop thread has already stopped */
1073 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1074 FAILED(IDirectSoundBuffer_Stop(wmcda->dsBuf)))
1075 return MCIERR_HARDWARE;
1077 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1078 return MCIERR_HARDWARE;
1080 EnterCriticalSection(&wmcda->cs);
1081 if (wmcda->hCallback) {
1082 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID, MCI_NOTIFY_SUPERSEDED);
1083 wmcda->hCallback = NULL;
1085 LeaveCriticalSection(&wmcda->cs);
1087 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1088 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1089 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1090 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1092 return 0;
1095 /**************************************************************************
1096 * MCICDA_Resume [internal]
1098 static DWORD MCICDA_Resume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1100 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1101 DWORD br;
1103 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1105 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1107 if (wmcda->hThread != 0) {
1108 /* Don't restart if the playLoop thread has already stopped */
1109 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1110 FAILED(IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING)))
1111 return MCIERR_HARDWARE;
1113 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_RESUME_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1114 return MCIERR_HARDWARE;
1116 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1117 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1118 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1119 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1121 return 0;
1124 /**************************************************************************
1125 * MCICDA_Seek [internal]
1127 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1129 DWORD at;
1130 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1131 CDROM_SEEK_AUDIO_MSF seek;
1132 DWORD br, ret;
1133 CDROM_TOC toc;
1135 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1137 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1138 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1140 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
1141 &toc, sizeof(toc), &br, NULL)) {
1142 WARN("error reading TOC !\n");
1143 return MCICDA_GetError(wmcda);
1145 switch (dwFlags & ~(MCI_NOTIFY|MCI_WAIT)) {
1146 case MCI_SEEK_TO_START:
1147 TRACE("Seeking to start\n");
1148 at = FRAME_OF_TOC(toc,toc.FirstTrack);
1149 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1150 return ret;
1151 break;
1152 case MCI_SEEK_TO_END:
1153 TRACE("Seeking to end\n");
1154 at = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
1155 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1156 return ret;
1157 break;
1158 case MCI_TO:
1159 TRACE("Seeking to %u\n", lpParms->dwTo);
1160 at = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
1161 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1162 return ret;
1163 break;
1164 default:
1165 TRACE("Unknown seek action %08lX\n",
1166 (dwFlags & ~(MCI_NOTIFY|MCI_WAIT)));
1167 return MCIERR_UNSUPPORTED_FUNCTION;
1170 if (wmcda->hThread != 0) {
1171 EnterCriticalSection(&wmcda->cs);
1172 wmcda->start = at - FRAME_OF_TOC(toc, toc.FirstTrack);
1173 /* Flush remaining data, or just let it play into the new data? */
1174 LeaveCriticalSection(&wmcda->cs);
1176 else {
1177 seek.M = at / CDFRAMES_PERMIN;
1178 seek.S = (at / CDFRAMES_PERSEC) % 60;
1179 seek.F = at % CDFRAMES_PERSEC;
1180 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_SEEK_AUDIO_MSF, &seek, sizeof(seek),
1181 NULL, 0, &br, NULL))
1182 return MCIERR_HARDWARE;
1185 if (dwFlags & MCI_NOTIFY) {
1186 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1187 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1188 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1190 return 0;
1193 /**************************************************************************
1194 * MCICDA_SetDoor [internal]
1196 static DWORD MCICDA_SetDoor(UINT wDevID, BOOL open)
1198 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1199 DWORD br;
1201 TRACE("(%04x, %s) !\n", wDevID, (open) ? "OPEN" : "CLOSE");
1203 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1205 if (!DeviceIoControl(wmcda->handle,
1206 (open) ? IOCTL_STORAGE_EJECT_MEDIA : IOCTL_STORAGE_LOAD_MEDIA,
1207 NULL, 0, NULL, 0, &br, NULL))
1208 return MCIERR_HARDWARE;
1210 return 0;
1213 /**************************************************************************
1214 * MCICDA_Set [internal]
1216 static DWORD MCICDA_Set(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1218 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1220 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1222 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1224 if (dwFlags & MCI_SET_DOOR_OPEN) {
1225 MCICDA_SetDoor(wDevID, TRUE);
1227 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1228 MCICDA_SetDoor(wDevID, FALSE);
1231 /* only functions which require valid lpParms below this line ! */
1232 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1234 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1235 TRACE("dwAudio=%08lX\n", lpParms->dwAudio);
1237 if (dwFlags & MCI_SET_TIME_FORMAT) {
1238 switch (lpParms->dwTimeFormat) {
1239 case MCI_FORMAT_MILLISECONDS:
1240 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1241 break;
1242 case MCI_FORMAT_MSF:
1243 TRACE("MCI_FORMAT_MSF !\n");
1244 break;
1245 case MCI_FORMAT_TMSF:
1246 TRACE("MCI_FORMAT_TMSF !\n");
1247 break;
1248 default:
1249 WARN("bad time format !\n");
1250 return MCIERR_BAD_TIME_FORMAT;
1252 wmcda->dwTimeFormat = lpParms->dwTimeFormat;
1254 if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
1255 if (dwFlags & MCI_SET_ON) return MCIERR_UNSUPPORTED_FUNCTION;
1256 if (dwFlags & MCI_SET_OFF) return MCIERR_UNSUPPORTED_FUNCTION;
1257 if (dwFlags & MCI_NOTIFY) {
1258 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n",
1259 lpParms->dwCallback);
1260 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1261 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1263 return 0;
1266 /**************************************************************************
1267 * DriverProc (MCICDA.@)
1269 LRESULT CALLBACK MCICDA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1270 LPARAM dwParam1, LPARAM dwParam2)
1272 switch(wMsg) {
1273 case DRV_LOAD: return 1;
1274 case DRV_FREE: return 1;
1275 case DRV_OPEN: return MCICDA_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1276 case DRV_CLOSE: return MCICDA_drvClose(dwDevID);
1277 case DRV_ENABLE: return 1;
1278 case DRV_DISABLE: return 1;
1279 case DRV_QUERYCONFIGURE: return 1;
1280 case DRV_CONFIGURE: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK); return 1;
1281 case DRV_INSTALL: return DRVCNF_RESTART;
1282 case DRV_REMOVE: return DRVCNF_RESTART;
1285 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1287 switch (wMsg) {
1288 case MCI_OPEN_DRIVER: return MCICDA_Open(dwDevID, dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1289 case MCI_CLOSE_DRIVER: return MCICDA_Close(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1290 case MCI_GETDEVCAPS: return MCICDA_GetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1291 case MCI_INFO: return MCICDA_Info(dwDevID, dwParam1, (LPMCI_INFO_PARMSW)dwParam2);
1292 case MCI_STATUS: return MCICDA_Status(dwDevID, dwParam1, (LPMCI_STATUS_PARMS)dwParam2);
1293 case MCI_SET: return MCICDA_Set(dwDevID, dwParam1, (LPMCI_SET_PARMS)dwParam2);
1294 case MCI_PLAY: return MCICDA_Play(dwDevID, dwParam1, (LPMCI_PLAY_PARMS)dwParam2);
1295 case MCI_STOP: return MCICDA_Stop(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1296 case MCI_PAUSE: return MCICDA_Pause(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1297 case MCI_RESUME: return MCICDA_Resume(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1298 case MCI_SEEK: return MCICDA_Seek(dwDevID, dwParam1, (LPMCI_SEEK_PARMS)dwParam2);
1299 /* commands that should report an error as they are not supported in
1300 * the native version */
1301 case MCI_SET_DOOR_CLOSED:
1302 case MCI_SET_DOOR_OPEN:
1303 case MCI_LOAD:
1304 case MCI_SAVE:
1305 case MCI_FREEZE:
1306 case MCI_PUT:
1307 case MCI_REALIZE:
1308 case MCI_UNFREEZE:
1309 case MCI_UPDATE:
1310 case MCI_WHERE:
1311 case MCI_STEP:
1312 case MCI_SPIN:
1313 case MCI_ESCAPE:
1314 case MCI_COPY:
1315 case MCI_CUT:
1316 case MCI_DELETE:
1317 case MCI_PASTE:
1318 case MCI_WINDOW:
1319 TRACE("Unsupported command [0x%x]\n", wMsg);
1320 break;
1321 case MCI_OPEN:
1322 case MCI_CLOSE:
1323 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1324 break;
1325 default:
1326 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg);
1327 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1329 return MCIERR_UNRECOGNIZED_COMMAND;
1332 /*-----------------------------------------------------------------------*/