mci.c 72.5 KB
Newer Older
1 2 3 4
/*
 * MCI internal functions
 *
 * Copyright 1998/1999 Eric Pouech
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20
 */

21 22 23 24 25 26 27 28 29 30 31 32 33
/* TODO:
 * - implement WINMM (32bit) multitasking and use it in all MCI drivers
 *   instead of the home grown one 
 * - 16bit mmTaskXXX functions are currently broken because the 16
 *   loader does not support binary command lines => provide Wine's
 *   own mmtask.tsk not using binary command line.
 * - correctly handle the MCI_ALL_DEVICE_ID in functions.
 * - finish mapping 16 <=> 32 of MCI structures and commands
 * - implement auto-open feature (ie, when a string command is issued
 *   for a not yet opened device, MCI automatically opens it) 
 * - use a default registry setting to replace the [mci] section in
 *   configuration file (layout of info in registry should be compatible
 *   with all Windows' version - which use different layouts of course)
34 35 36 37 38 39 40 41 42
 * - implement automatic open
 *      + only works on string interface, on regular devices (don't work on all
 *        nor custom devices)
 * - command table handling isn't thread safe
 */

/* to be cross checked:
 * - heapalloc for *sizeof(WCHAR) when needed
 * - size of string in WCHAR or bytes? (#chars for MCI_INFO, #bytes for MCI_SYSINFO)
43 44
 */

Steven Edwards's avatar
Steven Edwards committed
45 46 47
#include "config.h"
#include "wine/port.h"

48
#include <stdlib.h>
49
#include <stdarg.h>
50
#include <stdio.h>
51 52
#include <string.h>

53
#include "windef.h"
54
#include "winbase.h"
55
#include "wingdi.h"
56
#include "winreg.h"
57
#include "mmsystem.h"
58
#include "winuser.h"
59
#include "winnls.h"
60
#include "winreg.h"
61
#include "wownt32.h"
62 63

#include "digitalv.h"
64
#include "winemm.h"
65

66
#include "wine/debug.h"
67
#include "wine/unicode.h"
68

69
WINE_DEFAULT_DEBUG_CHANNEL(mci);
70

71 72 73 74
WINMM_MapType  (*pFnMciMapMsg16To32W)  (WORD,WORD,DWORD,DWORD_PTR*) = NULL;
WINMM_MapType  (*pFnMciUnMapMsg16To32W)(WORD,WORD,DWORD,DWORD_PTR) = NULL;
WINMM_MapType  (*pFnMciMapMsg32WTo16)  (WORD,WORD,DWORD,DWORD_PTR*) = NULL;
WINMM_MapType  (*pFnMciUnMapMsg32WTo16)(WORD,WORD,DWORD,DWORD_PTR) = NULL;
75 76 77

/* First MCI valid device ID (0 means error) */
#define MCI_MAGIC 0x0001
78

79
/* MCI settings */
80 81 82 83 84 85
static const WCHAR wszHklmMci  [] = {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\','W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\','M','C','I',0};
static const WCHAR wszNull     [] = {0};
static const WCHAR wszAll      [] = {'A','L','L',0};
static const WCHAR wszMci      [] = {'M','C','I',0};
static const WCHAR wszOpen     [] = {'o','p','e','n',0};
static const WCHAR wszSystemIni[] = {'s','y','s','t','e','m','.','i','n','i',0};
86

87 88
static WINE_MCIDRIVER *MciDrivers;

89
/* dup a string and uppercase it */
90
static inline LPWSTR str_dup_upper( LPCWSTR str )
91
{
92 93
    INT len = (strlenW(str) + 1) * sizeof(WCHAR);
    LPWSTR p = HeapAlloc( GetProcessHeap(), 0, len );
94 95 96
    if (p)
    {
        memcpy( p, str, len );
97
        CharUpperW( p );
98 99 100 101
    }
    return p;
}

102 103 104
/**************************************************************************
 * 				MCI_GetDriver			[internal]
 */
105
LPWINE_MCIDRIVER	MCI_GetDriver(UINT16 wDevID)
106 107 108
{
    LPWINE_MCIDRIVER	wmd = 0;

109 110
    EnterCriticalSection(&WINMM_cs);
    for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
111 112
	if (wmd->wDeviceID == wDevID)
	    break;
113
    }
114
    LeaveCriticalSection(&WINMM_cs);
115 116 117 118 119 120
    return wmd;
}

/**************************************************************************
 * 				MCI_GetDriverFromString		[internal]
 */
121
UINT	MCI_GetDriverFromString(LPCWSTR lpstrName)
122 123 124 125 126 127
{
    LPWINE_MCIDRIVER	wmd;
    UINT		ret = 0;

    if (!lpstrName)
	return 0;
128

129
    if (!strcmpiW(lpstrName, wszAll))
130
	return MCI_ALL_DEVICE_ID;
131

132 133
    EnterCriticalSection(&WINMM_cs);
    for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
134
	if (wmd->lpstrElementName && strcmpW(wmd->lpstrElementName, lpstrName) == 0) {
135 136 137
	    ret = wmd->wDeviceID;
	    break;
	}
138
	if (wmd->lpstrDeviceType && strcmpiW(wmd->lpstrDeviceType, lpstrName) == 0) {
139 140 141
	    ret = wmd->wDeviceID;
	    break;
	}
142
	if (wmd->lpstrAlias && strcmpiW(wmd->lpstrAlias, lpstrName) == 0) {
143 144
	    ret = wmd->wDeviceID;
	    break;
145 146
	}
    }
147
    LeaveCriticalSection(&WINMM_cs);
148

149 150 151 152 153 154
    return ret;
}

/**************************************************************************
 * 			MCI_MessageToString			[internal]
 */
155
const char* MCI_MessageToString(UINT wMsg)
156 157
{
    static char buffer[100];
158

159
#define CASE(s) case (s): return #s
160

161
    switch (wMsg) {
162 163 164 165 166 167 168 169 170 171 172 173 174
        CASE(DRV_LOAD);
        CASE(DRV_ENABLE);
        CASE(DRV_OPEN);
        CASE(DRV_CLOSE);
        CASE(DRV_DISABLE);
        CASE(DRV_FREE);
        CASE(DRV_CONFIGURE);
        CASE(DRV_QUERYCONFIGURE);
        CASE(DRV_INSTALL);
        CASE(DRV_REMOVE);
        CASE(DRV_EXITSESSION);
        CASE(DRV_EXITAPPLICATION);
        CASE(DRV_POWER);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
	CASE(MCI_BREAK);
	CASE(MCI_CLOSE);
	CASE(MCI_CLOSE_DRIVER);
	CASE(MCI_COPY);
	CASE(MCI_CUE);
	CASE(MCI_CUT);
	CASE(MCI_DELETE);
	CASE(MCI_ESCAPE);
	CASE(MCI_FREEZE);
	CASE(MCI_PAUSE);
	CASE(MCI_PLAY);
	CASE(MCI_GETDEVCAPS);
	CASE(MCI_INFO);
	CASE(MCI_LOAD);
	CASE(MCI_OPEN);
	CASE(MCI_OPEN_DRIVER);
	CASE(MCI_PASTE);
	CASE(MCI_PUT);
	CASE(MCI_REALIZE);
	CASE(MCI_RECORD);
	CASE(MCI_RESUME);
	CASE(MCI_SAVE);
	CASE(MCI_SEEK);
	CASE(MCI_SET);
	CASE(MCI_SPIN);
	CASE(MCI_STATUS);
	CASE(MCI_STEP);
	CASE(MCI_STOP);
	CASE(MCI_SYSINFO);
	CASE(MCI_UNFREEZE);
	CASE(MCI_UPDATE);
	CASE(MCI_WHERE);
	CASE(MCI_WINDOW);
	/* constants for digital video */
	CASE(MCI_CAPTURE);
	CASE(MCI_MONITOR);
	CASE(MCI_RESERVE);
	CASE(MCI_SETAUDIO);
	CASE(MCI_SIGNAL);
	CASE(MCI_SETVIDEO);
	CASE(MCI_QUALITY);
	CASE(MCI_LIST);
	CASE(MCI_UNDO);
	CASE(MCI_CONFIGURE);
	CASE(MCI_RESTORE);
#undef CASE
    default:
	sprintf(buffer, "MCI_<<%04X>>", wMsg);
	return buffer;
    }
}

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
LPWSTR MCI_strdupAtoW( LPCSTR str )
{
    LPWSTR ret;
    INT len;

    if (!str) return NULL;
    len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
    ret = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
    if (ret) MultiByteToWideChar( CP_ACP, 0, str, -1, ret, len );
    return ret;
}

LPSTR MCI_strdupWtoA( LPCWSTR str )
{
    LPSTR ret;
    INT len;

    if (!str) return NULL;
    len = WideCharToMultiByte( CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
    ret = HeapAlloc( GetProcessHeap(), 0, len );
    if (ret) WideCharToMultiByte( CP_ACP, 0, str, -1, ret, len, NULL, NULL );
    return ret;
}

static int MCI_MapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR *dwParam2)
{
    if (msg < DRV_RESERVED) return 0;

    switch (msg)
    {
    case MCI_CLOSE:
    case MCI_CONFIGURE:
    case MCI_PLAY:
    case MCI_SEEK:
    case MCI_STOP:
    case MCI_PAUSE:
    case MCI_GETDEVCAPS:
    case MCI_SPIN:
    case MCI_SET:
    case MCI_STEP:
    case MCI_RECORD:
    case MCI_BREAK:
    case MCI_SOUND:
    case MCI_STATUS:
    case MCI_CUE:
    case MCI_REALIZE:
    case MCI_PUT:
    case MCI_WHERE:
    case MCI_FREEZE:
    case MCI_UNFREEZE:
    case MCI_CUT:
    case MCI_COPY:
    case MCI_PASTE:
    case MCI_UPDATE:
    case MCI_RESUME:
    case MCI_DELETE:
283 284 285 286 287
    case MCI_MONITOR:
    case MCI_SETAUDIO:
    case MCI_SIGNAL:
    case MCI_SETVIDEO:
    case MCI_LIST:
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
        return 0;

    case MCI_OPEN:
        {
            MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA*)*dwParam2;
            MCI_OPEN_PARMSW *mci_openW;
            DWORD_PTR *ptr;

            ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(DWORD_PTR) + sizeof(*mci_openW) + 2 * sizeof(DWORD));
            if (!ptr) return -1;

            *ptr++ = *dwParam2; /* save the previous pointer */
            *dwParam2 = (DWORD_PTR)ptr;
            mci_openW = (MCI_OPEN_PARMSW *)ptr;

            if (dwParam1 & MCI_NOTIFY)
                mci_openW->dwCallback = mci_openA->dwCallback;

            if (dwParam1 & MCI_OPEN_TYPE)
            {
                if (dwParam1 & MCI_OPEN_TYPE_ID)
309
                    mci_openW->lpstrDeviceType = (LPCWSTR)mci_openA->lpstrDeviceType;
310 311 312 313 314 315
                else
                    mci_openW->lpstrDeviceType = MCI_strdupAtoW(mci_openA->lpstrDeviceType);
            }
            if (dwParam1 & MCI_OPEN_ELEMENT)
            {
                if (dwParam1 & MCI_OPEN_ELEMENT_ID)
316
                    mci_openW->lpstrElementName = (LPCWSTR)mci_openA->lpstrElementName;
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
                else
                    mci_openW->lpstrElementName = MCI_strdupAtoW(mci_openA->lpstrElementName);
            }
            if (dwParam1 & MCI_OPEN_ALIAS)
                mci_openW->lpstrAlias = MCI_strdupAtoW(mci_openA->lpstrAlias);
            /* FIXME: this is only needed for specific types of MCI devices, and
             * may cause a segfault if the two DWORD:s don't exist at the end of 
             * mci_openA
             */
            memcpy(mci_openW + 1, mci_openA + 1, 2 * sizeof(DWORD));
        }
        return 1;

    case MCI_WINDOW:
        if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
        {
            MCI_ANIM_WINDOW_PARMSA *mci_windowA = (MCI_ANIM_WINDOW_PARMSA *)*dwParam2;
            MCI_ANIM_WINDOW_PARMSW *mci_windowW;

            mci_windowW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_windowW));
            if (!mci_windowW) return -1;

            *dwParam2 = (DWORD_PTR)mci_windowW;

            mci_windowW->lpstrText = MCI_strdupAtoW(mci_windowA->lpstrText);

            if (dwParam1 & MCI_NOTIFY)
                mci_windowW->dwCallback = mci_windowA->dwCallback;
            if (dwParam1 & MCI_ANIM_WINDOW_HWND)
                mci_windowW->hWnd = mci_windowA->hWnd;
            if (dwParam1 & MCI_ANIM_WINDOW_STATE)
                mci_windowW->nCmdShow = mci_windowA->nCmdShow;

            return 1;
        }
        return 0;

    case MCI_SYSINFO:
        {
            MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*dwParam2;
            MCI_SYSINFO_PARMSW *mci_sysinfoW;
            DWORD_PTR *ptr;

            ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_sysinfoW) + sizeof(DWORD_PTR));
            if (!ptr) return -1;

            *ptr++ = *dwParam2; /* save the previous pointer */
            *dwParam2 = (DWORD_PTR)ptr;
            mci_sysinfoW = (MCI_SYSINFO_PARMSW *)ptr;

            if (dwParam1 & MCI_NOTIFY)
                mci_sysinfoW->dwCallback = mci_sysinfoA->dwCallback;

            mci_sysinfoW->dwRetSize = mci_sysinfoA->dwRetSize;
            mci_sysinfoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_sysinfoW->dwRetSize);
            mci_sysinfoW->dwNumber = mci_sysinfoA->dwNumber;
            mci_sysinfoW->wDeviceType = mci_sysinfoA->wDeviceType;
            return 1;
        }
    case MCI_INFO:
        {
            MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*dwParam2;
            MCI_INFO_PARMSW *mci_infoW;
            DWORD_PTR *ptr;

            ptr = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_infoW) + sizeof(DWORD_PTR));
            if (!ptr) return -1;

            *ptr++ = *dwParam2; /* save the previous pointer */
            *dwParam2 = (DWORD_PTR)ptr;
            mci_infoW = (MCI_INFO_PARMSW *)ptr;

            if (dwParam1 & MCI_NOTIFY)
                mci_infoW->dwCallback = mci_infoA->dwCallback;

            mci_infoW->dwRetSize = mci_infoA->dwRetSize * sizeof(WCHAR); /* it's not the same as SYSINFO !!! */
            mci_infoW->lpstrReturn = HeapAlloc(GetProcessHeap(), 0, mci_infoW->dwRetSize);
            return 1;
        }
    case MCI_SAVE:
        {
            MCI_SAVE_PARMSA *mci_saveA = (MCI_SAVE_PARMSA *)*dwParam2;
            MCI_SAVE_PARMSW *mci_saveW;

            mci_saveW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_saveW));
            if (!mci_saveW) return -1;

            *dwParam2 = (DWORD_PTR)mci_saveW;
            if (dwParam1 & MCI_NOTIFY)
                mci_saveW->dwCallback = mci_saveA->dwCallback;
            mci_saveW->lpfilename = MCI_strdupAtoW(mci_saveA->lpfilename);
            return 1;
        }
    case MCI_LOAD:
        {
            MCI_LOAD_PARMSA *mci_loadA = (MCI_LOAD_PARMSA *)*dwParam2;
            MCI_LOAD_PARMSW *mci_loadW;

            mci_loadW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_loadW));
            if (!mci_loadW) return -1;

            *dwParam2 = (DWORD_PTR)mci_loadW;
            if (dwParam1 & MCI_NOTIFY)
                mci_loadW->dwCallback = mci_loadA->dwCallback;
            mci_loadW->lpfilename = MCI_strdupAtoW(mci_loadA->lpfilename);
            return 1;
        }

    case MCI_ESCAPE:
        {
            MCI_VD_ESCAPE_PARMSA *mci_vd_escapeA = (MCI_VD_ESCAPE_PARMSA *)*dwParam2;
            MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW;

            mci_vd_escapeW = HeapAlloc(GetProcessHeap(), 0, sizeof(*mci_vd_escapeW));
            if (!mci_vd_escapeW) return -1;

            *dwParam2 = (DWORD_PTR)mci_vd_escapeW;
            if (dwParam1 & MCI_NOTIFY)
                mci_vd_escapeW->dwCallback = mci_vd_escapeA->dwCallback;
            mci_vd_escapeW->lpstrCommand = MCI_strdupAtoW(mci_vd_escapeA->lpstrCommand);
            return 1;
        }
    default:
        FIXME("Message %s needs translation\n", MCI_MessageToString(msg));
        return -1;
    }
}

