mcimidi.c 54.1 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 24 25 26 27 28
/* TODO:
 *      + implement it correctly
 *      + finish asynchronous commands (especially for reading/record)
 *      + better implement non waiting command (without the MCI_WAIT flag).
 *      + implement the recording features
 */

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

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

43
WINE_DEFAULT_DEBUG_CHANNEL(mcimidi);
44

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

48 49 50 51 52 53 54 55 56 57
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,
58
	                wLastCommand : 8;	/* last MIDI command on track */
59 60
} MCI_MIDITRACK;

61
typedef struct tagWINE_MCIMIDI {
62
    UINT		wDevID;			/* the MCI one */
63
    HMIDI		hMidi;
64 65
    int			nUseCount;          	/* Incremented for each shared open          */
    WORD		wNotifyDeviceID;    	/* MCI device ID with a pending notification */
66
    HANDLE 		hCallback;         	/* Callback handle for pending notification  */
67
    HMMIO		hFile;	            	/* mmio file handle open as Element          */
68 69 70
    LPWSTR		lpstrElementName;       /* Name of file (if any)                     */
    LPWSTR		lpstrCopyright;
    LPWSTR		lpstrName;
71
    WORD		dwStatus;		/* one from MCI_MODE_xxxx */
72
    DWORD		dwMciTimeFormat;	/* One of the supported MCI_FORMAT_xxxx */
73 74 75 76 77 78
    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 */
79
    DWORD		dwPulse;
80 81 82 83
    DWORD		dwPositionMS;
    DWORD		dwStartTicks;
} WINE_MCIMIDI;

84 85 86 87 88 89 90 91 92 93 94 95
/* ===================================================================
 * ===================================================================
 * 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;
96 97
    DWORD_PTR   dwParam1;
    DWORD_PTR   dwParam2;
98 99 100 101 102 103 104 105 106
};

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

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

110
    TRACE("In thread before async command (%08x,%u,%08lx,%08lx)\n",
111 112
	  sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
    ret = mciSendCommandA(sca->wDevID, sca->wMsg, sca->dwParam1 | MCI_WAIT, sca->dwParam2);
113
    TRACE("In thread after async command (%08x,%u,%08lx,%08lx)\n",
114 115 116
	  sca->wDevID, sca->wMsg, sca->dwParam1, sca->dwParam2);
    HeapFree(GetProcessHeap(), 0, sca);
    ExitThread(ret);
117
    WARN("Should not happen ? what's wrong\n");
118 119 120 121 122 123 124
    /* should not go after this point */
    return ret;
}

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

    if (sca == 0)
	return MCIERR_OUT_OF_MEMORY;

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

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

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

157
/*======================================================================*
158
 *                  	    MCI MIDI implementation			*
159 160
 *======================================================================*/

161
static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
162

163
/**************************************************************************
164
 * 				MIDI_drvOpen			[internal]
165
 */
166
static	DWORD	MIDI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
167
{
168 169 170 171 172
    WINE_MCIMIDI*	wmm;

    if (!modp) return 0xFFFFFFFF;

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

174 175 176 177
    if (!wmm)
	return 0;

    wmm->wDevID = modp->wDeviceID;
178
    mciSetDriverData(wmm->wDevID, (DWORD_PTR)wmm);
179
    modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
180 181
    modp->wType = MCI_DEVTYPE_SEQUENCER;
    return modp->wDeviceID;
182 183 184
}

/**************************************************************************
185
 * 				MCIMIDI_drvClose		[internal]
186 187 188
 */
static	DWORD	MIDI_drvClose(DWORD dwDevID)
{
189
    WINE_MCIMIDI*  wmm = (WINE_MCIMIDI*)mciGetDriverData(dwDevID);
190 191

    if (wmm) {
192
	HeapFree(GetProcessHeap(), 0, wmm);
193
	mciSetDriverData(dwDevID, 0);
194 195
	return 1;
    }
196
    return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
197 198
}

199
/**************************************************************************
200
 * 				MIDI_mciGetOpenDev		[internal]
201
 */
202
static WINE_MCIMIDI*  MIDI_mciGetOpenDev(UINT wDevID)
203
{
204
    WINE_MCIMIDI*	wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
205

206
    if (wmm == NULL || wmm->nUseCount == 0) {
207
	WARN("Invalid wDevID=%u\n", wDevID);
208 209
	return 0;
    }
210
    return wmm;
211 212 213
}

/**************************************************************************
214
 * 				MIDI_mciReadByte		[internal]
215 216 217 218
 */
static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
{
    DWORD	ret = 0;
219 220

    if (lpbyt == NULL ||
221
	mmioRead(wmm->hFile, (HPSTR)lpbyt, sizeof(BYTE)) != (long)sizeof(BYTE)) {
222
	WARN("Error reading wmm=%p\n", wmm);
223
	ret = MCIERR_INVALID_FILE;
224
    }
225

226 227 228 229
    return ret;
}

/**************************************************************************
230
 * 				MIDI_mciReadWord		[internal]
231 232 233 234
 */
static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
{
    BYTE	hibyte, lobyte;
235
    DWORD	ret = MCIERR_INVALID_FILE;
236 237 238

    if (lpw != NULL &&
	MIDI_mciReadByte(wmm, &hibyte) == 0 &&
239 240 241 242 243 244 245 246
	MIDI_mciReadByte(wmm, &lobyte) == 0) {
	*lpw = ((WORD)hibyte << 8) + lobyte;
	ret = 0;
    }
    return ret;
}

/**************************************************************************
247
 * 				MIDI_mciReadLong		[internal]
248 249 250 251
 */
static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
{
    WORD	hiword, loword;
252
    DWORD	ret = MCIERR_INVALID_FILE;
253

254 255 256 257 258 259 260 261 262 263
    if (lpdw != NULL &&
	MIDI_mciReadWord(wmm, &hiword) == 0 &&
	MIDI_mciReadWord(wmm, &loword) == 0) {
	*lpdw = MAKELONG(loword, hiword);
	ret = 0;
    }
    return ret;
}

/**************************************************************************
264
 *  				MIDI_mciReadVaryLen		[internal]
265 266 267 268 269 270
 */
