ddraw/tests: Properly release no longer used interfaces.
[wine/multimedia.git] / dlls / winmm / tests / midi.c
blobf02c4f0fd15ea07e5c66048364b813bf5e8659e7
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.dwUser = 0x56FA552C;
132 mhdr.dwBufferLength = 70000; /* > 64KB! */
133 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
134 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
135 if (mhdr.lpData) {
136 rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
137 ok(rc==MMSYSERR_INVALPARAM, "midiInPrepare tiny rc=%s\n", mmsys_error(rc));
138 rc = midiInPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
139 ok(!rc, "midiInPrepare old size rc=%s\n", mmsys_error(rc));
140 rc = midiInPrepareHeader(hm, &mhdr, sizeof(mhdr));
141 ok(!rc, "midiInPrepare rc=%s\n", mmsys_error(rc));
142 rc = midiInUnprepareHeader(hm, &mhdr, sizeof(mhdr));
143 ok(!rc, "midiInUnprepare rc=%s\n", mmsys_error(rc));
144 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
146 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
148 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
150 rc = midiInReset(hm); /* Return any pending buffer */
151 ok(!rc, "midiInReset rc=%s\n", mmsys_error(rc));
153 rc = midiInClose(hm);
154 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
155 test_notification(hwnd, "midiInClose", MIM_CLOSE, 0);
156 test_notification(hwnd, "midiIn over", 0, WHATEVER);
159 static void test_midi_infns(HWND hwnd)
161 HMIDIIN hm;
162 MMRESULT rc;
163 UINT udev, ndevs = midiInGetNumDevs();
165 rc = midiInOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
166 ok(rc==MMSYSERR_BADDEVICEID, "midiInOpen udev>max rc=%s\n", mmsys_error(rc));
167 if (!rc) {
168 rc = midiInClose(hm);
169 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
171 if (!ndevs) {
172 trace("Found no MIDI IN device\n"); /* no skip for this common situation */
173 rc = midiInOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
174 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiInOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
175 if (!rc) {
176 rc = midiInClose(hm);
177 ok(!rc, "midiInClose rc=%s\n", mmsys_error(rc));
179 return;
181 trace("Found %d MIDI IN devices\n", ndevs);
182 for (udev=0; udev < ndevs; udev++) {
183 trace("** Testing device %d\n", udev);
184 test_midiIn_device(udev, hwnd);
185 Sleep(50);
187 trace("** Testing MIDI mapper\n");
188 test_midiIn_device(MIDIMAPPER, hwnd);
192 static void test_midi_mci(HWND hwnd)
194 MCIERROR err;
195 char buf[1024];
196 memset(buf, 0, sizeof(buf));
198 err = mciSendString("sysinfo sequencer quantity", buf, sizeof(buf), hwnd);
199 ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
200 if (!err) trace("Found %s MCI sequencer devices\n", buf);
202 if (!strcmp(buf, "0")) return;
204 err = mciSendString("capability sequencer can record", buf, sizeof(buf), hwnd);
205 ok(!err, "mci sysinfo sequencer quantity returned %d\n", err);
206 if(!err) ok(!strcmp(buf, "false"), "capability can record is %s\n", buf);
210 static void test_midiOut_device(UINT udev, HWND hwnd)
212 HMIDIOUT hm;
213 MMRESULT rc;
214 MIDIOUTCAPSA capsA;
215 DWORD ovolume;
216 UINT udevid;
217 MIDIHDR mhdr;
219 rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
220 ok(!rc, "midiOutGetDevCaps(dev=%d) rc=%s\n", udev, mmsys_error(rc));
221 if (!rc) {
222 trace("* %s: manufacturer=%d, product=%d, tech=%d, support=%X: %d voices, %d notes\n",
223 capsA.szPname, capsA.wMid, capsA.wPid, capsA.wTechnology, capsA.dwSupport, capsA.wVoices, capsA.wNotes);
224 ok(!((MIDIMAPPER==udev) ^ (MOD_MAPPER==capsA.wTechnology)), "technology %d on device %d\n", capsA.wTechnology, udev);
225 if (MOD_MIDIPORT == capsA.wTechnology) {
226 ok(capsA.wVoices == 0 && capsA.wNotes == 0, "external device with notes or voices\n");
227 ok(capsA.wChannelMask == 0xFFFF, "external device channel mask %x\n", capsA.wChannelMask);
228 ok(!(capsA.dwSupport & (MIDICAPS_VOLUME|MIDICAPS_LRVOLUME|MIDICAPS_CACHE)), "external device support=%X\n", capsA.dwSupport);
232 if (hwnd)
233 rc = midiOutOpen(&hm, udev, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
234 else
235 rc = midiOutOpen(&hm, udev, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
236 if (rc == MMSYSERR_NOTSUPPORTED)
238 skip( "MIDI out not supported\n" );
239 return;
241 ok(!rc, "midiOutOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
242 if (rc) return;
244 test_notification(hwnd, "midiOutOpen", MOM_OPEN, 0);
246 rc = midiOutGetVolume(hm, &ovolume);
247 ok((capsA.dwSupport & MIDICAPS_VOLUME) ? rc==MMSYSERR_NOERROR : rc==MMSYSERR_NOTSUPPORTED, "midiOutGetVolume rc=%s\n", mmsys_error(rc));
248 /* The native mapper responds with FFFFFFFF initially,
249 * real devices with the volume GUI SW-synth settings. */
250 if (!rc) trace("Current volume %x on device %d\n", ovolume, udev);
252 /* The W95 ESFM Synthesis device reports NOTENABLED although
253 * GetVolume by handle works and music plays. */
254 rc = midiOutGetVolume(UlongToHandle(udev), &ovolume);
255 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));
257 rc = midiOutGetVolume(hm, NULL);
258 ok(rc==MMSYSERR_INVALPARAM, "midiOutGetVolume NULL rc=%s\n", mmsys_error(rc));
260 /* Tests with midiOutSetvolume show that the midi mapper forwards
261 * the value to the real device, but Get initially always reports
262 * FFFFFFFF. Therefore, a Get+SetVolume pair with the mapper is
263 * not adequate to restore the value prior to tests.
265 if (winetest_interactive && (capsA.dwSupport & MIDICAPS_VOLUME)) {
266 DWORD volume2 = (ovolume < 0x80000000) ? 0xC000C000 : 0x40004000;
267 rc = midiOutSetVolume(hm, volume2);
268 ok(!rc, "midiOutSetVolume rc=%s\n", mmsys_error(rc));
269 if (!rc) {
270 DWORD volume3;
271 rc = midiOutGetVolume(hm, &volume3);
272 ok(!rc, "midiOutGetVolume new rc=%s\n", mmsys_error(rc));
273 if (!rc) trace("New volume %x on device %d\n", volume3, udev);
274 todo_wine ok(volume2==volume3, "volume Set %x = Get %x\n", volume2, volume3);
276 rc = midiOutSetVolume(hm, ovolume);
277 ok(!rc, "midiOutSetVolume restore rc=%s\n", mmsys_error(rc));
280 rc = midiOutGetDevCapsA((UINT_PTR)hm, &capsA, sizeof(capsA));
281 ok(!rc, "midiOutGetDevCaps(dev=%d) by handle rc=%s\n", udev, mmsys_error(rc));
282 rc = midiInGetDevCapsA((UINT_PTR)hm, (LPMIDIINCAPSA)&capsA, sizeof(DWORD));
283 ok(rc==MMSYSERR_BADDEVICEID, "midiInGetDevCaps(dev=%d) by out handle rc=%s\n", udev, mmsys_error(rc));
285 { DWORD e = 0x006F4893; /* velocity, note (#69 would be 440Hz) channel */
286 trace("ShortMsg type %x\n", LOBYTE(LOWORD(e)));
287 rc = midiOutShortMsg(hm, e);
288 ok(!rc, "midiOutShortMsg rc=%s\n", mmsys_error(rc));
289 if (!rc) Sleep(400); /* Hear note */
292 memset(&mhdr, 0, sizeof(mhdr));
293 mhdr.dwUser = 0x56FA552C;
294 mhdr.dwOffset = 0xDEADBEEF;
295 mhdr.dwBufferLength = 70000; /* > 64KB! */
296 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
297 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
298 if (mhdr.lpData) {
299 rc = midiOutLongMsg(hm, &mhdr, sizeof(mhdr));
300 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
301 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
303 rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
304 ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
305 rc = midiOutPrepareHeader(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
306 ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
307 rc = midiOutPrepareHeader(hm, &mhdr, sizeof(mhdr));
308 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
309 rc = midiOutUnprepareHeader(hm, &mhdr, sizeof(mhdr));
310 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
311 trace("MIDIHDR flags=%x when unsent\n", mhdr.dwFlags);
313 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
315 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
316 ok(mhdr.dwOffset==0xDEADBEEF, "MIDIHDR.dwOffset changed to %x\n", mhdr.dwOffset);
318 rc = midiOutGetID(hm, &udevid);
319 ok(!rc, "midiOutGetID rc=%s\n", mmsys_error(rc));
320 if(!rc) ok(udevid==udev, "midiOutGetID gives %d, expect %d\n", udevid, udev);
322 rc = midiOutReset(hm); /* Quiet everything */
323 ok(!rc, "midiOutReset rc=%s\n", mmsys_error(rc));
325 rc = midiOutClose(hm);
326 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
327 test_notification(hwnd, "midiOutClose", MOM_CLOSE, 0);
329 rc = midiOutOpen(&hm, udev, 0, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
330 ok(!rc, "midiOutOpen(dev=%d) 0 CALLBACK_WINDOW rc=%s\n", udev, mmsys_error(rc));
331 /* PostMessage(hwnd=0) redirects to PostThreadMessage(GetCurrentThreadId())
332 * which PeekMessage((HWND)-1) queries. */
333 test_notification((HWND)-1, "midiOutOpen WINDOW->THREAD", 0, WHATEVER);
334 test_notification(hwnd, "midiOutOpen WINDOW", 0, WHATEVER);
335 if (!rc) {
336 rc = midiOutClose(hm);
337 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
338 test_notification((HWND)-1, "midiOutClose WINDOW->THREAD", 0, WHATEVER);
339 test_notification(hwnd, "midiOutClose", 0, WHATEVER);
341 test_notification(hwnd, "midiOut over", 0, WHATEVER);
344 static void test_position(HMIDISTRM hm, UINT typein, UINT typeout)
346 MMRESULT rc;
347 MMTIME mmtime;
348 mmtime.wType = typein;
349 rc = midiStreamPosition(hm, &mmtime, sizeof(MMTIME));
350 /* Ugly, but a single ok() herein enables using the todo_wine prefix */
351 ok(!rc && (mmtime.wType == typeout), "midiStreamPosition type %x converted to %x rc=%s\n", typein, mmtime.wType, mmsys_error(rc));
352 if (!rc) switch(mmtime.wType) {
353 case TIME_MS:
354 trace("Stream position %ums\n", mmtime.u.ms);
355 break;
356 case TIME_TICKS:
357 trace("Stream position %u ticks\n", mmtime.u.ticks);
358 break;
359 case TIME_MIDI:
360 trace("Stream position song pointer %u\n", mmtime.u.midi.songptrpos);
361 break;
365 typedef struct midishortevent_tag { /* ideal size for MEVT_F_SHORT event type */
366 DWORD dwDeltaTime;
367 DWORD dwStreamID;
368 DWORD dwEvent;
369 } MIDISHORTEVENT;
371 /* Native crashes on a second run with the const qualifier set on this data! */
372 static BYTE strmEvents[] = { /* A set of variable-sized MIDIEVENT structs */
373 0, 0, 0, 0, 0, 0, 0, 0, /* dwDeltaTime and dwStreamID */
374 0, 0, 0, MEVT_NOP | 0x40, /* with MEVT_F_CALLBACK */
375 0, 0, 0, 0, 0, 0, 0, 0,
376 0xE0, 0x93, 0x04, MEVT_TEMPO, /* 0493E0 == 300000 */
377 0, 0, 0, 0, 0, 0, 0, 0,
378 0x93, 0x48, 0x6F, MEVT_SHORTMSG,
381 static MIDISHORTEVENT strmNops[] = { /* Test callback + dwOffset */
382 { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
383 { 0, 0, (MEVT_NOP <<24)| MEVT_F_CALLBACK },
386 static MMRESULT playStream(HMIDISTRM hm, LPMIDIHDR lpMidiHdr)
388 MMRESULT rc = midiStreamOut(hm, lpMidiHdr, sizeof(MIDIHDR));
389 /* virtual machines may return MIDIERR_STILLPLAYING from the next request
390 * even after MHDR_DONE is set. It's still too early, so add MHDR_INQUEUE. */
391 if (!rc) while (!(lpMidiHdr->dwFlags & MHDR_DONE) || (lpMidiHdr->dwFlags & MHDR_INQUEUE)) { Sleep(100); }
392 return rc;
395 static void test_midiStream(UINT udev, HWND hwnd)
397 HMIDISTRM hm;
398 MMRESULT rc, rc2;
399 MIDIHDR mhdr;
400 union {
401 MIDIPROPTEMPO tempo;
402 MIDIPROPTIMEDIV tdiv;
403 } midiprop;
405 if (hwnd)
406 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)hwnd, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
407 else
408 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)callback_func, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
409 if (rc == MMSYSERR_NOTSUPPORTED)
411 skip( "MIDI stream not supported\n" );
412 return;
414 ok(!rc, "midiStreamOpen(dev=%d) rc=%s\n", udev, mmsys_error(rc));
415 if (rc) return;
417 test_notification(hwnd, "midiStreamOpen", MOM_OPEN, 0);
419 midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
420 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
421 ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
422 ok(midiprop.tempo.dwTempo==500000, "default stream tempo %u microsec per quarter note\n", midiprop.tempo.dwTempo);
424 midiprop.tdiv.cbStruct = sizeof(midiprop.tdiv);
425 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TIMEDIV);
426 ok(!rc, "midiStreamProperty TIMEDIV rc=%s\n", mmsys_error(rc));
427 todo_wine ok(24==LOWORD(midiprop.tdiv.dwTimeDiv), "default stream time division %u\n", midiprop.tdiv.dwTimeDiv);
429 memset(&mhdr, 0, sizeof(mhdr));
430 mhdr.dwUser = 0x56FA552C;
431 mhdr.dwOffset = 1234567890;
432 mhdr.dwBufferLength = sizeof(strmEvents);
433 mhdr.dwBytesRecorded = mhdr.dwBufferLength;
434 mhdr.lpData = (LPSTR)&strmEvents[0];
435 if (mhdr.lpData) {
436 rc = midiOutLongMsg((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
437 ok(rc==MIDIERR_UNPREPARED, "midiOutLongMsg unprepared rc=%s\n", mmsys_error(rc));
438 test_notification(hwnd, "midiOutLong unprepared", 0, WHATEVER);
440 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset)-1);
441 ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare tiny rc=%s\n", mmsys_error(rc));
442 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
443 ok(!rc, "midiOutPrepare old size rc=%s\n", mmsys_error(rc));
444 ok(mhdr.dwFlags & MHDR_PREPARED, "MHDR.dwFlags when prepared %x\n", mhdr.dwFlags);
446 /* The device is still in paused mode and should queue the message. */
447 rc = midiStreamOut(hm, &mhdr, offsetof(MIDIHDR,dwOffset));
448 ok(!rc, "midiStreamOut old size rc=%s\n", mmsys_error(rc));
449 rc2 = rc;
450 trace("MIDIHDR flags=%x when submitted\n", mhdr.dwFlags);
451 /* w9X/me does not set MHDR_ISSTRM when StreamOut exits,
452 * but it will be set on all systems after the job is finished. */
454 Sleep(90);
455 /* Wine <1.1.39 started playing immediately */
456 test_notification(hwnd, "midiStream still paused", 0, WHATEVER);
458 /* MSDN asks to use midiStreamRestart prior to midiStreamOut()
459 * because the starting state is 'pause', but some apps seem to
460 * work with the inverse order: queue everything, then play.
463 rc = midiStreamRestart(hm);
464 ok(!rc, "midiStreamRestart rc=%s\n", mmsys_error(rc));
466 if (!rc2) while(mhdr.dwFlags & MHDR_INQUEUE) {
467 trace("async MIDI still queued\n");
468 Sleep(100);
469 } /* Checking INQUEUE is not the recommended way to wait for the end of a job, but we're testing. */
470 /* MHDR_ISSTRM is not necessarily set when midiStreamOut returns
471 * rather than when the queue is eventually processed. */
472 ok(mhdr.dwFlags & MHDR_ISSTRM, "MHDR.dwFlags %x no ISSTRM when out of queue\n", mhdr.dwFlags);
473 if (!rc2) while(!(mhdr.dwFlags & MHDR_DONE)) {
474 /* Never to be seen except perhaps on multicore */
475 trace("async MIDI still not done\n");
476 Sleep(100);
478 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags %x not DONE when out of queue\n", mhdr.dwFlags);
479 test_notification(hwnd, "midiStream callback", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
480 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
482 /* Native fills dwOffset regardless of the cbMidiHdr size argument to midiStreamOut */
483 ok(1234567890!=mhdr.dwOffset, "play left MIDIHDR.dwOffset at %u\n", mhdr.dwOffset);
485 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
486 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
487 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
488 ok(!rc, "midiOutUnprepare #2 rc=%s\n", mmsys_error(rc));
490 trace("MIDIHDR stream flags=%x when finished\n", mhdr.dwFlags);
491 ok(mhdr.dwFlags & MHDR_DONE, "MHDR.dwFlags when done %x\n", mhdr.dwFlags);
493 test_position(hm, TIME_MS, TIME_MS);
494 test_position(hm, TIME_TICKS, TIME_TICKS);
495 todo_wine test_position(hm, TIME_MIDI, TIME_MIDI);
496 test_position(hm, TIME_SMPTE, TIME_MS);
497 test_position(hm, TIME_SAMPLES, TIME_MS);
498 test_position(hm, TIME_BYTES, TIME_MS);
500 Sleep(400); /* Hear note */
502 midiprop.tempo.cbStruct = sizeof(midiprop.tempo);
503 rc = midiStreamProperty(hm, (void*)&midiprop, MIDIPROP_GET|MIDIPROP_TEMPO);
504 ok(!rc, "midiStreamProperty TEMPO rc=%s\n", mmsys_error(rc));
505 ok(0x0493E0==midiprop.tempo.dwTempo, "stream set tempo %u\n", midiprop.tdiv.dwTimeDiv);
507 rc = midiStreamRestart(hm);
508 ok(!rc, "midiStreamRestart #2 rc=%s\n", mmsys_error(rc));
510 mhdr.dwFlags |= MHDR_ISSTRM;
511 /* Preset flags (e.g. MHDR_ISSTRM) do not disturb. */
512 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
513 ok(!rc, "midiOutPrepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
514 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, offsetof(MIDIHDR,dwOffset));
515 ok(!rc, "midiOutUnprepare used flags %x rc=%s\n", mhdr.dwFlags, mmsys_error(rc));
517 rc = midiStreamRestart(hm);
518 ok(!rc, "midiStreamRestart #3 rc=%s\n", mmsys_error(rc));
520 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
521 ok(0==((MIDISHORTEVENT*)&strmEvents)[0].dwStreamID, "dwStreamID set to %x\n", ((LPMIDIEVENT)&strmEvents[0])->dwStreamID);
523 /* dwBytesRecorded controls how much is played, not dwBufferLength
524 * allowing to immediately forward packets from midiIn to midiOut */
525 mhdr.dwOffset = 1234123123;
526 mhdr.dwBufferLength = sizeof(strmNops);
527 trace("buffer: %u\n", mhdr.dwBufferLength);
528 mhdr.dwBytesRecorded = 0;
529 mhdr.lpData = (LPSTR)&strmNops[0];
530 strmNops[0].dwEvent |= MEVT_F_CALLBACK;
531 strmNops[1].dwEvent |= MEVT_F_CALLBACK;
533 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
534 ok(!rc, "midiOutPrepare rc=%s\n", mmsys_error(rc));
536 rc = playStream(hm, &mhdr);
537 ok(!rc, "midiStreamOut 0 bytes recorded rc=%s\n", mmsys_error(rc));
539 test_notification(hwnd, "midiStreamOut", MOM_DONE, (DWORD_PTR)&mhdr);
540 test_notification(hwnd, "0 bytes recorded", 0, WHATEVER);
542 /* FIXME: check dwOffset within callback
543 * instead of the unspecified value afterwards */
544 ok(1234123123==mhdr.dwOffset || broken(0==mhdr.dwOffset), "play 0 set MIDIHDR.dwOffset to %u\n", mhdr.dwOffset);
545 /* w2k and later only set dwOffset when processing MEVT_T_CALLBACK,
546 * while w9X/me/nt always sets it. Have Wine behave like w2k because the
547 * dwOffset slot does not exist in the small size MIDIHDR. */
549 mhdr.dwOffset = 1234123123;
550 mhdr.dwBytesRecorded = 1*sizeof(MIDISHORTEVENT);
552 rc = playStream(hm, &mhdr);
553 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
555 test_notification(hwnd, "1 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
556 test_notification(hwnd, "1 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
557 test_notification(hwnd, "1 of 2 events", 0, WHATEVER);
558 ok(0==mhdr.dwOffset, "MIDIHDR.dwOffset 1/2 changed to %u\n", mhdr.dwOffset);
560 mhdr.dwOffset = 1234123123;
561 mhdr.dwBytesRecorded = 2*sizeof(MIDISHORTEVENT);
563 rc = playStream(hm, &mhdr);
564 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
566 test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
567 test_notification(hwnd, "2 of 2 events", MOM_POSITIONCB, (DWORD_PTR)&mhdr);
568 test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
569 test_notification(hwnd, "2 of 2 events", 0, WHATEVER);
570 ok(sizeof(MIDISHORTEVENT)==mhdr.dwOffset, "MIDIHDR.dwOffset 2/2 changed to %u\n", mhdr.dwOffset);
571 ok(mhdr.dwBytesRecorded == 2*sizeof(MIDISHORTEVENT), "dwBytesRecorded changed to %u\n", mhdr.dwBytesRecorded);
573 strmNops[0].dwEvent &= ~MEVT_F_CALLBACK;
574 strmNops[1].dwEvent &= ~MEVT_F_CALLBACK;
575 mhdr.dwOffset = 1234123123;
576 rc = playStream(hm, &mhdr);
577 ok(!rc, "midiStreamOut 1 event out of 2 rc=%s\n", mmsys_error(rc));
579 test_notification(hwnd, "0 CB in 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
580 test_notification(hwnd, "0 CB in 2 events", 0, WHATEVER);
581 /* w9X/me/nt set dwOffset to the position played last */
582 ok(1234123123==mhdr.dwOffset || broken(sizeof(MIDISHORTEVENT)==mhdr.dwOffset), "MIDIHDR.dwOffset nocb changed to %u\n", mhdr.dwOffset);
584 mhdr.dwBytesRecorded = mhdr.dwBufferLength-1;
585 rc = playStream(hm, &mhdr);
586 ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBytesRecorded modulo MIDIEVENT rc=%s\n", mmsys_error(rc));
587 if (!rc) {
588 test_notification(hwnd, "2 of 2 events", MOM_DONE, (DWORD_PTR)&mhdr);
591 mhdr.dwBytesRecorded = mhdr.dwBufferLength+1;
592 rc = playStream(hm, &mhdr);
593 ok(rc==MMSYSERR_INVALPARAM,"midiStreamOut dwBufferLength<dwBytesRecorded rc=%s\n", mmsys_error(rc));
594 test_notification(hwnd, "past MIDIHDR tests", 0, WHATEVER);
596 rc = midiStreamStop(hm);
597 ok(!rc, "midiStreamStop rc=%s\n", mmsys_error(rc));
598 ok(mhdr.dwUser==0x56FA552C, "MIDIHDR.dwUser changed to %lx\n", mhdr.dwUser);
600 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
601 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
602 ok(0==strmNops[0].dwStreamID, "dwStreamID[0] set to %x\n", strmNops[0].dwStreamID);
603 ok(0==strmNops[1].dwStreamID, "dwStreamID[1] set to %x\n", strmNops[1].dwStreamID);
605 mhdr.dwBufferLength = 70000; /* > 64KB! */
606 mhdr.lpData = HeapAlloc(GetProcessHeap(), 0 , mhdr.dwBufferLength);
607 ok(mhdr.lpData!=NULL, "No %d bytes of memory!\n", mhdr.dwBufferLength);
608 if (mhdr.lpData) {
609 mhdr.dwFlags = 0;
610 /* PrepareHeader detects the too large buffer is for a stream. */
611 rc = midiOutPrepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
612 todo_wine ok(rc==MMSYSERR_INVALPARAM, "midiOutPrepare stream too large rc=%s\n", mmsys_error(rc));
614 rc = midiOutUnprepareHeader((HMIDIOUT)hm, &mhdr, sizeof(mhdr));
615 ok(!rc, "midiOutUnprepare rc=%s\n", mmsys_error(rc));
617 HeapFree(GetProcessHeap(), 0, mhdr.lpData);
620 rc = midiStreamClose(hm);
621 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
622 test_notification(hwnd, "midiStreamClose", MOM_CLOSE, 0);
623 test_notification(hwnd, "midiStream over", 0, WHATEVER);
625 rc = midiStreamOpen(&hm, &udev, 1, 0, (DWORD_PTR)MYCBINST, CALLBACK_FUNCTION);
626 ok(!rc /*w2k*/|| rc==MMSYSERR_INVALPARAM/*w98*/, "midiStreamOpen NULL function rc=%s\n", mmsys_error(rc));
627 if (!rc) {
628 trace("Device %d accepts NULL CALLBACK_FUNCTION\n", udev);
629 rc = midiStreamClose(hm);
630 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
633 rc = midiStreamOpen(&hm, &udev, 1, (DWORD_PTR)0xDEADBEEF, (DWORD_PTR)MYCBINST, CALLBACK_WINDOW);
634 ok(rc==MMSYSERR_INVALPARAM, "midiStreamOpen bad window rc=%s\n", mmsys_error(rc));
635 if (!rc) {
636 rc = midiStreamClose(hm);
637 ok(!rc, "midiStreamClose rc=%s\n", mmsys_error(rc));
641 static BOOL scan_subkeys(HKEY parent, const LPCSTR *sub_keys)
643 char name[64];
644 DWORD index = 0;
645 DWORD name_len = sizeof(name);
646 BOOL found_vmware = FALSE;
648 if (sub_keys[0] == NULL)
650 /* We're at the deepest level, check "Identifier" value now */
651 char *test;
652 if (RegQueryValueExA(parent, "Identifier", NULL, NULL, (LPBYTE) name, &name_len) != ERROR_SUCCESS)
653 return FALSE;
654 for (test = name; test < name + lstrlenA(name) - 6 && ! found_vmware; test++)
656 char c = test[6];
657 test[6] = '\0';
658 found_vmware = (lstrcmpiA(test, "VMware") == 0);
659 test[6] = c;
661 return found_vmware;
664 while (RegEnumKeyExA(parent, index, name, &name_len, NULL, NULL, NULL, NULL) == ERROR_SUCCESS &&
665 ! found_vmware) {
666 char c = name[lstrlenA(sub_keys[0])];
667 name[lstrlenA(sub_keys[0])] = '\0';
668 if (lstrcmpiA(name, sub_keys[0]) == 0) {
669 HKEY sub_key;
670 name[lstrlenA(sub_keys[0])] = c;
671 if (RegOpenKeyExA(parent, name, 0, KEY_ENUMERATE_SUB_KEYS | KEY_QUERY_VALUE, &sub_key) == ERROR_SUCCESS) {
672 found_vmware = scan_subkeys(sub_key, sub_keys + 1);
673 RegCloseKey(sub_key);
677 name_len = sizeof(name);
678 index++;
681 return found_vmware;
685 * Usual method to detect whether running inside a VMware virtual machine involves direct port I/O requiring
686 * some assembly and an exception handler. Can't do that in Wine tests. Alternative method of querying WMI
687 * is not available on NT4. So instead we look at the device map and check the Identifier value in the
688 * registry keys HKLM\HARDWARE\DEVICEMAP\SCSI\Scsi Port x\Scsi Bus x\Target Id x\Logical Unit Id x (where
689 * x is some number). If the Identifier value contains the string "VMware" we assume running in a VMware VM.
691 static BOOL on_vmware(void)
693 static const LPCSTR sub_keys[] = { "Scsi Port ", "Scsi Bus ", "Target Id ", "Logical Unit Id ", NULL };
694 HKEY scsi;
695 BOOL found_vmware = FALSE;
697 if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\Scsi", 0, KEY_ENUMERATE_SUB_KEYS, &scsi) != ERROR_SUCCESS)
698 return FALSE;
700 found_vmware = scan_subkeys(scsi, sub_keys);
702 RegCloseKey(scsi);
704 return found_vmware;
707 static void test_midi_outfns(HWND hwnd)
709 HMIDIOUT hm;
710 MMRESULT rc;
711 UINT udev, ndevs = midiOutGetNumDevs();
713 rc = midiOutOpen(&hm, ndevs, 0, 0, CALLBACK_NULL);
714 ok(rc==MMSYSERR_BADDEVICEID, "midiOutOpen udev>max rc=%s\n", mmsys_error(rc));
715 if (!rc) {
716 rc = midiOutClose(hm);
717 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
719 if (!ndevs) {
720 MIDIOUTCAPSA capsA;
721 skip("Found no MIDI out device\n");
723 rc = midiOutGetDevCapsA(MIDIMAPPER, &capsA, sizeof(capsA));
724 /* GetDevCaps and Open must return compatible results */
725 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*nt,w2k*/), "midiOutGetDevCaps MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
727 rc = midiOutOpen(&hm, MIDIMAPPER, 0, 0, CALLBACK_NULL);
728 if (rc==MIDIERR_INVALIDSETUP) todo_wine /* Wine without snd-seq */
729 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k*/), "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
730 else
731 ok(rc==MMSYSERR_BADDEVICEID || broken(rc==MMSYSERR_NODRIVER /*w2k sound disabled*/),
732 "midiOutOpen MAPPER with no MIDI rc=%s\n", mmsys_error(rc));
733 if (!rc) {
734 rc = midiOutClose(hm);
735 ok(!rc, "midiOutClose rc=%s\n", mmsys_error(rc));
737 return;
739 trace("Found %d MIDI OUT devices\n", ndevs);
741 test_midi_mci(hwnd);
743 for (udev=0; udev < ndevs; udev++) {
744 MIDIOUTCAPSA capsA;
745 rc = midiOutGetDevCapsA(udev, &capsA, sizeof(capsA));
746 if (rc || strcmp(capsA.szPname, "Creative Sound Blaster MPU-401") != 0 || ! on_vmware()) {
747 trace("** Testing device %d\n", udev);
748 test_midiOut_device(udev, hwnd);
749 Sleep(800); /* Let the synth rest */
750 test_midiStream(udev, hwnd);
751 Sleep(800);
753 else
754 win_skip("Skipping this device on VMware, driver problem\n");
756 trace("** Testing MIDI mapper\n");
757 test_midiOut_device(MIDIMAPPER, hwnd);
758 Sleep(800);
759 test_midiStream(MIDIMAPPER, hwnd);
762 START_TEST(midi)
764 HWND hwnd = 0;
765 if (1) /* select 1 for CALLBACK_WINDOW or 0 for CALLBACK_FUNCTION */
766 hwnd = CreateWindowExA(0, "static", "winmm midi test", WS_POPUP, 0,0,100,100,
767 0, 0, 0, NULL);
768 test_midi_infns(hwnd);
769 test_midi_outfns(hwnd);
770 if (hwnd) DestroyWindow(hwnd);