static DWORD MCI_UnmapMsgAtoW(UINT msg, DWORD_PTR dwParam1, DWORD_PTR dwParam2,
                              DWORD result)
{
    switch (msg)
    {
    case MCI_OPEN:
        {
            DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
            MCI_OPEN_PARMSA *mci_openA = (MCI_OPEN_PARMSA *)*ptr;
            MCI_OPEN_PARMSW *mci_openW = (MCI_OPEN_PARMSW *)(ptr + 1);

            mci_openA->wDeviceID = mci_openW->wDeviceID;

            if (dwParam1 & MCI_OPEN_TYPE)
            {
                if (!(dwParam1 & MCI_OPEN_TYPE_ID))
461
                    HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrDeviceType);
462 463 464 465
            }
            if (dwParam1 & MCI_OPEN_ELEMENT)
            {
                if (!(dwParam1 & MCI_OPEN_ELEMENT_ID))
466
                    HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrElementName);
467 468
            }
            if (dwParam1 & MCI_OPEN_ALIAS)
469
                HeapFree(GetProcessHeap(), 0, (LPWSTR)mci_openW->lpstrAlias);
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
            HeapFree(GetProcessHeap(), 0, ptr);
        }
        break;
    case MCI_WINDOW:
        if (dwParam1 & MCI_ANIM_WINDOW_TEXT)
        {
            MCI_ANIM_WINDOW_PARMSW *mci_windowW = (MCI_ANIM_WINDOW_PARMSW *)dwParam2;

            HeapFree(GetProcessHeap(), 0, (void*)mci_windowW->lpstrText);
            HeapFree(GetProcessHeap(), 0, mci_windowW);
        }
        break;

    case MCI_SYSINFO:
        {
            DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
            MCI_SYSINFO_PARMSA *mci_sysinfoA = (MCI_SYSINFO_PARMSA *)*ptr;
            MCI_SYSINFO_PARMSW *mci_sysinfoW = (MCI_SYSINFO_PARMSW *)(ptr + 1);

            if (!result)
            {
                mci_sysinfoA->dwNumber = mci_sysinfoW->dwNumber;
                mci_sysinfoA->wDeviceType = mci_sysinfoW->wDeviceType;
                if (dwParam1 & MCI_SYSINFO_QUANTITY)
                    *(DWORD*)mci_sysinfoA->lpstrReturn = *(DWORD*)mci_sysinfoW->lpstrReturn;
                else
                    WideCharToMultiByte(CP_ACP, 0,
                                        mci_sysinfoW->lpstrReturn, mci_sysinfoW->dwRetSize,
                                        mci_sysinfoA->lpstrReturn, mci_sysinfoA->dwRetSize,
                                        NULL, NULL);
            }

            HeapFree(GetProcessHeap(), 0, mci_sysinfoW->lpstrReturn);
            HeapFree(GetProcessHeap(), 0, ptr);
        }
        break;
    case MCI_INFO:
        {
            DWORD_PTR *ptr = (DWORD_PTR *)dwParam2 - 1;
            MCI_INFO_PARMSA *mci_infoA = (MCI_INFO_PARMSA *)*ptr;
            MCI_INFO_PARMSW *mci_infoW = (MCI_INFO_PARMSW *)(ptr + 1);

            if (!result)
            {
                WideCharToMultiByte(CP_ACP, 0,
                                    mci_infoW->lpstrReturn, mci_infoW->dwRetSize / sizeof(WCHAR),
                                    mci_infoA->lpstrReturn, mci_infoA->dwRetSize,
                                    NULL, NULL);
            }

            HeapFree(GetProcessHeap(), 0, mci_infoW->lpstrReturn);
            HeapFree(GetProcessHeap(), 0, ptr);
        }
        break;
    case MCI_SAVE:
        {
            MCI_SAVE_PARMSW *mci_saveW = (MCI_SAVE_PARMSW *)dwParam2;

            HeapFree(GetProcessHeap(), 0, (void*)mci_saveW->lpfilename);
            HeapFree(GetProcessHeap(), 0, mci_saveW);
        }
        break;
    case MCI_LOAD:
        {
            MCI_LOAD_PARMSW *mci_loadW = (MCI_LOAD_PARMSW *)dwParam2;

            HeapFree(GetProcessHeap(), 0, (void*)mci_loadW->lpfilename);
            HeapFree(GetProcessHeap(), 0, mci_loadW);
        }
        break;
    case MCI_ESCAPE:
        {
            MCI_VD_ESCAPE_PARMSW *mci_vd_escapeW = (MCI_VD_ESCAPE_PARMSW *)dwParam2;

            HeapFree(GetProcessHeap(), 0, (void*)mci_vd_escapeW->lpstrCommand);
            HeapFree(GetProcessHeap(), 0, mci_vd_escapeW);
        }
        break;

    default:
        FIXME("Message %s needs unmapping\n", MCI_MessageToString(msg));
        break;
    }

    return result;
}

557 558 559
/**************************************************************************
 * 				MCI_GetDevTypeFromFileName	[internal]
 */
560
static	DWORD	MCI_GetDevTypeFromFileName(LPCWSTR fileName, LPWSTR buf, UINT len)
561
{
562
    LPCWSTR	tmp;
563
    HKEY	hKey;
564 565 566 567 568
    static const WCHAR keyW[] = {'S','O','F','T','W','A','R','E','\\','M','i','c','r','o','s','o','f','t','\\',
                                 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
                                 'M','C','I',' ','E','x','t','e','n','s','i','o','n','s',0};
    if ((tmp = strrchrW(fileName, '.'))) {
	if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, keyW,
569 570
			   0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
	    DWORD dwLen = len;
571
	    LONG lRet = RegQueryValueExW( hKey, tmp + 1, 0, 0, (void*)buf, &dwLen ); 
572 573 574
	    RegCloseKey( hKey );
	    if (lRet == ERROR_SUCCESS) return 0;
        }
575
	TRACE("No ...\\MCI Extensions entry for %s found.\n", debugstr_w(tmp));
576 577 578 579 580 581 582 583
    }
    return MCIERR_EXTENSION_NOT_FOUND;
}

#define	MAX_MCICMDTABLE			20
#define MCI_COMMAND_TABLE_NOT_LOADED	0xFFFE

typedef struct tagWINE_MCICMDTABLE {
584
    UINT		uDevType;
585
    const BYTE*		lpTable;
586
    UINT		nVerbs;		/* number of verbs in command table */
587
    LPCWSTR*		aVerbs;		/* array of verbs to speed up the verb look up process */
588
} WINE_MCICMDTABLE, *LPWINE_MCICMDTABLE;
589 590

static WINE_MCICMDTABLE S_MciCmdTable[MAX_MCICMDTABLE];
591 592 593 594 595 596

/**************************************************************************
 * 				MCI_IsCommandTableValid		[internal]
 */
static	BOOL		MCI_IsCommandTableValid(UINT uTbl)
{
597 598
    const BYTE* lmem;
    LPCWSTR     str;
599 600 601 602 603
    DWORD	flg;
    WORD	eid;
    int		idx = 0;
    BOOL	inCst = FALSE;

604 605
    TRACE("Dumping cmdTbl=%d [lpTable=%p devType=%d]\n",
	  uTbl, S_MciCmdTable[uTbl].lpTable, S_MciCmdTable[uTbl].uDevType);
606

607
    if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
608 609 610 611
	return FALSE;

    lmem = S_MciCmdTable[uTbl].lpTable;
    do {
612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
        str = (LPCWSTR)lmem;
        lmem += (strlenW(str) + 1) * sizeof(WCHAR);
        flg = *(const DWORD*)lmem;
        eid = *(const WORD*)(lmem + sizeof(DWORD));
        lmem += sizeof(DWORD) + sizeof(WORD);
        idx ++;
        /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
        switch (eid) {
        case MCI_COMMAND_HEAD:          if (!*str || !flg) return FALSE; idx = 0;		break;	/* check unicity of str in table */
        case MCI_STRING:                if (inCst) return FALSE;				break;
        case MCI_INTEGER:               if (!*str) return FALSE;				break;
        case MCI_END_COMMAND:           if (*str || flg || idx == 0) return FALSE; idx = 0;	break;
        case MCI_RETURN:		if (*str || idx != 1) return FALSE;			break;
        case MCI_FLAG:		        if (!*str) return FALSE;				break;
        case MCI_END_COMMAND_LIST:	if (*str || flg) return FALSE;	idx = 0;		break;
        case MCI_RECT:		        if (!*str || inCst) return FALSE;			break;
        case MCI_CONSTANT:              if (inCst) return FALSE; inCst = TRUE;			break;
        case MCI_END_CONSTANT:	        if (*str || flg || !inCst) return FALSE; inCst = FALSE; break;
        default:			return FALSE;
        }
632 633 634 635 636 637 638 639 640
    } while (eid != MCI_END_COMMAND_LIST);
    return TRUE;
}

/**************************************************************************
 * 				MCI_DumpCommandTable		[internal]
 */