static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
{
    BYTE	byte;
    DWORD	value = 0;
    WORD	ret = 0;
271

272
    if (lpdw == NULL) {
273
	ret = MCIERR_INVALID_FILE;
274 275 276 277 278 279
    } else {
	do {
	    if (MIDI_mciReadByte(wmm, &byte) != 0) {
		return 0;
	    }
	    value = (value << 7) + (byte & 0x7F);
280
	    ret++;
281 282 283
	} while (byte & 0x80);
	*lpdw = value;
	/*
284
	  TRACE("val=%08X\n", value);
285 286 287 288 289 290
	*/
    }
    return ret;
}

/**************************************************************************
291
 * 				MIDI_mciReadNextEvent		[internal]
292
 */
293
static DWORD	MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
294 295 296 297 298 299
{
    BYTE	b1, b2 = 0, b3;
    WORD	hw = 0;
    DWORD	evtPulse;
    DWORD	evtLength;
    DWORD	tmp;
300

301
    if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
302
	WARN("Can't seek at %08X\n", mmt->dwIndex);
303
	return MCIERR_INVALID_FILE;
304 305 306 307 308 309 310 311 312 313 314
    }
    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++;
315

316 317 318
	evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
	if (evtLength >= 0x10000u) {
	    /* this limitation shouldn't be a problem */
319
	    WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
	    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:
342
	    WARN("Strange indeed b1=0x%02x\n", b1);
343 344 345 346 347
	}
	break;
    }
    if (mmt->dwIndex + evtLength > mmt->dwLast)
	return MCIERR_INTERNAL;
348

349 350 351
    mmt->dwEventPulse += evtPulse;
    mmt->dwEventData   = (hw << 16) + (b2 << 8) + b1;
    mmt->wEventLength  = evtLength;
352

353
    /*
354
      TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
355
      mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
356
      mmt->dwEventData, mmt->wEventLength);
357 358 359 360 361
    */
    return 0;
}

/**************************************************************************
362
 * 				MIDI_mciReadMTrk		[internal]
363 364 365 366 367
 */
static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
{
    DWORD		toberead;
    FOURCC		fourcc;
368 369

    if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
370
	(long)sizeof(FOURCC)) {
371
	return MCIERR_INVALID_FILE;
372
    }
373

374
    if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
375
	WARN("Can't synchronize on 'MTrk' !\n");
376
	return MCIERR_INVALID_FILE;
377
    }
378

379
    if (MIDI_mciReadLong(wmm, &toberead) != 0) {
380
	return MCIERR_INVALID_FILE;
381
    }
382
    mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
383
    mmt->dwLast = mmt->dwFirst + toberead;
384

385 386 387
    /* compute # of pulses in this track */
    mmt->dwIndex = mmt->dwFirst;
    mmt->dwEventPulse = 0;
388

389 390 391 392
    while (MIDI_mciReadNextEvent(wmm, mmt) == 0 && LOWORD(mmt->dwEventData) != 0x2FFF) {
	char	buf[1024];
	WORD	len;

393
	mmt->dwIndex += mmt->wEventLength;
394 395 396 397 398 399 400

	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);
401

402
	    if (len >= sizeof(buf)) {
403
		WARN("Buffer for text is too small (%u are needed)\n", len);
404 405
		len = sizeof(buf) - 1;
	    }
406
            if (mmioRead(wmm->hFile, buf, len) == len) {
407 408 409 410
		buf[len] = 0;	/* end string in case */
		switch (HIBYTE(LOWORD(mmt->dwEventData))) {
		case 0x02:
		    if (wmm->lpstrCopyright) {
411
			WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm->lpstrCopyright), buf);
412
		    } else {
413 414 415
                        len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
                        wmm->lpstrCopyright = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
                        len = MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrCopyright, len );
416 417 418
		    }
		    break;
		case 0x03:
419
		    if (wmm->lpstrName) {
420
			WARN("Two names (%s|%s)\n", debugstr_w(wmm->lpstrName), buf);
421
		    } else {
422 423 424
                        len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
                        wmm->lpstrName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
                        len = MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrName, len );
425 426 427 428 429 430
		    }
		    break;
		}
	    }
	    break;
	}
431 432
    }
    mmt->dwLength = mmt->dwEventPulse;
433

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

436 437 438 439
    /* reset track data */
    mmt->wStatus = 1;	/* ok, playing */
    mmt->dwIndex = mmt->dwFirst;
    mmt->dwEventPulse = 0;
440

441
    if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
442
	WARN("Ouch, out of sync seek=%u track=%u\n",
443
	     mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
444
	/* position at end of this track, to be ready to read next track */
445
	mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
446
    }
447

448 449 450 451
    return 0;
}

/**************************************************************************
452
 * 				MIDI_mciReadMThd		[internal]
453 454 455 456 457 458
 */
static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
{
    DWORD	toberead;
    FOURCC	fourcc;
    WORD	nt;
459

460
    TRACE("(%p, %08X);\n", wmm, dwOffset);
461

462
    if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
463
	WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset);
464
	return MCIERR_INVALID_FILE;
465
    }
466
    if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
467
		   (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
468
	return MCIERR_INVALID_FILE;
469

470
    if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
471
	WARN("Can't synchronize on 'MThd' !\n");
472
	return MCIERR_INVALID_FILE;
473
    }
474

475
    if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
476
	return MCIERR_INVALID_FILE;
477

478 479 480
    if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
	MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
	MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
481
	return MCIERR_INVALID_FILE;
482
    }
483

484
    TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
485
	  toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
486

487 488 489 490 491
    /* 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
492
	 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
493
	 * In about 40 MB of MIDI files I have, none was SMPTE based...
494
	 * I'm just wondering if this is widely used :-). So, if someone has one of
495
	 * these files, I'd like to know about it.
496
	 */
497
	FIXME("Handling SMPTE time in MIDI files has not been tested\n"
498
	      "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
499

500 501 502 503 504 505
	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:
506
	    WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
507
	    return MCIERR_INVALID_FILE;
508 509 510 511 512 513 514 515
	}
	switch (LOBYTE(wmm->nDivision)) {
	case 4:	/* MIDI Time Code */
	case 8:
	case 10:
	case 80: /* SMPTE bit resolution */
	case 100:
	default:
516
	    WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
517
	    return MCIERR_INVALID_FILE;
518 519
	}
    } else if (wmm->nDivision == 0) {
520
	WARN("Number of division is 0, can't support that !!\n");
521
	return MCIERR_INVALID_FILE;
522 523 524
    } else {
	wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
    }
