mcimidi.c 51.8 KB
Newer Older
1 2 3
/*
 * Sample MIDI Wine Driver for Linux
 *
4 5
 * Copyright 1994 Martin Ayotte
 * Copyright 1999 Eric Pouech
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21
 */

22 23
/* TODO:
 *      + implement it correctly
24
 *      + finish asynchronous commands
25 26 27
 *      + better implement non waiting command (without the MCI_WAIT flag).
 */

28
#include <stdlib.h>
29
#include <stdarg.h>
30
#include <stdio.h>
31
#include <string.h>
32

33
#include "windef.h"
34
#include "winbase.h"
35
#include "wingdi.h"
36
#include "winuser.h"
37
#include "wownt32.h"
38
#include "mmddk.h"
39
#include "wine/debug.h"
40
#include "wine/unicode.h"
41

42
WINE_DEFAULT_DEBUG_CHANNEL(mcimidi);
43

44 45
#define MIDI_NOTEOFF             0x80
#define MIDI_NOTEON              0x90
Patrik Stridvall's avatar
Patrik Stridvall committed
46

47 48 49 50 51 52 53 54 55 56
typedef struct {
    DWORD		dwFirst;		/* offset in file of track */
    DWORD		dwLast;			/* number of bytes in file of track */
    DWORD		dwIndex;		/* current index in file (dwFirst <= dwIndex < dwLast) */
    DWORD		dwLength;		/* number of pulses in this track */
    DWORD		dwEventPulse;		/* current pulse # (event) pointed by dwIndex */
    DWORD		dwEventData;		/* current data    (event) pointed by dwIndex */
    WORD		wEventLength;		/* current length  (event) pointed by dwIndex */
    WORD		wStatus : 1,		/* 1 : playing, 0 : done */
	                wTrackNr : 7,
57
	                wLastCommand : 8;	/* last MIDI command on track */
58 59
} MCI_MIDITRACK;

60
typedef struct tagWINE_MCIMIDI {
61
    UINT		wDevID;			/* the MCI one */
62
    HMIDI		hMidi;
63
    int			nUseCount;          	/* Incremented for each shared open          */
64
    HANDLE 		hCallback;         	/* Callback handle for pending notification  */
65
    HMMIO		hFile;	            	/* mmio file handle open as Element          */
66 67 68
    LPWSTR		lpstrElementName;       /* Name of file (if any)                     */
    LPWSTR		lpstrCopyright;
    LPWSTR		lpstrName;
69
    WORD		wPort;			/* the WINMM device unit */
70
    WORD		dwStatus;		/* one from MCI_MODE_xxxx */
71
    DWORD		dwMciTimeFormat;	/* One of the supported MCI_FORMAT_xxxx */
72 73 74 75 76 77
    WORD		wFormat;		/* Format of MIDI hFile (0, 1 or 2) */
    WORD		nTracks;		/* Number of tracks in hFile */
    WORD		nDivision;		/* Number of division in hFile PPQN or SMPTE */
    WORD		wStartedPlaying;
    DWORD		dwTempo;		/* Tempo (# of 1/4 note per second */
    MCI_MIDITRACK*     	tracks;			/* Content of each track */
78
    DWORD		dwPulse;
79 80 81 82
    DWORD		dwPositionMS;
    DWORD		dwStartTicks;
} WINE_MCIMIDI;

83 84 85 86 87 88 89 90 91 92 93 94
/* ===================================================================
 * ===================================================================
 * FIXME: should be using the new mmThreadXXXX functions from WINMM
 * instead of those
 * it would require to add a wine internal flag to mmThreadCreate
 * in order to pass a 32 bit function instead of a 16 bit
 * ===================================================================
 * =================================================================== */

struct SCA {
    UINT 	wDevID;
    UINT 	wMsg;
95 96
    DWORD_PTR   dwParam1;
    DWORD_PTR   dwParam2;
97 98 99 100 101 102 103 104 105
};

/* EPP DWORD WINAPI mciSendCommandA(UINT wDevID, UINT wMsg, DWORD dwParam1, DWORD dwParam2); */

/**************************************************************************
 * 				MCI_SCAStarter			[internal]
 */
static DWORD CALLBACK	MCI_SCAStarter(LPVOID arg)
{
106 107
    struct SCA* sca = arg;
    DWORD       ret;
108

109
    TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
110 111
	  sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
    ret = mciSendCommandA(sca->wDevID, sca->wMsg, sca->dwParam1 | MCI_WAIT, sca->dwParam2);
112
    TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
113 114 115 116 117 118 119 120
	  sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
    HeapFree(GetProcessHeap(), 0, sca);
    return ret;
}

/**************************************************************************
 * 				MCI_SendCommandAsync		[internal]
 */
121 122
static	DWORD MCI_SendCommandAsync(UINT wDevID, UINT wMsg, DWORD_PTR dwParam1,
				   DWORD_PTR dwParam2, UINT size)
123
{
Robert Reif's avatar
Robert Reif committed
124
    HANDLE handle;
125
    struct SCA*	sca = HeapAlloc(GetProcessHeap(), 0, sizeof(struct SCA) + size);
126 127 128 129 130 131 132

    if (sca == 0)
	return MCIERR_OUT_OF_MEMORY;

    sca->wDevID   = wDevID;
    sca->wMsg     = wMsg;
    sca->dwParam1 = dwParam1;
133

134
    if (size && dwParam2) {
135
	sca->dwParam2 = (DWORD_PTR)sca + sizeof(struct SCA);
136 137
	/* copy structure passed by program in dwParam2 to be sure
	 * we can still use it whatever the program does
138 139 140 141 142 143
	 */
	memcpy((LPVOID)sca->dwParam2, (LPVOID)dwParam2, size);
    } else {
	sca->dwParam2 = dwParam2;
    }

Robert Reif's avatar
Robert Reif committed
144
    if ((handle = CreateThread(NULL, 0, MCI_SCAStarter, sca, 0, NULL)) == 0) {
145
	WARN("Couldn't allocate thread for async command handling, sending synchronously\n");
146
	return MCI_SCAStarter(sca);
147
    }
148
    SetThreadPriority(handle, THREAD_PRIORITY_TIME_CRITICAL);
Robert Reif's avatar
Robert Reif committed
149
    CloseHandle(handle);
150 151 152
    return 0;
}

153
/*======================================================================*
154
 *                  	    MCI MIDI implementation			*
155 156
 *======================================================================*/

157
static DWORD MIDI_mciResume(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
158

159
/**************************************************************************
160
 * 				MIDI_drvOpen			[internal]
161
 */
162
static	DWORD	MIDI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
163
{
164 165 166 167 168
    WINE_MCIMIDI*	wmm;

    if (!modp) return 0xFFFFFFFF;

    wmm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIMIDI));
169

170 171 172 173
    if (!wmm)
	return 0;

    wmm->wDevID = modp->wDeviceID;
174
    mciSetDriverData(wmm->wDevID, (DWORD_PTR)wmm);
175
    modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
176 177
    modp->wType = MCI_DEVTYPE_SEQUENCER;
    return modp->wDeviceID;
178 179 180
}

/**************************************************************************
181
 * 				MCIMIDI_drvClose		[internal]
182 183 184
 */
static	DWORD	MIDI_drvClose(DWORD dwDevID)
{
185
    WINE_MCIMIDI*  wmm = (WINE_MCIMIDI*)mciGetDriverData(dwDevID);
186 187

    if (wmm) {
188
	HeapFree(GetProcessHeap(), 0, wmm);
189
	mciSetDriverData(dwDevID, 0);
190 191
	return 1;
    }
192
    return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
193 194
}

195
/**************************************************************************
196
 * 				MIDI_mciGetOpenDev		[internal]
197
 */