static	BOOL		MCI_DumpCommandTable(UINT uTbl)
{
641 642
    const BYTE*	lmem;
    LPCWSTR	str;
643 644
    DWORD	flg;
    WORD	eid;
645

646 647 648 649 650 651 652 653
    if (!MCI_IsCommandTableValid(uTbl)) {
	ERR("Ooops: %d is not valid\n", uTbl);
	return FALSE;
    }

    lmem = S_MciCmdTable[uTbl].lpTable;
    do {
	do {
654 655
	    str = (LPCWSTR)lmem;
	    lmem += (strlenW(str) + 1) * sizeof(WCHAR);
Eric Pouech's avatar
Eric Pouech committed
656 657
	    flg = *(const DWORD*)lmem;
	    eid = *(const WORD*)(lmem + sizeof(DWORD));
658
            /* TRACE("cmd=%s %08lx %04x\n", debugstr_w(str), flg, eid); */
659 660
	    lmem += sizeof(DWORD) + sizeof(WORD);
	} while (eid != MCI_END_COMMAND && eid != MCI_END_COMMAND_LIST);
661
        /* EPP TRACE(" => end of command%s\n", (eid == MCI_END_COMMAND_LIST) ? " list" : ""); */
662 663 664 665 666 667 668 669
    } while (eid != MCI_END_COMMAND_LIST);
    return TRUE;
}


/**************************************************************************
 * 				MCI_GetCommandTable		[internal]
 */
670
static	UINT		MCI_GetCommandTable(UINT uDevType)
671 672
{
    UINT	uTbl;
673 674
    WCHAR	buf[32];
    LPCWSTR	str = NULL;
675

676 677
    /* first look up existing for existing devType */
    for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
678
	if (S_MciCmdTable[uTbl].lpTable && S_MciCmdTable[uTbl].uDevType == uDevType)
679
	    return uTbl;
680 681 682 683
    }

    /* well try to load id */
    if (uDevType >= MCI_DEVTYPE_FIRST && uDevType <= MCI_DEVTYPE_LAST) {
684
	if (LoadStringW(hWinMM32Instance, uDevType, buf, sizeof(buf) / sizeof(WCHAR))) {
685 686 687
	    str = buf;
	}
    } else if (uDevType == 0) {
688 689
        static const WCHAR wszCore[] = {'C','O','R','E',0};
	str = wszCore;
690 691 692
    }
    uTbl = MCI_NO_COMMAND_TABLE;
    if (str) {
693
	HRSRC 	hRsrc = FindResourceW(hWinMM32Instance, str, (LPCWSTR)RT_RCDATA);
694 695
	HANDLE	hMem = 0;

696
	if (hRsrc) hMem = LoadResource(hWinMM32Instance, hRsrc);
697
	if (hMem) {
698
	    uTbl = MCI_SetCommandTable(LockResource(hMem), uDevType);
699
	} else {
700
	    WARN("No command table found in resource %p[%s]\n",
701
		 hWinMM32Instance, debugstr_w(str));
702 703 704 705 706 707 708 709 710
	}
    }
    TRACE("=> %d\n", uTbl);
    return uTbl;
}

/**************************************************************************
 * 				MCI_SetCommandTable		[internal]
 */
711
UINT MCI_SetCommandTable(void *table, UINT uDevType)
712
{
713 714 715 716 717 718 719 720 721 722
    int		        uTbl;
    static	BOOL	bInitDone = FALSE;

    /* <HACK>
     * The CORE command table must be loaded first, so that MCI_GetCommandTable()
     * can be called with 0 as a uDevType to retrieve it.
     * </HACK>
     */
    if (!bInitDone) {
	bInitDone = TRUE;
723
	MCI_GetCommandTable(0);
724
    }
725
    TRACE("(%p, %u)\n", table, uDevType);
726
    for (uTbl = 0; uTbl < MAX_MCICMDTABLE; uTbl++) {
727
	if (!S_MciCmdTable[uTbl].lpTable) {
728 729
	    const BYTE* lmem;
	    LPCWSTR 	str;
730 731 732
	    WORD	eid;
	    WORD	count;

733
	    S_MciCmdTable[uTbl].uDevType = uDevType;
734
	    S_MciCmdTable[uTbl].lpTable = table;
735 736 737 738

	    if (TRACE_ON(mci)) {
		MCI_DumpCommandTable(uTbl);
	    }
739 740 741 742 743 744

	    /* create the verbs table */
	    /* get # of entries */
	    lmem = S_MciCmdTable[uTbl].lpTable;
	    count = 0;
	    do {
745 746
		str = (LPCWSTR)lmem;
		lmem += (strlenW(str) + 1) * sizeof(WCHAR);
Eric Pouech's avatar
Eric Pouech committed
747
		eid = *(const WORD*)(lmem + sizeof(DWORD));
748 749 750 751 752
		lmem += sizeof(DWORD) + sizeof(WORD);
		if (eid == MCI_COMMAND_HEAD)
		    count++;
	    } while (eid != MCI_END_COMMAND_LIST);

753
	    S_MciCmdTable[uTbl].aVerbs = HeapAlloc(GetProcessHeap(), 0, count * sizeof(LPCWSTR));
754 755 756 757 758
	    S_MciCmdTable[uTbl].nVerbs = count;

	    lmem = S_MciCmdTable[uTbl].lpTable;
	    count = 0;
	    do {
759 760
		str = (LPCWSTR)lmem;
		lmem += (strlenW(str) + 1) * sizeof(WCHAR);
Eric Pouech's avatar
Eric Pouech committed
761
		eid = *(const WORD*)(lmem + sizeof(DWORD));
762 763 764 765 766
		lmem += sizeof(DWORD) + sizeof(WORD);
		if (eid == MCI_COMMAND_HEAD)
		    S_MciCmdTable[uTbl].aVerbs[count++] = str;
	    } while (eid != MCI_END_COMMAND_LIST);
	    /* assert(count == S_MciCmdTable[uTbl].nVerbs); */
767 768 769 770 771 772 773 774 775 776
	    return uTbl;
	}
    }

    return MCI_NO_COMMAND_TABLE;
}

/**************************************************************************
 * 				MCI_DeleteCommandTable		[internal]
 */
777
BOOL	MCI_DeleteCommandTable(UINT uTbl, BOOL delete)
778
{
779
    if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
780 781
	return FALSE;

782
    if (delete) HeapFree(GetProcessHeap(), 0, (void*)S_MciCmdTable[uTbl].lpTable);
783
    S_MciCmdTable[uTbl].lpTable = NULL;
784 785
    HeapFree(GetProcessHeap(), 0, S_MciCmdTable[uTbl].aVerbs);
    S_MciCmdTable[uTbl].aVerbs = 0;
786 787 788 789 790 791
    return TRUE;
}

/**************************************************************************
 * 				MCI_UnLoadMciDriver		[internal]
 */
792
static	BOOL	MCI_UnLoadMciDriver(LPWINE_MCIDRIVER wmd)
793
{
794
    LPWINE_MCIDRIVER*		tmp;
795 796 797 798

    if (!wmd)
	return TRUE;

799
    CloseDriver(wmd->hDriver, 0, 0);
800 801 802 803

    if (wmd->dwPrivate != 0)
	WARN("Unloading mci driver with non nul dwPrivate field\n");

804 805
    EnterCriticalSection(&WINMM_cs);
    for (tmp = &MciDrivers; *tmp; tmp = &(*tmp)->lpNext) {
806 807 808 809 810
	if (*tmp == wmd) {
	    *tmp = wmd->lpNext;
	    break;
	}
    }
811
    LeaveCriticalSection(&WINMM_cs);
812 813 814 815 816 817 818 819

    HeapFree(GetProcessHeap(), 0, wmd->lpstrDeviceType);
    HeapFree(GetProcessHeap(), 0, wmd->lpstrAlias);
    HeapFree(GetProcessHeap(), 0, wmd->lpstrElementName);

    HeapFree(GetProcessHeap(), 0, wmd);
    return TRUE;
}
820

821 822 823
/**************************************************************************
 * 				MCI_OpenMciDriver		[internal]
 */
824
static	BOOL	MCI_OpenMciDriver(LPWINE_MCIDRIVER wmd, LPCWSTR drvTyp, DWORD_PTR lp)
825
{
826
    WCHAR	libName[128];
827

828
    if (!DRIVER_GetLibName(drvTyp, wszMci, libName, sizeof(libName)))
829 830 831 832 833 834
	return FALSE;

    wmd->bIs32 = 0xFFFF;
    /* First load driver */
    if ((wmd->hDriver = (HDRVR)DRIVER_TryOpenDriver32(libName, lp))) {
	wmd->bIs32 = TRUE;
835
    } else if (WINMM_CheckForMMSystem() && pFnMciMapMsg32WTo16) {
836
	WINMM_MapType 	res;
837

838
	switch (res = pFnMciMapMsg32WTo16(0, DRV_OPEN, 0, &lp)) {
839
	case WINMM_MAP_MSGERROR:
840
	    TRACE("Not handled yet (DRV_OPEN)\n");
841
	    break;
842
	case WINMM_MAP_NOMEM:
843
	    TRACE("Problem mapping msg=DRV_OPEN from 32W to 16\n");
844
	    break;
845 846
	case WINMM_MAP_OK:
	case WINMM_MAP_OKMEM:
847
	    if ((wmd->hDriver = OpenDriver(drvTyp, wszMci, lp)))
848
		wmd->bIs32 = FALSE;
849
	    if (res == WINMM_MAP_OKMEM)
850
		pFnMciUnMapMsg32WTo16(0, DRV_OPEN, 0, lp);
851 852 853 854 855 856
	    break;
	}
    }
    return (wmd->bIs32 == 0xFFFF) ? FALSE : TRUE;
}

857 858 859
/**************************************************************************
 * 				MCI_LoadMciDriver		[internal]
 */
860
static	DWORD	MCI_LoadMciDriver(LPCWSTR _strDevTyp, LPWINE_MCIDRIVER* lpwmd)
861
{
862
    LPWSTR			strDevTyp = str_dup_upper(_strDevTyp);
863
    LPWINE_MCIDRIVER		wmd = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*wmd));
864
    MCI_OPEN_DRIVER_PARMSW	modp;
865
    DWORD			dwRet = 0;
866

867 868 869 870
    if (!wmd || !strDevTyp) {
	dwRet = MCIERR_OUT_OF_MEMORY;
	goto errCleanUp;
    }
871

872 873
    wmd->lpfnYieldProc = MCI_DefYieldProc;
    wmd->dwYieldData = VK_CANCEL;
874
    wmd->CreatorThread = GetCurrentThreadId();
875

876
    EnterCriticalSection(&WINMM_cs);
877 878 879
    /* wmd must be inserted in list before sending opening the driver, coz' it
     * may want to lookup at wDevID
     */
880 881
    wmd->lpNext = MciDrivers;
    MciDrivers = wmd;
882

883 884
    for (modp.wDeviceID = MCI_MAGIC;
	 MCI_GetDriver(modp.wDeviceID) != 0;
885 886 887 888
	 modp.wDeviceID++);

    wmd->wDeviceID = modp.wDeviceID;

889
    LeaveCriticalSection(&WINMM_cs);
890

891
    TRACE("wDevID=%04X\n", modp.wDeviceID);
892 893

    modp.lpstrParams = NULL;
894

895
    if (!MCI_OpenMciDriver(wmd, strDevTyp, (DWORD_PTR)&modp)) {
896 897 898
	/* silence warning if all is used... some bogus program use commands like
	 * 'open all'...
	 */
899
	if (strcmpiW(strDevTyp, wszAll) == 0) {
900 901 902 903 904
	    dwRet = MCIERR_CANNOT_USE_ALL;
	} else {
	    FIXME("Couldn't load driver for type %s.\n"
		  "If you don't have a windows installation accessible from Wine,\n"
		  "you perhaps forgot to create a [mci] section in system.ini\n",
905
		  debugstr_w(strDevTyp));
906 907
	    dwRet = MCIERR_DEVICE_NOT_INSTALLED;
	}
908
	goto errCleanUp;
909
    }
910

911 912 913 914 915 916 917 918
    /* FIXME: should also check that module's description is of the form
     * MODULENAME:[MCI] comment
     */

    /* some drivers will return 0x0000FFFF, some others 0xFFFFFFFF */
    wmd->uSpecificCmdTable = LOWORD(modp.wCustomCommandTable);
    wmd->uTypeCmdTable = MCI_COMMAND_TABLE_NOT_LOADED;

919
    TRACE("Loaded driver %p (%s), type is %d, cmdTable=%08x\n",
920
	  wmd->hDriver, debugstr_w(strDevTyp), modp.wType, modp.wCustomCommandTable);
921

922 923 924
    wmd->lpstrDeviceType = strDevTyp;
    wmd->wType = modp.wType;

925
    TRACE("mcidev=%d, uDevTyp=%04X wDeviceID=%04X !\n",
926 927 928 929
	  modp.wDeviceID, modp.wType, modp.wDeviceID);
    *lpwmd = wmd;
    return 0;
errCleanUp:
930
    MCI_UnLoadMciDriver(wmd);
931 932 933 934
    HeapFree(GetProcessHeap(), 0, strDevTyp);
    *lpwmd = 0;
    return dwRet;
}
935

