- Moved Wine extension to get DSound interface from a wave device from
[wine/multimedia.git] / dlls / winmm / mci.c
blob0aa8104e81bbafc9c109c2cd58b2ce4978d4bcec
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * MCI internal functions
6 * Copyright 1998/1999 Eric Pouech
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
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"
39 #include "digitalv.h"
40 #include "winemm.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(mci);
46 static int MCI_InstalledCount;
47 static LPSTR MCI_lpInstallNames /* = NULL */;
49 WINMM_MapType (*pFnMciMapMsg16To32A) (WORD,WORD,DWORD*) /* = NULL */;
50 WINMM_MapType (*pFnMciUnMapMsg16To32A)(WORD,WORD,DWORD) /* = NULL */;
51 WINMM_MapType (*pFnMciMapMsg32ATo16) (WORD,WORD,DWORD,DWORD*) /* = NULL */;
52 WINMM_MapType (*pFnMciUnMapMsg32ATo16)(WORD,WORD,DWORD,DWORD) /* = NULL */;
54 /* First MCI valid device ID (0 means error) */
55 #define MCI_MAGIC 0x0001
57 /* dup a string and uppercase it */
58 inline static LPSTR str_dup_upper( LPCSTR str )
60 INT len = strlen(str) + 1;
61 LPSTR p = HeapAlloc( GetProcessHeap(), 0, len );
62 if (p)
64 memcpy( p, str, len );
65 CharUpperA( p );
67 return p;
70 /**************************************************************************
71 * MCI_GetDriver [internal]
73 LPWINE_MCIDRIVER MCI_GetDriver(UINT16 wDevID)
75 LPWINE_MCIDRIVER wmd = 0;
77 EnterCriticalSection(&WINMM_IData->cs);
78 for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
79 if (wmd->wDeviceID == wDevID)
80 break;
82 LeaveCriticalSection(&WINMM_IData->cs);
83 return wmd;
86 /**************************************************************************
87 * MCI_GetDriverFromString [internal]
89 UINT MCI_GetDriverFromString(LPCSTR lpstrName)
91 LPWINE_MCIDRIVER wmd;
92 UINT ret = 0;
94 if (!lpstrName)
95 return 0;
97 if (!lstrcmpiA(lpstrName, "ALL"))
98 return MCI_ALL_DEVICE_ID;
100 EnterCriticalSection(&WINMM_IData->cs);
101 for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
102 if (wmd->lpstrElementName && strcmp(wmd->lpstrElementName, lpstrName) == 0) {
103 ret = wmd->wDeviceID;
104 break;
106 if (wmd->lpstrDeviceType && strcasecmp(wmd->lpstrDeviceType, lpstrName) == 0) {
107 ret = wmd->wDeviceID;
108 break;
110 if (wmd->lpstrAlias && strcasecmp(wmd->lpstrAlias, lpstrName) == 0) {
111 ret = wmd->wDeviceID;
112 break;
115 LeaveCriticalSection(&WINMM_IData->cs);
117 return ret;
120 /**************************************************************************
121 * MCI_MessageToString [internal]
123 const char* MCI_MessageToString(UINT16 wMsg)
125 static char buffer[100];
127 #define CASE(s) case (s): return #s
129 switch (wMsg) {
130 CASE(MCI_BREAK);
131 CASE(MCI_CLOSE);
132 CASE(MCI_CLOSE_DRIVER);
133 CASE(MCI_COPY);
134 CASE(MCI_CUE);
135 CASE(MCI_CUT);
136 CASE(MCI_DELETE);
137 CASE(MCI_ESCAPE);
138 CASE(MCI_FREEZE);
139 CASE(MCI_PAUSE);
140 CASE(MCI_PLAY);
141 CASE(MCI_GETDEVCAPS);
142 CASE(MCI_INFO);
143 CASE(MCI_LOAD);
144 CASE(MCI_OPEN);
145 CASE(MCI_OPEN_DRIVER);
146 CASE(MCI_PASTE);
147 CASE(MCI_PUT);
148 CASE(MCI_REALIZE);
149 CASE(MCI_RECORD);
150 CASE(MCI_RESUME);
151 CASE(MCI_SAVE);
152 CASE(MCI_SEEK);
153 CASE(MCI_SET);
154 CASE(MCI_SPIN);
155 CASE(MCI_STATUS);
156 CASE(MCI_STEP);
157 CASE(MCI_STOP);
158 CASE(MCI_SYSINFO);
159 CASE(MCI_UNFREEZE);
160 CASE(MCI_UPDATE);
161 CASE(MCI_WHERE);
162 CASE(MCI_WINDOW);
163 /* constants for digital video */
164 CASE(MCI_CAPTURE);
165 CASE(MCI_MONITOR);
166 CASE(MCI_RESERVE);
167 CASE(MCI_SETAUDIO);
168 CASE(MCI_SIGNAL);
169 CASE(MCI_SETVIDEO);
170 CASE(MCI_QUALITY);
171 CASE(MCI_LIST);
172 CASE(MCI_UNDO);
173 CASE(MCI_CONFIGURE);
174 CASE(MCI_RESTORE);
175 #undef CASE
176 default:
177 sprintf(buffer, "MCI_<<%04X>>", wMsg);
178 return buffer;
182 /**************************************************************************
183 * MCI_GetDevTypeFromFileName [internal]
185 static DWORD MCI_GetDevTypeFromFileName(LPCSTR fileName, LPSTR buf, UINT len)
187 LPSTR tmp;
189 if ((tmp = strrchr(fileName, '.'))) {
190 GetProfileStringA("mci extensions", tmp + 1, "*", buf, len);
191 if (strcmp(buf, "*") != 0) {
192 return 0;
194 TRACE("No [mci extensions] entry for '%s' found.\n", tmp);
196 return MCIERR_EXTENSION_NOT_FOUND;
199 #define MAX_MCICMDTABLE 20
200 #define MCI_COMMAND_TABLE_NOT_LOADED 0xFFFE
202 typedef struct tagWINE_MCICMDTABLE {
203 UINT uDevType;
204 LPCSTR lpTable;
205 UINT nVerbs; /* number of verbs in command table */
206 LPCSTR* aVerbs; /* array of verbs to speed up the verb look up process */
207 } WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
209 static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
211 /**************************************************************************
212 * MCI_IsCommandTableValid [internal]
214 static BOOL MCI_IsCommandTableValid(UINT uTbl)
216 LPCSTR lmem, str;
217 DWORD flg;
218 WORD eid;
219 int idx = 0;
220 BOOL inCst = FALSE;
222 TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
223 uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
225 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
226 return FALSE;
228 lmem = S_MciCmdTable[uTbl].lpTable;
229 do {
230 do {
231 str = lmem;
232 lmem += strlen(lmem) + 1;
233 flg = *(LPDWORD)lmem;
234 eid = *(LPWORD)(lmem + sizeof(DWORD));
235 lmem += sizeof(DWORD) + sizeof(WORD);
236 idx ++;
237 /* EPP TRACE("cmd='%s' %08lx %04x\n", str, flg, eid); */
238 switch (eid) {
239 case MCI_COMMAND_HEAD: if (!*str || !flg) return FALSE; idx = 0; break; /* check unicity of str in table */
240 case MCI_STRING: if (inCst) return FALSE; break;
241 case MCI_INTEGER: if (!*str) return FALSE; break;
242 case MCI_END_COMMAND: if (*str || flg || idx == 0) return FALSE; idx = 0; break;
243 case MCI_RETURN: if (*str || idx != 1) return FALSE; break;
244 case MCI_FLAG: if (!*str) return FALSE; break;
245 case MCI_END_COMMAND_LIST: if (*str || flg) return FALSE; idx = 0; break;
246 case MCI_RECT: if (!*str || inCst) return FALSE; break;
247 case MCI_CONSTANT: if (inCst) return FALSE; inCst = TRUE; break;
248 case MCI_END_CONSTANT: if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
249 default: return FALSE;
251 } while (eid != MCI_END_COMMAND_LIST);
252 } while (eid != MCI_END_COMMAND_LIST);
253 return TRUE;
256 /**************************************************************************
257 * MCI_DumpCommandTable [internal]
259 static BOOL MCI_DumpCommandTable(UINT uTbl)
261 LPCSTR lmem;
262 LPCSTR str;
263 DWORD flg;
264 WORD eid;
266 if (!MCI_IsCommandTableValid(uTbl)) {
267 ERR("Ooops: %d is not valid\n", uTbl);
268 return FALSE;
271 lmem = S_MciCmdTable[uTbl].lpTable;
272 do {
273 do {
274 str = lmem;
275 lmem += strlen(lmem) + 1;
276 flg = *(LPDWORD)lmem;
277 eid = *(LPWORD)(lmem + sizeof(DWORD));
278 TRACE("cmd='%s' %08lx %04x\n", str, flg, eid);
279 lmem += sizeof(DWORD) + sizeof(WORD);
280 } while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
281 TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : "");
282 } while (eid != MCI_END_COMMAND_LIST);
283 return TRUE;
287 /**************************************************************************
288 * MCI_GetCommandTable [internal]
290 static UINT MCI_GetCommandTable(UINT uDevType)
292 UINT uTbl;
293 char buf[32];
294 LPSTR str = NULL;
296 /* first look up existing for existing devType */
297 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
298 if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
299 return uTbl;
302 /* well try to load id */
303 if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
304 if (LoadStringA(WINMM_IData->hWinMM32Instance, uDevType, buf, sizeof(buf))) {
305 str = buf;
307 } else if (uDevType == 0) {
308 str = "CORE";
310 uTbl = MCI_NO_COMMAND_TABLE;
311 if (str) {
312 HRSRC hRsrc = FindResourceA(WINMM_IData->hWinMM32Instance, str, (LPCSTR)RT_RCDATA);
313 HANDLE hMem = 0;
315 if (hRsrc) hMem = LoadResource(WINMM_IData->hWinMM32Instance, hRsrc);
316 if (hMem) {
317 uTbl = MCI_SetCommandTable(LockResource(hMem), uDevType);
318 } else {
319 WARN("No command table found in resource %p[%s]\n",
320 WINMM_IData->hWinMM32Instance, str);
323 TRACE("=> %d\n", uTbl);
324 return uTbl;
327 /**************************************************************************
328 * MCI_SetCommandTable [internal]
330 UINT MCI_SetCommandTable(void *table, UINT uDevType)
332 int uTbl;
333 static BOOL bInitDone = FALSE;
335 /* <HACK>
336 * The CORE command table must be loaded first, so that MCI_GetCommandTable()
337 * can be called with 0 as a uDevType to retrieve it.
338 * </HACK>
340 if (!bInitDone) {
341 bInitDone = TRUE;
342 MCI_GetCommandTable(0);
345 for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
346 if (!S_MciCmdTable[uTbl].lpTable) {
347 LPCSTR lmem, str;
348 WORD eid;
349 WORD count;
351 S_MciCmdTable[uTbl].uDevType = uDevType;
352 S_MciCmdTable[uTbl].lpTable = table;
354 if (TRACE_ON(mci)) {
355 MCI_DumpCommandTable(uTbl);
358 /* create the verbs table */
359 /* get # of entries */
360 lmem = S_MciCmdTable[uTbl].lpTable;
361 count = 0;
362 do {
363 lmem += strlen(lmem) + 1;
364 eid = *(LPWORD)(lmem + sizeof(DWORD));
365 lmem += sizeof(DWORD) + sizeof(WORD);
366 if (eid == MCI_COMMAND_HEAD)
367 count++;
368 } while (eid != MCI_END_COMMAND_LIST);
370 S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCSTR));
371 S_MciCmdTable[uTbl].nVerbs = count;
373 lmem = S_MciCmdTable[uTbl].lpTable;
374 count = 0;
375 do {
376 str = lmem;
377 lmem += strlen(lmem) + 1;
378 eid = *(LPWORD)(lmem + sizeof(DWORD));
379 lmem += sizeof(DWORD) + sizeof(WORD);
380 if (eid == MCI_COMMAND_HEAD)
381 S_MciCmdTable[uTbl].aVerbs[count++] = str;
382 } while (eid != MCI_END_COMMAND_LIST);
383 /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
384 return uTbl;
388 return MCI_NO_COMMAND_TABLE;
391 /**************************************************************************
392 * MCI_DeleteCommandTable [internal]
394 static BOOL MCI_DeleteCommandTable(UINT uTbl)
396 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
397 return FALSE;
399 S_MciCmdTable[uTbl].lpTable = NULL;
400 if (S_MciCmdTable[uTbl].aVerbs) {
401 HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTbl].aVerbs);
402 S_MciCmdTable[uTbl].aVerbs = 0;
404 return TRUE;
407 /**************************************************************************
408 * MCI_UnLoadMciDriver [internal]
410 static BOOL MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
412 LPWINE_MCIDRIVER* tmp;
414 if (!wmd)
415 return TRUE;
417 CloseDriver(wmd->hDriver, 0, 0);
419 if (wmd->dwPrivate != 0)
420 WARN("Unloading mci driver with non nul dwPrivate field\n");
422 EnterCriticalSection(&WINMM_IData->cs);
423 for (tmp = &WINMM_IData->lpMciDrvs; *tmp; tmp = &(*tmp)->lpNext) {
424 if (*tmp == wmd) {
425 *tmp = wmd->lpNext;
426 break;
429 LeaveCriticalSection(&WINMM_IData->cs);
431 HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
432 HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
433 HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);
435 HeapFree(GetProcessHeap(), 0, wmd);
436 return TRUE;
439 /**************************************************************************
440 * MCI_OpenMciDriver [internal]
442 static BOOL MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCSTR drvTyp, LPARAM lp)
444 char libName[128];
446 if (!DRIVER_GetLibName(drvTyp, "mci", libName, sizeof(libName)))
447 return FALSE;
449 wmd->bIs32 = 0xFFFF;
450 /* First load driver */
451 if ((wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp))) {
452 wmd->bIs32 = TRUE;
453 } else if (WINMM_CheckForMMSystem() && pFnMciMapMsg32ATo16) {
454 WINMM_MapType res;
456 switch (res = pFnMciMapMsg32ATo16(0, DRV_OPEN, 0, &lp)) {
457 case WINMM_MAP_MSGERROR:
458 TRACE("Not handled yet (DRV_OPEN)\n");
459 break;
460 case WINMM_MAP_NOMEM:
461 TRACE("Problem mapping msg=DRV_OPEN from 32a to 16\n");
462 break;
463 case WINMM_MAP_OK:
464 case WINMM_MAP_OKMEM:
465 if ((wmd->hDriver = OpenDriverA(drvTyp, "mci", lp)))
466 wmd->bIs32 = FALSE;
467 if (res == WINMM_MAP_OKMEM)
468 pFnMciUnMapMsg32ATo16(0, DRV_OPEN, 0, lp);
469 break;
472 return (wmd->bIs32 == 0xFFFF) ? FALSE : TRUE;
475 /**************************************************************************
476 * MCI_LoadMciDriver [internal]
478 static DWORD MCI_LoadMciDriver(LPCSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
480 LPSTR strDevTyp = str_dup_upper(_strDevTyp);
481 LPWINE_MCIDRIVER wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
482 MCI_OPEN_DRIVER_PARMSA modp;
483 DWORD dwRet = 0;
485 if (!wmd || !strDevTyp) {
486 dwRet = MCIERR_OUT_OF_MEMORY;
487 goto errCleanUp;
490 wmd->lpfnYieldProc = MCI_DefYieldProc;
491 wmd->dwYieldData = VK_CANCEL;
492 wmd->CreatorThread = GetCurrentThreadId();
494 EnterCriticalSection(&WINMM_IData->cs);
495 /* wmd must be inserted in list before sending opening the driver, coz' it
496 * may want to lookup at wDevID
498 wmd->lpNext = WINMM_IData->lpMciDrvs;
499 WINMM_IData->lpMciDrvs = wmd;
501 for (modp.wDeviceID = MCI_MAGIC;
502 MCI_GetDriver(modp.wDeviceID) != 0;
503 modp.wDeviceID++);
505 wmd->wDeviceID = modp.wDeviceID;
507 LeaveCriticalSection(&WINMM_IData->cs);
509 TRACE("wDevID=%04X \n", modp.wDeviceID);
511 modp.lpstrParams = NULL;
513 if (!MCI_OpenMciDriver(wmd, strDevTyp, (LPARAM)&modp)) {
514 /* silence warning if all is used... some bogus program use commands like
515 * 'open all'...
517 if (strcasecmp(strDevTyp, "all") == 0) {
518 dwRet = MCIERR_CANNOT_USE_ALL;
519 } else {
520 FIXME("Couldn't load driver for type %s.\n"
521 "If you don't have a windows installation accessible from Wine,\n"
522 "you perhaps forgot to create a [mci] section in system.ini\n",
523 strDevTyp);
524 dwRet = MCIERR_DEVICE_NOT_INSTALLED;
526 goto errCleanUp;
529 /* FIXME: should also check that module's description is of the form
530 * MODULENAME:[MCI] comment
533 /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
534 wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
535 wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;
537 TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
538 wmd->hDriver, strDevTyp, modp.wType, modp.wCustomCommandTable);
540 wmd->lpstrDeviceType = strDevTyp;
541 wmd->wType = modp.wType;
543 TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
544 modp.wDeviceID, modp.wType, modp.wDeviceID);
545 *lpwmd = wmd;
546 return 0;
547 errCleanUp:
548 MCI_UnLoadMciDriver(wmd);
549 HeapFree(GetProcessHeap(), 0, strDevTyp);
550 *lpwmd = 0;
551 return dwRet;
554 /**************************************************************************
555 * MCI_FinishOpen [internal]
557 static DWORD MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSA lpParms,
558 DWORD dwParam)
560 if (dwParam & MCI_OPEN_ELEMENT)
562 wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,strlen(lpParms->lpstrElementName)+1);
563 strcpy( wmd->lpstrElementName, lpParms->lpstrElementName );
565 if (dwParam & MCI_OPEN_ALIAS)
567 wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, strlen(lpParms->lpstrAlias)+1);
568 strcpy( wmd->lpstrAlias, lpParms->lpstrAlias);
570 lpParms->wDeviceID = wmd->wDeviceID;
572 return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
573 (DWORD)lpParms);
576 /**************************************************************************
577 * MCI_FindCommand [internal]
579 static LPCSTR MCI_FindCommand(UINT uTbl, LPCSTR verb)
581 UINT idx;
583 if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
584 return NULL;
586 /* another improvement would be to have the aVerbs array sorted,
587 * so that we could use a dichotomic search on it, rather than this dumb
588 * array look up
590 for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
591 if (strcmp(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
592 return S_MciCmdTable[uTbl].aVerbs[idx];
595 return NULL;
598 /**************************************************************************
599 * MCI_GetReturnType [internal]
601 static DWORD MCI_GetReturnType(LPCSTR lpCmd)
603 lpCmd += strlen(lpCmd) + 1 + sizeof(DWORD) + sizeof(WORD);
604 if (*lpCmd == '\0' && *(LPWORD)(lpCmd + 1 + sizeof(DWORD)) == MCI_RETURN) {
605 return *(LPDWORD)(lpCmd + 1);
607 return 0L;
610 /**************************************************************************
611 * MCI_GetMessage [internal]
613 static WORD MCI_GetMessage(LPCSTR lpCmd)
615 return (WORD)*(LPDWORD)(lpCmd + strlen(lpCmd) + 1);
618 /**************************************************************************
619 * MCI_GetDWord [internal]
621 static BOOL MCI_GetDWord(LPDWORD data, LPSTR* ptr)
623 DWORD val;
624 LPSTR ret;
626 val = strtoul(*ptr, &ret, 0);
628 switch (*ret) {
629 case '\0': break;
630 case ' ': ret++; break;
631 default: return FALSE;
634 *data |= val;
635 *ptr = ret;
636 return TRUE;
639 /**************************************************************************
640 * MCI_GetString [internal]
642 static DWORD MCI_GetString(LPSTR* str, LPSTR* args)
644 LPSTR ptr = *args;
646 /* see if we have a quoted string */
647 if (*ptr == '"') {
648 ptr = strchr(*str = ptr + 1, '"');
649 if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
650 /* FIXME: shall we escape \" from string ?? */
651 if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
652 *ptr++ = '\0'; /* remove trailing " */
653 if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
654 *ptr++ = '\0';
655 } else {
656 ptr = strchr(ptr, ' ');
658 if (ptr) {
659 *ptr++ = '\0';
660 } else {
661 ptr = *args + strlen(*args);
663 *str = *args;
666 *args = ptr;
667 return 0;
670 #define MCI_DATA_SIZE 16
672 /**************************************************************************
673 * MCI_ParseOptArgs [internal]
675 static DWORD MCI_ParseOptArgs(LPDWORD data, int _offset, LPCSTR lpCmd,
676 LPSTR args, LPDWORD dwFlags)
678 int len, offset;
679 LPCSTR lmem, str;
680 DWORD dwRet, flg, cflg = 0;
681 WORD eid;
682 BOOL inCst, found;
684 /* loop on arguments */
685 while (*args) {
686 lmem = lpCmd;
687 found = inCst = FALSE;
688 offset = _offset;
690 /* skip any leading white space(s) */
691 while (*args == ' ') args++;
692 TRACE("args='%s' offset=%d\n", args, offset);
694 do { /* loop on options for command table for the requested verb */
695 str = lmem;
696 lmem += (len = strlen(lmem)) + 1;
697 flg = *(LPDWORD)lmem;
698 eid = *(LPWORD)(lmem + sizeof(DWORD));
699 lmem += sizeof(DWORD) + sizeof(WORD);
700 /* EPP TRACE("\tcmd='%s' inCst=%c eid=%04x\n", str, inCst ? 'Y' : 'N', eid); */
702 switch (eid) {
703 case MCI_CONSTANT:
704 inCst = TRUE; cflg = flg; break;
705 case MCI_END_CONSTANT:
706 /* there may be additional integral values after flag in constant */
707 if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
708 *dwFlags |= cflg;
710 inCst = FALSE; cflg = 0;
711 break;
714 if (strncasecmp(args, str, len) == 0 &&
715 (args[len] == 0 || args[len] == ' ')) {
716 /* store good values into data[] */
717 args += len;
718 while (*args == ' ') args++;
719 found = TRUE;
721 switch (eid) {
722 case MCI_COMMAND_HEAD:
723 case MCI_RETURN:
724 case MCI_END_COMMAND:
725 case MCI_END_COMMAND_LIST:
726 case MCI_CONSTANT: /* done above */
727 case MCI_END_CONSTANT: /* done above */
728 break;
729 case MCI_FLAG:
730 *dwFlags |= flg;
731 break;
732 case MCI_INTEGER:
733 if (inCst) {
734 data[offset] |= flg;
735 *dwFlags |= cflg;
736 inCst = FALSE;
737 } else {
738 *dwFlags |= flg;
739 if (!MCI_GetDWord(&(data[offset]), &args)) {
740 return MCIERR_BAD_INTEGER;
743 break;
744 case MCI_RECT:
745 /* store rect in data (offset...offset+3) */
746 *dwFlags |= flg;
747 if (!MCI_GetDWord(&(data[offset+0]), &args) ||
748 !MCI_GetDWord(&(data[offset+1]), &args) ||
749 !MCI_GetDWord(&(data[offset+2]), &args) ||
750 !MCI_GetDWord(&(data[offset+3]), &args)) {
751 ERR("Bad rect '%s'\n", args);
752 return MCIERR_BAD_INTEGER;
754 break;
755 case MCI_STRING:
756 *dwFlags |= flg;
757 if ((dwRet = MCI_GetString((LPSTR*)&data[offset], &args)))
758 return dwRet;
759 break;
760 default: ERR("oops\n");
762 /* exit inside while loop, except if just entered in constant area definition */
763 if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
764 } else {
765 /* have offset incremented if needed */
766 switch (eid) {
767 case MCI_COMMAND_HEAD:
768 case MCI_RETURN:
769 case MCI_END_COMMAND:
770 case MCI_END_COMMAND_LIST:
771 case MCI_CONSTANT:
772 case MCI_FLAG: break;
773 case MCI_INTEGER: if (!inCst) offset++; break;
774 case MCI_END_CONSTANT:
775 case MCI_STRING: offset++; break;
776 case MCI_RECT: offset += 4; break;
777 default: ERR("oops\n");
780 } while (eid != MCI_END_COMMAND);
781 if (!found) {
782 WARN("Optarg '%s' not found\n", args);
783 return MCIERR_UNRECOGNIZED_COMMAND;
785 if (offset == MCI_DATA_SIZE) {
786 ERR("Internal data[] buffer overflow\n");
787 return MCIERR_PARSER_INTERNAL;
790 return 0;
793 /**************************************************************************
794 * MCI_HandleReturnValues [internal]
796 static DWORD MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType,
797 LPDWORD data, LPSTR lpstrRet, UINT uRetLen)
799 if (lpstrRet) {
800 switch (retType) {
801 case 0: /* nothing to return */
802 break;
803 case MCI_INTEGER:
804 switch (dwRet & 0xFFFF0000ul) {
805 case 0:
806 case MCI_INTEGER_RETURNED:
807 snprintf(lpstrRet, uRetLen, "%ld", data[1]);
808 break;
809 case MCI_RESOURCE_RETURNED:
810 /* return string which ID is HIWORD(data[1]),
811 * string is loaded from mmsystem.dll */
812 LoadStringA(WINMM_IData->hWinMM32Instance, HIWORD(data[1]),
813 lpstrRet, uRetLen);
814 break;
815 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
816 /* return string which ID is HIWORD(data[1]),
817 * string is loaded from driver */
818 /* FIXME: this is wrong for a 16 bit handle */
819 LoadStringA(GetDriverModuleHandle(wmd->hDriver),
820 HIWORD(data[1]), lpstrRet, uRetLen);
821 break;
822 case MCI_COLONIZED3_RETURN:
823 snprintf(lpstrRet, uRetLen, "%d:%d:%d",
824 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
825 LOBYTE(HIWORD(data[1])));
826 break;
827 case MCI_COLONIZED4_RETURN:
828 snprintf(lpstrRet, uRetLen, "%d:%d:%d:%d",
829 LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
830 LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
831 break;
832 default: ERR("Ooops (%04X)\n", HIWORD(dwRet));
834 break;
835 case MCI_STRING:
836 switch (dwRet & 0xFFFF0000ul) {
837 case 0:
838 /* nothing to do data[1] == lpstrRet */
839 break;
840 case MCI_INTEGER_RETURNED:
841 data[1] = *(LPDWORD)lpstrRet;
842 snprintf(lpstrRet, uRetLen, "%ld", data[1]);
843 break;
844 default:
845 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
846 break;
848 break;
849 case MCI_RECT:
850 if (dwRet & 0xFFFF0000ul)
851 WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
852 snprintf(lpstrRet, uRetLen, "%ld %ld %ld %ld",
853 data[1], data[2], data[3], data[4]);
854 break;
855 default: ERR("oops\n");
858 return LOWORD(dwRet);
861 /**************************************************************************
862 * mciSendStringA [WINMM.@]
864 DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
865 UINT uRetLen, HWND hwndCallback)
867 LPSTR verb, dev, args;
868 LPWINE_MCIDRIVER wmd = 0;
869 DWORD dwFlags = 0, dwRet = 0;
870 int offset = 0;
871 DWORD data[MCI_DATA_SIZE];
872 DWORD retType;
873 LPCSTR lpCmd = 0;
874 LPSTR devAlias = NULL;
875 BOOL bAutoOpen = FALSE;
877 TRACE("('%s', %p, %d, %p)\n", lpstrCommand, lpstrRet, uRetLen, hwndCallback);
879 /* format is <command> <device> <optargs> */
880 if (!(verb = HeapAlloc(GetProcessHeap(), 0, strlen(lpstrCommand)+1)))
881 return MCIERR_OUT_OF_MEMORY;
882 strcpy( verb, lpstrCommand );
884 memset(data, 0, sizeof(data));
886 if (!(args = strchr(verb, ' '))) {
887 dwRet = MCIERR_MISSING_DEVICE_NAME;
888 goto errCleanUp;
890 *args++ = '\0';
891 if ((dwRet = MCI_GetString(&dev, &args))) {
892 goto errCleanUp;
895 /* case dev == 'new' has to be handled */
896 if (!strcasecmp(dev, "new")) {
897 FIXME("'new': NIY as device name\n");
898 dwRet = MCIERR_MISSING_DEVICE_NAME;
899 goto errCleanUp;
902 /* otherwise, try to grab devType from open */
903 if (!strcmp(verb, "open")) {
904 LPSTR devType, tmp;
906 if ((devType = strchr(dev, '!')) != NULL) {
907 *devType++ = '\0';
908 tmp = devType; devType = dev; dev = tmp;
910 dwFlags |= MCI_OPEN_TYPE;
911 data[2] = (DWORD)devType;
912 devType = str_dup_upper(devType);
913 dwFlags |= MCI_OPEN_ELEMENT;
914 data[3] = (DWORD)dev;
915 } else if (strchr(dev, '.') == NULL) {
916 tmp = strchr(dev,' ');
917 if (tmp) *tmp = '\0';
918 data[2] = (DWORD)dev;
919 devType = str_dup_upper(dev);
920 if (tmp) *tmp = ' ';
921 dwFlags |= MCI_OPEN_TYPE;
922 } else {
923 if ((devType = strstr(args, "type ")) != NULL) {
924 devType += 5;
925 tmp = strchr(devType, ' ');
926 if (tmp) *tmp = '\0';
927 devType = str_dup_upper(devType);
928 if (tmp) *tmp = ' ';
929 /* dwFlags and data[2] will be correctly set in ParseOpt loop */
930 } else {
931 char buf[32];
932 if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
933 goto errCleanUp;
935 devType = str_dup_upper(buf);
937 dwFlags |= MCI_OPEN_ELEMENT;
938 data[3] = (DWORD)dev;
940 if ((devAlias = strstr(args," alias "))) {
941 char *tmp2;
942 devAlias += 7;
943 if (!(tmp = strchr(devAlias,' '))) tmp = devAlias + strlen(devAlias);
944 if (tmp) *tmp = '\0';
945 tmp2 = HeapAlloc(GetProcessHeap(), 0, tmp - devAlias + 1 );
946 memcpy( tmp2, devAlias, tmp - devAlias );
947 tmp2[tmp - devAlias] = 0;
948 data[4] = (DWORD)tmp2;
949 /* should be done in regular options parsing */
950 /* dwFlags |= MCI_OPEN_ALIAS; */
953 dwRet = MCI_LoadMciDriver(devType, &wmd);
954 HeapFree(GetProcessHeap(), 0, devType);
955 if (dwRet) {
956 MCI_UnLoadMciDriver(wmd);
957 goto errCleanUp;
959 } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDA(dev)))) {
960 /* auto open */
961 char buf[128];
962 sprintf(buf, "open %s wait", dev);
964 if ((dwRet = mciSendStringA(buf, NULL, 0, 0)) != 0)
965 goto errCleanUp;
967 wmd = MCI_GetDriver(mciGetDeviceIDA(dev));
968 if (!wmd) {
969 /* FIXME: memory leak, MCI driver is not closed */
970 dwRet = MCIERR_INVALID_DEVICE_ID;
971 goto errCleanUp;
975 /* get the verb in the different command tables */
976 if (wmd) {
977 /* try the device specific command table */
978 lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
979 if (!lpCmd) {
980 /* try the type specific command table */
981 if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
982 wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
983 if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
984 lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
987 /* try core command table */
988 if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
990 if (!lpCmd) {
991 TRACE("Command '%s' not found!\n", verb);
992 dwRet = MCIERR_UNRECOGNIZED_COMMAND;
993 goto errCleanUp;
996 /* set up call back */
997 if (hwndCallback != 0) {
998 dwFlags |= MCI_NOTIFY;
999 data[0] = (DWORD)hwndCallback;
1002 /* set return information */
1003 switch (retType = MCI_GetReturnType(lpCmd)) {
1004 case 0: offset = 1; break;
1005 case MCI_INTEGER: offset = 2; break;
1006 case MCI_STRING: data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
1007 case MCI_RECT: offset = 5; break;
1008 default: ERR("oops\n");
1011 TRACE("verb='%s' on dev='%s'; offset=%d\n", verb, dev, offset);
1013 if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
1014 goto errCleanUp;
1016 if (bAutoOpen && (dwFlags & MCI_NOTIFY)) {
1017 dwRet = MCIERR_NOTIFY_ON_AUTO_OPEN;
1018 goto errCleanUp;
1020 /* FIXME: the command should get it's own notification window set up and
1021 * ask for device closing while processing the notification mechanism
1023 if (lpstrRet && uRetLen) *lpstrRet = '\0';
1025 #define STR_OF(_x) (IsBadReadPtr((char*)_x,1)?"?":(char*)(_x))
1026 TRACE("[%d, %s, %08lx, %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s %08lx/%s]\n",
1027 wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1028 data[0], STR_OF(data[0]), data[1], STR_OF(data[1]),
1029 data[2], STR_OF(data[2]), data[3], STR_OF(data[3]),
1030 data[4], STR_OF(data[4]), data[5], STR_OF(data[5]));
1031 #undef STR_OF
1033 if (strcmp(verb, "open") == 0) {
1034 if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSA)data, dwFlags)))
1035 MCI_UnLoadMciDriver(wmd);
1036 /* FIXME: notification is not properly shared across two opens */
1037 } else {
1038 dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD)data, TRUE);
1040 TRACE("=> 1/ %lx (%s)\n", dwRet, lpstrRet);
1041 dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1042 TRACE("=> 2/ %lx (%s)\n", dwRet, lpstrRet);
1044 errCleanUp:
1045 HeapFree(GetProcessHeap(), 0, verb);
1046 HeapFree(GetProcessHeap(), 0, devAlias);
1047 return dwRet;
1050 /**************************************************************************
1051 * mciSendStringW [WINMM.@]
1053 DWORD WINAPI mciSendStringW(LPCWSTR lpwstrCommand, LPWSTR lpwstrRet,
1054 UINT uRetLen, HWND hwndCallback)
1056 LPSTR lpstrCommand;
1057 LPSTR lpstrRet = NULL;
1058 UINT ret;
1059 INT len;
1061 /* FIXME: is there something to do with lpstrReturnString ? */
1062 len = WideCharToMultiByte( CP_ACP, 0, lpwstrCommand, -1, NULL, 0, NULL, NULL );
1063 lpstrCommand = HeapAlloc( GetProcessHeap(), 0, len );
1064 WideCharToMultiByte( CP_ACP, 0, lpwstrCommand, -1, lpstrCommand, len, NULL, NULL );
1065 if (lpwstrRet)
1067 lpstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1068 if (!lpstrRet) return MMSYSERR_NOMEM;
1070 ret = mciSendStringA(lpstrCommand, lpstrRet, uRetLen, hwndCallback);
1071 if (lpwstrRet)
1072 MultiByteToWideChar( CP_ACP, 0, lpstrRet, -1, lpwstrRet, uRetLen );
1073 HeapFree(GetProcessHeap(), 0, lpstrCommand);
1074 if (lpstrRet) HeapFree(GetProcessHeap(), 0, lpstrRet);
1075 return ret;
1078 /**************************************************************************
1079 * mciExecute [WINMM.@]
1080 * mciExecute [MMSYSTEM.712]
1082 DWORD WINAPI mciExecute(LPCSTR lpstrCommand)
1084 char strRet[256];
1085 DWORD ret;
1087 TRACE("(%s)!\n", lpstrCommand);
1089 ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1090 if (ret != 0) {
1091 if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1092 sprintf(strRet, "Unknown MCI error (%ld)", ret);
1094 MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1096 /* FIXME: what shall I return ? */
1097 return 0;
1100 /**************************************************************************
1101 * mciLoadCommandResource [WINMM.@]
1103 * Strangely, this function only exists as an UNICODE one.
1105 UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1107 HRSRC hRsrc = 0;
1108 HGLOBAL hMem;
1109 UINT16 ret = MCI_NO_COMMAND_TABLE;
1111 TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1113 /* if a file named "resname.mci" exits, then load resource "resname" from it
1114 * otherwise directly from driver
1115 * We don't support it (who uses this feature ?), but we check anyway
1117 if (!type) {
1118 #if 0
1119 /* FIXME: we should put this back into order, but I never found a program
1120 * actually using this feature, so we not need it
1122 char buf[128];
1123 OFSTRUCT ofs;
1125 strcat(strcpy(buf, resname), ".mci");
1126 if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
1127 FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
1129 #endif
1131 if (!(hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA))) {
1132 WARN("No command table found in resource\n");
1133 } else if ((hMem = LoadResource(hInst, hRsrc))) {
1134 ret = MCI_SetCommandTable(LockResource(hMem), type);
1135 } else {
1136 WARN("Couldn't load resource.\n");
1138 TRACE("=> %04x\n", ret);
1139 return ret;
1142 /**************************************************************************
1143 * mciFreeCommandResource [WINMM.@]
1145 BOOL WINAPI mciFreeCommandResource(UINT uTable)
1147 TRACE("(%08x)!\n", uTable);
1149 return MCI_DeleteCommandTable(uTable);
1152 /**************************************************************************
1153 * MCI_SendCommandFrom32 [internal]
1155 DWORD MCI_SendCommandFrom32(UINT wDevID, UINT16 wMsg, DWORD dwParam1, DWORD dwParam2)
1157 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
1158 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
1160 if (wmd) {
1161 if (wmd->bIs32) {
1162 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1163 } else if (pFnMciMapMsg32ATo16) {
1164 WINMM_MapType res;
1166 switch (res = pFnMciMapMsg32ATo16(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1167 case WINMM_MAP_MSGERROR:
1168 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1169 dwRet = MCIERR_DRIVER_INTERNAL;
1170 break;
1171 case WINMM_MAP_NOMEM:
1172 TRACE("Problem mapping msg=%s from 32a to 16\n", MCI_MessageToString(wMsg));
1173 dwRet = MCIERR_OUT_OF_MEMORY;
1174 break;
1175 case WINMM_MAP_OK:
1176 case WINMM_MAP_OKMEM:
1177 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1178 if (res == WINMM_MAP_OKMEM)
1179 pFnMciUnMapMsg32ATo16(wmd->wType, wMsg, dwParam1, dwParam2);
1180 break;
1184 return dwRet;
1187 /**************************************************************************
1188 * MCI_SendCommandFrom16 [internal]
1190 DWORD MCI_SendCommandFrom16(UINT wDevID, UINT16 wMsg, DWORD dwParam1, DWORD dwParam2)
1192 DWORD dwRet = MCIERR_INVALID_DEVICE_ID;
1193 LPWINE_MCIDRIVER wmd = MCI_GetDriver(wDevID);
1195 if (wmd) {
1196 dwRet = MCIERR_INVALID_DEVICE_ID;
1198 if (wmd->bIs32 && pFnMciMapMsg16To32A) {
1199 WINMM_MapType res;
1201 switch (res = pFnMciMapMsg16To32A(wmd->wType, wMsg, &dwParam2)) {
1202 case WINMM_MAP_MSGERROR:
1203 TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1204 dwRet = MCIERR_DRIVER_INTERNAL;
1205 break;
1206 case WINMM_MAP_NOMEM:
1207 TRACE("Problem mapping msg=%s from 16 to 32a\n", MCI_MessageToString(wMsg));
1208 dwRet = MCIERR_OUT_OF_MEMORY;
1209 break;
1210 case WINMM_MAP_OK:
1211 case WINMM_MAP_OKMEM:
1212 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1213 if (res == WINMM_MAP_OKMEM)
1214 pFnMciUnMapMsg16To32A(wmd->wType, wMsg, dwParam2);
1215 break;
1217 } else {
1218 dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1221 return dwRet;
1224 /**************************************************************************
1225 * MCI_Open [internal]
1227 static DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSA lpParms)
1229 char strDevTyp[128];
1230 DWORD dwRet;
1231 LPWINE_MCIDRIVER wmd = NULL;
1233 TRACE("(%08lX, %p)\n", dwParam, lpParms);
1234 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1236 /* only two low bytes are generic, the other ones are dev type specific */
1237 #define WINE_MCIDRIVER_SUPP (0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1238 MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
1239 MCI_NOTIFY|MCI_WAIT)
1240 if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
1241 FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1243 #undef WINE_MCIDRIVER_SUPP
1245 strDevTyp[0] = 0;
1247 if (dwParam & MCI_OPEN_TYPE) {
1248 if (dwParam & MCI_OPEN_TYPE_ID) {
1249 WORD uDevType = LOWORD((DWORD)lpParms->lpstrDeviceType);
1251 if (uDevType < MCI_DEVTYPE_FIRST ||
1252 uDevType > MCI_DEVTYPE_LAST ||
1253 !LoadStringA(WINMM_IData->hWinMM32Instance, uDevType, strDevTyp, sizeof(strDevTyp))) {
1254 dwRet = MCIERR_BAD_INTEGER;
1255 goto errCleanUp;
1257 } else {
1258 LPSTR ptr;
1259 if (lpParms->lpstrDeviceType == NULL) {
1260 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1261 goto errCleanUp;
1263 strcpy(strDevTyp, lpParms->lpstrDeviceType);
1264 ptr = strchr(strDevTyp, '!');
1265 if (ptr) {
1266 /* this behavior is not documented in windows. However, since, in
1267 * some occasions, MCI_OPEN handling is translated by WinMM into
1268 * a call to mciSendString("open <type>"); this code shall be correct
1270 if (dwParam & MCI_OPEN_ELEMENT) {
1271 ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1272 lpParms->lpstrElementName, strDevTyp);
1273 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1274 goto errCleanUp;
1276 dwParam |= MCI_OPEN_ELEMENT;
1277 *ptr++ = 0;
1278 /* FIXME: not a good idea to write in user supplied buffer */
1279 lpParms->lpstrElementName = ptr;
1283 TRACE("devType='%s' !\n", strDevTyp);
1286 if (dwParam & MCI_OPEN_ELEMENT) {
1287 TRACE("lpstrElementName='%s'\n", lpParms->lpstrElementName);
1289 if (dwParam & MCI_OPEN_ELEMENT_ID) {
1290 FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
1291 dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
1292 goto errCleanUp;
1295 if (!lpParms->lpstrElementName) {
1296 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1297 goto errCleanUp;
1300 /* type, if given as a parameter, supersedes file extension */
1301 if (!strDevTyp[0] &&
1302 MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1303 strDevTyp, sizeof(strDevTyp))) {
1304 if (GetDriveTypeA(lpParms->lpstrElementName) != DRIVE_CDROM) {
1305 dwRet = MCIERR_EXTENSION_NOT_FOUND;
1306 goto errCleanUp;
1308 /* FIXME: this will not work if several CDROM drives are installed on the machine */
1309 strcpy(strDevTyp, "CDAUDIO");
1313 if (strDevTyp[0] == 0) {
1314 FIXME("Couldn't load driver\n");
1315 dwRet = MCIERR_INVALID_DEVICE_NAME;
1316 goto errCleanUp;
1319 if (dwParam & MCI_OPEN_ALIAS) {
1320 TRACE("Alias='%s' !\n", lpParms->lpstrAlias);
1321 if (!lpParms->lpstrAlias) {
1322 dwRet = MCIERR_NULL_PARAMETER_BLOCK;
1323 goto errCleanUp;
1327 if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1328 goto errCleanUp;
1331 if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1332 TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08lx], closing\n", dwRet);
1333 /* FIXME: is dwRet the correct ret code ? */
1334 goto errCleanUp;
1337 /* only handled devices fall through */
1338 TRACE("wDevID=%04X wDeviceID=%d dwRet=%ld\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1340 if (dwParam & MCI_NOTIFY)
1341 mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1343 return 0;
1344 errCleanUp:
1345 if (wmd) MCI_UnLoadMciDriver(wmd);
1347 if (dwParam & MCI_NOTIFY)
1348 mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1349 return dwRet;
1352 /**************************************************************************
1353 * MCI_Close [internal]
1355 static DWORD MCI_Close(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1357 DWORD dwRet;
1358 LPWINE_MCIDRIVER wmd;
1360 TRACE("(%04x, %08lX, %p)\n", wDevID, dwParam, lpParms);
1362 if (wDevID == MCI_ALL_DEVICE_ID) {
1363 LPWINE_MCIDRIVER next;
1365 EnterCriticalSection(&WINMM_IData->cs);
1366 /* FIXME: shall I notify once after all is done, or for
1367 * each of the open drivers ? if the latest, which notif
1368 * to return when only one fails ?
1370 for (wmd = WINMM_IData->lpMciDrvs; wmd; ) {
1371 next = wmd->lpNext;
1372 MCI_Close(wmd->wDeviceID, dwParam, lpParms);
1373 wmd = next;
1375 LeaveCriticalSection(&WINMM_IData->cs);
1376 return 0;
1379 if (!(wmd = MCI_GetDriver(wDevID))) {
1380 return MCIERR_INVALID_DEVICE_ID;
1383 dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD)lpParms);
1385 MCI_UnLoadMciDriver(wmd);
1387 if (dwParam & MCI_NOTIFY)
1388 mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1389 (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1391 return dwRet;
1394 /**************************************************************************
1395 * MCI_WriteString [internal]
1397 DWORD MCI_WriteString(LPSTR lpDstStr, DWORD dstSize, LPCSTR lpSrcStr)
1399 DWORD ret = 0;
1401 if (lpSrcStr) {
1402 if (dstSize <= strlen(lpSrcStr)) {
1403 lstrcpynA(lpDstStr, lpSrcStr, dstSize - 1);
1404 ret = MCIERR_PARAM_OVERFLOW;
1405 } else {
1406 strcpy(lpDstStr, lpSrcStr);
1408 } else {
1409 *lpDstStr = 0;
1411 return ret;
1414 /**************************************************************************
1415 * MCI_Sysinfo [internal]
1417 static DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSA lpParms)
1419 DWORD ret = MCIERR_INVALID_DEVICE_ID;
1420 LPWINE_MCIDRIVER wmd;
1422 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1424 TRACE("(%08x, %08lX, %08lX[num=%ld, wDevTyp=%u])\n",
1425 uDevID, dwFlags, (DWORD)lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1427 switch (dwFlags & ~MCI_SYSINFO_OPEN) {
1428 case MCI_SYSINFO_QUANTITY:
1430 DWORD cnt = 0;
1432 if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST ||
1433 lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
1434 if (dwFlags & MCI_SYSINFO_OPEN) {
1435 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1436 EnterCriticalSection(&WINMM_IData->cs);
1437 for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
1438 cnt++;
1440 LeaveCriticalSection(&WINMM_IData->cs);
1441 } else {
1442 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1443 cnt = MCI_InstalledCount;
1445 } else {
1446 if (dwFlags & MCI_SYSINFO_OPEN) {
1447 TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n",
1448 lpParms->wDeviceType);
1449 EnterCriticalSection(&WINMM_IData->cs);
1450 for (wmd = WINMM_IData->lpMciDrvs; wmd; wmd = wmd->lpNext) {
1451 if (wmd->wType == lpParms->wDeviceType)
1452 cnt++;
1454 LeaveCriticalSection(&WINMM_IData->cs);
1455 } else {
1456 TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n",
1457 lpParms->wDeviceType);
1458 FIXME("Don't know how to get # of MCI devices of a given type\n");
1459 cnt = 1;
1462 *(DWORD*)lpParms->lpstrReturn = cnt;
1464 TRACE("(%ld) => '%ld'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1465 ret = MCI_INTEGER_RETURNED;
1466 break;
1467 case MCI_SYSINFO_INSTALLNAME:
1468 TRACE("MCI_SYSINFO_INSTALLNAME \n");
1469 if ((wmd = MCI_GetDriver(uDevID))) {
1470 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1471 wmd->lpstrDeviceType);
1472 } else {
1473 *lpParms->lpstrReturn = 0;
1474 ret = MCIERR_INVALID_DEVICE_ID;
1476 TRACE("(%ld) => '%s'\n", lpParms->dwNumber, lpParms->lpstrReturn);
1477 break;
1478 case MCI_SYSINFO_NAME:
1479 TRACE("MCI_SYSINFO_NAME\n");
1480 if (dwFlags & MCI_SYSINFO_OPEN) {
1481 FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1482 ret = MCIERR_UNRECOGNIZED_COMMAND;
1483 } else if (lpParms->dwNumber > MCI_InstalledCount) {
1484 ret = MCIERR_OUTOFRANGE;
1485 } else {
1486 DWORD count = lpParms->dwNumber;
1487 LPSTR ptr = MCI_lpInstallNames;
1489 while (--count > 0) ptr += strlen(ptr) + 1;
1490 ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize, ptr);
1492 TRACE("(%ld) => '%s'\n", lpParms->dwNumber, lpParms->lpstrReturn);
1493 break;
1494 default:
1495 TRACE("Unsupported flag value=%08lx\n", dwFlags);
1496 ret = MCIERR_UNRECOGNIZED_COMMAND;
1498 return ret;
1501 /**************************************************************************
1502 * MCI_Break [internal]
1504 static DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1506 DWORD dwRet = 0;
1508 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1510 if (dwFlags & MCI_NOTIFY)
1511 mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1512 (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1514 return dwRet;
1517 /**************************************************************************
1518 * MCI_SendCommand [internal]
1520 DWORD MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD dwParam1,
1521 DWORD dwParam2, BOOL bFrom32)
1523 DWORD dwRet = MCIERR_UNRECOGNIZED_COMMAND;
1525 switch (wMsg) {
1526 case MCI_OPEN:
1527 if (bFrom32) {
1528 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSA)dwParam2);
1529 } else if (pFnMciMapMsg16To32A) {
1530 switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1531 case WINMM_MAP_OK:
1532 case WINMM_MAP_OKMEM:
1533 dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSA)dwParam2);
1534 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1535 break;
1536 default: break; /* so that gcc does not bark */
1539 break;
1540 case MCI_CLOSE:
1541 if (bFrom32) {
1542 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1543 } else if (pFnMciMapMsg16To32A) {
1544 switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1545 case WINMM_MAP_OK:
1546 case WINMM_MAP_OKMEM:
1547 dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1548 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1549 break;
1550 default: break; /* so that gcc does not bark */
1553 break;
1554 case MCI_SYSINFO:
1555 if (bFrom32) {
1556 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSA)dwParam2);
1557 } else if (pFnMciMapMsg16To32A) {
1558 switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1559 case WINMM_MAP_OK:
1560 case WINMM_MAP_OKMEM:
1561 dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSA)dwParam2);
1562 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1563 break;
1564 default: break; /* so that gcc doesnot bark */
1567 break;
1568 case MCI_BREAK:
1569 if (bFrom32) {
1570 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1571 } else if (pFnMciMapMsg16To32A) {
1572 switch (pFnMciMapMsg16To32A(0, wMsg, &dwParam2)) {
1573 case WINMM_MAP_OK:
1574 case WINMM_MAP_OKMEM:
1575 dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
1576 pFnMciUnMapMsg16To32A(0, wMsg, dwParam2);
1577 break;
1578 default: break; /* so that gcc does not bark */
1581 break;
1582 case MCI_SOUND:
1583 /* FIXME: it seems that MCI_SOUND needs the same handling as MCI_BREAK
1584 * but I couldn't get any doc on this MCI message
1586 break;
1587 default:
1588 if (wDevID == MCI_ALL_DEVICE_ID) {
1589 FIXME("unhandled MCI_ALL_DEVICE_ID\n");
1590 dwRet = MCIERR_CANNOT_USE_ALL;
1591 } else {
1592 dwRet = (bFrom32) ?
1593 MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2) :
1594 MCI_SendCommandFrom16(wDevID, wMsg, dwParam1, dwParam2);
1596 break;
1598 return dwRet;
1601 /**************************************************************************
1602 * MCI_CleanUp [internal]
1604 * Some MCI commands need to be cleaned-up (when not called from
1605 * mciSendString), because MCI drivers return extra information for string
1606 * transformation. This function gets rid of them.
1608 LRESULT MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD dwParam2)
1610 if (LOWORD(dwRet))
1611 return LOWORD(dwRet);
1613 switch (wMsg) {
1614 case MCI_GETDEVCAPS:
1615 switch (dwRet & 0xFFFF0000ul) {
1616 case 0:
1617 case MCI_COLONIZED3_RETURN:
1618 case MCI_COLONIZED4_RETURN:
1619 case MCI_INTEGER_RETURNED:
1620 /* nothing to do */
1621 break;
1622 case MCI_RESOURCE_RETURNED:
1623 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1625 LPMCI_GETDEVCAPS_PARMS lmgp;
1627 lmgp = (LPMCI_GETDEVCAPS_PARMS)(void*)dwParam2;
1628 TRACE("Changing %08lx to %08lx\n", lmgp->dwReturn, (DWORD)LOWORD(lmgp->dwReturn));
1629 lmgp->dwReturn = LOWORD(lmgp->dwReturn);
1631 break;
1632 default:
1633 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1634 HIWORD(dwRet), MCI_MessageToString(wMsg));
1636 break;
1637 case MCI_STATUS:
1638 switch (dwRet & 0xFFFF0000ul) {
1639 case 0:
1640 case MCI_COLONIZED3_RETURN:
1641 case MCI_COLONIZED4_RETURN:
1642 case MCI_INTEGER_RETURNED:
1643 /* nothing to do */
1644 break;
1645 case MCI_RESOURCE_RETURNED:
1646 case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1648 LPMCI_STATUS_PARMS lsp;
1650 lsp = (LPMCI_STATUS_PARMS)(void*)dwParam2;
1651 TRACE("Changing %08lx to %08lx\n", lsp->dwReturn, (DWORD)LOWORD(lsp->dwReturn));
1652 lsp->dwReturn = LOWORD(lsp->dwReturn);
1654 break;
1655 default:
1656 FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
1657 HIWORD(dwRet), MCI_MessageToString(wMsg));
1659 break;
1660 case MCI_SYSINFO:
1661 switch (dwRet & 0xFFFF0000ul) {
1662 case 0:
1663 case MCI_INTEGER_RETURNED:
1664 /* nothing to do */
1665 break;
1666 default:
1667 FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
1669 break;
1670 default:
1671 if (HIWORD(dwRet)) {
1672 FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
1673 dwRet, MCI_MessageToString(wMsg));
1675 break;
1677 return LOWORD(dwRet);
1680 /**************************************************************************
1681 * MCI_Init [internal]
1683 * Initializes the MCI internal variables.
1686 BOOL MCI_Init(void)
1688 LPSTR ptr1, ptr2;
1689 HKEY hWineConf;
1690 HKEY hkey;
1691 DWORD err;
1692 DWORD type;
1693 DWORD count = 2048;
1695 MCI_InstalledCount = 0;
1696 ptr1 = MCI_lpInstallNames = HeapAlloc(GetProcessHeap(), 0, count);
1698 if (!MCI_lpInstallNames)
1699 return FALSE;
1701 /* FIXME: should do also some registry diving here ? */
1702 if (!(err = RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config", &hWineConf)) &&
1703 !(err = RegOpenKeyA(hWineConf, "options", &hkey))) {
1704 err = RegQueryValueExA(hkey, "mci", 0, &type, MCI_lpInstallNames, &count);
1705 RegCloseKey(hkey);
1708 if (!err) {
1709 TRACE("Wine => '%s' \n", ptr1);
1710 while ((ptr2 = strchr(ptr1, ':')) != 0) {
1711 *ptr2++ = 0;
1712 TRACE("---> '%s' \n", ptr1);
1713 MCI_InstalledCount++;
1714 ptr1 = ptr2;
1716 MCI_InstalledCount++;
1717 TRACE("---> '%s' \n", ptr1);
1718 ptr1 += strlen(ptr1) + 1;
1719 } else {
1720 GetPrivateProfileStringA("mci", NULL, "", MCI_lpInstallNames, count, "SYSTEM.INI");
1721 while (strlen(ptr1) > 0) {
1722 TRACE("---> '%s' \n", ptr1);
1723 ptr1 += strlen(ptr1) + 1;
1724 MCI_InstalledCount++;
1727 RegCloseKey(hWineConf);
1728 return TRUE;