198
static WINE_MCIMIDI*  MIDI_mciGetOpenDev(MCIDEVICEID wDevID, UINT wMsg)
199
{
200
    WINE_MCIMIDI*	wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
201

202
    if (wmm == NULL || ((wmm->nUseCount == 0) ^ (wMsg == MCI_OPEN_DRIVER))) {
203
	WARN("Invalid wDevID=%u\n", wDevID);
204 205
	return 0;
    }
206
    return wmm;
207 208
}

209 210 211 212 213 214 215 216 217 218 219 220
/**************************************************************************
 *				MIDI_mciNotify			[internal]
 *
 * Notifications in MCI work like a 1-element queue.
 * Each new notification request supersedes the previous one.
 * This affects Play and Record; other commands are immediate.
 */
static void MIDI_mciNotify(DWORD_PTR hWndCallBack, WINE_MCIMIDI* wmm, UINT wStatus)
{
    /* We simply save one parameter by not passing the wDevID local
     * to the command.  They are the same (via mciGetDriverData).
     */
221
    MCIDEVICEID wDevID = wmm->wDevID;
222 223 224 225 226
    HANDLE old = InterlockedExchangePointer(&wmm->hCallback, NULL);
    if (old) mciDriverNotify(old, wDevID, MCI_NOTIFY_SUPERSEDED);
    mciDriverNotify(HWND_32(LOWORD(hWndCallBack)), wDevID, wStatus);
}

227
/**************************************************************************
228
 * 				MIDI_mciReadByte		[internal]
229 230 231 232
 */
static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
{
    DWORD	ret = 0;
233 234

    if (lpbyt == NULL ||
235
	mmioRead(wmm->hFile, (HPSTR)lpbyt, sizeof(BYTE)) != (long)sizeof(BYTE)) {
236
	WARN("Error reading wmm=%p\n", wmm);
237
	ret = MCIERR_INVALID_FILE;
238
    }
239

240 241 242 243
    return ret;
}

/**************************************************************************
244
 * 				MIDI_mciReadWord		[internal]
245 246 247 248
 */
static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
{
    BYTE	hibyte, lobyte;
249
    DWORD	ret = MCIERR_INVALID_FILE;
250 251 252

    if (lpw != NULL &&
	MIDI_mciReadByte(wmm, &hibyte) == 0 &&
253 254 255 256 257 258 259 260
	MIDI_mciReadByte(wmm, &lobyte) == 0) {
	*lpw = ((WORD)hibyte << 8) + lobyte;
	ret = 0;
    }
    return ret;
}

/**************************************************************************
261
 * 				MIDI_mciReadLong		[internal]
262 263 264 265
 */
static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
{
    WORD	hiword, loword;
266
    DWORD	ret = MCIERR_INVALID_FILE;
267

268 269 270 271 272 273 274 275 276 277
    if (lpdw != NULL &&
	MIDI_mciReadWord(wmm, &hiword) == 0 &&
	MIDI_mciReadWord(wmm, &loword) == 0) {
	*lpdw = MAKELONG(loword, hiword);
	ret = 0;
    }
    return ret;
}

/**************************************************************************
278
 *  				MIDI_mciReadVaryLen		[internal]
279 280 281 282 283 284
 */
static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
{
    BYTE	byte;
    DWORD	value = 0;
    WORD	ret = 0;
285

286
    if (lpdw == NULL) {
287
	ret = MCIERR_INVALID_FILE;
288 289 290 291 292 293
    } else {
	do {
	    if (MIDI_mciReadByte(wmm, &byte) != 0) {
		return 0;
	    }
	    value = (value << 7) + (byte & 0x7F);
294
	    ret++;
295 296 297
	} while (byte & 0x80);
	*lpdw = value;
	/*
298
	  TRACE("val=%08X\n", value);
299 300 301 302 303 304
	*/
    }
    return ret;
}

/**************************************************************************
305
 * 				MIDI_mciReadNextEvent		[internal]
306
 */
307
static DWORD	MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
308 309 310 311 312 313
{
    BYTE	b1, b2 = 0, b3;
    WORD	hw = 0;
    DWORD	evtPulse;
    DWORD	evtLength;
    DWORD	tmp;
314

315
    if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
316
	WARN("Can't seek at %08X\n", mmt->dwIndex);
317
	return MCIERR_INVALID_FILE;
318 319 320 321 322 323 324 325 326 327 328
    }
    evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1;	/* > 0 */
    MIDI_mciReadByte(wmm, &b1);
    switch (b1) {
    case 0xF0:
    case 0xF7:
	evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
	evtLength += tmp;
	break;
    case 0xFF:
	MIDI_mciReadByte(wmm, &b2);	evtLength++;
329

330 331 332
	evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
	if (evtLength >= 0x10000u) {
	    /* this limitation shouldn't be a problem */
333
	    WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
	    hw = 0xFFFF;
	} else {
	    hw = LOWORD(evtLength);
	}
	evtLength += tmp;
	break;
    default:
	if (b1 & 0x80) { /* use running status ? */
	    mmt->wLastCommand = b1;
	    MIDI_mciReadByte(wmm, &b2);	evtLength++;
	} else {
	    b2 = b1;
	    b1 = mmt->wLastCommand;
	}
	switch ((b1 >> 4) & 0x07) {
	case 0:	case 1:	case 2: case 3: case 6:
	    MIDI_mciReadByte(wmm, &b3);	evtLength++;
	    hw = b3;
	    break;
	case 4:	case 5:
	    break;
	case 7:
356
	    WARN("Strange indeed b1=0x%02x\n", b1);
357 358 359 360 361
	}
	break;
    }
    if (mmt->dwIndex + evtLength > mmt->dwLast)
	return MCIERR_INTERNAL;
362

363 364 365
    mmt->dwEventPulse += evtPulse;
    mmt->dwEventData   = (hw << 16) + (b2 << 8) + b1;
    mmt->wEventLength  = evtLength;
366

367
    /*
368
      TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
369
      mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
370
      mmt->dwEventData, mmt->wEventLength);
371 372 373 374 375
    */
    return 0;
}

/**************************************************************************
376
 * 				MIDI_mciReadMTrk		[internal]
377 378 379 380 381
 */
static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
{
    DWORD		toberead;
    FOURCC		fourcc;
382 383

    if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
384
	(long)sizeof(FOURCC)) {
385
	return MCIERR_INVALID_FILE;
386
    }
387

388
    if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
389
	WARN("Can't synchronize on 'MTrk' !\n");
390
	return MCIERR_INVALID_FILE;
391
    }
392

393
    if (MIDI_mciReadLong(wmm, &toberead) != 0) {
394
	return MCIERR_INVALID_FILE;
395
    }
396
    mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
397
    mmt->dwLast = mmt->dwFirst + toberead;
398

399 400 401
    /* compute # of pulses in this track */
    mmt->dwIndex = mmt->dwFirst;
    mmt->dwEventPulse = 0;
402

