dplayx: Adjust GetCaps behaviour to documentation
[wine/gsoc_dplay.git] / dlls / mcicda / mcicda.c
blob81dc18f753f252e81e17f7b77a82e0bc9b692935
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 return MCIERR_NO_ELEMENT_ALLOWED;
424 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms->lpstrElementName));
425 if (!isalpha(lpOpenParms->lpstrElementName[0]) || lpOpenParms->lpstrElementName[1] != ':' ||
426 (lpOpenParms->lpstrElementName[2] && lpOpenParms->lpstrElementName[2] != '\\'))
428 WARN("MCI_OPEN_ELEMENT unsupported format: %s\n",
429 debugstr_w(lpOpenParms->lpstrElementName));
430 ret = MCIERR_NO_ELEMENT_ALLOWED;
431 goto the_error;
433 drive = toupper(lpOpenParms->lpstrElementName[0]);
434 root[0] = drive; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
435 if (GetDriveTypeW(root) != DRIVE_CDROM)
437 ret = MCIERR_INVALID_DEVICE_NAME;
438 goto the_error;
441 else
443 /* drive letter isn't passed... get the dwDeviceID'th cdrom in the system */
444 root[0] = 'A'; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
445 for (count = 0; root[0] <= 'Z'; root[0]++)
447 if (GetDriveTypeW(root) == DRIVE_CDROM && ++count >= dwDeviceID)
449 drive = root[0];
450 break;
453 if (!drive)
455 ret = MCIERR_INVALID_DEVICE_ID;
456 goto the_error;
460 wmcda->wNotifyDeviceID = dwDeviceID;
461 wmcda->dwTimeFormat = MCI_FORMAT_MSF;
463 /* now, open the handle */
464 root[0] = root[1] = '\\'; root[2] = '.'; root[3] = '\\'; root[4] = drive; root[5] = ':'; root[6] = '\0';
465 wmcda->handle = CreateFileW(root, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
466 if (wmcda->handle != INVALID_HANDLE_VALUE)
467 return 0;
469 the_error:
470 --wmcda->nUseCount;
471 return ret;
474 /**************************************************************************
475 * MCICDA_Close [internal]
477 static DWORD MCICDA_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
479 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
481 TRACE("(%04X, %08X, %p);\n", wDevID, dwParam, lpParms);
483 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
485 if (--wmcda->nUseCount == 0) {
486 CloseHandle(wmcda->handle);
488 return 0;
491 /**************************************************************************
492 * MCICDA_GetDevCaps [internal]
494 static DWORD MCICDA_GetDevCaps(UINT wDevID, DWORD dwFlags,
495 LPMCI_GETDEVCAPS_PARMS lpParms)
497 DWORD ret = 0;
499 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
501 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
503 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
504 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms->dwItem);
506 switch (lpParms->dwItem) {
507 case MCI_GETDEVCAPS_CAN_RECORD:
508 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
509 ret = MCI_RESOURCE_RETURNED;
510 break;
511 case MCI_GETDEVCAPS_HAS_AUDIO:
512 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
513 ret = MCI_RESOURCE_RETURNED;
514 break;
515 case MCI_GETDEVCAPS_HAS_VIDEO:
516 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
517 ret = MCI_RESOURCE_RETURNED;
518 break;
519 case MCI_GETDEVCAPS_DEVICE_TYPE:
520 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO, MCI_DEVTYPE_CD_AUDIO);
521 ret = MCI_RESOURCE_RETURNED;
522 break;
523 case MCI_GETDEVCAPS_USES_FILES:
524 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
525 ret = MCI_RESOURCE_RETURNED;
526 break;
527 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
528 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
529 ret = MCI_RESOURCE_RETURNED;
530 break;
531 case MCI_GETDEVCAPS_CAN_EJECT:
532 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
533 ret = MCI_RESOURCE_RETURNED;
534 break;
535 case MCI_GETDEVCAPS_CAN_PLAY:
536 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
537 ret = MCI_RESOURCE_RETURNED;
538 break;
539 case MCI_GETDEVCAPS_CAN_SAVE:
540 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
541 ret = MCI_RESOURCE_RETURNED;
542 break;
543 default:
544 ERR("Unsupported %x devCaps item\n", lpParms->dwItem);
545 return MCIERR_UNRECOGNIZED_COMMAND;
547 } else {
548 TRACE("No GetDevCaps-Item !\n");
549 return MCIERR_UNRECOGNIZED_COMMAND;
551 TRACE("lpParms->dwReturn=%08X;\n", lpParms->dwReturn);
552 return ret;
555 static DWORD CDROM_Audio_GetSerial(CDROM_TOC* toc)
557 unsigned long serial = 0;
558 int i;
559 WORD wMagic;
560 DWORD dwStart, dwEnd;
563 * wMagic collects the wFrames from track 1
564 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
565 * frames.
566 * There it is collected for correcting the serial when there are less than
567 * 3 tracks.
569 wMagic = toc->TrackData[0].Address[3];
570 dwStart = FRAME_OF_TOC(*toc, toc->FirstTrack);
572 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++) {
573 serial += (toc->TrackData[i].Address[1] << 16) |
574 (toc->TrackData[i].Address[2] << 8) | toc->TrackData[i].Address[3];
576 dwEnd = FRAME_OF_TOC(*toc, toc->LastTrack + 1);
578 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
579 serial += wMagic + (dwEnd - dwStart);
581 return serial;
585 /**************************************************************************
586 * MCICDA_Info [internal]
588 static DWORD MCICDA_Info(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
590 LPCWSTR str = NULL;
591 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
592 DWORD ret = 0;
593 WCHAR buffer[16];
595 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
597 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
598 return MCIERR_NULL_PARAMETER_BLOCK;
599 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
601 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
603 if (dwFlags & MCI_INFO_PRODUCT) {
604 static const WCHAR wszAudioCd[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
605 str = wszAudioCd;
606 } else if (dwFlags & MCI_INFO_MEDIA_UPC) {
607 ret = MCIERR_NO_IDENTITY;
608 } else if (dwFlags & MCI_INFO_MEDIA_IDENTITY) {
609 DWORD res = 0;
610 CDROM_TOC toc;
611 DWORD br;
612 static const WCHAR wszLu[] = {'%','l','u',0};
614 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
615 &toc, sizeof(toc), &br, NULL)) {
616 return MCICDA_GetError(wmcda);
619 res = CDROM_Audio_GetSerial(&toc);
620 sprintfW(buffer, wszLu, res);
621 str = buffer;
622 } else {
623 WARN("Don't know this info command (%u)\n", dwFlags);
624 ret = MCIERR_UNRECOGNIZED_COMMAND;
626 if (str) {
627 if (lpParms->dwRetSize <= strlenW(str)) {
628 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
629 ret = MCIERR_PARAM_OVERFLOW;
630 } else {
631 strcpyW(lpParms->lpstrReturn, str);
633 } else {
634 *lpParms->lpstrReturn = 0;
636 TRACE("=> %s (%d)\n", debugstr_w(lpParms->lpstrReturn), ret);
637 return ret;
640 /**************************************************************************
641 * MCICDA_Status [internal]
643 static DWORD MCICDA_Status(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
645 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
646 DWORD ret = 0;
647 CDROM_SUB_Q_DATA_FORMAT fmt;
648 SUB_Q_CHANNEL_DATA data;
649 CDROM_TOC toc;
650 DWORD br;
652 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
654 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
655 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
657 if (dwFlags & MCI_NOTIFY) {
658 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
659 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
660 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
662 if (dwFlags & MCI_STATUS_ITEM) {
663 TRACE("dwItem = %x\n", lpParms->dwItem);
664 switch (lpParms->dwItem) {
665 case MCI_STATUS_CURRENT_TRACK:
666 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
667 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
668 &data, sizeof(data), &br, NULL))
670 return MCICDA_GetError(wmcda);
672 lpParms->dwReturn = data.CurrentPosition.TrackNumber;
673 TRACE("CURRENT_TRACK=%lu\n", lpParms->dwReturn);
674 break;
675 case MCI_STATUS_LENGTH:
676 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
677 &toc, sizeof(toc), &br, NULL)) {
678 WARN("error reading TOC !\n");
679 return MCICDA_GetError(wmcda);
681 if (dwFlags & MCI_TRACK) {
682 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms->dwTrack);
683 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
684 return MCIERR_OUTOFRANGE;
685 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack + 1) -
686 FRAME_OF_TOC(toc, lpParms->dwTrack);
687 /* Windows returns one frame less than the total track length for the
688 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
689 if (lpParms->dwTrack == toc.LastTrack)
690 lpParms->dwReturn--;
691 } else {
692 /* Sum of the lengths of all of the tracks. Inherits the
693 'off by one frame' behavior from the length of the last track.
694 See above comment. */
695 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.LastTrack + 1) -
696 FRAME_OF_TOC(toc, toc.FirstTrack) - 1;
698 lpParms->dwReturn = MCICDA_CalcTime(wmcda,
699 (wmcda->dwTimeFormat == MCI_FORMAT_TMSF)
700 ? MCI_FORMAT_MSF : wmcda->dwTimeFormat,
701 lpParms->dwReturn,
702 &ret);
703 TRACE("LENGTH=%lu\n", lpParms->dwReturn);
704 break;
705 case MCI_STATUS_MODE:
706 lpParms->dwReturn = MCICDA_GetStatus(wmcda);
707 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms->dwReturn);
708 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn, lpParms->dwReturn);
709 ret = MCI_RESOURCE_RETURNED;
710 break;
711 case MCI_STATUS_MEDIA_PRESENT:
712 lpParms->dwReturn = (MCICDA_GetStatus(wmcda) == MCI_MODE_OPEN) ?
713 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
714 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms->dwReturn) ? 'Y' : 'N');
715 ret = MCI_RESOURCE_RETURNED;
716 break;
717 case MCI_STATUS_NUMBER_OF_TRACKS:
718 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
719 &toc, sizeof(toc), &br, NULL)) {
720 WARN("error reading TOC !\n");
721 return MCICDA_GetError(wmcda);
723 lpParms->dwReturn = toc.LastTrack - toc.FirstTrack + 1;
724 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms->dwReturn);
725 if (lpParms->dwReturn == (WORD)-1)
726 return MCICDA_GetError(wmcda);
727 break;
728 case MCI_STATUS_POSITION:
729 if (dwFlags & MCI_STATUS_START) {
730 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
731 &toc, sizeof(toc), &br, NULL)) {
732 WARN("error reading TOC !\n");
733 return MCICDA_GetError(wmcda);
735 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.FirstTrack);
736 TRACE("get MCI_STATUS_START !\n");
737 } else if (dwFlags & MCI_TRACK) {
738 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
739 &toc, sizeof(toc), &br, NULL)) {
740 WARN("error reading TOC !\n");
741 return MCICDA_GetError(wmcda);
743 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
744 return MCIERR_OUTOFRANGE;
745 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack);
746 TRACE("get MCI_TRACK #%u !\n", lpParms->dwTrack);
747 } else {
748 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
749 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
750 &data, sizeof(data), &br, NULL)) {
751 return MCICDA_GetError(wmcda);
753 lpParms->dwReturn = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
755 lpParms->dwReturn = MCICDA_CalcTime(wmcda, wmcda->dwTimeFormat, lpParms->dwReturn, &ret);
756 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms->dwReturn);
757 break;
758 case MCI_STATUS_READY:
759 TRACE("MCI_STATUS_READY !\n");
760 switch (MCICDA_GetStatus(wmcda))
762 case MCI_MODE_NOT_READY:
763 case MCI_MODE_OPEN:
764 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
765 break;
766 default:
767 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
768 break;
770 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms->dwReturn));
771 ret = MCI_RESOURCE_RETURNED;
772 break;
773 case MCI_STATUS_TIME_FORMAT:
774 lpParms->dwReturn = MAKEMCIRESOURCE(wmcda->dwTimeFormat, MCI_FORMAT_RETURN_BASE + wmcda->dwTimeFormat);
775 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms->dwReturn));
776 ret = MCI_RESOURCE_RETURNED;
777 break;
778 case 4001: /* FIXME: for bogus FullCD */
779 case MCI_CDA_STATUS_TYPE_TRACK:
780 if (!(dwFlags & MCI_TRACK))
781 ret = MCIERR_MISSING_PARAMETER;
782 else {
783 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
784 &toc, sizeof(toc), &br, NULL)) {
785 WARN("error reading TOC !\n");
786 return MCICDA_GetError(wmcda);
788 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
789 ret = MCIERR_OUTOFRANGE;
790 else
791 lpParms->dwReturn = (toc.TrackData[lpParms->dwTrack - toc.FirstTrack].Control & 0x04) ?
792 MCI_CDA_TRACK_OTHER : MCI_CDA_TRACK_AUDIO;
794 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms->dwTrack, lpParms->dwReturn);
795 break;
796 default:
797 FIXME("unknown command %08X !\n", lpParms->dwItem);
798 return MCIERR_UNRECOGNIZED_COMMAND;
800 } else {
801 WARN("not MCI_STATUS_ITEM !\n");
803 return ret;
806 /**************************************************************************
807 * MCICDA_SkipDataTracks [internal]
809 static DWORD MCICDA_SkipDataTracks(WINE_MCICDAUDIO* wmcda,DWORD *frame)
811 int i;
812 DWORD br;
813 CDROM_TOC toc;
814 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
815 &toc, sizeof(toc), &br, NULL)) {
816 WARN("error reading TOC !\n");
817 return MCICDA_GetError(wmcda);
819 /* Locate first track whose starting frame is bigger than frame */
820 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
821 if ( FRAME_OF_TOC(toc, i) > *frame ) break;
822 if (i <= toc.FirstTrack && i>toc.LastTrack+1) {
823 i = 0; /* requested address is out of range: go back to start */
824 *frame = FRAME_OF_TOC(toc,toc.FirstTrack);
826 else
827 i--;
828 /* i points to last track whose start address is not greater than frame.
829 * Now skip non-audio tracks */
830 for(;i<=toc.LastTrack+1;i++)
831 if ( ! (toc.TrackData[i-toc.FirstTrack].Control & 4) )
832 break;
833 /* The frame will be an address in the next audio track or
834 * address of lead-out. */
835 if ( FRAME_OF_TOC(toc, i) > *frame )
836 *frame = FRAME_OF_TOC(toc, i);
837 return 0;
840 /**************************************************************************
841 * MCICDA_Play [internal]
843 static DWORD MCICDA_Play(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
845 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
846 DWORD ret = 0, start, end;
847 DWORD br;
848 CDROM_PLAY_AUDIO_MSF play;
849 CDROM_SUB_Q_DATA_FORMAT fmt;
850 SUB_Q_CHANNEL_DATA data;
851 CDROM_TOC toc;
853 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
855 if (lpParms == NULL)
856 return MCIERR_NULL_PARAMETER_BLOCK;
858 if (wmcda == NULL)
859 return MCIERR_INVALID_DEVICE_ID;
861 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
862 &toc, sizeof(toc), &br, NULL)) {
863 WARN("error reading TOC !\n");
864 return MCICDA_GetError(wmcda);
867 if (dwFlags & MCI_FROM) {
868 start = MCICDA_CalcFrame(wmcda, lpParms->dwFrom);
869 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
870 return ret;
871 TRACE("MCI_FROM=%08X -> %u\n", lpParms->dwFrom, start);
872 } else {
873 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
874 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
875 &data, sizeof(data), &br, NULL)) {
876 return MCICDA_GetError(wmcda);
878 start = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
879 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
880 return ret;
882 if (dwFlags & MCI_TO) {
883 end = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
884 TRACE("MCI_TO=%08X -> %u\n", lpParms->dwTo, end);
885 } else {
886 end = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
888 TRACE("Playing from %u to %u\n", start, end);
890 if (wmcda->hThread != 0) {
891 SetEvent(wmcda->stopEvent);
892 WaitForSingleObject(wmcda->hThread, INFINITE);
894 CloseHandle(wmcda->hThread);
895 wmcda->hThread = 0;
896 CloseHandle(wmcda->stopEvent);
897 wmcda->stopEvent = 0;
899 IDirectSoundBuffer_Stop(wmcda->dsBuf);
900 IDirectSoundBuffer_Release(wmcda->dsBuf);
901 wmcda->dsBuf = NULL;
902 IDirectSound_Release(wmcda->dsObj);
903 wmcda->dsObj = NULL;
905 else if(wmcda->hCallback) {
906 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID,
907 MCI_NOTIFY_ABORTED);
908 wmcda->hCallback = NULL;
911 if ((dwFlags&MCI_NOTIFY))
912 wmcda->hCallback = HWND_32(LOWORD(lpParms->dwCallback));
914 if (pDirectSoundCreate) {
915 WAVEFORMATEX format;
916 DSBUFFERDESC desc;
917 DWORD lockLen;
918 void *cdData;
919 HRESULT hr;
921 hr = pDirectSoundCreate(NULL, &wmcda->dsObj, NULL);
922 if (SUCCEEDED(hr)) {
923 IDirectSound_SetCooperativeLevel(wmcda->dsObj, GetDesktopWindow(), DSSCL_PRIORITY);
925 /* The "raw" frame is relative to the start of the first track */
926 wmcda->start = start - FRAME_OF_TOC(toc, toc.FirstTrack);
927 wmcda->end = end - FRAME_OF_TOC(toc, toc.FirstTrack);
929 memset(&format, 0, sizeof(format));
930 format.wFormatTag = WAVE_FORMAT_PCM;
931 format.nChannels = 2;
932 format.nSamplesPerSec = 44100;
933 format.wBitsPerSample = 16;
934 format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
935 format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
936 format.cbSize = 0;
938 memset(&desc, 0, sizeof(desc));
939 desc.dwSize = sizeof(desc);
940 desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
941 desc.dwBufferBytes = (CDDA_FRAG_SIZE - (CDDA_FRAG_SIZE%RAW_SECTOR_SIZE)) * CDDA_FRAG_COUNT;
942 desc.lpwfxFormat = &format;
944 hr = IDirectSound_CreateSoundBuffer(wmcda->dsObj, &desc, &wmcda->dsBuf, NULL);
946 if (SUCCEEDED(hr)) {
947 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, 0, 0, &cdData, &lockLen,
948 NULL, NULL, DSBLOCK_ENTIREBUFFER);
950 if (SUCCEEDED(hr)) {
951 RAW_READ_INFO rdInfo;
952 int readok;
954 rdInfo.DiskOffset.QuadPart = wmcda->start<<11;
955 rdInfo.SectorCount = min(desc.dwBufferBytes/RAW_SECTOR_SIZE,
956 wmcda->end-wmcda->start);
957 rdInfo.TrackMode = CDDA;
959 readok = DeviceIoControl(wmcda->handle, IOCTL_CDROM_RAW_READ,
960 &rdInfo, sizeof(rdInfo), cdData, lockLen,
961 &br, NULL);
962 IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
964 if (readok) {
965 wmcda->start += rdInfo.SectorCount;
966 wmcda->stopEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
968 if (wmcda->stopEvent != 0)
969 wmcda->hThread = CreateThread(NULL, 0, MCICDA_playLoop, wmcda, 0, &br);
970 if (wmcda->hThread != 0) {
971 hr = IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING);
972 if (SUCCEEDED(hr))
973 return ret;
975 SetEvent(wmcda->stopEvent);
976 WaitForSingleObject(wmcda->hThread, INFINITE);
977 CloseHandle(wmcda->hThread);
978 wmcda->hThread = 0;
982 if (wmcda->stopEvent != 0) {
983 CloseHandle(wmcda->stopEvent);
984 wmcda->stopEvent = 0;
986 if (wmcda->dsBuf) {
987 IDirectSoundBuffer_Release(wmcda->dsBuf);
988 wmcda->dsBuf = NULL;
990 if (wmcda->dsObj) {
991 IDirectSound_Release(wmcda->dsObj);
992 wmcda->dsObj = NULL;
996 play.StartingM = start / CDFRAMES_PERMIN;
997 play.StartingS = (start / CDFRAMES_PERSEC) % 60;
998 play.StartingF = start % CDFRAMES_PERSEC;
999 play.EndingM = end / CDFRAMES_PERMIN;
1000 play.EndingS = (end / CDFRAMES_PERSEC) % 60;
1001 play.EndingF = end % CDFRAMES_PERSEC;
1002 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PLAY_AUDIO_MSF, &play, sizeof(play),
1003 NULL, 0, &br, NULL)) {
1004 wmcda->hCallback = NULL;
1005 ret = MCIERR_HARDWARE;
1006 } else if (dwFlags & MCI_NOTIFY) {
1007 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1009 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1010 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1013 return ret;
1016 /**************************************************************************
1017 * MCICDA_Stop [internal]
1019 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1021 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1022 DWORD br;
1024 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1026 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1028 if (wmcda->hThread != 0) {
1029 SetEvent(wmcda->stopEvent);
1030 WaitForSingleObject(wmcda->hThread, INFINITE);
1032 CloseHandle(wmcda->hThread);
1033 wmcda->hThread = 0;
1034 CloseHandle(wmcda->stopEvent);
1035 wmcda->stopEvent = 0;
1037 IDirectSoundBuffer_Release(wmcda->dsBuf);
1038 wmcda->dsBuf = NULL;
1039 IDirectSound_Release(wmcda->dsObj);
1040 wmcda->dsObj = NULL;
1042 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_STOP_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1043 return MCIERR_HARDWARE;
1045 if (wmcda->hCallback) {
1046 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1047 wmcda->hCallback = NULL;
1050 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1051 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1052 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1053 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1055 return 0;
1058 /**************************************************************************
1059 * MCICDA_Pause [internal]
1061 static DWORD MCICDA_Pause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1063 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1064 DWORD br;
1066 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1068 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1070 if (wmcda->hThread != 0) {
1071 /* Don't bother calling stop if the playLoop thread has already stopped */
1072 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1073 FAILED(IDirectSoundBuffer_Stop(wmcda->dsBuf)))
1074 return MCIERR_HARDWARE;
1076 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1077 return MCIERR_HARDWARE;
1079 EnterCriticalSection(&wmcda->cs);
1080 if (wmcda->hCallback) {
1081 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID, MCI_NOTIFY_SUPERSEDED);
1082 wmcda->hCallback = NULL;
1084 LeaveCriticalSection(&wmcda->cs);
1086 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1087 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1088 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1089 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1091 return 0;
1094 /**************************************************************************
1095 * MCICDA_Resume [internal]
1097 static DWORD MCICDA_Resume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1099 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1100 DWORD br;
1102 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1104 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1106 if (wmcda->hThread != 0) {
1107 /* Don't restart if the playLoop thread has already stopped */
1108 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1109 FAILED(IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING)))
1110 return MCIERR_HARDWARE;
1112 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_RESUME_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1113 return MCIERR_HARDWARE;
1115 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1116 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1117 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1118 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1120 return 0;
1123 /**************************************************************************
1124 * MCICDA_Seek [internal]
1126 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1128 DWORD at;
1129 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1130 CDROM_SEEK_AUDIO_MSF seek;
1131 DWORD br, ret;
1132 CDROM_TOC toc;
1134 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1136 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1137 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1139 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
1140 &toc, sizeof(toc), &br, NULL)) {
1141 WARN("error reading TOC !\n");
1142 return MCICDA_GetError(wmcda);
1144 switch (dwFlags & ~(MCI_NOTIFY|MCI_WAIT)) {
1145 case MCI_SEEK_TO_START:
1146 TRACE("Seeking to start\n");
1147 at = FRAME_OF_TOC(toc,toc.FirstTrack);
1148 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1149 return ret;
1150 break;
1151 case MCI_SEEK_TO_END:
1152 TRACE("Seeking to end\n");
1153 at = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
1154 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1155 return ret;
1156 break;
1157 case MCI_TO:
1158 TRACE("Seeking to %u\n", lpParms->dwTo);
1159 at = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
1160 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1161 return ret;
1162 break;
1163 default:
1164 TRACE("Unknown seek action %08lX\n",
1165 (dwFlags & ~(MCI_NOTIFY|MCI_WAIT)));
1166 return MCIERR_UNSUPPORTED_FUNCTION;
1169 if (wmcda->hThread != 0) {
1170 EnterCriticalSection(&wmcda->cs);
1171 wmcda->start = at - FRAME_OF_TOC(toc, toc.FirstTrack);
1172 /* Flush remaining data, or just let it play into the new data? */
1173 LeaveCriticalSection(&wmcda->cs);
1175 else {
1176 seek.M = at / CDFRAMES_PERMIN;
1177 seek.S = (at / CDFRAMES_PERSEC) % 60;
1178 seek.F = at % CDFRAMES_PERSEC;
1179 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_SEEK_AUDIO_MSF, &seek, sizeof(seek),
1180 NULL, 0, &br, NULL))
1181 return MCIERR_HARDWARE;
1184 if (dwFlags & MCI_NOTIFY) {
1185 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1186 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1187 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1189 return 0;
1192 /**************************************************************************
1193 * MCICDA_SetDoor [internal]
1195 static DWORD MCICDA_SetDoor(UINT wDevID, BOOL open)
1197 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1198 DWORD br;
1200 TRACE("(%04x, %s) !\n", wDevID, (open) ? "OPEN" : "CLOSE");
1202 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1204 if (!DeviceIoControl(wmcda->handle,
1205 (open) ? IOCTL_STORAGE_EJECT_MEDIA : IOCTL_STORAGE_LOAD_MEDIA,
1206 NULL, 0, NULL, 0, &br, NULL))
1207 return MCIERR_HARDWARE;
1209 return 0;
1212 /**************************************************************************
1213 * MCICDA_Set [internal]
1215 static DWORD MCICDA_Set(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1217 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1219 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1221 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1223 if (dwFlags & MCI_SET_DOOR_OPEN) {
1224 MCICDA_SetDoor(wDevID, TRUE);
1226 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1227 MCICDA_SetDoor(wDevID, FALSE);
1230 /* only functions which require valid lpParms below this line ! */
1231 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1233 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1234 TRACE("dwAudio=%08lX\n", lpParms->dwAudio);
1236 if (dwFlags & MCI_SET_TIME_FORMAT) {
1237 switch (lpParms->dwTimeFormat) {
1238 case MCI_FORMAT_MILLISECONDS:
1239 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1240 break;
1241 case MCI_FORMAT_MSF:
1242 TRACE("MCI_FORMAT_MSF !\n");
1243 break;
1244 case MCI_FORMAT_TMSF:
1245 TRACE("MCI_FORMAT_TMSF !\n");
1246 break;
1247 default:
1248 WARN("bad time format !\n");
1249 return MCIERR_BAD_TIME_FORMAT;
1251 wmcda->dwTimeFormat = lpParms->dwTimeFormat;
1253 if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
1254 if (dwFlags & MCI_SET_ON) return MCIERR_UNSUPPORTED_FUNCTION;
1255 if (dwFlags & MCI_SET_OFF) return MCIERR_UNSUPPORTED_FUNCTION;
1256 if (dwFlags & MCI_NOTIFY) {
1257 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n",
1258 lpParms->dwCallback);
1259 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1260 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1262 return 0;
1265 /**************************************************************************
1266 * DriverProc (MCICDA.@)
1268 LRESULT CALLBACK MCICDA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1269 LPARAM dwParam1, LPARAM dwParam2)
1271 switch(wMsg) {
1272 case DRV_LOAD: return 1;
1273 case DRV_FREE: return 1;
1274 case DRV_OPEN: return MCICDA_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1275 case DRV_CLOSE: return MCICDA_drvClose(dwDevID);
1276 case DRV_ENABLE: return 1;
1277 case DRV_DISABLE: return 1;
1278 case DRV_QUERYCONFIGURE: return 1;
1279 case DRV_CONFIGURE: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK); return 1;
1280 case DRV_INSTALL: return DRVCNF_RESTART;
1281 case DRV_REMOVE: return DRVCNF_RESTART;
1284 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1286 switch (wMsg) {
1287 case MCI_OPEN_DRIVER: return MCICDA_Open(dwDevID, dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1288 case MCI_CLOSE_DRIVER: return MCICDA_Close(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1289 case MCI_GETDEVCAPS: return MCICDA_GetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1290 case MCI_INFO: return MCICDA_Info(dwDevID, dwParam1, (LPMCI_INFO_PARMSW)dwParam2);
1291 case MCI_STATUS: return MCICDA_Status(dwDevID, dwParam1, (LPMCI_STATUS_PARMS)dwParam2);
1292 case MCI_SET: return MCICDA_Set(dwDevID, dwParam1, (LPMCI_SET_PARMS)dwParam2);
1293 case MCI_PLAY: return MCICDA_Play(dwDevID, dwParam1, (LPMCI_PLAY_PARMS)dwParam2);
1294 case MCI_STOP: return MCICDA_Stop(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1295 case MCI_PAUSE: return MCICDA_Pause(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1296 case MCI_RESUME: return MCICDA_Resume(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1297 case MCI_SEEK: return MCICDA_Seek(dwDevID, dwParam1, (LPMCI_SEEK_PARMS)dwParam2);
1298 /* commands that should report an error as they are not supported in
1299 * the native version */
1300 case MCI_SET_DOOR_CLOSED:
1301 case MCI_SET_DOOR_OPEN:
1302 case MCI_LOAD:
1303 case MCI_SAVE:
1304 case MCI_FREEZE:
1305 case MCI_PUT:
1306 case MCI_REALIZE:
1307 case MCI_UNFREEZE:
1308 case MCI_UPDATE:
1309 case MCI_WHERE:
1310 case MCI_STEP:
1311 case MCI_SPIN:
1312 case MCI_ESCAPE:
1313 case MCI_COPY:
1314 case MCI_CUT:
1315 case MCI_DELETE:
1316 case MCI_PASTE:
1317 case MCI_WINDOW:
1318 TRACE("Unsupported command [0x%x]\n", wMsg);
1319 break;
1320 case MCI_OPEN:
1321 case MCI_CLOSE:
1322 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1323 break;
1324 default:
1325 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg);
1326 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1328 return MCIERR_UNRECOGNIZED_COMMAND;
1331 /*-----------------------------------------------------------------------*/