winmm.c 47.6 KB
Newer Older
1 2 3
/* -*- tab-width: 8; c-basic-offset: 4 -*- */

/*
4
 * WINMM functions
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * Copyright 1993      Martin Ayotte
 *           1998-2002 Eric Pouech
 *
 * 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
21
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 23 24 25 26 27 28 29 30
 */

/*
 * Eric POUECH :
 * 	98/9	added Win32 MCI support
 *  	99/4	added midiStream support
 *      99/9	added support for loadable low level drivers
 */

31 32 33 34 35 36 37 38 39 40 41
/* TODO
 *      + it seems that some programs check what's installed in
 *        registry against the value returned by drivers. Wine is
 *        currently broken regarding this point.
 *      + check thread-safeness for MMSYSTEM and WINMM entry points
 *      + unicode entry points are badly supported (would require
 *        moving 32 bit drivers as Unicode as they are supposed to be)
 *      + allow joystick and timer external calls as we do for wave,
 *        midi, mixer and aux
 */

42
#include <stdio.h>
43
#include <stdarg.h>
44 45
#include <string.h>

46 47
#define NONAMELESSUNION
#define NONAMELESSSTRUCT
48
#include "windef.h"
49
#include "winbase.h"
50
#include "mmsystem.h"
51
#include "winuser.h"
52
#include "winnls.h"
53 54 55 56 57 58 59 60 61 62 63
#include "winternl.h"
#include "winemm.h"

#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(winmm);

/* ========================================================================
 *                   G L O B A L   S E T T I N G S
 * ========================================================================*/

64 65 66 67 68 69 70 71 72 73
HINSTANCE hWinMM32Instance;
HANDLE psLastEvent;

static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &WINMM_cs,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": WINMM_cs") }
};
CRITICAL_SECTION WINMM_cs = { &critsect_debug, -1, 0, 0, 0, 0 };
74 75

/**************************************************************************
76
 * 			WINMM_CreateIData			[internal]
77
 */
78
static	BOOL	WINMM_CreateIData(HINSTANCE hInstDLL)
79
{
80 81
    hWinMM32Instance = hInstDLL;
    psLastEvent = CreateEventW(NULL, TRUE, FALSE, NULL);
82
    return TRUE;
83 84
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
/******************************************************************
 *             WINMM_ErrorToString
 */
const char* WINMM_ErrorToString(MMRESULT error)
{
#define ERR_TO_STR(dev) case dev: return #dev
    switch (error) {
    ERR_TO_STR(MMSYSERR_NOERROR);
    ERR_TO_STR(MMSYSERR_ERROR);
    ERR_TO_STR(MMSYSERR_BADDEVICEID);
    ERR_TO_STR(MMSYSERR_NOTENABLED);
    ERR_TO_STR(MMSYSERR_ALLOCATED);
    ERR_TO_STR(MMSYSERR_INVALHANDLE);
    ERR_TO_STR(MMSYSERR_NODRIVER);
    ERR_TO_STR(MMSYSERR_NOMEM);
    ERR_TO_STR(MMSYSERR_NOTSUPPORTED);
    ERR_TO_STR(MMSYSERR_BADERRNUM);
    ERR_TO_STR(MMSYSERR_INVALFLAG);
    ERR_TO_STR(MMSYSERR_INVALPARAM);
104 105 106 107 108 109 110 111 112
    ERR_TO_STR(MMSYSERR_HANDLEBUSY);
    ERR_TO_STR(MMSYSERR_INVALIDALIAS);
    ERR_TO_STR(MMSYSERR_BADDB);
    ERR_TO_STR(MMSYSERR_KEYNOTFOUND);
    ERR_TO_STR(MMSYSERR_READERROR);
    ERR_TO_STR(MMSYSERR_WRITEERROR);
    ERR_TO_STR(MMSYSERR_DELETEERROR);
    ERR_TO_STR(MMSYSERR_VALNOTFOUND);
    ERR_TO_STR(MMSYSERR_NODRIVERCB);
113 114 115 116
    ERR_TO_STR(WAVERR_BADFORMAT);
    ERR_TO_STR(WAVERR_STILLPLAYING);
    ERR_TO_STR(WAVERR_UNPREPARED);
    ERR_TO_STR(WAVERR_SYNC);
117 118 119 120
    ERR_TO_STR(MIDIERR_INVALIDSETUP);
    ERR_TO_STR(MIDIERR_NODEVICE);
    ERR_TO_STR(MIDIERR_STILLPLAYING);
    ERR_TO_STR(MIDIERR_UNPREPARED);
121 122
    }
#undef ERR_TO_STR
123
    return wine_dbg_sprintf("Unknown(0x%08x)", error);
124 125
}

126
/**************************************************************************
127
 *		DllMain (WINMM.init)
128 129 130 131
 *
 * WINMM DLL entry point
 *
 */
132
BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID fImpLoad)
133
{
134
    TRACE("%p 0x%x %p\n", hInstDLL, fdwReason, fImpLoad);
135 136 137

    switch (fdwReason) {
    case DLL_PROCESS_ATTACH:
138 139
        DisableThreadLibraryCalls(hInstDLL);

140
	if (!WINMM_CreateIData(hInstDLL))
141 142 143
	    return FALSE;
	break;
    case DLL_PROCESS_DETACH:
144 145 146
        if(fImpLoad)
            break;

147
        MCI_SendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0L);
148
        MMDRV_Exit();
149 150 151 152 153 154
        DRIVER_UnloadAll();
        WINMM_DeleteWaveform();
        TIME_MMTimeStop();
        CloseHandle(psLastEvent);
        DeleteCriticalSection(&WINMM_cs);
        break;
155 156 157 158
    }
    return TRUE;
}

159 160 161
/**************************************************************************
 * 			WINMM_CheckCallback			[internal]
 */
162
MMRESULT WINMM_CheckCallback(DWORD_PTR dwCallback, DWORD fdwOpen, BOOL mixer)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
    switch (fdwOpen & CALLBACK_TYPEMASK) {
    case CALLBACK_NULL:     /* dwCallback need not be NULL */
        break;
    case CALLBACK_WINDOW:
        if (dwCallback && !IsWindow((HWND)dwCallback))
            return MMSYSERR_INVALPARAM;
        break;

    case CALLBACK_FUNCTION:
        /* a NULL cb is acceptable since w2k, MMSYSERR_INVALPARAM earlier */
        if (mixer)
            return MMSYSERR_INVALFLAG; /* since w2k, MMSYSERR_NOTSUPPORTED earlier */
        break;
    case CALLBACK_THREAD:
    case CALLBACK_EVENT:
        if (mixer) /* FIXME: mixer supports THREAD+EVENT since w2k */
            return MMSYSERR_NOTSUPPORTED; /* w9X */
        break;
    default:
        WARN("Unknown callback type %d\n", HIWORD(fdwOpen));
    }
    return MMSYSERR_NOERROR;
}

188 189 190 191 192 193 194 195 196 197 198
/**************************************************************************
 * 				auxGetNumDevs		[WINMM.@]
 */
UINT WINAPI auxGetNumDevs(void)
{
    return MMDRV_GetNum(MMDRV_AUX);
}

/**************************************************************************
 * 				auxGetDevCapsW		[WINMM.@]
 */
