regedit: Update Italian translation.
[wine/hacks.git] / dlls / mcicda / mcicda.c
blobcb8840e1cf48cded9ab36b07b9bb9b78d0016354
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 goto the_error;
470 if (dwFlags & MCI_NOTIFY) {
471 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpOpenParms->dwCallback);
472 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
473 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
475 return 0;
477 the_error:
478 --wmcda->nUseCount;
479 return ret;
482 /**************************************************************************
483 * MCICDA_Close [internal]
485 static DWORD MCICDA_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
487 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
489 TRACE("(%04X, %08X, %p);\n", wDevID, dwParam, lpParms);
491 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
493 MCICDA_Stop(wDevID, MCI_WAIT, NULL);
495 if (--wmcda->nUseCount == 0) {
496 CloseHandle(wmcda->handle);
498 return 0;
501 /**************************************************************************
502 * MCICDA_GetDevCaps [internal]
504 static DWORD MCICDA_GetDevCaps(UINT wDevID, DWORD dwFlags,
505 LPMCI_GETDEVCAPS_PARMS lpParms)
507 DWORD ret = 0;
509 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
511 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
513 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
514 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms->dwItem);
516 switch (lpParms->dwItem) {
517 case MCI_GETDEVCAPS_CAN_RECORD:
518 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
519 ret = MCI_RESOURCE_RETURNED;
520 break;
521 case MCI_GETDEVCAPS_HAS_AUDIO:
522 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
523 ret = MCI_RESOURCE_RETURNED;
524 break;
525 case MCI_GETDEVCAPS_HAS_VIDEO:
526 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
527 ret = MCI_RESOURCE_RETURNED;
528 break;
529 case MCI_GETDEVCAPS_DEVICE_TYPE:
530 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO, MCI_DEVTYPE_CD_AUDIO);
531 ret = MCI_RESOURCE_RETURNED;
532 break;
533 case MCI_GETDEVCAPS_USES_FILES:
534 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
535 ret = MCI_RESOURCE_RETURNED;
536 break;
537 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
538 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
539 ret = MCI_RESOURCE_RETURNED;
540 break;
541 case MCI_GETDEVCAPS_CAN_EJECT:
542 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
543 ret = MCI_RESOURCE_RETURNED;
544 break;
545 case MCI_GETDEVCAPS_CAN_PLAY:
546 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
547 ret = MCI_RESOURCE_RETURNED;
548 break;
549 case MCI_GETDEVCAPS_CAN_SAVE:
550 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
551 ret = MCI_RESOURCE_RETURNED;
552 break;
553 default:
554 ERR("Unsupported %x devCaps item\n", lpParms->dwItem);
555 return MCIERR_UNRECOGNIZED_COMMAND;
557 } else {
558 TRACE("No GetDevCaps-Item !\n");
559 return MCIERR_UNRECOGNIZED_COMMAND;
561 TRACE("lpParms->dwReturn=%08X;\n", lpParms->dwReturn);
562 return ret;
565 static DWORD CDROM_Audio_GetSerial(CDROM_TOC* toc)
567 DWORD serial = 0;
568 int i;
569 WORD wMagic;
570 DWORD dwStart, dwEnd;
573 * wMagic collects the wFrames from track 1
574 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
575 * frames.
576 * There it is collected for correcting the serial when there are less than
577 * 3 tracks.
579 wMagic = toc->TrackData[0].Address[3];
580 dwStart = FRAME_OF_TOC(*toc, toc->FirstTrack);
582 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++) {
583 serial += (toc->TrackData[i].Address[1] << 16) |
584 (toc->TrackData[i].Address[2] << 8) | toc->TrackData[i].Address[3];
586 dwEnd = FRAME_OF_TOC(*toc, toc->LastTrack + 1);
588 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
589 serial += wMagic + (dwEnd - dwStart);
591 return serial;
595 /**************************************************************************
596 * MCICDA_Info [internal]
598 static DWORD MCICDA_Info(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
600 LPCWSTR str = NULL;
601 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
602 DWORD ret = 0;
603 WCHAR buffer[16];
605 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
607 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
608 return MCIERR_NULL_PARAMETER_BLOCK;
609 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
611 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
613 if (dwFlags & MCI_INFO_PRODUCT) {
614 static const WCHAR wszAudioCd[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
615 str = wszAudioCd;
616 } else if (dwFlags & MCI_INFO_MEDIA_UPC) {
617 ret = MCIERR_NO_IDENTITY;
618 } else if (dwFlags & MCI_INFO_MEDIA_IDENTITY) {
619 DWORD res = 0;
620 CDROM_TOC toc;
621 DWORD br;
622 static const WCHAR wszLu[] = {'%','l','u',0};
624 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
625 &toc, sizeof(toc), &br, NULL)) {
626 return MCICDA_GetError(wmcda);
629 res = CDROM_Audio_GetSerial(&toc);
630 sprintfW(buffer, wszLu, res);
631 str = buffer;
632 } else {
633 WARN("Don't know this info command (%u)\n", dwFlags);
634 ret = MCIERR_UNRECOGNIZED_COMMAND;
636 if (str) {
637 if (lpParms->dwRetSize <= strlenW(str)) {
638 lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
639 ret = MCIERR_PARAM_OVERFLOW;
640 } else {
641 strcpyW(lpParms->lpstrReturn, str);
643 } else {
644 *lpParms->lpstrReturn = 0;
646 TRACE("=> %s (%d)\n", debugstr_w(lpParms->lpstrReturn), ret);
647 return ret;
650 /**************************************************************************
651 * MCICDA_Status [internal]
653 static DWORD MCICDA_Status(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
655 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
656 DWORD ret = 0;
657 CDROM_SUB_Q_DATA_FORMAT fmt;
658 SUB_Q_CHANNEL_DATA data;
659 CDROM_TOC toc;
660 DWORD br;
662 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
664 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
665 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
667 if (dwFlags & MCI_NOTIFY) {
668 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
669 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
670 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
672 if (dwFlags & MCI_STATUS_ITEM) {
673 TRACE("dwItem = %x\n", lpParms->dwItem);
674 switch (lpParms->dwItem) {
675 case MCI_STATUS_CURRENT_TRACK:
676 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
677 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
678 &data, sizeof(data), &br, NULL))
680 return MCICDA_GetError(wmcda);
682 lpParms->dwReturn = data.CurrentPosition.TrackNumber;
683 TRACE("CURRENT_TRACK=%lu\n", lpParms->dwReturn);
684 break;
685 case MCI_STATUS_LENGTH:
686 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
687 &toc, sizeof(toc), &br, NULL)) {
688 WARN("error reading TOC !\n");
689 return MCICDA_GetError(wmcda);
691 if (dwFlags & MCI_TRACK) {
692 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms->dwTrack);
693 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
694 return MCIERR_OUTOFRANGE;
695 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack + 1) -
696 FRAME_OF_TOC(toc, lpParms->dwTrack);
697 /* Windows returns one frame less than the total track length for the
698 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
699 if (lpParms->dwTrack == toc.LastTrack)
700 lpParms->dwReturn--;
701 } else {
702 /* Sum of the lengths of all of the tracks. Inherits the
703 'off by one frame' behavior from the length of the last track.
704 See above comment. */
705 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.LastTrack + 1) -
706 FRAME_OF_TOC(toc, toc.FirstTrack) - 1;
708 lpParms->dwReturn = MCICDA_CalcTime(wmcda,
709 (wmcda->dwTimeFormat == MCI_FORMAT_TMSF)
710 ? MCI_FORMAT_MSF : wmcda->dwTimeFormat,
711 lpParms->dwReturn,
712 &ret);
713 TRACE("LENGTH=%lu\n", lpParms->dwReturn);
714 break;
715 case MCI_STATUS_MODE:
716 lpParms->dwReturn = MCICDA_GetStatus(wmcda);
717 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms->dwReturn);
718 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn, lpParms->dwReturn);
719 ret = MCI_RESOURCE_RETURNED;
720 break;
721 case MCI_STATUS_MEDIA_PRESENT:
722 lpParms->dwReturn = (MCICDA_GetStatus(wmcda) == MCI_MODE_OPEN) ?
723 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
724 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms->dwReturn) ? 'Y' : 'N');
725 ret = MCI_RESOURCE_RETURNED;
726 break;
727 case MCI_STATUS_NUMBER_OF_TRACKS:
728 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
729 &toc, sizeof(toc), &br, NULL)) {
730 WARN("error reading TOC !\n");
731 return MCICDA_GetError(wmcda);
733 lpParms->dwReturn = toc.LastTrack - toc.FirstTrack + 1;
734 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms->dwReturn);
735 if (lpParms->dwReturn == (WORD)-1)
736 return MCICDA_GetError(wmcda);
737 break;
738 case MCI_STATUS_POSITION:
739 if (dwFlags & MCI_STATUS_START) {
740 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
741 &toc, sizeof(toc), &br, NULL)) {
742 WARN("error reading TOC !\n");
743 return MCICDA_GetError(wmcda);
745 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.FirstTrack);
746 TRACE("get MCI_STATUS_START !\n");
747 } else if (dwFlags & MCI_TRACK) {
748 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
749 &toc, sizeof(toc), &br, NULL)) {
750 WARN("error reading TOC !\n");
751 return MCICDA_GetError(wmcda);
753 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
754 return MCIERR_OUTOFRANGE;
755 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack);
756 TRACE("get MCI_TRACK #%u !\n", lpParms->dwTrack);
757 } else {
758 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
759 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
760 &data, sizeof(data), &br, NULL)) {
761 return MCICDA_GetError(wmcda);
763 lpParms->dwReturn = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
765 lpParms->dwReturn = MCICDA_CalcTime(wmcda, wmcda->dwTimeFormat, lpParms->dwReturn, &ret);
766 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms->dwReturn);
767 break;
768 case MCI_STATUS_READY:
769 TRACE("MCI_STATUS_READY !\n");
770 switch (MCICDA_GetStatus(wmcda))
772 case MCI_MODE_NOT_READY:
773 case MCI_MODE_OPEN:
774 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
775 break;
776 default:
777 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
778 break;
780 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms->dwReturn));
781 ret = MCI_RESOURCE_RETURNED;
782 break;
783 case MCI_STATUS_TIME_FORMAT:
784 lpParms->dwReturn = MAKEMCIRESOURCE(wmcda->dwTimeFormat, MCI_FORMAT_RETURN_BASE + wmcda->dwTimeFormat);
785 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms->dwReturn));
786 ret = MCI_RESOURCE_RETURNED;
787 break;
788 case 4001: /* FIXME: for bogus FullCD */
789 case MCI_CDA_STATUS_TYPE_TRACK:
790 if (!(dwFlags & MCI_TRACK))
791 ret = MCIERR_MISSING_PARAMETER;
792 else {
793 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
794 &toc, sizeof(toc), &br, NULL)) {
795 WARN("error reading TOC !\n");
796 return MCICDA_GetError(wmcda);
798 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
799 ret = MCIERR_OUTOFRANGE;
800 else
801 lpParms->dwReturn = (toc.TrackData[lpParms->dwTrack - toc.FirstTrack].Control & 0x04) ?
802 MCI_CDA_TRACK_OTHER : MCI_CDA_TRACK_AUDIO;
804 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms->dwTrack, lpParms->dwReturn);
805 break;
806 default:
807 FIXME("unknown command %08X !\n", lpParms->dwItem);
808 return MCIERR_UNRECOGNIZED_COMMAND;
810 } else {
811 WARN("not MCI_STATUS_ITEM !\n");
813 return ret;
816 /**************************************************************************
817 * MCICDA_SkipDataTracks [internal]
819 static DWORD MCICDA_SkipDataTracks(WINE_MCICDAUDIO* wmcda,DWORD *frame)
821 int i;
822 DWORD br;
823 CDROM_TOC toc;
824 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
825 &toc, sizeof(toc), &br, NULL)) {
826 WARN("error reading TOC !\n");
827 return MCICDA_GetError(wmcda);
829 /* Locate first track whose starting frame is bigger than frame */
830 for(i=toc.FirstTrack;i<=toc.LastTrack+1;i++)
831 if ( FRAME_OF_TOC(toc, i) > *frame ) break;
832 if (i <= toc.FirstTrack && i>toc.LastTrack+1) {
833 i = 0; /* requested address is out of range: go back to start */
834 *frame = FRAME_OF_TOC(toc,toc.FirstTrack);
836 else
837 i--;
838 /* i points to last track whose start address is not greater than frame.
839 * Now skip non-audio tracks */
840 for(;i<=toc.LastTrack+1;i++)
841 if ( ! (toc.TrackData[i-toc.FirstTrack].Control & 4) )
842 break;
843 /* The frame will be an address in the next audio track or
844 * address of lead-out. */
845 if ( FRAME_OF_TOC(toc, i) > *frame )
846 *frame = FRAME_OF_TOC(toc, i);
847 return 0;
850 /**************************************************************************
851 * MCICDA_Play [internal]
853 static DWORD MCICDA_Play(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
855 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
856 DWORD ret = 0, start, end;
857 DWORD br;
858 CDROM_PLAY_AUDIO_MSF play;
859 CDROM_SUB_Q_DATA_FORMAT fmt;
860 SUB_Q_CHANNEL_DATA data;
861 CDROM_TOC toc;
863 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
865 if (lpParms == NULL)
866 return MCIERR_NULL_PARAMETER_BLOCK;
868 if (wmcda == NULL)
869 return MCIERR_INVALID_DEVICE_ID;
871 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
872 &toc, sizeof(toc), &br, NULL)) {
873 WARN("error reading TOC !\n");
874 return MCICDA_GetError(wmcda);
877 if (dwFlags & MCI_FROM) {
878 start = MCICDA_CalcFrame(wmcda, lpParms->dwFrom);
879 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
880 return ret;
881 TRACE("MCI_FROM=%08X -> %u\n", lpParms->dwFrom, start);
882 } else {
883 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
884 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
885 &data, sizeof(data), &br, NULL)) {
886 return MCICDA_GetError(wmcda);
888 start = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
889 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
890 return ret;
892 if (dwFlags & MCI_TO) {
893 end = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
894 TRACE("MCI_TO=%08X -> %u\n", lpParms->dwTo, end);
895 } else {
896 end = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
898 TRACE("Playing from %u to %u\n", start, end);
900 if (wmcda->hThread != 0) {
901 SetEvent(wmcda->stopEvent);
902 WaitForSingleObject(wmcda->hThread, INFINITE);
904 CloseHandle(wmcda->hThread);
905 wmcda->hThread = 0;
906 CloseHandle(wmcda->stopEvent);
907 wmcda->stopEvent = 0;
909 IDirectSoundBuffer_Stop(wmcda->dsBuf);
910 IDirectSoundBuffer_Release(wmcda->dsBuf);
911 wmcda->dsBuf = NULL;
912 IDirectSound_Release(wmcda->dsObj);
913 wmcda->dsObj = NULL;
915 else if(wmcda->hCallback) {
916 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID,
917 MCI_NOTIFY_ABORTED);
918 wmcda->hCallback = NULL;
921 if ((dwFlags&MCI_NOTIFY))
922 wmcda->hCallback = HWND_32(LOWORD(lpParms->dwCallback));
924 if (pDirectSoundCreate) {
925 WAVEFORMATEX format;
926 DSBUFFERDESC desc;
927 DWORD lockLen;
928 void *cdData;
929 HRESULT hr;
931 hr = pDirectSoundCreate(NULL, &wmcda->dsObj, NULL);
932 if (SUCCEEDED(hr)) {
933 IDirectSound_SetCooperativeLevel(wmcda->dsObj, GetDesktopWindow(), DSSCL_PRIORITY);
935 /* The "raw" frame is relative to the start of the first track */
936 wmcda->start = start - FRAME_OF_TOC(toc, toc.FirstTrack);
937 wmcda->end = end - FRAME_OF_TOC(toc, toc.FirstTrack);
939 memset(&format, 0, sizeof(format));
940 format.wFormatTag = WAVE_FORMAT_PCM;
941 format.nChannels = 2;
942 format.nSamplesPerSec = 44100;
943 format.wBitsPerSample = 16;
944 format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
945 format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
946 format.cbSize = 0;
948 memset(&desc, 0, sizeof(desc));
949 desc.dwSize = sizeof(desc);
950 desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
951 desc.dwBufferBytes = (CDDA_FRAG_SIZE - (CDDA_FRAG_SIZE%RAW_SECTOR_SIZE)) * CDDA_FRAG_COUNT;
952 desc.lpwfxFormat = &format;
954 hr = IDirectSound_CreateSoundBuffer(wmcda->dsObj, &desc, &wmcda->dsBuf, NULL);
956 if (SUCCEEDED(hr)) {
957 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, 0, 0, &cdData, &lockLen,
958 NULL, NULL, DSBLOCK_ENTIREBUFFER);
960 if (SUCCEEDED(hr)) {
961 RAW_READ_INFO rdInfo;
962 int readok;
964 rdInfo.DiskOffset.QuadPart = wmcda->start<<11;
965 rdInfo.SectorCount = min(desc.dwBufferBytes/RAW_SECTOR_SIZE,
966 wmcda->end-wmcda->start);
967 rdInfo.TrackMode = CDDA;
969 readok = DeviceIoControl(wmcda->handle, IOCTL_CDROM_RAW_READ,
970 &rdInfo, sizeof(rdInfo), cdData, lockLen,
971 &br, NULL);
972 IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
974 if (readok) {
975 wmcda->start += rdInfo.SectorCount;
976 wmcda->stopEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
978 if (wmcda->stopEvent != 0)
979 wmcda->hThread = CreateThread(NULL, 0, MCICDA_playLoop, wmcda, 0, &br);
980 if (wmcda->hThread != 0) {
981 hr = IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING);
982 if (SUCCEEDED(hr))
983 return ret;
985 SetEvent(wmcda->stopEvent);
986 WaitForSingleObject(wmcda->hThread, INFINITE);
987 CloseHandle(wmcda->hThread);
988 wmcda->hThread = 0;
992 if (wmcda->stopEvent != 0) {
993 CloseHandle(wmcda->stopEvent);
994 wmcda->stopEvent = 0;
996 if (wmcda->dsBuf) {
997 IDirectSoundBuffer_Release(wmcda->dsBuf);
998 wmcda->dsBuf = NULL;
1000 if (wmcda->dsObj) {
1001 IDirectSound_Release(wmcda->dsObj);
1002 wmcda->dsObj = NULL;
1006 play.StartingM = start / CDFRAMES_PERMIN;
1007 play.StartingS = (start / CDFRAMES_PERSEC) % 60;
1008 play.StartingF = start % CDFRAMES_PERSEC;
1009 play.EndingM = end / CDFRAMES_PERMIN;
1010 play.EndingS = (end / CDFRAMES_PERSEC) % 60;
1011 play.EndingF = end % CDFRAMES_PERSEC;
1012 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PLAY_AUDIO_MSF, &play, sizeof(play),
1013 NULL, 0, &br, NULL)) {
1014 wmcda->hCallback = NULL;
1015 ret = MCIERR_HARDWARE;
1016 } else if (dwFlags & MCI_NOTIFY) {
1017 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1019 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1020 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1023 return ret;
1026 /**************************************************************************
1027 * MCICDA_Stop [internal]
1029 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1031 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1032 DWORD br;
1034 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1036 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1038 if (wmcda->hThread != 0) {
1039 SetEvent(wmcda->stopEvent);
1040 WaitForSingleObject(wmcda->hThread, INFINITE);
1042 CloseHandle(wmcda->hThread);
1043 wmcda->hThread = 0;
1044 CloseHandle(wmcda->stopEvent);
1045 wmcda->stopEvent = 0;
1047 IDirectSoundBuffer_Release(wmcda->dsBuf);
1048 wmcda->dsBuf = NULL;
1049 IDirectSound_Release(wmcda->dsObj);
1050 wmcda->dsObj = NULL;
1052 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_STOP_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1053 return MCIERR_HARDWARE;
1055 if (wmcda->hCallback) {
1056 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1057 wmcda->hCallback = NULL;
1060 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1061 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1062 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1063 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1065 return 0;
1068 /**************************************************************************
1069 * MCICDA_Pause [internal]
1071 static DWORD MCICDA_Pause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1073 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1074 DWORD br;
1076 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1078 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1080 if (wmcda->hThread != 0) {
1081 /* Don't bother calling stop if the playLoop thread has already stopped */
1082 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1083 FAILED(IDirectSoundBuffer_Stop(wmcda->dsBuf)))
1084 return MCIERR_HARDWARE;
1086 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1087 return MCIERR_HARDWARE;
1089 EnterCriticalSection(&wmcda->cs);
1090 if (wmcda->hCallback) {
1091 mciDriverNotify(wmcda->hCallback, wmcda->wNotifyDeviceID, MCI_NOTIFY_SUPERSEDED);
1092 wmcda->hCallback = NULL;
1094 LeaveCriticalSection(&wmcda->cs);
1096 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1097 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1098 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1099 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1101 return 0;
1104 /**************************************************************************
1105 * MCICDA_Resume [internal]
1107 static DWORD MCICDA_Resume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1109 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1110 DWORD br;
1112 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1114 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1116 if (wmcda->hThread != 0) {
1117 /* Don't restart if the playLoop thread has already stopped */
1118 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1119 FAILED(IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING)))
1120 return MCIERR_HARDWARE;
1122 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_RESUME_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1123 return MCIERR_HARDWARE;
1125 if (lpParms && (dwFlags & MCI_NOTIFY)) {
1126 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1127 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1128 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1130 return 0;
1133 /**************************************************************************
1134 * MCICDA_Seek [internal]
1136 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1138 DWORD at;
1139 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1140 CDROM_SEEK_AUDIO_MSF seek;
1141 DWORD br, ret;
1142 CDROM_TOC toc;
1144 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1146 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1147 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1149 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
1150 &toc, sizeof(toc), &br, NULL)) {
1151 WARN("error reading TOC !\n");
1152 return MCICDA_GetError(wmcda);
1154 switch (dwFlags & ~(MCI_NOTIFY|MCI_WAIT)) {
1155 case MCI_SEEK_TO_START:
1156 TRACE("Seeking to start\n");
1157 at = FRAME_OF_TOC(toc,toc.FirstTrack);
1158 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1159 return ret;
1160 break;
1161 case MCI_SEEK_TO_END:
1162 TRACE("Seeking to end\n");
1163 at = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
1164 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1165 return ret;
1166 break;
1167 case MCI_TO:
1168 TRACE("Seeking to %u\n", lpParms->dwTo);
1169 at = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
1170 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1171 return ret;
1172 break;
1173 default:
1174 TRACE("Unknown seek action %08lX\n",
1175 (dwFlags & ~(MCI_NOTIFY|MCI_WAIT)));
1176 return MCIERR_UNSUPPORTED_FUNCTION;
1179 if (wmcda->hThread != 0) {
1180 EnterCriticalSection(&wmcda->cs);
1181 wmcda->start = at - FRAME_OF_TOC(toc, toc.FirstTrack);
1182 /* Flush remaining data, or just let it play into the new data? */
1183 LeaveCriticalSection(&wmcda->cs);
1185 else {
1186 seek.M = at / CDFRAMES_PERMIN;
1187 seek.S = (at / CDFRAMES_PERSEC) % 60;
1188 seek.F = at % CDFRAMES_PERSEC;
1189 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_SEEK_AUDIO_MSF, &seek, sizeof(seek),
1190 NULL, 0, &br, NULL))
1191 return MCIERR_HARDWARE;
1194 if (dwFlags & MCI_NOTIFY) {
1195 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1196 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1197 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1199 return 0;
1202 /**************************************************************************
1203 * MCICDA_SetDoor [internal]
1205 static DWORD MCICDA_SetDoor(UINT wDevID, BOOL open)
1207 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1208 DWORD br;
1210 TRACE("(%04x, %s) !\n", wDevID, (open) ? "OPEN" : "CLOSE");
1212 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1214 if (!DeviceIoControl(wmcda->handle,
1215 (open) ? IOCTL_STORAGE_EJECT_MEDIA : IOCTL_STORAGE_LOAD_MEDIA,
1216 NULL, 0, NULL, 0, &br, NULL))
1217 return MCIERR_HARDWARE;
1219 return 0;
1222 /**************************************************************************
1223 * MCICDA_Set [internal]
1225 static DWORD MCICDA_Set(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1227 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1229 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1231 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1233 if (dwFlags & MCI_SET_DOOR_OPEN) {
1234 MCICDA_SetDoor(wDevID, TRUE);
1236 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1237 MCICDA_SetDoor(wDevID, FALSE);
1240 /* only functions which require valid lpParms below this line ! */
1241 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1243 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1244 TRACE("dwAudio=%08lX\n", lpParms->dwAudio);
1246 if (dwFlags & MCI_SET_TIME_FORMAT) {
1247 switch (lpParms->dwTimeFormat) {
1248 case MCI_FORMAT_MILLISECONDS:
1249 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1250 break;
1251 case MCI_FORMAT_MSF:
1252 TRACE("MCI_FORMAT_MSF !\n");
1253 break;
1254 case MCI_FORMAT_TMSF:
1255 TRACE("MCI_FORMAT_TMSF !\n");
1256 break;
1257 default:
1258 WARN("bad time format !\n");
1259 return MCIERR_BAD_TIME_FORMAT;
1261 wmcda->dwTimeFormat = lpParms->dwTimeFormat;
1263 if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
1264 if (dwFlags & MCI_SET_ON) return MCIERR_UNSUPPORTED_FUNCTION;
1265 if (dwFlags & MCI_SET_OFF) return MCIERR_UNSUPPORTED_FUNCTION;
1266 if (dwFlags & MCI_NOTIFY) {
1267 TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n",
1268 lpParms->dwCallback);
1269 mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1270 wmcda->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1272 return 0;
1275 /**************************************************************************
1276 * DriverProc (MCICDA.@)
1278 LRESULT CALLBACK MCICDA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1279 LPARAM dwParam1, LPARAM dwParam2)
1281 switch(wMsg) {
1282 case DRV_LOAD: return 1;
1283 case DRV_FREE: return 1;
1284 case DRV_OPEN: return MCICDA_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1285 case DRV_CLOSE: return MCICDA_drvClose(dwDevID);
1286 case DRV_ENABLE: return 1;
1287 case DRV_DISABLE: return 1;
1288 case DRV_QUERYCONFIGURE: return 1;
1289 case DRV_CONFIGURE: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK); return 1;
1290 case DRV_INSTALL: return DRVCNF_RESTART;
1291 case DRV_REMOVE: return DRVCNF_RESTART;
1294 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1296 switch (wMsg) {
1297 case MCI_OPEN_DRIVER: return MCICDA_Open(dwDevID, dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1298 case MCI_CLOSE_DRIVER: return MCICDA_Close(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1299 case MCI_GETDEVCAPS: return MCICDA_GetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1300 case MCI_INFO: return MCICDA_Info(dwDevID, dwParam1, (LPMCI_INFO_PARMSW)dwParam2);
1301 case MCI_STATUS: return MCICDA_Status(dwDevID, dwParam1, (LPMCI_STATUS_PARMS)dwParam2);
1302 case MCI_SET: return MCICDA_Set(dwDevID, dwParam1, (LPMCI_SET_PARMS)dwParam2);
1303 case MCI_PLAY: return MCICDA_Play(dwDevID, dwParam1, (LPMCI_PLAY_PARMS)dwParam2);
1304 case MCI_STOP: return MCICDA_Stop(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1305 case MCI_PAUSE: return MCICDA_Pause(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1306 case MCI_RESUME: return MCICDA_Resume(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1307 case MCI_SEEK: return MCICDA_Seek(dwDevID, dwParam1, (LPMCI_SEEK_PARMS)dwParam2);
1308 /* commands that should report an error as they are not supported in
1309 * the native version */
1310 case MCI_SET_DOOR_CLOSED:
1311 case MCI_SET_DOOR_OPEN:
1312 case MCI_LOAD:
1313 case MCI_SAVE:
1314 case MCI_FREEZE:
1315 case MCI_PUT:
1316 case MCI_REALIZE:
1317 case MCI_UNFREEZE:
1318 case MCI_UPDATE:
1319 case MCI_WHERE:
1320 case MCI_STEP:
1321 case MCI_SPIN:
1322 case MCI_ESCAPE:
1323 case MCI_COPY:
1324 case MCI_CUT:
1325 case MCI_DELETE:
1326 case MCI_PASTE:
1327 case MCI_WINDOW:
1328 TRACE("Unsupported command [0x%x]\n", wMsg);
1329 break;
1330 case MCI_OPEN:
1331 case MCI_CLOSE:
1332 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1333 break;
1334 default:
1335 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg);
1336 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1338 return MCIERR_UNRECOGNIZED_COMMAND;
1341 /*-----------------------------------------------------------------------*/