936 937 938
/**************************************************************************
 * 			MCI_FinishOpen				[internal]
 */
939
static	DWORD	MCI_FinishOpen(LPWINE_MCIDRIVER wmd, LPMCI_OPEN_PARMSW lpParms,
940 941 942
			       DWORD dwParam)
{
    if (dwParam & MCI_OPEN_ELEMENT)
943
    {
944 945
        wmd->lpstrElementName = HeapAlloc(GetProcessHeap(),0,(strlenW(lpParms->lpstrElementName)+1) * sizeof(WCHAR));
        strcpyW( wmd->lpstrElementName, lpParms->lpstrElementName );
946
    }
947
    if (dwParam & MCI_OPEN_ALIAS)
948
    {
949 950
        wmd->lpstrAlias = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpParms->lpstrAlias)+1) * sizeof(WCHAR));
        strcpyW( wmd->lpstrAlias, lpParms->lpstrAlias);
951
    }
952 953
    lpParms->wDeviceID = wmd->wDeviceID;

954
    return MCI_SendCommandFrom32(wmd->wDeviceID, MCI_OPEN_DRIVER, dwParam,
955 956 957 958 959 960
				 (DWORD)lpParms);
}

/**************************************************************************
 * 				MCI_FindCommand		[internal]
 */
961
static	LPCWSTR		MCI_FindCommand(UINT uTbl, LPCWSTR verb)
962
{
963
    UINT	idx;
964

965
    if (uTbl >= MAX_MCICMDTABLE || !S_MciCmdTable[uTbl].lpTable)
966 967
	return NULL;

968 969 970 971 972
    /* another improvement would be to have the aVerbs array sorted,
     * so that we could use a dichotomic search on it, rather than this dumb
     * array look up
     */
    for (idx = 0; idx < S_MciCmdTable[uTbl].nVerbs; idx++) {
973
	if (strcmpiW(S_MciCmdTable[uTbl].aVerbs[idx], verb) == 0)
974 975
	    return S_MciCmdTable[uTbl].aVerbs[idx];
    }
976 977 978 979 980 981 982

    return NULL;
}

/**************************************************************************
 * 				MCI_GetReturnType		[internal]
 */
983
static	DWORD		MCI_GetReturnType(LPCWSTR lpCmd)
984
{
985 986
    lpCmd = (LPCWSTR)((const BYTE*)(lpCmd + strlenW(lpCmd) + 1) + sizeof(DWORD) + sizeof(WORD));
    if (*lpCmd == '\0' && *(const WORD*)((const BYTE*)(lpCmd + 1) + sizeof(DWORD)) == MCI_RETURN) {
Eric Pouech's avatar
Eric Pouech committed
987
	return *(const DWORD*)(lpCmd + 1);
988 989 990 991 992 993 994
    }
    return 0L;
}

/**************************************************************************
 * 				MCI_GetMessage			[internal]
 */
995
static	WORD		MCI_GetMessage(LPCWSTR lpCmd)
996
{
997
    return (WORD)*(const DWORD*)(lpCmd + strlenW(lpCmd) + 1);
998 999 1000 1001 1002
}

/**************************************************************************
 * 				MCI_GetDWord			[internal]
 */
1003
static	BOOL		MCI_GetDWord(LPDWORD data, LPWSTR* ptr)
1004 1005
{
    DWORD	val;
1006
    LPWSTR	ret;
1007

1008
    val = strtoulW(*ptr, &ret, 0);
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023

    switch (*ret) {
    case '\0':	break;
    case ' ':	ret++; break;
    default:	return FALSE;
    }

    *data |= val;
    *ptr = ret;
    return TRUE;
}

/**************************************************************************
 * 				MCI_GetString		[internal]
 */
1024
static	DWORD	MCI_GetString(LPWSTR* str, LPWSTR* args)
1025
{
1026
    LPWSTR      ptr = *args;
1027 1028 1029

    /* see if we have a quoted string */
    if (*ptr == '"') {
1030
	ptr = strchrW(*str = ptr + 1, '"');
1031 1032 1033 1034 1035 1036
	if (!ptr) return MCIERR_NO_CLOSING_QUOTE;
	/* FIXME: shall we escape \" from string ?? */
	if (ptr[-1] == '\\') TRACE("Ooops: un-escaped \"\n");
	*ptr++ = '\0'; /* remove trailing " */
	if (*ptr != ' ' && *ptr != '\0') return MCIERR_EXTRA_CHARACTERS;
    } else {
1037
	ptr = strchrW(ptr, ' ');
1038 1039 1040 1041

	if (ptr) {
	    *ptr++ = '\0';
	} else {
1042
	    ptr = *args + strlenW(*args);
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055
	}
	*str = *args;
    }

    *args = ptr;
    return 0;
}

#define	MCI_DATA_SIZE	16

/**************************************************************************
 * 				MCI_ParseOptArgs		[internal]
 */
1056 1057
static	DWORD	MCI_ParseOptArgs(LPDWORD data, int _offset, LPCWSTR lpCmd,
				 LPWSTR args, LPDWORD dwFlags)
1058 1059
{
    int		len, offset;
1060 1061
    const char* lmem;
    LPCWSTR     str;
1062 1063 1064 1065 1066 1067
    DWORD	dwRet, flg, cflg = 0;
    WORD	eid;
    BOOL	inCst, found;

    /* loop on arguments */
    while (*args) {
1068
	lmem = (const char*)lpCmd;
1069 1070
	found = inCst = FALSE;
	offset = _offset;
1071 1072 1073

	/* skip any leading white space(s) */
	while (*args == ' ') args++;
1074
	TRACE("args=%s offset=%d\n", debugstr_w(args), offset);
1075

1076
	do { /* loop on options for command table for the requested verb */
1077 1078
	    str = (LPCWSTR)lmem;
	    lmem += ((len = strlenW(str)) + 1) * sizeof(WCHAR);
Eric Pouech's avatar
Eric Pouech committed
1079 1080
	    flg = *(const DWORD*)lmem;
	    eid = *(const WORD*)(lmem + sizeof(DWORD));
1081
	    lmem += sizeof(DWORD) + sizeof(WORD);
1082
            /* TRACE("\tcmd=%s inCst=%c eid=%04x\n", debugstr_w(str), inCst ? 'Y' : 'N', eid); */
1083

1084
	    switch (eid) {
1085
	    case MCI_CONSTANT:
1086
		inCst = TRUE;	cflg = flg;	break;
1087
	    case MCI_END_CONSTANT:
1088 1089 1090 1091
		/* there may be additional integral values after flag in constant */
		if (inCst && MCI_GetDWord(&(data[offset]), &args)) {
		    *dwFlags |= cflg;
		}
1092
		inCst = FALSE;	cflg = 0;
1093 1094 1095
		break;
	    }

1096
	    if (strncmpiW(args, str, len) == 0 &&
1097
                ((eid == MCI_STRING && len == 0) || args[len] == 0 || args[len] == ' ')) {
1098 1099
		/* store good values into data[] */
		args += len;
1100
		while (*args == ' ') args++;
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
		found = TRUE;

		switch (eid) {
		case MCI_COMMAND_HEAD:
		case MCI_RETURN:
		case MCI_END_COMMAND:
		case MCI_END_COMMAND_LIST:
		case MCI_CONSTANT: 	/* done above */
		case MCI_END_CONSTANT:  /* done above */
		    break;
1111
		case MCI_FLAG:
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
		    *dwFlags |= flg;
		    break;
		case MCI_INTEGER:
		    if (inCst) {
			data[offset] |= flg;
			*dwFlags |= cflg;
			inCst = FALSE;
		    } else {
			*dwFlags |= flg;
			if (!MCI_GetDWord(&(data[offset]), &args)) {
			    return MCIERR_BAD_INTEGER;
			}
		    }
		    break;
1126
		case MCI_RECT:
1127 1128 1129 1130 1131 1132
		    /* store rect in data (offset...offset+3) */
		    *dwFlags |= flg;
		    if (!MCI_GetDWord(&(data[offset+0]), &args) ||
			!MCI_GetDWord(&(data[offset+1]), &args) ||
			!MCI_GetDWord(&(data[offset+2]), &args) ||
			!MCI_GetDWord(&(data[offset+3]), &args)) {
1133
			ERR("Bad rect %s\n", debugstr_w(args));
1134 1135 1136 1137 1138
			return MCIERR_BAD_INTEGER;
		    }
		    break;
		case MCI_STRING:
		    *dwFlags |= flg;
1139
		    if ((dwRet = MCI_GetString((LPWSTR*)&data[offset], &args)))
1140 1141
			return dwRet;
		    break;
1142
		default:	ERR("oops\n");
1143
		}
1144 1145
		/* exit inside while loop, except if just entered in constant area definition */
		if (!inCst || eid != MCI_CONSTANT) eid = MCI_END_COMMAND;
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
	    } else {
		/* have offset incremented if needed */
		switch (eid) {
		case MCI_COMMAND_HEAD:
		case MCI_RETURN:
		case MCI_END_COMMAND:
		case MCI_END_COMMAND_LIST:
		case MCI_CONSTANT:
		case MCI_FLAG:			break;
		case MCI_INTEGER:		if (!inCst) offset++;	break;
1156
		case MCI_END_CONSTANT:
1157 1158
		case MCI_STRING:		offset++; break;
		case MCI_RECT:			offset += 4; break;
1159
		default:			ERR("oops\n");
1160 1161 1162 1163
		}
	    }
	} while (eid != MCI_END_COMMAND);
	if (!found) {
1164
	    WARN("Optarg %s not found\n", debugstr_w(args));
1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177
	    return MCIERR_UNRECOGNIZED_COMMAND;
	}
	if (offset == MCI_DATA_SIZE) {
	    ERR("Internal data[] buffer overflow\n");
	    return MCIERR_PARSER_INTERNAL;
	}
    }
    return 0;
}

/**************************************************************************
 * 				MCI_HandleReturnValues	[internal]
 */
1178
static	DWORD	MCI_HandleReturnValues(DWORD dwRet, LPWINE_MCIDRIVER wmd, DWORD retType, 
1179
                                       LPDWORD data, LPWSTR lpstrRet, UINT uRetLen)
1180
{
1181 1182 1183 1184 1185
    static const WCHAR wszLd  [] = {'%','l','d',0};
    static const WCHAR wszLd4 [] = {'%','l','d',' ','%','l','d',' ','%','l','d',' ','%','l','d',0};
    static const WCHAR wszCol3[] = {'%','d',':','%','d',':','%','d',0};
    static const WCHAR wszCol4[] = {'%','d',':','%','d',':','%','d',':','%','d',0};

1186
    if (lpstrRet) {
1187
	switch (retType) {
1188 1189
	case 0: /* nothing to return */
	    break;
1190
	case MCI_INTEGER:
1191 1192 1193
	    switch (dwRet & 0xFFFF0000ul) {
	    case 0:
	    case MCI_INTEGER_RETURNED:
1194
		snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1195 1196
		break;
	    case MCI_RESOURCE_RETURNED:
1197
		/* return string which ID is HIWORD(data[1]),
1198
		 * string is loaded from mmsystem.dll */
1199
		LoadStringW(hWinMM32Instance, HIWORD(data[1]), lpstrRet, uRetLen);
1200 1201
		break;
	    case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
1202
		/* return string which ID is HIWORD(data[1]),
1203
		 * string is loaded from driver */
1204
		/* FIXME: this is wrong for a 16 bit handle */
1205
		LoadStringW(GetDriverModuleHandle(wmd->hDriver),
1206
			    HIWORD(data[1]), lpstrRet, uRetLen);
1207 1208
		break;
	    case MCI_COLONIZED3_RETURN:
1209 1210 1211
		snprintfW(lpstrRet, uRetLen, wszCol3,
			  LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
			  LOBYTE(HIWORD(data[1])));
1212 1213
		break;
	    case MCI_COLONIZED4_RETURN:
1214 1215 1216
		snprintfW(lpstrRet, uRetLen, wszCol4,
			  LOBYTE(LOWORD(data[1])), HIBYTE(LOWORD(data[1])),
			  LOBYTE(HIWORD(data[1])), HIBYTE(HIWORD(data[1])));
1217 1218 1219 1220
		break;
	    default:	ERR("Ooops (%04X)\n", HIWORD(dwRet));
	    }
	    break;
1221
	case MCI_STRING:
1222 1223 1224 1225 1226 1227
	    switch (dwRet & 0xFFFF0000ul) {
	    case 0:
		/* nothing to do data[1] == lpstrRet */
		break;
	    case MCI_INTEGER_RETURNED:
		data[1] = *(LPDWORD)lpstrRet;
1228
		snprintfW(lpstrRet, uRetLen, wszLd, data[1]);
1229 1230 1231 1232 1233 1234
		break;
	    default:
		WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
		break;
	    }
	    break;
1235 1236
	case MCI_RECT:
	    if (dwRet & 0xFFFF0000ul)
1237
		WARN("Oooch. MCI_STRING and HIWORD(dwRet)=%04x\n", HIWORD(dwRet));
1238 1239
	    snprintfW(lpstrRet, uRetLen, wszLd4,
                      data[1], data[2], data[3], data[4]);
1240 1241 1242 1243 1244 1245 1246 1247
	    break;
	default:		ERR("oops\n");
	}
    }
    return LOWORD(dwRet);
}

