winmm: Improve MCI's Sysinfo command.
[wine] / dlls / winmm / mci.c
1 /*
2  * MCI internal functions
3  *
4  * Copyright 1998/1999 Eric Pouech
5  *
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.
10  *
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.
15  *
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
19  */
20
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
38  */
39
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)
43  */
44
45 #include "config.h"
46 #include "wine/port.h"
47
48 #include <stdlib.h>
49 #include <stdarg.h>
50 #include <stdio.h>
51 #include <string.h>
52
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"
61
62 #include "digitalv.h"
63 #include "winemm.h"
64
65 #include "wine/debug.h"
66 #include "wine/unicode.h"
67
68 WINE_DEFAULT_DEBUG_CHANNEL(mci);
69
70 /* First MCI valid device ID (0 means error) */
71 #define MCI_MAGIC 0x0001
72
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};
80
81 static WINE_MCIDRIVER *MciDrivers;
82
83 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data);
84 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType);
85
86 /* dup a string and uppercase it */
87 static inline LPWSTR str_dup_upper( LPCWSTR str )
88 {
89     INT len = (strlenW(str) + 1) * sizeof(WCHAR);
90     LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
91     if (p)
92     {
93         memcpy( p, str, len );
94         CharUpperW( p );
95     }
96     return p;
97 }
98
99 /**************************************************************************
100  *                              MCI_GetDriver                   [internal]
101  */
102 static LPWINE_MCIDRIVER MCI_GetDriver(UINT wDevID)
103 {
104     LPWINE_MCIDRIVER    wmd = 0;
105
106     EnterCriticalSection(&WINMM_cs);
107     for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
108         if (wmd->wDeviceID == wDevID)
109             break;
110     }
111     LeaveCriticalSection(&WINMM_cs);
112     return wmd;
113 }
114
115 /**************************************************************************
116  *                              MCI_GetDriverFromString         [internal]
117  */
118 static UINT MCI_GetDriverFromString(LPCWSTR lpstrName)
119 {
120     LPWINE_MCIDRIVER    wmd;
121     UINT                ret = 0;
122
123     if (!lpstrName)
124         return 0;
125
126     if (!strcmpiW(lpstrName, wszAll))
127         return MCI_ALL_DEVICE_ID;
128
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;
134         }
135         if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
136             ret = wmd->wDeviceID;
137             break;
138         }
139         if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
140             ret = wmd->wDeviceID;
141             break;
142         }
143     }
144     LeaveCriticalSection(&WINMM_cs);
145
146     return ret;
147 }
148
149 /**************************************************************************
150  *                      MCI_MessageToString                     [internal]
151  */
152 const char* MCI_MessageToString(UINT wMsg)
153 {
154 #define CASE(s) case (s): return #s
155
156     switch (wMsg) {
157         CASE(DRV_LOAD);
158         CASE(DRV_ENABLE);
159         CASE(DRV_OPEN);
160         CASE(DRV_CLOSE);
161         CASE(DRV_DISABLE);
162         CASE(DRV_FREE);
163         CASE(DRV_CONFIGURE);
164         CASE(DRV_QUERYCONFIGURE);
165         CASE(DRV_INSTALL);
166         CASE(DRV_REMOVE);
167         CASE(DRV_EXITSESSION);
168         CASE(DRV_EXITAPPLICATION);
169         CASE(DRV_POWER);
170         CASE(MCI_BREAK);
171         CASE(MCI_CLOSE);
172         CASE(MCI_CLOSE_DRIVER);
173         CASE(MCI_COPY);
174         CASE(MCI_CUE);
175         CASE(MCI_CUT);
176         CASE(MCI_DELETE);
177         CASE(MCI_ESCAPE);
178         CASE(MCI_FREEZE);
179         CASE(MCI_PAUSE);
180         CASE(MCI_PLAY);
181         CASE(MCI_GETDEVCAPS);
182         CASE(MCI_INFO);
183         CASE(MCI_LOAD);
184         CASE(MCI_OPEN);
185         CASE(MCI_OPEN_DRIVER);
186         CASE(MCI_PASTE);
187         CASE(MCI_PUT);
188         CASE(MCI_REALIZE);
189         CASE(MCI_RECORD);
190         CASE(MCI_RESUME);
191         CASE(MCI_SAVE);
192         CASE(MCI_SEEK);
193         CASE(MCI_SET);
194         CASE(MCI_SPIN);
195         CASE(MCI_STATUS);
196         CASE(MCI_STEP);
197         CASE(MCI_STOP);
198         CASE(MCI_SYSINFO);
199         CASE(MCI_UNFREEZE);
200         CASE(MCI_UPDATE);
201         CASE(MCI_WHERE);
202         CASE(MCI_WINDOW);
203         /* constants for digital video */
204         CASE(MCI_CAPTURE);
205         CASE(MCI_MONITOR);
206         CASE(MCI_RESERVE);
207         CASE(MCI_SETAUDIO);
208         CASE(MCI_SIGNAL);
209         CASE(MCI_SETVIDEO);
210         CASE(MCI_QUALITY);
211         CASE(MCI_LIST);
212         CASE(MCI_UNDO);
213         CASE(MCI_CONFIGURE);
214         CASE(MCI_RESTORE);
215 #undef CASE
216     default:
217         return wine_dbg_sprintf("MCI_<<%04X>>", wMsg);
218     }
219 }
220
221 LPWSTR MCI_strdupAtoW( LPCSTR str )
222 {
223     LPWSTR ret;
224     INT len;
225
226     if (!str) return NULL;
227     len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
228     ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
229     if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
230     return ret;
231 }
232
233 LPSTR MCI_strdupWtoA( LPCWSTR str )
234 {
235     LPSTR ret;
236     INT len;
237
238     if (!str) return NULL;
239     len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
240     ret = HeapAlloc( GetProcessHeap(), 0, len );
241     if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
242     return ret;
243 }
244
245 static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
246 {
247     if (msg < DRV_RESERVED) return 0;
248
249     switch (msg)
250     {
251     case MCI_CLOSE:
252     case MCI_CONFIGURE:
253     case MCI_PLAY:
254     case MCI_SEEK:
255     case MCI_STOP:
256     case MCI_PAUSE:
257     case MCI_GETDEVCAPS:
258     case MCI_SPIN:
259     case MCI_SET:
260     case MCI_STEP:
261     case MCI_RECORD:
262     case MCI_BREAK:
263     case MCI_SOUND:
264     case MCI_STATUS:
265     case MCI_CUE:
266     case MCI_REALIZE:
267     case MCI_PUT:
268     case MCI_WHERE:
269     case MCI_FREEZE:
270     case MCI_UNFREEZE:
271     case MCI_CUT:
272     case MCI_COPY:
273     case MCI_PASTE:
274     case MCI_UPDATE:
275     case MCI_RESUME:
276     case MCI_DELETE:
277     case MCI_MONITOR:
278     case MCI_SETAUDIO:
279     case MCI_SIGNAL:
280     case MCI_SETVIDEO:
281     case MCI_LIST:
282         return 0;
283
284     case MCI_OPEN:
285         {
286             MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA*)*dwParam2;
287             MCI_OPEN_PARMSW *mci_openW;
288             DWORD_PTR *ptr;
289
290             ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW) + 2 * sizeof(DWORD));
291             if (!ptr) return -1;
292
293             *ptr++ = *dwParam2; /* save the previous pointer */
294             *dwParam2 = (DWORD_PTR)ptr;
295             mci_openW = (MCI_OPEN_PARMSW *)ptr;
296
297             if (dwParam1 & MCI_NOTIFY)
298                 mci_openW->dwCallback = mci_openA->dwCallback;
299
300             if (dwParam1 & MCI_OPEN_TYPE)
301             {
302                 if (dwParam1 & MCI_OPEN_TYPE_ID)
303                     mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
304                 else
305                     mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
306             }
307             if (dwParam1 & MCI_OPEN_ELEMENT)
308             {
309                 if (dwParam1 & MCI_OPEN_ELEMENT_ID)
310                     mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
311                 else
312                     mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
313             }
314             if (dwParam1 & MCI_OPEN_ALIAS)
315                 mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
316             /* FIXME: this is only needed for specific types of MCI devices, and
317              * may cause a segfault if the two DWORD:s don't exist at the end of 
318              * mci_openA
319              */
320             memcpy(mci_openW + 1, mci_openA + 1, 2 * sizeof(DWORD));
321         }
322         return 1;
323
324     case MCI_WINDOW:
325         if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
326         {
327             MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
328             MCI_ANIM_WINDOW_PARMSW *mci_windowW;
329
330             mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
331             if (!mci_windowW) return -1;
332
333             *dwParam2 = (DWORD_PTR)mci_windowW;
334
335             mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);
336
337             if (dwParam1 & MCI_NOTIFY)
338                 mci_windowW->dwCallback = mci_windowA->dwCallback;
339             if (dwParam1 & MCI_ANIM_WINDOW_HWND)
340                 mci_windowW->hWnd = mci_windowA->hWnd;
341             if (dwParam1 & MCI_ANIM_WINDOW_STATE)
342                 mci_windowW->nCmdShow = mci_windowA->nCmdShow;
343
344             return 1;
345         }
346         return 0;
347
348     case MCI_SYSINFO:
349         {
350             MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
351             MCI_SYSINFO_PARMSW *mci_sysinfoW;
352             DWORD_PTR *ptr;
353
354             ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
355             if (!ptr) return -1;
356
357             *ptr++ = *dwParam2; /* save the previous pointer */
358             *dwParam2 = (DWORD_PTR)ptr;
359             mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;
360
361             if (dwParam1 & MCI_NOTIFY)
362                 mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;
363
364             mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
365             mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize);
366             mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
367             mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
368             return 1;
369         }
370     case MCI_INFO:
371         {
372             MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
373             MCI_INFO_PARMSW *mci_infoW;
374             DWORD_PTR *ptr;
375
376             ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
377             if (!ptr) return -1;
378
379             *ptr++ = *dwParam2; /* save the previous pointer */
380             *dwParam2 = (DWORD_PTR)ptr;
381             mci_infoW = (MCI_INFO_PARMSW *)ptr;
382
383             if (dwParam1 & MCI_NOTIFY)
384                 mci_infoW->dwCallback = mci_infoA->dwCallback;
385
386             mci_infoW->dwRetSize = mci_infoA->dwRetSize * sizeof(WCHAR); /* it's not the same as SYSINFO !!! */
387             mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize);
388             return 1;
389         }
390     case MCI_SAVE:
391         {
392             MCI_SAVE_PARMSA *mci_saveA = (MCI_SAVE_PARMSA *)*dwParam2;
393             MCI_SAVE_PARMSW *mci_saveW;
394
395             mci_saveW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_saveW));
396             if (!mci_saveW) return -1;
397
398             *dwParam2 = (DWORD_PTR)mci_saveW;
399             if (dwParam1 & MCI_NOTIFY)
400                 mci_saveW->dwCallback = mci_saveA->dwCallback;
401             mci_saveW->lpfilename = MCI_strdupAtoW(mci_saveA->lpfilename);
402             return 1;
403         }
404     case MCI_LOAD:
405         {
406             MCI_LOAD_PARMSA *mci_loadA = (MCI_LOAD_PARMSA *)*dwParam2;
407             MCI_LOAD_PARMSW *mci_loadW;
408
409             mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
410             if (!mci_loadW) return -1;
411
412             *dwParam2 = (DWORD_PTR)mci_loadW;
413             if (dwParam1 & MCI_NOTIFY)
414                 mci_loadW->dwCallback = mci_loadA->dwCallback;
415             mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
416             return 1;
417         }
418
419     case MCI_ESCAPE:
420         {
421             MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
422             MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;
423
424             mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
425             if (!mci_vd_escapeW) return -1;
426
427             *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
428             if (dwParam1 & MCI_NOTIFY)
429                 mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
430             mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
431             return 1;
432         }
433     default:
434         FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
435         return -1;
436     }
437 }
438
439 static DWORD MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
440                               DWORD result)
441 {
442     switch (msg)
443     {
444     case MCI_OPEN:
445         {
446             DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
447             MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
448             MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)(ptr + 1);
449
450             mci_openA->wDeviceID = mci_openW->wDeviceID;
451
452             if (dwParam1 & MCI_OPEN_TYPE)
453             {
454                 if (!(dwParam1 & MCI_OPEN_TYPE_ID))
455                     HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
456             }
457             if (dwParam1 & MCI_OPEN_ELEMENT)
458             {
459                 if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
460                     HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
461             }
462             if (dwParam1 & MCI_OPEN_ALIAS)
463                 HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
464             HeapFree(GetProcessHeap(), 0, ptr);
465         }
466         break;
467     case MCI_WINDOW:
468         if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
469         {
470             MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;
471
472             HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
473             HeapFree(GetProcessHeap(), 0, mci_windowW);
474         }
475         break;
476
477     case MCI_SYSINFO:
478         {
479             DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
480             MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
481             MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)(ptr + 1);
482
483             if (!result)
484             {
485                 mci_sysinfoA->dwNumber = mci_sysinfoW->dwNumber;
486                 mci_sysinfoA->wDeviceType = mci_sysinfoW->wDeviceType;
487                 if (dwParam1 & MCI_SYSINFO_QUANTITY)
488                     *(DWORD*)mci_sysinfoA->lpstrReturn = *(DWORD*)mci_sysinfoW->lpstrReturn;
489                 else
490                     WideCharToMultiByte(CP_ACP, 0,
491                                         mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
492                                         mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
493                                         NULL, NULL);
494             }
495
496             HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
497             HeapFree(GetProcessHeap(), 0, ptr);
498         }
499         break;
500     case MCI_INFO:
501         {
502             DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
503             MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
504             MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)(ptr + 1);
505
506             if (!result)
507             {
508                 WideCharToMultiByte(CP_ACP, 0,
509                                     mci_infoW->lpstrReturn, mci_infoW->dwRetSize / sizeof(WCHAR),
510                                     mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
511                                     NULL, NULL);
512             }
513
514             HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
515             HeapFree(GetProcessHeap(), 0, ptr);
516         }
517         break;
518     case MCI_SAVE:
519         {
520             MCI_SAVE_PARMSW *mci_saveW = (MCI_SAVE_PARMSW *)dwParam2;
521
522             HeapFree(GetProcessHeap(), 0, (void*)mci_saveW->lpfilename);
523             HeapFree(GetProcessHeap(), 0, mci_saveW);
524         }
525         break;
526     case MCI_LOAD:
527         {
528             MCI_LOAD_PARMSW *mci_loadW = (MCI_LOAD_PARMSW *)dwParam2;
529
530             HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
531             HeapFree(GetProcessHeap(), 0, mci_loadW);
532         }
533         break;
534     case MCI_ESCAPE:
535         {
536             MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;
537
538             HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
539             HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
540         }
541         break;
542
543     default:
544         FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
545         break;
546     }
547
548     return result;
549 }
550
551 /**************************************************************************
552  *                              MCI_GetDevTypeFromFileName      [internal]
553  */
554 static  DWORD   MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
555 {
556     LPCWSTR     tmp;
557     HKEY        hKey;
558     static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
559                                  'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
560                                  'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
561     if ((tmp = strrchrW(fileName, '.'))) {
562         if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
563                            0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
564             DWORD dwLen = len;
565             LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen ); 
566             RegCloseKey( hKey );
567             if (lRet == ERROR_SUCCESS) return 0;
568         }
569         TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
570     }
571     return MCIERR_EXTENSION_NOT_FOUND;
572 }
573
574 #define MAX_MCICMDTABLE                 20
575 #define MCI_COMMAND_TABLE_NOT_LOADED    0xFFFE
576
577 typedef struct tagWINE_MCICMDTABLE {
578     UINT                uDevType;
579     HGLOBAL             hMem;
580     const BYTE*         lpTable;
581     UINT                nVerbs;         /* number of verbs in command table */
582     LPCWSTR*            aVerbs;         /* array of verbs to speed up the verb look up process */
583 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
584
585 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
586
587 /**************************************************************************
588  *                              MCI_IsCommandTableValid         [internal]
589  */
590 static  BOOL            MCI_IsCommandTableValid(UINT uTbl)
591 {
592     const BYTE* lmem;
593     LPCWSTR     str;
594     DWORD       flg;
595     WORD        eid;
596     int         idx = 0;
597     BOOL        inCst = FALSE;
598
599     TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
600           uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
601
602     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
603         return FALSE;
604
605     lmem = S_MciCmdTable[uTbl].lpTable;
606     do {
607         str = (LPCWSTR)lmem;
608         lmem += (strlenW(str) + 1) * sizeof(WCHAR);
609         flg = *(const DWORD*)lmem;
610         eid = *(const WORD*)(lmem + sizeof(DWORD));
611         lmem += sizeof(DWORD) + sizeof(WORD);
612         idx ++;
613         /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
614         switch (eid) {
615         case MCI_COMMAND_HEAD:          if (!*str || !flg) return FALSE; idx = 0;               break;  /* check unicity of str in table */
616         case MCI_STRING:                if (inCst) return FALSE;                                break;
617         case MCI_INTEGER:               if (!*str) return FALSE;                                break;
618         case MCI_END_COMMAND:           if (*str || flg || idx == 0) return FALSE; idx = 0;     break;
619         case MCI_RETURN:                if (*str || idx != 1) return FALSE;                     break;
620         case MCI_FLAG:                  if (!*str) return FALSE;                                break;
621         case MCI_END_COMMAND_LIST:      if (*str || flg) return FALSE;  idx = 0;                break;
622         case MCI_RECT:                  if (!*str || inCst) return FALSE;                       break;
623         case MCI_CONSTANT:              if (inCst) return FALSE; inCst = TRUE;                  break;
624         case MCI_END_CONSTANT:          if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
625         default:                        return FALSE;
626         }
627     } while (eid != MCI_END_COMMAND_LIST);
628     return TRUE;
629 }
630
631 /**************************************************************************
632  *                              MCI_DumpCommandTable            [internal]
633  */
634 static  BOOL            MCI_DumpCommandTable(UINT uTbl)
635 {
636     const BYTE* lmem;
637     LPCWSTR     str;
638     DWORD       flg;
639     WORD        eid;
640
641     if (!MCI_IsCommandTableValid(uTbl)) {
642         ERR("Ooops: %d is not valid\n", uTbl);
643         return FALSE;
644     }
645
646     lmem = S_MciCmdTable[uTbl].lpTable;
647     do {
648         do {
649             str = (LPCWSTR)lmem;
650             lmem += (strlenW(str) + 1) * sizeof(WCHAR);
651             flg = *(const DWORD*)lmem;
652             eid = *(const WORD*)(lmem + sizeof(DWORD));
653             /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
654             lmem += sizeof(DWORD) + sizeof(WORD);
655         } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
656         /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
657     } while (eid != MCI_END_COMMAND_LIST);
658     return TRUE;
659 }
660
661
662 /**************************************************************************
663  *                              MCI_GetCommandTable             [internal]
664  */
665 static  UINT            MCI_GetCommandTable(UINT uDevType)
666 {
667     UINT        uTbl;
668     WCHAR       buf[32];
669     LPCWSTR     str = NULL;
670
671     /* first look up existing for existing devType */
672     for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
673         if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
674             return uTbl;
675     }
676
677     /* well try to load id */
678     if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
679         if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
680             str = buf;
681         }
682     } else if (uDevType == 0) {
683         static const WCHAR wszCore[] = {'C','O','R','E',0};
684         str = wszCore;
685     }
686     uTbl = MCI_NO_COMMAND_TABLE;
687     if (str) {
688         HRSRC   hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
689         HANDLE  hMem = 0;
690
691         if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
692         if (hMem) {
693             uTbl = MCI_SetCommandTable(hMem, uDevType);
694         } else {
695             WARN("No command table found in resource %p[%s]\n",
696                  hWinMM32Instance, debugstr_w(str));
697         }
698     }
699     TRACE("=> %d\n", uTbl);
700     return uTbl;
701 }
702
703 /**************************************************************************
704  *                              MCI_SetCommandTable             [internal]
705  */
706 static UINT MCI_SetCommandTable(HGLOBAL hMem, UINT uDevType)
707 {
708     int                 uTbl;
709     static      BOOL    bInitDone = FALSE;
710
711     /* <HACK>
712      * The CORE command table must be loaded first, so that MCI_GetCommandTable()
713      * can be called with 0 as a uDevType to retrieve it.
714      * </HACK>
715      */
716     if (!bInitDone) {
717         bInitDone = TRUE;
718         MCI_GetCommandTable(0);
719     }
720     TRACE("(%p, %u)\n", hMem, uDevType);
721     for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
722         if (!S_MciCmdTable[uTbl].lpTable) {
723             const BYTE* lmem;
724             LPCWSTR     str;
725             WORD        eid;
726             WORD        count;
727
728             S_MciCmdTable[uTbl].uDevType = uDevType;
729             S_MciCmdTable[uTbl].lpTable = LockResource(hMem);
730             S_MciCmdTable[uTbl].hMem = hMem;
731
732             if (TRACE_ON(mci)) {
733                 MCI_DumpCommandTable(uTbl);
734             }
735
736             /* create the verbs table */
737             /* get # of entries */
738             lmem = S_MciCmdTable[uTbl].lpTable;
739             count = 0;
740             do {
741                 str = (LPCWSTR)lmem;
742                 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
743                 eid = *(const WORD*)(lmem + sizeof(DWORD));
744                 lmem += sizeof(DWORD) + sizeof(WORD);
745                 if (eid == MCI_COMMAND_HEAD)
746                     count++;
747             } while (eid != MCI_END_COMMAND_LIST);
748
749             S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
750             S_MciCmdTable[uTbl].nVerbs = count;
751
752             lmem = S_MciCmdTable[uTbl].lpTable;
753             count = 0;
754             do {
755                 str = (LPCWSTR)lmem;
756                 lmem += (strlenW(str) + 1) * sizeof(WCHAR);
757                 eid = *(const WORD*)(lmem + sizeof(DWORD));
758                 lmem += sizeof(DWORD) + sizeof(WORD);
759                 if (eid == MCI_COMMAND_HEAD)
760                     S_MciCmdTable[uTbl].aVerbs[count++] = str;
761             } while (eid != MCI_END_COMMAND_LIST);
762             /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
763             return uTbl;
764         }
765     }
766
767     return MCI_NO_COMMAND_TABLE;
768 }
769
770 /**************************************************************************
771  *                              MCI_UnLoadMciDriver             [internal]
772  */
773 static  BOOL    MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
774 {
775     LPWINE_MCIDRIVER*           tmp;
776
777     if (!wmd)
778         return TRUE;
779
780     CloseDriver(wmd->hDriver, 0, 0);
781
782     if (wmd->dwPrivate != 0)
783         WARN("Unloading mci driver with non nul dwPrivate field\n");
784
785     EnterCriticalSection(&WINMM_cs);
786     for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
787         if (*tmp == wmd) {
788             *tmp = wmd->lpNext;
789             break;
790         }
791     }
792     LeaveCriticalSection(&WINMM_cs);
793
794     HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
795     HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
796     HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
797
798     HeapFree(GetProcessHeap(), 0, wmd);
799     return TRUE;
800 }
801
802 /**************************************************************************
803  *                              MCI_OpenMciDriver               [internal]
804  */
805 static  BOOL    MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
806 {
807     WCHAR       libName[128];
808
809     if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
810         return FALSE;
811
812     /* First load driver */
813     wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp);
814     return wmd->hDriver != NULL;
815 }
816
817 /**************************************************************************
818  *                              MCI_LoadMciDriver               [internal]
819  */
820 static  DWORD   MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
821 {
822     LPWSTR                      strDevTyp = str_dup_upper(_strDevTyp);
823     LPWINE_MCIDRIVER            wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
824     MCI_OPEN_DRIVER_PARMSW      modp;
825     DWORD                       dwRet = 0;
826
827     if (!wmd || !strDevTyp) {
828         dwRet = MCIERR_OUT_OF_MEMORY;
829         goto errCleanUp;
830     }
831
832     wmd->lpfnYieldProc = MCI_DefYieldProc;
833     wmd->dwYieldData = VK_CANCEL;
834     wmd->CreatorThread = GetCurrentThreadId();
835
836     EnterCriticalSection(&WINMM_cs);
837     /* wmd must be inserted in list before sending opening the driver, because it
838      * may want to lookup at wDevID
839      */
840     wmd->lpNext = MciDrivers;
841     MciDrivers = wmd;
842
843     for (modp.wDeviceID = MCI_MAGIC;
844          MCI_GetDriver(modp.wDeviceID) != 0;
845          modp.wDeviceID++);
846
847     wmd->wDeviceID = modp.wDeviceID;
848
849     LeaveCriticalSection(&WINMM_cs);
850
851     TRACE("wDevID=%04X\n", modp.wDeviceID);
852
853     modp.lpstrParams = NULL;
854
855     if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
856         /* silence warning if all is used... some bogus program use commands like
857          * 'open all'...
858          */
859         if (strcmpiW(strDevTyp, wszAll) == 0) {
860             dwRet = MCIERR_CANNOT_USE_ALL;
861         } else {
862             FIXME("Couldn't load driver for type %s.\n",
863                   debugstr_w(strDevTyp));
864             dwRet = MCIERR_DEVICE_NOT_INSTALLED;
865         }
866         goto errCleanUp;
867     }
868
869     /* FIXME: should also check that module's description is of the form
870      * MODULENAME:[MCI] comment
871      */
872
873     /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
874     wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
875     wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
876
877     TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
878           wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
879
880     wmd->lpstrDeviceType = strDevTyp;
881     wmd->wType = modp.wType;
882
883     TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
884           modp.wDeviceID, modp.wType, modp.wDeviceID);
885     *lpwmd = wmd;
886     return 0;
887 errCleanUp:
888     MCI_UnLoadMciDriver(wmd);
889     HeapFree(GetProcessHeap(), 0, strDevTyp);
890     *lpwmd = 0;
891     return dwRet;
892 }
893
894 /**************************************************************************
895  *                      MCI_SendCommandFrom32                   [internal]
896  */
897 static DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
898 {
899     DWORD               dwRet = MCIERR_INVALID_DEVICE_ID;
900     LPWINE_MCIDRIVER    wmd = MCI_GetDriver(wDevID);
901
902     if (wmd) {
903         dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
904     }
905     return dwRet;
906 }
907
908 /**************************************************************************
909  *                      MCI_FinishOpen                          [internal]
910  */
911 static  DWORD   MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
912                                DWORD dwParam)
913 {
914     if (dwParam & MCI_OPEN_ELEMENT)
915     {
916         wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
917         strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
918     }
919     if (dwParam & MCI_OPEN_ALIAS)
920     {
921         wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
922         strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
923     }
924     lpParms->wDeviceID = wmd->wDeviceID;
925
926     return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
927                                  (DWORD_PTR)lpParms);
928 }
929
930 /**************************************************************************
931  *                              MCI_FindCommand         [internal]
932  */
933 static  LPCWSTR         MCI_FindCommand(UINT uTbl, LPCWSTR verb)
934 {
935     UINT        idx;
936
937     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
938         return NULL;
939
940     /* another improvement would be to have the aVerbs array sorted,
941      * so that we could use a dichotomic search on it, rather than this dumb
942      * array look up
943      */
944     for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
945         if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
946             return S_MciCmdTable[uTbl].aVerbs[idx];
947     }
948
949     return NULL;
950 }
951
952 /**************************************************************************
953  *                              MCI_GetReturnType               [internal]
954  */
955 static  DWORD           MCI_GetReturnType(LPCWSTR lpCmd)
956 {
957     lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
958     if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
959         return *(const DWORD*)(lpCmd + 1);
960     }
961     return 0L;
962 }
963
964 /**************************************************************************
965  *                              MCI_GetMessage                  [internal]
966  */
967 static  WORD            MCI_GetMessage(LPCWSTR lpCmd)
968 {
969     return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
970 }
971
972 /**************************************************************************
973  *                              MCI_GetDWord                    [internal]
974  */
975 static  BOOL            MCI_GetDWord(DWORD_PTR* data, LPWSTR* ptr)
976 {
977     DWORD       val;
978     LPWSTR      ret;
979
980     val = strtoulW(*ptr, &ret, 0);
981
982     switch (*ret) {
983     case '\0':  break;
984     case ' ':   ret++; break;
985     default:    return FALSE;
986     }
987
988     *data |= val;
989     *ptr = ret;
990     return TRUE;
991 }
992
993 /**************************************************************************
994  *                              MCI_GetString           [internal]
995  */
996 static  DWORD   MCI_GetString(LPWSTR* str, LPWSTR* args)
997 {
998     LPWSTR      ptr = *args;
999
1000     /* see if we have a quoted string */
1001     if (*ptr == '"') {
1002         ptr = strchrW(*str = ptr + 1, '"');
1003         if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
1004         /* FIXME: shall we escape \" from string ?? */
1005         if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
1006         *ptr++ = '\0'; /* remove trailing " */
1007         if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
1008     } else {
1009         ptr = strchrW(ptr, ' ');
1010
1011         if (ptr) {
1012             *ptr++ = '\0';
1013         } else {
1014             ptr = *args + strlenW(*args);
1015         }
1016         *str = *args;
1017     }
1018
1019     *args = ptr;
1020     return 0;
1021 }
1022
1023 #define MCI_DATA_SIZE   16
1024
1025 /**************************************************************************
1026  *                              MCI_ParseOptArgs                [internal]
1027  */
1028 static  DWORD   MCI_ParseOptArgs(DWORD_PTR* data, int _offset, LPCWSTR lpCmd,
1029                                  LPWSTR args, LPDWORD dwFlags)
1030 {
1031     int         len, offset;
1032     const char* lmem;
1033     LPCWSTR     str;
1034     DWORD       dwRet, flg, cflg = 0;
1035     WORD        eid;
1036     BOOL        inCst, found;
1037
1038     /* loop on arguments */
1039     while (*args) {
1040         lmem = (const char*)lpCmd;
1041         found = inCst = FALSE;
1042         offset = _offset;
1043
1044         /* skip any leading white space(s) */
1045         while (*args == ' ') args++;
1046         TRACE("args=%s offset=%d\n", debugstr_w(args), offset);
1047
1048         do { /* loop on options for command table for the requested verb */
1049             str = (LPCWSTR)lmem;
1050             lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
1051             flg = *(const DWORD*)lmem;
1052             eid = *(const WORD*)(lmem + sizeof(DWORD));
1053             lmem += sizeof(DWORD) + sizeof(WORD);
1054             /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1055
1056             switch (eid) {
1057             case MCI_CONSTANT:
1058                 inCst = TRUE;   cflg = flg;     break;
1059             case MCI_END_CONSTANT:
1060                 /* there may be additional integral values after flag in constant */
1061                 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
1062                     *dwFlags |= cflg;
1063                 }
1064                 inCst = FALSE;  cflg = 0;
1065                 break;
1066             }
1067
1068             if (strncmpiW(args, str, len) == 0 &&
1069                 ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1070                 /* store good values into data[] */
1071                 args += len;
1072                 while (*args == ' ') args++;
1073                 found = TRUE;
1074
1075                 switch (eid) {
1076                 case MCI_COMMAND_HEAD:
1077                 case MCI_RETURN:
1078                 case MCI_END_COMMAND:
1079                 case MCI_END_COMMAND_LIST:
1080                 case MCI_CONSTANT:      /* done above */
1081                 case MCI_END_CONSTANT:  /* done above */
1082                     break;
1083                 case MCI_FLAG:
1084                     *dwFlags |= flg;
1085                     break;
1086                 case MCI_INTEGER:
1087                     if (inCst) {
1088                         data[offset] |= flg;
1089                         *dwFlags |= cflg;
1090                         inCst = FALSE;
1091                     } else {
1092                         *dwFlags |= flg;
1093                         if (!MCI_GetDWord(&(data[offset]), &args)) {
1094                             return MCIERR_BAD_INTEGER;
1095                         }
1096                     }
1097                     break;
1098                 case MCI_RECT:
1099                     /* store rect in data (offset..offset+3) */
1100                     *dwFlags |= flg;
1101                     if (!MCI_GetDWord(&(data[offset+0]), &args) ||
1102                         !MCI_GetDWord(&(data[offset+1]), &args) ||
1103                         !MCI_GetDWord(&(data[offset+2]), &args) ||
1104                         !MCI_GetDWord(&(data[offset+3]), &args)) {
1105                         ERR("Bad rect %s\n", debugstr_w(args));
1106                         return MCIERR_BAD_INTEGER;
1107                     }
1108                     break;
1109                 case MCI_STRING:
1110                     *dwFlags |= flg;
1111                     if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1112                         return dwRet;
1113                     break;
1114                 default:        ERR("oops\n");
1115                 }
1116                 /* exit inside while loop, except if just entered in constant area definition */
1117                 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1118             } else {
1119                 /* have offset incremented if needed */
1120                 switch (eid) {
1121                 case MCI_COMMAND_HEAD:
1122                 case MCI_RETURN:
1123                 case MCI_END_COMMAND:
1124                 case MCI_END_COMMAND_LIST:
1125                 case MCI_CONSTANT:
1126                 case MCI_FLAG:                  break;
1127                 case MCI_INTEGER:               if (!inCst) offset++;   break;
1128                 case MCI_END_CONSTANT:
1129                 case MCI_STRING:                offset++; break;
1130                 case MCI_RECT:                  offset += 4; break;
1131                 default:                        ERR("oops\n");
1132                 }
1133             }
1134         } while (eid != MCI_END_COMMAND);
1135         if (!found) {
1136             WARN("Optarg %s not found\n", debugstr_w(args));
1137             return MCIERR_UNRECOGNIZED_COMMAND;
1138         }
1139         if (offset == MCI_DATA_SIZE) {
1140             ERR("Internal data[] buffer overflow\n");
1141             return MCIERR_PARSER_INTERNAL;
1142         }
1143     }
1144     return 0;
1145 }
1146
1147 /**************************************************************************
1148  *                              MCI_HandleReturnValues  [internal]
1149  */
1150 static  DWORD   MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
1151                                        DWORD_PTR* data, LPWSTR lpstrRet, UINT uRetLen)
1152 {
1153     static const WCHAR wszLd  [] = {'%','l','d',0};
1154     static const WCHAR wszLd4 [] = {'%','l','d',' ','%','l','d',' ','%','l','d',' ','%','l','d',0};
1155     static const WCHAR wszCol3[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1156     static const WCHAR wszCol4[] = {'%','0','2','d',':','%','0','2','d',':','%','0','2','d',':','%','0','2','d',0};
1157
1158     if (lpstrRet) {
1159         switch (retType) {
1160         case 0: /* nothing to return */
1161             break;
1162         case MCI_INTEGER:
1163             switch (dwRet & 0xFFFF0000ul) {
1164             case 0:
1165             case MCI_INTEGER_RETURNED:
1166                 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1167                 break;
1168             case MCI_RESOURCE_RETURNED:
1169                 /* return string which ID is HIWORD(data[1]),
1170                  * string is loaded from mmsystem.dll */
1171                 LoadStringW(hWinMM32Instance, HIWORD(data[1]), lpstrRet, uRetLen);
1172                 break;
1173             case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1174                 /* return string which ID is HIWORD(data[1]),
1175                  * string is loaded from driver */
1176                 /* FIXME: this is wrong for a 16 bit handle */
1177                 LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1178                             HIWORD(data[1]), lpstrRet, uRetLen);
1179                 break;
1180             case MCI_COLONIZED3_RETURN:
1181                 snprintfW(lpstrRet, uRetLen, wszCol3,
1182                           LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1183                           LOBYTE(HIWORD(data[1])));
1184                 break;
1185             case MCI_COLONIZED4_RETURN:
1186                 snprintfW(lpstrRet, uRetLen, wszCol4,
1187                           LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
1188                           LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
1189                 break;
1190             default:    ERR("Ooops (%04X)\n", HIWORD(dwRet));
1191             }
1192             break;
1193         case MCI_STRING:
1194             switch (dwRet & 0xFFFF0000ul) {
1195             case 0:
1196                 /* nothing to do data[1] == lpstrRet */
1197                 break;
1198             case MCI_INTEGER_RETURNED:
1199                 data[1] = *(LPDWORD)lpstrRet;
1200                 snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1201                 break;
1202             default:
1203                 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1204                 break;
1205             }
1206             break;
1207         case MCI_RECT:
1208             if (dwRet & 0xFFFF0000ul)
1209                 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1210             snprintfW(lpstrRet, uRetLen, wszLd4,
1211                       data[1], data[2], data[3], data[4]);
1212             break;
1213         default:                ERR("oops\n");
1214         }
1215     }
1216     return LOWORD(dwRet);
1217 }
1218
1219 /**************************************************************************
1220  *                              mciSendStringW          [WINMM.@]
1221  */
1222 DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1223                             UINT uRetLen, HWND hwndCallback)
1224 {
1225     LPWSTR              verb, dev, args;
1226     LPWINE_MCIDRIVER    wmd = 0;
1227     MCIDEVICEID         uDevID;
1228     DWORD               dwFlags = 0, dwRet = 0;
1229     int                 offset = 0;
1230     DWORD_PTR           data[MCI_DATA_SIZE];
1231     DWORD               retType;
1232     LPCWSTR             lpCmd = 0;
1233     static const WCHAR  wszNew[] = {'n','e','w',0};
1234     static const WCHAR  wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1235     static const WCHAR  wszTypeS[] = {'t','y','p','e',' ',0};
1236
1237     TRACE("(%s, %p, %d, %p)\n", 
1238           debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1239
1240     /* format is <command> <device> <optargs> */
1241     if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1242         return MCIERR_OUT_OF_MEMORY;
1243     strcpyW( verb, lpstrCommand );
1244     CharLowerW(verb);
1245
1246     memset(data, 0, sizeof(data));
1247
1248     if (!(args = strchrW(verb, ' '))) {
1249         dwRet = MCIERR_MISSING_DEVICE_NAME;
1250         goto errCleanUp;
1251     }
1252     *args++ = '\0';
1253     if ((dwRet = MCI_GetString(&dev, &args))) {
1254         goto errCleanUp;
1255     }
1256     uDevID = strcmpiW(dev, wszAll) ? 0 : MCI_ALL_DEVICE_ID;
1257
1258     /* Determine devType from open */
1259     if (!strcmpW(verb, wszOpen)) {
1260         LPWSTR  devType, tmp;
1261         WCHAR   buf[128];
1262
1263         /* case dev == 'new' has to be handled */
1264         if (!strcmpW(dev, wszNew)) {
1265             dev = 0;
1266             if ((devType = strstrW(args, wszTypeS)) != NULL) {
1267                 devType += 5;
1268                 tmp = strchrW(devType, ' ');
1269                 if (tmp) *tmp = '\0';
1270                 devType = str_dup_upper(devType);
1271                 if (tmp) *tmp = ' ';
1272                 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1273             } else {
1274                 WARN("open new requires device type\n");
1275                 dwRet = MCIERR_MISSING_DEVICE_NAME;
1276                 goto errCleanUp;
1277             }
1278         } else if ((devType = strchrW(dev, '!')) != NULL) {
1279             *devType++ = '\0';
1280             tmp = devType; devType = dev; dev = tmp;
1281
1282             dwFlags |= MCI_OPEN_TYPE;
1283             data[2] = (DWORD_PTR)devType;
1284             devType = str_dup_upper(devType);
1285             dwFlags |= MCI_OPEN_ELEMENT;
1286             data[3] = (DWORD_PTR)dev;
1287         } else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
1288             /* this is the name of a mci driver's type */
1289             tmp = strchrW(dev, ' ');
1290             if (tmp) *tmp = '\0';
1291             data[2] = (DWORD_PTR)dev;
1292             devType = str_dup_upper(dev);
1293             if (tmp) *tmp = ' ';
1294             dwFlags |= MCI_OPEN_TYPE;
1295         } else {
1296             if ((devType = strstrW(args, wszTypeS)) != NULL) {
1297                 devType += 5;
1298                 tmp = strchrW(devType, ' ');
1299                 if (tmp) *tmp = '\0';
1300                 devType = str_dup_upper(devType);
1301                 if (tmp) *tmp = ' ';
1302                 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
1303             } else {
1304                 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
1305                     goto errCleanUp;
1306
1307                 devType = str_dup_upper(buf);
1308             }
1309             dwFlags |= MCI_OPEN_ELEMENT;
1310             data[3] = (DWORD_PTR)dev;
1311         }
1312         if (MCI_ALL_DEVICE_ID == uDevID) {
1313             dwRet = MCIERR_CANNOT_USE_ALL;
1314             goto errCleanUp;
1315         }
1316         if (!strstrW(args, wszSAliasS) && !dev) {
1317             dwRet = MCIERR_NEW_REQUIRES_ALIAS;
1318             goto errCleanUp;
1319         }
1320
1321         dwRet = MCI_LoadMciDriver(devType, &wmd);
1322         if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
1323             dwRet = MCIERR_INVALID_DEVICE_NAME;
1324         HeapFree(GetProcessHeap(), 0, devType);
1325         if (dwRet) {
1326             MCI_UnLoadMciDriver(wmd);
1327             goto errCleanUp;
1328         }
1329     } else if ((MCI_ALL_DEVICE_ID != uDevID) && !(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))) {
1330         /* auto open */
1331         static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1332         WCHAR   buf[128];
1333         sprintfW(buf, wszOpenWait, dev);
1334
1335         if ((dwRet = mciSendStringW(buf, NULL, 0, 0)) != 0)
1336             goto errCleanUp;
1337
1338         wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1339         if (!wmd) {
1340             /* FIXME: memory leak, MCI driver is not closed */
1341             dwRet = MCIERR_INVALID_DEVICE_ID;
1342             goto errCleanUp;
1343         }
1344     }
1345
1346     /* get the verb in the different command tables */
1347     if (wmd) {
1348         /* try the device specific command table */
1349         lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
1350         if (!lpCmd) {
1351             /* try the type specific command table */
1352             if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1353                 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1354             if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
1355                 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
1356         }
1357     }
1358     /* try core command table */
1359     if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1360
1361     if (!lpCmd) {
1362         TRACE("Command %s not found!\n", debugstr_w(verb));
1363         dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1364         goto errCleanUp;
1365     }
1366
1367     /* set return information */
1368     switch (retType = MCI_GetReturnType(lpCmd)) {
1369     case 0:             offset = 1;     break;
1370     case MCI_INTEGER:   offset = 2;     break;
1371     case MCI_STRING:    data[1] = (DWORD_PTR)lpstrRet; data[2] = uRetLen; offset = 3; break;
1372     case MCI_RECT:      offset = 5;     break;
1373     default:    ERR("oops\n");
1374     }
1375
1376     TRACE("verb=%s on dev=%s; offset=%d\n", 
1377           debugstr_w(verb), debugstr_w(dev), offset);
1378
1379     if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1380         goto errCleanUp;
1381
1382     /* set up call back */
1383     if (dwFlags & MCI_NOTIFY) {
1384         data[0] = (DWORD_PTR)hwndCallback;
1385     }
1386
1387     /* FIXME: the command should get it's own notification window set up and
1388      * ask for device closing while processing the notification mechanism
1389      */
1390     if (lpstrRet && uRetLen) *lpstrRet = '\0';
1391
1392     TRACE("[%d, %s, %08x, %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s]\n",
1393           wmd ? wmd->wDeviceID : uDevID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1394           data[0], debugstr_w((WCHAR *)data[0]), data[1], debugstr_w((WCHAR *)data[1]),
1395           data[2], debugstr_w((WCHAR *)data[2]), data[3], debugstr_w((WCHAR *)data[3]),
1396           data[4], debugstr_w((WCHAR *)data[4]), data[5], debugstr_w((WCHAR *)data[5]));
1397
1398     if (strcmpW(verb, wszOpen) == 0) {
1399         if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSW)data, dwFlags)))
1400             MCI_UnLoadMciDriver(wmd);
1401         /* FIXME: notification is not properly shared across two opens */
1402     } else {
1403         dwRet = MCI_SendCommand(wmd ? wmd->wDeviceID : uDevID, MCI_GetMessage(lpCmd), dwFlags, (DWORD_PTR)data);
1404     }
1405     TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1406     dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1407     TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1408
1409 errCleanUp:
1410     HeapFree(GetProcessHeap(), 0, verb);
1411     return dwRet;
1412 }
1413
1414 /**************************************************************************
1415  *                              mciSendStringA                  [WINMM.@]
1416  */
1417 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1418                             UINT uRetLen, HWND hwndCallback)
1419 {
1420     LPWSTR      lpwstrCommand;
1421     LPWSTR      lpwstrRet = NULL;
1422     UINT        ret;
1423     INT len;
1424
1425     /* FIXME: is there something to do with lpstrReturnString ? */
1426     len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
1427     lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
1428     MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
1429     if (lpstrRet)
1430     {
1431         lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1432         if (!lpwstrRet) {
1433             WARN("no memory\n");
1434             HeapFree( GetProcessHeap(), 0, lpwstrCommand );
1435             return MCIERR_OUT_OF_MEMORY;
1436         }
1437     }
1438     ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1439     if (!ret && lpwstrRet)
1440         WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
1441     HeapFree(GetProcessHeap(), 0, lpwstrCommand);
1442     HeapFree(GetProcessHeap(), 0, lpwstrRet);
1443     return ret;
1444 }
1445
1446 /**************************************************************************
1447  *                              mciExecute                      [WINMM.@]
1448  */
1449 BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1450 {
1451     char        strRet[256];
1452     DWORD       ret;
1453
1454     TRACE("(%s)!\n", lpstrCommand);
1455
1456     ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1457     if (ret != 0) {
1458         if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1459             sprintf(strRet, "Unknown MCI error (%d)", ret);
1460         }
1461         MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1462     }
1463     /* FIXME: what shall I return ? */
1464     return TRUE;
1465 }
1466
1467 /**************************************************************************
1468  *                      mciLoadCommandResource                  [WINMM.@]
1469  *
1470  * Strangely, this function only exists as a UNICODE one.
1471  */
1472 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1473 {
1474     UINT        ret = MCI_NO_COMMAND_TABLE;
1475     HRSRC       hRsrc = 0;
1476     HGLOBAL     hMem;
1477
1478     TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1479
1480     /* if a file named "resname.mci" exits, then load resource "resname" from it
1481      * otherwise directly from driver
1482      * We don't support it (who uses this feature ?), but we check anyway
1483      */
1484     if (!type) {
1485 #if 0
1486         /* FIXME: we should put this back into order, but I never found a program
1487          * actually using this feature, so we may not need it
1488          */
1489         char            buf[128];
1490         OFSTRUCT        ofs;
1491
1492         strcat(strcpy(buf, resname), ".mci");
1493         if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1494             FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1495         }
1496 #endif
1497     }
1498     if ((hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA)) &&
1499         (hMem = LoadResource(hInst, hRsrc))) {
1500         ret = MCI_SetCommandTable(hMem, type);
1501         FreeResource(hMem);
1502     }
1503     else WARN("No command table found in module for %s\n", debugstr_w(resNameW));
1504
1505     TRACE("=> %04x\n", ret);
1506     return ret;
1507 }
1508
1509 /**************************************************************************
1510  *                      mciFreeCommandResource                  [WINMM.@]
1511  */
1512 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1513 {
1514     TRACE("(%08x)!\n", uTable);
1515
1516     if (uTable >= MAX_MCICMDTABLE || !S_MciCmdTable[uTable].lpTable)
1517         return FALSE;
1518
1519     FreeResource(S_MciCmdTable[uTable].hMem);
1520     S_MciCmdTable[uTable].hMem = NULL;
1521     S_MciCmdTable[uTable].lpTable = NULL;
1522     HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTable].aVerbs);
1523     S_MciCmdTable[uTable].aVerbs = 0;
1524     S_MciCmdTable[uTable].nVerbs = 0;
1525     return TRUE;
1526 }
1527
1528 /**************************************************************************
1529  *                      MCI_Open                                [internal]
1530  */
1531 static  DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1532 {
1533     WCHAR                       strDevTyp[128];
1534     DWORD                       dwRet;
1535     LPWINE_MCIDRIVER            wmd = NULL;
1536
1537     TRACE("(%08X, %p)\n", dwParam, lpParms);
1538     if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1539
1540     /* only two low bytes are generic, the other ones are dev type specific */
1541 #define WINE_MCIDRIVER_SUPP     (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1542                          MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1543                          MCI_NOTIFY|MCI_WAIT)
1544     if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1545         FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1546     }
1547 #undef WINE_MCIDRIVER_SUPP
1548
1549     strDevTyp[0] = 0;
1550
1551     if (dwParam & MCI_OPEN_TYPE) {
1552         if (dwParam & MCI_OPEN_TYPE_ID) {
1553             WORD uDevType = LOWORD(lpParms->lpstrDeviceType);
1554
1555             if (uDevType < MCI_DEVTYPE_FIRST ||
1556                 uDevType > MCI_DEVTYPE_LAST ||
1557                 !LoadStringW(hWinMM32Instance, uDevType,
1558                              strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1559                 dwRet = MCIERR_BAD_INTEGER;
1560                 goto errCleanUp;
1561             }
1562         } else {
1563             LPWSTR      ptr;
1564             if (lpParms->lpstrDeviceType == NULL) {
1565                 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1566                 goto errCleanUp;
1567             }
1568             strcpyW(strDevTyp, lpParms->lpstrDeviceType);
1569             ptr = strchrW(strDevTyp, '!');
1570             if (ptr) {
1571                 /* this behavior is not documented in windows. However, since, in
1572                  * some occasions, MCI_OPEN handling is translated by WinMM into
1573                  * a call to mciSendString("open <type>"); this code shall be correct
1574                  */
1575                 if (dwParam & MCI_OPEN_ELEMENT) {
1576                     ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1577                         debugstr_w(lpParms->lpstrElementName), 
1578                         debugstr_w(strDevTyp));
1579                     dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1580                     goto errCleanUp;
1581                 }
1582                 dwParam |= MCI_OPEN_ELEMENT;
1583                 *ptr++ = 0;
1584                 /* FIXME: not a good idea to write in user supplied buffer */
1585                 lpParms->lpstrElementName = ptr;
1586             }
1587
1588         }
1589         TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1590     }
1591
1592     if (dwParam & MCI_OPEN_ELEMENT) {
1593         TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1594
1595         if (dwParam & MCI_OPEN_ELEMENT_ID) {
1596             FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1597             dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1598             goto errCleanUp;
1599         }
1600
1601         if (!lpParms->lpstrElementName) {
1602             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1603             goto errCleanUp;
1604         }
1605
1606         /* type, if given as a parameter, supersedes file extension */
1607         if (!strDevTyp[0] &&
1608             MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1609                                        strDevTyp, sizeof(strDevTyp))) {
1610             static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
1611             if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1612                 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1613                 goto errCleanUp;
1614             }
1615             /* FIXME: this will not work if several CDROM drives are installed on the machine */
1616             strcpyW(strDevTyp, wszCdAudio);
1617         }
1618     }
1619
1620     if (strDevTyp[0] == 0) {
1621         FIXME("Couldn't load driver\n");
1622         dwRet = MCIERR_INVALID_DEVICE_NAME;
1623         goto errCleanUp;
1624     }
1625
1626     if (dwParam & MCI_OPEN_ALIAS) {
1627         TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1628         if (!lpParms->lpstrAlias) {
1629             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1630             goto errCleanUp;
1631         }
1632     }
1633
1634     if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1635         goto errCleanUp;
1636     }
1637
1638     if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1639         TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1640         /* FIXME: is dwRet the correct ret code ? */
1641         goto errCleanUp;
1642     }
1643
1644     /* only handled devices fall through */
1645     TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1646
1647     if (dwParam & MCI_NOTIFY)
1648         mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1649
1650     return 0;
1651 errCleanUp:
1652     if (wmd) MCI_UnLoadMciDriver(wmd);
1653
1654     if (dwParam & MCI_NOTIFY)
1655         mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1656     return dwRet;
1657 }
1658
1659 /**************************************************************************
1660  *                      MCI_Close                               [internal]
1661  */
1662 static  DWORD MCI_Close(UINT wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1663 {
1664     DWORD               dwRet;
1665     LPWINE_MCIDRIVER    wmd;
1666
1667     TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1668
1669     /* Every device must handle MCI_NOTIFY on its own. */
1670     if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1671         while (MciDrivers) {
1672             /* Retrieve the device ID under lock, but send the message without,
1673              * the driver might be calling some winmm functions from another
1674              * thread before being fully stopped.
1675              */
1676             EnterCriticalSection(&WINMM_cs);
1677             if (!MciDrivers)
1678             {
1679                 LeaveCriticalSection(&WINMM_cs);
1680                 break;
1681             }
1682             wDevID = MciDrivers->wDeviceID;
1683             LeaveCriticalSection(&WINMM_cs);
1684             MCI_Close(wDevID, dwParam, lpParms);
1685         }
1686         return 0;
1687     }
1688
1689     if (!(wmd = MCI_GetDriver(wDevID))) {
1690         return MCIERR_INVALID_DEVICE_ID;
1691     }
1692
1693     dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD_PTR)lpParms);
1694
1695     MCI_UnLoadMciDriver(wmd);
1696
1697     return dwRet;
1698 }
1699
1700 /**************************************************************************
1701  *                      MCI_WriteString                         [internal]
1702  */
1703 static DWORD MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1704 {
1705     DWORD       ret = 0;
1706
1707     if (lpSrcStr) {
1708         dstSize /= sizeof(WCHAR);
1709         if (dstSize <= strlenW(lpSrcStr)) {
1710             lstrcpynW(lpDstStr, lpSrcStr, dstSize - 1);
1711             ret = MCIERR_PARAM_OVERFLOW;
1712         } else {
1713             strcpyW(lpDstStr, lpSrcStr);
1714         }
1715     } else {
1716         *lpDstStr = 0;
1717     }
1718     return ret;
1719 }
1720
1721 /**************************************************************************
1722  *                      MCI_Sysinfo                             [internal]
1723  */
1724 static  DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1725 {
1726     DWORD               ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1727     WCHAR               buf[2048], *s = buf, *p;
1728     LPWINE_MCIDRIVER    wmd;
1729     HKEY                hKey;
1730
1731     if (lpParms == NULL)                        return MCIERR_NULL_PARAMETER_BLOCK;
1732     if (lpParms->lpstrReturn == NULL)           return MCIERR_PARAM_OVERFLOW;
1733
1734     TRACE("(%08x, %08X, %p[num=%d, wDevTyp=%u])\n",
1735           uDevID, dwFlags, lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1736     if ((WORD)MCI_ALL_DEVICE_ID == LOWORD(uDevID))
1737         uDevID = MCI_ALL_DEVICE_ID; /* Be compatible with Win9x */
1738
1739     switch (dwFlags & ~(MCI_SYSINFO_OPEN|MCI_NOTIFY|MCI_WAIT)) {
1740     case MCI_SYSINFO_QUANTITY:
1741         if (lpParms->dwRetSize < sizeof(DWORD))
1742             return MCIERR_PARAM_OVERFLOW;
1743         /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1744         if (uDevID == MCI_ALL_DEVICE_ID) {
1745             /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1746             if (dwFlags & MCI_SYSINFO_OPEN) {
1747                 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1748                 EnterCriticalSection(&WINMM_cs);
1749                 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1750                     cnt++;
1751                 }
1752                 LeaveCriticalSection(&WINMM_cs);
1753             } else {
1754                 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1755                 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1756                                    0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1757                     RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1758                     RegCloseKey( hKey );
1759                 }
1760                 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1761                     for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1762             }
1763         } else {
1764             if (dwFlags & MCI_SYSINFO_OPEN) {
1765                 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %d\n", lpParms->wDeviceType);
1766                 EnterCriticalSection(&WINMM_cs);
1767                 for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1768                     if (wmd->wType == lpParms->wDeviceType) cnt++;
1769                 }
1770                 LeaveCriticalSection(&WINMM_cs);
1771             } else {
1772                 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %d\n", lpParms->wDeviceType);
1773                 FIXME("Don't know how to get # of MCI devices of a given type\n");
1774                 cnt = 1;
1775             }
1776         }
1777         *(DWORD*)lpParms->lpstrReturn = cnt;
1778         TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1779         ret = MCI_INTEGER_RETURNED;
1780         /* return ret; Only Win9x sends a notification in this case. */
1781         break;
1782     case MCI_SYSINFO_INSTALLNAME:
1783         TRACE("MCI_SYSINFO_INSTALLNAME\n");
1784         if ((wmd = MCI_GetDriver(uDevID))) {
1785             ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1786                                   wmd->lpstrDeviceType);
1787         } else {
1788             *lpParms->lpstrReturn = 0;
1789             ret = MCIERR_INVALID_DEVICE_ID;
1790         }
1791         TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1792         break;
1793     case MCI_SYSINFO_NAME:
1794         s = NULL;
1795         if (dwFlags & MCI_SYSINFO_OPEN) {
1796             /* Win9x returns 0 for 0 < uDevID < (UINT16)MCI_ALL_DEVICE_ID */
1797             TRACE("MCI_SYSINFO_NAME: nth alias of type %d\n",
1798                   uDevID == MCI_ALL_DEVICE_ID ? MCI_ALL_DEVICE_ID : lpParms->wDeviceType);
1799             EnterCriticalSection(&WINMM_cs);
1800             for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1801                 /* wDeviceType == MCI_ALL_DEVICE_ID is not recognized. */
1802                 if (uDevID == MCI_ALL_DEVICE_ID ||
1803                     lpParms->wDeviceType == wmd->wType) {
1804                     cnt++;
1805                     if (cnt == lpParms->dwNumber) {
1806                         s = wmd->lpstrAlias;
1807                         break;
1808                     }
1809                 }
1810             }
1811             LeaveCriticalSection(&WINMM_cs);
1812             ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR), s) : MCIERR_OUTOFRANGE;
1813         } else if (MCI_ALL_DEVICE_ID == uDevID) {
1814             TRACE("MCI_SYSINFO_NAME: device #%d\n", lpParms->dwNumber);
1815             if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0, 
1816                                KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1817                 if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 
1818                                       0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS && 
1819                     lpParms->dwNumber <= cnt) {
1820                     DWORD bufLen = sizeof(buf)/sizeof(buf[0]);
1821                     if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1, 
1822                                       buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
1823                         s = buf;
1824                 }
1825                 RegCloseKey( hKey );
1826             }
1827             if (!s) {
1828                 if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1829                     for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1830                         TRACE("%d: %s\n", cnt, debugstr_w(p));
1831                         if (cnt == lpParms->dwNumber - 1) {
1832                             s = p;
1833                             break;
1834                         }
1835                     }
1836                 }
1837             }
1838             ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR), s) : MCIERR_OUTOFRANGE;
1839         } else {
1840             FIXME("MCI_SYSINFO_NAME: nth device of type %d\n", lpParms->wDeviceType);
1841             /* Cheating: what is asked for is the nth device from the registry. */
1842             if (1 != lpParms->dwNumber || /* Handle only one of each kind. */
1843                 lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST)
1844                 ret = MCIERR_OUTOFRANGE;
1845             else {
1846                 LoadStringW(hWinMM32Instance, LOWORD(lpParms->wDeviceType),
1847                             lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR));
1848                 ret = 0;
1849             }
1850         }
1851         TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1852         break;
1853     default:
1854         TRACE("Unsupported flag value=%08x\n", dwFlags);
1855         ret = MCIERR_UNRECOGNIZED_KEYWORD;
1856     }
1857     if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
1858         mciDriverNotify((HWND)lpParms->dwCallback, uDevID, MCI_NOTIFY_SUCCESSFUL);
1859     return ret;
1860 }
1861
1862 /**************************************************************************
1863  *                      MCI_Break                               [internal]
1864  */
1865 static  DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1866 {
1867     DWORD       dwRet = 0;
1868
1869     if (lpParms == NULL)        return MCIERR_NULL_PARAMETER_BLOCK;
1870
1871     if (dwFlags & MCI_NOTIFY)
1872         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1873                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1874
1875     return dwRet;
1876 }
1877
1878 /**************************************************************************
1879  *                      MCI_Sound                               [internal]
1880  */
1881 static  DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
1882 {
1883     DWORD       dwRet = 0;
1884
1885     if (lpParms == NULL)        return MCIERR_NULL_PARAMETER_BLOCK;
1886
1887     if (dwFlags & MCI_SOUND_NAME)
1888         dwRet = sndPlaySoundW(lpParms->lpstrSoundName, SND_SYNC) ? MMSYSERR_NOERROR : MMSYSERR_ERROR;
1889     else
1890         dwRet = MMSYSERR_ERROR; /* what should be done ??? */
1891     if (dwFlags & MCI_NOTIFY)
1892         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1893                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1894
1895     return dwRet;
1896 }
1897
1898 /**************************************************************************
1899  *                      MCI_SendCommand                         [internal]
1900  */
1901 DWORD   MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1902 {
1903     DWORD               dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1904
1905     switch (wMsg) {
1906     case MCI_OPEN:
1907         dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1908         break;
1909     case MCI_CLOSE:
1910         dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1911         break;
1912     case MCI_SYSINFO:
1913         dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
1914         break;
1915     case MCI_BREAK:
1916         dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1917         break;
1918     case MCI_SOUND:
1919         dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
1920         break;
1921     default:
1922       if ((UINT16)wDevID == (UINT16)MCI_ALL_DEVICE_ID) {
1923             FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1924             dwRet = MCIERR_CANNOT_USE_ALL;
1925         } else {
1926             dwRet = MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2);
1927         }
1928         break;
1929     }
1930     return dwRet;
1931 }
1932
1933 /**************************************************************************
1934  *                              MCI_CleanUp                     [internal]
1935  *
1936  * Some MCI commands need to be cleaned-up (when not called from
1937  * mciSendString), because MCI drivers return extra information for string
1938  * transformation. This function gets rid of them.
1939  */
1940 static LRESULT  MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD_PTR dwParam2)
1941 {
1942     if (LOWORD(dwRet))
1943         return LOWORD(dwRet);
1944
1945     switch (wMsg) {
1946     case MCI_GETDEVCAPS:
1947         switch (dwRet & 0xFFFF0000ul) {
1948         case 0:
1949         case MCI_COLONIZED3_RETURN:
1950         case MCI_COLONIZED4_RETURN:
1951         case MCI_INTEGER_RETURNED:
1952             /* nothing to do */
1953             break;
1954         case MCI_RESOURCE_RETURNED:
1955         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1956             {
1957                 LPMCI_GETDEVCAPS_PARMS  lmgp;
1958
1959                 lmgp = (LPMCI_GETDEVCAPS_PARMS)dwParam2;
1960                 TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
1961                 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1962             }
1963             break;
1964         default:
1965             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1966                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1967         }
1968         break;
1969     case MCI_STATUS:
1970         switch (dwRet & 0xFFFF0000ul) {
1971         case 0:
1972         case MCI_COLONIZED3_RETURN:
1973         case MCI_COLONIZED4_RETURN:
1974         case MCI_INTEGER_RETURNED:
1975             /* nothing to do */
1976             break;
1977         case MCI_RESOURCE_RETURNED:
1978         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1979             {
1980                 LPMCI_STATUS_PARMS      lsp;
1981
1982                 lsp = (LPMCI_STATUS_PARMS)dwParam2;
1983                 TRACE("Changing %08lx to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
1984                 lsp->dwReturn = LOWORD(lsp->dwReturn);
1985             }
1986             break;
1987         default:
1988             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1989                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1990         }
1991         break;
1992     case MCI_SYSINFO:
1993         switch (dwRet & 0xFFFF0000ul) {
1994         case 0:
1995         case MCI_INTEGER_RETURNED:
1996             /* nothing to do */
1997             break;
1998         default:
1999             FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2000         }
2001         break;
2002     default:
2003         if (HIWORD(dwRet)) {
2004             FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2005                   dwRet, MCI_MessageToString(wMsg));
2006         }
2007         break;
2008     }
2009     return LOWORD(dwRet);
2010 }
2011
2012 /**************************************************************************
2013  *                              mciGetErrorStringW              [WINMM.@]
2014  */
2015 BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
2016 {
2017     BOOL                ret = FALSE;
2018
2019     if (lpstrBuffer != NULL && uLength > 0 &&
2020         wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {
2021
2022         if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2023             ret = TRUE;
2024         }
2025     }
2026     return ret;
2027 }
2028
2029 /**************************************************************************
2030  *                              mciGetErrorStringA              [WINMM.@]
2031  */
2032 BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
2033 {
2034     BOOL                ret = FALSE;
2035
2036     if (lpstrBuffer != NULL && uLength > 0 &&
2037         dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {
2038
2039         if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2040             ret = TRUE;
2041         }
2042     }
2043     return ret;
2044 }
2045
2046 /**************************************************************************
2047  *                      mciDriverNotify                         [WINMM.@]
2048  */
2049 BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
2050 {
2051     TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);
2052
2053     return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
2054 }
2055
2056 /**************************************************************************
2057  *                      mciGetDriverData                        [WINMM.@]
2058  */
2059 DWORD_PTR WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
2060 {
2061     LPWINE_MCIDRIVER    wmd;
2062
2063     TRACE("(%04x)\n", uDeviceID);
2064
2065     wmd = MCI_GetDriver(uDeviceID);
2066
2067     if (!wmd) {
2068         WARN("Bad uDeviceID\n");
2069         return 0L;
2070     }
2071
2072     return wmd->dwPrivate;
2073 }
2074
2075 /**************************************************************************
2076  *                      mciSetDriverData                        [WINMM.@]
2077  */
2078 BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD_PTR data)
2079 {
2080     LPWINE_MCIDRIVER    wmd;
2081
2082     TRACE("(%04x, %08lx)\n", uDeviceID, data);
2083
2084     wmd = MCI_GetDriver(uDeviceID);
2085
2086     if (!wmd) {
2087         WARN("Bad uDeviceID\n");
2088         return FALSE;
2089     }
2090
2091     wmd->dwPrivate = data;
2092     return TRUE;
2093 }
2094
2095 /**************************************************************************
2096  *                              mciSendCommandW                 [WINMM.@]
2097  *
2098  */
2099 DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2100 {
2101     DWORD       dwRet;
2102
2103     TRACE("(%08x, %s, %08lx, %08lx)\n",
2104           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2105
2106     dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2);
2107     dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2108     TRACE("=> %08x\n", dwRet);
2109     return dwRet;
2110 }
2111
2112 /**************************************************************************
2113  *                              mciSendCommandA                 [WINMM.@]
2114  */
2115 DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2116 {
2117     DWORD ret;
2118     int mapped;
2119
2120     TRACE("(%08x, %s, %08lx, %08lx)\n",
2121           wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);
2122
2123     mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
2124     if (mapped == -1)
2125     {
2126         FIXME("message %04x mapping failed\n", wMsg);
2127         return MMSYSERR_NOMEM;
2128     }
2129     ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
2130     if (mapped)
2131         MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
2132     return ret;
2133 }
2134
2135 /**************************************************************************
2136  *                              mciGetDeviceIDA                 [WINMM.@]
2137  */
2138 UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
2139 {
2140     LPWSTR w = MCI_strdupAtoW(lpstrName);
2141     UINT ret = MCIERR_OUT_OF_MEMORY;
2142
2143     if (w)
2144     {
2145         ret = mciGetDeviceIDW(w);
2146         HeapFree(GetProcessHeap(), 0, w);
2147     }
2148     return ret;
2149 }
2150
2151 /**************************************************************************
2152  *                              mciGetDeviceIDW                 [WINMM.@]
2153  */
2154 UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
2155 {
2156     return MCI_GetDriverFromString(lpwstrName); 
2157 }
2158
2159 /**************************************************************************
2160  *                              MCI_DefYieldProc                [internal]
2161  */
2162 static UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
2163 {
2164     INT16       ret;
2165     MSG         msg;
2166
2167     TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2168
2169     if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
2170         (GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
2171         PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2172         ret = 0;
2173     } else {
2174         msg.hwnd = HWND_32(HIWORD(data));
2175         while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
2176         ret = -1;
2177     }
2178     return ret;
2179 }
2180
2181 /**************************************************************************
2182  *                              mciSetYieldProc                 [WINMM.@]
2183  */
2184 BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
2185 {
2186     LPWINE_MCIDRIVER    wmd;
2187
2188     TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2189
2190     if (!(wmd = MCI_GetDriver(uDeviceID))) {
2191         WARN("Bad uDeviceID\n");
2192         return FALSE;
2193     }
2194
2195     wmd->lpfnYieldProc = fpYieldProc;
2196     wmd->dwYieldData   = dwYieldData;
2197
2198     return TRUE;
2199 }
2200
2201 /**************************************************************************
2202  *                              mciGetDeviceIDFromElementIDA    [WINMM.@]
2203  */
2204 UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
2205 {
2206     LPWSTR w = MCI_strdupAtoW(lpstrType);
2207     UINT ret = 0;
2208
2209     if (w)
2210     {
2211         ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
2212         HeapFree(GetProcessHeap(), 0, w);
2213     }
2214     return ret;
2215 }
2216
2217 /**************************************************************************
2218  *                              mciGetDeviceIDFromElementIDW    [WINMM.@]
2219  */
2220 UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
2221 {
2222     /* FIXME: that's rather strange, there is no
2223      * mciGetDeviceIDFromElementID32A in winmm.spec
2224      */
2225     FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2226     return 0;
2227 }
2228
2229 /**************************************************************************
2230  *                              mciGetYieldProc                 [WINMM.@]
2231  */
2232 YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
2233 {
2234     LPWINE_MCIDRIVER    wmd;
2235
2236     TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);
2237
2238     if (!(wmd = MCI_GetDriver(uDeviceID))) {
2239         WARN("Bad uDeviceID\n");
2240         return NULL;
2241     }
2242     if (!wmd->lpfnYieldProc) {
2243         WARN("No proc set\n");
2244         return NULL;
2245     }
2246     if (lpdwYieldData) *lpdwYieldData = wmd->dwYieldData;
2247     return wmd->lpfnYieldProc;
2248 }
2249
2250 /**************************************************************************
2251  *                              mciGetCreatorTask               [WINMM.@]
2252  */
2253 HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
2254 {
2255     LPWINE_MCIDRIVER    wmd;
2256     HTASK ret = 0;
2257
2258     if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)(DWORD_PTR)wmd->CreatorThread;
2259
2260     TRACE("(%u) => %p\n", uDeviceID, ret);
2261     return ret;
2262 }
2263
2264 /**************************************************************************
2265  *                      mciDriverYield                          [WINMM.@]
2266  */
2267 UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
2268 {
2269     LPWINE_MCIDRIVER    wmd;
2270     UINT                ret = 0;
2271
2272     TRACE("(%04x)\n", uDeviceID);
2273
2274     if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc) {
2275         MSG msg;
2276         PeekMessageW(&msg, 0, 0, 0, PM_REMOVE | PM_QS_SENDMESSAGE);
2277     } else {
2278         ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
2279     }
2280
2281     return ret;
2282 }