kernel32: Fix off by one error.
[wine/wine-kai.git] / dlls / winmm / mci.c
blob5ca5c972c5510e5c99377c4beaf8f0bb5d312447
1 /*
2 * MCI internal functions
4 * Copyright 1998/1999 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* TODO:
22 * - implement WINMM (32bit) multitasking and use it in all MCI drivers
23 * instead of the home grown one
24 * - 16bit mmTaskXXX functions are currently broken because the 16
25 * loader does not support binary command lines => provide Wine's
26 * own mmtask.tsk not using binary command line.
27 * - correctly handle the MCI_ALL_DEVICE_ID in functions.
28 * - finish mapping 16 <=> 32 of MCI structures and commands
29 * - implement auto-open feature (ie, when a string command is issued
30 * for a not yet opened device, MCI automatically opens it)
31 * - use a default registry setting to replace the [mci] section in
32 * configuration file (layout of info in registry should be compatible
33 * with all Windows' version - which use different layouts of course)
34 * - implement automatic open
35 * + only works on string interface, on regular devices (don't work on all
36 * nor custom devices)
37 * - command table handling isn't thread safe
40 /* to be cross checked:
41 * - heapalloc for *sizeof(WCHAR) when needed
42 * - size of string in WCHAR or bytes? (#chars for MCI_INFO, #bytes for MCI_SYSINFO)
45 #include "config.h"
46 #include "wine/port.h"
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <string.h>
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "winreg.h"
57 #include "mmsystem.h"
58 #include "winuser.h"
59 #include "winnls.h"
60 #include "winreg.h"
61 #include "wownt32.h"
63 #include "digitalv.h"
64 #include "winemm.h"
66 #include "wine/debug.h"
67 #include "wine/unicode.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(mci);
71 WINMM_MapType (*pFnMciMapMsg16To32W) (WORD,WORD,DWORD,DWORD_PTR*) = NULL;
72 WINMM_MapType (*pFnMciUnMapMsg16To32W)(WORD,WORD,DWORD,DWORD_PTR) = NULL;
73 WINMM_MapType (*pFnMciMapMsg32WTo16) (WORD,WORD,DWORD,DWORD_PTR*) = NULL;
74 WINMM_MapType (*pFnMciUnMapMsg32WTo16)(WORD,WORD,DWORD,DWORD_PTR) = NULL;
76 /* First MCI valid device ID (0 means error) */
77 #define MCI_MAGIC 0x0001
79 /* MCI settings */
80 static const WCHAR wszHklmMci [] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','M','C','I',0};
81 static const WCHAR wszNull [] = {0};
82 static const WCHAR wszAll [] = {'A','L','L',0};
83 static const WCHAR wszMci [] = {'M','C','I',0};
84 static const WCHAR wszOpen [] = {'o','p','e','n',0};
85 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
87 /* dup a string and uppercase it */
88 static inline LPWSTR str_dup_upper( LPCWSTR str )
90 INT len = (strlenW(str) + 1) * sizeof(WCHAR);
91 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
92 if (p)
94 memcpy( p, str, len );
95 CharUpperW( p );
97 return p;
100 /**************************************************************************
101 * MCI_GetDriver [internal]
103 LPWINE_MCIDRIVER MCI_GetDriver(UINT16 wDevID)
105 LPWINE_MCIDRIVER wmd = 0;
107 EnterCriticalSection(&WINMM_IData.cs);
108 for (wmd = WINMM_IData.lpMciDrvs; wmd; wmd = wmd->lpNext) {
109 if (wmd->wDeviceID == wDevID)
110 break;
112 LeaveCriticalSection(&WINMM_IData.cs);
113 return wmd;
116 /**************************************************************************
117 * MCI_GetDriverFromString [internal]
119 UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
121 LPWINE_MCIDRIVER wmd;
122 UINT ret = 0;
124 if (!lpstrName)
125 return 0;
127 if (!strcmpiW(lpstrName, wszAll))
128 return MCI_ALL_DEVICE_ID;
130 EnterCriticalSection(&WINMM_IData.cs);
131 for (wmd = WINMM_IData.lpMciDrvs; wmd; wmd = wmd->lpNext) {
132 if (wmd->lpstrElementName && strcmpW(wmd->lpstrElementName, lpstrName) == 0) {
133 ret = wmd->wDeviceID;
134 break;
136 if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
137 ret = wmd->wDeviceID;
138 break;
140 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
141 ret = wmd->wDeviceID;
142 break;
145 LeaveCriticalSection(&WINMM_IData.cs);
147 return ret;
150 /**************************************************************************
151 * MCI_MessageToString [internal]
153 const char* MCI_MessageToString(UINT wMsg)
155 static char buffer[100];
157 #define CASE(s) case (s): return #s
159 switch (wMsg) {
160 CASE(DRV_LOAD);
161 CASE(DRV_ENABLE);
162 CASE(DRV_OPEN);
163 CASE(DRV_CLOSE);
164 CASE(DRV_DISABLE);
165 CASE(DRV_FREE);
166 CASE(DRV_CONFIGURE);
167 CASE(DRV_QUERYCONFIGURE);
168 CASE(DRV_INSTALL);
169 CASE(DRV_REMOVE);
170 CASE(DRV_EXITSESSION);
171 CASE(DRV_EXITAPPLICATION);
172 CASE(DRV_POWER);
173 CASE(MCI_BREAK);
174 CASE(MCI_CLOSE);
175 CASE(MCI_CLOSE_DRIVER);
176 CASE(MCI_COPY);
177 CASE(MCI_CUE);
178 CASE(MCI_CUT);
179 CASE(MCI_DELETE);
180 CASE(MCI_ESCAPE);
181 CASE(MCI_FREEZE);
182 CASE(MCI_PAUSE);
183 CASE(MCI_PLAY);
184 CASE(MCI_GETDEVCAPS);
185 CASE(MCI_INFO);
186 CASE(MCI_LOAD);
187 CASE(MCI_OPEN);
188 CASE(MCI_OPEN_DRIVER);
189 CASE(MCI_PASTE);
190 CASE(MCI_PUT);
191 CASE(MCI_REALIZE);
192 CASE(MCI_RECORD);
193 CASE(MCI_RESUME);
194 CASE(MCI_SAVE);
195 CASE(MCI_SEEK);
196 CASE(MCI_SET);
197 CASE(MCI_SPIN);
198 CASE(MCI_STATUS);
199 CASE(MCI_STEP);
200 CASE(MCI_STOP);
201 CASE(MCI_SYSINFO);
202 CASE(MCI_UNFREEZE);
203 CASE(MCI_UPDATE);
204 CASE(MCI_WHERE);
205 CASE(MCI_WINDOW);
206 /* constants for digital video */
207 CASE(MCI_CAPTURE);
208 CASE(MCI_MONITOR);
209 CASE(MCI_RESERVE);
210 CASE(MCI_SETAUDIO);
211 CASE(MCI_SIGNAL);
212 CASE(MCI_SETVIDEO);
213 CASE(MCI_QUALITY);
214 CASE(MCI_LIST);
215 CASE(MCI_UNDO);
216 CASE(MCI_CONFIGURE);
217 CASE(MCI_RESTORE);
218 #undef CASE
219 default:
220 sprintf(buffer, "MCI_<<%04X>>", wMsg);
221 return buffer;
225 LPWSTR MCI_strdupAtoW( LPCSTR str )
227 LPWSTR ret;
228 INT len;
230 if (!str) return NULL;
231 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
232 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
233 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
234 return ret;
237 LPSTR MCI_strdupWtoA( LPCWSTR str )
239 LPSTR ret;
240 INT len;
242 if (!str) return NULL;
243 len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
244 ret = HeapAlloc( GetProcessHeap(), 0, len );
245 if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
246 return ret;
249 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
251 if (msg < DRV_RESERVED) return 0;
253 switch (msg)
255 case MCI_CLOSE:
256 case MCI_CONFIGURE:
257 case MCI_PLAY:
258 case MCI_SEEK:
259 case MCI_STOP:
260 case MCI_PAUSE:
261 case MCI_GETDEVCAPS:
262 case MCI_SPIN:
263 case MCI_SET:
264 case MCI_STEP:
265 case MCI_RECORD:
266 case MCI_BREAK:
267 case MCI_SOUND:
268 case MCI_STATUS:
269 case MCI_CUE:
270 case MCI_REALIZE:
271 case MCI_PUT:
272 case MCI_WHERE:
273 case MCI_FREEZE:
274 case MCI_UNFREEZE:
275 case MCI_CUT:
276 case MCI_COPY:
277 case MCI_PASTE:
278 case MCI_UPDATE:
279 case MCI_RESUME:
280 case MCI_DELETE:
281 case MCI_MONITOR:
282 case MCI_SETAUDIO:
283 case MCI_SIGNAL:
284 case MCI_SETVIDEO:
285 case MCI_LIST:
286 return 0;
288 case MCI_OPEN:
290 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA*)*dwParam2;
291 MCI_OPEN_PARMSW *mci_openW;
292 DWORD_PTR *ptr;
294 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW) + 2 * sizeof(DWORD));
295 if (!ptr) return -1;
297 *ptr++ = *dwParam2; /* save the previous pointer */
298 *dwParam2 = (DWORD_PTR)ptr;
299 mci_openW = (MCI_OPEN_PARMSW *)ptr;
301 if (dwParam1 & MCI_NOTIFY)
302 mci_openW->dwCallback = mci_openA->dwCallback;
304 if (dwParam1 & MCI_OPEN_TYPE)
306 if (dwParam1 & MCI_OPEN_TYPE_ID)
307 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
308 else
309 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
311 if (dwParam1 & MCI_OPEN_ELEMENT)
313 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
314 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
315 else
316 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
318 if (dwParam1 & MCI_OPEN_ALIAS)
319 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
320 /* FIXME: this is only needed for specific types of MCI devices, and
321 * may cause a segfault if the two DWORD:s don't exist at the end of
322 * mci_openA
324 memcpy(mci_openW + 1, mci_openA + 1, 2 * sizeof(DWORD));
326 return 1;
328 case MCI_WINDOW:
329 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
331 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
332 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
334 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
335 if (!mci_windowW) return -1;
337 *dwParam2 = (DWORD_PTR)mci_windowW;
339 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
341 if (dwParam1 & MCI_NOTIFY)
342 mci_windowW->dwCallback = mci_windowA->dwCallback;
343 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
344 mci_windowW->hWnd = mci_windowA->hWnd;
345 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
346 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
348 return 1;
350 return 0;
352 case MCI_SYSINFO:
354 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
355 MCI_SYSINFO_PARMSW *mci_sysinfoW;
356 DWORD_PTR *ptr;
358 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
359 if (!ptr) return -1;
361 *ptr++ = *dwParam2; /* save the previous pointer */
362 *dwParam2 = (DWORD_PTR)ptr;
363 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
365 if (dwParam1 & MCI_NOTIFY)
366 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
368 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
369 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize);
370 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
371 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
372 return 1;
374 case MCI_INFO:
376 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
377 MCI_INFO_PARMSW *mci_infoW;
378 DWORD_PTR *ptr;
380 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
381 if (!ptr) return -1;
383 *ptr++ = *dwParam2; /* save the previous pointer */
384 *dwParam2 = (DWORD_PTR)ptr;
385 mci_infoW = (MCI_INFO_PARMSW *)ptr;
387 if (dwParam1 & MCI_NOTIFY)
388 mci_infoW->dwCallback = mci_infoA->dwCallback;
390 mci_infoW->dwRetSize = mci_infoA->dwRetSize * sizeof(WCHAR); /* it's not the same as SYSINFO !!! */
391 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize);
392 return 1;
394 case MCI_SAVE:
396 MCI_SAVE_PARMSA *mci_saveA = (MCI_SAVE_PARMSA *)*dwParam2;
397 MCI_SAVE_PARMSW *mci_saveW;
399 mci_saveW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_saveW));
400 if (!mci_saveW) return -1;
402 *dwParam2 = (DWORD_PTR)mci_saveW;
403 if (dwParam1 & MCI_NOTIFY)
404 mci_saveW->dwCallback = mci_saveA->dwCallback;
405 mci_saveW->lpfilename = MCI_strdupAtoW(mci_saveA->lpfilename);
406 return 1;
408 case MCI_LOAD:
410 MCI_LOAD_PARMSA *mci_loadA = (MCI_LOAD_PARMSA *)*dwParam2;
411 MCI_LOAD_PARMSW *mci_loadW;
413 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
414 if (!mci_loadW) return -1;
416 *dwParam2 = (DWORD_PTR)mci_loadW;
417 if (dwParam1 & MCI_NOTIFY)
418 mci_loadW->dwCallback = mci_loadA->dwCallback;
419 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
420 return 1;
423 case MCI_ESCAPE:
425 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
426 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
428 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
429 if (!mci_vd_escapeW) return -1;
431 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
432 if (dwParam1 & MCI_NOTIFY)
433 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
434 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
435 return 1;
437 default:
438 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
439 return -1;
443 static DWORD MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
444 DWORD result)
446 switch (msg)
448 case MCI_OPEN:
450 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
451 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
452 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)(ptr + 1);
454 mci_openA->wDeviceID = mci_openW->wDeviceID;
456 if (dwParam1 & MCI_OPEN_TYPE)
458 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
459 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
461 if (dwParam1 & MCI_OPEN_ELEMENT)
463 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
464 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
466 if (dwParam1 & MCI_OPEN_ALIAS)
467 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
468 HeapFree(GetProcessHeap(), 0, ptr);
470 break;
471 case MCI_WINDOW:
472 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
474 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
476 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
477 HeapFree(GetProcessHeap(), 0, mci_windowW);
479 break;
481 case MCI_SYSINFO:
483 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
484 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
485 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)(ptr + 1);
487 if (!result)
489 mci_sysinfoA->dwNumber = mci_sysinfoW->dwNumber;
490 mci_sysinfoA->wDeviceType = mci_sysinfoW->wDeviceType;
491 if (dwParam1 & MCI_SYSINFO_QUANTITY)
492 *(DWORD*)mci_sysinfoA->lpstrReturn = *(DWORD*)mci_sysinfoW->lpstrReturn;
493 else
494 WideCharToMultiByte(CP_ACP, 0,
495 mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
496 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
497 NULL, NULL);
500 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
501 HeapFree(GetProcessHeap(), 0, ptr);
503 break;
504 case MCI_INFO:
506 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
507 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
508 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)(ptr + 1);
510 if (!result)
512 WideCharToMultiByte(CP_ACP, 0,
513 mci_infoW->lpstrReturn, mci_infoW->dwRetSize / sizeof(WCHAR),
514 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
515 NULL, NULL);
518 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
519 HeapFree(GetProcessHeap(), 0, ptr);
521 break;
522 case MCI_SAVE:
524 MCI_SAVE_PARMSW *mci_saveW = (MCI_SAVE_PARMSW *)dwParam2;
526 HeapFree(GetProcessHeap(), 0, (void*)mci_saveW->lpfilename);
527 HeapFree(GetProcessHeap(), 0, mci_saveW);
529 break;
530 case MCI_LOAD:
532 MCI_LOAD_PARMSW *mci_loadW = (MCI_LOAD_PARMSW *)dwParam2;
534 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
535 HeapFree(GetProcessHeap(), 0, mci_loadW);
537 break;
538 case MCI_ESCAPE:
540 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
542 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
543 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
545 break;
547 default:
548 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
549 break;
552 return result;
555 /**************************************************************************
556 * MCI_GetDevTypeFromFileName [internal]
558 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
560 LPCWSTR tmp;
561 HKEY hKey;
562 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
563 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
564 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
565 if ((tmp = strrchrW(fileName, '.'))) {
566 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
567 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
568 DWORD dwLen = len;
569 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
570 RegCloseKey( hKey );
571 if (lRet == ERROR_SUCCESS) return 0;
573 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
575 return MCIERR_EXTENSION_NOT_FOUND;
578 #define MAX_MCICMDTABLE 20
579 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
581 typedef struct tagWINE_MCICMDTABLE {
582 UINT uDevType;
583 const BYTE* lpTable;
584 UINT nVerbs; /* number of verbs in command table */
585 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
586 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
588 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
590 /**************************************************************************
591 * MCI_IsCommandTableValid [internal]
593 static BOOL MCI_IsCommandTableValid(UINT uTbl)
595 const BYTE* lmem;
596 LPCWSTR str;
597 DWORD flg;
598 WORD eid;
599 int idx = 0;
600 BOOL inCst = FALSE;
602 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
603 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
605 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
606 return FALSE;
608 lmem = S_MciCmdTable[uTbl].lpTable;
609 do {
610 str = (LPCWSTR)lmem;
611 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
612 flg = *(const DWORD*)lmem;
613 eid = *(const WORD*)(lmem + sizeof(DWORD));
614 lmem += sizeof(DWORD) + sizeof(WORD);
615 idx ++;
616 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
617 switch (eid) {
618 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
619 case MCI_STRING: if (inCst) return FALSE; break;
620 case MCI_INTEGER: if (!*str) return FALSE; break;
621 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
622 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
623 case MCI_FLAG: if (!*str) return FALSE; break;
624 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
625 case MCI_RECT: if (!*str || inCst) return FALSE; break;
626 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
627 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
628 default: return FALSE;
630 } while (eid != MCI_END_COMMAND_LIST);
631 return TRUE;
634 /**************************************************************************
635 * MCI_DumpCommandTable [internal]
637 static BOOL MCI_DumpCommandTable(UINT uTbl)
639 const BYTE* lmem;
640 LPCWSTR str;
641 DWORD flg;
642 WORD eid;
644 if (!MCI_IsCommandTableValid(uTbl)) {
645 ERR("Ooops: %d is not valid\n", uTbl);
646 return FALSE;
649 lmem = S_MciCmdTable[uTbl].lpTable;
650 do {
651 do {
652 str = (LPCWSTR)lmem;
653 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
654 flg = *(const DWORD*)lmem;
655 eid = *(const WORD*)(lmem + sizeof(DWORD));
656 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
657 lmem += sizeof(DWORD) + sizeof(WORD);
658 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
659 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
660 } while (eid != MCI_END_COMMAND_LIST);
661 return TRUE;
665 /**************************************************************************
666 * MCI_GetCommandTable [internal]
668 static UINT MCI_GetCommandTable(UINT uDevType)
670 UINT uTbl;
671 WCHAR buf[32];
672 LPCWSTR str = NULL;
674 /* first look up existing for existing devType */
675 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
676 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
677 return uTbl;
680 /* well try to load id */
681 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
682 if (LoadStringW(WINMM_IData.hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
683 str = buf;
685 } else if (uDevType == 0) {
686 static const WCHAR wszCore[] = {'C','O','R','E',0};
687 str = wszCore;
689 uTbl = MCI_NO_COMMAND_TABLE;
690 if (str) {
691 HRSRC hRsrc = FindResourceW(WINMM_IData.hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
692 HANDLE hMem = 0;
694 if (hRsrc) hMem = LoadResource(WINMM_IData.hWinMM32Instance, hRsrc);
695 if (hMem) {
696 uTbl = MCI_SetCommandTable(LockResource(hMem), uDevType);
697 } else {
698 WARN("No command table found in resource %p[%s]\n",
699 WINMM_IData.hWinMM32Instance, debugstr_w(str));
702 TRACE("=> %d\n", uTbl);
703 return uTbl;
706 /**************************************************************************
707 * MCI_SetCommandTable [internal]
709 UINT MCI_SetCommandTable(void *table, UINT uDevType)
711 int uTbl;
712 static BOOL bInitDone = FALSE;
714 /* <HACK>
715 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
716 * can be called with 0 as a uDevType to retrieve it.
717 * </HACK>
719 if (!bInitDone) {
720 bInitDone = TRUE;
721 MCI_GetCommandTable(0);
723 TRACE("(%p, %u)\n", table, uDevType);
724 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
725 if (!S_MciCmdTable[uTbl].lpTable) {
726 const BYTE* lmem;
727 LPCWSTR str;
728 WORD eid;
729 WORD count;
731 S_MciCmdTable[uTbl].uDevType = uDevType;
732 S_MciCmdTable[uTbl].lpTable = table;
734 if (TRACE_ON(mci)) {
735 MCI_DumpCommandTable(uTbl);
738 /* create the verbs table */
739 /* get # of entries */
740 lmem = S_MciCmdTable[uTbl].lpTable;
741 count = 0;
742 do {
743 str = (LPCWSTR)lmem;
744 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
745 eid = *(const WORD*)(lmem + sizeof(DWORD));
746 lmem += sizeof(DWORD) + sizeof(WORD);
747 if (eid == MCI_COMMAND_HEAD)
748 count++;
749 } while (eid != MCI_END_COMMAND_LIST);
751 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
752 S_MciCmdTable[uTbl].nVerbs = count;
754 lmem = S_MciCmdTable[uTbl].lpTable;
755 count = 0;
756 do {
757 str = (LPCWSTR)lmem;
758 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
759 eid = *(const WORD*)(lmem + sizeof(DWORD));
760 lmem += sizeof(DWORD) + sizeof(WORD);
761 if (eid == MCI_COMMAND_HEAD)
762 S_MciCmdTable[uTbl].aVerbs[count++] = str;
763 } while (eid != MCI_END_COMMAND_LIST);
764 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
765 return uTbl;
769 return MCI_NO_COMMAND_TABLE;
772 /**************************************************************************
773 * MCI_DeleteCommandTable [internal]
775 BOOL MCI_DeleteCommandTable(UINT uTbl, BOOL delete)
777 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
778 return FALSE;
780 if (delete) HeapFree(GetProcessHeap(), 0, (void*)S_MciCmdTable[uTbl].lpTable);
781 S_MciCmdTable[uTbl].lpTable = NULL;
782 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTbl].aVerbs);
783 S_MciCmdTable[uTbl].aVerbs = 0;
784 return TRUE;
787 /**************************************************************************
788 * MCI_UnLoadMciDriver [internal]
790 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
792 LPWINE_MCIDRIVER* tmp;
794 if (!wmd)
795 return TRUE;
797 CloseDriver(wmd->hDriver, 0, 0);
799 if (wmd->dwPrivate != 0)
800 WARN("Unloading mci driver with non nul dwPrivate field\n");
802 EnterCriticalSection(&WINMM_IData.cs);
803 for (tmp = &WINMM_IData.lpMciDrvs; *tmp; tmp = &(*tmp)->lpNext) {
804 if (*tmp == wmd) {
805 *tmp = wmd->lpNext;
806 break;
809 LeaveCriticalSection(&WINMM_IData.cs);
811 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
812 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
813 HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
815 HeapFree(GetProcessHeap(), 0, wmd);
816 return TRUE;
819 /**************************************************************************
820 * MCI_OpenMciDriver [internal]
822 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
824 WCHAR libName[128];
826 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
827 return FALSE;
829 wmd->bIs32 = 0xFFFF;
830 /* First load driver */
831 if ((wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp))) {
832 wmd->bIs32 = TRUE;
833 } else if (WINMM_CheckForMMSystem() && pFnMciMapMsg32WTo16) {
834 WINMM_MapType res;
836 switch (res = pFnMciMapMsg32WTo16(0, DRV_OPEN, 0, &lp)) {
837 case WINMM_MAP_MSGERROR:
838 TRACE("Not handled yet (DRV_OPEN)\n");
839 break;
840 case WINMM_MAP_NOMEM:
841 TRACE("Problem mapping msg=DRV_OPEN from 32W to 16\n");
842 break;
843 case WINMM_MAP_OK:
844 case WINMM_MAP_OKMEM:
845 if ((wmd->hDriver = OpenDriver(drvTyp, wszMci, lp)))
846 wmd->bIs32 = FALSE;
847 if (res == WINMM_MAP_OKMEM)
848 pFnMciUnMapMsg32WTo16(0, DRV_OPEN, 0, lp);
849 break;
852 return (wmd->bIs32 == 0xFFFF) ? FALSE : TRUE;
855 /**************************************************************************
856 * MCI_LoadMciDriver [internal]
858 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
860 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
861 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
862 MCI_OPEN_DRIVER_PARMSW modp;
863 DWORD dwRet = 0;
865 if (!wmd || !strDevTyp) {
866 dwRet = MCIERR_OUT_OF_MEMORY;
867 goto errCleanUp;
870 wmd->lpfnYieldProc = MCI_DefYieldProc;
871 wmd->dwYieldData = VK_CANCEL;
872 wmd->CreatorThread = GetCurrentThreadId();
874 EnterCriticalSection(&WINMM_IData.cs);
875 /* wmd must be inserted in list before sending opening the driver, coz' it
876 * may want to lookup at wDevID
878 wmd->lpNext = WINMM_IData.lpMciDrvs;
879 WINMM_IData.lpMciDrvs = wmd;
881 for (modp.wDeviceID = MCI_MAGIC;
882 MCI_GetDriver(modp.wDeviceID) != 0;
883 modp.wDeviceID++);
885 wmd->wDeviceID = modp.wDeviceID;
887 LeaveCriticalSection(&WINMM_IData.cs);
889 TRACE("wDevID=%04X\n", modp.wDeviceID);
891 modp.lpstrParams = NULL;
893 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
894 /* silence warning if all is used... some bogus program use commands like
895 * 'open all'...
897 if (strcmpiW(strDevTyp, wszAll) == 0) {
898 dwRet = MCIERR_CANNOT_USE_ALL;
899 } else {
900 FIXME("Couldn't load driver for type %s.\n"
901 "If you don't have a windows installation accessible from Wine,\n"
902 "you perhaps forgot to create a [mci] section in system.ini\n",
903 debugstr_w(strDevTyp));
904 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
906 goto errCleanUp;
909 /* FIXME: should also check that module's description is of the form
910 * MODULENAME:[MCI] comment
913 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
914 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
915 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
917 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
918 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
920 wmd->lpstrDeviceType = strDevTyp;
921 wmd->wType = modp.wType;
923 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
924 modp.wDeviceID, modp.wType, modp.wDeviceID);
925 *lpwmd = wmd;
926 return 0;
927 errCleanUp:
928 MCI_UnLoadMciDriver(wmd);
929 HeapFree(GetProcessHeap(), 0, strDevTyp);
930 *lpwmd = 0;
931 return dwRet;
934 /**************************************************************************
935 * MCI_FinishOpen [internal]
937 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
938 DWORD dwParam)
940 if (dwParam & MCI_OPEN_ELEMENT)
942 wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
943 strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
945 if (dwParam & MCI_OPEN_ALIAS)
947 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
948 strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
950 lpParms->wDeviceID = wmd->wDeviceID;
952 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
953 (DWORD)lpParms);
956 /**************************************************************************
957 * MCI_FindCommand [internal]
959 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
961 UINT idx;
963 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
964 return NULL;
966 /* another improvement would be to have the aVerbs array sorted,
967 * so that we could use a dichotomic search on it, rather than this dumb
968 * array look up
970 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
971 if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
972 return S_MciCmdTable[uTbl].aVerbs[idx];
975 return NULL;
978 /**************************************************************************
979 * MCI_GetReturnType [internal]
981 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
983 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
984 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
985 return *(const DWORD*)(lpCmd + 1);
987 return 0L;
990 /**************************************************************************
991 * MCI_GetMessage [internal]
993 static WORD MCI_GetMessage(LPCWSTR lpCmd)
995 return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
998 /**************************************************************************
999 * MCI_GetDWord [internal]
1001 static BOOL MCI_GetDWord(LPDWORD data, LPWSTR* ptr)
1003 DWORD val;
1004 LPWSTR ret;
1006 val = strtoulW(*ptr, &ret, 0);
1008 switch (*ret) {
1009 case '\0': break;
1010 case ' ': ret++; break;
1011 default: return FALSE;
1014 *data |= val;
1015 *ptr = ret;
1016 return TRUE;
1019 /**************************************************************************
1020 * MCI_GetString [internal]
1022 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1024 LPWSTR ptr = *args;
1026 /* see if we have a quoted string */
1027 if (*ptr == '"') {
1028 ptr = strchrW(*str = ptr + 1, '"');
1029 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1030 /* FIXME: shall we escape \" from string ?? */
1031 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1032 *ptr++ = '\0'; /* remove trailing " */
1033 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1034 } else {
1035 ptr = strchrW(ptr, ' ');
1037 if (ptr) {
1038 *ptr++ = '\0';
1039 } else {
1040 ptr = *args + strlenW(*args);
1042 *str = *args;
1045 *args = ptr;
1046 return 0;
1049 #define MCI_DATA_SIZE 16
1051 /**************************************************************************
1052 * MCI_ParseOptArgs [internal]
1054 static DWORD MCI_ParseOptArgs(LPDWORD data, int _offset, LPCWSTR lpCmd,
1055 LPWSTR args, LPDWORD dwFlags)
1057 int len, offset;
1058 const char* lmem;
1059 LPCWSTR str;
1060 DWORD dwRet, flg, cflg = 0;
1061 WORD eid;
1062 BOOL inCst, found;
1064 /* loop on arguments */
1065 while (*args) {
1066 lmem = (const char*)lpCmd;
1067 found = inCst = FALSE;
1068 offset = _offset;
1070 /* skip any leading white space(s) */
1071 while (*args == ' ') args++;
1072 TRACE("args=%s offset=%d\n", debugstr_w(args), offset);
1074 do { /* loop on options for command table for the requested verb */
1075 str = (LPCWSTR)lmem;
1076 lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1077 flg = *(const DWORD*)lmem;
1078 eid = *(const WORD*)(lmem + sizeof(DWORD));
1079 lmem += sizeof(DWORD) + sizeof(WORD);
1080 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1082 switch (eid) {
1083 case MCI_CONSTANT:
1084 inCst = TRUE; cflg = flg; break;
1085 case MCI_END_CONSTANT:
1086 /* there may be additional integral values after flag in constant */
1087 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1088 *dwFlags |= cflg;
1090 inCst = FALSE; cflg = 0;
1091 break;
1094 if (strncmpiW(args, str, len) == 0 &&
1095 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1096 /* store good values into data[] */
1097 args += len;
1098 while (*args == ' ') args++;
1099 found = TRUE;
1101 switch (eid) {
1102 case MCI_COMMAND_HEAD:
1103 case MCI_RETURN:
1104 case MCI_END_COMMAND:
1105 case MCI_END_COMMAND_LIST:
1106 case MCI_CONSTANT: /* done above */
1107 case MCI_END_CONSTANT: /* done above */
1108 break;
1109 case MCI_FLAG:
1110 *dwFlags |= flg;
1111 break;
1112 case MCI_INTEGER:
1113 if (inCst) {
1114 data[offset] |= flg;
1115 *dwFlags |= cflg;
1116 inCst = FALSE;
1117 } else {
1118 *dwFlags |= flg;
1119 if (!MCI_GetDWord(&(data[offset]), &args)) {
1120 return MCIERR_BAD_INTEGER;
1123 break;
1124 case MCI_RECT:
1125 /* store rect in data (offset...offset+3) */
1126 *dwFlags |= flg;
1127 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1128 !MCI_GetDWord(&(data[offset+1]), &args) ||
1129 !MCI_GetDWord(&(data[offset+2]), &args) ||
1130 !MCI_GetDWord(&(data[offset+3]), &args)) {
1131 ERR("Bad rect %s\n", debugstr_w(args));
1132 return MCIERR_BAD_INTEGER;
1134 break;
1135 case MCI_STRING:
1136 *dwFlags |= flg;
1137 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1138 return dwRet;
1139 break;
1140 default: ERR("oops\n");
1142 /* exit inside while loop, except if just entered in constant area definition */
1143 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1144 } else {
1145 /* have offset incremented if needed */
1146 switch (eid) {
1147 case MCI_COMMAND_HEAD:
1148 case MCI_RETURN:
1149 case MCI_END_COMMAND:
1150 case MCI_END_COMMAND_LIST:
1151 case MCI_CONSTANT:
1152 case MCI_FLAG: break;
1153 case MCI_INTEGER: if (!inCst) offset++; break;
1154 case MCI_END_CONSTANT:
1155 case MCI_STRING: offset++; break;
1156 case MCI_RECT: offset += 4; break;
1157 default: ERR("oops\n");
1160 } while (eid != MCI_END_COMMAND);
1161 if (!found) {
1162 WARN("Optarg %s not found\n", debugstr_w(args));
1163 return MCIERR_UNRECOGNIZED_COMMAND;
1165 if (offset == MCI_DATA_SIZE) {
1166 ERR("Internal data[] buffer overflow\n");
1167 return MCIERR_PARSER_INTERNAL;
1170 return 0;
1173 /**************************************************************************
1174 * MCI_HandleReturnValues [internal]
1176 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1177 LPDWORD data, LPWSTR lpstrRet, UINT uRetLen)
1179 static const WCHAR wszLd [] = {'%','l','d',0};
1180 static const WCHAR wszLd4 [] = {'%','l','d',' ','%','l','d',' ','%','l','d',' ','%','l','d',0};
1181 static const WCHAR wszCol3[] = {'%','d',':','%','d',':','%','d',0};
1182 static const WCHAR wszCol4[] = {'%','d',':','%','d',':','%','d',':','%','d',0};
1184 if (lpstrRet) {
1185 switch (retType) {
1186 case 0: /* nothing to return */
1187 break;
1188 case MCI_INTEGER:
1189 switch (dwRet & 0xFFFF0000ul) {
1190 case 0:
1191 case MCI_INTEGER_RETURNED:
1192 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1193 break;
1194 case MCI_RESOURCE_RETURNED:
1195 /* return string which ID is HIWORD(data[1]),
1196 * string is loaded from mmsystem.dll */
1197 LoadStringW(WINMM_IData.hWinMM32Instance, HIWORD(data[1]),
1198 lpstrRet, uRetLen);
1199 break;
1200 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1201 /* return string which ID is HIWORD(data[1]),
1202 * string is loaded from driver */
1203 /* FIXME: this is wrong for a 16 bit handle */
1204 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1205 HIWORD(data[1]), lpstrRet, uRetLen);
1206 break;
1207 case MCI_COLONIZED3_RETURN:
1208 snprintfW(lpstrRet, uRetLen, wszCol3,
1209 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1210 LOBYTE(HIWORD(data[1])));
1211 break;
1212 case MCI_COLONIZED4_RETURN:
1213 snprintfW(lpstrRet, uRetLen, wszCol4,
1214 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1215 LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
1216 break;
1217 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1219 break;
1220 case MCI_STRING:
1221 switch (dwRet & 0xFFFF0000ul) {
1222 case 0:
1223 /* nothing to do data[1] == lpstrRet */
1224 break;
1225 case MCI_INTEGER_RETURNED:
1226 data[1] = *(LPDWORD)lpstrRet;
1227 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1228 break;
1229 default:
1230 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1231 break;
1233 break;
1234 case MCI_RECT:
1235 if (dwRet & 0xFFFF0000ul)
1236 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1237 snprintfW(lpstrRet, uRetLen, wszLd4,
1238 data[1], data[2], data[3], data[4]);
1239 break;
1240 default: ERR("oops\n");
1243 return LOWORD(dwRet);
1246 /**************************************************************************
1247 * mciSendStringW [WINMM.@]
1249 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1250 UINT uRetLen, HWND hwndCallback)
1252 LPWSTR verb, dev, args;
1253 LPWINE_MCIDRIVER wmd = 0;
1254 DWORD dwFlags = 0, dwRet = 0;
1255 int offset = 0;
1256 DWORD data[MCI_DATA_SIZE];
1257 DWORD retType;
1258 LPCWSTR lpCmd = 0;
1259 LPWSTR devAlias = NULL;
1260 static const WCHAR wszNew[] = {'n','e','w',0};
1261 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1262 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1264 TRACE("(%s, %p, %d, %p)\n",
1265 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1267 /* format is <command> <device> <optargs> */
1268 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1269 return MCIERR_OUT_OF_MEMORY;
1270 strcpyW( verb, lpstrCommand );
1271 CharLowerW(verb);
1273 memset(data, 0, sizeof(data));
1275 if (!(args = strchrW(verb, ' '))) {
1276 dwRet = MCIERR_MISSING_DEVICE_NAME;
1277 goto errCleanUp;
1279 *args++ = '\0';
1280 if ((dwRet = MCI_GetString(&dev, &args))) {
1281 goto errCleanUp;
1284 /* Determine devType from open */
1285 if (!strcmpW(verb, wszOpen)) {
1286 LPWSTR devType, tmp;
1287 WCHAR buf[128];
1289 /* case dev == 'new' has to be handled */
1290 if (!strcmpW(dev, wszNew)) {
1291 dev = 0;
1292 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1293 devType += 5;
1294 tmp = strchrW(devType, ' ');
1295 if (tmp) *tmp = '\0';
1296 devType = str_dup_upper(devType);
1297 if (tmp) *tmp = ' ';
1298 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1299 } else {
1300 WARN("open new requires device type\n");
1301 dwRet = MCIERR_MISSING_DEVICE_NAME;
1302 goto errCleanUp;
1304 } else if ((devType = strchrW(dev, '!')) != NULL) {
1305 *devType++ = '\0';
1306 tmp = devType; devType = dev; dev = tmp;
1308 dwFlags |= MCI_OPEN_TYPE;
1309 data[2] = (DWORD)devType;
1310 devType = str_dup_upper(devType);
1311 dwFlags |= MCI_OPEN_ELEMENT;
1312 data[3] = (DWORD)dev;
1313 } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1314 /* this is the name of a mci driver's type */
1315 tmp = strchrW(dev, ' ');
1316 if (tmp) *tmp = '\0';
1317 data[2] = (DWORD)dev;
1318 devType = str_dup_upper(dev);
1319 if (tmp) *tmp = ' ';
1320 dwFlags |= MCI_OPEN_TYPE;
1321 } else {
1322 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1323 devType += 5;
1324 tmp = strchrW(devType, ' ');
1325 if (tmp) *tmp = '\0';
1326 devType = str_dup_upper(devType);
1327 if (tmp) *tmp = ' ';
1328 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1329 } else {
1330 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1331 goto errCleanUp;
1333 devType = str_dup_upper(buf);
1335 dwFlags |= MCI_OPEN_ELEMENT;
1336 data[3] = (DWORD)dev;
1338 if ((devAlias = strstrW(args, wszSAliasS))) {
1339 WCHAR* tmp2;
1340 devAlias += 7;
1341 if (!(tmp = strchrW(devAlias,' '))) tmp = devAlias + strlenW(devAlias);
1342 if (tmp) *tmp = '\0';
1343 tmp2 = HeapAlloc(GetProcessHeap(), 0, (tmp - devAlias + 1) * sizeof(WCHAR) );
1344 memcpy( tmp2, devAlias, (tmp - devAlias) * sizeof(WCHAR) );
1345 tmp2[tmp - devAlias] = 0;
1346 data[4] = (DWORD)tmp2;
1347 /* should be done in regular options parsing */
1348 /* dwFlags |= MCI_OPEN_ALIAS; */
1349 } else if (dev == 0) {
1350 /* "open new" requires alias */
1351 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1352 goto errCleanUp;
1355 dwRet = MCI_LoadMciDriver(devType, &wmd);
1356 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1357 dwRet = MCIERR_INVALID_DEVICE_NAME;
1358 HeapFree(GetProcessHeap(), 0, devType);
1359 if (dwRet) {
1360 MCI_UnLoadMciDriver(wmd);
1361 goto errCleanUp;
1363 } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))) {
1364 /* auto open */
1365 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1366 WCHAR buf[128];
1367 sprintfW(buf, wszOpenWait, dev);
1369 if ((dwRet = mciSendStringW(buf, NULL, 0, 0)) != 0)
1370 goto errCleanUp;
1372 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1373 if (!wmd) {
1374 /* FIXME: memory leak, MCI driver is not closed */
1375 dwRet = MCIERR_INVALID_DEVICE_ID;
1376 goto errCleanUp;
1380 /* get the verb in the different command tables */
1381 if (wmd) {
1382 /* try the device specific command table */
1383 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1384 if (!lpCmd) {
1385 /* try the type specific command table */
1386 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1387 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1388 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1389 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1392 /* try core command table */
1393 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1395 if (!lpCmd) {
1396 TRACE("Command %s not found!\n", debugstr_w(verb));
1397 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1398 goto errCleanUp;
1401 /* set up call back */
1402 if (hwndCallback != 0) {
1403 dwFlags |= MCI_NOTIFY;
1404 data[0] = (DWORD)hwndCallback;
1407 /* set return information */
1408 switch (retType = MCI_GetReturnType(lpCmd)) {
1409 case 0: offset = 1; break;
1410 case MCI_INTEGER: offset = 2; break;
1411 case MCI_STRING: data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
1412 case MCI_RECT: offset = 5; break;
1413 default: ERR("oops\n");
1416 TRACE("verb=%s on dev=%s; offset=%d\n",
1417 debugstr_w(verb), debugstr_w(dev), offset);
1419 if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1420 goto errCleanUp;
1422 /* FIXME: the command should get it's own notification window set up and
1423 * ask for device closing while processing the notification mechanism
1425 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1427 TRACE("[%d, %s, %08x, %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s]\n",
1428 wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1429 data[0], debugstr_w((WCHAR *)data[0]), data[1], debugstr_w((WCHAR *)data[1]),
1430 data[2], debugstr_w((WCHAR *)data[2]), data[3], debugstr_w((WCHAR *)data[3]),
1431 data[4], debugstr_w((WCHAR *)data[4]), data[5], debugstr_w((WCHAR *)data[5]));
1433 if (strcmpW(verb, wszOpen) == 0) {
1434 if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSW)data, dwFlags)))
1435 MCI_UnLoadMciDriver(wmd);
1436 /* FIXME: notification is not properly shared across two opens */
1437 } else {
1438 dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD)data, TRUE);
1440 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1441 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1442 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1444 errCleanUp:
1445 HeapFree(GetProcessHeap(), 0, verb);
1446 HeapFree(GetProcessHeap(), 0, devAlias);
1447 return dwRet;
1450 /**************************************************************************
1451 * mciSendStringA [WINMM.@]
1453 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1454 UINT uRetLen, HWND hwndCallback)
1456 LPWSTR lpwstrCommand;
1457 LPWSTR lpwstrRet = NULL;
1458 UINT ret;
1459 INT len;
1461 /* FIXME: is there something to do with lpstrReturnString ? */
1462 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1463 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1464 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1465 if (lpstrRet)
1467 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1468 if (!lpwstrRet) {
1469 WARN("no memory\n");
1470 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1471 return MCIERR_OUT_OF_MEMORY;
1474 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1475 if (lpwstrRet)
1476 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1477 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1478 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1479 return ret;
1482 /**************************************************************************
1483 * mciExecute [WINMM.@]
1484 * mciExecute [MMSYSTEM.712]
1486 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1488 char strRet[256];
1489 DWORD ret;
1491 TRACE("(%s)!\n", lpstrCommand);
1493 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1494 if (ret != 0) {
1495 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1496 sprintf(strRet, "Unknown MCI error (%d)", ret);
1498 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1500 /* FIXME: what shall I return ? */
1501 return TRUE;
1504 /**************************************************************************
1505 * mciLoadCommandResource [WINMM.@]
1507 * Strangely, this function only exists as an UNICODE one.
1509 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1511 HRSRC hRsrc = 0;
1512 HGLOBAL hMem;
1513 UINT16 ret = MCI_NO_COMMAND_TABLE;
1515 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1517 /* if a file named "resname.mci" exits, then load resource "resname" from it
1518 * otherwise directly from driver
1519 * We don't support it (who uses this feature ?), but we check anyway
1521 if (!type) {
1522 #if 0
1523 /* FIXME: we should put this back into order, but I never found a program
1524 * actually using this feature, so we may not need it
1526 char buf[128];
1527 OFSTRUCT ofs;
1529 strcat(strcpy(buf, resname), ".mci");
1530 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1531 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1533 #endif
1535 if (!(hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA))) {
1536 WARN("No command table found in resource\n");
1537 } else if ((hMem = LoadResource(hInst, hRsrc))) {
1538 ret = MCI_SetCommandTable(LockResource(hMem), type);
1539 } else {
1540 WARN("Couldn't load resource.\n");
1542 TRACE("=> %04x\n", ret);
1543 return ret;
1546 /**************************************************************************
1547 * mciFreeCommandResource [WINMM.@]
1549 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1551 TRACE("(%08x)!\n", uTable);
1553 return MCI_DeleteCommandTable(uTable, FALSE);
1556 /**************************************************************************
1557 * MCI_SendCommandFrom32 [internal]
1559 DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1561 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
1562 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
1564 if (wmd) {
1565 if (wmd->bIs32) {
1566 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1567 } else if (pFnMciMapMsg32WTo16) {
1568 WINMM_MapType res;
1570 switch (res = pFnMciMapMsg32WTo16(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1571 case WINMM_MAP_MSGERROR:
1572 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1573 dwRet = MCIERR_DRIVER_INTERNAL;
1574 break;
1575 case WINMM_MAP_NOMEM:
1576 TRACE("Problem mapping msg=%s from 32a to 16\n", MCI_MessageToString(wMsg));
1577 dwRet = MCIERR_OUT_OF_MEMORY;
1578 break;
1579 case WINMM_MAP_OK:
1580 case WINMM_MAP_OKMEM:
1581 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1582 if (res == WINMM_MAP_OKMEM)
1583 pFnMciUnMapMsg32WTo16(wmd->wType, wMsg, dwParam1, dwParam2);
1584 break;
1588 return dwRet;
1591 /**************************************************************************
1592 * MCI_SendCommandFrom16 [internal]
1594 DWORD MCI_SendCommandFrom16(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1596 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
1597 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
1599 if (wmd) {
1600 dwRet = MCIERR_INVALID_DEVICE_ID;
1602 if (wmd->bIs32 && pFnMciMapMsg16To32W) {
1603 WINMM_MapType res;
1605 switch (res = pFnMciMapMsg16To32W(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1606 case WINMM_MAP_MSGERROR:
1607 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1608 dwRet = MCIERR_DRIVER_INTERNAL;
1609 break;
1610 case WINMM_MAP_NOMEM:
1611 TRACE("Problem mapping msg=%s from 16 to 32a\n", MCI_MessageToString(wMsg));
1612 dwRet = MCIERR_OUT_OF_MEMORY;
1613 break;
1614 case WINMM_MAP_OK:
1615 case WINMM_MAP_OKMEM:
1616 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1617 if (res == WINMM_MAP_OKMEM)
1618 pFnMciUnMapMsg16To32W(wmd->wType, wMsg, dwParam1, dwParam2);
1619 break;
1621 } else {
1622 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1625 return dwRet;
1628 /**************************************************************************
1629 * MCI_Open [internal]
1631 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1633 WCHAR strDevTyp[128];
1634 DWORD dwRet;
1635 LPWINE_MCIDRIVER wmd = NULL;
1637 TRACE("(%08X, %p)\n", dwParam, lpParms);
1638 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1640 /* only two low bytes are generic, the other ones are dev type specific */
1641 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1642 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1643 MCI_NOTIFY|MCI_WAIT)
1644 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1645 FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1647 #undef WINE_MCIDRIVER_SUPP
1649 strDevTyp[0] = 0;
1651 if (dwParam & MCI_OPEN_TYPE) {
1652 if (dwParam & MCI_OPEN_TYPE_ID) {
1653 WORD uDevType = LOWORD((DWORD)lpParms->lpstrDeviceType);
1655 if (uDevType < MCI_DEVTYPE_FIRST ||
1656 uDevType > MCI_DEVTYPE_LAST ||
1657 !LoadStringW(WINMM_IData.hWinMM32Instance, uDevType,
1658 strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1659 dwRet = MCIERR_BAD_INTEGER;
1660 goto errCleanUp;
1662 } else {
1663 LPWSTR ptr;
1664 if (lpParms->lpstrDeviceType == NULL) {
1665 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1666 goto errCleanUp;
1668 strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1669 ptr = strchrW(strDevTyp, '!');
1670 if (ptr) {
1671 /* this behavior is not documented in windows. However, since, in
1672 * some occasions, MCI_OPEN handling is translated by WinMM into
1673 * a call to mciSendString("open <type>"); this code shall be correct
1675 if (dwParam & MCI_OPEN_ELEMENT) {
1676 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1677 debugstr_w(lpParms->lpstrElementName),
1678 debugstr_w(strDevTyp));
1679 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1680 goto errCleanUp;
1682 dwParam |= MCI_OPEN_ELEMENT;
1683 *ptr++ = 0;
1684 /* FIXME: not a good idea to write in user supplied buffer */
1685 lpParms->lpstrElementName = ptr;
1689 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1692 if (dwParam & MCI_OPEN_ELEMENT) {
1693 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1695 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1696 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1697 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1698 goto errCleanUp;
1701 if (!lpParms->lpstrElementName) {
1702 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1703 goto errCleanUp;
1706 /* type, if given as a parameter, supersedes file extension */
1707 if (!strDevTyp[0] &&
1708 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1709 strDevTyp, sizeof(strDevTyp))) {
1710 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1711 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1712 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1713 goto errCleanUp;
1715 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1716 strcpyW(strDevTyp, wszCdAudio);
1720 if (strDevTyp[0] == 0) {
1721 FIXME("Couldn't load driver\n");
1722 dwRet = MCIERR_INVALID_DEVICE_NAME;
1723 goto errCleanUp;
1726 if (dwParam & MCI_OPEN_ALIAS) {
1727 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1728 if (!lpParms->lpstrAlias) {
1729 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1730 goto errCleanUp;
1734 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1735 goto errCleanUp;
1738 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1739 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1740 /* FIXME: is dwRet the correct ret code ? */
1741 goto errCleanUp;
1744 /* only handled devices fall through */
1745 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1747 if (dwParam & MCI_NOTIFY)
1748 mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1750 return 0;
1751 errCleanUp:
1752 if (wmd) MCI_UnLoadMciDriver(wmd);
1754 if (dwParam & MCI_NOTIFY)
1755 mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1756 return dwRet;
1759 /**************************************************************************
1760 * MCI_Close [internal]
1762 static DWORD MCI_Close(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1764 DWORD dwRet;
1765 LPWINE_MCIDRIVER wmd;
1767 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1769 if (wDevID == MCI_ALL_DEVICE_ID) {
1770 LPWINE_MCIDRIVER next;
1772 EnterCriticalSection(&WINMM_IData.cs);
1773 /* FIXME: shall I notify once after all is done, or for
1774 * each of the open drivers ? if the latest, which notif
1775 * to return when only one fails ?
1777 for (wmd = WINMM_IData.lpMciDrvs; wmd; ) {
1778 next = wmd->lpNext;
1779 MCI_Close(wmd->wDeviceID, dwParam, lpParms);
1780 wmd = next;
1782 LeaveCriticalSection(&WINMM_IData.cs);
1783 return 0;
1786 if (!(wmd = MCI_GetDriver(wDevID))) {
1787 return MCIERR_INVALID_DEVICE_ID;
1790 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD)lpParms);
1792 MCI_UnLoadMciDriver(wmd);
1794 if (dwParam & MCI_NOTIFY)
1795 mciDriverNotify(lpParms ? (HWND)lpParms->dwCallback : 0,
1796 wDevID,
1797 dwRet ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1799 return dwRet;
1802 /**************************************************************************
1803 * MCI_WriteString [internal]
1805 DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1807 DWORD ret = 0;
1809 if (lpSrcStr) {
1810 dstSize /= sizeof(WCHAR);
1811 if (dstSize <= strlenW(lpSrcStr)) {
1812 lstrcpynW(lpDstStr, lpSrcStr, dstSize - 1);
1813 ret = MCIERR_PARAM_OVERFLOW;
1814 } else {
1815 strcpyW(lpDstStr, lpSrcStr);
1817 } else {
1818 *lpDstStr = 0;
1820 return ret;
1823 /**************************************************************************
1824 * MCI_Sysinfo [internal]
1826 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1828 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1829 WCHAR buf[2048], *s = buf, *p;
1830 LPWINE_MCIDRIVER wmd;
1831 HKEY hKey;
1833 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1835 TRACE("(%08x, %08X, %08X[num=%d, wDevTyp=%u])\n",
1836 uDevID, dwFlags, (DWORD)lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1838 switch (dwFlags & ~MCI_SYSINFO_OPEN) {
1839 case MCI_SYSINFO_QUANTITY:
1840 if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
1841 if (dwFlags & MCI_SYSINFO_OPEN) {
1842 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1843 EnterCriticalSection(&WINMM_IData.cs);
1844 for (wmd = WINMM_IData.lpMciDrvs; wmd; wmd = wmd->lpNext) {
1845 cnt++;
1847 LeaveCriticalSection(&WINMM_IData.cs);
1848 } else {
1849 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1850 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1851 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1852 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1853 RegCloseKey( hKey );
1855 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1856 for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1858 } else {
1859 if (dwFlags & MCI_SYSINFO_OPEN) {
1860 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n", lpParms->wDeviceType);
1861 EnterCriticalSection(&WINMM_IData.cs);
1862 for (wmd = WINMM_IData.lpMciDrvs; wmd; wmd = wmd->lpNext) {
1863 if (wmd->wType == lpParms->wDeviceType) cnt++;
1865 LeaveCriticalSection(&WINMM_IData.cs);
1866 } else {
1867 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n", lpParms->wDeviceType);
1868 FIXME("Don't know how to get # of MCI devices of a given type\n");
1869 cnt = 1;
1872 *(DWORD*)lpParms->lpstrReturn = cnt;
1873 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1874 ret = MCI_INTEGER_RETURNED;
1875 break;
1876 case MCI_SYSINFO_INSTALLNAME:
1877 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1878 if ((wmd = MCI_GetDriver(uDevID))) {
1879 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1880 wmd->lpstrDeviceType);
1881 } else {
1882 *lpParms->lpstrReturn = 0;
1883 ret = MCIERR_INVALID_DEVICE_ID;
1885 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1886 break;
1887 case MCI_SYSINFO_NAME:
1888 TRACE("MCI_SYSINFO_NAME\n");
1889 if (dwFlags & MCI_SYSINFO_OPEN) {
1890 FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1891 ret = MCIERR_UNRECOGNIZED_COMMAND;
1892 } else {
1893 s = NULL;
1894 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1895 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1896 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1897 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1898 lpParms->dwNumber <= cnt) {
1899 DWORD bufLen = sizeof(buf);
1900 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1901 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1902 s = buf;
1904 RegCloseKey( hKey );
1906 if (!s) {
1907 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1908 for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1909 TRACE("%d: %s\n", cnt, debugstr_w(p));
1910 if (cnt == lpParms->dwNumber - 1) {
1911 s = p;
1912 break;
1917 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR), s) : MCIERR_OUTOFRANGE;
1919 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1920 break;
1921 default:
1922 TRACE("Unsupported flag value=%08x\n", dwFlags);
1923 ret = MCIERR_UNRECOGNIZED_COMMAND;
1925 return ret;
1928 /**************************************************************************
1929 * MCI_Break [internal]
1931 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1933 DWORD dwRet = 0;
1935 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1937 if (dwFlags & MCI_NOTIFY)
1938 mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1939 (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1941 return dwRet;
1944 /**************************************************************************
1945 * MCI_Sound [internal]
1947 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
1949 DWORD dwRet = 0;
1951 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1953 if (dwFlags & MCI_SOUND_NAME)
1954 dwRet = sndPlaySoundW(lpParms->lpstrSoundName, SND_SYNC) ? MMSYSERR_NOERROR : MMSYSERR_ERROR;
1955 else
1956 dwRet = MMSYSERR_ERROR; /* what should be done ??? */
1957 if (dwFlags & MCI_NOTIFY)
1958 mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1959 (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1961 return dwRet;
1964 /**************************************************************************
1965 * MCI_SendCommand [internal]
1967 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1,
1968 DWORD_PTR dwParam2, BOOL bFrom32)
1970 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1972 switch (wMsg) {
1973 case MCI_OPEN:
1974 if (bFrom32) {
1975 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1976 } else if (pFnMciMapMsg16To32W) {
1977 switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
1978 case WINMM_MAP_OK:
1979 case WINMM_MAP_OKMEM:
1980 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1981 pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
1982 break;
1983 default: break; /* so that gcc does not bark */
1986 break;
1987 case MCI_CLOSE:
1988 if (bFrom32) {
1989 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1990 } else if (pFnMciMapMsg16To32W) {
1991 switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
1992 case WINMM_MAP_OK:
1993 case WINMM_MAP_OKMEM:
1994 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1995 pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
1996 break;
1997 default: break; /* so that gcc does not bark */
2000 break;
2001 case MCI_SYSINFO:
2002 if (bFrom32) {
2003 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2004 } else if (pFnMciMapMsg16To32W) {
2005 switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
2006 case WINMM_MAP_OK:
2007 case WINMM_MAP_OKMEM:
2008 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2009 pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
2010 break;
2011 default: break; /* so that gcc does not bark */
2014 break;
2015 case MCI_BREAK:
2016 if (bFrom32) {
2017 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2018 } else if (pFnMciMapMsg16To32W) {
2019 switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
2020 case WINMM_MAP_OK:
2021 case WINMM_MAP_OKMEM:
2022 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2023 pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
2024 break;
2025 default: break; /* so that gcc does not bark */
2028 break;
2029 case MCI_SOUND:
2030 if (bFrom32) {
2031 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2032 } else if (pFnMciMapMsg16To32W) {
2033 switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
2034 case WINMM_MAP_OK:
2035 case WINMM_MAP_OKMEM:
2036 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2037 pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
2038 break;
2039 default: break; /* so that gcc does not bark */
2042 break;
2043 default:
2044 if (wDevID == MCI_ALL_DEVICE_ID) {
2045 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2046 dwRet = MCIERR_CANNOT_USE_ALL;
2047 } else {
2048 dwRet = (bFrom32) ?
2049 MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2) :
2050 MCI_SendCommandFrom16(wDevID, wMsg, dwParam1, dwParam2);
2052 break;
2054 return dwRet;
2057 /**************************************************************************
2058 * MCI_CleanUp [internal]
2060 * Some MCI commands need to be cleaned-up (when not called from
2061 * mciSendString), because MCI drivers return extra information for string
2062 * transformation. This function gets rid of them.
2064 LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD dwParam2)
2066 if (LOWORD(dwRet))
2067 return LOWORD(dwRet);
2069 switch (wMsg) {
2070 case MCI_GETDEVCAPS:
2071 switch (dwRet & 0xFFFF0000ul) {
2072 case 0:
2073 case MCI_COLONIZED3_RETURN:
2074 case MCI_COLONIZED4_RETURN:
2075 case MCI_INTEGER_RETURNED:
2076 /* nothing to do */
2077 break;
2078 case MCI_RESOURCE_RETURNED:
2079 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2081 LPMCI_GETDEVCAPS_PARMS lmgp;
2083 lmgp = (LPMCI_GETDEVCAPS_PARMS)(void*)dwParam2;
2084 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2085 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2087 break;
2088 default:
2089 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2090 HIWORD(dwRet), MCI_MessageToString(wMsg));
2092 break;
2093 case MCI_STATUS:
2094 switch (dwRet & 0xFFFF0000ul) {
2095 case 0:
2096 case MCI_COLONIZED3_RETURN:
2097 case MCI_COLONIZED4_RETURN:
2098 case MCI_INTEGER_RETURNED:
2099 /* nothing to do */
2100 break;
2101 case MCI_RESOURCE_RETURNED:
2102 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2104 LPMCI_STATUS_PARMS lsp;
2106 lsp = (LPMCI_STATUS_PARMS)(void*)dwParam2;
2107 TRACE("Changing %08x to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2108 lsp->dwReturn = LOWORD(lsp->dwReturn);
2110 break;
2111 default:
2112 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2113 HIWORD(dwRet), MCI_MessageToString(wMsg));
2115 break;
2116 case MCI_SYSINFO:
2117 switch (dwRet & 0xFFFF0000ul) {
2118 case 0:
2119 case MCI_INTEGER_RETURNED:
2120 /* nothing to do */
2121 break;
2122 default:
2123 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2125 break;
2126 default:
2127 if (HIWORD(dwRet)) {
2128 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2129 dwRet, MCI_MessageToString(wMsg));
2131 break;
2133 return LOWORD(dwRet);
2136 /**************************************************************************
2137 * mciGetErrorStringW [WINMM.@]
2139 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2141 BOOL ret = FALSE;
2143 if (lpstrBuffer != NULL && uLength > 0 &&
2144 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2146 if (LoadStringW(WINMM_IData.hWinMM32Instance,
2147 wError, lpstrBuffer, uLength) > 0) {
2148 ret = TRUE;
2151 return ret;
2154 /**************************************************************************
2155 * mciGetErrorStringA [WINMM.@]
2157 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2159 BOOL ret = FALSE;
2161 if (lpstrBuffer != NULL && uLength > 0 &&
2162 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2164 if (LoadStringA(WINMM_IData.hWinMM32Instance,
2165 dwError, lpstrBuffer, uLength) > 0) {
2166 ret = TRUE;
2169 return ret;
2172 /**************************************************************************
2173 * mciDriverNotify [WINMM.@]
2175 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2177 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2179 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2182 /**************************************************************************
2183 * mciGetDriverData [WINMM.@]
2185 DWORD WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2187 LPWINE_MCIDRIVER wmd;
2189 TRACE("(%04x)\n", uDeviceID);
2191 wmd = MCI_GetDriver(uDeviceID);
2193 if (!wmd) {
2194 WARN("Bad uDeviceID\n");
2195 return 0L;
2198 return wmd->dwPrivate;
2201 /**************************************************************************
2202 * mciSetDriverData [WINMM.@]
2204 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD data)
2206 LPWINE_MCIDRIVER wmd;
2208 TRACE("(%04x, %08x)\n", uDeviceID, data);
2210 wmd = MCI_GetDriver(uDeviceID);
2212 if (!wmd) {
2213 WARN("Bad uDeviceID\n");
2214 return FALSE;
2217 wmd->dwPrivate = data;
2218 return TRUE;
2221 /**************************************************************************
2222 * mciSendCommandW [WINMM.@]
2225 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2227 DWORD dwRet;
2229 TRACE("(%08x, %s, %08lx, %08lx)\n",
2230 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2232 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2, TRUE);
2233 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2234 TRACE("=> %08x\n", dwRet);
2235 return dwRet;
2238 /**************************************************************************
2239 * mciSendCommandA [WINMM.@]
2241 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2243 DWORD ret;
2244 int mapped;
2246 TRACE("(%08x, %s, %08lx, %08lx)\n",
2247 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2249 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2250 if (mapped == -1)
2252 FIXME("message %04x mapping failed\n", wMsg);
2253 return MMSYSERR_NOMEM;
2255 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2256 if (mapped)
2257 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2258 return ret;
2261 /**************************************************************************
2262 * mciGetDeviceIDA [WINMM.@]
2264 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2266 LPWSTR w = MCI_strdupAtoW(lpstrName);
2267 UINT ret = MCIERR_OUT_OF_MEMORY;
2269 if (w)
2271 ret = mciGetDeviceIDW(w);
2272 HeapFree(GetProcessHeap(), 0, w);
2274 return ret;
2277 /**************************************************************************
2278 * mciGetDeviceIDW [WINMM.@]
2280 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2282 return MCI_GetDriverFromString(lpwstrName);
2285 /******************************************************************
2286 * MyUserYield
2288 * Internal wrapper to call USER.UserYield16 (in fact through a Wine only export from USER32).
2290 static void MyUserYield(void)
2292 HMODULE mod = GetModuleHandleA( "user32.dll" );
2293 if (mod)
2295 FARPROC proc = GetProcAddress( mod, "UserYield16" );
2296 if (proc) proc();
2300 /**************************************************************************
2301 * MCI_DefYieldProc [internal]
2303 UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2305 INT16 ret;
2307 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2309 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2310 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2311 MyUserYield();
2312 ret = 0;
2313 } else {
2314 MSG msg;
2316 msg.hwnd = HWND_32(HIWORD(data));
2317 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2318 ret = -1;
2320 return ret;
2323 /**************************************************************************
2324 * mciSetYieldProc [WINMM.@]
2326 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2328 LPWINE_MCIDRIVER wmd;
2330 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2332 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2333 WARN("Bad uDeviceID\n");
2334 return FALSE;
2337 wmd->lpfnYieldProc = fpYieldProc;
2338 wmd->dwYieldData = dwYieldData;
2339 wmd->bIs32 = TRUE;
2341 return TRUE;
2344 /**************************************************************************
2345 * mciGetDeviceIDFromElementIDA [WINMM.@]
2347 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2349 LPWSTR w = MCI_strdupAtoW(lpstrType);
2350 UINT ret = 0;
2352 if (w)
2354 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2355 HeapFree(GetProcessHeap(), 0, w);
2357 return ret;
2360 /**************************************************************************
2361 * mciGetDeviceIDFromElementIDW [WINMM.@]
2363 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2365 /* FIXME: that's rather strange, there is no
2366 * mciGetDeviceIDFromElementID32A in winmm.spec
2368 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2369 return 0;
2372 /**************************************************************************
2373 * mciGetYieldProc [WINMM.@]
2375 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2377 LPWINE_MCIDRIVER wmd;
2379 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2381 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2382 WARN("Bad uDeviceID\n");
2383 return NULL;
2385 if (!wmd->lpfnYieldProc) {
2386 WARN("No proc set\n");
2387 return NULL;
2389 if (!wmd->bIs32) {
2390 WARN("Proc is 32 bit\n");
2391 return NULL;
2393 return wmd->lpfnYieldProc;
2396 /**************************************************************************
2397 * mciGetCreatorTask [WINMM.@]
2399 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2401 LPWINE_MCIDRIVER wmd;
2402 HTASK ret = 0;
2404 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)wmd->CreatorThread;
2406 TRACE("(%u) => %p\n", uDeviceID, ret);
2407 return ret;
2410 /**************************************************************************
2411 * mciDriverYield [WINMM.@]
2413 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2415 LPWINE_MCIDRIVER wmd;
2416 UINT ret = 0;
2418 TRACE("(%04x)\n", uDeviceID);
2420 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc || !wmd->bIs32) {
2421 MyUserYield();
2422 } else {
2423 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2426 return ret;