403 404 405 406
    while (MIDI_mciReadNextEvent(wmm, mmt) == 0 && LOWORD(mmt->dwEventData) != 0x2FFF) {
	char	buf[1024];
	WORD	len;

407
	mmt->dwIndex += mmt->wEventLength;
408 409 410 411 412 413 414

	switch (LOWORD(mmt->dwEventData)) {
	case 0x02FF:
	case 0x03FF:
	    /* position after meta data header */
	    mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
	    len = mmt->wEventLength - HIWORD(mmt->dwEventData);
415

416
	    if (len >= sizeof(buf)) {
417
		WARN("Buffer for text is too small (%u are needed)\n", len);
418 419
		len = sizeof(buf) - 1;
	    }
420
            if (mmioRead(wmm->hFile, buf, len) == len) {
421 422 423 424
		buf[len] = 0;	/* end string in case */
		switch (HIBYTE(LOWORD(mmt->dwEventData))) {
		case 0x02:
		    if (wmm->lpstrCopyright) {
425
			WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm->lpstrCopyright), buf);
426
		    } else {
427 428
                        len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
                        wmm->lpstrCopyright = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
429
                        MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrCopyright, len );
430 431 432
		    }
		    break;
		case 0x03:
433
		    if (wmm->lpstrName) {
434
			WARN("Two names (%s|%s)\n", debugstr_w(wmm->lpstrName), buf);
435
		    } else {
436 437
                        len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
                        wmm->lpstrName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
438
                        MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrName, len );
439 440 441 442 443 444
		    }
		    break;
		}
	    }
	    break;
	}
445 446
    }
    mmt->dwLength = mmt->dwEventPulse;
447

448
    TRACE("Track %u has %u bytes and %u pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
449

450 451 452 453
    /* reset track data */
    mmt->wStatus = 1;	/* ok, playing */
    mmt->dwIndex = mmt->dwFirst;
    mmt->dwEventPulse = 0;
454

455
    if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
456
	WARN("Ouch, out of sync seek=%u track=%u\n",
457
	     mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
458
	/* position at end of this track, to be ready to read next track */
459
	mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
460
    }
461

462 463 464 465
    return 0;
}

/**************************************************************************
466
 * 				MIDI_mciReadMThd		[internal]
467 468 469 470 471 472
 */
static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
{
    DWORD	toberead;
    FOURCC	fourcc;
    WORD	nt;
473

474
    TRACE("(%p, %08X);\n", wmm, dwOffset);
475

476
    if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
477
	WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset);
478
	return MCIERR_INVALID_FILE;
479
    }
480
    if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
481
		   (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
482
	return MCIERR_INVALID_FILE;
483

484
    if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
485
	WARN("Can't synchronize on 'MThd' !\n");
486
	return MCIERR_INVALID_FILE;
487
    }
488

489
    if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
490
	return MCIERR_INVALID_FILE;
491

492 493 494
    if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
	MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
	MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
495
	return MCIERR_INVALID_FILE;
496
    }
497

498
    TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
499
	  toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
500

501 502 503 504 505
    /* MS doc says that the MIDI MCI time format must be put by default to the format
     * stored in the MIDI file...
     */
    if (wmm->nDivision > 0x8000) {
	/* eric.pouech@lemel.fr 98/11
506
	 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
507
	 * In about 40 MB of MIDI files I have, none was SMPTE based...
508
	 * I'm just wondering if this is widely used :-). So, if someone has one of
509
	 * these files, I'd like to know about it.
510
	 */
511
	FIXME("Handling SMPTE time in MIDI files has not been tested\n"
512
	      "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
513

514 515 516 517 518 519
	switch (HIBYTE(wmm->nDivision)) {
	case 0xE8:	wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;	break;	/* -24 */
	case 0xE7:	wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;	break;	/* -25 */
	case 0xE3:	wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP;	break;	/* -29 */ /* is the MCI constant correct ? */
	case 0xE2:	wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;	break;	/* -30 */
	default:
520
	    WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
521
	    return MCIERR_INVALID_FILE;
522 523 524 525 526 527 528 529
	}
	switch (LOBYTE(wmm->nDivision)) {
	case 4:	/* MIDI Time Code */
	case 8:
	case 10:
	case 80: /* SMPTE bit resolution */
	case 100:
	default:
530
	    WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
531
	    return MCIERR_INVALID_FILE;
532 533
	}
    } else if (wmm->nDivision == 0) {
534
	WARN("Number of division is 0, can't support that !!\n");
535
	return MCIERR_INVALID_FILE;
536 537 538
    } else {
	wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
    }
539

540 541 542
    switch (wmm->wFormat) {
    case 0:
	if (wmm->nTracks != 1) {
543
	    WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
544 545 546 547 548 549 550
	    wmm->nTracks = 1;
	}
	break;
    case 1:
    case 2:
	break;
    default:
551
	WARN("Handling MIDI files which format = %d is not (yet) supported\n"
552
	     "Please report with MIDI file !\n", wmm->wFormat);
553
	return MCIERR_INVALID_FILE;
554
    }
555

556 557
    if (wmm->nTracks & 0x8000) {
	/* this shouldn't be a problem... */
558
	WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
559 560
	wmm->nTracks = 0x7FFF;
    }
561

562
    if ((wmm->tracks = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
563 564
	return MCIERR_OUT_OF_MEMORY;
    }
565 566

    toberead -= 3 * sizeof(WORD);
567
    if (toberead > 0) {
568
	TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead);
569
	mmioSeek(wmm->hFile, toberead, SEEK_CUR);
570
    }
571

572 573 574
    for (nt = 0; nt < wmm->nTracks; nt++) {
	wmm->tracks[nt].wTrackNr = nt;
	if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
575
	    WARN("Can't read 'MTrk' header\n");
576
	    return MCIERR_INVALID_FILE;
577 578
	}
    }
579

580
    wmm->dwTempo = 500000;
581

582 583 584 585 586 587 588 589 590
    return 0;
}

/**************************************************************************
 * 			MIDI_ConvertPulseToMS			[internal]
 */
static	DWORD	MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
{
    DWORD	ret = 0;
591

592 593 594 595
    /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
     * may change during file playing
     */
    if (wmm->nDivision == 0) {
596
	FIXME("Shouldn't happen. wmm->nDivision = 0\n");
597
    } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
598 599 600 601
	int	nf = -(char)HIBYTE(wmm->nDivision);	/* number of frames     */
	int	nsf = LOBYTE(wmm->nDivision);		/* number of sub-frames */
	ret = (pulse * 1000) / (nf * nsf);
    } else {
602
	ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
603 604
		      (double)wmm->nDivision);
    }
605

606
    /*
607
      TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
608 609
      pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
    */
610

611 612 613 614 615 616 617 618 619 620 621 622 623
    return ret;
}

#define TIME_MS_IN_ONE_HOUR	(60*60*1000)
#define TIME_MS_IN_ONE_MINUTE	(60*1000)
#define TIME_MS_IN_ONE_SECOND	(1000)

/**************************************************************************
 * 			MIDI_ConvertTimeFormatToMS		[internal]
 */
static	DWORD	MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
{
    DWORD	ret = 0;
624

625
    switch (wmm->dwMciTimeFormat) {
626 627
    case MCI_FORMAT_MILLISECONDS:
	ret = val;
628 629
	break;
    case MCI_FORMAT_SMPTE_24:
630
	ret =
631 632 633
	    (HIBYTE(HIWORD(val)) * 125) / 3 +             LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
	    HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
	break;
634 635
    case MCI_FORMAT_SMPTE_25:
	ret =
636 637 638
	    HIBYTE(HIWORD(val)) * 40 + 		  	  LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
	    HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
	break;
639 640
    case MCI_FORMAT_SMPTE_30:
	ret =
641 642 643 644
	    (HIBYTE(HIWORD(val)) * 100) / 3 + 		  LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
	    HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
	break;
    default:
645
	WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
646 647
    }
    /*
648
      TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
649 650
    */
    return ret;
651
}
652 653 654 655 656 657 658 659

/**************************************************************************
 * 			MIDI_ConvertMSToTimeFormat		[internal]
 */