525

526 527 528
    switch (wmm->wFormat) {
    case 0:
	if (wmm->nTracks != 1) {
529
	    WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
530 531 532 533 534 535 536
	    wmm->nTracks = 1;
	}
	break;
    case 1:
    case 2:
	break;
    default:
537
	WARN("Handling MIDI files which format = %d is not (yet) supported\n"
538
	     "Please report with MIDI file !\n", wmm->wFormat);
539
	return MCIERR_INVALID_FILE;
540
    }
541

542 543
    if (wmm->nTracks & 0x8000) {
	/* this shouldn't be a problem... */
544
	WARN("Ouch !! Implementation limitation to 32k tracks per MIDI file is overflowed\n");
545 546
	wmm->nTracks = 0x7FFF;
    }
547

548
    if ((wmm->tracks = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
549 550
	return MCIERR_OUT_OF_MEMORY;
    }
551 552

    toberead -= 3 * sizeof(WORD);
553
    if (toberead > 0) {
554
	TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead);
555
	mmioSeek(wmm->hFile, toberead, SEEK_CUR);
556
    }
557

558 559 560
    for (nt = 0; nt < wmm->nTracks; nt++) {
	wmm->tracks[nt].wTrackNr = nt;
	if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
561
	    WARN("Can't read 'MTrk' header\n");
562
	    return MCIERR_INVALID_FILE;
563 564
	}
    }
565

566
    wmm->dwTempo = 500000;
567

568 569 570 571 572 573 574 575 576
    return 0;
}

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

578 579 580 581
    /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
     * may change during file playing
     */
    if (wmm->nDivision == 0) {
582
	FIXME("Shouldn't happen. wmm->nDivision = 0\n");
583
    } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
584 585 586 587
	int	nf = -(char)HIBYTE(wmm->nDivision);	/* number of frames     */
	int	nsf = LOBYTE(wmm->nDivision);		/* number of sub-frames */
	ret = (pulse * 1000) / (nf * nsf);
    } else {
588
	ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
589 590
		      (double)wmm->nDivision);
    }
591

592
    /*
593
      TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
594 595
      pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
    */
596

597 598 599 600 601 602 603 604 605 606 607 608 609
    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;
610

611
    switch (wmm->dwMciTimeFormat) {
612 613
    case MCI_FORMAT_MILLISECONDS:
	ret = val;
614 615
	break;
    case MCI_FORMAT_SMPTE_24:
616
	ret =
617 618 619
	    (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;
620 621
    case MCI_FORMAT_SMPTE_25:
	ret =
622 623 624
	    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;
625 626
    case MCI_FORMAT_SMPTE_30:
	ret =
627 628 629 630
	    (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:
631
	WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
632 633
    }
    /*
634
      TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
635 636
    */
    return ret;
637
}
638 639 640 641 642 643 644 645

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

647
    switch (wmm->dwMciTimeFormat) {
648 649
    case MCI_FORMAT_MILLISECONDS:
	ret = val;
650 651
	break;
    case MCI_FORMAT_SMPTE_24:
652 653 654 655
    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;
656 657 658 659 660 661
	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;
662
	case MCI_FORMAT_SMPTE_25:
663 664 665
	    /* one frame is 1000/25 ms long, 1000/25 == 40 */
	    f = val / 40; 		val -= f * 40;
	    break;
666
	case MCI_FORMAT_SMPTE_30:
667 668 669 670
	    /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
	    f = (val * 3) / 100; 	val -= (f * 100) / 3;
	    break;
	default:
671
	    FIXME("There must be some bad bad programmer\n");
672 673 674 675 676 677 678
	    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:
679
	WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
680 681
    }
    /*
682
      TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
683 684
    */
    return ret;
685
}
686 687 688 689 690 691 692 693

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

695 696 697 698 699 700 701
    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;
	}
    }
702
    /* FIXME: this is wrong if there is a tempo change inside the file */
703 704 705 706
    return MIDI_ConvertPulseToMS(wmm, ret);
}

/**************************************************************************
707
 * 				MIDI_mciOpen			[internal]
708
 */
709
static DWORD MIDI_mciOpen(UINT wDevID, DWORD dwFlags, LPMCI_OPEN_PARMSW lpParms)
710
{
711
    DWORD		dwRet = 0;
712
    DWORD		dwDeviceID;
713
    WINE_MCIMIDI*	wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
714

715
    TRACE("(%04x, %08X, %p)\n", wDevID, dwFlags, lpParms);
716

717
    if (lpParms == NULL) 	return MCIERR_NULL_PARAMETER_BLOCK;
718
    if (wmm == NULL)		return MCIERR_INVALID_DEVICE_ID;
719 720 721
    if (dwFlags & MCI_OPEN_SHAREABLE)
	return MCIERR_HARDWARE;

722 723 724 725 726 727 728
    if (wmm->nUseCount > 0) {
	/* The driver is already opened on this channel
	 * MIDI sequencer cannot be shared
	 */
	return MCIERR_DEVICE_OPEN;
    }
    wmm->nUseCount++;
729

730
    wmm->hFile = 0;
731
    wmm->hMidi = 0;
732
    wmm->lpstrElementName = NULL;
733
    dwDeviceID = lpParms->wDeviceID;
734

735
    TRACE("wDevID=%04X (lpParams->wDeviceID=%08X)\n", wDevID, dwDeviceID);
736
    /*	lpParms->wDeviceID = wDevID;*/
737

738
    if (dwFlags & MCI_OPEN_ELEMENT) {
739 740
	TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms->lpstrElementName));
	if (lpParms->lpstrElementName && strlenW(lpParms->lpstrElementName) > 0) {
741
	    wmm->hFile = mmioOpenW((LPWSTR)lpParms->lpstrElementName, NULL,
742
				   MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
743
	    if (wmm->hFile == 0) {
744
		WARN("Can't find file %s!\n", debugstr_w(lpParms->lpstrElementName));
745
		wmm->nUseCount--;
746 747
		return MCIERR_FILE_NOT_FOUND;
	    }
748 749 750
            wmm->lpstrElementName = HeapAlloc(GetProcessHeap(), 0, 
                                              (strlenW(lpParms->lpstrElementName) + 1) * sizeof(WCHAR));
            strcpyW(wmm->lpstrElementName, lpParms->lpstrElementName);
751 752
	}
    }
