powrprof: Stub PowerSetActiveScheme.
[wine.git] / dlls / winmm / mci.c
blobb8adea6119a83c67aa659c22762cc7e6d370038e
1 /*
2 * MCI internal functions
4 * Copyright 1998/1999 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* TODO:
22 * - implement WINMM (32bit) multitasking and use it in all MCI drivers
23 * instead of the home grown one
24 * - 16bit mmTaskXXX functions are currently broken because the 16
25 * loader does not support binary command lines => provide Wine's
26 * own mmtask.tsk not using binary command line.
27 * - correctly handle the MCI_ALL_DEVICE_ID in functions.
28 * - finish mapping 16 <=> 32 of MCI structures and commands
29 * - implement auto-open feature (ie, when a string command is issued
30 * for a not yet opened device, MCI automatically opens it)
31 * - use a default registry setting to replace the [mci] section in
32 * configuration file (layout of info in registry should be compatible
33 * with all Windows' version - which use different layouts of course)
34 * - implement automatic open
35 * + only works on string interface, on regular devices (don't work on all
36 * nor custom devices)
37 * - command table handling isn't thread safe
40 #include "config.h"
41 #include "wine/port.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "mmsystem.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "winreg.h"
55 #include "wownt32.h"
57 #include "digitalv.h"
58 #include "winemm.h"
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(mci);
65 /* First MCI valid device ID (0 means error) */
66 #define MCI_MAGIC 0x0001
68 /* MCI settings */
69 static const WCHAR wszHklmMci [] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','M','C','I',0};
70 static const WCHAR wszNull [] = {0};
71 static const WCHAR wszAll [] = {'A','L','L',0};
72 static const WCHAR wszMci [] = {'M','C','I',0};
73 static const WCHAR wszOpen [] = {'o','p','e','n',0};
74 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
76 static WINE_MCIDRIVER *MciDrivers;
78 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
79 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
81 /* dup a string and uppercase it */
82 static inline LPWSTR str_dup_upper( LPCWSTR str )
84 INT len = (strlenW(str) + 1) * sizeof(WCHAR);
85 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
86 if (p)
88 memcpy( p, str, len );
89 CharUpperW( p );
91 return p;
94 /**************************************************************************
95 * MCI_GetDriver [internal]
97 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
99 LPWINE_MCIDRIVER wmd = 0;
101 EnterCriticalSection(&WINMM_cs);
102 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
103 if (wmd->wDeviceID == wDevID)
104 break;
106 LeaveCriticalSection(&WINMM_cs);
107 return wmd;
110 /**************************************************************************
111 * MCI_GetDriverFromString [internal]
113 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
115 LPWINE_MCIDRIVER wmd;
116 UINT ret = 0;
118 if (!lpstrName)
119 return 0;
121 if (!strcmpiW(lpstrName, wszAll))
122 return MCI_ALL_DEVICE_ID;
124 EnterCriticalSection(&WINMM_cs);
125 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
126 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
127 ret = wmd->wDeviceID;
128 break;
131 LeaveCriticalSection(&WINMM_cs);
133 return ret;
136 /**************************************************************************
137 * MCI_MessageToString [internal]
139 static const char* MCI_MessageToString(UINT wMsg)
141 #define CASE(s) case (s): return #s
143 switch (wMsg) {
144 CASE(DRV_LOAD);
145 CASE(DRV_ENABLE);
146 CASE(DRV_OPEN);
147 CASE(DRV_CLOSE);
148 CASE(DRV_DISABLE);
149 CASE(DRV_FREE);
150 CASE(DRV_CONFIGURE);
151 CASE(DRV_QUERYCONFIGURE);
152 CASE(DRV_INSTALL);
153 CASE(DRV_REMOVE);
154 CASE(DRV_EXITSESSION);
155 CASE(DRV_EXITAPPLICATION);
156 CASE(DRV_POWER);
157 CASE(MCI_BREAK);
158 CASE(MCI_CLOSE);
159 CASE(MCI_CLOSE_DRIVER);
160 CASE(MCI_COPY);
161 CASE(MCI_CUE);
162 CASE(MCI_CUT);
163 CASE(MCI_DELETE);
164 CASE(MCI_ESCAPE);
165 CASE(MCI_FREEZE);
166 CASE(MCI_PAUSE);
167 CASE(MCI_PLAY);
168 CASE(MCI_GETDEVCAPS);
169 CASE(MCI_INFO);
170 CASE(MCI_LOAD);
171 CASE(MCI_OPEN);
172 CASE(MCI_OPEN_DRIVER);
173 CASE(MCI_PASTE);
174 CASE(MCI_PUT);
175 CASE(MCI_REALIZE);
176 CASE(MCI_RECORD);
177 CASE(MCI_RESUME);
178 CASE(MCI_SAVE);
179 CASE(MCI_SEEK);
180 CASE(MCI_SET);
181 CASE(MCI_SOUND);
182 CASE(MCI_SPIN);
183 CASE(MCI_STATUS);
184 CASE(MCI_STEP);
185 CASE(MCI_STOP);
186 CASE(MCI_SYSINFO);
187 CASE(MCI_UNFREEZE);
188 CASE(MCI_UPDATE);
189 CASE(MCI_WHERE);
190 CASE(MCI_WINDOW);
191 /* constants for digital video */
192 CASE(MCI_CAPTURE);
193 CASE(MCI_MONITOR);
194 CASE(MCI_RESERVE);
195 CASE(MCI_SETAUDIO);
196 CASE(MCI_SIGNAL);
197 CASE(MCI_SETVIDEO);
198 CASE(MCI_QUALITY);
199 CASE(MCI_LIST);
200 CASE(MCI_UNDO);
201 CASE(MCI_CONFIGURE);
202 CASE(MCI_RESTORE);
203 #undef CASE
204 default:
205 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
209 static LPWSTR MCI_strdupAtoW( LPCSTR str )
211 LPWSTR ret;
212 INT len;
214 if (!str) return NULL;
215 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
216 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
217 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
218 return ret;
221 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
223 if (msg < DRV_RESERVED) return 0;
225 switch (msg)
227 case MCI_CLOSE:
228 case MCI_CONFIGURE:
229 case MCI_PLAY:
230 case MCI_SEEK:
231 case MCI_STOP:
232 case MCI_PAUSE:
233 case MCI_GETDEVCAPS:
234 case MCI_SPIN:
235 case MCI_SET:
236 case MCI_STEP:
237 case MCI_RECORD:
238 case MCI_BREAK:
239 case MCI_STATUS:
240 case MCI_CUE:
241 case MCI_REALIZE:
242 case MCI_PUT:
243 case MCI_WHERE:
244 case MCI_FREEZE:
245 case MCI_UNFREEZE:
246 case MCI_CUT:
247 case MCI_COPY:
248 case MCI_PASTE:
249 case MCI_UPDATE:
250 case MCI_RESUME:
251 case MCI_DELETE:
252 case MCI_MONITOR:
253 case MCI_SIGNAL:
254 case MCI_UNDO:
255 return 0;
257 case MCI_OPEN:
258 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
259 * structure, larger than MCI_WAVE_OPEN_PARMS */
260 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
261 MCI_ANIM_OPEN_PARMSW *mci_openW;
262 DWORD_PTR *ptr;
264 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW));
265 if (!ptr) return -1;
267 *ptr++ = *dwParam2; /* save the previous pointer */
268 *dwParam2 = (DWORD_PTR)ptr;
269 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
271 if (dwParam1 & MCI_NOTIFY)
272 mci_openW->dwCallback = mci_openA->dwCallback;
274 if (dwParam1 & MCI_OPEN_TYPE)
276 if (dwParam1 & MCI_OPEN_TYPE_ID)
277 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
278 else
279 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
281 if (dwParam1 & MCI_OPEN_ELEMENT)
283 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
284 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
285 else
286 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
288 if (dwParam1 & MCI_OPEN_ALIAS)
289 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
290 /* We don't know how many DWORD follow, as
291 * the structure depends on the device. */
292 if (HIWORD(dwParam1))
293 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
295 return 1;
297 case MCI_WINDOW:
298 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
300 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
301 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
303 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
304 if (!mci_windowW) return -1;
306 *dwParam2 = (DWORD_PTR)mci_windowW;
308 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
310 if (dwParam1 & MCI_NOTIFY)
311 mci_windowW->dwCallback = mci_windowA->dwCallback;
312 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
313 mci_windowW->hWnd = mci_windowA->hWnd;
314 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
315 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
317 return 1;
319 return 0;
321 case MCI_SYSINFO:
322 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
324 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
325 MCI_SYSINFO_PARMSW *mci_sysinfoW;
326 DWORD_PTR *ptr;
328 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
329 if (!ptr) return -1;
331 *ptr++ = *dwParam2; /* save the previous pointer */
332 *dwParam2 = (DWORD_PTR)ptr;
333 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
335 if (dwParam1 & MCI_NOTIFY)
336 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
338 /* Size is measured in numbers of characters, despite what MSDN says. */
339 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
340 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize * sizeof(WCHAR));
341 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
342 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
343 return 1;
345 return 0;
346 case MCI_INFO:
348 MCI_DGV_INFO_PARMSA *mci_infoA = (MCI_DGV_INFO_PARMSA *)*dwParam2;
349 MCI_DGV_INFO_PARMSW *mci_infoW;
350 DWORD_PTR *ptr;
352 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
353 if (!ptr) return -1;
355 *ptr++ = *dwParam2; /* save the previous pointer */
356 *dwParam2 = (DWORD_PTR)ptr;
357 mci_infoW = (MCI_DGV_INFO_PARMSW *)ptr;
359 if (dwParam1 & MCI_NOTIFY)
360 mci_infoW->dwCallback = mci_infoA->dwCallback;
362 /* Size is measured in numbers of characters. */
363 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
364 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize * sizeof(WCHAR));
365 if (dwParam1 & MCI_DGV_INFO_ITEM)
366 mci_infoW->dwItem = mci_infoA->dwItem;
367 return 1;
369 case MCI_SAVE:
370 case MCI_LOAD:
371 case MCI_CAPTURE:
372 case MCI_RESTORE:
373 { /* All these commands have the same layout: callback + string + optional rect */
374 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
375 MCI_OVLY_LOAD_PARMSW *mci_loadW;
377 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
378 if (!mci_loadW) return -1;
380 *dwParam2 = (DWORD_PTR)mci_loadW;
381 if (dwParam1 & MCI_NOTIFY)
382 mci_loadW->dwCallback = mci_loadA->dwCallback;
383 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
384 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
385 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
386 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
387 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
388 mci_loadW->rc = mci_loadA->rc;
389 return 1;
391 case MCI_SOUND:
392 case MCI_ESCAPE:
393 { /* All these commands have the same layout: callback + string */
394 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
395 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
397 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
398 if (!mci_vd_escapeW) return -1;
400 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
401 if (dwParam1 & MCI_NOTIFY)
402 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
403 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
404 return 1;
406 case MCI_SETAUDIO:
407 case MCI_SETVIDEO:
408 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
409 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
410 return 0;
411 /* fall through to default */
412 case MCI_RESERVE:
413 case MCI_QUALITY:
414 case MCI_LIST:
415 default:
416 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
417 return 0; /* pass through untouched */
421 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
422 DWORD result)
424 switch (msg)
426 case MCI_OPEN:
428 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
429 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
430 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
432 mci_openA->wDeviceID = mci_openW->wDeviceID;
434 if (dwParam1 & MCI_OPEN_TYPE)
436 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
437 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
439 if (dwParam1 & MCI_OPEN_ELEMENT)
441 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
442 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
444 if (dwParam1 & MCI_OPEN_ALIAS)
445 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
446 HeapFree(GetProcessHeap(), 0, ptr);
448 break;
449 case MCI_WINDOW:
450 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
452 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
454 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
455 HeapFree(GetProcessHeap(), 0, mci_windowW);
457 break;
459 case MCI_SYSINFO:
460 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
462 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
463 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
464 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
466 if (!result)
468 WideCharToMultiByte(CP_ACP, 0,
469 mci_sysinfoW->lpstrReturn, -1,
470 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
471 NULL, NULL);
474 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
475 HeapFree(GetProcessHeap(), 0, ptr);
477 break;
478 case MCI_INFO:
480 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
481 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
482 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
484 if (!result)
486 WideCharToMultiByte(CP_ACP, 0,
487 mci_infoW->lpstrReturn, -1,
488 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
489 NULL, NULL);
492 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
493 HeapFree(GetProcessHeap(), 0, ptr);
495 break;
496 case MCI_SAVE:
497 case MCI_LOAD:
498 case MCI_CAPTURE:
499 case MCI_RESTORE:
500 { /* All these commands have the same layout: callback + string + optional rect */
501 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
503 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
504 HeapFree(GetProcessHeap(), 0, mci_loadW);
506 break;
507 case MCI_SOUND:
508 case MCI_ESCAPE:
509 { /* All these commands have the same layout: callback + string */
510 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
512 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
513 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
515 break;
517 default:
518 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
519 break;
523 /**************************************************************************
524 * MCI_GetDevTypeFromFileName [internal]
526 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
528 LPCWSTR tmp;
529 HKEY hKey;
530 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
531 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
532 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
533 if ((tmp = strrchrW(fileName, '.'))) {
534 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
535 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
536 DWORD dwLen = len;
537 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
538 RegCloseKey( hKey );
539 if (lRet == ERROR_SUCCESS) return 0;
541 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
543 return MCIERR_EXTENSION_NOT_FOUND;
546 /**************************************************************************
547 * MCI_GetDevTypeFromResource [internal]
549 static UINT MCI_GetDevTypeFromResource(LPCWSTR lpstrName)
551 WCHAR buf[32];
552 UINT uDevType;
553 for (uDevType = MCI_DEVTYPE_FIRST; uDevType <= MCI_DEVTYPE_LAST; uDevType++) {
554 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
555 /* FIXME: ignore digits suffix */
556 if (!strcmpiW(buf, lpstrName))
557 return uDevType;
560 return 0;
563 #define MAX_MCICMDTABLE 20
564 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
566 typedef struct tagWINE_MCICMDTABLE {
567 UINT uDevType;
568 HGLOBAL hMem;
569 const BYTE* lpTable;
570 UINT nVerbs; /* number of verbs in command table */
571 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
572 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
574 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
576 /**************************************************************************
577 * MCI_IsCommandTableValid [internal]
579 static BOOL MCI_IsCommandTableValid(UINT uTbl)
581 const BYTE* lmem;
582 LPCWSTR str;
583 DWORD flg;
584 WORD eid;
585 int idx = 0;
586 BOOL inCst = FALSE;
588 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
589 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
591 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
592 return FALSE;
594 lmem = S_MciCmdTable[uTbl].lpTable;
595 do {
596 str = (LPCWSTR)lmem;
597 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
598 flg = *(const DWORD*)lmem;
599 eid = *(const WORD*)(lmem + sizeof(DWORD));
600 lmem += sizeof(DWORD) + sizeof(WORD);
601 idx ++;
602 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
603 switch (eid) {
604 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
605 case MCI_STRING: if (inCst) return FALSE; break;
606 case MCI_HWND: /* Occurs inside MCI_CONSTANT as in "window handle default" */
607 case MCI_HPAL:
608 case MCI_HDC:
609 case MCI_INTEGER: if (!*str) return FALSE; break;
610 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
611 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
612 case MCI_FLAG: if (!*str) return FALSE; break;
613 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
614 case MCI_RECT: if (!*str || inCst) return FALSE; break;
615 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
616 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
617 default: return FALSE;
619 } while (eid != MCI_END_COMMAND_LIST);
620 return TRUE;
623 /**************************************************************************
624 * MCI_DumpCommandTable [internal]
626 static BOOL MCI_DumpCommandTable(UINT uTbl)
628 const BYTE* lmem;
629 LPCWSTR str;
630 WORD eid;
632 if (!MCI_IsCommandTableValid(uTbl)) {
633 ERR("Ooops: %d is not valid\n", uTbl);
634 return FALSE;
637 lmem = S_MciCmdTable[uTbl].lpTable;
638 do {
639 do {
640 /* DWORD flg; */
641 str = (LPCWSTR)lmem;
642 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
643 /* flg = *(const DWORD*)lmem; */
644 eid = *(const WORD*)(lmem + sizeof(DWORD));
645 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
646 lmem += sizeof(DWORD) + sizeof(WORD);
647 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
648 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
649 } while (eid != MCI_END_COMMAND_LIST);
650 return TRUE;
654 /**************************************************************************
655 * MCI_GetCommandTable [internal]
657 static UINT MCI_GetCommandTable(UINT uDevType)
659 UINT uTbl;
660 WCHAR buf[32];
661 LPCWSTR str = NULL;
663 /* first look up existing for existing devType */
664 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
665 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
666 return uTbl;
669 /* well try to load id */
670 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
671 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
672 str = buf;
674 } else if (uDevType == 0) {
675 static const WCHAR wszCore[] = {'C','O','R','E',0};
676 str = wszCore;
678 uTbl = MCI_NO_COMMAND_TABLE;
679 if (str) {
680 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
681 HANDLE hMem = 0;
683 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
684 if (hMem) {
685 uTbl = MCI_SetCommandTable(hMem, uDevType);
686 } else {
687 WARN("No command table found in resource %p[%s]\n",
688 hWinMM32Instance, debugstr_w(str));
691 TRACE("=> %d\n", uTbl);
692 return uTbl;
695 /**************************************************************************
696 * MCI_SetCommandTable [internal]
698 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
700 int uTbl;
701 static BOOL bInitDone = FALSE;
703 /* <HACK>
704 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
705 * can be called with 0 as a uDevType to retrieve it.
706 * </HACK>
708 if (!bInitDone) {
709 bInitDone = TRUE;
710 MCI_GetCommandTable(0);
712 TRACE("(%p, %u)\n", hMem, uDevType);
713 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
714 if (!S_MciCmdTable[uTbl].lpTable) {
715 const BYTE* lmem;
716 LPCWSTR str;
717 WORD eid;
718 WORD count;
720 S_MciCmdTable[uTbl].uDevType = uDevType;
721 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
722 S_MciCmdTable[uTbl].hMem = hMem;
724 if (TRACE_ON(mci)) {
725 MCI_DumpCommandTable(uTbl);
728 /* create the verbs table */
729 /* get # of entries */
730 lmem = S_MciCmdTable[uTbl].lpTable;
731 count = 0;
732 do {
733 str = (LPCWSTR)lmem;
734 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
735 eid = *(const WORD*)(lmem + sizeof(DWORD));
736 lmem += sizeof(DWORD) + sizeof(WORD);
737 if (eid == MCI_COMMAND_HEAD)
738 count++;
739 } while (eid != MCI_END_COMMAND_LIST);
741 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
742 S_MciCmdTable[uTbl].nVerbs = count;
744 lmem = S_MciCmdTable[uTbl].lpTable;
745 count = 0;
746 do {
747 str = (LPCWSTR)lmem;
748 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
749 eid = *(const WORD*)(lmem + sizeof(DWORD));
750 lmem += sizeof(DWORD) + sizeof(WORD);
751 if (eid == MCI_COMMAND_HEAD)
752 S_MciCmdTable[uTbl].aVerbs[count++] = str;
753 } while (eid != MCI_END_COMMAND_LIST);
754 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
755 return uTbl;
759 return MCI_NO_COMMAND_TABLE;
762 /**************************************************************************
763 * MCI_UnLoadMciDriver [internal]
765 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
767 LPWINE_MCIDRIVER* tmp;
769 if (!wmd)
770 return TRUE;
772 CloseDriver(wmd->hDriver, 0, 0);
774 if (wmd->dwPrivate != 0)
775 WARN("Unloading mci driver with non nul dwPrivate field\n");
777 EnterCriticalSection(&WINMM_cs);
778 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
779 if (*tmp == wmd) {
780 *tmp = wmd->lpNext;
781 break;
784 LeaveCriticalSection(&WINMM_cs);
786 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
787 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
789 HeapFree(GetProcessHeap(), 0, wmd);
790 return TRUE;
793 /**************************************************************************
794 * MCI_OpenMciDriver [internal]
796 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
798 WCHAR libName[128];
800 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
801 return FALSE;
803 /* First load driver */
804 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
805 return wmd->hDriver != NULL;
808 /**************************************************************************
809 * MCI_LoadMciDriver [internal]
811 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
813 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
814 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
815 MCI_OPEN_DRIVER_PARMSW modp;
816 DWORD dwRet = 0;
818 if (!wmd || !strDevTyp) {
819 dwRet = MCIERR_OUT_OF_MEMORY;
820 goto errCleanUp;
823 wmd->lpfnYieldProc = MCI_DefYieldProc;
824 wmd->dwYieldData = VK_CANCEL;
825 wmd->CreatorThread = GetCurrentThreadId();
827 EnterCriticalSection(&WINMM_cs);
828 /* wmd must be inserted in list before sending opening the driver, because it
829 * may want to lookup at wDevID
831 wmd->lpNext = MciDrivers;
832 MciDrivers = wmd;
834 for (modp.wDeviceID = MCI_MAGIC;
835 MCI_GetDriver(modp.wDeviceID) != 0;
836 modp.wDeviceID++);
838 wmd->wDeviceID = modp.wDeviceID;
840 LeaveCriticalSection(&WINMM_cs);
842 TRACE("wDevID=%04X\n", modp.wDeviceID);
844 modp.lpstrParams = NULL;
846 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
847 /* silence warning if all is used... some bogus program use commands like
848 * 'open all'...
850 if (strcmpiW(strDevTyp, wszAll) == 0) {
851 dwRet = MCIERR_CANNOT_USE_ALL;
852 } else {
853 FIXME("Couldn't load driver for type %s.\n",
854 debugstr_w(strDevTyp));
855 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
857 goto errCleanUp;
860 /* FIXME: should also check that module's description is of the form
861 * MODULENAME:[MCI] comment
864 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
865 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
866 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
868 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
869 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
871 wmd->lpstrDeviceType = strDevTyp;
872 wmd->wType = modp.wType;
874 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
875 modp.wDeviceID, modp.wType, modp.wDeviceID);
876 *lpwmd = wmd;
877 return 0;
878 errCleanUp:
879 MCI_UnLoadMciDriver(wmd);
880 HeapFree(GetProcessHeap(), 0, strDevTyp);
881 *lpwmd = 0;
882 return dwRet;
885 /**************************************************************************
886 * MCI_SendCommandFrom32 [internal]
888 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
890 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
891 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
893 if (wmd) {
894 if(wmd->CreatorThread != GetCurrentThreadId())
895 return MCIERR_INVALID_DEVICE_NAME;
897 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
899 return dwRet;
902 /**************************************************************************
903 * MCI_FinishOpen [internal]
905 * Three modes of operation:
906 * 1 open foo.ext ... -> OPEN_ELEMENT with lpstrElementName=foo.ext
907 * open sequencer!foo.ext same with lpstrElementName=foo.ext
908 * 2 open new type waveaudio -> OPEN_ELEMENT with empty ("") lpstrElementName
909 * 3 open sequencer -> OPEN_ELEMENT unset, and
910 * capability sequencer (auto-open) likewise
912 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
913 DWORD dwParam)
915 LPCWSTR alias = NULL;
916 /* Open always defines an alias for further reference */
917 if (dwParam & MCI_OPEN_ALIAS) { /* open ... alias */
918 alias = lpParms->lpstrAlias;
919 if (MCI_GetDriverFromString(alias))
920 return MCIERR_DUPLICATE_ALIAS;
921 } else {
922 if ((dwParam & MCI_OPEN_ELEMENT) /* open file.wav */
923 && !(dwParam & MCI_OPEN_ELEMENT_ID))
924 alias = lpParms->lpstrElementName;
925 else if (dwParam & MCI_OPEN_TYPE ) /* open cdaudio */
926 alias = wmd->lpstrDeviceType;
927 if (alias && MCI_GetDriverFromString(alias))
928 return MCIERR_DEVICE_OPEN;
930 if (alias) {
931 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(alias)+1) * sizeof(WCHAR));
932 if (!wmd->lpstrAlias) return MCIERR_OUT_OF_MEMORY;
933 strcpyW( wmd->lpstrAlias, alias);
934 /* In most cases, natives adds MCI_OPEN_ALIAS to the flags passed to the driver.
935 * Don't. The drivers don't care about the winmm alias. */
937 lpParms->wDeviceID = wmd->wDeviceID;
939 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
940 (DWORD_PTR)lpParms);
943 /**************************************************************************
944 * MCI_FindCommand [internal]
946 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
948 UINT idx;
950 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
951 return NULL;
953 /* another improvement would be to have the aVerbs array sorted,
954 * so that we could use a dichotomic search on it, rather than this dumb
955 * array look up
957 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
958 if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
959 return S_MciCmdTable[uTbl].aVerbs[idx];
962 return NULL;
965 /**************************************************************************
966 * MCI_GetReturnType [internal]
968 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
970 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
971 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
972 return *(const DWORD*)(lpCmd + 1);
974 return 0L;
977 /**************************************************************************
978 * MCI_GetMessage [internal]
980 static WORD MCI_GetMessage(LPCWSTR lpCmd)
982 return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
985 /**************************************************************************
986 * MCI_GetDWord [internal]
988 * Accept 0 -1 255 255:0 255:255:255:255 :::1 1::: 2::3 ::4: 12345678
989 * Refuse -1:0 0:-1 :: 256:0 1:256 0::::1
991 static BOOL MCI_GetDWord(DWORD* data, LPWSTR* ptr)
993 LPWSTR ret = *ptr;
994 DWORD total = 0, shift = 0;
995 BOOL sign = FALSE, digits = FALSE;
997 while (*ret == ' ' || *ret == '\t') ret++;
998 if (*ret == '-') {
999 ret++;
1000 sign = TRUE;
1002 for(;;) {
1003 DWORD val = 0;
1004 while ('0' <= *ret && *ret <= '9') {
1005 val = *ret++ - '0' + 10 * val;
1006 digits = TRUE;
1008 switch (*ret) {
1009 case '\0': break;
1010 case '\t':
1011 case ' ': ret++; break;
1012 default: return FALSE;
1013 case ':':
1014 if ((val >= 256) || (shift >= 24)) return FALSE;
1015 total |= val << shift;
1016 shift += 8;
1017 ret++;
1018 continue;
1021 if (!digits) return FALSE;
1022 if (shift && (val >= 256 || sign)) return FALSE;
1023 total |= val << shift;
1024 *data = sign ? -total : total;
1025 *ptr = ret;
1026 return TRUE;
1030 /**************************************************************************
1031 * MCI_GetString [internal]
1033 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1035 LPWSTR ptr = *args;
1037 /* see if we have a quoted string */
1038 if (*ptr == '"') {
1039 ptr = strchrW(*str = ptr + 1, '"');
1040 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1041 /* FIXME: shall we escape \" from string ?? */
1042 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1043 *ptr++ = '\0'; /* remove trailing " */
1044 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1045 } else {
1046 ptr = strchrW(ptr, ' ');
1048 if (ptr) {
1049 *ptr++ = '\0';
1050 } else {
1051 ptr = *args + strlenW(*args);
1053 *str = *args;
1056 *args = ptr;
1057 return 0;
1060 #define MCI_DATA_SIZE 16
1062 /**************************************************************************
1063 * MCI_ParseOptArgs [internal]
1065 static DWORD MCI_ParseOptArgs(DWORD* data, int _offset, LPCWSTR lpCmd,
1066 LPWSTR args, LPDWORD dwFlags)
1068 int len, offset;
1069 const char* lmem;
1070 LPCWSTR str;
1071 DWORD dwRet, flg, cflg = 0;
1072 WORD eid;
1073 BOOL inCst, found;
1075 /* loop on arguments */
1076 while (*args) {
1077 lmem = (const char*)lpCmd;
1078 found = inCst = FALSE;
1079 offset = _offset;
1081 /* skip any leading white space(s) */
1082 while (*args == ' ') args++;
1083 TRACE("args=%s\n", debugstr_w(args));
1085 do { /* loop on options for command table for the requested verb */
1086 str = (LPCWSTR)lmem;
1087 lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1088 flg = *(const DWORD*)lmem;
1089 eid = *(const WORD*)(lmem + sizeof(DWORD));
1090 lmem += sizeof(DWORD) + sizeof(WORD);
1091 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1093 switch (eid) {
1094 case MCI_CONSTANT:
1095 inCst = TRUE; cflg = flg; break;
1096 case MCI_END_CONSTANT:
1097 /* there may be additional integral values after flag in constant */
1098 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1099 *dwFlags |= cflg;
1101 inCst = FALSE; cflg = 0;
1102 break;
1103 case MCI_RETURN:
1104 if (offset != _offset) {
1105 FIXME("MCI_RETURN not in first position\n");
1106 return MCIERR_PARSER_INTERNAL;
1110 if (strncmpiW(args, str, len) == 0 &&
1111 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1112 /* store good values into data[] */
1113 args += len;
1114 while (*args == ' ') args++;
1115 found = TRUE;
1117 switch (eid) {
1118 case MCI_COMMAND_HEAD:
1119 case MCI_RETURN:
1120 case MCI_END_COMMAND:
1121 case MCI_END_COMMAND_LIST:
1122 case MCI_CONSTANT: /* done above */
1123 case MCI_END_CONSTANT: /* done above */
1124 break;
1125 case MCI_FLAG:
1126 *dwFlags |= flg;
1127 TRACE("flag=%08x\n", flg);
1128 break;
1129 case MCI_HWND:
1130 case MCI_HPAL:
1131 case MCI_HDC:
1132 case MCI_INTEGER:
1133 if (inCst) {
1134 data[offset] |= flg;
1135 *dwFlags |= cflg;
1136 inCst = FALSE;
1137 TRACE("flag=%08x constant=%08x\n", cflg, flg);
1138 } else {
1139 *dwFlags |= flg;
1140 if (!MCI_GetDWord(&(data[offset]), &args)) {
1141 return MCIERR_BAD_INTEGER;
1143 TRACE("flag=%08x int=%d\n", flg, data[offset]);
1145 break;
1146 case MCI_RECT:
1147 /* store rect in data (offset..offset+3) */
1148 *dwFlags |= flg;
1149 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1150 !MCI_GetDWord(&(data[offset+1]), &args) ||
1151 !MCI_GetDWord(&(data[offset+2]), &args) ||
1152 !MCI_GetDWord(&(data[offset+3]), &args)) {
1153 return MCIERR_BAD_INTEGER;
1155 TRACE("flag=%08x for rectangle\n", flg);
1156 break;
1157 case MCI_STRING:
1158 *dwFlags |= flg;
1159 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1160 return dwRet;
1161 TRACE("flag=%08x string=%s\n", flg, debugstr_w(*(LPWSTR*)&data[offset]));
1162 break;
1163 default: ERR("oops\n");
1165 /* exit inside while loop, except if just entered in constant area definition */
1166 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1167 } else {
1168 /* have offset incremented if needed */
1169 switch (eid) {
1170 case MCI_COMMAND_HEAD:
1171 case MCI_RETURN:
1172 case MCI_END_COMMAND:
1173 case MCI_END_COMMAND_LIST:
1174 case MCI_CONSTANT:
1175 case MCI_FLAG: break;
1176 case MCI_HWND:
1177 case MCI_HPAL:
1178 case MCI_HDC: if (!inCst) offset += sizeof(HANDLE)/sizeof(DWORD); break;
1179 case MCI_INTEGER: if (!inCst) offset++; break;
1180 case MCI_END_CONSTANT: offset++; break;
1181 case MCI_STRING: offset += sizeof(LPWSTR)/sizeof(DWORD); break;
1182 case MCI_RECT: offset += 4; break;
1183 default: ERR("oops\n");
1186 } while (eid != MCI_END_COMMAND);
1187 if (!found) {
1188 WARN("Optarg %s not found\n", debugstr_w(args));
1189 return MCIERR_UNRECOGNIZED_COMMAND;
1191 if (offset == MCI_DATA_SIZE) {
1192 FIXME("Internal data[] buffer overflow\n");
1193 return MCIERR_PARSER_INTERNAL;
1196 return 0;
1199 /**************************************************************************
1200 * MCI_HandleReturnValues [internal]
1202 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1203 MCI_GENERIC_PARMS *params, LPWSTR lpstrRet, UINT uRetLen)
1205 static const WCHAR fmt_d [] = {'%','d',0};
1206 static const WCHAR fmt_d4 [] = {'%','d',' ','%','d',' ','%','d',' ','%','d',0};
1207 static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1208 static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1210 if (lpstrRet) {
1211 switch (retType) {
1212 case 0: /* nothing to return */
1213 break;
1214 case MCI_INTEGER:
1216 DWORD data = *(DWORD *)(params + 1);
1217 switch (dwRet & 0xFFFF0000ul) {
1218 case 0:
1219 case MCI_INTEGER_RETURNED:
1220 snprintfW(lpstrRet, uRetLen, fmt_d, data);
1221 break;
1222 case MCI_RESOURCE_RETURNED:
1223 /* return string which ID is HIWORD(data),
1224 * string is loaded from mmsystem.dll */
1225 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1226 break;
1227 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1228 /* return string which ID is HIWORD(data),
1229 * string is loaded from driver */
1230 /* FIXME: this is wrong for a 16 bit handle */
1231 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1232 HIWORD(data), lpstrRet, uRetLen);
1233 break;
1234 case MCI_COLONIZED3_RETURN:
1235 snprintfW(lpstrRet, uRetLen, wszCol3,
1236 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1237 LOBYTE(HIWORD(data)));
1238 break;
1239 case MCI_COLONIZED4_RETURN:
1240 snprintfW(lpstrRet, uRetLen, wszCol4,
1241 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1242 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1243 break;
1244 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1246 break;
1248 #ifdef MCI_INTEGER64
1249 case MCI_INTEGER64:
1251 static const WCHAR fmt_ld [] = {'%','l','d',0};
1252 DWORD_PTR data = *(DWORD_PTR *)(params + 1);
1253 switch (dwRet & 0xFFFF0000ul) {
1254 case 0:
1255 case MCI_INTEGER_RETURNED:
1256 snprintfW(lpstrRet, uRetLen, fmt_ld, data);
1257 break;
1258 case MCI_RESOURCE_RETURNED:
1259 /* return string which ID is HIWORD(data),
1260 * string is loaded from mmsystem.dll */
1261 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1262 break;
1263 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1264 /* return string which ID is HIWORD(data),
1265 * string is loaded from driver */
1266 /* FIXME: this is wrong for a 16 bit handle */
1267 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1268 HIWORD(data), lpstrRet, uRetLen);
1269 break;
1270 case MCI_COLONIZED3_RETURN:
1271 snprintfW(lpstrRet, uRetLen, wszCol3,
1272 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1273 LOBYTE(HIWORD(data)));
1274 break;
1275 case MCI_COLONIZED4_RETURN:
1276 snprintfW(lpstrRet, uRetLen, wszCol4,
1277 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1278 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1279 break;
1280 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1282 break;
1284 #endif
1285 case MCI_STRING:
1286 switch (dwRet & 0xFFFF0000ul) {
1287 case 0:
1288 /* nothing to do data[0] == lpstrRet */
1289 break;
1290 case MCI_INTEGER_RETURNED:
1292 DWORD *data = (DWORD *)(params + 1);
1293 *data = *(LPDWORD)lpstrRet;
1294 snprintfW(lpstrRet, uRetLen, fmt_d, *data);
1295 break;
1297 default:
1298 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1299 break;
1301 break;
1302 case MCI_RECT:
1304 DWORD *data = (DWORD *)(params + 1);
1305 if (dwRet & 0xFFFF0000ul)
1306 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1307 snprintfW(lpstrRet, uRetLen, fmt_d4, data[0], data[1], data[2], data[3]);
1308 break;
1310 default: FIXME("Unknown MCI return type %d\n", retType);
1313 return LOWORD(dwRet);
1316 /**************************************************************************
1317 * mciSendStringW [WINMM.@]
1319 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1320 UINT uRetLen, HWND hwndCallback)
1322 LPWSTR verb, dev, args, devType = NULL;
1323 LPWINE_MCIDRIVER wmd = 0;
1324 MCIDEVICEID uDevID, auto_open = 0;
1325 DWORD dwFlags = 0, dwRet = 0;
1326 int offset = 0;
1327 DWORD retType;
1328 LPCWSTR lpCmd = 0;
1329 WORD wMsg = 0;
1330 static const WCHAR wszNew[] = {'n','e','w',0};
1331 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1332 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1333 union
1335 MCI_GENERIC_PARMS generic;
1336 MCI_OPEN_PARMSW open;
1337 MCI_SOUND_PARMSW sound;
1338 MCI_SYSINFO_PARMSW sysinfo;
1339 DWORD dw[MCI_DATA_SIZE];
1340 } data;
1342 TRACE("(%s, %p, %d, %p)\n",
1343 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1344 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1346 /* format is <command> <device> <optargs> */
1347 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1348 return MCIERR_OUT_OF_MEMORY;
1349 strcpyW( verb, lpstrCommand );
1350 CharLowerW(verb);
1352 memset(&data, 0, sizeof(data));
1354 if (!(args = strchrW(verb, ' '))) {
1355 dwRet = MCIERR_MISSING_DEVICE_NAME;
1356 goto errCleanUp;
1358 *args++ = '\0';
1359 if ((dwRet = MCI_GetString(&dev, &args))) {
1360 goto errCleanUp;
1362 uDevID = strcmpiW(dev, wszAll) ? 0 : MCI_ALL_DEVICE_ID;
1364 /* Determine devType from open */
1365 if (!strcmpW(verb, wszOpen)) {
1366 LPWSTR tmp;
1367 WCHAR buf[128];
1369 /* case dev == 'new' has to be handled */
1370 if (!strcmpW(dev, wszNew)) {
1371 dev = 0;
1372 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1373 devType += 5;
1374 tmp = strchrW(devType, ' ');
1375 if (tmp) *tmp = '\0';
1376 devType = str_dup_upper(devType);
1377 if (tmp) *tmp = ' ';
1378 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1379 } else {
1380 WARN("open new requires device type\n");
1381 dwRet = MCIERR_MISSING_DEVICE_NAME;
1382 goto errCleanUp;
1384 dwFlags |= MCI_OPEN_ELEMENT;
1385 data.open.lpstrElementName = &wszNull[0];
1386 } else if ((devType = strchrW(dev, '!')) != NULL) {
1387 *devType++ = '\0';
1388 tmp = devType; devType = dev; dev = tmp;
1390 dwFlags |= MCI_OPEN_TYPE;
1391 data.open.lpstrDeviceType = devType;
1392 devType = str_dup_upper(devType);
1393 dwFlags |= MCI_OPEN_ELEMENT;
1394 data.open.lpstrElementName = dev;
1395 } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1396 /* this is the name of a mci driver's type */
1397 tmp = strchrW(dev, ' ');
1398 if (tmp) *tmp = '\0';
1399 data.open.lpstrDeviceType = dev;
1400 devType = str_dup_upper(dev);
1401 if (tmp) *tmp = ' ';
1402 dwFlags |= MCI_OPEN_TYPE;
1403 } else {
1404 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1405 devType += 5;
1406 tmp = strchrW(devType, ' ');
1407 if (tmp) *tmp = '\0';
1408 devType = str_dup_upper(devType);
1409 if (tmp) *tmp = ' ';
1410 /* dwFlags and lpstrDeviceType will be correctly set in ParseOpt loop */
1411 } else {
1412 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1413 goto errCleanUp;
1415 devType = str_dup_upper(buf);
1417 dwFlags |= MCI_OPEN_ELEMENT;
1418 data.open.lpstrElementName = dev;
1420 if (MCI_ALL_DEVICE_ID == uDevID) {
1421 dwRet = MCIERR_CANNOT_USE_ALL;
1422 goto errCleanUp;
1424 if (!strstrW(args, wszSAliasS) && !dev) {
1425 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1426 goto errCleanUp;
1429 dwRet = MCI_LoadMciDriver(devType, &wmd);
1430 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1431 dwRet = MCIERR_INVALID_DEVICE_NAME;
1432 if (dwRet)
1433 goto errCleanUp;
1434 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))
1435 && (lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb))) {
1436 /* auto-open uses the core command table */
1437 switch (MCI_GetMessage(lpCmd)) {
1438 case MCI_SOUND: /* command does not use a device name */
1439 case MCI_SYSINFO:
1440 break;
1441 case MCI_CLOSE: /* don't auto-open for close */
1442 case MCI_BREAK: /* no auto-open for system commands */
1443 dwRet = MCIERR_INVALID_DEVICE_NAME;
1444 goto errCleanUp;
1445 break;
1446 default:
1448 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1449 WCHAR buf[138], retbuf[6];
1450 snprintfW(buf, sizeof(buf)/sizeof(WCHAR), wszOpenWait, dev);
1451 /* open via mciSendString handles quoting, dev!file syntax and alias creation */
1452 if ((dwRet = mciSendStringW(buf, retbuf, sizeof(retbuf)/sizeof(WCHAR), 0)) != 0)
1453 goto errCleanUp;
1454 auto_open = strtoulW(retbuf, NULL, 10);
1455 TRACE("auto-opened %u for %s\n", auto_open, debugstr_w(dev));
1457 /* FIXME: test for notify flag (how to preparse?) before opening */
1458 wmd = MCI_GetDriver(auto_open);
1459 if (!wmd) {
1460 ERR("No auto-open device %u\n", auto_open);
1461 dwRet = MCIERR_INVALID_DEVICE_ID;
1462 goto errCleanUp;
1468 /* get the verb in the different command tables */
1469 if (wmd) {
1470 /* try the device specific command table */
1471 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1472 if (!lpCmd) {
1473 /* try the type specific command table */
1474 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1475 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1476 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1477 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1480 /* try core command table */
1481 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1483 if (!lpCmd) {
1484 TRACE("Command %s not found!\n", debugstr_w(verb));
1485 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1486 goto errCleanUp;
1488 wMsg = MCI_GetMessage(lpCmd);
1490 /* set return information */
1491 offset = sizeof(data.generic);
1492 switch (retType = MCI_GetReturnType(lpCmd)) {
1493 case 0:
1494 break;
1495 case MCI_INTEGER:
1496 offset += sizeof(DWORD);
1497 break;
1498 case MCI_STRING:
1499 data.sysinfo.lpstrReturn = lpstrRet;
1500 data.sysinfo.dwRetSize = uRetLen;
1501 offset = FIELD_OFFSET( MCI_SYSINFO_PARMSW, dwNumber );
1502 break;
1503 case MCI_RECT:
1504 offset += 4 * sizeof(DWORD);
1505 break;
1506 #ifdef MCI_INTEGER64
1507 case MCI_INTEGER64:
1508 offset += sizeof(DWORD_PTR);
1509 break;
1510 #endif
1511 default:
1512 FIXME("Unknown MCI return type %d\n", retType);
1513 dwRet = MCIERR_PARSER_INTERNAL;
1514 goto errCleanUp;
1517 TRACE("verb=%s on dev=%s; offset=%d\n",
1518 debugstr_w(verb), debugstr_w(dev), offset);
1520 if ((dwRet = MCI_ParseOptArgs(data.dw, offset / sizeof(DWORD), lpCmd, args, &dwFlags)))
1521 goto errCleanUp;
1523 /* set up call back */
1524 if (auto_open) {
1525 if (dwFlags & MCI_NOTIFY) {
1526 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1527 goto errCleanUp;
1529 /* FIXME: the command should get its own notification window set up and
1530 * ask for device closing while processing the notification mechanism.
1531 * hwndCallback = ...
1532 * dwFlags |= MCI_NOTIFY;
1533 * In the meantime special-case all commands but PLAY and RECORD below. */
1535 if (dwFlags & MCI_NOTIFY) {
1536 data.generic.dwCallback = (DWORD_PTR)hwndCallback;
1539 switch (wMsg) {
1540 case MCI_OPEN:
1541 if (strcmpW(verb, wszOpen)) {
1542 FIXME("Cannot open with command %s\n", debugstr_w(verb));
1543 dwRet = MCIERR_DRIVER_INTERNAL;
1544 wMsg = 0;
1545 goto errCleanUp;
1547 break;
1548 case MCI_SYSINFO:
1549 /* Requirements on dev depend on the flags:
1550 * alias with INSTALLNAME, name like "digitalvideo"
1551 * with QUANTITY and NAME. */
1553 data.sysinfo.wDeviceType = MCI_ALL_DEVICE_ID;
1554 if (uDevID != MCI_ALL_DEVICE_ID) {
1555 if (dwFlags & MCI_SYSINFO_INSTALLNAME)
1556 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1557 else if (!(data.sysinfo.wDeviceType = MCI_GetDevTypeFromResource(dev))) {
1558 dwRet = MCIERR_DEVICE_TYPE_REQUIRED;
1559 goto errCleanUp;
1563 break;
1564 case MCI_SOUND:
1565 /* FIXME: name is optional, "sound" is a valid command.
1566 * FIXME: Parse "sound notify" as flag, not as name. */
1567 data.sound.lpstrSoundName = dev;
1568 dwFlags |= MCI_SOUND_NAME;
1569 break;
1572 TRACE("[%d, %s, %08x, %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x]\n",
1573 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(wMsg), dwFlags,
1574 data.dw[0], data.dw[1], data.dw[2], data.dw[3], data.dw[4],
1575 data.dw[5], data.dw[6], data.dw[7], data.dw[8], data.dw[9]);
1577 if (wMsg == MCI_OPEN) {
1578 if ((dwRet = MCI_FinishOpen(wmd, &data.open, dwFlags)))
1579 goto errCleanUp;
1580 /* FIXME: notification is not properly shared across two opens */
1581 } else {
1582 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, wMsg, dwFlags, (DWORD_PTR)&data);
1584 if (!LOWORD(dwRet)) {
1585 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1586 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, &data.generic, lpstrRet, uRetLen);
1587 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1588 } else
1589 TRACE("=> %x\n", dwRet);
1591 errCleanUp:
1592 if (auto_open) {
1593 /* PLAY and RECORD are the only known non-immediate commands */
1594 if (LOWORD(dwRet) || !(wMsg == MCI_PLAY || wMsg == MCI_RECORD))
1595 MCI_SendCommand(auto_open, MCI_CLOSE, 0, 0);
1596 else
1597 FIXME("leaking auto-open device %u\n", auto_open);
1599 if (wMsg == MCI_OPEN && LOWORD(dwRet) && wmd)
1600 MCI_UnLoadMciDriver(wmd);
1601 HeapFree(GetProcessHeap(), 0, devType);
1602 HeapFree(GetProcessHeap(), 0, verb);
1603 return dwRet;
1606 /**************************************************************************
1607 * mciSendStringA [WINMM.@]
1609 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1610 UINT uRetLen, HWND hwndCallback)
1612 LPWSTR lpwstrCommand;
1613 LPWSTR lpwstrRet = NULL;
1614 UINT ret;
1615 INT len;
1617 /* FIXME: is there something to do with lpstrReturnString ? */
1618 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1619 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1620 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1621 if (lpstrRet)
1623 if (uRetLen) *lpstrRet = '\0'; /* NT-w2k3 use memset(lpstrRet, 0, uRetLen); */
1624 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1625 if (!lpwstrRet) {
1626 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1627 return MCIERR_OUT_OF_MEMORY;
1630 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1631 if (!ret && lpwstrRet)
1632 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1633 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1634 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1635 return ret;
1638 /**************************************************************************
1639 * mciExecute [WINMM.@]
1641 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1643 char strRet[256];
1644 DWORD ret;
1646 TRACE("(%s)!\n", lpstrCommand);
1648 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1649 if (ret != 0) {
1650 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1651 sprintf(strRet, "Unknown MCI error (%d)", ret);
1653 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1655 /* FIXME: what shall I return ? */
1656 return TRUE;
1659 /**************************************************************************
1660 * mciLoadCommandResource [WINMM.@]
1662 * Strangely, this function only exists as a UNICODE one.
1664 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1666 UINT ret = MCI_NO_COMMAND_TABLE;
1667 HRSRC hRsrc = 0;
1668 HGLOBAL hMem;
1670 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1672 /* if a file named "resname.mci" exits, then load resource "resname" from it
1673 * otherwise directly from driver
1674 * We don't support it (who uses this feature ?), but we check anyway
1676 if (!type) {
1677 #if 0
1678 /* FIXME: we should put this back into order, but I never found a program
1679 * actually using this feature, so we may not need it
1681 char buf[128];
1682 OFSTRUCT ofs;
1684 strcat(strcpy(buf, resname), ".mci");
1685 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1686 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1688 #endif
1690 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1691 (hMem = LoadResource(hInst, hRsrc))) {
1692 ret = MCI_SetCommandTable(hMem, type);
1693 FreeResource(hMem);
1695 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1697 TRACE("=> %04x\n", ret);
1698 return ret;
1701 /**************************************************************************
1702 * mciFreeCommandResource [WINMM.@]
1704 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1706 TRACE("(%08x)!\n", uTable);
1708 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1709 return FALSE;
1711 FreeResource(S_MciCmdTable[uTable].hMem);
1712 S_MciCmdTable[uTable].hMem = NULL;
1713 S_MciCmdTable[uTable].lpTable = NULL;
1714 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1715 S_MciCmdTable[uTable].aVerbs = 0;
1716 S_MciCmdTable[uTable].nVerbs = 0;
1717 return TRUE;
1720 /**************************************************************************
1721 * MCI_Open [internal]
1723 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1725 WCHAR strDevTyp[128];
1726 DWORD dwRet;
1727 LPWINE_MCIDRIVER wmd = NULL;
1729 TRACE("(%08X, %p)\n", dwParam, lpParms);
1730 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1732 /* only two low bytes are generic, the other ones are dev type specific */
1733 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1734 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1735 MCI_NOTIFY|MCI_WAIT)
1736 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0)
1737 FIXME("Unsupported yet dwFlags=%08X\n", dwParam);
1738 #undef WINE_MCIDRIVER_SUPP
1740 strDevTyp[0] = 0;
1742 if (dwParam & MCI_OPEN_TYPE) {
1743 if (dwParam & MCI_OPEN_TYPE_ID) {
1744 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1746 if (uDevType < MCI_DEVTYPE_FIRST ||
1747 uDevType > MCI_DEVTYPE_LAST ||
1748 !LoadStringW(hWinMM32Instance, uDevType,
1749 strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1750 dwRet = MCIERR_BAD_INTEGER;
1751 goto errCleanUp;
1753 } else {
1754 LPWSTR ptr;
1755 if (lpParms->lpstrDeviceType == NULL) {
1756 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1757 goto errCleanUp;
1759 strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1760 ptr = strchrW(strDevTyp, '!');
1761 if (ptr) {
1762 /* this behavior is not documented in windows. However, since, in
1763 * some occasions, MCI_OPEN handling is translated by WinMM into
1764 * a call to mciSendString("open <type>"); this code shall be correct
1766 if (dwParam & MCI_OPEN_ELEMENT) {
1767 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1768 debugstr_w(lpParms->lpstrElementName),
1769 debugstr_w(strDevTyp));
1770 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1771 goto errCleanUp;
1773 dwParam |= MCI_OPEN_ELEMENT;
1774 *ptr++ = 0;
1775 /* FIXME: not a good idea to write in user supplied buffer */
1776 lpParms->lpstrElementName = ptr;
1780 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1783 if (dwParam & MCI_OPEN_ELEMENT) {
1784 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1786 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1787 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1788 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1789 goto errCleanUp;
1792 if (!lpParms->lpstrElementName) {
1793 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1794 goto errCleanUp;
1797 /* type, if given as a parameter, supersedes file extension */
1798 if (!strDevTyp[0] &&
1799 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1800 strDevTyp, sizeof(strDevTyp))) {
1801 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1802 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1803 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1804 goto errCleanUp;
1806 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1807 strcpyW(strDevTyp, wszCdAudio);
1811 if (strDevTyp[0] == 0) {
1812 FIXME("Couldn't load driver\n");
1813 dwRet = MCIERR_INVALID_DEVICE_NAME;
1814 goto errCleanUp;
1817 if (dwParam & MCI_OPEN_ALIAS) {
1818 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1819 if (!lpParms->lpstrAlias) {
1820 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1821 goto errCleanUp;
1825 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1826 goto errCleanUp;
1829 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1830 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1831 /* FIXME: is dwRet the correct ret code ? */
1832 goto errCleanUp;
1835 /* only handled devices fall through */
1836 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1837 return 0;
1839 errCleanUp:
1840 if (wmd) MCI_UnLoadMciDriver(wmd);
1841 return dwRet;
1844 /**************************************************************************
1845 * MCI_Close [internal]
1847 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1849 DWORD dwRet;
1850 LPWINE_MCIDRIVER wmd;
1852 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1854 /* Every device must handle MCI_NOTIFY on its own. */
1855 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1856 while (MciDrivers) {
1857 /* Retrieve the device ID under lock, but send the message without,
1858 * the driver might be calling some winmm functions from another
1859 * thread before being fully stopped.
1861 EnterCriticalSection(&WINMM_cs);
1862 if (!MciDrivers)
1864 LeaveCriticalSection(&WINMM_cs);
1865 break;
1867 wDevID = MciDrivers->wDeviceID;
1868 LeaveCriticalSection(&WINMM_cs);
1869 MCI_Close(wDevID, dwParam, lpParms);
1871 return 0;
1874 if (!(wmd = MCI_GetDriver(wDevID))) {
1875 return MCIERR_INVALID_DEVICE_ID;
1878 if(wmd->CreatorThread != GetCurrentThreadId())
1879 return MCIERR_INVALID_DEVICE_NAME;
1881 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1883 MCI_UnLoadMciDriver(wmd);
1885 return dwRet;
1888 /**************************************************************************
1889 * MCI_WriteString [internal]
1891 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1893 DWORD ret = 0;
1895 if (lpSrcStr) {
1896 if (dstSize <= strlenW(lpSrcStr)) {
1897 ret = MCIERR_PARAM_OVERFLOW;
1898 } else {
1899 strcpyW(lpDstStr, lpSrcStr);
1901 } else {
1902 *lpDstStr = 0;
1904 return ret;
1907 /**************************************************************************
1908 * MCI_Sysinfo [internal]
1910 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1912 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1913 WCHAR buf[2048], *s, *p;
1914 LPWINE_MCIDRIVER wmd;
1915 HKEY hKey;
1917 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1918 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1920 TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1921 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1922 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1923 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1925 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1926 case MCI_SYSINFO_QUANTITY:
1927 if (lpParms->dwRetSize < sizeof(DWORD))
1928 return MCIERR_PARAM_OVERFLOW;
1929 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1930 if (uDevID == MCI_ALL_DEVICE_ID) {
1931 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1932 if (dwFlags & MCI_SYSINFO_OPEN) {
1933 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1934 EnterCriticalSection(&WINMM_cs);
1935 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1936 cnt++;
1938 LeaveCriticalSection(&WINMM_cs);
1939 } else {
1940 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1941 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1942 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1943 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1944 RegCloseKey( hKey );
1946 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1947 for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1949 } else {
1950 if (dwFlags & MCI_SYSINFO_OPEN) {
1951 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1952 EnterCriticalSection(&WINMM_cs);
1953 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1954 if (wmd->wType == lpParms->wDeviceType) cnt++;
1956 LeaveCriticalSection(&WINMM_cs);
1957 } else {
1958 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1959 FIXME("Don't know how to get # of MCI devices of a given type\n");
1960 /* name = LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType))
1961 * then lookup registry and/or system.ini for name, ignoring digits suffix */
1962 switch (LOWORD(lpParms->wDeviceType)) {
1963 case MCI_DEVTYPE_CD_AUDIO:
1964 case MCI_DEVTYPE_WAVEFORM_AUDIO:
1965 case MCI_DEVTYPE_SEQUENCER:
1966 cnt = 1;
1967 break;
1968 default: /* "digitalvideo" gets 0 because it's not in the registry */
1969 cnt = 0;
1973 *(DWORD*)lpParms->lpstrReturn = cnt;
1974 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1975 ret = MCI_INTEGER_RETURNED;
1976 /* return ret; Only Win9x sends a notification in this case. */
1977 break;
1978 case MCI_SYSINFO_INSTALLNAME:
1979 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1980 if ((wmd = MCI_GetDriver(uDevID))) {
1981 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1982 wmd->lpstrDeviceType);
1983 } else {
1984 ret = (uDevID == MCI_ALL_DEVICE_ID)
1985 ? MCIERR_CANNOT_USE_ALL : MCIERR_INVALID_DEVICE_NAME;
1987 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1988 break;
1989 case MCI_SYSINFO_NAME:
1990 s = NULL;
1991 if (dwFlags & MCI_SYSINFO_OPEN) {
1992 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1993 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1994 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1995 EnterCriticalSection(&WINMM_cs);
1996 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1997 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1998 if (uDevID == MCI_ALL_DEVICE_ID ||
1999 lpParms->wDeviceType == wmd->wType) {
2000 cnt++;
2001 if (cnt == lpParms->dwNumber) {
2002 s = wmd->lpstrAlias;
2003 break;
2007 LeaveCriticalSection(&WINMM_cs);
2008 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
2009 } else if (MCI_ALL_DEVICE_ID == uDevID) {
2010 TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
2011 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
2012 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
2013 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
2014 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
2015 lpParms->dwNumber <= cnt) {
2016 DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
2017 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
2018 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
2019 s = buf;
2021 RegCloseKey( hKey );
2023 if (!s) {
2024 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
2025 for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
2026 TRACE("%d: %s\n", cnt, debugstr_w(p));
2027 if (cnt == lpParms->dwNumber - 1) {
2028 s = p;
2029 break;
2034 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
2035 } else {
2036 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
2037 /* Cheating: what is asked for is the nth device from the registry. */
2038 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
2039 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
2040 ret = MCIERR_OUTOFRANGE;
2041 else {
2042 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
2043 lpParms->lpstrReturn, lpParms->dwRetSize);
2044 ret = 0;
2047 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
2048 break;
2049 default:
2050 TRACE("Unsupported flag value=%08x\n", dwFlags);
2051 ret = MCIERR_UNRECOGNIZED_KEYWORD;
2053 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
2054 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
2055 return ret;
2058 /**************************************************************************
2059 * MCI_Break [internal]
2061 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
2063 if (lpParms == NULL)
2064 return MCIERR_NULL_PARAMETER_BLOCK;
2065 FIXME("(%04x) vkey %04X stub\n", dwFlags, lpParms->nVirtKey);
2067 if (dwFlags & MCI_NOTIFY)
2068 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2069 return MMSYSERR_NOERROR;
2072 /**************************************************************************
2073 * MCI_Sound [internal]
2075 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
2077 DWORD dwRet;
2079 if (dwFlags & MCI_SOUND_NAME) {
2080 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2081 else dwRet = PlaySoundW(lpParms->lpstrSoundName, NULL,
2082 SND_ALIAS | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2083 ? 0 : MCIERR_HARDWARE;
2084 } else dwRet = PlaySoundW((LPCWSTR)SND_ALIAS_SYSTEMDEFAULT, NULL,
2085 SND_ALIAS_ID | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2086 ? 0 : MCIERR_HARDWARE;
2088 if (!dwRet && lpParms && (dwFlags & MCI_NOTIFY))
2089 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2090 return dwRet;
2093 /**************************************************************************
2094 * MCI_SendCommand [internal]
2096 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2098 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
2100 switch (wMsg) {
2101 case MCI_OPEN:
2102 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
2103 break;
2104 case MCI_CLOSE:
2105 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
2106 break;
2107 case MCI_SYSINFO:
2108 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2109 break;
2110 case MCI_BREAK:
2111 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2112 break;
2113 case MCI_SOUND:
2114 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2115 break;
2116 default:
2117 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
2118 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2119 dwRet = MCIERR_CANNOT_USE_ALL;
2120 } else {
2121 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
2123 break;
2125 return dwRet;
2128 /**************************************************************************
2129 * MCI_CleanUp [internal]
2131 * Some MCI commands need to be cleaned-up (when not called from
2132 * mciSendString), because MCI drivers return extra information for string
2133 * transformation. This function gets rid of them.
2135 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
2137 if (LOWORD(dwRet))
2138 return LOWORD(dwRet);
2140 switch (wMsg) {
2141 case MCI_GETDEVCAPS:
2142 switch (dwRet & 0xFFFF0000ul) {
2143 case 0:
2144 case MCI_COLONIZED3_RETURN:
2145 case MCI_COLONIZED4_RETURN:
2146 case MCI_INTEGER_RETURNED:
2147 /* nothing to do */
2148 break;
2149 case MCI_RESOURCE_RETURNED:
2150 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2152 LPMCI_GETDEVCAPS_PARMS lmgp;
2154 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
2155 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2156 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2158 break;
2159 default:
2160 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2161 HIWORD(dwRet), MCI_MessageToString(wMsg));
2163 break;
2164 case MCI_STATUS:
2165 switch (dwRet & 0xFFFF0000ul) {
2166 case 0:
2167 case MCI_COLONIZED3_RETURN:
2168 case MCI_COLONIZED4_RETURN:
2169 case MCI_INTEGER_RETURNED:
2170 /* nothing to do */
2171 break;
2172 case MCI_RESOURCE_RETURNED:
2173 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2175 LPMCI_STATUS_PARMS lsp;
2177 lsp = (LPMCI_STATUS_PARMS)dwParam2;
2178 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2179 lsp->dwReturn = LOWORD(lsp->dwReturn);
2181 break;
2182 default:
2183 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2184 HIWORD(dwRet), MCI_MessageToString(wMsg));
2186 break;
2187 case MCI_SYSINFO:
2188 switch (dwRet & 0xFFFF0000ul) {
2189 case 0:
2190 case MCI_INTEGER_RETURNED:
2191 /* nothing to do */
2192 break;
2193 default:
2194 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2196 break;
2197 default:
2198 if (HIWORD(dwRet)) {
2199 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2200 dwRet, MCI_MessageToString(wMsg));
2202 break;
2204 return LOWORD(dwRet);
2207 /**************************************************************************
2208 * mciGetErrorStringW [WINMM.@]
2210 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2212 BOOL ret = FALSE;
2214 if (lpstrBuffer != NULL && uLength > 0 &&
2215 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2217 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2218 ret = TRUE;
2221 return ret;
2224 /**************************************************************************
2225 * mciGetErrorStringA [WINMM.@]
2227 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2229 BOOL ret = FALSE;
2231 if (lpstrBuffer != NULL && uLength > 0 &&
2232 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2234 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2235 ret = TRUE;
2238 return ret;
2241 /**************************************************************************
2242 * mciDriverNotify [WINMM.@]
2244 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2246 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2248 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2251 /**************************************************************************
2252 * mciGetDriverData [WINMM.@]
2254 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2256 LPWINE_MCIDRIVER wmd;
2258 TRACE("(%04x)\n", uDeviceID);
2260 wmd = MCI_GetDriver(uDeviceID);
2262 if (!wmd) {
2263 WARN("Bad uDeviceID\n");
2264 return 0L;
2267 return wmd->dwPrivate;
2270 /**************************************************************************
2271 * mciSetDriverData [WINMM.@]
2273 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2275 LPWINE_MCIDRIVER wmd;
2277 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2279 wmd = MCI_GetDriver(uDeviceID);
2281 if (!wmd) {
2282 WARN("Bad uDeviceID\n");
2283 return FALSE;
2286 wmd->dwPrivate = data;
2287 return TRUE;
2290 /**************************************************************************
2291 * mciSendCommandW [WINMM.@]
2294 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2296 DWORD dwRet;
2298 TRACE("(%08x, %s, %08lx, %08lx)\n",
2299 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2301 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2302 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2303 TRACE("=> %08x\n", dwRet);
2304 return dwRet;
2307 /**************************************************************************
2308 * mciSendCommandA [WINMM.@]
2310 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2312 DWORD ret;
2313 int mapped;
2315 TRACE("(%08x, %s, %08lx, %08lx)\n",
2316 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2318 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2319 if (mapped == -1)
2321 FIXME("message %04x mapping failed\n", wMsg);
2322 return MCIERR_OUT_OF_MEMORY;
2324 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2325 if (mapped)
2326 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2327 return ret;
2330 /**************************************************************************
2331 * mciGetDeviceIDA [WINMM.@]
2333 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2335 LPWSTR w = MCI_strdupAtoW(lpstrName);
2336 UINT ret = MCIERR_OUT_OF_MEMORY;
2338 if (w)
2340 ret = mciGetDeviceIDW(w);
2341 HeapFree(GetProcessHeap(), 0, w);
2343 return ret;
2346 /**************************************************************************
2347 * mciGetDeviceIDW [WINMM.@]
2349 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2351 return MCI_GetDriverFromString(lpwstrName);
2354 /**************************************************************************
2355 * MCI_DefYieldProc [internal]
2357 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2359 INT16 ret;
2360 MSG msg;
2362 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2364 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2365 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2366 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2367 ret = 0;
2368 } else {
2369 msg.hwnd = HWND_32(HIWORD(data));
2370 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2371 ret = -1;
2373 return ret;
2376 /**************************************************************************
2377 * mciSetYieldProc [WINMM.@]
2379 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2381 LPWINE_MCIDRIVER wmd;
2383 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2385 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2386 WARN("Bad uDeviceID\n");
2387 return FALSE;
2390 wmd->lpfnYieldProc = fpYieldProc;
2391 wmd->dwYieldData = dwYieldData;
2393 return TRUE;
2396 /**************************************************************************
2397 * mciGetDeviceIDFromElementIDA [WINMM.@]
2399 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2401 LPWSTR w = MCI_strdupAtoW(lpstrType);
2402 UINT ret = 0;
2404 if (w)
2406 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2407 HeapFree(GetProcessHeap(), 0, w);
2409 return ret;
2412 /**************************************************************************
2413 * mciGetDeviceIDFromElementIDW [WINMM.@]
2415 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2417 /* FIXME: that's rather strange, there is no
2418 * mciGetDeviceIDFromElementID32A in winmm.spec
2420 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2421 return 0;
2424 /**************************************************************************
2425 * mciGetYieldProc [WINMM.@]
2427 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2429 LPWINE_MCIDRIVER wmd;
2431 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2433 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2434 WARN("Bad uDeviceID\n");
2435 return NULL;
2437 if (!wmd->lpfnYieldProc) {
2438 WARN("No proc set\n");
2439 return NULL;
2441 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2442 return wmd->lpfnYieldProc;
2445 /**************************************************************************
2446 * mciGetCreatorTask [WINMM.@]
2448 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2450 LPWINE_MCIDRIVER wmd;
2451 HTASK ret = 0;
2453 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2455 TRACE("(%u) => %p\n", uDeviceID, ret);
2456 return ret;
2459 /**************************************************************************
2460 * mciDriverYield [WINMM.@]
2462 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2464 LPWINE_MCIDRIVER wmd;
2465 UINT ret = 0;
2467 TRACE("(%04x)\n", uDeviceID);
2469 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2470 MSG msg;
2471 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2472 } else {
2473 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2476 return ret;