user32: Use the stored color and mask bitmaps instead of the raw bits in GetIconInfo.
[wine] / dlls / winmm / tests / mci.c
1 /*
2  * Test winmm mci
3  *
4  * Copyright 2006 Jan Zerebecki
5  *           2009 Jörg Höhle
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #include <stdio.h>
23 #include "windows.h"
24 #include "mmsystem.h"
25 #include "mmreg.h"
26 #include "wine/test.h"
27
28 /* The tests use the MCI's own save capability to create the tempfile.wav to play.
29  * To use a pre-existing file, write-protect it. */
30 static MCIERROR ok_saved = MCIERR_FILE_NOT_FOUND;
31
32 typedef union {
33       MCI_STATUS_PARMS    status;
34       MCI_WAVE_SET_PARMS  set;
35       MCI_WAVE_OPEN_PARMS open;
36       MCI_GETDEVCAPS_PARMS caps;
37       MCI_SYSINFO_PARMS   sys;
38       MCI_SEEK_PARMS      seek;
39       MCI_GENERIC_PARMS   gen;
40     } MCI_PARMS_UNION;
41
42 static const char* dbg_mcierr(MCIERROR err)
43 {
44      switch (err) {
45      case 0: return "0=NOERROR";
46 #define X(label) case label: return #label ;
47      X(MCIERR_INVALID_DEVICE_ID)
48      X(MCIERR_UNRECOGNIZED_KEYWORD)
49      X(MCIERR_UNRECOGNIZED_COMMAND)
50      X(MCIERR_HARDWARE)
51      X(MCIERR_INVALID_DEVICE_NAME)
52      X(MCIERR_OUT_OF_MEMORY)
53      X(MCIERR_DEVICE_OPEN)
54      X(MCIERR_CANNOT_LOAD_DRIVER)
55      X(MCIERR_MISSING_COMMAND_STRING)
56      X(MCIERR_PARAM_OVERFLOW)
57      X(MCIERR_MISSING_STRING_ARGUMENT)
58      X(MCIERR_BAD_INTEGER)
59      X(MCIERR_PARSER_INTERNAL)
60      X(MCIERR_DRIVER_INTERNAL)
61      X(MCIERR_MISSING_PARAMETER)
62      X(MCIERR_UNSUPPORTED_FUNCTION)
63      X(MCIERR_FILE_NOT_FOUND)
64      X(MCIERR_DEVICE_NOT_READY)
65      X(MCIERR_INTERNAL)
66      X(MCIERR_DRIVER)
67      X(MCIERR_CANNOT_USE_ALL)
68      X(MCIERR_MULTIPLE)
69      X(MCIERR_EXTENSION_NOT_FOUND)
70      X(MCIERR_OUTOFRANGE)
71      X(MCIERR_FLAGS_NOT_COMPATIBLE)
72      X(MCIERR_FILE_NOT_SAVED)
73      X(MCIERR_DEVICE_TYPE_REQUIRED)
74      X(MCIERR_DEVICE_LOCKED)
75      X(MCIERR_DUPLICATE_ALIAS)
76      X(MCIERR_BAD_CONSTANT)
77      X(MCIERR_MUST_USE_SHAREABLE)
78      X(MCIERR_MISSING_DEVICE_NAME)
79      X(MCIERR_BAD_TIME_FORMAT)
80      X(MCIERR_NO_CLOSING_QUOTE)
81      X(MCIERR_DUPLICATE_FLAGS)
82      X(MCIERR_INVALID_FILE)
83      X(MCIERR_NULL_PARAMETER_BLOCK)
84      X(MCIERR_UNNAMED_RESOURCE)
85      X(MCIERR_NEW_REQUIRES_ALIAS)
86      X(MCIERR_NOTIFY_ON_AUTO_OPEN)
87      X(MCIERR_NO_ELEMENT_ALLOWED)
88      X(MCIERR_NONAPPLICABLE_FUNCTION)
89      X(MCIERR_ILLEGAL_FOR_AUTO_OPEN)
90      X(MCIERR_FILENAME_REQUIRED)
91      X(MCIERR_EXTRA_CHARACTERS)
92      X(MCIERR_DEVICE_NOT_INSTALLED)
93      X(MCIERR_GET_CD)
94      X(MCIERR_SET_CD)
95      X(MCIERR_SET_DRIVE)
96      X(MCIERR_DEVICE_LENGTH)
97      X(MCIERR_DEVICE_ORD_LENGTH)
98      X(MCIERR_NO_INTEGER)
99      X(MCIERR_WAVE_OUTPUTSINUSE)
100      X(MCIERR_WAVE_SETOUTPUTINUSE)
101      X(MCIERR_WAVE_INPUTSINUSE)
102      X(MCIERR_WAVE_SETINPUTINUSE)
103      X(MCIERR_WAVE_OUTPUTUNSPECIFIED)
104      X(MCIERR_WAVE_INPUTUNSPECIFIED)
105      X(MCIERR_WAVE_OUTPUTSUNSUITABLE)
106      X(MCIERR_WAVE_SETOUTPUTUNSUITABLE)
107      X(MCIERR_WAVE_INPUTSUNSUITABLE)
108      X(MCIERR_WAVE_SETINPUTUNSUITABLE)
109      X(MCIERR_SEQ_DIV_INCOMPATIBLE)
110      X(MCIERR_SEQ_PORT_INUSE)
111      X(MCIERR_SEQ_PORT_NONEXISTENT)
112      X(MCIERR_SEQ_PORT_MAPNODEVICE)
113      X(MCIERR_SEQ_PORT_MISCERROR)
114      X(MCIERR_SEQ_TIMER)
115      X(MCIERR_SEQ_PORTUNSPECIFIED)
116      X(MCIERR_SEQ_NOMIDIPRESENT)
117      X(MCIERR_NO_WINDOW)
118      X(MCIERR_CREATEWINDOW)
119      X(MCIERR_FILE_READ)
120      X(MCIERR_FILE_WRITE)
121      X(MCIERR_NO_IDENTITY)
122 #undef X
123      default: {
124          static char name[20]; /* Not to be called twice in a parameter list! */
125          sprintf(name, "MMSYSERR %u", err);
126          return name;
127          }
128      }
129 }
130
131 static BOOL spurious_message(LPMSG msg)
132 {
133   /* WM_DEVICECHANGE 0x0219 appears randomly */
134   if(msg->message != MM_MCINOTIFY) {
135     trace("skipping spurious message %04x\n",msg->message);
136     return TRUE;
137   }
138   return FALSE;
139 }
140
141 /* A single ok() in each code path allows to prefix this with todo_wine */
142 #define test_notification(hwnd, command, type) test_notification_dbg(hwnd, command, type, __LINE__)
143 static void test_notification_dbg(HWND hwnd, const char* command, WPARAM type, int line)
144 {   /* Use type 0 as meaning no message */
145     MSG msg;
146     BOOL seen;
147     do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
148     while(seen && spurious_message(&msg));
149     if(type && !seen) {
150       /* We observe transient delayed notification, mostly on native.
151        * Notification is not always present right when mciSend returns. */
152       trace_(__FILE__,line)("Waiting for delayed notification from %s\n", command);
153       MsgWaitForMultipleObjects(0, NULL, FALSE, 3000, QS_POSTMESSAGE);
154       seen = PeekMessageA(&msg, hwnd, MM_MCINOTIFY, MM_MCINOTIFY, PM_REMOVE);
155     }
156     if(!seen)
157       ok_(__FILE__,line)(type==0, "Expect message %04lx from %s\n", type, command);
158     else if(msg.hwnd != hwnd)
159         ok_(__FILE__,line)(msg.hwnd == hwnd, "Didn't get the handle to our test window\n");
160     else if(msg.message != MM_MCINOTIFY)
161         ok_(__FILE__,line)(msg.message == MM_MCINOTIFY, "got %04x instead of MM_MCINOTIFY from command %s\n", msg.message, command);
162     else ok_(__FILE__,line)(msg.wParam == type, "got %04lx instead of MCI_NOTIFY_xyz %04lx from command %s\n", msg.wParam, type, command);
163 }
164
165 static void test_openCloseWAVE(HWND hwnd)
166 {
167     MCIERROR err;
168     MCI_PARMS_UNION parm;
169     const char command_open[] = "open new type waveaudio alias mysound notify";
170     const char command_close_my[] = "close mysound notify";
171     const char command_close_all[] = "close all notify";
172     const char command_sysinfo[] = "sysinfo waveaudio quantity open";
173     char buf[1024];
174     DWORD intbuf[3] = { 0xDEADF00D, 99, 0xABADCAFE };
175     memset(buf, 0, sizeof(buf));
176     test_notification(hwnd, "-prior to any command-", 0);
177
178     /* Avoid Sysinfo quantity with notify because Win9x and newer differ. */
179     err = mciSendString("sysinfo all quantity", buf, sizeof(buf), hwnd);
180     ok(!err,"mci sysinfo all quantity returned %s\n", dbg_mcierr(err));
181     if(!err) trace("[MCI] with %s drivers\n", buf);
182
183     parm.sys.lpstrReturn = (LPSTR)&intbuf[1];
184     parm.sys.dwRetSize = sizeof(DWORD);
185     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
186     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_QUANTITY | MCI_WAIT, (DWORD_PTR)&parm);
187     ok(!err,"mciSendCommand(MCI_ALL_DEVICE_ID, MCI_SYSINFO QUANTITY, MCI_WAIT, 0) returned %s\n", dbg_mcierr(err));
188     if(!err) ok(atoi(buf)==intbuf[1],"sysinfo all quantity string and command differ\n");
189
190     parm.sys.dwRetSize = sizeof(DWORD)-1;
191     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_QUANTITY, (DWORD_PTR)&parm);
192     ok(err==MCIERR_PARAM_OVERFLOW || broken(!err/* Win9x */),"mciSendCommand MCI_SYSINFO with too small buffer returned %s\n", dbg_mcierr(err));
193
194     err = mciSendString("open new type waveaudio alias r shareable", buf, sizeof(buf), NULL);
195     ok(err==MCIERR_UNSUPPORTED_FUNCTION,"mci open new shareable returned %s\n", dbg_mcierr(err));
196     if(!err) {
197         err = mciSendString("close r", NULL, 0, NULL);
198         ok(!err,"mci close shareable returned %s\n", dbg_mcierr(err));
199     }
200
201     err = mciSendString(command_open, buf, sizeof(buf), hwnd);
202     ok(!err,"mci %s returned %s\n", command_open, dbg_mcierr(err));
203     ok(!strcmp(buf,"1"), "mci open deviceId: %s, expected 1\n", buf);
204     /* Wine<=1.1.33 used to ignore anything past alias XY */
205     test_notification(hwnd,"open new alias notify",MCI_NOTIFY_SUCCESSFUL);
206
207     err = mciSendString("status mysound time format", buf, sizeof(buf), hwnd);
208     ok(!err,"mci status time format returned %s\n", dbg_mcierr(err));
209     if(!err) {
210         if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) == LANG_ENGLISH)
211             ok(!strcmp(buf,"milliseconds"), "mci status time format: %s\n", buf);
212         else trace("locale-dependent time format: %s (ms)\n", buf);
213     }
214
215     memset(buf, 0, sizeof(buf));
216     parm.sys.dwNumber = 1;
217     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
218     parm.sys.lpstrReturn = buf;
219     parm.sys.dwRetSize = sizeof(buf);
220     parm.sys.dwCallback = (DWORD_PTR)hwnd;
221     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN | MCI_NOTIFY, (DWORD_PTR)&parm);
222     ok(!err,"mciCommand MCI_SYSINFO all name 1 open notify: %s\n", dbg_mcierr(err));
223     if(!err) ok(!strcmp(buf,"mysound"), "sysinfo name returned %s\n", buf);
224     test_notification(hwnd, "SYSINFO name notify\n", MCI_NOTIFY_SUCCESSFUL);
225
226     memset(buf, 0, sizeof(buf));
227     parm.sys.dwNumber = 1;
228     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
229     parm.sys.lpstrReturn = buf;
230     parm.sys.dwRetSize = 8; /* mysound\0 */
231     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
232     ok(!err,"mciCommand MCI_SYSINFO all name 1 open buffer[8]: %s\n", dbg_mcierr(err));
233     if(!err) ok(!strcmp(buf,"mysound"), "sysinfo name returned %s\n", buf);
234
235     /* dwRetSize counts characters, not bytes, despite what MSDN says. */
236     parm.sys.dwNumber = 1;
237     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
238     parm.sys.lpstrReturn = buf;
239     parm.sys.dwRetSize = 8; /* mysound\0 */
240     /* MCI_..._PARMSA and PARMSW share the same layout, use one for both tests. */
241     err = mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
242     ok(!err || broken(err==MMSYSERR_NOTSUPPORTED/* Win9x */), "mciCommandW MCI_SYSINFO all name 1 open buffer[8]: %s\n", dbg_mcierr(err));
243     /* TODO strcmpW((LPWSTR)buf,"mysound") */
244
245     parm.sys.dwNumber = 1;
246     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
247     parm.sys.lpstrReturn = buf;
248     parm.sys.dwRetSize = 7; /* too short for mysound\0 */
249     err = mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
250     ok(err==MCIERR_PARAM_OVERFLOW || broken(err==MMSYSERR_NOTSUPPORTED/* Win9x */), "mciCommandW MCI_SYSINFO all name 1 open too small: %s\n", dbg_mcierr(err));
251
252     /* Win9x overwrites the tiny buffer and returns success, newer versions signal overflow. */
253     memset(buf, 0, sizeof(buf));
254     parm.sys.dwNumber = 1;
255     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
256     parm.sys.lpstrReturn = buf;
257     parm.sys.dwRetSize = 2; /* too short for mysound\0 */
258     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
259     ok(err==MCIERR_PARAM_OVERFLOW || broken(!err /* Win9x */),"mciCommand MCI_SYSINFO all name 1 open too small: %s\n", dbg_mcierr(err));
260     if(!err) ok(!strcmp(buf,"mysound"), "sysinfo short name returned %s\n", buf);
261
262     err = mciGetDeviceID("all");
263     ok(MCI_ALL_DEVICE_ID==err || /* Win9x */(UINT16)MCI_ALL_DEVICE_ID==err,"mciGetDeviceID all returned %u, expected %d\n", err, MCI_ALL_DEVICE_ID);
264
265     err = mciSendString(command_close_my, NULL, 0, hwnd);
266     ok(!err,"mci %s returned %s\n", command_close_my, dbg_mcierr(err));
267     test_notification(hwnd, command_close_my, MCI_NOTIFY_SUCCESSFUL);
268     Sleep(5);
269     test_notification(hwnd, command_close_my, 0);
270
271     err = mciSendString("open no-such-file-exists.wav alias y", buf, sizeof(buf), NULL);
272     ok(err==MCIERR_FILE_NOT_FOUND,"open no-such-file.wav returned %s\n", dbg_mcierr(err));
273     if(!err) {
274         err = mciSendString("close y", NULL, 0, NULL);
275         ok(!err,"close y returned %s\n", dbg_mcierr(err));
276     }
277
278     err = mciSendString("open no-such-dir\\file.wav alias y type waveaudio", buf, sizeof(buf), NULL);
279     ok(err==MCIERR_FILE_NOT_FOUND || broken(err==MCIERR_INVALID_FILE /* Win9X */),"open no-such-dir/file.wav returned %s\n", dbg_mcierr(err));
280     if(!err) {
281         err = mciSendString("close y", NULL, 0, NULL);
282         ok(!err,"close y returned %s\n", dbg_mcierr(err));
283     }
284
285     err = mciSendString(command_close_all, NULL, 0, NULL);
286     ok(!err,"mci %s (without buffer) returned %s\n", command_close_all, dbg_mcierr(err));
287
288     memset(buf, 0, sizeof(buf));
289     err = mciSendString(command_close_all, buf, sizeof(buf), hwnd);
290     ok(!err,"mci %s (with output buffer) returned %s\n", command_close_all, dbg_mcierr(err));
291     ok(buf[0] == 0, "mci %s changed output buffer: %s\n", command_close_all, buf);
292     /* No notification left, everything closed already */
293     test_notification(hwnd, command_close_all, 0);
294     /* TODO test close all sends one notification per open device */
295
296     memset(buf, 0, sizeof(buf));
297     err = mciSendString(command_sysinfo, buf, sizeof(buf), NULL);
298     ok(!err,"mci %s returned %s\n", command_sysinfo, dbg_mcierr(err));
299     ok(buf[0] == '0' && buf[1] == 0, "mci %s, expected output buffer '0', got: '%s'\n", command_sysinfo, buf);
300
301     err = mciSendString("open new type waveaudio", buf, sizeof(buf), NULL);
302     ok(err==MCIERR_NEW_REQUIRES_ALIAS,"mci open new without alias returned %s\n", dbg_mcierr(err));
303
304     parm.open.lpstrDeviceType = (LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
305     err = mciSendCommand(0, MCI_OPEN,
306         MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID,
307         (DWORD_PTR)&parm);
308     ok(!err,"mciCommand OPEN_TYPE_ID waveaudio: %s\n", dbg_mcierr(err));
309
310     if(!err) {
311         MCIDEVICEID wDeviceID = parm.open.wDeviceID;
312         parm.caps.dwItem = MCI_GETDEVCAPS_DEVICE_TYPE;
313         err = mciSendCommand(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
314         ok(!err,"mciCommand MCI_GETDEVCAPS device type: %s\n", dbg_mcierr(err));
315         ok(MCI_DEVTYPE_WAVEFORM_AUDIO==parm.caps.dwReturn,"mciCommand GETDEVCAPS says %u, expected %u\n", parm.caps.dwReturn, MCI_DEVTYPE_WAVEFORM_AUDIO);
316     }
317
318     ok(0xDEADF00D==intbuf[0] && 0xABADCAFE==intbuf[2],"DWORD buffer corruption\n");
319
320     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0); /* from MSDN */
321     ok(!err,"mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0) returned %s\n", dbg_mcierr(err));
322
323     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_NOTIFY, 0);
324     ok(!err,"mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_NOTIFY, 0) returned %s\n", dbg_mcierr(err));
325
326     parm.gen.dwCallback = (DWORD_PTR)hwnd;
327     err = mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_NOTIFY, (DWORD_PTR)&parm);
328     ok(!err,"mciSendCommand(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_NOTIFY, hwnd) returned %s\n", dbg_mcierr(err));
329     test_notification(hwnd, command_close_all, 0); /* None left */
330 }
331
332 static void test_recordWAVE(HWND hwnd)
333 {
334     WORD nch    = 1;
335     WORD nbits  = 16;
336     DWORD nsamp = 16000, expect;
337     UINT ndevs  = waveInGetNumDevs();
338     MCIERROR err, ok_pcm;
339     MCIDEVICEID wDeviceID;
340     MCI_PARMS_UNION parm;
341     char buf[1024];
342     memset(buf, 0, sizeof(buf));
343     test_notification(hwnd, "-prior to recording-", 0);
344
345     parm.open.lpstrDeviceType = "waveaudio";
346     parm.open.lpstrElementName = ""; /* "new" at the command level */
347     parm.open.lpstrAlias = "x"; /* to enable mciSendString */
348     parm.open.dwCallback = (DWORD_PTR)hwnd;
349     err = mciSendCommand(0, MCI_OPEN,
350         MCI_OPEN_ELEMENT | MCI_OPEN_TYPE | MCI_OPEN_ALIAS | MCI_NOTIFY,
351         (DWORD_PTR)&parm);
352     ok(!err,"mciCommand open new type waveaudio alias x notify: %s\n", dbg_mcierr(err));
353     wDeviceID = parm.open.wDeviceID;
354
355     err = mciGetDeviceID("x");
356     ok(err==wDeviceID,"mciGetDeviceID x returned %u, expected %u\n", err, wDeviceID);
357
358     /* Only the alias is looked up. */
359     err = mciGetDeviceID("waveaudio");
360     todo_wine ok(err==0,"mciGetDeviceID waveaudio returned %u, expected 0\n", err);
361
362     /* In Wine, both MCI_Open and the individual drivers send notifications. */
363     test_notification(hwnd, "open new", MCI_NOTIFY_SUCCESSFUL);
364     todo_wine test_notification(hwnd, "open new no #2", 0);
365
366     /* Do not query time format as string because result depends on locale! */
367     parm.status.dwItem = MCI_STATUS_TIME_FORMAT;
368     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
369     ok(!err,"mciCommand status time format: %s\n", dbg_mcierr(err));
370     ok(parm.status.dwReturn==MCI_FORMAT_MILLISECONDS,"status time format: %ld\n",parm.status.dwReturn);
371
372     /* Info file fails until named in Open or Save. */
373     err = mciSendString("info x file", buf, sizeof(buf), NULL);
374     todo_wine ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci info new file returned %s\n", dbg_mcierr(err));
375
376     /* Check the default recording: 8-bits per sample, mono, 11kHz */
377     err = mciSendString("status x samplespersec", buf, sizeof(buf), NULL);
378     ok(!err,"mci status samplespersec returned %s\n", dbg_mcierr(err));
379     if(!err) ok(!strcmp(buf,"11025"), "mci status samplespersec expected 11025, got: %s\n", buf);
380
381     /* MCI seems to solely support PCM, no need for ACM conversion. */
382     err = mciSendString("set x format tag 2", NULL, 0, NULL);
383     ok(err==MCIERR_OUTOFRANGE,"mci set format tag 2 returned %s\n", dbg_mcierr(err));
384
385     /* MCI appears to scan the available devices for support of this format,
386      * returning MCIERR_OUTOFRANGE on machines with no sound.
387      * However some w2k8/w7 machines return no error when there's no wave
388      * input device (perhaps querying waveOutGetNumDevs instead of waveIn?),
389      * still the record command below fails with MCIERR_WAVE_INPUTSUNSUITABLE.
390      * Don't skip here, record will fail below. */
391     err = mciSendString("set x format tag pcm", NULL, 0, NULL);
392     ok(!err || err==MCIERR_OUTOFRANGE,"mci set format tag pcm returned %s\n", dbg_mcierr(err));
393     ok_pcm = err;
394
395     /* MSDN warns against not setting all wave format parameters.
396      * Indeed, it produces strange results, incl.
397      * inconsistent PCMWAVEFORMAT headers in the saved file.
398      */
399     err = mciSendString("set x bytespersec 22050 alignment 2 samplespersec 11025 channels 1 bitspersample 16", NULL, 0, NULL);
400     ok(err==ok_pcm,"mci set 5 wave parameters returned %s\n", dbg_mcierr(err));
401     /* Investigate: on w2k, set samplespersec 22050 sets nChannels to 2!
402      *  err = mciSendString("set x samplespersec 22050", NULL, 0, NULL);
403      *  ok(!err,"mci set samplespersec returned %s\n", dbg_mcierr(err));
404      */
405
406     /* Checks are generally performed immediately. */
407     err = mciSendString("set x bitspersample 4", NULL, 0, NULL);
408     todo_wine ok(err==MCIERR_OUTOFRANGE,"mci set bitspersample 4 returned %s\n", dbg_mcierr(err));
409
410     parm.set.wFormatTag = WAVE_FORMAT_PCM;
411     parm.set.nSamplesPerSec = nsamp;
412     parm.set.wBitsPerSample = nbits;
413     parm.set.nChannels      = nch;
414     parm.set.nBlockAlign    = parm.set.nChannels * parm.set.wBitsPerSample /8;
415     parm.set.nAvgBytesPerSec= parm.set.nSamplesPerSec * parm.set.nBlockAlign;
416     err = mciSendCommand(wDeviceID, MCI_SET,
417         MCI_WAVE_SET_SAMPLESPERSEC | MCI_WAVE_SET_CHANNELS |
418         MCI_WAVE_SET_BITSPERSAMPLE | MCI_WAVE_SET_BLOCKALIGN |
419         MCI_WAVE_SET_AVGBYTESPERSEC| MCI_WAVE_SET_FORMATTAG, (DWORD_PTR)&parm);
420     ok(err==ok_pcm,"mciCommand set wave format: %s\n", dbg_mcierr(err));
421
422     parm.caps.dwItem = MCI_WAVE_GETDEVCAPS_INPUTS;
423     parm.caps.dwCallback = (DWORD_PTR)hwnd;
424     err = mciSendCommand(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM | MCI_NOTIFY, (DWORD_PTR)&parm);
425     ok(!err,"mciCommand MCI_GETDEVCAPS inputs: %s\n", dbg_mcierr(err));
426     ok(parm.caps.dwReturn==ndevs,"mciCommand GETDEVCAPS claims %u inputs, expected %u\n", parm.caps.dwReturn, ndevs);
427     ok(!ok_pcm || !parm.caps.dwReturn,"No input device accepts PCM!?\n");
428     test_notification(hwnd, "GETDEVCAPS inputs", MCI_NOTIFY_SUCCESSFUL);
429
430     /* A few ME machines pass all tests except set format tag pcm! */
431     err = mciSendString("record x to 2000 wait", NULL, 0, hwnd);
432     ok(err || !ok_pcm,"can record yet set wave format pcm returned %s\n", dbg_mcierr(ok_pcm));
433     if(!ndevs) todo_wine /* with sound disabled */
434     ok(ndevs>0 ? !err : err==MCIERR_WAVE_INPUTSUNSUITABLE,"mci record to 2000 returned %s\n", dbg_mcierr(err));
435     else
436     ok(ndevs>0 ? !err : err==MCIERR_WAVE_INPUTSUNSUITABLE,"mci record to 2000 returned %s\n", dbg_mcierr(err));
437     if(err) {
438         if (err==MCIERR_WAVE_INPUTSUNSUITABLE)
439              skip("Please install audio driver. Everything is skipped.\n");
440         else skip("Cannot record cause %s. Everything is skipped.\n", dbg_mcierr(err));
441
442         err = mciSendString("close x", NULL, 0, NULL);
443         ok(!err,"mci close returned %s\n", dbg_mcierr(err));
444         test_notification(hwnd,"record skipped",0);
445         return;
446     }
447
448     /* Query some wave format parameters depending on the time format. */
449     err = mciSendString("status x position", buf, sizeof(buf), NULL);
450     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
451     if(!err) todo_wine ok(!strcmp(buf,"2000"), "mci status position gave %s, expected 2000, some tests will fail\n", buf);
452
453     err = mciSendString("set x time format 8", NULL, 0, NULL); /* bytes */
454     ok(!err,"mci returned %s\n", dbg_mcierr(err));
455
456     parm.status.dwItem = MCI_STATUS_POSITION;
457     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
458     ok(!err,"mciCommand status position: %s\n", dbg_mcierr(err));
459     expect = 2 * nsamp * nch * nbits/8;
460     if(!err) todo_wine ok(parm.status.dwReturn==expect,"recorded %lu bytes, expected %u\n",parm.status.dwReturn,expect);
461
462     parm.set.dwTimeFormat = MCI_FORMAT_SAMPLES;
463     err = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD_PTR)&parm);
464     ok(!err,"mciCommand set time format samples: %s\n", dbg_mcierr(err));
465
466     parm.status.dwItem = MCI_STATUS_POSITION;
467     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
468     ok(!err,"mciCommand status position: %s\n", dbg_mcierr(err));
469     expect = 2 * nsamp;
470     if(!err) todo_wine ok(parm.status.dwReturn==expect,"recorded %lu samples, expected %u\n",parm.status.dwReturn,expect);
471
472     err = mciSendString("set x time format milliseconds", NULL, 0, NULL);
473     ok(!err,"mci set time format milliseconds returned %s\n", dbg_mcierr(err));
474
475     err = mciSendString("save x tempfile1.wav", NULL, 0, NULL);
476     ok(!err,"mci save returned %s\n", dbg_mcierr(err));
477
478     err = mciSendString("save x tempfile.wav", NULL, 0, NULL);
479     ok(!err,"mci save returned %s\n", dbg_mcierr(err));
480     if(!err) ok_saved = 0;
481
482     /* Save must not rename the original file. */
483     if (!DeleteFile("tempfile1.wav"))
484         todo_wine ok(FALSE, "Save must not rename the original file; DeleteFile returned %d\n", GetLastError());
485
486     err = mciSendString("set x channels 2", NULL, 0, NULL);
487     ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci set channels after saving returned %s\n", dbg_mcierr(err));
488
489     parm.seek.dwTo = 600;
490     err = mciSendCommand(wDeviceID, MCI_SEEK, MCI_TO | MCI_WAIT, (DWORD_PTR)&parm);
491     ok(!err,"mciCommand seek to 600: %s\n", dbg_mcierr(err));
492
493     /* Truncate to current position */
494     err = mciSendString("delete x", NULL, 0, NULL);
495     todo_wine ok(!err,"mci delete returned %s\n", dbg_mcierr(err));
496
497     buf[0]='\0';
498     err = mciSendString("status x length", buf, sizeof(buf), NULL);
499     ok(!err,"mci status length returned %s\n", dbg_mcierr(err));
500     todo_wine ok(!strcmp(buf,"600"), "mci status length after delete gave %s, expected 600\n", buf);
501
502     err = mciSendString("close x", NULL, 0, NULL);
503     ok(!err,"mci close returned %s\n", dbg_mcierr(err));
504     test_notification(hwnd,"record complete",0);
505 }
506
507 static void test_playWAVE(HWND hwnd)
508 {
509     MCIERROR err;
510     char buf[1024];
511     memset(buf, 0, sizeof(buf));
512
513     err = mciSendString("open waveaudio!tempfile.wav alias mysound", NULL, 0, NULL);
514     ok(err==ok_saved,"mci open waveaudio!tempfile.wav returned %s\n", dbg_mcierr(err));
515     if(err) {
516         skip("Cannot open waveaudio!tempfile.wav for playing (%s), skipping\n", dbg_mcierr(err));
517         return;
518     }
519
520     err = mciSendString("status mysound length", buf, sizeof(buf), NULL);
521     ok(!err,"mci status length returned %s\n", dbg_mcierr(err));
522     todo_wine ok(!strcmp(buf,"2000"), "mci status length gave %s, expected 2000, some tests will fail.\n", buf);
523
524     err = mciSendString("cue output", NULL, 0, NULL);
525     todo_wine ok(err==MCIERR_UNRECOGNIZED_COMMAND,"mci incorrect cue output returned %s\n", dbg_mcierr(err));
526
527     /* Test MCI to the bones -- Some todo_wine from Cue and
528      * from Play from 0 to 0 are not worth fixing. */
529     err = mciSendString("cue mysound output notify", NULL, 0, hwnd);
530     ok(!err,"mci cue output after open file returned %s\n", dbg_mcierr(err));
531     /* Notification is delayed as a play thread is started. */
532     todo_wine test_notification(hwnd, "cue immediate", 0);
533
534     /* Cue pretends to put the MCI into paused state. */
535     err = mciSendString("status mysound mode", buf, sizeof(buf), hwnd);
536     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
537     todo_wine ok(!strcmp(buf,"paused"), "mci status mode: %s, expected (pseudo)paused\n", buf);
538
539     /* Strange pause where Pause is rejected, unlike Play; Pause; Pause tested below */
540     err = mciSendString("pause mysound", NULL, 0, hwnd);
541     ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci pause after cue returned %s\n", dbg_mcierr(err));
542
543     /* MCI appears to start the play thread in this border case.
544      * Guessed that from (flaky) status mode and late notification arrival. */
545     err = mciSendString("play mysound from 0 to 0 notify", NULL, 0, hwnd);
546     ok(!err,"mci play from 0 to 0 returned %s\n", dbg_mcierr(err));
547     todo_wine test_notification(hwnd, "cue aborted by play", MCI_NOTIFY_ABORTED);
548     /* play's own notification follows below */
549
550     err = mciSendString("play mysound from 250 to 0", NULL, 0, NULL);
551     ok(err==MCIERR_OUTOFRANGE,"mci play from 250 to 0 returned %s\n", dbg_mcierr(err));
552
553     Sleep(50); /* Give play from 0 to 0 time to finish. */
554     todo_wine test_notification(hwnd, "play from 0 to 0", MCI_NOTIFY_SUCCESSFUL);
555
556     err = mciSendString("status mysound mode", buf, sizeof(buf), hwnd);
557     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
558     ok(!strcmp(buf,"stopped"), "mci status mode: %s after play from 0 to 0\n", buf);
559
560     err = mciSendString("play MYSOUND from 250 to 0 notify", NULL, 0, hwnd);
561     ok(err==MCIERR_OUTOFRANGE,"mci play from 250 to 0 notify returned %s\n", dbg_mcierr(err));
562     /* No notification (checked below) sent if error */
563
564     /* A second play caused Wine<1.1.33 to hang */
565     err = mciSendString("play mysound from 500 to 1500 wait", NULL, 0, NULL);
566     ok(!err,"mci play from 500 to 1500 returned %s\n", dbg_mcierr(err));
567
568     memset(buf, 0, sizeof(buf));
569     err = mciSendString("status mysound position", buf, sizeof(buf), hwnd);
570     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
571     if(!err) ok(!strcmp(buf,"1500"), "mci status position: %s\n", buf);
572
573     /* mci will not play position < current */
574     err = mciSendString("play mysound to 1000", NULL, 0, NULL);
575     ok(err==MCIERR_OUTOFRANGE,"mci play to 1000 returned %s\n", dbg_mcierr(err));
576
577     /* mci will not play to > end */
578     err = mciSendString("play mysound TO 3000 notify", NULL, 0, hwnd);
579     ok(err==MCIERR_OUTOFRANGE,"mci play to 3000 notify returned %s\n", dbg_mcierr(err));
580
581     err = mciSendString("play mysound to 2000", NULL, 0, NULL);
582     ok(!err,"mci play to 2000 returned %s\n", dbg_mcierr(err));
583
584     /* Rejected while playing */
585     err = mciSendString("cue mysound output", NULL, 0, NULL);
586     ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci cue output while playing returned %s\n", dbg_mcierr(err));
587
588     err = mciSendString("play mysound to 3000", NULL, 0, NULL);
589     ok(err==MCIERR_OUTOFRANGE,"mci play to 3000 returned %s\n", dbg_mcierr(err));
590
591     err = mciSendString("stop mysound Wait", NULL, 0, NULL);
592     ok(!err,"mci stop wait returned %s\n", dbg_mcierr(err));
593     test_notification(hwnd, "play/cue/pause/stop", 0);
594
595     err = mciSendString("Seek Mysound to 250 wait Notify", NULL, 0, hwnd);
596     ok(!err,"mci seek to 250 wait notify returned %s\n", dbg_mcierr(err));
597     test_notification(hwnd,"seek wait notify",MCI_NOTIFY_SUCCESSFUL);
598
599     memset(buf, 0, sizeof(buf));
600     err = mciSendString("status mysound position notify", buf, sizeof(buf), hwnd);
601     ok(!err,"mci status position notify returned %s\n", dbg_mcierr(err));
602     if(!err) ok(!strcmp(buf,"250"), "mci status position: %s\n", buf);
603     /* Immediate commands like status also send notifications. */
604     test_notification(hwnd,"status position",MCI_NOTIFY_SUCCESSFUL);
605
606     memset(buf, 0, sizeof(buf));
607     err = mciSendString("status mysound mode", buf, sizeof(buf), hwnd);
608     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
609     ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);
610
611     /* Another play from == to testcase */
612     err = mciSendString("play mysound to 250 wait notify", NULL, 0, hwnd);
613     ok(!err,"mci play (from 250) to 250 returned %s\n", dbg_mcierr(err));
614     todo_wine test_notification(hwnd,"play to 250 wait notify",MCI_NOTIFY_SUCCESSFUL);
615
616     err = mciSendString("cue mysound output", NULL, 0, NULL);
617     ok(!err,"mci cue output after play returned %s\n", dbg_mcierr(err));
618
619     err = mciSendString("close mysound", NULL, 0, NULL);
620     ok(!err,"mci close returned %s\n", dbg_mcierr(err));
621     test_notification(hwnd,"after close",0);
622 }
623
624 static void test_asyncWAVE(HWND hwnd)
625 {
626     MCIDEVICEID wDeviceID;
627     MCI_PARMS_UNION parm;
628     int err, p1, p2;
629     char buf[1024];
630     memset(buf, 0, sizeof(buf));
631
632     err = mciSendString("open tempfile.wav alias mysound notify type waveaudio", buf, sizeof(buf), hwnd);
633     ok(err==ok_saved,"mci open tempfile.wav returned %s\n", dbg_mcierr(err));
634     if(err) {
635         skip("Cannot open tempfile.wav for playing (%s), skipping\n", dbg_mcierr(err));
636         return;
637     }
638     ok(!strcmp(buf,"1"), "mci open deviceId: %s, expected 1\n", buf);
639     wDeviceID = atoi(buf);
640     ok(wDeviceID,"mci open DeviceID: %d\n", wDeviceID);
641     test_notification(hwnd,"open alias notify",MCI_NOTIFY_SUCCESSFUL);
642
643     err = mciGetDeviceID("mysound");
644     ok(err==wDeviceID,"mciGetDeviceID alias returned %u, expected %u\n", err, wDeviceID);
645
646     /* Only the alias is looked up. */
647     err = mciGetDeviceID("tempfile.wav");
648     todo_wine ok(err==0,"mciGetDeviceID element returned %u, expected 0\n", err);
649
650     err = mciGetDeviceID("waveaudio");
651     todo_wine ok(err==0,"mciGetDeviceID waveaudio returned %u, expected 0\n", err);
652
653     err = mciSendString("status mysound mode", buf, sizeof(buf), hwnd);
654     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
655     ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);
656
657     err = mciSendString("play mysound notify", NULL, 0, hwnd);
658     ok(!err,"mci play returned %s\n", dbg_mcierr(err));
659
660     /* Give Wine's asynchronous thread time to start up.  Furthermore,
661      * it uses 3 buffers per second, so that the positions reported
662      * will be 333ms, 667ms etc. at best. */
663     Sleep(100); /* milliseconds */
664
665     /* Do not query time format as string because result depends on locale! */
666     parm.status.dwItem = MCI_STATUS_TIME_FORMAT;
667     err = mciSendCommand(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
668     ok(!err,"mciCommand status time format: %s\n", dbg_mcierr(err));
669     if(!err) ok(parm.status.dwReturn==MCI_FORMAT_MILLISECONDS,"status time format: %ld\n",parm.status.dwReturn);
670
671     parm.set.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
672     err = mciSendCommand(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD_PTR)&parm);
673     ok(!err,"mciCommand set time format ms: %s\n", dbg_mcierr(err));
674
675     buf[0]=0;
676     err = mciSendString("status mysound position", buf, sizeof(buf), hwnd);
677     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
678     ok(strcmp(buf,"2000"), "mci status position: %s, expected 2000\n", buf);
679     trace("position after Sleep: %sms\n",buf);
680     p2 = atoi(buf);
681     /* Some machines reach 79ms only during the 100ms sleep. */
682     ok(p2>=67,"not enough time elapsed %ums\n",p2);
683     test_notification(hwnd,"play (nowait)",0);
684
685     err = mciSendString("pause mysound wait", NULL, 0, hwnd);
686     ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));
687
688     buf[0]=0;
689     err = mciSendString("status mysound mode notify", buf, sizeof(buf), hwnd);
690     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
691     if(!err) ok(!strcmp(buf,"paused"), "mci status mode: %s\n", buf);
692     test_notification(hwnd,"play",MCI_NOTIFY_SUPERSEDED);
693     test_notification(hwnd,"status",MCI_NOTIFY_SUCCESSFUL);
694
695     buf[0]=0;
696     err = mciSendString("status mysound position", buf, sizeof(buf), hwnd);
697     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
698     trace("position while paused: %sms\n",buf);
699     p1 = atoi(buf);
700     ok(p1>=p2, "position not increasing: %u > %u\n", p2, p1);
701
702     err = mciSendString("stop mysound wait", NULL, 0, NULL);
703     ok(!err,"mci stop returned %s\n", dbg_mcierr(err));
704
705     buf[0]=0;
706     err = mciSendString("info mysound file notify", buf, sizeof(buf), hwnd);
707     ok(!err,"mci info file returned %s\n", dbg_mcierr(err));
708     if(!err) { /* fully qualified name */
709         int len = strlen(buf);
710         todo_wine ok(len>2 && buf[1]==':',"Expected full pathname from info file: %s\n", buf);
711         ok(len>=12 && !strcmp(&buf[len-12],"tempfile.wav"), "info file returned: %s\n", buf);
712     }
713     test_notification(hwnd,"info file",MCI_NOTIFY_SUCCESSFUL);
714
715     buf[0]=0;
716     err = mciSendString("status mysound mode", buf, sizeof(buf), hwnd);
717     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
718     ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);
719
720     buf[0]=0;
721     err = mciSendString("status mysound position", buf, sizeof(buf), hwnd);
722     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
723     trace("position once stopped: %sms\n",buf);
724     p2 = atoi(buf);
725     /* An XP machine let the position increase slightly after pause. */
726     ok(p2>=p1 && p2<=p1+16,"position changed from %ums to %ums\n",p1,p2);
727
728     /* No Resume once stopped (waveaudio, sequencer and cdaudio differ). */
729     err = mciSendString("resume mysound wait", NULL, 0, NULL);
730     ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci resume wait returned %s\n", dbg_mcierr(err));
731
732     err = mciSendString("play mysound wait", NULL, 0, NULL);
733     ok(!err,"mci play wait returned %s\n", dbg_mcierr(err));
734
735     buf[0]=0;
736     err = mciSendString("status mysound position", buf, sizeof(buf), hwnd);
737     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
738     todo_wine ok(!strcmp(buf,"2000"), "mci status position: %s\n", buf);
739
740     err = mciSendString("seek mysound to start wait", NULL, 0, NULL);
741     ok(!err,"mci seek to start wait returned %s\n", dbg_mcierr(err));
742
743     err = mciSendString("play mysound to 1000 notify", NULL, 0, hwnd);
744     ok(!err,"mci play returned %s\n", dbg_mcierr(err));
745
746     /* Sleep(200); not needed with Wine any more. */
747
748     err = mciSendString("pause mysound notify", NULL, 0, NULL); /* notify no callback */
749     ok(!err,"mci pause notify returned %s\n", dbg_mcierr(err));
750     /* Supersede even though pause cannot notify given no callback */
751     test_notification(hwnd,"pause aborted play #1 notification",MCI_NOTIFY_SUPERSEDED);
752     test_notification(hwnd,"impossible pause notification",0);
753
754     err = mciSendString("cue mysound output notify", NULL, 0, hwnd);
755     ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci cue output while paused returned %s\n", dbg_mcierr(err));
756     test_notification(hwnd,"cue output notify #2",0);
757
758     err = mciSendString("resume mysound notify", NULL, 0, hwnd);
759     ok(!err,"mci resume notify returned %s\n", dbg_mcierr(err));
760     test_notification(hwnd, "resume notify", MCI_NOTIFY_SUCCESSFUL);
761
762     /* Seek or even Stop used to hang Wine<1.1.32 on MacOS. */
763     err = mciSendString("seek mysound to 0 wait", NULL, 0, NULL);
764     ok(!err,"mci seek to start returned %s\n", dbg_mcierr(err));
765
766     /* Seek stops. */
767     err = mciSendString("status mysound mode", buf, sizeof(buf), NULL);
768     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
769     if(!err) ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);
770
771     err = mciSendString("seek mysound wait", NULL, 0, NULL);
772     ok(err==MCIERR_MISSING_PARAMETER,"mci seek to nowhere returned %s\n", dbg_mcierr(err));
773
774     /* cdaudio does not detect to start to end as error */
775     err = mciSendString("seek mysound to start to 0", NULL, 0, NULL);
776     ok(err==MCIERR_FLAGS_NOT_COMPATIBLE,"mci seek to start to 0 returned %s\n", dbg_mcierr(err));
777
778     err = mciSendString("PLAY mysound to 1000 notify", NULL, 0, hwnd);
779     ok(!err,"mci play to 1000 notify returned %s\n", dbg_mcierr(err));
780
781     /* Sleep(200); not needed with Wine any more. */
782     /* Give it 400ms and resume will appear to complete below. */
783
784     err = mciSendString("pause mysound wait", NULL, 0, NULL);
785     ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));
786     /* Unlike sequencer and cdaudio, waveaudio's pause does not abort. */
787     test_notification(hwnd,"pause aborted play #2 notification",0);
788
789     err = mciSendString("resume mysound wait", NULL, 0, NULL);
790     ok(!err,"mci resume wait returned %s\n", dbg_mcierr(err));
791     /* Resume is a short asynchronous call, something else is playing. */
792
793     err = mciSendString("status mysound mode", buf, sizeof(buf), NULL);
794     ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
795     if(!err) ok(!strcmp(buf,"playing"), "mci status mode: %s\n", buf);
796
797     /* Note extra space before alias */
798     err = mciSendString("pause  mysound wait", NULL, 0, NULL);
799     todo_wine ok(!err,"mci pause (space) wait returned %s\n", dbg_mcierr(err));
800
801     err = mciSendString("pause mysound wait", NULL, 0, NULL);
802     ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));
803
804     /* Better ask position only when paused, is it updated while playing? */
805     buf[0]='\0';
806     err = mciSendString("status mysound position", buf, sizeof(buf), NULL);
807     ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
808     /* TODO compare position < 900 */
809     ok(strcmp(buf,"1000"), "mci resume waited\n");
810     ok(strcmp(buf,"2000"), "mci resume played to end\n");
811     trace("position after resume: %sms\n",buf);
812     test_notification(hwnd,"play (aborted by pause/resume/pause)",0);
813
814     err = mciSendString("close mysound wait", NULL, 0, NULL);
815     ok(!err,"mci close wait returned %s\n", dbg_mcierr(err));
816     test_notification(hwnd,"play (aborted by close)",MCI_NOTIFY_ABORTED);
817 }
818
819 static void test_AutoOpenWAVE(HWND hwnd)
820 {
821     /* This test used(?) to cause intermittent crashes when Wine exits, after
822      * fixme:winmm:MMDRV_Exit Closing while ll-driver open
823      */
824     UINT ndevs = waveOutGetNumDevs();
825     MCIERROR err, ok_snd = ndevs ? 0 : MCIERR_HARDWARE;
826     MCI_PARMS_UNION parm;
827     char buf[512], path[300], command[330];
828     DWORD intbuf[3] = { 0xDEADF00D, 99, 0xABADCAFE };
829     memset(buf, 0, sizeof(buf)); memset(path, 0, sizeof(path));
830
831     /* Do not crash on NULL buffer pointer */
832     err = mciSendString("sysinfo waveaudio quantity open", NULL, 0, NULL);
833     ok(err==MCIERR_PARAM_OVERFLOW,"mci sysinfo without buffer returned %s\n", dbg_mcierr(err));
834
835     err = mciSendString("sysinfo waveaudio quantity open", buf, sizeof(buf), NULL);
836     ok(!err,"mci sysinfo waveaudio quantity open returned %s\n", dbg_mcierr(err));
837     if(!err) ok(!strcmp(buf,"0"), "sysinfo quantity open expected 0, got: %s, some more tests will fail.\n", buf);
838
839     /* Who knows why some machines pass all tests but return MCIERR_HARDWARE here? */
840     err = mciSendString("sound NoSuchSoundDefined wait", NULL, 0, NULL);
841     todo_wine ok(err==ok_snd || broken(err==MCIERR_HARDWARE),"mci sound NoSuchSoundDefined returned %s\n", dbg_mcierr(err));
842
843     err = mciSendString("sound SystemExclamation notify wait", NULL, 0, hwnd);
844     todo_wine ok(err==ok_snd || broken(err==MCIERR_HARDWARE),"mci sound SystemExclamation returned %s\n", dbg_mcierr(err));
845     test_notification(hwnd, "sound notify", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
846
847     Sleep(16); /* time to auto-close makes sysinfo below return expected error */
848     buf[0]=0;
849     err = mciSendString("sysinfo waveaudio notify name 1 open", buf, sizeof(buf), hwnd);
850     ok(err==MCIERR_OUTOFRANGE,"sysinfo waveaudio name 1 returned %s\n", dbg_mcierr(err));
851     if(!err) trace("sysinfo dangling open alias: %s\n", buf);
852     test_notification(hwnd, "sysinfo name outofrange\n", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
853
854     err = mciSendString("play no-such-file-exists.wav notify", buf, sizeof(buf), NULL);
855     if(err==MCIERR_FILE_NOT_FOUND) { /* a Wine detector */
856         /* Unsupported auto-open leaves the file open, preventing clean-up */
857         skip("Skipping auto-open tests in Wine\n");
858         return;
859     }
860
861     test_notification(hwnd, "-prior to auto-open-", 0);
862
863     err = mciSendString("play tempfile.wav notify", buf, sizeof(buf), hwnd);
864     todo_wine ok(err==MCIERR_NOTIFY_ON_AUTO_OPEN,"mci auto-open play notify returned %s\n", dbg_mcierr(err));
865
866     if(err) /* FIXME: don't open twice yet, it confuses Wine. */
867     err = mciSendString("play tempfile.wav", buf, sizeof(buf), hwnd);
868     ok(err==ok_saved,"mci auto-open play returned %s\n", dbg_mcierr(err));
869
870     if(err==MCIERR_FILE_NOT_FOUND) {
871         skip("Cannot open tempfile.wav for auto-play, skipping\n");
872         return;
873     }
874
875     buf[0]=0;
876     err = mciSendString("sysinfo waveaudio quantity open", buf, sizeof(buf), NULL);
877     ok(!err,"mci sysinfo waveaudio quantity after auto-open returned %s\n", dbg_mcierr(err));
878     if(!err) todo_wine ok(!strcmp(buf,"1"), "sysinfo quantity open expected 1, got: %s\n", buf);
879
880     parm.sys.lpstrReturn = (LPSTR)&intbuf[1];
881     parm.sys.dwRetSize = 2*sizeof(DWORD); /* only one DWORD is used */
882     parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
883     err = mciSendCommand(0, MCI_SYSINFO, MCI_SYSINFO_QUANTITY | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
884     ok(!err,"mciSendCommand(0(WAVEAUDIO), MCI_SYSINFO, OPEN | MCI_NOTIFY) returned %s\n", dbg_mcierr(err));
885     if(!err) ok(atoi(buf)==intbuf[1],"sysinfo waveaudio quantity open string and command differ\n");
886
887     buf[0]=0;
888     err = mciSendString("sysinfo waveaudio name 1 open notify", buf, sizeof(buf), hwnd);
889     ok(!err,"mci sysinfo waveaudio name after auto-open returned %s\n", dbg_mcierr(err));
890     /* This is the alias, not necessarily a file name. */
891     if(!err) ok(!strcmp(buf,"tempfile.wav"), "sysinfo name 1 open: %s\n", buf);
892     test_notification(hwnd, "sysinfo name notify\n", MCI_NOTIFY_SUCCESSFUL);
893
894     /* Save the full pathname to the file. */
895     err = mciSendString("info tempfile.wav file", path, sizeof(path), NULL);
896     ok(!err,"mci info tempfile.wav file returned %s\n", dbg_mcierr(err));
897     if(err) strcpy(path,"tempfile.wav");
898
899     err = mciSendString("status tempfile.wav mode", NULL, 0, hwnd);
900     ok(!err,"mci status tempfile.wav mode without buffer returned %s\n", dbg_mcierr(err));
901
902     sprintf(command,"status \"%s\" mode",path);
903     err = mciSendString(command, buf, sizeof(buf), hwnd);
904     ok(!err,"mci status \"%s\" mode returned %s\n", path, dbg_mcierr(err));
905
906     buf[0]=0;
907     err = mciSendString("status tempfile.wav mode", buf, sizeof(buf), hwnd);
908     ok(!err,"mci status tempfile.wav mode returned %s\n", dbg_mcierr(err));
909     if(!err) ok(!strcmp(buf,"playing"), "mci auto-open status mode, got: %s\n", buf);
910
911     err = mciSendString("open tempfile.wav", buf, sizeof(buf), NULL);
912     todo_wine ok(err==MCIERR_DEVICE_OPEN, "mci open from auto-open returned %s\n", dbg_mcierr(err));
913
914     /* w2k/xp and Wine differ. While the device is busy playing, it is
915      * regularly open and accessible via the filename: subsequent
916      * commands must not cause auto-open each.  In Wine, a subsequent
917      * command with notify request may cause the initial play
918      * notification to be superseded, in turn causing MCI to close the
919      * device.  I.e. MCI uses the auto-open notification for itself,
920      * that's why it's not available to the app.  On w2k/xp,
921      * subsequent commands with notify requests are returned with
922      * MCIERR_NOTIFY_ON_AUTO_OPEN and thus don't abort the original
923      * command.
924      */
925     err = mciSendString("status tempfile.wav mode notify", buf, sizeof(buf), hwnd);
926     todo_wine ok(err==MCIERR_NOTIFY_ON_AUTO_OPEN, "mci status auto-open notify returned %s\n", dbg_mcierr(err));
927     if(!err) {
928         trace("Wine style MCI auto-close upon notification\n");
929
930         /* "playing" because auto-close comes after the status call. */
931         todo_wine ok(!strcmp(buf,"playing"), "mci auto-open status mode notify, got: %s\n", buf);
932         /* fixme:winmm:MMDRV_Exit Closing while ll-driver open
933          *  is explained by failure to auto-close a device. */
934         test_notification(hwnd,"status notify",MCI_NOTIFY_SUCCESSFUL);
935         /* MCI received NOTIFY_SUPERSEDED and auto-closed the device. */
936         Sleep(16);
937         test_notification(hwnd,"auto-open",0);
938     } else if(err==MCIERR_NOTIFY_ON_AUTO_OPEN) { /* MS style */
939         trace("MS style MCI auto-open forbids notification\n");
940
941         err = mciSendString("pause tempfile.wav", NULL, 0, hwnd);
942         ok(!err,"mci auto-still-open pause returned %s\n", dbg_mcierr(err));
943
944         err = mciSendString("status tempfile.wav mode", buf, sizeof(buf), hwnd);
945         ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
946         if(!err) ok(!strcmp(buf,"paused"), "mci auto-open status mode, got: %s\n", buf);
947
948         /* Auto-close */
949         err = mciSendString("stop tempfile.wav", NULL, 0, hwnd);
950         ok(!err,"mci auto-still-open stop returned %s\n", dbg_mcierr(err));
951         Sleep(16); /* makes sysinfo quantity open below succeed */
952     }
953
954     err = mciSendString("sysinfo waveaudio quantity open", buf, sizeof(buf), NULL);
955     ok(!err,"mci sysinfo waveaudio quantity open after close returned %s\n", dbg_mcierr(err));
956     if(!err) todo_wine ok(!strcmp(buf,"0"), "sysinfo quantity open expected 0 after auto-close, got: %s\n", buf);
957
958     /* w95-WinME (not w2k/XP) switch to C:\ after auto-playing once.  Prevent
959      * MCIERR_FILE_NOT_FOUND by using the full path name from the Info file command.
960      */
961     sprintf(command,"status \"%s\" mode wait",path);
962     err = mciSendString(command, buf, sizeof(buf), hwnd);
963     ok(!err,"mci re-auto-open status mode returned %s\n", dbg_mcierr(err));
964     if(!err) ok(!strcmp(buf,"stopped"), "mci re-auto-open status mode, got: %s\n", buf);
965
966     buf[0]=0; /* This uses auto-open as well. */
967     err = mciSendString("capability waveaudio outputs", buf, sizeof(buf), NULL);
968     ok(!err,"mci capability waveaudio outputs returned %s\n", dbg_mcierr(err));
969     /* Wine with no sound selected in winecfg's audio tab fails this test. */
970     if(!err) ok(atoi(buf)==ndevs,"Expected %d audio outputs, got %s\n", ndevs, buf);
971
972     err = mciSendString("capability waveaudio device type", buf, sizeof(buf), hwnd);
973     ok(!err,"mci capability device type returned %s\n", dbg_mcierr(err));
974     if(!err) ok(!strcmp(buf,"waveaudio"), "mci capability device type response: %s\n", buf);
975
976     /* waveaudio forbids Pause without Play. */
977     sprintf(command,"pause \"%s\"",path);
978     err = mciSendString(command, NULL, 0, hwnd);
979     ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci auto-open pause returned %s\n", dbg_mcierr(err));
980
981     ok(0xDEADF00D==intbuf[0] && 0xABADCAFE==intbuf[2],"DWORD buffer corruption\n");
982 }
983
984 START_TEST(mci)
985 {
986     MCIERROR err;
987     HWND hwnd;
988     hwnd = CreateWindowExA(0, "static", "winmm test", WS_POPUP, 0,0,100,100,
989                            0, 0, 0, NULL);
990     test_openCloseWAVE(hwnd);
991     test_recordWAVE(hwnd);
992     test_playWAVE(hwnd);
993     test_asyncWAVE(hwnd);
994     test_AutoOpenWAVE(hwnd);
995     /* Win9X hangs when exiting with something still open. */
996     err = mciSendString("close all", NULL, 0, hwnd);
997     ok(!err,"final close all returned %s\n", dbg_mcierr(err));
998     ok(DeleteFile("tempfile.wav")||ok_saved,"Delete tempfile.wav (cause auto-open?)\n");
999     DestroyWindow(hwnd);
1000 }