199
UINT WINAPI auxGetDevCapsW(UINT_PTR uDeviceID, LPAUXCAPSW lpCaps, UINT uSize)
200
{
201
    LPWINE_MLD		wmld;
202

203
    TRACE("(%04lX, %p, %d) !\n", uDeviceID, lpCaps, uSize);
204

205 206 207
    if (lpCaps == NULL)	return MMSYSERR_INVALPARAM;

    if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
208
	return MMSYSERR_BADDEVICEID;
209
    return MMDRV_Message(wmld, AUXDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
210 211 212 213 214
}

/**************************************************************************
 * 				auxGetDevCapsA		[WINMM.@]
 */
215
UINT WINAPI auxGetDevCapsA(UINT_PTR uDeviceID, LPAUXCAPSA lpCaps, UINT uSize)
216
{
217 218
    AUXCAPSW	acW;
    UINT	ret;
219

220
    if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
221

222
    ret = auxGetDevCapsW(uDeviceID, &acW, sizeof(acW));
223

224 225 226 227 228 229 230 231 232 233 234 235
    if (ret == MMSYSERR_NOERROR) {
	AUXCAPSA acA;
	acA.wMid           = acW.wMid;
	acA.wPid           = acW.wPid;
	acA.vDriverVersion = acW.vDriverVersion;
	WideCharToMultiByte( CP_ACP, 0, acW.szPname, -1, acA.szPname,
                             sizeof(acA.szPname), NULL, NULL );
	acA.wTechnology    = acW.wTechnology;
	acA.dwSupport      = acW.dwSupport;
	memcpy(lpCaps, &acA, min(uSize, sizeof(acA)));
    }
    return ret;
236 237 238 239 240 241 242 243 244 245 246
}

/**************************************************************************
 * 				auxGetVolume		[WINMM.@]
 */
UINT WINAPI auxGetVolume(UINT uDeviceID, DWORD* lpdwVolume)
{
    LPWINE_MLD		wmld;

    TRACE("(%04X, %p) !\n", uDeviceID, lpdwVolume);

247
    if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
248
	return MMSYSERR_INVALHANDLE;
249
    return MMDRV_Message(wmld, AUXDM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
250 251 252 253 254 255 256 257 258
}

/**************************************************************************
 * 				auxSetVolume		[WINMM.@]
 */
UINT WINAPI auxSetVolume(UINT uDeviceID, DWORD dwVolume)
{
    LPWINE_MLD		wmld;

259
    TRACE("(%04X, %u) !\n", uDeviceID, dwVolume);
260

261
    if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
262
	return MMSYSERR_INVALHANDLE;
263
    return MMDRV_Message(wmld, AUXDM_SETVOLUME, dwVolume, 0L);
264 265 266 267 268
}

/**************************************************************************
 * 				auxOutMessage		[WINMM.@]
 */
269
UINT WINAPI auxOutMessage(UINT uDeviceID, UINT uMessage, DWORD_PTR dw1, DWORD_PTR dw2)
270 271 272
{
    LPWINE_MLD		wmld;

273
    if ((wmld = MMDRV_Get((HANDLE)(DWORD_PTR)uDeviceID, MMDRV_AUX, TRUE)) == NULL)
274 275
	return MMSYSERR_INVALHANDLE;

276
    return MMDRV_Message(wmld, uMessage, dw1, dw2);
277 278 279 280 281 282 283 284 285 286 287 288 289
}

/**************************************************************************
 * 				midiOutGetNumDevs	[WINMM.@]
 */
UINT WINAPI midiOutGetNumDevs(void)
{
    return MMDRV_GetNum(MMDRV_MIDIOUT);
}

/**************************************************************************
 * 				midiOutGetDevCapsW	[WINMM.@]
 */
290
UINT WINAPI midiOutGetDevCapsW(UINT_PTR uDeviceID, LPMIDIOUTCAPSW lpCaps,
291 292
			       UINT uSize)
{
293
    LPWINE_MLD	wmld;
294

295
    TRACE("(%lu, %p, %u);\n", uDeviceID, lpCaps, uSize);
296

297 298 299
    if (lpCaps == NULL)	return MMSYSERR_INVALPARAM;

    if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIOUT, TRUE)) == NULL)
300
	return MMSYSERR_BADDEVICEID;
301

302
    return MMDRV_Message(wmld, MODM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
303 304 305 306 307
}

/**************************************************************************
 * 				midiOutGetDevCapsA	[WINMM.@]
 */
308
UINT WINAPI midiOutGetDevCapsA(UINT_PTR uDeviceID, LPMIDIOUTCAPSA lpCaps,
309 310
			       UINT uSize)
{
311 312
    MIDIOUTCAPSW	mocW;
    UINT		ret;
313

314
    if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
315

316
    ret = midiOutGetDevCapsW(uDeviceID, &mocW, sizeof(mocW));
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    if (ret == MMSYSERR_NOERROR) {
	MIDIOUTCAPSA mocA;
	mocA.wMid		= mocW.wMid;
	mocA.wPid		= mocW.wPid;
	mocA.vDriverVersion	= mocW.vDriverVersion;
	WideCharToMultiByte( CP_ACP, 0, mocW.szPname, -1, mocA.szPname,
                             sizeof(mocA.szPname), NULL, NULL );
	mocA.wTechnology        = mocW.wTechnology;
	mocA.wVoices		= mocW.wVoices;
	mocA.wNotes		= mocW.wNotes;
	mocA.wChannelMask	= mocW.wChannelMask;
	mocA.dwSupport	        = mocW.dwSupport;
	memcpy(lpCaps, &mocA, min(uSize, sizeof(mocA)));
    }
    return ret;
333 334 335
}

/**************************************************************************
336 337
 * 				midiOutGetErrorTextA 	[WINMM.@]
 * 				midiInGetErrorTextA 	[WINMM.@]
338
 */
339
UINT WINAPI midiOutGetErrorTextA(UINT uError, LPSTR lpText, UINT uSize)
340
{
341
    UINT	ret;
342

343 344 345 346 347 348 349 350 351 352 353 354 355
    if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
    else if (uSize == 0) ret = MMSYSERR_NOERROR;
    else
    {
        LPWSTR	xstr = HeapAlloc(GetProcessHeap(), 0, uSize * sizeof(WCHAR));
        if (!xstr) ret = MMSYSERR_NOMEM;
        else
        {
            ret = midiOutGetErrorTextW(uError, xstr, uSize);
            if (ret == MMSYSERR_NOERROR)
                WideCharToMultiByte(CP_ACP, 0, xstr, -1, lpText, uSize, NULL, NULL);
            HeapFree(GetProcessHeap(), 0, xstr);
        }
356 357 358 359 360 361
    }
    return ret;
}

/**************************************************************************
 * 				midiOutGetErrorTextW 	[WINMM.@]
362
 * 				midiInGetErrorTextW 	[WINMM.@]
363 364 365
 */
UINT WINAPI midiOutGetErrorTextW(UINT uError, LPWSTR lpText, UINT uSize)
{
366
    UINT        ret = MMSYSERR_BADERRNUM;
367

368 369 370
    if (lpText == NULL) ret = MMSYSERR_INVALPARAM;
    else if (uSize == 0) ret = MMSYSERR_NOERROR;
    else if (
Austin English's avatar
Austin English committed
371
	       /* test has been removed because MMSYSERR_BASE is 0, and gcc did emit
372 373 374
		* a warning for the test was always true */
	       (/*uError >= MMSYSERR_BASE && */ uError <= MMSYSERR_LASTERROR) ||
	       (uError >= MIDIERR_BASE  && uError <= MIDIERR_LASTERROR)) {
375
	if (LoadStringW(hWinMM32Instance, uError, lpText, uSize) > 0) {
376 377 378
	    ret = MMSYSERR_NOERROR;
	}
    }
379 380 381 382 383 384
    return ret;
}

/**************************************************************************
 * 				MIDI_OutAlloc    		[internal]
 */
385 386
static	LPWINE_MIDI	MIDI_OutAlloc(HMIDIOUT* lphMidiOut, DWORD_PTR* lpdwCallback,
				      DWORD_PTR* lpdwInstance, LPDWORD lpdwFlags,
387
				      DWORD cIDs, MIDIOPENSTRMID* lpIDs)
388
{
389
    HANDLE	      	hMidiOut;
390 391 392 393 394 395
    LPWINE_MIDI		lpwm;
    UINT		size;

    size = sizeof(WINE_MIDI) + (cIDs ? (cIDs-1) : 0) * sizeof(MIDIOPENSTRMID);

    lpwm = (LPWINE_MIDI)MMDRV_Alloc(size, MMDRV_MIDIOUT, &hMidiOut, lpdwFlags,
396
				    lpdwCallback, lpdwInstance);
397
    *lphMidiOut = hMidiOut;
398 399

    if (lpwm) {
400
        lpwm->mod.hMidi = hMidiOut;
401 402 403 404 405 406 407 408 409 410
	lpwm->mod.dwCallback = *lpdwCallback;
	lpwm->mod.dwInstance = *lpdwInstance;
	lpwm->mod.dnDevNode = 0;
	lpwm->mod.cIds = cIDs;
	if (cIDs)
	    memcpy(&(lpwm->mod.rgIds), lpIDs, cIDs * sizeof(MIDIOPENSTRMID));
    }
    return lpwm;
}

