Much improved mciavi driver. Fixed synchronization, RIFF file with
[wine] / dlls / winmm / mci.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * MCI internal functions
5  *
6  * Copyright 1998/1999 Eric Pouech
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winreg.h"
35 #include "mmsystem.h"
36 #include "winuser.h"
37 #include "winnls.h"
38 #include "winreg.h"
39
40 #include "digitalv.h"
41 #include "winemm.h"
42
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(mci);
46
47 static  int                     MCI_InstalledCount;
48 static  LPSTR                   MCI_lpInstallNames /* = NULL */;
49
50 WINMM_MapType  (*pFnMciMapMsg16To32A)  (WORD,WORD,DWORD*) /* = NULL */;
51 WINMM_MapType  (*pFnMciUnMapMsg16To32A)(WORD,WORD,DWORD) /* = NULL */;
52 WINMM_MapType  (*pFnMciMapMsg32ATo16)  (WORD,WORD,DWORD,DWORD*) /* = NULL */;
53 WINMM_MapType  (*pFnMciUnMapMsg32ATo16)(WORD,WORD,DWORD,DWORD) /* = NULL */;
54
55 /* First MCI valid device ID (0 means error) */
56 #define MCI_MAGIC 0x0001
57
58 /* dup a string and uppercase it */
59 inline static LPSTR str_dup_upper( LPCSTR str )
60 {
61     INT len = strlen(str) + 1;
62     LPSTR p = HeapAlloc( GetProcessHeap(), 0, len );
63     if (p)
64     {
65         memcpy( p, str, len );
66         CharUpperA( p );
67     }
68     return p;
69 }
70
71 /**************************************************************************
72  *                              MCI_GetDriver                   [internal]
73  */
74 LPWINE_MCIDRIVER        MCI_GetDriver(UINT16 wDevID)
75 {
76     LPWINE_MCIDRIVER    wmd = 0;
77
78     EnterCriticalSection(&WINMM_IData->cs);
79     for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
80         if (wmd->wDeviceID == wDevID)
81             break;
82     }
83     LeaveCriticalSection(&WINMM_IData->cs);
84     return wmd;
85 }
86
87 /**************************************************************************
88  *                              MCI_GetDriverFromString         [internal]
89  */
90 UINT    MCI_GetDriverFromString(LPCSTR lpstrName)
91 {
92     LPWINE_MCIDRIVER    wmd;
93     UINT                ret = 0;
94
95     if (!lpstrName)
96         return 0;
97
98     if (!lstrcmpiA(lpstrName, "ALL"))
99         return MCI_ALL_DEVICE_ID;
100
101     EnterCriticalSection(&WINMM_IData->cs);
102     for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
103         if (wmd->lpstrElementName && strcmp(wmd->lpstrElementName, lpstrName) == 0) {
104             ret = wmd->wDeviceID;
105             break;
106         }
107         if (wmd->lpstrDeviceType && strcasecmp(wmd->lpstrDeviceType, lpstrName) == 0) {
108             ret = wmd->wDeviceID;
109             break;
110         }
111         if (wmd->lpstrAlias && strcasecmp(wmd->lpstrAlias, lpstrName) == 0) {
112             ret = wmd->wDeviceID;
113             break;
114         }
115     }
116     LeaveCriticalSection(&WINMM_IData->cs);
117
118     return ret;
119 }
120
121 /**************************************************************************
122  *                      MCI_MessageToString                     [internal]
123  */
124 const char* MCI_MessageToString(UINT16 wMsg)
125 {
126     static char buffer[100];
127
128 #define CASE(s) case (s): return #s
129
130     switch (wMsg) {
131         CASE(MCI_BREAK);
132         CASE(MCI_CLOSE);
133         CASE(MCI_CLOSE_DRIVER);
134         CASE(MCI_COPY);
135         CASE(MCI_CUE);
136         CASE(MCI_CUT);
137         CASE(MCI_DELETE);
138         CASE(MCI_ESCAPE);
139         CASE(MCI_FREEZE);
140         CASE(MCI_PAUSE);
141         CASE(MCI_PLAY);
142         CASE(MCI_GETDEVCAPS);
143         CASE(MCI_INFO);
144         CASE(MCI_LOAD);
145         CASE(MCI_OPEN);
146         CASE(MCI_OPEN_DRIVER);
147         CASE(MCI_PASTE);
148         CASE(MCI_PUT);
149         CASE(MCI_REALIZE);
150         CASE(MCI_RECORD);
151         CASE(MCI_RESUME);
152         CASE(MCI_SAVE);
153         CASE(MCI_SEEK);
154         CASE(MCI_SET);
155         CASE(MCI_SPIN);
156         CASE(MCI_STATUS);
157         CASE(MCI_STEP);
158         CASE(MCI_STOP);
159         CASE(MCI_SYSINFO);
160         CASE(MCI_UNFREEZE);
161         CASE(MCI_UPDATE);
162         CASE(MCI_WHERE);
163         CASE(MCI_WINDOW);
164         /* constants for digital video */
165         CASE(MCI_CAPTURE);
166         CASE(MCI_MONITOR);
167         CASE(MCI_RESERVE);
168         CASE(MCI_SETAUDIO);
169         CASE(MCI_SIGNAL);
170         CASE(MCI_SETVIDEO);
171         CASE(MCI_QUALITY);
172         CASE(MCI_LIST);
173         CASE(MCI_UNDO);
174         CASE(MCI_CONFIGURE);
175         CASE(MCI_RESTORE);
176 #undef CASE
177     default:
178         sprintf(buffer, "MCI_<<%04X>>", wMsg);
179         return buffer;
180     }
181 }
182
183 /**************************************************************************
184  *                              MCI_GetDevTypeFromFileName      [internal]
185  */
186 static  DWORD   MCI_GetDevTypeFromFileName(LPCSTR fileName, LPSTR buf, UINT len)
187 {
188     LPSTR       tmp;
189     HKEY        hKey;
190
191     if ((tmp = strrchr(fileName, '.'))) {
192         if (RegOpenKeyExA( HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\MCI Extensions",
193                            0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
194             DWORD dwLen = len;
195             LONG lRet = RegQueryValueExA( hKey, tmp + 1, 0, 0, buf, &dwLen ); 
196             RegCloseKey( hKey );
197             if (lRet == ERROR_SUCCESS) return 0;
198         }
199         TRACE("No ...\\MCI Extensions entry for '%s' found.\n", tmp);
200     }
201     return MCIERR_EXTENSION_NOT_FOUND;
202 }
203
204 #define MAX_MCICMDTABLE                 20
205 #define MCI_COMMAND_TABLE_NOT_LOADED    0xFFFE
206
207 typedef struct tagWINE_MCICMDTABLE {
208     UINT                uDevType;
209     LPCSTR              lpTable;
210     UINT                nVerbs;         /* number of verbs in command table */
211     LPCSTR*             aVerbs;         /* array of verbs to speed up the verb look up process */
212 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
213
214 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
215
216 /**************************************************************************
217  *                              MCI_IsCommandTableValid         [internal]
218  */
219 static  BOOL            MCI_IsCommandTableValid(UINT uTbl)
220 {
221     LPCSTR      lmem, str;
222     DWORD       flg;
223     WORD        eid;
224     int         idx = 0;
225     BOOL        inCst = FALSE;
226
227     TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
228           uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
229
230     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
231         return FALSE;
232
233     lmem = S_MciCmdTable[uTbl].lpTable;
234     do {
235         do {
236             str = lmem;
237             lmem += strlen(lmem) + 1;
238             flg = *(LPDWORD)lmem;
239             eid = *(LPWORD)(lmem + sizeof(DWORD));
240             lmem += sizeof(DWORD) + sizeof(WORD);
241             idx ++;
242             /* EPP          TRACE("cmd='%s' %08lx %04x\n", str, flg, eid); */
243             switch (eid) {
244             case MCI_COMMAND_HEAD:      if (!*str || !flg) return FALSE; idx = 0;                       break;  /* check unicity of str in table */
245             case MCI_STRING:            if (inCst) return FALSE;                                        break;
246             case MCI_INTEGER:           if (!*str) return FALSE;                                        break;
247             case MCI_END_COMMAND:       if (*str || flg || idx == 0) return FALSE; idx = 0;             break;
248             case MCI_RETURN:            if (*str || idx != 1) return FALSE;                             break;
249             case MCI_FLAG:              if (!*str) return FALSE;                                        break;
250             case MCI_END_COMMAND_LIST:  if (*str || flg) return FALSE;  idx = 0;                        break;
251             case MCI_RECT:              if (!*str || inCst) return FALSE;                               break;
252             case MCI_CONSTANT:          if (inCst) return FALSE; inCst = TRUE;                          break;
253             case MCI_END_CONSTANT:      if (*str || flg || !inCst) return FALSE; inCst = FALSE;         break;
254             default:                    return FALSE;
255             }
256         } while (eid != MCI_END_COMMAND_LIST);
257     } while (eid != MCI_END_COMMAND_LIST);
258     return TRUE;
259 }
260
261 /**************************************************************************
262  *                              MCI_DumpCommandTable            [internal]
263  */
264 static  BOOL            MCI_DumpCommandTable(UINT uTbl)
265 {
266     LPCSTR      lmem;
267     LPCSTR      str;
268     DWORD       flg;
269     WORD        eid;
270
271     if (!MCI_IsCommandTableValid(uTbl)) {
272         ERR("Ooops: %d is not valid\n", uTbl);
273         return FALSE;
274     }
275
276     lmem = S_MciCmdTable[uTbl].lpTable;
277     do {
278         do {
279             str = lmem;
280             lmem += strlen(lmem) + 1;
281             flg = *(LPDWORD)lmem;
282             eid = *(LPWORD)(lmem + sizeof(DWORD));
283             TRACE("cmd='%s' %08lx %04x\n", str, flg, eid);
284             lmem += sizeof(DWORD) + sizeof(WORD);
285         } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
286         TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : "");
287     } while (eid != MCI_END_COMMAND_LIST);
288     return TRUE;
289 }
290
291
292 /**************************************************************************
293  *                              MCI_GetCommandTable             [internal]
294  */
295 static  UINT            MCI_GetCommandTable(UINT uDevType)
296 {
297     UINT        uTbl;
298     char        buf[32];
299     LPCSTR      str = NULL;
300
301     /* first look up existing for existing devType */
302     for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
303         if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
304             return uTbl;
305     }
306
307     /* well try to load id */
308     if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
309         if (LoadStringA(WINMM_IData->hWinMM32Instance, uDevType, buf, sizeof(buf))) {
310             str = buf;
311         }
312     } else if (uDevType == 0) {
313         str = "CORE";
314     }
315     uTbl = MCI_NO_COMMAND_TABLE;
316     if (str) {
317         HRSRC   hRsrc = FindResourceA(WINMM_IData->hWinMM32Instance, str, (LPCSTR)RT_RCDATA);
318         HANDLE  hMem = 0;
319
320         if (hRsrc) hMem = LoadResource(WINMM_IData->hWinMM32Instance, hRsrc);
321         if (hMem) {
322             uTbl = MCI_SetCommandTable(LockResource(hMem), uDevType);
323         } else {
324             WARN("No command table found in resource %p[%s]\n",
325                  WINMM_IData->hWinMM32Instance, str);
326         }
327     }
328     TRACE("=> %d\n", uTbl);
329     return uTbl;
330 }
331
332 /**************************************************************************
333  *                              MCI_SetCommandTable             [internal]
334  */
335 UINT MCI_SetCommandTable(void *table, UINT uDevType)
336 {
337     int                 uTbl;
338     static      BOOL    bInitDone = FALSE;
339
340     /* <HACK>
341      * The CORE command table must be loaded first, so that MCI_GetCommandTable()
342      * can be called with 0 as a uDevType to retrieve it.
343      * </HACK>
344      */
345     if (!bInitDone) {
346         bInitDone = TRUE;
347         MCI_GetCommandTable(0);
348     }
349
350     for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
351         if (!S_MciCmdTable[uTbl].lpTable) {
352             LPCSTR      lmem, str;
353             WORD        eid;
354             WORD        count;
355
356             S_MciCmdTable[uTbl].uDevType = uDevType;
357             S_MciCmdTable[uTbl].lpTable = table;
358
359             if (TRACE_ON(mci)) {
360                 MCI_DumpCommandTable(uTbl);
361             }
362
363             /* create the verbs table */
364             /* get # of entries */
365             lmem = S_MciCmdTable[uTbl].lpTable;
366             count = 0;
367             do {
368                 lmem += strlen(lmem) + 1;
369                 eid = *(LPWORD)(lmem + sizeof(DWORD));
370                 lmem += sizeof(DWORD) + sizeof(WORD);
371                 if (eid == MCI_COMMAND_HEAD)
372                     count++;
373             } while (eid != MCI_END_COMMAND_LIST);
374
375             S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCSTR));
376             S_MciCmdTable[uTbl].nVerbs = count;
377
378             lmem = S_MciCmdTable[uTbl].lpTable;
379             count = 0;
380             do {
381                 str = lmem;
382                 lmem += strlen(lmem) + 1;
383                 eid = *(LPWORD)(lmem + sizeof(DWORD));
384                 lmem += sizeof(DWORD) + sizeof(WORD);
385                 if (eid == MCI_COMMAND_HEAD)
386                     S_MciCmdTable[uTbl].aVerbs[count++] = str;
387             } while (eid != MCI_END_COMMAND_LIST);
388             /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
389             return uTbl;
390         }
391     }
392
393     return MCI_NO_COMMAND_TABLE;
394 }
395
396 /**************************************************************************
397  *                              MCI_DeleteCommandTable          [internal]
398  */
399 static  BOOL    MCI_DeleteCommandTable(UINT uTbl)
400 {
401     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
402         return FALSE;
403
404     S_MciCmdTable[uTbl].lpTable = NULL;
405     if (S_MciCmdTable[uTbl].aVerbs) {
406         HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTbl].aVerbs);
407         S_MciCmdTable[uTbl].aVerbs = 0;
408     }
409     return TRUE;
410 }
411
412 /**************************************************************************
413  *                              MCI_UnLoadMciDriver             [internal]
414  */
415 static  BOOL    MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
416 {
417     LPWINE_MCIDRIVER*           tmp;
418
419     if (!wmd)
420         return TRUE;
421
422     CloseDriver(wmd->hDriver, 0, 0);
423
424     if (wmd->dwPrivate != 0)
425         WARN("Unloading mci driver with non nul dwPrivate field\n");
426
427     EnterCriticalSection(&WINMM_IData->cs);
428     for (tmp = &WINMM_IData->lpMciDrvs; *tmp; tmp = &(*tmp)->lpNext) {
429         if (*tmp == wmd) {
430             *tmp = wmd->lpNext;
431             break;
432         }
433     }
434     LeaveCriticalSection(&WINMM_IData->cs);
435
436     HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
437     HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
438     HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
439
440     HeapFree(GetProcessHeap(), 0, wmd);
441     return TRUE;
442 }
443
444 /**************************************************************************
445  *                              MCI_OpenMciDriver               [internal]
446  */
447 static  BOOL    MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCSTR drvTyp, LPARAM lp)
448 {
449     char        libName[128];
450
451     if (!DRIVER_GetLibName(drvTyp, "mci", libName, sizeof(libName)))
452         return FALSE;
453
454     wmd->bIs32 = 0xFFFF;
455     /* First load driver */
456     if ((wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp))) {
457         wmd->bIs32 = TRUE;
458     } else if (WINMM_CheckForMMSystem() && pFnMciMapMsg32ATo16) {
459         WINMM_MapType   res;
460
461         switch (res = pFnMciMapMsg32ATo16(0, DRV_OPEN, 0, &lp)) {
462         case WINMM_MAP_MSGERROR:
463             TRACE("Not handled yet (DRV_OPEN)\n");
464             break;
465         case WINMM_MAP_NOMEM:
466             TRACE("Problem mapping msg=DRV_OPEN from 32a to 16\n");
467             break;
468         case WINMM_MAP_OK:
469         case WINMM_MAP_OKMEM:
470             if ((wmd->hDriver = OpenDriverA(drvTyp, "mci", lp)))
471                 wmd->bIs32 = FALSE;
472             if (res == WINMM_MAP_OKMEM)
473                 pFnMciUnMapMsg32ATo16(0, DRV_OPEN, 0, lp);
474             break;
475         }
476     }
477     return (wmd->bIs32 == 0xFFFF) ? FALSE : TRUE;
478 }
479
480 /**************************************************************************
481  *                              MCI_LoadMciDriver               [internal]
482  */
483 static  DWORD   MCI_LoadMciDriver(LPCSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
484 {
485     LPSTR                       strDevTyp = str_dup_upper(_strDevTyp);
486     LPWINE_MCIDRIVER            wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
487     MCI_OPEN_DRIVER_PARMSA      modp;
488     DWORD                       dwRet = 0;
489
490     if (!wmd || !strDevTyp) {
491         dwRet = MCIERR_OUT_OF_MEMORY;
492         goto errCleanUp;
493     }
494
495     wmd->lpfnYieldProc = MCI_DefYieldProc;
496     wmd->dwYieldData = VK_CANCEL;
497     wmd->CreatorThread = GetCurrentThreadId();
498
499     EnterCriticalSection(&WINMM_IData->cs);
500     /* wmd must be inserted in list before sending opening the driver, coz' it
501      * may want to lookup at wDevID
502      */
503     wmd->lpNext = WINMM_IData->lpMciDrvs;
504     WINMM_IData->lpMciDrvs = wmd;
505
506     for (modp.wDeviceID = MCI_MAGIC;
507          MCI_GetDriver(modp.wDeviceID) != 0;
508          modp.wDeviceID++);
509
510     wmd->wDeviceID = modp.wDeviceID;
511
512     LeaveCriticalSection(&WINMM_IData->cs);
513
514     TRACE("wDevID=%04X \n", modp.wDeviceID);
515
516     modp.lpstrParams = NULL;
517
518     if (!MCI_OpenMciDriver(wmd, strDevTyp, (LPARAM)&modp)) {
519         /* silence warning if all is used... some bogus program use commands like
520          * 'open all'...
521          */
522         if (strcasecmp(strDevTyp, "all") == 0) {
523             dwRet = MCIERR_CANNOT_USE_ALL;
524         } else {
525             FIXME("Couldn't load driver for type %s.\n"
526                   "If you don't have a windows installation accessible from Wine,\n"
527                   "you perhaps forgot to create a [mci] section in system.ini\n",
528                   strDevTyp);
529             dwRet = MCIERR_DEVICE_NOT_INSTALLED;
530         }
531         goto errCleanUp;
532     }
533
534     /* FIXME: should also check that module's description is of the form
535      * MODULENAME:[MCI] comment
536      */
537
538     /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
539     wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
540     wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
541
542     TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
543           wmd->hDriver, strDevTyp, modp.wType, modp.wCustomCommandTable);
544
545     wmd->lpstrDeviceType = strDevTyp;
546     wmd->wType = modp.wType;
547
548     TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
549           modp.wDeviceID, modp.wType, modp.wDeviceID);
550     *lpwmd = wmd;
551     return 0;
552 errCleanUp:
553     MCI_UnLoadMciDriver(wmd);
554     HeapFree(GetProcessHeap(), 0, strDevTyp);
555     *lpwmd = 0;
556     return dwRet;
557 }
558
559 /**************************************************************************
560  *                      MCI_FinishOpen                          [internal]
561  */
562 static  DWORD   MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSA lpParms,
563                                DWORD dwParam)
564 {
565     if (dwParam & MCI_OPEN_ELEMENT)
566     {
567         wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,strlen(lpParms->lpstrElementName)+1);
568         strcpy( wmd->lpstrElementName, lpParms->lpstrElementName );
569     }
570     if (dwParam & MCI_OPEN_ALIAS)
571     {
572         wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, strlen(lpParms->lpstrAlias)+1);
573         strcpy( wmd->lpstrAlias, lpParms->lpstrAlias);
574     }
575     lpParms->wDeviceID = wmd->wDeviceID;
576
577     return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
578                                  (DWORD)lpParms);
579 }
580
581 /**************************************************************************
582  *                              MCI_FindCommand         [internal]
583  */
584 static  LPCSTR          MCI_FindCommand(UINT uTbl, LPCSTR verb)
585 {
586     UINT        idx;
587
588     if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
589         return NULL;
590
591     /* another improvement would be to have the aVerbs array sorted,
592      * so that we could use a dichotomic search on it, rather than this dumb
593      * array look up
594      */
595     for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
596         if (strcmp(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
597             return S_MciCmdTable[uTbl].aVerbs[idx];
598     }
599
600     return NULL;
601 }
602
603 /**************************************************************************
604  *                              MCI_GetReturnType               [internal]
605  */
606 static  DWORD           MCI_GetReturnType(LPCSTR lpCmd)
607 {
608     lpCmd += strlen(lpCmd) + 1 + sizeof(DWORD) + sizeof(WORD);
609     if (*lpCmd == '\0' && *(LPWORD)(lpCmd + 1 + sizeof(DWORD)) == MCI_RETURN) {
610         return *(LPDWORD)(lpCmd + 1);
611     }
612     return 0L;
613 }
614
615 /**************************************************************************
616  *                              MCI_GetMessage                  [internal]
617  */
618 static  WORD            MCI_GetMessage(LPCSTR lpCmd)
619 {
620     return (WORD)*(LPDWORD)(lpCmd + strlen(lpCmd) + 1);
621 }
622
623 /**************************************************************************
624  *                              MCI_GetDWord                    [internal]
625  */
626 static  BOOL            MCI_GetDWord(LPDWORD data, LPSTR* ptr)
627 {
628     DWORD       val;
629     LPSTR       ret;
630
631     val = strtoul(*ptr, &ret, 0);
632
633     switch (*ret) {
634     case '\0':  break;
635     case ' ':   ret++; break;
636     default:    return FALSE;
637     }
638
639     *data |= val;
640     *ptr = ret;
641     return TRUE;
642 }
643
644 /**************************************************************************
645  *                              MCI_GetString           [internal]
646  */
647 static  DWORD   MCI_GetString(LPSTR* str, LPSTR* args)
648 {
649     LPSTR       ptr = *args;
650
651     /* see if we have a quoted string */
652     if (*ptr == '"') {
653         ptr = strchr(*str = ptr + 1, '"');
654         if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
655         /* FIXME: shall we escape \" from string ?? */
656         if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
657         *ptr++ = '\0'; /* remove trailing " */
658         if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
659         *ptr++ = '\0';
660     } else {
661         ptr = strchr(ptr, ' ');
662
663         if (ptr) {
664             *ptr++ = '\0';
665         } else {
666             ptr = *args + strlen(*args);
667         }
668         *str = *args;
669     }
670
671     *args = ptr;
672     return 0;
673 }
674
675 #define MCI_DATA_SIZE   16
676
677 /**************************************************************************
678  *                              MCI_ParseOptArgs                [internal]
679  */
680 static  DWORD   MCI_ParseOptArgs(LPDWORD data, int _offset, LPCSTR lpCmd,
681                                  LPSTR args, LPDWORD dwFlags)
682 {
683     int         len, offset;
684     LPCSTR      lmem, str;
685     DWORD       dwRet, flg, cflg = 0;
686     WORD        eid;
687     BOOL        inCst, found;
688
689     /* loop on arguments */
690     while (*args) {
691         lmem = lpCmd;
692         found = inCst = FALSE;
693         offset = _offset;
694
695         /* skip any leading white space(s) */
696         while (*args == ' ') args++;
697         TRACE("args='%s' offset=%d\n", args, offset);
698
699         do { /* loop on options for command table for the requested verb */
700             str = lmem;
701             lmem += (len = strlen(lmem)) + 1;
702             flg = *(LPDWORD)lmem;
703             eid = *(LPWORD)(lmem + sizeof(DWORD));
704             lmem += sizeof(DWORD) + sizeof(WORD);
705 /* EPP      TRACE("\tcmd='%s' inCst=%c eid=%04x\n", str, inCst ? 'Y' : 'N', eid); */
706
707             switch (eid) {
708             case MCI_CONSTANT:
709                 inCst = TRUE;   cflg = flg;     break;
710             case MCI_END_CONSTANT:
711                 /* there may be additional integral values after flag in constant */
712                 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
713                     *dwFlags |= cflg;
714                 }
715                 inCst = FALSE;  cflg = 0;
716                 break;
717             }
718
719             if (strncasecmp(args, str, len) == 0 &&
720                 (args[len] == 0 || args[len] == ' ')) {
721                 /* store good values into data[] */
722                 args += len;
723                 while (*args == ' ') args++;
724                 found = TRUE;
725
726                 switch (eid) {
727                 case MCI_COMMAND_HEAD:
728                 case MCI_RETURN:
729                 case MCI_END_COMMAND:
730                 case MCI_END_COMMAND_LIST:
731                 case MCI_CONSTANT:      /* done above */
732                 case MCI_END_CONSTANT:  /* done above */
733                     break;
734                 case MCI_FLAG:
735                     *dwFlags |= flg;
736                     break;
737                 case MCI_INTEGER:
738                     if (inCst) {
739                         data[offset] |= flg;
740                         *dwFlags |= cflg;
741                         inCst = FALSE;
742                     } else {
743                         *dwFlags |= flg;
744                         if (!MCI_GetDWord(&(data[offset]), &args)) {
745                             return MCIERR_BAD_INTEGER;
746                         }
747                     }
748                     break;
749                 case MCI_RECT:
750                     /* store rect in data (offset...offset+3) */
751                     *dwFlags |= flg;
752                     if (!MCI_GetDWord(&(data[offset+0]), &args) ||
753                         !MCI_GetDWord(&(data[offset+1]), &args) ||
754                         !MCI_GetDWord(&(data[offset+2]), &args) ||
755                         !MCI_GetDWord(&(data[offset+3]), &args)) {
756                         ERR("Bad rect '%s'\n", args);
757                         return MCIERR_BAD_INTEGER;
758                     }
759                     break;
760                 case MCI_STRING:
761                     *dwFlags |= flg;
762                     if ((dwRet = MCI_GetString((LPSTR*)&data[offset], &args)))
763                         return dwRet;
764                     break;
765                 default:        ERR("oops\n");
766                 }
767                 /* exit inside while loop, except if just entered in constant area definition */
768                 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
769             } else {
770                 /* have offset incremented if needed */
771                 switch (eid) {
772                 case MCI_COMMAND_HEAD:
773                 case MCI_RETURN:
774                 case MCI_END_COMMAND:
775                 case MCI_END_COMMAND_LIST:
776                 case MCI_CONSTANT:
777                 case MCI_FLAG:                  break;
778                 case MCI_INTEGER:               if (!inCst) offset++;   break;
779                 case MCI_END_CONSTANT:
780                 case MCI_STRING:                offset++; break;
781                 case MCI_RECT:                  offset += 4; break;
782                 default:                        ERR("oops\n");
783                 }
784             }
785         } while (eid != MCI_END_COMMAND);
786         if (!found) {
787             WARN("Optarg '%s' not found\n", args);
788             return MCIERR_UNRECOGNIZED_COMMAND;
789         }
790         if (offset == MCI_DATA_SIZE) {
791             ERR("Internal data[] buffer overflow\n");
792             return MCIERR_PARSER_INTERNAL;
793         }
794     }
795     return 0;
796 }
797
798 /**************************************************************************
799  *                              MCI_HandleReturnValues  [internal]
800  */
801 static  DWORD   MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType, 
802                                        LPDWORD data, LPSTR lpstrRet, UINT uRetLen)
803 {
804     if (lpstrRet) {
805         switch (retType) {
806         case 0: /* nothing to return */
807             break;
808         case MCI_INTEGER:
809             switch (dwRet & 0xFFFF0000ul) {
810             case 0:
811             case MCI_INTEGER_RETURNED:
812                 snprintf(lpstrRet, uRetLen, "%ld", data[1]);
813                 break;
814             case MCI_RESOURCE_RETURNED:
815                 /* return string which ID is HIWORD(data[1]),
816                  * string is loaded from mmsystem.dll */
817                 LoadStringA(WINMM_IData->hWinMM32Instance, HIWORD(data[1]),
818                             lpstrRet, uRetLen);
819                 break;
820             case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
821                 /* return string which ID is HIWORD(data[1]),
822                  * string is loaded from driver */
823                 /* FIXME: this is wrong for a 16 bit handle */
824                 LoadStringA(GetDriverModuleHandle(wmd->hDriver),
825                             HIWORD(data[1]), lpstrRet, uRetLen);
826                 break;
827             case MCI_COLONIZED3_RETURN:
828                 snprintf(lpstrRet, uRetLen, "%d:%d:%d",
829                          LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
830                          LOBYTE(HIWORD(data[1])));
831                 break;
832             case MCI_COLONIZED4_RETURN:
833                 snprintf(lpstrRet, uRetLen, "%d:%d:%d:%d",
834                          LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
835                          LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
836                 break;
837             default:    ERR("Ooops (%04X)\n", HIWORD(dwRet));
838             }
839             break;
840         case MCI_STRING:
841             switch (dwRet & 0xFFFF0000ul) {
842             case 0:
843                 /* nothing to do data[1] == lpstrRet */
844                 break;
845             case MCI_INTEGER_RETURNED:
846                 data[1] = *(LPDWORD)lpstrRet;
847                 snprintf(lpstrRet, uRetLen, "%ld", data[1]);
848                 break;
849             default:
850                 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
851                 break;
852             }
853             break;
854         case MCI_RECT:
855             if (dwRet & 0xFFFF0000ul)
856                 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
857             snprintf(lpstrRet, uRetLen, "%ld %ld %ld %ld",
858                        data[1], data[2], data[3], data[4]);
859             break;
860         default:                ERR("oops\n");
861         }
862     }
863     return LOWORD(dwRet);
864 }
865
866 /**************************************************************************
867  *                              mciSendStringA          [WINMM.@]
868  */
869 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
870                             UINT uRetLen, HWND hwndCallback)
871 {
872     LPSTR               verb, dev, args;
873     LPWINE_MCIDRIVER    wmd = 0;
874     DWORD               dwFlags = 0, dwRet = 0;
875     int                 offset = 0;
876     DWORD               data[MCI_DATA_SIZE];
877     DWORD               retType;
878     LPCSTR              lpCmd = 0;
879     LPSTR               devAlias = NULL;
880     BOOL                bAutoOpen = FALSE;
881
882     TRACE("('%s', %p, %d, %p)\n", lpstrCommand, lpstrRet, uRetLen, hwndCallback);
883
884     /* format is <command> <device> <optargs> */
885     if (!(verb = HeapAlloc(GetProcessHeap(), 0, strlen(lpstrCommand)+1)))
886         return MCIERR_OUT_OF_MEMORY;
887     strcpy( verb, lpstrCommand );
888
889     memset(data, 0, sizeof(data));
890
891     if (!(args = strchr(verb, ' '))) {
892         dwRet = MCIERR_MISSING_DEVICE_NAME;
893         goto errCleanUp;
894     }
895     *args++ = '\0';
896     if ((dwRet = MCI_GetString(&dev, &args))) {
897         goto errCleanUp;
898     }
899
900     /* case dev == 'new' has to be handled */
901     if (!strcasecmp(dev, "new")) {
902         FIXME("'new': NIY as device name\n");
903         dwRet = MCIERR_MISSING_DEVICE_NAME;
904         goto errCleanUp;
905     }
906
907     /* otherwise, try to grab devType from open */
908     if (!strcmp(verb, "open")) {
909         LPSTR   devType, tmp;
910
911         if ((devType = strchr(dev, '!')) != NULL) {
912             *devType++ = '\0';
913             tmp = devType; devType = dev; dev = tmp;
914
915             dwFlags |= MCI_OPEN_TYPE;
916             data[2] = (DWORD)devType;
917             devType = str_dup_upper(devType);
918             dwFlags |= MCI_OPEN_ELEMENT;
919             data[3] = (DWORD)dev;
920         } else if (strchr(dev, '.') == NULL) {
921             tmp = strchr(dev,' ');
922             if (tmp) *tmp = '\0';
923             data[2] = (DWORD)dev;
924             devType = str_dup_upper(dev);
925             if (tmp) *tmp = ' ';
926             dwFlags |= MCI_OPEN_TYPE;
927         } else {
928             if ((devType = strstr(args, "type ")) != NULL) {
929                 devType += 5;
930                 tmp = strchr(devType, ' ');
931                 if (tmp) *tmp = '\0';
932                 devType = str_dup_upper(devType);
933                 if (tmp) *tmp = ' ';
934                 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
935             } else {
936                 char    buf[32];
937                 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
938                     goto errCleanUp;
939
940                 devType = str_dup_upper(buf);
941             }
942             dwFlags |= MCI_OPEN_ELEMENT;
943             data[3] = (DWORD)dev;
944         }
945         if ((devAlias = strstr(args," alias "))) {
946             char *tmp2;
947             devAlias += 7;
948             if (!(tmp = strchr(devAlias,' '))) tmp = devAlias + strlen(devAlias);
949             if (tmp) *tmp = '\0';
950             tmp2 = HeapAlloc(GetProcessHeap(), 0, tmp - devAlias + 1 );
951             memcpy( tmp2, devAlias, tmp - devAlias );
952             tmp2[tmp - devAlias] = 0;
953             data[4] = (DWORD)tmp2;
954             /* should be done in regular options parsing */
955             /* dwFlags |= MCI_OPEN_ALIAS; */
956         }
957
958         dwRet = MCI_LoadMciDriver(devType, &wmd);
959         HeapFree(GetProcessHeap(), 0, devType);
960         if (dwRet) {
961             MCI_UnLoadMciDriver(wmd);
962             goto errCleanUp;
963         }
964     } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDA(dev)))) {
965         /* auto open */
966         char    buf[128];
967         sprintf(buf, "open %s wait", dev);
968
969         if ((dwRet = mciSendStringA(buf, NULL, 0, 0)) != 0)
970             goto errCleanUp;
971
972         wmd = MCI_GetDriver(mciGetDeviceIDA(dev));
973         if (!wmd) {
974             /* FIXME: memory leak, MCI driver is not closed */
975             dwRet = MCIERR_INVALID_DEVICE_ID;
976             goto errCleanUp;
977         }
978     }
979
980     /* get the verb in the different command tables */
981     if (wmd) {
982         /* try the device specific command table */
983         lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
984         if (!lpCmd) {
985             /* try the type specific command table */
986             if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
987                 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
988             if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
989                 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
990         }
991     }
992     /* try core command table */
993     if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
994
995     if (!lpCmd) {
996         TRACE("Command '%s' not found!\n", verb);
997         dwRet = MCIERR_UNRECOGNIZED_COMMAND;
998         goto errCleanUp;
999     }
1000
1001     /* set up call back */
1002     if (hwndCallback != 0) {
1003         dwFlags |= MCI_NOTIFY;
1004         data[0] = (DWORD)hwndCallback;
1005     }
1006
1007     /* set return information */
1008     switch (retType = MCI_GetReturnType(lpCmd)) {
1009     case 0:             offset = 1;     break;
1010     case MCI_INTEGER:   offset = 2;     break;
1011     case MCI_STRING:    data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
1012     case MCI_RECT:      offset = 5;     break;
1013     default:    ERR("oops\n");
1014     }
1015
1016     TRACE("verb='%s' on dev='%s'; offset=%d\n", verb, dev, offset);
1017
1018     if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1019         goto errCleanUp;
1020
1021     if (bAutoOpen && (dwFlags & MCI_NOTIFY)) {
1022         dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1023         goto errCleanUp;
1024     }
1025     /* FIXME: the command should get it's own notification window set up and
1026      * ask for device closing while processing the notification mechanism
1027      */
1028     if (lpstrRet && uRetLen) *lpstrRet = '\0';
1029
1030     TRACE("[%d, %s, %08lx, %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s]\n",
1031           wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1032           data[0], debugstr_a((char *)data[0]), data[1], debugstr_a((char *)data[1]),
1033           data[2], debugstr_a((char *)data[2]), data[3], debugstr_a((char *)data[3]),
1034           data[4], debugstr_a((char *)data[4]), data[5], debugstr_a((char *)data[5]));
1035
1036     if (strcmp(verb, "open") == 0) {
1037         if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSA)data, dwFlags)))
1038             MCI_UnLoadMciDriver(wmd);
1039         /* FIXME: notification is not properly shared across two opens */
1040     } else {
1041         dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD)data, TRUE);
1042     }
1043     TRACE("=> 1/ %lx (%s)\n", dwRet, lpstrRet);
1044     dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1045     TRACE("=> 2/ %lx (%s)\n", dwRet, lpstrRet);
1046
1047 errCleanUp:
1048     HeapFree(GetProcessHeap(), 0, verb);
1049     HeapFree(GetProcessHeap(), 0, devAlias);
1050     return dwRet;
1051 }
1052
1053 /**************************************************************************
1054  *                              mciSendStringW                  [WINMM.@]
1055  */
1056 DWORD WINAPI mciSendStringW(LPCWSTR lpwstrCommand, LPWSTR lpwstrRet,
1057                             UINT uRetLen, HWND hwndCallback)
1058 {
1059     LPSTR       lpstrCommand;
1060     LPSTR       lpstrRet = NULL;
1061     UINT        ret;
1062     INT len;
1063
1064     /* FIXME: is there something to do with lpstrReturnString ? */
1065     len = WideCharToMultiByte( CP_ACP, 0, lpwstrCommand, -1, NULL, 0, NULL, NULL );
1066     lpstrCommand = HeapAlloc( GetProcessHeap(), 0, len );
1067     WideCharToMultiByte( CP_ACP, 0, lpwstrCommand, -1, lpstrCommand, len, NULL, NULL );
1068     if (lpwstrRet)
1069     {
1070         lpstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1071         if (!lpstrRet) return MMSYSERR_NOMEM;
1072     }
1073     ret = mciSendStringA(lpstrCommand, lpstrRet, uRetLen, hwndCallback);
1074     if (lpwstrRet)
1075         MultiByteToWideChar( CP_ACP, 0, lpstrRet, -1, lpwstrRet, uRetLen );
1076     HeapFree(GetProcessHeap(), 0, lpstrCommand);
1077     if (lpstrRet) HeapFree(GetProcessHeap(), 0, lpstrRet);
1078     return ret;
1079 }
1080
1081 /**************************************************************************
1082  *                              mciExecute                      [WINMM.@]
1083  *                              mciExecute                      [MMSYSTEM.712]
1084  */
1085 DWORD WINAPI mciExecute(LPCSTR lpstrCommand)
1086 {
1087     char        strRet[256];
1088     DWORD       ret;
1089
1090     TRACE("(%s)!\n", lpstrCommand);
1091
1092     ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1093     if (ret != 0) {
1094         if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1095             sprintf(strRet, "Unknown MCI error (%ld)", ret);
1096         }
1097         MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1098     }
1099     /* FIXME: what shall I return ? */
1100     return 0;
1101 }
1102
1103 /**************************************************************************
1104  *                      mciLoadCommandResource                  [WINMM.@]
1105  *
1106  * Strangely, this function only exists as an UNICODE one.
1107  */
1108 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1109 {
1110     HRSRC               hRsrc = 0;
1111     HGLOBAL             hMem;
1112     UINT16              ret = MCI_NO_COMMAND_TABLE;
1113
1114     TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1115
1116     /* if a file named "resname.mci" exits, then load resource "resname" from it
1117      * otherwise directly from driver
1118      * We don't support it (who uses this feature ?), but we check anyway
1119      */
1120     if (!type) {
1121 #if 0
1122         /* FIXME: we should put this back into order, but I never found a program
1123          * actually using this feature, so we not need it
1124          */
1125         char            buf[128];
1126         OFSTRUCT        ofs;
1127
1128         strcat(strcpy(buf, resname), ".mci");
1129         if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1130             FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1131         }
1132 #endif
1133     }
1134     if (!(hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA))) {
1135         WARN("No command table found in resource\n");
1136     } else if ((hMem = LoadResource(hInst, hRsrc))) {
1137         ret = MCI_SetCommandTable(LockResource(hMem), type);
1138     } else {
1139         WARN("Couldn't load resource.\n");
1140     }
1141     TRACE("=> %04x\n", ret);
1142     return ret;
1143 }
1144
1145 /**************************************************************************
1146  *                      mciFreeCommandResource                  [WINMM.@]
1147  */
1148 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1149 {
1150     TRACE("(%08x)!\n", uTable);
1151
1152     return MCI_DeleteCommandTable(uTable);
1153 }
1154
1155 /**************************************************************************
1156  *                      MCI_SendCommandFrom32                   [internal]
1157  */
1158 DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1159 {
1160     DWORD               dwRet = MCIERR_INVALID_DEVICE_ID;
1161     LPWINE_MCIDRIVER    wmd = MCI_GetDriver(wDevID);
1162
1163     if (wmd) {
1164         if (wmd->bIs32) {
1165             dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1166         } else if (pFnMciMapMsg32ATo16) {
1167             WINMM_MapType       res;
1168
1169             switch (res = pFnMciMapMsg32ATo16(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1170             case WINMM_MAP_MSGERROR:
1171                 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1172                 dwRet = MCIERR_DRIVER_INTERNAL;
1173                 break;
1174             case WINMM_MAP_NOMEM:
1175                 TRACE("Problem mapping msg=%s from 32a to 16\n", MCI_MessageToString(wMsg));
1176                 dwRet = MCIERR_OUT_OF_MEMORY;
1177                 break;
1178             case WINMM_MAP_OK:
1179             case WINMM_MAP_OKMEM:
1180                 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1181                 if (res == WINMM_MAP_OKMEM)
1182                     pFnMciUnMapMsg32ATo16(wmd->wType, wMsg, dwParam1, dwParam2);
1183                 break;
1184             }
1185         }
1186     }
1187     return dwRet;
1188 }
1189
1190 /**************************************************************************
1191  *                      MCI_SendCommandFrom16                   [internal]
1192  */
1193 DWORD MCI_SendCommandFrom16(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1194 {
1195     DWORD               dwRet = MCIERR_INVALID_DEVICE_ID;
1196     LPWINE_MCIDRIVER    wmd = MCI_GetDriver(wDevID);
1197
1198     if (wmd) {
1199         dwRet = MCIERR_INVALID_DEVICE_ID;
1200
1201         if (wmd->bIs32 && pFnMciMapMsg16To32A) {
1202             WINMM_MapType               res;
1203
1204             switch (res = pFnMciMapMsg16To32A(wmd->wType, wMsg, &dwParam2)) {
1205             case WINMM_MAP_MSGERROR:
1206                 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1207                 dwRet = MCIERR_DRIVER_INTERNAL;
1208                 break;
1209             case WINMM_MAP_NOMEM:
1210                 TRACE("Problem mapping msg=%s from 16 to 32a\n", MCI_MessageToString(wMsg));
1211                 dwRet = MCIERR_OUT_OF_MEMORY;
1212                 break;
1213             case WINMM_MAP_OK:
1214             case WINMM_MAP_OKMEM:
1215                 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1216                 if (res == WINMM_MAP_OKMEM)
1217                     pFnMciUnMapMsg16To32A(wmd->wType, wMsg, dwParam2);
1218                 break;
1219             }
1220         } else {
1221             dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1222         }
1223     }
1224     return dwRet;
1225 }
1226
1227 /**************************************************************************
1228  *                      MCI_Open                                [internal]
1229  */
1230 static  DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSA lpParms)
1231 {
1232     char                        strDevTyp[128];
1233     DWORD                       dwRet;
1234     LPWINE_MCIDRIVER            wmd = NULL;
1235
1236     TRACE("(%08lX, %p)\n", dwParam, lpParms);
1237     if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1238
1239     /* only two low bytes are generic, the other ones are dev type specific */
1240 #define WINE_MCIDRIVER_SUPP     (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1241                          MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1242                          MCI_NOTIFY|MCI_WAIT)
1243     if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1244         FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1245     }
1246 #undef WINE_MCIDRIVER_SUPP
1247
1248     strDevTyp[0] = 0;
1249
1250     if (dwParam & MCI_OPEN_TYPE) {
1251         if (dwParam & MCI_OPEN_TYPE_ID) {
1252             WORD uDevType = LOWORD((DWORD)lpParms->lpstrDeviceType);
1253
1254             if (uDevType < MCI_DEVTYPE_FIRST ||
1255                 uDevType > MCI_DEVTYPE_LAST ||
1256                 !LoadStringA(WINMM_IData->hWinMM32Instance, uDevType, strDevTyp, sizeof(strDevTyp))) {
1257                 dwRet = MCIERR_BAD_INTEGER;
1258                 goto errCleanUp;
1259             }
1260         } else {
1261             LPSTR       ptr;
1262             if (lpParms->lpstrDeviceType == NULL) {
1263                 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1264                 goto errCleanUp;
1265             }
1266             strcpy(strDevTyp, lpParms->lpstrDeviceType);
1267             ptr = strchr(strDevTyp, '!');
1268             if (ptr) {
1269                 /* this behavior is not documented in windows. However, since, in
1270                  * some occasions, MCI_OPEN handling is translated by WinMM into
1271                  * a call to mciSendString("open <type>"); this code shall be correct
1272                  */
1273                 if (dwParam & MCI_OPEN_ELEMENT) {
1274                     ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1275                         lpParms->lpstrElementName, strDevTyp);
1276                     dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1277                     goto errCleanUp;
1278                 }
1279                 dwParam |= MCI_OPEN_ELEMENT;
1280                 *ptr++ = 0;
1281                 /* FIXME: not a good idea to write in user supplied buffer */
1282                 lpParms->lpstrElementName = ptr;
1283             }
1284
1285         }
1286         TRACE("devType='%s' !\n", strDevTyp);
1287     }
1288
1289     if (dwParam & MCI_OPEN_ELEMENT) {
1290         TRACE("lpstrElementName='%s'\n", lpParms->lpstrElementName);
1291
1292         if (dwParam & MCI_OPEN_ELEMENT_ID) {
1293             FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1294             dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1295             goto errCleanUp;
1296         }
1297
1298         if (!lpParms->lpstrElementName) {
1299             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1300             goto errCleanUp;
1301         }
1302
1303         /* type, if given as a parameter, supersedes file extension */
1304         if (!strDevTyp[0] &&
1305             MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1306                                        strDevTyp, sizeof(strDevTyp))) {
1307             if (GetDriveTypeA(lpParms->lpstrElementName) != DRIVE_CDROM) {
1308                 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1309                 goto errCleanUp;
1310             }
1311             /* FIXME: this will not work if several CDROM drives are installed on the machine */
1312             strcpy(strDevTyp, "CDAUDIO");
1313         }
1314     }
1315
1316     if (strDevTyp[0] == 0) {
1317         FIXME("Couldn't load driver\n");
1318         dwRet = MCIERR_INVALID_DEVICE_NAME;
1319         goto errCleanUp;
1320     }
1321
1322     if (dwParam & MCI_OPEN_ALIAS) {
1323         TRACE("Alias='%s' !\n", lpParms->lpstrAlias);
1324         if (!lpParms->lpstrAlias) {
1325             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1326             goto errCleanUp;
1327         }
1328     }
1329
1330     if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1331         goto errCleanUp;
1332     }
1333
1334     if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1335         TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08lx], closing\n", dwRet);
1336         /* FIXME: is dwRet the correct ret code ? */
1337         goto errCleanUp;
1338     }
1339
1340     /* only handled devices fall through */
1341     TRACE("wDevID=%04X wDeviceID=%d dwRet=%ld\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1342
1343     if (dwParam & MCI_NOTIFY)
1344         mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1345
1346     return 0;
1347 errCleanUp:
1348     if (wmd) MCI_UnLoadMciDriver(wmd);
1349
1350     if (dwParam & MCI_NOTIFY)
1351         mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1352     return dwRet;
1353 }
1354
1355 /**************************************************************************
1356  *                      MCI_Close                               [internal]
1357  */
1358 static  DWORD MCI_Close(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1359 {
1360     DWORD               dwRet;
1361     LPWINE_MCIDRIVER    wmd;
1362
1363     TRACE("(%04x, %08lX, %p)\n", wDevID, dwParam, lpParms);
1364
1365     if (wDevID == MCI_ALL_DEVICE_ID) {
1366         LPWINE_MCIDRIVER        next;
1367
1368         EnterCriticalSection(&WINMM_IData->cs);
1369         /* FIXME: shall I notify once after all is done, or for
1370          * each of the open drivers ? if the latest, which notif
1371          * to return when only one fails ?
1372          */
1373         for (wmd = WINMM_IData->lpMciDrvs; wmd; ) {
1374             next = wmd->lpNext;
1375             MCI_Close(wmd->wDeviceID, dwParam, lpParms);
1376             wmd = next;
1377         }
1378         LeaveCriticalSection(&WINMM_IData->cs);
1379         return 0;
1380     }
1381
1382     if (!(wmd = MCI_GetDriver(wDevID))) {
1383         return MCIERR_INVALID_DEVICE_ID;
1384     }
1385
1386     dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD)lpParms);
1387
1388     MCI_UnLoadMciDriver(wmd);
1389
1390     if (dwParam & MCI_NOTIFY)
1391         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1392                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1393
1394     return dwRet;
1395 }
1396
1397 /**************************************************************************
1398  *                      MCI_WriteString                         [internal]
1399  */
1400 DWORD   MCI_WriteString(LPSTR lpDstStr, DWORD dstSize, LPCSTR lpSrcStr)
1401 {
1402     DWORD       ret = 0;
1403
1404     if (lpSrcStr) {
1405         if (dstSize <= strlen(lpSrcStr)) {
1406             lstrcpynA(lpDstStr, lpSrcStr, dstSize - 1);
1407             ret = MCIERR_PARAM_OVERFLOW;
1408         } else {
1409             strcpy(lpDstStr, lpSrcStr);
1410         }
1411     } else {
1412         *lpDstStr = 0;
1413     }
1414     return ret;
1415 }
1416
1417 /**************************************************************************
1418  *                      MCI_Sysinfo                             [internal]
1419  */
1420 static  DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSA lpParms)
1421 {
1422     DWORD               ret = MCIERR_INVALID_DEVICE_ID;
1423     LPWINE_MCIDRIVER    wmd;
1424
1425     if (lpParms == NULL)                        return MCIERR_NULL_PARAMETER_BLOCK;
1426
1427     TRACE("(%08x, %08lX, %08lX[num=%ld, wDevTyp=%u])\n",
1428           uDevID, dwFlags, (DWORD)lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1429
1430     switch (dwFlags & ~MCI_SYSINFO_OPEN) {
1431     case MCI_SYSINFO_QUANTITY:
1432         {
1433             DWORD       cnt = 0;
1434
1435             if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST ||
1436                 lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
1437                 if (dwFlags & MCI_SYSINFO_OPEN) {
1438                     TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1439                     EnterCriticalSection(&WINMM_IData->cs);
1440                     for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
1441                         cnt++;
1442                     }
1443                     LeaveCriticalSection(&WINMM_IData->cs);
1444                 } else {
1445                     TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1446                     cnt = MCI_InstalledCount;
1447                 }
1448             } else {
1449                 if (dwFlags & MCI_SYSINFO_OPEN) {
1450                     TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n",
1451                           lpParms->wDeviceType);
1452                     EnterCriticalSection(&WINMM_IData->cs);
1453                     for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
1454                         if (wmd->wType == lpParms->wDeviceType)
1455                             cnt++;
1456                     }
1457                     LeaveCriticalSection(&WINMM_IData->cs);
1458                 } else {
1459                     TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n",
1460                           lpParms->wDeviceType);
1461                     FIXME("Don't know how to get # of MCI devices of a given type\n");
1462                     cnt = 1;
1463                 }
1464             }
1465             *(DWORD*)lpParms->lpstrReturn = cnt;
1466         }
1467         TRACE("(%ld) => '%ld'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1468         ret = MCI_INTEGER_RETURNED;
1469         break;
1470     case MCI_SYSINFO_INSTALLNAME:
1471         TRACE("MCI_SYSINFO_INSTALLNAME \n");
1472         if ((wmd = MCI_GetDriver(uDevID))) {
1473             ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1474                                   wmd->lpstrDeviceType);
1475         } else {
1476             *lpParms->lpstrReturn = 0;
1477             ret = MCIERR_INVALID_DEVICE_ID;
1478         }
1479         TRACE("(%ld) => '%s'\n", lpParms->dwNumber, lpParms->lpstrReturn);
1480         break;
1481     case MCI_SYSINFO_NAME:
1482         TRACE("MCI_SYSINFO_NAME\n");
1483         if (dwFlags & MCI_SYSINFO_OPEN) {
1484             FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1485             ret = MCIERR_UNRECOGNIZED_COMMAND;
1486         } else if (lpParms->dwNumber > MCI_InstalledCount) {
1487             ret = MCIERR_OUTOFRANGE;
1488         } else {
1489             DWORD       count = lpParms->dwNumber;
1490             LPSTR       ptr = MCI_lpInstallNames;
1491
1492             while (--count > 0) ptr += strlen(ptr) + 1;
1493             ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, ptr);
1494         }
1495         TRACE("(%ld) => '%s'\n", lpParms->dwNumber, lpParms->lpstrReturn);
1496         break;
1497     default:
1498         TRACE("Unsupported flag value=%08lx\n", dwFlags);
1499         ret = MCIERR_UNRECOGNIZED_COMMAND;
1500     }
1501     return ret;
1502 }
1503
1504 /**************************************************************************
1505  *                      MCI_Break                               [internal]
1506  */
1507 static  DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1508 {
1509     DWORD       dwRet = 0;
1510
1511     if (lpParms == NULL)        return MCIERR_NULL_PARAMETER_BLOCK;
1512
1513     if (dwFlags & MCI_NOTIFY)
1514         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1515                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1516
1517     return dwRet;
1518 }
1519
1520 /**************************************************************************
1521  *                      MCI_SendCommand                         [internal]
1522  */
1523 DWORD   MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD dwParam1,
1524                         DWORD dwParam2, BOOL bFrom32)
1525 {
1526     DWORD               dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1527
1528     switch (wMsg) {
1529     case MCI_OPEN:
1530         if (bFrom32) {
1531             dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSA)dwParam2);
1532         } else if (pFnMciMapMsg16To32A) {
1533             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1534             case WINMM_MAP_OK:
1535             case WINMM_MAP_OKMEM:
1536                 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSA)dwParam2);
1537                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1538                 break;
1539             default: break; /* so that gcc does not bark */
1540             }
1541         }
1542         break;
1543     case MCI_CLOSE:
1544         if (bFrom32) {
1545             dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1546         } else if (pFnMciMapMsg16To32A) {
1547             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1548             case WINMM_MAP_OK:
1549             case WINMM_MAP_OKMEM:
1550                 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1551                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1552                 break;
1553             default: break; /* so that gcc does not bark */
1554             }
1555         }
1556         break;
1557     case MCI_SYSINFO:
1558         if (bFrom32) {
1559             dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSA)dwParam2);
1560         } else if (pFnMciMapMsg16To32A) {
1561             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1562             case WINMM_MAP_OK:
1563             case WINMM_MAP_OKMEM:
1564                 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSA)dwParam2);
1565                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1566                 break;
1567             default: break; /* so that gcc doesnot  bark */
1568             }
1569         }
1570         break;
1571     case MCI_BREAK:
1572         if (bFrom32) {
1573             dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1574         } else if (pFnMciMapMsg16To32A) {
1575             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1576             case WINMM_MAP_OK:
1577             case WINMM_MAP_OKMEM:
1578                 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1579                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1580                 break;
1581             default: break; /* so that gcc does not bark */
1582             }
1583         }
1584         break;
1585     case MCI_SOUND:
1586         /* FIXME: it seems that MCI_SOUND needs the same handling as MCI_BREAK
1587          * but I couldn't get any doc on this MCI message
1588          */
1589         break;
1590     default:
1591         if (wDevID == MCI_ALL_DEVICE_ID) {
1592             FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1593             dwRet = MCIERR_CANNOT_USE_ALL;
1594         } else {
1595             dwRet = (bFrom32) ?
1596                 MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2) :
1597                 MCI_SendCommandFrom16(wDevID, wMsg, dwParam1, dwParam2);
1598         }
1599         break;
1600     }
1601     return dwRet;
1602 }
1603
1604 /**************************************************************************
1605  *                              MCI_CleanUp                     [internal]
1606  *
1607  * Some MCI commands need to be cleaned-up (when not called from
1608  * mciSendString), because MCI drivers return extra information for string
1609  * transformation. This function gets rid of them.
1610  */
1611 LRESULT         MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD dwParam2)
1612 {
1613     if (LOWORD(dwRet))
1614         return LOWORD(dwRet);
1615
1616     switch (wMsg) {
1617     case MCI_GETDEVCAPS:
1618         switch (dwRet & 0xFFFF0000ul) {
1619         case 0:
1620         case MCI_COLONIZED3_RETURN:
1621         case MCI_COLONIZED4_RETURN:
1622         case MCI_INTEGER_RETURNED:
1623             /* nothing to do */
1624             break;
1625         case MCI_RESOURCE_RETURNED:
1626         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1627             {
1628                 LPMCI_GETDEVCAPS_PARMS  lmgp;
1629
1630                 lmgp = (LPMCI_GETDEVCAPS_PARMS)(void*)dwParam2;
1631                 TRACE("Changing %08lx to %08lx\n", lmgp->dwReturn, (DWORD)LOWORD(lmgp->dwReturn));
1632                 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1633             }
1634             break;
1635         default:
1636             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1637                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1638         }
1639         break;
1640     case MCI_STATUS:
1641         switch (dwRet & 0xFFFF0000ul) {
1642         case 0:
1643         case MCI_COLONIZED3_RETURN:
1644         case MCI_COLONIZED4_RETURN:
1645         case MCI_INTEGER_RETURNED:
1646             /* nothing to do */
1647             break;
1648         case MCI_RESOURCE_RETURNED:
1649         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1650             {
1651                 LPMCI_STATUS_PARMS      lsp;
1652
1653                 lsp = (LPMCI_STATUS_PARMS)(void*)dwParam2;
1654                 TRACE("Changing %08lx to %08lx\n", lsp->dwReturn, (DWORD)LOWORD(lsp->dwReturn));
1655                 lsp->dwReturn = LOWORD(lsp->dwReturn);
1656             }
1657             break;
1658         default:
1659             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1660                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1661         }
1662         break;
1663     case MCI_SYSINFO:
1664         switch (dwRet & 0xFFFF0000ul) {
1665         case 0:
1666         case MCI_INTEGER_RETURNED:
1667             /* nothing to do */
1668             break;
1669         default:
1670             FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
1671         }
1672         break;
1673     default:
1674         if (HIWORD(dwRet)) {
1675             FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
1676                   dwRet, MCI_MessageToString(wMsg));
1677         }
1678         break;
1679     }
1680     return LOWORD(dwRet);
1681 }
1682
1683 /**************************************************************************
1684  *                      MCI_Init                        [internal]
1685  *
1686  * Initializes the MCI internal variables.
1687  *
1688  */
1689 BOOL MCI_Init(void)
1690 {
1691     LPSTR       ptr1, ptr2;
1692     HKEY        hWineConf;
1693     HKEY        hkey;
1694     DWORD       err;
1695     DWORD       type;
1696     DWORD       count = 2048;
1697
1698     MCI_InstalledCount = 0;
1699     ptr1 = MCI_lpInstallNames = HeapAlloc(GetProcessHeap(), 0, count);
1700
1701     if (!MCI_lpInstallNames)
1702         return FALSE;
1703
1704     /* FIXME: should do also some registry diving here ? */
1705     if (!(err = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config", &hWineConf)) &&
1706         !(err = RegOpenKeyA(hWineConf, "options", &hkey))) {
1707         err = RegQueryValueExA(hkey, "mci", 0, &type, MCI_lpInstallNames, &count);
1708         RegCloseKey(hkey);
1709
1710     }
1711     if (!err) {
1712         TRACE("Wine => '%s' \n", ptr1);
1713         while ((ptr2 = strchr(ptr1, ':')) != 0) {
1714             *ptr2++ = 0;
1715             TRACE("---> '%s' \n", ptr1);
1716             MCI_InstalledCount++;
1717             ptr1 = ptr2;
1718         }
1719         MCI_InstalledCount++;
1720         TRACE("---> '%s' \n", ptr1);
1721         ptr1 += strlen(ptr1) + 1;
1722     } else {
1723         GetPrivateProfileStringA("mci", NULL, "", MCI_lpInstallNames, count, "SYSTEM.INI");
1724         while (strlen(ptr1) > 0) {
1725             TRACE("---> '%s' \n", ptr1);
1726             ptr1 += strlen(ptr1) + 1;
1727             MCI_InstalledCount++;
1728         }
1729     }
1730     RegCloseKey(hWineConf);
1731     return TRUE;
1732 }