00d7bb4a77635083f055e29f9746a4a16a1688d9
[reactos.git] / reactos / dll / win32 / mcicda / mcicda.c
1 /*
2 * MCI driver for audio CD (MCICDA)
3 *
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1998-99 Eric Pouech
6 * Copyright 2000 Andreas Mohr
7 *
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.
12 *
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.
17 *
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
21 */
22
23 //#include <config.h>
24 #include <stdarg.h>
25 //#include <stdio.h>
26 //#include <string.h>
27
28 #define WIN32_NO_STATUS
29 #define _INC_WINDOWS
30 #define COM_NO_WINDOWS_H
31 #include <windef.h>
32 //#include "winbase.h"
33 //#include "wingdi.h"
34 #include <winuser.h>
35 #include <wownt32.h>
36 #include <mmddk.h>
37 #include <winioctl.h>
38 #include <ntddcdrm.h>
39 //#include "winternl.h"
40 #include <wine/debug.h>
41 #include <wine/unicode.h>
42 #include <dsound.h>
43
44 WINE_DEFAULT_DEBUG_CHANNEL(mcicda);
45
46 #define CDFRAMES_PERSEC 75
47 #define CDFRAMES_PERMIN (CDFRAMES_PERSEC * 60)
48 #define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
49 #define FRAME_OF_TOC(toc, idx) FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)
50
51 /* Defined by red-book standard; do not change! */
52 #define RAW_SECTOR_SIZE (2352)
53
54 /* Must be >= RAW_SECTOR_SIZE */
55 #define CDDA_FRAG_SIZE (32768)
56 /* Must be >= 2 */
57 #define CDDA_FRAG_COUNT (3)
58
59 typedef struct {
60 UINT wDevID;
61 int nUseCount; /* Incremented for each shared open */
62 BOOL fShareable; /* TRUE if first open was shareable */
63 MCIDEVICEID wNotifyDeviceID; /* MCI device ID with a pending notification */
64 HANDLE hCallback; /* Callback handle for pending notification */
65 DWORD dwTimeFormat;
66 HANDLE handle;
67
68 /* The following are used for digital playback only */
69 HANDLE hThread;
70 HANDLE stopEvent;
71 DWORD start, end;
72
73 IDirectSound *dsObj;
74 IDirectSoundBuffer *dsBuf;
75
76 CRITICAL_SECTION cs;
77 } WINE_MCICDAUDIO;
78
79 /*-----------------------------------------------------------------------*/
80
81 typedef HRESULT(WINAPI*LPDIRECTSOUNDCREATE)(LPCGUID,LPDIRECTSOUND*,LPUNKNOWN);
82 static LPDIRECTSOUNDCREATE pDirectSoundCreate;
83
84 static DWORD CALLBACK MCICDA_playLoop(void *ptr)
85 {
86 WINE_MCICDAUDIO *wmcda = (WINE_MCICDAUDIO*)ptr;
87 DWORD lastPos, curPos, endPos, br;
88 void *cdData;
89 DWORD lockLen, fragLen;
90 DSBCAPS caps;
91 RAW_READ_INFO rdInfo;
92 HRESULT hr = DS_OK;
93
94 memset(&caps, 0, sizeof(caps));
95 caps.dwSize = sizeof(caps);
96 hr = IDirectSoundBuffer_GetCaps(wmcda->dsBuf, &caps);
97
98 fragLen = caps.dwBufferBytes/CDDA_FRAG_COUNT;
99 curPos = lastPos = 0;
100 endPos = ~0u;
101 while (SUCCEEDED(hr) && endPos != lastPos &&
102 WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0) {
103 hr = IDirectSoundBuffer_GetCurrentPosition(wmcda->dsBuf, &curPos, NULL);
104 if ((curPos-lastPos+caps.dwBufferBytes)%caps.dwBufferBytes < fragLen) {
105 Sleep(1);
106 continue;
107 }
108
109 EnterCriticalSection(&wmcda->cs);
110 rdInfo.DiskOffset.QuadPart = wmcda->start<<11;
111 rdInfo.SectorCount = min(fragLen/RAW_SECTOR_SIZE, wmcda->end-wmcda->start);
112 rdInfo.TrackMode = CDDA;
113
114 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, lastPos, fragLen, &cdData, &lockLen, NULL, NULL, 0);
115 if (hr == DSERR_BUFFERLOST) {
116 if(FAILED(IDirectSoundBuffer_Restore(wmcda->dsBuf)) ||
117 FAILED(IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING))) {
118 LeaveCriticalSection(&wmcda->cs);
119 break;
120 }
121 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, lastPos, fragLen, &cdData, &lockLen, NULL, NULL, 0);
122 }
123
124 if (SUCCEEDED(hr)) {
125 if (rdInfo.SectorCount > 0) {
126 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_RAW_READ, &rdInfo, sizeof(rdInfo), cdData, lockLen, &br, NULL))
127 WARN("CD read failed at sector %d: 0x%x\n", wmcda->start, GetLastError());
128 }
129 if (rdInfo.SectorCount*RAW_SECTOR_SIZE < lockLen) {
130 if(endPos == ~0u) endPos = lastPos;
131 memset((BYTE*)cdData + rdInfo.SectorCount*RAW_SECTOR_SIZE, 0,
132 lockLen - rdInfo.SectorCount*RAW_SECTOR_SIZE);
133 }
134 hr = IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
135 }
136
137 lastPos += fragLen;
138 lastPos %= caps.dwBufferBytes;
139 wmcda->start += rdInfo.SectorCount;
140
141 LeaveCriticalSection(&wmcda->cs);
142 }
143 IDirectSoundBuffer_Stop(wmcda->dsBuf);
144 SetEvent(wmcda->stopEvent);
145
146 /* A design bug in native: the independent CD player called by the
147 * MCI has no means to signal end of playing, therefore the MCI
148 * notification is left hanging. MCI_NOTIFY_SUPERSEDED will be
149 * signaled by the next command that has MCI_NOTIFY set (or
150 * MCI_NOTIFY_ABORTED for MCI_PLAY). */
151
152 return 0;
153 }
154
155
156
157 /**************************************************************************
158 * MCICDA_drvOpen [internal]
159 */
160 static DWORD MCICDA_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
161 {
162 static HMODULE dsHandle;
163 WINE_MCICDAUDIO* wmcda;
164
165 if (!modp) return 0xFFFFFFFF;
166 /* FIXME: MCIERR_CANNOT_LOAD_DRIVER if there's no drive of type CD-ROM */
167
168 wmcda = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCICDAUDIO));
169
170 if (!wmcda)
171 return 0;
172
173 if (!dsHandle) {
174 dsHandle = LoadLibraryA("dsound.dll");
175 if(dsHandle)
176 pDirectSoundCreate = (LPDIRECTSOUNDCREATE)GetProcAddress(dsHandle, "DirectSoundCreate");
177 }
178
179 wmcda->wDevID = modp->wDeviceID;
180 mciSetDriverData(wmcda->wDevID, (DWORD_PTR)wmcda);
181 modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
182 modp->wType = MCI_DEVTYPE_CD_AUDIO;
183 InitializeCriticalSection(&wmcda->cs);
184 wmcda->cs.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": WINE_MCICDAUDIO.cs");
185 return modp->wDeviceID;
186 }
187
188 /**************************************************************************
189 * MCICDA_drvClose [internal]
190 */
191 static DWORD MCICDA_drvClose(DWORD dwDevID)
192 {
193 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(dwDevID);
194
195 if (wmcda) {
196 wmcda->cs.DebugInfo->Spare[0] = 0;
197 DeleteCriticalSection(&wmcda->cs);
198 HeapFree(GetProcessHeap(), 0, wmcda);
199 mciSetDriverData(dwDevID, 0);
200 }
201 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
202 }
203
204 /**************************************************************************
205 * MCICDA_GetOpenDrv [internal]
206 */
207 static WINE_MCICDAUDIO* MCICDA_GetOpenDrv(UINT wDevID)
208 {
209 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
210
211 if (wmcda == NULL || wmcda->nUseCount == 0) {
212 WARN("Invalid wDevID=%u\n", wDevID);
213 return 0;
214 }
215 return wmcda;
216 }
217
218 /**************************************************************************
219 * MCICDA_mciNotify [internal]
220 *
221 * Notifications in MCI work like a 1-element queue.
222 * Each new notification request supersedes the previous one.
223 */
224 static void MCICDA_Notify(DWORD_PTR hWndCallBack, WINE_MCICDAUDIO* wmcda, UINT wStatus)
225 {
226 MCIDEVICEID wDevID = wmcda->wNotifyDeviceID;
227 HANDLE old = InterlockedExchangePointer(&wmcda->hCallback, NULL);
228 if (old) mciDriverNotify(old, wDevID, MCI_NOTIFY_SUPERSEDED);
229 mciDriverNotify(HWND_32(LOWORD(hWndCallBack)), wDevID, wStatus);
230 }
231
232 /**************************************************************************
233 * MCICDA_GetStatus [internal]
234 */
235 static DWORD MCICDA_GetStatus(WINE_MCICDAUDIO* wmcda)
236 {
237 CDROM_SUB_Q_DATA_FORMAT fmt;
238 SUB_Q_CHANNEL_DATA data;
239 DWORD br;
240 DWORD mode = MCI_MODE_NOT_READY;
241
242 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
243 if(wmcda->hThread != 0) {
244 DWORD status;
245 HRESULT hr;
246
247 hr = IDirectSoundBuffer_GetStatus(wmcda->dsBuf, &status);
248 if(SUCCEEDED(hr)) {
249 if(!(status&DSBSTATUS_PLAYING)) {
250 if(WaitForSingleObject(wmcda->stopEvent, 0) == WAIT_OBJECT_0)
251 mode = MCI_MODE_STOP;
252 else
253 mode = MCI_MODE_PAUSE;
254 }
255 else
256 mode = MCI_MODE_PLAY;
257 }
258 }
259 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
260 &data, sizeof(data), &br, NULL)) {
261 if (GetLastError() == ERROR_NOT_READY) mode = MCI_MODE_OPEN;
262 } else {
263 switch (data.CurrentPosition.Header.AudioStatus)
264 {
265 case AUDIO_STATUS_IN_PROGRESS: mode = MCI_MODE_PLAY; break;
266 case AUDIO_STATUS_PAUSED: mode = MCI_MODE_PAUSE; break;
267 case AUDIO_STATUS_NO_STATUS:
268 case AUDIO_STATUS_PLAY_COMPLETE: mode = MCI_MODE_STOP; break;
269 case AUDIO_STATUS_PLAY_ERROR:
270 case AUDIO_STATUS_NOT_SUPPORTED:
271 default:
272 break;
273 }
274 }
275 return mode;
276 }
277
278 /**************************************************************************
279 * MCICDA_GetError [internal]
280 */
281 static int MCICDA_GetError(WINE_MCICDAUDIO* wmcda)
282 {
283 switch (GetLastError())
284 {
285 case ERROR_NOT_READY: return MCIERR_DEVICE_NOT_READY;
286 case ERROR_NOT_SUPPORTED:
287 case ERROR_IO_DEVICE: return MCIERR_HARDWARE;
288 default:
289 FIXME("Unknown mode %u\n", GetLastError());
290 }
291 return MCIERR_DRIVER_INTERNAL;
292 }
293
294 /**************************************************************************
295 * MCICDA_CalcFrame [internal]
296 */
297 static DWORD MCICDA_CalcFrame(WINE_MCICDAUDIO* wmcda, DWORD dwTime)
298 {
299 DWORD dwFrame = 0;
300 UINT wTrack;
301 CDROM_TOC toc;
302 DWORD br;
303 BYTE* addr;
304
305 TRACE("(%p, %08X, %u);\n", wmcda, wmcda->dwTimeFormat, dwTime);
306
307 switch (wmcda->dwTimeFormat) {
308 case MCI_FORMAT_MILLISECONDS:
309 dwFrame = ((dwTime - 1) * CDFRAMES_PERSEC + 500) / 1000;
310 TRACE("MILLISECONDS %u\n", dwFrame);
311 break;
312 case MCI_FORMAT_MSF:
313 TRACE("MSF %02u:%02u:%02u\n",
314 MCI_MSF_MINUTE(dwTime), MCI_MSF_SECOND(dwTime), MCI_MSF_FRAME(dwTime));
315 dwFrame += CDFRAMES_PERMIN * MCI_MSF_MINUTE(dwTime);
316 dwFrame += CDFRAMES_PERSEC * MCI_MSF_SECOND(dwTime);
317 dwFrame += MCI_MSF_FRAME(dwTime);
318 break;
319 case MCI_FORMAT_TMSF:
320 default: /* unknown format ! force TMSF ! ... */
321 wTrack = MCI_TMSF_TRACK(dwTime);
322 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
323 &toc, sizeof(toc), &br, NULL))
324 return 0;
325 if (wTrack < toc.FirstTrack || wTrack > toc.LastTrack)
326 return 0;
327 TRACE("MSF %02u-%02u:%02u:%02u\n",
328 MCI_TMSF_TRACK(dwTime), MCI_TMSF_MINUTE(dwTime),
329 MCI_TMSF_SECOND(dwTime), MCI_TMSF_FRAME(dwTime));
330 addr = toc.TrackData[wTrack - toc.FirstTrack].Address;
331 TRACE("TMSF trackpos[%u]=%d:%d:%d\n",
332 wTrack, addr[1], addr[2], addr[3]);
333 dwFrame = CDFRAMES_PERMIN * (addr[1] + MCI_TMSF_MINUTE(dwTime)) +
334 CDFRAMES_PERSEC * (addr[2] + MCI_TMSF_SECOND(dwTime)) +
335 addr[3] + MCI_TMSF_FRAME(dwTime);
336 break;
337 }
338 return dwFrame;
339 }
340
341 /**************************************************************************
342 * MCICDA_CalcTime [internal]
343 */
344 static DWORD MCICDA_CalcTime(WINE_MCICDAUDIO* wmcda, DWORD tf, DWORD dwFrame, LPDWORD lpRet)
345 {
346 DWORD dwTime = 0;
347 UINT wTrack;
348 UINT wMinutes;
349 UINT wSeconds;
350 UINT wFrames;
351 CDROM_TOC toc;
352 DWORD br;
353
354 TRACE("(%p, %08X, %u);\n", wmcda, tf, dwFrame);
355
356 switch (tf) {
357 case MCI_FORMAT_MILLISECONDS:
358 dwTime = (dwFrame * 1000) / CDFRAMES_PERSEC + 1;
359 TRACE("MILLISECONDS %u\n", dwTime);
360 *lpRet = 0;
361 break;
362 case MCI_FORMAT_MSF:
363 wMinutes = dwFrame / CDFRAMES_PERMIN;
364 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
365 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - CDFRAMES_PERSEC * wSeconds;
366 dwTime = MCI_MAKE_MSF(wMinutes, wSeconds, wFrames);
367 TRACE("MSF %02u:%02u:%02u -> dwTime=%u\n",
368 wMinutes, wSeconds, wFrames, dwTime);
369 *lpRet = MCI_COLONIZED3_RETURN;
370 break;
371 case MCI_FORMAT_TMSF:
372 default: /* unknown format ! force TMSF ! ... */
373 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
374 &toc, sizeof(toc), &br, NULL))
375 return 0;
376 if (dwFrame < FRAME_OF_TOC(toc, toc.FirstTrack) ||
377 dwFrame > FRAME_OF_TOC(toc, toc.LastTrack + 1)) {
378 ERR("Out of range value %u [%u,%u]\n",
379 dwFrame, FRAME_OF_TOC(toc, toc.FirstTrack),
380 FRAME_OF_TOC(toc, toc.LastTrack + 1));
381 *lpRet = 0;
382 return 0;
383 }
384 for (wTrack = toc.FirstTrack; wTrack <= toc.LastTrack; wTrack++) {
385 if (FRAME_OF_TOC(toc, wTrack) > dwFrame)
386 break;
387 }
388 wTrack--;
389 dwFrame -= FRAME_OF_TOC(toc, wTrack);
390 wMinutes = dwFrame / CDFRAMES_PERMIN;
391 wSeconds = (dwFrame - CDFRAMES_PERMIN * wMinutes) / CDFRAMES_PERSEC;
392 wFrames = dwFrame - CDFRAMES_PERMIN * wMinutes - CDFRAMES_PERSEC * wSeconds;
393 dwTime = MCI_MAKE_TMSF(wTrack, wMinutes, wSeconds, wFrames);
394 TRACE("%02u-%02u:%02u:%02u\n", wTrack, wMinutes, wSeconds, wFrames);
395 *lpRet = MCI_COLONIZED4_RETURN;
396 break;
397 }
398 return dwTime;
399 }
400
401 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
402
403 /**************************************************************************
404 * MCICDA_Open [internal]
405 */
406 static DWORD MCICDA_Open(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpOpenParms)
407 {
408 MCIDEVICEID dwDeviceID;
409 DWORD ret;
410 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
411 WCHAR root[7], drive = 0;
412
413 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpOpenParms);
414
415 if (lpOpenParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
416 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
417
418 dwDeviceID = lpOpenParms->wDeviceID;
419
420 if (wmcda->nUseCount > 0) {
421 /* The driver is already open on this channel */
422 /* If the driver was opened shareable before and this open specifies */
423 /* shareable then increment the use count */
424 if (wmcda->fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
425 ++wmcda->nUseCount;
426 else
427 return MCIERR_MUST_USE_SHAREABLE;
428 } else {
429 wmcda->nUseCount = 1;
430 wmcda->fShareable = dwFlags & MCI_OPEN_SHAREABLE;
431 }
432 if (dwFlags & MCI_OPEN_ELEMENT) {
433 if (dwFlags & MCI_OPEN_ELEMENT_ID) {
434 WARN("MCI_OPEN_ELEMENT_ID %p! Abort\n", lpOpenParms->lpstrElementName);
435 ret = MCIERR_FLAGS_NOT_COMPATIBLE;
436 goto the_error;
437 }
438 TRACE("MCI_OPEN_ELEMENT element name: %s\n", debugstr_w(lpOpenParms->lpstrElementName));
439 /* Only the first letter counts since w2k
440 * Win9x-NT accept only d: and w98SE accepts d:\foobar as well.
441 * Play d:\Track03.cda plays from the first track, not #3. */
442 if (!isalpha(lpOpenParms->lpstrElementName[0]))
443 {
444 ret = MCIERR_INVALID_FILE;
445 goto the_error;
446 }
447 drive = toupper(lpOpenParms->lpstrElementName[0]);
448 root[0] = drive; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
449 if (GetDriveTypeW(root) != DRIVE_CDROM)
450 {
451 ret = MCIERR_INVALID_FILE;
452 goto the_error;
453 }
454 }
455 else
456 {
457 root[0] = 'A'; root[1] = ':'; root[2] = '\\'; root[3] = '\0';
458 for ( ; root[0] <= 'Z'; root[0]++)
459 {
460 if (GetDriveTypeW(root) == DRIVE_CDROM)
461 {
462 drive = root[0];
463 break;
464 }
465 }
466 if (!drive)
467 {
468 ret = MCIERR_CANNOT_LOAD_DRIVER; /* drvOpen should return this */
469 goto the_error;
470 }
471 }
472
473 wmcda->wNotifyDeviceID = dwDeviceID;
474 wmcda->dwTimeFormat = MCI_FORMAT_MSF;
475
476 /* now, open the handle */
477 root[0] = root[1] = '\\'; root[2] = '.'; root[3] = '\\'; root[4] = drive; root[5] = ':'; root[6] = '\0';
478 wmcda->handle = CreateFileW(root, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
479 if (wmcda->handle == INVALID_HANDLE_VALUE)
480 {
481 ret = MCIERR_MUST_USE_SHAREABLE;
482 goto the_error;
483 }
484
485 if (dwFlags & MCI_NOTIFY) {
486 mciDriverNotify(HWND_32(LOWORD(lpOpenParms->dwCallback)),
487 dwDeviceID, MCI_NOTIFY_SUCCESSFUL);
488 }
489 return 0;
490
491 the_error:
492 --wmcda->nUseCount;
493 return ret;
494 }
495
496 /**************************************************************************
497 * MCICDA_Close [internal]
498 */
499 static DWORD MCICDA_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
500 {
501 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
502
503 TRACE("(%04X, %08X, %p);\n", wDevID, dwParam, lpParms);
504
505 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
506
507 MCICDA_Stop(wDevID, MCI_WAIT, NULL);
508
509 if (--wmcda->nUseCount == 0) {
510 CloseHandle(wmcda->handle);
511 }
512 if ((dwParam & MCI_NOTIFY) && lpParms)
513 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
514 return 0;
515 }
516
517 /**************************************************************************
518 * MCICDA_GetDevCaps [internal]
519 */
520 static DWORD MCICDA_GetDevCaps(UINT wDevID, DWORD dwFlags,
521 LPMCI_GETDEVCAPS_PARMS lpParms)
522 {
523 WINE_MCICDAUDIO* wmcda = (WINE_MCICDAUDIO*)mciGetDriverData(wDevID);
524 DWORD ret = 0;
525
526 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
527
528 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
529 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
530
531 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
532 TRACE("MCI_GETDEVCAPS_ITEM dwItem=%08X;\n", lpParms->dwItem);
533
534 switch (lpParms->dwItem) {
535 case MCI_GETDEVCAPS_CAN_RECORD:
536 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
537 ret = MCI_RESOURCE_RETURNED;
538 break;
539 case MCI_GETDEVCAPS_HAS_AUDIO:
540 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
541 ret = MCI_RESOURCE_RETURNED;
542 break;
543 case MCI_GETDEVCAPS_HAS_VIDEO:
544 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
545 ret = MCI_RESOURCE_RETURNED;
546 break;
547 case MCI_GETDEVCAPS_DEVICE_TYPE:
548 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_CD_AUDIO, MCI_DEVTYPE_CD_AUDIO);
549 ret = MCI_RESOURCE_RETURNED;
550 break;
551 case MCI_GETDEVCAPS_USES_FILES:
552 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
553 ret = MCI_RESOURCE_RETURNED;
554 break;
555 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
556 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
557 ret = MCI_RESOURCE_RETURNED;
558 break;
559 case MCI_GETDEVCAPS_CAN_EJECT:
560 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
561 ret = MCI_RESOURCE_RETURNED;
562 break;
563 case MCI_GETDEVCAPS_CAN_PLAY:
564 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
565 ret = MCI_RESOURCE_RETURNED;
566 break;
567 case MCI_GETDEVCAPS_CAN_SAVE:
568 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
569 ret = MCI_RESOURCE_RETURNED;
570 break;
571 default:
572 WARN("Unsupported %x devCaps item\n", lpParms->dwItem);
573 return MCIERR_UNSUPPORTED_FUNCTION;
574 }
575 } else {
576 TRACE("No GetDevCaps-Item !\n");
577 return MCIERR_MISSING_PARAMETER;
578 }
579 TRACE("lpParms->dwReturn=%08X;\n", lpParms->dwReturn);
580 if (dwFlags & MCI_NOTIFY) {
581 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
582 }
583 return ret;
584 }
585
586 static DWORD CDROM_Audio_GetSerial(CDROM_TOC* toc)
587 {
588 DWORD serial = 0;
589 int i;
590 WORD wMagic;
591 DWORD dwStart, dwEnd;
592
593 /*
594 * wMagic collects the wFrames from track 1
595 * dwStart, dwEnd collect the beginning and end of the disc respectively, in
596 * frames.
597 * There it is collected for correcting the serial when there are less than
598 * 3 tracks.
599 */
600 wMagic = toc->TrackData[0].Address[3];
601 dwStart = FRAME_OF_TOC(*toc, toc->FirstTrack);
602
603 for (i = 0; i <= toc->LastTrack - toc->FirstTrack; i++) {
604 serial += (toc->TrackData[i].Address[1] << 16) |
605 (toc->TrackData[i].Address[2] << 8) | toc->TrackData[i].Address[3];
606 }
607 dwEnd = FRAME_OF_TOC(*toc, toc->LastTrack + 1);
608
609 if (toc->LastTrack - toc->FirstTrack + 1 < 3)
610 serial += wMagic + (dwEnd - dwStart);
611
612 return serial;
613 }
614
615
616 /**************************************************************************
617 * MCICDA_Info [internal]
618 */
619 static DWORD MCICDA_Info(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
620 {
621 LPCWSTR str = NULL;
622 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
623 DWORD ret = 0;
624 WCHAR buffer[16];
625
626 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
627
628 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
629 return MCIERR_NULL_PARAMETER_BLOCK;
630 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
631
632 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
633
634 if (dwFlags & MCI_INFO_PRODUCT) {
635 static const WCHAR wszAudioCd[] = {'W','i','n','e','\'','s',' ','a','u','d','i','o',' ','C','D',0};
636 str = wszAudioCd;
637 } else if (dwFlags & MCI_INFO_MEDIA_UPC) {
638 ret = MCIERR_NO_IDENTITY;
639 } else if (dwFlags & MCI_INFO_MEDIA_IDENTITY) {
640 DWORD res = 0;
641 CDROM_TOC toc;
642 DWORD br;
643 static const WCHAR wszLu[] = {'%','l','u',0};
644
645 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
646 &toc, sizeof(toc), &br, NULL)) {
647 return MCICDA_GetError(wmcda);
648 }
649
650 res = CDROM_Audio_GetSerial(&toc);
651 sprintfW(buffer, wszLu, res);
652 str = buffer;
653 } else {
654 WARN("Don't know this info command (%u)\n", dwFlags);
655 ret = MCIERR_MISSING_PARAMETER;
656 }
657 if (!ret) {
658 TRACE("=> %s\n", debugstr_w(str));
659 if (lpParms->dwRetSize) {
660 WCHAR zero = 0;
661 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
662 * to the number of characters written, excluding \0. */
663 lstrcpynW(lpParms->lpstrReturn, str ? str : &zero, lpParms->dwRetSize);
664 } else ret = MCIERR_PARAM_OVERFLOW;
665 }
666 if (MMSYSERR_NOERROR==ret && (dwFlags & MCI_NOTIFY))
667 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
668 return ret;
669 }
670
671 /**************************************************************************
672 * MCICDA_Status [internal]
673 */
674 static DWORD MCICDA_Status(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
675 {
676 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
677 DWORD ret = 0;
678 CDROM_SUB_Q_DATA_FORMAT fmt;
679 SUB_Q_CHANNEL_DATA data;
680 CDROM_TOC toc;
681 DWORD br;
682
683 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
684
685 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
686 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
687
688 if (dwFlags & MCI_STATUS_ITEM) {
689 TRACE("dwItem = %x\n", lpParms->dwItem);
690 switch (lpParms->dwItem) {
691 case MCI_STATUS_CURRENT_TRACK:
692 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
693 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
694 &data, sizeof(data), &br, NULL))
695 {
696 return MCICDA_GetError(wmcda);
697 /* alt. data.CurrentPosition.TrackNumber = 1; -- what native yields */
698 }
699 lpParms->dwReturn = data.CurrentPosition.TrackNumber;
700 TRACE("CURRENT_TRACK=%lu\n", lpParms->dwReturn);
701 break;
702 case MCI_STATUS_LENGTH:
703 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
704 &toc, sizeof(toc), &br, NULL)) {
705 WARN("error reading TOC !\n");
706 return MCICDA_GetError(wmcda);
707 }
708 if (dwFlags & MCI_TRACK) {
709 TRACE("MCI_TRACK #%u LENGTH=??? !\n", lpParms->dwTrack);
710 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
711 return MCIERR_OUTOFRANGE;
712 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack + 1) -
713 FRAME_OF_TOC(toc, lpParms->dwTrack);
714 /* Windows returns one frame less than the total track length for the
715 last track on the CD. See CDDB HOWTO. Verified on Win95OSR2. */
716 if (lpParms->dwTrack == toc.LastTrack)
717 lpParms->dwReturn--;
718 } else {
719 /* Sum of the lengths of all of the tracks. Inherits the
720 'off by one frame' behavior from the length of the last track.
721 See above comment. */
722 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.LastTrack + 1) -
723 FRAME_OF_TOC(toc, toc.FirstTrack) - 1;
724 }
725 lpParms->dwReturn = MCICDA_CalcTime(wmcda,
726 (wmcda->dwTimeFormat == MCI_FORMAT_TMSF)
727 ? MCI_FORMAT_MSF : wmcda->dwTimeFormat,
728 lpParms->dwReturn,
729 &ret);
730 TRACE("LENGTH=%lu\n", lpParms->dwReturn);
731 break;
732 case MCI_STATUS_MODE:
733 lpParms->dwReturn = MCICDA_GetStatus(wmcda);
734 TRACE("MCI_STATUS_MODE=%08lX\n", lpParms->dwReturn);
735 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn, lpParms->dwReturn);
736 ret = MCI_RESOURCE_RETURNED;
737 break;
738 case MCI_STATUS_MEDIA_PRESENT:
739 lpParms->dwReturn = (MCICDA_GetStatus(wmcda) == MCI_MODE_OPEN) ?
740 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
741 TRACE("MCI_STATUS_MEDIA_PRESENT =%c!\n", LOWORD(lpParms->dwReturn) ? 'Y' : 'N');
742 ret = MCI_RESOURCE_RETURNED;
743 break;
744 case MCI_STATUS_NUMBER_OF_TRACKS:
745 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
746 &toc, sizeof(toc), &br, NULL)) {
747 WARN("error reading TOC !\n");
748 return MCICDA_GetError(wmcda);
749 }
750 lpParms->dwReturn = toc.LastTrack - toc.FirstTrack + 1;
751 TRACE("MCI_STATUS_NUMBER_OF_TRACKS = %lu\n", lpParms->dwReturn);
752 if (lpParms->dwReturn == (WORD)-1)
753 return MCICDA_GetError(wmcda);
754 break;
755 case MCI_STATUS_POSITION:
756 switch (dwFlags & (MCI_STATUS_START | MCI_TRACK)) {
757 case MCI_STATUS_START:
758 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
759 &toc, sizeof(toc), &br, NULL)) {
760 WARN("error reading TOC !\n");
761 return MCICDA_GetError(wmcda);
762 }
763 lpParms->dwReturn = FRAME_OF_TOC(toc, toc.FirstTrack);
764 TRACE("get MCI_STATUS_START !\n");
765 break;
766 case MCI_TRACK:
767 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
768 &toc, sizeof(toc), &br, NULL)) {
769 WARN("error reading TOC !\n");
770 return MCICDA_GetError(wmcda);
771 }
772 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
773 return MCIERR_OUTOFRANGE;
774 lpParms->dwReturn = FRAME_OF_TOC(toc, lpParms->dwTrack);
775 TRACE("get MCI_TRACK #%u !\n", lpParms->dwTrack);
776 break;
777 case 0:
778 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
779 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
780 &data, sizeof(data), &br, NULL)) {
781 return MCICDA_GetError(wmcda);
782 }
783 lpParms->dwReturn = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
784 break;
785 default:
786 return MCIERR_FLAGS_NOT_COMPATIBLE;
787 }
788 lpParms->dwReturn = MCICDA_CalcTime(wmcda, wmcda->dwTimeFormat, lpParms->dwReturn, &ret);
789 TRACE("MCI_STATUS_POSITION=%08lX\n", lpParms->dwReturn);
790 break;
791 case MCI_STATUS_READY:
792 TRACE("MCI_STATUS_READY !\n");
793 switch (MCICDA_GetStatus(wmcda))
794 {
795 case MCI_MODE_NOT_READY:
796 case MCI_MODE_OPEN:
797 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
798 break;
799 default:
800 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
801 break;
802 }
803 TRACE("MCI_STATUS_READY=%u!\n", LOWORD(lpParms->dwReturn));
804 ret = MCI_RESOURCE_RETURNED;
805 break;
806 case MCI_STATUS_TIME_FORMAT:
807 lpParms->dwReturn = MAKEMCIRESOURCE(wmcda->dwTimeFormat, MCI_FORMAT_RETURN_BASE + wmcda->dwTimeFormat);
808 TRACE("MCI_STATUS_TIME_FORMAT=%08x!\n", LOWORD(lpParms->dwReturn));
809 ret = MCI_RESOURCE_RETURNED;
810 break;
811 case 4001: /* FIXME: for bogus FullCD */
812 case MCI_CDA_STATUS_TYPE_TRACK:
813 if (!(dwFlags & MCI_TRACK))
814 ret = MCIERR_MISSING_PARAMETER;
815 else {
816 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
817 &toc, sizeof(toc), &br, NULL)) {
818 WARN("error reading TOC !\n");
819 return MCICDA_GetError(wmcda);
820 }
821 if (lpParms->dwTrack < toc.FirstTrack || lpParms->dwTrack > toc.LastTrack)
822 ret = MCIERR_OUTOFRANGE;
823 else
824 lpParms->dwReturn = (toc.TrackData[lpParms->dwTrack - toc.FirstTrack].Control & 0x04) ?
825 MCI_CDA_TRACK_OTHER : MCI_CDA_TRACK_AUDIO;
826 /* FIXME: MAKEMCIRESOURCE "audio" | "other", localised */
827 }
828 TRACE("MCI_CDA_STATUS_TYPE_TRACK[%d]=%ld\n", lpParms->dwTrack, lpParms->dwReturn);
829 break;
830 default:
831 FIXME("unknown command %08X !\n", lpParms->dwItem);
832 return MCIERR_UNSUPPORTED_FUNCTION;
833 }
834 } else return MCIERR_MISSING_PARAMETER;
835 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
836 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
837 return ret;
838 }
839
840 /**************************************************************************
841 * MCICDA_SkipDataTracks [internal]
842 */
843 static DWORD MCICDA_SkipDataTracks(WINE_MCICDAUDIO* wmcda,DWORD *frame)
844 {
845 int i;
846 DWORD br;
847 CDROM_TOC toc;
848 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
849 &toc, sizeof(toc), &br, NULL)) {
850 WARN("error reading TOC !\n");
851 return MCICDA_GetError(wmcda);
852 }
853 if (*frame < FRAME_OF_TOC(toc,toc.FirstTrack) ||
854 *frame >= FRAME_OF_TOC(toc,toc.LastTrack+1)) /* lead-out */
855 return MCIERR_OUTOFRANGE;
856 for(i=toc.LastTrack+1;i>toc.FirstTrack;i--)
857 if ( FRAME_OF_TOC(toc, i) <= *frame ) break;
858 /* i points to last track whose start address is not greater than frame.
859 * Now skip non-audio tracks */
860 for(;i<=toc.LastTrack;i++)
861 if ( ! (toc.TrackData[i-toc.FirstTrack].Control & 4) )
862 break;
863 /* The frame will be an address in the next audio track or
864 * address of lead-out. */
865 if ( FRAME_OF_TOC(toc, i) > *frame )
866 *frame = FRAME_OF_TOC(toc, i);
867 /* Lead-out is an invalid seek position (on Linux as well). */
868 if (*frame == FRAME_OF_TOC(toc,toc.LastTrack+1))
869 (*frame)--;
870 return 0;
871 }
872
873 /**************************************************************************
874 * MCICDA_Play [internal]
875 */
876 static DWORD MCICDA_Play(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
877 {
878 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
879 DWORD ret = 0, start, end;
880 HANDLE oldcb;
881 DWORD br;
882 CDROM_PLAY_AUDIO_MSF play;
883 CDROM_SUB_Q_DATA_FORMAT fmt;
884 SUB_Q_CHANNEL_DATA data;
885 CDROM_TOC toc;
886
887 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
888
889 if (lpParms == NULL)
890 return MCIERR_NULL_PARAMETER_BLOCK;
891
892 if (wmcda == NULL)
893 return MCIERR_INVALID_DEVICE_ID;
894
895 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
896 &toc, sizeof(toc), &br, NULL)) {
897 WARN("error reading TOC !\n");
898 return MCICDA_GetError(wmcda);
899 }
900
901 if (dwFlags & MCI_FROM) {
902 start = MCICDA_CalcFrame(wmcda, lpParms->dwFrom);
903 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
904 return ret;
905 TRACE("MCI_FROM=%08X -> %u\n", lpParms->dwFrom, start);
906 } else {
907 fmt.Format = IOCTL_CDROM_CURRENT_POSITION;
908 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_Q_CHANNEL, &fmt, sizeof(fmt),
909 &data, sizeof(data), &br, NULL)) {
910 return MCICDA_GetError(wmcda);
911 }
912 start = FRAME_OF_ADDR(data.CurrentPosition.AbsoluteAddress);
913 if ( (ret=MCICDA_SkipDataTracks(wmcda, &start)) )
914 return ret;
915 }
916 if (dwFlags & MCI_TO) {
917 end = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
918 if ( (ret=MCICDA_SkipDataTracks(wmcda, &end)) )
919 return ret;
920 TRACE("MCI_TO=%08X -> %u\n", lpParms->dwTo, end);
921 } else {
922 end = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
923 }
924 if (end < start) return MCIERR_OUTOFRANGE;
925 TRACE("Playing from %u to %u\n", start, end);
926
927 oldcb = InterlockedExchangePointer(&wmcda->hCallback,
928 (dwFlags & MCI_NOTIFY) ? HWND_32(LOWORD(lpParms->dwCallback)) : NULL);
929 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
930
931 if (start == end || start == FRAME_OF_TOC(toc,toc.LastTrack+1)-1) {
932 if (dwFlags & MCI_NOTIFY) {
933 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
934 if (oldcb) mciDriverNotify(oldcb, wDevID, MCI_NOTIFY_SUCCESSFUL);
935 }
936 return MMSYSERR_NOERROR;
937 }
938
939 if (wmcda->hThread != 0) {
940 SetEvent(wmcda->stopEvent);
941 WaitForSingleObject(wmcda->hThread, INFINITE);
942
943 CloseHandle(wmcda->hThread);
944 wmcda->hThread = 0;
945 CloseHandle(wmcda->stopEvent);
946 wmcda->stopEvent = 0;
947
948 IDirectSoundBuffer_Stop(wmcda->dsBuf);
949 IDirectSoundBuffer_Release(wmcda->dsBuf);
950 wmcda->dsBuf = NULL;
951 IDirectSound_Release(wmcda->dsObj);
952 wmcda->dsObj = NULL;
953 }
954
955 if (pDirectSoundCreate) {
956 WAVEFORMATEX format;
957 DSBUFFERDESC desc;
958 DWORD lockLen;
959 void *cdData;
960 HRESULT hr;
961
962 hr = pDirectSoundCreate(NULL, &wmcda->dsObj, NULL);
963 if (SUCCEEDED(hr)) {
964 IDirectSound_SetCooperativeLevel(wmcda->dsObj, GetDesktopWindow(), DSSCL_PRIORITY);
965
966 /* The "raw" frame is relative to the start of the first track */
967 wmcda->start = start - FRAME_OF_TOC(toc, toc.FirstTrack);
968 wmcda->end = end - FRAME_OF_TOC(toc, toc.FirstTrack);
969
970 memset(&format, 0, sizeof(format));
971 format.wFormatTag = WAVE_FORMAT_PCM;
972 format.nChannels = 2;
973 format.nSamplesPerSec = 44100;
974 format.wBitsPerSample = 16;
975 format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8;
976 format.nAvgBytesPerSec = format.nSamplesPerSec * format.nBlockAlign;
977 format.cbSize = 0;
978
979 memset(&desc, 0, sizeof(desc));
980 desc.dwSize = sizeof(desc);
981 desc.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_GLOBALFOCUS;
982 desc.dwBufferBytes = (CDDA_FRAG_SIZE - (CDDA_FRAG_SIZE%RAW_SECTOR_SIZE)) * CDDA_FRAG_COUNT;
983 desc.lpwfxFormat = &format;
984
985 hr = IDirectSound_CreateSoundBuffer(wmcda->dsObj, &desc, &wmcda->dsBuf, NULL);
986 }
987 if (SUCCEEDED(hr)) {
988 hr = IDirectSoundBuffer_Lock(wmcda->dsBuf, 0, 0, &cdData, &lockLen,
989 NULL, NULL, DSBLOCK_ENTIREBUFFER);
990 }
991 if (SUCCEEDED(hr)) {
992 RAW_READ_INFO rdInfo;
993 int readok;
994
995 rdInfo.DiskOffset.QuadPart = wmcda->start<<11;
996 rdInfo.SectorCount = min(desc.dwBufferBytes/RAW_SECTOR_SIZE,
997 wmcda->end-wmcda->start);
998 rdInfo.TrackMode = CDDA;
999
1000 readok = DeviceIoControl(wmcda->handle, IOCTL_CDROM_RAW_READ,
1001 &rdInfo, sizeof(rdInfo), cdData, lockLen,
1002 &br, NULL);
1003 IDirectSoundBuffer_Unlock(wmcda->dsBuf, cdData, lockLen, NULL, 0);
1004
1005 if (readok) {
1006 wmcda->start += rdInfo.SectorCount;
1007 wmcda->stopEvent = CreateEventA(NULL, TRUE, FALSE, NULL);
1008 }
1009 if (wmcda->stopEvent != 0)
1010 wmcda->hThread = CreateThread(NULL, 0, MCICDA_playLoop, wmcda, 0, &br);
1011 if (wmcda->hThread != 0) {
1012 hr = IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING);
1013 if (SUCCEEDED(hr)) {
1014 /* FIXME: implement MCI_WAIT and send notification only in that case */
1015 if (0) {
1016 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1017 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID,
1018 FAILED(hr) ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1019 }
1020 return ret;
1021 }
1022
1023 SetEvent(wmcda->stopEvent);
1024 WaitForSingleObject(wmcda->hThread, INFINITE);
1025 CloseHandle(wmcda->hThread);
1026 wmcda->hThread = 0;
1027 }
1028 }
1029
1030 if (wmcda->stopEvent != 0) {
1031 CloseHandle(wmcda->stopEvent);
1032 wmcda->stopEvent = 0;
1033 }
1034 if (wmcda->dsBuf) {
1035 IDirectSoundBuffer_Release(wmcda->dsBuf);
1036 wmcda->dsBuf = NULL;
1037 }
1038 if (wmcda->dsObj) {
1039 IDirectSound_Release(wmcda->dsObj);
1040 wmcda->dsObj = NULL;
1041 }
1042 }
1043
1044 play.StartingM = start / CDFRAMES_PERMIN;
1045 play.StartingS = (start / CDFRAMES_PERSEC) % 60;
1046 play.StartingF = start % CDFRAMES_PERSEC;
1047 play.EndingM = end / CDFRAMES_PERMIN;
1048 play.EndingS = (end / CDFRAMES_PERSEC) % 60;
1049 play.EndingF = end % CDFRAMES_PERSEC;
1050 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PLAY_AUDIO_MSF, &play, sizeof(play),
1051 NULL, 0, &br, NULL)) {
1052 wmcda->hCallback = NULL;
1053 ret = MCIERR_HARDWARE;
1054 }
1055 /* The independent CD player has no means to signal MCI_NOTIFY when it's done.
1056 * Native sends a notification with MCI_WAIT only. */
1057 return ret;
1058 }
1059
1060 /**************************************************************************
1061 * MCICDA_Stop [internal]
1062 */
1063 static DWORD MCICDA_Stop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1064 {
1065 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1066 HANDLE oldcb;
1067 DWORD br;
1068
1069 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1070
1071 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1072
1073 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1074 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1075
1076 if (wmcda->hThread != 0) {
1077 SetEvent(wmcda->stopEvent);
1078 WaitForSingleObject(wmcda->hThread, INFINITE);
1079
1080 CloseHandle(wmcda->hThread);
1081 wmcda->hThread = 0;
1082 CloseHandle(wmcda->stopEvent);
1083 wmcda->stopEvent = 0;
1084
1085 IDirectSoundBuffer_Release(wmcda->dsBuf);
1086 wmcda->dsBuf = NULL;
1087 IDirectSound_Release(wmcda->dsObj);
1088 wmcda->dsObj = NULL;
1089 }
1090 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_STOP_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1091 return MCIERR_HARDWARE;
1092
1093 if ((dwFlags & MCI_NOTIFY) && lpParms)
1094 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1095 return 0;
1096 }
1097
1098 /**************************************************************************
1099 * MCICDA_Pause [internal]
1100 */
1101 static DWORD MCICDA_Pause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1102 {
1103 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1104 HANDLE oldcb;
1105 DWORD br;
1106
1107 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1108
1109 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1110
1111 oldcb = InterlockedExchangePointer(&wmcda->hCallback, NULL);
1112 if (oldcb) mciDriverNotify(oldcb, wmcda->wNotifyDeviceID, MCI_NOTIFY_ABORTED);
1113
1114 if (wmcda->hThread != 0) {
1115 /* Don't bother calling stop if the playLoop thread has already stopped */
1116 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1117 FAILED(IDirectSoundBuffer_Stop(wmcda->dsBuf)))
1118 return MCIERR_HARDWARE;
1119 }
1120 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_PAUSE_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1121 return MCIERR_HARDWARE;
1122
1123 if ((dwFlags & MCI_NOTIFY) && lpParms)
1124 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1125 return 0;
1126 }
1127
1128 /**************************************************************************
1129 * MCICDA_Resume [internal]
1130 */
1131 static DWORD MCICDA_Resume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1132 {
1133 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1134 DWORD br;
1135
1136 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1137
1138 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1139
1140 if (wmcda->hThread != 0) {
1141 /* Don't restart if the playLoop thread has already stopped */
1142 if(WaitForSingleObject(wmcda->stopEvent, 0) != WAIT_OBJECT_0 &&
1143 FAILED(IDirectSoundBuffer_Play(wmcda->dsBuf, 0, 0, DSBPLAY_LOOPING)))
1144 return MCIERR_HARDWARE;
1145 }
1146 else if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_RESUME_AUDIO, NULL, 0, NULL, 0, &br, NULL))
1147 return MCIERR_HARDWARE;
1148
1149 if ((dwFlags & MCI_NOTIFY) && lpParms)
1150 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1151 return 0;
1152 }
1153
1154 /**************************************************************************
1155 * MCICDA_Seek [internal]
1156 */
1157 static DWORD MCICDA_Seek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1158 {
1159 DWORD at;
1160 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1161 CDROM_SEEK_AUDIO_MSF seek;
1162 DWORD br, position, ret;
1163 CDROM_TOC toc;
1164
1165 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1166
1167 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1168 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1169
1170 position = dwFlags & (MCI_SEEK_TO_START|MCI_SEEK_TO_END|MCI_TO);
1171 if (!position) return MCIERR_MISSING_PARAMETER;
1172 if (position&(position-1)) return MCIERR_FLAGS_NOT_COMPATIBLE;
1173
1174 /* Stop sends MCI_NOTIFY_ABORTED when needed.
1175 * Tests show that native first sends ABORTED and reads the TOC,
1176 * then only checks the position flags, then stops and seeks. */
1177 MCICDA_Stop(wDevID, MCI_WAIT, 0);
1178
1179 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_READ_TOC, NULL, 0,
1180 &toc, sizeof(toc), &br, NULL)) {
1181 WARN("error reading TOC !\n");
1182 return MCICDA_GetError(wmcda);
1183 }
1184 switch (position) {
1185 case MCI_SEEK_TO_START:
1186 TRACE("Seeking to start\n");
1187 at = FRAME_OF_TOC(toc,toc.FirstTrack);
1188 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1189 return ret;
1190 break;
1191 case MCI_SEEK_TO_END:
1192 TRACE("Seeking to end\n");
1193 /* End is prior to lead-out
1194 * yet Win9X seeks to even one frame less than that. */
1195 at = FRAME_OF_TOC(toc, toc.LastTrack + 1) - 1;
1196 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1197 return ret;
1198 break;
1199 case MCI_TO:
1200 TRACE("Seeking to %u\n", lpParms->dwTo);
1201 at = MCICDA_CalcFrame(wmcda, lpParms->dwTo);
1202 if ( (ret=MCICDA_SkipDataTracks(wmcda, &at)) )
1203 return ret;
1204 break;
1205 default:
1206 return MCIERR_FLAGS_NOT_COMPATIBLE;
1207 }
1208
1209 {
1210 seek.M = at / CDFRAMES_PERMIN;
1211 seek.S = (at / CDFRAMES_PERSEC) % 60;
1212 seek.F = at % CDFRAMES_PERSEC;
1213 if (!DeviceIoControl(wmcda->handle, IOCTL_CDROM_SEEK_AUDIO_MSF, &seek, sizeof(seek),
1214 NULL, 0, &br, NULL))
1215 return MCIERR_HARDWARE;
1216 }
1217
1218 if (dwFlags & MCI_NOTIFY)
1219 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1220 return 0;
1221 }
1222
1223 /**************************************************************************
1224 * MCICDA_SetDoor [internal]
1225 */
1226 static DWORD MCICDA_SetDoor(UINT wDevID, BOOL open)
1227 {
1228 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1229 DWORD br;
1230
1231 TRACE("(%04x, %s) !\n", wDevID, (open) ? "OPEN" : "CLOSE");
1232
1233 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1234
1235 if (!DeviceIoControl(wmcda->handle,
1236 (open) ? IOCTL_STORAGE_EJECT_MEDIA : IOCTL_STORAGE_LOAD_MEDIA,
1237 NULL, 0, NULL, 0, &br, NULL))
1238 return MCIERR_HARDWARE;
1239
1240 return 0;
1241 }
1242
1243 /**************************************************************************
1244 * MCICDA_Set [internal]
1245 */
1246 static DWORD MCICDA_Set(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1247 {
1248 WINE_MCICDAUDIO* wmcda = MCICDA_GetOpenDrv(wDevID);
1249
1250 TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1251
1252 if (wmcda == NULL) return MCIERR_INVALID_DEVICE_ID;
1253
1254 if (dwFlags & MCI_SET_DOOR_OPEN) {
1255 MCICDA_SetDoor(wDevID, TRUE);
1256 }
1257 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1258 MCICDA_SetDoor(wDevID, FALSE);
1259 }
1260
1261 /* only functions which require valid lpParms below this line ! */
1262 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1263 /*
1264 TRACE("dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
1265 */
1266 if (dwFlags & MCI_SET_TIME_FORMAT) {
1267 switch (lpParms->dwTimeFormat) {
1268 case MCI_FORMAT_MILLISECONDS:
1269 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1270 break;
1271 case MCI_FORMAT_MSF:
1272 TRACE("MCI_FORMAT_MSF !\n");
1273 break;
1274 case MCI_FORMAT_TMSF:
1275 TRACE("MCI_FORMAT_TMSF !\n");
1276 break;
1277 default:
1278 return MCIERR_BAD_TIME_FORMAT;
1279 }
1280 wmcda->dwTimeFormat = lpParms->dwTimeFormat;
1281 }
1282 if (dwFlags & MCI_SET_AUDIO) /* one xp machine ignored it */
1283 TRACE("SET_AUDIO %X %x\n", dwFlags, lpParms->dwAudio);
1284
1285 if (dwFlags & MCI_NOTIFY)
1286 MCICDA_Notify(lpParms->dwCallback, wmcda, MCI_NOTIFY_SUCCESSFUL);
1287 return 0;
1288 }
1289
1290 /**************************************************************************
1291 * DriverProc (MCICDA.@)
1292 */
1293 LRESULT CALLBACK MCICDA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1294 LPARAM dwParam1, LPARAM dwParam2)
1295 {
1296 switch(wMsg) {
1297 case DRV_LOAD: return 1;
1298 case DRV_FREE: return 1;
1299 case DRV_OPEN: return MCICDA_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1300 case DRV_CLOSE: return MCICDA_drvClose(dwDevID);
1301 case DRV_ENABLE: return 1;
1302 case DRV_DISABLE: return 1;
1303 case DRV_QUERYCONFIGURE: return 1;
1304 case DRV_CONFIGURE: MessageBoxA(0, "MCI audio CD driver !", "Wine Driver", MB_OK); return 1;
1305 case DRV_INSTALL: return DRVCNF_RESTART;
1306 case DRV_REMOVE: return DRVCNF_RESTART;
1307 }
1308
1309 if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;
1310
1311 switch (wMsg) {
1312 case MCI_OPEN_DRIVER: return MCICDA_Open(dwDevID, dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1313 case MCI_CLOSE_DRIVER: return MCICDA_Close(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1314 case MCI_GETDEVCAPS: return MCICDA_GetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1315 case MCI_INFO: return MCICDA_Info(dwDevID, dwParam1, (LPMCI_INFO_PARMSW)dwParam2);
1316 case MCI_STATUS: return MCICDA_Status(dwDevID, dwParam1, (LPMCI_STATUS_PARMS)dwParam2);
1317 case MCI_SET: return MCICDA_Set(dwDevID, dwParam1, (LPMCI_SET_PARMS)dwParam2);
1318 case MCI_PLAY: return MCICDA_Play(dwDevID, dwParam1, (LPMCI_PLAY_PARMS)dwParam2);
1319 case MCI_STOP: return MCICDA_Stop(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1320 case MCI_PAUSE: return MCICDA_Pause(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1321 case MCI_RESUME: return MCICDA_Resume(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1322 case MCI_SEEK: return MCICDA_Seek(dwDevID, dwParam1, (LPMCI_SEEK_PARMS)dwParam2);
1323 /* commands that should report an error as they are not supported in
1324 * the native version */
1325 case MCI_RECORD:
1326 case MCI_LOAD:
1327 case MCI_SAVE:
1328 return MCIERR_UNSUPPORTED_FUNCTION;
1329 case MCI_BREAK:
1330 case MCI_FREEZE:
1331 case MCI_PUT:
1332 case MCI_REALIZE:
1333 case MCI_UNFREEZE:
1334 case MCI_UPDATE:
1335 case MCI_WHERE:
1336 case MCI_STEP:
1337 case MCI_SPIN:
1338 case MCI_ESCAPE:
1339 case MCI_COPY:
1340 case MCI_CUT:
1341 case MCI_DELETE:
1342 case MCI_PASTE:
1343 case MCI_WINDOW:
1344 TRACE("Unsupported command [0x%x]\n", wMsg);
1345 break;
1346 case MCI_OPEN:
1347 case MCI_CLOSE:
1348 ERR("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1349 break;
1350 default:
1351 TRACE("Sending msg [0x%x] to default driver proc\n", wMsg);
1352 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1353 }
1354 return MCIERR_UNRECOGNIZED_COMMAND;
1355 }
1356
1357 /*-----------------------------------------------------------------------*/