411 412 413 414 415
/**************************************************************************
 * 				midiOutOpen    		[WINMM.@]
 */
UINT WINAPI midiOutOpen(LPHMIDIOUT lphMidiOut, UINT uDeviceID,
                       DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
416 417 418
{
    HMIDIOUT		hMidiOut;
    LPWINE_MIDI		lpwm;
419
    UINT		dwRet;
420

421
    TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
422 423 424 425
	  lphMidiOut, uDeviceID, dwCallback, dwInstance, dwFlags);

    if (lphMidiOut != NULL) *lphMidiOut = 0;

426 427 428
    dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
    if (dwRet != MMSYSERR_NOERROR)
	return dwRet;
429

430
    lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &dwFlags, 0, NULL);
431 432 433 434 435 436

    if (lpwm == NULL)
	return MMSYSERR_NOMEM;

    lpwm->mld.uDeviceID = uDeviceID;

437
    dwRet = MMDRV_Open((LPWINE_MLD)lpwm, MODM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
438 439 440 441 442 443 444

    if (dwRet != MMSYSERR_NOERROR) {
	MMDRV_Free(hMidiOut, (LPWINE_MLD)lpwm);
	hMidiOut = 0;
    }

    if (lphMidiOut) *lphMidiOut = hMidiOut;
445
    TRACE("=> %d hMidi=%p\n", dwRet, hMidiOut);
446 447 448 449 450 451 452 453 454 455 456 457

    return dwRet;
}

/**************************************************************************
 * 				midiOutClose		[WINMM.@]
 */
UINT WINAPI midiOutClose(HMIDIOUT hMidiOut)
{
    LPWINE_MLD		wmld;
    DWORD		dwRet;

458
    TRACE("(%p)\n", hMidiOut);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

    dwRet = MMDRV_Close(wmld, MODM_CLOSE);
    MMDRV_Free(hMidiOut, wmld);

    return dwRet;
}

/**************************************************************************
 * 				midiOutPrepareHeader	[WINMM.@]
 */
UINT WINAPI midiOutPrepareHeader(HMIDIOUT hMidiOut,
				 MIDIHDR* lpMidiOutHdr, UINT uSize)
{
    LPWINE_MLD		wmld;

477
    TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
478

479
    if (lpMidiOutHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
480 481
	return MMSYSERR_INVALPARAM;

482 483
    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;
484
    /* FIXME: detect MIDIStream handles and enforce 64KB buffer limit on those */
485

486
    return MMDRV_Message(wmld, MODM_PREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
487 488 489 490 491 492 493 494 495 496
}

/**************************************************************************
 * 				midiOutUnprepareHeader	[WINMM.@]
 */
UINT WINAPI midiOutUnprepareHeader(HMIDIOUT hMidiOut,
				   MIDIHDR* lpMidiOutHdr, UINT uSize)
{
    LPWINE_MLD		wmld;

497
    TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
498

499
    if (lpMidiOutHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
500 501
	return MMSYSERR_INVALPARAM;

502 503 504 505 506 507 508
    if (!(lpMidiOutHdr->dwFlags & MHDR_PREPARED)) {
	return MMSYSERR_NOERROR;
    }

    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

509
    return MMDRV_Message(wmld, MODM_UNPREPARE, (DWORD_PTR)lpMidiOutHdr, uSize);
510 511 512 513 514 515 516 517 518
}

/**************************************************************************
 * 				midiOutShortMsg		[WINMM.@]
 */
UINT WINAPI midiOutShortMsg(HMIDIOUT hMidiOut, DWORD dwMsg)
{
    LPWINE_MLD		wmld;

519
    TRACE("(%p, %08X)\n", hMidiOut, dwMsg);
520 521 522 523

    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

524
    return MMDRV_Message(wmld, MODM_DATA, dwMsg, 0L);
525 526 527 528 529 530 531 532 533 534
}

/**************************************************************************
 * 				midiOutLongMsg		[WINMM.@]
 */
UINT WINAPI midiOutLongMsg(HMIDIOUT hMidiOut,
			   MIDIHDR* lpMidiOutHdr, UINT uSize)
{
    LPWINE_MLD		wmld;

535
    TRACE("(%p, %p, %d)\n", hMidiOut, lpMidiOutHdr, uSize);
536 537 538 539

    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

540
    return MMDRV_Message(wmld, MODM_LONGDATA, (DWORD_PTR)lpMidiOutHdr, uSize);
541 542 543 544 545 546 547 548 549
}

/**************************************************************************
 * 				midiOutReset		[WINMM.@]
 */
UINT WINAPI midiOutReset(HMIDIOUT hMidiOut)
{
    LPWINE_MLD		wmld;

550
    TRACE("(%p)\n", hMidiOut);
551 552 553 554

    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

555
    return MMDRV_Message(wmld, MODM_RESET, 0L, 0L);
556 557 558 559 560
}

/**************************************************************************
 * 				midiOutGetVolume	[WINMM.@]
 */
561
UINT WINAPI midiOutGetVolume(HMIDIOUT hMidiOut, DWORD* lpdwVolume)
562 563 564
{
    LPWINE_MLD		wmld;

565
    TRACE("(%p, %p);\n", hMidiOut, lpdwVolume);
566

567
    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
568 569
	return MMSYSERR_INVALHANDLE;

570
    return MMDRV_Message(wmld, MODM_GETVOLUME, (DWORD_PTR)lpdwVolume, 0L);
571 572 573 574 575
}

/**************************************************************************
 * 				midiOutSetVolume	[WINMM.@]
 */
576
UINT WINAPI midiOutSetVolume(HMIDIOUT hMidiOut, DWORD dwVolume)
577 578 579
{
    LPWINE_MLD		wmld;

580
    TRACE("(%p, %d);\n", hMidiOut, dwVolume);
581

582
    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) == NULL)
583 584
	return MMSYSERR_INVALHANDLE;

585
    return MMDRV_Message(wmld, MODM_SETVOLUME, dwVolume, 0L);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
}

/**************************************************************************
 * 				midiOutCachePatches		[WINMM.@]
 */
UINT WINAPI midiOutCachePatches(HMIDIOUT hMidiOut, UINT uBank,
				WORD* lpwPatchArray, UINT uFlags)
{
    /* not really necessary to support this */
    FIXME("not supported yet\n");
    return MMSYSERR_NOTSUPPORTED;
}

/**************************************************************************
 * 				midiOutCacheDrumPatches	[WINMM.@]
 */
UINT WINAPI midiOutCacheDrumPatches(HMIDIOUT hMidiOut, UINT uPatch,
				    WORD* lpwKeyArray, UINT uFlags)
{
    FIXME("not supported yet\n");
    return MMSYSERR_NOTSUPPORTED;
}

/**************************************************************************
 * 				midiOutGetID		[WINMM.@]
 */
UINT WINAPI midiOutGetID(HMIDIOUT hMidiOut, UINT* lpuDeviceID)
{
    LPWINE_MLD		wmld;

616
    TRACE("(%p, %p)\n", hMidiOut, lpuDeviceID);
617 618 619 620 621 622 623 624 625 626 627 628

    if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;
    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

    *lpuDeviceID = wmld->uDeviceID;
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				midiOutMessage		[WINMM.@]
 */
629
UINT WINAPI midiOutMessage(HMIDIOUT hMidiOut, UINT uMessage,
630
                           DWORD_PTR dwParam1, DWORD_PTR dwParam2)
631 632 633
{
    LPWINE_MLD		wmld;

634
    TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiOut, uMessage, dwParam1, dwParam2);
635 636 637 638 639 640 641 642

    if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, FALSE)) == NULL) {
	/* HACK... */
	if (uMessage == 0x0001) {
	    *(LPDWORD)dwParam1 = 1;
	    return 0;
	}
	if ((wmld = MMDRV_Get(hMidiOut, MMDRV_MIDIOUT, TRUE)) != NULL) {
643
	    return MMDRV_PhysicalFeatures(wmld, uMessage, dwParam1, dwParam2);
644 645 646 647 648 649 650 651 652 653
	}
	return MMSYSERR_INVALHANDLE;
    }

    switch (uMessage) {
    case MODM_OPEN:
    case MODM_CLOSE:
	FIXME("can't handle OPEN or CLOSE message!\n");
	return MMSYSERR_NOTSUPPORTED;
    }