static	DWORD	MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
{
    DWORD	ret = 0, val = _val;
    DWORD	h, m, s, f;
660

661
    switch (wmm->dwMciTimeFormat) {
662 663
    case MCI_FORMAT_MILLISECONDS:
	ret = val;
664 665
	break;
    case MCI_FORMAT_SMPTE_24:
666 667 668 669
    case MCI_FORMAT_SMPTE_25:
    case MCI_FORMAT_SMPTE_30:
	h = val / TIME_MS_IN_ONE_HOUR;
	m = (val -= h * TIME_MS_IN_ONE_HOUR)   / TIME_MS_IN_ONE_MINUTE;
670 671 672 673 674 675
	s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
	switch (wmm->dwMciTimeFormat) {
	case MCI_FORMAT_SMPTE_24:
	    /* one frame is 1000/24 val long, 1000/24 == 125/3 */
	    f = (val * 3) / 125; 	val -= (f * 125) / 3;
	    break;
676
	case MCI_FORMAT_SMPTE_25:
677 678 679
	    /* one frame is 1000/25 ms long, 1000/25 == 40 */
	    f = val / 40; 		val -= f * 40;
	    break;
680
	case MCI_FORMAT_SMPTE_30:
681 682 683 684
	    /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
	    f = (val * 3) / 100; 	val -= (f * 100) / 3;
	    break;
	default:
685
	    FIXME("There must be some bad bad programmer\n");
686 687 688 689 690 691 692
	    f = 0;
	}
	/* val contains the number of ms which cannot make a complete frame */
	/* FIXME: is this correct ? programs seem to be happy with that */
	ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
	break;
    default:
693
	WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
694 695
    }
    /*
696
      TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
697 698
    */
    return ret;
699
}
700 701 702 703 704 705 706 707

/**************************************************************************
 * 			MIDI_GetMThdLengthMS			[internal]
 */
static	DWORD	MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
{
    WORD	nt;
    DWORD	ret = 0;
708

709 710 711 712 713 714 715
    for (nt = 0; nt < wmm->nTracks; nt++) {
	if (wmm->wFormat == 2) {
	    ret += wmm->tracks[nt].dwLength;
	} else if (wmm->tracks[nt].dwLength > ret) {
	    ret = wmm->tracks[nt].dwLength;
	}
    }
716
    /* FIXME: this is wrong if there is a tempo change inside the file */
717 718 719 720
    return MIDI_ConvertPulseToMS(wmm, ret);
}

/**************************************************************************
721
 * 				MIDI_mciOpen			[internal]
722
 */
723
static DWORD MIDI_mciOpen(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_OPEN_PARMSW lpParms)
724
{
725
    DWORD		dwRet = 0;
726

727
    TRACE("(%d, %08X, %p)\n", wmm->wDevID, dwFlags, lpParms);
728

729
    if (lpParms == NULL) 	return MCIERR_NULL_PARAMETER_BLOCK;
730 731 732
    if (dwFlags & MCI_OPEN_SHAREABLE)
	return MCIERR_HARDWARE;

733 734 735 736 737 738 739
    if (wmm->nUseCount > 0) {
	/* The driver is already opened on this channel
	 * MIDI sequencer cannot be shared
	 */
	return MCIERR_DEVICE_OPEN;
    }
    wmm->nUseCount++;
740

741
    wmm->hFile = 0;
742
    wmm->hMidi = 0;
743
    wmm->wPort = MIDI_MAPPER;
744
    wmm->lpstrElementName = NULL;
745

746
    TRACE("wDevID=%d (lpParams->wDeviceID=%d)\n", wmm->wDevID, lpParms->wDeviceID);
747
    /*	lpParms->wDeviceID = wDevID;*/
748

749
    if (dwFlags & MCI_OPEN_ELEMENT) {
750 751
	TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms->lpstrElementName));
	if (lpParms->lpstrElementName && strlenW(lpParms->lpstrElementName) > 0) {
752
	    wmm->hFile = mmioOpenW((LPWSTR)lpParms->lpstrElementName, NULL,
753
				   MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
754
	    if (wmm->hFile == 0) {
755
		WARN("Can't find file %s!\n", debugstr_w(lpParms->lpstrElementName));
756
		wmm->nUseCount--;
757 758
		return MCIERR_FILE_NOT_FOUND;
	    }
759 760 761
            wmm->lpstrElementName = HeapAlloc(GetProcessHeap(), 0, 
                                              (strlenW(lpParms->lpstrElementName) + 1) * sizeof(WCHAR));
            strcpyW(wmm->lpstrElementName, lpParms->lpstrElementName);
762 763
	}
    }
764
    TRACE("hFile=%p\n", wmm->hFile);
765

766 767 768
    wmm->lpstrCopyright = NULL;
    wmm->lpstrName = NULL;

769
    wmm->dwStatus = MCI_MODE_NOT_READY;	/* while loading file contents */
770
    /* spec says it should be the default format from the MIDI file... */
771
    wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
772

773 774
    if (wmm->hFile != 0) {
	MMCKINFO	ckMainRIFF;
775
	MMCKINFO	mmckInfo;
776
	DWORD		dwOffset = 0;
777

778
	if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
779 780
	    dwRet = MCIERR_INVALID_FILE;
	} else {
781
	    TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
782
		  (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
783

784 785
	    if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
		mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
786
		mmioSeek(wmm->hFile, ckMainRIFF.dwDataOffset + ((ckMainRIFF.cksize + 1) & ~1), SEEK_SET);
787
		if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
788
		    TRACE("... is a 'RMID' file\n");
789 790 791
		    dwOffset = mmckInfo.dwDataOffset;
		} else {
		    dwRet = MCIERR_INVALID_FILE;
792
		}
793 794
	    }
	    if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
795
		WARN("Can't read 'MThd' header\n");
796
		dwRet = MCIERR_INVALID_FILE;
797
	    }
798 799
	}
    } else {
800
	TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
801 802 803 804
	wmm->nTracks = 0;
	wmm->wFormat = 0;
	wmm->nDivision = 1;
    }
805 806 807
    if (dwRet != 0) {
	wmm->nUseCount--;
	if (wmm->hFile != 0)
808
	    mmioClose(wmm->hFile, 0);
809 810
	wmm->hFile = 0;
    } else {
811 812
	wmm->dwPositionMS = 0;
	wmm->dwStatus = MCI_MODE_STOP;
813 814
	if (dwFlags & MCI_NOTIFY)
	    MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
815 816
    }
    return dwRet;
817 818 819
}

/**************************************************************************
820
 * 				MIDI_mciStop			[internal]
821
 */
