api-ms-win-core-console-l1-2-0: Add dll.
[wine.git] / dlls / winmm / mci.c
blobc19ab4532907153a9dbfb12dbd8d8a7cc98b3455
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 [] = L"Software\\Microsoft\\Windows NT\\CurrentVersion\\MCI";
67 static WINE_MCIDRIVER *MciDrivers;
69 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
70 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
72 /* dup a string and uppercase it */
73 static inline LPWSTR str_dup_upper( LPCWSTR str )
75 INT len = (lstrlenW(str) + 1) * sizeof(WCHAR);
76 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
77 if (p)
79 memcpy( p, str, len );
80 CharUpperW( p );
82 return p;
85 /**************************************************************************
86 * MCI_GetDriver [internal]
88 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
90 LPWINE_MCIDRIVER wmd = 0;
92 EnterCriticalSection(&WINMM_cs);
93 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
94 if (wmd->wDeviceID == wDevID)
95 break;
97 LeaveCriticalSection(&WINMM_cs);
98 return wmd;
101 /**************************************************************************
102 * MCI_GetDriverFromString [internal]
104 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
106 LPWINE_MCIDRIVER wmd;
107 UINT ret = 0;
109 if (!lpstrName)
110 return 0;
112 if (!wcsicmp(lpstrName, L"ALL"))
113 return MCI_ALL_DEVICE_ID;
115 EnterCriticalSection(&WINMM_cs);
116 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
117 if (wmd->lpstrAlias && wcsicmp(wmd->lpstrAlias, lpstrName) == 0) {
118 ret = wmd->wDeviceID;
119 break;
122 LeaveCriticalSection(&WINMM_cs);
124 return ret;
127 /**************************************************************************
128 * MCI_MessageToString [internal]
130 static const char* MCI_MessageToString(UINT wMsg)
132 #define CASE(s) case (s): return #s
134 switch (wMsg) {
135 CASE(DRV_LOAD);
136 CASE(DRV_ENABLE);
137 CASE(DRV_OPEN);
138 CASE(DRV_CLOSE);
139 CASE(DRV_DISABLE);
140 CASE(DRV_FREE);
141 CASE(DRV_CONFIGURE);
142 CASE(DRV_QUERYCONFIGURE);
143 CASE(DRV_INSTALL);
144 CASE(DRV_REMOVE);
145 CASE(DRV_EXITSESSION);
146 CASE(DRV_EXITAPPLICATION);
147 CASE(DRV_POWER);
148 CASE(MCI_BREAK);
149 CASE(MCI_CLOSE);
150 CASE(MCI_CLOSE_DRIVER);
151 CASE(MCI_COPY);
152 CASE(MCI_CUE);
153 CASE(MCI_CUT);
154 CASE(MCI_DELETE);
155 CASE(MCI_ESCAPE);
156 CASE(MCI_FREEZE);
157 CASE(MCI_PAUSE);
158 CASE(MCI_PLAY);
159 CASE(MCI_GETDEVCAPS);
160 CASE(MCI_INFO);
161 CASE(MCI_LOAD);
162 CASE(MCI_OPEN);
163 CASE(MCI_OPEN_DRIVER);
164 CASE(MCI_PASTE);
165 CASE(MCI_PUT);
166 CASE(MCI_REALIZE);
167 CASE(MCI_RECORD);
168 CASE(MCI_RESUME);
169 CASE(MCI_SAVE);
170 CASE(MCI_SEEK);
171 CASE(MCI_SET);
172 CASE(MCI_SOUND);
173 CASE(MCI_SPIN);
174 CASE(MCI_STATUS);
175 CASE(MCI_STEP);
176 CASE(MCI_STOP);
177 CASE(MCI_SYSINFO);
178 CASE(MCI_UNFREEZE);
179 CASE(MCI_UPDATE);
180 CASE(MCI_WHERE);
181 CASE(MCI_WINDOW);
182 /* constants for digital video */
183 CASE(MCI_CAPTURE);
184 CASE(MCI_MONITOR);
185 CASE(MCI_RESERVE);
186 CASE(MCI_SETAUDIO);
187 CASE(MCI_SIGNAL);
188 CASE(MCI_SETVIDEO);
189 CASE(MCI_QUALITY);
190 CASE(MCI_LIST);
191 CASE(MCI_UNDO);
192 CASE(MCI_CONFIGURE);
193 CASE(MCI_RESTORE);
194 #undef CASE
195 default:
196 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
200 static LPWSTR MCI_strdupAtoW( LPCSTR str )
202 LPWSTR ret;
203 INT len;
205 if (!str) return NULL;
206 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
207 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
208 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
209 return ret;
212 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
214 if (msg < DRV_RESERVED) return 0;
216 switch (msg)
218 case MCI_CLOSE:
219 case MCI_CONFIGURE:
220 case MCI_PLAY:
221 case MCI_SEEK:
222 case MCI_STOP:
223 case MCI_PAUSE:
224 case MCI_GETDEVCAPS:
225 case MCI_SPIN:
226 case MCI_SET:
227 case MCI_STEP:
228 case MCI_RECORD:
229 case MCI_BREAK:
230 case MCI_STATUS:
231 case MCI_CUE:
232 case MCI_REALIZE:
233 case MCI_PUT:
234 case MCI_WHERE:
235 case MCI_FREEZE:
236 case MCI_UNFREEZE:
237 case MCI_CUT:
238 case MCI_COPY:
239 case MCI_PASTE:
240 case MCI_UPDATE:
241 case MCI_RESUME:
242 case MCI_DELETE:
243 case MCI_MONITOR:
244 case MCI_SIGNAL:
245 case MCI_UNDO:
246 return 0;
248 case MCI_OPEN:
249 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
250 * structure, larger than MCI_WAVE_OPEN_PARMS */
251 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
252 MCI_ANIM_OPEN_PARMSW *mci_openW;
253 DWORD_PTR *ptr;
255 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW));
256 if (!ptr) return -1;
258 *ptr++ = *dwParam2; /* save the previous pointer */
259 *dwParam2 = (DWORD_PTR)ptr;
260 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
262 if (dwParam1 & MCI_NOTIFY)
263 mci_openW->dwCallback = mci_openA->dwCallback;
265 if (dwParam1 & MCI_OPEN_TYPE)
267 if (dwParam1 & MCI_OPEN_TYPE_ID)
268 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
269 else
270 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
272 if (dwParam1 & MCI_OPEN_ELEMENT)
274 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
275 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
276 else
277 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
279 if (dwParam1 & MCI_OPEN_ALIAS)
280 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
281 /* We don't know how many DWORD follow, as
282 * the structure depends on the device. */
283 if (HIWORD(dwParam1))
284 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
286 return 1;
288 case MCI_WINDOW:
289 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
291 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
292 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
294 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
295 if (!mci_windowW) return -1;
297 *dwParam2 = (DWORD_PTR)mci_windowW;
299 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
301 if (dwParam1 & MCI_NOTIFY)
302 mci_windowW->dwCallback = mci_windowA->dwCallback;
303 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
304 mci_windowW->hWnd = mci_windowA->hWnd;
305 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
306 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
308 return 1;
310 return 0;
312 case MCI_SYSINFO:
313 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
315 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
316 MCI_SYSINFO_PARMSW *mci_sysinfoW;
317 DWORD_PTR *ptr;
319 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
320 if (!ptr) return -1;
322 *ptr++ = *dwParam2; /* save the previous pointer */
323 *dwParam2 = (DWORD_PTR)ptr;
324 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
326 if (dwParam1 & MCI_NOTIFY)
327 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
329 /* Size is measured in numbers of characters, despite what MSDN says. */
330 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
331 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize * sizeof(WCHAR));
332 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
333 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
334 return 1;
336 return 0;
337 case MCI_INFO:
339 MCI_DGV_INFO_PARMSA *mci_infoA = (MCI_DGV_INFO_PARMSA *)*dwParam2;
340 MCI_DGV_INFO_PARMSW *mci_infoW;
341 DWORD_PTR *ptr;
343 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
344 if (!ptr) return -1;
346 *ptr++ = *dwParam2; /* save the previous pointer */
347 *dwParam2 = (DWORD_PTR)ptr;
348 mci_infoW = (MCI_DGV_INFO_PARMSW *)ptr;
350 if (dwParam1 & MCI_NOTIFY)
351 mci_infoW->dwCallback = mci_infoA->dwCallback;
353 /* Size is measured in numbers of characters. */
354 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
355 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize * sizeof(WCHAR));
356 if (dwParam1 & MCI_DGV_INFO_ITEM)
357 mci_infoW->dwItem = mci_infoA->dwItem;
358 return 1;
360 case MCI_SAVE:
361 case MCI_LOAD:
362 case MCI_CAPTURE:
363 case MCI_RESTORE:
364 { /* All these commands have the same layout: callback + string + optional rect */
365 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
366 MCI_OVLY_LOAD_PARMSW *mci_loadW;
368 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
369 if (!mci_loadW) return -1;
371 *dwParam2 = (DWORD_PTR)mci_loadW;
372 if (dwParam1 & MCI_NOTIFY)
373 mci_loadW->dwCallback = mci_loadA->dwCallback;
374 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
375 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
376 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
377 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
378 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
379 mci_loadW->rc = mci_loadA->rc;
380 return 1;
382 case MCI_SOUND:
383 case MCI_ESCAPE:
384 { /* All these commands have the same layout: callback + string */
385 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
386 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
388 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
389 if (!mci_vd_escapeW) return -1;
391 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
392 if (dwParam1 & MCI_NOTIFY)
393 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
394 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
395 return 1;
397 case MCI_SETAUDIO:
398 case MCI_SETVIDEO:
399 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
400 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
401 return 0;
402 /* fall through to default */
403 case MCI_RESERVE:
404 case MCI_QUALITY:
405 case MCI_LIST:
406 default:
407 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
408 return 0; /* pass through untouched */
412 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
413 DWORD result)
415 switch (msg)
417 case MCI_OPEN:
419 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
420 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
421 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
423 mci_openA->wDeviceID = mci_openW->wDeviceID;
425 if (dwParam1 & MCI_OPEN_TYPE)
427 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
428 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
430 if (dwParam1 & MCI_OPEN_ELEMENT)
432 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
433 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
435 if (dwParam1 & MCI_OPEN_ALIAS)
436 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
437 HeapFree(GetProcessHeap(), 0, ptr);
439 break;
440 case MCI_WINDOW:
441 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
443 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
445 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
446 HeapFree(GetProcessHeap(), 0, mci_windowW);
448 break;
450 case MCI_SYSINFO:
451 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
453 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
454 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
455 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
457 if (!result)
459 WideCharToMultiByte(CP_ACP, 0,
460 mci_sysinfoW->lpstrReturn, -1,
461 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
462 NULL, NULL);
465 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
466 HeapFree(GetProcessHeap(), 0, ptr);
468 break;
469 case MCI_INFO:
471 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
472 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
473 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
475 if (!result)
477 WideCharToMultiByte(CP_ACP, 0,
478 mci_infoW->lpstrReturn, -1,
479 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
480 NULL, NULL);
483 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
484 HeapFree(GetProcessHeap(), 0, ptr);
486 break;
487 case MCI_SAVE:
488 case MCI_LOAD:
489 case MCI_CAPTURE:
490 case MCI_RESTORE:
491 { /* All these commands have the same layout: callback + string + optional rect */
492 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
494 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
495 HeapFree(GetProcessHeap(), 0, mci_loadW);
497 break;
498 case MCI_SOUND:
499 case MCI_ESCAPE:
500 { /* All these commands have the same layout: callback + string */
501 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
503 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
504 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
506 break;
508 default:
509 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
510 break;
514 /**************************************************************************
515 * MCI_GetDevTypeFromFileName [internal]
517 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
519 LPCWSTR tmp;
520 HKEY hKey;
521 if ((tmp = wcsrchr(fileName, '.'))) {
522 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE,
523 L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions",
524 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
525 DWORD dwLen = len;
526 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
527 RegCloseKey( hKey );
528 if (lRet == ERROR_SUCCESS) return 0;
530 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
532 return MCIERR_EXTENSION_NOT_FOUND;
535 /**************************************************************************
536 * MCI_GetDevTypeFromResource [internal]
538 static UINT MCI_GetDevTypeFromResource(LPCWSTR lpstrName)
540 WCHAR buf[32];
541 UINT uDevType;
542 for (uDevType = MCI_DEVTYPE_FIRST; uDevType <= MCI_DEVTYPE_LAST; uDevType++) {
543 if (LoadStringW(hWinMM32Instance, uDevType, buf, ARRAY_SIZE(buf))) {
544 /* FIXME: ignore digits suffix */
545 if (!wcsicmp(buf, lpstrName))
546 return uDevType;
549 return 0;
552 #define MAX_MCICMDTABLE 20
553 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
555 typedef struct tagWINE_MCICMDTABLE {
556 UINT uDevType;
557 HGLOBAL hMem;
558 const BYTE* lpTable;
559 UINT nVerbs; /* number of verbs in command table */
560 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
561 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
563 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
565 /**************************************************************************
566 * MCI_IsCommandTableValid [internal]
568 static BOOL MCI_IsCommandTableValid(UINT uTbl)
570 const BYTE* lmem;
571 LPCWSTR str;
572 DWORD flg;
573 WORD eid;
574 int idx = 0;
575 BOOL inCst = FALSE;
577 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
578 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
580 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
581 return FALSE;
583 lmem = S_MciCmdTable[uTbl].lpTable;
584 do {
585 str = (LPCWSTR)lmem;
586 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
587 flg = *(const DWORD*)lmem;
588 eid = *(const WORD*)(lmem + sizeof(DWORD));
589 lmem += sizeof(DWORD) + sizeof(WORD);
590 idx ++;
591 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
592 switch (eid) {
593 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
594 case MCI_STRING: if (inCst) return FALSE; break;
595 case MCI_HWND: /* Occurs inside MCI_CONSTANT as in "window handle default" */
596 case MCI_HPAL:
597 case MCI_HDC:
598 case MCI_INTEGER: if (!*str) return FALSE; break;
599 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
600 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
601 case MCI_FLAG: if (!*str) return FALSE; break;
602 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
603 case MCI_RECT: if (!*str || inCst) return FALSE; break;
604 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
605 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
606 default: return FALSE;
608 } while (eid != MCI_END_COMMAND_LIST);
609 return TRUE;
612 /**************************************************************************
613 * MCI_DumpCommandTable [internal]
615 static BOOL MCI_DumpCommandTable(UINT uTbl)
617 const BYTE* lmem;
618 LPCWSTR str;
619 WORD eid;
621 if (!MCI_IsCommandTableValid(uTbl)) {
622 ERR("Ooops: %d is not valid\n", uTbl);
623 return FALSE;
626 lmem = S_MciCmdTable[uTbl].lpTable;
627 do {
628 do {
629 /* DWORD flg; */
630 str = (LPCWSTR)lmem;
631 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
632 /* flg = *(const DWORD*)lmem; */
633 eid = *(const WORD*)(lmem + sizeof(DWORD));
634 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
635 lmem += sizeof(DWORD) + sizeof(WORD);
636 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
637 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
638 } while (eid != MCI_END_COMMAND_LIST);
639 return TRUE;
643 /**************************************************************************
644 * MCI_GetCommandTable [internal]
646 static UINT MCI_GetCommandTable(UINT uDevType)
648 UINT uTbl;
649 WCHAR buf[32];
650 LPCWSTR str = NULL;
652 /* first look up existing for existing devType */
653 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
654 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
655 return uTbl;
658 /* well try to load id */
659 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
660 if (LoadStringW(hWinMM32Instance, uDevType, buf, ARRAY_SIZE(buf))) {
661 str = buf;
663 } else if (uDevType == 0) {
664 str = L"CORE";
666 uTbl = MCI_NO_COMMAND_TABLE;
667 if (str) {
668 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
669 HANDLE hMem = 0;
671 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
672 if (hMem) {
673 uTbl = MCI_SetCommandTable(hMem, uDevType);
674 } else {
675 WARN("No command table found in resource %p[%s]\n",
676 hWinMM32Instance, debugstr_w(str));
679 TRACE("=> %d\n", uTbl);
680 return uTbl;
683 /**************************************************************************
684 * MCI_SetCommandTable [internal]
686 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
688 int uTbl;
689 static BOOL bInitDone = FALSE;
691 /* <HACK>
692 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
693 * can be called with 0 as a uDevType to retrieve it.
694 * </HACK>
696 if (!bInitDone) {
697 bInitDone = TRUE;
698 MCI_GetCommandTable(0);
700 TRACE("(%p, %u)\n", hMem, uDevType);
701 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
702 if (!S_MciCmdTable[uTbl].lpTable) {
703 const BYTE* lmem;
704 LPCWSTR str;
705 WORD eid;
706 WORD count;
708 S_MciCmdTable[uTbl].uDevType = uDevType;
709 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
710 S_MciCmdTable[uTbl].hMem = hMem;
712 if (TRACE_ON(mci)) {
713 MCI_DumpCommandTable(uTbl);
716 /* create the verbs table */
717 /* get # of entries */
718 lmem = S_MciCmdTable[uTbl].lpTable;
719 count = 0;
720 do {
721 str = (LPCWSTR)lmem;
722 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
723 eid = *(const WORD*)(lmem + sizeof(DWORD));
724 lmem += sizeof(DWORD) + sizeof(WORD);
725 if (eid == MCI_COMMAND_HEAD)
726 count++;
727 } while (eid != MCI_END_COMMAND_LIST);
729 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
730 S_MciCmdTable[uTbl].nVerbs = count;
732 lmem = S_MciCmdTable[uTbl].lpTable;
733 count = 0;
734 do {
735 str = (LPCWSTR)lmem;
736 lmem += (lstrlenW(str) + 1) * sizeof(WCHAR);
737 eid = *(const WORD*)(lmem + sizeof(DWORD));
738 lmem += sizeof(DWORD) + sizeof(WORD);
739 if (eid == MCI_COMMAND_HEAD)
740 S_MciCmdTable[uTbl].aVerbs[count++] = str;
741 } while (eid != MCI_END_COMMAND_LIST);
742 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
743 return uTbl;
747 return MCI_NO_COMMAND_TABLE;
750 /**************************************************************************
751 * MCI_UnLoadMciDriver [internal]
753 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
755 LPWINE_MCIDRIVER* tmp;
757 if (!wmd)
758 return TRUE;
760 CloseDriver(wmd->hDriver, 0, 0);
762 if (wmd->dwPrivate != 0)
763 WARN("Unloading mci driver with non nul dwPrivate field\n");
765 EnterCriticalSection(&WINMM_cs);
766 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
767 if (*tmp == wmd) {
768 *tmp = wmd->lpNext;
769 break;
772 LeaveCriticalSection(&WINMM_cs);
774 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
775 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
777 HeapFree(GetProcessHeap(), 0, wmd);
778 return TRUE;
781 /**************************************************************************
782 * MCI_OpenMciDriver [internal]
784 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
786 WCHAR libName[128];
788 if (!DRIVER_GetLibName(drvTyp, L"MCI", libName, sizeof(libName)))
789 return FALSE;
791 /* First load driver */
792 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
793 return wmd->hDriver != NULL;
796 /**************************************************************************
797 * MCI_LoadMciDriver [internal]
799 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
801 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
802 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
803 MCI_OPEN_DRIVER_PARMSW modp;
804 DWORD dwRet = 0;
806 if (!wmd || !strDevTyp) {
807 dwRet = MCIERR_OUT_OF_MEMORY;
808 goto errCleanUp;
811 wmd->lpfnYieldProc = MCI_DefYieldProc;
812 wmd->dwYieldData = VK_CANCEL;
813 wmd->CreatorThread = GetCurrentThreadId();
815 EnterCriticalSection(&WINMM_cs);
816 /* wmd must be inserted in list before sending opening the driver, because it
817 * may want to lookup at wDevID
819 wmd->lpNext = MciDrivers;
820 MciDrivers = wmd;
822 for (modp.wDeviceID = MCI_MAGIC;
823 MCI_GetDriver(modp.wDeviceID) != 0;
824 modp.wDeviceID++);
826 wmd->wDeviceID = modp.wDeviceID;
828 LeaveCriticalSection(&WINMM_cs);
830 TRACE("wDevID=%04X\n", modp.wDeviceID);
832 modp.lpstrParams = NULL;
834 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
835 /* silence warning if all is used... some bogus program use commands like
836 * 'open all'...
838 if (wcsicmp(strDevTyp, L"ALL") == 0) {
839 dwRet = MCIERR_CANNOT_USE_ALL;
840 } else {
841 FIXME("Couldn't load driver for type %s.\n",
842 debugstr_w(strDevTyp));
843 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
845 goto errCleanUp;
848 /* FIXME: should also check that module's description is of the form
849 * MODULENAME:[MCI] comment
852 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
853 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
854 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
856 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
857 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
859 wmd->lpstrDeviceType = strDevTyp;
860 wmd->wType = modp.wType;
862 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
863 modp.wDeviceID, modp.wType, modp.wDeviceID);
864 *lpwmd = wmd;
865 return 0;
866 errCleanUp:
867 MCI_UnLoadMciDriver(wmd);
868 HeapFree(GetProcessHeap(), 0, strDevTyp);
869 *lpwmd = 0;
870 return dwRet;
873 /**************************************************************************
874 * MCI_SendCommandFrom32 [internal]
876 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
878 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
879 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
881 if (wmd) {
882 if(wmd->CreatorThread != GetCurrentThreadId())
883 return MCIERR_INVALID_DEVICE_NAME;
885 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
887 return dwRet;
890 /**************************************************************************
891 * MCI_FinishOpen [internal]
893 * Three modes of operation:
894 * 1 open foo.ext ... -> OPEN_ELEMENT with lpstrElementName=foo.ext
895 * open sequencer!foo.ext same with lpstrElementName=foo.ext
896 * 2 open new type waveaudio -> OPEN_ELEMENT with empty ("") lpstrElementName
897 * 3 open sequencer -> OPEN_ELEMENT unset, and
898 * capability sequencer (auto-open) likewise
900 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
901 DWORD dwParam)
903 LPCWSTR alias = NULL;
904 /* Open always defines an alias for further reference */
905 if (dwParam & MCI_OPEN_ALIAS) { /* open ... alias */
906 alias = lpParms->lpstrAlias;
907 if (MCI_GetDriverFromString(alias))
908 return MCIERR_DUPLICATE_ALIAS;
909 } else {
910 if ((dwParam & MCI_OPEN_ELEMENT) /* open file.wav */
911 && !(dwParam & MCI_OPEN_ELEMENT_ID))
912 alias = lpParms->lpstrElementName;
913 else if (dwParam & MCI_OPEN_TYPE ) /* open cdaudio */
914 alias = wmd->lpstrDeviceType;
915 if (alias && MCI_GetDriverFromString(alias))
916 return MCIERR_DEVICE_OPEN;
918 if (alias) {
919 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(alias)+1) * sizeof(WCHAR));
920 if (!wmd->lpstrAlias) return MCIERR_OUT_OF_MEMORY;
921 lstrcpyW( wmd->lpstrAlias, alias);
922 /* In most cases, natives adds MCI_OPEN_ALIAS to the flags passed to the driver.
923 * Don't. The drivers don't care about the winmm alias. */
925 lpParms->wDeviceID = wmd->wDeviceID;
927 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
928 (DWORD_PTR)lpParms);
931 /**************************************************************************
932 * MCI_FindCommand [internal]
934 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
936 UINT idx;
938 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
939 return NULL;
941 /* another improvement would be to have the aVerbs array sorted,
942 * so that we could use a dichotomic search on it, rather than this dumb
943 * array look up
945 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
946 if (wcsicmp(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
947 return S_MciCmdTable[uTbl].aVerbs[idx];
950 return NULL;
953 /**************************************************************************
954 * MCI_GetReturnType [internal]
956 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
958 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + lstrlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
959 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
960 return *(const DWORD*)(lpCmd + 1);
962 return 0L;
965 /**************************************************************************
966 * MCI_GetMessage [internal]
968 static WORD MCI_GetMessage(LPCWSTR lpCmd)
970 return (WORD)*(const DWORD*)(lpCmd + lstrlenW(lpCmd) + 1);
973 /**************************************************************************
974 * MCI_GetDWord [internal]
976 * Accept 0 -1 255 255:0 255:255:255:255 :::1 1::: 2::3 ::4: 12345678
977 * Refuse -1:0 0:-1 :: 256:0 1:256 0::::1
979 static BOOL MCI_GetDWord(DWORD* data, LPWSTR* ptr)
981 LPWSTR ret = *ptr;
982 DWORD total = 0, shift = 0;
983 BOOL sign = FALSE, digits = FALSE;
985 while (*ret == ' ' || *ret == '\t') ret++;
986 if (*ret == '-') {
987 ret++;
988 sign = TRUE;
990 for(;;) {
991 DWORD val = 0;
992 while ('0' <= *ret && *ret <= '9') {
993 val = *ret++ - '0' + 10 * val;
994 digits = TRUE;
996 switch (*ret) {
997 case '\0': break;
998 case '\t':
999 case ' ': ret++; break;
1000 default: return FALSE;
1001 case ':':
1002 if ((val >= 256) || (shift >= 24)) return FALSE;
1003 total |= val << shift;
1004 shift += 8;
1005 ret++;
1006 continue;
1009 if (!digits) return FALSE;
1010 if (shift && (val >= 256 || sign)) return FALSE;
1011 total |= val << shift;
1012 *data = sign ? -total : total;
1013 *ptr = ret;
1014 return TRUE;
1018 /**************************************************************************
1019 * MCI_GetString [internal]
1021 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1023 LPWSTR ptr = *args;
1025 /* see if we have a quoted string */
1026 if (*ptr == '"') {
1027 ptr = wcschr(*str = ptr + 1, '"');
1028 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1029 /* FIXME: shall we escape \" from string ?? */
1030 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1031 *ptr++ = '\0'; /* remove trailing " */
1032 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1033 } else {
1034 ptr = wcschr(ptr, ' ');
1036 if (ptr) {
1037 *ptr++ = '\0';
1038 } else {
1039 ptr = *args + lstrlenW(*args);
1041 *str = *args;
1044 *args = ptr;
1045 return 0;
1048 #define MCI_DATA_SIZE 16
1050 /**************************************************************************
1051 * MCI_ParseOptArgs [internal]
1053 static DWORD MCI_ParseOptArgs(DWORD* data, int _offset, LPCWSTR lpCmd,
1054 LPWSTR args, LPDWORD dwFlags)
1056 int len, offset;
1057 const char* lmem;
1058 LPCWSTR str;
1059 DWORD dwRet, flg, cflg = 0;
1060 WORD eid;
1061 BOOL inCst, found;
1063 /* loop on arguments */
1064 while (*args) {
1065 lmem = (const char*)lpCmd;
1066 found = inCst = FALSE;
1067 offset = _offset;
1069 /* skip any leading white space(s) */
1070 while (*args == ' ') args++;
1071 TRACE("args=%s\n", debugstr_w(args));
1073 do { /* loop on options for command table for the requested verb */
1074 str = (LPCWSTR)lmem;
1075 lmem += ((len = lstrlenW(str)) + 1) * sizeof(WCHAR);
1076 flg = *(const DWORD*)lmem;
1077 eid = *(const WORD*)(lmem + sizeof(DWORD));
1078 lmem += sizeof(DWORD) + sizeof(WORD);
1079 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1081 switch (eid) {
1082 case MCI_CONSTANT:
1083 inCst = TRUE; cflg = flg; break;
1084 case MCI_END_CONSTANT:
1085 /* there may be additional integral values after flag in constant */
1086 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1087 *dwFlags |= cflg;
1089 inCst = FALSE; cflg = 0;
1090 break;
1091 case MCI_RETURN:
1092 if (offset != _offset) {
1093 FIXME("MCI_RETURN not in first position\n");
1094 return MCIERR_PARSER_INTERNAL;
1098 if (wcsnicmp(args, str, len) == 0 &&
1099 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1100 /* store good values into data[] */
1101 args += len;
1102 while (*args == ' ') args++;
1103 found = TRUE;
1105 switch (eid) {
1106 case MCI_COMMAND_HEAD:
1107 case MCI_RETURN:
1108 case MCI_END_COMMAND:
1109 case MCI_END_COMMAND_LIST:
1110 case MCI_CONSTANT: /* done above */
1111 case MCI_END_CONSTANT: /* done above */
1112 break;
1113 case MCI_FLAG:
1114 *dwFlags |= flg;
1115 TRACE("flag=%08x\n", flg);
1116 break;
1117 case MCI_HWND:
1118 case MCI_HPAL:
1119 case MCI_HDC:
1120 case MCI_INTEGER:
1121 if (inCst) {
1122 data[offset] |= flg;
1123 *dwFlags |= cflg;
1124 inCst = FALSE;
1125 TRACE("flag=%08x constant=%08x\n", cflg, flg);
1126 } else {
1127 *dwFlags |= flg;
1128 if (!MCI_GetDWord(&(data[offset]), &args)) {
1129 return MCIERR_BAD_INTEGER;
1131 TRACE("flag=%08x int=%d\n", flg, data[offset]);
1133 break;
1134 case MCI_RECT:
1135 /* store rect in data (offset..offset+3) */
1136 *dwFlags |= flg;
1137 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1138 !MCI_GetDWord(&(data[offset+1]), &args) ||
1139 !MCI_GetDWord(&(data[offset+2]), &args) ||
1140 !MCI_GetDWord(&(data[offset+3]), &args)) {
1141 return MCIERR_BAD_INTEGER;
1143 TRACE("flag=%08x for rectangle\n", flg);
1144 break;
1145 case MCI_STRING:
1146 *dwFlags |= flg;
1147 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1148 return dwRet;
1149 TRACE("flag=%08x string=%s\n", flg, debugstr_w(*(LPWSTR*)&data[offset]));
1150 break;
1151 default: ERR("oops\n");
1153 /* exit inside while loop, except if just entered in constant area definition */
1154 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1155 } else {
1156 /* have offset incremented if needed */
1157 switch (eid) {
1158 case MCI_COMMAND_HEAD:
1159 case MCI_RETURN:
1160 case MCI_END_COMMAND:
1161 case MCI_END_COMMAND_LIST:
1162 case MCI_CONSTANT:
1163 case MCI_FLAG: break;
1164 case MCI_HWND:
1165 case MCI_HPAL:
1166 case MCI_HDC: if (!inCst) offset += sizeof(HANDLE)/sizeof(DWORD); break;
1167 case MCI_INTEGER: if (!inCst) offset++; break;
1168 case MCI_END_CONSTANT: offset++; break;
1169 case MCI_STRING: offset += sizeof(LPWSTR)/sizeof(DWORD); break;
1170 case MCI_RECT: offset += 4; break;
1171 default: ERR("oops\n");
1174 } while (eid != MCI_END_COMMAND);
1175 if (!found) {
1176 WARN("Optarg %s not found\n", debugstr_w(args));
1177 return MCIERR_UNRECOGNIZED_COMMAND;
1179 if (offset == MCI_DATA_SIZE) {
1180 FIXME("Internal data[] buffer overflow\n");
1181 return MCIERR_PARSER_INTERNAL;
1184 return 0;
1187 /**************************************************************************
1188 * MCI_HandleReturnValues [internal]
1190 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1191 MCI_GENERIC_PARMS *params, LPWSTR lpstrRet, UINT uRetLen)
1193 if (lpstrRet) {
1194 switch (retType) {
1195 case 0: /* nothing to return */
1196 break;
1197 case MCI_INTEGER:
1199 DWORD data = *(DWORD *)(params + 1);
1200 switch (dwRet & 0xFFFF0000ul) {
1201 case 0:
1202 case MCI_INTEGER_RETURNED:
1203 swprintf(lpstrRet, uRetLen, L"%d", data);
1204 break;
1205 case MCI_RESOURCE_RETURNED:
1206 /* return string which ID is HIWORD(data),
1207 * string is loaded from mmsystem.dll */
1208 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1209 break;
1210 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1211 /* return string which ID is HIWORD(data),
1212 * string is loaded from driver */
1213 /* FIXME: this is wrong for a 16 bit handle */
1214 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1215 HIWORD(data), lpstrRet, uRetLen);
1216 break;
1217 case MCI_COLONIZED3_RETURN:
1218 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d",
1219 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1220 LOBYTE(HIWORD(data)));
1221 break;
1222 case MCI_COLONIZED4_RETURN:
1223 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d:%02d",
1224 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1225 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1226 break;
1227 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1229 break;
1231 #ifdef MCI_INTEGER64
1232 case MCI_INTEGER64:
1234 DWORD_PTR data = *(DWORD_PTR *)(params + 1);
1235 switch (dwRet & 0xFFFF0000ul) {
1236 case 0:
1237 case MCI_INTEGER_RETURNED:
1238 swprintf(lpstrRet, uRetLen, L"%ld", data);
1239 break;
1240 case MCI_RESOURCE_RETURNED:
1241 /* return string which ID is HIWORD(data),
1242 * string is loaded from mmsystem.dll */
1243 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1244 break;
1245 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1246 /* return string which ID is HIWORD(data),
1247 * string is loaded from driver */
1248 /* FIXME: this is wrong for a 16 bit handle */
1249 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1250 HIWORD(data), lpstrRet, uRetLen);
1251 break;
1252 case MCI_COLONIZED3_RETURN:
1253 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d",
1254 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1255 LOBYTE(HIWORD(data)));
1256 break;
1257 case MCI_COLONIZED4_RETURN:
1258 swprintf(lpstrRet, uRetLen, L"%02d:%02d:%02d:%02d",
1259 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1260 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1261 break;
1262 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1264 break;
1266 #endif
1267 case MCI_STRING:
1268 switch (dwRet & 0xFFFF0000ul) {
1269 case 0:
1270 /* nothing to do data[0] == lpstrRet */
1271 break;
1272 case MCI_INTEGER_RETURNED:
1274 DWORD *data = (DWORD *)(params + 1);
1275 *data = *(LPDWORD)lpstrRet;
1276 swprintf(lpstrRet, uRetLen, L"%d", *data);
1277 break;
1279 default:
1280 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1281 break;
1283 break;
1284 case MCI_RECT:
1286 DWORD *data = (DWORD *)(params + 1);
1287 if (dwRet & 0xFFFF0000ul)
1288 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1289 swprintf(lpstrRet, uRetLen, L"%d %d %d %d", data[0], data[1], data[2], data[3]);
1290 break;
1292 default: FIXME("Unknown MCI return type %d\n", retType);
1295 return LOWORD(dwRet);
1298 /**************************************************************************
1299 * mciSendStringW [WINMM.@]
1301 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1302 UINT uRetLen, HWND hwndCallback)
1304 LPWSTR verb, dev, args, devType = NULL;
1305 LPWINE_MCIDRIVER wmd = 0;
1306 MCIDEVICEID uDevID, auto_open = 0;
1307 DWORD dwFlags = 0, dwRet = 0;
1308 int offset = 0;
1309 DWORD retType;
1310 LPCWSTR lpCmd = 0;
1311 WORD wMsg = 0;
1312 union
1314 MCI_GENERIC_PARMS generic;
1315 MCI_OPEN_PARMSW open;
1316 MCI_SOUND_PARMSW sound;
1317 MCI_SYSINFO_PARMSW sysinfo;
1318 DWORD dw[MCI_DATA_SIZE];
1319 } data;
1321 TRACE("(%s, %p, %d, %p)\n",
1322 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1323 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1325 if (!lpstrCommand[0])
1326 return MCIERR_MISSING_COMMAND_STRING;
1328 /* format is <command> <device> <optargs> */
1329 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1330 return MCIERR_OUT_OF_MEMORY;
1331 lstrcpyW( verb, lpstrCommand );
1332 CharLowerW(verb);
1334 memset(&data, 0, sizeof(data));
1336 if (!(args = wcschr(verb, ' '))) {
1337 dwRet = MCIERR_MISSING_DEVICE_NAME;
1338 goto errCleanUp;
1340 *args++ = '\0';
1341 if ((dwRet = MCI_GetString(&dev, &args))) {
1342 goto errCleanUp;
1344 uDevID = wcsicmp(dev, L"ALL") ? 0 : MCI_ALL_DEVICE_ID;
1346 /* Determine devType from open */
1347 if (!wcscmp(verb, L"open")) {
1348 LPWSTR tmp;
1349 WCHAR buf[128];
1351 /* case dev == 'new' has to be handled */
1352 if (!wcscmp(dev, L"new")) {
1353 dev = 0;
1354 if ((devType = wcsstr(args, L"type ")) != NULL) {
1355 devType += 5;
1356 tmp = wcschr(devType, ' ');
1357 if (tmp) *tmp = '\0';
1358 devType = str_dup_upper(devType);
1359 if (tmp) *tmp = ' ';
1360 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1361 } else {
1362 WARN("open new requires device type\n");
1363 dwRet = MCIERR_MISSING_DEVICE_NAME;
1364 goto errCleanUp;
1366 dwFlags |= MCI_OPEN_ELEMENT;
1367 data.open.lpstrElementName = &L""[0];
1368 } else if ((devType = wcschr(dev, '!')) != NULL) {
1369 *devType++ = '\0';
1370 tmp = devType; devType = dev; dev = tmp;
1372 dwFlags |= MCI_OPEN_TYPE;
1373 data.open.lpstrDeviceType = devType;
1374 devType = str_dup_upper(devType);
1375 dwFlags |= MCI_OPEN_ELEMENT;
1376 data.open.lpstrElementName = dev;
1377 } else if (DRIVER_GetLibName(dev, L"MCI", buf, sizeof(buf))) {
1378 /* this is the name of a mci driver's type */
1379 tmp = wcschr(dev, ' ');
1380 if (tmp) *tmp = '\0';
1381 data.open.lpstrDeviceType = dev;
1382 devType = str_dup_upper(dev);
1383 if (tmp) *tmp = ' ';
1384 dwFlags |= MCI_OPEN_TYPE;
1385 } else {
1386 if ((devType = wcsstr(args, L"type ")) != NULL) {
1387 devType += 5;
1388 tmp = wcschr(devType, ' ');
1389 if (tmp) *tmp = '\0';
1390 devType = str_dup_upper(devType);
1391 if (tmp) *tmp = ' ';
1392 /* dwFlags and lpstrDeviceType will be correctly set in ParseOpt loop */
1393 } else {
1394 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1395 goto errCleanUp;
1397 devType = str_dup_upper(buf);
1399 dwFlags |= MCI_OPEN_ELEMENT;
1400 data.open.lpstrElementName = dev;
1402 if (MCI_ALL_DEVICE_ID == uDevID) {
1403 dwRet = MCIERR_CANNOT_USE_ALL;
1404 goto errCleanUp;
1406 if (!wcsstr(args, L" alias ") && !dev) {
1407 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1408 goto errCleanUp;
1411 dwRet = MCI_LoadMciDriver(devType, &wmd);
1412 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1413 dwRet = MCIERR_INVALID_DEVICE_NAME;
1414 if (dwRet)
1415 goto errCleanUp;
1416 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))
1417 && (lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb))) {
1418 /* auto-open uses the core command table */
1419 switch (MCI_GetMessage(lpCmd)) {
1420 case MCI_SOUND: /* command does not use a device name */
1421 case MCI_SYSINFO:
1422 break;
1423 case MCI_CLOSE: /* don't auto-open for close */
1424 case MCI_BREAK: /* no auto-open for system commands */
1425 dwRet = MCIERR_INVALID_DEVICE_NAME;
1426 goto errCleanUp;
1427 break;
1428 default:
1430 WCHAR buf[138], retbuf[6];
1431 swprintf(buf, ARRAY_SIZE(buf), L"open %s wait", dev);
1432 /* open via mciSendString handles quoting, dev!file syntax and alias creation */
1433 if ((dwRet = mciSendStringW(buf, retbuf, ARRAY_SIZE(retbuf), 0)) != 0)
1434 goto errCleanUp;
1435 auto_open = wcstoul(retbuf, NULL, 10);
1436 TRACE("auto-opened %u for %s\n", auto_open, debugstr_w(dev));
1438 /* FIXME: test for notify flag (how to preparse?) before opening */
1439 wmd = MCI_GetDriver(auto_open);
1440 if (!wmd) {
1441 ERR("No auto-open device %u\n", auto_open);
1442 dwRet = MCIERR_INVALID_DEVICE_ID;
1443 goto errCleanUp;
1449 /* get the verb in the different command tables */
1450 if (wmd) {
1451 /* try the device specific command table */
1452 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1453 if (!lpCmd) {
1454 /* try the type specific command table */
1455 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1456 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1457 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1458 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1461 /* try core command table */
1462 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1464 if (!lpCmd) {
1465 TRACE("Command %s not found!\n", debugstr_w(verb));
1466 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1467 goto errCleanUp;
1469 wMsg = MCI_GetMessage(lpCmd);
1471 /* set return information */
1472 offset = sizeof(data.generic);
1473 switch (retType = MCI_GetReturnType(lpCmd)) {
1474 case 0:
1475 break;
1476 case MCI_INTEGER:
1477 offset += sizeof(DWORD);
1478 break;
1479 case MCI_STRING:
1480 data.sysinfo.lpstrReturn = lpstrRet;
1481 data.sysinfo.dwRetSize = uRetLen;
1482 offset = FIELD_OFFSET( MCI_SYSINFO_PARMSW, dwNumber );
1483 break;
1484 case MCI_RECT:
1485 offset += 4 * sizeof(DWORD);
1486 break;
1487 #ifdef MCI_INTEGER64
1488 case MCI_INTEGER64:
1489 offset += sizeof(DWORD_PTR);
1490 break;
1491 #endif
1492 default:
1493 FIXME("Unknown MCI return type %d\n", retType);
1494 dwRet = MCIERR_PARSER_INTERNAL;
1495 goto errCleanUp;
1498 TRACE("verb=%s on dev=%s; offset=%d\n",
1499 debugstr_w(verb), debugstr_w(dev), offset);
1501 if ((dwRet = MCI_ParseOptArgs(data.dw, offset / sizeof(DWORD), lpCmd, args, &dwFlags)))
1502 goto errCleanUp;
1504 /* set up call back */
1505 if (auto_open) {
1506 if (dwFlags & MCI_NOTIFY) {
1507 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1508 goto errCleanUp;
1510 /* FIXME: the command should get its own notification window set up and
1511 * ask for device closing while processing the notification mechanism.
1512 * hwndCallback = ...
1513 * dwFlags |= MCI_NOTIFY;
1514 * In the meantime special-case all commands but PLAY and RECORD below. */
1516 if (dwFlags & MCI_NOTIFY) {
1517 data.generic.dwCallback = (DWORD_PTR)hwndCallback;
1520 switch (wMsg) {
1521 case MCI_OPEN:
1522 if (wcscmp(verb, L"open")) {
1523 FIXME("Cannot open with command %s\n", debugstr_w(verb));
1524 dwRet = MCIERR_DRIVER_INTERNAL;
1525 wMsg = 0;
1526 goto errCleanUp;
1528 break;
1529 case MCI_SYSINFO:
1530 /* Requirements on dev depend on the flags:
1531 * alias with INSTALLNAME, name like "digitalvideo"
1532 * with QUANTITY and NAME. */
1534 data.sysinfo.wDeviceType = MCI_ALL_DEVICE_ID;
1535 if (uDevID != MCI_ALL_DEVICE_ID) {
1536 if (dwFlags & MCI_SYSINFO_INSTALLNAME)
1537 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1538 else if (!(data.sysinfo.wDeviceType = MCI_GetDevTypeFromResource(dev))) {
1539 dwRet = MCIERR_DEVICE_TYPE_REQUIRED;
1540 goto errCleanUp;
1544 break;
1545 case MCI_SOUND:
1546 /* FIXME: name is optional, "sound" is a valid command.
1547 * FIXME: Parse "sound notify" as flag, not as name. */
1548 data.sound.lpstrSoundName = dev;
1549 dwFlags |= MCI_SOUND_NAME;
1550 break;
1553 TRACE("[%d, %s, %08x, %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x]\n",
1554 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(wMsg), dwFlags,
1555 data.dw[0], data.dw[1], data.dw[2], data.dw[3], data.dw[4],
1556 data.dw[5], data.dw[6], data.dw[7], data.dw[8], data.dw[9]);
1558 if (wMsg == MCI_OPEN) {
1559 if ((dwRet = MCI_FinishOpen(wmd, &data.open, dwFlags)))
1560 goto errCleanUp;
1561 /* FIXME: notification is not properly shared across two opens */
1562 } else {
1563 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, wMsg, dwFlags, (DWORD_PTR)&data);
1565 if (!LOWORD(dwRet)) {
1566 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1567 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, &data.generic, lpstrRet, uRetLen);
1568 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1569 } else
1570 TRACE("=> %x\n", dwRet);
1572 errCleanUp:
1573 if (auto_open) {
1574 /* PLAY and RECORD are the only known non-immediate commands */
1575 if (LOWORD(dwRet) || !(wMsg == MCI_PLAY || wMsg == MCI_RECORD))
1576 MCI_SendCommand(auto_open, MCI_CLOSE, 0, 0);
1577 else
1578 FIXME("leaking auto-open device %u\n", auto_open);
1580 if (wMsg == MCI_OPEN && LOWORD(dwRet) && wmd)
1581 MCI_UnLoadMciDriver(wmd);
1582 HeapFree(GetProcessHeap(), 0, devType);
1583 HeapFree(GetProcessHeap(), 0, verb);
1584 return dwRet;
1587 /**************************************************************************
1588 * mciSendStringA [WINMM.@]
1590 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1591 UINT uRetLen, HWND hwndCallback)
1593 LPWSTR lpwstrCommand;
1594 LPWSTR lpwstrRet = NULL;
1595 UINT ret;
1596 INT len;
1598 /* FIXME: is there something to do with lpstrReturnString ? */
1599 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1600 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1601 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1602 if (lpstrRet)
1604 if (uRetLen) *lpstrRet = '\0'; /* NT-w2k3 use memset(lpstrRet, 0, uRetLen); */
1605 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1606 if (!lpwstrRet) {
1607 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1608 return MCIERR_OUT_OF_MEMORY;
1611 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1612 if (!ret && lpwstrRet)
1613 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1614 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1615 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1616 return ret;
1619 /**************************************************************************
1620 * mciExecute [WINMM.@]
1622 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1624 char strRet[256];
1625 DWORD ret;
1627 TRACE("(%s)!\n", lpstrCommand);
1629 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1630 if (ret != 0) {
1631 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1632 sprintf(strRet, "Unknown MCI error (%d)", ret);
1634 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1636 /* FIXME: what shall I return ? */
1637 return TRUE;
1640 /**************************************************************************
1641 * mciLoadCommandResource [WINMM.@]
1643 * Strangely, this function only exists as a UNICODE one.
1645 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1647 UINT ret = MCI_NO_COMMAND_TABLE;
1648 HRSRC hRsrc = 0;
1649 HGLOBAL hMem;
1651 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1653 /* if a file named "resname.mci" exits, then load resource "resname" from it
1654 * otherwise directly from driver
1655 * We don't support it (who uses this feature ?), but we check anyway
1657 if (!type) {
1658 #if 0
1659 /* FIXME: we should put this back into order, but I never found a program
1660 * actually using this feature, so we may not need it
1662 char buf[128];
1663 OFSTRUCT ofs;
1665 strcat(strcpy(buf, resname), ".mci");
1666 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1667 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1669 #endif
1671 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1672 (hMem = LoadResource(hInst, hRsrc))) {
1673 ret = MCI_SetCommandTable(hMem, type);
1674 FreeResource(hMem);
1676 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1678 TRACE("=> %04x\n", ret);
1679 return ret;
1682 /**************************************************************************
1683 * mciFreeCommandResource [WINMM.@]
1685 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1687 TRACE("(%08x)!\n", uTable);
1689 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1690 return FALSE;
1692 FreeResource(S_MciCmdTable[uTable].hMem);
1693 S_MciCmdTable[uTable].hMem = NULL;
1694 S_MciCmdTable[uTable].lpTable = NULL;
1695 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1696 S_MciCmdTable[uTable].aVerbs = 0;
1697 S_MciCmdTable[uTable].nVerbs = 0;
1698 return TRUE;
1701 /**************************************************************************
1702 * MCI_Open [internal]
1704 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1706 WCHAR strDevTyp[128];
1707 DWORD dwRet;
1708 LPWINE_MCIDRIVER wmd = NULL;
1710 TRACE("(%08X, %p)\n", dwParam, lpParms);
1711 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1713 /* only two low bytes are generic, the other ones are dev type specific */
1714 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1715 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1716 MCI_NOTIFY|MCI_WAIT)
1717 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0)
1718 FIXME("Unsupported yet dwFlags=%08X\n", dwParam);
1719 #undef WINE_MCIDRIVER_SUPP
1721 strDevTyp[0] = 0;
1723 if (dwParam & MCI_OPEN_TYPE) {
1724 if (dwParam & MCI_OPEN_TYPE_ID) {
1725 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1727 if (uDevType < MCI_DEVTYPE_FIRST || uDevType > MCI_DEVTYPE_LAST ||
1728 !LoadStringW(hWinMM32Instance, uDevType, strDevTyp, ARRAY_SIZE(strDevTyp))) {
1729 dwRet = MCIERR_BAD_INTEGER;
1730 goto errCleanUp;
1732 } else {
1733 LPWSTR ptr;
1734 if (lpParms->lpstrDeviceType == NULL) {
1735 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1736 goto errCleanUp;
1738 lstrcpyW(strDevTyp, lpParms->lpstrDeviceType);
1739 ptr = wcschr(strDevTyp, '!');
1740 if (ptr) {
1741 /* this behavior is not documented in windows. However, since, in
1742 * some occasions, MCI_OPEN handling is translated by WinMM into
1743 * a call to mciSendString("open <type>"); this code shall be correct
1745 if (dwParam & MCI_OPEN_ELEMENT) {
1746 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1747 debugstr_w(lpParms->lpstrElementName),
1748 debugstr_w(strDevTyp));
1749 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1750 goto errCleanUp;
1752 dwParam |= MCI_OPEN_ELEMENT;
1753 *ptr++ = 0;
1754 /* FIXME: not a good idea to write in user supplied buffer */
1755 lpParms->lpstrElementName = ptr;
1759 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1762 if (dwParam & MCI_OPEN_ELEMENT) {
1763 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1765 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1766 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1767 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1768 goto errCleanUp;
1771 if (!lpParms->lpstrElementName) {
1772 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1773 goto errCleanUp;
1776 /* type, if given as a parameter, supersedes file extension */
1777 if (!strDevTyp[0] &&
1778 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1779 strDevTyp, sizeof(strDevTyp))) {
1780 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1781 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1782 goto errCleanUp;
1784 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1785 lstrcpyW(strDevTyp, L"CDAUDIO");
1789 if (strDevTyp[0] == 0) {
1790 FIXME("Couldn't load driver\n");
1791 dwRet = MCIERR_INVALID_DEVICE_NAME;
1792 goto errCleanUp;
1795 if (dwParam & MCI_OPEN_ALIAS) {
1796 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1797 if (!lpParms->lpstrAlias) {
1798 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1799 goto errCleanUp;
1803 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1804 goto errCleanUp;
1807 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1808 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1809 /* FIXME: is dwRet the correct ret code ? */
1810 goto errCleanUp;
1813 /* only handled devices fall through */
1814 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1815 return 0;
1817 errCleanUp:
1818 if (wmd) MCI_UnLoadMciDriver(wmd);
1819 return dwRet;
1822 /**************************************************************************
1823 * MCI_Close [internal]
1825 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1827 DWORD dwRet;
1828 LPWINE_MCIDRIVER wmd;
1830 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1832 /* Every device must handle MCI_NOTIFY on its own. */
1833 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1834 while (MciDrivers) {
1835 /* Retrieve the device ID under lock, but send the message without,
1836 * the driver might be calling some winmm functions from another
1837 * thread before being fully stopped.
1839 EnterCriticalSection(&WINMM_cs);
1840 if (!MciDrivers)
1842 LeaveCriticalSection(&WINMM_cs);
1843 break;
1845 wDevID = MciDrivers->wDeviceID;
1846 LeaveCriticalSection(&WINMM_cs);
1847 MCI_Close(wDevID, dwParam, lpParms);
1849 return 0;
1852 if (!(wmd = MCI_GetDriver(wDevID))) {
1853 return MCIERR_INVALID_DEVICE_ID;
1856 if(wmd->CreatorThread != GetCurrentThreadId())
1857 return MCIERR_INVALID_DEVICE_NAME;
1859 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1861 MCI_UnLoadMciDriver(wmd);
1863 return dwRet;
1866 /**************************************************************************
1867 * MCI_WriteString [internal]
1869 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1871 DWORD ret = 0;
1873 if (lpSrcStr) {
1874 if (dstSize <= lstrlenW(lpSrcStr)) {
1875 ret = MCIERR_PARAM_OVERFLOW;
1876 } else {
1877 lstrcpyW(lpDstStr, lpSrcStr);
1879 } else {
1880 *lpDstStr = 0;
1882 return ret;
1885 /**************************************************************************
1886 * MCI_Sysinfo [internal]
1888 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1890 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1891 WCHAR buf[2048], *s, *p;
1892 LPWINE_MCIDRIVER wmd;
1893 HKEY hKey;
1895 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1896 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1898 TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1899 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1900 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1901 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1903 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1904 case MCI_SYSINFO_QUANTITY:
1905 if (lpParms->dwRetSize < sizeof(DWORD))
1906 return MCIERR_PARAM_OVERFLOW;
1907 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1908 if (uDevID == MCI_ALL_DEVICE_ID) {
1909 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1910 if (dwFlags & MCI_SYSINFO_OPEN) {
1911 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1912 EnterCriticalSection(&WINMM_cs);
1913 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1914 cnt++;
1916 LeaveCriticalSection(&WINMM_cs);
1917 } else {
1918 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1919 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1920 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1921 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1922 RegCloseKey( hKey );
1924 if (GetPrivateProfileStringW(L"MCI", 0, L"", buf, ARRAY_SIZE(buf), L"system.ini"))
1925 for (s = buf; *s; s += lstrlenW(s) + 1) cnt++;
1927 } else {
1928 if (dwFlags & MCI_SYSINFO_OPEN) {
1929 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1930 EnterCriticalSection(&WINMM_cs);
1931 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1932 if (wmd->wType == lpParms->wDeviceType) cnt++;
1934 LeaveCriticalSection(&WINMM_cs);
1935 } else {
1936 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1937 FIXME("Don't know how to get # of MCI devices of a given type\n");
1938 /* name = LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType))
1939 * then lookup registry and/or system.ini for name, ignoring digits suffix */
1940 switch (LOWORD(lpParms->wDeviceType)) {
1941 case MCI_DEVTYPE_CD_AUDIO:
1942 case MCI_DEVTYPE_WAVEFORM_AUDIO:
1943 case MCI_DEVTYPE_SEQUENCER:
1944 cnt = 1;
1945 break;
1946 default: /* "digitalvideo" gets 0 because it's not in the registry */
1947 cnt = 0;
1951 *(DWORD*)lpParms->lpstrReturn = cnt;
1952 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1953 ret = MCI_INTEGER_RETURNED;
1954 /* return ret; Only Win9x sends a notification in this case. */
1955 break;
1956 case MCI_SYSINFO_INSTALLNAME:
1957 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1958 if ((wmd = MCI_GetDriver(uDevID))) {
1959 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1960 wmd->lpstrDeviceType);
1961 } else {
1962 ret = (uDevID == MCI_ALL_DEVICE_ID)
1963 ? MCIERR_CANNOT_USE_ALL : MCIERR_INVALID_DEVICE_NAME;
1965 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1966 break;
1967 case MCI_SYSINFO_NAME:
1968 s = NULL;
1969 if (dwFlags & MCI_SYSINFO_OPEN) {
1970 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1971 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1972 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1973 EnterCriticalSection(&WINMM_cs);
1974 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1975 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1976 if (uDevID == MCI_ALL_DEVICE_ID ||
1977 lpParms->wDeviceType == wmd->wType) {
1978 cnt++;
1979 if (cnt == lpParms->dwNumber) {
1980 s = wmd->lpstrAlias;
1981 break;
1985 LeaveCriticalSection(&WINMM_cs);
1986 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1987 } else if (MCI_ALL_DEVICE_ID == uDevID) {
1988 TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
1989 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1990 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1991 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1992 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1993 lpParms->dwNumber <= cnt) {
1994 DWORD bufLen = ARRAY_SIZE(buf);
1995 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1996 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1997 s = buf;
1999 RegCloseKey( hKey );
2001 if (!s) {
2002 if (GetPrivateProfileStringW(L"MCI", 0, L"", buf, ARRAY_SIZE(buf), L"system.ini")) {
2003 for (p = buf; *p; p += lstrlenW(p) + 1, cnt++) {
2004 TRACE("%d: %s\n", cnt, debugstr_w(p));
2005 if (cnt == lpParms->dwNumber - 1) {
2006 s = p;
2007 break;
2012 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
2013 } else {
2014 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
2015 /* Cheating: what is asked for is the nth device from the registry. */
2016 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
2017 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
2018 ret = MCIERR_OUTOFRANGE;
2019 else {
2020 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
2021 lpParms->lpstrReturn, lpParms->dwRetSize);
2022 ret = 0;
2025 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
2026 break;
2027 default:
2028 TRACE("Unsupported flag value=%08x\n", dwFlags);
2029 ret = MCIERR_UNRECOGNIZED_KEYWORD;
2031 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
2032 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
2033 return ret;
2036 /**************************************************************************
2037 * MCI_Break [internal]
2039 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
2041 DWORD dwRet;
2043 if (lpParms == NULL)
2044 return MCIERR_NULL_PARAMETER_BLOCK;
2046 TRACE("(%08x, %08X, vkey %04X, hwnd %p)\n", wDevID, dwFlags,
2047 lpParms->nVirtKey, lpParms->hwndBreak);
2049 dwRet = MCI_SendCommandFrom32(wDevID, MCI_BREAK, dwFlags, (DWORD_PTR)lpParms);
2050 if (!dwRet && (dwFlags & MCI_NOTIFY))
2051 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2052 return dwRet;
2055 /**************************************************************************
2056 * MCI_Sound [internal]
2058 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
2060 DWORD dwRet;
2062 if (dwFlags & MCI_SOUND_NAME) {
2063 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2064 else dwRet = PlaySoundW(lpParms->lpstrSoundName, NULL,
2065 SND_ALIAS | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2066 ? 0 : MCIERR_HARDWARE;
2067 } else dwRet = PlaySoundW((LPCWSTR)SND_ALIAS_SYSTEMDEFAULT, NULL,
2068 SND_ALIAS_ID | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2069 ? 0 : MCIERR_HARDWARE;
2071 if (!dwRet && lpParms && (dwFlags & MCI_NOTIFY))
2072 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2073 return dwRet;
2076 /**************************************************************************
2077 * MCI_SendCommand [internal]
2079 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2081 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
2083 switch (wMsg) {
2084 case MCI_OPEN:
2085 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
2086 break;
2087 case MCI_CLOSE:
2088 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
2089 break;
2090 case MCI_SYSINFO:
2091 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2092 break;
2093 case MCI_BREAK:
2094 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2095 break;
2096 case MCI_SOUND:
2097 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2098 break;
2099 default:
2100 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
2101 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2102 dwRet = MCIERR_CANNOT_USE_ALL;
2103 } else {
2104 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
2106 break;
2108 return dwRet;
2111 /**************************************************************************
2112 * MCI_CleanUp [internal]
2114 * Some MCI commands need to be cleaned-up (when not called from
2115 * mciSendString), because MCI drivers return extra information for string
2116 * transformation. This function gets rid of them.
2118 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
2120 if (LOWORD(dwRet))
2121 return LOWORD(dwRet);
2123 switch (wMsg) {
2124 case MCI_GETDEVCAPS:
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_GETDEVCAPS_PARMS lmgp;
2137 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
2138 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2139 lmgp->dwReturn = LOWORD(lmgp->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_STATUS:
2148 switch (dwRet & 0xFFFF0000ul) {
2149 case 0:
2150 case MCI_COLONIZED3_RETURN:
2151 case MCI_COLONIZED4_RETURN:
2152 case MCI_INTEGER_RETURNED:
2153 /* nothing to do */
2154 break;
2155 case MCI_RESOURCE_RETURNED:
2156 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2158 LPMCI_STATUS_PARMS lsp;
2160 lsp = (LPMCI_STATUS_PARMS)dwParam2;
2161 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2162 lsp->dwReturn = LOWORD(lsp->dwReturn);
2164 break;
2165 default:
2166 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2167 HIWORD(dwRet), MCI_MessageToString(wMsg));
2169 break;
2170 case MCI_SYSINFO:
2171 switch (dwRet & 0xFFFF0000ul) {
2172 case 0:
2173 case MCI_INTEGER_RETURNED:
2174 /* nothing to do */
2175 break;
2176 default:
2177 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2179 break;
2180 default:
2181 if (HIWORD(dwRet)) {
2182 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2183 dwRet, MCI_MessageToString(wMsg));
2185 break;
2187 return LOWORD(dwRet);
2190 /**************************************************************************
2191 * mciGetErrorStringW [WINMM.@]
2193 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2195 BOOL ret = FALSE;
2197 if (lpstrBuffer != NULL && uLength > 0 &&
2198 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2200 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2201 ret = TRUE;
2204 return ret;
2207 /**************************************************************************
2208 * mciGetErrorStringA [WINMM.@]
2210 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2212 BOOL ret = FALSE;
2214 if (lpstrBuffer != NULL && uLength > 0 &&
2215 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2217 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2218 ret = TRUE;
2221 return ret;
2224 /**************************************************************************
2225 * mciDriverNotify [WINMM.@]
2227 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2229 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2231 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2234 /**************************************************************************
2235 * mciGetDriverData [WINMM.@]
2237 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2239 LPWINE_MCIDRIVER wmd;
2241 TRACE("(%04x)\n", uDeviceID);
2243 wmd = MCI_GetDriver(uDeviceID);
2245 if (!wmd) {
2246 WARN("Bad uDeviceID\n");
2247 return 0L;
2250 return wmd->dwPrivate;
2253 /**************************************************************************
2254 * mciSetDriverData [WINMM.@]
2256 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2258 LPWINE_MCIDRIVER wmd;
2260 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2262 wmd = MCI_GetDriver(uDeviceID);
2264 if (!wmd) {
2265 WARN("Bad uDeviceID\n");
2266 return FALSE;
2269 wmd->dwPrivate = data;
2270 return TRUE;
2273 /**************************************************************************
2274 * mciSendCommandW [WINMM.@]
2277 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2279 DWORD dwRet;
2281 TRACE("(%08x, %s, %08lx, %08lx)\n",
2282 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2284 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2285 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2286 TRACE("=> %08x\n", dwRet);
2287 return dwRet;
2290 /**************************************************************************
2291 * mciSendCommandA [WINMM.@]
2293 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2295 DWORD ret;
2296 int mapped;
2298 TRACE("(%08x, %s, %08lx, %08lx)\n",
2299 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2301 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2302 if (mapped == -1)
2304 FIXME("message %04x mapping failed\n", wMsg);
2305 return MCIERR_OUT_OF_MEMORY;
2307 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2308 if (mapped)
2309 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2310 return ret;
2313 /**************************************************************************
2314 * mciGetDeviceIDA [WINMM.@]
2316 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2318 LPWSTR w = MCI_strdupAtoW(lpstrName);
2319 UINT ret = MCIERR_OUT_OF_MEMORY;
2321 if (w)
2323 ret = mciGetDeviceIDW(w);
2324 HeapFree(GetProcessHeap(), 0, w);
2326 return ret;
2329 /**************************************************************************
2330 * mciGetDeviceIDW [WINMM.@]
2332 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2334 return MCI_GetDriverFromString(lpwstrName);
2337 /**************************************************************************
2338 * MCI_DefYieldProc [internal]
2340 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2342 INT16 ret;
2343 MSG msg;
2345 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2347 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2348 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2349 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2350 ret = 0;
2351 } else {
2352 msg.hwnd = HWND_32(HIWORD(data));
2353 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2354 ret = -1;
2356 return ret;
2359 /**************************************************************************
2360 * mciSetYieldProc [WINMM.@]
2362 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2364 LPWINE_MCIDRIVER wmd;
2366 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2368 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2369 WARN("Bad uDeviceID\n");
2370 return FALSE;
2373 wmd->lpfnYieldProc = fpYieldProc;
2374 wmd->dwYieldData = dwYieldData;
2376 return TRUE;
2379 /**************************************************************************
2380 * mciGetDeviceIDFromElementIDA [WINMM.@]
2382 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2384 LPWSTR w = MCI_strdupAtoW(lpstrType);
2385 UINT ret = 0;
2387 if (w)
2389 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2390 HeapFree(GetProcessHeap(), 0, w);
2392 return ret;
2395 /**************************************************************************
2396 * mciGetDeviceIDFromElementIDW [WINMM.@]
2398 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2400 /* FIXME: that's rather strange, there is no
2401 * mciGetDeviceIDFromElementID32A in winmm.spec
2403 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2404 return 0;
2407 /**************************************************************************
2408 * mciGetYieldProc [WINMM.@]
2410 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2412 LPWINE_MCIDRIVER wmd;
2414 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2416 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2417 WARN("Bad uDeviceID\n");
2418 return NULL;
2420 if (!wmd->lpfnYieldProc) {
2421 WARN("No proc set\n");
2422 return NULL;
2424 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2425 return wmd->lpfnYieldProc;
2428 /**************************************************************************
2429 * mciGetCreatorTask [WINMM.@]
2431 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2433 LPWINE_MCIDRIVER wmd;
2434 HTASK ret = 0;
2436 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2438 TRACE("(%u) => %p\n", uDeviceID, ret);
2439 return ret;
2442 /**************************************************************************
2443 * mciDriverYield [WINMM.@]
2445 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2447 LPWINE_MCIDRIVER wmd;
2448 UINT ret = 0;
2450 TRACE("(%04x)\n", uDeviceID);
2452 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2453 MSG msg;
2454 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2455 } else {
2456 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2459 return ret;