/**************************************************************************
1248
 * 				mciSendStringW		[WINMM.@]
1249
 */
1250
DWORD WINAPI mciSendStringW(LPCWSTR lpstrCommand, LPWSTR lpstrRet,
1251 1252
			    UINT uRetLen, HWND hwndCallback)
{
1253
    LPWSTR		verb, dev, args;
1254 1255 1256 1257
    LPWINE_MCIDRIVER	wmd = 0;
    DWORD		dwFlags = 0, dwRet = 0;
    int			offset = 0;
    DWORD		data[MCI_DATA_SIZE];
1258
    DWORD		retType;
1259 1260 1261 1262
    LPCWSTR		lpCmd = 0;
    LPWSTR		devAlias = NULL;
    static const WCHAR  wszNew[] = {'n','e','w',0};
    static const WCHAR  wszSAliasS[] = {' ','a','l','i','a','s',' ',0};
1263
    static const WCHAR wszTypeS[] = {'t','y','p','e',' ',0};
1264

1265 1266
    TRACE("(%s, %p, %d, %p)\n", 
          debugstr_w(lpstrCommand), lpstrRet, uRetLen, hwndCallback);
1267 1268

    /* format is <command> <device> <optargs> */
1269
    if (!(verb = HeapAlloc(GetProcessHeap(), 0, (strlenW(lpstrCommand)+1) * sizeof(WCHAR))))
1270
	return MCIERR_OUT_OF_MEMORY;
1271 1272
    strcpyW( verb, lpstrCommand );
    CharLowerW(verb);
1273 1274 1275

    memset(data, 0, sizeof(data));

1276
    if (!(args = strchrW(verb, ' '))) {
1277 1278 1279 1280 1281 1282 1283 1284
	dwRet = MCIERR_MISSING_DEVICE_NAME;
	goto errCleanUp;
    }
    *args++ = '\0';
    if ((dwRet = MCI_GetString(&dev, &args))) {
	goto errCleanUp;
    }

1285
    /* Determine devType from open */
1286 1287
    if (!strcmpW(verb, wszOpen)) {
	LPWSTR	devType, tmp;
1288
        WCHAR	buf[128];
1289

1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
	/* case dev == 'new' has to be handled */
	if (!strcmpW(dev, wszNew)) {
	    dev = 0;
	    if ((devType = strstrW(args, wszTypeS)) != NULL) {
		devType += 5;
		tmp = strchrW(devType, ' ');
		if (tmp) *tmp = '\0';
		devType = str_dup_upper(devType);
		if (tmp) *tmp = ' ';
		/* dwFlags and data[2] will be correctly set in ParseOpt loop */
	    } else {
		WARN("open new requires device type\n");
		dwRet = MCIERR_MISSING_DEVICE_NAME;
		goto errCleanUp;
	    }
	} else if ((devType = strchrW(dev, '!')) != NULL) {
1306
	    *devType++ = '\0';
1307 1308 1309 1310
	    tmp = devType; devType = dev; dev = tmp;

	    dwFlags |= MCI_OPEN_TYPE;
	    data[2] = (DWORD)devType;
1311
	    devType = str_dup_upper(devType);
1312 1313
	    dwFlags |= MCI_OPEN_ELEMENT;
	    data[3] = (DWORD)dev;
1314 1315 1316
	} else if (DRIVER_GetLibName(dev, wszMci, buf, sizeof(buf))) {
            /* this is the name of a mci driver's type */
	    tmp = strchrW(dev, ' ');
1317 1318
	    if (tmp) *tmp = '\0';
	    data[2] = (DWORD)dev;
1319
	    devType = str_dup_upper(dev);
1320 1321
	    if (tmp) *tmp = ' ';
	    dwFlags |= MCI_OPEN_TYPE;
1322
	} else {
1323
	    if ((devType = strstrW(args, wszTypeS)) != NULL) {
1324
		devType += 5;
1325
		tmp = strchrW(devType, ' ');
1326
		if (tmp) *tmp = '\0';
1327
		devType = str_dup_upper(devType);
1328 1329 1330 1331 1332 1333
		if (tmp) *tmp = ' ';
		/* dwFlags and data[2] will be correctly set in ParseOpt loop */
	    } else {
		if ((dwRet = MCI_GetDevTypeFromFileName(dev, buf, sizeof(buf))))
		    goto errCleanUp;

1334
		devType = str_dup_upper(buf);
1335 1336 1337 1338
	    }
	    dwFlags |= MCI_OPEN_ELEMENT;
	    data[3] = (DWORD)dev;
	}
1339 1340
	if ((devAlias = strstrW(args, wszSAliasS))) {
            WCHAR*      tmp2;
1341
	    devAlias += 7;
1342
	    if (!(tmp = strchrW(devAlias,' '))) tmp = devAlias + strlenW(devAlias);
1343
	    if (tmp) *tmp = '\0';
1344 1345
            tmp2 = HeapAlloc(GetProcessHeap(), 0, (tmp - devAlias + 1) * sizeof(WCHAR) );
            memcpy( tmp2, devAlias, (tmp - devAlias) * sizeof(WCHAR) );
1346 1347
            tmp2[tmp - devAlias] = 0;
            data[4] = (DWORD)tmp2;
1348 1349
	    /* should be done in regular options parsing */
	    /* dwFlags |= MCI_OPEN_ALIAS; */
1350 1351 1352 1353
	} else if (dev == 0) {
	    /* "open new" requires alias */
	    dwRet = MCIERR_NEW_REQUIRES_ALIAS;
	    goto errCleanUp;
1354 1355
	}

1356
	dwRet = MCI_LoadMciDriver(devType, &wmd);
1357 1358
	if (dwRet == MCIERR_DEVICE_NOT_INSTALLED)
	    dwRet = MCIERR_INVALID_DEVICE_NAME;
1359 1360
	HeapFree(GetProcessHeap(), 0, devType);
	if (dwRet) {
1361
	    MCI_UnLoadMciDriver(wmd);
1362 1363
	    goto errCleanUp;
	}
1364
    } else if (!(wmd = MCI_GetDriver(mciGetDeviceIDW(dev)))) {
1365
	/* auto open */
1366
        static const WCHAR wszOpenWait[] = {'o','p','e','n',' ','%','s',' ','w','a','i','t',0};
1367 1368
	WCHAR   buf[128];
	sprintfW(buf, wszOpenWait, dev);
1369

1370
	if ((dwRet = mciSendStringW(buf, NULL, 0, 0)) != 0)
1371
	    goto errCleanUp;
1372

1373
	wmd = MCI_GetDriver(mciGetDeviceIDW(dev));
1374
	if (!wmd) {
1375
	    /* FIXME: memory leak, MCI driver is not closed */
1376 1377 1378
	    dwRet = MCIERR_INVALID_DEVICE_ID;
	    goto errCleanUp;
	}
1379 1380 1381
    }

    /* get the verb in the different command tables */
1382 1383 1384 1385 1386 1387
    if (wmd) {
	/* try the device specific command table */
	lpCmd = MCI_FindCommand(wmd->uSpecificCmdTable, verb);
	if (!lpCmd) {
	    /* try the type specific command table */
	    if (wmd->uTypeCmdTable == MCI_COMMAND_TABLE_NOT_LOADED)
1388
		wmd->uTypeCmdTable = MCI_GetCommandTable(wmd->wType);
1389 1390 1391
	    if (wmd->uTypeCmdTable != MCI_NO_COMMAND_TABLE)
		lpCmd = MCI_FindCommand(wmd->uTypeCmdTable, verb);
	}
1392 1393
    }
    /* try core command table */
1394
    if (!lpCmd) lpCmd = MCI_FindCommand(MCI_GetCommandTable(0), verb);
1395 1396

    if (!lpCmd) {
1397
	TRACE("Command %s not found!\n", debugstr_w(verb));
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
	dwRet = MCIERR_UNRECOGNIZED_COMMAND;
	goto errCleanUp;
    }

    /* set up call back */
    if (hwndCallback != 0) {
	dwFlags |= MCI_NOTIFY;
	data[0] = (DWORD)hwndCallback;
    }

    /* set return information */
1409
    switch (retType = MCI_GetReturnType(lpCmd)) {
1410 1411 1412 1413 1414 1415 1416
    case 0:		offset = 1;	break;
    case MCI_INTEGER:	offset = 2;	break;
    case MCI_STRING:	data[1] = (DWORD)lpstrRet; data[2] = uRetLen; offset = 3; break;
    case MCI_RECT:	offset = 5;	break;
    default:	ERR("oops\n");
    }

1417 1418
    TRACE("verb=%s on dev=%s; offset=%d\n", 
          debugstr_w(verb), debugstr_w(dev), offset);
1419 1420 1421 1422

    if ((dwRet = MCI_ParseOptArgs(data, offset, lpCmd, args, &dwFlags)))
	goto errCleanUp;

1423 1424 1425
    /* FIXME: the command should get it's own notification window set up and
     * ask for device closing while processing the notification mechanism
     */
1426 1427
    if (lpstrRet && uRetLen) *lpstrRet = '\0';

1428
    TRACE("[%d, %s, %08x, %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s %08x/%s]\n",
1429
	  wmd->wDeviceID, MCI_MessageToString(MCI_GetMessage(lpCmd)), dwFlags,
1430 1431 1432
	  data[0], debugstr_w((WCHAR *)data[0]), data[1], debugstr_w((WCHAR *)data[1]),
	  data[2], debugstr_w((WCHAR *)data[2]), data[3], debugstr_w((WCHAR *)data[3]),
	  data[4], debugstr_w((WCHAR *)data[4]), data[5], debugstr_w((WCHAR *)data[5]));
1433

1434 1435
    if (strcmpW(verb, wszOpen) == 0) {
	if ((dwRet = MCI_FinishOpen(wmd, (LPMCI_OPEN_PARMSW)data, dwFlags)))
1436
	    MCI_UnLoadMciDriver(wmd);
1437 1438 1439 1440
	/* FIXME: notification is not properly shared across two opens */
    } else {
	dwRet = MCI_SendCommand(wmd->wDeviceID, MCI_GetMessage(lpCmd), dwFlags, (DWORD)data, TRUE);
    }
1441
    TRACE("=> 1/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1442
    dwRet = MCI_HandleReturnValues(dwRet, wmd, retType, data, lpstrRet, uRetLen);
1443
    TRACE("=> 2/ %x (%s)\n", dwRet, debugstr_w(lpstrRet));
1444 1445 1446

errCleanUp:
    HeapFree(GetProcessHeap(), 0, verb);
1447
    HeapFree(GetProcessHeap(), 0, devAlias);
1448 1449 1450 1451
    return dwRet;
}

/**************************************************************************
1452
 * 				mciSendStringA			[WINMM.@]
1453
 */
1454
DWORD WINAPI mciSendStringA(LPCSTR lpstrCommand, LPSTR lpstrRet,
1455
			    UINT uRetLen, HWND hwndCallback)
1456
{
1457 1458
    LPWSTR 	lpwstrCommand;
    LPWSTR      lpwstrRet = NULL;
1459
    UINT	ret;
1460
    INT len;
1461

1462
    /* FIXME: is there something to do with lpstrReturnString ? */
1463 1464 1465 1466
    len = MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, NULL, 0 );
    lpwstrCommand = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
    MultiByteToWideChar( CP_ACP, 0, lpstrCommand, -1, lpwstrCommand, len );
    if (lpstrRet)
1467
    {
1468
        lpwstrRet = HeapAlloc(GetProcessHeap(), 0, uRetLen * sizeof(WCHAR));
1469 1470 1471 1472 1473
        if (!lpwstrRet) {
            WARN("no memory\n");
            HeapFree( GetProcessHeap(), 0, lpwstrCommand );
            return MCIERR_OUT_OF_MEMORY;
        }
1474
    }
1475
    ret = mciSendStringW(lpwstrCommand, lpwstrRet, uRetLen, hwndCallback);
1476
    if (lpwstrRet)
1477 1478 1479
        WideCharToMultiByte( CP_ACP, 0, lpwstrRet, -1, lpstrRet, uRetLen, NULL, NULL );
    HeapFree(GetProcessHeap(), 0, lpwstrCommand);
    HeapFree(GetProcessHeap(), 0, lpwstrRet);
1480
    return ret;
1481 1482
}

1483
/**************************************************************************
1484
 * 				mciExecute			[WINMM.@]
Patrik Stridvall's avatar
Patrik Stridvall committed
1485
 * 				mciExecute			[MMSYSTEM.712]
1486
 */
