msscript.ocx: Keep script host running as long as any script module is alive.
[wine.git] / dlls / winmm / mci.c
blob870f5d1ca244cd2af1e42460f197e6b10717a5d8
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 <stdlib.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "wingdi.h"
48 #include "mmsystem.h"
49 #include "winuser.h"
50 #include "winnls.h"
51 #include "winreg.h"
52 #include "wownt32.h"
54 #include "digitalv.h"
55 #include "winemm.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(mci);
61 /* First MCI valid device ID (0 means error) */
62 #define MCI_MAGIC 0x0001
64 /* MCI settings */
65 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};
66 static const WCHAR wszNull [] = {0};
67 static const WCHAR wszAll [] = {'A','L','L',0};
68 static const WCHAR wszMci [] = {'M','C','I',0};
69 static const WCHAR wszOpen [] = {'o','p','e','n',0};
70 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
72 static WINE_MCIDRIVER *MciDrivers;
74 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
75 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
77 /* dup a string and uppercase it */
78 static inline LPWSTR str_dup_upper( LPCWSTR str )
80 INT len = (lstrlenW(str) + 1) * sizeof(WCHAR);
81 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
82 if (p)
84 memcpy( p, str, len );
85 CharUpperW( p );
87 return p;
90 /**************************************************************************
91 * MCI_GetDriver [internal]
93 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
95 LPWINE_MCIDRIVER wmd = 0;
97 EnterCriticalSection(&WINMM_cs);
98 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
99 if (wmd->wDeviceID == wDevID)
100 break;
102 LeaveCriticalSection(&WINMM_cs);
103 return wmd;
106 /**************************************************************************
107 * MCI_GetDriverFromString [internal]
109 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
111 LPWINE_MCIDRIVER wmd;
112 UINT ret = 0;
114 if (!lpstrName)
115 return 0;
117 if (!wcsicmp(lpstrName, wszAll))
118 return MCI_ALL_DEVICE_ID;
120 EnterCriticalSection(&WINMM_cs);
121 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
122 if (wmd->lpstrAlias && wcsicmp(wmd->lpstrAlias, lpstrName) == 0) {
123 ret = wmd->wDeviceID;
124 break;
127 LeaveCriticalSection(&WINMM_cs);
129 return ret;
132 /**************************************************************************
133 * MCI_MessageToString [internal]
135 static const char* MCI_MessageToString(UINT wMsg)
137 #define CASE(s) case (s): return #s
139 switch (wMsg) {
140 CASE(DRV_LOAD);
141 CASE(DRV_ENABLE);
142 CASE(DRV_OPEN);
143 CASE(DRV_CLOSE);
144 CASE(DRV_DISABLE);
145 CASE(DRV_FREE);
146 CASE(DRV_CONFIGURE);
147 CASE(DRV_QUERYCONFIGURE);
148 CASE(DRV_INSTALL);
149 CASE(DRV_REMOVE);
150 CASE(DRV_EXITSESSION);
151 CASE(DRV_EXITAPPLICATION);
152 CASE(DRV_POWER);
153 CASE(MCI_BREAK);
154 CASE(MCI_CLOSE);
155 CASE(MCI_CLOSE_DRIVER);
156 CASE(MCI_COPY);
157 CASE(MCI_CUE);
158 CASE(MCI_CUT);
159 CASE(MCI_DELETE);
160 CASE(MCI_ESCAPE);
161 CASE(MCI_FREEZE);
162 CASE(MCI_PAUSE);
163 CASE(MCI_PLAY);
164 CASE(MCI_GETDEVCAPS);
165 CASE(MCI_INFO);
166 CASE(MCI_LOAD);
167 CASE(MCI_OPEN);
168 CASE(MCI_OPEN_DRIVER);
169 CASE(MCI_PASTE);
170 CASE(MCI_PUT);
171 CASE(MCI_REALIZE);
172 CASE(MCI_RECORD);
173 CASE(MCI_RESUME);
174 CASE(MCI_SAVE);
175 CASE(MCI_SEEK);
176 CASE(MCI_SET);
177 CASE(MCI_SOUND);
178 CASE(MCI_SPIN);
179 CASE(MCI_STATUS);
180 CASE(MCI_STEP);
181 CASE(MCI_STOP);
182 CASE(MCI_SYSINFO);
183 CASE(MCI_UNFREEZE);
184 CASE(MCI_UPDATE);
185 CASE(MCI_WHERE);
186 CASE(MCI_WINDOW);
187 /* constants for digital video */
188 CASE(MCI_CAPTURE);
189 CASE(MCI_MONITOR);
190 CASE(MCI_RESERVE);
191 CASE(MCI_SETAUDIO);
192 CASE(MCI_SIGNAL);
193 CASE(MCI_SETVIDEO);
194 CASE(MCI_QUALITY);
195 CASE(MCI_LIST);
196 CASE(MCI_UNDO);
197 CASE(MCI_CONFIGURE);
198 CASE(MCI_RESTORE);
199 #undef CASE
200 default:
201 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
205 static LPWSTR MCI_strdupAtoW( LPCSTR str )
207 LPWSTR ret;
208 INT len;
210 if (!str) return NULL;
211 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
212 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
213 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
214 return ret;
217 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
219 if (msg < DRV_RESERVED) return 0;
221 switch (msg)
223 case MCI_CLOSE:
224 case MCI_CONFIGURE:
225 case MCI_PLAY:
226 case MCI_SEEK:
227 case MCI_STOP:
228 case MCI_PAUSE:
229 case MCI_GETDEVCAPS:
230 case MCI_SPIN:
231 case MCI_SET:
232 case MCI_STEP:
233 case MCI_RECORD:
234 case MCI_BREAK:
235 case MCI_STATUS:
236 case MCI_CUE:
237 case MCI_REALIZE:
238 case MCI_PUT:
239 case MCI_WHERE:
240 case MCI_FREEZE:
241 case MCI_UNFREEZE:
242 case MCI_CUT:
243 case MCI_COPY:
244 case MCI_PASTE:
245 case MCI_UPDATE:
246 case MCI_RESUME:
247 case MCI_DELETE:
248 case MCI_MONITOR:
249 case MCI_SIGNAL:
250 case MCI_UNDO:
251 return 0;
253 case MCI_OPEN:
254 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
255 * structure, larger than MCI_WAVE_OPEN_PARMS */
256 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
257 MCI_ANIM_OPEN_PARMSW *mci_openW;
258 DWORD_PTR *ptr;
260 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW));
261 if (!ptr) return -1;
263 *ptr++ = *dwParam2; /* save the previous pointer */
264 *dwParam2 = (DWORD_PTR)ptr;
265 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
267 if (dwParam1 & MCI_NOTIFY)
268 mci_openW->dwCallback = mci_openA->dwCallback;
270 if (dwParam1 & MCI_OPEN_TYPE)
272 if (dwParam1 & MCI_OPEN_TYPE_ID)
273 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
274 else
275 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
277 if (dwParam1 & MCI_OPEN_ELEMENT)
279 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
280 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
281 else
282 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
284 if (dwParam1 & MCI_OPEN_ALIAS)
285 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
286 /* We don't know how many DWORD follow, as
287 * the structure depends on the device. */
288 if (HIWORD(dwParam1))
289 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
291 return 1;
293 case MCI_WINDOW:
294 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
296 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
297 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
299 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
300 if (!mci_windowW) return -1;
302 *dwParam2 = (DWORD_PTR)mci_windowW;
304 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
306 if (dwParam1 & MCI_NOTIFY)
307 mci_windowW->dwCallback = mci_windowA->dwCallback;
308 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
309 mci_windowW->hWnd = mci_windowA->hWnd;
310 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
311 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
313 return 1;
315 return 0;
317 case MCI_SYSINFO:
318 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
320 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
321 MCI_SYSINFO_PARMSW *mci_sysinfoW;
322 DWORD_PTR *ptr;
324 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
325 if (!ptr) return -1;
327 *ptr++ = *dwParam2; /* save the previous pointer */
328 *dwParam2 = (DWORD_PTR)ptr;
329 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
331 if (dwParam1 & MCI_NOTIFY)
332 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
334 /* Size is measured in numbers of characters, despite what MSDN says. */
335 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
336 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize * sizeof(WCHAR));
337 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
338 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
339 return 1;
341 return 0;
342 case MCI_INFO:
344 MCI_DGV_INFO_PARMSA *mci_infoA = (MCI_DGV_INFO_PARMSA *)*dwParam2;
345 MCI_DGV_INFO_PARMSW *mci_infoW;
346 DWORD_PTR *ptr;
348 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
349 if (!ptr) return -1;
351 *ptr++ = *dwParam2; /* save the previous pointer */
352 *dwParam2 = (DWORD_PTR)ptr;
353 mci_infoW = (MCI_DGV_INFO_PARMSW *)ptr;
355 if (dwParam1 & MCI_NOTIFY)
356 mci_infoW->dwCallback = mci_infoA->dwCallback;
358 /* Size is measured in numbers of characters. */
359 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
360 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize * sizeof(WCHAR));
361 if (dwParam1 & MCI_DGV_INFO_ITEM)
362 mci_infoW->dwItem = mci_infoA->dwItem;
363 return 1;
365 case MCI_SAVE:
366 case MCI_LOAD:
367 case MCI_CAPTURE:
368 case MCI_RESTORE:
369 { /* All these commands have the same layout: callback + string + optional rect */
370 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
371 MCI_OVLY_LOAD_PARMSW *mci_loadW;
373 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
374 if (!mci_loadW) return -1;
376 *dwParam2 = (DWORD_PTR)mci_loadW;
377 if (dwParam1 & MCI_NOTIFY)
378 mci_loadW->dwCallback = mci_loadA->dwCallback;
379 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
380 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
381 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
382 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
383 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
384 mci_loadW->rc = mci_loadA->rc;
385 return 1;
387 case MCI_SOUND:
388 case MCI_ESCAPE:
389 { /* All these commands have the same layout: callback + string */
390 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
391 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
393 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
394 if (!mci_vd_escapeW) return -1;
396 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
397 if (dwParam1 & MCI_NOTIFY)
398 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
399 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
400 return 1;
402 case MCI_SETAUDIO:
403 case MCI_SETVIDEO:
404 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
405 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
406 return 0;
407 /* fall through to default */
408 case MCI_RESERVE:
409 case MCI_QUALITY:
410 case MCI_LIST:
411 default:
412 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
413 return 0; /* pass through untouched */
417 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
418 DWORD result)
420 switch (msg)
422 case MCI_OPEN:
424 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
425 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
426 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
428 mci_openA->wDeviceID = mci_openW->wDeviceID;
430 if (dwParam1 & MCI_OPEN_TYPE)
432 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
433 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
435 if (dwParam1 & MCI_OPEN_ELEMENT)
437 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
438 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
440 if (dwParam1 & MCI_OPEN_ALIAS)
441 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
442 HeapFree(GetProcessHeap(), 0, ptr);
444 break;
445 case MCI_WINDOW:
446 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
448 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
450 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
451 HeapFree(GetProcessHeap(), 0, mci_windowW);
453 break;
455 case MCI_SYSINFO:
456 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
458 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
459 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
460 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
462 if (!result)
464 WideCharToMultiByte(CP_ACP, 0,
465 mci_sysinfoW->lpstrReturn, -1,
466 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
467 NULL, NULL);
470 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
471 HeapFree(GetProcessHeap(), 0, ptr);
473 break;
474 case MCI_INFO:
476 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
477 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
478 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
480 if (!result)
482 WideCharToMultiByte(CP_ACP, 0,
483 mci_infoW->lpstrReturn, -1,
484 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
485 NULL, NULL);
488 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
489 HeapFree(GetProcessHeap(), 0, ptr);
491 break;
492 case MCI_SAVE:
493 case MCI_LOAD:
494 case MCI_CAPTURE:
495 case MCI_RESTORE:
496 { /* All these commands have the same layout: callback + string + optional rect */
497 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
499 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
500 HeapFree(GetProcessHeap(), 0, mci_loadW);
502 break;
503 case MCI_SOUND:
504 case MCI_ESCAPE:
505 { /* All these commands have the same layout: callback + string */
506 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
508 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
509 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
511 break;
513 default:
514 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
515 break;
519 /**************************************************************************
520 * MCI_GetDevTypeFromFileName [internal]
522 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
524 LPCWSTR tmp;
525 HKEY hKey;
526 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
527 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
528 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
529 if ((tmp = wcsrchr(fileName, '.'))) {
530 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
531 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
532 DWORD dwLen = len;
533 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
534 RegCloseKey( hKey );
535 if (lRet == ERROR_SUCCESS) return 0;
537 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
539 return MCIERR_EXTENSION_NOT_FOUND;
542 /**************************************************************************
543 * MCI_GetDevTypeFromResource [internal]
545 static UINT MCI_GetDevTypeFromResource(LPCWSTR lpstrName)
547 WCHAR buf[32];
548 UINT uDevType;
549 for (uDevType = MCI_DEVTYPE_FIRST; uDevType <= MCI_DEVTYPE_LAST; uDevType++) {
550 if (LoadStringW(hWinMM32Instance, uDevType, buf, ARRAY_SIZE(buf))) {
551 /* FIXME: ignore digits suffix */
552 if (!wcsicmp(buf, lpstrName))
553 return uDevType;
556 return 0;
559 #define MAX_MCICMDTABLE 20
560 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
562 typedef struct tagWINE_MCICMDTABLE {
563 UINT uDevType;
564 HGLOBAL hMem;
565 const BYTE* lpTable;
566 UINT nVerbs; /* number of verbs in command table */
567 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
568 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
570 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
572 /**************************************************************************
573 * MCI_IsCommandTableValid [internal]
575 static BOOL MCI_IsCommandTableValid(UINT uTbl)
577 const BYTE* lmem;
578 LPCWSTR str;
579 DWORD flg;
580 WORD eid;
581 int idx = 0;
582 BOOL inCst = FALSE;
584 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
585 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
587 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
588 return FALSE;
590 lmem = S_MciCmdTable[uTbl].lpTable;
591 do {
592 str = (LPCWSTR)lmem;
593 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
594 flg = *(const DWORD*)lmem;
595 eid = *(const WORD*)(lmem + sizeof(DWORD));
596 lmem += sizeof(DWORD) + sizeof(WORD);
597 idx ++;
598 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
599 switch (eid) {
600 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
601 case MCI_STRING: if (inCst) return FALSE; break;
602 case MCI_HWND: /* Occurs inside MCI_CONSTANT as in "window handle default" */
603 case MCI_HPAL:
604 case MCI_HDC:
605 case MCI_INTEGER: if (!*str) return FALSE; break;
606 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
607 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
608 case MCI_FLAG: if (!*str) return FALSE; break;
609 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
610 case MCI_RECT: if (!*str || inCst) return FALSE; break;
611 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
612 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
613 default: return FALSE;
615 } while (eid != MCI_END_COMMAND_LIST);
616 return TRUE;
619 /**************************************************************************
620 * MCI_DumpCommandTable [internal]
622 static BOOL MCI_DumpCommandTable(UINT uTbl)
624 const BYTE* lmem;
625 LPCWSTR str;
626 WORD eid;
628 if (!MCI_IsCommandTableValid(uTbl)) {
629 ERR("Ooops: %d is not valid\n", uTbl);
630 return FALSE;
633 lmem = S_MciCmdTable[uTbl].lpTable;
634 do {
635 do {
636 /* DWORD flg; */
637 str = (LPCWSTR)lmem;
638 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
639 /* flg = *(const DWORD*)lmem; */
640 eid = *(const WORD*)(lmem + sizeof(DWORD));
641 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
642 lmem += sizeof(DWORD) + sizeof(WORD);
643 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
644 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
645 } while (eid != MCI_END_COMMAND_LIST);
646 return TRUE;
650 /**************************************************************************
651 * MCI_GetCommandTable [internal]
653 static UINT MCI_GetCommandTable(UINT uDevType)
655 UINT uTbl;
656 WCHAR buf[32];
657 LPCWSTR str = NULL;
659 /* first look up existing for existing devType */
660 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
661 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
662 return uTbl;
665 /* well try to load id */
666 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
667 if (LoadStringW(hWinMM32Instance, uDevType, buf, ARRAY_SIZE(buf))) {
668 str = buf;
670 } else if (uDevType == 0) {
671 static const WCHAR wszCore[] = {'C','O','R','E',0};
672 str = wszCore;
674 uTbl = MCI_NO_COMMAND_TABLE;
675 if (str) {
676 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
677 HANDLE hMem = 0;
679 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
680 if (hMem) {
681 uTbl = MCI_SetCommandTable(hMem, uDevType);
682 } else {
683 WARN("No command table found in resource %p[%s]\n",
684 hWinMM32Instance, debugstr_w(str));
687 TRACE("=> %d\n", uTbl);
688 return uTbl;
691 /**************************************************************************
692 * MCI_SetCommandTable [internal]
694 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
696 int uTbl;
697 static BOOL bInitDone = FALSE;
699 /* <HACK>
700 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
701 * can be called with 0 as a uDevType to retrieve it.
702 * </HACK>
704 if (!bInitDone) {
705 bInitDone = TRUE;
706 MCI_GetCommandTable(0);
708 TRACE("(%p, %u)\n", hMem, uDevType);
709 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
710 if (!S_MciCmdTable[uTbl].lpTable) {
711 const BYTE* lmem;
712 LPCWSTR str;
713 WORD eid;
714 WORD count;
716 S_MciCmdTable[uTbl].uDevType = uDevType;
717 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
718 S_MciCmdTable[uTbl].hMem = hMem;
720 if (TRACE_ON(mci)) {
721 MCI_DumpCommandTable(uTbl);
724 /* create the verbs table */
725 /* get # of entries */
726 lmem = S_MciCmdTable[uTbl].lpTable;
727 count = 0;
728 do {
729 str = (LPCWSTR)lmem;
730 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
731 eid = *(const WORD*)(lmem + sizeof(DWORD));
732 lmem += sizeof(DWORD) + sizeof(WORD);
733 if (eid == MCI_COMMAND_HEAD)
734 count++;
735 } while (eid != MCI_END_COMMAND_LIST);
737 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
738 S_MciCmdTable[uTbl].nVerbs = count;
740 lmem = S_MciCmdTable[uTbl].lpTable;
741 count = 0;
742 do {
743 str = (LPCWSTR)lmem;
744 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
745 eid = *(const WORD*)(lmem + sizeof(DWORD));
746 lmem += sizeof(DWORD) + sizeof(WORD);
747 if (eid == MCI_COMMAND_HEAD)
748 S_MciCmdTable[uTbl].aVerbs[count++] = str;
749 } while (eid != MCI_END_COMMAND_LIST);
750 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
751 return uTbl;
755 return MCI_NO_COMMAND_TABLE;
758 /**************************************************************************
759 * MCI_UnLoadMciDriver [internal]
761 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
763 LPWINE_MCIDRIVER* tmp;
765 if (!wmd)
766 return TRUE;
768 CloseDriver(wmd->hDriver, 0, 0);
770 if (wmd->dwPrivate != 0)
771 WARN("Unloading mci driver with non nul dwPrivate field\n");
773 EnterCriticalSection(&WINMM_cs);
774 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
775 if (*tmp == wmd) {
776 *tmp = wmd->lpNext;
777 break;
780 LeaveCriticalSection(&WINMM_cs);
782 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
783 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
785 HeapFree(GetProcessHeap(), 0, wmd);
786 return TRUE;
789 /**************************************************************************
790 * MCI_OpenMciDriver [internal]
792 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
794 WCHAR libName[128];
796 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
797 return FALSE;
799 /* First load driver */
800 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
801 return wmd->hDriver != NULL;
804 /**************************************************************************
805 * MCI_LoadMciDriver [internal]
807 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
809 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
810 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
811 MCI_OPEN_DRIVER_PARMSW modp;
812 DWORD dwRet = 0;
814 if (!wmd || !strDevTyp) {
815 dwRet = MCIERR_OUT_OF_MEMORY;
816 goto errCleanUp;
819 wmd->lpfnYieldProc = MCI_DefYieldProc;
820 wmd->dwYieldData = VK_CANCEL;
821 wmd->CreatorThread = GetCurrentThreadId();
823 EnterCriticalSection(&WINMM_cs);
824 /* wmd must be inserted in list before sending opening the driver, because it
825 * may want to lookup at wDevID
827 wmd->lpNext = MciDrivers;
828 MciDrivers = wmd;
830 for (modp.wDeviceID = MCI_MAGIC;
831 MCI_GetDriver(modp.wDeviceID) != 0;
832 modp.wDeviceID++);
834 wmd->wDeviceID = modp.wDeviceID;
836 LeaveCriticalSection(&WINMM_cs);
838 TRACE("wDevID=%04X\n", modp.wDeviceID);
840 modp.lpstrParams = NULL;
842 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
843 /* silence warning if all is used... some bogus program use commands like
844 * 'open all'...
846 if (wcsicmp(strDevTyp, wszAll) == 0) {
847 dwRet = MCIERR_CANNOT_USE_ALL;
848 } else {
849 FIXME("Couldn't load driver for type %s.\n",
850 debugstr_w(strDevTyp));
851 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
853 goto errCleanUp;
856 /* FIXME: should also check that module's description is of the form
857 * MODULENAME:[MCI] comment
860 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
861 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
862 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
864 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
865 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
867 wmd->lpstrDeviceType = strDevTyp;
868 wmd->wType = modp.wType;
870 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
871 modp.wDeviceID, modp.wType, modp.wDeviceID);
872 *lpwmd = wmd;
873 return 0;
874 errCleanUp:
875 MCI_UnLoadMciDriver(wmd);
876 HeapFree(GetProcessHeap(), 0, strDevTyp);
877 *lpwmd = 0;
878 return dwRet;
881 /**************************************************************************
882 * MCI_SendCommandFrom32 [internal]
884 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
886 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
887 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
889 if (wmd) {
890 if(wmd->CreatorThread != GetCurrentThreadId())
891 return MCIERR_INVALID_DEVICE_NAME;
893 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
895 return dwRet;
898 /**************************************************************************
899 * MCI_FinishOpen [internal]
901 * Three modes of operation:
902 * 1 open foo.ext ... -> OPEN_ELEMENT with lpstrElementName=foo.ext
903 * open sequencer!foo.ext same with lpstrElementName=foo.ext
904 * 2 open new type waveaudio -> OPEN_ELEMENT with empty ("") lpstrElementName
905 * 3 open sequencer -> OPEN_ELEMENT unset, and
906 * capability sequencer (auto-open) likewise
908 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
909 DWORD dwParam)
911 LPCWSTR alias = NULL;
912 /* Open always defines an alias for further reference */
913 if (dwParam & MCI_OPEN_ALIAS) { /* open ... alias */
914 alias = lpParms->lpstrAlias;
915 if (MCI_GetDriverFromString(alias))
916 return MCIERR_DUPLICATE_ALIAS;
917 } else {
918 if ((dwParam & MCI_OPEN_ELEMENT) /* open file.wav */
919 && !(dwParam & MCI_OPEN_ELEMENT_ID))
920 alias = lpParms->lpstrElementName;
921 else if (dwParam & MCI_OPEN_TYPE ) /* open cdaudio */
922 alias = wmd->lpstrDeviceType;
923 if (alias && MCI_GetDriverFromString(alias))
924 return MCIERR_DEVICE_OPEN;
926 if (alias) {
927 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(alias)+1) * sizeof(WCHAR));
928 if (!wmd->lpstrAlias) return MCIERR_OUT_OF_MEMORY;
929 lstrcpyW( wmd->lpstrAlias, alias);
930 /* In most cases, natives adds MCI_OPEN_ALIAS to the flags passed to the driver.
931 * Don't. The drivers don't care about the winmm alias. */
933 lpParms->wDeviceID = wmd->wDeviceID;
935 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
936 (DWORD_PTR)lpParms);
939 /**************************************************************************
940 * MCI_FindCommand [internal]
942 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
944 UINT idx;
946 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
947 return NULL;
949 /* another improvement would be to have the aVerbs array sorted,
950 * so that we could use a dichotomic search on it, rather than this dumb
951 * array look up
953 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
954 if (wcsicmp(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
955 return S_MciCmdTable[uTbl].aVerbs[idx];
958 return NULL;
961 /**************************************************************************
962 * MCI_GetReturnType [internal]
964 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
966 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + lstrlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
967 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
968 return *(const DWORD*)(lpCmd + 1);
970 return 0L;
973 /**************************************************************************
974 * MCI_GetMessage [internal]
976 static WORD MCI_GetMessage(LPCWSTR lpCmd)
978 return (WORD)*(const DWORD*)(lpCmd + lstrlenW(lpCmd) + 1);
981 /**************************************************************************
982 * MCI_GetDWord [internal]
984 * Accept 0 -1 255 255:0 255:255:255:255 :::1 1::: 2::3 ::4: 12345678
985 * Refuse -1:0 0:-1 :: 256:0 1:256 0::::1
987 static BOOL MCI_GetDWord(DWORD* data, LPWSTR* ptr)
989 LPWSTR ret = *ptr;
990 DWORD total = 0, shift = 0;
991 BOOL sign = FALSE, digits = FALSE;
993 while (*ret == ' ' || *ret == '\t') ret++;
994 if (*ret == '-') {
995 ret++;
996 sign = TRUE;
998 for(;;) {
999 DWORD val = 0;
1000 while ('0' <= *ret && *ret <= '9') {
1001 val = *ret++ - '0' + 10 * val;
1002 digits = TRUE;
1004 switch (*ret) {
1005 case '\0': break;
1006 case '\t':
1007 case ' ': ret++; break;
1008 default: return FALSE;
1009 case ':':
1010 if ((val >= 256) || (shift >= 24)) return FALSE;
1011 total |= val << shift;
1012 shift += 8;
1013 ret++;
1014 continue;
1017 if (!digits) return FALSE;
1018 if (shift && (val >= 256 || sign)) return FALSE;
1019 total |= val << shift;
1020 *data = sign ? -total : total;
1021 *ptr = ret;
1022 return TRUE;
1026 /**************************************************************************
1027 * MCI_GetString [internal]
1029 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1031 LPWSTR ptr = *args;
1033 /* see if we have a quoted string */
1034 if (*ptr == '"') {
1035 ptr = wcschr(*str = ptr + 1, '"');
1036 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1037 /* FIXME: shall we escape \" from string ?? */
1038 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1039 *ptr++ = '\0'; /* remove trailing " */
1040 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1041 } else {
1042 ptr = wcschr(ptr, ' ');
1044 if (ptr) {
1045 *ptr++ = '\0';
1046 } else {
1047 ptr = *args + lstrlenW(*args);
1049 *str = *args;
1052 *args = ptr;
1053 return 0;
1056 #define MCI_DATA_SIZE 16
1058 /**************************************************************************
1059 * MCI_ParseOptArgs [internal]
1061 static DWORD MCI_ParseOptArgs(DWORD* data, int _offset, LPCWSTR lpCmd,
1062 LPWSTR args, LPDWORD dwFlags)
1064 int len, offset;
1065 const char* lmem;
1066 LPCWSTR str;
1067 DWORD dwRet, flg, cflg = 0;
1068 WORD eid;
1069 BOOL inCst, found;
1071 /* loop on arguments */
1072 while (*args) {
1073 lmem = (const char*)lpCmd;
1074 found = inCst = FALSE;
1075 offset = _offset;
1077 /* skip any leading white space(s) */
1078 while (*args == ' ') args++;
1079 TRACE("args=%s\n", debugstr_w(args));
1081 do { /* loop on options for command table for the requested verb */
1082 str = (LPCWSTR)lmem;
1083 lmem += ((len = lstrlenW(str)) + 1) * sizeof(WCHAR);
1084 flg = *(const DWORD*)lmem;
1085 eid = *(const WORD*)(lmem + sizeof(DWORD));
1086 lmem += sizeof(DWORD) + sizeof(WORD);
1087 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1089 switch (eid) {
1090 case MCI_CONSTANT:
1091 inCst = TRUE; cflg = flg; break;
1092 case MCI_END_CONSTANT:
1093 /* there may be additional integral values after flag in constant */
1094 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1095 *dwFlags |= cflg;
1097 inCst = FALSE; cflg = 0;
1098 break;
1099 case MCI_RETURN:
1100 if (offset != _offset) {
1101 FIXME("MCI_RETURN not in first position\n");
1102 return MCIERR_PARSER_INTERNAL;
1106 if (wcsnicmp(args, str, len) == 0 &&
1107 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1108 /* store good values into data[] */
1109 args += len;
1110 while (*args == ' ') args++;
1111 found = TRUE;
1113 switch (eid) {
1114 case MCI_COMMAND_HEAD:
1115 case MCI_RETURN:
1116 case MCI_END_COMMAND:
1117 case MCI_END_COMMAND_LIST:
1118 case MCI_CONSTANT: /* done above */
1119 case MCI_END_CONSTANT: /* done above */
1120 break;
1121 case MCI_FLAG:
1122 *dwFlags |= flg;
1123 TRACE("flag=%08x\n", flg);
1124 break;
1125 case MCI_HWND:
1126 case MCI_HPAL:
1127 case MCI_HDC:
1128 case MCI_INTEGER:
1129 if (inCst) {
1130 data[offset] |= flg;
1131 *dwFlags |= cflg;
1132 inCst = FALSE;
1133 TRACE("flag=%08x constant=%08x\n", cflg, flg);
1134 } else {
1135 *dwFlags |= flg;
1136 if (!MCI_GetDWord(&(data[offset]), &args)) {
1137 return MCIERR_BAD_INTEGER;
1139 TRACE("flag=%08x int=%d\n", flg, data[offset]);
1141 break;
1142 case MCI_RECT:
1143 /* store rect in data (offset..offset+3) */
1144 *dwFlags |= flg;
1145 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1146 !MCI_GetDWord(&(data[offset+1]), &args) ||
1147 !MCI_GetDWord(&(data[offset+2]), &args) ||
1148 !MCI_GetDWord(&(data[offset+3]), &args)) {
1149 return MCIERR_BAD_INTEGER;
1151 TRACE("flag=%08x for rectangle\n", flg);
1152 break;
1153 case MCI_STRING:
1154 *dwFlags |= flg;
1155 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1156 return dwRet;
1157 TRACE("flag=%08x string=%s\n", flg, debugstr_w(*(LPWSTR*)&data[offset]));
1158 break;
1159 default: ERR("oops\n");
1161 /* exit inside while loop, except if just entered in constant area definition */
1162 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1163 } else {
1164 /* have offset incremented if needed */
1165 switch (eid) {
1166 case MCI_COMMAND_HEAD:
1167 case MCI_RETURN:
1168 case MCI_END_COMMAND:
1169 case MCI_END_COMMAND_LIST:
1170 case MCI_CONSTANT:
1171 case MCI_FLAG: break;
1172 case MCI_HWND:
1173 case MCI_HPAL:
1174 case MCI_HDC: if (!inCst) offset += sizeof(HANDLE)/sizeof(DWORD); break;
1175 case MCI_INTEGER: if (!inCst) offset++; break;
1176 case MCI_END_CONSTANT: offset++; break;
1177 case MCI_STRING: offset += sizeof(LPWSTR)/sizeof(DWORD); break;
1178 case MCI_RECT: offset += 4; break;
1179 default: ERR("oops\n");
1182 } while (eid != MCI_END_COMMAND);
1183 if (!found) {
1184 WARN("Optarg %s not found\n", debugstr_w(args));
1185 return MCIERR_UNRECOGNIZED_COMMAND;
1187 if (offset == MCI_DATA_SIZE) {
1188 FIXME("Internal data[] buffer overflow\n");
1189 return MCIERR_PARSER_INTERNAL;
1192 return 0;
1195 /**************************************************************************
1196 * MCI_HandleReturnValues [internal]
1198 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1199 MCI_GENERIC_PARMS *params, LPWSTR lpstrRet, UINT uRetLen)
1201 static const WCHAR fmt_d [] = {'%','d',0};
1202 static const WCHAR fmt_d4 [] = {'%','d',' ','%','d',' ','%','d',' ','%','d',0};
1203 static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1204 static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1206 if (lpstrRet) {
1207 switch (retType) {
1208 case 0: /* nothing to return */
1209 break;
1210 case MCI_INTEGER:
1212 DWORD data = *(DWORD *)(params + 1);
1213 switch (dwRet & 0xFFFF0000ul) {
1214 case 0:
1215 case MCI_INTEGER_RETURNED:
1216 swprintf(lpstrRet, uRetLen, fmt_d, data);
1217 break;
1218 case MCI_RESOURCE_RETURNED:
1219 /* return string which ID is HIWORD(data),
1220 * string is loaded from mmsystem.dll */
1221 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1222 break;
1223 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1224 /* return string which ID is HIWORD(data),
1225 * string is loaded from driver */
1226 /* FIXME: this is wrong for a 16 bit handle */
1227 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1228 HIWORD(data), lpstrRet, uRetLen);
1229 break;
1230 case MCI_COLONIZED3_RETURN:
1231 swprintf(lpstrRet, uRetLen, wszCol3,
1232 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1233 LOBYTE(HIWORD(data)));
1234 break;
1235 case MCI_COLONIZED4_RETURN:
1236 swprintf(lpstrRet, uRetLen, wszCol4,
1237 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1238 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1239 break;
1240 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1242 break;
1244 #ifdef MCI_INTEGER64
1245 case MCI_INTEGER64:
1247 static const WCHAR fmt_ld [] = {'%','l','d',0};
1248 DWORD_PTR data = *(DWORD_PTR *)(params + 1);
1249 switch (dwRet & 0xFFFF0000ul) {
1250 case 0:
1251 case MCI_INTEGER_RETURNED:
1252 swprintf(lpstrRet, uRetLen, fmt_ld, data);
1253 break;
1254 case MCI_RESOURCE_RETURNED:
1255 /* return string which ID is HIWORD(data),
1256 * string is loaded from mmsystem.dll */
1257 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1258 break;
1259 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1260 /* return string which ID is HIWORD(data),
1261 * string is loaded from driver */
1262 /* FIXME: this is wrong for a 16 bit handle */
1263 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1264 HIWORD(data), lpstrRet, uRetLen);
1265 break;
1266 case MCI_COLONIZED3_RETURN:
1267 swprintf(lpstrRet, uRetLen, wszCol3,
1268 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1269 LOBYTE(HIWORD(data)));
1270 break;
1271 case MCI_COLONIZED4_RETURN:
1272 swprintf(lpstrRet, uRetLen, wszCol4,
1273 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1274 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1275 break;
1276 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1278 break;
1280 #endif
1281 case MCI_STRING:
1282 switch (dwRet & 0xFFFF0000ul) {
1283 case 0:
1284 /* nothing to do data[0] == lpstrRet */
1285 break;
1286 case MCI_INTEGER_RETURNED:
1288 DWORD *data = (DWORD *)(params + 1);
1289 *data = *(LPDWORD)lpstrRet;
1290 swprintf(lpstrRet, uRetLen, fmt_d, *data);
1291 break;
1293 default:
1294 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1295 break;
1297 break;
1298 case MCI_RECT:
1300 DWORD *data = (DWORD *)(params + 1);
1301 if (dwRet & 0xFFFF0000ul)
1302 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1303 swprintf(lpstrRet, uRetLen, fmt_d4, data[0], data[1], data[2], data[3]);
1304 break;
1306 default: FIXME("Unknown MCI return type %d\n", retType);
1309 return LOWORD(dwRet);
1312 /**************************************************************************
1313 * mciSendStringW [WINMM.@]
1315 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1316 UINT uRetLen, HWND hwndCallback)
1318 LPWSTR verb, dev, args, devType = NULL;
1319 LPWINE_MCIDRIVER wmd = 0;
1320 MCIDEVICEID uDevID, auto_open = 0;
1321 DWORD dwFlags = 0, dwRet = 0;
1322 int offset = 0;
1323 DWORD retType;
1324 LPCWSTR lpCmd = 0;
1325 WORD wMsg = 0;
1326 static const WCHAR wszNew[] = {'n','e','w',0};
1327 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1328 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1329 union
1331 MCI_GENERIC_PARMS generic;
1332 MCI_OPEN_PARMSW open;
1333 MCI_SOUND_PARMSW sound;
1334 MCI_SYSINFO_PARMSW sysinfo;
1335 DWORD dw[MCI_DATA_SIZE];
1336 } data;
1338 TRACE("(%s, %p, %d, %p)\n",
1339 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1340 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1342 if (!lpstrCommand[0])
1343 return MCIERR_MISSING_COMMAND_STRING;
1345 /* format is <command> <device> <optargs> */
1346 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1347 return MCIERR_OUT_OF_MEMORY;
1348 lstrcpyW( verb, lpstrCommand );
1349 CharLowerW(verb);
1351 memset(&data, 0, sizeof(data));
1353 if (!(args = wcschr(verb, ' '))) {
1354 dwRet = MCIERR_MISSING_DEVICE_NAME;
1355 goto errCleanUp;
1357 *args++ = '\0';
1358 if ((dwRet = MCI_GetString(&dev, &args))) {
1359 goto errCleanUp;
1361 uDevID = wcsicmp(dev, wszAll) ? 0 : MCI_ALL_DEVICE_ID;
1363 /* Determine devType from open */
1364 if (!wcscmp(verb, wszOpen)) {
1365 LPWSTR tmp;
1366 WCHAR buf[128];
1368 /* case dev == 'new' has to be handled */
1369 if (!wcscmp(dev, wszNew)) {
1370 dev = 0;
1371 if ((devType = wcsstr(args, wszTypeS)) != NULL) {
1372 devType += 5;
1373 tmp = wcschr(devType, ' ');
1374 if (tmp) *tmp = '\0';
1375 devType = str_dup_upper(devType);
1376 if (tmp) *tmp = ' ';
1377 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1378 } else {
1379 WARN("open new requires device type\n");
1380 dwRet = MCIERR_MISSING_DEVICE_NAME;
1381 goto errCleanUp;
1383 dwFlags |= MCI_OPEN_ELEMENT;
1384 data.open.lpstrElementName = &wszNull[0];
1385 } else if ((devType = wcschr(dev, '!')) != NULL) {
1386 *devType++ = '\0';
1387 tmp = devType; devType = dev; dev = tmp;
1389 dwFlags |= MCI_OPEN_TYPE;
1390 data.open.lpstrDeviceType = devType;
1391 devType = str_dup_upper(devType);
1392 dwFlags |= MCI_OPEN_ELEMENT;
1393 data.open.lpstrElementName = dev;
1394 } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1395 /* this is the name of a mci driver's type */
1396 tmp = wcschr(dev, ' ');
1397 if (tmp) *tmp = '\0';
1398 data.open.lpstrDeviceType = dev;
1399 devType = str_dup_upper(dev);
1400 if (tmp) *tmp = ' ';
1401 dwFlags |= MCI_OPEN_TYPE;
1402 } else {
1403 if ((devType = wcsstr(args, wszTypeS)) != NULL) {
1404 devType += 5;
1405 tmp = wcschr(devType, ' ');
1406 if (tmp) *tmp = '\0';
1407 devType = str_dup_upper(devType);
1408 if (tmp) *tmp = ' ';
1409 /* dwFlags and lpstrDeviceType will be correctly set in ParseOpt loop */
1410 } else {
1411 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1412 goto errCleanUp;
1414 devType = str_dup_upper(buf);
1416 dwFlags |= MCI_OPEN_ELEMENT;
1417 data.open.lpstrElementName = dev;
1419 if (MCI_ALL_DEVICE_ID == uDevID) {
1420 dwRet = MCIERR_CANNOT_USE_ALL;
1421 goto errCleanUp;
1423 if (!wcsstr(args, wszSAliasS) && !dev) {
1424 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1425 goto errCleanUp;
1428 dwRet = MCI_LoadMciDriver(devType, &wmd);
1429 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1430 dwRet = MCIERR_INVALID_DEVICE_NAME;
1431 if (dwRet)
1432 goto errCleanUp;
1433 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))
1434 && (lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb))) {
1435 /* auto-open uses the core command table */
1436 switch (MCI_GetMessage(lpCmd)) {
1437 case MCI_SOUND: /* command does not use a device name */
1438 case MCI_SYSINFO:
1439 break;
1440 case MCI_CLOSE: /* don't auto-open for close */
1441 case MCI_BREAK: /* no auto-open for system commands */
1442 dwRet = MCIERR_INVALID_DEVICE_NAME;
1443 goto errCleanUp;
1444 break;
1445 default:
1447 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1448 WCHAR buf[138], retbuf[6];
1449 swprintf(buf, ARRAY_SIZE(buf), wszOpenWait, dev);
1450 /* open via mciSendString handles quoting, dev!file syntax and alias creation */
1451 if ((dwRet = mciSendStringW(buf, retbuf, ARRAY_SIZE(retbuf), 0)) != 0)
1452 goto errCleanUp;
1453 auto_open = wcstoul(retbuf, NULL, 10);
1454 TRACE("auto-opened %u for %s\n", auto_open, debugstr_w(dev));
1456 /* FIXME: test for notify flag (how to preparse?) before opening */
1457 wmd = MCI_GetDriver(auto_open);
1458 if (!wmd) {
1459 ERR("No auto-open device %u\n", auto_open);
1460 dwRet = MCIERR_INVALID_DEVICE_ID;
1461 goto errCleanUp;
1467 /* get the verb in the different command tables */
1468 if (wmd) {
1469 /* try the device specific command table */
1470 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1471 if (!lpCmd) {
1472 /* try the type specific command table */
1473 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1474 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1475 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1476 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1479 /* try core command table */
1480 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1482 if (!lpCmd) {
1483 TRACE("Command %s not found!\n", debugstr_w(verb));
1484 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1485 goto errCleanUp;
1487 wMsg = MCI_GetMessage(lpCmd);
1489 /* set return information */
1490 offset = sizeof(data.generic);
1491 switch (retType = MCI_GetReturnType(lpCmd)) {
1492 case 0:
1493 break;
1494 case MCI_INTEGER:
1495 offset += sizeof(DWORD);
1496 break;
1497 case MCI_STRING:
1498 data.sysinfo.lpstrReturn = lpstrRet;
1499 data.sysinfo.dwRetSize = uRetLen;
1500 offset = FIELD_OFFSET( MCI_SYSINFO_PARMSW, dwNumber );
1501 break;
1502 case MCI_RECT:
1503 offset += 4 * sizeof(DWORD);
1504 break;
1505 #ifdef MCI_INTEGER64
1506 case MCI_INTEGER64:
1507 offset += sizeof(DWORD_PTR);
1508 break;
1509 #endif
1510 default:
1511 FIXME("Unknown MCI return type %d\n", retType);
1512 dwRet = MCIERR_PARSER_INTERNAL;
1513 goto errCleanUp;
1516 TRACE("verb=%s on dev=%s; offset=%d\n",
1517 debugstr_w(verb), debugstr_w(dev), offset);
1519 if ((dwRet = MCI_ParseOptArgs(data.dw, offset / sizeof(DWORD), lpCmd, args, &dwFlags)))
1520 goto errCleanUp;
1522 /* set up call back */
1523 if (auto_open) {
1524 if (dwFlags & MCI_NOTIFY) {
1525 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1526 goto errCleanUp;
1528 /* FIXME: the command should get its own notification window set up and
1529 * ask for device closing while processing the notification mechanism.
1530 * hwndCallback = ...
1531 * dwFlags |= MCI_NOTIFY;
1532 * In the meantime special-case all commands but PLAY and RECORD below. */
1534 if (dwFlags & MCI_NOTIFY) {
1535 data.generic.dwCallback = (DWORD_PTR)hwndCallback;
1538 switch (wMsg) {
1539 case MCI_OPEN:
1540 if (wcscmp(verb, wszOpen)) {
1541 FIXME("Cannot open with command %s\n", debugstr_w(verb));
1542 dwRet = MCIERR_DRIVER_INTERNAL;
1543 wMsg = 0;
1544 goto errCleanUp;
1546 break;
1547 case MCI_SYSINFO:
1548 /* Requirements on dev depend on the flags:
1549 * alias with INSTALLNAME, name like "digitalvideo"
1550 * with QUANTITY and NAME. */
1552 data.sysinfo.wDeviceType = MCI_ALL_DEVICE_ID;
1553 if (uDevID != MCI_ALL_DEVICE_ID) {
1554 if (dwFlags & MCI_SYSINFO_INSTALLNAME)
1555 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1556 else if (!(data.sysinfo.wDeviceType = MCI_GetDevTypeFromResource(dev))) {
1557 dwRet = MCIERR_DEVICE_TYPE_REQUIRED;
1558 goto errCleanUp;
1562 break;
1563 case MCI_SOUND:
1564 /* FIXME: name is optional, "sound" is a valid command.
1565 * FIXME: Parse "sound notify" as flag, not as name. */
1566 data.sound.lpstrSoundName = dev;
1567 dwFlags |= MCI_SOUND_NAME;
1568 break;
1571 TRACE("[%d, %s, %08x, %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x]\n",
1572 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(wMsg), dwFlags,
1573 data.dw[0], data.dw[1], data.dw[2], data.dw[3], data.dw[4],
1574 data.dw[5], data.dw[6], data.dw[7], data.dw[8], data.dw[9]);
1576 if (wMsg == MCI_OPEN) {
1577 if ((dwRet = MCI_FinishOpen(wmd, &data.open, dwFlags)))
1578 goto errCleanUp;
1579 /* FIXME: notification is not properly shared across two opens */
1580 } else {
1581 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, wMsg, dwFlags, (DWORD_PTR)&data);
1583 if (!LOWORD(dwRet)) {
1584 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1585 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, &data.generic, lpstrRet, uRetLen);
1586 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1587 } else
1588 TRACE("=> %x\n", dwRet);
1590 errCleanUp:
1591 if (auto_open) {
1592 /* PLAY and RECORD are the only known non-immediate commands */
1593 if (LOWORD(dwRet) || !(wMsg == MCI_PLAY || wMsg == MCI_RECORD))
1594 MCI_SendCommand(auto_open, MCI_CLOSE, 0, 0);
1595 else
1596 FIXME("leaking auto-open device %u\n", auto_open);
1598 if (wMsg == MCI_OPEN && LOWORD(dwRet) && wmd)
1599 MCI_UnLoadMciDriver(wmd);
1600 HeapFree(GetProcessHeap(), 0, devType);
1601 HeapFree(GetProcessHeap(), 0, verb);
1602 return dwRet;
1605 /**************************************************************************
1606 * mciSendStringA [WINMM.@]
1608 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1609 UINT uRetLen, HWND hwndCallback)
1611 LPWSTR lpwstrCommand;
1612 LPWSTR lpwstrRet = NULL;
1613 UINT ret;
1614 INT len;
1616 /* FIXME: is there something to do with lpstrReturnString ? */
1617 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1618 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1619 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1620 if (lpstrRet)
1622 if (uRetLen) *lpstrRet = '\0'; /* NT-w2k3 use memset(lpstrRet, 0, uRetLen); */
1623 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1624 if (!lpwstrRet) {
1625 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1626 return MCIERR_OUT_OF_MEMORY;
1629 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1630 if (!ret && lpwstrRet)
1631 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1632 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1633 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1634 return ret;
1637 /**************************************************************************
1638 * mciExecute [WINMM.@]
1640 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1642 char strRet[256];
1643 DWORD ret;
1645 TRACE("(%s)!\n", lpstrCommand);
1647 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1648 if (ret != 0) {
1649 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1650 sprintf(strRet, "Unknown MCI error (%d)", ret);
1652 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1654 /* FIXME: what shall I return ? */
1655 return TRUE;
1658 /**************************************************************************
1659 * mciLoadCommandResource [WINMM.@]
1661 * Strangely, this function only exists as a UNICODE one.
1663 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1665 UINT ret = MCI_NO_COMMAND_TABLE;
1666 HRSRC hRsrc = 0;
1667 HGLOBAL hMem;
1669 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1671 /* if a file named "resname.mci" exits, then load resource "resname" from it
1672 * otherwise directly from driver
1673 * We don't support it (who uses this feature ?), but we check anyway
1675 if (!type) {
1676 #if 0
1677 /* FIXME: we should put this back into order, but I never found a program
1678 * actually using this feature, so we may not need it
1680 char buf[128];
1681 OFSTRUCT ofs;
1683 strcat(strcpy(buf, resname), ".mci");
1684 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1685 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1687 #endif
1689 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1690 (hMem = LoadResource(hInst, hRsrc))) {
1691 ret = MCI_SetCommandTable(hMem, type);
1692 FreeResource(hMem);
1694 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1696 TRACE("=> %04x\n", ret);
1697 return ret;
1700 /**************************************************************************
1701 * mciFreeCommandResource [WINMM.@]
1703 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1705 TRACE("(%08x)!\n", uTable);
1707 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1708 return FALSE;
1710 FreeResource(S_MciCmdTable[uTable].hMem);
1711 S_MciCmdTable[uTable].hMem = NULL;
1712 S_MciCmdTable[uTable].lpTable = NULL;
1713 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1714 S_MciCmdTable[uTable].aVerbs = 0;
1715 S_MciCmdTable[uTable].nVerbs = 0;
1716 return TRUE;
1719 /**************************************************************************
1720 * MCI_Open [internal]
1722 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1724 WCHAR strDevTyp[128];
1725 DWORD dwRet;
1726 LPWINE_MCIDRIVER wmd = NULL;
1728 TRACE("(%08X, %p)\n", dwParam, lpParms);
1729 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1731 /* only two low bytes are generic, the other ones are dev type specific */
1732 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1733 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1734 MCI_NOTIFY|MCI_WAIT)
1735 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0)
1736 FIXME("Unsupported yet dwFlags=%08X\n", dwParam);
1737 #undef WINE_MCIDRIVER_SUPP
1739 strDevTyp[0] = 0;
1741 if (dwParam & MCI_OPEN_TYPE) {
1742 if (dwParam & MCI_OPEN_TYPE_ID) {
1743 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1745 if (uDevType < MCI_DEVTYPE_FIRST || uDevType > MCI_DEVTYPE_LAST ||
1746 !LoadStringW(hWinMM32Instance, uDevType, strDevTyp, ARRAY_SIZE(strDevTyp))) {
1747 dwRet = MCIERR_BAD_INTEGER;
1748 goto errCleanUp;
1750 } else {
1751 LPWSTR ptr;
1752 if (lpParms->lpstrDeviceType == NULL) {
1753 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1754 goto errCleanUp;
1756 lstrcpyW(strDevTyp, lpParms->lpstrDeviceType);
1757 ptr = wcschr(strDevTyp, '!');
1758 if (ptr) {
1759 /* this behavior is not documented in windows. However, since, in
1760 * some occasions, MCI_OPEN handling is translated by WinMM into
1761 * a call to mciSendString("open <type>"); this code shall be correct
1763 if (dwParam & MCI_OPEN_ELEMENT) {
1764 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1765 debugstr_w(lpParms->lpstrElementName),
1766 debugstr_w(strDevTyp));
1767 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1768 goto errCleanUp;
1770 dwParam |= MCI_OPEN_ELEMENT;
1771 *ptr++ = 0;
1772 /* FIXME: not a good idea to write in user supplied buffer */
1773 lpParms->lpstrElementName = ptr;
1777 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1780 if (dwParam & MCI_OPEN_ELEMENT) {
1781 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1783 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1784 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1785 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1786 goto errCleanUp;
1789 if (!lpParms->lpstrElementName) {
1790 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1791 goto errCleanUp;
1794 /* type, if given as a parameter, supersedes file extension */
1795 if (!strDevTyp[0] &&
1796 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1797 strDevTyp, sizeof(strDevTyp))) {
1798 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1799 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1800 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1801 goto errCleanUp;
1803 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1804 lstrcpyW(strDevTyp, wszCdAudio);
1808 if (strDevTyp[0] == 0) {
1809 FIXME("Couldn't load driver\n");
1810 dwRet = MCIERR_INVALID_DEVICE_NAME;
1811 goto errCleanUp;
1814 if (dwParam & MCI_OPEN_ALIAS) {
1815 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1816 if (!lpParms->lpstrAlias) {
1817 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1818 goto errCleanUp;
1822 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1823 goto errCleanUp;
1826 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1827 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1828 /* FIXME: is dwRet the correct ret code ? */
1829 goto errCleanUp;
1832 /* only handled devices fall through */
1833 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1834 return 0;
1836 errCleanUp:
1837 if (wmd) MCI_UnLoadMciDriver(wmd);
1838 return dwRet;
1841 /**************************************************************************
1842 * MCI_Close [internal]
1844 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1846 DWORD dwRet;
1847 LPWINE_MCIDRIVER wmd;
1849 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1851 /* Every device must handle MCI_NOTIFY on its own. */
1852 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1853 while (MciDrivers) {
1854 /* Retrieve the device ID under lock, but send the message without,
1855 * the driver might be calling some winmm functions from another
1856 * thread before being fully stopped.
1858 EnterCriticalSection(&WINMM_cs);
1859 if (!MciDrivers)
1861 LeaveCriticalSection(&WINMM_cs);
1862 break;
1864 wDevID = MciDrivers->wDeviceID;
1865 LeaveCriticalSection(&WINMM_cs);
1866 MCI_Close(wDevID, dwParam, lpParms);
1868 return 0;
1871 if (!(wmd = MCI_GetDriver(wDevID))) {
1872 return MCIERR_INVALID_DEVICE_ID;
1875 if(wmd->CreatorThread != GetCurrentThreadId())
1876 return MCIERR_INVALID_DEVICE_NAME;
1878 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1880 MCI_UnLoadMciDriver(wmd);
1882 return dwRet;
1885 /**************************************************************************
1886 * MCI_WriteString [internal]
1888 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1890 DWORD ret = 0;
1892 if (lpSrcStr) {
1893 if (dstSize <= lstrlenW(lpSrcStr)) {
1894 ret = MCIERR_PARAM_OVERFLOW;
1895 } else {
1896 lstrcpyW(lpDstStr, lpSrcStr);
1898 } else {
1899 *lpDstStr = 0;
1901 return ret;
1904 /**************************************************************************
1905 * MCI_Sysinfo [internal]
1907 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1909 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1910 WCHAR buf[2048], *s, *p;
1911 LPWINE_MCIDRIVER wmd;
1912 HKEY hKey;
1914 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1915 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1917 TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1918 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1919 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1920 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1922 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1923 case MCI_SYSINFO_QUANTITY:
1924 if (lpParms->dwRetSize < sizeof(DWORD))
1925 return MCIERR_PARAM_OVERFLOW;
1926 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1927 if (uDevID == MCI_ALL_DEVICE_ID) {
1928 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1929 if (dwFlags & MCI_SYSINFO_OPEN) {
1930 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1931 EnterCriticalSection(&WINMM_cs);
1932 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1933 cnt++;
1935 LeaveCriticalSection(&WINMM_cs);
1936 } else {
1937 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1938 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1939 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1940 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1941 RegCloseKey( hKey );
1943 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, ARRAY_SIZE(buf), wszSystemIni))
1944 for (s = buf; *s; s += lstrlenW(s) + 1) cnt++;
1946 } else {
1947 if (dwFlags & MCI_SYSINFO_OPEN) {
1948 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1949 EnterCriticalSection(&WINMM_cs);
1950 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1951 if (wmd->wType == lpParms->wDeviceType) cnt++;
1953 LeaveCriticalSection(&WINMM_cs);
1954 } else {
1955 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1956 FIXME("Don't know how to get # of MCI devices of a given type\n");
1957 /* name = LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType))
1958 * then lookup registry and/or system.ini for name, ignoring digits suffix */
1959 switch (LOWORD(lpParms->wDeviceType)) {
1960 case MCI_DEVTYPE_CD_AUDIO:
1961 case MCI_DEVTYPE_WAVEFORM_AUDIO:
1962 case MCI_DEVTYPE_SEQUENCER:
1963 cnt = 1;
1964 break;
1965 default: /* "digitalvideo" gets 0 because it's not in the registry */
1966 cnt = 0;
1970 *(DWORD*)lpParms->lpstrReturn = cnt;
1971 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1972 ret = MCI_INTEGER_RETURNED;
1973 /* return ret; Only Win9x sends a notification in this case. */
1974 break;
1975 case MCI_SYSINFO_INSTALLNAME:
1976 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1977 if ((wmd = MCI_GetDriver(uDevID))) {
1978 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1979 wmd->lpstrDeviceType);
1980 } else {
1981 ret = (uDevID == MCI_ALL_DEVICE_ID)
1982 ? MCIERR_CANNOT_USE_ALL : MCIERR_INVALID_DEVICE_NAME;
1984 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1985 break;
1986 case MCI_SYSINFO_NAME:
1987 s = NULL;
1988 if (dwFlags & MCI_SYSINFO_OPEN) {
1989 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1990 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1991 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1992 EnterCriticalSection(&WINMM_cs);
1993 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1994 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1995 if (uDevID == MCI_ALL_DEVICE_ID ||
1996 lpParms->wDeviceType == wmd->wType) {
1997 cnt++;
1998 if (cnt == lpParms->dwNumber) {
1999 s = wmd->lpstrAlias;
2000 break;
2004 LeaveCriticalSection(&WINMM_cs);
2005 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
2006 } else if (MCI_ALL_DEVICE_ID == uDevID) {
2007 TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
2008 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
2009 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
2010 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
2011 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
2012 lpParms->dwNumber <= cnt) {
2013 DWORD bufLen = ARRAY_SIZE(buf);
2014 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
2015 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
2016 s = buf;
2018 RegCloseKey( hKey );
2020 if (!s) {
2021 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, ARRAY_SIZE(buf), wszSystemIni)) {
2022 for (p = buf; *p; p += lstrlenW(p) + 1, cnt++) {
2023 TRACE("%d: %s\n", cnt, debugstr_w(p));
2024 if (cnt == lpParms->dwNumber - 1) {
2025 s = p;
2026 break;
2031 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
2032 } else {
2033 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
2034 /* Cheating: what is asked for is the nth device from the registry. */
2035 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
2036 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
2037 ret = MCIERR_OUTOFRANGE;
2038 else {
2039 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
2040 lpParms->lpstrReturn, lpParms->dwRetSize);
2041 ret = 0;
2044 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
2045 break;
2046 default:
2047 TRACE("Unsupported flag value=%08x\n", dwFlags);
2048 ret = MCIERR_UNRECOGNIZED_KEYWORD;
2050 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
2051 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
2052 return ret;
2055 /**************************************************************************
2056 * MCI_Break [internal]
2058 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
2060 DWORD dwRet;
2062 if (lpParms == NULL)
2063 return MCIERR_NULL_PARAMETER_BLOCK;
2065 TRACE("(%08x, %08X, vkey %04X, hwnd %p)\n", wDevID, dwFlags,
2066 lpParms->nVirtKey, lpParms->hwndBreak);
2068 dwRet = MCI_SendCommandFrom32(wDevID, MCI_BREAK, dwFlags, (DWORD_PTR)lpParms);
2069 if (!dwRet && (dwFlags & MCI_NOTIFY))
2070 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2071 return dwRet;
2074 /**************************************************************************
2075 * MCI_Sound [internal]
2077 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
2079 DWORD dwRet;
2081 if (dwFlags & MCI_SOUND_NAME) {
2082 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2083 else dwRet = PlaySoundW(lpParms->lpstrSoundName, NULL,
2084 SND_ALIAS | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2085 ? 0 : MCIERR_HARDWARE;
2086 } else dwRet = PlaySoundW((LPCWSTR)SND_ALIAS_SYSTEMDEFAULT, NULL,
2087 SND_ALIAS_ID | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2088 ? 0 : MCIERR_HARDWARE;
2090 if (!dwRet && lpParms && (dwFlags & MCI_NOTIFY))
2091 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2092 return dwRet;
2095 /**************************************************************************
2096 * MCI_SendCommand [internal]
2098 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2100 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
2102 switch (wMsg) {
2103 case MCI_OPEN:
2104 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
2105 break;
2106 case MCI_CLOSE:
2107 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
2108 break;
2109 case MCI_SYSINFO:
2110 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2111 break;
2112 case MCI_BREAK:
2113 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2114 break;
2115 case MCI_SOUND:
2116 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2117 break;
2118 default:
2119 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
2120 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2121 dwRet = MCIERR_CANNOT_USE_ALL;
2122 } else {
2123 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
2125 break;
2127 return dwRet;
2130 /**************************************************************************
2131 * MCI_CleanUp [internal]
2133 * Some MCI commands need to be cleaned-up (when not called from
2134 * mciSendString), because MCI drivers return extra information for string
2135 * transformation. This function gets rid of them.
2137 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
2139 if (LOWORD(dwRet))
2140 return LOWORD(dwRet);
2142 switch (wMsg) {
2143 case MCI_GETDEVCAPS:
2144 switch (dwRet & 0xFFFF0000ul) {
2145 case 0:
2146 case MCI_COLONIZED3_RETURN:
2147 case MCI_COLONIZED4_RETURN:
2148 case MCI_INTEGER_RETURNED:
2149 /* nothing to do */
2150 break;
2151 case MCI_RESOURCE_RETURNED:
2152 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2154 LPMCI_GETDEVCAPS_PARMS lmgp;
2156 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
2157 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2158 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2160 break;
2161 default:
2162 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2163 HIWORD(dwRet), MCI_MessageToString(wMsg));
2165 break;
2166 case MCI_STATUS:
2167 switch (dwRet & 0xFFFF0000ul) {
2168 case 0:
2169 case MCI_COLONIZED3_RETURN:
2170 case MCI_COLONIZED4_RETURN:
2171 case MCI_INTEGER_RETURNED:
2172 /* nothing to do */
2173 break;
2174 case MCI_RESOURCE_RETURNED:
2175 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2177 LPMCI_STATUS_PARMS lsp;
2179 lsp = (LPMCI_STATUS_PARMS)dwParam2;
2180 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2181 lsp->dwReturn = LOWORD(lsp->dwReturn);
2183 break;
2184 default:
2185 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2186 HIWORD(dwRet), MCI_MessageToString(wMsg));
2188 break;
2189 case MCI_SYSINFO:
2190 switch (dwRet & 0xFFFF0000ul) {
2191 case 0:
2192 case MCI_INTEGER_RETURNED:
2193 /* nothing to do */
2194 break;
2195 default:
2196 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2198 break;
2199 default:
2200 if (HIWORD(dwRet)) {
2201 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2202 dwRet, MCI_MessageToString(wMsg));
2204 break;
2206 return LOWORD(dwRet);
2209 /**************************************************************************
2210 * mciGetErrorStringW [WINMM.@]
2212 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2214 BOOL ret = FALSE;
2216 if (lpstrBuffer != NULL && uLength > 0 &&
2217 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2219 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2220 ret = TRUE;
2223 return ret;
2226 /**************************************************************************
2227 * mciGetErrorStringA [WINMM.@]
2229 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2231 BOOL ret = FALSE;
2233 if (lpstrBuffer != NULL && uLength > 0 &&
2234 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2236 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2237 ret = TRUE;
2240 return ret;
2243 /**************************************************************************
2244 * mciDriverNotify [WINMM.@]
2246 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2248 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2250 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2253 /**************************************************************************
2254 * mciGetDriverData [WINMM.@]
2256 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2258 LPWINE_MCIDRIVER wmd;
2260 TRACE("(%04x)\n", uDeviceID);
2262 wmd = MCI_GetDriver(uDeviceID);
2264 if (!wmd) {
2265 WARN("Bad uDeviceID\n");
2266 return 0L;
2269 return wmd->dwPrivate;
2272 /**************************************************************************
2273 * mciSetDriverData [WINMM.@]
2275 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2277 LPWINE_MCIDRIVER wmd;
2279 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2281 wmd = MCI_GetDriver(uDeviceID);
2283 if (!wmd) {
2284 WARN("Bad uDeviceID\n");
2285 return FALSE;
2288 wmd->dwPrivate = data;
2289 return TRUE;
2292 /**************************************************************************
2293 * mciSendCommandW [WINMM.@]
2296 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2298 DWORD dwRet;
2300 TRACE("(%08x, %s, %08lx, %08lx)\n",
2301 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2303 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2304 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2305 TRACE("=> %08x\n", dwRet);
2306 return dwRet;
2309 /**************************************************************************
2310 * mciSendCommandA [WINMM.@]
2312 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2314 DWORD ret;
2315 int mapped;
2317 TRACE("(%08x, %s, %08lx, %08lx)\n",
2318 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2320 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2321 if (mapped == -1)
2323 FIXME("message %04x mapping failed\n", wMsg);
2324 return MCIERR_OUT_OF_MEMORY;
2326 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2327 if (mapped)
2328 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2329 return ret;
2332 /**************************************************************************
2333 * mciGetDeviceIDA [WINMM.@]
2335 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2337 LPWSTR w = MCI_strdupAtoW(lpstrName);
2338 UINT ret = MCIERR_OUT_OF_MEMORY;
2340 if (w)
2342 ret = mciGetDeviceIDW(w);
2343 HeapFree(GetProcessHeap(), 0, w);
2345 return ret;
2348 /**************************************************************************
2349 * mciGetDeviceIDW [WINMM.@]
2351 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2353 return MCI_GetDriverFromString(lpwstrName);
2356 /**************************************************************************
2357 * MCI_DefYieldProc [internal]
2359 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2361 INT16 ret;
2362 MSG msg;
2364 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2366 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2367 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2368 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2369 ret = 0;
2370 } else {
2371 msg.hwnd = HWND_32(HIWORD(data));
2372 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2373 ret = -1;
2375 return ret;
2378 /**************************************************************************
2379 * mciSetYieldProc [WINMM.@]
2381 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2383 LPWINE_MCIDRIVER wmd;
2385 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2387 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2388 WARN("Bad uDeviceID\n");
2389 return FALSE;
2392 wmd->lpfnYieldProc = fpYieldProc;
2393 wmd->dwYieldData = dwYieldData;
2395 return TRUE;
2398 /**************************************************************************
2399 * mciGetDeviceIDFromElementIDA [WINMM.@]
2401 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2403 LPWSTR w = MCI_strdupAtoW(lpstrType);
2404 UINT ret = 0;
2406 if (w)
2408 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2409 HeapFree(GetProcessHeap(), 0, w);
2411 return ret;
2414 /**************************************************************************
2415 * mciGetDeviceIDFromElementIDW [WINMM.@]
2417 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2419 /* FIXME: that's rather strange, there is no
2420 * mciGetDeviceIDFromElementID32A in winmm.spec
2422 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2423 return 0;
2426 /**************************************************************************
2427 * mciGetYieldProc [WINMM.@]
2429 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2431 LPWINE_MCIDRIVER wmd;
2433 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2435 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2436 WARN("Bad uDeviceID\n");
2437 return NULL;
2439 if (!wmd->lpfnYieldProc) {
2440 WARN("No proc set\n");
2441 return NULL;
2443 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2444 return wmd->lpfnYieldProc;
2447 /**************************************************************************
2448 * mciGetCreatorTask [WINMM.@]
2450 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2452 LPWINE_MCIDRIVER wmd;
2453 HTASK ret = 0;
2455 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2457 TRACE("(%u) => %p\n", uDeviceID, ret);
2458 return ret;
2461 /**************************************************************************
2462 * mciDriverYield [WINMM.@]
2464 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2466 LPWINE_MCIDRIVER wmd;
2467 UINT ret = 0;
2469 TRACE("(%04x)\n", uDeviceID);
2471 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2472 MSG msg;
2473 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2474 } else {
2475 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2478 return ret;