654
    return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
655 656 657 658 659 660 661 662 663 664 665 666 667
}

/**************************************************************************
 * 				midiInGetNumDevs	[WINMM.@]
 */
UINT WINAPI midiInGetNumDevs(void)
{
    return MMDRV_GetNum(MMDRV_MIDIIN);
}

/**************************************************************************
 * 				midiInGetDevCapsW	[WINMM.@]
 */
668
UINT WINAPI midiInGetDevCapsW(UINT_PTR uDeviceID, LPMIDIINCAPSW lpCaps, UINT uSize)
669
{
670
    LPWINE_MLD	wmld;
671

672
    TRACE("(%ld, %p, %d);\n", uDeviceID, lpCaps, uSize);
673

674 675 676
    if (lpCaps == NULL)	return MMSYSERR_INVALPARAM;

    if ((wmld = MMDRV_Get((HANDLE)uDeviceID, MMDRV_MIDIIN, TRUE)) == NULL)
677
	return MMSYSERR_BADDEVICEID;
678

679
   return MMDRV_Message(wmld, MIDM_GETDEVCAPS, (DWORD_PTR)lpCaps, uSize);
680 681 682 683 684
}

/**************************************************************************
 * 				midiInGetDevCapsA	[WINMM.@]
 */
685
UINT WINAPI midiInGetDevCapsA(UINT_PTR uDeviceID, LPMIDIINCAPSA lpCaps, UINT uSize)
686
{
687 688
    MIDIINCAPSW		micW;
    UINT		ret;
689

690
    if (lpCaps == NULL)        return MMSYSERR_INVALPARAM;
691

692
    ret = midiInGetDevCapsW(uDeviceID, &micW, sizeof(micW));
693

694 695 696 697 698 699 700 701 702 703 704
    if (ret == MMSYSERR_NOERROR) {
	MIDIINCAPSA micA;
	micA.wMid           = micW.wMid;
	micA.wPid           = micW.wPid;
	micA.vDriverVersion = micW.vDriverVersion;
        WideCharToMultiByte( CP_ACP, 0, micW.szPname, -1, micA.szPname,
                             sizeof(micA.szPname), NULL, NULL );
	micA.dwSupport      = micW.dwSupport;
	memcpy(lpCaps, &micA, min(uSize, sizeof(micA)));
    }
    return ret;
705 706
}

707 708 709 710 711
/**************************************************************************
 * 				midiInOpen		[WINMM.@]
 */
UINT WINAPI midiInOpen(HMIDIIN* lphMidiIn, UINT uDeviceID,
		       DWORD_PTR dwCallback, DWORD_PTR dwInstance, DWORD dwFlags)
712
{
713
    HANDLE		hMidiIn;
714
    LPWINE_MIDI		lpwm;
715
    DWORD		dwRet;
716

717
    TRACE("(%p, %d, %08lX, %08lX, %08X);\n",
718 719 720 721
	  lphMidiIn, uDeviceID, dwCallback, dwInstance, dwFlags);

    if (lphMidiIn != NULL) *lphMidiIn = 0;

722 723 724 725
    dwRet = WINMM_CheckCallback(dwCallback, dwFlags, FALSE);
    if (dwRet != MMSYSERR_NOERROR)
	return dwRet;

726
    lpwm = (LPWINE_MIDI)MMDRV_Alloc(sizeof(WINE_MIDI), MMDRV_MIDIIN, &hMidiIn,
727
				    &dwFlags, &dwCallback, &dwInstance);
728 729 730 731

    if (lpwm == NULL)
	return MMSYSERR_NOMEM;

732
    lpwm->mod.hMidi = hMidiIn;
733 734 735 736
    lpwm->mod.dwCallback = dwCallback;
    lpwm->mod.dwInstance = dwInstance;

    lpwm->mld.uDeviceID = uDeviceID;
737
    dwRet = MMDRV_Open(&lpwm->mld, MIDM_OPEN, (DWORD_PTR)&lpwm->mod, dwFlags);
738 739 740 741 742 743

    if (dwRet != MMSYSERR_NOERROR) {
	MMDRV_Free(hMidiIn, &lpwm->mld);
	hMidiIn = 0;
    }
    if (lphMidiIn != NULL) *lphMidiIn = hMidiIn;
744
    TRACE("=> %d hMidi=%p\n", dwRet, hMidiIn);
745 746 747 748 749 750 751 752 753 754 755 756

    return dwRet;
}

/**************************************************************************
 * 				midiInClose		[WINMM.@]
 */
UINT WINAPI midiInClose(HMIDIIN hMidiIn)
{
    LPWINE_MLD		wmld;
    DWORD		dwRet;

757
    TRACE("(%p)\n", hMidiIn);
758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

    dwRet = MMDRV_Close(wmld, MIDM_CLOSE);
    MMDRV_Free(hMidiIn, wmld);
    return dwRet;
}

/**************************************************************************
 * 				midiInPrepareHeader	[WINMM.@]
 */
UINT WINAPI midiInPrepareHeader(HMIDIIN hMidiIn,
				MIDIHDR* lpMidiInHdr, UINT uSize)
{
    LPWINE_MLD		wmld;

775
    TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
776

777
    if (lpMidiInHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
778 779
	return MMSYSERR_INVALPARAM;

780 781 782
    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

783
    return MMDRV_Message(wmld, MIDM_PREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
784 785 786 787 788 789 790 791 792 793
}

/**************************************************************************
 * 				midiInUnprepareHeader	[WINMM.@]
 */
UINT WINAPI midiInUnprepareHeader(HMIDIIN hMidiIn,
				  MIDIHDR* lpMidiInHdr, UINT uSize)
{
    LPWINE_MLD		wmld;

794
    TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
795

796
    if (lpMidiInHdr == NULL || uSize < offsetof(MIDIHDR,dwOffset))
797 798
	return MMSYSERR_INVALPARAM;

799 800 801 802 803 804 805
    if (!(lpMidiInHdr->dwFlags & MHDR_PREPARED)) {
	return MMSYSERR_NOERROR;
    }

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

806
    return MMDRV_Message(wmld, MIDM_UNPREPARE, (DWORD_PTR)lpMidiInHdr, uSize);
807 808 809 810 811 812 813 814 815 816
}

/**************************************************************************
 * 				midiInAddBuffer		[WINMM.@]
 */
UINT WINAPI midiInAddBuffer(HMIDIIN hMidiIn,
			    MIDIHDR* lpMidiInHdr, UINT uSize)
{
    LPWINE_MLD		wmld;

817
    TRACE("(%p, %p, %d)\n", hMidiIn, lpMidiInHdr, uSize);
818 819 820 821

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

822
    return MMDRV_Message(wmld, MIDM_ADDBUFFER, (DWORD_PTR)lpMidiInHdr, uSize);
823 824 825 826 827 828 829 830 831
}

/**************************************************************************
 * 				midiInStart			[WINMM.@]
 */
UINT WINAPI midiInStart(HMIDIIN hMidiIn)
{
    LPWINE_MLD		wmld;

832
    TRACE("(%p)\n", hMidiIn);
833 834 835 836

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

837
    return MMDRV_Message(wmld, MIDM_START, 0L, 0L);
838 839 840 841 842 843 844 845 846
}

/**************************************************************************
 * 				midiInStop			[WINMM.@]
 */
UINT WINAPI midiInStop(HMIDIIN hMidiIn)
{
    LPWINE_MLD		wmld;

847
    TRACE("(%p)\n", hMidiIn);
848 849 850 851

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

852
    return MMDRV_Message(wmld, MIDM_STOP, 0L, 0L);
853 854 855 856 857 858 859 860 861
}

/**************************************************************************
 * 				midiInReset			[WINMM.@]
 */
UINT WINAPI midiInReset(HMIDIIN hMidiIn)
{
    LPWINE_MLD		wmld;

862
    TRACE("(%p)\n", hMidiIn);
863 864 865 866

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

867
    return MMDRV_Message(wmld, MIDM_RESET, 0L, 0L);
868 869 870 871 872 873 874 875 876
}

/**************************************************************************
 * 				midiInGetID			[WINMM.@]
 */
UINT WINAPI midiInGetID(HMIDIIN hMidiIn, UINT* lpuDeviceID)
{
    LPWINE_MLD		wmld;

877
    TRACE("(%p, %p)\n", hMidiIn, lpuDeviceID);
878 879 880 881 882 883 884 885 886 887 888 889 890 891

    if (lpuDeviceID == NULL) return MMSYSERR_INVALPARAM;

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, TRUE)) == NULL)
	return MMSYSERR_INVALHANDLE;

    *lpuDeviceID = wmld->uDeviceID;

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				midiInMessage		[WINMM.@]
 */