753
    TRACE("hFile=%p\n", wmm->hFile);
754

755 756 757
    wmm->lpstrCopyright = NULL;
    wmm->lpstrName = NULL;

758 759
    wmm->wNotifyDeviceID = dwDeviceID;
    wmm->dwStatus = MCI_MODE_NOT_READY;	/* while loading file contents */
760
    /* spec says it should be the default format from the MIDI file... */
761
    wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
762

763 764
    if (wmm->hFile != 0) {
	MMCKINFO	ckMainRIFF;
765
	MMCKINFO	mmckInfo;
766
	DWORD		dwOffset = 0;
767

768
	if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
769 770
	    dwRet = MCIERR_INVALID_FILE;
	} else {
771
	    TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
772
		  (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
773

774 775
	    if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
		mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
776
		mmioSeek(wmm->hFile, ckMainRIFF.dwDataOffset + ((ckMainRIFF.cksize + 1) & ~1), SEEK_SET);
777
		if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
778
		    TRACE("... is a 'RMID' file\n");
779 780 781
		    dwOffset = mmckInfo.dwDataOffset;
		} else {
		    dwRet = MCIERR_INVALID_FILE;
782
		}
783 784
	    }
	    if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
785
		WARN("Can't read 'MThd' header\n");
786
		dwRet = MCIERR_INVALID_FILE;
787
	    }
788 789
	}
    } else {
790
	TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
791 792 793 794
	wmm->nTracks = 0;
	wmm->wFormat = 0;
	wmm->nDivision = 1;
    }
795 796 797
    if (dwRet != 0) {
	wmm->nUseCount--;
	if (wmm->hFile != 0)
798
	    mmioClose(wmm->hFile, 0);
799 800
	wmm->hFile = 0;
    } else {
801 802
	wmm->dwPositionMS = 0;
	wmm->dwStatus = MCI_MODE_STOP;
803 804
    }
    return dwRet;
805 806 807
}

/**************************************************************************
808
 * 				MIDI_mciStop			[internal]
809
 */
810
static DWORD MIDI_mciStop(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
811
{
812
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
813

814
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
815

816
    if (wmm == NULL)	return MCIERR_INVALID_DEVICE_ID;
817

818
    if (wmm->dwStatus != MCI_MODE_STOP) {
819 820 821 822
	int	oldstat = wmm->dwStatus;

	wmm->dwStatus = MCI_MODE_NOT_READY;
	if (oldstat == MCI_MODE_PAUSE)
823
	    midiOutReset((HMIDIOUT)wmm->hMidi);
824 825 826

	while (wmm->dwStatus != MCI_MODE_STOP)
	    Sleep(10);
827
    }
828

Austin English's avatar
Austin English committed
829
    /* sanity reset */
830 831
    wmm->dwStatus = MCI_MODE_STOP;

832
    TRACE("wmm->dwStatus=%d\n", wmm->dwStatus);
833

834
    if (lpParms && (dwFlags & MCI_NOTIFY)) {
835
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
836
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
837
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
838 839 840 841 842 843 844
    }
    return 0;
}

/**************************************************************************
 * 				MIDI_mciClose			[internal]
 */
845
static DWORD MIDI_mciClose(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
846 847
{
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
848

849
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
850

851
    if (wmm == NULL)	return MCIERR_INVALID_DEVICE_ID;
852

853 854 855
    if (wmm->dwStatus != MCI_MODE_STOP) {
	MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
    }
856

857 858 859
    wmm->nUseCount--;
    if (wmm->nUseCount == 0) {
	if (wmm->hFile != 0) {
860
	    mmioClose(wmm->hFile, 0);
861
	    wmm->hFile = 0;
862
	    TRACE("hFile closed !\n");
863
	}
864
	HeapFree(GetProcessHeap(), 0, wmm->tracks);
865 866 867
	HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
	HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
	HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
868
    } else {
869
	TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
870 871
	return MCIERR_INTERNAL;
    }
872

873
    if (lpParms && (dwFlags & MCI_NOTIFY)) {
874
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
875
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
876
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
877 878 879 880
    }
    return 0;
}

881 882 883
/**************************************************************************
 * 				MIDI_mciFindNextEvent		[internal]
 */
884
static MCI_MIDITRACK*	MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
885 886 887
{
    WORD		cnt, nt;
    MCI_MIDITRACK*	mmt;
888

889 890 891 892
    *hiPulse = 0xFFFFFFFFul;
    cnt = 0xFFFFu;
    for (nt = 0; nt < wmm->nTracks; nt++) {
	mmt = &wmm->tracks[nt];
893

894 895 896 897 898 899 900 901 902 903 904 905 906 907
	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]
 */
908
static DWORD MIDI_mciPlay(UINT wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
909
{
910 911
    DWORD		dwStartMS, dwEndMS;
    DWORD		dwRet = 0;
912 913 914 915
    WORD		doPlay, nt;
    MCI_MIDITRACK*	mmt;
    DWORD		hiPulse;
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
916

917
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
918

919
    if (wmm == NULL)	return MCIERR_INVALID_DEVICE_ID;
920

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

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

    if (!(dwFlags & MCI_WAIT)) {
936
	/** FIXME: I'm not 100% sure that wNotifyDeviceID is the right value in all cases ??? */
937
	return MCI_SendCommandAsync(wmm->wNotifyDeviceID, MCI_PLAY, dwFlags, (DWORD_PTR)lpParms, sizeof(LPMCI_PLAY_PARMS));
938
    }
939

940
    if (lpParms && (dwFlags & MCI_FROM)) {
941
	dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
942 943 944
    } else {
	dwStartMS = wmm->dwPositionMS;
    }
945

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

952
    TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
953

954 955 956
    /* init tracks */
    for (nt = 0; nt < wmm->nTracks; nt++) {
	mmt = &wmm->tracks[nt];
957

958 959 960 961 962 963 964 965 966
	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 */
    }
967

968
    dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);
969 970 971 972 973
    /*	dwRet = midiInOpen(&wmm->hMidi, MIDIMAPPER, 0L, 0L, CALLBACK_NULL);*/
    if (dwRet != MMSYSERR_NOERROR) {
	return dwRet;
    }

974 975 976 977 978
    wmm->dwPulse = 0;
    wmm->dwTempo = 500000;
    wmm->dwStatus = MCI_MODE_PLAY;
    wmm->dwPositionMS = 0;
    wmm->wStartedPlaying = FALSE;
979

980
    while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
981 982
	/* 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.
983 984
	 */
	while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
985

986
	doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= dwEndMS);
987

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

990 991
	if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
	    break;  /* no more event on tracks */
992

993 994 995
	/* if starting playing, then set StartTicks to the value it would have had
	 * if play had started at position 0
	 */
996 997 998
	if (doPlay && !wmm->wStartedPlaying) {
	    wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
	    wmm->wStartedPlaying = TRUE;
999
	    TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
1000
	}
1001

1002
	if (hiPulse > wmm->dwPulse) {
Eric Pouech's avatar
Eric Pouech committed
1003
	    wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
1004
	    if (doPlay) {
Eric Pouech's avatar
Eric Pouech committed
1005
		DWORD	togo = wmm->dwStartTicks + wmm->dwPositionMS;
1006
		DWORD	tc = GetTickCount();
1007

1008 1009
		TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
		TRACE("Wait until %u => %u ms\n",
1010
		      tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
1011
		if (tc < togo)
1012 1013 1014 1015
		    Sleep(togo - tc);
	    }
	    wmm->dwPulse = hiPulse;
	}
1016

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

1032
		    MIDI_mciReadWord(wmm, &twd);	/* == 0 */
1033
		    TRACE("Got sequence number %u\n", twd);
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
		}
		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);
1046 1047
		    static const char* const	info[8] = {"", "Text", "Copyright", "Seq/Trk name",
							   "Instrument", "Lyric", "Marker", "Cue-point"};
1048
		    WORD	idx = HIBYTE(LOWORD(mmt->dwEventData));
1049

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

1067
		    MIDI_mciReadByte(wmm, &bt);	/* == 0 */
1068
		    FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
1069 1070 1071 1072 1073 1074
		}
		break;
	    case 0x21:
		/* MIDI port (pp) */
		if (FIXME_ON(mcimidi)) {
		    BYTE	bt;
1075

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

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

1108 1109 1110 1111 1112
		    MIDI_mciReadByte(wmm, &h);
		    MIDI_mciReadByte(wmm, &m);
		    MIDI_mciReadByte(wmm, &s);
		    MIDI_mciReadByte(wmm, &f);
		    MIDI_mciReadByte(wmm, &ff);
1113
		    FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1114 1115
		}
		break;
1116
	    case 0x58: /* file rhythm */
1117 1118
		if (TRACE_ON(mcimidi)) {
		    BYTE	num, den, cpmc, _32npqn;
1119 1120

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

1125
		    TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1126 1127 1128 1129 1130
		}
		break;
	    case 0x59: /* key signature */
		if (TRACE_ON(mcimidi)) {
		    BYTE	sf, mm;
1131 1132 1133 1134 1135 1136

		    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);
1137
		    else 		TRACE("Key of C\n");
1138
		    TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1139 1140 1141
		}
		break;
	    default:
