Fix the GL_MAX_VERTEX_UNITS_ARB undeclared compilation error.
[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 (strcasecmp(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     CharLowerA(verb);
889
890     memset(data, 0, sizeof(data));
891
892     if (!(args = strchr(verb, ' '))) {
893         dwRet = MCIERR_MISSING_DEVICE_NAME;
894         goto errCleanUp;
895     }
896     *args++ = '\0';
897     if ((dwRet = MCI_GetString(&dev, &args))) {
898         goto errCleanUp;
899     }
900
901     /* case dev == 'new' has to be handled */
902     if (!strcmp(dev, "new")) {
903         FIXME("'new': NIY as device name\n");
904         dwRet = MCIERR_MISSING_DEVICE_NAME;
905         goto errCleanUp;
906     }
907
908     /* otherwise, try to grab devType from open */
909     if (!strcmp(verb, "open")) {
910         LPSTR   devType, tmp;
911
912         if ((devType = strchr(dev, '!')) != NULL) {
913             *devType++ = '\0';
914             tmp = devType; devType = dev; dev = tmp;
915
916             dwFlags |= MCI_OPEN_TYPE;
917             data[2] = (DWORD)devType;
918             devType = str_dup_upper(devType);
919             dwFlags |= MCI_OPEN_ELEMENT;
920             data[3] = (DWORD)dev;
921         } else if (strchr(dev, '.') == NULL) {
922             tmp = strchr(dev,' ');
923             if (tmp) *tmp = '\0';
924             data[2] = (DWORD)dev;
925             devType = str_dup_upper(dev);
926             if (tmp) *tmp = ' ';
927             dwFlags |= MCI_OPEN_TYPE;
928         } else {
929             if ((devType = strstr(args, "type ")) != NULL) {
930                 devType += 5;
931                 tmp = strchr(devType, ' ');
932                 if (tmp) *tmp = '\0';
933                 devType = str_dup_upper(devType);
934                 if (tmp) *tmp = ' ';
935                 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
936             } else {
937                 char    buf[32];
938                 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
939                     goto errCleanUp;
940
941                 devType = str_dup_upper(buf);
942             }
943             dwFlags |= MCI_OPEN_ELEMENT;
944             data[3] = (DWORD)dev;
945         }
946         if ((devAlias = strstr(args," alias "))) {
947             char *tmp2;
948             devAlias += 7;
949             if (!(tmp = strchr(devAlias,' '))) tmp = devAlias + strlen(devAlias);
950             if (tmp) *tmp = '\0';
951             tmp2 = HeapAlloc(GetProcessHeap(), 0, tmp - devAlias + 1 );
952             memcpy( tmp2, devAlias, tmp - devAlias );
953             tmp2[tmp - devAlias] = 0;
954             data[4] = (DWORD)tmp2;
955             /* should be done in regular options parsing */
956             /* dwFlags |= MCI_OPEN_ALIAS; */
957         }
958
959         dwRet = MCI_LoadMciDriver(devType, &wmd);
960         if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
961             dwRet = MCIERR_INVALID_DEVICE_NAME;
962         HeapFree(GetProcessHeap(), 0, devType);
963         if (dwRet) {
964             MCI_UnLoadMciDriver(wmd);
965             goto errCleanUp;
966         }
967     } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDA(dev)))) {
968         /* auto open */
969         char    buf[128];
970         sprintf(buf, "open %s wait", dev);
971
972         if ((dwRet = mciSendStringA(buf, NULL, 0, 0)) != 0)
973             goto errCleanUp;
974
975         wmd = MCI_GetDriver(mciGetDeviceIDA(dev));
976         if (!wmd) {
977             /* FIXME: memory leak, MCI driver is not closed */
978             dwRet = MCIERR_INVALID_DEVICE_ID;
979             goto errCleanUp;
980         }
981     }
982
983     /* get the verb in the different command tables */
984     if (wmd) {
985         /* try the device specific command table */
986         lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
987         if (!lpCmd) {
988             /* try the type specific command table */
989             if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
990                 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
991             if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
992                 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
993         }
994     }
995     /* try core command table */
996     if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
997
998     if (!lpCmd) {
999         TRACE("Command '%s' not found!\n", verb);
1000         dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1001         goto errCleanUp;
1002     }
1003
1004     /* set up call back */
1005     if (hwndCallback != 0) {
1006         dwFlags |= MCI_NOTIFY;
1007         data[0] = (DWORD)hwndCallback;
1008     }
1009
1010     /* set return information */
1011     switch (retType = MCI_GetReturnType(lpCmd)) {
1012     case 0:             offset = 1;     break;
1013     case MCI_INTEGER:   offset = 2;     break;
1014     case MCI_STRING:    data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
1015     case MCI_RECT:      offset = 5;     break;
1016     default:    ERR("oops\n");
1017     }
1018
1019     TRACE("verb='%s' on dev='%s'; offset=%d\n", verb, dev, offset);
1020
1021     if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1022         goto errCleanUp;
1023
1024     if (bAutoOpen && (dwFlags & MCI_NOTIFY)) {
1025         dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1026         goto errCleanUp;
1027     }
1028     /* FIXME: the command should get it's own notification window set up and
1029      * ask for device closing while processing the notification mechanism
1030      */
1031     if (lpstrRet && uRetLen) *lpstrRet = '\0';
1032
1033     TRACE("[%d, %s, %08lx, %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s]\n",
1034           wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1035           data[0], debugstr_a((char *)data[0]), data[1], debugstr_a((char *)data[1]),
1036           data[2], debugstr_a((char *)data[2]), data[3], debugstr_a((char *)data[3]),
1037           data[4], debugstr_a((char *)data[4]), data[5], debugstr_a((char *)data[5]));
1038
1039     if (strcmp(verb, "open") == 0) {
1040         if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSA)data, dwFlags)))
1041             MCI_UnLoadMciDriver(wmd);
1042         /* FIXME: notification is not properly shared across two opens */
1043     } else {
1044         dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD)data, TRUE);
1045     }
1046     TRACE("=> 1/ %lx (%s)\n", dwRet, lpstrRet);
1047     dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1048     TRACE("=> 2/ %lx (%s)\n", dwRet, lpstrRet);
1049
1050 errCleanUp:
1051     HeapFree(GetProcessHeap(), 0, verb);
1052     HeapFree(GetProcessHeap(), 0, devAlias);
1053     return dwRet;
1054 }
1055
1056 /**************************************************************************
1057  *                              mciSendStringW                  [WINMM.@]
1058  */
1059 DWORD WINAPI mciSendStringW(LPCWSTR lpwstrCommand, LPWSTR lpwstrRet,
1060                             UINT uRetLen, HWND hwndCallback)
1061 {
1062     LPSTR       lpstrCommand;
1063     LPSTR       lpstrRet = NULL;
1064     UINT        ret;
1065     INT len;
1066
1067     /* FIXME: is there something to do with lpstrReturnString ? */
1068     len = WideCharToMultiByte( CP_ACP, 0, lpwstrCommand, -1, NULL, 0, NULL, NULL );
1069     lpstrCommand = HeapAlloc( GetProcessHeap(), 0, len );
1070     WideCharToMultiByte( CP_ACP, 0, lpwstrCommand, -1, lpstrCommand, len, NULL, NULL );
1071     if (lpwstrRet)
1072     {
1073         lpstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1074         if (!lpstrRet) return MMSYSERR_NOMEM;
1075     }
1076     ret = mciSendStringA(lpstrCommand, lpstrRet, uRetLen, hwndCallback);
1077     if (lpwstrRet)
1078         MultiByteToWideChar( CP_ACP, 0, lpstrRet, -1, lpwstrRet, uRetLen );
1079     HeapFree(GetProcessHeap(), 0, lpstrCommand);
1080     if (lpstrRet) HeapFree(GetProcessHeap(), 0, lpstrRet);
1081     return ret;
1082 }
1083
1084 /**************************************************************************
1085  *                              mciExecute                      [WINMM.@]
1086  *                              mciExecute                      [MMSYSTEM.712]
1087  */
1088 DWORD WINAPI mciExecute(LPCSTR lpstrCommand)
1089 {
1090     char        strRet[256];
1091     DWORD       ret;
1092
1093     TRACE("(%s)!\n", lpstrCommand);
1094
1095     ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1096     if (ret != 0) {
1097         if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1098             sprintf(strRet, "Unknown MCI error (%ld)", ret);
1099         }
1100         MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1101     }
1102     /* FIXME: what shall I return ? */
1103     return 0;
1104 }
1105
1106 /**************************************************************************
1107  *                      mciLoadCommandResource                  [WINMM.@]
1108  *
1109  * Strangely, this function only exists as an UNICODE one.
1110  */
1111 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1112 {
1113     HRSRC               hRsrc = 0;
1114     HGLOBAL             hMem;
1115     UINT16              ret = MCI_NO_COMMAND_TABLE;
1116
1117     TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1118
1119     /* if a file named "resname.mci" exits, then load resource "resname" from it
1120      * otherwise directly from driver
1121      * We don't support it (who uses this feature ?), but we check anyway
1122      */
1123     if (!type) {
1124 #if 0
1125         /* FIXME: we should put this back into order, but I never found a program
1126          * actually using this feature, so we not need it
1127          */
1128         char            buf[128];
1129         OFSTRUCT        ofs;
1130
1131         strcat(strcpy(buf, resname), ".mci");
1132         if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1133             FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1134         }
1135 #endif
1136     }
1137     if (!(hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA))) {
1138         WARN("No command table found in resource\n");
1139     } else if ((hMem = LoadResource(hInst, hRsrc))) {
1140         ret = MCI_SetCommandTable(LockResource(hMem), type);
1141     } else {
1142         WARN("Couldn't load resource.\n");
1143     }
1144     TRACE("=> %04x\n", ret);
1145     return ret;
1146 }
1147
1148 /**************************************************************************
1149  *                      mciFreeCommandResource                  [WINMM.@]
1150  */
1151 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1152 {
1153     TRACE("(%08x)!\n", uTable);
1154
1155     return MCI_DeleteCommandTable(uTable);
1156 }
1157
1158 /**************************************************************************
1159  *                      MCI_SendCommandFrom32                   [internal]
1160  */
1161 DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1162 {
1163     DWORD               dwRet = MCIERR_INVALID_DEVICE_ID;
1164     LPWINE_MCIDRIVER    wmd = MCI_GetDriver(wDevID);
1165
1166     if (wmd) {
1167         if (wmd->bIs32) {
1168             dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1169         } else if (pFnMciMapMsg32ATo16) {
1170             WINMM_MapType       res;
1171
1172             switch (res = pFnMciMapMsg32ATo16(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1173             case WINMM_MAP_MSGERROR:
1174                 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1175                 dwRet = MCIERR_DRIVER_INTERNAL;
1176                 break;
1177             case WINMM_MAP_NOMEM:
1178                 TRACE("Problem mapping msg=%s from 32a to 16\n", MCI_MessageToString(wMsg));
1179                 dwRet = MCIERR_OUT_OF_MEMORY;
1180                 break;
1181             case WINMM_MAP_OK:
1182             case WINMM_MAP_OKMEM:
1183                 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1184                 if (res == WINMM_MAP_OKMEM)
1185                     pFnMciUnMapMsg32ATo16(wmd->wType, wMsg, dwParam1, dwParam2);
1186                 break;
1187             }
1188         }
1189     }
1190     return dwRet;
1191 }
1192
1193 /**************************************************************************
1194  *                      MCI_SendCommandFrom16                   [internal]
1195  */
1196 DWORD MCI_SendCommandFrom16(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1197 {
1198     DWORD               dwRet = MCIERR_INVALID_DEVICE_ID;
1199     LPWINE_MCIDRIVER    wmd = MCI_GetDriver(wDevID);
1200
1201     if (wmd) {
1202         dwRet = MCIERR_INVALID_DEVICE_ID;
1203
1204         if (wmd->bIs32 && pFnMciMapMsg16To32A) {
1205             WINMM_MapType               res;
1206
1207             switch (res = pFnMciMapMsg16To32A(wmd->wType, wMsg, &dwParam2)) {
1208             case WINMM_MAP_MSGERROR:
1209                 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1210                 dwRet = MCIERR_DRIVER_INTERNAL;
1211                 break;
1212             case WINMM_MAP_NOMEM:
1213                 TRACE("Problem mapping msg=%s from 16 to 32a\n", MCI_MessageToString(wMsg));
1214                 dwRet = MCIERR_OUT_OF_MEMORY;
1215                 break;
1216             case WINMM_MAP_OK:
1217             case WINMM_MAP_OKMEM:
1218                 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1219                 if (res == WINMM_MAP_OKMEM)
1220                     pFnMciUnMapMsg16To32A(wmd->wType, wMsg, dwParam2);
1221                 break;
1222             }
1223         } else {
1224             dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1225         }
1226     }
1227     return dwRet;
1228 }
1229
1230 /**************************************************************************
1231  *                      MCI_Open                                [internal]
1232  */
1233 static  DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSA lpParms)
1234 {
1235     char                        strDevTyp[128];
1236     DWORD                       dwRet;
1237     LPWINE_MCIDRIVER            wmd = NULL;
1238
1239     TRACE("(%08lX, %p)\n", dwParam, lpParms);
1240     if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1241
1242     /* only two low bytes are generic, the other ones are dev type specific */
1243 #define WINE_MCIDRIVER_SUPP     (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1244                          MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1245                          MCI_NOTIFY|MCI_WAIT)
1246     if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1247         FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1248     }
1249 #undef WINE_MCIDRIVER_SUPP
1250
1251     strDevTyp[0] = 0;
1252
1253     if (dwParam & MCI_OPEN_TYPE) {
1254         if (dwParam & MCI_OPEN_TYPE_ID) {
1255             WORD uDevType = LOWORD((DWORD)lpParms->lpstrDeviceType);
1256
1257             if (uDevType < MCI_DEVTYPE_FIRST ||
1258                 uDevType > MCI_DEVTYPE_LAST ||
1259                 !LoadStringA(WINMM_IData->hWinMM32Instance, uDevType, strDevTyp, sizeof(strDevTyp))) {
1260                 dwRet = MCIERR_BAD_INTEGER;
1261                 goto errCleanUp;
1262             }
1263         } else {
1264             LPSTR       ptr;
1265             if (lpParms->lpstrDeviceType == NULL) {
1266                 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1267                 goto errCleanUp;
1268             }
1269             strcpy(strDevTyp, lpParms->lpstrDeviceType);
1270             ptr = strchr(strDevTyp, '!');
1271             if (ptr) {
1272                 /* this behavior is not documented in windows. However, since, in
1273                  * some occasions, MCI_OPEN handling is translated by WinMM into
1274                  * a call to mciSendString("open <type>"); this code shall be correct
1275                  */
1276                 if (dwParam & MCI_OPEN_ELEMENT) {
1277                     ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1278                         lpParms->lpstrElementName, strDevTyp);
1279                     dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1280                     goto errCleanUp;
1281                 }
1282                 dwParam |= MCI_OPEN_ELEMENT;
1283                 *ptr++ = 0;
1284                 /* FIXME: not a good idea to write in user supplied buffer */
1285                 lpParms->lpstrElementName = ptr;
1286             }
1287
1288         }
1289         TRACE("devType='%s' !\n", strDevTyp);
1290     }
1291
1292     if (dwParam & MCI_OPEN_ELEMENT) {
1293         TRACE("lpstrElementName='%s'\n", lpParms->lpstrElementName);
1294
1295         if (dwParam & MCI_OPEN_ELEMENT_ID) {
1296             FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1297             dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1298             goto errCleanUp;
1299         }
1300
1301         if (!lpParms->lpstrElementName) {
1302             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1303             goto errCleanUp;
1304         }
1305
1306         /* type, if given as a parameter, supersedes file extension */
1307         if (!strDevTyp[0] &&
1308             MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1309                                        strDevTyp, sizeof(strDevTyp))) {
1310             if (GetDriveTypeA(lpParms->lpstrElementName) != DRIVE_CDROM) {
1311                 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1312                 goto errCleanUp;
1313             }
1314             /* FIXME: this will not work if several CDROM drives are installed on the machine */
1315             strcpy(strDevTyp, "CDAUDIO");
1316         }
1317     }
1318
1319     if (strDevTyp[0] == 0) {
1320         FIXME("Couldn't load driver\n");
1321         dwRet = MCIERR_INVALID_DEVICE_NAME;
1322         goto errCleanUp;
1323     }
1324
1325     if (dwParam & MCI_OPEN_ALIAS) {
1326         TRACE("Alias='%s' !\n", lpParms->lpstrAlias);
1327         if (!lpParms->lpstrAlias) {
1328             dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1329             goto errCleanUp;
1330         }
1331     }
1332
1333     if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1334         goto errCleanUp;
1335     }
1336
1337     if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1338         TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08lx], closing\n", dwRet);
1339         /* FIXME: is dwRet the correct ret code ? */
1340         goto errCleanUp;
1341     }
1342
1343     /* only handled devices fall through */
1344     TRACE("wDevID=%04X wDeviceID=%d dwRet=%ld\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1345
1346     if (dwParam & MCI_NOTIFY)
1347         mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1348
1349     return 0;
1350 errCleanUp:
1351     if (wmd) MCI_UnLoadMciDriver(wmd);
1352
1353     if (dwParam & MCI_NOTIFY)
1354         mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1355     return dwRet;
1356 }
1357
1358 /**************************************************************************
1359  *                      MCI_Close                               [internal]
1360  */
1361 static  DWORD MCI_Close(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1362 {
1363     DWORD               dwRet;
1364     LPWINE_MCIDRIVER    wmd;
1365
1366     TRACE("(%04x, %08lX, %p)\n", wDevID, dwParam, lpParms);
1367
1368     if (wDevID == MCI_ALL_DEVICE_ID) {
1369         LPWINE_MCIDRIVER        next;
1370
1371         EnterCriticalSection(&WINMM_IData->cs);
1372         /* FIXME: shall I notify once after all is done, or for
1373          * each of the open drivers ? if the latest, which notif
1374          * to return when only one fails ?
1375          */
1376         for (wmd = WINMM_IData->lpMciDrvs; wmd; ) {
1377             next = wmd->lpNext;
1378             MCI_Close(wmd->wDeviceID, dwParam, lpParms);
1379             wmd = next;
1380         }
1381         LeaveCriticalSection(&WINMM_IData->cs);
1382         return 0;
1383     }
1384
1385     if (!(wmd = MCI_GetDriver(wDevID))) {
1386         return MCIERR_INVALID_DEVICE_ID;
1387     }
1388
1389     dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD)lpParms);
1390
1391     MCI_UnLoadMciDriver(wmd);
1392
1393     if (dwParam & MCI_NOTIFY)
1394         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1395                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1396
1397     return dwRet;
1398 }
1399
1400 /**************************************************************************
1401  *                      MCI_WriteString                         [internal]
1402  */
1403 DWORD   MCI_WriteString(LPSTR lpDstStr, DWORD dstSize, LPCSTR lpSrcStr)
1404 {
1405     DWORD       ret = 0;
1406
1407     if (lpSrcStr) {
1408         if (dstSize <= strlen(lpSrcStr)) {
1409             lstrcpynA(lpDstStr, lpSrcStr, dstSize - 1);
1410             ret = MCIERR_PARAM_OVERFLOW;
1411         } else {
1412             strcpy(lpDstStr, lpSrcStr);
1413         }
1414     } else {
1415         *lpDstStr = 0;
1416     }
1417     return ret;
1418 }
1419
1420 /**************************************************************************
1421  *                      MCI_Sysinfo                             [internal]
1422  */
1423 static  DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSA lpParms)
1424 {
1425     DWORD               ret = MCIERR_INVALID_DEVICE_ID;
1426     LPWINE_MCIDRIVER    wmd;
1427
1428     if (lpParms == NULL)                        return MCIERR_NULL_PARAMETER_BLOCK;
1429
1430     TRACE("(%08x, %08lX, %08lX[num=%ld, wDevTyp=%u])\n",
1431           uDevID, dwFlags, (DWORD)lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1432
1433     switch (dwFlags & ~MCI_SYSINFO_OPEN) {
1434     case MCI_SYSINFO_QUANTITY:
1435         {
1436             DWORD       cnt = 0;
1437
1438             if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST ||
1439                 lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
1440                 if (dwFlags & MCI_SYSINFO_OPEN) {
1441                     TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1442                     EnterCriticalSection(&WINMM_IData->cs);
1443                     for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
1444                         cnt++;
1445                     }
1446                     LeaveCriticalSection(&WINMM_IData->cs);
1447                 } else {
1448                     TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1449                     cnt = MCI_InstalledCount;
1450                 }
1451             } else {
1452                 if (dwFlags & MCI_SYSINFO_OPEN) {
1453                     TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n",
1454                           lpParms->wDeviceType);
1455                     EnterCriticalSection(&WINMM_IData->cs);
1456                     for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
1457                         if (wmd->wType == lpParms->wDeviceType)
1458                             cnt++;
1459                     }
1460                     LeaveCriticalSection(&WINMM_IData->cs);
1461                 } else {
1462                     TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n",
1463                           lpParms->wDeviceType);
1464                     FIXME("Don't know how to get # of MCI devices of a given type\n");
1465                     cnt = 1;
1466                 }
1467             }
1468             *(DWORD*)lpParms->lpstrReturn = cnt;
1469         }
1470         TRACE("(%ld) => '%ld'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1471         ret = MCI_INTEGER_RETURNED;
1472         break;
1473     case MCI_SYSINFO_INSTALLNAME:
1474         TRACE("MCI_SYSINFO_INSTALLNAME \n");
1475         if ((wmd = MCI_GetDriver(uDevID))) {
1476             ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1477                                   wmd->lpstrDeviceType);
1478         } else {
1479             *lpParms->lpstrReturn = 0;
1480             ret = MCIERR_INVALID_DEVICE_ID;
1481         }
1482         TRACE("(%ld) => '%s'\n", lpParms->dwNumber, lpParms->lpstrReturn);
1483         break;
1484     case MCI_SYSINFO_NAME:
1485         TRACE("MCI_SYSINFO_NAME\n");
1486         if (dwFlags & MCI_SYSINFO_OPEN) {
1487             FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1488             ret = MCIERR_UNRECOGNIZED_COMMAND;
1489         } else if (lpParms->dwNumber > MCI_InstalledCount) {
1490             ret = MCIERR_OUTOFRANGE;
1491         } else {
1492             DWORD       count = lpParms->dwNumber;
1493             LPSTR       ptr = MCI_lpInstallNames;
1494
1495             while (--count > 0) ptr += strlen(ptr) + 1;
1496             ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, ptr);
1497         }
1498         TRACE("(%ld) => '%s'\n", lpParms->dwNumber, lpParms->lpstrReturn);
1499         break;
1500     default:
1501         TRACE("Unsupported flag value=%08lx\n", dwFlags);
1502         ret = MCIERR_UNRECOGNIZED_COMMAND;
1503     }
1504     return ret;
1505 }
1506
1507 /**************************************************************************
1508  *                      MCI_Break                               [internal]
1509  */
1510 static  DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1511 {
1512     DWORD       dwRet = 0;
1513
1514     if (lpParms == NULL)        return MCIERR_NULL_PARAMETER_BLOCK;
1515
1516     if (dwFlags & MCI_NOTIFY)
1517         mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1518                         (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1519
1520     return dwRet;
1521 }
1522
1523 /**************************************************************************
1524  *                      MCI_SendCommand                         [internal]
1525  */
1526 DWORD   MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD dwParam1,
1527                         DWORD dwParam2, BOOL bFrom32)
1528 {
1529     DWORD               dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1530
1531     switch (wMsg) {
1532     case MCI_OPEN:
1533         if (bFrom32) {
1534             dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSA)dwParam2);
1535         } else if (pFnMciMapMsg16To32A) {
1536             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1537             case WINMM_MAP_OK:
1538             case WINMM_MAP_OKMEM:
1539                 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSA)dwParam2);
1540                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1541                 break;
1542             default: break; /* so that gcc does not bark */
1543             }
1544         }
1545         break;
1546     case MCI_CLOSE:
1547         if (bFrom32) {
1548             dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1549         } else if (pFnMciMapMsg16To32A) {
1550             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1551             case WINMM_MAP_OK:
1552             case WINMM_MAP_OKMEM:
1553                 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1554                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1555                 break;
1556             default: break; /* so that gcc does not bark */
1557             }
1558         }
1559         break;
1560     case MCI_SYSINFO:
1561         if (bFrom32) {
1562             dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSA)dwParam2);
1563         } else if (pFnMciMapMsg16To32A) {
1564             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1565             case WINMM_MAP_OK:
1566             case WINMM_MAP_OKMEM:
1567                 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSA)dwParam2);
1568                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1569                 break;
1570             default: break; /* so that gcc doesnot  bark */
1571             }
1572         }
1573         break;
1574     case MCI_BREAK:
1575         if (bFrom32) {
1576             dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1577         } else if (pFnMciMapMsg16To32A) {
1578             switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1579             case WINMM_MAP_OK:
1580             case WINMM_MAP_OKMEM:
1581                 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1582                 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1583                 break;
1584             default: break; /* so that gcc does not bark */
1585             }
1586         }
1587         break;
1588     case MCI_SOUND:
1589         /* FIXME: it seems that MCI_SOUND needs the same handling as MCI_BREAK
1590          * but I couldn't get any doc on this MCI message
1591          */
1592         break;
1593     default:
1594         if (wDevID == MCI_ALL_DEVICE_ID) {
1595             FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1596             dwRet = MCIERR_CANNOT_USE_ALL;
1597         } else {
1598             dwRet = (bFrom32) ?
1599                 MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2) :
1600                 MCI_SendCommandFrom16(wDevID, wMsg, dwParam1, dwParam2);
1601         }
1602         break;
1603     }
1604     return dwRet;
1605 }
1606
1607 /**************************************************************************
1608  *                              MCI_CleanUp                     [internal]
1609  *
1610  * Some MCI commands need to be cleaned-up (when not called from
1611  * mciSendString), because MCI drivers return extra information for string
1612  * transformation. This function gets rid of them.
1613  */
1614 LRESULT         MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD dwParam2)
1615 {
1616     if (LOWORD(dwRet))
1617         return LOWORD(dwRet);
1618
1619     switch (wMsg) {
1620     case MCI_GETDEVCAPS:
1621         switch (dwRet & 0xFFFF0000ul) {
1622         case 0:
1623         case MCI_COLONIZED3_RETURN:
1624         case MCI_COLONIZED4_RETURN:
1625         case MCI_INTEGER_RETURNED:
1626             /* nothing to do */
1627             break;
1628         case MCI_RESOURCE_RETURNED:
1629         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1630             {
1631                 LPMCI_GETDEVCAPS_PARMS  lmgp;
1632
1633                 lmgp = (LPMCI_GETDEVCAPS_PARMS)(void*)dwParam2;
1634                 TRACE("Changing %08lx to %08lx\n", lmgp->dwReturn, (DWORD)LOWORD(lmgp->dwReturn));
1635                 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1636             }
1637             break;
1638         default:
1639             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1640                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1641         }
1642         break;
1643     case MCI_STATUS:
1644         switch (dwRet & 0xFFFF0000ul) {
1645         case 0:
1646         case MCI_COLONIZED3_RETURN:
1647         case MCI_COLONIZED4_RETURN:
1648         case MCI_INTEGER_RETURNED:
1649             /* nothing to do */
1650             break;
1651         case MCI_RESOURCE_RETURNED:
1652         case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1653             {
1654                 LPMCI_STATUS_PARMS      lsp;
1655
1656                 lsp = (LPMCI_STATUS_PARMS)(void*)dwParam2;
1657                 TRACE("Changing %08lx to %08lx\n", lsp->dwReturn, (DWORD)LOWORD(lsp->dwReturn));
1658                 lsp->dwReturn = LOWORD(lsp->dwReturn);
1659             }
1660             break;
1661         default:
1662             FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1663                   HIWORD(dwRet), MCI_MessageToString(wMsg));
1664         }
1665         break;
1666     case MCI_SYSINFO:
1667         switch (dwRet & 0xFFFF0000ul) {
1668         case 0:
1669         case MCI_INTEGER_RETURNED:
1670             /* nothing to do */
1671             break;
1672         default:
1673             FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
1674         }
1675         break;
1676     default:
1677         if (HIWORD(dwRet)) {
1678             FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
1679                   dwRet, MCI_MessageToString(wMsg));
1680         }
1681         break;
1682     }
1683     return LOWORD(dwRet);
1684 }
1685
1686 /**************************************************************************
1687  *                      MCI_Init                        [internal]
1688  *
1689  * Initializes the MCI internal variables.
1690  *
1691  */
1692 BOOL MCI_Init(void)
1693 {
1694     LPSTR       ptr1, ptr2;
1695     HKEY        hWineConf;
1696     HKEY        hkey;
1697     DWORD       err;
1698     DWORD       type;
1699     DWORD       count = 2048;
1700
1701     MCI_InstalledCount = 0;
1702     ptr1 = MCI_lpInstallNames = HeapAlloc(GetProcessHeap(), 0, count);
1703
1704     if (!MCI_lpInstallNames)
1705         return FALSE;
1706
1707     /* FIXME: should do also some registry diving here ? */
1708     if (!(err = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config", &hWineConf)) &&
1709         !(err = RegOpenKeyA(hWineConf, "options", &hkey))) {
1710         err = RegQueryValueExA(hkey, "mci", 0, &type, MCI_lpInstallNames, &count);
1711         RegCloseKey(hkey);
1712
1713     }
1714     if (!err) {
1715         TRACE("Wine => '%s' \n", ptr1);
1716         while ((ptr2 = strchr(ptr1, ':')) != 0) {
1717             *ptr2++ = 0;
1718             TRACE("---> '%s' \n", ptr1);
1719             MCI_InstalledCount++;
1720             ptr1 = ptr2;
1721         }
1722         MCI_InstalledCount++;
1723         TRACE("---> '%s' \n", ptr1);
1724         ptr1 += strlen(ptr1) + 1;
1725     } else {
1726         GetPrivateProfileStringA("mci", NULL, "", MCI_lpInstallNames, count, "SYSTEM.INI");
1727         while (strlen(ptr1) > 0) {
1728             TRACE("---> '%s' \n", ptr1);
1729             ptr1 += strlen(ptr1) + 1;
1730             MCI_InstalledCount++;
1731         }
1732     }
1733     RegCloseKey(hWineConf);
1734     return TRUE;
1735 }