892
UINT WINAPI midiInMessage(HMIDIIN hMidiIn, UINT uMessage,
893
                          DWORD_PTR dwParam1, DWORD_PTR dwParam2)
894 895 896
{
    LPWINE_MLD		wmld;

897
    TRACE("(%p, %04X, %08lX, %08lX)\n", hMidiIn, uMessage, dwParam1, dwParam2);
898 899 900 901 902 903 904 905 906 907

    if ((wmld = MMDRV_Get(hMidiIn, MMDRV_MIDIIN, FALSE)) == NULL)
	return MMSYSERR_INVALHANDLE;

    switch (uMessage) {
    case MIDM_OPEN:
    case MIDM_CLOSE:
	FIXME("can't handle OPEN or CLOSE message!\n");
	return MMSYSERR_NOTSUPPORTED;
    }
908
    return MMDRV_Message(wmld, uMessage, dwParam1, dwParam2);
909 910
}

911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928
/**************************************************************************
 * 				midiConnect			[WINMM.@]
 */
MMRESULT WINAPI midiConnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
{
    FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
    return MMSYSERR_ERROR;
}

/**************************************************************************
 * 				midiDisconnect			[WINMM.@]
 */
MMRESULT WINAPI midiDisconnect(HMIDI hMidi, HMIDIOUT hmo, LPVOID pReserved)
{
    FIXME("(%p, %p, %p): Stub\n", hMidi, hmo, pReserved);
    return MMSYSERR_ERROR;
}

929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944
typedef struct WINE_MIDIStream {
    HMIDIOUT			hDevice;
    HANDLE			hThread;
    DWORD			dwThreadID;
    DWORD			dwTempo;
    DWORD			dwTimeDiv;
    DWORD			dwPositionMS;
    DWORD			dwPulses;
    DWORD			dwStartTicks;
    WORD			wFlags;
    HANDLE			hEvent;
    LPMIDIHDR			lpMidiHdr;
} WINE_MIDIStream;

#define WINE_MSM_HEADER		(WM_USER+0)
#define WINE_MSM_STOP		(WM_USER+1)
945 946
#define WINE_MSM_PAUSE		(WM_USER+2)
#define WINE_MSM_RESUME		(WM_USER+3)
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995

/**************************************************************************
 * 				MMSYSTEM_GetMidiStream		[internal]
 */
static	BOOL	MMSYSTEM_GetMidiStream(HMIDISTRM hMidiStrm, WINE_MIDIStream** lpMidiStrm, WINE_MIDI** lplpwm)
{
    WINE_MIDI* lpwm = (LPWINE_MIDI)MMDRV_Get(hMidiStrm, MMDRV_MIDIOUT, FALSE);

    if (lplpwm)
	*lplpwm = lpwm;

    if (lpwm == NULL) {
	return FALSE;
    }

    *lpMidiStrm = (WINE_MIDIStream*)lpwm->mod.rgIds.dwStreamID;

    return *lpMidiStrm != NULL;
}

/**************************************************************************
 * 				MMSYSTEM_MidiStream_Convert	[internal]
 */
static	DWORD	MMSYSTEM_MidiStream_Convert(WINE_MIDIStream* lpMidiStrm, DWORD pulse)
{
    DWORD	ret = 0;

    if (lpMidiStrm->dwTimeDiv == 0) {
	FIXME("Shouldn't happen. lpMidiStrm->dwTimeDiv = 0\n");
    } else if (lpMidiStrm->dwTimeDiv > 0x8000) { /* SMPTE, unchecked FIXME? */
	int	nf = -(char)HIBYTE(lpMidiStrm->dwTimeDiv);	/* number of frames     */
	int	nsf = LOBYTE(lpMidiStrm->dwTimeDiv);		/* number of sub-frames */
	ret = (pulse * 1000) / (nf * nsf);
    } else {
	ret = (DWORD)((double)pulse * ((double)lpMidiStrm->dwTempo / 1000) /
		      (double)lpMidiStrm->dwTimeDiv);
    }

    return ret;
}

/**************************************************************************
 * 			MMSYSTEM_MidiStream_MessageHandler	[internal]
 */