1487
BOOL WINAPI mciExecute(LPCSTR lpstrCommand)
1488 1489 1490
{
    char	strRet[256];
    DWORD	ret;
1491

1492 1493
    TRACE("(%s)!\n", lpstrCommand);

1494
    ret = mciSendStringA(lpstrCommand, strRet, sizeof(strRet), 0);
1495 1496
    if (ret != 0) {
	if (!mciGetErrorStringA(ret, strRet, sizeof(strRet))) {
1497
	    sprintf(strRet, "Unknown MCI error (%d)", ret);
1498
	}
1499
	MessageBoxA(0, strRet, "Error in mciExecute()", MB_OK);
1500 1501
    }
    /* FIXME: what shall I return ? */
1502
    return TRUE;
1503
}
1504

1505
/**************************************************************************
1506
 *                    	mciLoadCommandResource  		[WINMM.@]
1507 1508 1509
 *
 * Strangely, this function only exists as an UNICODE one.
 */
1510
UINT WINAPI mciLoadCommandResource(HINSTANCE hInst, LPCWSTR resNameW, UINT type)
1511
{
1512 1513 1514 1515
    HRSRC	        hRsrc = 0;
    HGLOBAL      	hMem;
    UINT16		ret = MCI_NO_COMMAND_TABLE;

1516
    TRACE("(%p, %s, %d)!\n", hInst, debugstr_w(resNameW), type);
1517

1518
    /* if a file named "resname.mci" exits, then load resource "resname" from it
1519 1520 1521 1522
     * otherwise directly from driver
     * We don't support it (who uses this feature ?), but we check anyway
     */
    if (!type) {
1523 1524
#if 0
        /* FIXME: we should put this back into order, but I never found a program
1525
         * actually using this feature, so we may not need it
1526
         */
1527 1528 1529 1530 1531 1532 1533 1534
	char		buf[128];
	OFSTRUCT       	ofs;

	strcat(strcpy(buf, resname), ".mci");
	if (OpenFile(buf, &ofs, OF_EXIST) != HFILE_ERROR) {
	    FIXME("NIY: command table to be loaded from '%s'\n", ofs.szPathName);
	}
#endif
1535
    }
1536
    if (!(hRsrc = FindResourceW(hInst, resNameW, (LPWSTR)RT_RCDATA))) {
1537 1538
	WARN("No command table found in resource\n");
    } else if ((hMem = LoadResource(hInst, hRsrc))) {
1539
	ret = MCI_SetCommandTable(LockResource(hMem), type);
1540 1541 1542 1543 1544 1545 1546
    } else {
	WARN("Couldn't load resource.\n");
    }
    TRACE("=> %04x\n", ret);
    return ret;
}

1547
/**************************************************************************
1548
 *                    	mciFreeCommandResource			[WINMM.@]
1549
 */
1550
BOOL WINAPI mciFreeCommandResource(UINT uTable)
1551
{
1552 1553
    TRACE("(%08x)!\n", uTable);

1554
    return MCI_DeleteCommandTable(uTable, FALSE);
1555 1556 1557
}

/**************************************************************************
1558
 * 			MCI_SendCommandFrom32			[internal]
1559
 */
1560
DWORD MCI_SendCommandFrom32(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1561
{
1562
    DWORD		dwRet = MCIERR_INVALID_DEVICE_ID;
1563 1564
    LPWINE_MCIDRIVER	wmd = MCI_GetDriver(wDevID);

1565 1566 1567
    if (wmd) {
	if (wmd->bIs32) {
	    dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1568
	} else if (pFnMciMapMsg32WTo16) {
1569
	    WINMM_MapType	res;
1570

1571
	    switch (res = pFnMciMapMsg32WTo16(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1572
	    case WINMM_MAP_MSGERROR:
1573 1574 1575
		TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
		dwRet = MCIERR_DRIVER_INTERNAL;
		break;
1576
	    case WINMM_MAP_NOMEM:
1577 1578 1579
		TRACE("Problem mapping msg=%s from 32a to 16\n", MCI_MessageToString(wMsg));
		dwRet = MCIERR_OUT_OF_MEMORY;
		break;
1580 1581
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
1582
		dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1583
		if (res == WINMM_MAP_OKMEM)
1584
		    pFnMciUnMapMsg32WTo16(wmd->wType, wMsg, dwParam1, dwParam2);
1585
		break;
1586 1587 1588 1589 1590 1591 1592 1593 1594
	    }
	}
    }
    return dwRet;
}

/**************************************************************************
 * 			MCI_SendCommandFrom16			[internal]
 */
1595
DWORD MCI_SendCommandFrom16(MCIDEVICEID wDevID, UINT16 wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1596
{
1597
    DWORD		dwRet = MCIERR_INVALID_DEVICE_ID;
1598 1599
    LPWINE_MCIDRIVER	wmd = MCI_GetDriver(wDevID);

1600
    if (wmd) {
1601
	dwRet = MCIERR_INVALID_DEVICE_ID;
1602

1603
	if (wmd->bIs32 && pFnMciMapMsg16To32W) {
1604
	    WINMM_MapType		res;
1605

1606
	    switch (res = pFnMciMapMsg16To32W(wmd->wType, wMsg, dwParam1, &dwParam2)) {
1607
	    case WINMM_MAP_MSGERROR:
1608
		TRACE("Not handled yet (%s)\n", MCI_MessageToString(wMsg));
1609
		dwRet = MCIERR_DRIVER_INTERNAL;
1610
		break;
1611
	    case WINMM_MAP_NOMEM:
1612
		TRACE("Problem mapping msg=%s from 16 to 32a\n", MCI_MessageToString(wMsg));
1613 1614
		dwRet = MCIERR_OUT_OF_MEMORY;
		break;
1615 1616
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
1617
		dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1618
		if (res == WINMM_MAP_OKMEM)
1619
		    pFnMciUnMapMsg16To32W(wmd->wType, wMsg, dwParam1, dwParam2);
1620
		break;
1621
	    }
1622 1623
	} else {
	    dwRet = SendDriverMessage(wmd->hDriver, wMsg, dwParam1, dwParam2);
1624 1625 1626 1627 1628 1629 1630 1631
	}
    }
    return dwRet;
}

/**************************************************************************
 * 			MCI_Open				[internal]
 */
1632
static	DWORD MCI_Open(DWORD dwParam, LPMCI_OPEN_PARMSW lpParms)
1633
{
1634
    WCHAR			strDevTyp[128];
1635
    DWORD 			dwRet;
1636
    LPWINE_MCIDRIVER		wmd = NULL;
1637

1638
    TRACE("(%08X, %p)\n", dwParam, lpParms);
1639
    if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1640 1641

    /* only two low bytes are generic, the other ones are dev type specific */
1642
#define WINE_MCIDRIVER_SUPP	(0xFFFF0000|MCI_OPEN_SHAREABLE|MCI_OPEN_ELEMENT| \
1643 1644
                         MCI_OPEN_ALIAS|MCI_OPEN_TYPE|MCI_OPEN_TYPE_ID| \
                         MCI_NOTIFY|MCI_WAIT)
1645 1646
    if ((dwParam & ~WINE_MCIDRIVER_SUPP) != 0) {
	FIXME("Unsupported yet dwFlags=%08lX\n", dwParam & ~WINE_MCIDRIVER_SUPP);
1647
    }
1648
#undef WINE_MCIDRIVER_SUPP
1649 1650 1651

    strDevTyp[0] = 0;

1652 1653 1654 1655 1656 1657
    if (dwParam & MCI_OPEN_TYPE) {
	if (dwParam & MCI_OPEN_TYPE_ID) {
	    WORD uDevType = LOWORD((DWORD)lpParms->lpstrDeviceType);

	    if (uDevType < MCI_DEVTYPE_FIRST ||
		uDevType > MCI_DEVTYPE_LAST ||
1658
		!LoadStringW(hWinMM32Instance, uDevType,
1659
                             strDevTyp, sizeof(strDevTyp) / sizeof(WCHAR))) {
1660 1661 1662 1663
		dwRet = MCIERR_BAD_INTEGER;
		goto errCleanUp;
	    }
	} else {
1664
	    LPWSTR	ptr;
1665 1666 1667 1668
	    if (lpParms->lpstrDeviceType == NULL) {
		dwRet = MCIERR_NULL_PARAMETER_BLOCK;
		goto errCleanUp;
	    }
1669 1670
	    strcpyW(strDevTyp, lpParms->lpstrDeviceType);
	    ptr = strchrW(strDevTyp, '!');
1671 1672 1673 1674 1675 1676 1677
	    if (ptr) {
		/* this behavior is not documented in windows. However, since, in
		 * some occasions, MCI_OPEN handling is translated by WinMM into
		 * a call to mciSendString("open <type>"); this code shall be correct
		 */
		if (dwParam & MCI_OPEN_ELEMENT) {
		    ERR("Both MCI_OPEN_ELEMENT(%s) and %s are used\n",
1678 1679
			debugstr_w(lpParms->lpstrElementName), 
                        debugstr_w(strDevTyp));
1680 1681 1682 1683 1684 1685 1686 1687
		    dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
		    goto errCleanUp;
		}
		dwParam |= MCI_OPEN_ELEMENT;
		*ptr++ = 0;
		/* FIXME: not a good idea to write in user supplied buffer */
		lpParms->lpstrElementName = ptr;
	    }
1688

1689
	}
1690
	TRACE("devType=%s !\n", debugstr_w(strDevTyp));
1691 1692
    }

1693
    if (dwParam & MCI_OPEN_ELEMENT) {
1694
	TRACE("lpstrElementName=%s\n", debugstr_w(lpParms->lpstrElementName));
1695 1696 1697 1698 1699 1700 1701

	if (dwParam & MCI_OPEN_ELEMENT_ID) {
	    FIXME("Unsupported yet flag MCI_OPEN_ELEMENT_ID\n");
	    dwRet = MCIERR_UNRECOGNIZED_KEYWORD;
	    goto errCleanUp;
	}

1702 1703 1704 1705 1706
	if (!lpParms->lpstrElementName) {
	    dwRet = MCIERR_NULL_PARAMETER_BLOCK;
	    goto errCleanUp;
	}

1707
	/* type, if given as a parameter, supersedes file extension */
1708 1709
	if (!strDevTyp[0] &&
	    MCI_GetDevTypeFromFileName(lpParms->lpstrElementName,
1710
				       strDevTyp, sizeof(strDevTyp))) {
1711 1712
            static const WCHAR wszCdAudio[] = {'C','D','A','U','D','I','O',0};
	    if (GetDriveTypeW(lpParms->lpstrElementName) != DRIVE_CDROM) {
1713 1714
		dwRet = MCIERR_EXTENSION_NOT_FOUND;
		goto errCleanUp;
1715
	    }
1716
	    /* FIXME: this will not work if several CDROM drives are installed on the machine */
1717
	    strcpyW(strDevTyp, wszCdAudio);
1718 1719
	}
    }
1720

1721
    if (strDevTyp[0] == 0) {
1722
	FIXME("Couldn't load driver\n");
1723
	dwRet = MCIERR_INVALID_DEVICE_NAME;
1724
	goto errCleanUp;
1725
    }
1726

1727
    if (dwParam & MCI_OPEN_ALIAS) {
1728
	TRACE("Alias=%s !\n", debugstr_w(lpParms->lpstrAlias));
1729 1730 1731 1732 1733
	if (!lpParms->lpstrAlias) {
	    dwRet = MCIERR_NULL_PARAMETER_BLOCK;
	    goto errCleanUp;
	}
    }
1734

1735
    if ((dwRet = MCI_LoadMciDriver(strDevTyp, &wmd))) {
1736
	goto errCleanUp;
1737
    }
1738

1739
    if ((dwRet = MCI_FinishOpen(wmd, lpParms, dwParam))) {
1740
	TRACE("Failed to open driver (MCI_OPEN_DRIVER) [%08x], closing\n", dwRet);
1741 1742
	/* FIXME: is dwRet the correct ret code ? */
	goto errCleanUp;
1743
    }
1744 1745

    /* only handled devices fall through */
1746
    TRACE("wDevID=%04X wDeviceID=%d dwRet=%d\n", wmd->wDeviceID, lpParms->wDeviceID, dwRet);
1747

1748
    if (dwParam & MCI_NOTIFY)
1749
	mciDriverNotify((HWND)lpParms->dwCallback, wmd->wDeviceID, MCI_NOTIFY_SUCCESSFUL);
1750

1751 1752
    return 0;
errCleanUp:
1753
    if (wmd) MCI_UnLoadMciDriver(wmd);
1754

1755
    if (dwParam & MCI_NOTIFY)
1756
	mciDriverNotify((HWND)lpParms->dwCallback, 0, MCI_NOTIFY_FAILURE);
1757 1758 1759 1760 1761 1762
    return dwRet;
}

/**************************************************************************
 * 			MCI_Close				[internal]
 */
1763
static	DWORD MCI_Close(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
1764
{
1765 1766 1767
    DWORD		dwRet;
    LPWINE_MCIDRIVER	wmd;

1768
    TRACE("(%04x, %08X, %p)\n", wDevID, dwParam, lpParms);
1769 1770

    if (wDevID == MCI_ALL_DEVICE_ID) {
1771 1772
	LPWINE_MCIDRIVER	next;

1773
	EnterCriticalSection(&WINMM_cs);
1774
	/* FIXME: shall I notify once after all is done, or for
1775 1776 1777
	 * each of the open drivers ? if the latest, which notif
	 * to return when only one fails ?
	 */
1778
	for (wmd = MciDrivers; wmd; ) {
1779 1780 1781
	    next = wmd->lpNext;
	    MCI_Close(wmd->wDeviceID, dwParam, lpParms);
	    wmd = next;
1782
	}
1783
	LeaveCriticalSection(&WINMM_cs);
1784 1785 1786 1787 1788
	return 0;
    }

    if (!(wmd = MCI_GetDriver(wDevID))) {
	return MCIERR_INVALID_DEVICE_ID;
1789 1790
    }

1791
    dwRet = MCI_SendCommandFrom32(wDevID, MCI_CLOSE_DRIVER, dwParam, (DWORD)lpParms);
1792

1793
    MCI_UnLoadMciDriver(wmd);
1794

1795
    if (dwParam & MCI_NOTIFY)
1796 1797 1798
        mciDriverNotify(lpParms ? (HWND)lpParms->dwCallback : 0,
                        wDevID,
                        dwRet ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1799

1800 1801 1802 1803 1804 1805
    return dwRet;
}

/**************************************************************************
 * 			MCI_WriteString				[internal]
 */
1806
DWORD	MCI_WriteString(LPWSTR lpDstStr, DWORD dstSize, LPCWSTR lpSrcStr)
1807
{
1808
    DWORD	ret = 0;
1809

1810
    if (lpSrcStr) {
1811 1812 1813
        dstSize /= sizeof(WCHAR);
	if (dstSize <= strlenW(lpSrcStr)) {
	    lstrcpynW(lpDstStr, lpSrcStr, dstSize - 1);
1814 1815
	    ret = MCIERR_PARAM_OVERFLOW;
	} else {
1816
	    strcpyW(lpDstStr, lpSrcStr);
1817
	}
1818
    } else {
1819
	*lpDstStr = 0;
1820 1821 1822 1823 1824 1825 1826
    }
    return ret;
}

/**************************************************************************
 * 			MCI_Sysinfo				[internal]
 */
1827
static	DWORD MCI_SysInfo(UINT uDevID, DWORD dwFlags, LPMCI_SYSINFO_PARMSW lpParms)
1828
{
1829
    DWORD		ret = MCIERR_INVALID_DEVICE_ID, cnt = 0;
1830
    WCHAR		buf[2048], *s = buf, *p;
1831
    LPWINE_MCIDRIVER	wmd;
1832
    HKEY		hKey;
1833 1834 1835

    if (lpParms == NULL)			return MCIERR_NULL_PARAMETER_BLOCK;

1836
    TRACE("(%08x, %08X, %08X[num=%d, wDevTyp=%u])\n",
1837
	  uDevID, dwFlags, (DWORD)lpParms, lpParms->dwNumber, lpParms->wDeviceType);
1838

1839 1840
    switch (dwFlags & ~MCI_SYSINFO_OPEN) {
    case MCI_SYSINFO_QUANTITY:
1841 1842 1843
	if (lpParms->wDeviceType < MCI_DEVTYPE_FIRST || lpParms->wDeviceType > MCI_DEVTYPE_LAST) {
	    if (dwFlags & MCI_SYSINFO_OPEN) {
		TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers\n");
1844 1845
		EnterCriticalSection(&WINMM_cs);
		for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1846
		    cnt++;
1847
		}
1848
		LeaveCriticalSection(&WINMM_cs);
1849
	    } else {
1850
		TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers\n");
1851
		if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci,
1852
			  	   0, KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
1853
		    RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 0, 0, 0, 0, 0, 0, 0);
1854
		    RegCloseKey( hKey );
1855
		}
1856
		if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni))
1857
		    for (s = buf; *s; s += strlenW(s) + 1) cnt++;
1858 1859 1860 1861
	    }
	} else {
	    if (dwFlags & MCI_SYSINFO_OPEN) {
		TRACE("MCI_SYSINFO_QUANTITY: # of open MCI drivers of type %u\n", lpParms->wDeviceType);
1862 1863
		EnterCriticalSection(&WINMM_cs);
		for (wmd = MciDrivers; wmd; wmd = wmd->lpNext) {
1864 1865
		    if (wmd->wType == lpParms->wDeviceType) cnt++;
		}
1866
		LeaveCriticalSection(&WINMM_cs);
1867 1868 1869 1870
	    } else {
		TRACE("MCI_SYSINFO_QUANTITY: # of installed MCI drivers of type %u\n", lpParms->wDeviceType);
		FIXME("Don't know how to get # of MCI devices of a given type\n");
		cnt = 1;
1871 1872
	    }
	}
1873
	*(DWORD*)lpParms->lpstrReturn = cnt;
1874
	TRACE("(%d) => '%d'\n", lpParms->dwNumber, *(DWORD*)lpParms->lpstrReturn);
1875
	ret = MCI_INTEGER_RETURNED;
1876 1877
	break;
    case MCI_SYSINFO_INSTALLNAME:
1878
	TRACE("MCI_SYSINFO_INSTALLNAME\n");
1879
	if ((wmd = MCI_GetDriver(uDevID))) {
1880
	    ret = MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize,
1881
				  wmd->lpstrDeviceType);
1882 1883 1884 1885
	} else {
	    *lpParms->lpstrReturn = 0;
	    ret = MCIERR_INVALID_DEVICE_ID;
	}
