winmm/tests: Initialize whole MIDIHDR struct before using it.
[wine.git] / dlls / winmm / tests / midi.c
blob972899e3c60f2b37e2310eaaa7cad2eebdb76c3d
1 /*
2 * Test winmm midi
4 * Copyright 2010 Jörg Höhle
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.
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.
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
21 #include <stdio.h>
22 #include <stddef.h>
23 #include "windows.h"
24 #include "mmsystem.h"
25 #include "wine/test.h"
27 extern const char* mmsys_error(MMRESULT error); /* from wave.c */
29 /* Test in order of increasing probability to hang.
30 * On many UNIX systems, the Timidity softsynth provides
31 * MIDI sequencer services and it is not particularly robust.
34 #define MYCBINST 0x4CAFE5A8 /* not used with window or thread callbacks */
35 #define WHATEVER 0xFEEDF00D
37 static BOOL spurious_message(LPMSG msg)
39 /* WM_DEVICECHANGE 0x0219 appears randomly */
40 if(msg->message == WM_DEVICECHANGE) {
41 trace("skipping spurious message %04x\n", msg->message);
42 return TRUE;
44 return FALSE;
47 static UINT cbmsg = 0;
48 static DWORD_PTR cbval1 = WHATEVER;
49 static DWORD_PTR cbval2 = 0;
50 static DWORD_PTR cbinst = MYCBINST;
52 static void CALLBACK callback_func(HWAVEOUT hwo, UINT uMsg,
53 DWORD_PTR dwInstance,
54 DWORD_PTR dwParam1, DWORD_PTR dwParam2)
56 if (winetest_debug>1)
57 trace("Callback! msg=%x %lx %lx\n", uMsg, dwParam1, dwParam2);
58 cbmsg = uMsg;
59 cbval1 = dwParam1; /* mhdr or 0 */
60 cbval2 = dwParam2; /* always 0 */
61 cbinst = dwInstance; /* MYCBINST, see midiOut/StreamOpen */
64 #define test_notification(hwnd, command, m1, p2) test_notification_dbg(hwnd, command, m1, p2, __LINE__)
65 static void test_notification_dbg(HWND hwnd, const char* command, UINT m1, DWORD_PTR p2, int line)
66 { /* Use message type 0 as meaning no message */
67 MSG msg;
68 if (hwnd) {
69 /* msg.wParam is hmidiout, msg.lParam is the mhdr (or 0) */
70 BOOL seen;
71 do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
72 while(seen && spurious_message(&msg));
73 if (m1 && !seen) {
74 /* We observe transient delayed notification, mostly on native.
75 * Perhaps the OS preempts the player thread after setting MHDR_DONE
76 * or clearing MHDR_INQUEUE, before calling DriverCallback. */
77 DWORD rc;
78 trace_(__FILE__,line)("Waiting for delayed message %x from %s\n", m1, command);
79 SetLastError(0xDEADBEEF);
80 rc = MsgWaitForMultipleObjects(0, NULL, FALSE, 3000, QS_POSTMESSAGE);
81 ok_(__FILE__,line)(rc==WAIT_OBJECT_0, "Wait failed: %04x %d\n", rc, GetLastError());
82 seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE);
84 if (seen) {
85 trace_(__FILE__,line)("Message %x, wParam=%lx, lParam=%lx from %s\n",
86 msg.message, msg.wParam, msg.lParam, command);
87 ok_(__FILE__,line)(msg.hwnd==hwnd, "Didn't get the handle to our test window\n");
88 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);
90 else ok_(__FILE__,line)(m1==0, "Expect message %x from %s\n", m1, command);
92 else {
93 /* FIXME: MOM_POSITIONCB and MOM_DONE are so close that a queue is needed. */
94 if (cbmsg) {
95 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);
96 cbmsg = 0; /* Mark as read */
97 cbval1 = cbval2 = WHATEVER;
98 ok_(__FILE__,line)(cbinst==MYCBINST, "callback dwInstance changed to %lx\n", cbinst);
100 else ok_(__FILE__,line)(m1==0, "Expect callback %x from %s\n", m1, command);
105 static void test_midiIn_device(UINT udev, HWND hwnd)
107 HMIDIIN hm;
108 MMRESULT rc;
109 MIDIINCAPSA capsA;
110 MIDIHDR mhdr;
112 rc = midiInGetDevCapsA(udev, &capsA, sizeof(capsA));
113 ok((MIDIMAPPER==udev) ? (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)) : rc==0,
114 "midiInGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
115 if (!rc) {
116 /* MIDI IN capsA.dwSupport may contain garbage, absent in old MS-Windows */
117 trace("* %s: manufacturer=%d, product=%d, support=%X\n", capsA.szPname, capsA.wMid, capsA.wPid, capsA.dwSupport);
120 if (hwnd)
121 rc = midiInOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
122 else
123 rc = midiInOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
124 ok((MIDIMAPPER!=udev) ? rc==0 : (rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/)),
125 "midiInOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
126 if (rc) return;
128 test_notification(hwnd, "midiInOpen", MIM_OPEN, 0);
130 memset(&mhdr, 0, sizeof(mhdr));
131 mhdr.dwFlags = 0;
132 mhdr.dwUser = 0x56FA552C;
133 mhdr.dwBufferLength = 70000; /* > 64KB! */
134 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
135 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
136 if (mhdr.lpData) {
137 rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
138 ok(rc==MMSYSERR_INVALPARAM, "midiInPrepare tiny rc=%s\n", mmsys_error(rc));
139 rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
140 ok(!rc, "midiInPrepare old size rc=%s\n", mmsys_error(rc));
141 rc = midiInPrepareHeader(hm, &mhdr, sizeof(mhdr));
142 ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
143 rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
144 ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
145 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
147 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
149 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
151 rc = midiInReset(hm); /* Return any pending buffer */
152 ok(!rc, "midiInReset rc=%s\n", mmsys_error(rc));
154 rc = midiInClose(hm);
155 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
156 test_notification(hwnd, "midiInClose", MIM_CLOSE, 0);
157 test_notification(hwnd, "midiIn over", 0, WHATEVER);
160 static void test_midi_infns(HWND hwnd)
162 HMIDIIN hm;
163 MMRESULT rc;
164 UINT udev, ndevs = midiInGetNumDevs();
166 rc = midiInOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
167 ok(rc==MMSYSERR_BADDEVICEID, "midiInOpen udev>max rc=%s\n", mmsys_error(rc));
168 if (!rc) {
169 rc = midiInClose(hm);
170 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
172 if (!ndevs) {
173 trace("Found no MIDI IN device\n"); /* no skip for this common situation */
174 rc = midiInOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
175 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiInOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
176 if (!rc) {
177 rc = midiInClose(hm);
178 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
180 return;
182 trace("Found %d MIDI IN devices\n", ndevs);
183 for (udev=0; udev < ndevs; udev++) {
184 trace("** Testing device %d\n", udev);
185 test_midiIn_device(udev, hwnd);
186 Sleep(50);
188 trace("** Testing MIDI mapper\n");
189 test_midiIn_device(MIDIMAPPER, hwnd);
193 static void test_midi_mci(HWND hwnd)
195 MCIERROR err;
196 char buf[1024];
197 memset(buf, 0, sizeof(buf));
199 err = mciSendString("sysinfo sequencer quantity", buf, sizeof(buf), hwnd);
200 ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
201 if (!err) trace("Found %s MCI sequencer devices\n", buf);
205 static void test_midiOut_device(UINT udev, HWND hwnd)
207 HMIDIOUT hm;
208 MMRESULT rc;
209 MIDIOUTCAPSA capsA;
210 DWORD ovolume;
211 UINT udevid;
212 MIDIHDR mhdr;
214 rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
215 ok(!rc, "midiOutGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
216 if (!rc) {
217 trace("* %s: manufacturer=%d, product=%d, tech=%d, support=%X: %d voices, %d notes\n",
218 capsA.szPname, capsA.wMid, capsA.wPid, capsA.wTechnology, capsA.dwSupport, capsA.wVoices, capsA.wNotes);
219 ok(!((MIDIMAPPER==udev) ^ (MOD_MAPPER==capsA.wTechnology)), "technology %d on device %d\n", capsA.wTechnology, udev);
220 if (MOD_MIDIPORT == capsA.wTechnology) {
221 ok(capsA.wVoices == 0 && capsA.wNotes == 0, "external device with notes or voices\n");
222 ok(capsA.wChannelMask == 0xFFFF, "external device channel mask %x\n", capsA.wChannelMask);
223 ok(!(capsA.dwSupport & (MIDICAPS_VOLUME|MIDICAPS_LRVOLUME|MIDICAPS_CACHE)), "external device support=%X\n", capsA.dwSupport);
227 if (hwnd)
228 rc = midiOutOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
229 else
230 rc = midiOutOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
231 if (rc == MMSYSERR_NOTSUPPORTED)
233 skip( "MIDI out not supported\n" );
234 return;
236 ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
237 if (rc) return;
239 test_notification(hwnd, "midiOutOpen", MOM_OPEN, 0);
241 rc = midiOutGetVolume(hm, &ovolume);
242 ok((capsA.dwSupport & MIDICAPS_VOLUME) ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED, "midiOutGetVolume rc=%s\n", mmsys_error(rc));
243 /* The native mapper responds with FFFFFFFF initially,
244 * real devices with the volume GUI SW-synth settings. */
245 if (!rc) trace("Current volume %x on device %d\n", ovolume, udev);
247 /* The W95 ESFM Synthesis device reports NOTENABLED although
248 * GetVolume by handle works and music plays. */
249 rc = midiOutGetVolume(UlongToHandle(udev), &ovolume);
250 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));
252 rc = midiOutGetVolume(hm, NULL);
253 ok(rc==MMSYSERR_INVALPARAM, "midiOutGetVolume NULL rc=%s\n", mmsys_error(rc));
255 /* Tests with midiOutSetvolume show that the midi mapper forwards
256 * the value to the real device, but Get initially always reports
257 * FFFFFFFF. Therefore, a Get+SetVolume pair with the mapper is
258 * not adequate to restore the value prior to tests.
260 if (winetest_interactive && (capsA.dwSupport & MIDICAPS_VOLUME)) {
261 DWORD volume2 = (ovolume < 0x80000000) ? 0xC000C000 : 0x40004000;
262 rc = midiOutSetVolume(hm, volume2);
263 ok(!rc, "midiOutSetVolume rc=%s\n", mmsys_error(rc));
264 if (!rc) {
265 DWORD volume3;
266 rc = midiOutGetVolume(hm, &volume3);
267 ok(!rc, "midiOutGetVolume new rc=%s\n", mmsys_error(rc));
268 if (!rc) trace("New volume %x on device %d\n", volume3, udev);
269 todo_wine ok(volume2==volume3, "volume Set %x = Get %x\n", volume2, volume3);
271 rc = midiOutSetVolume(hm, ovolume);
272 ok(!rc, "midiOutSetVolume restore rc=%s\n", mmsys_error(rc));
275 rc = midiOutGetDevCapsA((UINT_PTR)hm, &capsA, sizeof(capsA));
276 ok(!rc, "midiOutGetDevCaps(dev=%d) by handle rc=%s\n", udev, mmsys_error(rc));
277 rc = midiInGetDevCapsA((UINT_PTR)hm, (LPMIDIINCAPSA)&capsA, sizeof(DWORD));
278 ok(rc==MMSYSERR_BADDEVICEID, "midiInGetDevCaps(dev=%d) by out handle rc=%s\n", udev, mmsys_error(rc));
280 { DWORD e = 0x006F4893; /* velocity, note (#69 would be 440Hz) channel */
281 trace("ShortMsg type %x\n", LOBYTE(LOWORD(e)));
282 rc = midiOutShortMsg(hm, e);
283 ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
284 if (!rc) Sleep(400); /* Hear note */
287 memset(&mhdr, 0, sizeof(mhdr));
288 mhdr.dwFlags = 0;
289 mhdr.dwUser = 0x56FA552C;
290 mhdr.dwOffset = 0xDEADBEEF;
291 mhdr.dwBufferLength = 70000; /* > 64KB! */
292 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
293 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
294 if (mhdr.lpData) {
295 rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
296 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
297 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
299 rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
300 ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
301 rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
302 ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
303 rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
304 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
305 rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
306 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
307 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
309 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
311 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
312 ok(mhdr.dwOffset==0xDEADBEEF, "MIDIHDR.dwOffset changed to %x\n", mhdr.dwOffset);
314 rc = midiOutGetID(hm, &udevid);
315 ok(!rc, "midiOutGetID rc=%s\n", mmsys_error(rc));
316 if(!rc) ok(udevid==udev, "midiOutGetID gives %d, expect %d\n", udevid, udev);
318 rc = midiOutReset(hm); /* Quiet everything */
319 ok(!rc, "midiOutReset rc=%s\n", mmsys_error(rc));
321 rc = midiOutClose(hm);
322 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
323 test_notification(hwnd, "midiOutClose", MOM_CLOSE, 0);
324 test_notification(hwnd, "midiOut over", 0, WHATEVER);
327 static void test_position(HMIDISTRM hm, UINT typein, UINT typeout)
329 MMRESULT rc;
330 MMTIME mmtime;
331 mmtime.wType = typein;
332 rc = midiStreamPosition(hm, &mmtime, sizeof(MMTIME));
333 /* Ugly, but a single ok() herein enables using the todo_wine prefix */
334 ok(!rc && (mmtime.wType == typeout), "midiStreamPosition type %x converted to %x rc=%s\n", typein, mmtime.wType, mmsys_error(rc));
335 if (!rc) switch(mmtime.wType) {
336 case TIME_MS:
337 trace("Stream position %ums\n", mmtime.u.ms);
338 break;
339 case TIME_TICKS:
340 trace("Stream position %u ticks\n", mmtime.u.ticks);
341 break;
342 case TIME_MIDI:
343 trace("Stream position song pointer %u\n", mmtime.u.midi.songptrpos);
344 break;
348 typedef struct midishortevent_tag { /* ideal size for MEVT_F_SHORT event type */
349 DWORD dwDeltaTime;
350 DWORD dwStreamID;
351 DWORD dwEvent;
352 } MIDISHORTEVENT;
354 /* Native crashes on a second run with the const qualifier set on this data! */
355 static BYTE strmEvents[] = { /* A set of variable-sized MIDIEVENT structs */
356 0, 0, 0, 0, 0, 0, 0, 0, /* dwDeltaTime and dwStreamID */
357 0, 0, 0, MEVT_NOP | 0x40, /* with MEVT_F_CALLBACK */
358 0, 0, 0, 0, 0, 0, 0, 0,
359 0xE0, 0x93, 0x04, MEVT_TEMPO, /* 0493E0 == 300000 */
360 0, 0, 0, 0, 0, 0, 0, 0,
361 0x93, 0x48, 0x6F, MEVT_SHORTMSG,
364 static MIDISHORTEVENT strmNops[] = { /* Test callback + dwOffset */
365 { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
366 { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
369 static MMRESULT playStream(HMIDISTRM hm, LPMIDIHDR lpMidiHdr)
371 MMRESULT rc = midiStreamOut(hm, lpMidiHdr, sizeof(MIDIHDR));
372 /* virtual machines may return MIDIERR_STILLPLAYING from the next request
373 * even after MHDR_DONE is set. It's still too early, so add MHDR_INQUEUE. */
374 if (!rc) while (!(lpMidiHdr->dwFlags & MHDR_DONE) || (lpMidiHdr->dwFlags & MHDR_INQUEUE)) { Sleep(100); }
375 return rc;
378 static void test_midiStream(UINT udev, HWND hwnd)
380 HMIDISTRM hm;
381 MMRESULT rc, rc2;
382 MIDIHDR mhdr;
383 union {
384 MIDIPROPTEMPO tempo;
385 MIDIPROPTIMEDIV tdiv;
386 } midiprop;
388 if (hwnd)
389 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
390 else
391 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
392 if (rc == MMSYSERR_NOTSUPPORTED)
394 skip( "MIDI stream not supported\n" );
395 return;
397 ok(!rc, "midiStreamOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
398 if (rc) return;
400 test_notification(hwnd, "midiStreamOpen", MOM_OPEN, 0);
402 midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
403 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
404 ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
405 ok(midiprop.tempo.dwTempo==500000, "default stream tempo %u microsec per quarter note\n", midiprop.tempo.dwTempo);
407 midiprop.tdiv.cbStruct = sizeof(midiprop.tdiv);
408 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TIMEDIV);
409 ok(!rc, "midiStreamProperty TIMEDIV rc=%s\n", mmsys_error(rc));
410 todo_wine ok(24==LOWORD(midiprop.tdiv.dwTimeDiv), "default stream time division %u\n", midiprop.tdiv.dwTimeDiv);
412 memset(&mhdr, 0, sizeof(mhdr));
413 mhdr.dwFlags = 0;
414 mhdr.dwUser = 0x56FA552C;
415 mhdr.dwOffset = 1234567890;
416 mhdr.dwBufferLength = sizeof(strmEvents);
417 mhdr.dwBytesRecorded = mhdr.dwBufferLength;
418 mhdr.lpData = (LPSTR)&strmEvents[0];
419 if (mhdr.lpData) {
420 rc = midiOutLongMsg((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
421 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
422 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
424 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
425 ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
426 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
427 ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
428 ok(mhdr.dwFlags & MHDR_PREPARED, "MHDR.dwFlags when prepared %x\n", mhdr.dwFlags);
430 /* The device is still in paused mode and should queue the message. */
431 rc = midiStreamOut(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
432 ok(!rc, "midiStreamOut old size rc=%s\n", mmsys_error(rc));
433 rc2 = rc;
434 trace("MIDIHDR flags=%x when submitted\n", mhdr.dwFlags);
435 /* w9X/me does not set MHDR_ISSTRM when StreamOut exits,
436 * but it will be set on all systems after the job is finished. */
438 Sleep(90);
439 /* Wine <1.1.39 started playing immediately */
440 test_notification(hwnd, "midiStream still paused", 0, WHATEVER);
442 /* MSDN asks to use midiStreamRestart prior to midiStreamOut()
443 * because the starting state is 'pause', but some apps seem to
444 * work with the inverse order: queue everything, then play.
447 rc = midiStreamRestart(hm);
448 ok(!rc, "midiStreamRestart rc=%s\n", mmsys_error(rc));
450 if (!rc2) while(mhdr.dwFlags & MHDR_INQUEUE) {
451 trace("async MIDI still queued\n");
452 Sleep(100);
453 } /* Checking INQUEUE is not the recommended way to wait for the end of a job, but we're testing. */
454 /* MHDR_ISSTRM is not necessarily set when midiStreamOut returns
455 * rather than when the queue is eventually processed. */
456 ok(mhdr.dwFlags & MHDR_ISSTRM, "MHDR.dwFlags %x no ISSTRM when out of queue\n", mhdr.dwFlags);
457 if (!rc2) while(!(mhdr.dwFlags & MHDR_DONE)) {
458 /* Never to be seen except perhaps on multicore */
459 trace("async MIDI still not done\n");
460 Sleep(100);
462 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags %x not DONE when out of queue\n", mhdr.dwFlags);
463 test_notification(hwnd, "midiStream callback", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
464 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
466 /* Native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
467 ok(1234567890!=mhdr.dwOffset, "play left MIDIHDR.dwOffset at %u\n", mhdr.dwOffset);
469 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
470 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
471 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
472 ok(!rc, "midiOutUnprepare #2 rc=%s\n", mmsys_error(rc));
474 trace("MIDIHDR stream flags=%x when finished\n", mhdr.dwFlags);
475 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags when done %x\n", mhdr.dwFlags);
477 test_position(hm, TIME_MS, TIME_MS);
478 test_position(hm, TIME_TICKS, TIME_TICKS);
479 todo_wine test_position(hm, TIME_MIDI, TIME_MIDI);
480 test_position(hm, TIME_SMPTE, TIME_MS);
481 test_position(hm, TIME_SAMPLES, TIME_MS);
482 test_position(hm, TIME_BYTES, TIME_MS);
484 Sleep(400); /* Hear note */
486 midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
487 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
488 ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
489 ok(0x0493E0==midiprop.tempo.dwTempo, "stream set tempo %u\n", midiprop.tdiv.dwTimeDiv);
491 rc = midiStreamRestart(hm);
492 ok(!rc, "midiStreamRestart #2 rc=%s\n", mmsys_error(rc));
494 mhdr.dwFlags |= MHDR_ISSTRM;
495 /* Preset flags (e.g. MHDR_ISSTRM) do not disturb. */
496 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
497 ok(!rc, "midiOutPrepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
498 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
499 ok(!rc, "midiOutUnprepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
501 rc = midiStreamRestart(hm);
502 ok(!rc, "midiStreamRestart #3 rc=%s\n", mmsys_error(rc));
504 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
505 ok(0==((MIDISHORTEVENT*)&strmEvents)[0].dwStreamID, "dwStreamID set to %x\n", ((LPMIDIEVENT)&strmEvents[0])->dwStreamID);
507 /* dwBytesRecorded controls how much is played, not dwBufferLength
508 * allowing to immediately forward packets from midiIn to midiOut */
509 mhdr.dwOffset = 1234123123;
510 mhdr.dwBufferLength = sizeof(strmNops);
511 trace("buffer: %u\n", mhdr.dwBufferLength);
512 mhdr.dwBytesRecorded = 0;
513 mhdr.lpData = (LPSTR)&strmNops[0];
514 strmNops[0].dwEvent |= MEVT_F_CALLBACK;
515 strmNops[1].dwEvent |= MEVT_F_CALLBACK;
517 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
518 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
520 rc = playStream(hm, &mhdr);
521 ok(!rc, "midiStreamOut 0 bytes recorded rc=%s\n", mmsys_error(rc));
523 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
524 test_notification(hwnd, "0 bytes recorded", 0, WHATEVER);
526 /* FIXME: check dwOffset within callback
527 * instead of the unspecified value afterwards */
528 ok(1234123123==mhdr.dwOffset || broken(0==mhdr.dwOffset), "play 0 set MIDIHDR.dwOffset to %u\n", mhdr.dwOffset);
529 /* w2k and later only set dwOffset when processing MEVT_T_CALLBACK,
530 * while w9X/me/nt always sets it. Have Wine behave like w2k because the
531 * dwOffset slot does not exist in the small size MIDIHDR. */
533 mhdr.dwOffset = 1234123123;
534 mhdr.dwBytesRecorded = 1*sizeof(MIDISHORTEVENT);
536 rc = playStream(hm, &mhdr);
537 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
539 test_notification(hwnd, "1 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
540 test_notification(hwnd, "1 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
541 test_notification(hwnd, "1 of 2 events", 0, WHATEVER);
542 ok(0==mhdr.dwOffset, "MIDIHDR.dwOffset 1/2 changed to %u\n", mhdr.dwOffset);
544 mhdr.dwOffset = 1234123123;
545 mhdr.dwBytesRecorded = 2*sizeof(MIDISHORTEVENT);
547 rc = playStream(hm, &mhdr);
548 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
550 test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
551 test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
552 test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
553 test_notification(hwnd, "2 of 2 events", 0, WHATEVER);
554 ok(sizeof(MIDISHORTEVENT)==mhdr.dwOffset, "MIDIHDR.dwOffset 2/2 changed to %u\n", mhdr.dwOffset);
555 ok(mhdr.dwBytesRecorded == 2*sizeof(MIDISHORTEVENT), "dwBytesRecorded changed to %u\n", mhdr.dwBytesRecorded);
557 strmNops[0].dwEvent &= ~MEVT_F_CALLBACK;
558 strmNops[1].dwEvent &= ~MEVT_F_CALLBACK;
559 mhdr.dwOffset = 1234123123;
560 rc = playStream(hm, &mhdr);
561 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
563 test_notification(hwnd, "0 CB in 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
564 test_notification(hwnd, "0 CB in 2 events", 0, WHATEVER);
565 /* w9X/me/nt set dwOffset to the position played last */
566 ok(1234123123==mhdr.dwOffset || broken(sizeof(MIDISHORTEVENT)==mhdr.dwOffset), "MIDIHDR.dwOffset nocb changed to %u\n", mhdr.dwOffset);
568 mhdr.dwBytesRecorded = mhdr.dwBufferLength-1;
569 rc = playStream(hm, &mhdr);
570 ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBytesRecorded modulo MIDIEVENT rc=%s\n", mmsys_error(rc));
571 if (!rc) {
572 test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
575 mhdr.dwBytesRecorded = mhdr.dwBufferLength+1;
576 rc = playStream(hm, &mhdr);
577 ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBufferLength<dwBytesRecorded rc=%s\n", mmsys_error(rc));
578 test_notification(hwnd, "past MIDIHDR tests", 0, WHATEVER);
580 rc = midiStreamStop(hm);
581 ok(!rc, "midiStreamStop rc=%s\n", mmsys_error(rc));
582 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
584 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
585 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
586 ok(0==strmNops[0].dwStreamID, "dwStreamID[0] set to %x\n", strmNops[0].dwStreamID);
587 ok(0==strmNops[1].dwStreamID, "dwStreamID[1] set to %x\n", strmNops[1].dwStreamID);
589 mhdr.dwBufferLength = 70000; /* > 64KB! */
590 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
591 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
592 if (mhdr.lpData) {
593 mhdr.dwFlags = 0;
594 /* PrepareHeader detects the too large buffer is for a stream. */
595 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
596 todo_wine ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare stream too large rc=%s\n", mmsys_error(rc));
598 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
599 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
601 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
604 rc = midiStreamClose(hm);
605 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
606 test_notification(hwnd, "midiStreamClose", MOM_CLOSE, 0);
607 test_notification(hwnd, "midiStream over", 0, WHATEVER);
609 rc = midiStreamOpen(&hm, &udev, 1, 0, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
610 ok(!rc /*w2k*/|| rc==MMSYSERR_INVALPARAM/*w98*/, "midiStreamOpen NULL function rc=%s\n", mmsys_error(rc));
611 if (!rc) {
612 trace("Device %d accepts NULL CALLBACK_FUNCTION\n", udev);
613 rc = midiStreamClose(hm);
614 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
617 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)0xDEADBEEF, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
618 ok(rc==MMSYSERR_INVALPARAM, "midiStreamOpen bad window rc=%s\n", mmsys_error(rc));
619 if (!rc) {
620 rc = midiStreamClose(hm);
621 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
625 static void test_midi_outfns(HWND hwnd)
627 HMIDIOUT hm;
628 MMRESULT rc;
629 UINT udev, ndevs = midiOutGetNumDevs();
631 rc = midiOutOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
632 ok(rc==MMSYSERR_BADDEVICEID, "midiOutOpen udev>max rc=%s\n", mmsys_error(rc));
633 if (!rc) {
634 rc = midiOutClose(hm);
635 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
637 if (!ndevs) {
638 MIDIOUTCAPSA capsA;
639 skip("Found no MIDI out device\n");
641 rc = midiOutGetDevCapsA(MIDIMAPPER, &capsA, sizeof(capsA));
642 /* GetDevCaps and Open must return compatible results */
643 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiOutGetDevCaps MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
645 rc = midiOutOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
646 if (rc==MIDIERR_INVALIDSETUP) todo_wine /* Wine without snd-seq */
647 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k*/), "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
648 else
649 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k sound disabled*/),
650 "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
651 if (!rc) {
652 rc = midiOutClose(hm);
653 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
655 return;
657 trace("Found %d MIDI OUT devices\n", ndevs);
659 test_midi_mci(hwnd);
661 for (udev=0; udev < ndevs; udev++) {
662 trace("** Testing device %d\n", udev);
663 test_midiOut_device(udev, hwnd);
664 Sleep(800); /* Let the synth rest */
665 test_midiStream(udev, hwnd);
666 Sleep(800);
668 trace("** Testing MIDI mapper\n");
669 test_midiOut_device(MIDIMAPPER, hwnd);
670 Sleep(800);
671 test_midiStream(MIDIMAPPER, hwnd);
674 START_TEST(midi)
676 HWND hwnd = 0;
677 if (1) /* select 1 for CALLBACK_WINDOW or 0 for CALLBACK_FUNCTION */
678 hwnd = CreateWindowExA(0, "static", "winmm midi test", WS_POPUP, 0,0,100,100,
679 0, 0, 0, NULL);
680 test_midi_infns(hwnd);
681 test_midi_outfns(hwnd);
682 if (hwnd) DestroyWindow(hwnd);