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