1886
	TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1887 1888
	break;
    case MCI_SYSINFO_NAME:
1889
	TRACE("MCI_SYSINFO_NAME\n");
1890
	if (dwFlags & MCI_SYSINFO_OPEN) {
1891
	    FIXME("Don't handle MCI_SYSINFO_NAME|MCI_SYSINFO_OPEN (yet)\n");
1892 1893
	    ret = MCIERR_UNRECOGNIZED_COMMAND;
	} else {
1894 1895 1896 1897 1898 1899
	    s = NULL;
	    if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, wszHklmMci, 0, 
                               KEY_QUERY_VALUE, &hKey ) == ERROR_SUCCESS) {
		if (RegQueryInfoKeyW( hKey, 0, 0, 0, &cnt, 
                                      0, 0, 0, 0, 0, 0, 0) == ERROR_SUCCESS && 
                    lpParms->dwNumber <= cnt) {
1900
    		    DWORD bufLen = sizeof(buf);
1901 1902 1903
		    if (RegEnumKeyExW(hKey, lpParms->dwNumber - 1, 
                                      buf, &bufLen, 0, 0, 0, 0) == ERROR_SUCCESS)
                        s = buf;
1904 1905 1906 1907
		}
	        RegCloseKey( hKey );
	    }
	    if (!s) {
1908
		if (GetPrivateProfileStringW(wszMci, 0, wszNull, buf, sizeof(buf) / sizeof(buf[0]), wszSystemIni)) {
1909
		    for (p = buf; *p; p += strlenW(p) + 1, cnt++) {
1910
                        TRACE("%d: %s\n", cnt, debugstr_w(p));
1911 1912 1913 1914 1915 1916 1917
			if (cnt == lpParms->dwNumber - 1) {
			    s = p;
			    break;
			}
		    }
		}
	    }
1918
	    ret = s ? MCI_WriteString(lpParms->lpstrReturn, lpParms->dwRetSize / sizeof(WCHAR), s) : MCIERR_OUTOFRANGE;
1919
	}
1920
	TRACE("(%d) => %s\n", lpParms->dwNumber, debugstr_w(lpParms->lpstrReturn));
1921 1922
	break;
    default:
1923
	TRACE("Unsupported flag value=%08x\n", dwFlags);
1924 1925 1926 1927 1928
	ret = MCIERR_UNRECOGNIZED_COMMAND;
    }
    return ret;
}

1929 1930 1931
/**************************************************************************
 * 			MCI_Break				[internal]
 */
1932
static	DWORD MCI_Break(UINT wDevID, DWORD dwFlags, LPMCI_BREAK_PARMS lpParms)
1933 1934
{
    DWORD	dwRet = 0;
1935

1936 1937 1938
    if (lpParms == NULL)	return MCIERR_NULL_PARAMETER_BLOCK;

    if (dwFlags & MCI_NOTIFY)
1939
	mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
1940
                        (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);
1941 1942 1943

    return dwRet;
}
1944

1945 1946 1947
/**************************************************************************
 * 			MCI_Sound				[internal]
 */
1948
static	DWORD MCI_Sound(UINT wDevID, DWORD dwFlags, LPMCI_SOUND_PARMSW lpParms)
1949 1950 1951 1952 1953 1954
{
    DWORD	dwRet = 0;

    if (lpParms == NULL)	return MCIERR_NULL_PARAMETER_BLOCK;

    if (dwFlags & MCI_SOUND_NAME)
1955
        dwRet = sndPlaySoundW(lpParms->lpstrSoundName, SND_SYNC) ? MMSYSERR_NOERROR : MMSYSERR_ERROR;
1956 1957 1958 1959 1960 1961 1962 1963 1964
    else
        dwRet = MMSYSERR_ERROR; /* what should be done ??? */
    if (dwFlags & MCI_NOTIFY)
	mciDriverNotify((HWND)lpParms->dwCallback, wDevID,
                        (dwRet == 0) ? MCI_NOTIFY_SUCCESSFUL : MCI_NOTIFY_FAILURE);

    return dwRet;
}

1965 1966 1967
/**************************************************************************
 * 			MCI_SendCommand				[internal]
 */
1968 1969
DWORD	MCI_SendCommand(UINT wDevID, UINT16 wMsg, DWORD_PTR dwParam1,
			DWORD_PTR dwParam2, BOOL bFrom32)
1970 1971 1972 1973 1974 1975
{
    DWORD		dwRet = MCIERR_UNRECOGNIZED_COMMAND;

    switch (wMsg) {
    case MCI_OPEN:
	if (bFrom32) {
1976 1977
	    dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
	} else if (pFnMciMapMsg16To32W) {
1978
	    switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
1979 1980
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
1981
		dwRet = MCI_Open(dwParam1, (LPMCI_OPEN_PARMSW)dwParam2);
1982
		pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
1983 1984 1985 1986 1987 1988 1989 1990
		break;
	    default: break; /* so that gcc does not bark */
	    }
	}
	break;
    case MCI_CLOSE:
	if (bFrom32) {
	    dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1991
	} else if (pFnMciMapMsg16To32W) {
1992
	    switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
1993 1994
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
1995
		dwRet = MCI_Close(wDevID, dwParam1, (LPMCI_GENERIC_PARMS)dwParam2);
1996
		pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
1997 1998 1999 2000 2001 2002 2003
		break;
	    default: break; /* so that gcc does not bark */
	    }
	}
	break;
    case MCI_SYSINFO:
	if (bFrom32) {
2004 2005
	    dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
	} else if (pFnMciMapMsg16To32W) {
2006
	    switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
2007 2008
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
2009
		dwRet = MCI_SysInfo(wDevID, dwParam1, (LPMCI_SYSINFO_PARMSW)dwParam2);
2010
		pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
2011
		break;
2012
	    default: break; /* so that gcc does not bark */
2013 2014 2015 2016 2017 2018
	    }
	}
	break;
    case MCI_BREAK:
	if (bFrom32) {
	    dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2019
	} else if (pFnMciMapMsg16To32W) {
2020
	    switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
2021 2022
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
2023
		dwRet = MCI_Break(wDevID, dwParam1, (LPMCI_BREAK_PARMS)dwParam2);
2024
		pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
2025 2026 2027 2028 2029 2030
		break;
	    default: break; /* so that gcc does not bark */
	    }
	}
	break;
    case MCI_SOUND:
2031
	if (bFrom32) {
2032 2033
	    dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
	} else if (pFnMciMapMsg16To32W) {
2034
	    switch (pFnMciMapMsg16To32W(0, wMsg, dwParam1, &dwParam2)) {
2035 2036
	    case WINMM_MAP_OK:
	    case WINMM_MAP_OKMEM:
2037
		dwRet = MCI_Sound(wDevID, dwParam1, (LPMCI_SOUND_PARMSW)dwParam2);
2038
		pFnMciUnMapMsg16To32W(0, wMsg, dwParam1, dwParam2);
2039 2040 2041 2042
		break;
	    default: break; /* so that gcc does not bark */
	    }
	}
2043 2044 2045 2046 2047 2048 2049 2050 2051
	break;
    default:
	if (wDevID == MCI_ALL_DEVICE_ID) {
	    FIXME("unhandled MCI_ALL_DEVICE_ID\n");
	    dwRet = MCIERR_CANNOT_USE_ALL;
	} else {
	    dwRet = (bFrom32) ?
		MCI_SendCommandFrom32(wDevID, wMsg, dwParam1, dwParam2) :
		MCI_SendCommandFrom16(wDevID, wMsg, dwParam1, dwParam2);
2052
	}
