3c0c685963aec02be7bdd4d838b139fbf4c66ffc
[reactos.git] / reactos / dll / win32 / mciseq / mcimidi.c
1 /*
2 * Sample MIDI Wine Driver for Linux
3 *
4 * Copyright 1994 Martin Ayotte
5 * Copyright 1999 Eric Pouech
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * 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 /* TODO:
23 * + implement it correctly
24 * + finish asynchronous commands
25 * + better implement non waiting command (without the MCI_WAIT flag).
26 */
27
28 #define WIN32_NO_STATUS
29
30 //#include <stdlib.h>
31 #include <stdarg.h>
32 //#include <stdio.h>
33 //#include <string.h>
34
35 #include <windef.h>
36 //#include "winbase.h"
37 //#include "wingdi.h"
38 #include <winuser.h>
39 #include <wownt32.h>
40 #include <mmddk.h>
41 #include <wine/debug.h>
42 #include <wine/unicode.h>
43
44 WINE_DEFAULT_DEBUG_CHANNEL(mcimidi);
45
46 #define MIDI_NOTEOFF 0x80
47 #define MIDI_NOTEON 0x90
48
49 typedef struct {
50 DWORD dwFirst; /* offset in file of track */
51 DWORD dwLast; /* number of bytes in file of track */
52 DWORD dwIndex; /* current index in file (dwFirst <= dwIndex < dwLast) */
53 DWORD dwLength; /* number of pulses in this track */
54 DWORD dwEventPulse; /* current pulse # (event) pointed by dwIndex */
55 DWORD dwEventData; /* current data (event) pointed by dwIndex */
56 WORD wEventLength; /* current length (event) pointed by dwIndex */
57 WORD wStatus : 1, /* 1 : playing, 0 : done */
58 wTrackNr : 7,
59 wLastCommand : 8; /* last MIDI command on track */
60 } MCI_MIDITRACK;
61
62 typedef struct tagWINE_MCIMIDI {
63 UINT wDevID; /* the MCI one */
64 HMIDI hMidi;
65 int nUseCount; /* Incremented for each shared open */
66 HANDLE hCallback; /* Callback handle for pending notification */
67 HANDLE hThread; /* Player thread */
68 HMMIO hFile; /* mmio file handle open as Element */
69 LPWSTR lpstrElementName; /* Name of file (if any) */
70 LPWSTR lpstrCopyright;
71 LPWSTR lpstrName;
72 WORD wPort; /* the WINMM device unit */
73 WORD dwStatus; /* one from MCI_MODE_xxxx */
74 DWORD dwMciTimeFormat; /* One of the supported MCI_FORMAT_xxxx */
75 WORD wFormat; /* Format of MIDI hFile (0, 1 or 2) */
76 WORD nTracks; /* Number of tracks in hFile */
77 WORD nDivision; /* Number of division in hFile PPQN or SMPTE */
78 WORD wStartedPlaying;
79 DWORD dwTempo; /* Tempo (# of 1/4 note per second */
80 MCI_MIDITRACK* tracks; /* Content of each track */
81 DWORD dwPulse;
82 DWORD dwPositionMS;
83 DWORD dwEndMS;
84 DWORD dwStartTicks;
85 } WINE_MCIMIDI;
86
87 /*======================================================================*
88 * MCI MIDI implementation *
89 *======================================================================*/
90
91 static DWORD mmr2mci(DWORD ret)
92 {
93 switch (ret) {
94 case MMSYSERR_ALLOCATED:
95 return MCIERR_SEQ_PORT_INUSE;
96 case MMSYSERR_NOMEM:
97 return MCIERR_OUT_OF_MEMORY;
98 case MMSYSERR_BADDEVICEID: /* wine*.drv disabled */
99 return MCIERR_SEQ_PORT_NONEXISTENT;
100 case MIDIERR_INVALIDSETUP: /* from midimap.dll without snd-seq module */
101 return MCIERR_SEQ_PORT_MAPNODEVICE;
102 default:
103 return ret;
104 }
105 }
106
107 static DWORD MIDI_mciResume(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms);
108
109 /**************************************************************************
110 * MIDI_drvOpen [internal]
111 */
112 static DWORD MIDI_drvOpen(LPCWSTR str, LPMCI_OPEN_DRIVER_PARMSW modp)
113 {
114 WINE_MCIMIDI* wmm;
115
116 if (!modp) return 0xFFFFFFFF;
117
118 wmm = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WINE_MCIMIDI));
119
120 if (!wmm)
121 return 0;
122
123 wmm->wDevID = modp->wDeviceID;
124 mciSetDriverData(wmm->wDevID, (DWORD_PTR)wmm);
125 modp->wCustomCommandTable = MCI_NO_COMMAND_TABLE;
126 modp->wType = MCI_DEVTYPE_SEQUENCER;
127 return modp->wDeviceID;
128 }
129
130 /**************************************************************************
131 * MCIMIDI_drvClose [internal]
132 */
133 static DWORD MIDI_drvClose(DWORD dwDevID)
134 {
135 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(dwDevID);
136
137 if (wmm) {
138 HeapFree(GetProcessHeap(), 0, wmm);
139 mciSetDriverData(dwDevID, 0);
140 return 1;
141 }
142 return (dwDevID == 0xFFFFFFFF) ? 1 : 0;
143 }
144
145 /**************************************************************************
146 * MIDI_mciGetOpenDev [internal]
147 */
148 static WINE_MCIMIDI* MIDI_mciGetOpenDev(MCIDEVICEID wDevID, UINT wMsg)
149 {
150 WINE_MCIMIDI* wmm = (WINE_MCIMIDI*)mciGetDriverData(wDevID);
151
152 if (wmm == NULL || ((wmm->nUseCount == 0) ^ (wMsg == MCI_OPEN_DRIVER))) {
153 WARN("Invalid wDevID=%u\n", wDevID);
154 return 0;
155 }
156 return wmm;
157 }
158
159 /**************************************************************************
160 * MIDI_mciNotify [internal]
161 *
162 * Notifications in MCI work like a 1-element queue.
163 * Each new notification request supersedes the previous one.
164 * This affects Play and Record; other commands are immediate.
165 */
166 static void MIDI_mciNotify(DWORD_PTR hWndCallBack, WINE_MCIMIDI* wmm, UINT wStatus)
167 {
168 /* We simply save one parameter by not passing the wDevID local
169 * to the command. They are the same (via mciGetDriverData).
170 */
171 MCIDEVICEID wDevID = wmm->wDevID;
172 HANDLE old = InterlockedExchangePointer(&wmm->hCallback, NULL);
173 if (old) mciDriverNotify(old, wDevID, MCI_NOTIFY_SUPERSEDED);
174 mciDriverNotify(HWND_32(LOWORD(hWndCallBack)), wDevID, wStatus);
175 }
176
177 /**************************************************************************
178 * MIDI_mciReadByte [internal]
179 */
180 static DWORD MIDI_mciReadByte(WINE_MCIMIDI* wmm, BYTE *lpbyt)
181 {
182 DWORD ret = 0;
183
184 if (mmioRead(wmm->hFile, (HPSTR)lpbyt, sizeof(BYTE)) != (long)sizeof(BYTE)) {
185 WARN("Error reading wmm=%p\n", wmm);
186 ret = MCIERR_INVALID_FILE;
187 }
188
189 return ret;
190 }
191
192 /**************************************************************************
193 * MIDI_mciReadWord [internal]
194 */
195 static DWORD MIDI_mciReadWord(WINE_MCIMIDI* wmm, LPWORD lpw)
196 {
197 BYTE hibyte, lobyte;
198 DWORD ret = MCIERR_INVALID_FILE;
199
200 if (MIDI_mciReadByte(wmm, &hibyte) == 0 &&
201 MIDI_mciReadByte(wmm, &lobyte) == 0) {
202 *lpw = ((WORD)hibyte << 8) + lobyte;
203 ret = 0;
204 }
205 return ret;
206 }
207
208 /**************************************************************************
209 * MIDI_mciReadLong [internal]
210 */
211 static DWORD MIDI_mciReadLong(WINE_MCIMIDI* wmm, LPDWORD lpdw)
212 {
213 WORD hiword, loword;
214 DWORD ret = MCIERR_INVALID_FILE;
215
216 if (MIDI_mciReadWord(wmm, &hiword) == 0 &&
217 MIDI_mciReadWord(wmm, &loword) == 0) {
218 *lpdw = MAKELONG(loword, hiword);
219 ret = 0;
220 }
221 return ret;
222 }
223
224 /**************************************************************************
225 * MIDI_mciReadVaryLen [internal]
226 */
227 static WORD MIDI_mciReadVaryLen(WINE_MCIMIDI* wmm, LPDWORD lpdw)
228 {
229 BYTE byte;
230 DWORD value = 0;
231 WORD len = 0;
232
233 do {
234 if (MIDI_mciReadByte(wmm, &byte) != 0) {
235 return 0;
236 }
237 value = (value << 7) + (byte & 0x7F);
238 len++;
239 } while (byte & 0x80);
240 *lpdw = value;
241 return len;
242 }
243
244 /**************************************************************************
245 * MIDI_mciReadNextEvent [internal]
246 */
247 static DWORD MIDI_mciReadNextEvent(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
248 {
249 BYTE b1, b2 = 0, b3;
250 WORD hw = 0;
251 DWORD evtPulse;
252 DWORD evtLength;
253 DWORD tmp;
254
255 if (mmioSeek(wmm->hFile, mmt->dwIndex, SEEK_SET) != mmt->dwIndex) {
256 WARN("Can't seek at %08X\n", mmt->dwIndex);
257 return MCIERR_INVALID_FILE;
258 }
259 evtLength = MIDI_mciReadVaryLen(wmm, &evtPulse) + 1; /* > 0 */
260 MIDI_mciReadByte(wmm, &b1);
261 switch (b1) {
262 case 0xF0:
263 case 0xF7:
264 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
265 evtLength += tmp;
266 break;
267 case 0xFF:
268 MIDI_mciReadByte(wmm, &b2); evtLength++;
269
270 evtLength += MIDI_mciReadVaryLen(wmm, &tmp);
271 if (evtLength >= 0x10000u) {
272 /* this limitation shouldn't be a problem */
273 WARN("Ouch !! Implementation limitation to 64k bytes for a MIDI event is overflowed\n");
274 hw = 0xFFFF;
275 } else {
276 hw = LOWORD(evtLength);
277 }
278 evtLength += tmp;
279 break;
280 default:
281 if (b1 & 0x80) { /* use running status ? */
282 mmt->wLastCommand = b1;
283 MIDI_mciReadByte(wmm, &b2); evtLength++;
284 } else {
285 b2 = b1;
286 b1 = mmt->wLastCommand;
287 }
288 switch ((b1 >> 4) & 0x07) {
289 case 0: case 1: case 2: case 3: case 6:
290 MIDI_mciReadByte(wmm, &b3); evtLength++;
291 hw = b3;
292 break;
293 case 4: case 5:
294 break;
295 case 7:
296 WARN("Strange indeed b1=0x%02x\n", b1);
297 }
298 break;
299 }
300 if (mmt->dwIndex + evtLength > mmt->dwLast)
301 return MCIERR_INTERNAL;
302
303 mmt->dwEventPulse += evtPulse;
304 mmt->dwEventData = (hw << 16) + (b2 << 8) + b1;
305 mmt->wEventLength = evtLength;
306
307 /*
308 TRACE("[%u] => pulse=%08x(%08x), data=%08x, length=%u\n",
309 mmt->wTrackNr, mmt->dwEventPulse, evtPulse,
310 mmt->dwEventData, mmt->wEventLength);
311 */
312 return 0;
313 }
314
315 /**************************************************************************
316 * MIDI_mciReadMTrk [internal]
317 */
318 static DWORD MIDI_mciReadMTrk(WINE_MCIMIDI* wmm, MCI_MIDITRACK* mmt)
319 {
320 DWORD toberead;
321 FOURCC fourcc;
322
323 if (mmioRead(wmm->hFile, (HPSTR)&fourcc, (long)sizeof(FOURCC)) !=
324 (long)sizeof(FOURCC)) {
325 return MCIERR_INVALID_FILE;
326 }
327
328 if (fourcc != mmioFOURCC('M', 'T', 'r', 'k')) {
329 WARN("Can't synchronize on 'MTrk' !\n");
330 return MCIERR_INVALID_FILE;
331 }
332
333 if (MIDI_mciReadLong(wmm, &toberead) != 0) {
334 return MCIERR_INVALID_FILE;
335 }
336 mmt->dwFirst = mmioSeek(wmm->hFile, 0, SEEK_CUR); /* >= 0 */
337 mmt->dwLast = mmt->dwFirst + toberead;
338
339 /* compute # of pulses in this track */
340 mmt->dwIndex = mmt->dwFirst;
341 mmt->dwEventPulse = 0;
342
343 while (MIDI_mciReadNextEvent(wmm, mmt) == 0 && LOWORD(mmt->dwEventData) != 0x2FFF) {
344 char buf[1024];
345 WORD len;
346
347 mmt->dwIndex += mmt->wEventLength;
348
349 switch (LOWORD(mmt->dwEventData)) {
350 case 0x02FF:
351 case 0x03FF:
352 len = mmt->wEventLength - HIWORD(mmt->dwEventData);
353 if (len >= sizeof(buf)) {
354 WARN("Buffer for text is too small (%u are needed)\n", len);
355 len = sizeof(buf) - 1;
356 }
357 if (mmioRead(wmm->hFile, buf, len) == len) {
358 buf[len] = 0; /* end string in case */
359 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
360 case 0x02:
361 if (wmm->lpstrCopyright) {
362 WARN("Two copyright notices (%s|%s)\n", debugstr_w(wmm->lpstrCopyright), buf);
363 HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
364 }
365 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
366 wmm->lpstrCopyright = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
367 MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrCopyright, len );
368 break;
369 case 0x03:
370 if (wmm->lpstrName) {
371 WARN("Two names (%s|%s)\n", debugstr_w(wmm->lpstrName), buf);
372 HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
373 } /* last name or name from last track wins */
374 len = MultiByteToWideChar( CP_ACP, 0, buf, -1, NULL, 0 );
375 wmm->lpstrName = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) );
376 MultiByteToWideChar( CP_ACP, 0, buf, -1, wmm->lpstrName, len );
377 break;
378 }
379 }
380 break;
381 }
382 }
383 mmt->dwLength = mmt->dwEventPulse;
384
385 TRACE("Track %u has %u bytes and %u pulses\n", mmt->wTrackNr, toberead, mmt->dwLength);
386
387 /* reset track data */
388 mmt->wStatus = 1; /* ok, playing */
389 mmt->dwIndex = mmt->dwFirst;
390 mmt->dwEventPulse = 0;
391
392 if (mmioSeek(wmm->hFile, 0, SEEK_CUR) != mmt->dwLast) {
393 WARN("Ouch, out of sync seek=%u track=%u\n",
394 mmioSeek(wmm->hFile, 0, SEEK_CUR), mmt->dwLast);
395 /* position at end of this track, to be ready to read next track */
396 mmioSeek(wmm->hFile, mmt->dwLast, SEEK_SET);
397 }
398
399 return 0;
400 }
401
402 /**************************************************************************
403 * MIDI_mciReadMThd [internal]
404 */
405 static DWORD MIDI_mciReadMThd(WINE_MCIMIDI* wmm, DWORD dwOffset)
406 {
407 DWORD toberead;
408 FOURCC fourcc;
409 WORD nt;
410
411 TRACE("(%p, %08X);\n", wmm, dwOffset);
412
413 if (mmioSeek(wmm->hFile, dwOffset, SEEK_SET) != dwOffset) {
414 WARN("Can't seek at %08X begin of 'MThd'\n", dwOffset);
415 return MCIERR_INVALID_FILE;
416 }
417 if (mmioRead(wmm->hFile, (HPSTR)&fourcc,
418 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC))
419 return MCIERR_INVALID_FILE;
420
421 if (fourcc != mmioFOURCC('M', 'T', 'h', 'd')) {
422 WARN("Can't synchronize on 'MThd' !\n");
423 return MCIERR_INVALID_FILE;
424 }
425
426 if (MIDI_mciReadLong(wmm, &toberead) != 0 || toberead < 3 * sizeof(WORD))
427 return MCIERR_INVALID_FILE;
428
429 if (MIDI_mciReadWord(wmm, &wmm->wFormat) != 0 ||
430 MIDI_mciReadWord(wmm, &wmm->nTracks) != 0 ||
431 MIDI_mciReadWord(wmm, &wmm->nDivision) != 0) {
432 return MCIERR_INVALID_FILE;
433 }
434
435 TRACE("toberead=0x%08X, wFormat=0x%04X nTracks=0x%04X nDivision=0x%04X\n",
436 toberead, wmm->wFormat, wmm->nTracks, wmm->nDivision);
437
438 /* MS doc says that the MIDI MCI time format must be put by default to the format
439 * stored in the MIDI file...
440 */
441 if (wmm->nDivision > 0x8000) {
442 /* eric.pouech@lemel.fr 98/11
443 * I did not check this very code (pulses are expressed as SMPTE sub-frames).
444 * In about 40 MB of MIDI files I have, none was SMPTE based...
445 * I'm just wondering if this is widely used :-). So, if someone has one of
446 * these files, I'd like to know about it.
447 */
448 FIXME("Handling SMPTE time in MIDI files has not been tested\n"
449 "Please report to comp.emulators.ms-windows.wine with MIDI file !\n");
450
451 switch (HIBYTE(wmm->nDivision)) {
452 case 0xE8: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24; break; /* -24 */
453 case 0xE7: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25; break; /* -25 */
454 case 0xE3: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
455 case 0xE2: wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30; break; /* -30 */
456 default:
457 WARN("Unsupported number of frames %d\n", -(char)HIBYTE(wmm->nDivision));
458 return MCIERR_INVALID_FILE;
459 }
460 switch (LOBYTE(wmm->nDivision)) {
461 case 4: /* MIDI Time Code */
462 case 8:
463 case 10:
464 case 80: /* SMPTE bit resolution */
465 case 100:
466 default:
467 WARN("Unsupported number of sub-frames %d\n", LOBYTE(wmm->nDivision));
468 return MCIERR_INVALID_FILE;
469 }
470 } else if (wmm->nDivision == 0) {
471 WARN("Number of division is 0, can't support that !!\n");
472 return MCIERR_INVALID_FILE;
473 } else {
474 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
475 }
476
477 switch (wmm->wFormat) {
478 case 0:
479 if (wmm->nTracks != 1) {
480 WARN("Got type 0 file whose number of track is not 1. Setting it to 1\n");
481 wmm->nTracks = 1;
482 }
483 break;
484 case 1:
485 case 2:
486 break;
487 default:
488 WARN("Handling MIDI files which format = %d is not (yet) supported\n"
489 "Please report with MIDI file !\n", wmm->wFormat);
490 return MCIERR_INVALID_FILE;
491 }
492
493 if (wmm->nTracks > 0x80) {
494 /* wTrackNr is 7 bits only */
495 FIXME("Truncating MIDI file with %u tracks\n", wmm->nTracks);
496 wmm->nTracks = 0x80;
497 }
498
499 if ((wmm->tracks = HeapAlloc(GetProcessHeap(), 0, sizeof(MCI_MIDITRACK) * wmm->nTracks)) == NULL) {
500 return MCIERR_OUT_OF_MEMORY;
501 }
502
503 toberead -= 3 * sizeof(WORD);
504 if (toberead > 0) {
505 TRACE("Size of MThd > 6, skipping %d extra bytes\n", toberead);
506 mmioSeek(wmm->hFile, toberead, SEEK_CUR);
507 }
508
509 for (nt = 0; nt < wmm->nTracks; nt++) {
510 wmm->tracks[nt].wTrackNr = nt;
511 if (MIDI_mciReadMTrk(wmm, &wmm->tracks[nt]) != 0) {
512 WARN("Can't read 'MTrk' header\n");
513 return MCIERR_INVALID_FILE;
514 }
515 }
516
517 wmm->dwTempo = 500000;
518
519 return 0;
520 }
521
522 /**************************************************************************
523 * MIDI_ConvertPulseToMS [internal]
524 */
525 static DWORD MIDI_ConvertPulseToMS(WINE_MCIMIDI* wmm, DWORD pulse)
526 {
527 DWORD ret = 0;
528
529 /* FIXME: this function may return false values since the tempo (wmm->dwTempo)
530 * may change during file playing
531 */
532 if (wmm->nDivision == 0) {
533 FIXME("Shouldn't happen. wmm->nDivision = 0\n");
534 } else if (wmm->nDivision > 0x8000) { /* SMPTE, unchecked FIXME? */
535 int nf = -(char)HIBYTE(wmm->nDivision); /* number of frames */
536 int nsf = LOBYTE(wmm->nDivision); /* number of sub-frames */
537 ret = (pulse * 1000) / (nf * nsf);
538 } else {
539 ret = (DWORD)((double)pulse * ((double)wmm->dwTempo / 1000) /
540 (double)wmm->nDivision);
541 }
542
543 /*
544 TRACE("pulse=%u tempo=%u division=%u=0x%04x => ms=%u\n",
545 pulse, wmm->dwTempo, wmm->nDivision, wmm->nDivision, ret);
546 */
547
548 return ret;
549 }
550
551 #define TIME_MS_IN_ONE_HOUR (60*60*1000)
552 #define TIME_MS_IN_ONE_MINUTE (60*1000)
553 #define TIME_MS_IN_ONE_SECOND (1000)
554
555 /**************************************************************************
556 * MIDI_ConvertTimeFormatToMS [internal]
557 */
558 static DWORD MIDI_ConvertTimeFormatToMS(WINE_MCIMIDI* wmm, DWORD val)
559 {
560 DWORD ret = 0;
561
562 switch (wmm->dwMciTimeFormat) {
563 case MCI_FORMAT_MILLISECONDS:
564 ret = val;
565 break;
566 case MCI_FORMAT_SMPTE_24:
567 ret =
568 (HIBYTE(HIWORD(val)) * 125) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
569 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
570 break;
571 case MCI_FORMAT_SMPTE_25:
572 ret =
573 HIBYTE(HIWORD(val)) * 40 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
574 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
575 break;
576 case MCI_FORMAT_SMPTE_30:
577 ret =
578 (HIBYTE(HIWORD(val)) * 100) / 3 + LOBYTE(HIWORD(val)) * TIME_MS_IN_ONE_SECOND +
579 HIBYTE(LOWORD(val)) * TIME_MS_IN_ONE_MINUTE + LOBYTE(LOWORD(val)) * TIME_MS_IN_ONE_HOUR;
580 break;
581 default:
582 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
583 }
584 /*
585 TRACE("val=%u=0x%08x [tf=%u] => ret=%u\n", val, val, wmm->dwMciTimeFormat, ret);
586 */
587 return ret;
588 }
589
590 /**************************************************************************
591 * MIDI_ConvertMSToTimeFormat [internal]
592 */
593 static DWORD MIDI_ConvertMSToTimeFormat(WINE_MCIMIDI* wmm, DWORD _val)
594 {
595 DWORD ret = 0, val = _val;
596 DWORD h, m, s, f;
597
598 switch (wmm->dwMciTimeFormat) {
599 case MCI_FORMAT_MILLISECONDS:
600 ret = val;
601 break;
602 case MCI_FORMAT_SMPTE_24:
603 case MCI_FORMAT_SMPTE_25:
604 case MCI_FORMAT_SMPTE_30:
605 h = val / TIME_MS_IN_ONE_HOUR;
606 m = (val -= h * TIME_MS_IN_ONE_HOUR) / TIME_MS_IN_ONE_MINUTE;
607 s = (val -= m * TIME_MS_IN_ONE_MINUTE) / TIME_MS_IN_ONE_SECOND;
608 switch (wmm->dwMciTimeFormat) {
609 case MCI_FORMAT_SMPTE_24:
610 /* one frame is 1000/24 val long, 1000/24 == 125/3 */
611 f = (val * 3) / 125; val -= (f * 125) / 3;
612 break;
613 case MCI_FORMAT_SMPTE_25:
614 /* one frame is 1000/25 ms long, 1000/25 == 40 */
615 f = val / 40; val -= f * 40;
616 break;
617 case MCI_FORMAT_SMPTE_30:
618 /* one frame is 1000/30 ms long, 1000/30 == 100/3 */
619 f = (val * 3) / 100; val -= (f * 100) / 3;
620 break;
621 default:
622 FIXME("There must be some bad bad programmer\n");
623 f = 0;
624 }
625 /* val contains the number of ms which cannot make a complete frame */
626 /* FIXME: is this correct ? programs seem to be happy with that */
627 ret = (f << 24) | (s << 16) | (m << 8) | (h << 0);
628 break;
629 default:
630 WARN("Bad time format %u!\n", wmm->dwMciTimeFormat);
631 }
632 /*
633 TRACE("val=%u [tf=%u] => ret=%u=0x%08x\n", _val, wmm->dwMciTimeFormat, ret, ret);
634 */
635 return ret;
636 }
637
638 /**************************************************************************
639 * MIDI_GetMThdLengthMS [internal]
640 */
641 static DWORD MIDI_GetMThdLengthMS(WINE_MCIMIDI* wmm)
642 {
643 WORD nt;
644 DWORD ret = 0;
645
646 for (nt = 0; nt < wmm->nTracks; nt++) {
647 if (wmm->wFormat == 2) {
648 ret += wmm->tracks[nt].dwLength;
649 } else if (wmm->tracks[nt].dwLength > ret) {
650 ret = wmm->tracks[nt].dwLength;
651 }
652 }
653 /* FIXME: this is wrong if there is a tempo change inside the file */
654 return MIDI_ConvertPulseToMS(wmm, ret);
655 }
656
657 /**************************************************************************
658 * MIDI_mciOpen [internal]
659 */
660 static DWORD MIDI_mciOpen(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_OPEN_PARMSW lpParms)
661 {
662 DWORD dwRet = 0;
663
664 TRACE("(%d, %08X, %p)\n", wmm->wDevID, dwFlags, lpParms);
665
666 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
667 if (dwFlags & MCI_OPEN_SHAREABLE)
668 return MCIERR_HARDWARE;
669
670 if (wmm->nUseCount > 0) {
671 /* The driver is already opened on this channel
672 * MIDI sequencer cannot be shared
673 */
674 return MCIERR_DEVICE_OPEN;
675 }
676 wmm->nUseCount++;
677
678 wmm->hFile = 0;
679 wmm->hMidi = 0;
680 wmm->wPort = MIDI_MAPPER;
681 wmm->lpstrElementName = NULL;
682
683 TRACE("wDevID=%d (lpParams->wDeviceID=%d)\n", wmm->wDevID, lpParms->wDeviceID);
684 /* lpParms->wDeviceID = wDevID;*/
685
686 if (dwFlags & MCI_OPEN_ELEMENT) {
687 TRACE("MCI_OPEN_ELEMENT %s!\n", debugstr_w(lpParms->lpstrElementName));
688 if (lpParms->lpstrElementName && lpParms->lpstrElementName[0]) {
689 wmm->hFile = mmioOpenW((LPWSTR)lpParms->lpstrElementName, NULL,
690 MMIO_ALLOCBUF | MMIO_READ | MMIO_DENYWRITE);
691 if (wmm->hFile == 0) {
692 WARN("Can't find file %s!\n", debugstr_w(lpParms->lpstrElementName));
693 wmm->nUseCount--;
694 return MCIERR_FILE_NOT_FOUND;
695 }
696 wmm->lpstrElementName = HeapAlloc(GetProcessHeap(), 0,
697 (strlenW(lpParms->lpstrElementName) + 1) * sizeof(WCHAR));
698 strcpyW(wmm->lpstrElementName, lpParms->lpstrElementName);
699 }
700 }
701 TRACE("hFile=%p\n", wmm->hFile);
702
703 wmm->lpstrCopyright = NULL;
704 wmm->lpstrName = NULL;
705
706 wmm->dwStatus = MCI_MODE_NOT_READY; /* while loading file contents */
707 /* spec says it should be the default format from the MIDI file... */
708 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
709
710 if (wmm->hFile != 0) {
711 MMCKINFO ckMainRIFF;
712 MMCKINFO mmckInfo;
713 DWORD dwOffset = 0;
714
715 if (mmioDescend(wmm->hFile, &ckMainRIFF, NULL, 0) != 0) {
716 dwRet = MCIERR_INVALID_FILE;
717 } else {
718 TRACE("ParentChunk ckid=%.4s fccType=%.4s cksize=%08X\n",
719 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType, ckMainRIFF.cksize);
720
721 if (ckMainRIFF.ckid == FOURCC_RIFF && ckMainRIFF.fccType == mmioFOURCC('R', 'M', 'I', 'D')) {
722 mmckInfo.ckid = mmioFOURCC('d', 'a', 't', 'a');
723 mmioSeek(wmm->hFile, ckMainRIFF.dwDataOffset + ((ckMainRIFF.cksize + 1) & ~1), SEEK_SET);
724 if (mmioDescend(wmm->hFile, &mmckInfo, &ckMainRIFF, MMIO_FINDCHUNK) == 0) {
725 TRACE("... is a 'RMID' file\n");
726 dwOffset = mmckInfo.dwDataOffset;
727 } else {
728 dwRet = MCIERR_INVALID_FILE;
729 }
730 }
731 if (dwRet == 0 && MIDI_mciReadMThd(wmm, dwOffset) != 0) {
732 WARN("Can't read 'MThd' header\n");
733 dwRet = MCIERR_INVALID_FILE;
734 }
735 }
736 } else {
737 TRACE("hFile==0, setting #tracks to 0; is this correct ?\n");
738 wmm->nTracks = 0;
739 wmm->wFormat = 0;
740 wmm->nDivision = 1;
741 }
742 if (dwRet != 0) {
743 wmm->nUseCount--;
744 if (wmm->hFile != 0)
745 mmioClose(wmm->hFile, 0);
746 wmm->hFile = 0;
747 HeapFree(GetProcessHeap(), 0, wmm->tracks);
748 HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
749 HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
750 HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
751 } else {
752 wmm->dwPositionMS = 0;
753 wmm->dwStatus = MCI_MODE_STOP;
754 if (dwFlags & MCI_NOTIFY)
755 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
756 }
757 return dwRet;
758 }
759
760 /**************************************************************************
761 * MIDI_mciStop [internal]
762 */
763 static DWORD MIDI_mciStop(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
764 {
765 DWORD dwRet = 0;
766
767 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
768
769 if (wmm->dwStatus != MCI_MODE_STOP) {
770 HANDLE old = InterlockedExchangePointer(&wmm->hCallback, NULL);
771 if (old) mciDriverNotify(old, wmm->wDevID, MCI_NOTIFY_ABORTED);
772 }
773
774 if (wmm->dwStatus != MCI_MODE_STOP) {
775 int oldstat = wmm->dwStatus;
776
777 wmm->dwStatus = MCI_MODE_NOT_READY;
778 if (oldstat == MCI_MODE_PAUSE)
779 dwRet = midiOutReset((HMIDIOUT)wmm->hMidi);
780
781 if ((dwFlags & MCI_WAIT) && wmm->hThread)
782 WaitForSingleObject(wmm->hThread, INFINITE);
783 }
784
785 /* sanity reset */
786 wmm->dwStatus = MCI_MODE_STOP;
787
788 if ((dwFlags & MCI_NOTIFY) && lpParms && MMSYSERR_NOERROR==dwRet)
789 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
790 return dwRet;
791 }
792
793 /**************************************************************************
794 * MIDI_mciClose [internal]
795 */
796 static DWORD MIDI_mciClose(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
797 {
798
799 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
800
801 if (wmm->dwStatus != MCI_MODE_STOP) {
802 /* mciStop handles MCI_NOTIFY_ABORTED */
803 MIDI_mciStop(wmm, MCI_WAIT, lpParms);
804 }
805
806 wmm->nUseCount--;
807 if (wmm->nUseCount == 0) {
808 if (wmm->hFile != 0) {
809 mmioClose(wmm->hFile, 0);
810 wmm->hFile = 0;
811 TRACE("hFile closed !\n");
812 }
813 if (wmm->hThread) CloseHandle(wmm->hThread);
814 HeapFree(GetProcessHeap(), 0, wmm->tracks);
815 HeapFree(GetProcessHeap(), 0, wmm->lpstrElementName);
816 HeapFree(GetProcessHeap(), 0, wmm->lpstrCopyright);
817 HeapFree(GetProcessHeap(), 0, wmm->lpstrName);
818 } else {
819 TRACE("Shouldn't happen... nUseCount=%d\n", wmm->nUseCount);
820 return MCIERR_INTERNAL;
821 }
822
823 if ((dwFlags & MCI_NOTIFY) && lpParms) {
824 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
825 }
826 return 0;
827 }
828
829 /**************************************************************************
830 * MIDI_mciFindNextEvent [internal]
831 */
832 static MCI_MIDITRACK* MIDI_mciFindNextEvent(WINE_MCIMIDI* wmm, LPDWORD hiPulse)
833 {
834 WORD cnt, nt;
835 MCI_MIDITRACK* mmt;
836
837 *hiPulse = 0xFFFFFFFFul;
838 cnt = 0xFFFFu;
839 for (nt = 0; nt < wmm->nTracks; nt++) {
840 mmt = &wmm->tracks[nt];
841
842 if (mmt->wStatus == 0)
843 continue;
844 if (mmt->dwEventPulse < *hiPulse) {
845 *hiPulse = mmt->dwEventPulse;
846 cnt = nt;
847 }
848 }
849 return (cnt == 0xFFFFu) ? 0 /* no more event on all tracks */
850 : &wmm->tracks[cnt];
851 }
852
853 /**************************************************************************
854 * MIDI_player [internal]
855 */
856 static DWORD MIDI_player(WINE_MCIMIDI* wmm, DWORD dwFlags)
857 {
858 DWORD dwRet;
859 WORD doPlay, nt;
860 MCI_MIDITRACK* mmt;
861 DWORD hiPulse, dwStartMS = wmm->dwPositionMS;
862 HANDLE oldcb = NULL;
863
864 /* init tracks */
865 for (nt = 0; nt < wmm->nTracks; nt++) {
866 mmt = &wmm->tracks[nt];
867
868 mmt->wStatus = 1; /* ok, playing */
869 mmt->dwIndex = mmt->dwFirst;
870 if (wmm->wFormat == 2 && nt > 0) {
871 mmt->dwEventPulse = wmm->tracks[nt - 1].dwLength;
872 } else {
873 mmt->dwEventPulse = 0;
874 }
875 MIDI_mciReadNextEvent(wmm, mmt); /* FIXME == 0 */
876 }
877
878 dwRet = midiOutOpen((LPHMIDIOUT)&wmm->hMidi, wmm->wPort, 0L, 0L, CALLBACK_NULL);
879 if (dwRet != MMSYSERR_NOERROR) {
880 return mmr2mci(dwRet);
881 }
882
883 wmm->dwPulse = 0;
884 wmm->dwTempo = 500000;
885 wmm->dwPositionMS = 0;
886 wmm->wStartedPlaying = FALSE;
887
888 while (wmm->dwStatus != MCI_MODE_STOP && wmm->dwStatus != MCI_MODE_NOT_READY) {
889 /* it seems that in case of multi-threading, gcc is optimizing just a little bit
890 * too much. Tell gcc not to optimize status value using volatile.
891 */
892 while (((volatile WINE_MCIMIDI*)wmm)->dwStatus == MCI_MODE_PAUSE);
893
894 doPlay = (wmm->dwPositionMS >= dwStartMS && wmm->dwPositionMS <= wmm->dwEndMS);
895
896 TRACE("wmm->dwStatus=%d, doPlay=%c\n", wmm->dwStatus, doPlay ? 'T' : 'F');
897
898 if ((mmt = MIDI_mciFindNextEvent(wmm, &hiPulse)) == NULL)
899 break; /* no more event on tracks */
900
901 /* if starting playing, then set StartTicks to the value it would have had
902 * if play had started at position 0
903 */
904 if (doPlay && !wmm->wStartedPlaying) {
905 wmm->dwStartTicks = GetTickCount() - MIDI_ConvertPulseToMS(wmm, wmm->dwPulse);
906 wmm->wStartedPlaying = TRUE;
907 TRACE("Setting dwStartTicks to %u\n", wmm->dwStartTicks);
908 }
909
910 if (hiPulse > wmm->dwPulse) {
911 wmm->dwPositionMS += MIDI_ConvertPulseToMS(wmm, hiPulse - wmm->dwPulse);
912 if (doPlay) {
913 DWORD togo = wmm->dwStartTicks + wmm->dwPositionMS;
914 DWORD tc = GetTickCount();
915
916 TRACE("Pulses hi=0x%08x <> cur=0x%08x\n", hiPulse, wmm->dwPulse);
917 TRACE("Wait until %u => %u ms\n",
918 tc - wmm->dwStartTicks, togo - wmm->dwStartTicks);
919 if (tc < togo)
920 Sleep(togo - tc);
921 }
922 wmm->dwPulse = hiPulse;
923 }
924
925 switch (LOBYTE(LOWORD(mmt->dwEventData))) {
926 case 0xF0:
927 case 0xF7: /* sysex events */
928 {
929 FIXME("Not handling SysEx events (yet)\n");
930 }
931 break;
932 case 0xFF:
933 /* position after meta data header */
934 mmioSeek(wmm->hFile, mmt->dwIndex + HIWORD(mmt->dwEventData), SEEK_SET);
935 switch (HIBYTE(LOWORD(mmt->dwEventData))) {
936 case 0x00: /* 16-bit sequence number */
937 if (TRACE_ON(mcimidi)) {
938 WORD twd;
939
940 MIDI_mciReadWord(wmm, &twd); /* == 0 */
941 TRACE("Got sequence number %u\n", twd);
942 }
943 break;
944 case 0x01: /* any text */
945 case 0x02: /* Copyright Message text */
946 case 0x03: /* Sequence/Track Name text */
947 case 0x04: /* Instrument Name text */
948 case 0x05: /* Lyric text */
949 case 0x06: /* Marker text */
950 case 0x07: /* Cue-point text */
951 if (TRACE_ON(mcimidi)) {
952 char buf[1024];
953 WORD len = mmt->wEventLength - HIWORD(mmt->dwEventData);
954 static const char* const info[8] = {"", "Text", "Copyright", "Seq/Trk name",
955 "Instrument", "Lyric", "Marker", "Cue-point"};
956 WORD idx = HIBYTE(LOWORD(mmt->dwEventData));
957
958 if (len >= sizeof(buf)) {
959 WARN("Buffer for text is too small (%u are needed)\n", len);
960 len = sizeof(buf) - 1;
961 }
962 if (mmioRead(wmm->hFile, buf, len) == len) {
963 buf[len] = 0; /* end string in case */
964 TRACE("%s => \"%s\"\n", (idx < 8 ) ? info[idx] : "", buf);
965 } else {
966 WARN("Couldn't read data for %s\n", (idx < 8) ? info[idx] : "");
967 }
968 }
969 break;
970 case 0x20:
971 /* MIDI channel (cc) */
972 if (FIXME_ON(mcimidi)) {
973 BYTE bt;
974
975 MIDI_mciReadByte(wmm, &bt); /* == 0 */
976 FIXME("NIY: MIDI channel=%u, track=%u\n", bt, mmt->wTrackNr);
977 }
978 break;
979 case 0x21:
980 /* MIDI port (pp) */
981 if (FIXME_ON(mcimidi)) {
982 BYTE bt;
983
984 MIDI_mciReadByte(wmm, &bt); /* == 0 */
985 FIXME("NIY: MIDI port=%u, track=%u\n", bt, mmt->wTrackNr);
986 }
987 break;
988 case 0x2F: /* end of track */
989 mmt->wStatus = 0;
990 break;
991 case 0x51:/* set tempo */
992 /* Tempo is expressed in -seconds per midi quarter note
993 * for format 1 MIDI files, this can only be present on track #0
994 */
995 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
996 WARN("For format #1 MIDI files, tempo can only be changed on track #0 (%u)\n", mmt->wTrackNr);
997 } else {
998 BYTE tbt;
999 DWORD value = 0;
1000
1001 MIDI_mciReadByte(wmm, &tbt); value = ((DWORD)tbt) << 16;
1002 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 8;
1003 MIDI_mciReadByte(wmm, &tbt); value |= ((DWORD)tbt) << 0;
1004 TRACE("Setting tempo to %d (BPM=%d)\n", wmm->dwTempo, (value) ? (60000000 / value) : 0);
1005 wmm->dwTempo = value;
1006 }
1007 break;
1008 case 0x54: /* (hour) (min) (second) (frame) (fractional-frame) - SMPTE track start */
1009 if (mmt->wTrackNr != 0 && wmm->wFormat == 1) {
1010 WARN("For format #1 MIDI files, SMPTE track start can only be expressed on track #0 (%u)\n", mmt->wTrackNr);
1011 } if (mmt->dwEventPulse != 0) {
1012 WARN("SMPTE track start can only be expressed at start of track (%u)\n", mmt->dwEventPulse);
1013 } else {
1014 BYTE h, m, s, f, ff;
1015
1016 MIDI_mciReadByte(wmm, &h);
1017 MIDI_mciReadByte(wmm, &m);
1018 MIDI_mciReadByte(wmm, &s);
1019 MIDI_mciReadByte(wmm, &f);
1020 MIDI_mciReadByte(wmm, &ff);
1021 FIXME("NIY: SMPTE track start %u:%u:%u %u.%u\n", h, m, s, f, ff);
1022 }
1023 break;
1024 case 0x58: /* file rhythm */
1025 if (TRACE_ON(mcimidi)) {
1026 BYTE num, den, cpmc, _32npqn;
1027
1028 MIDI_mciReadByte(wmm, &num);
1029 MIDI_mciReadByte(wmm, &den); /* to notate e.g. 6/8 */
1030 MIDI_mciReadByte(wmm, &cpmc); /* number of MIDI clocks per metronome click */
1031 MIDI_mciReadByte(wmm, &_32npqn); /* number of notated 32nd notes per MIDI quarter note */
1032
1033 TRACE("%u/%u, clock per metronome click=%u, 32nd notes by 1/4 note=%u\n", num, 1 << den, cpmc, _32npqn);
1034 }
1035 break;
1036 case 0x59: /* key signature */
1037 if (TRACE_ON(mcimidi)) {
1038 BYTE sf, mm;
1039
1040 MIDI_mciReadByte(wmm, &sf);
1041 MIDI_mciReadByte(wmm, &mm);
1042
1043 if (sf >= 0x80) TRACE("%d flats\n", -(char)sf);
1044 else if (sf > 0) TRACE("%d sharps\n", (char)sf);
1045 else TRACE("Key of C\n");
1046 TRACE("Mode: %s\n", (mm == 0) ? "major" : "minor");
1047 }
1048 break;
1049 default:
1050 WARN("Unknown MIDI meta event %02x. Skipping...\n", HIBYTE(LOWORD(mmt->dwEventData)));
1051 break;
1052 }
1053 break;
1054 default:
1055 if (doPlay) {
1056 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1057 } else {
1058 switch (LOBYTE(LOWORD(mmt->dwEventData)) & 0xF0) {
1059 case MIDI_NOTEON:
1060 case MIDI_NOTEOFF:
1061 dwRet = 0;
1062 break;
1063 default:
1064 dwRet = midiOutShortMsg((HMIDIOUT)wmm->hMidi, mmt->dwEventData);
1065 }
1066 }
1067 }
1068 mmt->dwIndex += mmt->wEventLength;
1069 if (mmt->dwIndex < mmt->dwFirst || mmt->dwIndex >= mmt->dwLast) {
1070 mmt->wStatus = 0;
1071 }
1072 if (mmt->wStatus) {
1073 MIDI_mciReadNextEvent(wmm, mmt);
1074 }
1075 }
1076
1077 midiOutReset((HMIDIOUT)wmm->hMidi);
1078
1079 dwRet = midiOutClose((HMIDIOUT)wmm->hMidi);
1080
1081 if (dwFlags & MCI_NOTIFY)
1082 oldcb = InterlockedExchangePointer(&wmm->hCallback, NULL);
1083
1084 wmm->dwStatus = MCI_MODE_STOP;
1085
1086 /* Let the potentially asynchronous commands support FAILURE notification. */
1087 if (oldcb) mciDriverNotify(oldcb, wmm->wDevID,
1088 dwRet ? MCI_NOTIFY_FAILURE : MCI_NOTIFY_SUCCESSFUL);
1089 return mmr2mci(dwRet);
1090 }
1091
1092 static DWORD CALLBACK MIDI_Starter(void *ptr)
1093 {
1094 WINE_MCIMIDI* wmm = ptr;
1095 return MIDI_player(wmm, MCI_NOTIFY);
1096 }
1097
1098 static DWORD ensurePlayerThread(WINE_MCIMIDI* wmm)
1099 {
1100 if (1) {
1101 DWORD dwRet;
1102
1103 switch (wmm->dwStatus) {
1104 default:
1105 return MCIERR_NONAPPLICABLE_FUNCTION;
1106 case MCI_MODE_PAUSE:
1107 return MIDI_mciResume(wmm, 0, NULL);
1108 case MCI_MODE_PLAY:
1109 /* the player was not stopped, use it */
1110 return 0;
1111 case MCI_MODE_STOP:
1112 break;
1113 }
1114 wmm->dwStatus = MCI_MODE_PLAY;
1115 if (wmm->hThread) {
1116 WaitForSingleObject(wmm->hThread, INFINITE);
1117 CloseHandle(wmm->hThread);
1118 wmm->hThread = 0;
1119 }
1120 wmm->hThread = CreateThread(NULL, 0, MIDI_Starter, wmm, 0, NULL);
1121 if (!wmm->hThread) {
1122 dwRet = MCIERR_OUT_OF_MEMORY;
1123 } else {
1124 SetThreadPriority(wmm->hThread, THREAD_PRIORITY_TIME_CRITICAL);
1125 dwRet = 0;
1126 }
1127 if (dwRet)
1128 wmm->dwStatus = MCI_MODE_STOP;
1129 return dwRet;
1130 }
1131 }
1132
1133 /**************************************************************************
1134 * MIDI_mciPlay [internal]
1135 */
1136 static DWORD MIDI_mciPlay(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
1137 {
1138 DWORD dwStartMS, dwEndMS;
1139 DWORD dwRet;
1140 HANDLE oldcb;
1141
1142 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1143
1144 if (wmm->hFile == 0) {
1145 WARN("Can't play: no file %s!\n", debugstr_w(wmm->lpstrElementName));
1146 return MCIERR_FILE_NOT_FOUND;
1147 }
1148
1149 if (lpParms && (dwFlags & MCI_TO)) {
1150 dwEndMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1151 /* FIXME: if (dwEndMS > length) return MCIERR_OUTOFRANGE; */
1152 } else {
1153 dwEndMS = 0xFFFFFFFFul; /* FIXME: dwEndMS = length; */
1154 }
1155 if (lpParms && (dwFlags & MCI_FROM)) {
1156 dwStartMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwFrom);
1157 } else {
1158 dwStartMS = wmm->dwPositionMS;
1159 }
1160 if (dwEndMS < dwStartMS)
1161 return MCIERR_OUTOFRANGE;
1162
1163 if (dwFlags & MCI_FROM) {
1164 /* Stop with MCI_NOTIFY_ABORTED and set new position. */
1165 MIDI_mciStop(wmm, MCI_WAIT, NULL);
1166 wmm->dwPositionMS = dwStartMS;
1167 } /* else use existing player. */
1168 if (wmm->dwEndMS != dwEndMS) {
1169 oldcb = InterlockedExchangePointer(&wmm->hCallback, NULL);
1170 if (oldcb) mciDriverNotify(oldcb, wmm->wDevID, MCI_NOTIFY_ABORTED);
1171 wmm->dwEndMS = dwEndMS;
1172 }
1173
1174 TRACE("Playing from %u to %u\n", dwStartMS, dwEndMS);
1175
1176 if ((dwFlags & MCI_NOTIFY) && lpParms) {
1177 oldcb = InterlockedExchangePointer(&wmm->hCallback, HWND_32(LOWORD(lpParms->dwCallback)));
1178 if (oldcb) mciDriverNotify(oldcb, wmm->wDevID, MCI_NOTIFY_SUPERSEDED);
1179 }
1180
1181 dwRet = ensurePlayerThread(wmm);
1182
1183 if (!dwRet && (dwFlags & MCI_WAIT)) {
1184 WaitForSingleObject(wmm->hThread, INFINITE);
1185 GetExitCodeThread(wmm->hThread, &dwRet);
1186 /* STATUS_PENDING cannot happen. It folds onto MCIERR_UNRECOGNIZED_KEYWORD */
1187 }
1188 /* The player thread performs notification at exit. */
1189 return dwRet;
1190 }
1191
1192 /**************************************************************************
1193 * MIDI_mciPause [internal]
1194 */
1195 static DWORD MIDI_mciPause(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1196 {
1197 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1198
1199 if (wmm->dwStatus == MCI_MODE_PLAY) {
1200 /* stop all notes */
1201 unsigned chn;
1202 for (chn = 0; chn < 16; chn++)
1203 midiOutShortMsg((HMIDIOUT)(wmm->hMidi), 0x78B0 | chn);
1204 wmm->dwStatus = MCI_MODE_PAUSE;
1205 }
1206 if ((dwFlags & MCI_NOTIFY) && lpParms)
1207 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1208 return 0;
1209 }
1210
1211 /**************************************************************************
1212 * MIDI_mciResume [internal]
1213 */
1214 static DWORD MIDI_mciResume(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
1215 {
1216 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1217
1218 if (wmm->dwStatus == MCI_MODE_PAUSE) {
1219 wmm->wStartedPlaying = FALSE;
1220 wmm->dwStatus = MCI_MODE_PLAY;
1221 }
1222 if ((dwFlags & MCI_NOTIFY) && lpParms)
1223 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1224 return 0;
1225 }
1226
1227 /**************************************************************************
1228 * MIDI_mciSet [internal]
1229 */
1230 static DWORD MIDI_mciSet(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_SEQ_SET_PARMS lpParms)
1231 {
1232 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1233
1234 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1235
1236 if (dwFlags & MCI_SET_TIME_FORMAT) {
1237 switch (lpParms->dwTimeFormat) {
1238 case MCI_FORMAT_MILLISECONDS:
1239 TRACE("MCI_FORMAT_MILLISECONDS !\n");
1240 wmm->dwMciTimeFormat = MCI_FORMAT_MILLISECONDS;
1241 break;
1242 case MCI_FORMAT_SMPTE_24:
1243 TRACE("MCI_FORMAT_SMPTE_24 !\n");
1244 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_24;
1245 break;
1246 case MCI_FORMAT_SMPTE_25:
1247 TRACE("MCI_FORMAT_SMPTE_25 !\n");
1248 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_25;
1249 break;
1250 case MCI_FORMAT_SMPTE_30:
1251 TRACE("MCI_FORMAT_SMPTE_30 !\n");
1252 wmm->dwMciTimeFormat = MCI_FORMAT_SMPTE_30;
1253 break;
1254 default:
1255 WARN("Bad time format %u!\n", lpParms->dwTimeFormat);
1256 return MCIERR_BAD_TIME_FORMAT;
1257 }
1258 }
1259 if (dwFlags & MCI_SET_VIDEO) {
1260 TRACE("No support for video !\n");
1261 return MCIERR_UNSUPPORTED_FUNCTION;
1262 }
1263 if (dwFlags & MCI_SET_DOOR_OPEN) {
1264 TRACE("No support for door open !\n");
1265 return MCIERR_UNSUPPORTED_FUNCTION;
1266 }
1267 if (dwFlags & MCI_SET_DOOR_CLOSED) {
1268 TRACE("No support for door close !\n");
1269 return MCIERR_UNSUPPORTED_FUNCTION;
1270 }
1271 if (dwFlags & MCI_SET_AUDIO) {
1272 if (dwFlags & MCI_SET_ON) {
1273 TRACE("MCI_SET_ON audio !\n");
1274 } else if (dwFlags & MCI_SET_OFF) {
1275 TRACE("MCI_SET_OFF audio !\n");
1276 } else {
1277 WARN("MCI_SET_AUDIO without SET_ON or SET_OFF\n");
1278 return MCIERR_BAD_INTEGER;
1279 }
1280
1281 switch (lpParms->dwAudio)
1282 {
1283 case MCI_SET_AUDIO_ALL: TRACE("MCI_SET_AUDIO_ALL !\n"); break;
1284 case MCI_SET_AUDIO_LEFT: TRACE("MCI_SET_AUDIO_LEFT !\n"); break;
1285 case MCI_SET_AUDIO_RIGHT: TRACE("MCI_SET_AUDIO_RIGHT !\n"); break;
1286 default: WARN("Unknown audio channel %u\n", lpParms->dwAudio); break;
1287 }
1288 }
1289
1290 if (dwFlags & MCI_SEQ_SET_MASTER)
1291 TRACE("MCI_SEQ_SET_MASTER !\n");
1292 if (dwFlags & MCI_SEQ_SET_SLAVE)
1293 TRACE("MCI_SEQ_SET_SLAVE !\n");
1294 if (dwFlags & MCI_SEQ_SET_OFFSET)
1295 TRACE("MCI_SEQ_SET_OFFSET !\n");
1296 if (dwFlags & MCI_SEQ_SET_PORT) {
1297 TRACE("MCI_SEQ_SET_PORT = %d\n", lpParms->dwPort);
1298 if ((UINT16)lpParms->dwPort != (UINT16)MIDI_MAPPER &&
1299 (UINT16)lpParms->dwPort >= midiOutGetNumDevs())
1300 /* FIXME: input/output port distinction? */
1301 return MCIERR_SEQ_PORT_NONEXISTENT;
1302 /* FIXME: Native manages to swap the device while playing! */
1303 wmm->wPort = lpParms->dwPort;
1304 }
1305 if (dwFlags & MCI_SEQ_SET_TEMPO)
1306 TRACE("MCI_SEQ_SET_TEMPO !\n");
1307 if (dwFlags & MCI_NOTIFY)
1308 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1309 return 0;
1310 }
1311
1312 /**************************************************************************
1313 * MIDI_mciStatus [internal]
1314 */
1315 static DWORD MIDI_mciStatus(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
1316 {
1317 DWORD ret = 0;
1318
1319 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1320
1321 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1322
1323 if (dwFlags & MCI_STATUS_ITEM) {
1324 switch (lpParms->dwItem) {
1325 case MCI_STATUS_CURRENT_TRACK:
1326 /* FIXME in Format 2 */
1327 lpParms->dwReturn = 1;
1328 TRACE("MCI_STATUS_CURRENT_TRACK => %lu\n", lpParms->dwReturn);
1329 break;
1330 case MCI_STATUS_LENGTH:
1331 if ((dwFlags & MCI_TRACK) && wmm->wFormat == 2) {
1332 if (lpParms->dwTrack >= wmm->nTracks)
1333 return MCIERR_OUTOFRANGE;
1334 /* FIXME: this is wrong if there is a tempo change inside the file */
1335 lpParms->dwReturn = MIDI_ConvertPulseToMS(wmm, wmm->tracks[lpParms->dwTrack].dwLength);
1336 } else {
1337 lpParms->dwReturn = MIDI_GetMThdLengthMS(wmm);
1338 }
1339 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm, lpParms->dwReturn);
1340 /* FIXME: ret = MCI_COLONIZED4_RETURN if SMPTE */
1341 TRACE("MCI_STATUS_LENGTH => %lu\n", lpParms->dwReturn);
1342 break;
1343 case MCI_STATUS_MODE:
1344 TRACE("MCI_STATUS_MODE => %u\n", wmm->dwStatus);
1345 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwStatus, wmm->dwStatus);
1346 ret = MCI_RESOURCE_RETURNED;
1347 break;
1348 case MCI_STATUS_MEDIA_PRESENT:
1349 TRACE("MCI_STATUS_MEDIA_PRESENT => TRUE\n");
1350 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1351 ret = MCI_RESOURCE_RETURNED;
1352 break;
1353 case MCI_STATUS_NUMBER_OF_TRACKS:
1354 lpParms->dwReturn = (wmm->wFormat == 2) ? wmm->nTracks : 1;
1355 TRACE("MCI_STATUS_NUMBER_OF_TRACKS => %lu\n", lpParms->dwReturn);
1356 break;
1357 case MCI_STATUS_POSITION:
1358 /* FIXME: check MCI_TRACK == 1 if set */
1359 lpParms->dwReturn = MIDI_ConvertMSToTimeFormat(wmm,
1360 (dwFlags & MCI_STATUS_START) ? 0 : wmm->dwPositionMS);
1361 /* FIXME: ret = MCI_COLONIZED4_RETURN if SMPTE */
1362 TRACE("MCI_STATUS_POSITION %s => %lu\n",
1363 (dwFlags & MCI_STATUS_START) ? "start" : "current", lpParms->dwReturn);
1364 break;
1365 case MCI_STATUS_READY:
1366 lpParms->dwReturn = (wmm->dwStatus == MCI_MODE_NOT_READY) ?
1367 MAKEMCIRESOURCE(FALSE, MCI_FALSE) : MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1368 ret = MCI_RESOURCE_RETURNED;
1369 TRACE("MCI_STATUS_READY = %u\n", LOWORD(lpParms->dwReturn));
1370 break;
1371 case MCI_STATUS_TIME_FORMAT:
1372 lpParms->dwReturn = MAKEMCIRESOURCE(wmm->dwMciTimeFormat, MCI_FORMAT_RETURN_BASE + wmm->dwMciTimeFormat);
1373 TRACE("MCI_STATUS_TIME_FORMAT => %u\n", LOWORD(lpParms->dwReturn));
1374 ret = MCI_RESOURCE_RETURNED;
1375 break;
1376 case MCI_SEQ_STATUS_DIVTYPE:
1377 TRACE("MCI_SEQ_STATUS_DIVTYPE !\n");
1378 if (wmm->nDivision > 0x8000) {
1379 switch (HIBYTE(wmm->nDivision)) {
1380 case 0xE8: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_24; break; /* -24 */
1381 case 0xE7: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_25; break; /* -25 */
1382 case 0xE3: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30DROP; break; /* -29 */ /* is the MCI constant correct ? */
1383 case 0xE2: lpParms->dwReturn = MCI_SEQ_DIV_SMPTE_30; break; /* -30 */
1384 default: FIXME("There is a bad bad programmer\n");
1385 }
1386 } else {
1387 lpParms->dwReturn = MCI_SEQ_DIV_PPQN;
1388 }
1389 lpParms->dwReturn = MAKEMCIRESOURCE(lpParms->dwReturn,lpParms->dwReturn);
1390 ret = MCI_RESOURCE_RETURNED;
1391 break;
1392 case MCI_SEQ_STATUS_MASTER:
1393 TRACE("MCI_SEQ_STATUS_MASTER !\n");
1394 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_SEQ_NONE, MCI_SEQ_NONE_S);
1395 ret = MCI_RESOURCE_RETURNED;
1396 break;
1397 case MCI_SEQ_STATUS_SLAVE:
1398 TRACE("MCI_SEQ_STATUS_SLAVE !\n");
1399 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_SEQ_FILE, MCI_SEQ_FILE_S);
1400 ret = MCI_RESOURCE_RETURNED;
1401 break;
1402 case MCI_SEQ_STATUS_OFFSET:
1403 TRACE("MCI_SEQ_STATUS_OFFSET !\n");
1404 lpParms->dwReturn = 0;
1405 break;
1406 case MCI_SEQ_STATUS_PORT:
1407 if (wmm->wPort != (UINT16)MIDI_MAPPER)
1408 lpParms->dwReturn = wmm->wPort;
1409 else {
1410 lpParms->dwReturn = MAKEMCIRESOURCE(MIDI_MAPPER, MCI_SEQ_MAPPER_S);
1411 ret = MCI_RESOURCE_RETURNED;
1412 }
1413 TRACE("MCI_SEQ_STATUS_PORT (%u) => %d\n", wmm->wDevID, wmm->wPort);
1414 break;
1415 case MCI_SEQ_STATUS_TEMPO:
1416 TRACE("MCI_SEQ_STATUS_TEMPO !\n");
1417 lpParms->dwReturn = wmm->dwTempo;
1418 break;
1419 default:
1420 FIXME("Unknown command %08X !\n", lpParms->dwItem);
1421 return MCIERR_UNSUPPORTED_FUNCTION;
1422 }
1423 } else {
1424 return MCIERR_MISSING_PARAMETER;
1425 }
1426 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
1427 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1428 return ret;
1429 }
1430
1431 /**************************************************************************
1432 * MIDI_mciGetDevCaps [internal]
1433 */
1434 static DWORD MIDI_mciGetDevCaps(WINE_MCIMIDI* wmm, DWORD dwFlags,
1435 LPMCI_GETDEVCAPS_PARMS lpParms)
1436 {
1437 DWORD ret;
1438
1439 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1440
1441 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1442
1443 if (dwFlags & MCI_GETDEVCAPS_ITEM) {
1444 switch (lpParms->dwItem) {
1445 case MCI_GETDEVCAPS_DEVICE_TYPE:
1446 TRACE("MCI_GETDEVCAPS_DEVICE_TYPE !\n");
1447 lpParms->dwReturn = MAKEMCIRESOURCE(MCI_DEVTYPE_SEQUENCER, MCI_DEVTYPE_SEQUENCER);
1448 ret = MCI_RESOURCE_RETURNED;
1449 break;
1450 case MCI_GETDEVCAPS_HAS_AUDIO:
1451 TRACE("MCI_GETDEVCAPS_HAS_AUDIO !\n");
1452 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1453 ret = MCI_RESOURCE_RETURNED;
1454 break;
1455 case MCI_GETDEVCAPS_HAS_VIDEO:
1456 TRACE("MCI_GETDEVCAPS_HAS_VIDEO !\n");
1457 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1458 ret = MCI_RESOURCE_RETURNED;
1459 break;
1460 case MCI_GETDEVCAPS_USES_FILES:
1461 TRACE("MCI_GETDEVCAPS_USES_FILES !\n");
1462 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1463 ret = MCI_RESOURCE_RETURNED;
1464 break;
1465 case MCI_GETDEVCAPS_COMPOUND_DEVICE:
1466 TRACE("MCI_GETDEVCAPS_COMPOUND_DEVICE !\n");
1467 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1468 ret = MCI_RESOURCE_RETURNED;
1469 break;
1470 case MCI_GETDEVCAPS_CAN_EJECT:
1471 TRACE("MCI_GETDEVCAPS_CAN_EJECT !\n");
1472 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1473 ret = MCI_RESOURCE_RETURNED;
1474 break;
1475 case MCI_GETDEVCAPS_CAN_PLAY:
1476 TRACE("MCI_GETDEVCAPS_CAN_PLAY !\n");
1477 lpParms->dwReturn = MAKEMCIRESOURCE(TRUE, MCI_TRUE);
1478 ret = MCI_RESOURCE_RETURNED;
1479 break;
1480 case MCI_GETDEVCAPS_CAN_RECORD:
1481 TRACE("MCI_GETDEVCAPS_CAN_RECORD !\n");
1482 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1483 ret = MCI_RESOURCE_RETURNED;
1484 break;
1485 case MCI_GETDEVCAPS_CAN_SAVE:
1486 TRACE("MCI_GETDEVCAPS_CAN_SAVE !\n");
1487 lpParms->dwReturn = MAKEMCIRESOURCE(FALSE, MCI_FALSE);
1488 ret = MCI_RESOURCE_RETURNED;
1489 break;
1490 default:
1491 FIXME("Unknown capability (%08x) !\n", lpParms->dwItem);
1492 return MCIERR_UNRECOGNIZED_COMMAND;
1493 }
1494 } else {
1495 WARN("No GetDevCaps-Item !\n");
1496 return MCIERR_UNRECOGNIZED_COMMAND;
1497 }
1498 if ((dwFlags & MCI_NOTIFY) && HRESULT_CODE(ret)==0)
1499 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1500 return ret;
1501 }
1502
1503 /**************************************************************************
1504 * MIDI_mciInfo [internal]
1505 */
1506 static DWORD MIDI_mciInfo(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_INFO_PARMSW lpParms)
1507 {
1508 LPCWSTR str = 0;
1509 DWORD ret = 0;
1510 static const WCHAR wszMidiSeq[] = {'W','i','n','e','\'','s',' ','M','I','D','I',' ','s','e','q','u','e','n','c','e','r',0};
1511
1512 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1513
1514 if (lpParms == NULL || lpParms->lpstrReturn == NULL)
1515 return MCIERR_NULL_PARAMETER_BLOCK;
1516
1517 TRACE("buf=%p, len=%u\n", lpParms->lpstrReturn, lpParms->dwRetSize);
1518
1519 switch (dwFlags & ~(MCI_WAIT|MCI_NOTIFY)) {
1520 case MCI_INFO_PRODUCT: str = wszMidiSeq; break;
1521 case MCI_INFO_FILE: str = wmm->lpstrElementName; break;
1522 case MCI_INFO_COPYRIGHT: str = wmm->lpstrCopyright; break;
1523 case MCI_INFO_NAME: str = wmm->lpstrName; break;
1524 default:
1525 WARN("Don't know this info command (%u)\n", dwFlags);
1526 return MCIERR_MISSING_PARAMETER; /* not MCIERR_FLAGS_... */
1527 }
1528 if (!ret) {
1529 if (lpParms->dwRetSize) {
1530 WCHAR zero = 0;
1531 /* FIXME? Since NT, mciwave, mciseq and mcicda set dwRetSize
1532 * to the number of characters written, excluding \0. */
1533 lstrcpynW(lpParms->lpstrReturn, str ? str : &zero, lpParms->dwRetSize);
1534 } else ret = MCIERR_PARAM_OVERFLOW;
1535 }
1536 if (MMSYSERR_NOERROR==ret && (dwFlags & MCI_NOTIFY))
1537 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1538 return ret;
1539 }
1540
1541 /**************************************************************************
1542 * MIDI_mciSeek [internal]
1543 */
1544 static DWORD MIDI_mciSeek(WINE_MCIMIDI* wmm, DWORD dwFlags, LPMCI_SEEK_PARMS lpParms)
1545 {
1546 DWORD position;
1547
1548 TRACE("(%d, %08X, %p);\n", wmm->wDevID, dwFlags, lpParms);
1549
1550 if (lpParms == NULL) return MCIERR_NULL_PARAMETER_BLOCK;
1551
1552 position = dwFlags & (MCI_SEEK_TO_START|MCI_SEEK_TO_END|MCI_TO);
1553 if (!position) return MCIERR_MISSING_PARAMETER;
1554 if (position&(position-1)) return MCIERR_FLAGS_NOT_COMPATIBLE;
1555
1556 MIDI_mciStop(wmm, MCI_WAIT, 0);
1557
1558 if (dwFlags & MCI_TO) { /* FIXME: compare with length */
1559 wmm->dwPositionMS = MIDI_ConvertTimeFormatToMS(wmm, lpParms->dwTo);
1560 } else if (dwFlags & MCI_SEEK_TO_START) {
1561 wmm->dwPositionMS = 0;
1562 } else {
1563 wmm->dwPositionMS = 0xFFFFFFFF; /* FIXME */
1564 }
1565
1566 TRACE("Seeking to position=%u ms\n", wmm->dwPositionMS);
1567
1568 if (dwFlags & MCI_NOTIFY)
1569 MIDI_mciNotify(lpParms->dwCallback, wmm, MCI_NOTIFY_SUCCESSFUL);
1570
1571 return 0;
1572 }
1573
1574 /*======================================================================*
1575 * MIDI entry points *
1576 *======================================================================*/
1577
1578 /**************************************************************************
1579 * DriverProc (MCISEQ.@)
1580 */
1581 LRESULT CALLBACK MCIMIDI_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
1582 LPARAM dwParam1, LPARAM dwParam2)
1583 {
1584 WINE_MCIMIDI* wmm;
1585 switch (wMsg) {
1586 case DRV_LOAD: return 1;
1587 case DRV_FREE: return 1;
1588 case DRV_ENABLE: return 1;
1589 case DRV_DISABLE: return 1;
1590 case DRV_QUERYCONFIGURE: return 1;
1591 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Driver !", "OSS Driver", MB_OK); return 1;
1592 case DRV_INSTALL: return DRVCNF_RESTART;
1593 case DRV_REMOVE: return DRVCNF_RESTART;
1594 case DRV_OPEN: return MIDI_drvOpen((LPCWSTR)dwParam1, (LPMCI_OPEN_DRIVER_PARMSW)dwParam2);
1595 case DRV_CLOSE: return MIDI_drvClose(dwDevID);
1596 }
1597 if ((wMsg < DRV_MCI_FIRST) || (wMsg > DRV_MCI_LAST)) {
1598 TRACE("Sending msg %04x to default driver proc\n", wMsg);
1599 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1600 }
1601
1602 wmm = MIDI_mciGetOpenDev(dwDevID, wMsg);
1603 if (wmm == NULL) return MCIERR_INVALID_DEVICE_ID;
1604
1605 switch (wMsg) {
1606 case MCI_OPEN_DRIVER: return MIDI_mciOpen (wmm, dwParam1, (LPMCI_OPEN_PARMSW) dwParam2);
1607 case MCI_CLOSE_DRIVER: return MIDI_mciClose (wmm, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1608 case MCI_PLAY: return MIDI_mciPlay (wmm, dwParam1, (LPMCI_PLAY_PARMS) dwParam2);
1609 case MCI_STOP: return MIDI_mciStop (wmm, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1610 case MCI_SET: return MIDI_mciSet (wmm, dwParam1, (LPMCI_SEQ_SET_PARMS) dwParam2);
1611 case MCI_PAUSE: return MIDI_mciPause (wmm, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1612 case MCI_RESUME: return MIDI_mciResume (wmm, dwParam1, (LPMCI_GENERIC_PARMS) dwParam2);
1613 case MCI_STATUS: return MIDI_mciStatus (wmm, dwParam1, (LPMCI_STATUS_PARMS) dwParam2);
1614 case MCI_GETDEVCAPS: return MIDI_mciGetDevCaps(wmm, dwParam1, (LPMCI_GETDEVCAPS_PARMS)dwParam2);
1615 case MCI_INFO: return MIDI_mciInfo (wmm, dwParam1, (LPMCI_INFO_PARMSW) dwParam2);
1616 case MCI_SEEK: return MIDI_mciSeek (wmm, dwParam1, (LPMCI_SEEK_PARMS) dwParam2);
1617 case MCI_OPEN:
1618 case MCI_CLOSE:
1619 FIXME("Shouldn't receive a MCI_OPEN or CLOSE message\n");
1620 /* fall through */
1621 default:
1622 TRACE("Unsupported command [0x%x]\n", wMsg);
1623 return MCIERR_UNSUPPORTED_FUNCTION; /* Win9x: MCIERR_UNRECOGNIZED_COMMAND */
1624 }
1625 }