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