server: Fix connect failures on newer kernels.
[wine.git] / dlls / winmm / mci.c
blob30db36355267ecd1957b33278aee834666332eee
1 /*
2 * MCI internal functions
4 * Copyright 1998/1999 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 /* TODO:
22 * - implement WINMM (32bit) multitasking and use it in all MCI drivers
23 * instead of the home grown one
24 * - 16bit mmTaskXXX functions are currently broken because the 16
25 * loader does not support binary command lines => provide Wine's
26 * own mmtask.tsk not using binary command line.
27 * - correctly handle the MCI_ALL_DEVICE_ID in functions.
28 * - finish mapping 16 <=> 32 of MCI structures and commands
29 * - implement auto-open feature (ie, when a string command is issued
30 * for a not yet opened device, MCI automatically opens it)
31 * - use a default registry setting to replace the [mci] section in
32 * configuration file (layout of info in registry should be compatible
33 * with all Windows' version - which use different layouts of course)
34 * - implement automatic open
35 * + only works on string interface, on regular devices (don't work on all
36 * nor custom devices)
37 * - command table handling isn't thread safe
40 #include "config.h"
41 #include "wine/port.h"
43 #include <stdlib.h>
44 #include <stdarg.h>
45 #include <stdio.h>
46 #include <string.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "mmsystem.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "winreg.h"
55 #include "wownt32.h"
57 #include "digitalv.h"
58 #include "winemm.h"
60 #include "wine/debug.h"
61 #include "wine/unicode.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(mci);
65 /* First MCI valid device ID (0 means error) */
66 #define MCI_MAGIC 0x0001
68 /* MCI settings */
69 static const WCHAR wszHklmMci [] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','M','C','I',0};
70 static const WCHAR wszNull [] = {0};
71 static const WCHAR wszAll [] = {'A','L','L',0};
72 static const WCHAR wszMci [] = {'M','C','I',0};
73 static const WCHAR wszOpen [] = {'o','p','e','n',0};
74 static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
76 static WINE_MCIDRIVER *MciDrivers;
78 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
79 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
81 /* dup a string and uppercase it */
82 static inline LPWSTR str_dup_upper( LPCWSTR str )
84 INT len = (strlenW(str) + 1) * sizeof(WCHAR);
85 LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
86 if (p)
88 memcpy( p, str, len );
89 CharUpperW( p );
91 return p;
94 /**************************************************************************
95 * MCI_GetDriver [internal]
97 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
99 LPWINE_MCIDRIVER wmd = 0;
101 EnterCriticalSection(&WINMM_cs);
102 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
103 if (wmd->wDeviceID == wDevID)
104 break;
106 LeaveCriticalSection(&WINMM_cs);
107 return wmd;
110 /**************************************************************************
111 * MCI_GetDriverFromString [internal]
113 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
115 LPWINE_MCIDRIVER wmd;
116 UINT ret = 0;
118 if (!lpstrName)
119 return 0;
121 if (!strcmpiW(lpstrName, wszAll))
122 return MCI_ALL_DEVICE_ID;
124 EnterCriticalSection(&WINMM_cs);
125 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
126 if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
127 ret = wmd->wDeviceID;
128 break;
131 LeaveCriticalSection(&WINMM_cs);
133 return ret;
136 /**************************************************************************
137 * MCI_MessageToString [internal]
139 static const char* MCI_MessageToString(UINT wMsg)
141 #define CASE(s) case (s): return #s
143 switch (wMsg) {
144 CASE(DRV_LOAD);
145 CASE(DRV_ENABLE);
146 CASE(DRV_OPEN);
147 CASE(DRV_CLOSE);
148 CASE(DRV_DISABLE);
149 CASE(DRV_FREE);
150 CASE(DRV_CONFIGURE);
151 CASE(DRV_QUERYCONFIGURE);
152 CASE(DRV_INSTALL);
153 CASE(DRV_REMOVE);
154 CASE(DRV_EXITSESSION);
155 CASE(DRV_EXITAPPLICATION);
156 CASE(DRV_POWER);
157 CASE(MCI_BREAK);
158 CASE(MCI_CLOSE);
159 CASE(MCI_CLOSE_DRIVER);
160 CASE(MCI_COPY);
161 CASE(MCI_CUE);
162 CASE(MCI_CUT);
163 CASE(MCI_DELETE);
164 CASE(MCI_ESCAPE);
165 CASE(MCI_FREEZE);
166 CASE(MCI_PAUSE);
167 CASE(MCI_PLAY);
168 CASE(MCI_GETDEVCAPS);
169 CASE(MCI_INFO);
170 CASE(MCI_LOAD);
171 CASE(MCI_OPEN);
172 CASE(MCI_OPEN_DRIVER);
173 CASE(MCI_PASTE);
174 CASE(MCI_PUT);
175 CASE(MCI_REALIZE);
176 CASE(MCI_RECORD);
177 CASE(MCI_RESUME);
178 CASE(MCI_SAVE);
179 CASE(MCI_SEEK);
180 CASE(MCI_SET);
181 CASE(MCI_SOUND);
182 CASE(MCI_SPIN);
183 CASE(MCI_STATUS);
184 CASE(MCI_STEP);
185 CASE(MCI_STOP);
186 CASE(MCI_SYSINFO);
187 CASE(MCI_UNFREEZE);
188 CASE(MCI_UPDATE);
189 CASE(MCI_WHERE);
190 CASE(MCI_WINDOW);
191 /* constants for digital video */
192 CASE(MCI_CAPTURE);
193 CASE(MCI_MONITOR);
194 CASE(MCI_RESERVE);
195 CASE(MCI_SETAUDIO);
196 CASE(MCI_SIGNAL);
197 CASE(MCI_SETVIDEO);
198 CASE(MCI_QUALITY);
199 CASE(MCI_LIST);
200 CASE(MCI_UNDO);
201 CASE(MCI_CONFIGURE);
202 CASE(MCI_RESTORE);
203 #undef CASE
204 default:
205 return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
209 static LPWSTR MCI_strdupAtoW( LPCSTR str )
211 LPWSTR ret;
212 INT len;
214 if (!str) return NULL;
215 len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
216 ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
217 if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
218 return ret;
221 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
223 if (msg < DRV_RESERVED) return 0;
225 switch (msg)
227 case MCI_CLOSE:
228 case MCI_CONFIGURE:
229 case MCI_PLAY:
230 case MCI_SEEK:
231 case MCI_STOP:
232 case MCI_PAUSE:
233 case MCI_GETDEVCAPS:
234 case MCI_SPIN:
235 case MCI_SET:
236 case MCI_STEP:
237 case MCI_RECORD:
238 case MCI_BREAK:
239 case MCI_STATUS:
240 case MCI_CUE:
241 case MCI_REALIZE:
242 case MCI_PUT:
243 case MCI_WHERE:
244 case MCI_FREEZE:
245 case MCI_UNFREEZE:
246 case MCI_CUT:
247 case MCI_COPY:
248 case MCI_PASTE:
249 case MCI_UPDATE:
250 case MCI_RESUME:
251 case MCI_DELETE:
252 case MCI_MONITOR:
253 case MCI_SIGNAL:
254 case MCI_UNDO:
255 return 0;
257 case MCI_OPEN:
258 { /* MCI_ANIM_OPEN_PARMS is the largest known MCI_OPEN_PARMS
259 * structure, larger than MCI_WAVE_OPEN_PARMS */
260 MCI_ANIM_OPEN_PARMSA *mci_openA = (MCI_ANIM_OPEN_PARMSA*)*dwParam2;
261 MCI_ANIM_OPEN_PARMSW *mci_openW;
262 DWORD_PTR *ptr;
264 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW));
265 if (!ptr) return -1;
267 *ptr++ = *dwParam2; /* save the previous pointer */
268 *dwParam2 = (DWORD_PTR)ptr;
269 mci_openW = (MCI_ANIM_OPEN_PARMSW *)ptr;
271 if (dwParam1 & MCI_NOTIFY)
272 mci_openW->dwCallback = mci_openA->dwCallback;
274 if (dwParam1 & MCI_OPEN_TYPE)
276 if (dwParam1 & MCI_OPEN_TYPE_ID)
277 mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
278 else
279 mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
281 if (dwParam1 & MCI_OPEN_ELEMENT)
283 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
284 mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
285 else
286 mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
288 if (dwParam1 & MCI_OPEN_ALIAS)
289 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
290 /* We don't know how many DWORD follow, as
291 * the structure depends on the device. */
292 if (HIWORD(dwParam1))
293 memcpy(&mci_openW->dwStyle, &mci_openA->dwStyle, sizeof(MCI_ANIM_OPEN_PARMSW) - sizeof(MCI_OPEN_PARMSW));
295 return 1;
297 case MCI_WINDOW:
298 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
300 MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
301 MCI_ANIM_WINDOW_PARMSW *mci_windowW;
303 mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
304 if (!mci_windowW) return -1;
306 *dwParam2 = (DWORD_PTR)mci_windowW;
308 mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
310 if (dwParam1 & MCI_NOTIFY)
311 mci_windowW->dwCallback = mci_windowA->dwCallback;
312 if (dwParam1 & MCI_ANIM_WINDOW_HWND)
313 mci_windowW->hWnd = mci_windowA->hWnd;
314 if (dwParam1 & MCI_ANIM_WINDOW_STATE)
315 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
317 return 1;
319 return 0;
321 case MCI_SYSINFO:
322 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
324 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
325 MCI_SYSINFO_PARMSW *mci_sysinfoW;
326 DWORD_PTR *ptr;
328 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
329 if (!ptr) return -1;
331 *ptr++ = *dwParam2; /* save the previous pointer */
332 *dwParam2 = (DWORD_PTR)ptr;
333 mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
335 if (dwParam1 & MCI_NOTIFY)
336 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
338 /* Size is measured in numbers of characters, despite what MSDN says. */
339 mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
340 mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize * sizeof(WCHAR));
341 mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
342 mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
343 return 1;
345 return 0;
346 case MCI_INFO:
348 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
349 MCI_INFO_PARMSW *mci_infoW;
350 DWORD_PTR *ptr;
352 ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
353 if (!ptr) return -1;
355 *ptr++ = *dwParam2; /* save the previous pointer */
356 *dwParam2 = (DWORD_PTR)ptr;
357 mci_infoW = (MCI_INFO_PARMSW *)ptr;
359 if (dwParam1 & MCI_NOTIFY)
360 mci_infoW->dwCallback = mci_infoA->dwCallback;
362 /* Size is measured in numbers of characters. */
363 mci_infoW->dwRetSize = mci_infoA->dwRetSize;
364 mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize * sizeof(WCHAR));
365 return 1;
367 case MCI_SAVE:
368 case MCI_LOAD:
369 case MCI_CAPTURE:
370 case MCI_RESTORE:
371 { /* All these commands have the same layout: callback + string + optional rect */
372 MCI_OVLY_LOAD_PARMSA *mci_loadA = (MCI_OVLY_LOAD_PARMSA *)*dwParam2;
373 MCI_OVLY_LOAD_PARMSW *mci_loadW;
375 mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
376 if (!mci_loadW) return -1;
378 *dwParam2 = (DWORD_PTR)mci_loadW;
379 if (dwParam1 & MCI_NOTIFY)
380 mci_loadW->dwCallback = mci_loadA->dwCallback;
381 mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
382 if ((MCI_SAVE == msg && dwParam1 & MCI_DGV_RECT) ||
383 (MCI_LOAD == msg && dwParam1 & MCI_OVLY_RECT) ||
384 (MCI_CAPTURE == msg && dwParam1 & MCI_DGV_CAPTURE_AT) ||
385 (MCI_RESTORE == msg && dwParam1 & MCI_DGV_RESTORE_AT))
386 mci_loadW->rc = mci_loadA->rc;
387 return 1;
389 case MCI_SOUND:
390 case MCI_ESCAPE:
391 { /* All these commands have the same layout: callback + string */
392 MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
393 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
395 mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
396 if (!mci_vd_escapeW) return -1;
398 *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
399 if (dwParam1 & MCI_NOTIFY)
400 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
401 mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
402 return 1;
404 case MCI_SETAUDIO:
405 case MCI_SETVIDEO:
406 if (!(dwParam1 & (MCI_DGV_SETVIDEO_QUALITY | MCI_DGV_SETVIDEO_ALG
407 | MCI_DGV_SETAUDIO_QUALITY | MCI_DGV_SETAUDIO_ALG)))
408 return 0;
409 /* fall through to default */
410 case MCI_RESERVE:
411 case MCI_QUALITY:
412 case MCI_LIST:
413 default:
414 FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
415 return 0; /* pass through untouched */
419 static void MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
420 DWORD result)
422 switch (msg)
424 case MCI_OPEN:
426 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
427 MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
428 MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)dwParam2;
430 mci_openA->wDeviceID = mci_openW->wDeviceID;
432 if (dwParam1 & MCI_OPEN_TYPE)
434 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
435 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
437 if (dwParam1 & MCI_OPEN_ELEMENT)
439 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
440 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
442 if (dwParam1 & MCI_OPEN_ALIAS)
443 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
444 HeapFree(GetProcessHeap(), 0, ptr);
446 break;
447 case MCI_WINDOW:
448 if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
450 MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
452 HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
453 HeapFree(GetProcessHeap(), 0, mci_windowW);
455 break;
457 case MCI_SYSINFO:
458 if (dwParam1 & (MCI_SYSINFO_INSTALLNAME | MCI_SYSINFO_NAME))
460 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
461 MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
462 MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)dwParam2;
464 if (!result)
466 WideCharToMultiByte(CP_ACP, 0,
467 mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
468 mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
469 NULL, NULL);
472 HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
473 HeapFree(GetProcessHeap(), 0, ptr);
475 break;
476 case MCI_INFO:
478 DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
479 MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
480 MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)dwParam2;
482 if (!result)
484 WideCharToMultiByte(CP_ACP, 0,
485 mci_infoW->lpstrReturn, mci_infoW->dwRetSize,
486 mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
487 NULL, NULL);
490 HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
491 HeapFree(GetProcessHeap(), 0, ptr);
493 break;
494 case MCI_SAVE:
495 case MCI_LOAD:
496 case MCI_CAPTURE:
497 case MCI_RESTORE:
498 { /* All these commands have the same layout: callback + string + optional rect */
499 MCI_OVLY_LOAD_PARMSW *mci_loadW = (MCI_OVLY_LOAD_PARMSW *)dwParam2;
501 HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
502 HeapFree(GetProcessHeap(), 0, mci_loadW);
504 break;
505 case MCI_SOUND:
506 case MCI_ESCAPE:
507 { /* All these commands have the same layout: callback + string */
508 MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
510 HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
511 HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
513 break;
515 default:
516 FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
517 break;
521 /**************************************************************************
522 * MCI_GetDevTypeFromFileName [internal]
524 static DWORD MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
526 LPCWSTR tmp;
527 HKEY hKey;
528 static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
529 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
530 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
531 if ((tmp = strrchrW(fileName, '.'))) {
532 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
533 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
534 DWORD dwLen = len;
535 LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen );
536 RegCloseKey( hKey );
537 if (lRet == ERROR_SUCCESS) return 0;
539 TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
541 return MCIERR_EXTENSION_NOT_FOUND;
544 /**************************************************************************
545 * MCI_GetDevTypeFromResource [internal]
547 static UINT MCI_GetDevTypeFromResource(LPCWSTR lpstrName)
549 WCHAR buf[32];
550 UINT uDevType;
551 for (uDevType = MCI_DEVTYPE_FIRST; uDevType <= MCI_DEVTYPE_LAST; uDevType++) {
552 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
553 /* FIXME: ignore digits suffix */
554 if (!strcmpiW(buf, lpstrName))
555 return uDevType;
558 return 0;
561 #define MAX_MCICMDTABLE 20
562 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
564 typedef struct tagWINE_MCICMDTABLE {
565 UINT uDevType;
566 HGLOBAL hMem;
567 const BYTE* lpTable;
568 UINT nVerbs; /* number of verbs in command table */
569 LPCWSTR* aVerbs; /* array of verbs to speed up the verb look up process */
570 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
572 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
574 /**************************************************************************
575 * MCI_IsCommandTableValid [internal]
577 static BOOL MCI_IsCommandTableValid(UINT uTbl)
579 const BYTE* lmem;
580 LPCWSTR str;
581 DWORD flg;
582 WORD eid;
583 int idx = 0;
584 BOOL inCst = FALSE;
586 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
587 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
589 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
590 return FALSE;
592 lmem = S_MciCmdTable[uTbl].lpTable;
593 do {
594 str = (LPCWSTR)lmem;
595 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
596 flg = *(const DWORD*)lmem;
597 eid = *(const WORD*)(lmem + sizeof(DWORD));
598 lmem += sizeof(DWORD) + sizeof(WORD);
599 idx ++;
600 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
601 switch (eid) {
602 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
603 case MCI_STRING: if (inCst) return FALSE; break;
604 case MCI_HWND: /* Occurs inside MCI_CONSTANT as in "window handle default" */
605 case MCI_HPAL:
606 case MCI_HDC:
607 case MCI_INTEGER: if (!*str) return FALSE; break;
608 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
609 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
610 case MCI_FLAG: if (!*str) return FALSE; break;
611 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
612 case MCI_RECT: if (!*str || inCst) return FALSE; break;
613 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
614 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
615 default: return FALSE;
617 } while (eid != MCI_END_COMMAND_LIST);
618 return TRUE;
621 /**************************************************************************
622 * MCI_DumpCommandTable [internal]
624 static BOOL MCI_DumpCommandTable(UINT uTbl)
626 const BYTE* lmem;
627 LPCWSTR str;
628 DWORD flg;
629 WORD eid;
631 if (!MCI_IsCommandTableValid(uTbl)) {
632 ERR("Ooops: %d is not valid\n", uTbl);
633 return FALSE;
636 lmem = S_MciCmdTable[uTbl].lpTable;
637 do {
638 do {
639 str = (LPCWSTR)lmem;
640 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
641 flg = *(const DWORD*)lmem;
642 eid = *(const WORD*)(lmem + sizeof(DWORD));
643 /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
644 lmem += sizeof(DWORD) + sizeof(WORD);
645 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
646 /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
647 } while (eid != MCI_END_COMMAND_LIST);
648 return TRUE;
652 /**************************************************************************
653 * MCI_GetCommandTable [internal]
655 static UINT MCI_GetCommandTable(UINT uDevType)
657 UINT uTbl;
658 WCHAR buf[32];
659 LPCWSTR str = NULL;
661 /* first look up existing for existing devType */
662 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
663 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
664 return uTbl;
667 /* well try to load id */
668 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
669 if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
670 str = buf;
672 } else if (uDevType == 0) {
673 static const WCHAR wszCore[] = {'C','O','R','E',0};
674 str = wszCore;
676 uTbl = MCI_NO_COMMAND_TABLE;
677 if (str) {
678 HRSRC hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
679 HANDLE hMem = 0;
681 if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
682 if (hMem) {
683 uTbl = MCI_SetCommandTable(hMem, uDevType);
684 } else {
685 WARN("No command table found in resource %p[%s]\n",
686 hWinMM32Instance, debugstr_w(str));
689 TRACE("=> %d\n", uTbl);
690 return uTbl;
693 /**************************************************************************
694 * MCI_SetCommandTable [internal]
696 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
698 int uTbl;
699 static BOOL bInitDone = FALSE;
701 /* <HACK>
702 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
703 * can be called with 0 as a uDevType to retrieve it.
704 * </HACK>
706 if (!bInitDone) {
707 bInitDone = TRUE;
708 MCI_GetCommandTable(0);
710 TRACE("(%p, %u)\n", hMem, uDevType);
711 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
712 if (!S_MciCmdTable[uTbl].lpTable) {
713 const BYTE* lmem;
714 LPCWSTR str;
715 WORD eid;
716 WORD count;
718 S_MciCmdTable[uTbl].uDevType = uDevType;
719 S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
720 S_MciCmdTable[uTbl].hMem = hMem;
722 if (TRACE_ON(mci)) {
723 MCI_DumpCommandTable(uTbl);
726 /* create the verbs table */
727 /* get # of entries */
728 lmem = S_MciCmdTable[uTbl].lpTable;
729 count = 0;
730 do {
731 str = (LPCWSTR)lmem;
732 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
733 eid = *(const WORD*)(lmem + sizeof(DWORD));
734 lmem += sizeof(DWORD) + sizeof(WORD);
735 if (eid == MCI_COMMAND_HEAD)
736 count++;
737 } while (eid != MCI_END_COMMAND_LIST);
739 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
740 S_MciCmdTable[uTbl].nVerbs = count;
742 lmem = S_MciCmdTable[uTbl].lpTable;
743 count = 0;
744 do {
745 str = (LPCWSTR)lmem;
746 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
747 eid = *(const WORD*)(lmem + sizeof(DWORD));
748 lmem += sizeof(DWORD) + sizeof(WORD);
749 if (eid == MCI_COMMAND_HEAD)
750 S_MciCmdTable[uTbl].aVerbs[count++] = str;
751 } while (eid != MCI_END_COMMAND_LIST);
752 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
753 return uTbl;
757 return MCI_NO_COMMAND_TABLE;
760 /**************************************************************************
761 * MCI_UnLoadMciDriver [internal]
763 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
765 LPWINE_MCIDRIVER* tmp;
767 if (!wmd)
768 return TRUE;
770 CloseDriver(wmd->hDriver, 0, 0);
772 if (wmd->dwPrivate != 0)
773 WARN("Unloading mci driver with non nul dwPrivate field\n");
775 EnterCriticalSection(&WINMM_cs);
776 for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
777 if (*tmp == wmd) {
778 *tmp = wmd->lpNext;
779 break;
782 LeaveCriticalSection(&WINMM_cs);
784 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
785 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
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 LPCWSTR alias = NULL;
904 /* Open always defines an alias for further reference */
905 if (dwParam & MCI_OPEN_ALIAS) { /* open ... alias */
906 alias = lpParms->lpstrAlias;
907 if (MCI_GetDriverFromString(alias))
908 return MCIERR_DUPLICATE_ALIAS;
909 } else {
910 if ((dwParam & MCI_OPEN_ELEMENT) /* open file.wav */
911 && !(dwParam & MCI_OPEN_ELEMENT_ID))
912 alias = lpParms->lpstrElementName;
913 else if (dwParam & MCI_OPEN_TYPE ) /* open cdaudio */
914 alias = wmd->lpstrDeviceType;
915 if (alias && MCI_GetDriverFromString(alias))
916 return MCIERR_DEVICE_OPEN;
918 if (alias) {
919 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(alias)+1) * sizeof(WCHAR));
920 if (!wmd->lpstrAlias) return MCIERR_OUT_OF_MEMORY;
921 strcpyW( wmd->lpstrAlias, alias);
922 /* In most cases, natives adds MCI_OPEN_ALIAS to the flags passed to the driver.
923 * Don't. The drivers don't care about the winmm alias. */
925 lpParms->wDeviceID = wmd->wDeviceID;
927 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
928 (DWORD_PTR)lpParms);
931 /**************************************************************************
932 * MCI_FindCommand [internal]
934 static LPCWSTR MCI_FindCommand(UINT uTbl, LPCWSTR verb)
936 UINT idx;
938 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
939 return NULL;
941 /* another improvement would be to have the aVerbs array sorted,
942 * so that we could use a dichotomic search on it, rather than this dumb
943 * array look up
945 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
946 if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
947 return S_MciCmdTable[uTbl].aVerbs[idx];
950 return NULL;
953 /**************************************************************************
954 * MCI_GetReturnType [internal]
956 static DWORD MCI_GetReturnType(LPCWSTR lpCmd)
958 lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
959 if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
960 return *(const DWORD*)(lpCmd + 1);
962 return 0L;
965 /**************************************************************************
966 * MCI_GetMessage [internal]
968 static WORD MCI_GetMessage(LPCWSTR lpCmd)
970 return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
973 /**************************************************************************
974 * MCI_GetDWord [internal]
976 static BOOL MCI_GetDWord(DWORD* data, LPWSTR* ptr)
978 DWORD val;
979 LPWSTR ret;
981 val = strtoulW(*ptr, &ret, 0);
983 switch (*ret) {
984 case '\0': break;
985 case ' ': ret++; break;
986 default: return FALSE;
989 *data |= val;
990 *ptr = ret;
991 return TRUE;
994 /**************************************************************************
995 * MCI_GetString [internal]
997 static DWORD MCI_GetString(LPWSTR* str, LPWSTR* args)
999 LPWSTR ptr = *args;
1001 /* see if we have a quoted string */
1002 if (*ptr == '"') {
1003 ptr = strchrW(*str = ptr + 1, '"');
1004 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1005 /* FIXME: shall we escape \" from string ?? */
1006 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1007 *ptr++ = '\0'; /* remove trailing " */
1008 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1009 } else {
1010 ptr = strchrW(ptr, ' ');
1012 if (ptr) {
1013 *ptr++ = '\0';
1014 } else {
1015 ptr = *args + strlenW(*args);
1017 *str = *args;
1020 *args = ptr;
1021 return 0;
1024 #define MCI_DATA_SIZE 16
1026 /**************************************************************************
1027 * MCI_ParseOptArgs [internal]
1029 static DWORD MCI_ParseOptArgs(DWORD* data, int _offset, LPCWSTR lpCmd,
1030 LPWSTR args, LPDWORD dwFlags)
1032 int len, offset;
1033 const char* lmem;
1034 LPCWSTR str;
1035 DWORD dwRet, flg, cflg = 0;
1036 WORD eid;
1037 BOOL inCst, found;
1039 /* loop on arguments */
1040 while (*args) {
1041 lmem = (const char*)lpCmd;
1042 found = inCst = FALSE;
1043 offset = _offset;
1045 /* skip any leading white space(s) */
1046 while (*args == ' ') args++;
1047 TRACE("args=%s\n", debugstr_w(args));
1049 do { /* loop on options for command table for the requested verb */
1050 str = (LPCWSTR)lmem;
1051 lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1052 flg = *(const DWORD*)lmem;
1053 eid = *(const WORD*)(lmem + sizeof(DWORD));
1054 lmem += sizeof(DWORD) + sizeof(WORD);
1055 /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1057 switch (eid) {
1058 case MCI_CONSTANT:
1059 inCst = TRUE; cflg = flg; break;
1060 case MCI_END_CONSTANT:
1061 /* there may be additional integral values after flag in constant */
1062 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1063 *dwFlags |= cflg;
1065 inCst = FALSE; cflg = 0;
1066 break;
1067 case MCI_RETURN:
1068 if (offset != _offset) {
1069 ERR("MCI_RETURN not in first position\n");
1070 return MCIERR_PARSER_INTERNAL;
1074 if (strncmpiW(args, str, len) == 0 &&
1075 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1076 /* store good values into data[] */
1077 args += len;
1078 while (*args == ' ') args++;
1079 found = TRUE;
1081 switch (eid) {
1082 case MCI_COMMAND_HEAD:
1083 case MCI_RETURN:
1084 case MCI_END_COMMAND:
1085 case MCI_END_COMMAND_LIST:
1086 case MCI_CONSTANT: /* done above */
1087 case MCI_END_CONSTANT: /* done above */
1088 break;
1089 case MCI_FLAG:
1090 *dwFlags |= flg;
1091 TRACE("flag=%08x\n", flg);
1092 break;
1093 case MCI_HWND:
1094 case MCI_HPAL:
1095 case MCI_HDC:
1096 case MCI_INTEGER:
1097 if (inCst) {
1098 data[offset] |= flg;
1099 *dwFlags |= cflg;
1100 inCst = FALSE;
1101 TRACE("flag=%08x constant=%08x\n", cflg, flg);
1102 } else {
1103 *dwFlags |= flg;
1104 if (!MCI_GetDWord(&(data[offset]), &args)) {
1105 return MCIERR_BAD_INTEGER;
1107 TRACE("flag=%08x int=%d\n", flg, data[offset]);
1109 break;
1110 case MCI_RECT:
1111 /* store rect in data (offset..offset+3) */
1112 *dwFlags |= flg;
1113 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1114 !MCI_GetDWord(&(data[offset+1]), &args) ||
1115 !MCI_GetDWord(&(data[offset+2]), &args) ||
1116 !MCI_GetDWord(&(data[offset+3]), &args)) {
1117 ERR("Bad rect %s\n", debugstr_w(args));
1118 return MCIERR_BAD_INTEGER;
1120 TRACE("flag=%08x for rectangle\n", flg);
1121 break;
1122 case MCI_STRING:
1123 *dwFlags |= flg;
1124 if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1125 return dwRet;
1126 TRACE("flag=%08x string=%s\n", flg, debugstr_w(*(LPWSTR*)&data[offset]));
1127 break;
1128 default: ERR("oops\n");
1130 /* exit inside while loop, except if just entered in constant area definition */
1131 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1132 } else {
1133 /* have offset incremented if needed */
1134 switch (eid) {
1135 case MCI_COMMAND_HEAD:
1136 case MCI_RETURN:
1137 case MCI_END_COMMAND:
1138 case MCI_END_COMMAND_LIST:
1139 case MCI_CONSTANT:
1140 case MCI_FLAG: break;
1141 case MCI_HWND:
1142 case MCI_HPAL:
1143 case MCI_HDC: if (!inCst) offset += sizeof(HANDLE)/sizeof(DWORD); break;
1144 case MCI_INTEGER: if (!inCst) offset++; break;
1145 case MCI_END_CONSTANT: offset++; break;
1146 case MCI_STRING: offset += sizeof(LPWSTR)/sizeof(DWORD); break;
1147 case MCI_RECT: offset += 4; break;
1148 default: ERR("oops\n");
1151 } while (eid != MCI_END_COMMAND);
1152 if (!found) {
1153 WARN("Optarg %s not found\n", debugstr_w(args));
1154 return MCIERR_UNRECOGNIZED_COMMAND;
1156 if (offset == MCI_DATA_SIZE) {
1157 ERR("Internal data[] buffer overflow\n");
1158 return MCIERR_PARSER_INTERNAL;
1161 return 0;
1164 /**************************************************************************
1165 * MCI_HandleReturnValues [internal]
1167 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1168 MCI_GENERIC_PARMS *params, LPWSTR lpstrRet, UINT uRetLen)
1170 static const WCHAR fmt_d [] = {'%','d',0};
1171 static const WCHAR fmt_d4 [] = {'%','d',' ','%','d',' ','%','d',' ','%','d',0};
1172 static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1173 static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1175 if (lpstrRet) {
1176 switch (retType) {
1177 case 0: /* nothing to return */
1178 break;
1179 case MCI_INTEGER:
1181 DWORD data = *(DWORD *)(params + 1);
1182 switch (dwRet & 0xFFFF0000ul) {
1183 case 0:
1184 case MCI_INTEGER_RETURNED:
1185 snprintfW(lpstrRet, uRetLen, fmt_d, data);
1186 break;
1187 case MCI_RESOURCE_RETURNED:
1188 /* return string which ID is HIWORD(data),
1189 * string is loaded from mmsystem.dll */
1190 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1191 break;
1192 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1193 /* return string which ID is HIWORD(data),
1194 * string is loaded from driver */
1195 /* FIXME: this is wrong for a 16 bit handle */
1196 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1197 HIWORD(data), lpstrRet, uRetLen);
1198 break;
1199 case MCI_COLONIZED3_RETURN:
1200 snprintfW(lpstrRet, uRetLen, wszCol3,
1201 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1202 LOBYTE(HIWORD(data)));
1203 break;
1204 case MCI_COLONIZED4_RETURN:
1205 snprintfW(lpstrRet, uRetLen, wszCol4,
1206 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1207 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1208 break;
1209 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1211 break;
1213 #ifdef MCI_INTEGER64
1214 case MCI_INTEGER64:
1216 static const WCHAR fmt_ld [] = {'%','l','d',0};
1217 DWORD_PTR data = *(DWORD_PTR *)(params + 1);
1218 switch (dwRet & 0xFFFF0000ul) {
1219 case 0:
1220 case MCI_INTEGER_RETURNED:
1221 snprintfW(lpstrRet, uRetLen, fmt_ld, data);
1222 break;
1223 case MCI_RESOURCE_RETURNED:
1224 /* return string which ID is HIWORD(data),
1225 * string is loaded from mmsystem.dll */
1226 LoadStringW(hWinMM32Instance, HIWORD(data), lpstrRet, uRetLen);
1227 break;
1228 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1229 /* return string which ID is HIWORD(data),
1230 * string is loaded from driver */
1231 /* FIXME: this is wrong for a 16 bit handle */
1232 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1233 HIWORD(data), lpstrRet, uRetLen);
1234 break;
1235 case MCI_COLONIZED3_RETURN:
1236 snprintfW(lpstrRet, uRetLen, wszCol3,
1237 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1238 LOBYTE(HIWORD(data)));
1239 break;
1240 case MCI_COLONIZED4_RETURN:
1241 snprintfW(lpstrRet, uRetLen, wszCol4,
1242 LOBYTE(LOWORD(data)), HIBYTE(LOWORD(data)),
1243 LOBYTE(HIWORD(data)), HIBYTE(HIWORD(data)));
1244 break;
1245 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
1247 break;
1249 #endif
1250 case MCI_STRING:
1251 switch (dwRet & 0xFFFF0000ul) {
1252 case 0:
1253 /* nothing to do data[0] == lpstrRet */
1254 break;
1255 case MCI_INTEGER_RETURNED:
1257 DWORD *data = (DWORD *)(params + 1);
1258 *data = *(LPDWORD)lpstrRet;
1259 snprintfW(lpstrRet, uRetLen, fmt_d, *data);
1260 break;
1262 default:
1263 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1264 break;
1266 break;
1267 case MCI_RECT:
1269 DWORD *data = (DWORD *)(params + 1);
1270 if (dwRet & 0xFFFF0000ul)
1271 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1272 snprintfW(lpstrRet, uRetLen, fmt_d4, data[0], data[1], data[2], data[3]);
1273 break;
1275 default: ERR("oops\n");
1278 return LOWORD(dwRet);
1281 /**************************************************************************
1282 * mciSendStringW [WINMM.@]
1284 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1285 UINT uRetLen, HWND hwndCallback)
1287 LPWSTR verb, dev, args;
1288 LPWINE_MCIDRIVER wmd = 0;
1289 MCIDEVICEID uDevID, auto_open = 0;
1290 DWORD dwFlags = 0, dwRet = 0;
1291 int offset = 0;
1292 DWORD retType;
1293 LPCWSTR lpCmd = 0;
1294 WORD wMsg = 0;
1295 static const WCHAR wszNew[] = {'n','e','w',0};
1296 static const WCHAR wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1297 static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1298 union
1300 MCI_GENERIC_PARMS generic;
1301 MCI_OPEN_PARMSW open;
1302 MCI_SOUND_PARMSW sound;
1303 MCI_SYSINFO_PARMSW sysinfo;
1304 DWORD dw[MCI_DATA_SIZE];
1305 } data;
1307 TRACE("(%s, %p, %d, %p)\n",
1308 debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1309 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1311 /* format is <command> <device> <optargs> */
1312 if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1313 return MCIERR_OUT_OF_MEMORY;
1314 strcpyW( verb, lpstrCommand );
1315 CharLowerW(verb);
1317 memset(&data, 0, sizeof(data));
1319 if (!(args = strchrW(verb, ' '))) {
1320 dwRet = MCIERR_MISSING_DEVICE_NAME;
1321 goto errCleanUp;
1323 *args++ = '\0';
1324 if ((dwRet = MCI_GetString(&dev, &args))) {
1325 goto errCleanUp;
1327 uDevID = strcmpiW(dev, wszAll) ? 0 : MCI_ALL_DEVICE_ID;
1329 /* Determine devType from open */
1330 if (!strcmpW(verb, wszOpen)) {
1331 LPWSTR devType, tmp;
1332 WCHAR buf[128];
1334 /* case dev == 'new' has to be handled */
1335 if (!strcmpW(dev, wszNew)) {
1336 dev = 0;
1337 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1338 devType += 5;
1339 tmp = strchrW(devType, ' ');
1340 if (tmp) *tmp = '\0';
1341 devType = str_dup_upper(devType);
1342 if (tmp) *tmp = ' ';
1343 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1344 } else {
1345 WARN("open new requires device type\n");
1346 dwRet = MCIERR_MISSING_DEVICE_NAME;
1347 goto errCleanUp;
1349 } else if ((devType = strchrW(dev, '!')) != NULL) {
1350 *devType++ = '\0';
1351 tmp = devType; devType = dev; dev = tmp;
1353 dwFlags |= MCI_OPEN_TYPE;
1354 data.open.lpstrDeviceType = devType;
1355 devType = str_dup_upper(devType);
1356 dwFlags |= MCI_OPEN_ELEMENT;
1357 data.open.lpstrElementName = dev;
1358 } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1359 /* this is the name of a mci driver's type */
1360 tmp = strchrW(dev, ' ');
1361 if (tmp) *tmp = '\0';
1362 data.open.lpstrDeviceType = dev;
1363 devType = str_dup_upper(dev);
1364 if (tmp) *tmp = ' ';
1365 dwFlags |= MCI_OPEN_TYPE;
1366 } else {
1367 if ((devType = strstrW(args, wszTypeS)) != NULL) {
1368 devType += 5;
1369 tmp = strchrW(devType, ' ');
1370 if (tmp) *tmp = '\0';
1371 devType = str_dup_upper(devType);
1372 if (tmp) *tmp = ' ';
1373 /* dwFlags and lpstrDeviceType will be correctly set in ParseOpt loop */
1374 } else {
1375 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1376 goto errCleanUp;
1378 devType = str_dup_upper(buf);
1380 dwFlags |= MCI_OPEN_ELEMENT;
1381 data.open.lpstrElementName = dev;
1383 if (MCI_ALL_DEVICE_ID == uDevID) {
1384 dwRet = MCIERR_CANNOT_USE_ALL;
1385 goto errCleanUp;
1387 if (!strstrW(args, wszSAliasS) && !dev) {
1388 dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1389 goto errCleanUp;
1392 dwRet = MCI_LoadMciDriver(devType, &wmd);
1393 if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1394 dwRet = MCIERR_INVALID_DEVICE_NAME;
1395 HeapFree(GetProcessHeap(), 0, devType);
1396 if (dwRet)
1397 goto errCleanUp;
1398 } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))
1399 && (lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb))) {
1400 /* auto-open uses the core command table */
1401 switch (MCI_GetMessage(lpCmd)) {
1402 case MCI_SOUND: /* command does not use a device name */
1403 case MCI_SYSINFO:
1404 break;
1405 case MCI_CLOSE: /* don't auto-open for close */
1406 case MCI_BREAK: /* no auto-open for system commands */
1407 dwRet = MCIERR_INVALID_DEVICE_NAME;
1408 goto errCleanUp;
1409 break;
1410 default:
1412 static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1413 WCHAR buf[138], retbuf[6];
1414 snprintfW(buf, sizeof(buf)/sizeof(WCHAR), wszOpenWait, dev);
1415 /* open via mciSendString handles quoting, dev!file syntax and alias creation */
1416 if ((dwRet = mciSendStringW(buf, retbuf, sizeof(retbuf)/sizeof(WCHAR), 0)) != 0)
1417 goto errCleanUp;
1418 auto_open = strtoulW(retbuf, NULL, 10);
1419 TRACE("auto-opened %u for %s\n", auto_open, debugstr_w(dev));
1421 /* FIXME: test for notify flag (how to preparse?) before opening */
1422 wmd = MCI_GetDriver(auto_open);
1423 if (!wmd) {
1424 ERR("No auto-open device %u\n", auto_open);
1425 dwRet = MCIERR_INVALID_DEVICE_ID;
1426 goto errCleanUp;
1432 /* get the verb in the different command tables */
1433 if (wmd) {
1434 /* try the device specific command table */
1435 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1436 if (!lpCmd) {
1437 /* try the type specific command table */
1438 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1439 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1440 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1441 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1444 /* try core command table */
1445 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1447 if (!lpCmd) {
1448 TRACE("Command %s not found!\n", debugstr_w(verb));
1449 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1450 goto errCleanUp;
1452 wMsg = MCI_GetMessage(lpCmd);
1454 /* set return information */
1455 offset = sizeof(data.generic);
1456 switch (retType = MCI_GetReturnType(lpCmd)) {
1457 case 0:
1458 break;
1459 case MCI_INTEGER:
1460 offset += sizeof(DWORD);
1461 break;
1462 case MCI_STRING:
1463 data.sysinfo.lpstrReturn = lpstrRet;
1464 data.sysinfo.dwRetSize = uRetLen;
1465 offset = FIELD_OFFSET( MCI_SYSINFO_PARMSW, dwNumber );
1466 break;
1467 case MCI_RECT:
1468 offset += 4 * sizeof(DWORD);
1469 break;
1470 #ifdef MCI_INTEGER64
1471 case MCI_INTEGER64:
1472 offset += sizeof(DWORD_PTR);
1473 break;
1474 #endif
1475 default:
1476 ERR("oops\n");
1479 TRACE("verb=%s on dev=%s; offset=%d\n",
1480 debugstr_w(verb), debugstr_w(dev), offset);
1482 if ((dwRet = MCI_ParseOptArgs(data.dw, offset / sizeof(DWORD), lpCmd, args, &dwFlags)))
1483 goto errCleanUp;
1485 /* set up call back */
1486 if (auto_open) {
1487 if (dwFlags & MCI_NOTIFY) {
1488 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1489 goto errCleanUp;
1491 /* FIXME: the command should get its own notification window set up and
1492 * ask for device closing while processing the notification mechanism.
1493 * hwndCallback = ...
1494 * dwFlags |= MCI_NOTIFY;
1495 * In the meantime special-case all commands but PLAY and RECORD below. */
1497 if (dwFlags & MCI_NOTIFY) {
1498 data.generic.dwCallback = (DWORD_PTR)hwndCallback;
1501 switch (wMsg) {
1502 case MCI_OPEN:
1503 if (strcmpW(verb, wszOpen)) {
1504 FIXME("Cannot open with command %s\n", debugstr_w(verb));
1505 dwRet = MCIERR_INTERNAL;
1506 wMsg = 0;
1507 goto errCleanUp;
1509 break;
1510 case MCI_SYSINFO:
1511 /* Requirements on dev depend on the flags:
1512 * alias with INSTALLNAME, name like "digitalvideo"
1513 * with QUANTITY and NAME. */
1515 data.sysinfo.wDeviceType = MCI_ALL_DEVICE_ID;
1516 if (uDevID != MCI_ALL_DEVICE_ID) {
1517 if (dwFlags & MCI_SYSINFO_INSTALLNAME)
1518 wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1519 else if (!(data.sysinfo.wDeviceType = MCI_GetDevTypeFromResource(dev))) {
1520 dwRet = MCIERR_DEVICE_TYPE_REQUIRED;
1521 goto errCleanUp;
1525 break;
1526 case MCI_SOUND:
1527 /* FIXME: name is optional, "sound" is a valid command.
1528 * FIXME: Parse "sound notify" as flag, not as name. */
1529 data.sound.lpstrSoundName = dev;
1530 dwFlags |= MCI_SOUND_NAME;
1531 break;
1534 TRACE("[%d, %s, %08x, %08x %08x %08x %08x %08x %08x %08x %08x %08x %08x]\n",
1535 wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(wMsg), dwFlags,
1536 data.dw[0], data.dw[1], data.dw[2], data.dw[3], data.dw[4],
1537 data.dw[5], data.dw[6], data.dw[7], data.dw[8], data.dw[9]);
1539 if (wMsg == MCI_OPEN) {
1540 if ((dwRet = MCI_FinishOpen(wmd, &data.open, dwFlags)))
1541 goto errCleanUp;
1542 /* FIXME: notification is not properly shared across two opens */
1543 } else {
1544 dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, wMsg, dwFlags, (DWORD_PTR)&data);
1546 TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1547 if (!LOWORD(dwRet)) {
1548 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, &data.generic, lpstrRet, uRetLen);
1549 TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1552 errCleanUp:
1553 if (auto_open) {
1554 /* PLAY and RECORD are the only known non-immediate commands */
1555 if (LOWORD(dwRet) || !(wMsg == MCI_PLAY || wMsg == MCI_RECORD))
1556 MCI_SendCommand(auto_open, MCI_CLOSE, 0, 0);
1557 else
1558 FIXME("leaking auto-open device %u\n", auto_open);
1560 if (wMsg == MCI_OPEN && LOWORD(dwRet) && wmd)
1561 MCI_UnLoadMciDriver(wmd);
1562 HeapFree(GetProcessHeap(), 0, verb);
1563 return dwRet;
1566 /**************************************************************************
1567 * mciSendStringA [WINMM.@]
1569 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1570 UINT uRetLen, HWND hwndCallback)
1572 LPWSTR lpwstrCommand;
1573 LPWSTR lpwstrRet = NULL;
1574 UINT ret;
1575 INT len;
1577 /* FIXME: is there something to do with lpstrReturnString ? */
1578 len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1579 lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1580 MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1581 if (lpstrRet)
1583 if (uRetLen) *lpstrRet = '\0'; /* NT-w2k3 use memset(lpstrRet, 0, uRetLen); */
1584 lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1585 if (!lpwstrRet) {
1586 HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1587 return MCIERR_OUT_OF_MEMORY;
1590 ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1591 if (!ret && lpwstrRet)
1592 WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1593 HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1594 HeapFree(GetProcessHeap(), 0, lpwstrRet);
1595 return ret;
1598 /**************************************************************************
1599 * mciExecute [WINMM.@]
1601 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1603 char strRet[256];
1604 DWORD ret;
1606 TRACE("(%s)!\n", lpstrCommand);
1608 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1609 if (ret != 0) {
1610 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1611 sprintf(strRet, "Unknown MCI error (%d)", ret);
1613 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1615 /* FIXME: what shall I return ? */
1616 return TRUE;
1619 /**************************************************************************
1620 * mciLoadCommandResource [WINMM.@]
1622 * Strangely, this function only exists as a UNICODE one.
1624 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1626 UINT ret = MCI_NO_COMMAND_TABLE;
1627 HRSRC hRsrc = 0;
1628 HGLOBAL hMem;
1630 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1632 /* if a file named "resname.mci" exits, then load resource "resname" from it
1633 * otherwise directly from driver
1634 * We don't support it (who uses this feature ?), but we check anyway
1636 if (!type) {
1637 #if 0
1638 /* FIXME: we should put this back into order, but I never found a program
1639 * actually using this feature, so we may not need it
1641 char buf[128];
1642 OFSTRUCT ofs;
1644 strcat(strcpy(buf, resname), ".mci");
1645 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1646 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1648 #endif
1650 if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1651 (hMem = LoadResource(hInst, hRsrc))) {
1652 ret = MCI_SetCommandTable(hMem, type);
1653 FreeResource(hMem);
1655 else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1657 TRACE("=> %04x\n", ret);
1658 return ret;
1661 /**************************************************************************
1662 * mciFreeCommandResource [WINMM.@]
1664 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1666 TRACE("(%08x)!\n", uTable);
1668 if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1669 return FALSE;
1671 FreeResource(S_MciCmdTable[uTable].hMem);
1672 S_MciCmdTable[uTable].hMem = NULL;
1673 S_MciCmdTable[uTable].lpTable = NULL;
1674 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1675 S_MciCmdTable[uTable].aVerbs = 0;
1676 S_MciCmdTable[uTable].nVerbs = 0;
1677 return TRUE;
1680 /**************************************************************************
1681 * MCI_Open [internal]
1683 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1685 WCHAR strDevTyp[128];
1686 DWORD dwRet;
1687 LPWINE_MCIDRIVER wmd = NULL;
1689 TRACE("(%08X, %p)\n", dwParam, lpParms);
1690 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1692 /* only two low bytes are generic, the other ones are dev type specific */
1693 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1694 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1695 MCI_NOTIFY|MCI_WAIT)
1696 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1697 FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1699 #undef WINE_MCIDRIVER_SUPP
1701 strDevTyp[0] = 0;
1703 if (dwParam & MCI_OPEN_TYPE) {
1704 if (dwParam & MCI_OPEN_TYPE_ID) {
1705 WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1707 if (uDevType < MCI_DEVTYPE_FIRST ||
1708 uDevType > MCI_DEVTYPE_LAST ||
1709 !LoadStringW(hWinMM32Instance, uDevType,
1710 strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1711 dwRet = MCIERR_BAD_INTEGER;
1712 goto errCleanUp;
1714 } else {
1715 LPWSTR ptr;
1716 if (lpParms->lpstrDeviceType == NULL) {
1717 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1718 goto errCleanUp;
1720 strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1721 ptr = strchrW(strDevTyp, '!');
1722 if (ptr) {
1723 /* this behavior is not documented in windows. However, since, in
1724 * some occasions, MCI_OPEN handling is translated by WinMM into
1725 * a call to mciSendString("open <type>"); this code shall be correct
1727 if (dwParam & MCI_OPEN_ELEMENT) {
1728 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1729 debugstr_w(lpParms->lpstrElementName),
1730 debugstr_w(strDevTyp));
1731 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1732 goto errCleanUp;
1734 dwParam |= MCI_OPEN_ELEMENT;
1735 *ptr++ = 0;
1736 /* FIXME: not a good idea to write in user supplied buffer */
1737 lpParms->lpstrElementName = ptr;
1741 TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1744 if (dwParam & MCI_OPEN_ELEMENT) {
1745 TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1747 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1748 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1749 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1750 goto errCleanUp;
1753 if (!lpParms->lpstrElementName) {
1754 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1755 goto errCleanUp;
1758 /* type, if given as a parameter, supersedes file extension */
1759 if (!strDevTyp[0] &&
1760 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1761 strDevTyp, sizeof(strDevTyp))) {
1762 static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1763 if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1764 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1765 goto errCleanUp;
1767 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1768 strcpyW(strDevTyp, wszCdAudio);
1772 if (strDevTyp[0] == 0) {
1773 FIXME("Couldn't load driver\n");
1774 dwRet = MCIERR_INVALID_DEVICE_NAME;
1775 goto errCleanUp;
1778 if (dwParam & MCI_OPEN_ALIAS) {
1779 TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1780 if (!lpParms->lpstrAlias) {
1781 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1782 goto errCleanUp;
1786 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1787 goto errCleanUp;
1790 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1791 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1792 /* FIXME: is dwRet the correct ret code ? */
1793 goto errCleanUp;
1796 /* only handled devices fall through */
1797 TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1798 return 0;
1800 errCleanUp:
1801 if (wmd) MCI_UnLoadMciDriver(wmd);
1802 return dwRet;
1805 /**************************************************************************
1806 * MCI_Close [internal]
1808 static DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1810 DWORD dwRet;
1811 LPWINE_MCIDRIVER wmd;
1813 TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1815 /* Every device must handle MCI_NOTIFY on its own. */
1816 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1817 while (MciDrivers) {
1818 /* Retrieve the device ID under lock, but send the message without,
1819 * the driver might be calling some winmm functions from another
1820 * thread before being fully stopped.
1822 EnterCriticalSection(&WINMM_cs);
1823 if (!MciDrivers)
1825 LeaveCriticalSection(&WINMM_cs);
1826 break;
1828 wDevID = MciDrivers->wDeviceID;
1829 LeaveCriticalSection(&WINMM_cs);
1830 MCI_Close(wDevID, dwParam, lpParms);
1832 return 0;
1835 if (!(wmd = MCI_GetDriver(wDevID))) {
1836 return MCIERR_INVALID_DEVICE_ID;
1839 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1841 MCI_UnLoadMciDriver(wmd);
1843 return dwRet;
1846 /**************************************************************************
1847 * MCI_WriteString [internal]
1849 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1851 DWORD ret = 0;
1853 if (lpSrcStr) {
1854 if (dstSize <= strlenW(lpSrcStr)) {
1855 ret = MCIERR_PARAM_OVERFLOW;
1856 } else {
1857 strcpyW(lpDstStr, lpSrcStr);
1859 } else {
1860 *lpDstStr = 0;
1862 return ret;
1865 /**************************************************************************
1866 * MCI_Sysinfo [internal]
1868 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1870 DWORD ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1871 WCHAR buf[2048], *s = buf, *p;
1872 LPWINE_MCIDRIVER wmd;
1873 HKEY hKey;
1875 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1876 if (lpParms->lpstrReturn == NULL) return MCIERR_PARAM_OVERFLOW;
1878 TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1879 uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1880 if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1881 uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1883 switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1884 case MCI_SYSINFO_QUANTITY:
1885 if (lpParms->dwRetSize < sizeof(DWORD))
1886 return MCIERR_PARAM_OVERFLOW;
1887 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1888 if (uDevID == MCI_ALL_DEVICE_ID) {
1889 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1890 if (dwFlags & MCI_SYSINFO_OPEN) {
1891 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1892 EnterCriticalSection(&WINMM_cs);
1893 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1894 cnt++;
1896 LeaveCriticalSection(&WINMM_cs);
1897 } else {
1898 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1899 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1900 0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1901 RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1902 RegCloseKey( hKey );
1904 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1905 for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1907 } else {
1908 if (dwFlags & MCI_SYSINFO_OPEN) {
1909 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1910 EnterCriticalSection(&WINMM_cs);
1911 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1912 if (wmd->wType == lpParms->wDeviceType) cnt++;
1914 LeaveCriticalSection(&WINMM_cs);
1915 } else {
1916 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1917 FIXME("Don't know how to get # of MCI devices of a given type\n");
1918 /* name = LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType))
1919 * then lookup registry and/or system.ini for name, ignoring digits suffix */
1920 switch (LOWORD(lpParms->wDeviceType)) {
1921 case MCI_DEVTYPE_CD_AUDIO:
1922 case MCI_DEVTYPE_WAVEFORM_AUDIO:
1923 case MCI_DEVTYPE_SEQUENCER:
1924 cnt = 1;
1925 break;
1926 default: /* "digitalvideo" gets 0 because it's not in the registry */
1927 cnt = 0;
1931 *(DWORD*)lpParms->lpstrReturn = cnt;
1932 TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1933 ret = MCI_INTEGER_RETURNED;
1934 /* return ret; Only Win9x sends a notification in this case. */
1935 break;
1936 case MCI_SYSINFO_INSTALLNAME:
1937 TRACE("MCI_SYSINFO_INSTALLNAME\n");
1938 if ((wmd = MCI_GetDriver(uDevID))) {
1939 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1940 wmd->lpstrDeviceType);
1941 } else {
1942 ret = (uDevID == MCI_ALL_DEVICE_ID)
1943 ? MCIERR_CANNOT_USE_ALL : MCIERR_INVALID_DEVICE_NAME;
1945 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1946 break;
1947 case MCI_SYSINFO_NAME:
1948 s = NULL;
1949 if (dwFlags & MCI_SYSINFO_OPEN) {
1950 /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1951 TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1952 uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1953 EnterCriticalSection(&WINMM_cs);
1954 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1955 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1956 if (uDevID == MCI_ALL_DEVICE_ID ||
1957 lpParms->wDeviceType == wmd->wType) {
1958 cnt++;
1959 if (cnt == lpParms->dwNumber) {
1960 s = wmd->lpstrAlias;
1961 break;
1965 LeaveCriticalSection(&WINMM_cs);
1966 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1967 } else if (MCI_ALL_DEVICE_ID == uDevID) {
1968 TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
1969 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0,
1970 KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1971 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt,
1972 0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS &&
1973 lpParms->dwNumber <= cnt) {
1974 DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
1975 if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1,
1976 buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1977 s = buf;
1979 RegCloseKey( hKey );
1981 if (!s) {
1982 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1983 for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1984 TRACE("%d: %s\n", cnt, debugstr_w(p));
1985 if (cnt == lpParms->dwNumber - 1) {
1986 s = p;
1987 break;
1992 ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, s) : MCIERR_OUTOFRANGE;
1993 } else {
1994 FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
1995 /* Cheating: what is asked for is the nth device from the registry. */
1996 if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
1997 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
1998 ret = MCIERR_OUTOFRANGE;
1999 else {
2000 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
2001 lpParms->lpstrReturn, lpParms->dwRetSize);
2002 ret = 0;
2005 TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
2006 break;
2007 default:
2008 TRACE("Unsupported flag value=%08x\n", dwFlags);
2009 ret = MCIERR_UNRECOGNIZED_KEYWORD;
2011 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
2012 mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
2013 return ret;
2016 /**************************************************************************
2017 * MCI_Break [internal]
2019 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
2021 DWORD dwRet = 0;
2023 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2024 FIXME("(%04x) vkey %04X stub\n", dwFlags, lpParms->nVirtKey);
2026 if (MMSYSERR_NOERROR==dwRet && (dwFlags & MCI_NOTIFY))
2027 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2028 return dwRet;
2031 /**************************************************************************
2032 * MCI_Sound [internal]
2034 static DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
2036 DWORD dwRet;
2038 if (dwFlags & MCI_SOUND_NAME) {
2039 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
2040 else dwRet = PlaySoundW(lpParms->lpstrSoundName, NULL,
2041 SND_ALIAS | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2042 ? 0 : MCIERR_HARDWARE;
2043 } else dwRet = PlaySoundW((LPCWSTR)SND_ALIAS_SYSTEMDEFAULT, NULL,
2044 SND_ALIAS_ID | (dwFlags & MCI_WAIT ? SND_SYNC : SND_ASYNC))
2045 ? 0 : MCIERR_HARDWARE;
2047 if (!dwRet && lpParms && (dwFlags & MCI_NOTIFY))
2048 mciDriverNotify((HWND)lpParms->dwCallback, wDevID, MCI_NOTIFY_SUCCESSFUL);
2049 return dwRet;
2052 /**************************************************************************
2053 * MCI_SendCommand [internal]
2055 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2057 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
2059 switch (wMsg) {
2060 case MCI_OPEN:
2061 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
2062 break;
2063 case MCI_CLOSE:
2064 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
2065 break;
2066 case MCI_SYSINFO:
2067 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2068 break;
2069 case MCI_BREAK:
2070 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2071 break;
2072 case MCI_SOUND:
2073 dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2074 break;
2075 default:
2076 if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
2077 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
2078 dwRet = MCIERR_CANNOT_USE_ALL;
2079 } else {
2080 dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
2082 break;
2084 return dwRet;
2087 /**************************************************************************
2088 * MCI_CleanUp [internal]
2090 * Some MCI commands need to be cleaned-up (when not called from
2091 * mciSendString), because MCI drivers return extra information for string
2092 * transformation. This function gets rid of them.
2094 static LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
2096 if (LOWORD(dwRet))
2097 return LOWORD(dwRet);
2099 switch (wMsg) {
2100 case MCI_GETDEVCAPS:
2101 switch (dwRet & 0xFFFF0000ul) {
2102 case 0:
2103 case MCI_COLONIZED3_RETURN:
2104 case MCI_COLONIZED4_RETURN:
2105 case MCI_INTEGER_RETURNED:
2106 /* nothing to do */
2107 break;
2108 case MCI_RESOURCE_RETURNED:
2109 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2111 LPMCI_GETDEVCAPS_PARMS lmgp;
2113 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
2114 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2115 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2117 break;
2118 default:
2119 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2120 HIWORD(dwRet), MCI_MessageToString(wMsg));
2122 break;
2123 case MCI_STATUS:
2124 switch (dwRet & 0xFFFF0000ul) {
2125 case 0:
2126 case MCI_COLONIZED3_RETURN:
2127 case MCI_COLONIZED4_RETURN:
2128 case MCI_INTEGER_RETURNED:
2129 /* nothing to do */
2130 break;
2131 case MCI_RESOURCE_RETURNED:
2132 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2134 LPMCI_STATUS_PARMS lsp;
2136 lsp = (LPMCI_STATUS_PARMS)dwParam2;
2137 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2138 lsp->dwReturn = LOWORD(lsp->dwReturn);
2140 break;
2141 default:
2142 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2143 HIWORD(dwRet), MCI_MessageToString(wMsg));
2145 break;
2146 case MCI_SYSINFO:
2147 switch (dwRet & 0xFFFF0000ul) {
2148 case 0:
2149 case MCI_INTEGER_RETURNED:
2150 /* nothing to do */
2151 break;
2152 default:
2153 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2155 break;
2156 default:
2157 if (HIWORD(dwRet)) {
2158 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2159 dwRet, MCI_MessageToString(wMsg));
2161 break;
2163 return LOWORD(dwRet);
2166 /**************************************************************************
2167 * mciGetErrorStringW [WINMM.@]
2169 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2171 BOOL ret = FALSE;
2173 if (lpstrBuffer != NULL && uLength > 0 &&
2174 wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2176 if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2177 ret = TRUE;
2180 return ret;
2183 /**************************************************************************
2184 * mciGetErrorStringA [WINMM.@]
2186 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2188 BOOL ret = FALSE;
2190 if (lpstrBuffer != NULL && uLength > 0 &&
2191 dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2193 if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2194 ret = TRUE;
2197 return ret;
2200 /**************************************************************************
2201 * mciDriverNotify [WINMM.@]
2203 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2205 TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2207 return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2210 /**************************************************************************
2211 * mciGetDriverData [WINMM.@]
2213 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2215 LPWINE_MCIDRIVER wmd;
2217 TRACE("(%04x)\n", uDeviceID);
2219 wmd = MCI_GetDriver(uDeviceID);
2221 if (!wmd) {
2222 WARN("Bad uDeviceID\n");
2223 return 0L;
2226 return wmd->dwPrivate;
2229 /**************************************************************************
2230 * mciSetDriverData [WINMM.@]
2232 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2234 LPWINE_MCIDRIVER wmd;
2236 TRACE("(%04x, %08lx)\n", uDeviceID, data);
2238 wmd = MCI_GetDriver(uDeviceID);
2240 if (!wmd) {
2241 WARN("Bad uDeviceID\n");
2242 return FALSE;
2245 wmd->dwPrivate = data;
2246 return TRUE;
2249 /**************************************************************************
2250 * mciSendCommandW [WINMM.@]
2253 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2255 DWORD dwRet;
2257 TRACE("(%08x, %s, %08lx, %08lx)\n",
2258 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2260 dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2261 dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2262 TRACE("=> %08x\n", dwRet);
2263 return dwRet;
2266 /**************************************************************************
2267 * mciSendCommandA [WINMM.@]
2269 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2271 DWORD ret;
2272 int mapped;
2274 TRACE("(%08x, %s, %08lx, %08lx)\n",
2275 wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2277 mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2278 if (mapped == -1)
2280 FIXME("message %04x mapping failed\n", wMsg);
2281 return MCIERR_OUT_OF_MEMORY;
2283 ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2284 if (mapped)
2285 MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2286 return ret;
2289 /**************************************************************************
2290 * mciGetDeviceIDA [WINMM.@]
2292 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2294 LPWSTR w = MCI_strdupAtoW(lpstrName);
2295 UINT ret = MCIERR_OUT_OF_MEMORY;
2297 if (w)
2299 ret = mciGetDeviceIDW(w);
2300 HeapFree(GetProcessHeap(), 0, w);
2302 return ret;
2305 /**************************************************************************
2306 * mciGetDeviceIDW [WINMM.@]
2308 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2310 return MCI_GetDriverFromString(lpwstrName);
2313 /**************************************************************************
2314 * MCI_DefYieldProc [internal]
2316 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2318 INT16 ret;
2319 MSG msg;
2321 TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2323 if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2324 (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2325 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2326 ret = 0;
2327 } else {
2328 msg.hwnd = HWND_32(HIWORD(data));
2329 while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2330 ret = -1;
2332 return ret;
2335 /**************************************************************************
2336 * mciSetYieldProc [WINMM.@]
2338 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2340 LPWINE_MCIDRIVER wmd;
2342 TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2344 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2345 WARN("Bad uDeviceID\n");
2346 return FALSE;
2349 wmd->lpfnYieldProc = fpYieldProc;
2350 wmd->dwYieldData = dwYieldData;
2352 return TRUE;
2355 /**************************************************************************
2356 * mciGetDeviceIDFromElementIDA [WINMM.@]
2358 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2360 LPWSTR w = MCI_strdupAtoW(lpstrType);
2361 UINT ret = 0;
2363 if (w)
2365 ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2366 HeapFree(GetProcessHeap(), 0, w);
2368 return ret;
2371 /**************************************************************************
2372 * mciGetDeviceIDFromElementIDW [WINMM.@]
2374 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2376 /* FIXME: that's rather strange, there is no
2377 * mciGetDeviceIDFromElementID32A in winmm.spec
2379 FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2380 return 0;
2383 /**************************************************************************
2384 * mciGetYieldProc [WINMM.@]
2386 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2388 LPWINE_MCIDRIVER wmd;
2390 TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2392 if (!(wmd = MCI_GetDriver(uDeviceID))) {
2393 WARN("Bad uDeviceID\n");
2394 return NULL;
2396 if (!wmd->lpfnYieldProc) {
2397 WARN("No proc set\n");
2398 return NULL;
2400 if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2401 return wmd->lpfnYieldProc;
2404 /**************************************************************************
2405 * mciGetCreatorTask [WINMM.@]
2407 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2409 LPWINE_MCIDRIVER wmd;
2410 HTASK ret = 0;
2412 if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2414 TRACE("(%u) => %p\n", uDeviceID, ret);
2415 return ret;
2418 /**************************************************************************
2419 * mciDriverYield [WINMM.@]
2421 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2423 LPWINE_MCIDRIVER wmd;
2424 UINT ret = 0;
2426 TRACE("(%04x)\n", uDeviceID);
2428 if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2429 MSG msg;
2430 PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2431 } else {
2432 ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2435 return ret;