2053 2054 2055 2056 2057
	break;
    }
    return dwRet;
}

2058 2059 2060
/**************************************************************************
 * 				MCI_CleanUp			[internal]
 *
2061
 * Some MCI commands need to be cleaned-up (when not called from
2062
 * mciSendString), because MCI drivers return extra information for string
2063
 * transformation. This function gets rid of them.
2064
 */
2065
LRESULT		MCI_CleanUp(LRESULT dwRet, UINT wMsg, DWORD dwParam2)
2066
{
2067
    if (LOWORD(dwRet))
2068 2069
	return LOWORD(dwRet);

2070 2071 2072 2073 2074 2075 2076
    switch (wMsg) {
    case MCI_GETDEVCAPS:
	switch (dwRet & 0xFFFF0000ul) {
	case 0:
	case MCI_COLONIZED3_RETURN:
	case MCI_COLONIZED4_RETURN:
	case MCI_INTEGER_RETURNED:
2077 2078 2079 2080
	    /* nothing to do */
	    break;
	case MCI_RESOURCE_RETURNED:
	case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2081
	    {
2082
		LPMCI_GETDEVCAPS_PARMS	lmgp;
2083

2084
		lmgp = (LPMCI_GETDEVCAPS_PARMS)(void*)dwParam2;
2085
		TRACE("Changing %08x to %08x\n", lmgp->dwReturn, LOWORD(lmgp->dwReturn));
2086
		lmgp->dwReturn = LOWORD(lmgp->dwReturn);
2087
	    }
2088 2089
	    break;
	default:
2090
	    FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2091
		  HIWORD(dwRet), MCI_MessageToString(wMsg));
2092 2093 2094 2095 2096 2097 2098 2099
	}
	break;
    case MCI_STATUS:
	switch (dwRet & 0xFFFF0000ul) {
	case 0:
	case MCI_COLONIZED3_RETURN:
	case MCI_COLONIZED4_RETURN:
	case MCI_INTEGER_RETURNED:
2100 2101 2102 2103
	    /* nothing to do */
	    break;
	case MCI_RESOURCE_RETURNED:
	case MCI_RESOURCE_RETURNED|MCI_RESOURCE_DRIVER:
2104
	    {
2105
		LPMCI_STATUS_PARMS	lsp;
2106

2107
		lsp = (LPMCI_STATUS_PARMS)(void*)dwParam2;
2108
		TRACE("Changing %08x to %08x\n", lsp->dwReturn, LOWORD(lsp->dwReturn));
2109 2110 2111 2112
		lsp->dwReturn = LOWORD(lsp->dwReturn);
	    }
	    break;
	default:
2113
	    FIXME("Unsupported value for hiword (%04x) returned by DriverProc(%s)\n",
2114 2115 2116 2117 2118 2119 2120
		  HIWORD(dwRet), MCI_MessageToString(wMsg));
	}
	break;
    case MCI_SYSINFO:
	switch (dwRet & 0xFFFF0000ul) {
	case 0:
	case MCI_INTEGER_RETURNED:
2121
	    /* nothing to do */
2122 2123
	    break;
	default:
2124
	    FIXME("Unsupported value for hiword (%04x)\n", HIWORD(dwRet));
2125 2126 2127
	}
	break;
    default:
2128
	if (HIWORD(dwRet)) {
2129
	    FIXME("Got non null hiword for dwRet=0x%08lx for command %s\n",
2130
		  dwRet, MCI_MessageToString(wMsg));
2131
	}
2132 2133
	break;
    }
2134
    return LOWORD(dwRet);
2135
}
2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146

/**************************************************************************
 * 				mciGetErrorStringW		[WINMM.@]
 */
BOOL WINAPI mciGetErrorStringW(MCIERROR wError, LPWSTR lpstrBuffer, UINT uLength)
{
    BOOL		ret = FALSE;

    if (lpstrBuffer != NULL && uLength > 0 &&
	wError >= MCIERR_BASE && wError <= MCIERR_CUSTOM_DRIVER_BASE) {

2147
	if (LoadStringW(hWinMM32Instance, wError, lpstrBuffer, uLength) > 0) {
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163
	    ret = TRUE;
	}
    }
    return ret;
}

/**************************************************************************
 * 				mciGetErrorStringA		[WINMM.@]
 */
BOOL WINAPI mciGetErrorStringA(MCIERROR dwError, LPSTR lpstrBuffer, UINT uLength)
{
    BOOL		ret = FALSE;

    if (lpstrBuffer != NULL && uLength > 0 &&
	dwError >= MCIERR_BASE && dwError <= MCIERR_CUSTOM_DRIVER_BASE) {

2164
	if (LoadStringA(hWinMM32Instance, dwError, lpstrBuffer, uLength) > 0) {
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206
	    ret = TRUE;
	}
    }
    return ret;
}

/**************************************************************************
 *			mciDriverNotify				[WINMM.@]
 */
BOOL WINAPI mciDriverNotify(HWND hWndCallBack, MCIDEVICEID wDevID, UINT wStatus)
{
    TRACE("(%p, %04x, %04X)\n", hWndCallBack, wDevID, wStatus);

    return PostMessageW(hWndCallBack, MM_MCINOTIFY, wStatus, wDevID);
}

/**************************************************************************
 * 			mciGetDriverData			[WINMM.@]
 */
DWORD WINAPI mciGetDriverData(MCIDEVICEID uDeviceID)
{
    LPWINE_MCIDRIVER	wmd;

    TRACE("(%04x)\n", uDeviceID);

    wmd = MCI_GetDriver(uDeviceID);

    if (!wmd) {
	WARN("Bad uDeviceID\n");
	return 0L;
    }

    return wmd->dwPrivate;
}

/**************************************************************************
 * 			mciSetDriverData			[WINMM.@]
 */
BOOL WINAPI mciSetDriverData(MCIDEVICEID uDeviceID, DWORD data)
{
    LPWINE_MCIDRIVER	wmd;

2207
    TRACE("(%04x, %08x)\n", uDeviceID, data);
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232

    wmd = MCI_GetDriver(uDeviceID);

    if (!wmd) {
	WARN("Bad uDeviceID\n");
	return FALSE;
    }

    wmd->dwPrivate = data;
    return TRUE;
}

/**************************************************************************
 * 				mciSendCommandW			[WINMM.@]
 *
 */
DWORD WINAPI mciSendCommandW(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
    DWORD	dwRet;

    TRACE("(%08x, %s, %08lx, %08lx)\n",
	  wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);

    dwRet = MCI_SendCommand(wDevID, wMsg, dwParam1, dwParam2, TRUE);
    dwRet = MCI_CleanUp(dwRet, wMsg, dwParam2);
2233
    TRACE("=> %08x\n", dwRet);
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305
    return dwRet;
}

/**************************************************************************
 * 				mciSendCommandA			[WINMM.@]
 */
DWORD WINAPI mciSendCommandA(MCIDEVICEID wDevID, UINT wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
    DWORD ret;
    int mapped;

    TRACE("(%08x, %s, %08lx, %08lx)\n",
	  wDevID, MCI_MessageToString(wMsg), dwParam1, dwParam2);

    mapped = MCI_MapMsgAtoW(wMsg, dwParam1, &dwParam2);
    if (mapped == -1)
    {
        FIXME("message %04x mapping failed\n", wMsg);
        return MMSYSERR_NOMEM;
    }
    ret = mciSendCommandW(wDevID, wMsg, dwParam1, dwParam2);
    if (mapped)
        MCI_UnmapMsgAtoW(wMsg, dwParam1, dwParam2, ret);
    return ret;
}

/**************************************************************************
 * 				mciGetDeviceIDA    		[WINMM.@]
 */
UINT WINAPI mciGetDeviceIDA(LPCSTR lpstrName)
{
    LPWSTR w = MCI_strdupAtoW(lpstrName);
    UINT ret = MCIERR_OUT_OF_MEMORY;

    if (w)
    {
        ret = mciGetDeviceIDW(w);
        HeapFree(GetProcessHeap(), 0, w);
    }
    return ret;
}

/**************************************************************************
 * 				mciGetDeviceIDW		       	[WINMM.@]
 */
UINT WINAPI mciGetDeviceIDW(LPCWSTR lpwstrName)
{
    return MCI_GetDriverFromString(lpwstrName); 
}

/******************************************************************
 *		MyUserYield
 *
 * Internal wrapper to call USER.UserYield16 (in fact through a Wine only export from USER32).
 */
static void MyUserYield(void)
{
    HMODULE mod = GetModuleHandleA( "user32.dll" );
    if (mod)
    {
        FARPROC proc = GetProcAddress( mod, "UserYield16" );
        if (proc) proc();
    }
}

/**************************************************************************
 * 				MCI_DefYieldProc	       	[internal]
 */
UINT WINAPI MCI_DefYieldProc(MCIDEVICEID wDevID, DWORD data)
{
    INT16	ret;

2306
    TRACE("(0x%04x, 0x%08x)\n", wDevID, data);
2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328

    if ((HIWORD(data) != 0 && HWND_16(GetActiveWindow()) != HIWORD(data)) ||
	(GetAsyncKeyState(LOWORD(data)) & 1) == 0) {
	MyUserYield();
	ret = 0;
    } else {
	MSG		msg;

	msg.hwnd = HWND_32(HIWORD(data));
	while (!PeekMessageW(&msg, msg.hwnd, WM_KEYFIRST, WM_KEYLAST, PM_REMOVE));
	ret = -1;
    }
    return ret;
}

/**************************************************************************
 * 				mciSetYieldProc			[WINMM.@]
 */
BOOL WINAPI mciSetYieldProc(MCIDEVICEID uDeviceID, YIELDPROC fpYieldProc, DWORD dwYieldData)
{
    LPWINE_MCIDRIVER	wmd;

2329
    TRACE("(%u, %p, %08x)\n", uDeviceID, fpYieldProc, dwYieldData);
2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366

    if (!(wmd = MCI_GetDriver(uDeviceID))) {
	WARN("Bad uDeviceID\n");
	return FALSE;
    }

    wmd->lpfnYieldProc = fpYieldProc;
    wmd->dwYieldData   = dwYieldData;
    wmd->bIs32         = TRUE;

    return TRUE;
}

/**************************************************************************
 * 				mciGetDeviceIDFromElementIDA	[WINMM.@]
 */
UINT WINAPI mciGetDeviceIDFromElementIDA(DWORD dwElementID, LPCSTR lpstrType)
{
    LPWSTR w = MCI_strdupAtoW(lpstrType);
    UINT ret = 0;

    if (w)
    {
        ret = mciGetDeviceIDFromElementIDW(dwElementID, w);
        HeapFree(GetProcessHeap(), 0, w);
    }
    return ret;
}

/**************************************************************************
 * 				mciGetDeviceIDFromElementIDW	[WINMM.@]
 */
UINT WINAPI mciGetDeviceIDFromElementIDW(DWORD dwElementID, LPCWSTR lpstrType)
{
    /* FIXME: that's rather strange, there is no
     * mciGetDeviceIDFromElementID32A in winmm.spec
     */
2367
    FIXME("(%u, %s) stub\n", dwElementID, debugstr_w(lpstrType));
2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426
    return 0;
}

/**************************************************************************
 * 				mciGetYieldProc			[WINMM.@]
 */
YIELDPROC WINAPI mciGetYieldProc(MCIDEVICEID uDeviceID, DWORD* lpdwYieldData)
{
    LPWINE_MCIDRIVER	wmd;

    TRACE("(%u, %p)\n", uDeviceID, lpdwYieldData);

    if (!(wmd = MCI_GetDriver(uDeviceID))) {
	WARN("Bad uDeviceID\n");
	return NULL;
    }
    if (!wmd->lpfnYieldProc) {
	WARN("No proc set\n");
	return NULL;
    }
    if (!wmd->bIs32) {
	WARN("Proc is 32 bit\n");
	return NULL;
    }
    return wmd->lpfnYieldProc;
}

/**************************************************************************
 * 				mciGetCreatorTask		[WINMM.@]
 */
HTASK WINAPI mciGetCreatorTask(MCIDEVICEID uDeviceID)
{
    LPWINE_MCIDRIVER	wmd;
    HTASK ret = 0;

    if ((wmd = MCI_GetDriver(uDeviceID))) ret = (HTASK)wmd->CreatorThread;

    TRACE("(%u) => %p\n", uDeviceID, ret);
    return ret;
}

/**************************************************************************
 * 			mciDriverYield				[WINMM.@]
 */
UINT WINAPI mciDriverYield(MCIDEVICEID uDeviceID)
{
    LPWINE_MCIDRIVER	wmd;
    UINT		ret = 0;

    TRACE("(%04x)\n", uDeviceID);

    if (!(wmd = MCI_GetDriver(uDeviceID)) || !wmd->lpfnYieldProc || !wmd->bIs32) {
	MyUserYield();
    } else {
	ret = wmd->lpfnYieldProc(uDeviceID, wmd->dwYieldData);
    }

    return ret;
}