winmm: MCI Close all causes one notification per open driver.
[wine/hacks.git] / dlls / winmm / mci.c
blob82b1de166712b6e688555aaad67598564ed08897
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 /* to be cross checked:
41 * - heapalloc for *sizeof(WCHAR) when needed
42 * - size of string in WCHAR or bytes? (#chars for MCI_INFO, #bytes for MCI_SYSINFO)
45 #include "config.h"
46 #include "wine/port.h"
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <string.h>
53 #include "windef.h"
54 #include "winbase.h"
55 #include "wingdi.h"
56 #include "mmsystem.h"
57 #include "winuser.h"
58 #include "winnls.h"
59 #include "winreg.h"
60 #include "wownt32.h"
62 #include "digitalv.h"
63 #include "winemm.h"
65 #include "wine/debug.h"
66 #include "wine/unicode.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(mci);
70 /* First MCI valid device ID (0 means error) */
71 #define MCI_MAGIC 0x0001
73 /* MCI settings */
74 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};
75 static const WCHAR wszNull [] = {0};
76 static const WCHAR wszAll [] = {'A','L','L',0};
77 static const WCHAR wszMci [] = {'M','C','I',0};
78 static const WCHAR wszOpen [] = {'o','p','e','n',0};
79 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
81 static WINE_MCIDRIVER *MciDrivers;
83 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
84 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
86 /* dup a string and uppercase it */
87 static inline LPWSTR str_dup_upper( LPCWSTR str )
89 INT len = (strlenW(str) + 1) * sizeof(WCHAR);
90 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
91 if (p)
93 memcpy( p, str, len );
94 CharUpperW( p );
96 return p;
99 /**************************************************************************
100 * MCI_GetDriver [internal]
102 static LPWINE_MCIDRIVER MCI_GetDriver(UINT16 wDevID)
104 LPWINE_MCIDRIVER wmd = 0;
106 EnterCriticalSection(&WINMM_cs);
107 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
108 if (wmd->wDeviceID == wDevID)
109 break;
111 LeaveCriticalSection(&WINMM_cs);
112 return wmd;
115 /**************************************************************************
116 * MCI_GetDriverFromString [internal]
118 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
120 LPWINE_MCIDRIVER wmd;
121 UINT ret = 0;
123 if (!lpstrName)
124 return 0;
126 if (!strcmpiW(lpstrName, wszAll))
127 return MCI_ALL_DEVICE_ID;
129 EnterCriticalSection(&WINMM_cs);
130 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
131 if (wmd->lpstrElementName && strcmpW(wmd->lpstrElementName, lpstrName) == 0) {
132 ret = wmd->wDeviceID;
133 break;
135 if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
136 ret = wmd->wDeviceID;
137 break;
139 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
140 ret = wmd->wDeviceID;
141 break;
144 LeaveCriticalSection(&WINMM_cs);
146 return ret;
149 /**************************************************************************
150 * MCI_MessageToString [internal]
152 const char* MCI_MessageToString(UINT wMsg)
154 static char buffer[100];
156 #define CASE(s) case (s): return #s
158 switch (wMsg) {
159 CASE(DRV_LOAD);
160 CASE(DRV_ENABLE);
161 CASE(DRV_OPEN);
162 CASE(DRV_CLOSE);
163 CASE(DRV_DISABLE);
164 CASE(DRV_FREE);
165 CASE(DRV_CONFIGURE);
166 CASE(DRV_QUERYCONFIGURE);
167 CASE(DRV_INSTALL);
168 CASE(DRV_REMOVE);
169 CASE(DRV_EXITSESSION);
170 CASE(DRV_EXITAPPLICATION);
171 CASE(DRV_POWER);
172 CASE(MCI_BREAK);
173 CASE(MCI_CLOSE);
174 CASE(MCI_CLOSE_DRIVER);
175 CASE(MCI_COPY);
176 CASE(MCI_CUE);
177 CASE(MCI_CUT);
178 CASE(MCI_DELETE);
179 CASE(MCI_ESCAPE);
180 CASE(MCI_FREEZE);
181 CASE(MCI_PAUSE);
182 CASE(MCI_PLAY);
183 CASE(MCI_GETDEVCAPS);
184 CASE(MCI_INFO);
185 CASE(MCI_LOAD);
186 CASE(MCI_OPEN);
187 CASE(MCI_OPEN_DRIVER);
188 CASE(MCI_PASTE);
189 CASE(MCI_PUT);
190 CASE(MCI_REALIZE);
191 CASE(MCI_RECORD);
192 CASE(MCI_RESUME);
193 CASE(MCI_SAVE);
194 CASE(MCI_SEEK);
195 CASE(MCI_SET);
196 CASE(MCI_SPIN);
197 CASE(MCI_STATUS);
198 CASE(MCI_STEP);
199 CASE(MCI_STOP);
200 CASE(MCI_SYSINFO);
201 CASE(MCI_UNFREEZE);
202 CASE(MCI_UPDATE);
203 CASE(MCI_WHERE);
204 CASE(MCI_WINDOW);
205 /* constants for digital video */
206 CASE(MCI_CAPTURE);
207 CASE(MCI_MONITOR);
208 CASE(MCI_RESERVE);
209 CASE(MCI_SETAUDIO);
210 CASE(MCI_SIGNAL);
211 CASE(MCI_SETVIDEO);
212 CASE(MCI_QUALITY);
213 CASE(MCI_LIST);
214 CASE(MCI_UNDO);
215 CASE(MCI_CONFIGURE);
216 CASE(MCI_RESTORE);
217 #undef CASE
218 default:
219 sprintf(buffer, "MCI_<<%04X>>", wMsg);
220 return buffer;
224 LPWSTR MCI_strdupAtoW( LPCSTR str )
226 LPWSTR ret;
227 INT len;
229 if (!str) return NULL;
230 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
231 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
232 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
233 return ret;
236 LPSTR MCI_strdupWtoA( LPCWSTR str )
238 LPSTR ret;
239 INT len;
241 if (!str) return NULL;
242 len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
243 ret = HeapAlloc( GetProcessHeap(), 0, len );
244 if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
245 return ret;
248 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
250 if (msg < DRV_RESERVED) return 0;
252 switch (msg)
254 case MCI_CLOSE:
255 case MCI_CONFIGURE:
256 case MCI_PLAY:
257 case MCI_SEEK:
258 case MCI_STOP:
259 case MCI_PAUSE:
260 case MCI_GETDEVCAPS:
261 case MCI_SPIN:
262 case MCI_SET:
263 case MCI_STEP:
264 case MCI_RECORD:
265 case MCI_BREAK:
266 case MCI_SOUND:
267 case MCI_STATUS:
268 case MCI_CUE:
269 case MCI_REALIZE:
270 case MCI_PUT:
271 case MCI_WHERE:
272 case MCI_FREEZE:
273 case MCI_UNFREEZE:
274 case MCI_CUT:
275 case MCI_COPY:
276 case MCI_PASTE:
277 case MCI_UPDATE:
278 case MCI_RESUME:
279 case MCI_DELETE:
280 case MCI_MONITOR:
281 case MCI_SETAUDIO:
282 case MCI_SIGNAL:
283 case MCI_SETVIDEO:
284 case MCI_LIST:
285 return 0;
287 case MCI_OPEN:
289 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA*)*dwParam2;
290 MCI_OPEN_PARMSW *mci_openW;
291 DWORD_PTR *ptr;
293 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW) + 2 * sizeof(DWORD));
294 if (!ptr) return -1;
296 *ptr++ = *dwParam2; /* save the previous pointer */
297 *dwParam2 = (DWORD_PTR)ptr;
298 mci_openW = (MCI_OPEN_PARMSW *)ptr;
300 if (dwParam1 & MCI_NOTIFY)
301 mci_openW->dwCallback = mci_openA->dwCallback;
303 if (dwParam1 & MCI_OPEN_TYPE)
305 if (dwParam1 & MCI_OPEN_TYPE_ID)
306 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
307 else
308 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
310 if (dwParam1 & MCI_OPEN_ELEMENT)
312 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
313 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
314 else
315 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
317 if (dwParam1 & MCI_OPEN_ALIAS)
318 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
319 /* FIXME: this is only needed for specific types of MCI devices, and
320 * may cause a segfault if the two DWORD:s don't exist at the end of
321 * mci_openA
323 memcpy(mci_openW + 1, mci_openA + 1, 2 * sizeof(DWORD));
325 return 1;
327 case MCI_WINDOW:
328 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
330 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
331 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
333 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
334 if (!mci_windowW) return -1;
336 *dwParam2 = (DWORD_PTR)mci_windowW;
338 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
340 if (dwParam1 & MCI_NOTIFY)
341 mci_windowW->dwCallback = mci_windowA->dwCallback;
342 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
343 mci_windowW->hWnd = mci_windowA->hWnd;
344 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
345 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
347 return 1;
349 return 0;
351 case MCI_SYSINFO:
353 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
354 MCI_SYSINFO_PARMSW *mci_sysinfoW;
355 DWORD_PTR *ptr;
357 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
358 if (!ptr) return -1;
360 *ptr++ = *dwParam2; /* save the previous pointer */
361 *dwParam2 = (DWORD_PTR)ptr;
362 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
364 if (dwParam1 & MCI_NOTIFY)
365 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
367 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
368 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize);
369 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
370 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
371 return 1;
373 case MCI_INFO:
375 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
376 MCI_INFO_PARMSW *mci_infoW;
377 DWORD_PTR *ptr;
379 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
380 if (!ptr) return -1;
382 *ptr++ = *dwParam2; /* save the previous pointer */
383 *dwParam2 = (DWORD_PTR)ptr;
384 mci_infoW = (MCI_INFO_PARMSW *)ptr;
386 if (dwParam1 & MCI_NOTIFY)
387 mci_infoW->dwCallback = mci_infoA->dwCallback;
389 mci_infoW->dwRetSize = mci_infoA->dwRetSize * sizeof(WCHAR); /* it's not the same as SYSINFO !!! */
390 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize);
391 return 1;
393 case MCI_SAVE:
395 MCI_SAVE_PARMSA *mci_saveA = (MCI_SAVE_PARMSA *)*dwParam2;
396 MCI_SAVE_PARMSW *mci_saveW;
398 mci_saveW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_saveW));
399 if (!mci_saveW) return -1;
401 *dwParam2 = (DWORD_PTR)mci_saveW;
402 if (dwParam1 & MCI_NOTIFY)
403 mci_saveW->dwCallback = mci_saveA->dwCallback;
404 mci_saveW->lpfilename = MCI_strdupAtoW(mci_saveA->lpfilename);
405 return 1;
407 case MCI_LOAD:
409 MCI_LOAD_PARMSA *mci_loadA = (MCI_LOAD_PARMSA *)*dwParam2;
410 MCI_LOAD_PARMSW *mci_loadW;
412 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
413 if (!mci_loadW) return -1;
415 *dwParam2 = (DWORD_PTR)mci_loadW;
416 if (dwParam1 & MCI_NOTIFY)
417 mci_loadW->dwCallback = mci_loadA->dwCallback;
418 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
419 return 1;
422 case MCI_ESCAPE:
424 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
425 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
427 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
428 if (!mci_vd_escapeW) return -1;
430 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
431 if (dwParam1 & MCI_NOTIFY)
432 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
433 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
434 return 1;
436 default:
437 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
438 return -1;
442 static DWORD MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
443 DWORD result)
445 switch (msg)
447 case MCI_OPEN:
449 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
450 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
451 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)(ptr + 1);
453 mci_openA->wDeviceID = mci_openW->wDeviceID;
455 if (dwParam1 & MCI_OPEN_TYPE)
457 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
458 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
460 if (dwParam1 & MCI_OPEN_ELEMENT)
462 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
463 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
465 if (dwParam1 & MCI_OPEN_ALIAS)
466 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
467 HeapFree(GetProcessHeap(), 0, ptr);
469 break;
470 case MCI_WINDOW:
471 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
473 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
475 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
476 HeapFree(GetProcessHeap(), 0, mci_windowW);
478 break;
480 case MCI_SYSINFO:
482 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
483 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
484 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)(ptr + 1);
486 if (!result)
488 mci_sysinfoA->dwNumber = mci_sysinfoW->dwNumber;
489 mci_sysinfoA->wDeviceType = mci_sysinfoW->wDeviceType;
490 if (dwParam1 & MCI_SYSINFO_QUANTITY)
491 *(DWORD*)mci_sysinfoA->lpstrReturn = *(DWORD*)mci_sysinfoW->lpstrReturn;
492 else
493 WideCharToMultiByte(CP_ACP, 0,
494 mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
495 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
496 NULL, NULL);
499 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
500 HeapFree(GetProcessHeap(), 0, ptr);
502 break;
503 case MCI_INFO:
505 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
506 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
507 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)(ptr + 1);
509 if (!result)
511 WideCharToMultiByte(CP_ACP, 0,
512 mci_infoW->lpstrReturn, mci_infoW->dwRetSize / sizeof(WCHAR),
513 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
514 NULL, NULL);
517 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
518 HeapFree(GetProcessHeap(), 0, ptr);
520 break;
521 case MCI_SAVE:
523 MCI_SAVE_PARMSW *mci_saveW = (MCI_SAVE_PARMSW *)dwParam2;
525 HeapFree(GetProcessHeap(), 0, (void*)mci_saveW->lpfilename);
526 HeapFree(GetProcessHeap(), 0, mci_saveW);
528 break;
529 case MCI_LOAD:
531 MCI_LOAD_PARMSW *mci_loadW = (MCI_LOAD_PARMSW *)dwParam2;
533 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
534 HeapFree(GetProcessHeap(), 0, mci_loadW);
536 break;
537 case MCI_ESCAPE:
539 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
541 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
542 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
544 break;
546 default:
547 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
548 break;
551 return result;
554 /**************************************************************************
555 * MCI_GetDevTypeFromFileName [internal]
557 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
559 LPCWSTR tmp;
560 HKEY hKey;
561 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
562 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
563 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
564 if ((tmp = strrchrW(fileName, '.'))) {
565 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
566 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
567 DWORD dwLen = len;
568 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
569 RegCloseKey( hKey );
570 if (lRet == ERROR_SUCCESS) return 0;
572 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
574 return MCIERR_EXTENSION_NOT_FOUND;
577 #define MAX_MCICMDTABLE 20
578 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
580 typedef struct tagWINE_MCICMDTABLE {
581 UINT uDevType;
582 HGLOBAL hMem;
583 const BYTE* lpTable;
584 UINT nVerbs; /* number of verbs in command table */
585 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
586 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
588 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
590 /**************************************************************************
591 * MCI_IsCommandTableValid [internal]
593 static BOOL MCI_IsCommandTableValid(UINT uTbl)
595 const BYTE* lmem;
596 LPCWSTR str;
597 DWORD flg;
598 WORD eid;
599 int idx = 0;
600 BOOL inCst = FALSE;
602 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
603 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
605 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
606 return FALSE;
608 lmem = S_MciCmdTable[uTbl].lpTable;
609 do {
610 str = (LPCWSTR)lmem;
611 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
612 flg = *(const DWORD*)lmem;
613 eid = *(const WORD*)(lmem + sizeof(DWORD));
614 lmem += sizeof(DWORD) + sizeof(WORD);
615 idx ++;
616 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
617 switch (eid) {
618 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
619 case MCI_STRING: if (inCst) return FALSE; break;
620 case MCI_INTEGER: if (!*str) return FALSE; break;
621 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
622 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
623 case MCI_FLAG: if (!*str) return FALSE; break;
624 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
625 case MCI_RECT: if (!*str || inCst) return FALSE; break;
626 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
627 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
628 default: return FALSE;
630 } while (eid != MCI_END_COMMAND_LIST);
631 return TRUE;
634 /**************************************************************************
635 * MCI_DumpCommandTable [internal]
637 static BOOL MCI_DumpCommandTable(UINT uTbl)
639 const BYTE* lmem;
640 LPCWSTR str;
641 DWORD flg;
642 WORD eid;
644 if (!MCI_IsCommandTableValid(uTbl)) {
645 ERR("Ooops: %d is not valid\n", uTbl);
646 return FALSE;
649 lmem = S_MciCmdTable[uTbl].lpTable;
650 do {
651 do {
652 str = (LPCWSTR)lmem;
653 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
654 flg = *(const DWORD*)lmem;
655 eid = *(const WORD*)(lmem + sizeof(DWORD));
656 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
657 lmem += sizeof(DWORD) + sizeof(WORD);
658 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
659 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
660 } while (eid != MCI_END_COMMAND_LIST);
661 return TRUE;
665 /**************************************************************************
666 * MCI_GetCommandTable [internal]
668 static UINT MCI_GetCommandTable(UINT uDevType)
670 UINT uTbl;
671 WCHAR buf[32];
672 LPCWSTR str = NULL;
674 /* first look up existing for existing devType */
675 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
676 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
677 return uTbl;
680 /* well try to load id */
681 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
682 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
683 str = buf;
685 } else if (uDevType == 0) {
686 static const WCHAR wszCore[] = {'C','O','R','E',0};
687 str = wszCore;
689 uTbl = MCI_NO_COMMAND_TABLE;
690 if (str) {
691 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
692 HANDLE hMem = 0;
694 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
695 if (hMem) {
696 uTbl = MCI_SetCommandTable(hMem, uDevType);
697 } else {
698 WARN("No command table found in resource %p[%s]\n",
699 hWinMM32Instance, debugstr_w(str));
702 TRACE("=> %d\n", uTbl);
703 return uTbl;
706 /**************************************************************************
707 * MCI_SetCommandTable [internal]
709 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
711 int uTbl;
712 static BOOL bInitDone = FALSE;
714 /* <HACK>
715 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
716 * can be called with 0 as a uDevType to retrieve it.
717 * </HACK>
719 if (!bInitDone) {
720 bInitDone = TRUE;
721 MCI_GetCommandTable(0);
723 TRACE("(%p, %u)\n", hMem, uDevType);
724 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
725 if (!S_MciCmdTable[uTbl].lpTable) {
726 const BYTE* lmem;
727 LPCWSTR str;
728 WORD eid;
729 WORD count;
731 S_MciCmdTable[uTbl].uDevType = uDevType;
732 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
733 S_MciCmdTable[uTbl].hMem = hMem;
735 if (TRACE_ON(mci)) {
736 MCI_DumpCommandTable(uTbl);
739 /* create the verbs table */
740 /* get # of entries */
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 count++;
750 } while (eid != MCI_END_COMMAND_LIST);
752 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
753 S_MciCmdTable[uTbl].nVerbs = count;
755 lmem = S_MciCmdTable[uTbl].lpTable;
756 count = 0;
757 do {
758 str = (LPCWSTR)lmem;
759 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
760 eid = *(const WORD*)(lmem + sizeof(DWORD));
761 lmem += sizeof(DWORD) + sizeof(WORD);
762 if (eid == MCI_COMMAND_HEAD)
763 S_MciCmdTable[uTbl].aVerbs[count++] = str;
764 } while (eid != MCI_END_COMMAND_LIST);
765 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
766 return uTbl;
770 return MCI_NO_COMMAND_TABLE;
773 /**************************************************************************
774 * MCI_UnLoadMciDriver [internal]
776 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
778 LPWINE_MCIDRIVER* tmp;
780 if (!wmd)
781 return TRUE;
783 CloseDriver(wmd->hDriver, 0, 0);
785 if (wmd->dwPrivate != 0)
786 WARN("Unloading mci driver with non nul dwPrivate field\n");
788 EnterCriticalSection(&WINMM_cs);
789 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
790 if (*tmp == wmd) {
791 *tmp = wmd->lpNext;
792 break;
795 LeaveCriticalSection(&WINMM_cs);
797 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
798 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
799 HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
801 HeapFree(GetProcessHeap(), 0, wmd);
802 return TRUE;
805 /**************************************************************************
806 * MCI_OpenMciDriver [internal]
808 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
810 WCHAR libName[128];
812 if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
813 return FALSE;
815 /* First load driver */
816 wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
817 return wmd->hDriver != NULL;
820 /**************************************************************************
821 * MCI_LoadMciDriver [internal]
823 static DWORD MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
825 LPWSTR strDevTyp = str_dup_upper(_strDevTyp);
826 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
827 MCI_OPEN_DRIVER_PARMSW modp;
828 DWORD dwRet = 0;
830 if (!wmd || !strDevTyp) {
831 dwRet = MCIERR_OUT_OF_MEMORY;
832 goto errCleanUp;
835 wmd->lpfnYieldProc = MCI_DefYieldProc;
836 wmd->dwYieldData = VK_CANCEL;
837 wmd->CreatorThread = GetCurrentThreadId();
839 EnterCriticalSection(&WINMM_cs);
840 /* wmd must be inserted in list before sending opening the driver, because it
841 * may want to lookup at wDevID
843 wmd->lpNext = MciDrivers;
844 MciDrivers = wmd;
846 for (modp.wDeviceID = MCI_MAGIC;
847 MCI_GetDriver(modp.wDeviceID) != 0;
848 modp.wDeviceID++);
850 wmd->wDeviceID = modp.wDeviceID;
852 LeaveCriticalSection(&WINMM_cs);
854 TRACE("wDevID=%04X\n", modp.wDeviceID);
856 modp.lpstrParams = NULL;
858 if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
859 /* silence warning if all is used... some bogus program use commands like
860 * 'open all'...
862 if (strcmpiW(strDevTyp, wszAll) == 0) {
863 dwRet = MCIERR_CANNOT_USE_ALL;
864 } else {
865 FIXME("Couldn't load driver for type %s.\n",
866 debugstr_w(strDevTyp));
867 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
869 goto errCleanUp;
872 /* FIXME: should also check that module's description is of the form
873 * MODULENAME:[MCI] comment
876 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
877 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
878 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
880 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
881 wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
883 wmd->lpstrDeviceType = strDevTyp;
884 wmd->wType = modp.wType;
886 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
887 modp.wDeviceID, modp.wType, modp.wDeviceID);
888 *lpwmd = wmd;
889 return 0;
890 errCleanUp:
891 MCI_UnLoadMciDriver(wmd);
892 HeapFree(GetProcessHeap(), 0, strDevTyp);
893 *lpwmd = 0;
894 return dwRet;
897 /**************************************************************************
898 * MCI_SendCommandFrom32 [internal]
900 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
902 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
903 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
905 if (wmd) {
906 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
908 return dwRet;
911 /**************************************************************************
912 * MCI_FinishOpen [internal]
914 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
915 DWORD dwParam)
917 if (dwParam & MCI_OPEN_ELEMENT)
919 wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
920 strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
922 if (dwParam & MCI_OPEN_ALIAS)
924 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
925 strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
927 lpParms->wDeviceID = wmd->wDeviceID;
929 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
930 (DWORD_PTR)lpParms);
933 /**************************************************************************
934 * MCI_FindCommand [internal]
936 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
938 UINT idx;
940 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
941 return NULL;
943 /* another improvement would be to have the aVerbs array sorted,
944 * so that we could use a dichotomic search on it, rather than this dumb
945 * array look up
947 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
948 if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
949 return S_MciCmdTable[uTbl].aVerbs[idx];
952 return NULL;
955 /**************************************************************************
956 * MCI_GetReturnType [internal]
958 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
960 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
961 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
962 return *(const DWORD*)(lpCmd + 1);
964 return 0L;
967 /**************************************************************************
968 * MCI_GetMessage [internal]
970 static WORD MCI_GetMessage(LPCWSTR lpCmd)
972 return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
975 /**************************************************************************
976 * MCI_GetDWord [internal]
978 static BOOL MCI_GetDWord(LPDWORD data, LPWSTR* ptr)
980 DWORD val;
981 LPWSTR ret;
983 val = strtoulW(*ptr, &ret, 0);
985 switch (*ret) {
986 case '\0': break;
987 case ' ': ret++; break;
988 default: return FALSE;
991 *data |= val;
992 *ptr = ret;
993 return TRUE;
996 /**************************************************************************
997 * MCI_GetString [internal]
999 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
1001 LPWSTR ptr = *args;
1003 /* see if we have a quoted string */
1004 if (*ptr == '"') {
1005 ptr = strchrW(*str = ptr + 1, '"');
1006 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1007 /* FIXME: shall we escape \" from string ?? */
1008 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1009 *ptr++ = '\0'; /* remove trailing " */
1010 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1011 } else {
1012 ptr = strchrW(ptr, ' ');
1014 if (ptr) {
1015 *ptr++ = '\0';
1016 } else {
1017 ptr = *args + strlenW(*args);
1019 *str = *args;
1022 *args = ptr;
1023 return 0;
1026 #define MCI_DATA_SIZE 16
1028 /**************************************************************************
1029 * MCI_ParseOptArgs [internal]
1031 static DWORD MCI_ParseOptArgs(LPDWORD data, int _offset, LPCWSTR lpCmd,
1032 LPWSTR args, LPDWORD dwFlags)
1034 int len, offset;
1035 const char* lmem;
1036 LPCWSTR str;
1037 DWORD dwRet, flg, cflg = 0;
1038 WORD eid;
1039 BOOL inCst, found;
1041 /* loop on arguments */
1042 while (*args) {
1043 lmem = (const char*)lpCmd;
1044 found = inCst = FALSE;
1045 offset = _offset;
1047 /* skip any leading white space(s) */
1048 while (*args == ' ') args++;
1049 TRACE("args=%s offset=%d\n", debugstr_w(args), offset);
1051 do { /* loop on options for command table for the requested verb */
1052 str = (LPCWSTR)lmem;
1053 lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1054 flg = *(const DWORD*)lmem;
1055 eid = *(const WORD*)(lmem + sizeof(DWORD));
1056 lmem += sizeof(DWORD) + sizeof(WORD);
1057 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1059 switch (eid) {
1060 case MCI_CONSTANT:
1061 inCst = TRUE; cflg = flg; break;
1062 case MCI_END_CONSTANT:
1063 /* there may be additional integral values after flag in constant */
1064 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1065 *dwFlags |= cflg;
1067 inCst = FALSE; cflg = 0;
1068 break;
1071 if (strncmpiW(args, str, len) == 0 &&
1072 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1073 /* store good values into data[] */
1074 args += len;
1075 while (*args == ' ') args++;
1076 found = TRUE;
1078 switch (eid) {
1079 case MCI_COMMAND_HEAD:
1080 case MCI_RETURN:
1081 case MCI_END_COMMAND:
1082 case MCI_END_COMMAND_LIST:
1083 case MCI_CONSTANT: /* done above */
1084 case MCI_END_CONSTANT: /* done above */
1085 break;
1086 case MCI_FLAG:
1087 *dwFlags |= flg;
1088 break;
1089 case MCI_INTEGER:
1090 if (inCst) {
1091 data[offset] |= flg;
1092 *dwFlags |= cflg;
1093 inCst = FALSE;
1094 } else {
1095 *dwFlags |= flg;
1096 if (!MCI_GetDWord(&(data[offset]), &args)) {
1097 return MCIERR_BAD_INTEGER;
1100 break;
1101 case MCI_RECT:
1102 /* store rect in data (offset..offset+3) */
1103 *dwFlags |= flg;
1104 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1105 !MCI_GetDWord(&(data[offset+1]), &args) ||
1106 !MCI_GetDWord(&(data[offset+2]), &args) ||
1107 !MCI_GetDWord(&(data[offset+3]), &args)) {
1108 ERR("Bad rect %s\n", debugstr_w(args));
1109 return MCIERR_BAD_INTEGER;
1111 break;
1112 case MCI_STRING:
1113 *dwFlags |= flg;
1114 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1115 return dwRet;
1116 break;
1117 default: ERR("oops\n");
1119 /* exit inside while loop, except if just entered in constant area definition */
1120 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1121 } else {
1122 /* have offset incremented if needed */
1123 switch (eid) {
1124 case MCI_COMMAND_HEAD:
1125 case MCI_RETURN:
1126 case MCI_END_COMMAND:
1127 case MCI_END_COMMAND_LIST:
1128 case MCI_CONSTANT:
1129 case MCI_FLAG: break;
1130 case MCI_INTEGER: if (!inCst) offset++; break;
1131 case MCI_END_CONSTANT:
1132 case MCI_STRING: offset++; break;
1133 case MCI_RECT: offset += 4; break;
1134 default: ERR("oops\n");
1137 } while (eid != MCI_END_COMMAND);
1138 if (!found) {
1139 WARN("Optarg %s not found\n", debugstr_w(args));
1140 return MCIERR_UNRECOGNIZED_COMMAND;
1142 if (offset == MCI_DATA_SIZE) {
1143 ERR("Internal data[] buffer overflow\n");
1144 return MCIERR_PARSER_INTERNAL;
1147 return 0;
1150 /**************************************************************************
1151 * MCI_HandleReturnValues [internal]
1153 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1154 LPDWORD data, LPWSTR lpstrRet, UINT uRetLen)
1156 static const WCHAR wszLd [] = {'%','l','d',0};
1157 static const WCHAR wszLd4 [] = {'%','l','d',' ','%','l','d',' ','%','l','d',' ','%','l','d',0};
1158 static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1159 static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1161 if (lpstrRet) {
1162 switch (retType) {
1163 case 0: /* nothing to return */
1164 break;
1165 case MCI_INTEGER:
1166 switch (dwRet & 0xFFFF0000ul) {
1167 case 0:
1168 case MCI_INTEGER_RETURNED:
1169 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1170 break;
1171 case MCI_RESOURCE_RETURNED:
1172 /* return string which ID is HIWORD(data[1]),
1173 * string is loaded from mmsystem.dll */
1174 LoadStringW(hWinMM32Instance, HIWORD(data[1]), lpstrRet, uRetLen);
1175 break;
1176 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1177 /* return string which ID is HIWORD(data[1]),
1178 * string is loaded from driver */
1179 /* FIXME: this is wrong for a 16 bit handle */
1180 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1181 HIWORD(data[1]), lpstrRet, uRetLen);
1182 break;
1183 case MCI_COLONIZED3_RETURN:
1184 snprintfW(lpstrRet, uRetLen, wszCol3,
1185 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1186 LOBYTE(HIWORD(data[1])));
1187 break;
1188 case MCI_COLONIZED4_RETURN:
1189 snprintfW(lpstrRet, uRetLen, wszCol4,
1190 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1191 LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
1192 break;
1193 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1195 break;
1196 case MCI_STRING:
1197 switch (dwRet & 0xFFFF0000ul) {
1198 case 0:
1199 /* nothing to do data[1] == lpstrRet */
1200 break;
1201 case MCI_INTEGER_RETURNED:
1202 data[1] = *(LPDWORD)lpstrRet;
1203 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1204 break;
1205 default:
1206 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1207 break;
1209 break;
1210 case MCI_RECT:
1211 if (dwRet & 0xFFFF0000ul)
1212 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1213 snprintfW(lpstrRet, uRetLen, wszLd4,
1214 data[1], data[2], data[3], data[4]);
1215 break;
1216 default: ERR("oops\n");
1219 return LOWORD(dwRet);
1222 /**************************************************************************
1223 * mciSendStringW [WINMM.@]
1225 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1226 UINT uRetLen, HWND hwndCallback)
1228 LPWSTR verb, dev, args;
1229 LPWINE_MCIDRIVER wmd = 0;
1230 DWORD dwFlags = 0, dwRet = 0;
1231 int offset = 0;
1232 DWORD data[MCI_DATA_SIZE];
1233 DWORD retType;
1234 LPCWSTR lpCmd = 0;
1235 LPWSTR devAlias = NULL;
1236 static const WCHAR wszNew[] = {'n','e','w',0};
1237 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1238 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1240 TRACE("(%s, %p, %d, %p)\n",
1241 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1243 /* format is <command> <device> <optargs> */
1244 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1245 return MCIERR_OUT_OF_MEMORY;
1246 strcpyW( verb, lpstrCommand );
1247 CharLowerW(verb);
1249 memset(data, 0, sizeof(data));
1251 if (!(args = strchrW(verb, ' '))) {
1252 dwRet = MCIERR_MISSING_DEVICE_NAME;
1253 goto errCleanUp;
1255 *args++ = '\0';
1256 if ((dwRet = MCI_GetString(&dev, &args))) {
1257 goto errCleanUp;
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)devType;
1286 devType = str_dup_upper(devType);
1287 dwFlags |= MCI_OPEN_ELEMENT;
1288 data[3] = (DWORD)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)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)dev;
1314 if ((devAlias = strstrW(args, wszSAliasS))) {
1315 WCHAR* tmp2;
1316 devAlias += 7;
1317 if (!(tmp = strchrW(devAlias,' '))) tmp = devAlias + strlenW(devAlias);
1318 if (tmp) *tmp = '\0';
1319 tmp2 = HeapAlloc(GetProcessHeap(), 0, (tmp - devAlias + 1) * sizeof(WCHAR) );
1320 memcpy( tmp2, devAlias, (tmp - devAlias) * sizeof(WCHAR) );
1321 tmp2[tmp - devAlias] = 0;
1322 data[4] = (DWORD)tmp2;
1323 /* should be done in regular options parsing */
1324 /* dwFlags |= MCI_OPEN_ALIAS; */
1325 } else if (dev == 0) {
1326 /* "open new" requires alias */
1327 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1328 goto errCleanUp;
1331 dwRet = MCI_LoadMciDriver(devType, &wmd);
1332 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1333 dwRet = MCIERR_INVALID_DEVICE_NAME;
1334 HeapFree(GetProcessHeap(), 0, devType);
1335 if (dwRet) {
1336 MCI_UnLoadMciDriver(wmd);
1337 goto errCleanUp;
1339 } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))) {
1340 /* auto open */
1341 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1342 WCHAR buf[128];
1343 sprintfW(buf, wszOpenWait, dev);
1345 if ((dwRet = mciSendStringW(buf, NULL, 0, 0)) != 0)
1346 goto errCleanUp;
1348 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1349 if (!wmd) {
1350 /* FIXME: memory leak, MCI driver is not closed */
1351 dwRet = MCIERR_INVALID_DEVICE_ID;
1352 goto errCleanUp;
1356 /* get the verb in the different command tables */
1357 if (wmd) {
1358 /* try the device specific command table */
1359 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1360 if (!lpCmd) {
1361 /* try the type specific command table */
1362 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1363 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1364 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1365 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1368 /* try core command table */
1369 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1371 if (!lpCmd) {
1372 TRACE("Command %s not found!\n", debugstr_w(verb));
1373 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1374 goto errCleanUp;
1377 /* set return information */
1378 switch (retType = MCI_GetReturnType(lpCmd)) {
1379 case 0: offset = 1; break;
1380 case MCI_INTEGER: offset = 2; break;
1381 case MCI_STRING: data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
1382 case MCI_RECT: offset = 5; break;
1383 default: ERR("oops\n");
1386 TRACE("verb=%s on dev=%s; offset=%d\n",
1387 debugstr_w(verb), debugstr_w(dev), offset);
1389 if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1390 goto errCleanUp;
1392 /* set up call back */
1393 if (dwFlags & MCI_NOTIFY) {
1394 data[0] = (DWORD)hwndCallback;
1397 /* FIXME: the command should get it's own notification window set up and
1398 * ask for device closing while processing the notification mechanism
1400 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1402 TRACE("[%d, %s, %08x, %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s]\n",
1403 wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1404 data[0], debugstr_w((WCHAR *)data[0]), data[1], debugstr_w((WCHAR *)data[1]),
1405 data[2], debugstr_w((WCHAR *)data[2]), data[3], debugstr_w((WCHAR *)data[3]),
1406 data[4], debugstr_w((WCHAR *)data[4]), data[5], debugstr_w((WCHAR *)data[5]));
1408 if (strcmpW(verb, wszOpen) == 0) {
1409 if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSW)data, dwFlags)))
1410 MCI_UnLoadMciDriver(wmd);
1411 /* FIXME: notification is not properly shared across two opens */
1412 } else {
1413 dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD_PTR)data);
1415 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1416 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1417 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1419 errCleanUp:
1420 HeapFree(GetProcessHeap(), 0, verb);
1421 return dwRet;
1424 /**************************************************************************
1425 * mciSendStringA [WINMM.@]
1427 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1428 UINT uRetLen, HWND hwndCallback)
1430 LPWSTR lpwstrCommand;
1431 LPWSTR lpwstrRet = NULL;
1432 UINT ret;
1433 INT len;
1435 /* FIXME: is there something to do with lpstrReturnString ? */
1436 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1437 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1438 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1439 if (lpstrRet)
1441 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1442 if (!lpwstrRet) {
1443 WARN("no memory\n");
1444 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1445 return MCIERR_OUT_OF_MEMORY;
1448 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1449 if (!ret && lpwstrRet)
1450 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1451 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1452 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1453 return ret;
1456 /**************************************************************************
1457 * mciExecute [WINMM.@]
1459 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1461 char strRet[256];
1462 DWORD ret;
1464 TRACE("(%s)!\n", lpstrCommand);
1466 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1467 if (ret != 0) {
1468 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1469 sprintf(strRet, "Unknown MCI error (%d)", ret);
1471 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1473 /* FIXME: what shall I return ? */
1474 return TRUE;
1477 /**************************************************************************
1478 * mciLoadCommandResource [WINMM.@]
1480 * Strangely, this function only exists as a UNICODE one.
1482 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1484 UINT ret = MCI_NO_COMMAND_TABLE;
1485 HRSRC hRsrc = 0;
1486 HGLOBAL hMem;
1488 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1490 /* if a file named "resname.mci" exits, then load resource "resname" from it
1491 * otherwise directly from driver
1492 * We don't support it (who uses this feature ?), but we check anyway
1494 if (!type) {
1495 #if 0
1496 /* FIXME: we should put this back into order, but I never found a program
1497 * actually using this feature, so we may not need it
1499 char buf[128];
1500 OFSTRUCT ofs;
1502 strcat(strcpy(buf, resname), ".mci");
1503 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1504 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1506 #endif
1508 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1509 (hMem = LoadResource(hInst, hRsrc))) {
1510 ret = MCI_SetCommandTable(hMem, type);
1511 FreeResource(hMem);
1513 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1515 TRACE("=> %04x\n", ret);
1516 return ret;
1519 /**************************************************************************
1520 * mciFreeCommandResource [WINMM.@]
1522 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1524 TRACE("(%08x)!\n", uTable);
1526 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1527 return FALSE;
1529 FreeResource(S_MciCmdTable[uTable].hMem);
1530 S_MciCmdTable[uTable].hMem = NULL;
1531 S_MciCmdTable[uTable].lpTable = NULL;
1532 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1533 S_MciCmdTable[uTable].aVerbs = 0;
1534 S_MciCmdTable[uTable].nVerbs = 0;
1535 return TRUE;
1538 /**************************************************************************
1539 * MCI_Open [internal]
1541 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1543 WCHAR strDevTyp[128];
1544 DWORD dwRet;
1545 LPWINE_MCIDRIVER wmd = NULL;
1547 TRACE("(%08X, %p)\n", dwParam, lpParms);
1548 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1550 /* only two low bytes are generic, the other ones are dev type specific */
1551 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1552 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1553 MCI_NOTIFY|MCI_WAIT)
1554 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1555 FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1557 #undef WINE_MCIDRIVER_SUPP
1559 strDevTyp[0] = 0;
1561 if (dwParam & MCI_OPEN_TYPE) {
1562 if (dwParam & MCI_OPEN_TYPE_ID) {
1563 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1565 if (uDevType < MCI_DEVTYPE_FIRST ||
1566 uDevType > MCI_DEVTYPE_LAST ||
1567 !LoadStringW(hWinMM32Instance, uDevType,
1568 strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1569 dwRet = MCIERR_BAD_INTEGER;
1570 goto errCleanUp;
1572 } else {
1573 LPWSTR ptr;
1574 if (lpParms->lpstrDeviceType == NULL) {
1575 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1576 goto errCleanUp;
1578 strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1579 ptr = strchrW(strDevTyp, '!');
1580 if (ptr) {
1581 /* this behavior is not documented in windows. However, since, in
1582 * some occasions, MCI_OPEN handling is translated by WinMM into
1583 * a call to mciSendString("open <type>"); this code shall be correct
1585 if (dwParam & MCI_OPEN_ELEMENT) {
1586 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1587 debugstr_w(lpParms->lpstrElementName),
1588 debugstr_w(strDevTyp));
1589 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1590 goto errCleanUp;
1592 dwParam |= MCI_OPEN_ELEMENT;
1593 *ptr++ = 0;
1594 /* FIXME: not a good idea to write in user supplied buffer */
1595 lpParms->lpstrElementName = ptr;
1599 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1602 if (dwParam & MCI_OPEN_ELEMENT) {
1603 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1605 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1606 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1607 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1608 goto errCleanUp;
1611 if (!lpParms->lpstrElementName) {
1612 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1613 goto errCleanUp;
1616 /* type, if given as a parameter, supersedes file extension */
1617 if (!strDevTyp[0] &&
1618 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1619 strDevTyp, sizeof(strDevTyp))) {
1620 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1621 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1622 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1623 goto errCleanUp;
1625 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1626 strcpyW(strDevTyp, wszCdAudio);
1630 if (strDevTyp[0] == 0) {
1631 FIXME("Couldn't load driver\n");
1632 dwRet = MCIERR_INVALID_DEVICE_NAME;
1633 goto errCleanUp;
1636 if (dwParam & MCI_OPEN_ALIAS) {
1637 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1638 if (!lpParms->lpstrAlias) {
1639 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1640 goto errCleanUp;
1644 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1645 goto errCleanUp;
1648 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1649 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1650 /* FIXME: is dwRet the correct ret code ? */
1651 goto errCleanUp;
1654 /* only handled devices fall through */
1655 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1657 if (dwParam & MCI_NOTIFY)
1658 mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1660 return 0;
1661 errCleanUp:
1662 if (wmd) MCI_UnLoadMciDriver(wmd);
1664 if (dwParam & MCI_NOTIFY)
1665 mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1666 return dwRet;
1669 /**************************************************************************
1670 * MCI_Close [internal]
1672 static DWORD MCI_Close(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1674 DWORD dwRet;
1675 LPWINE_MCIDRIVER wmd;
1677 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1679 /* Every device must handle MCI_NOTIFY on its own. */
1680 if (wDevID == MCI_ALL_DEVICE_ID) {
1681 while (MciDrivers) {
1682 /* Retrieve the device ID under lock, but send the message without,
1683 * the driver might be calling some winmm functions from another
1684 * thread before being fully stopped.
1686 EnterCriticalSection(&WINMM_cs);
1687 if (!MciDrivers)
1689 LeaveCriticalSection(&WINMM_cs);
1690 break;
1692 wDevID = MciDrivers->wDeviceID;
1693 LeaveCriticalSection(&WINMM_cs);
1694 MCI_Close(wDevID, dwParam, lpParms);
1696 return 0;
1699 if (!(wmd = MCI_GetDriver(wDevID))) {
1700 return MCIERR_INVALID_DEVICE_ID;
1703 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1705 MCI_UnLoadMciDriver(wmd);
1707 return dwRet;
1710 /**************************************************************************
1711 * MCI_WriteString [internal]
1713 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1715 DWORD ret = 0;
1717 if (lpSrcStr) {
1718 dstSize /= sizeof(WCHAR);
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);
1747 switch (dwFlags & ~MCI_SYSINFO_OPEN) {
1748 case MCI_SYSINFO_QUANTITY:
1749 if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
1750 if (dwFlags & MCI_SYSINFO_OPEN) {
1751 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1752 EnterCriticalSection(&WINMM_cs);
1753 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1754 cnt++;
1756 LeaveCriticalSection(&WINMM_cs);
1757 } else {
1758 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1759 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1760 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1761 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1762 RegCloseKey( hKey );
1764 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1765 for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1767 } else {
1768 if (dwFlags & MCI_SYSINFO_OPEN) {
1769 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n", lpParms->wDeviceType);
1770 EnterCriticalSection(&WINMM_cs);
1771 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1772 if (wmd->wType == lpParms->wDeviceType) cnt++;
1774 LeaveCriticalSection(&WINMM_cs);
1775 } else {
1776 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n", lpParms->wDeviceType);
1777 FIXME("Don't know how to get # of MCI devices of a given type\n");
1778 cnt = 1;
1781 *(DWORD*)lpParms->lpstrReturn = cnt;
1782 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1783 ret = MCI_INTEGER_RETURNED;
1784 break;
1785 case MCI_SYSINFO_INSTALLNAME:
1786 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1787 if ((wmd = MCI_GetDriver(uDevID))) {
1788 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1789 wmd->lpstrDeviceType);
1790 } else {
1791 *lpParms->lpstrReturn = 0;
1792 ret = MCIERR_INVALID_DEVICE_ID;
1794 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1795 break;
1796 case MCI_SYSINFO_NAME:
1797 TRACE("MCI_SYSINFO_NAME\n");
1798 if (dwFlags & MCI_SYSINFO_OPEN) {
1799 FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1800 ret = MCIERR_UNRECOGNIZED_COMMAND;
1801 } else {
1802 s = NULL;
1803 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1804 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1805 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1806 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1807 lpParms->dwNumber <= cnt) {
1808 DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
1809 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1810 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1811 s = buf;
1813 RegCloseKey( hKey );
1815 if (!s) {
1816 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1817 for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1818 TRACE("%d: %s\n", cnt, debugstr_w(p));
1819 if (cnt == lpParms->dwNumber - 1) {
1820 s = p;
1821 break;
1826 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR), s) : MCIERR_OUTOFRANGE;
1828 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1829 break;
1830 default:
1831 TRACE("Unsupported flag value=%08x\n", dwFlags);
1832 ret = MCIERR_UNRECOGNIZED_COMMAND;
1834 return ret;
1837 /**************************************************************************
1838 * MCI_Break [internal]
1840 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1842 DWORD dwRet = 0;
1844 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1846 if (dwFlags & MCI_NOTIFY)
1847 mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1848 (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1850 return dwRet;
1853 /**************************************************************************
1854 * MCI_Sound [internal]
1856 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
1858 DWORD dwRet = 0;
1860 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1862 if (dwFlags & MCI_SOUND_NAME)
1863 dwRet = sndPlaySoundW(lpParms->lpstrSoundName, SND_SYNC) ? MMSYSERR_NOERROR : MMSYSERR_ERROR;
1864 else
1865 dwRet = MMSYSERR_ERROR; /* what should be done ??? */
1866 if (dwFlags & MCI_NOTIFY)
1867 mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1868 (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1870 return dwRet;
1873 /**************************************************************************
1874 * MCI_SendCommand [internal]
1876 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1878 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1880 switch (wMsg) {
1881 case MCI_OPEN:
1882 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1883 break;
1884 case MCI_CLOSE:
1885 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1886 break;
1887 case MCI_SYSINFO:
1888 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
1889 break;
1890 case MCI_BREAK:
1891 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1892 break;
1893 case MCI_SOUND:
1894 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
1895 break;
1896 default:
1897 if (wDevID == MCI_ALL_DEVICE_ID) {
1898 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1899 dwRet = MCIERR_CANNOT_USE_ALL;
1900 } else {
1901 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
1903 break;
1905 return dwRet;
1908 /**************************************************************************
1909 * MCI_CleanUp [internal]
1911 * Some MCI commands need to be cleaned-up (when not called from
1912 * mciSendString), because MCI drivers return extra information for string
1913 * transformation. This function gets rid of them.
1915 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
1917 if (LOWORD(dwRet))
1918 return LOWORD(dwRet);
1920 switch (wMsg) {
1921 case MCI_GETDEVCAPS:
1922 switch (dwRet & 0xFFFF0000ul) {
1923 case 0:
1924 case MCI_COLONIZED3_RETURN:
1925 case MCI_COLONIZED4_RETURN:
1926 case MCI_INTEGER_RETURNED:
1927 /* nothing to do */
1928 break;
1929 case MCI_RESOURCE_RETURNED:
1930 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1932 LPMCI_GETDEVCAPS_PARMS lmgp;
1934 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
1935 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
1936 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1938 break;
1939 default:
1940 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1941 HIWORD(dwRet), MCI_MessageToString(wMsg));
1943 break;
1944 case MCI_STATUS:
1945 switch (dwRet & 0xFFFF0000ul) {
1946 case 0:
1947 case MCI_COLONIZED3_RETURN:
1948 case MCI_COLONIZED4_RETURN:
1949 case MCI_INTEGER_RETURNED:
1950 /* nothing to do */
1951 break;
1952 case MCI_RESOURCE_RETURNED:
1953 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1955 LPMCI_STATUS_PARMS lsp;
1957 lsp = (LPMCI_STATUS_PARMS)dwParam2;
1958 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
1959 lsp->dwReturn = LOWORD(lsp->dwReturn);
1961 break;
1962 default:
1963 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1964 HIWORD(dwRet), MCI_MessageToString(wMsg));
1966 break;
1967 case MCI_SYSINFO:
1968 switch (dwRet & 0xFFFF0000ul) {
1969 case 0:
1970 case MCI_INTEGER_RETURNED:
1971 /* nothing to do */
1972 break;
1973 default:
1974 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
1976 break;
1977 default:
1978 if (HIWORD(dwRet)) {
1979 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
1980 dwRet, MCI_MessageToString(wMsg));
1982 break;
1984 return LOWORD(dwRet);
1987 /**************************************************************************
1988 * mciGetErrorStringW [WINMM.@]
1990 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
1992 BOOL ret = FALSE;
1994 if (lpstrBuffer != NULL && uLength > 0 &&
1995 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
1997 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
1998 ret = TRUE;
2001 return ret;
2004 /**************************************************************************
2005 * mciGetErrorStringA [WINMM.@]
2007 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2009 BOOL ret = FALSE;
2011 if (lpstrBuffer != NULL && uLength > 0 &&
2012 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2014 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2015 ret = TRUE;
2018 return ret;
2021 /**************************************************************************
2022 * mciDriverNotify [WINMM.@]
2024 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2026 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2028 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2031 /**************************************************************************
2032 * mciGetDriverData [WINMM.@]
2034 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2036 LPWINE_MCIDRIVER wmd;
2038 TRACE("(%04x)\n", uDeviceID);
2040 wmd = MCI_GetDriver(uDeviceID);
2042 if (!wmd) {
2043 WARN("Bad uDeviceID\n");
2044 return 0L;
2047 return wmd->dwPrivate;
2050 /**************************************************************************
2051 * mciSetDriverData [WINMM.@]
2053 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2055 LPWINE_MCIDRIVER wmd;
2057 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2059 wmd = MCI_GetDriver(uDeviceID);
2061 if (!wmd) {
2062 WARN("Bad uDeviceID\n");
2063 return FALSE;
2066 wmd->dwPrivate = data;
2067 return TRUE;
2070 /**************************************************************************
2071 * mciSendCommandW [WINMM.@]
2074 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2076 DWORD dwRet;
2078 TRACE("(%08x, %s, %08lx, %08lx)\n",
2079 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2081 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2082 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2083 TRACE("=> %08x\n", dwRet);
2084 return dwRet;
2087 /**************************************************************************
2088 * mciSendCommandA [WINMM.@]
2090 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2092 DWORD ret;
2093 int mapped;
2095 TRACE("(%08x, %s, %08lx, %08lx)\n",
2096 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2098 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2099 if (mapped == -1)
2101 FIXME("message %04x mapping failed\n", wMsg);
2102 return MMSYSERR_NOMEM;
2104 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2105 if (mapped)
2106 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2107 return ret;
2110 /**************************************************************************
2111 * mciGetDeviceIDA [WINMM.@]
2113 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2115 LPWSTR w = MCI_strdupAtoW(lpstrName);
2116 UINT ret = MCIERR_OUT_OF_MEMORY;
2118 if (w)
2120 ret = mciGetDeviceIDW(w);
2121 HeapFree(GetProcessHeap(), 0, w);
2123 return ret;
2126 /**************************************************************************
2127 * mciGetDeviceIDW [WINMM.@]
2129 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2131 return MCI_GetDriverFromString(lpwstrName);
2134 /******************************************************************
2135 * MyUserYield
2137 * Internal wrapper to call USER.UserYield16 (in fact through a Wine only export from USER32).
2139 static void MyUserYield(void)
2141 HMODULE mod = GetModuleHandleA( "user32.dll" );
2142 if (mod)
2144 FARPROC proc = GetProcAddress( mod, "UserYield16" );
2145 if (proc) proc();
2149 /**************************************************************************
2150 * MCI_DefYieldProc [internal]
2152 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2154 INT16 ret;
2156 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2158 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2159 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2160 MyUserYield();
2161 ret = 0;
2162 } else {
2163 MSG msg;
2165 msg.hwnd = HWND_32(HIWORD(data));
2166 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2167 ret = -1;
2169 return ret;
2172 /**************************************************************************
2173 * mciSetYieldProc [WINMM.@]
2175 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2177 LPWINE_MCIDRIVER wmd;
2179 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2181 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2182 WARN("Bad uDeviceID\n");
2183 return FALSE;
2186 wmd->lpfnYieldProc = fpYieldProc;
2187 wmd->dwYieldData = dwYieldData;
2189 return TRUE;
2192 /**************************************************************************
2193 * mciGetDeviceIDFromElementIDA [WINMM.@]
2195 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2197 LPWSTR w = MCI_strdupAtoW(lpstrType);
2198 UINT ret = 0;
2200 if (w)
2202 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2203 HeapFree(GetProcessHeap(), 0, w);
2205 return ret;
2208 /**************************************************************************
2209 * mciGetDeviceIDFromElementIDW [WINMM.@]
2211 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2213 /* FIXME: that's rather strange, there is no
2214 * mciGetDeviceIDFromElementID32A in winmm.spec
2216 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2217 return 0;
2220 /**************************************************************************
2221 * mciGetYieldProc [WINMM.@]
2223 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2225 LPWINE_MCIDRIVER wmd;
2227 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2229 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2230 WARN("Bad uDeviceID\n");
2231 return NULL;
2233 if (!wmd->lpfnYieldProc) {
2234 WARN("No proc set\n");
2235 return NULL;
2237 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2238 return wmd->lpfnYieldProc;
2241 /**************************************************************************
2242 * mciGetCreatorTask [WINMM.@]
2244 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2246 LPWINE_MCIDRIVER wmd;
2247 HTASK ret = 0;
2249 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)wmd->CreatorThread;
2251 TRACE("(%u) => %p\n", uDeviceID, ret);
2252 return ret;
2255 /**************************************************************************
2256 * mciDriverYield [WINMM.@]
2258 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2260 LPWINE_MCIDRIVER wmd;
2261 UINT ret = 0;
2263 TRACE("(%04x)\n", uDeviceID);
2265 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2266 MyUserYield();
2267 } else {
2268 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2271 return ret;