mshtml: Respond to SID_SContainerDispatch service id.
[wine/multimedia.git] / dlls / winmm / mci.c
blob9f85bde4081529b115c4f0b2a7d7bd3a9672bb0d
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 #include "config.h"
41 #include "wine/port.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "mmsystem.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "winreg.h"
55 #include "wownt32.h"
57 #include "digitalv.h"
58 #include "winemm.h"
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(mci);
65 /* First MCI valid device ID (0 means error) */
66 #define MCI_MAGIC 0x0001
68 /* MCI settings */
69 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};
70 static const WCHAR wszNull [] = {0};
71 static const WCHAR wszAll [] = {'A','L','L',0};
72 static const WCHAR wszMci [] = {'M','C','I',0};
73 static const WCHAR wszOpen [] = {'o','p','e','n',0};
74 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
76 static WINE_MCIDRIVER *MciDrivers;
78 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
79 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
81 /* dup a string and uppercase it */
82 static inline LPWSTR str_dup_upper( LPCWSTR str )
84 INT len = (strlenW(str) + 1) * sizeof(WCHAR);
85 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
86 if (p)
88 memcpy( p, str, len );
89 CharUpperW( p );
91 return p;
94 /**************************************************************************
95 * MCI_GetDriver [internal]
97 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
99 LPWINE_MCIDRIVER wmd = 0;
101 EnterCriticalSection(&WINMM_cs);
102 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
103 if (wmd->wDeviceID == wDevID)
104 break;
106 LeaveCriticalSection(&WINMM_cs);
107 return wmd;
110 /**************************************************************************
111 * MCI_GetDriverFromString [internal]
113 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
115 LPWINE_MCIDRIVER wmd;
116 UINT ret = 0;
118 if (!lpstrName)
119 return 0;
121 if (!strcmpiW(lpstrName, wszAll))
122 return MCI_ALL_DEVICE_ID;
124 EnterCriticalSection(&WINMM_cs);
125 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
126 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
127 ret = wmd->wDeviceID;
128 break;
131 LeaveCriticalSection(&WINMM_cs);
133 return ret;
136 /**************************************************************************
137 * MCI_MessageToString [internal]
139 static const char* MCI_MessageToString(UINT wMsg)
141 #define CASE(s) case (s): return #s
143 switch (wMsg) {
144 CASE(DRV_LOAD);
145 CASE(DRV_ENABLE);
146 CASE(DRV_OPEN);
147 CASE(DRV_CLOSE);
148 CASE(DRV_DISABLE);
149 CASE(DRV_FREE);
150 CASE(DRV_CONFIGURE);
151 CASE(DRV_QUERYCONFIGURE);
152 CASE(DRV_INSTALL);
153 CASE(DRV_REMOVE);
154 CASE(DRV_EXITSESSION);
155 CASE(DRV_EXITAPPLICATION);
156 CASE(DRV_POWER);
157 CASE(MCI_BREAK);
158 CASE(MCI_CLOSE);
159 CASE(MCI_CLOSE_DRIVER);
160 CASE(MCI_COPY);
161 CASE(MCI_CUE);
162 CASE(MCI_CUT);
163 CASE(MCI_DELETE);
164 CASE(MCI_ESCAPE);
165 CASE(MCI_FREEZE);
166 CASE(MCI_PAUSE);
167 CASE(MCI_PLAY);
168 CASE(MCI_GETDEVCAPS);
169 CASE(MCI_INFO);
170 CASE(MCI_LOAD);
171 CASE(MCI_OPEN);
172 CASE(MCI_OPEN_DRIVER);
173 CASE(MCI_PASTE);
174 CASE(MCI_PUT);
175 CASE(MCI_REALIZE);
176 CASE(MCI_RECORD);
177 CASE(MCI_RESUME);
178 CASE(MCI_SAVE);
179 CASE(MCI_SEEK);
180 CASE(MCI_SET);
181 CASE(MCI_SOUND);
182 CASE(MCI_SPIN);
183 CASE(MCI_STATUS);
184 CASE(MCI_STEP);
185 CASE(MCI_STOP);
186 CASE(MCI_SYSINFO);
187 CASE(MCI_UNFREEZE);
188 CASE(MCI_UPDATE);
189 CASE(MCI_WHERE);
190 CASE(MCI_WINDOW);
191 /* constants for digital video */
192 CASE(MCI_CAPTURE);
193 CASE(MCI_MONITOR);
194 CASE(MCI_RESERVE);
195 CASE(MCI_SETAUDIO);
196 CASE(MCI_SIGNAL);
197 CASE(MCI_SETVIDEO);
198 CASE(MCI_QUALITY);
199 CASE(MCI_LIST);
200 CASE(MCI_UNDO);
201 CASE(MCI_CONFIGURE);
202 CASE(MCI_RESTORE);
203 #undef CASE
204 default:
205 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
209 static LPWSTR MCI_strdupAtoW( LPCSTR str )
211 LPWSTR ret;
212 INT len;
214 if (!str) return NULL;
215 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
216 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
217 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
218 return ret;
221 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
223 if (msg < DRV_RESERVED) return 0;
225 switch (msg)
227 case MCI_CLOSE:
228 case MCI_CONFIGURE:
229 case MCI_PLAY:
230 case MCI_SEEK:
231 case MCI_STOP:
232 case MCI_PAUSE:
233 case MCI_GETDEVCAPS:
234 case MCI_SPIN:
235 case MCI_SET:
236 case MCI_STEP:
237 case MCI_RECORD:
238 case MCI_BREAK:
239 case MCI_STATUS:
240 case MCI_CUE:
241 case MCI_REALIZE:
242 case MCI_PUT:
243 case MCI_WHERE:
244 case MCI_FREEZE:
245 case MCI_UNFREEZE:
246 case MCI_CUT:
247 case MCI_COPY:
248 case MCI_PASTE:
249 case MCI_UPDATE:
250 case MCI_RESUME:
251 case MCI_DELETE:
252 case MCI_MONITOR:
253 case MCI_SIGNAL:
254 case MCI_UNDO:
255 return 0;
257 case MCI_OPEN:
258 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
259 * structure, larger than MCI_WAVE_OPEN_PARMS */
260 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
261 MCI_ANIM_OPEN_PARMSW *mci_openW;
262 DWORD_PTR *ptr;
264 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW));
265 if (!ptr) return -1;
267 *ptr++ = *dwParam2; /* save the previous pointer */
268 *dwParam2 = (DWORD_PTR)ptr;
269 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
271 if (dwParam1 & MCI_NOTIFY)
272 mci_openW->dwCallback = mci_openA->dwCallback;
274 if (dwParam1 & MCI_OPEN_TYPE)
276 if (dwParam1 & MCI_OPEN_TYPE_ID)
277 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
278 else
279 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
281 if (dwParam1 & MCI_OPEN_ELEMENT)
283 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
284 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
285 else
286 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
288 if (dwParam1 & MCI_OPEN_ALIAS)
289 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
290 /* We don't know how many DWORD follow, as
291 * the structure depends on the device. */
292 if (HIWORD(dwParam1))
293 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
295 return 1;
297 case MCI_WINDOW:
298 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
300 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
301 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
303 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
304 if (!mci_windowW) return -1;
306 *dwParam2 = (DWORD_PTR)mci_windowW;
308 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
310 if (dwParam1 & MCI_NOTIFY)
311 mci_windowW->dwCallback = mci_windowA->dwCallback;
312 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
313 mci_windowW->hWnd = mci_windowA->hWnd;
314 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
315 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
317 return 1;
319 return 0;
321 case MCI_SYSINFO:
322 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
324 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
325 MCI_SYSINFO_PARMSW *mci_sysinfoW;
326 DWORD_PTR *ptr;
328 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
329 if (!ptr) return -1;
331 *ptr++ = *dwParam2; /* save the previous pointer */
332 *dwParam2 = (DWORD_PTR)ptr;
333 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
335 if (dwParam1 & MCI_NOTIFY)
336 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
338 /* Size is measured in numbers of characters, despite what MSDN says. */
339 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
340 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize * sizeof(WCHAR));
341 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
342 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
343 return 1;
345 return 0;
346 case MCI_INFO:
348 MCI_DGV_INFO_PARMSA *mci_infoA = (MCI_DGV_INFO_PARMSA *)*dwParam2;
349 MCI_DGV_INFO_PARMSW *mci_infoW;
350 DWORD_PTR *ptr;
352 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
353 if (!ptr) return -1;
355 *ptr++ = *dwParam2; /* save the previous pointer */
356 *dwParam2 = (DWORD_PTR)ptr;
357 mci_infoW = (MCI_DGV_INFO_PARMSW *)ptr;
359 if (dwParam1 & MCI_NOTIFY)
360 mci_infoW->dwCallback = mci_infoA->dwCallback;
362 /* Size is measured in numbers of characters. */
363 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
364 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize * sizeof(WCHAR));
365 if (dwParam1 & MCI_DGV_INFO_ITEM)
366 mci_infoW->dwItem = mci_infoA->dwItem;
367 return 1;
369 case MCI_SAVE:
370 case MCI_LOAD:
371 case MCI_CAPTURE:
372 case MCI_RESTORE:
373 { /* All these commands have the same layout: callback + string + optional rect */
374 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
375 MCI_OVLY_LOAD_PARMSW *mci_loadW;
377 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
378 if (!mci_loadW) return -1;
380 *dwParam2 = (DWORD_PTR)mci_loadW;
381 if (dwParam1 & MCI_NOTIFY)
382 mci_loadW->dwCallback = mci_loadA->dwCallback;
383 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
384 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
385 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
386 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
387 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
388 mci_loadW->rc = mci_loadA->rc;
389 return 1;
391 case MCI_SOUND:
392 case MCI_ESCAPE:
393 { /* All these commands have the same layout: callback + string */
394 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
395 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
397 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
398 if (!mci_vd_escapeW) return -1;
400 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
401 if (dwParam1 & MCI_NOTIFY)
402 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
403 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
404 return 1;
406 case MCI_SETAUDIO:
407 case MCI_SETVIDEO:
408 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
409 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
410 return 0;
411 /* fall through to default */
412 case MCI_RESERVE:
413 case MCI_QUALITY:
414 case MCI_LIST:
415 default:
416 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
417 return 0; /* pass through untouched */
421 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
422 DWORD result)
424 switch (msg)
426 case MCI_OPEN:
428 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
429 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
430 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
432 mci_openA->wDeviceID = mci_openW->wDeviceID;
434 if (dwParam1 & MCI_OPEN_TYPE)
436 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
437 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
439 if (dwParam1 & MCI_OPEN_ELEMENT)
441 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
442 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
444 if (dwParam1 & MCI_OPEN_ALIAS)
445 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
446 HeapFree(GetProcessHeap(), 0, ptr);
448 break;
449 case MCI_WINDOW:
450 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
452 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
454 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
455 HeapFree(GetProcessHeap(), 0, mci_windowW);
457 break;
459 case MCI_SYSINFO:
460 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
462 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
463 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
464 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
466 if (!result)
468 WideCharToMultiByte(CP_ACP, 0,
469 mci_sysinfoW->lpstrReturn, -1,
470 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
471 NULL, NULL);
474 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
475 HeapFree(GetProcessHeap(), 0, ptr);
477 break;
478 case MCI_INFO:
480 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
481 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
482 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
484 if (!result)
486 WideCharToMultiByte(CP_ACP, 0,
487 mci_infoW->lpstrReturn, -1,
488 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
489 NULL, NULL);
492 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
493 HeapFree(GetProcessHeap(), 0, ptr);
495 break;
496 case MCI_SAVE:
497 case MCI_LOAD:
498 case MCI_CAPTURE:
499 case MCI_RESTORE:
500 { /* All these commands have the same layout: callback + string + optional rect */
501 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
503 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
504 HeapFree(GetProcessHeap(), 0, mci_loadW);
506 break;
507 case MCI_SOUND:
508 case MCI_ESCAPE:
509 { /* All these commands have the same layout: callback + string */
510 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
512 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
513 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
515 break;
517 default:
518 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
519 break;
523 /**************************************************************************
524 * MCI_GetDevTypeFromFileName [internal]
526 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
528 LPCWSTR tmp;
529 HKEY hKey;
530 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
531 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
532 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
533 if ((tmp = strrchrW(fileName, '.'))) {
534 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
535 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
536 DWORD dwLen = len;
537 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
538 RegCloseKey( hKey );
539 if (lRet == ERROR_SUCCESS) return 0;
541 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
543 return MCIERR_EXTENSION_NOT_FOUND;
546 /**************************************************************************
547 * MCI_GetDevTypeFromResource [internal]
549 static UINT MCI_GetDevTypeFromResource(LPCWSTR lpstrName)
551 WCHAR buf[32];
552 UINT uDevType;
553 for (uDevType = MCI_DEVTYPE_FIRST; uDevType <= MCI_DEVTYPE_LAST; uDevType++) {
554 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
555 /* FIXME: ignore digits suffix */
556 if (!strcmpiW(buf, lpstrName))
557 return uDevType;
560 return 0;
563 #define MAX_MCICMDTABLE 20
564 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
566 typedef struct tagWINE_MCICMDTABLE {
567 UINT uDevType;
568 HGLOBAL hMem;
569 const BYTE* lpTable;
570 UINT nVerbs; /* number of verbs in command table */
571 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
572 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
574 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
576 /**************************************************************************
577 * MCI_IsCommandTableValid [internal]
579 static BOOL MCI_IsCommandTableValid(UINT uTbl)
581 const BYTE* lmem;
582 LPCWSTR str;
583 DWORD flg;
584 WORD eid;
585 int idx = 0;
586 BOOL inCst = FALSE;
588 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
589 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
591 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
592 return FALSE;
594 lmem = S_MciCmdTable[uTbl].lpTable;
595 do {
596 str = (LPCWSTR)lmem;
597 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
598 flg = *(const DWORD*)lmem;
599 eid = *(const WORD*)(lmem + sizeof(DWORD));
600 lmem += sizeof(DWORD) + sizeof(WORD);
601 idx ++;
602 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
603 switch (eid) {
604 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
605 case MCI_STRING: if (inCst) return FALSE; break;
606 case MCI_HWND: /* Occurs inside MCI_CONSTANT as in "window handle default" */
607 case MCI_HPAL:
608 case MCI_HDC:
609 case MCI_INTEGER: if (!*str) return FALSE; break;
610 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
611 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
612 case MCI_FLAG: if (!*str) return FALSE; break;
613 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
614 case MCI_RECT: if (!*str || inCst) return FALSE; break;
615 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
616 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
617 default: return FALSE;
619 } while (eid != MCI_END_COMMAND_LIST);
620 return TRUE;
623 /**************************************************************************
624 * MCI_DumpCommandTable [internal]
626 static BOOL MCI_DumpCommandTable(UINT uTbl)
628 const BYTE* lmem;
629 LPCWSTR str;
630 WORD eid;
632 if (!MCI_IsCommandTableValid(uTbl)) {
633 ERR("Ooops: %d is not valid\n", uTbl);
634 return FALSE;
637 lmem = S_MciCmdTable[uTbl].lpTable;
638 do {
639 do {
640 /* DWORD flg; */
641 str = (LPCWSTR)lmem;
642 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
643 /* flg = *(const DWORD*)lmem; */
644 eid = *(const WORD*)(lmem + sizeof(DWORD));
645 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
646 lmem += sizeof(DWORD) + sizeof(WORD);
647 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
648 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
649 } while (eid != MCI_END_COMMAND_LIST);
650 return TRUE;
654 /**************************************************************************
655 * MCI_GetCommandTable [internal]
657 static UINT MCI_GetCommandTable(UINT uDevType)
659 UINT uTbl;
660 WCHAR buf[32];
661 LPCWSTR str = NULL;
663 /* first look up existing for existing devType */
664 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
665 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
666 return uTbl;
669 /* well try to load id */
670 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
671 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
672 str = buf;
674 } else if (uDevType == 0) {
675 static const WCHAR wszCore[] = {'C','O','R','E',0};
676 str = wszCore;
678 uTbl = MCI_NO_COMMAND_TABLE;
679 if (str) {
680 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
681 HANDLE hMem = 0;
683 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
684 if (hMem) {
685 uTbl = MCI_SetCommandTable(hMem, uDevType);
686 } else {
687 WARN("No command table found in resource %p[%s]\n",
688 hWinMM32Instance, debugstr_w(str));
691 TRACE("=> %d\n", uTbl);
692 return uTbl;
695 /**************************************************************************
696 * MCI_SetCommandTable [internal]
698 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
700 int uTbl;
701 static BOOL bInitDone = FALSE;
703 /* <HACK>
704 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
705 * can be called with 0 as a uDevType to retrieve it.
706 * </HACK>
708 if (!bInitDone) {
709 bInitDone = TRUE;
710 MCI_GetCommandTable(0);
712 TRACE("(%p, %u)\n", hMem, uDevType);
713 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
714 if (!S_MciCmdTable[uTbl].lpTable) {
715 const BYTE* lmem;
716 LPCWSTR str;
717 WORD eid;
718 WORD count;
720 S_MciCmdTable[uTbl].uDevType = uDevType;
721 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
722 S_MciCmdTable[uTbl].hMem = hMem;
724 if (TRACE_ON(mci)) {
725 MCI_DumpCommandTable(uTbl);
728 /* create the verbs table */
729 /* get # of entries */
730 lmem = S_MciCmdTable[uTbl].lpTable;
731 count = 0;
732 do {
733 str = (LPCWSTR)lmem;
734 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
735 eid = *(const WORD*)(lmem + sizeof(DWORD));
736 lmem += sizeof(DWORD) + sizeof(WORD);
737 if (eid == MCI_COMMAND_HEAD)
738 count++;
739 } while (eid != MCI_END_COMMAND_LIST);
741 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
742 S_MciCmdTable[uTbl].nVerbs = count;
744 lmem = S_MciCmdTable[uTbl].lpTable;
745 count = 0;
746 do {
747 str = (LPCWSTR)lmem;
748 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
749 eid = *(const WORD*)(lmem + sizeof(DWORD));
750 lmem += sizeof(DWORD) + sizeof(WORD);
751 if (eid == MCI_COMMAND_HEAD)
752 S_MciCmdTable[uTbl].aVerbs[count++] = str;
753 } while (eid != MCI_END_COMMAND_LIST);
754 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
755 return uTbl;
759 return MCI_NO_COMMAND_TABLE;
762 /**************************************************************************
763 * MCI_UnLoadMciDriver [internal]
765 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
767 LPWINE_MCIDRIVER* tmp;
769 if (!wmd)
770 return TRUE;
772 CloseDriver(wmd->hDriver, 0, 0);
774 if (wmd->dwPrivate != 0)
775 WARN("Unloading mci driver with non nul dwPrivate field\n");
777 EnterCriticalSection(&WINMM_cs);
778 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
779 if (*tmp == wmd) {
780 *tmp = wmd->lpNext;
781 break;
784 LeaveCriticalSection(&WINMM_cs);
786 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
787 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
789 HeapFree(GetProcessHeap(), 0, wmd);
790 return TRUE;
793 /**************************************************************************
794 * MCI_OpenMciDriver [internal]
796 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
798 WCHAR libName[128];
800 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
801 return FALSE;
803 /* First load driver */
804 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
805 return wmd->hDriver != NULL;
808 /**************************************************************************
809 * MCI_LoadMciDriver [internal]
811 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
813 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
814 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
815 MCI_OPEN_DRIVER_PARMSW modp;
816 DWORD dwRet = 0;
818 if (!wmd || !strDevTyp) {
819 dwRet = MCIERR_OUT_OF_MEMORY;
820 goto errCleanUp;
823 wmd->lpfnYieldProc = MCI_DefYieldProc;
824 wmd->dwYieldData = VK_CANCEL;
825 wmd->CreatorThread = GetCurrentThreadId();
827 EnterCriticalSection(&WINMM_cs);
828 /* wmd must be inserted in list before sending opening the driver, because it
829 * may want to lookup at wDevID
831 wmd->lpNext = MciDrivers;
832 MciDrivers = wmd;
834 for (modp.wDeviceID = MCI_MAGIC;
835 MCI_GetDriver(modp.wDeviceID) != 0;
836 modp.wDeviceID++);
838 wmd->wDeviceID = modp.wDeviceID;
840 LeaveCriticalSection(&WINMM_cs);
842 TRACE("wDevID=%04X\n", modp.wDeviceID);
844 modp.lpstrParams = NULL;
846 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
847 /* silence warning if all is used... some bogus program use commands like
848 * 'open all'...
850 if (strcmpiW(strDevTyp, wszAll) == 0) {
851 dwRet = MCIERR_CANNOT_USE_ALL;
852 } else {
853 FIXME("Couldn't load driver for type %s.\n",
854 debugstr_w(strDevTyp));
855 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
857 goto errCleanUp;
860 /* FIXME: should also check that module's description is of the form
861 * MODULENAME:[MCI] comment
864 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
865 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
866 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
868 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
869 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
871 wmd->lpstrDeviceType = strDevTyp;
872 wmd->wType = modp.wType;
874 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
875 modp.wDeviceID, modp.wType, modp.wDeviceID);
876 *lpwmd = wmd;
877 return 0;
878 errCleanUp:
879 MCI_UnLoadMciDriver(wmd);
880 HeapFree(GetProcessHeap(), 0, strDevTyp);
881 *lpwmd = 0;
882 return dwRet;
885 /**************************************************************************
886 * MCI_SendCommandFrom32 [internal]
888 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
890 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
891 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
893 if (wmd) {
894 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
896 return dwRet;
899 /**************************************************************************
900 * MCI_FinishOpen [internal]
902 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
903 DWORD dwParam)
905 LPCWSTR alias = NULL;
906 /* Open always defines an alias for further reference */
907 if (dwParam & MCI_OPEN_ALIAS) { /* open ... alias */
908 alias = lpParms->lpstrAlias;
909 if (MCI_GetDriverFromString(alias))
910 return MCIERR_DUPLICATE_ALIAS;
911 } else {
912 if ((dwParam & MCI_OPEN_ELEMENT) /* open file.wav */
913 && !(dwParam & MCI_OPEN_ELEMENT_ID))
914 alias = lpParms->lpstrElementName;
915 else if (dwParam & MCI_OPEN_TYPE ) /* open cdaudio */
916 alias = wmd->lpstrDeviceType;
917 if (alias && MCI_GetDriverFromString(alias))
918 return MCIERR_DEVICE_OPEN;
920 if (alias) {
921 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(alias)+1) * sizeof(WCHAR));
922 if (!wmd->lpstrAlias) return MCIERR_OUT_OF_MEMORY;
923 strcpyW( wmd->lpstrAlias, alias);
924 /* In most cases, natives adds MCI_OPEN_ALIAS to the flags passed to the driver.
925 * Don't. The drivers don't care about the winmm alias. */
927 lpParms->wDeviceID = wmd->wDeviceID;
929 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
930 (DWORD_PTR)lpParms);
933 /**************************************************************************
934 * MCI_FindCommand [internal]
936 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
938 UINT idx;
940 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
941 return NULL;
943 /* another improvement would be to have the aVerbs array sorted,
944 * so that we could use a dichotomic search on it, rather than this dumb
945 * array look up
947 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
948 if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
949 return S_MciCmdTable[uTbl].aVerbs[idx];
952 return NULL;
955 /**************************************************************************
956 * MCI_GetReturnType [internal]
958 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
960 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
961 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
962 return *(const DWORD*)(lpCmd + 1);
964 return 0L;
967 /**************************************************************************
968 * MCI_GetMessage [internal]
970 static WORD MCI_GetMessage(LPCWSTR lpCmd)
972 return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
975 /**************************************************************************
976 * MCI_GetDWord [internal]
978 static BOOL MCI_GetDWord(DWORD* data, LPWSTR* ptr)
980 DWORD val;
981 LPWSTR ret;
983 val = strtoulW(*ptr, &ret, 10);
985 switch (*ret) {
986 case '\0': break;
987 case ' ': ret++; break;
988 default: return FALSE;
991 *data |= val;
992 *ptr = ret;
993 return TRUE;
996 /**************************************************************************
997 * MCI_GetString [internal]
999 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1001 LPWSTR ptr = *args;
1003 /* see if we have a quoted string */
1004 if (*ptr == '"') {
1005 ptr = strchrW(*str = ptr + 1, '"');
1006 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1007 /* FIXME: shall we escape \" from string ?? */
1008 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1009 *ptr++ = '\0'; /* remove trailing " */
1010 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1011 } else {
1012 ptr = strchrW(ptr, ' ');
1014 if (ptr) {
1015 *ptr++ = '\0';
1016 } else {
1017 ptr = *args + strlenW(*args);
1019 *str = *args;
1022 *args = ptr;
1023 return 0;
1026 #define MCI_DATA_SIZE 16
1028 /**************************************************************************
1029 * MCI_ParseOptArgs [internal]
1031 static DWORD MCI_ParseOptArgs(DWORD* data, int _offset, LPCWSTR lpCmd,
1032 LPWSTR args, LPDWORD dwFlags)
1034 int len, offset;
1035 const char* lmem;
1036 LPCWSTR str;
1037 DWORD dwRet, flg, cflg = 0;
1038 WORD eid;
1039 BOOL inCst, found;
1041 /* loop on arguments */
1042 while (*args) {
1043 lmem = (const char*)lpCmd;
1044 found = inCst = FALSE;
1045 offset = _offset;
1047 /* skip any leading white space(s) */
1048 while (*args == ' ') args++;
1049 TRACE("args=%s\n", debugstr_w(args));
1051 do { /* loop on options for command table for the requested verb */
1052 str = (LPCWSTR)lmem;
1053 lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1054 flg = *(const DWORD*)lmem;
1055 eid = *(const WORD*)(lmem + sizeof(DWORD));
1056 lmem += sizeof(DWORD) + sizeof(WORD);
1057 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1059 switch (eid) {
1060 case MCI_CONSTANT:
1061 inCst = TRUE; cflg = flg; break;
1062 case MCI_END_CONSTANT:
1063 /* there may be additional integral values after flag in constant */
1064 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1065 *dwFlags |= cflg;
1067 inCst = FALSE; cflg = 0;
1068 break;
1069 case MCI_RETURN:
1070 if (offset != _offset) {
1071 ERR("MCI_RETURN not in first position\n");
1072 return MCIERR_PARSER_INTERNAL;
1076 if (strncmpiW(args, str, len) == 0 &&
1077 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1078 /* store good values into data[] */
1079 args += len;
1080 while (*args == ' ') args++;
1081 found = TRUE;
1083 switch (eid) {
1084 case MCI_COMMAND_HEAD:
1085 case MCI_RETURN:
1086 case MCI_END_COMMAND:
1087 case MCI_END_COMMAND_LIST:
1088 case MCI_CONSTANT: /* done above */
1089 case MCI_END_CONSTANT: /* done above */
1090 break;
1091 case MCI_FLAG:
1092 *dwFlags |= flg;
1093 TRACE("flag=%08x\n", flg);
1094 break;
1095 case MCI_HWND:
1096 case MCI_HPAL:
1097 case MCI_HDC:
1098 case MCI_INTEGER:
1099 if (inCst) {
1100 data[offset] |= flg;
1101 *dwFlags |= cflg;
1102 inCst = FALSE;
1103 TRACE("flag=%08x constant=%08x\n", cflg, flg);
1104 } else {
1105 *dwFlags |= flg;
1106 if (!MCI_GetDWord(&(data[offset]), &args)) {
1107 return MCIERR_BAD_INTEGER;
1109 TRACE("flag=%08x int=%d\n", flg, data[offset]);
1111 break;
1112 case MCI_RECT:
1113 /* store rect in data (offset..offset+3) */
1114 *dwFlags |= flg;
1115 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1116 !MCI_GetDWord(&(data[offset+1]), &args) ||
1117 !MCI_GetDWord(&(data[offset+2]), &args) ||
1118 !MCI_GetDWord(&(data[offset+3]), &args)) {
1119 ERR("Bad rect %s\n", debugstr_w(args));
1120 return MCIERR_BAD_INTEGER;
1122 TRACE("flag=%08x for rectangle\n", flg);
1123 break;
1124 case MCI_STRING:
1125 *dwFlags |= flg;
1126 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1127 return dwRet;
1128 TRACE("flag=%08x string=%s\n", flg, debugstr_w(*(LPWSTR*)&data[offset]));
1129 break;
1130 default: ERR("oops\n");
1132 /* exit inside while loop, except if just entered in constant area definition */
1133 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1134 } else {
1135 /* have offset incremented if needed */
1136 switch (eid) {
1137 case MCI_COMMAND_HEAD:
1138 case MCI_RETURN:
1139 case MCI_END_COMMAND:
1140 case MCI_END_COMMAND_LIST:
1141 case MCI_CONSTANT:
1142 case MCI_FLAG: break;
1143 case MCI_HWND:
1144 case MCI_HPAL:
1145 case MCI_HDC: if (!inCst) offset += sizeof(HANDLE)/sizeof(DWORD); break;
1146 case MCI_INTEGER: if (!inCst) offset++; break;
1147 case MCI_END_CONSTANT: offset++; break;
1148 case MCI_STRING: offset += sizeof(LPWSTR)/sizeof(DWORD); break;
1149 case MCI_RECT: offset += 4; break;
1150 default: ERR("oops\n");
1153 } while (eid != MCI_END_COMMAND);
1154 if (!found) {
1155 WARN("Optarg %s not found\n", debugstr_w(args));
1156 return MCIERR_UNRECOGNIZED_COMMAND;
1158 if (offset == MCI_DATA_SIZE) {
1159 ERR("Internal data[] buffer overflow\n");
1160 return MCIERR_PARSER_INTERNAL;
1163 return 0;
1166 /**************************************************************************
1167 * MCI_HandleReturnValues [internal]
1169 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1170 MCI_GENERIC_PARMS *params, LPWSTR lpstrRet, UINT uRetLen)
1172 static const WCHAR fmt_d [] = {'%','d',0};
1173 static const WCHAR fmt_d4 [] = {'%','d',' ','%','d',' ','%','d',' ','%','d',0};
1174 static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1175 static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1177 if (lpstrRet) {
1178 switch (retType) {
1179 case 0: /* nothing to return */
1180 break;
1181 case MCI_INTEGER:
1183 DWORD data = *(DWORD *)(params + 1);
1184 switch (dwRet & 0xFFFF0000ul) {
1185 case 0:
1186 case MCI_INTEGER_RETURNED:
1187 snprintfW(lpstrRet, uRetLen, fmt_d, data);
1188 break;
1189 case MCI_RESOURCE_RETURNED:
1190 /* return string which ID is HIWORD(data),
1191 * string is loaded from mmsystem.dll */
1192 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1193 break;
1194 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1195 /* return string which ID is HIWORD(data),
1196 * string is loaded from driver */
1197 /* FIXME: this is wrong for a 16 bit handle */
1198 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1199 HIWORD(data), lpstrRet, uRetLen);
1200 break;
1201 case MCI_COLONIZED3_RETURN:
1202 snprintfW(lpstrRet, uRetLen, wszCol3,
1203 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1204 LOBYTE(HIWORD(data)));
1205 break;
1206 case MCI_COLONIZED4_RETURN:
1207 snprintfW(lpstrRet, uRetLen, wszCol4,
1208 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1209 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1210 break;
1211 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1213 break;
1215 #ifdef MCI_INTEGER64
1216 case MCI_INTEGER64:
1218 static const WCHAR fmt_ld [] = {'%','l','d',0};
1219 DWORD_PTR data = *(DWORD_PTR *)(params + 1);
1220 switch (dwRet & 0xFFFF0000ul) {
1221 case 0:
1222 case MCI_INTEGER_RETURNED:
1223 snprintfW(lpstrRet, uRetLen, fmt_ld, data);
1224 break;
1225 case MCI_RESOURCE_RETURNED:
1226 /* return string which ID is HIWORD(data),
1227 * string is loaded from mmsystem.dll */
1228 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1229 break;
1230 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1231 /* return string which ID is HIWORD(data),
1232 * string is loaded from driver */
1233 /* FIXME: this is wrong for a 16 bit handle */
1234 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1235 HIWORD(data), lpstrRet, uRetLen);
1236 break;
1237 case MCI_COLONIZED3_RETURN:
1238 snprintfW(lpstrRet, uRetLen, wszCol3,
1239 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1240 LOBYTE(HIWORD(data)));
1241 break;
1242 case MCI_COLONIZED4_RETURN:
1243 snprintfW(lpstrRet, uRetLen, wszCol4,
1244 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1245 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1246 break;
1247 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1249 break;
1251 #endif
1252 case MCI_STRING:
1253 switch (dwRet & 0xFFFF0000ul) {
1254 case 0:
1255 /* nothing to do data[0] == lpstrRet */
1256 break;
1257 case MCI_INTEGER_RETURNED:
1259 DWORD *data = (DWORD *)(params + 1);
1260 *data = *(LPDWORD)lpstrRet;
1261 snprintfW(lpstrRet, uRetLen, fmt_d, *data);
1262 break;
1264 default:
1265 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1266 break;
1268 break;
1269 case MCI_RECT:
1271 DWORD *data = (DWORD *)(params + 1);
1272 if (dwRet & 0xFFFF0000ul)
1273 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1274 snprintfW(lpstrRet, uRetLen, fmt_d4, data[0], data[1], data[2], data[3]);
1275 break;
1277 default: ERR("oops\n");
1280 return LOWORD(dwRet);
1283 /**************************************************************************
1284 * mciSendStringW [WINMM.@]
1286 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1287 UINT uRetLen, HWND hwndCallback)
1289 LPWSTR verb, dev, args;
1290 LPWINE_MCIDRIVER wmd = 0;
1291 MCIDEVICEID uDevID, auto_open = 0;
1292 DWORD dwFlags = 0, dwRet = 0;
1293 int offset = 0;
1294 DWORD retType;
1295 LPCWSTR lpCmd = 0;
1296 WORD wMsg = 0;
1297 static const WCHAR wszNew[] = {'n','e','w',0};
1298 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1299 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1300 union
1302 MCI_GENERIC_PARMS generic;
1303 MCI_OPEN_PARMSW open;
1304 MCI_SOUND_PARMSW sound;
1305 MCI_SYSINFO_PARMSW sysinfo;
1306 DWORD dw[MCI_DATA_SIZE];
1307 } data;
1309 TRACE("(%s, %p, %d, %p)\n",
1310 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1311 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1313 /* format is <command> <device> <optargs> */
1314 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1315 return MCIERR_OUT_OF_MEMORY;
1316 strcpyW( verb, lpstrCommand );
1317 CharLowerW(verb);
1319 memset(&data, 0, sizeof(data));
1321 if (!(args = strchrW(verb, ' '))) {
1322 dwRet = MCIERR_MISSING_DEVICE_NAME;
1323 goto errCleanUp;
1325 *args++ = '\0';
1326 if ((dwRet = MCI_GetString(&dev, &args))) {
1327 goto errCleanUp;
1329 uDevID = strcmpiW(dev, wszAll) ? 0 : MCI_ALL_DEVICE_ID;
1331 /* Determine devType from open */
1332 if (!strcmpW(verb, wszOpen)) {
1333 LPWSTR devType, tmp;
1334 WCHAR buf[128];
1336 /* case dev == 'new' has to be handled */
1337 if (!strcmpW(dev, wszNew)) {
1338 dev = 0;
1339 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1340 devType += 5;
1341 tmp = strchrW(devType, ' ');
1342 if (tmp) *tmp = '\0';
1343 devType = str_dup_upper(devType);
1344 if (tmp) *tmp = ' ';
1345 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1346 } else {
1347 WARN("open new requires device type\n");
1348 dwRet = MCIERR_MISSING_DEVICE_NAME;
1349 goto errCleanUp;
1351 } else if ((devType = strchrW(dev, '!')) != NULL) {
1352 *devType++ = '\0';
1353 tmp = devType; devType = dev; dev = tmp;
1355 dwFlags |= MCI_OPEN_TYPE;
1356 data.open.lpstrDeviceType = devType;
1357 devType = str_dup_upper(devType);
1358 dwFlags |= MCI_OPEN_ELEMENT;
1359 data.open.lpstrElementName = dev;
1360 } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1361 /* this is the name of a mci driver's type */
1362 tmp = strchrW(dev, ' ');
1363 if (tmp) *tmp = '\0';
1364 data.open.lpstrDeviceType = dev;
1365 devType = str_dup_upper(dev);
1366 if (tmp) *tmp = ' ';
1367 dwFlags |= MCI_OPEN_TYPE;
1368 } else {
1369 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1370 devType += 5;
1371 tmp = strchrW(devType, ' ');
1372 if (tmp) *tmp = '\0';
1373 devType = str_dup_upper(devType);
1374 if (tmp) *tmp = ' ';
1375 /* dwFlags and lpstrDeviceType will be correctly set in ParseOpt loop */
1376 } else {
1377 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1378 goto errCleanUp;
1380 devType = str_dup_upper(buf);
1382 dwFlags |= MCI_OPEN_ELEMENT;
1383 data.open.lpstrElementName = dev;
1385 if (MCI_ALL_DEVICE_ID == uDevID) {
1386 dwRet = MCIERR_CANNOT_USE_ALL;
1387 goto errCleanUp;
1389 if (!strstrW(args, wszSAliasS) && !dev) {
1390 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1391 goto errCleanUp;
1394 dwRet = MCI_LoadMciDriver(devType, &wmd);
1395 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1396 dwRet = MCIERR_INVALID_DEVICE_NAME;
1397 HeapFree(GetProcessHeap(), 0, devType);
1398 if (dwRet)
1399 goto errCleanUp;
1400 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))
1401 && (lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb))) {
1402 /* auto-open uses the core command table */
1403 switch (MCI_GetMessage(lpCmd)) {
1404 case MCI_SOUND: /* command does not use a device name */
1405 case MCI_SYSINFO:
1406 break;
1407 case MCI_CLOSE: /* don't auto-open for close */
1408 case MCI_BREAK: /* no auto-open for system commands */
1409 dwRet = MCIERR_INVALID_DEVICE_NAME;
1410 goto errCleanUp;
1411 break;
1412 default:
1414 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1415 WCHAR buf[138], retbuf[6];
1416 snprintfW(buf, sizeof(buf)/sizeof(WCHAR), wszOpenWait, dev);
1417 /* open via mciSendString handles quoting, dev!file syntax and alias creation */
1418 if ((dwRet = mciSendStringW(buf, retbuf, sizeof(retbuf)/sizeof(WCHAR), 0)) != 0)
1419 goto errCleanUp;
1420 auto_open = strtoulW(retbuf, NULL, 10);
1421 TRACE("auto-opened %u for %s\n", auto_open, debugstr_w(dev));
1423 /* FIXME: test for notify flag (how to preparse?) before opening */
1424 wmd = MCI_GetDriver(auto_open);
1425 if (!wmd) {
1426 ERR("No auto-open device %u\n", auto_open);
1427 dwRet = MCIERR_INVALID_DEVICE_ID;
1428 goto errCleanUp;
1434 /* get the verb in the different command tables */
1435 if (wmd) {
1436 /* try the device specific command table */
1437 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1438 if (!lpCmd) {
1439 /* try the type specific command table */
1440 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1441 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1442 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1443 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1446 /* try core command table */
1447 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1449 if (!lpCmd) {
1450 TRACE("Command %s not found!\n", debugstr_w(verb));
1451 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1452 goto errCleanUp;
1454 wMsg = MCI_GetMessage(lpCmd);
1456 /* set return information */
1457 offset = sizeof(data.generic);
1458 switch (retType = MCI_GetReturnType(lpCmd)) {
1459 case 0:
1460 break;
1461 case MCI_INTEGER:
1462 offset += sizeof(DWORD);
1463 break;
1464 case MCI_STRING:
1465 data.sysinfo.lpstrReturn = lpstrRet;
1466 data.sysinfo.dwRetSize = uRetLen;
1467 offset = FIELD_OFFSET( MCI_SYSINFO_PARMSW, dwNumber );
1468 break;
1469 case MCI_RECT:
1470 offset += 4 * sizeof(DWORD);
1471 break;
1472 #ifdef MCI_INTEGER64
1473 case MCI_INTEGER64:
1474 offset += sizeof(DWORD_PTR);
1475 break;
1476 #endif
1477 default:
1478 ERR("oops\n");
1481 TRACE("verb=%s on dev=%s; offset=%d\n",
1482 debugstr_w(verb), debugstr_w(dev), offset);
1484 if ((dwRet = MCI_ParseOptArgs(data.dw, offset / sizeof(DWORD), lpCmd, args, &dwFlags)))
1485 goto errCleanUp;
1487 /* set up call back */
1488 if (auto_open) {
1489 if (dwFlags & MCI_NOTIFY) {
1490 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1491 goto errCleanUp;
1493 /* FIXME: the command should get its own notification window set up and
1494 * ask for device closing while processing the notification mechanism.
1495 * hwndCallback = ...
1496 * dwFlags |= MCI_NOTIFY;
1497 * In the meantime special-case all commands but PLAY and RECORD below. */
1499 if (dwFlags & MCI_NOTIFY) {
1500 data.generic.dwCallback = (DWORD_PTR)hwndCallback;
1503 switch (wMsg) {
1504 case MCI_OPEN:
1505 if (strcmpW(verb, wszOpen)) {
1506 FIXME("Cannot open with command %s\n", debugstr_w(verb));
1507 dwRet = MCIERR_INTERNAL;
1508 wMsg = 0;
1509 goto errCleanUp;
1511 break;
1512 case MCI_SYSINFO:
1513 /* Requirements on dev depend on the flags:
1514 * alias with INSTALLNAME, name like "digitalvideo"
1515 * with QUANTITY and NAME. */
1517 data.sysinfo.wDeviceType = MCI_ALL_DEVICE_ID;
1518 if (uDevID != MCI_ALL_DEVICE_ID) {
1519 if (dwFlags & MCI_SYSINFO_INSTALLNAME)
1520 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1521 else if (!(data.sysinfo.wDeviceType = MCI_GetDevTypeFromResource(dev))) {
1522 dwRet = MCIERR_DEVICE_TYPE_REQUIRED;
1523 goto errCleanUp;
1527 break;
1528 case MCI_SOUND:
1529 /* FIXME: name is optional, "sound" is a valid command.
1530 * FIXME: Parse "sound notify" as flag, not as name. */
1531 data.sound.lpstrSoundName = dev;
1532 dwFlags |= MCI_SOUND_NAME;
1533 break;
1536 TRACE("[%d, %s, %08x, %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x]\n",
1537 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(wMsg), dwFlags,
1538 data.dw[0], data.dw[1], data.dw[2], data.dw[3], data.dw[4],
1539 data.dw[5], data.dw[6], data.dw[7], data.dw[8], data.dw[9]);
1541 if (wMsg == MCI_OPEN) {
1542 if ((dwRet = MCI_FinishOpen(wmd, &data.open, dwFlags)))
1543 goto errCleanUp;
1544 /* FIXME: notification is not properly shared across two opens */
1545 } else {
1546 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, wMsg, dwFlags, (DWORD_PTR)&data);
1548 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1549 if (!LOWORD(dwRet)) {
1550 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, &data.generic, lpstrRet, uRetLen);
1551 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1554 errCleanUp:
1555 if (auto_open) {
1556 /* PLAY and RECORD are the only known non-immediate commands */
1557 if (LOWORD(dwRet) || !(wMsg == MCI_PLAY || wMsg == MCI_RECORD))
1558 MCI_SendCommand(auto_open, MCI_CLOSE, 0, 0);
1559 else
1560 FIXME("leaking auto-open device %u\n", auto_open);
1562 if (wMsg == MCI_OPEN && LOWORD(dwRet) && wmd)
1563 MCI_UnLoadMciDriver(wmd);
1564 HeapFree(GetProcessHeap(), 0, verb);
1565 return dwRet;
1568 /**************************************************************************
1569 * mciSendStringA [WINMM.@]
1571 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1572 UINT uRetLen, HWND hwndCallback)
1574 LPWSTR lpwstrCommand;
1575 LPWSTR lpwstrRet = NULL;
1576 UINT ret;
1577 INT len;
1579 /* FIXME: is there something to do with lpstrReturnString ? */
1580 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1581 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1582 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1583 if (lpstrRet)
1585 if (uRetLen) *lpstrRet = '\0'; /* NT-w2k3 use memset(lpstrRet, 0, uRetLen); */
1586 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1587 if (!lpwstrRet) {
1588 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1589 return MCIERR_OUT_OF_MEMORY;
1592 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1593 if (!ret && lpwstrRet)
1594 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1595 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1596 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1597 return ret;
1600 /**************************************************************************
1601 * mciExecute [WINMM.@]
1603 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1605 char strRet[256];
1606 DWORD ret;
1608 TRACE("(%s)!\n", lpstrCommand);
1610 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1611 if (ret != 0) {
1612 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1613 sprintf(strRet, "Unknown MCI error (%d)", ret);
1615 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1617 /* FIXME: what shall I return ? */
1618 return TRUE;
1621 /**************************************************************************
1622 * mciLoadCommandResource [WINMM.@]
1624 * Strangely, this function only exists as a UNICODE one.
1626 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1628 UINT ret = MCI_NO_COMMAND_TABLE;
1629 HRSRC hRsrc = 0;
1630 HGLOBAL hMem;
1632 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1634 /* if a file named "resname.mci" exits, then load resource "resname" from it
1635 * otherwise directly from driver
1636 * We don't support it (who uses this feature ?), but we check anyway
1638 if (!type) {
1639 #if 0
1640 /* FIXME: we should put this back into order, but I never found a program
1641 * actually using this feature, so we may not need it
1643 char buf[128];
1644 OFSTRUCT ofs;
1646 strcat(strcpy(buf, resname), ".mci");
1647 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1648 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1650 #endif
1652 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1653 (hMem = LoadResource(hInst, hRsrc))) {
1654 ret = MCI_SetCommandTable(hMem, type);
1655 FreeResource(hMem);
1657 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1659 TRACE("=> %04x\n", ret);
1660 return ret;
1663 /**************************************************************************
1664 * mciFreeCommandResource [WINMM.@]
1666 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1668 TRACE("(%08x)!\n", uTable);
1670 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1671 return FALSE;
1673 FreeResource(S_MciCmdTable[uTable].hMem);
1674 S_MciCmdTable[uTable].hMem = NULL;
1675 S_MciCmdTable[uTable].lpTable = NULL;
1676 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1677 S_MciCmdTable[uTable].aVerbs = 0;
1678 S_MciCmdTable[uTable].nVerbs = 0;
1679 return TRUE;
1682 /**************************************************************************
1683 * MCI_Open [internal]
1685 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1687 WCHAR strDevTyp[128];
1688 DWORD dwRet;
1689 LPWINE_MCIDRIVER wmd = NULL;
1691 TRACE("(%08X, %p)\n", dwParam, lpParms);
1692 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1694 /* only two low bytes are generic, the other ones are dev type specific */
1695 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1696 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1697 MCI_NOTIFY|MCI_WAIT)
1698 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0)
1699 FIXME("Unsupported yet dwFlags=%08X\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1700 #undef WINE_MCIDRIVER_SUPP
1702 strDevTyp[0] = 0;
1704 if (dwParam & MCI_OPEN_TYPE) {
1705 if (dwParam & MCI_OPEN_TYPE_ID) {
1706 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1708 if (uDevType < MCI_DEVTYPE_FIRST ||
1709 uDevType > MCI_DEVTYPE_LAST ||
1710 !LoadStringW(hWinMM32Instance, uDevType,
1711 strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1712 dwRet = MCIERR_BAD_INTEGER;
1713 goto errCleanUp;
1715 } else {
1716 LPWSTR ptr;
1717 if (lpParms->lpstrDeviceType == NULL) {
1718 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1719 goto errCleanUp;
1721 strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1722 ptr = strchrW(strDevTyp, '!');
1723 if (ptr) {
1724 /* this behavior is not documented in windows. However, since, in
1725 * some occasions, MCI_OPEN handling is translated by WinMM into
1726 * a call to mciSendString("open <type>"); this code shall be correct
1728 if (dwParam & MCI_OPEN_ELEMENT) {
1729 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1730 debugstr_w(lpParms->lpstrElementName),
1731 debugstr_w(strDevTyp));
1732 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1733 goto errCleanUp;
1735 dwParam |= MCI_OPEN_ELEMENT;
1736 *ptr++ = 0;
1737 /* FIXME: not a good idea to write in user supplied buffer */
1738 lpParms->lpstrElementName = ptr;
1742 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1745 if (dwParam & MCI_OPEN_ELEMENT) {
1746 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1748 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1749 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1750 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1751 goto errCleanUp;
1754 if (!lpParms->lpstrElementName) {
1755 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1756 goto errCleanUp;
1759 /* type, if given as a parameter, supersedes file extension */
1760 if (!strDevTyp[0] &&
1761 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1762 strDevTyp, sizeof(strDevTyp))) {
1763 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1764 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1765 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1766 goto errCleanUp;
1768 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1769 strcpyW(strDevTyp, wszCdAudio);
1773 if (strDevTyp[0] == 0) {
1774 FIXME("Couldn't load driver\n");
1775 dwRet = MCIERR_INVALID_DEVICE_NAME;
1776 goto errCleanUp;
1779 if (dwParam & MCI_OPEN_ALIAS) {
1780 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1781 if (!lpParms->lpstrAlias) {
1782 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1783 goto errCleanUp;
1787 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1788 goto errCleanUp;
1791 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1792 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1793 /* FIXME: is dwRet the correct ret code ? */
1794 goto errCleanUp;
1797 /* only handled devices fall through */
1798 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1799 return 0;
1801 errCleanUp:
1802 if (wmd) MCI_UnLoadMciDriver(wmd);
1803 return dwRet;
1806 /**************************************************************************
1807 * MCI_Close [internal]
1809 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1811 DWORD dwRet;
1812 LPWINE_MCIDRIVER wmd;
1814 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1816 /* Every device must handle MCI_NOTIFY on its own. */
1817 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1818 while (MciDrivers) {
1819 /* Retrieve the device ID under lock, but send the message without,
1820 * the driver might be calling some winmm functions from another
1821 * thread before being fully stopped.
1823 EnterCriticalSection(&WINMM_cs);
1824 if (!MciDrivers)
1826 LeaveCriticalSection(&WINMM_cs);
1827 break;
1829 wDevID = MciDrivers->wDeviceID;
1830 LeaveCriticalSection(&WINMM_cs);
1831 MCI_Close(wDevID, dwParam, lpParms);
1833 return 0;
1836 if (!(wmd = MCI_GetDriver(wDevID))) {
1837 return MCIERR_INVALID_DEVICE_ID;
1840 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1842 MCI_UnLoadMciDriver(wmd);
1844 return dwRet;
1847 /**************************************************************************
1848 * MCI_WriteString [internal]
1850 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1852 DWORD ret = 0;
1854 if (lpSrcStr) {
1855 if (dstSize <= strlenW(lpSrcStr)) {
1856 ret = MCIERR_PARAM_OVERFLOW;
1857 } else {
1858 strcpyW(lpDstStr, lpSrcStr);
1860 } else {
1861 *lpDstStr = 0;
1863 return ret;
1866 /**************************************************************************
1867 * MCI_Sysinfo [internal]
1869 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1871 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1872 WCHAR buf[2048], *s, *p;
1873 LPWINE_MCIDRIVER wmd;
1874 HKEY hKey;
1876 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1877 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1879 TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1880 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1881 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1882 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1884 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1885 case MCI_SYSINFO_QUANTITY:
1886 if (lpParms->dwRetSize < sizeof(DWORD))
1887 return MCIERR_PARAM_OVERFLOW;
1888 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1889 if (uDevID == MCI_ALL_DEVICE_ID) {
1890 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1891 if (dwFlags & MCI_SYSINFO_OPEN) {
1892 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1893 EnterCriticalSection(&WINMM_cs);
1894 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1895 cnt++;
1897 LeaveCriticalSection(&WINMM_cs);
1898 } else {
1899 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1900 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1901 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1902 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1903 RegCloseKey( hKey );
1905 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1906 for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1908 } else {
1909 if (dwFlags & MCI_SYSINFO_OPEN) {
1910 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1911 EnterCriticalSection(&WINMM_cs);
1912 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1913 if (wmd->wType == lpParms->wDeviceType) cnt++;
1915 LeaveCriticalSection(&WINMM_cs);
1916 } else {
1917 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1918 FIXME("Don't know how to get # of MCI devices of a given type\n");
1919 /* name = LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType))
1920 * then lookup registry and/or system.ini for name, ignoring digits suffix */
1921 switch (LOWORD(lpParms->wDeviceType)) {
1922 case MCI_DEVTYPE_CD_AUDIO:
1923 case MCI_DEVTYPE_WAVEFORM_AUDIO:
1924 case MCI_DEVTYPE_SEQUENCER:
1925 cnt = 1;
1926 break;
1927 default: /* "digitalvideo" gets 0 because it's not in the registry */
1928 cnt = 0;
1932 *(DWORD*)lpParms->lpstrReturn = cnt;
1933 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1934 ret = MCI_INTEGER_RETURNED;
1935 /* return ret; Only Win9x sends a notification in this case. */
1936 break;
1937 case MCI_SYSINFO_INSTALLNAME:
1938 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1939 if ((wmd = MCI_GetDriver(uDevID))) {
1940 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1941 wmd->lpstrDeviceType);
1942 } else {
1943 ret = (uDevID == MCI_ALL_DEVICE_ID)
1944 ? MCIERR_CANNOT_USE_ALL : MCIERR_INVALID_DEVICE_NAME;
1946 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1947 break;
1948 case MCI_SYSINFO_NAME:
1949 s = NULL;
1950 if (dwFlags & MCI_SYSINFO_OPEN) {
1951 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1952 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1953 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1954 EnterCriticalSection(&WINMM_cs);
1955 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1956 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1957 if (uDevID == MCI_ALL_DEVICE_ID ||
1958 lpParms->wDeviceType == wmd->wType) {
1959 cnt++;
1960 if (cnt == lpParms->dwNumber) {
1961 s = wmd->lpstrAlias;
1962 break;
1966 LeaveCriticalSection(&WINMM_cs);
1967 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1968 } else if (MCI_ALL_DEVICE_ID == uDevID) {
1969 TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
1970 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1971 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1972 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1973 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1974 lpParms->dwNumber <= cnt) {
1975 DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
1976 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1977 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1978 s = buf;
1980 RegCloseKey( hKey );
1982 if (!s) {
1983 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1984 for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1985 TRACE("%d: %s\n", cnt, debugstr_w(p));
1986 if (cnt == lpParms->dwNumber - 1) {
1987 s = p;
1988 break;
1993 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1994 } else {
1995 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
1996 /* Cheating: what is asked for is the nth device from the registry. */
1997 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
1998 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
1999 ret = MCIERR_OUTOFRANGE;
2000 else {
2001 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
2002 lpParms->lpstrReturn, lpParms->dwRetSize);
2003 ret = 0;
2006 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
2007 break;
2008 default:
2009 TRACE("Unsupported flag value=%08x\n", dwFlags);
2010 ret = MCIERR_UNRECOGNIZED_KEYWORD;
2012 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
2013 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
2014 return ret;
2017 /**************************************************************************
2018 * MCI_Break [internal]
2020 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
2022 DWORD dwRet = 0;
2024 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2025 FIXME("(%04x) vkey %04X stub\n", dwFlags, lpParms->nVirtKey);
2027 if (MMSYSERR_NOERROR==dwRet && (dwFlags & MCI_NOTIFY))
2028 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2029 return dwRet;
2032 /**************************************************************************
2033 * MCI_Sound [internal]
2035 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
2037 DWORD dwRet;
2039 if (dwFlags & MCI_SOUND_NAME) {
2040 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2041 else dwRet = PlaySoundW(lpParms->lpstrSoundName, NULL,
2042 SND_ALIAS | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2043 ? 0 : MCIERR_HARDWARE;
2044 } else dwRet = PlaySoundW((LPCWSTR)SND_ALIAS_SYSTEMDEFAULT, NULL,
2045 SND_ALIAS_ID | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2046 ? 0 : MCIERR_HARDWARE;
2048 if (!dwRet && lpParms && (dwFlags & MCI_NOTIFY))
2049 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2050 return dwRet;
2053 /**************************************************************************
2054 * MCI_SendCommand [internal]
2056 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2058 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
2060 switch (wMsg) {
2061 case MCI_OPEN:
2062 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
2063 break;
2064 case MCI_CLOSE:
2065 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
2066 break;
2067 case MCI_SYSINFO:
2068 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2069 break;
2070 case MCI_BREAK:
2071 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2072 break;
2073 case MCI_SOUND:
2074 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2075 break;
2076 default:
2077 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
2078 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2079 dwRet = MCIERR_CANNOT_USE_ALL;
2080 } else {
2081 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
2083 break;
2085 return dwRet;
2088 /**************************************************************************
2089 * MCI_CleanUp [internal]
2091 * Some MCI commands need to be cleaned-up (when not called from
2092 * mciSendString), because MCI drivers return extra information for string
2093 * transformation. This function gets rid of them.
2095 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
2097 if (LOWORD(dwRet))
2098 return LOWORD(dwRet);
2100 switch (wMsg) {
2101 case MCI_GETDEVCAPS:
2102 switch (dwRet & 0xFFFF0000ul) {
2103 case 0:
2104 case MCI_COLONIZED3_RETURN:
2105 case MCI_COLONIZED4_RETURN:
2106 case MCI_INTEGER_RETURNED:
2107 /* nothing to do */
2108 break;
2109 case MCI_RESOURCE_RETURNED:
2110 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2112 LPMCI_GETDEVCAPS_PARMS lmgp;
2114 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
2115 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2116 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2118 break;
2119 default:
2120 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2121 HIWORD(dwRet), MCI_MessageToString(wMsg));
2123 break;
2124 case MCI_STATUS:
2125 switch (dwRet & 0xFFFF0000ul) {
2126 case 0:
2127 case MCI_COLONIZED3_RETURN:
2128 case MCI_COLONIZED4_RETURN:
2129 case MCI_INTEGER_RETURNED:
2130 /* nothing to do */
2131 break;
2132 case MCI_RESOURCE_RETURNED:
2133 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2135 LPMCI_STATUS_PARMS lsp;
2137 lsp = (LPMCI_STATUS_PARMS)dwParam2;
2138 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2139 lsp->dwReturn = LOWORD(lsp->dwReturn);
2141 break;
2142 default:
2143 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2144 HIWORD(dwRet), MCI_MessageToString(wMsg));
2146 break;
2147 case MCI_SYSINFO:
2148 switch (dwRet & 0xFFFF0000ul) {
2149 case 0:
2150 case MCI_INTEGER_RETURNED:
2151 /* nothing to do */
2152 break;
2153 default:
2154 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2156 break;
2157 default:
2158 if (HIWORD(dwRet)) {
2159 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2160 dwRet, MCI_MessageToString(wMsg));
2162 break;
2164 return LOWORD(dwRet);
2167 /**************************************************************************
2168 * mciGetErrorStringW [WINMM.@]
2170 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2172 BOOL ret = FALSE;
2174 if (lpstrBuffer != NULL && uLength > 0 &&
2175 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2177 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2178 ret = TRUE;
2181 return ret;
2184 /**************************************************************************
2185 * mciGetErrorStringA [WINMM.@]
2187 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2189 BOOL ret = FALSE;
2191 if (lpstrBuffer != NULL && uLength > 0 &&
2192 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2194 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2195 ret = TRUE;
2198 return ret;
2201 /**************************************************************************
2202 * mciDriverNotify [WINMM.@]
2204 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2206 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2208 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2211 /**************************************************************************
2212 * mciGetDriverData [WINMM.@]
2214 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2216 LPWINE_MCIDRIVER wmd;
2218 TRACE("(%04x)\n", uDeviceID);
2220 wmd = MCI_GetDriver(uDeviceID);
2222 if (!wmd) {
2223 WARN("Bad uDeviceID\n");
2224 return 0L;
2227 return wmd->dwPrivate;
2230 /**************************************************************************
2231 * mciSetDriverData [WINMM.@]
2233 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2235 LPWINE_MCIDRIVER wmd;
2237 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2239 wmd = MCI_GetDriver(uDeviceID);
2241 if (!wmd) {
2242 WARN("Bad uDeviceID\n");
2243 return FALSE;
2246 wmd->dwPrivate = data;
2247 return TRUE;
2250 /**************************************************************************
2251 * mciSendCommandW [WINMM.@]
2254 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2256 DWORD dwRet;
2258 TRACE("(%08x, %s, %08lx, %08lx)\n",
2259 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2261 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2262 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2263 TRACE("=> %08x\n", dwRet);
2264 return dwRet;
2267 /**************************************************************************
2268 * mciSendCommandA [WINMM.@]
2270 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2272 DWORD ret;
2273 int mapped;
2275 TRACE("(%08x, %s, %08lx, %08lx)\n",
2276 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2278 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2279 if (mapped == -1)
2281 FIXME("message %04x mapping failed\n", wMsg);
2282 return MCIERR_OUT_OF_MEMORY;
2284 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2285 if (mapped)
2286 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2287 return ret;
2290 /**************************************************************************
2291 * mciGetDeviceIDA [WINMM.@]
2293 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2295 LPWSTR w = MCI_strdupAtoW(lpstrName);
2296 UINT ret = MCIERR_OUT_OF_MEMORY;
2298 if (w)
2300 ret = mciGetDeviceIDW(w);
2301 HeapFree(GetProcessHeap(), 0, w);
2303 return ret;
2306 /**************************************************************************
2307 * mciGetDeviceIDW [WINMM.@]
2309 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2311 return MCI_GetDriverFromString(lpwstrName);
2314 /**************************************************************************
2315 * MCI_DefYieldProc [internal]
2317 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2319 INT16 ret;
2320 MSG msg;
2322 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2324 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2325 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2326 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2327 ret = 0;
2328 } else {
2329 msg.hwnd = HWND_32(HIWORD(data));
2330 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2331 ret = -1;
2333 return ret;
2336 /**************************************************************************
2337 * mciSetYieldProc [WINMM.@]
2339 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2341 LPWINE_MCIDRIVER wmd;
2343 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2345 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2346 WARN("Bad uDeviceID\n");
2347 return FALSE;
2350 wmd->lpfnYieldProc = fpYieldProc;
2351 wmd->dwYieldData = dwYieldData;
2353 return TRUE;
2356 /**************************************************************************
2357 * mciGetDeviceIDFromElementIDA [WINMM.@]
2359 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2361 LPWSTR w = MCI_strdupAtoW(lpstrType);
2362 UINT ret = 0;
2364 if (w)
2366 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2367 HeapFree(GetProcessHeap(), 0, w);
2369 return ret;
2372 /**************************************************************************
2373 * mciGetDeviceIDFromElementIDW [WINMM.@]
2375 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2377 /* FIXME: that's rather strange, there is no
2378 * mciGetDeviceIDFromElementID32A in winmm.spec
2380 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2381 return 0;
2384 /**************************************************************************
2385 * mciGetYieldProc [WINMM.@]
2387 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2389 LPWINE_MCIDRIVER wmd;
2391 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2393 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2394 WARN("Bad uDeviceID\n");
2395 return NULL;
2397 if (!wmd->lpfnYieldProc) {
2398 WARN("No proc set\n");
2399 return NULL;
2401 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2402 return wmd->lpfnYieldProc;
2405 /**************************************************************************
2406 * mciGetCreatorTask [WINMM.@]
2408 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2410 LPWINE_MCIDRIVER wmd;
2411 HTASK ret = 0;
2413 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2415 TRACE("(%u) => %p\n", uDeviceID, ret);
2416 return ret;
2419 /**************************************************************************
2420 * mciDriverYield [WINMM.@]
2422 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2424 LPWINE_MCIDRIVER wmd;
2425 UINT ret = 0;
2427 TRACE("(%04x)\n", uDeviceID);
2429 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2430 MSG msg;
2431 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2432 } else {
2433 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2436 return ret;