1 /*
2  * Test winmm midi
3  *
4  * Copyright 2010 Jörg Höhle
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 
21 #define _WINE
22 
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stddef.h>
27 #include "windows.h"
28 #include "mmsystem.h"
29 #include "objbase.h"
30 #include "wine/test.h"
31 
32 extern const char* mmsys_error(MMRESULT error); /* from wave.c */
33 
34 /* Test in order of increasing probability to hang.
35  * On many UNIX systems, the Timidity softsynth provides
36  * MIDI sequencer services and it is not particularly robust.
37  */
38 
39 #define MYCBINST 0x4CAFE5A8 /* not used with window or thread callbacks */
40 #define WHATEVER 0xFEEDF00D
41 
42 static BOOL spurious_message(LPMSG msg)
43 {
44   /* WM_DEVICECHANGE 0x0219 appears randomly */
45   if(msg->message == WM_DEVICECHANGE) {
46     trace("skipping spurious message %04x\n", msg->message);
47     return TRUE;
48   }
49   return FALSE;
50 }
51 
52 static UINT      cbmsg  = 0;
53 static DWORD_PTR cbval1 = WHATEVER;
54 static DWORD_PTR cbval2 = 0;
55 static DWORD_PTR cbinst = MYCBINST;
56 
57 static void CALLBACK callback_func(HWAVEOUT hwo, UINT uMsg,
58                                    DWORD_PTR dwInstance,
59                                    DWORD_PTR dwParam1, DWORD_PTR dwParam2)
60 {
61     if (winetest_debug>1)
62         trace("Callback! msg=%x %lx %lx\n", uMsg, dwParam1, dwParam2);
63     cbmsg = uMsg;
64     cbval1 = dwParam1;   /* mhdr or 0 */
65     cbval2 = dwParam2;   /* always 0 */
66     cbinst = dwInstance; /* MYCBINST, see midiOut/StreamOpen */
67 }
68 
69 #define test_notification(hwnd, command, m1, p2) test_notification_dbg(hwnd, command, m1, p2, __LINE__)
70 static void test_notification_dbg(HWND hwnd, const char* command, UINT m1, DWORD_PTR p2, int line)
71 {   /* Use message type 0 as meaning no message */
72     MSG msg;
73     if (hwnd) {
74         /* msg.wParam is hmidiout, msg.lParam is the mhdr (or 0) */
75         BOOL seen;
76         do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
77         while(seen && spurious_message(&msg));
78         if (m1 && !seen) {
79           /* We observe transient delayed notification, mostly on native.
80            * Perhaps the OS preempts the player thread after setting MHDR_DONE
81            * or clearing MHDR_INQUEUE, before calling DriverCallback. */
82           DWORD rc;
83           trace_(__FILE__,line)("Waiting for delayed message %x from %s\n", m1, command);
84           SetLastError(0xDEADBEEF);
85           rc = MsgWaitForMultipleObjects(0, NULL, FALSE, 3000, QS_POSTMESSAGE);
86           ok_(__FILE__,line)(rc==WAIT_OBJECT_0, "Wait failed: %04x %d\n", rc, GetLastError());
87           seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE);
88         }
89         if (seen) {
90             trace_(__FILE__,line)("Message %x, wParam=%lx, lParam=%lx from %s\n",
91                   msg.message, msg.wParam, msg.lParam, command);
92             ok_(__FILE__,line)(msg.hwnd==hwnd, "Didn't get the handle to our test window\n");
93             ok_(__FILE__,line)(msg.message==m1 && msg.lParam==p2, "bad message %x/%lx from %s, expect %x/%lx\n", msg.message, msg.lParam, command, m1, p2);
94         }
95         else ok_(__FILE__,line)(m1==0, "Expect message %x from %s\n", m1, command);
96     }
97     else {
98         /* FIXME: MOM_POSITIONCB and MOM_DONE are so close that a queue is needed. */
99         if (cbmsg) {
100             ok_(__FILE__,line)(cbmsg==m1 && cbval1==p2 && cbval2==0, "bad callback %x/%lx/%lx from %s, expect %x/%lx\n", cbmsg, cbval1, cbval2, command, m1, p2);
101             cbmsg = 0; /* Mark as read */
102             cbval1 = cbval2 = WHATEVER;
103             ok_(__FILE__,line)(cbinst==MYCBINST, "callback dwInstance changed to %lx\n", cbinst);
104         }
105         else ok_(__FILE__,line)(m1==0, "Expect callback %x from %s\n", m1, command);
106     }
107 }
108 
109 
110 static void test_midiIn_device(UINT udev, HWND hwnd)
111 {
112     HMIDIIN hm;
113     MMRESULT rc;
114     MIDIINCAPSA capsA;
115     MIDIHDR mhdr;
116 
117     rc = midiInGetDevCapsA(udev, &capsA, sizeof(capsA));
118     ok((MIDIMAPPER==udev) ? (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)) : rc==0,
119        "midiInGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
120     if (!rc) {
121         /* MIDI IN capsA.dwSupport may contain garbage, absent in old MS-Windows */
122         trace("* %s: manufacturer=%d, product=%d, support=%X\n", capsA.szPname, capsA.wMid, capsA.wPid, capsA.dwSupport);
123     }
124 
125     if (hwnd)
126         rc = midiInOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
127     else
128         rc = midiInOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
129     ok((MIDIMAPPER!=udev) ? rc==0 : (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)),
130        "midiInOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
131     if (rc) return;
132 
133     test_notification(hwnd, "midiInOpen", MIM_OPEN, 0);
134 
135     memset(&mhdr, 0, sizeof(mhdr));
136     mhdr.dwFlags = MHDR_DONE;
137     mhdr.dwUser = 0x56FA552C;
138     mhdr.dwBufferLength = 70000; /* > 64KB! */
139     mhdr.dwBytesRecorded = 5;
140     mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
141     ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
142     if (mhdr.lpData) {
143         rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
144         ok(rc==MMSYSERR_INVALPARAM, "midiInPrepare tiny rc=%s\n", mmsys_error(rc));
145         ok(mhdr.dwFlags == MHDR_DONE, "dwFlags=%x\n", mhdr.dwFlags);
146 
147         mhdr.dwFlags |= MHDR_INQUEUE;
148         rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
149         ok(!rc, "midiInPrepare old size rc=%s\n", mmsys_error(rc));
150         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_INQUEUE|MHDR_DONE)/*w9x*/ ||
151            mhdr.dwFlags == MHDR_PREPARED, "dwFlags=%x\n", mhdr.dwFlags);
152         trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
153 
154         mhdr.dwFlags |= MHDR_INQUEUE|MHDR_DONE;
155         rc = midiInPrepareHeader(hm, &mhdr, sizeof(mhdr));
156         ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
157         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_INQUEUE|MHDR_DONE), "dwFlags=%x\n", mhdr.dwFlags);
158 
159         mhdr.dwFlags &= ~MHDR_INQUEUE;
160         rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
161         ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
162         ok(mhdr.dwFlags == MHDR_DONE, "dwFlags=%x\n", mhdr.dwFlags);
163 
164         mhdr.dwFlags &= ~MHDR_DONE;
165         rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
166         ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
167         ok(mhdr.dwFlags == 0, "dwFlags=%x\n", mhdr.dwFlags);
168 
169         rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
170         ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
171         ok(mhdr.dwFlags == MHDR_PREPARED, "dwFlags=%x\n", mhdr.dwFlags);
172 
173         mhdr.dwFlags |= MHDR_DONE;
174         rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
175         ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
176         ok(mhdr.dwBytesRecorded == 5, "BytesRec=%u\n", mhdr.dwBytesRecorded);
177 
178         mhdr.dwFlags |= MHDR_DONE;
179         rc = midiInAddBuffer(hm, &mhdr, sizeof(mhdr));
180         ok(!rc, "midiAddBuffer rc=%s\n", mmsys_error(rc));
181         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_INQUEUE), "dwFlags=%x\n", mhdr.dwFlags);
182 
183         /* w95 does not set dwBytesRecorded=0 within midiInAddBuffer.  Wine does. */
184         if (mhdr.dwBytesRecorded != 0)
185             trace("dwBytesRecorded %u\n", mhdr.dwBytesRecorded);
186 
187         rc = midiInAddBuffer(hm, &mhdr, sizeof(mhdr));
188         ok(rc==MIDIERR_STILLPLAYING, "midiAddBuffer rc=%s\n", mmsys_error(rc));
189 
190         rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
191         ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
192         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_INQUEUE), "dwFlags=%x\n", mhdr.dwFlags);
193     }
194     rc = midiInReset(hm); /* Return any pending buffer */
195     ok(!rc, "midiInReset rc=%s\n", mmsys_error(rc));
196     test_notification(hwnd, "midiInReset", MIM_LONGDATA, (DWORD_PTR)&mhdr);
197 
198     ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_DONE), "dwFlags=%x\n", mhdr.dwFlags);
199     rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
200     ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
201 
202     ok(mhdr.dwBytesRecorded == 0, "Did some MIDI HW send %u bytes?\n", mhdr.dwBytesRecorded);
203 
204     rc = midiInClose(hm);
205     ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
206 
207     ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
208     HeapFree(GetProcessHeap(), 0, mhdr.lpData);
209     test_notification(hwnd, "midiInClose", MIM_CLOSE, 0);
210     test_notification(hwnd, "midiIn over", 0, WHATEVER);
211 }
212 
213 static void test_midi_infns(HWND hwnd)
214 {
215     HMIDIIN hm;
216     MMRESULT rc;
217     UINT udev, ndevs = midiInGetNumDevs();
218 
219     rc = midiInOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
220     ok(rc==MMSYSERR_BADDEVICEID, "midiInOpen udev>max rc=%s\n", mmsys_error(rc));
221     if (!rc) {
222         rc = midiInClose(hm);
223         ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
224     }
225     if (!ndevs) {
226         trace("Found no MIDI IN device\n"); /* no skip for this common situation */
227         rc = midiInOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
228         ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiInOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
229         if (!rc) {
230             rc = midiInClose(hm);
231             ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
232         }
233         return;
234     }
235     trace("Found %d MIDI IN devices\n", ndevs);
236     for (udev=0; udev < ndevs; udev++) {
237         trace("** Testing device %d\n", udev);
238         test_midiIn_device(udev, hwnd);
239         Sleep(50);
240     }
241     trace("** Testing MIDI mapper\n");
242     test_midiIn_device(MIDIMAPPER, hwnd);
243 }
244 
245 
246 static void test_midi_mci(HWND hwnd)
247 {
248     MCIERROR err;
249     char buf[1024];
250     memset(buf, 0, sizeof(buf));
251 
252     err = mciSendStringA("sysinfo sequencer quantity", buf, sizeof(buf), hwnd);
253     ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
254     if (!err) trace("Found %s MCI sequencer devices\n", buf);
255 
256     if (!strcmp(buf, "0")) return;
257 
258     err = mciSendStringA("capability sequencer can record", buf, sizeof(buf), hwnd);
259     ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
260     if(!err) ok(!strcmp(buf, "false"), "capability can record is %s\n", buf);
261 }
262 
263 static BYTE SysEx_reset[] = {
264     0xF0, 0x7E, 0x7F, 0x09, 0x01, 0xF7 /* GM System ON */
265 };
266 static BYTE SysEx_volume_off[] = {
267     0xF0, 0x7F, 0x7F, 0x04, 0x01, 0x00, 0x00, 0xF7
268 };
269 static BYTE SysEx_volume_full[] = {
270     0xF0, 0x7F, 0x7F, 0x04, 0x01, 0x00, 0x7F, 0xF7
271 };
272 static BOOL found_fluidsynth;
273 static const char fluidsynth_prefix[] = "Synth input port ";
274 
275 static void test_midiOut_device(UINT udev, HWND hwnd)
276 {
277     HMIDIOUT hm;
278     MMRESULT rc;
279     MIDIOUTCAPSA capsA;
280     DWORD ovolume;
281     UINT  udevid;
282     MIDIHDR mhdr;
283 
284     rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
285     ok(!rc, "midiOutGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
286     if (!rc) {
287         trace("* %s: manufacturer=%d, product=%d, tech=%d, support=%X: %d voices, %d notes\n",
288               capsA.szPname, capsA.wMid, capsA.wPid, capsA.wTechnology, capsA.dwSupport, capsA.wVoices, capsA.wNotes);
289         ok(!((MIDIMAPPER==udev) ^ (MOD_MAPPER==capsA.wTechnology)), "technology %d on device %d\n", capsA.wTechnology, udev);
290         if (MOD_MIDIPORT == capsA.wTechnology) {
291             ok(capsA.wVoices == 0 && capsA.wNotes == 0, "external device with notes or voices\n");
292             ok(capsA.wChannelMask == 0xFFFF, "external device channel mask %x\n", capsA.wChannelMask);
293             ok(!(capsA.dwSupport & (MIDICAPS_VOLUME|MIDICAPS_LRVOLUME|MIDICAPS_CACHE)), "external device support=%X\n", capsA.dwSupport);
294         }
295     }
296 
297     if (hwnd)
298         rc = midiOutOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
299     else
300         rc = midiOutOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
301     if (rc == MMSYSERR_NOTSUPPORTED || rc == MMSYSERR_NODRIVER)
302     {
303         skip( "MIDI out not supported\n" );
304         return;
305     }
306     ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
307     if (rc) return;
308 
309     test_notification(hwnd, "midiOutOpen", MOM_OPEN, 0);
310 
311     rc = midiOutGetVolume(hm, &ovolume);
312     ok((capsA.dwSupport & MIDICAPS_VOLUME) ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED, "midiOutGetVolume rc=%s\n", mmsys_error(rc));
313     /* The native mapper responds with FFFFFFFF initially,
314      * real devices with the volume GUI SW-synth settings. */
315     if (!rc) trace("Current volume %x on device %d\n", ovolume, udev);
316 
317     /* The W95 ESFM Synthesis device reports NOTENABLED although
318      * GetVolume by handle works and music plays. */
319     rc = midiOutGetVolume(UlongToHandle(udev), &ovolume);
320     ok((capsA.dwSupport & MIDICAPS_VOLUME) ? rc==MMSYSERR_NOERROR || broken(rc==MMSYSERR_NOTENABLED) : rc==MMSYSERR_NOTSUPPORTED, "midiOutGetVolume(dev=%d) rc=%s\n", udev, mmsys_error(rc));
321 
322     rc = midiOutGetVolume(hm, NULL);
323     ok(rc==MMSYSERR_INVALPARAM, "midiOutGetVolume NULL rc=%s\n", mmsys_error(rc));
324 
325     /* Tests with midiOutSetvolume show that the midi mapper forwards
326      * the value to the real device, but Get initially always reports
327      * FFFFFFFF.  Therefore, a Get+SetVolume pair with the mapper is
328      * not adequate to restore the value prior to tests.
329      */
330     if (winetest_interactive && (capsA.dwSupport & MIDICAPS_VOLUME)) {
331         DWORD volume2 = (ovolume < 0x80000000) ? 0xC000C000 : 0x40004000;
332         rc = midiOutSetVolume(hm, volume2);
333         ok(!rc, "midiOutSetVolume rc=%s\n", mmsys_error(rc));
334         if (!rc) {
335             DWORD volume3;
336             rc = midiOutGetVolume(hm, &volume3);
337             ok(!rc, "midiOutGetVolume new rc=%s\n", mmsys_error(rc));
338             if (!rc) trace("New volume %x on device %d\n", volume3, udev);
339             todo_wine ok(volume2==volume3, "volume Set %x = Get %x\n", volume2, volume3);
340 
341             rc = midiOutSetVolume(hm, ovolume);
342             ok(!rc, "midiOutSetVolume restore rc=%s\n", mmsys_error(rc));
343         }
344     }
345     rc = midiOutGetDevCapsA((UINT_PTR)hm, &capsA, sizeof(capsA));
346     ok(!rc, "midiOutGetDevCaps(dev=%d) by handle rc=%s\n", udev, mmsys_error(rc));
347     rc = midiInGetDevCapsA((UINT_PTR)hm, (LPMIDIINCAPSA)&capsA, sizeof(DWORD));
348     ok(rc==MMSYSERR_BADDEVICEID, "midiInGetDevCaps(dev=%d) by out handle rc=%s\n", udev, mmsys_error(rc));
349 
350     {   DWORD e = 0x006F4893; /* velocity, note (#69 would be 440Hz) channel */
351         trace("ShortMsg type %x\n", LOBYTE(LOWORD(e)));
352         rc = midiOutShortMsg(hm, e);
353         ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
354         if (!rc) Sleep(400); /* Hear note */
355     }
356 
357     memset(&mhdr, 0, sizeof(mhdr));
358     mhdr.dwFlags = MHDR_DONE;
359     mhdr.dwUser   = 0x56FA552C;
360     mhdr.dwOffset = 0xDEADBEEF;
361     mhdr.dwBufferLength = 70000; /* > 64KB! */
362     mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
363     ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
364     if (mhdr.lpData) {
365         rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
366         ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
367         ok(mhdr.dwFlags == MHDR_DONE, "dwFlags=%x\n", mhdr.dwFlags);
368         test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
369 
370         rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
371         ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
372         ok(mhdr.dwFlags == MHDR_DONE, "dwFlags=%x\n", mhdr.dwFlags);
373 
374         /* Since at least w2k, midiOutPrepare clears the DONE and INQUEUE flags.  w95 didn't. */
375         /* mhdr.dwFlags |= MHDR_INQUEUE; would cause w95 to return STILLPLAYING from Unprepare */
376         rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
377         ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
378         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_DONE)/*w9x*/ ||
379            mhdr.dwFlags == MHDR_PREPARED, "dwFlags=%x\n", mhdr.dwFlags);
380         trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
381 
382         /* No flag is cleared when already prepared. */
383         mhdr.dwFlags |= MHDR_DONE|MHDR_INQUEUE;
384         rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
385         ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
386         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_DONE|MHDR_INQUEUE), "dwFlags=%x\n", mhdr.dwFlags);
387 
388         mhdr.dwFlags |= MHDR_INQUEUE;
389         rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
390         ok(rc==MIDIERR_STILLPLAYING, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
391         ok(mhdr.dwFlags == (MHDR_PREPARED|MHDR_DONE|MHDR_INQUEUE), "dwFlags=%x\n", mhdr.dwFlags);
392 
393         mhdr.dwFlags &= ~MHDR_INQUEUE;
394         rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
395         ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
396         ok(mhdr.dwFlags == MHDR_DONE, "dwFlags=%x\n", mhdr.dwFlags);
397 
398         mhdr.dwFlags |= MHDR_INQUEUE;
399         rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
400         ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
401         ok(mhdr.dwFlags == (MHDR_INQUEUE|MHDR_DONE), "dwFlags=%x\n", mhdr.dwFlags);
402 
403         HeapFree(GetProcessHeap(), 0, mhdr.lpData);
404     }
405     ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
406     ok(mhdr.dwOffset==0xDEADBEEF, "MIDIHDR.dwOffset changed to %x\n", mhdr.dwOffset);
407 
408     rc = midiOutGetID(hm, &udevid);
409     ok(!rc, "midiOutGetID rc=%s\n", mmsys_error(rc));
410     if(!rc) ok(udevid==udev, "midiOutGetID gives %d, expect %d\n", udevid, udev);
411 
412     rc = midiOutReset(hm); /* Quiet everything */
413     ok(!rc, "midiOutReset rc=%s\n", mmsys_error(rc));
414 
415     rc = midiOutClose(hm);
416     ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
417     test_notification(hwnd, "midiOutClose", MOM_CLOSE, 0);
418 
419     rc = midiOutOpen(&hm, udev, 0, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
420     /* w95 broken(rc==MMSYSERR_INVALPARAM) see WINMM_CheckCallback */
421     ok(!rc, "midiOutOpen(dev=%d) 0 CALLBACK_WINDOW rc=%s\n", udev, mmsys_error(rc));
422     /* PostMessage(hwnd=0) redirects to PostThreadMessage(GetCurrentThreadId())
423      * which PeekMessage((HWND)-1) queries. */
424     test_notification((HWND)-1, "midiOutOpen WINDOW->THREAD", 0, WHATEVER);
425     test_notification(hwnd, "midiOutOpen WINDOW", 0, WHATEVER);
426     if (!rc) {
427         rc = midiOutClose(hm);
428         ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
429         test_notification((HWND)-1, "midiOutClose WINDOW->THREAD", 0, WHATEVER);
430         test_notification(hwnd, "midiOutClose", 0, WHATEVER);
431     }
432     test_notification(hwnd, "midiOut over", 0, WHATEVER);
433     if (!strncmp(capsA.szPname, fluidsynth_prefix, strlen(fluidsynth_prefix)) ||
434         (udev == MIDIMAPPER && found_fluidsynth)) {
435         found_fluidsynth = TRUE;
436         skip("FluidSynth (at least 1.1.6) doesn't support desired System Exclusive message.\n");
437         return;
438     }
439 
440     if (hwnd)
441         rc = midiOutOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
442     else
443         rc = midiOutOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
444     ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
445     test_notification(hwnd, "midiOutOpen", MOM_OPEN, 0);
446 
447     memset(&mhdr, 0, sizeof(mhdr));
448     mhdr.lpData = (LPSTR)SysEx_reset;
449     mhdr.dwBufferLength = sizeof(SysEx_reset);
450     rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
451     ok(!rc, "midiOutPrepareHeader rc=%s\n", mmsys_error(rc));
452     rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
453     ok(!rc, "midiOutLongMsg rc=%s\n", mmsys_error(rc));
454     rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
455     ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
456     test_notification(hwnd, "midiOutLongMsg", MOM_DONE, (DWORD_PTR)&mhdr);
457     Sleep(60);
458 
459     mhdr.lpData = (LPSTR)SysEx_volume_off;
460     mhdr.dwBufferLength = sizeof(SysEx_volume_off);
461     rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
462     ok(!rc, "midiOutPrepareHeader rc=%s\n", mmsys_error(rc));
463     rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
464     ok(!rc, "midiOutLongMsg rc=%s\n", mmsys_error(rc));
465     rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
466     ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
467     test_notification(hwnd, "midiOutLongMsg", MOM_DONE, (DWORD_PTR)&mhdr);
468 
469     {
470         DWORD e = 0x006F4593; /* velocity 111, note #69, channel 4 */
471         trace("ShortMsg type %x (muted)\n", LOBYTE(LOWORD(e)));
472         rc = midiOutShortMsg(hm, e);
473         ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
474         /* We can't hear this voice due to volume settings */
475         if (!rc) Sleep(200);
476 
477         rc = midiOutShortMsg(hm, 0x00004593); /* velocity 0 */
478         ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
479     }
480 
481     mhdr.lpData = (LPSTR)SysEx_volume_full;
482     mhdr.dwBufferLength = sizeof(SysEx_volume_full);
483     rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
484     ok(!rc, "midiOutPrepareHeader rc=%s\n", mmsys_error(rc));
485     rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
486     ok(!rc, "midiOutLongMsg rc=%s\n", mmsys_error(rc));
487     rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
488     ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
489     test_notification(hwnd, "midiOutLongMsg", MOM_DONE, (DWORD_PTR)&mhdr);
490 
491     rc = midiOutClose(hm);
492     ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
493     test_notification(hwnd, "midiOuClose", MOM_CLOSE, 0);
494 }
495 
496 static void test_position(HMIDISTRM hm, UINT typein, UINT typeout)
497 {
498     MMRESULT rc;
499     MMTIME mmtime;
500     mmtime.wType = typein;
501     rc = midiStreamPosition(hm, &mmtime, sizeof(MMTIME));
502     /* Ugly, but a single ok() herein enables using the todo_wine prefix */
503     ok(!rc && (mmtime.wType == typeout), "midiStreamPosition type %x converted to %x rc=%s\n", typein, mmtime.wType, mmsys_error(rc));
504     if (!rc) switch(mmtime.wType) {
505     case TIME_MS:
506         trace("Stream position %ums\n", mmtime.u.ms);
507         break;
508     case TIME_TICKS:
509         trace("Stream position %u ticks\n", mmtime.u.ticks);
510         break;
511     case TIME_MIDI:
512         trace("Stream position song pointer %u\n", mmtime.u.midi.songptrpos);
513         break;
514     case TIME_SMPTE:
515         trace("Stream position %02u:%02u:%02u.%02u/%02u\n",
516               mmtime.u.smpte.hour, mmtime.u.smpte.min, mmtime.u.smpte.sec,
517               mmtime.u.smpte.frame, mmtime.u.smpte.fps);
518         break;
519     }
520 }
521 
522 typedef struct midishortevent_tag { /* ideal size for MEVT_F_SHORT event type */
523     DWORD dwDeltaTime;
524     DWORD dwStreamID;
525     DWORD dwEvent;
526 } MIDISHORTEVENT;
527 
528 /* Native crashes on a second run with the const qualifier set on this data! */
529 static BYTE strmEvents[] = { /* A set of variable-sized MIDIEVENT structs */
530     0, 0, 0, 0,  0, 0, 0, 0, /* dwDeltaTime and dwStreamID */
531     0, 0, 0, MEVT_NOP | 0x40, /* with MEVT_F_CALLBACK */
532     0, 0, 0, 0,  0, 0, 0, 0,
533     0xE0, 0x93, 0x04, MEVT_TEMPO, /* 0493E0 == 300000 */
534     0, 0, 0, 0,  0, 0, 0, 0,
535     0x93, 0x48, 0x6F, MEVT_SHORTMSG,
536 };
537 
538 static MIDISHORTEVENT strmNops[] = { /* Test callback + dwOffset */
539   { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
540   { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
541 };
542 
543 static MIDISHORTEVENT strmNopsWithDelta[] = {
544   {  0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
545   { 12, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
546 };
547 
548 struct time_stamp_records {
549     UINT  count;
550     DWORD time_stamp[2];
551     HANDLE done;
552 };
553 
554 static void CALLBACK time_stamp_callback(HMIDIOUT hmo, UINT msg, DWORD_PTR instance, DWORD_PTR p1, DWORD_PTR p2)
555 {
556     struct time_stamp_records *records = (struct time_stamp_records *)instance;
557     switch (msg) {
558     case MM_MOM_POSITIONCB:
559         if (records->count < ARRAY_SIZE(records->time_stamp))
560             records->time_stamp[records->count] = GetTickCount();
561         records->count++;
562         break;
563     case MM_MOM_DONE:
564         SetEvent(records->done);
565         break;
566     }
567 }
568 
569 static DWORD get_position(HMIDISTRM hm, UINT type)
570 {
571     MMRESULT rc;
572     MMTIME mmtime;
573     mmtime.wType = type;
574     rc = midiStreamPosition(hm, &mmtime, sizeof(mmtime));
575     if (rc != MMSYSERR_NOERROR || mmtime.wType != type)
576         return MAXDWORD;
577 
578     switch (mmtime.wType) {
579     case TIME_MS:
580         return mmtime.u.ms;
581     case TIME_TICKS:
582         return mmtime.u.ticks;
583     case TIME_MIDI:
584         return mmtime.u.midi.songptrpos;
585     default:
586         return MAXDWORD;
587     }
588 }
589 
590 static MMRESULT playStream(HMIDISTRM hm, LPMIDIHDR lpMidiHdr)
591 {
592     MMRESULT rc = midiStreamOut(hm, lpMidiHdr, sizeof(MIDIHDR));
593     /* virtual machines may return MIDIERR_STILLPLAYING from the next request
594      * even after MHDR_DONE is set. It's still too early, so add MHDR_INQUEUE. */
595     if (!rc) while (!(lpMidiHdr->dwFlags & MHDR_DONE) || (lpMidiHdr->dwFlags & MHDR_INQUEUE)) { Sleep(100); }
596     return rc;
597 }
598 
599 static void test_midiStream(UINT udev, HWND hwnd)
600 {
601     HMIDISTRM hm;
602     MMRESULT rc, rc2;
603     MIDIHDR mhdr;
604     union {
605         MIDIPROPTEMPO tempo;
606         MIDIPROPTIMEDIV tdiv;
607     } midiprop;
608     DWORD diff, expected, ret;
609     const DWORD MARGIN = 50;
610     struct time_stamp_records records;
611     MIDIOUTCAPSA capsA;
612 
613     if (hwnd)
614         rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
615     else
616         rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
617     if (rc == MMSYSERR_NOTSUPPORTED || rc == MMSYSERR_NODRIVER)
618     {
619         skip( "MIDI stream not supported\n" );
620         return;
621     }
622     ok(!rc, "midiStreamOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
623     if (rc) return;
624 
625     test_notification(hwnd, "midiStreamOpen", MOM_OPEN, 0);
626 
627     midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
628     rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
629     ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
630     ok(midiprop.tempo.dwTempo==500000, "default stream tempo %u microsec per quarter note\n", midiprop.tempo.dwTempo);
631 
632     midiprop.tdiv.cbStruct = sizeof(midiprop.tdiv);
633     rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TIMEDIV);
634     ok(!rc, "midiStreamProperty TIMEDIV rc=%s\n", mmsys_error(rc));
635     ok(24==LOWORD(midiprop.tdiv.dwTimeDiv), "default stream time division %u\n", midiprop.tdiv.dwTimeDiv);
636 
637     memset(&mhdr, 0, sizeof(mhdr));
638     mhdr.dwUser   = 0x56FA552C;
639     mhdr.dwOffset = 1234567890;
640     mhdr.dwBufferLength = sizeof(strmEvents);
641     mhdr.dwBytesRecorded = mhdr.dwBufferLength;
642     mhdr.lpData = (LPSTR)&strmEvents[0];
643     if (mhdr.lpData) {
644         rc = midiOutLongMsg((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
645         ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
646         test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
647 
648         rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
649         ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
650         rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
651         ok(!rc, "midiOutPrepare size rc=%s\n", mmsys_error(rc));
652         ok(mhdr.dwFlags & MHDR_PREPARED, "MHDR.dwFlags when prepared %x\n", mhdr.dwFlags);
653 
654         /* The device is still in paused mode and should queue the message. */
655         rc = midiStreamOut(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
656         ok(!rc, "midiStreamOut old size rc=%s\n", mmsys_error(rc));
657         rc2 = rc;
658         trace("MIDIHDR flags=%x when submitted\n", mhdr.dwFlags);
659         /* w9X/me does not set MHDR_ISSTRM when StreamOut exits,
660          * but it will be set on all systems after the job is finished. */
661 
662         Sleep(90);
663         /* Wine <1.1.39 started playing immediately */
664         test_notification(hwnd, "midiStream still paused", 0, WHATEVER);
665 
666     /* MSDN asks to use midiStreamRestart prior to midiStreamOut()
667      * because the starting state is 'pause', but some apps seem to
668      * work with the inverse order: queue everything, then play.
669      */
670 
671         rc = midiStreamRestart(hm);
672         ok(!rc, "midiStreamRestart rc=%s\n", mmsys_error(rc));
673 
674         if (!rc2) while(mhdr.dwFlags & MHDR_INQUEUE) {
675             trace("async MIDI still queued\n");
676             Sleep(100);
677         } /* Checking INQUEUE is not the recommended way to wait for the end of a job, but we're testing. */
678         /* MHDR_ISSTRM is not necessarily set when midiStreamOut returns
679          * rather than when the queue is eventually processed. */
680         ok(mhdr.dwFlags & MHDR_ISSTRM, "MHDR.dwFlags %x no ISSTRM when out of queue\n", mhdr.dwFlags);
681         if (!rc2) while(!(mhdr.dwFlags & MHDR_DONE)) {
682             /* Never to be seen except perhaps on multicore */
683             trace("async MIDI still not done\n");
684             Sleep(100);
685         }
686         ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags %x not DONE when out of queue\n", mhdr.dwFlags);
687         test_notification(hwnd, "midiStream callback", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
688         test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
689 
690         /* Native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
691         ok(1234567890!=mhdr.dwOffset, "play left MIDIHDR.dwOffset at %u\n", mhdr.dwOffset);
692 
693         rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
694         ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
695         rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
696         ok(!rc, "midiOutUnprepare #2 rc=%s\n", mmsys_error(rc));
697 
698         trace("MIDIHDR stream flags=%x when finished\n", mhdr.dwFlags);
699         ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags when done %x\n", mhdr.dwFlags);
700 
701         test_position(hm, TIME_MS,      TIME_MS);
702         test_position(hm, TIME_TICKS,   TIME_TICKS);
703         test_position(hm, TIME_MIDI,    TIME_MIDI);
704         test_position(hm, TIME_SMPTE,   TIME_MS);
705         test_position(hm, TIME_SAMPLES, TIME_MS);
706         test_position(hm, TIME_BYTES,   TIME_MS);
707 
708         Sleep(400); /* Hear note */
709 
710         midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
711         rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
712         ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
713         ok(0x0493E0==midiprop.tempo.dwTempo, "stream set tempo %u\n", midiprop.tdiv.dwTimeDiv);
714 
715         rc = midiStreamRestart(hm);
716         ok(!rc, "midiStreamRestart #2 rc=%s\n", mmsys_error(rc));
717 
718         mhdr.dwFlags |= MHDR_ISSTRM;
719         /* Preset flags (e.g. MHDR_ISSTRM) do not disturb. */
720         rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
721         ok(!rc, "midiOutPrepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
722         rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
723         ok(!rc, "midiOutUnprepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
724 
725         rc = midiStreamRestart(hm);
726         ok(!rc, "midiStreamRestart #3 rc=%s\n", mmsys_error(rc));
727     }
728     ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
729     ok(0==((MIDISHORTEVENT*)&strmEvents)[0].dwStreamID, "dwStreamID set to %x\n", ((LPMIDIEVENT)&strmEvents[0])->dwStreamID);
730 
731     /* dwBytesRecorded controls how much is played, not dwBufferLength
732      * allowing to immediately forward packets from midiIn to midiOut */
733     mhdr.dwOffset = 1234123123;
734     mhdr.dwBufferLength = sizeof(strmNops);
735     trace("buffer: %u\n", mhdr.dwBufferLength);
736     mhdr.dwBytesRecorded = 0;
737     mhdr.lpData = (LPSTR)&strmNops[0];
738     strmNops[0].dwEvent |= MEVT_F_CALLBACK;
739     strmNops[1].dwEvent |= MEVT_F_CALLBACK;
740 
741     rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
742     ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
743 
744     rc = playStream(hm, &mhdr);
745     ok(!rc, "midiStreamOut 0 bytes recorded rc=%s\n", mmsys_error(rc));
746 
747     test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
748     test_notification(hwnd, "0 bytes recorded", 0, WHATEVER);
749 
750     /* FIXME: check dwOffset within callback
751      * instead of the unspecified value afterwards */
752     ok(1234123123==mhdr.dwOffset || broken(0==mhdr.dwOffset), "play 0 set MIDIHDR.dwOffset to %u\n", mhdr.dwOffset);
753     /* w2k and later only set dwOffset when processing MEVT_T_CALLBACK,
754      * while w9X/me/nt always sets it.  Have Wine behave like w2k because the
755      * dwOffset slot does not exist in the small size MIDIHDR. */
756 
757     mhdr.dwOffset = 1234123123;
758     mhdr.dwBytesRecorded = 1*sizeof(MIDISHORTEVENT);
759 
760     rc = playStream(hm, &mhdr);
761     ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
762 
763     test_notification(hwnd, "1 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
764     test_notification(hwnd, "1 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
765     test_notification(hwnd, "1 of 2 events", 0, WHATEVER);
766     ok(0==mhdr.dwOffset, "MIDIHDR.dwOffset 1/2 changed to %u\n", mhdr.dwOffset);
767 
768     mhdr.dwOffset = 1234123123;
769     mhdr.dwBytesRecorded = 2*sizeof(MIDISHORTEVENT);
770 
771     rc = playStream(hm, &mhdr);
772     ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
773 
774     test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
775     test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
776     test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
777     test_notification(hwnd, "2 of 2 events", 0, WHATEVER);
778     ok(sizeof(MIDISHORTEVENT)==mhdr.dwOffset, "MIDIHDR.dwOffset 2/2 changed to %u\n", mhdr.dwOffset);
779     ok(mhdr.dwBytesRecorded == 2*sizeof(MIDISHORTEVENT), "dwBytesRecorded changed to %u\n", mhdr.dwBytesRecorded);
780 
781     strmNops[0].dwEvent &= ~MEVT_F_CALLBACK;
782     strmNops[1].dwEvent &= ~MEVT_F_CALLBACK;
783     mhdr.dwOffset = 1234123123;
784     rc = playStream(hm, &mhdr);
785     ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
786 
787     test_notification(hwnd, "0 CB in 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
788     test_notification(hwnd, "0 CB in 2 events", 0, WHATEVER);
789     /* w9X/me/nt set dwOffset to the position played last */
790     ok(1234123123==mhdr.dwOffset || broken(sizeof(MIDISHORTEVENT)==mhdr.dwOffset), "MIDIHDR.dwOffset nocb changed to %u\n", mhdr.dwOffset);
791 
792     mhdr.dwBytesRecorded = mhdr.dwBufferLength-1;
793     rc = playStream(hm, &mhdr);
794     ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBytesRecorded modulo MIDIEVENT rc=%s\n", mmsys_error(rc));
795     if (!rc) {
796          test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
797     }
798 
799     mhdr.dwBytesRecorded = mhdr.dwBufferLength+1;
800     rc = playStream(hm, &mhdr);
801     ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBufferLength<dwBytesRecorded rc=%s\n", mmsys_error(rc));
802     test_notification(hwnd, "past MIDIHDR tests", 0, WHATEVER);
803 
804     rc = midiStreamStop(hm);
805     ok(!rc, "midiStreamStop rc=%s\n", mmsys_error(rc));
806     ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
807 
808     rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
809     ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
810     ok(0==strmNops[0].dwStreamID, "dwStreamID[0] set to %x\n", strmNops[0].dwStreamID);
811     ok(0==strmNops[1].dwStreamID, "dwStreamID[1] set to %x\n", strmNops[1].dwStreamID);
812 
813     mhdr.dwBufferLength = 70000; /* > 64KB! */
814     mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
815     ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
816     if (mhdr.lpData) {
817         mhdr.dwFlags = 0;
818         /* PrepareHeader detects the too large buffer is for a stream. */
819         rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
820         todo_wine ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare stream too large rc=%s\n", mmsys_error(rc));
821 
822         rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
823         ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
824 
825         HeapFree(GetProcessHeap(), 0, mhdr.lpData);
826     }
827 
828     rc = midiStreamClose(hm);
829     ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
830     test_notification(hwnd, "midiStreamClose", MOM_CLOSE, 0);
831     test_notification(hwnd, "midiStream over", 0, WHATEVER);
832 
833     rc = midiStreamOpen(&hm, &udev, 1, 0, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
834     ok(!rc /*w2k*/|| rc==MMSYSERR_INVALPARAM/*w98*/, "midiStreamOpen NULL function rc=%s\n", mmsys_error(rc));
835     if (!rc) {
836         trace("Device %d accepts NULL CALLBACK_FUNCTION\n", udev);
837         rc = midiStreamClose(hm);
838         ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
839     }
840 
841     rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)0xDEADBEEF, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
842     ok(rc==MMSYSERR_INVALPARAM, "midiStreamOpen bad window rc=%s\n", mmsys_error(rc));
843     if (!rc) {
844         rc = midiStreamClose(hm);
845         ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
846     }
847 
848     /* Test the player time clock and positions */
849     memset(&records, 0, sizeof(records));
850     records.done = CreateEventA(NULL, FALSE, FALSE, NULL);
851     ok(records.done != NULL, "CreateEvent failed (dev=%d)\n", udev);
852 
853     rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)time_stamp_callback, (DWORD_PTR)&records, CALLBACK_FUNCTION);
854     ok(!rc, "midiStreamOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
855 
856     expected = 0;
857     ret = get_position(hm, TIME_MS);
858     ok(ret == expected, "expected %u, got %u\n", expected, ret);
859 
860     memset(&mhdr, 0, sizeof(mhdr));
861     mhdr.lpData = (LPSTR)strmNopsWithDelta;
862     mhdr.dwBytesRecorded = mhdr.dwBufferLength = sizeof(strmNopsWithDelta);
863     rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
864     ok(!rc, "midiOutPrepareHeader(dev=%d) rc=%s\n", udev, mmsys_error(rc));
865 
866     rc = midiStreamOut(hm, &mhdr, sizeof(mhdr));
867     ok(!rc, "midiStreamOut(dev=%d) rc=%s\n", udev, mmsys_error(rc));
868 
869     ret = get_position(hm, TIME_MS);
870     ok(ret == expected, "expected %u, got %u\n", expected, ret);
871 
872     rc = midiStreamRestart(hm);
873     ok(!rc, "midiStreamRestart(dev=%d) rc=%s\n", udev, mmsys_error(rc));
874     Sleep(50);
875 
876     rc = midiStreamPause(hm);
877     ok(!rc, "midiStreamPause(dev=%d) rc=%s\n", udev, mmsys_error(rc));
878 
879     expected = 50;
880     ret = get_position(hm, TIME_MS);
881     ok(ret >= expected && ret < expected + MARGIN, "expected %ums or greater, got %ums\n", expected, ret);
882     expected = ret;
883 
884     Sleep(100);
885 
886     ret = get_position(hm, TIME_MS);
887     ok(ret == expected, "expected %ums, got %ums\n", expected, ret);
888 
889     rc = midiStreamRestart(hm);
890     ok(!rc, "midiStreamRestart(dev=%d) rc=%s\n", udev, mmsys_error(rc));
891 
892     Sleep(1);
893     ret = get_position(hm, TIME_MS);
894     ok(ret > expected && ret < expected + MARGIN, "expected greater than %ums, got %ums\n", expected, ret);
895     expected = ret;
896 
897     ret = get_position(hm, TIME_TICKS);
898     ok(ret > strmNopsWithDelta[0].dwDeltaTime && ret < strmNopsWithDelta[1].dwDeltaTime,
899        "TIME_TICKS position is continuous, got %u\n", ret);
900 
901     /* shouldn't set time division property while playing */
902     midiprop.tdiv.cbStruct  = sizeof(midiprop.tdiv);
903     midiprop.tdiv.dwTimeDiv = 24;
904     rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_SET | MIDIPROP_TIMEDIV);
905     ok(rc == MMSYSERR_INVALPARAM, "midiStreamProperty(SET|TIMEDIV, dev=%d) rc=%s\n", udev, mmsys_error(rc));
906 
907     ret = WaitForSingleObject(records.done, INFINITE);
908     ok(ret == WAIT_OBJECT_0, "WaitForSingleObject failed, got %d\n", ret);
909 
910     rc = midiStreamPause(hm);
911     ok(!rc, "midiStreamPause(dev=%d) rc=%s\n", udev, mmsys_error(rc));
912 
913     expected = 250; /* = 12 ticks in 120 BPM */
914     ret = get_position(hm, TIME_MS);
915     ok(ret >= expected - MARGIN && ret <= expected + MARGIN,
916        "expected greater than %ums, got %ums\n", expected, ret);
917     trace("after playing, got %ums\n", ret);
918 
919     /* set tempo to 240 BPM */
920     midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
921     midiprop.tempo.dwTempo = 250000;
922     rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_SET | MIDIPROP_TEMPO);
923     ok(!rc, "midiStreamProperty(SET|TEMPO, dev=%d) rc=%s\n", udev, mmsys_error(rc));
924 
925     /* a tempo change doesn't affect elapsed ticks */
926     ret = get_position(hm, TIME_TICKS);
927     ok(ret >= strmNopsWithDelta[1].dwDeltaTime && ret < strmNopsWithDelta[1].dwDeltaTime + 3,
928        "expected %u ticks, got %u\n", strmNopsWithDelta[1].dwDeltaTime, ret);
929 
930     midiprop.tdiv.cbStruct = sizeof(midiprop.tdiv);
931     rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET | MIDIPROP_TIMEDIV);
932     ok(!rc, "midiStreamProperty(GET|TIMEDIV, dev=%d) rc=%s\n", udev, mmsys_error(rc));
933     ok(midiprop.tdiv.dwTimeDiv == 24, "expected 24, got %u\n", midiprop.tdiv.dwTimeDiv);
934 
935     /* TIME_MIDI value is a quarter of TIME_TICKS, rounded */
936     expected = (ret + midiprop.tdiv.dwTimeDiv/8) / (midiprop.tdiv.dwTimeDiv/4);
937     ret = get_position(hm, TIME_MIDI);
938     ok(ret == expected, "expected song pointer %u, got %u\n", expected, ret);
939 
940     ok(records.count == 2, "expected 2 MM_MOM_DONE messages, got %d\n", records.count);
941 
942     /* Time between midiStreamPause and midiStreamRestart isn't counted.
943        So, the second event happens at dwDeltaTime(250ms) + 100ms after the first event. */
944     expected = 250 + 100;
945     diff = records.time_stamp[1] - records.time_stamp[0];
946     ok(diff >= expected - MARGIN && diff <= expected + MARGIN,
947        "expected %u ~ %ums, got %ums (dev=%d)\n", expected - MARGIN, expected + MARGIN, diff, udev);
948 
949     rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
950     ok(!rc, "midiOutUnprepareHeader(dev=%d) rc=%s\n", udev, mmsys_error(rc));
951 
952     rc = midiStreamStop(hm);
953     ok(!rc, "midiStreamStop(dev=%d) rc=%s\n", udev, mmsys_error(rc));
954 
955     ret = get_position(hm, TIME_MS);
956     ok(ret == 0, "expected 0ms, got %ums\n", ret);
957 
958     midiprop.tdiv.cbStruct  = sizeof(midiprop.tdiv);
959     midiprop.tdiv.dwTimeDiv = 0xe204; /* 30 fps, 4 ticks/frame */
960     rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_SET | MIDIPROP_TIMEDIV);
961     ok(!rc, "midiStreamProperty(SET|TIMEDIV, dev=%d) rc=%s\n", udev, mmsys_error(rc));
962 
963     test_position(hm, TIME_MS,      TIME_MS);
964     test_position(hm, TIME_TICKS,   TIME_TICKS);
965     test_position(hm, TIME_MIDI,    TIME_MS);
966     todo_wine test_position(hm, TIME_SMPTE,   TIME_SMPTE);
967     test_position(hm, TIME_SAMPLES, TIME_MS);
968     test_position(hm, TIME_BYTES,   TIME_MS);
969 
970     rc = midiStreamClose(hm);
971     ok(!rc, "midiStreamClose(dev=%d) rc=%s\n", udev, mmsys_error(rc));
972     CloseHandle(records.done);
973 
974     rc = midiOutGetDevCapsA((UINT_PTR)udev, &capsA, sizeof(capsA));
975     ok(!rc, "midiOutGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
976     if (!strncmp(capsA.szPname, fluidsynth_prefix, strlen(fluidsynth_prefix)) ||
977         (udev == MIDIMAPPER && found_fluidsynth)) {
978         found_fluidsynth = TRUE;
979         skip("FluidSynth (at least 1.1.6) doesn't support desired System Exclusive message.\n");
980         return;
981     }
982 
983 #define ROUNDUP4(n) (((n) + 3) & ~3)
984     hm = NULL;
985     if (hwnd)
986         rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
987     else
988         rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
989     ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
990     test_notification(hwnd, "midiStreamOpen", MOM_OPEN, 0);
991 
992     midiprop.tdiv.cbStruct  = sizeof(midiprop.tdiv);
993     midiprop.tdiv.dwTimeDiv = 480;
994     rc = midiStreamProperty(hm, (LPBYTE)&midiprop, MIDIPROP_TIMEDIV | MIDIPROP_SET);
995     ok(!rc, "midiStreamProperty(TimeDiv) rc=%s\n", mmsys_error(rc));
996 
997     rc = midiStreamRestart(hm);
998     ok(!rc, "midiStreamRestart rc=%s\n", mmsys_error(rc));
999 
1000     memset(&mhdr, 0, sizeof(mhdr));
1001     mhdr.dwBufferLength = sizeof(MIDISHORTEVENT) * 5 + ROUNDUP4(sizeof(SysEx_reset)) +
1002         ROUNDUP4(sizeof(SysEx_volume_off)) + ROUNDUP4(sizeof(SysEx_volume_full));
1003     mhdr.lpData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, mhdr.dwBufferLength);
1004     ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
1005     if (mhdr.lpData) {
1006         MIDIEVENT *e;
1007         char *p = mhdr.lpData;
1008         /* GM System ON */
1009         e = (MIDIEVENT *)p;
1010         e->dwEvent = MEVT_F_LONG | sizeof(SysEx_reset);
1011         memcpy(&e->dwParms[0], SysEx_reset, sizeof(SysEx_reset));
1012         p += sizeof(MIDISHORTEVENT) + ROUNDUP4(sizeof(SysEx_reset));
1013         /* Master volume: off */
1014         e = (MIDIEVENT *)p;
1015         e->dwDeltaTime = 96;
1016         e->dwEvent = MEVT_F_LONG | sizeof(SysEx_volume_off);
1017         memcpy(&e->dwParms[0], SysEx_volume_off, sizeof(SysEx_volume_off));
1018         p += sizeof(MIDISHORTEVENT) + ROUNDUP4(sizeof(SysEx_volume_off));
1019         /* Note On (We can't hear this voice due to volume settings) */
1020         e = (MIDIEVENT *)p;
1021         e->dwEvent = MEVT_F_SHORT | 0x6F4593; /* note #69 */
1022         p += sizeof(MIDISHORTEVENT);
1023         /* Note Off */
1024         e = (MIDIEVENT *)p;
1025         e->dwDeltaTime = 240;
1026         e->dwEvent = MEVT_F_SHORT | 0x004593; /* velocity 0 */
1027         p += sizeof(MIDISHORTEVENT);
1028         /* Master volume: full */
1029         e = (MIDIEVENT *)p;
1030         e->dwEvent = MEVT_F_LONG | sizeof(SysEx_volume_full);
1031         memcpy(&e->dwParms[0], SysEx_volume_full, sizeof(SysEx_volume_full));
1032         p += sizeof(MIDISHORTEVENT) + ROUNDUP4(sizeof(SysEx_volume_full));
1033         mhdr.dwBytesRecorded = (DWORD)(p - mhdr.lpData);
1034 #undef ROUNDUP4
1035 
1036         rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
1037         ok(!rc, "midiOutPrepareHeader rc=%s\n", mmsys_error(rc));
1038 
1039         rc = playStream(hm, &mhdr);
1040         ok(!rc, "midiStreamOut rc=%s\n", mmsys_error(rc));
1041         test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
1042 
1043         rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
1044         ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
1045 
1046         HeapFree(GetProcessHeap(), 0, mhdr.lpData);
1047     }
1048     rc = midiStreamClose(hm);
1049     ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
1050     test_notification(hwnd, "midiStreamClose", MOM_CLOSE, 0);
1051 }
1052 
1053 static BOOL scan_subkeys(HKEY parent, const LPCSTR *sub_keys)
1054 {
1055     char name[64];
1056     DWORD index = 0;
1057     DWORD name_len = sizeof(name);
1058     BOOL found_vmware = FALSE;
1059 
1060     if (sub_keys[0] == NULL)
1061     {
1062        /* We're at the deepest level, check "Identifier" value now */
1063        char *test;
1064        if (RegQueryValueExA(parent, "Identifier", NULL, NULL, (LPBYTE) name, &name_len) != ERROR_SUCCESS)
1065            return FALSE;
1066        for (test = name; test < name + lstrlenA(name) - 6 && ! found_vmware; test++)
1067        {
1068            char c = test[6];
1069            test[6] = '\0';
1070            found_vmware = (lstrcmpiA(test, "VMware") == 0);
1071            test[6] = c;
1072        }
1073        return found_vmware;
1074     }
1075 
1076     while (RegEnumKeyExA(parent, index, name, &name_len, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
1077            ! found_vmware) {
1078         char c = name[lstrlenA(sub_keys[0])];
1079         name[lstrlenA(sub_keys[0])] = '\0';
1080         if (lstrcmpiA(name, sub_keys[0]) == 0) {
1081             HKEY sub_key;
1082             name[lstrlenA(sub_keys[0])] = c;
1083             if (RegOpenKeyExA(parent, name, 0, KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE, &sub_key) == ERROR_SUCCESS) {
1084                 found_vmware = scan_subkeys(sub_key, sub_keys + 1);
1085                 RegCloseKey(sub_key);
1086             }
1087         }
1088 
1089         name_len = sizeof(name);
1090         index++;
1091     }
1092 
1093     return found_vmware;
1094 }
1095 
1096 /*
1097  * Usual method to detect whether running inside a VMware virtual machine involves direct port I/O requiring
1098  * some assembly and an exception handler. Can't do that in Wine tests. Alternative method of querying WMI
1099  * is not available on NT4. So instead we look at the device map and check the Identifier value in the
1100  * registry keys HKLM\HARDWARE\DEVICEMAP\SCSI\Scsi Port x\Scsi Bus x\Target Id x\Logical Unit Id x (where
1101  * x is some number). If the Identifier value contains the string "VMware" we assume running in a VMware VM.
1102  */
1103 static BOOL on_vmware(void)
1104 {
1105     static const LPCSTR sub_keys[] = { "Scsi Port ", "Scsi Bus ", "Target Id ", "Logical Unit Id ", NULL };
1106     HKEY scsi;
1107     BOOL found_vmware = FALSE;
1108 
1109     if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\Scsi", 0, KEY_ENUMERATE_SUB_KEYS, &scsi) != ERROR_SUCCESS)
1110         return FALSE;
1111 
1112     found_vmware = scan_subkeys(scsi, sub_keys);
1113 
1114     RegCloseKey(scsi);
1115 
1116     return found_vmware;
1117 }
1118 
1119 static void test_midi_outfns(HWND hwnd)
1120 {
1121     HMIDIOUT hm;
1122     MMRESULT rc;
1123     UINT udev, ndevs = midiOutGetNumDevs();
1124 
1125     rc = midiOutOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
1126     ok(rc==MMSYSERR_BADDEVICEID, "midiOutOpen udev>max rc=%s\n", mmsys_error(rc));
1127     if (!rc) {
1128         rc = midiOutClose(hm);
1129         ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
1130     }
1131     if (!ndevs) {
1132         MIDIOUTCAPSA capsA;
1133         skip("Found no MIDI out device\n");
1134 
1135         rc = midiOutGetDevCapsA(MIDIMAPPER, &capsA, sizeof(capsA));
1136         /* GetDevCaps and Open must return compatible results */
1137         ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiOutGetDevCaps MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
1138 
1139         rc = midiOutOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
1140         todo_wine_if (rc == MIDIERR_INVALIDSETUP) /* Wine without snd-seq */
1141             ok(rc == MMSYSERR_BADDEVICEID || broken(rc == MMSYSERR_NODRIVER /*w2k sound disabled*/),
1142                "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
1143         if (!rc) {
1144             rc = midiOutClose(hm);
1145             ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
1146         }
1147         return;
1148     }
1149     trace("Found %d MIDI OUT devices\n", ndevs);
1150 
1151     test_midi_mci(hwnd);
1152 
1153     for (udev=0; udev < ndevs; udev++) {
1154         MIDIOUTCAPSA capsA;
1155         rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
1156         if (rc || strcmp(capsA.szPname, "Creative Sound Blaster MPU-401") != 0 || ! on_vmware()) {
1157             trace("** Testing device %d\n", udev);
1158             test_midiOut_device(udev, hwnd);
1159             Sleep(800); /* Let the synth rest */
1160             test_midiStream(udev, hwnd);
1161             Sleep(800);
1162         }
1163         else
1164             win_skip("Skipping this device on VMware, driver problem\n");
1165     }
1166     trace("** Testing MIDI mapper\n");
1167     test_midiOut_device(MIDIMAPPER, hwnd);
1168     Sleep(800);
1169     test_midiStream(MIDIMAPPER, hwnd);
1170 }
1171 
1172 START_TEST(midi)
1173 {
1174     HWND hwnd = 0;
1175 
1176     CoInitialize(NULL); /* Needed for Win 10 */
1177 
1178     if (1) /* select 1 for CALLBACK_WINDOW or 0 for CALLBACK_FUNCTION */
1179     hwnd = CreateWindowExA(0, "static", "winmm midi test", WS_POPUP, 0,0,100,100,
1180                            0, 0, 0, NULL);
1181     test_midi_infns(hwnd);
1182     test_midi_outfns(hwnd);
1183     if (hwnd) DestroyWindow(hwnd);
1184 
1185     CoUninitialize();
1186 }
1187