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