4 * Copyright 1993 Martin Ayotte
5 * 1998-2003,2009 Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #define NONAMELESSUNION
27 #define NONAMELESSSTRUCT
35 #include "wine/winuser16.h"
39 #include "wine/debug.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(mmsys);
43 /**************************************************************************
44 * MCI_MessageToString [internal]
46 static const char* MCI_MessageToString(UINT wMsg)
48 static char buffer[100];
50 #define CASE(s) case (s): return #s
60 CASE(DRV_QUERYCONFIGURE);
63 CASE(DRV_EXITSESSION);
64 CASE(DRV_EXITAPPLICATION);
68 CASE(MCI_CLOSE_DRIVER);
81 CASE(MCI_OPEN_DRIVER);
99 /* constants for digital video */
113 sprintf(buffer, "MCI_<<%04X>>", wMsg);
118 static LPWSTR MCI_strdupAtoW( LPCSTR str )
123 if (!str) return NULL;
124 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
125 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
126 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
130 /**************************************************************************
131 * MCI_MapMsg16To32W [internal]
133 static MMSYSTEM_MapType MCI_MapMsg16To32W(WORD wMsg, DWORD dwFlags, DWORD_PTR* lParam)
136 return MMSYSTEM_MAP_OK;
137 /* FIXME: to add also (with seg/linear modifications to do):
138 * MCI_LIST, MCI_LOAD, MCI_QUALITY, MCI_RESERVE, MCI_RESTORE, MCI_SAVE
139 * MCI_SETAUDIO, MCI_SETTUNER, MCI_SETVIDEO
142 /* case MCI_CAPTURE */
144 case MCI_CLOSE_DRIVER:
152 /* case MCI_INDEX: */
154 /* case MCI_MONITOR: */
164 /* case MCI_SETTIMECODE:*/
165 /* case MCI_SIGNAL:*/
167 case MCI_STATUS: /* FIXME: is wrong for digital video */
174 *lParam = (DWORD)MapSL(*lParam);
175 return MMSYSTEM_MAP_OK;
177 /* in fact, I would also need the dwFlags... to see
178 * which members of lParam are effectively used
180 *lParam = (DWORD)MapSL(*lParam);
181 FIXME("Current mapping may be wrong\n");
185 LPMCI_BREAK_PARMS mbp32 = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_BREAK_PARMS));
186 LPMCI_BREAK_PARMS16 mbp16 = MapSL(*lParam);
189 mbp32->dwCallback = mbp16->dwCallback;
190 mbp32->nVirtKey = mbp16->nVirtKey;
191 mbp32->hwndBreak = HWND_32(mbp16->hwndBreak);
193 return MMSYSTEM_MAP_NOMEM;
195 *lParam = (DWORD)mbp32;
197 return MMSYSTEM_MAP_OKMEM;
200 LPMCI_VD_ESCAPE_PARMSW mvep32w = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_VD_ESCAPE_PARMSW));
201 LPMCI_VD_ESCAPE_PARMS16 mvep16 = MapSL(*lParam);
204 mvep32w->dwCallback = mvep16->dwCallback;
205 mvep32w->lpstrCommand = MCI_strdupAtoW(MapSL(mvep16->lpstrCommand));
207 return MMSYSTEM_MAP_NOMEM;
209 *lParam = (DWORD)mvep32w;
211 return MMSYSTEM_MAP_OKMEM;
214 LPMCI_INFO_PARMSW mip32w = HeapAlloc(GetProcessHeap(), 0, sizeof(LPMCI_OPEN_PARMS16) + sizeof(MCI_INFO_PARMSW));
215 LPMCI_INFO_PARMS16 mip16 = MapSL(*lParam);
217 /* FIXME this is wrong if device is of type
218 * MCI_DEVTYPE_DIGITAL_VIDEO, some members are not mapped
221 *(LPMCI_INFO_PARMS16*)(mip32w) = mip16;
222 mip32w = (LPMCI_INFO_PARMSW)((char*)mip32w + sizeof(LPMCI_INFO_PARMS16));
223 mip32w->dwCallback = mip16->dwCallback;
224 mip32w->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mip16->dwRetSize * sizeof(WCHAR));
225 mip32w->dwRetSize = mip16->dwRetSize * sizeof(WCHAR);
227 return MMSYSTEM_MAP_NOMEM;
229 *lParam = (DWORD)mip32w;
231 return MMSYSTEM_MAP_OKMEM;
233 case MCI_OPEN_DRIVER:
235 LPMCI_OPEN_PARMSW mop32w = HeapAlloc(GetProcessHeap(), 0, sizeof(LPMCI_OPEN_PARMS16) + sizeof(MCI_OPEN_PARMSW) + 2 * sizeof(DWORD));
236 LPMCI_OPEN_PARMS16 mop16 = MapSL(*lParam);
239 *(LPMCI_OPEN_PARMS16*)(mop32w) = mop16;
240 mop32w = (LPMCI_OPEN_PARMSW)((char*)mop32w + sizeof(LPMCI_OPEN_PARMS16));
241 mop32w->dwCallback = mop16->dwCallback;
242 mop32w->wDeviceID = mop16->wDeviceID;
243 if( ( dwFlags & ( MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID)) == MCI_OPEN_TYPE)
244 mop32w->lpstrDeviceType = MCI_strdupAtoW(MapSL(mop16->lpstrDeviceType));
246 mop32w->lpstrDeviceType = (LPWSTR) mop16->lpstrDeviceType;
247 if( ( dwFlags & ( MCI_OPEN_ELEMENT | MCI_OPEN_ELEMENT_ID)) == MCI_OPEN_ELEMENT)
248 mop32w->lpstrElementName = MCI_strdupAtoW(MapSL(mop16->lpstrElementName));
250 mop32w->lpstrElementName = (LPWSTR) mop16->lpstrElementName;
251 if( ( dwFlags & MCI_OPEN_ALIAS))
252 mop32w->lpstrAlias = MCI_strdupAtoW(MapSL(mop16->lpstrAlias));
254 mop32w->lpstrAlias = (LPWSTR) mop16->lpstrAlias;
255 /* copy extended information if any...
256 * FIXME: this may seg fault if initial structure does not contain them and
257 * the reads after msip16 fail under LDT limits...
258 * NOTE: this should be split in two. First pass, while calling MCI_OPEN, and
259 * should not take care of extended parameters, and should be used by MCI_Open
260 * to fetch uDevType. When, this is known, the mapping for sending the
261 * MCI_OPEN_DRIVER shall be done depending on uDevType.
263 memcpy(mop32w + 1, mop16 + 1, 2 * sizeof(DWORD));
265 return MMSYSTEM_MAP_NOMEM;
267 *lParam = (DWORD)mop32w;
269 return MMSYSTEM_MAP_OKMEM;
272 LPMCI_SYSINFO_PARMSW msip32w = HeapAlloc(GetProcessHeap(), 0, sizeof(LPMCI_OPEN_PARMS16) + sizeof(MCI_SYSINFO_PARMSW));
273 LPMCI_SYSINFO_PARMS16 msip16 = MapSL(*lParam);
276 *(LPMCI_SYSINFO_PARMS16*)(msip32w) = msip16;
277 msip32w = (LPMCI_SYSINFO_PARMSW)((char*)msip32w + sizeof(LPMCI_OPEN_PARMS16));
278 msip32w->dwCallback = msip16->dwCallback;
279 msip32w->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, msip16->dwRetSize * sizeof(WCHAR));
280 msip32w->dwRetSize = msip16->dwRetSize;
281 msip32w->dwNumber = msip16->dwNumber;
282 msip32w->wDeviceType = msip16->wDeviceType;
284 return MMSYSTEM_MAP_NOMEM;
286 *lParam = (DWORD)msip32w;
288 return MMSYSTEM_MAP_OKMEM;
291 LPMCI_SOUND_PARMSW mbp32 = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_SOUND_PARMSW));
292 LPMCI_SOUND_PARMS16 mbp16 = MapSL(*lParam);
295 mbp32->dwCallback = mbp16->dwCallback;
296 mbp32->lpstrSoundName = MCI_strdupAtoW(MapSL(mbp16->lpstrSoundName));
298 return MMSYSTEM_MAP_NOMEM;
300 *lParam = (DWORD)mbp32;
302 return MMSYSTEM_MAP_OKMEM;
310 case DRV_QUERYCONFIGURE:
313 case DRV_EXITSESSION:
314 case DRV_EXITAPPLICATION:
316 FIXME("This is a hack\n");
317 return MMSYSTEM_MAP_OK;
319 FIXME("Don't know how to map msg=%s\n", MCI_MessageToString(wMsg));
321 return MMSYSTEM_MAP_MSGERROR;
324 /**************************************************************************
325 * MCI_UnMapMsg16To32W [internal]
327 static MMSYSTEM_MapType MCI_UnMapMsg16To32W(WORD wMsg, DWORD dwFlags, DWORD_PTR lParam)
330 /* case MCI_CAPTURE */
332 case MCI_CLOSE_DRIVER:
340 /* case MCI_INDEX: */
342 /* case MCI_MONITOR: */
352 /* case MCI_SETTIMECODE:*/
353 /* case MCI_SIGNAL:*/
362 return MMSYSTEM_MAP_OK;
365 /* FIXME ?? see Map function */
366 return MMSYSTEM_MAP_OK;
369 HeapFree(GetProcessHeap(), 0, (LPVOID)lParam);
370 return MMSYSTEM_MAP_OK;
373 LPMCI_VD_ESCAPE_PARMSW mvep32W = (LPMCI_VD_ESCAPE_PARMSW)lParam;
374 HeapFree(GetProcessHeap(), 0, (LPVOID)mvep32W->lpstrCommand);
375 HeapFree(GetProcessHeap(), 0, (LPVOID)lParam);
377 return MMSYSTEM_MAP_OK;
380 LPMCI_INFO_PARMSW mip32w = (LPMCI_INFO_PARMSW)lParam;
381 LPMCI_INFO_PARMS16 mip16 = *(LPMCI_INFO_PARMS16*)((char*)mip32w - sizeof(LPMCI_INFO_PARMS16));
383 WideCharToMultiByte(CP_ACP, 0,
384 mip32w->lpstrReturn, mip32w->dwRetSize / sizeof(WCHAR),
385 MapSL(mip16->lpstrReturn), mip16->dwRetSize,
387 HeapFree(GetProcessHeap(), 0, mip32w->lpstrReturn);
388 HeapFree(GetProcessHeap(), 0, (LPVOID)lParam);
390 return MMSYSTEM_MAP_OK;
393 LPMCI_SYSINFO_PARMSW msip32w = (LPMCI_SYSINFO_PARMSW)lParam;
394 LPMCI_SYSINFO_PARMS16 msip16 = *(LPMCI_SYSINFO_PARMS16*)((char*)msip32w - sizeof(LPMCI_SYSINFO_PARMS16));
396 WideCharToMultiByte(CP_ACP, 0,
397 msip32w->lpstrReturn, msip32w->dwRetSize,
398 MapSL(msip16->lpstrReturn), msip16->dwRetSize,
400 HeapFree(GetProcessHeap(), 0, msip32w->lpstrReturn);
401 HeapFree(GetProcessHeap(), 0, (LPVOID)lParam);
403 return MMSYSTEM_MAP_OK;
406 LPMCI_SOUND_PARMSW msp32W = (LPMCI_SOUND_PARMSW)lParam;
407 HeapFree(GetProcessHeap(), 0, (LPVOID)msp32W->lpstrSoundName);
408 HeapFree(GetProcessHeap(), 0, (LPVOID)lParam);
410 return MMSYSTEM_MAP_OK;
412 case MCI_OPEN_DRIVER:
414 LPMCI_OPEN_PARMSW mop32w = (LPMCI_OPEN_PARMSW)lParam;
415 LPMCI_OPEN_PARMS16 mop16 = *(LPMCI_OPEN_PARMS16*)((char*)mop32w - sizeof(LPMCI_OPEN_PARMS16));
417 mop16->wDeviceID = mop32w->wDeviceID;
418 if( ( dwFlags & ( MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID)) == MCI_OPEN_TYPE)
419 HeapFree(GetProcessHeap(), 0, (LPWSTR)mop32w->lpstrDeviceType);
420 if( ( dwFlags & ( MCI_OPEN_ELEMENT | MCI_OPEN_ELEMENT_ID)) == MCI_OPEN_ELEMENT)
421 HeapFree(GetProcessHeap(), 0, (LPWSTR)mop32w->lpstrElementName);
422 if( ( dwFlags & MCI_OPEN_ALIAS))
423 HeapFree(GetProcessHeap(), 0, (LPWSTR)mop32w->lpstrAlias);
424 if (!HeapFree(GetProcessHeap(), 0, (LPVOID)(lParam - sizeof(LPMCI_OPEN_PARMS16))))
425 FIXME("bad free line=%d\n", __LINE__);
427 return MMSYSTEM_MAP_OK;
435 case DRV_QUERYCONFIGURE:
438 case DRV_EXITSESSION:
439 case DRV_EXITAPPLICATION:
441 FIXME("This is a hack\n");
442 return MMSYSTEM_MAP_OK;
444 FIXME("Map/Unmap internal error on msg=%s\n", MCI_MessageToString(wMsg));
446 return MMSYSTEM_MAP_MSGERROR;
449 /* ###################################################
451 * ###################################################
454 #include <pshpack1.h>
455 #define MCI_MAX_THUNKS 32
457 static struct mci_thunk
459 BYTE popl_eax; /* popl %eax (return address) */
460 BYTE pushl_func; /* pushl $pfn16 (16bit callback function) */
462 BYTE pushl_eax; /* pushl %eax */
463 BYTE jmp; /* ljmp MCI_Yield1632 */
470 static CRITICAL_SECTION mci_cs;
471 static CRITICAL_SECTION_DEBUG mci_critsect_debug =
474 { &mci_critsect_debug.ProcessLocksList, &mci_critsect_debug.ProcessLocksList },
475 0, 0, { (DWORD_PTR)(__FILE__ ": mmsystem_mci_cs") }
477 static CRITICAL_SECTION mci_cs = { &mci_critsect_debug, -1, 0, 0, 0, 0 };
479 static UINT MCI_Yield1632(DWORD pfn16, MCIDEVICEID id, DWORD yield_data)
486 PeekMessageW( &msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE );
490 /* 16 bit func, call it */
491 TRACE("Function (16 bit) !\n");
493 args[2] = (MCIDEVICEID16)id;
494 args[1] = HIWORD(yield_data);
495 args[0] = LOWORD(yield_data);
496 return WOWCallback16Ex(pfn16, WCB16_PASCAL, sizeof(args), args, NULL);
499 /******************************************************************
503 static struct mci_thunk* MCI_AddThunk(MCIDEVICEID id, YIELDPROC16 pfn16)
505 struct mci_thunk* thunk;
509 MCI_Thunks = VirtualAlloc(NULL, MCI_MAX_THUNKS * sizeof(*MCI_Thunks), MEM_COMMIT,
510 PAGE_EXECUTE_READWRITE);
511 if (!MCI_Thunks) return NULL;
512 for (thunk = MCI_Thunks; thunk < &MCI_Thunks[MCI_MAX_THUNKS]; thunk++)
514 thunk->popl_eax = 0x58; /* popl %eax */
515 thunk->pushl_func = 0x68; /* pushl $pfn16 */
517 thunk->pushl_eax = 0x50; /* pushl %eax */
518 thunk->jmp = 0xe9; /* jmp MCI_Yield1632 */
519 thunk->callback = (char *)MCI_Yield1632 - (char *)(&thunk->callback + 1);
523 for (thunk = MCI_Thunks; thunk < &MCI_Thunks[MCI_MAX_THUNKS]; thunk++)
525 if (thunk->yield16 == 0)
527 thunk->yield16 = pfn16;
532 FIXME("Out of mci-thunks. Bump MCI_MAX_THUNKS\n");
536 /******************************************************************
540 static struct mci_thunk* MCI_HasThunk(YIELDPROC pfn)
542 struct mci_thunk* thunk;
544 if (!MCI_Thunks) return NULL;
545 for (thunk = MCI_Thunks; thunk < &MCI_Thunks[MCI_MAX_THUNKS]; thunk++)
547 if ((YIELDPROC)thunk == pfn) return thunk;
552 /**************************************************************************
553 * mciSetYieldProc [MMSYSTEM.714]
555 BOOL16 WINAPI mciSetYieldProc16(UINT16 uDeviceID, YIELDPROC16 fpYieldProc, DWORD dwYieldData)
557 struct mci_thunk* thunk;
560 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
562 if (!(thunk = MCI_AddThunk(uDeviceID, fpYieldProc)))
564 ret = mciSetYieldProc(uDeviceID, (YIELDPROC)thunk, dwYieldData);
565 if (!ret) thunk->yield16 = NULL;
569 /**************************************************************************
570 * mciGetYieldProc [MMSYSTEM.716]
572 YIELDPROC16 WINAPI mciGetYieldProc16(UINT16 uDeviceID, DWORD* lpdwYieldData)
576 struct mci_thunk* thunk;
578 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
580 yield = mciGetYieldProc(uDeviceID, &data);
581 if (!yield || !(thunk = MCI_HasThunk(yield))) return NULL;
583 if (lpdwYieldData) *lpdwYieldData = data;
584 return thunk->yield16;
587 /**************************************************************************
588 * mciGetErrorString [MMSYSTEM.706]
590 BOOL16 WINAPI mciGetErrorString16(DWORD wError, LPSTR lpstrBuffer, UINT16 uLength)
592 return mciGetErrorStringA(wError, lpstrBuffer, uLength);
595 /**************************************************************************
596 * mciDriverNotify [MMSYSTEM.711]
598 BOOL16 WINAPI mciDriverNotify16(HWND16 hWndCallBack, UINT16 wDevID, UINT16 wStatus)
600 TRACE("(%04X, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
602 return PostMessageA(HWND_32(hWndCallBack), MM_MCINOTIFY, wStatus, wDevID);
605 /**************************************************************************
606 * mciGetDriverData [MMSYSTEM.708]
608 DWORD WINAPI mciGetDriverData16(UINT16 uDeviceID)
610 return mciGetDriverData(uDeviceID);
613 /**************************************************************************
614 * mciSetDriverData [MMSYSTEM.707]
616 BOOL16 WINAPI mciSetDriverData16(UINT16 uDeviceID, DWORD data)
618 return mciSetDriverData(uDeviceID, data);
621 /**************************************************************************
622 * mciSendCommand [MMSYSTEM.701]
624 DWORD WINAPI mciSendCommand16(UINT16 wDevID, UINT16 wMsg, DWORD dwParam1, DWORD p2)
628 DWORD_PTR dwParam2 = p2;
630 TRACE("(%04X, %u, %08X, %08lX)\n", wDevID, wMsg, dwParam1, dwParam2);
641 /* FIXME: this is suboptimal. If MCI driver is a 16bit one, we'll be
642 * doing 16=>32W, then 32W=>16 conversions.
643 * We could directly call the 16bit driver if we had the information.
648 MMSYSTEM_MapType res;
650 dwRet = MCIERR_INVALID_DEVICE_ID;
652 switch (res = MCI_MapMsg16To32W(wMsg, dwParam1, &dwParam2)) {
653 case MMSYSTEM_MAP_MSGERROR:
654 TRACE("Not handled yet (%u)\n", wMsg);
655 dwRet = MCIERR_DRIVER_INTERNAL;
657 case MMSYSTEM_MAP_NOMEM:
658 TRACE("Problem mapping msg=%u from 16 to 32a\n", wMsg);
659 dwRet = MCIERR_OUT_OF_MEMORY;
661 case MMSYSTEM_MAP_OK:
662 case MMSYSTEM_MAP_OKMEM:
663 dwRet = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
664 if (res == MMSYSTEM_MAP_OKMEM)
665 MCI_UnMapMsg16To32W(wMsg, dwParam1, dwParam2);
672 if (wDevID == MCI_ALL_DEVICE_ID) {
673 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
674 dwRet = MCIERR_CANNOT_USE_ALL;
676 dwRet = SendDriverMessage(hdrv, wMsg, dwParam1, dwParam2);
680 if (wMsg == MCI_CLOSE && dwRet == 0 && MCI_Thunks)
682 /* free yield thunks, if any */
684 for (i = 0; i < MCI_MAX_THUNKS; i++)
686 if (MCI_Thunks[i].id == wDevID)
687 MCI_Thunks[i].yield16 = NULL;
693 /**************************************************************************
694 * mciGetDeviceID [MMSYSTEM.703]
696 UINT16 WINAPI mciGetDeviceID16(LPCSTR lpstrName)
698 TRACE("(\"%s\")\n", lpstrName);
700 return mciGetDeviceIDA(lpstrName);
703 /**************************************************************************
704 * mciGetDeviceIDFromElementID [MMSYSTEM.715]
706 UINT16 WINAPI mciGetDeviceIDFromElementID16(DWORD dwElementID, LPCSTR lpstrType)
708 return mciGetDeviceIDFromElementIDA(dwElementID, lpstrType);
711 /**************************************************************************
712 * mciGetCreatorTask [MMSYSTEM.717]
714 HTASK16 WINAPI mciGetCreatorTask16(UINT16 uDeviceID)
716 return HTASK_16(mciGetCreatorTask(uDeviceID));
719 /**************************************************************************
720 * mciDriverYield [MMSYSTEM.710]
722 UINT16 WINAPI mciDriverYield16(UINT16 uDeviceID)
724 return mciDriverYield(uDeviceID);
727 /**************************************************************************
728 * mciSendString [MMSYSTEM.702]
730 DWORD WINAPI mciSendString16(LPCSTR lpstrCommand, LPSTR lpstrRet,
731 UINT16 uRetLen, HWND16 hwndCallback)
733 return mciSendStringA(lpstrCommand, lpstrRet, uRetLen, HWND_32(hwndCallback));
736 /**************************************************************************
737 * mciLoadCommandResource [MMSYSTEM.705]
739 UINT16 WINAPI mciLoadCommandResource16(HINSTANCE16 hInst, LPCSTR resname, UINT16 type)
741 TRACE("(%04x, %s, %x)!\n", hInst, resname, type);
742 return MCI_NO_COMMAND_TABLE;
745 /**************************************************************************
746 * mciFreeCommandResource [MMSYSTEM.713]
748 BOOL16 WINAPI mciFreeCommandResource16(UINT16 uTable)
750 TRACE("(%04x)!\n", uTable);
755 /**************************************************************************
756 * mciExecute [MMSYSTEM.712]
758 BOOL16 WINAPI mciExecute16(LPCSTR lpstrCommand)
760 return mciExecute(lpstrCommand);