static	BOOL	MMSYSTEM_MidiStream_MessageHandler(WINE_MIDIStream* lpMidiStrm, LPWINE_MIDI lpwm, LPMSG msg)
{
    LPMIDIHDR	lpMidiHdr;
    LPMIDIHDR*	lpmh;
    LPBYTE	lpData;
996 997 998 999 1000 1001 1002 1003 1004 1005 1006
    BOOL	paused = FALSE;

    for (;;) {
        switch (msg->message) {
        case WM_QUIT:
            return FALSE;
        case WINE_MSM_STOP:
            TRACE("STOP\n");
            /* this is not quite what MS doc says... */
            midiOutReset(lpMidiStrm->hDevice);
            /* empty list of already submitted buffers */
1007 1008 1009
            lpMidiHdr = lpMidiStrm->lpMidiHdr;
            lpMidiStrm->lpMidiHdr = NULL;
            while (lpMidiHdr) {
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                LPMIDIHDR lphdr = lpMidiHdr;
                lpMidiHdr = lpMidiHdr->lpNext;
                lphdr->dwFlags |= MHDR_DONE;
                lphdr->dwFlags &= ~MHDR_INQUEUE;

                DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
                               (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
                               lpwm->mod.dwInstance, (DWORD_PTR)lphdr, 0);
            }
            return TRUE;
        case WINE_MSM_RESUME:
            /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
            lpMidiStrm->dwStartTicks = GetTickCount() - lpMidiStrm->dwPositionMS;
            return TRUE;
        case WINE_MSM_PAUSE:
            /* FIXME: send out cc64 0 (turn off sustain pedal) on every channel */
            paused = TRUE;
            break;
1028 1029
	/* FIXME(EPP): "I don't understand the content of the first MIDIHDR sent
	 * by native mcimidi, it doesn't look like a correct one".
1030
	 * this trick allows us to throw it away... but I don't like it.
1031
	 * It looks like part of the file I'm trying to play and definitively looks
1032
	 * like raw midi content.
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	 * I'd really like to understand why native mcimidi sends it. Perhaps a bad
	 * synchronization issue where native mcimidi is still processing raw MIDI
	 * content before generating MIDIEVENTs ?
	 *
	 * 4c 04 89 3b 00 81 7c 99 3b 43 00 99 23 5e 04 89 L..;..|.;C..#^..
	 * 3b 00 00 89 23 00 7c 99 3b 45 00 99 28 62 04 89 ;...#.|.;E..(b..
	 * 3b 00 00 89 28 00 81 7c 99 3b 4e 00 99 23 5e 04 ;...(..|.;N..#^.
	 * 89 3b 00 00 89 23 00 7c 99 3b 45 00 99 23 78 04 .;...#.|.;E..#x.
	 * 89 3b 00 00 89 23 00 81 7c 99 3b 48 00 99 23 5e .;...#..|.;H..#^
	 * 04 89 3b 00 00 89 23 00 7c 99 3b 4e 00 99 28 62 ..;...#.|.;N..(b
	 * 04 89 3b 00 00 89 28 00 81 7c 99 39 4c 00 99 23 ..;...(..|.9L..#
	 * 5e 04 89 39 00 00 89 23 00 82 7c 99 3b 4c 00 99 ^..9...#..|.;L..
	 * 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 00 99 #^..;...#.|.;H..
	 * 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b 3f 04 (b..;...(..|.;?.
	 * 89 3b 00 1c 99 23 5e 04 89 23 00 5c 99 3b 45 00 .;...#^..#.\.;E.
	 * 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 3b 46 .#x..;...#..|.;F
	 * 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b 48 ..#^..;...#.|.;H
	 * 00 99 28 62 04 89 3b 00 00 89 28 00 81 7c 99 3b ..(b..;...(..|.;
	 * 46 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 3b F..#^..;...#.|.;
	 * 48 00 99 23 78 04 89 3b 00 00 89 23 00 81 7c 99 H..#x..;...#..|.
	 * 3b 4c 00 99 23 5e 04 89 3b 00 00 89 23 00 7c 99 ;L..#^..;...#.|.
	 */
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
        case WINE_MSM_HEADER:
            /* sets initial tick count for first MIDIHDR */
            if (!lpMidiStrm->dwStartTicks)
                lpMidiStrm->dwStartTicks = GetTickCount();
            lpMidiHdr = (LPMIDIHDR)msg->lParam;
            lpData = (LPBYTE)lpMidiHdr->lpData;
            TRACE("Adding %s lpMidiHdr=%p [lpData=0x%p dwBytesRecorded=%u/%u dwFlags=0x%08x size=%lu]\n",
                  (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular", lpMidiHdr,
                  lpData, lpMidiHdr->dwBytesRecorded, lpMidiHdr->dwBufferLength,
                  lpMidiHdr->dwFlags, msg->wParam);
1065
#if 0
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082
            /* dumps content of lpMidiHdr->lpData
             * FIXME: there should be a debug routine somewhere that already does this
             */
            for (dwToGo = 0; dwToGo < lpMidiHdr->dwBufferLength; dwToGo += 16) {
                DWORD       i;
                BYTE        ch;

                for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++)
                    printf("%02x ", lpData[dwToGo + i]);
                for (; i < 16; i++)
                    printf("   ");
                for (i = 0; i < min(16, lpMidiHdr->dwBufferLength - dwToGo); i++) {
                    ch = lpData[dwToGo + i];
                    printf("%c", (ch >= 0x20 && ch <= 0x7F) ? ch : '.');
                }
                printf("\n");
            }
1083
#endif
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
            if (((LPMIDIEVENT)lpData)->dwStreamID != 0 &&
                ((LPMIDIEVENT)lpData)->dwStreamID != 0xFFFFFFFF &&
                ((LPMIDIEVENT)lpData)->dwStreamID != (DWORD)lpMidiStrm) {
                FIXME("Dropping bad %s lpMidiHdr (streamID=%08x)\n",
                      (lpMidiHdr->dwFlags & MHDR_ISSTRM) ? "stream" : "regular",
                      ((LPMIDIEVENT)lpData)->dwStreamID);
                lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
                lpMidiHdr->dwFlags |= MHDR_DONE;

                DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
                               (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
                               lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
                break;
            }

            lpMidiHdr->lpNext = 0;
            for (lpmh = &lpMidiStrm->lpMidiHdr; *lpmh; lpmh = &(*lpmh)->lpNext);
            *lpmh = lpMidiHdr;
            break;
        default:
            FIXME("Unknown message %d\n", msg->message);
            break;
        }
        if (!paused)
            return TRUE;
        GetMessageA(msg, 0, 0, 0);
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    }
}

/**************************************************************************
 * 				MMSYSTEM_MidiStream_Player	[internal]
 */
static	DWORD	CALLBACK	MMSYSTEM_MidiStream_Player(LPVOID pmt)
{
    WINE_MIDIStream* 	lpMidiStrm = pmt;
    WINE_MIDI*		lpwm;
    MSG			msg;
    DWORD		dwToGo;
    DWORD		dwCurrTC;
    LPMIDIHDR		lpMidiHdr;
1124
    DWORD		dwOffset;
1125 1126 1127 1128 1129 1130 1131 1132

    TRACE("(%p)!\n", lpMidiStrm);

    if (!lpMidiStrm ||
	(lpwm = (LPWINE_MIDI)MMDRV_Get(lpMidiStrm->hDevice, MMDRV_MIDIOUT, FALSE)) == NULL)
	goto the_end;

    /* force thread's queue creation */
1133
    PeekMessageA(&msg, 0, 0, 0, PM_NOREMOVE);
1134 1135 1136 1137 1138 1139

    lpMidiStrm->dwStartTicks = 0;
    lpMidiStrm->dwPulses = 0;

    lpMidiStrm->lpMidiHdr = 0;

1140 1141 1142
    /* midiStreamOpen is waiting for ack */
    SetEvent(lpMidiStrm->hEvent);

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153
start_header:
    lpMidiHdr = lpMidiStrm->lpMidiHdr;
    if (!lpMidiHdr) {
	/* for first message, block until one arrives, then process all that are available */
	GetMessageA(&msg, 0, 0, 0);
	do {
	    if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
		goto the_end;
	} while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE));
	goto start_header;
    }
1154

1155 1156 1157
    dwOffset = 0;
    while (dwOffset + offsetof(MIDIEVENT,dwParms) <= lpMidiHdr->dwBytesRecorded) {
	LPMIDIEVENT me = (LPMIDIEVENT)(lpMidiHdr->lpData+dwOffset);
1158 1159 1160 1161 1162 1163 1164 1165

	/* do we have to wait ? */
	if (me->dwDeltaTime) {
	    lpMidiStrm->dwPositionMS += MMSYSTEM_MidiStream_Convert(lpMidiStrm, me->dwDeltaTime);
	    lpMidiStrm->dwPulses += me->dwDeltaTime;

	    dwToGo = lpMidiStrm->dwStartTicks + lpMidiStrm->dwPositionMS;

1166
	    TRACE("%d/%d/%d\n", dwToGo, GetTickCount(), me->dwDeltaTime);
1167 1168 1169 1170 1171 1172
	    while ((dwCurrTC = GetTickCount()) < dwToGo) {
		if (MsgWaitForMultipleObjects(0, NULL, FALSE, dwToGo - dwCurrTC, QS_ALLINPUT) == WAIT_OBJECT_0) {
		    /* got a message, handle it */
		    while (PeekMessageA(&msg, 0, 0, 0, PM_REMOVE)) {
			if (!MMSYSTEM_MidiStream_MessageHandler(lpMidiStrm, lpwm, &msg))
			    goto the_end;
1173 1174 1175 1176
			/* is lpMidiHdr still current? */
			if (lpMidiHdr != lpMidiStrm->lpMidiHdr) {
			    goto start_header;
			}
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
		    }
		} else {
		    /* timeout, so me->dwDeltaTime is elapsed, can break the while loop */
		    break;
		}
	    }
	}
	switch (MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK)) {
	case MEVT_COMMENT:
	    FIXME("NIY: MEVT_COMMENT\n");
	    /* do nothing, skip bytes */
	    break;
	case MEVT_LONGMSG:
1190
	    midiOutLongMsg(lpMidiStrm->hDevice, lpMidiStrm->lpMidiHdr, MEVT_EVENTPARM(me->dwEvent));
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	    break;
	case MEVT_NOP:
	    break;
	case MEVT_SHORTMSG:
	    midiOutShortMsg(lpMidiStrm->hDevice, MEVT_EVENTPARM(me->dwEvent));
	    break;
	case MEVT_TEMPO:
	    lpMidiStrm->dwTempo = MEVT_EVENTPARM(me->dwEvent);
	    break;
	case MEVT_VERSION:
	    break;
	default:
	    FIXME("Unknown MEVT (0x%02x)\n", MEVT_EVENTTYPE(me->dwEvent & ~MEVT_F_CALLBACK));
	    break;
	}
	if (me->dwEvent & MEVT_F_CALLBACK) {
1207 1208
	    /* native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
	    lpMidiHdr->dwOffset = dwOffset;
1209 1210 1211 1212
	    DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
			   (HDRVR)lpMidiStrm->hDevice, MM_MOM_POSITIONCB,
			   lpwm->mod.dwInstance, (LPARAM)lpMidiHdr, 0L);
	}
1213
	dwOffset += offsetof(MIDIEVENT,dwParms);
1214
	if (me->dwEvent & MEVT_F_LONG)
1215
	    dwOffset += (MEVT_EVENTPARM(me->dwEvent) + 3) & ~3;
1216
    }
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
    /* done with this header */
    lpMidiStrm->lpMidiHdr = lpMidiHdr->lpNext;
    lpMidiHdr->dwFlags |= MHDR_DONE;
    lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;

    DriverCallback(lpwm->mod.dwCallback, lpMidiStrm->wFlags,
		   (HDRVR)lpMidiStrm->hDevice, MM_MOM_DONE,
		   lpwm->mod.dwInstance, (DWORD_PTR)lpMidiHdr, 0);
    goto start_header;

1227 1228
the_end:
    TRACE("End of thread\n");
1229
    return 0;
1230 1231 1232 1233 1234 1235 1236 1237
}

