winepulse.drv: add a pulseaudio driver
[wine/hacks.git] / dlls / winmm / mci.c
blobf65aee75495c2e7d14121215f518e8be4940828d
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->lpstrElementName && strcmpW(wmd->lpstrElementName, lpstrName) == 0) {
127 ret = wmd->wDeviceID;
128 break;
130 if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
131 ret = wmd->wDeviceID;
132 break;
134 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
135 ret = wmd->wDeviceID;
136 break;
139 LeaveCriticalSection(&WINMM_cs);
141 return ret;
144 /**************************************************************************
145 * MCI_MessageToString [internal]
147 const char* MCI_MessageToString(UINT wMsg)
149 #define CASE(s) case (s): return #s
151 switch (wMsg) {
152 CASE(DRV_LOAD);
153 CASE(DRV_ENABLE);
154 CASE(DRV_OPEN);
155 CASE(DRV_CLOSE);
156 CASE(DRV_DISABLE);
157 CASE(DRV_FREE);
158 CASE(DRV_CONFIGURE);
159 CASE(DRV_QUERYCONFIGURE);
160 CASE(DRV_INSTALL);
161 CASE(DRV_REMOVE);
162 CASE(DRV_EXITSESSION);
163 CASE(DRV_EXITAPPLICATION);
164 CASE(DRV_POWER);
165 CASE(MCI_BREAK);
166 CASE(MCI_CLOSE);
167 CASE(MCI_CLOSE_DRIVER);
168 CASE(MCI_COPY);
169 CASE(MCI_CUE);
170 CASE(MCI_CUT);
171 CASE(MCI_DELETE);
172 CASE(MCI_ESCAPE);
173 CASE(MCI_FREEZE);
174 CASE(MCI_PAUSE);
175 CASE(MCI_PLAY);
176 CASE(MCI_GETDEVCAPS);
177 CASE(MCI_INFO);
178 CASE(MCI_LOAD);
179 CASE(MCI_OPEN);
180 CASE(MCI_OPEN_DRIVER);
181 CASE(MCI_PASTE);
182 CASE(MCI_PUT);
183 CASE(MCI_REALIZE);
184 CASE(MCI_RECORD);
185 CASE(MCI_RESUME);
186 CASE(MCI_SAVE);
187 CASE(MCI_SEEK);
188 CASE(MCI_SET);
189 CASE(MCI_SPIN);
190 CASE(MCI_STATUS);
191 CASE(MCI_STEP);
192 CASE(MCI_STOP);
193 CASE(MCI_SYSINFO);
194 CASE(MCI_UNFREEZE);
195 CASE(MCI_UPDATE);
196 CASE(MCI_WHERE);
197 CASE(MCI_WINDOW);
198 /* constants for digital video */
199 CASE(MCI_CAPTURE);
200 CASE(MCI_MONITOR);
201 CASE(MCI_RESERVE);
202 CASE(MCI_SETAUDIO);
203 CASE(MCI_SIGNAL);
204 CASE(MCI_SETVIDEO);
205 CASE(MCI_QUALITY);
206 CASE(MCI_LIST);
207 CASE(MCI_UNDO);
208 CASE(MCI_CONFIGURE);
209 CASE(MCI_RESTORE);
210 #undef CASE
211 default:
212 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
216 LPWSTR MCI_strdupAtoW( LPCSTR str )
218 LPWSTR ret;
219 INT len;
221 if (!str) return NULL;
222 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
223 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
224 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
225 return ret;
228 LPSTR MCI_strdupWtoA( LPCWSTR str )
230 LPSTR ret;
231 INT len;
233 if (!str) return NULL;
234 len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
235 ret = HeapAlloc( GetProcessHeap(), 0, len );
236 if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
237 return ret;
240 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
242 if (msg < DRV_RESERVED) return 0;
244 switch (msg)
246 case MCI_CLOSE:
247 case MCI_CONFIGURE:
248 case MCI_PLAY:
249 case MCI_SEEK:
250 case MCI_STOP:
251 case MCI_PAUSE:
252 case MCI_GETDEVCAPS:
253 case MCI_SPIN:
254 case MCI_SET:
255 case MCI_STEP:
256 case MCI_RECORD:
257 case MCI_BREAK:
258 case MCI_STATUS:
259 case MCI_CUE:
260 case MCI_REALIZE:
261 case MCI_PUT:
262 case MCI_WHERE:
263 case MCI_FREEZE:
264 case MCI_UNFREEZE:
265 case MCI_CUT:
266 case MCI_COPY:
267 case MCI_PASTE:
268 case MCI_UPDATE:
269 case MCI_RESUME:
270 case MCI_DELETE:
271 case MCI_MONITOR:
272 case MCI_SIGNAL:
273 case MCI_UNDO:
274 return 0;
276 case MCI_OPEN:
277 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
278 * structure, larger than MCI_WAVE_OPEN_PARMS */
279 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
280 MCI_ANIM_OPEN_PARMSW *mci_openW;
281 DWORD_PTR *ptr;
283 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW));
284 if (!ptr) return -1;
286 *ptr++ = *dwParam2; /* save the previous pointer */
287 *dwParam2 = (DWORD_PTR)ptr;
288 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
290 if (dwParam1 & MCI_NOTIFY)
291 mci_openW->dwCallback = mci_openA->dwCallback;
293 if (dwParam1 & MCI_OPEN_TYPE)
295 if (dwParam1 & MCI_OPEN_TYPE_ID)
296 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
297 else
298 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
300 if (dwParam1 & MCI_OPEN_ELEMENT)
302 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
303 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
304 else
305 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
307 if (dwParam1 & MCI_OPEN_ALIAS)
308 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
309 /* We don't know how many DWORD follow, as
310 * the structure depends on the device. */
311 if (HIWORD(dwParam1))
312 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
314 return 1;
316 case MCI_WINDOW:
317 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
319 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
320 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
322 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
323 if (!mci_windowW) return -1;
325 *dwParam2 = (DWORD_PTR)mci_windowW;
327 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
329 if (dwParam1 & MCI_NOTIFY)
330 mci_windowW->dwCallback = mci_windowA->dwCallback;
331 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
332 mci_windowW->hWnd = mci_windowA->hWnd;
333 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
334 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
336 return 1;
338 return 0;
340 case MCI_SYSINFO:
341 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
343 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
344 MCI_SYSINFO_PARMSW *mci_sysinfoW;
345 DWORD_PTR *ptr;
347 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
348 if (!ptr) return -1;
350 *ptr++ = *dwParam2; /* save the previous pointer */
351 *dwParam2 = (DWORD_PTR)ptr;
352 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
354 if (dwParam1 & MCI_NOTIFY)
355 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
357 /* Size is measured in numbers of characters, despite what MSDN says. */
358 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
359 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize * sizeof(WCHAR));
360 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
361 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
362 return 1;
364 return 0;
365 case MCI_INFO:
367 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
368 MCI_INFO_PARMSW *mci_infoW;
369 DWORD_PTR *ptr;
371 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
372 if (!ptr) return -1;
374 *ptr++ = *dwParam2; /* save the previous pointer */
375 *dwParam2 = (DWORD_PTR)ptr;
376 mci_infoW = (MCI_INFO_PARMSW *)ptr;
378 if (dwParam1 & MCI_NOTIFY)
379 mci_infoW->dwCallback = mci_infoA->dwCallback;
381 /* Size is measured in numbers of characters. */
382 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
383 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize * sizeof(WCHAR));
384 return 1;
386 case MCI_SAVE:
387 case MCI_LOAD:
388 case MCI_CAPTURE:
389 case MCI_RESTORE:
390 { /* All these commands have the same layout: callback + string + optional rect */
391 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
392 MCI_OVLY_LOAD_PARMSW *mci_loadW;
394 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
395 if (!mci_loadW) return -1;
397 *dwParam2 = (DWORD_PTR)mci_loadW;
398 if (dwParam1 & MCI_NOTIFY)
399 mci_loadW->dwCallback = mci_loadA->dwCallback;
400 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
401 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
402 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
403 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
404 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
405 mci_loadW->rc = mci_loadA->rc;
406 return 1;
408 case MCI_SOUND:
409 case MCI_ESCAPE:
410 { /* All these commands have the same layout: callback + string */
411 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
412 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
414 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
415 if (!mci_vd_escapeW) return -1;
417 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
418 if (dwParam1 & MCI_NOTIFY)
419 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
420 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
421 return 1;
423 case MCI_SETAUDIO:
424 case MCI_SETVIDEO:
425 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
426 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
427 return 0;
428 /* fall through to default */
429 case MCI_RESERVE:
430 case MCI_QUALITY:
431 case MCI_LIST:
432 default:
433 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
434 return 0; /* pass through untouched */
438 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
439 DWORD result)
441 switch (msg)
443 case MCI_OPEN:
445 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
446 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
447 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
449 mci_openA->wDeviceID = mci_openW->wDeviceID;
451 if (dwParam1 & MCI_OPEN_TYPE)
453 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
454 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
456 if (dwParam1 & MCI_OPEN_ELEMENT)
458 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
459 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
461 if (dwParam1 & MCI_OPEN_ALIAS)
462 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
463 HeapFree(GetProcessHeap(), 0, ptr);
465 break;
466 case MCI_WINDOW:
467 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
469 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
471 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
472 HeapFree(GetProcessHeap(), 0, mci_windowW);
474 break;
476 case MCI_SYSINFO:
477 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
479 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
480 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
481 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
483 if (!result)
485 WideCharToMultiByte(CP_ACP, 0,
486 mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
487 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
488 NULL, NULL);
491 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
492 HeapFree(GetProcessHeap(), 0, ptr);
494 break;
495 case MCI_INFO:
497 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
498 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
499 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
501 if (!result)
503 WideCharToMultiByte(CP_ACP, 0,
504 mci_infoW->lpstrReturn, mci_infoW->dwRetSize,
505 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
506 NULL, NULL);
509 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
510 HeapFree(GetProcessHeap(), 0, ptr);
512 break;
513 case MCI_SAVE:
514 case MCI_LOAD:
515 case MCI_CAPTURE:
516 case MCI_RESTORE:
517 { /* All these commands have the same layout: callback + string + optional rect */
518 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
520 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
521 HeapFree(GetProcessHeap(), 0, mci_loadW);
523 break;
524 case MCI_SOUND:
525 case MCI_ESCAPE:
526 { /* All these commands have the same layout: callback + string */
527 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
529 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
530 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
532 break;
534 default:
535 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
536 break;
540 /**************************************************************************
541 * MCI_GetDevTypeFromFileName [internal]
543 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
545 LPCWSTR tmp;
546 HKEY hKey;
547 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
548 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
549 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
550 if ((tmp = strrchrW(fileName, '.'))) {
551 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
552 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
553 DWORD dwLen = len;
554 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
555 RegCloseKey( hKey );
556 if (lRet == ERROR_SUCCESS) return 0;
558 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
560 return MCIERR_EXTENSION_NOT_FOUND;
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_INTEGER: if (!*str) return FALSE; break;
607 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
608 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
609 case MCI_FLAG: if (!*str) return FALSE; break;
610 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
611 case MCI_RECT: if (!*str || inCst) return FALSE; break;
612 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
613 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
614 default: return FALSE;
616 } while (eid != MCI_END_COMMAND_LIST);
617 return TRUE;
620 /**************************************************************************
621 * MCI_DumpCommandTable [internal]
623 static BOOL MCI_DumpCommandTable(UINT uTbl)
625 const BYTE* lmem;
626 LPCWSTR str;
627 DWORD flg;
628 WORD eid;
630 if (!MCI_IsCommandTableValid(uTbl)) {
631 ERR("Ooops: %d is not valid\n", uTbl);
632 return FALSE;
635 lmem = S_MciCmdTable[uTbl].lpTable;
636 do {
637 do {
638 str = (LPCWSTR)lmem;
639 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
640 flg = *(const DWORD*)lmem;
641 eid = *(const WORD*)(lmem + sizeof(DWORD));
642 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
643 lmem += sizeof(DWORD) + sizeof(WORD);
644 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
645 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
646 } while (eid != MCI_END_COMMAND_LIST);
647 return TRUE;
651 /**************************************************************************
652 * MCI_GetCommandTable [internal]
654 static UINT MCI_GetCommandTable(UINT uDevType)
656 UINT uTbl;
657 WCHAR buf[32];
658 LPCWSTR str = NULL;
660 /* first look up existing for existing devType */
661 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
662 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
663 return uTbl;
666 /* well try to load id */
667 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
668 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
669 str = buf;
671 } else if (uDevType == 0) {
672 static const WCHAR wszCore[] = {'C','O','R','E',0};
673 str = wszCore;
675 uTbl = MCI_NO_COMMAND_TABLE;
676 if (str) {
677 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
678 HANDLE hMem = 0;
680 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
681 if (hMem) {
682 uTbl = MCI_SetCommandTable(hMem, uDevType);
683 } else {
684 WARN("No command table found in resource %p[%s]\n",
685 hWinMM32Instance, debugstr_w(str));
688 TRACE("=> %d\n", uTbl);
689 return uTbl;
692 /**************************************************************************
693 * MCI_SetCommandTable [internal]
695 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
697 int uTbl;
698 static BOOL bInitDone = FALSE;
700 /* <HACK>
701 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
702 * can be called with 0 as a uDevType to retrieve it.
703 * </HACK>
705 if (!bInitDone) {
706 bInitDone = TRUE;
707 MCI_GetCommandTable(0);
709 TRACE("(%p, %u)\n", hMem, uDevType);
710 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
711 if (!S_MciCmdTable[uTbl].lpTable) {
712 const BYTE* lmem;
713 LPCWSTR str;
714 WORD eid;
715 WORD count;
717 S_MciCmdTable[uTbl].uDevType = uDevType;
718 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
719 S_MciCmdTable[uTbl].hMem = hMem;
721 if (TRACE_ON(mci)) {
722 MCI_DumpCommandTable(uTbl);
725 /* create the verbs table */
726 /* get # of entries */
727 lmem = S_MciCmdTable[uTbl].lpTable;
728 count = 0;
729 do {
730 str = (LPCWSTR)lmem;
731 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
732 eid = *(const WORD*)(lmem + sizeof(DWORD));
733 lmem += sizeof(DWORD) + sizeof(WORD);
734 if (eid == MCI_COMMAND_HEAD)
735 count++;
736 } while (eid != MCI_END_COMMAND_LIST);
738 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
739 S_MciCmdTable[uTbl].nVerbs = count;
741 lmem = S_MciCmdTable[uTbl].lpTable;
742 count = 0;
743 do {
744 str = (LPCWSTR)lmem;
745 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
746 eid = *(const WORD*)(lmem + sizeof(DWORD));
747 lmem += sizeof(DWORD) + sizeof(WORD);
748 if (eid == MCI_COMMAND_HEAD)
749 S_MciCmdTable[uTbl].aVerbs[count++] = str;
750 } while (eid != MCI_END_COMMAND_LIST);
751 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
752 return uTbl;
756 return MCI_NO_COMMAND_TABLE;
759 /**************************************************************************
760 * MCI_UnLoadMciDriver [internal]
762 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
764 LPWINE_MCIDRIVER* tmp;
766 if (!wmd)
767 return TRUE;
769 CloseDriver(wmd->hDriver, 0, 0);
771 if (wmd->dwPrivate != 0)
772 WARN("Unloading mci driver with non nul dwPrivate field\n");
774 EnterCriticalSection(&WINMM_cs);
775 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
776 if (*tmp == wmd) {
777 *tmp = wmd->lpNext;
778 break;
781 LeaveCriticalSection(&WINMM_cs);
783 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
784 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
785 HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
787 HeapFree(GetProcessHeap(), 0, wmd);
788 return TRUE;
791 /**************************************************************************
792 * MCI_OpenMciDriver [internal]
794 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
796 WCHAR libName[128];
798 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
799 return FALSE;
801 /* First load driver */
802 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
803 return wmd->hDriver != NULL;
806 /**************************************************************************
807 * MCI_LoadMciDriver [internal]
809 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
811 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
812 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
813 MCI_OPEN_DRIVER_PARMSW modp;
814 DWORD dwRet = 0;
816 if (!wmd || !strDevTyp) {
817 dwRet = MCIERR_OUT_OF_MEMORY;
818 goto errCleanUp;
821 wmd->lpfnYieldProc = MCI_DefYieldProc;
822 wmd->dwYieldData = VK_CANCEL;
823 wmd->CreatorThread = GetCurrentThreadId();
825 EnterCriticalSection(&WINMM_cs);
826 /* wmd must be inserted in list before sending opening the driver, because it
827 * may want to lookup at wDevID
829 wmd->lpNext = MciDrivers;
830 MciDrivers = wmd;
832 for (modp.wDeviceID = MCI_MAGIC;
833 MCI_GetDriver(modp.wDeviceID) != 0;
834 modp.wDeviceID++);
836 wmd->wDeviceID = modp.wDeviceID;
838 LeaveCriticalSection(&WINMM_cs);
840 TRACE("wDevID=%04X\n", modp.wDeviceID);
842 modp.lpstrParams = NULL;
844 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
845 /* silence warning if all is used... some bogus program use commands like
846 * 'open all'...
848 if (strcmpiW(strDevTyp, wszAll) == 0) {
849 dwRet = MCIERR_CANNOT_USE_ALL;
850 } else {
851 FIXME("Couldn't load driver for type %s.\n",
852 debugstr_w(strDevTyp));
853 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
855 goto errCleanUp;
858 /* FIXME: should also check that module's description is of the form
859 * MODULENAME:[MCI] comment
862 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
863 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
864 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
866 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
867 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
869 wmd->lpstrDeviceType = strDevTyp;
870 wmd->wType = modp.wType;
872 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
873 modp.wDeviceID, modp.wType, modp.wDeviceID);
874 *lpwmd = wmd;
875 return 0;
876 errCleanUp:
877 MCI_UnLoadMciDriver(wmd);
878 HeapFree(GetProcessHeap(), 0, strDevTyp);
879 *lpwmd = 0;
880 return dwRet;
883 /**************************************************************************
884 * MCI_SendCommandFrom32 [internal]
886 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
888 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
889 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
891 if (wmd) {
892 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
894 return dwRet;
897 /**************************************************************************
898 * MCI_FinishOpen [internal]
900 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
901 DWORD dwParam)
903 if (dwParam & MCI_OPEN_ELEMENT)
905 wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
906 strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
908 if (dwParam & MCI_OPEN_ALIAS)
910 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
911 strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
913 lpParms->wDeviceID = wmd->wDeviceID;
915 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
916 (DWORD_PTR)lpParms);
919 /**************************************************************************
920 * MCI_FindCommand [internal]
922 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
924 UINT idx;
926 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
927 return NULL;
929 /* another improvement would be to have the aVerbs array sorted,
930 * so that we could use a dichotomic search on it, rather than this dumb
931 * array look up
933 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
934 if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
935 return S_MciCmdTable[uTbl].aVerbs[idx];
938 return NULL;
941 /**************************************************************************
942 * MCI_GetReturnType [internal]
944 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
946 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
947 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
948 return *(const DWORD*)(lpCmd + 1);
950 return 0L;
953 /**************************************************************************
954 * MCI_GetMessage [internal]
956 static WORD MCI_GetMessage(LPCWSTR lpCmd)
958 return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
961 /**************************************************************************
962 * MCI_GetDWord [internal]
964 static BOOL MCI_GetDWord(DWORD_PTR* data, LPWSTR* ptr)
966 DWORD val;
967 LPWSTR ret;
969 val = strtoulW(*ptr, &ret, 0);
971 switch (*ret) {
972 case '\0': break;
973 case ' ': ret++; break;
974 default: return FALSE;
977 *data |= val;
978 *ptr = ret;
979 return TRUE;
982 /**************************************************************************
983 * MCI_GetString [internal]
985 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
987 LPWSTR ptr = *args;
989 /* see if we have a quoted string */
990 if (*ptr == '"') {
991 ptr = strchrW(*str = ptr + 1, '"');
992 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
993 /* FIXME: shall we escape \" from string ?? */
994 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
995 *ptr++ = '\0'; /* remove trailing " */
996 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
997 } else {
998 ptr = strchrW(ptr, ' ');
1000 if (ptr) {
1001 *ptr++ = '\0';
1002 } else {
1003 ptr = *args + strlenW(*args);
1005 *str = *args;
1008 *args = ptr;
1009 return 0;
1012 #define MCI_DATA_SIZE 16
1014 /**************************************************************************
1015 * MCI_ParseOptArgs [internal]
1017 static DWORD MCI_ParseOptArgs(DWORD_PTR* data, int _offset, LPCWSTR lpCmd,
1018 LPWSTR args, LPDWORD dwFlags)
1020 int len, offset;
1021 const char* lmem;
1022 LPCWSTR str;
1023 DWORD dwRet, flg, cflg = 0;
1024 WORD eid;
1025 BOOL inCst, found;
1027 /* loop on arguments */
1028 while (*args) {
1029 lmem = (const char*)lpCmd;
1030 found = inCst = FALSE;
1031 offset = _offset;
1033 /* skip any leading white space(s) */
1034 while (*args == ' ') args++;
1035 TRACE("args=%s\n", debugstr_w(args));
1037 do { /* loop on options for command table for the requested verb */
1038 str = (LPCWSTR)lmem;
1039 lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1040 flg = *(const DWORD*)lmem;
1041 eid = *(const WORD*)(lmem + sizeof(DWORD));
1042 lmem += sizeof(DWORD) + sizeof(WORD);
1043 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1045 switch (eid) {
1046 case MCI_CONSTANT:
1047 inCst = TRUE; cflg = flg; break;
1048 case MCI_END_CONSTANT:
1049 /* there may be additional integral values after flag in constant */
1050 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1051 *dwFlags |= cflg;
1053 inCst = FALSE; cflg = 0;
1054 break;
1055 case MCI_RETURN:
1056 if (offset != _offset) {
1057 ERR("MCI_RETURN not in first position\n");
1058 return MCIERR_PARSER_INTERNAL;
1062 if (strncmpiW(args, str, len) == 0 &&
1063 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1064 /* store good values into data[] */
1065 args += len;
1066 while (*args == ' ') args++;
1067 found = TRUE;
1069 switch (eid) {
1070 case MCI_COMMAND_HEAD:
1071 case MCI_RETURN:
1072 case MCI_END_COMMAND:
1073 case MCI_END_COMMAND_LIST:
1074 case MCI_CONSTANT: /* done above */
1075 case MCI_END_CONSTANT: /* done above */
1076 break;
1077 case MCI_FLAG:
1078 *dwFlags |= flg;
1079 TRACE("flag=%08x\n", flg);
1080 break;
1081 case MCI_INTEGER:
1082 if (inCst) {
1083 data[offset] |= flg;
1084 *dwFlags |= cflg;
1085 inCst = FALSE;
1086 TRACE("flag=%08x constant=%08x\n", cflg, flg);
1087 } else {
1088 *dwFlags |= flg;
1089 if (!MCI_GetDWord(&(data[offset]), &args)) {
1090 return MCIERR_BAD_INTEGER;
1092 TRACE("flag=%08x int=%ld\n", flg, data[offset]);
1094 break;
1095 case MCI_RECT:
1096 /* store rect in data (offset..offset+3) */
1097 *dwFlags |= flg;
1098 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1099 !MCI_GetDWord(&(data[offset+1]), &args) ||
1100 !MCI_GetDWord(&(data[offset+2]), &args) ||
1101 !MCI_GetDWord(&(data[offset+3]), &args)) {
1102 ERR("Bad rect %s\n", debugstr_w(args));
1103 return MCIERR_BAD_INTEGER;
1105 TRACE("flag=%08x for rectangle\n", flg);
1106 break;
1107 case MCI_STRING:
1108 *dwFlags |= flg;
1109 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1110 return dwRet;
1111 TRACE("flag=%08x string=%s\n", flg, debugstr_w((LPWSTR)data[offset]));
1112 break;
1113 default: ERR("oops\n");
1115 /* exit inside while loop, except if just entered in constant area definition */
1116 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1117 } else {
1118 /* have offset incremented if needed */
1119 switch (eid) {
1120 case MCI_COMMAND_HEAD:
1121 case MCI_RETURN:
1122 case MCI_END_COMMAND:
1123 case MCI_END_COMMAND_LIST:
1124 case MCI_CONSTANT:
1125 case MCI_FLAG: break;
1126 case MCI_INTEGER: if (!inCst) offset++; break;
1127 case MCI_END_CONSTANT:
1128 case MCI_STRING: offset++; break;
1129 case MCI_RECT: offset += 4; break;
1130 default: ERR("oops\n");
1133 } while (eid != MCI_END_COMMAND);
1134 if (!found) {
1135 WARN("Optarg %s not found\n", debugstr_w(args));
1136 return MCIERR_UNRECOGNIZED_COMMAND;
1138 if (offset == MCI_DATA_SIZE) {
1139 ERR("Internal data[] buffer overflow\n");
1140 return MCIERR_PARSER_INTERNAL;
1143 return 0;
1146 /**************************************************************************
1147 * MCI_HandleReturnValues [internal]
1149 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1150 DWORD_PTR* data, LPWSTR lpstrRet, UINT uRetLen)
1152 static const WCHAR wszLd [] = {'%','l','d',0};
1153 static const WCHAR wszLd4 [] = {'%','l','d',' ','%','l','d',' ','%','l','d',' ','%','l','d',0};
1154 static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1155 static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1157 if (lpstrRet) {
1158 switch (retType) {
1159 case 0: /* nothing to return */
1160 break;
1161 case MCI_INTEGER:
1162 switch (dwRet & 0xFFFF0000ul) {
1163 case 0:
1164 case MCI_INTEGER_RETURNED:
1165 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1166 break;
1167 case MCI_RESOURCE_RETURNED:
1168 /* return string which ID is HIWORD(data[1]),
1169 * string is loaded from mmsystem.dll */
1170 LoadStringW(hWinMM32Instance, HIWORD(data[1]), lpstrRet, uRetLen);
1171 break;
1172 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1173 /* return string which ID is HIWORD(data[1]),
1174 * string is loaded from driver */
1175 /* FIXME: this is wrong for a 16 bit handle */
1176 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1177 HIWORD(data[1]), lpstrRet, uRetLen);
1178 break;
1179 case MCI_COLONIZED3_RETURN:
1180 snprintfW(lpstrRet, uRetLen, wszCol3,
1181 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1182 LOBYTE(HIWORD(data[1])));
1183 break;
1184 case MCI_COLONIZED4_RETURN:
1185 snprintfW(lpstrRet, uRetLen, wszCol4,
1186 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1187 LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
1188 break;
1189 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1191 break;
1192 case MCI_STRING:
1193 switch (dwRet & 0xFFFF0000ul) {
1194 case 0:
1195 /* nothing to do data[1] == lpstrRet */
1196 break;
1197 case MCI_INTEGER_RETURNED:
1198 data[1] = *(LPDWORD)lpstrRet;
1199 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1200 break;
1201 default:
1202 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1203 break;
1205 break;
1206 case MCI_RECT:
1207 if (dwRet & 0xFFFF0000ul)
1208 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1209 snprintfW(lpstrRet, uRetLen, wszLd4,
1210 data[1], data[2], data[3], data[4]);
1211 break;
1212 default: ERR("oops\n");
1215 return LOWORD(dwRet);
1218 /**************************************************************************
1219 * mciSendStringW [WINMM.@]
1221 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1222 UINT uRetLen, HWND hwndCallback)
1224 LPWSTR verb, dev, args;
1225 LPWINE_MCIDRIVER wmd = 0;
1226 MCIDEVICEID uDevID;
1227 DWORD dwFlags = 0, dwRet = 0;
1228 int offset = 0;
1229 DWORD_PTR data[MCI_DATA_SIZE];
1230 DWORD retType;
1231 LPCWSTR lpCmd = 0;
1232 static const WCHAR wszNew[] = {'n','e','w',0};
1233 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1234 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1235 static const WCHAR wszSysinfo[] = {'s','y','s','i','n','f','o',0};
1236 static const WCHAR wszSound[] = {'s','o','u','n','d',0};
1237 static const WCHAR wszBreak[] = {'b','r','e','a','k',0};
1239 TRACE("(%s, %p, %d, %p)\n",
1240 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1242 /* format is <command> <device> <optargs> */
1243 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1244 return MCIERR_OUT_OF_MEMORY;
1245 strcpyW( verb, lpstrCommand );
1246 CharLowerW(verb);
1248 memset(data, 0, sizeof(data));
1250 if (!(args = strchrW(verb, ' '))) {
1251 dwRet = MCIERR_MISSING_DEVICE_NAME;
1252 goto errCleanUp;
1254 *args++ = '\0';
1255 if ((dwRet = MCI_GetString(&dev, &args))) {
1256 goto errCleanUp;
1258 uDevID = strcmpiW(dev, wszAll) ? 0 : MCI_ALL_DEVICE_ID;
1260 /* Determine devType from open */
1261 if (!strcmpW(verb, wszOpen)) {
1262 LPWSTR devType, tmp;
1263 WCHAR buf[128];
1265 /* case dev == 'new' has to be handled */
1266 if (!strcmpW(dev, wszNew)) {
1267 dev = 0;
1268 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1269 devType += 5;
1270 tmp = strchrW(devType, ' ');
1271 if (tmp) *tmp = '\0';
1272 devType = str_dup_upper(devType);
1273 if (tmp) *tmp = ' ';
1274 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1275 } else {
1276 WARN("open new requires device type\n");
1277 dwRet = MCIERR_MISSING_DEVICE_NAME;
1278 goto errCleanUp;
1280 } else if ((devType = strchrW(dev, '!')) != NULL) {
1281 *devType++ = '\0';
1282 tmp = devType; devType = dev; dev = tmp;
1284 dwFlags |= MCI_OPEN_TYPE;
1285 data[2] = (DWORD_PTR)devType;
1286 devType = str_dup_upper(devType);
1287 dwFlags |= MCI_OPEN_ELEMENT;
1288 data[3] = (DWORD_PTR)dev;
1289 } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1290 /* this is the name of a mci driver's type */
1291 tmp = strchrW(dev, ' ');
1292 if (tmp) *tmp = '\0';
1293 data[2] = (DWORD_PTR)dev;
1294 devType = str_dup_upper(dev);
1295 if (tmp) *tmp = ' ';
1296 dwFlags |= MCI_OPEN_TYPE;
1297 } else {
1298 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1299 devType += 5;
1300 tmp = strchrW(devType, ' ');
1301 if (tmp) *tmp = '\0';
1302 devType = str_dup_upper(devType);
1303 if (tmp) *tmp = ' ';
1304 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1305 } else {
1306 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1307 goto errCleanUp;
1309 devType = str_dup_upper(buf);
1311 dwFlags |= MCI_OPEN_ELEMENT;
1312 data[3] = (DWORD_PTR)dev;
1314 if (MCI_ALL_DEVICE_ID == uDevID) {
1315 dwRet = MCIERR_CANNOT_USE_ALL;
1316 goto errCleanUp;
1318 if (!strstrW(args, wszSAliasS) && !dev) {
1319 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1320 goto errCleanUp;
1323 dwRet = MCI_LoadMciDriver(devType, &wmd);
1324 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1325 dwRet = MCIERR_INVALID_DEVICE_NAME;
1326 HeapFree(GetProcessHeap(), 0, devType);
1327 if (dwRet) {
1328 MCI_UnLoadMciDriver(wmd);
1329 goto errCleanUp;
1331 } else if (!strcmpW(verb, wszSysinfo)) {
1332 /* System commands are not subject to auto-open. */
1333 /* It's too early to handle Sysinfo here because the
1334 * requirements on dev depend on the flags:
1335 * alias with INSTALLNAME, name like "waveaudio"
1336 * with QUANTITY and NAME. */
1337 data[4] = MCI_ALL_DEVICE_ID;
1338 if (MCI_ALL_DEVICE_ID != uDevID) {
1339 /* FIXME: Map device name like waveaudio to MCI_DEVTYPE_xyz */
1340 uDevID = mciGetDeviceIDW(dev);
1341 wmd = MCI_GetDriver(uDevID);
1342 if (wmd)
1343 data[4] = wmd->wType;
1345 } else if (!strcmpW(verb, wszSound) || !strcmpW(verb, wszBreak)) {
1346 /* Prevent auto-open for system commands. */
1347 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))) {
1348 /* auto open */
1349 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1350 WCHAR buf[128];
1351 sprintfW(buf, wszOpenWait, dev);
1353 if ((dwRet = mciSendStringW(buf, NULL, 0, 0)) != 0)
1354 goto errCleanUp;
1356 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1357 if (!wmd) {
1358 /* FIXME: memory leak, MCI driver is not closed */
1359 dwRet = MCIERR_INVALID_DEVICE_ID;
1360 goto errCleanUp;
1364 /* get the verb in the different command tables */
1365 if (wmd) {
1366 /* try the device specific command table */
1367 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1368 if (!lpCmd) {
1369 /* try the type specific command table */
1370 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1371 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1372 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1373 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1376 /* try core command table */
1377 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1379 if (!lpCmd) {
1380 TRACE("Command %s not found!\n", debugstr_w(verb));
1381 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1382 goto errCleanUp;
1385 /* set return information */
1386 switch (retType = MCI_GetReturnType(lpCmd)) {
1387 case 0: offset = 1; break;
1388 case MCI_INTEGER: offset = 2; break;
1389 case MCI_STRING: data[1] = (DWORD_PTR)lpstrRet; data[2] = uRetLen; offset = 3; break;
1390 case MCI_RECT: offset = 5; break;
1391 default: ERR("oops\n");
1394 TRACE("verb=%s on dev=%s; offset=%d\n",
1395 debugstr_w(verb), debugstr_w(dev), offset);
1397 if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1398 goto errCleanUp;
1400 /* set up call back */
1401 if (dwFlags & MCI_NOTIFY) {
1402 data[0] = (DWORD_PTR)hwndCallback;
1405 /* FIXME: the command should get it's own notification window set up and
1406 * ask for device closing while processing the notification mechanism
1408 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1410 TRACE("[%d, %s, %08x, %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx %08lx]\n",
1411 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1412 data[0], data[1], data[2], data[3], data[4],
1413 data[5], data[6], data[7], data[8], data[9]);
1415 if (strcmpW(verb, wszOpen) == 0) {
1416 if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSW)data, dwFlags)))
1417 MCI_UnLoadMciDriver(wmd);
1418 /* FIXME: notification is not properly shared across two opens */
1419 } else {
1420 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, MCI_GetMessage(lpCmd), dwFlags, (DWORD_PTR)data);
1422 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1423 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1424 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1426 errCleanUp:
1427 HeapFree(GetProcessHeap(), 0, verb);
1428 return dwRet;
1431 /**************************************************************************
1432 * mciSendStringA [WINMM.@]
1434 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1435 UINT uRetLen, HWND hwndCallback)
1437 LPWSTR lpwstrCommand;
1438 LPWSTR lpwstrRet = NULL;
1439 UINT ret;
1440 INT len;
1442 /* FIXME: is there something to do with lpstrReturnString ? */
1443 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1444 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1445 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1446 if (lpstrRet)
1448 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1449 if (!lpwstrRet) {
1450 WARN("no memory\n");
1451 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1452 return MCIERR_OUT_OF_MEMORY;
1455 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1456 if (!ret && lpwstrRet)
1457 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1458 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1459 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1460 return ret;
1463 /**************************************************************************
1464 * mciExecute [WINMM.@]
1466 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1468 char strRet[256];
1469 DWORD ret;
1471 TRACE("(%s)!\n", lpstrCommand);
1473 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1474 if (ret != 0) {
1475 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1476 sprintf(strRet, "Unknown MCI error (%d)", ret);
1478 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1480 /* FIXME: what shall I return ? */
1481 return TRUE;
1484 /**************************************************************************
1485 * mciLoadCommandResource [WINMM.@]
1487 * Strangely, this function only exists as a UNICODE one.
1489 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1491 UINT ret = MCI_NO_COMMAND_TABLE;
1492 HRSRC hRsrc = 0;
1493 HGLOBAL hMem;
1495 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1497 /* if a file named "resname.mci" exits, then load resource "resname" from it
1498 * otherwise directly from driver
1499 * We don't support it (who uses this feature ?), but we check anyway
1501 if (!type) {
1502 #if 0
1503 /* FIXME: we should put this back into order, but I never found a program
1504 * actually using this feature, so we may not need it
1506 char buf[128];
1507 OFSTRUCT ofs;
1509 strcat(strcpy(buf, resname), ".mci");
1510 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1511 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1513 #endif
1515 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1516 (hMem = LoadResource(hInst, hRsrc))) {
1517 ret = MCI_SetCommandTable(hMem, type);
1518 FreeResource(hMem);
1520 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1522 TRACE("=> %04x\n", ret);
1523 return ret;
1526 /**************************************************************************
1527 * mciFreeCommandResource [WINMM.@]
1529 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1531 TRACE("(%08x)!\n", uTable);
1533 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1534 return FALSE;
1536 FreeResource(S_MciCmdTable[uTable].hMem);
1537 S_MciCmdTable[uTable].hMem = NULL;
1538 S_MciCmdTable[uTable].lpTable = NULL;
1539 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1540 S_MciCmdTable[uTable].aVerbs = 0;
1541 S_MciCmdTable[uTable].nVerbs = 0;
1542 return TRUE;
1545 /**************************************************************************
1546 * MCI_Open [internal]
1548 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1550 WCHAR strDevTyp[128];
1551 DWORD dwRet;
1552 LPWINE_MCIDRIVER wmd = NULL;
1554 TRACE("(%08X, %p)\n", dwParam, lpParms);
1555 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1557 /* only two low bytes are generic, the other ones are dev type specific */
1558 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1559 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1560 MCI_NOTIFY|MCI_WAIT)
1561 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1562 FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1564 #undef WINE_MCIDRIVER_SUPP
1566 strDevTyp[0] = 0;
1568 if (dwParam & MCI_OPEN_TYPE) {
1569 if (dwParam & MCI_OPEN_TYPE_ID) {
1570 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1572 if (uDevType < MCI_DEVTYPE_FIRST ||
1573 uDevType > MCI_DEVTYPE_LAST ||
1574 !LoadStringW(hWinMM32Instance, uDevType,
1575 strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1576 dwRet = MCIERR_BAD_INTEGER;
1577 goto errCleanUp;
1579 } else {
1580 LPWSTR ptr;
1581 if (lpParms->lpstrDeviceType == NULL) {
1582 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1583 goto errCleanUp;
1585 strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1586 ptr = strchrW(strDevTyp, '!');
1587 if (ptr) {
1588 /* this behavior is not documented in windows. However, since, in
1589 * some occasions, MCI_OPEN handling is translated by WinMM into
1590 * a call to mciSendString("open <type>"); this code shall be correct
1592 if (dwParam & MCI_OPEN_ELEMENT) {
1593 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1594 debugstr_w(lpParms->lpstrElementName),
1595 debugstr_w(strDevTyp));
1596 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1597 goto errCleanUp;
1599 dwParam |= MCI_OPEN_ELEMENT;
1600 *ptr++ = 0;
1601 /* FIXME: not a good idea to write in user supplied buffer */
1602 lpParms->lpstrElementName = ptr;
1606 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1609 if (dwParam & MCI_OPEN_ELEMENT) {
1610 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1612 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1613 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1614 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1615 goto errCleanUp;
1618 if (!lpParms->lpstrElementName) {
1619 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1620 goto errCleanUp;
1623 /* type, if given as a parameter, supersedes file extension */
1624 if (!strDevTyp[0] &&
1625 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1626 strDevTyp, sizeof(strDevTyp))) {
1627 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1628 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1629 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1630 goto errCleanUp;
1632 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1633 strcpyW(strDevTyp, wszCdAudio);
1637 if (strDevTyp[0] == 0) {
1638 FIXME("Couldn't load driver\n");
1639 dwRet = MCIERR_INVALID_DEVICE_NAME;
1640 goto errCleanUp;
1643 if (dwParam & MCI_OPEN_ALIAS) {
1644 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1645 if (!lpParms->lpstrAlias) {
1646 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1647 goto errCleanUp;
1651 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1652 goto errCleanUp;
1655 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1656 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1657 /* FIXME: is dwRet the correct ret code ? */
1658 goto errCleanUp;
1661 /* only handled devices fall through */
1662 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1663 return 0;
1665 errCleanUp:
1666 if (wmd) MCI_UnLoadMciDriver(wmd);
1667 return dwRet;
1670 /**************************************************************************
1671 * MCI_Close [internal]
1673 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1675 DWORD dwRet;
1676 LPWINE_MCIDRIVER wmd;
1678 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1680 /* Every device must handle MCI_NOTIFY on its own. */
1681 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1682 while (MciDrivers) {
1683 /* Retrieve the device ID under lock, but send the message without,
1684 * the driver might be calling some winmm functions from another
1685 * thread before being fully stopped.
1687 EnterCriticalSection(&WINMM_cs);
1688 if (!MciDrivers)
1690 LeaveCriticalSection(&WINMM_cs);
1691 break;
1693 wDevID = MciDrivers->wDeviceID;
1694 LeaveCriticalSection(&WINMM_cs);
1695 MCI_Close(wDevID, dwParam, lpParms);
1697 return 0;
1700 if (!(wmd = MCI_GetDriver(wDevID))) {
1701 return MCIERR_INVALID_DEVICE_ID;
1704 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1706 MCI_UnLoadMciDriver(wmd);
1708 return dwRet;
1711 /**************************************************************************
1712 * MCI_WriteString [internal]
1714 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1716 DWORD ret = 0;
1718 if (lpSrcStr) {
1719 if (dstSize <= strlenW(lpSrcStr)) {
1720 lstrcpynW(lpDstStr, lpSrcStr, dstSize - 1);
1721 ret = MCIERR_PARAM_OVERFLOW;
1722 } else {
1723 strcpyW(lpDstStr, lpSrcStr);
1725 } else {
1726 *lpDstStr = 0;
1728 return ret;
1731 /**************************************************************************
1732 * MCI_Sysinfo [internal]
1734 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1736 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1737 WCHAR buf[2048], *s = buf, *p;
1738 LPWINE_MCIDRIVER wmd;
1739 HKEY hKey;
1741 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1742 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1744 TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1745 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1746 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1747 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1749 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1750 case MCI_SYSINFO_QUANTITY:
1751 if (lpParms->dwRetSize < sizeof(DWORD))
1752 return MCIERR_PARAM_OVERFLOW;
1753 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1754 if (uDevID == MCI_ALL_DEVICE_ID) {
1755 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1756 if (dwFlags & MCI_SYSINFO_OPEN) {
1757 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1758 EnterCriticalSection(&WINMM_cs);
1759 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1760 cnt++;
1762 LeaveCriticalSection(&WINMM_cs);
1763 } else {
1764 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1765 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1766 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1767 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1768 RegCloseKey( hKey );
1770 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1771 for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1773 } else {
1774 if (dwFlags & MCI_SYSINFO_OPEN) {
1775 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1776 EnterCriticalSection(&WINMM_cs);
1777 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1778 if (wmd->wType == lpParms->wDeviceType) cnt++;
1780 LeaveCriticalSection(&WINMM_cs);
1781 } else {
1782 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1783 FIXME("Don't know how to get # of MCI devices of a given type\n");
1784 cnt = 1;
1787 *(DWORD*)lpParms->lpstrReturn = cnt;
1788 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1789 ret = MCI_INTEGER_RETURNED;
1790 /* return ret; Only Win9x sends a notification in this case. */
1791 break;
1792 case MCI_SYSINFO_INSTALLNAME:
1793 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1794 if ((wmd = MCI_GetDriver(uDevID))) {
1795 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1796 wmd->lpstrDeviceType);
1797 } else {
1798 *lpParms->lpstrReturn = 0;
1799 ret = MCIERR_INVALID_DEVICE_ID;
1801 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1802 break;
1803 case MCI_SYSINFO_NAME:
1804 s = NULL;
1805 if (dwFlags & MCI_SYSINFO_OPEN) {
1806 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1807 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1808 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1809 EnterCriticalSection(&WINMM_cs);
1810 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1811 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1812 if (uDevID == MCI_ALL_DEVICE_ID ||
1813 lpParms->wDeviceType == wmd->wType) {
1814 cnt++;
1815 if (cnt == lpParms->dwNumber) {
1816 s = wmd->lpstrAlias;
1817 break;
1821 LeaveCriticalSection(&WINMM_cs);
1822 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1823 } else if (MCI_ALL_DEVICE_ID == uDevID) {
1824 TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
1825 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1826 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1827 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1828 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1829 lpParms->dwNumber <= cnt) {
1830 DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
1831 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1832 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1833 s = buf;
1835 RegCloseKey( hKey );
1837 if (!s) {
1838 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1839 for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1840 TRACE("%d: %s\n", cnt, debugstr_w(p));
1841 if (cnt == lpParms->dwNumber - 1) {
1842 s = p;
1843 break;
1848 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1849 } else {
1850 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
1851 /* Cheating: what is asked for is the nth device from the registry. */
1852 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
1853 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
1854 ret = MCIERR_OUTOFRANGE;
1855 else {
1856 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
1857 lpParms->lpstrReturn, lpParms->dwRetSize);
1858 ret = 0;
1861 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1862 break;
1863 default:
1864 TRACE("Unsupported flag value=%08x\n", dwFlags);
1865 ret = MCIERR_UNRECOGNIZED_KEYWORD;
1867 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
1868 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
1869 return ret;
1872 /**************************************************************************
1873 * MCI_Break [internal]
1875 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1877 DWORD dwRet = 0;
1879 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1880 FIXME("(%04x) vkey %04X stub\n", dwFlags, lpParms->nVirtKey);
1882 if (MMSYSERR_NOERROR==dwRet && (dwFlags & MCI_NOTIFY))
1883 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
1884 return dwRet;
1887 /**************************************************************************
1888 * MCI_Sound [internal]
1890 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
1892 DWORD dwRet = 0;
1894 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1896 if (dwFlags & MCI_SOUND_NAME)
1897 dwRet = sndPlaySoundW(lpParms->lpstrSoundName, SND_SYNC) ? MMSYSERR_NOERROR : MMSYSERR_ERROR;
1898 else
1899 dwRet = MMSYSERR_ERROR; /* what should be done ??? */
1901 if (MMSYSERR_NOERROR==dwRet && (dwFlags & MCI_NOTIFY))
1902 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
1903 return dwRet;
1906 /**************************************************************************
1907 * MCI_SendCommand [internal]
1909 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1911 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1913 switch (wMsg) {
1914 case MCI_OPEN:
1915 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1916 break;
1917 case MCI_CLOSE:
1918 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1919 break;
1920 case MCI_SYSINFO:
1921 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
1922 break;
1923 case MCI_BREAK:
1924 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1925 break;
1926 case MCI_SOUND:
1927 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
1928 break;
1929 default:
1930 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1931 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1932 dwRet = MCIERR_CANNOT_USE_ALL;
1933 } else {
1934 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
1936 break;
1938 return dwRet;
1941 /**************************************************************************
1942 * MCI_CleanUp [internal]
1944 * Some MCI commands need to be cleaned-up (when not called from
1945 * mciSendString), because MCI drivers return extra information for string
1946 * transformation. This function gets rid of them.
1948 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
1950 if (LOWORD(dwRet))
1951 return LOWORD(dwRet);
1953 switch (wMsg) {
1954 case MCI_GETDEVCAPS:
1955 switch (dwRet & 0xFFFF0000ul) {
1956 case 0:
1957 case MCI_COLONIZED3_RETURN:
1958 case MCI_COLONIZED4_RETURN:
1959 case MCI_INTEGER_RETURNED:
1960 /* nothing to do */
1961 break;
1962 case MCI_RESOURCE_RETURNED:
1963 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1965 LPMCI_GETDEVCAPS_PARMS lmgp;
1967 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
1968 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
1969 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1971 break;
1972 default:
1973 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1974 HIWORD(dwRet), MCI_MessageToString(wMsg));
1976 break;
1977 case MCI_STATUS:
1978 switch (dwRet & 0xFFFF0000ul) {
1979 case 0:
1980 case MCI_COLONIZED3_RETURN:
1981 case MCI_COLONIZED4_RETURN:
1982 case MCI_INTEGER_RETURNED:
1983 /* nothing to do */
1984 break;
1985 case MCI_RESOURCE_RETURNED:
1986 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1988 LPMCI_STATUS_PARMS lsp;
1990 lsp = (LPMCI_STATUS_PARMS)dwParam2;
1991 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
1992 lsp->dwReturn = LOWORD(lsp->dwReturn);
1994 break;
1995 default:
1996 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1997 HIWORD(dwRet), MCI_MessageToString(wMsg));
1999 break;
2000 case MCI_SYSINFO:
2001 switch (dwRet & 0xFFFF0000ul) {
2002 case 0:
2003 case MCI_INTEGER_RETURNED:
2004 /* nothing to do */
2005 break;
2006 default:
2007 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2009 break;
2010 default:
2011 if (HIWORD(dwRet)) {
2012 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2013 dwRet, MCI_MessageToString(wMsg));
2015 break;
2017 return LOWORD(dwRet);
2020 /**************************************************************************
2021 * mciGetErrorStringW [WINMM.@]
2023 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2025 BOOL ret = FALSE;
2027 if (lpstrBuffer != NULL && uLength > 0 &&
2028 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2030 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2031 ret = TRUE;
2034 return ret;
2037 /**************************************************************************
2038 * mciGetErrorStringA [WINMM.@]
2040 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2042 BOOL ret = FALSE;
2044 if (lpstrBuffer != NULL && uLength > 0 &&
2045 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2047 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2048 ret = TRUE;
2051 return ret;
2054 /**************************************************************************
2055 * mciDriverNotify [WINMM.@]
2057 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2059 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2061 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2064 /**************************************************************************
2065 * mciGetDriverData [WINMM.@]
2067 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2069 LPWINE_MCIDRIVER wmd;
2071 TRACE("(%04x)\n", uDeviceID);
2073 wmd = MCI_GetDriver(uDeviceID);
2075 if (!wmd) {
2076 WARN("Bad uDeviceID\n");
2077 return 0L;
2080 return wmd->dwPrivate;
2083 /**************************************************************************
2084 * mciSetDriverData [WINMM.@]
2086 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2088 LPWINE_MCIDRIVER wmd;
2090 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2092 wmd = MCI_GetDriver(uDeviceID);
2094 if (!wmd) {
2095 WARN("Bad uDeviceID\n");
2096 return FALSE;
2099 wmd->dwPrivate = data;
2100 return TRUE;
2103 /**************************************************************************
2104 * mciSendCommandW [WINMM.@]
2107 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2109 DWORD dwRet;
2111 TRACE("(%08x, %s, %08lx, %08lx)\n",
2112 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2114 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2115 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2116 TRACE("=> %08x\n", dwRet);
2117 return dwRet;
2120 /**************************************************************************
2121 * mciSendCommandA [WINMM.@]
2123 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2125 DWORD ret;
2126 int mapped;
2128 TRACE("(%08x, %s, %08lx, %08lx)\n",
2129 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2131 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2132 if (mapped == -1)
2134 FIXME("message %04x mapping failed\n", wMsg);
2135 return MCIERR_OUT_OF_MEMORY;
2137 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2138 if (mapped)
2139 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2140 return ret;
2143 /**************************************************************************
2144 * mciGetDeviceIDA [WINMM.@]
2146 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2148 LPWSTR w = MCI_strdupAtoW(lpstrName);
2149 UINT ret = MCIERR_OUT_OF_MEMORY;
2151 if (w)
2153 ret = mciGetDeviceIDW(w);
2154 HeapFree(GetProcessHeap(), 0, w);
2156 return ret;
2159 /**************************************************************************
2160 * mciGetDeviceIDW [WINMM.@]
2162 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2164 return MCI_GetDriverFromString(lpwstrName);
2167 /**************************************************************************
2168 * MCI_DefYieldProc [internal]
2170 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2172 INT16 ret;
2173 MSG msg;
2175 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2177 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2178 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2179 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2180 ret = 0;
2181 } else {
2182 msg.hwnd = HWND_32(HIWORD(data));
2183 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2184 ret = -1;
2186 return ret;
2189 /**************************************************************************
2190 * mciSetYieldProc [WINMM.@]
2192 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2194 LPWINE_MCIDRIVER wmd;
2196 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2198 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2199 WARN("Bad uDeviceID\n");
2200 return FALSE;
2203 wmd->lpfnYieldProc = fpYieldProc;
2204 wmd->dwYieldData = dwYieldData;
2206 return TRUE;
2209 /**************************************************************************
2210 * mciGetDeviceIDFromElementIDA [WINMM.@]
2212 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2214 LPWSTR w = MCI_strdupAtoW(lpstrType);
2215 UINT ret = 0;
2217 if (w)
2219 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2220 HeapFree(GetProcessHeap(), 0, w);
2222 return ret;
2225 /**************************************************************************
2226 * mciGetDeviceIDFromElementIDW [WINMM.@]
2228 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2230 /* FIXME: that's rather strange, there is no
2231 * mciGetDeviceIDFromElementID32A in winmm.spec
2233 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2234 return 0;
2237 /**************************************************************************
2238 * mciGetYieldProc [WINMM.@]
2240 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2242 LPWINE_MCIDRIVER wmd;
2244 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2246 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2247 WARN("Bad uDeviceID\n");
2248 return NULL;
2250 if (!wmd->lpfnYieldProc) {
2251 WARN("No proc set\n");
2252 return NULL;
2254 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2255 return wmd->lpfnYieldProc;
2258 /**************************************************************************
2259 * mciGetCreatorTask [WINMM.@]
2261 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2263 LPWINE_MCIDRIVER wmd;
2264 HTASK ret = 0;
2266 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2268 TRACE("(%u) => %p\n", uDeviceID, ret);
2269 return ret;
2272 /**************************************************************************
2273 * mciDriverYield [WINMM.@]
2275 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2277 LPWINE_MCIDRIVER wmd;
2278 UINT ret = 0;
2280 TRACE("(%04x)\n", uDeviceID);
2282 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2283 MSG msg;
2284 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2285 } else {
2286 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2289 return ret;