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