/**************************************************************************
 * 				midiStreamClose			[WINMM.@]
 */
MMRESULT WINAPI midiStreamClose(HMIDISTRM hMidiStrm)
{
    WINE_MIDIStream*	lpMidiStrm;
1238
    MMRESULT		ret = 0;
1239

1240
    TRACE("(%p)!\n", hMidiStrm);
1241 1242 1243 1244 1245

    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
	return MMSYSERR_INVALHANDLE;

    midiStreamStop(hMidiStrm);
1246
    PostThreadMessageA(lpMidiStrm->dwThreadID, WM_QUIT, 0, 0);
1247
    CloseHandle(lpMidiStrm->hEvent);
1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
    if (lpMidiStrm->hThread) {
        if (GetCurrentThreadId() != lpMidiStrm->dwThreadID)
            WaitForSingleObject(lpMidiStrm->hThread, INFINITE);
        else {
            FIXME("leak from call within function callback\n");
            ret = MMSYSERR_HANDLEBUSY; /* yet don't signal it to app */
        }
        CloseHandle(lpMidiStrm->hThread);
    }
    if(!ret)
        HeapFree(GetProcessHeap(), 0, lpMidiStrm);
1259 1260 1261 1262 1263

    return midiOutClose((HMIDIOUT)hMidiStrm);
}

/**************************************************************************
1264
 * 				midiStreamOpen			[WINMM.@]
1265
 */
1266 1267 1268
MMRESULT WINAPI midiStreamOpen(HMIDISTRM* lphMidiStrm, LPUINT lpuDeviceID,
			       DWORD cMidi, DWORD_PTR dwCallback,
			       DWORD_PTR dwInstance, DWORD fdwOpen)
1269 1270 1271 1272 1273 1274 1275
{
    WINE_MIDIStream*	lpMidiStrm;
    MMRESULT		ret;
    MIDIOPENSTRMID	mosm;
    LPWINE_MIDI		lpwm;
    HMIDIOUT		hMidiOut;

1276
    TRACE("(%p, %p, %d, 0x%08lx, 0x%08lx, 0x%08x)!\n",
1277 1278 1279 1280 1281
	  lphMidiStrm, lpuDeviceID, cMidi, dwCallback, dwInstance, fdwOpen);

    if (cMidi != 1 || lphMidiStrm == NULL || lpuDeviceID == NULL)
	return MMSYSERR_INVALPARAM;

1282 1283 1284
    ret = WINMM_CheckCallback(dwCallback, fdwOpen, FALSE);
    if (ret != MMSYSERR_NOERROR)
	return ret;
1285

1286 1287 1288 1289 1290
    lpMidiStrm = HeapAlloc(GetProcessHeap(), 0, sizeof(WINE_MIDIStream));
    if (!lpMidiStrm)
	return MMSYSERR_NOMEM;

    lpMidiStrm->dwTempo = 500000;
Austin English's avatar
Austin English committed
1291
    lpMidiStrm->dwTimeDiv = 480; 	/* 480 is 120 quarter notes per minute *//* FIXME ??*/
1292 1293 1294 1295 1296
    lpMidiStrm->dwPositionMS = 0;

    mosm.dwStreamID = (DWORD)lpMidiStrm;
    /* FIXME: the correct value is not allocated yet for MAPPER */
    mosm.wDeviceID  = *lpuDeviceID;
1297
    lpwm = MIDI_OutAlloc(&hMidiOut, &dwCallback, &dwInstance, &fdwOpen, 1, &mosm);
1298 1299 1300 1301
    if (!lpwm) {
	HeapFree(GetProcessHeap(), 0, lpMidiStrm);
	return MMSYSERR_NOMEM;
    }
1302
    lpMidiStrm->hDevice = hMidiOut;
1303
    *lphMidiStrm = (HMIDISTRM)hMidiOut;
1304

1305
    lpwm->mld.uDeviceID = *lpuDeviceID;
1306

1307
    ret = MMDRV_Open(&lpwm->mld, MODM_OPEN, (DWORD_PTR)&lpwm->mod, fdwOpen);
1308 1309 1310 1311 1312 1313
    if (ret != MMSYSERR_NOERROR) {
	MMDRV_Free(hMidiOut, &lpwm->mld);
	HeapFree(GetProcessHeap(), 0, lpMidiStrm);
	return ret;
    }

1314
    lpMidiStrm->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1315 1316 1317 1318 1319 1320 1321 1322 1323
    lpMidiStrm->wFlags = HIWORD(fdwOpen);

    lpMidiStrm->hThread = CreateThread(NULL, 0, MMSYSTEM_MidiStream_Player,
				       lpMidiStrm, 0, &(lpMidiStrm->dwThreadID));

    if (!lpMidiStrm->hThread) {
	midiStreamClose((HMIDISTRM)hMidiOut);
	return MMSYSERR_NOMEM;
    }
1324
    SetThreadPriority(lpMidiStrm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1325 1326

    /* wait for thread to have started, and for its queue to be created */
1327
    WaitForSingleObject(lpMidiStrm->hEvent, INFINITE);
1328

1329
    PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1330

1331
    TRACE("=> (%u/%d) hMidi=%p ret=%d lpMidiStrm=%p\n",
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
	  *lpuDeviceID, lpwm->mld.uDeviceID, *lphMidiStrm, ret, lpMidiStrm);
    return ret;
}

/**************************************************************************
 * 				midiStreamOut			[WINMM.@]
 */
MMRESULT WINAPI midiStreamOut(HMIDISTRM hMidiStrm, LPMIDIHDR lpMidiHdr,
			      UINT cbMidiHdr)
{
    WINE_MIDIStream*	lpMidiStrm;
    DWORD		ret = MMSYSERR_NOERROR;

1345
    TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMidiHdr, cbMidiHdr);
1346

1347
    if (cbMidiHdr < offsetof(MIDIHDR,dwOffset) || !lpMidiHdr || !lpMidiHdr->lpData
1348 1349
	|| lpMidiHdr->dwBufferLength < lpMidiHdr->dwBytesRecorded
	|| lpMidiHdr->dwBytesRecorded % 4 /* player expects DWORD padding */)
1350
	return MMSYSERR_INVALPARAM;
1351
    /* FIXME: Native additionally checks if the MIDIEVENTs in lpData
1352
     * exactly fit dwBytesRecorded. */
1353 1354 1355 1356 1357 1358 1359

    if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
	return MIDIERR_UNPREPARED;

    if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
	return MIDIERR_STILLPLAYING;

1360 1361 1362
    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
	ret = MMSYSERR_INVALHANDLE;
    } else {
1363 1364
	lpMidiHdr->dwFlags |= MHDR_ISSTRM | MHDR_INQUEUE;
	lpMidiHdr->dwFlags &= ~MHDR_DONE;
1365 1366
	if (!PostThreadMessageA(lpMidiStrm->dwThreadID,
                                WINE_MSM_HEADER, cbMidiHdr,
1367
                                (LPARAM)lpMidiHdr)) {
1368
	    ERR("bad PostThreadMessageA\n");
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
	    ret = MMSYSERR_ERROR;
	}
    }
    return ret;
}

/**************************************************************************
 * 				midiStreamPause			[WINMM.@]
 */