1142
		WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1143 1144 1145 1146 1147
		break;
	    }
	    break;
	default:
	    if (doPlay) {
1148
		dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1149 1150 1151 1152
	    } else {
		switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
		case MIDI_NOTEON:
		case MIDI_NOTEOFF:
1153
		    dwRet = 0;
1154 1155
		    break;
		default:
1156
		    dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1157 1158 1159 1160 1161 1162
		}
	    }
	}
	mmt->dwIndex += mmt->wEventLength;
	if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
	    mmt->wStatus = 0;
1163 1164
	}
	if (mmt->wStatus) {
1165 1166 1167
	    MIDI_mciReadNextEvent(wmm, mmt);
	}
    }
1168

1169
    midiOutReset((HMIDIOUT)wmm->hMidi);
1170

1171
    dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1172 1173
    /* to restart playing at beginning when it's over */
    wmm->dwPositionMS = 0;
1174

1175
    if (lpParms && (dwFlags & MCI_NOTIFY)) {
1176
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1177
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1178
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1179
    }
1180 1181 1182

    wmm->dwStatus = MCI_MODE_STOP;
    return dwRet;
1183 1184 1185 1186 1187
}

/**************************************************************************
 * 				MIDI_mciRecord			[internal]
 */
1188
static DWORD MIDI_mciRecord(UINT wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
1189 1190
{
    int			start, end;
1191
    MIDIHDR		midiHdr;
1192
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1193

1194
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1195

1196
    if (wmm == 0)	return MCIERR_INVALID_DEVICE_ID;
1197

1198
    if (wmm->hFile == 0) {
1199
	WARN("Can't find file=%s!\n", debugstr_w(wmm->lpstrElementName));
1200 1201 1202 1203
	return MCIERR_FILE_NOT_FOUND;
    }
    start = 1; 		end = 99999;
    if (lpParms && (dwFlags & MCI_FROM)) {
1204
	start = lpParms->dwFrom;
1205
	TRACE("MCI_FROM=%d\n", start);
1206 1207 1208
    }
    if (lpParms && (dwFlags & MCI_TO)) {
	end = lpParms->dwTo;
1209
	TRACE("MCI_TO=%d\n", end);
1210
    }
1211
    midiHdr.lpData = HeapAlloc(GetProcessHeap(), 0, 1200);
1212
    if (!midiHdr.lpData)
1213
	return MCIERR_OUT_OF_MEMORY;
1214 1215 1216
    midiHdr.dwBufferLength = 1024;
    midiHdr.dwUser = 0L;
    midiHdr.dwFlags = 0L;
1217
    midiInPrepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1218
    TRACE("After MIDM_PREPARE\n");
1219
    wmm->dwStatus = MCI_MODE_RECORD;
1220
    /* FIXME: there is no buffer added */
1221
    while (wmm->dwStatus != MCI_MODE_STOP) {
1222
	TRACE("wmm->dwStatus=%p %d\n",
1223
	      &wmm->dwStatus, wmm->dwStatus);
1224
	midiHdr.dwBytesRecorded = 0;
1225
	midiInStart((HMIDIIN)wmm->hMidi);
1226
	TRACE("midiInStart => dwBytesRecorded=%u\n", midiHdr.dwBytesRecorded);
1227
	if (midiHdr.dwBytesRecorded == 0) break;
1228
    }
1229
    TRACE("Before MIDM_UNPREPARE\n");
1230
    midiInUnprepareHeader((HMIDIIN)wmm->hMidi, &midiHdr, sizeof(MIDIHDR));
1231
    TRACE("After MIDM_UNPREPARE\n");
1232 1233
    HeapFree(GetProcessHeap(), 0, midiHdr.lpData);
    midiHdr.lpData = NULL;
1234 1235
    wmm->dwStatus = MCI_MODE_STOP;
    if (lpParms && (dwFlags & MCI_NOTIFY)) {
1236
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1237
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1238
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1239 1240 1241 1242 1243 1244 1245
    }
    return 0;
}

/**************************************************************************
 * 				MIDI_mciPause			[internal]
 */
1246
static DWORD MIDI_mciPause(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1247 1248
{
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1249

1250
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1251

1252
    if (wmm == NULL)	return MCIERR_INVALID_DEVICE_ID;
1253

1254 1255 1256 1257
    if (wmm->dwStatus == MCI_MODE_PLAY) {
	/* stop all notes */
	unsigned chn;
	for (chn = 0; chn < 16; chn++)
1258
	    midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1259
	wmm->dwStatus = MCI_MODE_PAUSE;
1260
    }
1261
    if (lpParms && (dwFlags & MCI_NOTIFY)) {
1262
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1263
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1264
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1265
    }
1266

1267 1268 1269 1270 1271 1272
    return 0;
}

/**************************************************************************
 * 				MIDI_mciResume			[internal]
 */
1273
static DWORD MIDI_mciResume(UINT wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1274 1275
{
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1276

1277
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1278

1279
    if (wmm == NULL)	return MCIERR_INVALID_DEVICE_ID;
1280

1281 1282 1283
    if (wmm->dwStatus == MCI_MODE_PAUSE) {
	wmm->wStartedPlaying = FALSE;
	wmm->dwStatus = MCI_MODE_PLAY;
1284
    }
1285
    if (lpParms && (dwFlags & MCI_NOTIFY)) {
1286
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1287
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1288
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1289 1290 1291 1292 1293 1294 1295
    }
    return 0;
}

/**************************************************************************
 * 				MIDI_mciSet			[internal]
 */
1296
static DWORD MIDI_mciSet(UINT wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
1297 1298
{
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1299

1300
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1301

1302 1303
    if (lpParms == NULL)	return MCIERR_NULL_PARAMETER_BLOCK;
    if (wmm == NULL)		return MCIERR_INVALID_DEVICE_ID;
1304

1305 1306 1307
    if (dwFlags & MCI_SET_TIME_FORMAT) {
	switch (lpParms->dwTimeFormat) {
	case MCI_FORMAT_MILLISECONDS:
1308
	    TRACE("MCI_FORMAT_MILLISECONDS !\n");
1309 1310 1311
	    wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
	    break;
	case MCI_FORMAT_SMPTE_24:
1312
	    TRACE("MCI_FORMAT_SMPTE_24 !\n");
1313 1314 1315
	    wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
	    break;
	case MCI_FORMAT_SMPTE_25:
1316
	    TRACE("MCI_FORMAT_SMPTE_25 !\n");
1317 1318 1319
	    wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
	    break;
	case MCI_FORMAT_SMPTE_30:
1320
	    TRACE("MCI_FORMAT_SMPTE_30 !\n");
1321 1322 1323
	    wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
	    break;
	default:
1324
	    WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1325 1326 1327 1328
	    return MCIERR_BAD_TIME_FORMAT;
	}
    }
    if (dwFlags & MCI_SET_VIDEO) {
1329
	TRACE("No support for video !\n");
1330 1331 1332
	return MCIERR_UNSUPPORTED_FUNCTION;
    }
    if (dwFlags & MCI_SET_DOOR_OPEN) {
1333
	TRACE("No support for door open !\n");
1334 1335 1336
	return MCIERR_UNSUPPORTED_FUNCTION;
    }
    if (dwFlags & MCI_SET_DOOR_CLOSED) {
1337
	TRACE("No support for door close !\n");
1338 1339 1340 1341
	return MCIERR_UNSUPPORTED_FUNCTION;
    }
    if (dwFlags & MCI_SET_AUDIO) {
	if (dwFlags & MCI_SET_ON) {
1342
	    TRACE("MCI_SET_ON audio !\n");
1343
	} else if (dwFlags & MCI_SET_OFF) {
1344
	    TRACE("MCI_SET_OFF audio !\n");
1345
	} else {
1346
	    WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1347 1348
	    return MCIERR_BAD_INTEGER;
	}
1349

1350 1351 1352 1353 1354
	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;
1355
        default:                        WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1356
        }
1357
    }
1358

1359
    if (dwFlags & MCI_SEQ_SET_MASTER)
1360
	TRACE("MCI_SEQ_SET_MASTER !\n");
1361
    if (dwFlags & MCI_SEQ_SET_SLAVE)
1362
	TRACE("MCI_SEQ_SET_SLAVE !\n");
1363
    if (dwFlags & MCI_SEQ_SET_OFFSET)
1364
	TRACE("MCI_SEQ_SET_OFFSET !\n");
1365
    if (dwFlags & MCI_SEQ_SET_PORT)
1366
	TRACE("MCI_SEQ_SET_PORT !\n");
1367
    if (dwFlags & MCI_SEQ_SET_TEMPO)
1368
	TRACE("MCI_SEQ_SET_TEMPO !\n");
1369 1370 1371 1372 1373 1374
    return 0;
}

/**************************************************************************
 * 				MIDI_mciStatus			[internal]
 */
1375
static DWORD MIDI_mciStatus(UINT wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1376 1377
{
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1378 1379
    DWORD		ret = 0;

1380
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1381

1382 1383
    if (lpParms == NULL) 	return MCIERR_NULL_PARAMETER_BLOCK;
    if (wmm == NULL)		return MCIERR_INVALID_DEVICE_ID;
1384

1385 1386 1387 1388 1389
    if (dwFlags & MCI_STATUS_ITEM) {
	switch (lpParms->dwItem) {
	case MCI_STATUS_CURRENT_TRACK:
	    /* FIXME in Format 2 */
	    lpParms->dwReturn = 1;
1390
	    TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1391 1392 1393 1394 1395
	    break;
	case MCI_STATUS_LENGTH:
	    if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
		if (lpParms->dwTrack >= wmm->nTracks)
		    return MCIERR_BAD_INTEGER;
1396
		/* FIXME: this is wrong if there is a tempo change inside the file */
1397 1398 1399 1400 1401
		lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
	    } else {
		lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
	    }
	    lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1402
	    TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1403 1404
	    break;
	case MCI_STATUS_MODE:
1405 1406 1407
	    TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
 	    lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
	    ret = MCI_RESOURCE_RETURNED;
1408 1409
	    break;
	case MCI_STATUS_MEDIA_PRESENT:
1410
	    TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1411 1412
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1413 1414 1415
	    break;
	case MCI_STATUS_NUMBER_OF_TRACKS:
	    lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1416
	    TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1417 1418 1419
	    break;
	case MCI_STATUS_POSITION:
	    /* FIXME: do I need to use MCI_TRACK ? */
1420
	    lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1421
							   (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1422
	    TRACE("MCI_STATUS_POSITION %s => %lu\n",
1423 1424 1425
		  (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
	    break;
	case MCI_STATUS_READY:
1426 1427 1428 1429
	    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));
1430 1431
	    break;
	case MCI_STATUS_TIME_FORMAT:
1432
	    lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1433 1434
	    TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
	    ret = MCI_RESOURCE_RETURNED;
1435 1436
	    break;
	case MCI_SEQ_STATUS_DIVTYPE:
1437
	    TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1438 1439 1440 1441 1442 1443
	    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 */
1444
		default:	FIXME("There is a bad bad programmer\n");
1445 1446 1447 1448
		}
	    } else {
		lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
	    }
1449 1450
	    lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
	    ret = MCI_RESOURCE_RETURNED;
1451 1452
	    break;
	case MCI_SEQ_STATUS_MASTER:
1453
	    TRACE("MCI_SEQ_STATUS_MASTER !\n");
1454 1455 1456
	    lpParms->dwReturn = 0;
	    break;
	case MCI_SEQ_STATUS_SLAVE:
1457
	    TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1458 1459 1460
	    lpParms->dwReturn = 0;
	    break;
	case MCI_SEQ_STATUS_OFFSET:
1461
	    TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1462 1463 1464
	    lpParms->dwReturn = 0;
	    break;
	case MCI_SEQ_STATUS_PORT:
1465
	    TRACE("MCI_SEQ_STATUS_PORT (%u)!\n", wmm->wDevID);
Eric Pouech's avatar
Eric Pouech committed
1466
	    lpParms->dwReturn = MIDI_MAPPER;
1467 1468
	    break;
	case MCI_SEQ_STATUS_TEMPO:
1469
	    TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1470 1471 1472
	    lpParms->dwReturn = wmm->dwTempo;
	    break;
	default:
Anders Jonsson's avatar
Anders Jonsson committed
1473
	    FIXME("Unknown command %08X !\n", lpParms->dwItem);
1474 1475 1476
	    return MCIERR_UNRECOGNIZED_COMMAND;
	}
    } else {
1477
	WARN("No Status-Item!\n");
1478 1479 1480
	return MCIERR_UNRECOGNIZED_COMMAND;
    }
    if (dwFlags & MCI_NOTIFY) {
1481
	TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1482
	mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1483
			wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1484
    }
1485
    return ret;
1486 1487 1488 1489 1490
}

/**************************************************************************
 * 				MIDI_mciGetDevCaps		[internal]
 */
1491
static DWORD MIDI_mciGetDevCaps(UINT wDevID, DWORD dwFlags,
1492 1493 1494
				LPMCI_GETDEVCAPS_PARMS lpParms)
{
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1495 1496
    DWORD		ret;

1497
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1498

1499 1500
    if (lpParms == NULL) 	return MCIERR_NULL_PARAMETER_BLOCK;
    if (wmm == NULL)		return MCIERR_INVALID_DEVICE_ID;
1501

1502 1503 1504
    if (dwFlags & MCI_GETDEVCAPS_ITEM) {
	switch (lpParms->dwItem) {
	case MCI_GETDEVCAPS_DEVICE_TYPE:
1505
	    TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1506 1507
	    lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
	    ret = MCI_RESOURCE_RETURNED;
1508 1509
	    break;
	case MCI_GETDEVCAPS_HAS_AUDIO:
1510
	    TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1511 1512
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1513 1514
	    break;
	case MCI_GETDEVCAPS_HAS_VIDEO:
1515
	    TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1516 1517
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
	    ret = MCI_RESOURCE_RETURNED;
1518 1519
	    break;
	case MCI_GETDEVCAPS_USES_FILES:
1520
	    TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1521 1522
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1523 1524
	    break;
	case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1525
	    TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1526 1527
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1528 1529
	    break;
	case MCI_GETDEVCAPS_CAN_EJECT:
1530
	    TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1531 1532
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
	    ret = MCI_RESOURCE_RETURNED;
1533 1534
	    break;
	case MCI_GETDEVCAPS_CAN_PLAY:
1535
	    TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1536 1537
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1538 1539
	    break;
	case MCI_GETDEVCAPS_CAN_RECORD:
1540
	    TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1541 1542
	    lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
	    ret = MCI_RESOURCE_RETURNED;
1543 1544
	    break;
	case MCI_GETDEVCAPS_CAN_SAVE:
1545
	    TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1546 1547
	    lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
	    ret = MCI_RESOURCE_RETURNED;
1548 1549
	    break;
	default:
1550
	    FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1551 1552 1553
	    return MCIERR_UNRECOGNIZED_COMMAND;
	}
    } else {
1554
	WARN("No GetDevCaps-Item !\n");
1555 1556
	return MCIERR_UNRECOGNIZED_COMMAND;
    }
1557
    return ret;
1558 1559 1560 1561 1562
}

/**************************************************************************
 * 				MIDI_mciInfo			[internal]
 */
1563
static DWORD MIDI_mciInfo(UINT wDevID, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1564
{
1565
    LPCWSTR		str = 0;
1566
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1567
    DWORD		ret = 0;
1568
    static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1569

1570
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1571

1572 1573 1574 1575
    if (lpParms == NULL || lpParms->lpstrReturn == NULL)
	return MCIERR_NULL_PARAMETER_BLOCK;
    if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;

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

1578
    switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1579 1580 1581 1582
    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;
1583
    default:
1584
	WARN("Don't know this info command (%u)\n", dwFlags);
1585
	return MCIERR_UNRECOGNIZED_COMMAND;
1586
    }
1587
    if (str) {
1588 1589
	if (lpParms->dwRetSize <= strlenW(str)) {
	    lstrcpynW(lpParms->lpstrReturn, str, lpParms->dwRetSize - 1);
1590 1591
	    ret = MCIERR_PARAM_OVERFLOW;
	} else {
1592
	    strcpyW(lpParms->lpstrReturn, str);
1593
	}
1594 1595 1596 1597
    } else {
	*lpParms->lpstrReturn = 0;
    }
    return ret;
1598 1599 1600 1601 1602
}

/**************************************************************************
 * 				MIDI_mciSeek			[internal]
 */
1603
static DWORD MIDI_mciSeek(UINT wDevID, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1604 1605 1606
{
    DWORD		ret = 0;
    WINE_MCIMIDI*	wmm = MIDI_mciGetOpenDev(wDevID);
1607

1608
    TRACE("(%04X, %08X, %p);\n", wDevID, dwFlags, lpParms);
1609

1610 1611 1612 1613 1614 1615
    if (lpParms == NULL) {
	ret = MCIERR_NULL_PARAMETER_BLOCK;
    } else if (wmm == NULL) {
	ret = MCIERR_INVALID_DEVICE_ID;
    } else {
	MIDI_mciStop(wDevID, MCI_WAIT, 0);
1616

1617 1618 1619
	if (dwFlags & MCI_SEEK_TO_START) {
	    wmm->dwPositionMS = 0;
	} else if (dwFlags & MCI_SEEK_TO_END) {
1620
	    wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1621 1622 1623
	} else if (dwFlags & MCI_TO) {
	    wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
	} else {
1624
	    WARN("dwFlag doesn't tell where to seek to...\n");
1625 1626
	    return MCIERR_MISSING_PARAMETER;
	}
1627

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

1630
	if (dwFlags & MCI_NOTIFY) {
1631
	    TRACE("MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
1632
	    mciDriverNotify(HWND_32(LOWORD(lpParms->dwCallback)),
1633
			    wmm->wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
1634 1635
	}
    }
1636 1637
    return ret;
}
1638 1639 1640 1641 1642 1643

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

/**************************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
1644
 * 				DriverProc (MCISEQ.@)
1645
 */
1646 1647
LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
                                    LPARAM dwParam1, LPARAM dwParam2)
1648 1649 1650 1651 1652 1653 1654
{
    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;
1655
    case DRV_CONFIGURE:		MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1656 1657
    case DRV_INSTALL:		return DRVCNF_RESTART;
    case DRV_REMOVE:		return DRVCNF_RESTART;
1658
    case DRV_OPEN:		return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1659
    case DRV_CLOSE:		return MIDI_drvClose(dwDevID);
1660 1661 1662 1663 1664
    }

    if (dwDevID == 0xFFFFFFFF) return MCIERR_UNSUPPORTED_FUNCTION;

    switch (wMsg) {
1665
    case MCI_OPEN_DRIVER:	return MIDI_mciOpen      (dwDevID, dwParam1, (LPMCI_OPEN_PARMSW)     dwParam2);
1666 1667 1668 1669 1670 1671 1672 1673 1674
    case MCI_CLOSE_DRIVER:	return MIDI_mciClose     (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_PLAY:		return MIDI_mciPlay      (dwDevID, dwParam1, (LPMCI_PLAY_PARMS)      dwParam2);
    case MCI_RECORD:		return MIDI_mciRecord    (dwDevID, dwParam1, (LPMCI_RECORD_PARMS)    dwParam2);
    case MCI_STOP:		return MIDI_mciStop      (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_SET:		return MIDI_mciSet       (dwDevID, dwParam1, (LPMCI_SET_PARMS)       dwParam2);
    case MCI_PAUSE:		return MIDI_mciPause     (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_RESUME:		return MIDI_mciResume    (dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)   dwParam2);
    case MCI_STATUS:		return MIDI_mciStatus    (dwDevID, dwParam1, (LPMCI_STATUS_PARMS)    dwParam2);
    case MCI_GETDEVCAPS:	return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1675
    case MCI_INFO:		return MIDI_mciInfo      (dwDevID, dwParam1, (LPMCI_INFO_PARMSW)     dwParam2);
1676
    case MCI_SEEK:		return MIDI_mciSeek      (dwDevID, dwParam1, (LPMCI_SEEK_PARMS)      dwParam2);
1677
    /* commands that should be supported */
1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
    case MCI_LOAD:
    case MCI_SAVE:
    case MCI_FREEZE:
    case MCI_PUT:
    case MCI_REALIZE:
    case MCI_UNFREEZE:
    case MCI_UPDATE:
    case MCI_WHERE:
    case MCI_STEP:
    case MCI_SPIN:
    case MCI_ESCAPE:
    case MCI_COPY:
    case MCI_CUT:
    case MCI_DELETE:
    case MCI_PASTE:
1693
	WARN("Unsupported command [%u]\n", wMsg);
1694 1695
	break;
    /* commands that should report an error */
1696
    case MCI_WINDOW:
1697
	TRACE("Unsupported command [%u]\n", wMsg);
1698 1699 1700
	break;
    case MCI_OPEN:
    case MCI_CLOSE:
1701
	FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1702
	break;
1703
    default:
1704
	TRACE("Sending msg [%u] to default driver proc\n", wMsg);
1705
	return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1706 1707 1708
    }
    return MCIERR_UNRECOGNIZED_COMMAND;
}