822
static DWORD MIDI_mciStop(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
823
{
824
    DWORD		dwRet = 0;
825

826
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
827

828 829
    if (wmm->dwStatus != MCI_MODE_STOP) {
	HANDLE old = InterlockedExchangePointer(&wmm->hCallback, NULL);
830
	if (old) mciDriverNotify(old, wmm->wDevID, MCI_NOTIFY_ABORTED);
831 832
    }

833
    if (wmm->dwStatus != MCI_MODE_STOP) {
834 835 836 837
	int	oldstat = wmm->dwStatus;

	wmm->dwStatus = MCI_MODE_NOT_READY;
	if (oldstat == MCI_MODE_PAUSE)
838
	    dwRet = midiOutReset((HMIDIOUT)wmm->hMidi);
839 840 841

	while (wmm->dwStatus != MCI_MODE_STOP)
	    Sleep(10);
842
    }
843

Austin English's avatar
Austin English committed
844
    /* sanity reset */
845 846
    wmm->dwStatus = MCI_MODE_STOP;

847 848 849
    if ((dwFlags & MCI_NOTIFY) && lpParms && MMSYSERR_NOERROR==dwRet)
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
    return dwRet;
850 851 852 853 854
}

/**************************************************************************
 * 				MIDI_mciClose			[internal]
 */
855
static DWORD MIDI_mciClose(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
856
{
857

858
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
859

860
    if (wmm->dwStatus != MCI_MODE_STOP) {
861
	/* mciStop handles MCI_NOTIFY_ABORTED */
862
	MIDI_mciStop(wmm, MCI_WAIT, lpParms);
863
    }
864

865 866 867
    wmm->nUseCount--;
    if (wmm->nUseCount == 0) {
	if (wmm->hFile != 0) {
868
	    mmioClose(wmm->hFile, 0);
869
	    wmm->hFile = 0;
870
	    TRACE("hFile closed !\n");
871
	}
872
	HeapFree(GetProcessHeap(), 0, wmm->tracks);
873 874 875
	HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
	HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
	HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
876
    } else {
877
	TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
878 879
	return MCIERR_INTERNAL;
    }
880

881 882
    if ((dwFlags & MCI_NOTIFY) && lpParms) {
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
883 884 885 886
    }
    return 0;
}

887 888 889
/**************************************************************************
 * 				MIDI_mciFindNextEvent		[internal]
 */
890
static MCI_MIDITRACK*	MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
891 892 893
{
    WORD		cnt, nt;
    MCI_MIDITRACK*	mmt;
894

895 896 897 898
    *hiPulse = 0xFFFFFFFFul;
    cnt = 0xFFFFu;
    for (nt = 0; nt < wmm->nTracks; nt++) {
	mmt = &wmm->tracks[nt];
899

900 901 902 903 904 905 906 907 908 909 910 911 912 913
	if (mmt->wStatus == 0)
	    continue;
	if (mmt->dwEventPulse < *hiPulse) {
	    *hiPulse = mmt->dwEventPulse;
	    cnt = nt;
	}
    }
    return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
	: &wmm->tracks[cnt];
}

/**************************************************************************
 * 				MIDI_mciPlay			[internal]
 */
914
static DWORD MIDI_mciPlay(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
915
{
916 917
    DWORD		dwStartMS, dwEndMS;
    DWORD		dwRet = 0;
918 919 920
    WORD		doPlay, nt;
    MCI_MIDITRACK*	mmt;
    DWORD		hiPulse;
921
    HANDLE		oldcb;
922

923
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
924

925
    if (wmm->hFile == 0) {
926
	WARN("Can't play: no file %s!\n", debugstr_w(wmm->lpstrElementName));
927 928
	return MCIERR_FILE_NOT_FOUND;
    }
929

930
    if (wmm->dwStatus != MCI_MODE_STOP) {
931 932
	if (wmm->dwStatus == MCI_MODE_PAUSE) {
	    /* FIXME: parameters (start/end) in lpParams may not be used */
933
	    return MIDI_mciResume(wmm, dwFlags, (LPMCI_GENERIC_PARMS)lpParms);
934 935
	}
	WARN("Can't play: device is not stopped !\n");
936 937
	return MCIERR_INTERNAL;
    }
938 939

    if (!(dwFlags & MCI_WAIT)) {
940
	return MCI_SendCommandAsync(wmm->wDevID, MCI_PLAY, dwFlags, (DWORD_PTR)lpParms, sizeof(MCI_PLAY_PARMS));
941
    }
942

943
    if (lpParms && (dwFlags & MCI_FROM)) {
944
	dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
945 946 947
    } else {
	dwStartMS = wmm->dwPositionMS;
    }
948

949 950 951 952 953
    if (lpParms && (dwFlags & MCI_TO)) {
	dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
    } else {
	dwEndMS = 0xFFFFFFFFul;
    }
954

955
    TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
956

957 958
    oldcb = InterlockedExchangePointer(&wmm->hCallback,
	(dwFlags & MCI_NOTIFY) ? HWND_32(LOWORD(lpParms->dwCallback)) : NULL);
959
    if (oldcb) mciDriverNotify(oldcb, wmm->wDevID, MCI_NOTIFY_ABORTED);
960 961
    oldcb = NULL;

962 963 964
    /* init tracks */
    for (nt = 0; nt < wmm->nTracks; nt++) {
	mmt = &wmm->tracks[nt];
965

966 967 968 969 970 971 972 973 974
	mmt->wStatus = 1;	/* ok, playing */
	mmt->dwIndex = mmt->dwFirst;
	if (wmm->wFormat == 2 && nt > 0) {
	    mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
	} else {
	    mmt->dwEventPulse = 0;
	}
	MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
    }
975

976
    dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, wmm->wPort, 0L, 0L, CALLBACK_NULL);
977 978 979 980
    if (dwRet != MMSYSERR_NOERROR) {
	return dwRet;
    }

981 982 983 984 985
    wmm->dwPulse = 0;
    wmm->dwTempo = 500000;
    wmm->dwStatus = MCI_MODE_PLAY;
    wmm->dwPositionMS = 0;
    wmm->wStartedPlaying = FALSE;
986

987
    while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
988 989
	/* it seems that in case of multi-threading, gcc is optimizing just a little bit
	 * too much. Tell gcc not to optimize status value using volatile.
990 991
	 */
	while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
992

993
	doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
994

995
	TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm->dwStatus, doPlay ? 'T' : 'F');
996

997 998
	if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
	    break;  /* no more event on tracks */
999

1000 1001 1002
	/* if starting playing, then set StartTicks to the value it would have had
	 * if play had started at position 0
	 */
1003 1004 1005
	if (doPlay && !wmm->wStartedPlaying) {
	    wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
	    wmm->wStartedPlaying = TRUE;
1006
	    TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
1007
	}
1008

1009
	if (hiPulse > wmm->dwPulse) {
Eric Pouech's avatar
Eric Pouech committed
1010
	    wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
1011
	    if (doPlay) {
Eric Pouech's avatar
Eric Pouech committed
1012
		DWORD	togo = wmm->dwStartTicks + wmm->dwPositionMS;
1013
		DWORD	tc = GetTickCount();
1014

1015 1016
		TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
		TRACE("Wait until %u => %u ms\n",
1017
		      tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
1018
		if (tc < togo)
1019 1020 1021 1022
		    Sleep(togo - tc);
	    }
	    wmm->dwPulse = hiPulse;
	}
1023

1024 1025 1026 1027
	switch (LOBYTE(LOWORD(mmt->dwEventData))) {
	case 0xF0:
	case 0xF7:	/* sysex events */
	    {
1028
		FIXME("Not handling SysEx events (yet)\n");
1029 1030 1031 1032
	    }
	    break;
	case 0xFF:
	    /* position after meta data header */
1033
	    mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
1034 1035
	    switch (HIBYTE(LOWORD(mmt->dwEventData))) {
	    case 0x00: /* 16-bit sequence number */
1036
		if (TRACE_ON(mcimidi)) {
1037
		    WORD	twd;
1038

1039
		    MIDI_mciReadWord(wmm, &twd);	/* == 0 */
1040
		    TRACE("Got sequence number %u\n", twd);
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
		}
		break;
	    case 0x01: /* any text */
	    case 0x02: /* Copyright Message text */
	    case 0x03: /* Sequence/Track Name text */
	    case 0x04: /* Instrument Name text */
	    case 0x05: /* Lyric text */
	    case 0x06: /* Marker text */
	    case 0x07: /* Cue-point text */
		if (TRACE_ON(mcimidi)) {
		    char	buf[1024];
		    WORD	len = mmt->wEventLength - HIWORD(mmt->dwEventData);
1053 1054
		    static const char* const	info[8] = {"", "Text", "Copyright", "Seq/Trk name",
							   "Instrument", "Lyric", "Marker", "Cue-point"};
1055
		    WORD	idx = HIBYTE(LOWORD(mmt->dwEventData));
1056

1057
		    if (len >= sizeof(buf)) {
1058
			WARN("Buffer for text is too small (%u are needed)\n", len);
1059 1060
			len = sizeof(buf) - 1;
		    }
1061
                    if (mmioRead(wmm->hFile, buf, len) == len) {
1062
			buf[len] = 0;	/* end string in case */
1063
			TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
1064
		    } else {
1065
			WARN("Couldn't read data for %s\n", (idx < 8) ? info[idx] : "");
1066 1067 1068
		    }
		}
		break;
1069
	    case 0x20:
1070 1071 1072
		/* MIDI channel (cc) */
		if (FIXME_ON(mcimidi)) {
		    BYTE	bt;
1073

1074
		    MIDI_mciReadByte(wmm, &bt);	/* == 0 */
1075
		    FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
1076 1077 1078 1079 1080 1081
		}
		break;
	    case 0x21:
		/* MIDI port (pp) */
		if (FIXME_ON(mcimidi)) {
		    BYTE	bt;
1082

1083
		    MIDI_mciReadByte(wmm, &bt);	/* == 0 */
1084
		    FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
1085 1086 1087 1088 1089 1090
		}
		break;
	    case 0x2F: /* end of track */
		mmt->wStatus = 0;
		break;
	    case 0x51:/* set tempo */
1091
		/* Tempo is expressed in -seconds per midi quarter note
1092 1093 1094
		 * for format 1 MIDI files, this can only be present on track #0
		 */
		if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1095
		    WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
1096
		} else {
1097
		    BYTE	tbt;
1098
		    DWORD	value = 0;
1099

1100 1101 1102
		    MIDI_mciReadByte(wmm, &tbt);	value  = ((DWORD)tbt) << 16;
		    MIDI_mciReadByte(wmm, &tbt);	value |= ((DWORD)tbt) << 8;
		    MIDI_mciReadByte(wmm, &tbt);	value |= ((DWORD)tbt) << 0;
1103
		    TRACE("Setting tempo to %d (BPM=%d)\n", wmm->dwTempo, (value) ? (60000000 / value) : 0);
1104
		    wmm->dwTempo = value;
1105 1106 1107 1108
		}
		break;
	    case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
		if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1109
		    WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
1110
		} if (mmt->dwEventPulse != 0) {
1111
		    WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt->dwEventPulse);
1112 1113
		} else {
		    BYTE	h, m, s, f, ff;
1114

1115 1116 1117 1118 1119
		    MIDI_mciReadByte(wmm, &h);
		    MIDI_mciReadByte(wmm, &m);
		    MIDI_mciReadByte(wmm, &s);
		    MIDI_mciReadByte(wmm, &f);
		    MIDI_mciReadByte(wmm, &ff);
1120
		    FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1121 1122
		}
		break;
1123
	    case 0x58: /* file rhythm */
1124 1125
		if (TRACE_ON(mcimidi)) {
		    BYTE	num, den, cpmc, _32npqn;
1126 1127

		    MIDI_mciReadByte(wmm, &num);
1128
		    MIDI_mciReadByte(wmm, &den);		/* to notate e.g. 6/8 */
1129
		    MIDI_mciReadByte(wmm, &cpmc);		/* number of MIDI clocks per metronome click */
1130
		    MIDI_mciReadByte(wmm, &_32npqn);		/* number of notated 32nd notes per MIDI quarter note */
1131

1132
		    TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1133 1134 1135 1136 1137
		}
		break;
	    case 0x59: /* key signature */
		if (TRACE_ON(mcimidi)) {
		    BYTE	sf, mm;
1138 1139 1140 1141 1142 1143

		    MIDI_mciReadByte(wmm, &sf);
		    MIDI_mciReadByte(wmm, &mm);

		    if (sf >= 0x80) 	TRACE("%d flats\n", -(char)sf);
		    else if (sf > 0) 	TRACE("%d sharps\n", (char)sf);
1144
		    else 		TRACE("Key of C\n");
1145
		    TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1146 1147 1148
		}
		break;
	    default:
1149
		WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1150 1151 1152 1153 1154
		break;
	    }
	    break;
	default:
	    if (doPlay) {
1155
		dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1156 1157 1158 1159
	    } else {
		switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
		case MIDI_NOTEON:
		case MIDI_NOTEOFF:
1160
		    dwRet = 0;
1161 1162
		    break;
		default:
1163
		    dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1164 1165 1166 1167 1168 1169
		}
	    }
	}
	mmt->dwIndex += mmt->wEventLength;
	if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
	    mmt->wStatus = 0;
