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