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