MMRESULT WINAPI midiStreamPause(HMIDISTRM hMidiStrm)
{
    WINE_MIDIStream*	lpMidiStrm;
    DWORD		ret = MMSYSERR_NOERROR;

1383
    TRACE("(%p)!\n", hMidiStrm);
1384

1385 1386
    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
        return MMSYSERR_INVALHANDLE;
1387
    PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_PAUSE, 0, 0);
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
    return ret;
}

/**************************************************************************
 * 				midiStreamPosition		[WINMM.@]
 */
MMRESULT WINAPI midiStreamPosition(HMIDISTRM hMidiStrm, LPMMTIME lpMMT, UINT cbmmt)
{
    WINE_MIDIStream*	lpMidiStrm;
    DWORD		ret = MMSYSERR_NOERROR;

1399
    TRACE("(%p, %p, %u)!\n", hMidiStrm, lpMMT, cbmmt);
1400 1401 1402 1403 1404 1405 1406

    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
	ret = MMSYSERR_INVALHANDLE;
    } else if (lpMMT == NULL || cbmmt != sizeof(MMTIME)) {
	ret = MMSYSERR_INVALPARAM;
    } else {
	switch (lpMMT->wType) {
1407 1408 1409 1410 1411 1412
	default:
	    FIXME("Unsupported time type %x\n", lpMMT->wType);
	case TIME_BYTES:
	case TIME_SAMPLES:
	    lpMMT->wType = TIME_MS;
	    /* fall through to alternative format */
1413 1414
	case TIME_MS:
	    lpMMT->u.ms = lpMidiStrm->dwPositionMS;
1415
	    TRACE("=> %d ms\n", lpMMT->u.ms);
1416 1417 1418
	    break;
	case TIME_TICKS:
	    lpMMT->u.ticks = lpMidiStrm->dwPulses;
1419
	    TRACE("=> %d ticks\n", lpMMT->u.ticks);
1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
	    break;
	}
    }
    return ret;
}

/**************************************************************************
 * 				midiStreamProperty		[WINMM.@]
 */
MMRESULT WINAPI midiStreamProperty(HMIDISTRM hMidiStrm, LPBYTE lpPropData, DWORD dwProperty)
{
    WINE_MIDIStream*	lpMidiStrm;
    MMRESULT		ret = MMSYSERR_NOERROR;

1434
    TRACE("(%p, %p, %x)\n", hMidiStrm, lpPropData, dwProperty);
1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL)) {
	ret = MMSYSERR_INVALHANDLE;
    } else if ((dwProperty & (MIDIPROP_GET|MIDIPROP_SET)) == 0) {
	ret = MMSYSERR_INVALPARAM;
    } else if (dwProperty & MIDIPROP_TEMPO) {
	MIDIPROPTEMPO*	mpt = (MIDIPROPTEMPO*)lpPropData;

	if (sizeof(MIDIPROPTEMPO) != mpt->cbStruct) {
	    ret = MMSYSERR_INVALPARAM;
	} else if (dwProperty & MIDIPROP_SET) {
	    lpMidiStrm->dwTempo = mpt->dwTempo;
1447
	    TRACE("Setting tempo to %d\n", mpt->dwTempo);
1448 1449
	} else if (dwProperty & MIDIPROP_GET) {
	    mpt->dwTempo = lpMidiStrm->dwTempo;
1450
	    TRACE("Getting tempo <= %d\n", mpt->dwTempo);
1451 1452 1453 1454 1455 1456 1457 1458
	}
    } else if (dwProperty & MIDIPROP_TIMEDIV) {
	MIDIPROPTIMEDIV*	mptd = (MIDIPROPTIMEDIV*)lpPropData;

	if (sizeof(MIDIPROPTIMEDIV) != mptd->cbStruct) {
	    ret = MMSYSERR_INVALPARAM;
	} else if (dwProperty & MIDIPROP_SET) {
	    lpMidiStrm->dwTimeDiv = mptd->dwTimeDiv;
1459
	    TRACE("Setting time div to %d\n", mptd->dwTimeDiv);
1460 1461
	} else if (dwProperty & MIDIPROP_GET) {
	    mptd->dwTimeDiv = lpMidiStrm->dwTimeDiv;
1462
	    TRACE("Getting time div <= %d\n", mptd->dwTimeDiv);
1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
	}
    } else {
	ret = MMSYSERR_INVALPARAM;
    }

    return ret;
}

/**************************************************************************
 * 				midiStreamRestart		[WINMM.@]
 */
MMRESULT WINAPI midiStreamRestart(HMIDISTRM hMidiStrm)
{
    WINE_MIDIStream*	lpMidiStrm;
    MMRESULT		ret = MMSYSERR_NOERROR;

1479
    TRACE("(%p)!\n", hMidiStrm);
1480

1481 1482
    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
        return MMSYSERR_INVALHANDLE;
1483
    PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_RESUME, 0, 0);
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494
    return ret;
}

/**************************************************************************
 * 				midiStreamStop			[WINMM.@]
 */
MMRESULT WINAPI midiStreamStop(HMIDISTRM hMidiStrm)
{
    WINE_MIDIStream*	lpMidiStrm;
    MMRESULT		ret = MMSYSERR_NOERROR;

1495
    TRACE("(%p)!\n", hMidiStrm);
1496

1497 1498
    if (!MMSYSTEM_GetMidiStream(hMidiStrm, &lpMidiStrm, NULL))
        return MMSYSERR_INVALHANDLE;
1499
    PostThreadMessageA(lpMidiStrm->dwThreadID, WINE_MSM_STOP, 0, 0);
1500 1501 1502
    return ret;
}

1503 1504 1505 1506 1507 1508 1509
struct mm_starter
{
    LPTASKCALLBACK      cb;
    DWORD               client;
    HANDLE              event;
};

1510
static DWORD WINAPI mmTaskRun(void* pmt)
1511 1512 1513 1514 1515 1516 1517 1518 1519 1520
{
    struct mm_starter mms;

    memcpy(&mms, pmt, sizeof(struct mm_starter));
    HeapFree(GetProcessHeap(), 0, pmt);
    mms.cb(mms.client);
    if (mms.event) SetEvent(mms.event);
    return 0;
}

1521 1522 1523
/******************************************************************
 *		mmTaskCreate (WINMM.@)
 */
1524
UINT     WINAPI mmTaskCreate(LPTASKCALLBACK cb, HANDLE* ph, DWORD_PTR client)
1525 1526 1527 1528 1529 1530
{
    HANDLE               hThread;
    HANDLE               hEvent = 0;
    struct mm_starter   *mms;

    mms = HeapAlloc(GetProcessHeap(), 0, sizeof(struct mm_starter));
1531
    if (mms == NULL) return TASKERR_OUTOFMEMORY;
1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543

    mms->cb = cb;
    mms->client = client;
    if (ph) hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
    mms->event = hEvent;

    hThread = CreateThread(0, 0, mmTaskRun, mms, 0, NULL);
    if (!hThread) {
        HeapFree(GetProcessHeap(), 0, mms);
        if (hEvent) CloseHandle(hEvent);
        return TASKERR_OUTOFMEMORY;
    }
1544
    SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
1545 1546 1547 1548
    if (ph) *ph = hEvent;
    CloseHandle(hThread);
    return 0;
}
1549 1550 1551 1552

/******************************************************************
 *		mmTaskBlock (WINMM.@)
 */
1553
VOID     WINAPI mmTaskBlock(DWORD tid)
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566
{
    MSG		msg;

    do
    {
	GetMessageA(&msg, 0, 0, 0);
	if (msg.hwnd) DispatchMessageA(&msg);
    } while (msg.message != WM_USER);
}

/******************************************************************
 *		mmTaskSignal (WINMM.@)
 */
1567
BOOL     WINAPI mmTaskSignal(DWORD tid)
1568
{
1569
    return PostThreadMessageW(tid, WM_USER, 0, 0);
1570 1571 1572 1573 1574
}

/******************************************************************
 *		mmTaskYield (WINMM.@)
 */
1575
VOID     WINAPI mmTaskYield(VOID) {}
1576 1577 1578 1579

/******************************************************************
 *		mmGetCurrentTask (WINMM.@)
 */
1580
DWORD    WINAPI mmGetCurrentTask(VOID)
1581
{
1582
    return GetCurrentThreadId();
1583
}