1170 1171
	}
	if (mmt->wStatus) {
1172 1173 1174
	    MIDI_mciReadNextEvent(wmm, mmt);
	}
    }
1175

1176
    midiOutReset((HMIDIOUT)wmm->hMidi);
1177

1178
    dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1179

1180 1181
    if (dwFlags & MCI_NOTIFY)
	oldcb = InterlockedExchangePointer(&wmm->hCallback, NULL);
1182 1183

    wmm->dwStatus = MCI_MODE_STOP;
1184 1185

    /* Let the potentially asynchronous commands support FAILURE notification. */
1186
    if (oldcb) mciDriverNotify(oldcb, wmm->wDevID,
1187
	dwRet ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1188
    return dwRet;
1189 1190 1191 1192 1193
}

/**************************************************************************
 * 				MIDI_mciPause			[internal]
 */
1194
static DWORD MIDI_mciPause(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1195
{
1196
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1197

1198 1199 1200 1201
    if (wmm->dwStatus == MCI_MODE_PLAY) {
	/* stop all notes */
	unsigned chn;
	for (chn = 0; chn < 16; chn++)
1202
	    midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1203
	wmm->dwStatus = MCI_MODE_PAUSE;
1204
    }
1205 1206
    if ((dwFlags & MCI_NOTIFY) && lpParms)
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1207 1208 1209 1210 1211 1212
    return 0;
}

/**************************************************************************
 * 				MIDI_mciResume			[internal]
 */
1213
static DWORD MIDI_mciResume(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1214
{
1215
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1216

1217 1218 1219
    if (wmm->dwStatus == MCI_MODE_PAUSE) {
	wmm->wStartedPlaying = FALSE;
	wmm->dwStatus = MCI_MODE_PLAY;
1220
    }
1221 1222
    if ((dwFlags & MCI_NOTIFY) && lpParms)
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1223 1224 1225 1226 1227 1228
    return 0;
}

/**************************************************************************
 * 				MIDI_mciSet			[internal]
 */
1229
static DWORD MIDI_mciSet(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_SEQ_SET_PARMS lpParms)
1230
{
1231
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1232

1233
    if (lpParms == NULL)	return MCIERR_NULL_PARAMETER_BLOCK;
1234

1235 1236 1237
    if (dwFlags & MCI_SET_TIME_FORMAT) {
	switch (lpParms->dwTimeFormat) {
	case MCI_FORMAT_MILLISECONDS:
1238
	    TRACE("MCI_FORMAT_MILLISECONDS !\n");
1239 1240 1241
	    wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
	    break;
	case MCI_FORMAT_SMPTE_24:
1242
	    TRACE("MCI_FORMAT_SMPTE_24 !\n");
1243 1244 1245
	    wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
	    break;
	case MCI_FORMAT_SMPTE_25:
1246
	    TRACE("MCI_FORMAT_SMPTE_25 !\n");
1247 1248 1249
	    wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
	    break;
	case MCI_FORMAT_SMPTE_30:
1250
	    TRACE("MCI_FORMAT_SMPTE_30 !\n");
1251 1252 1253
	    wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
	    break;
	default:
1254
	    WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1255 1256 1257 1258
	    return MCIERR_BAD_TIME_FORMAT;
	}
    }
    if (dwFlags & MCI_SET_VIDEO) {
1259
	TRACE("No support for video !\n");
1260 1261 1262
	return MCIERR_UNSUPPORTED_FUNCTION;
    }
    if (dwFlags & MCI_SET_DOOR_OPEN) {
1263
	TRACE("No support for door open !\n");
1264 1265 1266
	return MCIERR_UNSUPPORTED_FUNCTION;
    }
    if (dwFlags & MCI_SET_DOOR_CLOSED) {
1267
	TRACE("No support for door close !\n");
1268 1269 1270 1271
	return MCIERR_UNSUPPORTED_FUNCTION;
    }
    if (dwFlags & MCI_SET_AUDIO) {
	if (dwFlags & MCI_SET_ON) {
1272
	    TRACE("MCI_SET_ON audio !\n");
1273
	} else if (dwFlags & MCI_SET_OFF) {
1274
	    TRACE("MCI_SET_OFF audio !\n");
1275
	} else {
1276
	    WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1277 1278
	    return MCIERR_BAD_INTEGER;
	}
1279

1280 1281 1282 1283 1284
	switch (lpParms->dwAudio)
        {
        case MCI_SET_AUDIO_ALL:         TRACE("MCI_SET_AUDIO_ALL !\n"); break;
        case MCI_SET_AUDIO_LEFT:        TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
        case MCI_SET_AUDIO_RIGHT:       TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1285
        default:                        WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1286
        }
1287
    }
1288

1289
    if (dwFlags & MCI_SEQ_SET_MASTER)
1290
	TRACE("MCI_SEQ_SET_MASTER !\n");
1291
    if (dwFlags & MCI_SEQ_SET_SLAVE)
1292
	TRACE("MCI_SEQ_SET_SLAVE !\n");
1293
    if (dwFlags & MCI_SEQ_SET_OFFSET)
1294
	TRACE("MCI_SEQ_SET_OFFSET !\n");
1295 1296 1297 1298 1299 1300 1301 1302 1303
    if (dwFlags & MCI_SEQ_SET_PORT) {
	TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms->dwPort);
	if ((UINT16)lpParms->dwPort != (UINT16)MIDI_MAPPER &&
	    (UINT16)lpParms->dwPort >= midiOutGetNumDevs())
	    /* FIXME: input/output port distinction? */
	    return MCIERR_SEQ_PORT_NONEXISTENT;
	/* FIXME: Native manages to swap the device while playing! */
	wmm->wPort = lpParms->dwPort;
    }
1304
    if (dwFlags & MCI_SEQ_SET_TEMPO)
1305
	TRACE("MCI_SEQ_SET_TEMPO !\n");
1306 1307
    if (dwFlags & MCI_NOTIFY)
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1308 1309 1310 1311 1312 1313
    return 0;
}

/**************************************************************************
 * 				MIDI_mciStatus			[internal]
 */
1314
static DWORD MIDI_mciStatus(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1315
{
1316 1317
    DWORD		ret = 0;

1318
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1319

1320
    if (lpParms == NULL) 	return MCIERR_NULL_PARAMETER_BLOCK;
1321

1322 1323 1324 1325 1326
    if (dwFlags & MCI_STATUS_ITEM) {
	switch (lpParms->dwItem) {
	case MCI_STATUS_CURRENT_TRACK:
	    /* FIXME in Format 2 */
	    lpParms->dwReturn = 1;
1327
	    TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1328 1329 1330 1331 1332
	    break;
	case MCI_STATUS_LENGTH:
	    if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
		if (lpParms->dwTrack >= wmm->nTracks)
		    return MCIERR_BAD_INTEGER;
1333
		/* FIXME: this is wrong if there is a tempo change inside the file */
1334 1335 1336 1337 1338
		lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
	    } else {
		lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
	    }
	    lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1339
	    TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1340 1341
	    break;
	case MCI_STATUS_MODE:
1342 1343 1344
	    TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
 	    lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
	    ret = MCI_RESOURCE_RETURNED;
1345 1346
	    break;
	case MCI_STATUS_MEDIA_PRESENT:
1347
	    TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1348 1349
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1350 1351 1352
	    break;
	case MCI_STATUS_NUMBER_OF_TRACKS:
	    lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1353
	    TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1354 1355 1356
	    break;
	case MCI_STATUS_POSITION:
	    /* FIXME: do I need to use MCI_TRACK ? */
1357
	    lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1358
							   (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1359
	    TRACE("MCI_STATUS_POSITION %s => %lu\n",
1360 1361 1362
		  (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
	    break;
	case MCI_STATUS_READY:
1363 1364 1365 1366
	    lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
		MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
	    TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1367 1368
	    break;
	case MCI_STATUS_TIME_FORMAT:
1369
	    lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1370 1371
	    TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
	    ret = MCI_RESOURCE_RETURNED;
1372 1373
	    break;
	case MCI_SEQ_STATUS_DIVTYPE:
1374
	    TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1375 1376 1377 1378 1379 1380
	    if (wmm->nDivision > 0x8000) {
		switch (wmm->nDivision) {
		case 0xE8:	lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24;	break;	/* -24 */
		case 0xE7:	lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25;	break;	/* -25 */
		case 0xE3:	lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP;	break;	/* -29 */ /* is the MCI constant correct ? */
		case 0xE2:	lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30;	break;	/* -30 */
1381
		default:	FIXME("There is a bad bad programmer\n");
1382 1383 1384 1385
		}
	    } else {
		lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
	    }
1386 1387
	    lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
	    ret = MCI_RESOURCE_RETURNED;
1388 1389
	    break;
	case MCI_SEQ_STATUS_MASTER:
1390
	    TRACE("MCI_SEQ_STATUS_MASTER !\n");
1391 1392 1393
	    lpParms->dwReturn = 0;
	    break;
	case MCI_SEQ_STATUS_SLAVE:
1394
	    TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1395 1396 1397
	    lpParms->dwReturn = 0;
	    break;
	case MCI_SEQ_STATUS_OFFSET:
1398
	    TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1399 1400 1401
	    lpParms->dwReturn = 0;
	    break;
	case MCI_SEQ_STATUS_PORT:
1402 1403 1404 1405 1406 1407 1408
	    if (wmm->wPort != (UINT16)MIDI_MAPPER)
		lpParms->dwReturn = wmm->wPort;
	    else {
		lpParms->dwReturn = MAKEMCIRESOURCE(MIDI_MAPPER, MCI_SEQ_MAPPER_S);
		ret = MCI_RESOURCE_RETURNED;
	    }
	    TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm->wDevID, wmm->wPort);
1409 1410
	    break;
	case MCI_SEQ_STATUS_TEMPO:
1411
	    TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1412 1413 1414
	    lpParms->dwReturn = wmm->dwTempo;
	    break;
	default:
Anders Jonsson's avatar
Anders Jonsson committed
1415
	    FIXME("Unknown command %08X !\n", lpParms->dwItem);
1416 1417 1418
	    return MCIERR_UNRECOGNIZED_COMMAND;
	}
    } else {
1419
	WARN("No Status-Item!\n");
1420 1421
	return MCIERR_UNRECOGNIZED_COMMAND;
    }
1422 1423
    if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1424
    return ret;
1425 1426 1427 1428 1429
}

/**************************************************************************
 * 				MIDI_mciGetDevCaps		[internal]
 */
1430
static DWORD MIDI_mciGetDevCaps(WINE_MCIMIDI* wmm, DWORD dwFlags,
1431 1432
				LPMCI_GETDEVCAPS_PARMS lpParms)
{
1433 1434
    DWORD		ret;

1435
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1436

1437
    if (lpParms == NULL) 	return MCIERR_NULL_PARAMETER_BLOCK;
1438

1439 1440 1441
    if (dwFlags & MCI_GETDEVCAPS_ITEM) {
	switch (lpParms->dwItem) {
	case MCI_GETDEVCAPS_DEVICE_TYPE:
1442
	    TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1443 1444
	    lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
	    ret = MCI_RESOURCE_RETURNED;
1445 1446
	    break;
	case MCI_GETDEVCAPS_HAS_AUDIO:
1447
	    TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1448 1449
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1450 1451
	    break;
	case MCI_GETDEVCAPS_HAS_VIDEO:
1452
	    TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1453 1454
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
	    ret = MCI_RESOURCE_RETURNED;
1455 1456
	    break;
	case MCI_GETDEVCAPS_USES_FILES:
1457
	    TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1458 1459
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1460 1461
	    break;
	case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1462
	    TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1463 1464
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1465 1466
	    break;
	case MCI_GETDEVCAPS_CAN_EJECT:
1467
	    TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1468 1469
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
	    ret = MCI_RESOURCE_RETURNED;
1470 1471
	    break;
	case MCI_GETDEVCAPS_CAN_PLAY:
1472
	    TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1473 1474
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1475 1476
	    break;
	case MCI_GETDEVCAPS_CAN_RECORD:
1477
	    TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1478
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1479
	    ret = MCI_RESOURCE_RETURNED;
1480 1481
	    break;
	case MCI_GETDEVCAPS_CAN_SAVE:
1482
	    TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1483 1484
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
	    ret = MCI_RESOURCE_RETURNED;
1485 1486
	    break;
	default:
1487
	    FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1488 1489 1490
	    return MCIERR_UNRECOGNIZED_COMMAND;
	}
    } else {
1491
	WARN("No GetDevCaps-Item !\n");
1492 1493
	return MCIERR_UNRECOGNIZED_COMMAND;
    }
1494 1495
    if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1496
    return ret;
1497 1498 1499 1500 1501
}

/**************************************************************************
 * 				MIDI_mciInfo			[internal]
 */
1502
static DWORD MIDI_mciInfo(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1503
{
1504
    LPCWSTR		str = 0;
1505
    DWORD		ret = 0;
1506
    static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1507

1508
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1509

1510 1511 1512
    if (lpParms == NULL || lpParms->lpstrReturn == NULL)
	return MCIERR_NULL_PARAMETER_BLOCK;

1513
    TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1514

1515
    switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1516 1517 1518 1519
    case MCI_INFO_PRODUCT:      str = wszMidiSeq; break;
    case MCI_INFO_FILE:         str = wmm->lpstrElementName; break;
    case MCI_INFO_COPYRIGHT:    str = wmm->lpstrCopyright; break;
    case MCI_INFO_NAME:         str = wmm->lpstrName; break;
1520
    default:
1521
	WARN("Don't know this info command (%u)\n", dwFlags);
1522
	return MCIERR_UNRECOGNIZED_COMMAND;
1523
    }
1524 1525 1526 1527 1528 1529 1530
    if (!ret) {
	if (lpParms->dwRetSize) {
	    WCHAR zero = 0;
	    /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
	     *        to the number of characters written, excluding \0. */
	    lstrcpynW(lpParms->lpstrReturn, str ? str : &zero, lpParms->dwRetSize);
	} else ret = MCIERR_PARAM_OVERFLOW;
1531
    }
1532 1533
    if (MMSYSERR_NOERROR==ret && (dwFlags & MCI_NOTIFY))
	MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1534
    return ret;
1535 1536 1537 1538 1539
}

/**************************************************************************
 * 				MIDI_mciSeek			[internal]
 */
1540
static DWORD MIDI_mciSeek(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1541 1542
{
    DWORD		ret = 0;
1543

1544
    TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1545

1546 1547 1548
    if (lpParms == NULL) {
	ret = MCIERR_NULL_PARAMETER_BLOCK;
    } else {
1549
	MIDI_mciStop(wmm, MCI_WAIT, 0);
1550

1551 1552 1553
	if (dwFlags & MCI_SEEK_TO_START) {
	    wmm->dwPositionMS = 0;
	} else if (dwFlags & MCI_SEEK_TO_END) {
1554
	    wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1555 1556 1557
	} else if (dwFlags & MCI_TO) {
	    wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
	} else {
1558
	    WARN("dwFlag doesn't tell where to seek to...\n");
1559 1560
	    return MCIERR_MISSING_PARAMETER;
	}
1561

1562
	TRACE("Seeking to position=%u ms\n", wmm->dwPositionMS);
1563

1564 1565
	if (dwFlags & MCI_NOTIFY)
	    MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1566
    }
1567 1568
    return ret;
}
1569 1570 1571 1572 1573 1574

/*======================================================================*
 *                  	    MIDI entry points 				*
 *======================================================================*/

/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1575
 * 				DriverProc (MCISEQ.@)
1576
 */
1577 1578
LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
                                    LPARAM dwParam1, LPARAM dwParam2)
1579
{
1580
    WINE_MCIMIDI*	wmm;
1581 1582 1583 1584 1585 1586
    switch (wMsg) {
    case DRV_LOAD:		return 1;
    case DRV_FREE:		return 1;
    case DRV_ENABLE:		return 1;
    case DRV_DISABLE:		return 1;
    case DRV_QUERYCONFIGURE:	return 1;
1587
    case DRV_CONFIGURE:		MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1588 1589
    case DRV_INSTALL:		return DRVCNF_RESTART;
    case DRV_REMOVE:		return DRVCNF_RESTART;
1590
    case DRV_OPEN:		return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1591
    case DRV_CLOSE:		return MIDI_drvClose(dwDevID);
1592
    }
1593 1594 1595 1596
    if ((wMsg < DRV_MCI_FIRST) || (wMsg > DRV_MCI_LAST)) {
	TRACE("Sending msg %04x to default driver proc\n", wMsg);
	return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
    }
1597

1598
    wmm = MIDI_mciGetOpenDev(dwDevID, wMsg);
1599 1600
    if (wmm == NULL)		return MCIERR_INVALID_DEVICE_ID;

1601
    switch (wMsg) {
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612
    case MCI_OPEN_DRIVER:	return MIDI_mciOpen      (wmm, dwParam1, (LPMCI_OPEN_PARMSW)     dwParam2);
    case MCI_CLOSE_DRIVER:	return MIDI_mciClose     (wmm, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_PLAY:		return MIDI_mciPlay      (wmm, dwParam1, (LPMCI_PLAY_PARMS)      dwParam2);
    case MCI_STOP:		return MIDI_mciStop      (wmm, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_SET:		return MIDI_mciSet       (wmm, dwParam1, (LPMCI_SEQ_SET_PARMS)   dwParam2);
    case MCI_PAUSE:		return MIDI_mciPause     (wmm, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_RESUME:		return MIDI_mciResume    (wmm, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_STATUS:		return MIDI_mciStatus    (wmm, dwParam1, (LPMCI_STATUS_PARMS)    dwParam2);
    case MCI_GETDEVCAPS:	return MIDI_mciGetDevCaps(wmm, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
    case MCI_INFO:		return MIDI_mciInfo      (wmm, dwParam1, (LPMCI_INFO_PARMSW)     dwParam2);
    case MCI_SEEK:		return MIDI_mciSeek      (wmm, dwParam1, (LPMCI_SEEK_PARMS)      dwParam2);
1613 1614
    case MCI_OPEN:
    case MCI_CLOSE:
1615
	FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1616
	/* fall through */
1617
    default:
1618 1619
	TRACE("Unsupported command [0x%x]\n", wMsg);
        return MCIERR_UNSUPPORTED_FUNCTION; /* Win9x: MCIERR_UNRECOGNIZED_COMMAND */
1620 1621
    }
}