mci.c 68.6 KB
Newer Older
1 2 3 4
/*
 * Test winmm mci
 *
 * Copyright 2006 Jan Zerebecki
5
 *           2009 Jörg Höhle
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

22
#include <stdio.h>
23
#include "windows.h"
24
#include "mmsystem.h"
25 26
#include "mmreg.h"
#include "wine/test.h"
27

28 29 30 31
/* The tests use the MCI's own save capability to create the tempfile.wav to play.
 * To use a pre-existing file, write-protect it. */
static MCIERROR ok_saved = MCIERR_FILE_NOT_FOUND;

32
typedef union {
33
      MCI_INFO_PARMSA     info;
34 35
      MCI_STATUS_PARMS    status;
      MCI_WAVE_SET_PARMS  set;
36
      MCI_WAVE_OPEN_PARMSA open;
37
      MCI_GETDEVCAPS_PARMS caps;
38
      MCI_SYSINFO_PARMSA  sys;
39
      MCI_SEEK_PARMS      seek;
40
      MCI_GENERIC_PARMS   gen;
41 42
    } MCI_PARMS_UNION;

43
const char* dbg_mcierr(MCIERROR err)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
     switch (err) {
     case 0: return "0=NOERROR";
#define X(label) case label: return #label ;
     X(MCIERR_INVALID_DEVICE_ID)
     X(MCIERR_UNRECOGNIZED_KEYWORD)
     X(MCIERR_UNRECOGNIZED_COMMAND)
     X(MCIERR_HARDWARE)
     X(MCIERR_INVALID_DEVICE_NAME)
     X(MCIERR_OUT_OF_MEMORY)
     X(MCIERR_DEVICE_OPEN)
     X(MCIERR_CANNOT_LOAD_DRIVER)
     X(MCIERR_MISSING_COMMAND_STRING)
     X(MCIERR_PARAM_OVERFLOW)
     X(MCIERR_MISSING_STRING_ARGUMENT)
     X(MCIERR_BAD_INTEGER)
     X(MCIERR_PARSER_INTERNAL)
     X(MCIERR_DRIVER_INTERNAL)
     X(MCIERR_MISSING_PARAMETER)
     X(MCIERR_UNSUPPORTED_FUNCTION)
     X(MCIERR_FILE_NOT_FOUND)
     X(MCIERR_DEVICE_NOT_READY)
     X(MCIERR_INTERNAL)
     X(MCIERR_DRIVER)
     X(MCIERR_CANNOT_USE_ALL)
     X(MCIERR_MULTIPLE)
     X(MCIERR_EXTENSION_NOT_FOUND)
     X(MCIERR_OUTOFRANGE)
     X(MCIERR_FLAGS_NOT_COMPATIBLE)
     X(MCIERR_FILE_NOT_SAVED)
     X(MCIERR_DEVICE_TYPE_REQUIRED)
     X(MCIERR_DEVICE_LOCKED)
     X(MCIERR_DUPLICATE_ALIAS)
     X(MCIERR_BAD_CONSTANT)
     X(MCIERR_MUST_USE_SHAREABLE)
     X(MCIERR_MISSING_DEVICE_NAME)
     X(MCIERR_BAD_TIME_FORMAT)
     X(MCIERR_NO_CLOSING_QUOTE)
     X(MCIERR_DUPLICATE_FLAGS)
     X(MCIERR_INVALID_FILE)
     X(MCIERR_NULL_PARAMETER_BLOCK)
     X(MCIERR_UNNAMED_RESOURCE)
     X(MCIERR_NEW_REQUIRES_ALIAS)
     X(MCIERR_NOTIFY_ON_AUTO_OPEN)
     X(MCIERR_NO_ELEMENT_ALLOWED)
     X(MCIERR_NONAPPLICABLE_FUNCTION)
     X(MCIERR_ILLEGAL_FOR_AUTO_OPEN)
     X(MCIERR_FILENAME_REQUIRED)
     X(MCIERR_EXTRA_CHARACTERS)
     X(MCIERR_DEVICE_NOT_INSTALLED)
     X(MCIERR_GET_CD)
     X(MCIERR_SET_CD)
     X(MCIERR_SET_DRIVE)
     X(MCIERR_DEVICE_LENGTH)
     X(MCIERR_DEVICE_ORD_LENGTH)
     X(MCIERR_NO_INTEGER)
     X(MCIERR_WAVE_OUTPUTSINUSE)
     X(MCIERR_WAVE_SETOUTPUTINUSE)
     X(MCIERR_WAVE_INPUTSINUSE)
     X(MCIERR_WAVE_SETINPUTINUSE)
     X(MCIERR_WAVE_OUTPUTUNSPECIFIED)
     X(MCIERR_WAVE_INPUTUNSPECIFIED)
     X(MCIERR_WAVE_OUTPUTSUNSUITABLE)
     X(MCIERR_WAVE_SETOUTPUTUNSUITABLE)
     X(MCIERR_WAVE_INPUTSUNSUITABLE)
     X(MCIERR_WAVE_SETINPUTUNSUITABLE)
     X(MCIERR_SEQ_DIV_INCOMPATIBLE)
     X(MCIERR_SEQ_PORT_INUSE)
     X(MCIERR_SEQ_PORT_NONEXISTENT)
     X(MCIERR_SEQ_PORT_MAPNODEVICE)
     X(MCIERR_SEQ_PORT_MISCERROR)
     X(MCIERR_SEQ_TIMER)
     X(MCIERR_SEQ_PORTUNSPECIFIED)
     X(MCIERR_SEQ_NOMIDIPRESENT)
     X(MCIERR_NO_WINDOW)
     X(MCIERR_CREATEWINDOW)
     X(MCIERR_FILE_READ)
     X(MCIERR_FILE_WRITE)
     X(MCIERR_NO_IDENTITY)
#undef X
124 125 126 127 128
     default: {
         static char name[20]; /* Not to be called twice in a parameter list! */
         sprintf(name, "MMSYSERR %u", err);
         return name;
         }
129 130 131 132 133 134 135 136 137 138 139 140 141
     }
}

static BOOL spurious_message(LPMSG msg)
{
  /* WM_DEVICECHANGE 0x0219 appears randomly */
  if(msg->message != MM_MCINOTIFY) {
    trace("skipping spurious message %04x\n",msg->message);
    return TRUE;
  }
  return FALSE;
}

142
/* A single ok() in each code path allows us to prefix this with todo_wine */
143 144
#define test_notification(hwnd, command, type) test_notification_dbg(hwnd, command, type, __LINE__)
static void test_notification_dbg(HWND hwnd, const char* command, WPARAM type, int line)
145 146 147 148 149
{   /* Use type 0 as meaning no message */
    MSG msg;
    BOOL seen;
    do { seen = PeekMessageA(&msg, hwnd, 0, 0, PM_REMOVE); }
    while(seen && spurious_message(&msg));
150 151 152
    if(type && !seen) {
      /* We observe transient delayed notification, mostly on native.
       * Notification is not always present right when mciSend returns. */
153
      trace_(__FILE__,line)("Waiting for delayed notification from %s\n", command);
154 155 156 157
      MsgWaitForMultipleObjects(0, NULL, FALSE, 3000, QS_POSTMESSAGE);
      seen = PeekMessageA(&msg, hwnd, MM_MCINOTIFY, MM_MCINOTIFY, PM_REMOVE);
    }
    if(!seen)
158
      ok_(__FILE__,line)(type==0, "Expect message %04lx from %s\n", type, command);
159
    else if(msg.hwnd != hwnd)
160
        ok_(__FILE__,line)(msg.hwnd == hwnd, "Didn't get the handle to our test window\n");
161
    else if(msg.message != MM_MCINOTIFY)
162 163
        ok_(__FILE__,line)(msg.message == MM_MCINOTIFY, "got %04x instead of MM_MCINOTIFY from command %s\n", msg.message, command);
    else ok_(__FILE__,line)(msg.wParam == type, "got %04lx instead of MCI_NOTIFY_xyz %04lx from command %s\n", msg.wParam, type, command);
164 165
}

166 167 168 169 170 171 172
static int strcmp_wa(LPCWSTR strw, const char *stra)
{
    CHAR buf[512];
    WideCharToMultiByte(CP_ACP, 0, strw, -1, buf, sizeof(buf), 0, 0);
    return lstrcmpA(buf, stra);
}

173 174 175 176 177 178 179 180 181 182 183 184
static void test_mciParser(HWND hwnd)
{
    MCIERROR err;
    MCIDEVICEID wDeviceID;
    MCI_PARMS_UNION parm;
    char buf[1024];
    memset(buf, 0, sizeof(buf));
    test_notification(hwnd, "-prior to parser test-", 0);

    /* Get a handle on an MCI device, works even without sound. */
    parm.open.lpstrDeviceType = "waveaudio";
    parm.open.lpstrElementName = ""; /* "new" at the command level */
185
    parm.open.lpstrAlias = "x"; /* to enable mciSendStringA */
186
    parm.open.dwCallback = (DWORD_PTR)hwnd;
187 188
    err = mciSendCommandA(0, MCI_OPEN,
            MCI_OPEN_ELEMENT | MCI_OPEN_TYPE | MCI_OPEN_ALIAS | MCI_NOTIFY, (DWORD_PTR)&parm);
189 190 191 192 193 194 195
    ok(!err,"mciCommand open new type waveaudio alias x notify: %s\n", dbg_mcierr(err));
    wDeviceID = parm.open.wDeviceID;
    ok(!strcmp(parm.open.lpstrDeviceType,"waveaudio"), "open modified device type\n");

    test_notification(hwnd, "MCI_OPEN", MCI_NOTIFY_SUCCESSFUL);
    test_notification(hwnd, "MCI_OPEN no #2", 0);

196
    err = mciSendStringA("open avivideo alias a", buf, sizeof(buf), hwnd);
197 198
    ok(!err,"open another: %s\n", dbg_mcierr(err));

199
    buf[0]='z';
200
    err = mciSendStringA("", buf, sizeof(buf), NULL);
201
    todo_wine ok(err==MCIERR_MISSING_COMMAND_STRING,"empty string: %s\n", dbg_mcierr(err));
202
    ok(!buf[0], "error buffer %s\n", buf);
203

204
    buf[0]='d';
205
    err = mciSendStringA("open", buf, sizeof(buf), NULL);
206
    ok(err==MCIERR_MISSING_DEVICE_NAME,"open void: %s\n", dbg_mcierr(err));
207
    ok(!buf[0], "open error buffer %s\n", buf);
208

209
    err = mciSendStringA("open notify", buf, sizeof(buf), NULL);
210 211
    todo_wine ok(err==MCIERR_INVALID_DEVICE_NAME,"open notify: %s\n", dbg_mcierr(err));

212
    err = mciSendStringA("open new", buf, sizeof(buf), NULL);
213 214
    todo_wine ok(err==MCIERR_NEW_REQUIRES_ALIAS,"open new: %s\n", dbg_mcierr(err));

215
    err = mciSendStringA("open new type waveaudio alias r shareable shareable", buf, sizeof(buf), NULL);
216
    todo_wine ok(err==MCIERR_DUPLICATE_FLAGS,"open new: %s\n", dbg_mcierr(err));
217
    if(!err) mciSendStringA("close r", NULL, 0, NULL);
218

219
    err = mciSendStringA("status x position wait wait", buf, sizeof(buf), NULL);
220 221
    todo_wine ok(err==MCIERR_DUPLICATE_FLAGS,"status wait wait: %s\n", dbg_mcierr(err));

222
    err = mciSendStringA("status x length length", buf, sizeof(buf), NULL);
223 224
    todo_wine ok(err==MCIERR_FLAGS_NOT_COMPATIBLE,"status 2xlength: %s\n", dbg_mcierr(err));

225
    err = mciSendStringA("status x length position", buf, sizeof(buf), NULL);
226 227
    todo_wine ok(err==MCIERR_FLAGS_NOT_COMPATIBLE,"status length+position: %s\n", dbg_mcierr(err));

228
    buf[0]='I';
229
    err = mciSendStringA("set x time format milliseconds time format ms", buf, sizeof(buf), NULL);
230
    todo_wine ok(err==MCIERR_FLAGS_NOT_COMPATIBLE,"status length+position: %s\n", dbg_mcierr(err));
231
    ok(!buf[0], "set error buffer %s\n", buf);
232 233

    /* device's response, not a parser test */
234
    err = mciSendStringA("status x", buf, sizeof(buf), NULL);
235
    ok(err==MCIERR_MISSING_PARAMETER,"status waveaudio nokeyword: %s\n", dbg_mcierr(err));
236

237
    buf[0]='G';
238
    err = mciSendStringA("status a", buf, sizeof(buf), NULL);
239
    todo_wine ok(err==MCIERR_UNSUPPORTED_FUNCTION,"status avivideo nokeyword: %s\n", dbg_mcierr(err));
240
    ok(!buf[0], "status error buffer %s\n", buf);
241

242
    err = mciSendStringA("status x track", buf, sizeof(buf), NULL);
243
    ok(err==MCIERR_BAD_INTEGER,"status waveaudio no track: %s\n", dbg_mcierr(err));
244

245
    err = mciSendStringA("status x track 3", buf, sizeof(buf), NULL);
246
    ok(err==MCIERR_MISSING_PARAMETER,"status waveaudio track 3: %s\n", dbg_mcierr(err));
247

248
    err = mciSendStringA("status x 2 track 3", buf, sizeof(buf), NULL);
249 250
    todo_wine ok(err==MCIERR_OUTOFRANGE,"status 2(position) track 3: %s\n", dbg_mcierr(err));

251
    err = mciSendStringA("status x 0x4", buf, sizeof(buf), NULL);
252 253
    todo_wine ok(err==MCIERR_BAD_CONSTANT, "status 0x4: %s\n", dbg_mcierr(err));

254
    err = mciSendStringA("status x 4", buf, sizeof(buf), hwnd);
255 256 257
    ok(!err,"status 4(mode): %s\n", dbg_mcierr(err));
    if(!err)ok(!strcmp(buf,"stopped"), "status 4(mode), got: %s\n", buf);

258
    err = mciSendStringA("status x 4 notify", buf, sizeof(buf), hwnd);
259 260 261 262
    todo_wine ok(!err,"status 4(mode) notify: %s\n", dbg_mcierr(err));
    if(!err)ok(!strcmp(buf,"stopped"), "status 4(mode), got: %s\n", buf);
    test_notification(hwnd, "status 4 notify", err ? 0 : MCI_NOTIFY_SUCCESSFUL);

263
    err = mciSendStringA("set x milliseconds", buf, sizeof(buf), hwnd);
264 265
    todo_wine ok(err==MCIERR_UNRECOGNIZED_KEYWORD,"set milliseconds: %s\n", dbg_mcierr(err));

266
    err = mciSendStringA("set x milliseconds ms", buf, sizeof(buf), hwnd);
267 268
    todo_wine ok(err==MCIERR_UNRECOGNIZED_KEYWORD,"set milliseconds ms: %s\n", dbg_mcierr(err));

269
    err = mciSendStringA("capability x can   save", buf, sizeof(buf), hwnd);
270 271
    todo_wine ok(!err,"capability can (space) save: %s\n", dbg_mcierr(err));

272
    err = mciSendStringA("status x nsa", buf, sizeof(buf), hwnd);
273 274
    todo_wine ok(err==MCIERR_BAD_CONSTANT,"status nsa: %s\n", dbg_mcierr(err));

275
    err = mciSendStringA("seek x to 0:0:0:0:0", buf, sizeof(buf), NULL);
276 277
    ok(err==MCIERR_BAD_INTEGER,"seek to 0:0:0:0:0 returned %s\n", dbg_mcierr(err));

278
    err = mciSendStringA("seek x to 0:0:0:0:", buf, sizeof(buf), NULL);
279 280
    ok(err==MCIERR_BAD_INTEGER,"seek to 0:0:0:0: returned %s\n", dbg_mcierr(err));

281
    err = mciSendStringA("seek x to :0:0:0:0", buf, sizeof(buf), NULL);
282 283
    ok(err==MCIERR_BAD_INTEGER,"seek to :0:0:0:0 returned %s\n", dbg_mcierr(err));

284
    err = mciSendStringA("seek x to 256:0:0:0", buf, sizeof(buf), NULL);
285 286
    ok(err==MCIERR_BAD_INTEGER,"seek to 256:0:0:0 returned %s\n", dbg_mcierr(err));

287
    err = mciSendStringA("seek x to 0:256", buf, sizeof(buf), NULL);
288 289
    ok(err==MCIERR_BAD_INTEGER,"seek to 0:256 returned %s\n", dbg_mcierr(err));

290
    err = mciSendStringA("status all time format", buf, sizeof(buf), hwnd);
291 292
    ok(err==MCIERR_CANNOT_USE_ALL,"status all: %s\n", dbg_mcierr(err));

293
    err = mciSendStringA("cue all", buf, sizeof(buf), NULL);
294 295
    ok(err==MCIERR_UNRECOGNIZED_COMMAND,"cue all: %s\n", dbg_mcierr(err));

296
    err = mciSendStringA("open all", buf, sizeof(buf), NULL);
297 298
    todo_wine ok(err==MCIERR_CANNOT_USE_ALL,"open all: %s\n", dbg_mcierr(err));

299
    /* avivideo is not a known MCI_DEVTYPE resource name */
300
    err = mciSendStringA("sysinfo avivideo quantity", buf, sizeof(buf), hwnd);
301 302
    ok(err==MCIERR_DEVICE_TYPE_REQUIRED,"sysinfo sequencer quantity: %s\n", dbg_mcierr(err));

303
    err = mciSendStringA("sysinfo digitalvideo quantity", buf, sizeof(buf), hwnd);
304 305 306 307
    ok(!err,"sysinfo digitalvideo quantity: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf,"0"), "sysinfo digitalvideo quantity returned %s\n", buf);

    /* quantity 0 yet open 1 (via type "avivideo"), fun */
308
    err = mciSendStringA("sysinfo digitalvideo quantity open", buf, sizeof(buf), hwnd);
309 310 311
    ok(!err,"sysinfo digitalvideo quantity open: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf,"1"), "sysinfo digitalvideo quantity open returned %s\n", buf);

312
    err = mciSendStringA("put a window at 0 0", buf, sizeof(buf), NULL);
313
    ok(err==MCIERR_BAD_INTEGER,"put incomplete rect: %s\n", dbg_mcierr(err));
314 315 316

    /*w9X-w2k report code from device last opened, newer versions compare them all
     * and return the one error code or MCIERR_MULTIPLE if they differ. */
317
    err = mciSendStringA("pause all", buf, sizeof(buf), NULL);
318
    todo_wine ok(err==MCIERR_MULTIPLE || broken(err==MCIERR_NONAPPLICABLE_FUNCTION),"pause all: %s\n", dbg_mcierr(err));
319
    ok(!buf[0], "pause error buffer %s\n", buf);
320

321 322 323
    /* MCI_STATUS' dwReturn is a DWORD_PTR, others' a plain DWORD. */
    parm.status.dwItem = MCI_STATUS_TIME_FORMAT;
    parm.status.dwReturn = 0xFEEDABAD;
324
    err = mciSendCommandA(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
325 326 327 328 329
    ok(!err,"mciCommand status time format: %s\n", dbg_mcierr(err));
    if(!err) ok(MCI_FORMAT_MILLISECONDS==parm.status.dwReturn,"status time format: %ld\n",parm.status.dwReturn);

    parm.status.dwItem = MCI_STATUS_MODE;
    parm.status.dwReturn = 0xFEEDABAD;
330
    err = mciSendCommandA(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
331 332 333
    ok(!err,"mciCommand status mode: %s\n", dbg_mcierr(err));
    if(!err) ok(MCI_MODE_STOP==parm.status.dwReturn,"STATUS mode: %ld\n",parm.status.dwReturn);

334
    err = mciSendStringA("status x mode", buf, sizeof(buf), hwnd);
335 336 337 338 339
    ok(!err,"status mode: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf, "stopped"), "status mode is %s\n", buf);

    parm.caps.dwItem = MCI_GETDEVCAPS_USES_FILES;
    parm.caps.dwReturn = 0xFEEDABAD;
340
    err = mciSendCommandA(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
341 342 343 344 345
    ok(!err,"mciCommand getdevcaps files: %s\n", dbg_mcierr(err));
    if(!err) ok(1==parm.caps.dwReturn,"getdevcaps files: %d\n",parm.caps.dwReturn);

    parm.caps.dwItem = MCI_GETDEVCAPS_HAS_VIDEO;
    parm.caps.dwReturn = 0xFEEDABAD;
346
    err = mciSendCommandA(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
347 348 349 350 351
    ok(!err,"mciCommand getdevcaps video: %s\n", dbg_mcierr(err));
    if(!err) ok(0==parm.caps.dwReturn,"getdevcaps video: %d\n",parm.caps.dwReturn);

    parm.caps.dwItem = MCI_GETDEVCAPS_DEVICE_TYPE;
    parm.caps.dwReturn = 0xFEEDABAD;
352
    err = mciSendCommandA(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
353 354 355
    ok(!err,"mciCommand getdevcaps video: %s\n", dbg_mcierr(err));
    if(!err) ok(MCI_DEVTYPE_WAVEFORM_AUDIO==parm.caps.dwReturn,"getdevcaps device type: %d\n",parm.caps.dwReturn);

356
    err = mciSendStringA("capability x uses files", buf, sizeof(buf), hwnd);
357 358 359
    ok(!err,"capability files: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf, "true"), "capability files is %s\n", buf);

360
    err = mciSendStringA("capability x has video", buf, sizeof(buf), hwnd);
361 362 363
    ok(!err,"capability video: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf, "false"), "capability video is %s\n", buf);

364
    err = mciSendStringA("capability x device type", buf, sizeof(buf), hwnd);
365 366 367
    ok(!err,"capability device type: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf, "waveaudio"), "capability device type is %s\n", buf);

368
    err = mciSendCommandA(wDeviceID, MCI_CLOSE, 0, 0);
369 370
    ok(!err,"mciCommand close returned %s\n", dbg_mcierr(err));

371
    err = mciSendStringA("close a", buf, sizeof(buf), hwnd);
372 373
    ok(!err,"close avi: %s\n", dbg_mcierr(err));

374 375 376
    test_notification(hwnd, "-end of 1st set-", 0);
}

377
static void test_openCloseWAVE(HWND hwnd)
378
{
379
    MCIERROR err;
380
    MCI_PARMS_UNION parm;
381
    const char command_open[] = "open new type waveaudio alias mysound notify";
382 383
    const char command_close_my[] = "close mysound notify";
    const char command_close_all[] = "close all notify";
384 385
    const char command_sysinfo[] = "sysinfo waveaudio quantity open";
    char buf[1024];
386
    DWORD intbuf[3] = { 0xDEADF00D, 99, 0xABADCAFE };
387 388
    memset(buf, 0, sizeof(buf));
    test_notification(hwnd, "-prior to any command-", 0);
389

390
    /* Avoid Sysinfo quantity with notify because Win9x and newer differ. */
391
    err = mciSendStringA("sysinfo all quantity", buf, sizeof(buf), hwnd);
392
    ok(!err,"mci sysinfo all quantity returned %s\n", dbg_mcierr(err));
393 394
    if(!err) trace("[MCI] with %s drivers\n", buf);

395 396 397
    parm.sys.lpstrReturn = (LPSTR)&intbuf[1];
    parm.sys.dwRetSize = sizeof(DWORD);
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
398 399
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_QUANTITY | MCI_WAIT,
            (DWORD_PTR)&parm);
400
    ok(!err, "mciCommand sysinfo all quantity returned %s\n", dbg_mcierr(err));
401 402 403
    if(!err) ok(atoi(buf)==intbuf[1],"sysinfo all quantity string and command differ\n");

    parm.sys.dwRetSize = sizeof(DWORD)-1;
404
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_QUANTITY, (DWORD_PTR)&parm);
405 406
    ok(err == MCIERR_PARAM_OVERFLOW || broken(!err/* Win9x */),
            "mciCommand sysinfo with too small buffer returned %s\n", dbg_mcierr(err));
407

408
    err = mciSendStringA("open new type waveaudio alias r shareable", buf, sizeof(buf), NULL);
409 410
    ok(err==MCIERR_UNSUPPORTED_FUNCTION,"mci open new shareable returned %s\n", dbg_mcierr(err));
    if(!err) {
411
        err = mciSendStringA("close r", NULL, 0, NULL);
412 413 414
        ok(!err,"mci close shareable returned %s\n", dbg_mcierr(err));
    }

415 416
    err = mciGetDeviceIDA("waveaudio");
    ok(!err, "mciGetDeviceIDA waveaudio returned %u, expected 0\n", err);
417

418
    err = mciSendStringA(command_open, buf, sizeof(buf), hwnd);
419
    ok(!err,"mci %s returned %s\n", command_open, dbg_mcierr(err));
420
    ok(!strcmp(buf,"1"), "mci open deviceId: %s, expected 1\n", buf);
421 422
    /* Wine<=1.1.33 used to ignore anything past alias XY */
    test_notification(hwnd,"open new alias notify",MCI_NOTIFY_SUCCESSFUL);
423

424
    err = mciSendStringA("status mysound time format", buf, sizeof(buf), hwnd);
425
    ok(!err,"mci status time format returned %s\n", dbg_mcierr(err));
426 427 428 429
    if(!err) {
        if (PRIMARYLANGID(LANGIDFROMLCID(GetThreadLocale())) == LANG_ENGLISH)
            ok(!strcmp(buf,"milliseconds"), "mci status time format: %s\n", buf);
        else trace("locale-dependent time format: %s (ms)\n", buf);
430
    }
431

432
    memset(buf, 0, sizeof(buf));
433 434 435 436 437
    parm.sys.dwNumber = 1;
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = sizeof(buf);
    parm.sys.dwCallback = (DWORD_PTR)hwnd;
438 439
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_SYSINFO,
            MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN | MCI_NOTIFY, (DWORD_PTR)&parm);
440
    ok(!err,"mciCommand MCI_SYSINFO all name 1 open notify: %s\n", dbg_mcierr(err));
441
    if(!err) ok(!strcmp(buf,"mysound"), "sysinfo name returned %s\n", buf);
442 443
    test_notification(hwnd, "SYSINFO name notify\n", MCI_NOTIFY_SUCCESSFUL);

444 445 446 447 448
    memset(buf, 0, sizeof(buf));
    parm.sys.dwNumber = 1;
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = 8; /* mysound\0 */
449 450
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN,
            (DWORD_PTR)&parm);
451 452 453
    ok(!err,"mciCommand MCI_SYSINFO all name 1 open buffer[8]: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf,"mysound"), "sysinfo name returned %s\n", buf);

454
    memset(buf, 0, sizeof(buf));
455 456 457 458 459 460 461 462
    /* dwRetSize counts characters, not bytes, despite what MSDN says. */
    parm.sys.dwNumber = 1;
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = 8; /* mysound\0 */
    /* MCI_..._PARMSA and PARMSW share the same layout, use one for both tests. */
    err = mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
    ok(!err || broken(err==MMSYSERR_NOTSUPPORTED/* Win9x */), "mciCommandW MCI_SYSINFO all name 1 open buffer[8]: %s\n", dbg_mcierr(err));
463
    if(!err) ok(!strcmp_wa((LPWSTR)buf,"mysound"), "sysinfo name 1 open contents\n");
464

465
    memset(buf, 0, sizeof(buf));
466
    buf[0] = 'Y';
467 468 469 470 471 472
    parm.sys.dwNumber = 1;
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = 7; /* too short for mysound\0 */
    err = mciSendCommandW(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
    ok(err==MCIERR_PARAM_OVERFLOW || broken(err==MMSYSERR_NOTSUPPORTED/* Win9x */), "mciCommandW MCI_SYSINFO all name 1 open too small: %s\n", dbg_mcierr(err));
473
    ok(!strcmp(buf,"Y"), "output buffer %s\n", buf);
474 475 476

    /* Win9x overwrites the tiny buffer and returns success, newer versions signal overflow. */
    memset(buf, 0, sizeof(buf));
477
    buf[0] = 'Y';
478 479 480 481
    parm.sys.dwNumber = 1;
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO; /* ignored */
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = 2; /* too short for mysound\0 */
482 483
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_NAME | MCI_SYSINFO_OPEN,
            (DWORD_PTR)&parm);
484
    ok(err==MCIERR_PARAM_OVERFLOW || broken(!err /* Win9x */),"mciCommand MCI_SYSINFO all name 1 open too small: %s\n", dbg_mcierr(err));
485
    ok(!strcmp(buf, err ? "Y" : "mysound"), "sysinfo short name returned %s\n", buf);
486

487
    err = mciSendStringA("sysinfo mysound quantity open", buf, sizeof(buf), hwnd);
488
    ok(err==MCIERR_DEVICE_TYPE_REQUIRED,"sysinfo alias quantity: %s\n", dbg_mcierr(err));
489

490
    err = mciSendStringA("sysinfo nosuchalias quantity open", buf, sizeof(buf), hwnd);
491 492
    ok(err==MCIERR_DEVICE_TYPE_REQUIRED,"sysinfo unknown quantity open: %s\n", dbg_mcierr(err));

493
    err = mciSendStringA("sysinfo all installname", buf, sizeof(buf), hwnd);
494 495
    ok(err==MCIERR_CANNOT_USE_ALL,"sysinfo all installname: %s\n", dbg_mcierr(err));

496 497 498
    buf[0] = 'M'; buf[1] = 0;
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = sizeof(buf);
499
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_SYSINFO, MCI_SYSINFO_INSTALLNAME, (DWORD_PTR)&parm);
500 501 502
    ok(err==MCIERR_CANNOT_USE_ALL,"mciCommand MCI_SYSINFO all installname: %s\n", dbg_mcierr(err));
    ok(!strcmp(buf,"M"), "output buffer %s\n", buf);

503
    err = mciSendStringA("sysinfo nodev installname", buf, sizeof(buf), hwnd);
504
    ok(err==MCIERR_INVALID_DEVICE_NAME,"sysinfo nodev installname: %s\n", dbg_mcierr(err));
505
    ok(!buf[0], "sysinfo error buffer %s\n", buf);
506

507 508 509 510 511 512 513
    buf[0] = 'K';
    parm.sys.lpstrReturn = buf;
    parm.sys.dwRetSize = sizeof(buf);
    err = mciSendCommandW(24000, MCI_SYSINFO, MCI_SYSINFO_INSTALLNAME, (DWORD_PTR)&parm);
    ok(err==MCIERR_INVALID_DEVICE_NAME || broken(err==MMSYSERR_NOTSUPPORTED/* Win9x */), "mciCommand MCI_SYSINFO nodev installname: %s\n", dbg_mcierr(err));
    ok(!strcmp(buf,"K"), "output buffer %s\n", buf);

514
    buf[0] = 0; buf[1] = 'A'; buf[2] = 'j'; buf[3] = 0;
515 516
    parm.info.lpstrReturn = buf;
    parm.info.dwRetSize = 2;
517
    err = mciSendCommandA(1, MCI_INFO, MCI_INFO_PRODUCT, (DWORD_PTR)&parm);
518
    ok(!err, "mciCommand MCI_INFO product: %s\n", dbg_mcierr(err));
519 520 521 522
    ok(buf[0] /* && !buf[1] */ && (buf[2] == 'j' || broken(!buf[2])), "info product output buffer %s\n", buf);
    /* Producing non-ASCII multi-byte output, native forgets to zero-terminate a too small buffer
     * with SendStringA, while SendStringW works correctly (jap. and chin. locale): ignore buf[1] */
    /* Bug in 64 bit Vista/w2k8/w7: mciSendStringW is used! (not in xp nor w2k3) */
523

524
    buf[0] = 'K'; buf[1] = 0;
525 526
    parm.info.dwRetSize = sizeof(buf);
    err = mciSendCommandW(1, MCI_INFO, 0x07000000, (DWORD_PTR)&parm);
527
    ok(err==MCIERR_UNRECOGNIZED_KEYWORD || broken(err==MMSYSERR_NOTSUPPORTED/* Win9x */), "mciCommand MCI_INFO other: %s\n", dbg_mcierr(err));
528 529
    ok(!strcmp(buf,"K"), "info output buffer %s\n", buf);

530 531
    err = mciGetDeviceIDA("all");
    ok(err == MCI_ALL_DEVICE_ID, "mciGetDeviceIDA all returned %u, expected MCI_ALL_DEVICE_ID\n", err);
532

533
    err = mciSendStringA(command_close_my, NULL, 0, hwnd);
534
    ok(!err,"mci %s returned %s\n", command_close_my, dbg_mcierr(err));
535 536
    test_notification(hwnd, command_close_my, MCI_NOTIFY_SUCCESSFUL);
    Sleep(5);
537
    test_notification(hwnd, command_close_my, 0);
538

539
    err = mciSendStringA("open no-such-file-exists.wav alias y buffer 6", buf, sizeof(buf), NULL);
540 541
    ok(err==MCIERR_FILE_NOT_FOUND,"open no-such-file.wav returned %s\n", dbg_mcierr(err));
    if(!err) {
542
        err = mciSendStringA("close y", NULL, 0, NULL);
543 544 545
        ok(!err,"close y returned %s\n", dbg_mcierr(err));
    }

546
    err = mciSendStringA("open no-such-dir\\file.wav alias y type waveaudio", buf, sizeof(buf), NULL);
547 548
    ok(err==MCIERR_FILE_NOT_FOUND || broken(err==MCIERR_INVALID_FILE /* Win9X */),"open no-such-dir/file.wav returned %s\n", dbg_mcierr(err));
    if(!err) {
549
        err = mciSendStringA("close y", NULL, 0, NULL);
550 551 552
        ok(!err,"close y returned %s\n", dbg_mcierr(err));
    }

553
    err = mciSendStringA("open ! alias no", buf, sizeof(buf), NULL);
554 555
    ok(err==MCIERR_INVALID_DEVICE_NAME,"open !(void): %s\n", dbg_mcierr(err));

556
    err = mciSendStringA("open !no-such-file-exists.wav alias no", buf, sizeof(buf), NULL);
557 558 559 560
    ok(err==MCIERR_FILE_NOT_FOUND || /* Win9X */err==MCIERR_INVALID_DEVICE_NAME,"open !name: %s\n", dbg_mcierr(err));

    /* FILE_NOT_FOUND stems from mciwave,
     * the complete name including ! is passed through since NT */
561
    err = mciSendStringA("open nosuchdevice!tempfile.wav alias no", buf, sizeof(buf), NULL);
562 563 564
    ok(err==MCIERR_FILE_NOT_FOUND || /* Win9X */err==MCIERR_INVALID_DEVICE_NAME,"open nosuchdevice!name: %s\n", dbg_mcierr(err));
    /* FIXME? use broken(INVALID_DEVICE_NAME) and have Wine not mimic Win9X? */

565
    err = mciSendStringA("close waveaudio", buf, sizeof(buf), NULL);
566
    ok(err==MCIERR_INVALID_DEVICE_NAME,"close waveaudio: %s\n", dbg_mcierr(err));
567

568
    err = mciSendStringA(command_close_all, NULL, 0, NULL);
569
    ok(!err,"mci %s (without buffer) returned %s\n", command_close_all, dbg_mcierr(err));
570

571
    err = mciSendStringA(command_close_all, buf, sizeof(buf), hwnd);
572
    ok(!err,"mci %s (with output buffer) returned %s\n", command_close_all, dbg_mcierr(err));
573
    ok(buf[0] == 0, "mci %s output buffer: %s\n", command_close_all, buf);
574 575 576
    /* No notification left, everything closed already */
    test_notification(hwnd, command_close_all, 0);
    /* TODO test close all sends one notification per open device */
577

578
    err = mciSendStringA(command_sysinfo, buf, sizeof(buf), NULL);
579
    ok(!err,"mci %s returned %s\n", command_sysinfo, dbg_mcierr(err));
580
    ok(buf[0] == '0' && buf[1] == 0, "mci %s, expected output buffer '0', got: '%s'\n", command_sysinfo, buf);
581

582
    err = mciSendStringA("open new type waveaudio", buf, sizeof(buf), NULL);
583 584
    ok(err==MCIERR_NEW_REQUIRES_ALIAS,"mci open new without alias returned %s\n", dbg_mcierr(err));

585
    parm.open.lpstrDeviceType = (LPSTR)MCI_DEVTYPE_WAVEFORM_AUDIO;
586
    err = mciSendCommandA(0, MCI_OPEN, MCI_OPEN_TYPE | MCI_OPEN_TYPE_ID, (DWORD_PTR)&parm);
587 588 589 590 591
    ok(!err,"mciCommand OPEN_TYPE_ID waveaudio: %s\n", dbg_mcierr(err));

    if(!err) {
        MCIDEVICEID wDeviceID = parm.open.wDeviceID;
        parm.caps.dwItem = MCI_GETDEVCAPS_DEVICE_TYPE;
592
        err = mciSendCommandA(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM, (DWORD_PTR)&parm);
593 594 595 596 597 598
        ok(!err,"mciCommand MCI_GETDEVCAPS device type: %s\n", dbg_mcierr(err));
        ok(MCI_DEVTYPE_WAVEFORM_AUDIO==parm.caps.dwReturn,"mciCommand GETDEVCAPS says %u, expected %u\n", parm.caps.dwReturn, MCI_DEVTYPE_WAVEFORM_AUDIO);
    }

    ok(0xDEADF00D==intbuf[0] && 0xABADCAFE==intbuf[2],"DWORD buffer corruption\n");

599 600
    err = mciGetDeviceIDA("waveaudio");
    ok(err == 1, "mciGetDeviceIDA waveaudio returned %u, expected 1\n", err);
601

602
    err = mciSendStringA("open no-such-file.wav alias waveaudio", buf, sizeof(buf), NULL);
603 604 605 606
    ok(err==MCIERR_DUPLICATE_ALIAS, "mci open alias waveaudio returned %s\n", dbg_mcierr(err));
    /* If it were not already in use, open avivideo alias waveaudio would succeed,
     * making for funny test cases. */

607
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_WAIT, 0); /* from MSDN */
608
    ok(!err, "mciCommand close returned %s\n", dbg_mcierr(err));
609

610
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_NOTIFY, 0);
611
    ok(!err, "mciCommand close returned %s\n", dbg_mcierr(err));
612

613
    parm.gen.dwCallback = (DWORD_PTR)hwnd;
614
    err = mciSendCommandA(MCI_ALL_DEVICE_ID, MCI_CLOSE, MCI_NOTIFY, (DWORD_PTR)&parm);
615
    ok(!err, "mciCommand close returned %s\n", dbg_mcierr(err));
616
    test_notification(hwnd, command_close_all, 0); /* None left */
617 618 619 620 621 622 623
}

static void test_recordWAVE(HWND hwnd)
{
    WORD nch    = 1;
    WORD nbits  = 16;
    DWORD nsamp = 16000, expect;
624
    UINT ndevs  = waveInGetNumDevs();
625
    MCIERROR err, ok_pcm;
626
    MCIDEVICEID wDeviceID;
627
    MCI_PARMS_UNION parm;
628 629
    char buf[1024];
    memset(buf, 0, sizeof(buf));
630
    test_notification(hwnd, "-prior to recording-", 0);
631 632 633

    parm.open.lpstrDeviceType = "waveaudio";
    parm.open.lpstrElementName = ""; /* "new" at the command level */
634
    parm.open.lpstrAlias = "x"; /* to enable mciSendStringA */
635
    parm.open.dwCallback = (DWORD_PTR)hwnd;
636 637
    err = mciSendCommandA(0, MCI_OPEN,
            MCI_OPEN_ELEMENT | MCI_OPEN_TYPE | MCI_OPEN_ALIAS | MCI_NOTIFY, (DWORD_PTR)&parm);
638
    ok(!err,"mciCommand open new type waveaudio alias x notify: %s\n", dbg_mcierr(err));
639 640
    wDeviceID = parm.open.wDeviceID;

641 642
    err = mciGetDeviceIDA("x");
    ok(err == wDeviceID, "mciGetDeviceIDA x returned %u, expected %u\n", err, wDeviceID);
643 644

    /* Only the alias is looked up. */
645 646
    err = mciGetDeviceIDA("waveaudio");
    ok(!err, "mciGetDeviceIDA waveaudio returned %u, expected 0\n", err);
647

648
    test_notification(hwnd, "open new", MCI_NOTIFY_SUCCESSFUL);
649
    test_notification(hwnd, "open new no #2", 0);
650

651 652
    /* Do not query time format as string because result depends on locale! */
    parm.status.dwItem = MCI_STATUS_TIME_FORMAT;
653
    err = mciSendCommandA(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
654
    ok(!err,"mciCommand status time format: %s\n", dbg_mcierr(err));
655 656
    ok(parm.status.dwReturn==MCI_FORMAT_MILLISECONDS,"status time format: %ld\n",parm.status.dwReturn);

657
    /* Info file fails until named in Open or Save. */
658
    err = mciSendStringA("info x file", buf, sizeof(buf), NULL);
659
    todo_wine ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci info new file returned %s\n", dbg_mcierr(err));
660
    ok(!buf[0], "info error buffer %s\n", buf);
661

662
    err = mciSendStringA("status x length", buf, sizeof(buf), NULL);
663 664 665
    todo_wine ok(!err,"status x length initial: %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf,"0"), "mci status length expected 0, got: %s\n", buf);

666
    /* Check the default recording: 8-bits per sample, mono, 11kHz */
667
    err = mciSendStringA("status x samplespersec", buf, sizeof(buf), NULL);
668
    ok(!err,"mci status samplespersec returned %s\n", dbg_mcierr(err));
669 670 671
    if(!err) ok(!strcmp(buf,"11025"), "mci status samplespersec expected 11025, got: %s\n", buf);

    /* MCI seems to solely support PCM, no need for ACM conversion. */
672
    err = mciSendStringA("set x format tag 2", NULL, 0, NULL);
673
    ok(err==MCIERR_OUTOFRANGE,"mci set format tag 2 returned %s\n", dbg_mcierr(err));
674

675 676
    /* MCI appears to scan the available devices for support of this format,
     * returning MCIERR_OUTOFRANGE on machines with no sound.
677 678 679
     * However some w2k8/w7 machines return no error when there's no wave
     * input device (perhaps querying waveOutGetNumDevs instead of waveIn?),
     * still the record command below fails with MCIERR_WAVE_INPUTSUNSUITABLE.
680
     * Don't skip here, record will fail below. */
681
    err = mciSendStringA("set x format tag pcm", NULL, 0, NULL);
682
    ok(!err || err==MCIERR_OUTOFRANGE,"mci set format tag pcm returned %s\n", dbg_mcierr(err));
683
    ok_pcm = err;
684

685 686 687 688
    /* MSDN warns against not setting all wave format parameters.
     * Indeed, it produces strange results, incl.
     * inconsistent PCMWAVEFORMAT headers in the saved file.
     */
689
    err = mciSendStringA("set x bytespersec 22050 alignment 2 samplespersec 11025 channels 1 bitspersample 16", NULL, 0, NULL);
690
    ok(err==ok_pcm,"mci set 5 wave parameters returned %s\n", dbg_mcierr(err));
691
    /* Investigate: on w2k, set samplespersec 22050 sets nChannels to 2!
692
     *  err = mciSendStringA("set x samplespersec 22050", NULL, 0, NULL);
693
     *  ok(!err,"mci set samplespersec returned %s\n", dbg_mcierr(err));
694 695
     */

696
    /* Checks are generally performed immediately. */
697
    err = mciSendStringA("set x bitspersample 4", NULL, 0, NULL);
698 699
    todo_wine ok(err==MCIERR_OUTOFRANGE,"mci set bitspersample 4 returned %s\n", dbg_mcierr(err));

700 701 702 703 704 705
    parm.set.wFormatTag = WAVE_FORMAT_PCM;
    parm.set.nSamplesPerSec = nsamp;
    parm.set.wBitsPerSample = nbits;
    parm.set.nChannels      = nch;
    parm.set.nBlockAlign    = parm.set.nChannels * parm.set.wBitsPerSample /8;
    parm.set.nAvgBytesPerSec= parm.set.nSamplesPerSec * parm.set.nBlockAlign;
706 707 708 709
    err = mciSendCommandA(wDeviceID, MCI_SET,
            MCI_WAVE_SET_SAMPLESPERSEC | MCI_WAVE_SET_CHANNELS | MCI_WAVE_SET_BITSPERSAMPLE |
            MCI_WAVE_SET_BLOCKALIGN | MCI_WAVE_SET_AVGBYTESPERSEC| MCI_WAVE_SET_FORMATTAG,
            (DWORD_PTR)&parm);
710
    ok(err==ok_pcm,"mciCommand set wave format: %s\n", dbg_mcierr(err));
711

712 713
    parm.caps.dwItem = MCI_WAVE_GETDEVCAPS_INPUTS;
    parm.caps.dwCallback = (DWORD_PTR)hwnd;
714 715
    err = mciSendCommandA(wDeviceID, MCI_GETDEVCAPS, MCI_GETDEVCAPS_ITEM | MCI_NOTIFY,
            (DWORD_PTR)&parm);
716 717 718 719 720
    ok(!err,"mciCommand MCI_GETDEVCAPS inputs: %s\n", dbg_mcierr(err));
    ok(parm.caps.dwReturn==ndevs,"mciCommand GETDEVCAPS claims %u inputs, expected %u\n", parm.caps.dwReturn, ndevs);
    ok(!ok_pcm || !parm.caps.dwReturn,"No input device accepts PCM!?\n");
    test_notification(hwnd, "GETDEVCAPS inputs", MCI_NOTIFY_SUCCESSFUL);

721
    /* A few ME machines pass all tests except set format tag pcm! */
722
    err = mciSendStringA("record x to 2000 wait", NULL, 0, hwnd);
723
    ok(err || !ok_pcm,"can record yet set wave format pcm returned %s\n", dbg_mcierr(ok_pcm));
724 725
    todo_wine_if (!ndevs) /* with sound disabled */
        ok(ndevs > 0 ? !err : err == MCIERR_WAVE_INPUTSUNSUITABLE, "mci record to 2000 returned %s\n", dbg_mcierr(err));
726 727 728 729
    if(err) {
        if (err==MCIERR_WAVE_INPUTSUNSUITABLE)
             skip("Please install audio driver. Everything is skipped.\n");
        else skip("Cannot record cause %s. Everything is skipped.\n", dbg_mcierr(err));
730

731
        err = mciSendStringA("close x", NULL, 0, NULL);
732
        ok(!err,"mci close returned %s\n", dbg_mcierr(err));
733
        test_notification(hwnd,"record skipped",0);
734 735 736 737
        return;
    }

    /* Query some wave format parameters depending on the time format. */
738
    err = mciSendStringA("status x position", buf, sizeof(buf), NULL);
739
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
740 741
    if(!err) todo_wine ok(!strcmp(buf,"2000"), "mci status position gave %s, expected 2000, some tests will fail\n", buf);

742
    err = mciSendStringA("set x time format 8", NULL, 0, NULL); /* bytes */
743
    ok(!err,"mci returned %s\n", dbg_mcierr(err));
744 745

    parm.status.dwItem = MCI_STATUS_POSITION;
746
    err = mciSendCommandA(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
747
    ok(!err,"mciCommand status position: %s\n", dbg_mcierr(err));
748 749 750 751
    expect = 2 * nsamp * nch * nbits/8;
    if(!err) todo_wine ok(parm.status.dwReturn==expect,"recorded %lu bytes, expected %u\n",parm.status.dwReturn,expect);

    parm.set.dwTimeFormat = MCI_FORMAT_SAMPLES;
752
    err = mciSendCommandA(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD_PTR)&parm);
753
    ok(!err,"mciCommand set time format samples: %s\n", dbg_mcierr(err));
754 755

    parm.status.dwItem = MCI_STATUS_POSITION;
756
    err = mciSendCommandA(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
757
    ok(!err,"mciCommand status position: %s\n", dbg_mcierr(err));
758 759 760
    expect = 2 * nsamp;
    if(!err) todo_wine ok(parm.status.dwReturn==expect,"recorded %lu samples, expected %u\n",parm.status.dwReturn,expect);

761
    err = mciSendStringA("set x time format milliseconds", NULL, 0, NULL);
762
    ok(!err,"mci set time format milliseconds returned %s\n", dbg_mcierr(err));
763

764
    err = mciSendStringA("save x tempfile1.wav", NULL, 0, NULL);
765 766
    ok(!err,"mci save returned %s\n", dbg_mcierr(err));

767
    err = mciSendStringA("save x tempfile.wav", NULL, 0, NULL);
768
    ok(!err,"mci save returned %s\n", dbg_mcierr(err));
769
    if(!err) ok_saved = 0;
770

771
    /* Save must not rename the original file. */
772 773 774
    if (!DeleteFileA("tempfile1.wav"))
        todo_wine ok(FALSE, "Save must not rename the original file; DeleteFileA returned %d\n",
                GetLastError());
775

776
    err = mciSendStringA("set x channels 2", NULL, 0, NULL);
777
    ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci set channels after saving returned %s\n", dbg_mcierr(err));
778

779
    parm.seek.dwTo = 600;
780
    err = mciSendCommandA(wDeviceID, MCI_SEEK, MCI_TO | MCI_WAIT, (DWORD_PTR)&parm);
781 782 783
    ok(!err,"mciCommand seek to 600: %s\n", dbg_mcierr(err));

    /* Truncate to current position */
784
    err = mciSendStringA("delete x", NULL, 0, NULL);
785 786
    todo_wine ok(!err,"mci delete returned %s\n", dbg_mcierr(err));

787
    err = mciSendStringA("status x length", buf, sizeof(buf), NULL);
788 789 790
    ok(!err,"mci status length returned %s\n", dbg_mcierr(err));
    todo_wine ok(!strcmp(buf,"600"), "mci status length after delete gave %s, expected 600\n", buf);

791
    err = mciSendStringA("close x", NULL, 0, NULL);
792
    ok(!err,"mci close returned %s\n", dbg_mcierr(err));
793 794 795 796 797 798 799 800
    test_notification(hwnd,"record complete",0);
}

static void test_playWAVE(HWND hwnd)
{
    MCIERROR err;
    char buf[1024];
    memset(buf, 0, sizeof(buf));
801

802
    err = mciSendStringA("open waveaudio!tempfile.wav alias mysound", NULL, 0, NULL);
803
    ok(err==ok_saved,"mci open waveaudio!tempfile.wav returned %s\n", dbg_mcierr(err));
804
    if(err) {
805
        skip("Cannot open waveaudio!tempfile.wav for playing (%s), skipping\n", dbg_mcierr(err));
806 807 808
        return;
    }

809 810
    err = mciGetDeviceIDA("mysound");
    ok(err == 1, "mciGetDeviceIDA mysound returned %u, expected 1\n", err);
811

812 813
    err = mciGetDeviceIDA("tempfile.wav");
    ok(!err, "mciGetDeviceIDA tempfile.wav returned %u, expected 0\n", err);
814

815 816
    err = mciGetDeviceIDA("waveaudio");
    ok(!err, "mciGetDeviceIDA waveaudio returned %u, expected 0\n", err);
817

818
    err = mciSendStringA("status mysound length", buf, sizeof(buf), NULL);
819
    ok(!err,"mci status length returned %s\n", dbg_mcierr(err));
820 821
    todo_wine ok(!strcmp(buf,"2000"), "mci status length gave %s, expected 2000, some tests will fail.\n", buf);

822
    err = mciSendStringA("cue output", NULL, 0, NULL);
823
    ok(err==MCIERR_UNRECOGNIZED_COMMAND,"mci incorrect cue output returned %s\n", dbg_mcierr(err));
824

825 826
    /* Test MCI to the bones -- Some todo_wine from Cue and
     * from Play from 0 to 0 are not worth fixing. */
827
    err = mciSendStringA("cue mysound output notify", NULL, 0, hwnd);
828 829
    ok(!err,"mci cue output after open file returned %s\n", dbg_mcierr(err));
    /* Notification is delayed as a play thread is started. */
830
    todo_wine test_notification(hwnd, "cue immediate", 0);
831

832
    /* Cue pretends to put the MCI into paused state. */
833
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), hwnd);
834
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
835 836 837
    todo_wine ok(!strcmp(buf,"paused"), "mci status mode: %s, expected (pseudo)paused\n", buf);

    /* Strange pause where Pause is rejected, unlike Play; Pause; Pause tested below */
838
    err = mciSendStringA("pause mysound", NULL, 0, hwnd);
839 840 841 842
    ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci pause after cue returned %s\n", dbg_mcierr(err));

    /* MCI appears to start the play thread in this border case.
     * Guessed that from (flaky) status mode and late notification arrival. */
843
    err = mciSendStringA("play mysound from 0 to 0 notify", NULL, 0, hwnd);
844
    ok(!err,"mci play from 0 to 0 returned %s\n", dbg_mcierr(err));
845
    todo_wine test_notification(hwnd, "cue aborted by play", MCI_NOTIFY_ABORTED);
846
    /* play's own notification follows below */
847

848
    err = mciSendStringA("play mysound from 250 to 0", NULL, 0, NULL);
849
    ok(err==MCIERR_OUTOFRANGE,"mci play from 250 to 0 returned %s\n", dbg_mcierr(err));
850

851
    Sleep(50); /* Give play from 0 to 0 time to finish. */
852
    todo_wine test_notification(hwnd, "play from 0 to 0", MCI_NOTIFY_SUCCESSFUL);
853

854
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), hwnd);
855 856 857
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
    ok(!strcmp(buf,"stopped"), "mci status mode: %s after play from 0 to 0\n", buf);

858
    err = mciSendStringA("play MYSOUND from 250 to 0 notify", NULL, 0, hwnd);
859
    ok(err==MCIERR_OUTOFRANGE,"mci play from 250 to 0 notify returned %s\n", dbg_mcierr(err));
860 861
    /* No notification (checked below) sent if error */

862
    /* A second play caused Wine<1.1.33 to hang */
863
    err = mciSendStringA("play mysound from 500 to 220:5:0 wait", NULL, 0, NULL);
864
    ok(!err,"mci play from 500 to 220:5:0 (=1500) returned %s\n", dbg_mcierr(err));
865

866
    err = mciSendStringA("status mysound position", buf, sizeof(buf), hwnd);
867
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
868 869
    if(!err) ok(!strcmp(buf,"1500"), "mci status position: %s\n", buf);

870
    /* mci will not play position < current */
871
    err = mciSendStringA("play mysound to 1000", NULL, 0, NULL);
872
    ok(err==MCIERR_OUTOFRANGE,"mci play to 1000 returned %s\n", dbg_mcierr(err));
873 874

    /* mci will not play to > end */
875
    err = mciSendStringA("play mysound TO 3000 notify", NULL, 0, hwnd);
876
    ok(err==MCIERR_OUTOFRANGE,"mci play to 3000 notify returned %s\n", dbg_mcierr(err));
877

878
    err = mciSendStringA("play mysound to 2000", NULL, 0, NULL);
879
    ok(!err,"mci play to 2000 returned %s\n", dbg_mcierr(err));
880 881

    /* Rejected while playing */
882
    err = mciSendStringA("cue mysound output", NULL, 0, NULL);
883
    ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci cue output while playing returned %s\n", dbg_mcierr(err));
884

885
    err = mciSendStringA("play mysound to 3000", NULL, 0, NULL);
886
    ok(err==MCIERR_OUTOFRANGE,"mci play to 3000 returned %s\n", dbg_mcierr(err));
887

888
    err = mciSendStringA("stop mysound Wait", NULL, 0, NULL);
889
    ok(!err,"mci stop wait returned %s\n", dbg_mcierr(err));
890
    test_notification(hwnd, "play/cue/pause/stop", 0);
891

892
    err = mciSendStringA("Seek Mysound to 250 wait Notify", NULL, 0, hwnd);
893
    ok(!err,"mci seek to 250 wait notify returned %s\n", dbg_mcierr(err));
894 895
    test_notification(hwnd,"seek wait notify",MCI_NOTIFY_SUCCESSFUL);

896
    err = mciSendStringA("seek mysound to 0xfa", NULL, 0, NULL);
897 898
    ok(err==MCIERR_BAD_INTEGER,"mci seek to 0xfa returned %s\n", dbg_mcierr(err));

899
    /* MCI_INTEGER always accepts colon notation */
900
    err = mciSendStringA("seek mysound to :1", NULL, 0, NULL);
901 902
    ok(!err,"mci seek to :1 (=256) returned %s\n", dbg_mcierr(err));

903
    err = mciSendStringA("seek mysound to 250::", NULL, 0, NULL);
904 905
    ok(!err,"mci seek to 250:: returned %s\n", dbg_mcierr(err));

906
    err = mciSendStringA("seek mysound to 250:0", NULL, 0, NULL);
907 908
    ok(!err,"mci seek to 250:0 returned %s\n", dbg_mcierr(err));

909
    err = mciSendStringA("status mysound position notify", buf, sizeof(buf), hwnd);
910
    ok(!err,"mci status position notify returned %s\n", dbg_mcierr(err));
911 912 913 914
    if(!err) ok(!strcmp(buf,"250"), "mci status position: %s\n", buf);
    /* Immediate commands like status also send notifications. */
    test_notification(hwnd,"status position",MCI_NOTIFY_SUCCESSFUL);

915
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), hwnd);
916
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
917 918
    ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);

919
    /* Another play from == to testcase */
920
    err = mciSendStringA("play mysound to 250 wait notify", NULL, 0, hwnd);
921
    ok(!err,"mci play (from 250) to 250 returned %s\n", dbg_mcierr(err));
922
    todo_wine test_notification(hwnd,"play to 250 wait notify",MCI_NOTIFY_SUCCESSFUL);
923

924
    err = mciSendStringA("cue mysound output", NULL, 0, NULL);
925 926
    ok(!err,"mci cue output after play returned %s\n", dbg_mcierr(err));

927
    err = mciSendStringA("close mysound", NULL, 0, NULL);
928
    ok(!err,"mci close returned %s\n", dbg_mcierr(err));
929 930 931 932 933
    test_notification(hwnd,"after close",0);
}

static void test_asyncWAVE(HWND hwnd)
{
934 935
    MCIDEVICEID wDeviceID;
    MCI_PARMS_UNION parm;
936 937 938 939
    int err, p1, p2;
    char buf[1024];
    memset(buf, 0, sizeof(buf));

940
    err = mciSendStringA("open tempfile.wav alias mysound notify type waveaudio", buf, sizeof(buf), hwnd);
941
    ok(err==ok_saved,"mci open tempfile.wav returned %s\n", dbg_mcierr(err));
942
    if(err) {
943
        skip("Cannot open tempfile.wav for playing (%s), skipping\n", dbg_mcierr(err));
944 945
        return;
    }
946 947 948
    ok(!strcmp(buf,"1"), "mci open deviceId: %s, expected 1\n", buf);
    wDeviceID = atoi(buf);
    ok(wDeviceID,"mci open DeviceID: %d\n", wDeviceID);
949
    test_notification(hwnd,"open alias notify",MCI_NOTIFY_SUCCESSFUL);
950

951 952
    err = mciGetDeviceIDA("mysound");
    ok(err == wDeviceID, "mciGetDeviceIDA alias returned %u, expected %u\n", err, wDeviceID);
953 954

    /* Only the alias is looked up. */
955 956
    err = mciGetDeviceIDA("tempfile.wav");
    ok(!err, "mciGetDeviceIDA tempfile.wav returned %u, expected 0\n", err);
957

958 959
    err = mciGetDeviceIDA("waveaudio");
    ok(!err, "mciGetDeviceIDA waveaudio returned %u, expected 0\n", err);
960

961
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), hwnd);
962
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
963 964
    ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);

965
    err = mciSendStringA("play mysound notify", NULL, 0, hwnd);
966
    ok(!err,"mci play returned %s\n", dbg_mcierr(err));
967

968
    Sleep(500); /* milliseconds */
969

970 971
    /* Do not query time format as string because result depends on locale! */
    parm.status.dwItem = MCI_STATUS_TIME_FORMAT;
972
    err = mciSendCommandA(wDeviceID, MCI_STATUS, MCI_STATUS_ITEM, (DWORD_PTR)&parm);
973
    ok(!err,"mciCommand status time format: %s\n", dbg_mcierr(err));
974 975 976
    if(!err) ok(parm.status.dwReturn==MCI_FORMAT_MILLISECONDS,"status time format: %ld\n",parm.status.dwReturn);

    parm.set.dwTimeFormat = MCI_FORMAT_MILLISECONDS;
977
    err = mciSendCommandA(wDeviceID, MCI_SET, MCI_SET_TIME_FORMAT, (DWORD_PTR)&parm);
978
    ok(!err,"mciCommand set time format ms: %s\n", dbg_mcierr(err));
979

980
    err = mciSendStringA("status mysound position", buf, sizeof(buf), hwnd);
981
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
982
    trace("position after Sleep: %sms\n", buf);
983
    p2 = atoi(buf);
984 985 986
    /* Check that the 2s sound plays at a normal pace, giving a wide margin to
     * account for timing granularity and small delays.
     */
987
    todo_wine ok(350 <= p2 && p2 <= 600, "%ums is not in the expected 350-600ms range\n", p2);
988 989 990 991 992 993
    /* Wine's asynchronous thread needs some time to start up. Furthermore, it
     * uses 3 buffers per second, so that the positions reported will be 333ms,
     * 667ms etc. at best, which is why it fails the above test. So add a
     * second test specifically to prevent Wine from getting even worse.
     * FIXME: To be removed when Wine is fixed and passes the above test.
     */
994
    ok(350 <= p2 && p2 <= 1000, "%ums is not even in the expected 350-1000ms range\n", p2);
995 996
    test_notification(hwnd,"play (nowait)",0);

997
    err = mciSendStringA("pause mysound wait", NULL, 0, hwnd);
998
    ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));
999

1000
    err = mciSendStringA("status mysound mode notify", buf, sizeof(buf), hwnd);
1001
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1002
    if(!err) ok(!strcmp(buf,"paused"), "mci status mode: %s\n", buf);
1003 1004
    test_notification(hwnd,"play",MCI_NOTIFY_SUPERSEDED);
    test_notification(hwnd,"status",MCI_NOTIFY_SUCCESSFUL);
1005

1006
    err = mciSendStringA("status mysound position", buf, sizeof(buf), hwnd);
1007
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
1008 1009
    trace("position while paused: %sms\n",buf);
    p1 = atoi(buf);
1010
    ok(p1>=p2, "position not increasing: %u > %u\n", p2, p1);
1011

1012
    err = mciSendStringA("stop mysound wait", NULL, 0, NULL);
1013
    ok(!err,"mci stop returned %s\n", dbg_mcierr(err));
1014

1015
    err = mciSendStringA("info mysound file notify", buf, sizeof(buf), hwnd);
1016
    ok(!err,"mci info file returned %s\n", dbg_mcierr(err));
1017 1018 1019 1020 1021
    if(!err) { /* fully qualified name */
        int len = strlen(buf);
        todo_wine ok(len>2 && buf[1]==':',"Expected full pathname from info file: %s\n", buf);
        ok(len>=12 && !strcmp(&buf[len-12],"tempfile.wav"), "info file returned: %s\n", buf);
    }
1022
    test_notification(hwnd,"info file",MCI_NOTIFY_SUCCESSFUL);
1023

1024
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), hwnd);
1025
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1026 1027
    ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);

1028
    err = mciSendStringA("status mysound position", buf, sizeof(buf), hwnd);
1029
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
1030 1031 1032
    trace("position once stopped: %sms\n",buf);
    p2 = atoi(buf);
    /* An XP machine let the position increase slightly after pause. */
1033
    ok(p2>=p1 && p2<=p1+16,"position changed from %ums to %ums\n",p1,p2);
1034 1035

    /* No Resume once stopped (waveaudio, sequencer and cdaudio differ). */
1036
    err = mciSendStringA("resume mysound wait", NULL, 0, NULL);
1037
    ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci resume wait returned %s\n", dbg_mcierr(err));
1038

1039
    err = mciSendStringA("play mysound wait", NULL, 0, NULL);
1040
    ok(!err,"mci play wait returned %s\n", dbg_mcierr(err));
1041

1042
    err = mciSendStringA("status mysound position", buf, sizeof(buf), hwnd);
1043
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
1044 1045
    todo_wine ok(!strcmp(buf,"2000"), "mci status position: %s\n", buf);

1046
    err = mciSendStringA("seek mysound to start wait", NULL, 0, NULL);
1047
    ok(!err,"mci seek to start wait returned %s\n", dbg_mcierr(err));
1048

1049
    err = mciSendStringA("play mysound to 1000 notify", NULL, 0, hwnd);
1050
    ok(!err,"mci play returned %s\n", dbg_mcierr(err));
1051

1052
    /* Sleep(200); not needed with Wine any more. */
1053

1054
    err = mciSendStringA("pause mysound notify", NULL, 0, NULL); /* notify no callback */
1055
    ok(!err,"mci pause notify returned %s\n", dbg_mcierr(err));
1056 1057
    /* Supersede even though pause cannot notify given no callback */
    test_notification(hwnd,"pause aborted play #1 notification",MCI_NOTIFY_SUPERSEDED);
1058 1059
    test_notification(hwnd,"impossible pause notification",0);

1060
    err = mciSendStringA("cue mysound output notify", NULL, 0, hwnd);
1061
    ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci cue output while paused returned %s\n", dbg_mcierr(err));
1062 1063
    test_notification(hwnd,"cue output notify #2",0);

1064
    err = mciSendStringA("resume mysound notify", NULL, 0, hwnd);
1065 1066 1067 1068
    ok(!err,"mci resume notify returned %s\n", dbg_mcierr(err));
    test_notification(hwnd, "resume notify", MCI_NOTIFY_SUCCESSFUL);

    /* Seek or even Stop used to hang Wine<1.1.32 on MacOS. */
1069
    err = mciSendStringA("seek mysound to 0 wait", NULL, 0, NULL);
1070
    ok(!err,"mci seek to start returned %s\n", dbg_mcierr(err));
1071

1072
    /* Seek stops. */
1073
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), NULL);
1074
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1075 1076
    if(!err) ok(!strcmp(buf,"stopped"), "mci status mode: %s\n", buf);

1077
    err = mciSendStringA("seek mysound wait", NULL, 0, NULL);
1078
    ok(err==MCIERR_MISSING_PARAMETER,"mci seek to nowhere returned %s\n", dbg_mcierr(err));
1079 1080

    /* cdaudio does not detect to start to end as error */
1081
    err = mciSendStringA("seek mysound to start to 0", NULL, 0, NULL);
1082
    ok(err==MCIERR_FLAGS_NOT_COMPATIBLE,"mci seek to start to 0 returned %s\n", dbg_mcierr(err));
1083

1084
    err = mciSendStringA("PLAY mysound to 1000 notify", NULL, 0, hwnd);
1085
    ok(!err,"mci play to 1000 notify returned %s\n", dbg_mcierr(err));
1086

1087
    /* Sleep(200); not needed with Wine any more. */
1088 1089
    /* Give it 400ms and resume will appear to complete below. */

1090
    err = mciSendStringA("pause mysound wait", NULL, 0, NULL);
1091
    ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));
1092
    /* Unlike sequencer and cdaudio, waveaudio's pause does not abort. */
1093
    test_notification(hwnd,"pause aborted play #2 notification",0);
1094

1095
    err = mciSendStringA("resume mysound wait", NULL, 0, NULL);
1096
    ok(!err,"mci resume wait returned %s\n", dbg_mcierr(err));
1097 1098
    /* Resume is a short asynchronous call, something else is playing. */

1099
    err = mciSendStringA("status mysound mode", buf, sizeof(buf), NULL);
1100
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1101 1102
    if(!err) ok(!strcmp(buf,"playing"), "mci status mode: %s\n", buf);

1103
    /* Note extra space before alias */
1104
    err = mciSendStringA("pause  mysound wait", NULL, 0, NULL);
1105
    todo_wine ok(!err,"mci pause (space) wait returned %s\n", dbg_mcierr(err));
1106

1107
    err = mciSendStringA("pause mysound wait", NULL, 0, NULL);
1108
    ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));
1109 1110

    /* Better ask position only when paused, is it updated while playing? */
1111
    err = mciSendStringA("status mysound position", buf, sizeof(buf), NULL);
1112
    ok(!err,"mci status position returned %s\n", dbg_mcierr(err));
1113 1114 1115 1116 1117
    /* TODO compare position < 900 */
    ok(strcmp(buf,"1000"), "mci resume waited\n");
    ok(strcmp(buf,"2000"), "mci resume played to end\n");
    trace("position after resume: %sms\n",buf);
    test_notification(hwnd,"play (aborted by pause/resume/pause)",0);
1118

1119
    err = mciSendStringA("close mysound wait", NULL, 0, NULL);
1120
    ok(!err,"mci close wait returned %s\n", dbg_mcierr(err));
1121
    test_notification(hwnd,"play (aborted by close)",MCI_NOTIFY_ABORTED);
1122 1123 1124 1125 1126 1127 1128
}

static void test_AutoOpenWAVE(HWND hwnd)
{
    /* This test used(?) to cause intermittent crashes when Wine exits, after
     * fixme:winmm:MMDRV_Exit Closing while ll-driver open
     */
1129 1130 1131
    UINT ndevs = waveOutGetNumDevs();
    MCIERROR err, ok_snd = ndevs ? 0 : MCIERR_HARDWARE;
    MCI_PARMS_UNION parm;
1132
    char buf[512], path[300], command[330];
1133
    DWORD intbuf[3] = { 0xDEADF00D, 99, 0xABADCAFE };
1134
    memset(buf, 0, sizeof(buf)); memset(path, 0, sizeof(path));
1135 1136

    /* Do not crash on NULL buffer pointer */
1137
    err = mciSendStringA("sysinfo waveaudio quantity open", NULL, 0, NULL);
1138
    ok(err==MCIERR_PARAM_OVERFLOW,"mci sysinfo without buffer returned %s\n", dbg_mcierr(err));
1139

1140
    err = mciSendStringA("sysinfo waveaudio quantity open", buf, sizeof(buf), NULL);
1141
    ok(!err,"mci sysinfo waveaudio quantity open returned %s\n", dbg_mcierr(err));
1142
    if(!err) ok(!strcmp(buf,"0"), "sysinfo quantity open expected 0, got: %s, some more tests will fail.\n", buf);
1143

Jörg Höhle's avatar
Jörg Höhle committed
1144 1145
    /* Who knows why some MS machines pass all tests but return MCIERR_HARDWARE here? */
    /* Wine returns MCIERR_HARDWARE when no default sound is found in win.ini or the registry. */
1146
    err = mciSendStringA("sound NoSuchSoundDefined wait", NULL, 0, NULL);
Jörg Höhle's avatar
Jörg Höhle committed
1147
    ok(err==ok_snd || err==MCIERR_HARDWARE, "mci sound NoSuchSoundDefined returned %s\n", dbg_mcierr(err));
1148

1149
    err = mciSendStringA("sound SystemExclamation notify wait", NULL, 0, hwnd);
Jörg Höhle's avatar
Jörg Höhle committed
1150
    ok(err==ok_snd || err==MCIERR_HARDWARE, "mci sound SystemExclamation returned %s\n", dbg_mcierr(err));
1151
    test_notification(hwnd, "sound notify", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
1152

1153
    Sleep(16); /* time to auto-close makes sysinfo below return expected error */
1154
    err = mciSendStringA("sysinfo waveaudio notify name 1 open", buf, sizeof(buf), hwnd);
1155
    ok(err==MCIERR_OUTOFRANGE,"sysinfo waveaudio name 1 returned %s\n", dbg_mcierr(err));
1156
    if(!err) trace("sysinfo dangling open alias: %s\n", buf);
1157
    test_notification(hwnd, "sysinfo name outofrange\n", err ? 0 : MCI_NOTIFY_SUCCESSFUL);
1158

1159
    err = mciSendStringA("play no-such-file-exists.wav notify", buf, sizeof(buf), NULL);
1160 1161
    todo_wine ok(err==MCIERR_NOTIFY_ON_AUTO_OPEN,"mci auto-open notify returned %s\n", dbg_mcierr(err));
    /* FILE_NOT_FOUND in Wine because auto-open fails before testing the notify flag */
1162

1163 1164
    test_notification(hwnd, "-prior to auto-open-", 0);

1165
    err = mciSendStringA("play tempfile.wav notify", buf, sizeof(buf), hwnd);
1166 1167
    todo_wine_if (ok_saved == MCIERR_FILE_NOT_FOUND) /* same as above */
        ok(err==MCIERR_NOTIFY_ON_AUTO_OPEN,"mci auto-open play notify returned %s\n", dbg_mcierr(err));
1168 1169

    if(err) /* FIXME: don't open twice yet, it confuses Wine. */
1170
    err = mciSendStringA("play tempfile.wav", buf, sizeof(buf), hwnd);
1171 1172
    ok(err==ok_saved,"mci auto-open play returned %s\n", dbg_mcierr(err));

1173 1174 1175 1176
    if(err==MCIERR_FILE_NOT_FOUND) {
        skip("Cannot open tempfile.wav for auto-play, skipping\n");
        return;
    }
1177

1178
    err = mciSendStringA("sysinfo waveaudio quantity open", buf, sizeof(buf), NULL);
1179
    ok(!err,"mci sysinfo waveaudio quantity after auto-open returned %s\n", dbg_mcierr(err));
1180
    if(!err) ok(!strcmp(buf,"1"), "sysinfo quantity open expected 1, got: %s\n", buf);
1181

1182 1183 1184
    parm.sys.lpstrReturn = (LPSTR)&intbuf[1];
    parm.sys.dwRetSize = 2*sizeof(DWORD); /* only one DWORD is used */
    parm.sys.wDeviceType = MCI_DEVTYPE_WAVEFORM_AUDIO;
1185
    err = mciSendCommandA(0, MCI_SYSINFO, MCI_SYSINFO_QUANTITY | MCI_SYSINFO_OPEN, (DWORD_PTR)&parm);
1186
    ok(!err, "mciCommand sysinfo waveaudio open notify returned %s\n", dbg_mcierr(err));
1187 1188
    if(!err) ok(atoi(buf)==intbuf[1],"sysinfo waveaudio quantity open string and command differ\n");

1189
    err = mciSendStringA("sysinfo waveaudio name 1 open notify", buf, sizeof(buf), hwnd);
1190
    ok(!err,"mci sysinfo waveaudio name after auto-open returned %s\n", dbg_mcierr(err));
1191
    /* This is the alias, not necessarily a file name. */
1192
    if(!err) ok(!strcmp(buf,"tempfile.wav"), "sysinfo name 1 open: %s\n", buf);
1193
    test_notification(hwnd, "sysinfo name notify\n", MCI_NOTIFY_SUCCESSFUL);
1194

1195 1196
    err = mciGetDeviceIDA("tempfile.wav");
    ok(err == 1, "mciGetDeviceIDA tempfile.wav returned %u, expected 1\n", err);
1197

1198
    /* Save the full pathname to the file. */
1199
    err = mciSendStringA("info tempfile.wav file", path, sizeof(path), NULL);
1200
    ok(!err,"mci info tempfile.wav file returned %s\n", dbg_mcierr(err));
1201 1202
    if(err) strcpy(path,"tempfile.wav");

1203
    err = mciSendStringA("status tempfile.wav mode", NULL, 0, hwnd);
1204
    ok(!err,"mci status tempfile.wav mode without buffer returned %s\n", dbg_mcierr(err));
1205

1206
    sprintf(command,"status \"%s\" mode",path);
1207
    err = mciSendStringA(command, buf, sizeof(buf), hwnd);
1208
    ok(!err,"mci status \"%s\" mode returned %s\n", path, dbg_mcierr(err));
1209

1210
    err = mciSendStringA("status tempfile.wav mode", buf, sizeof(buf), hwnd);
1211
    ok(!err,"mci status tempfile.wav mode returned %s\n", dbg_mcierr(err));
1212 1213
    if(!err) ok(!strcmp(buf,"playing"), "mci auto-open status mode, got: %s\n", buf);

1214
    err = mciSendStringA("open tempfile.wav", buf, sizeof(buf), NULL);
1215 1216
    ok(err==MCIERR_DEVICE_OPEN, "mci open from auto-open returned %s\n", dbg_mcierr(err));

1217
    err = mciSendStringA("open foo.wav alias tempfile.wav", buf, sizeof(buf), NULL);
1218 1219
    ok(err==MCIERR_DUPLICATE_ALIAS, "mci open re-using alias returned %s\n", dbg_mcierr(err));

1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
    /* w2k/xp and Wine differ. While the device is busy playing, it is
     * regularly open and accessible via the filename: subsequent
     * commands must not cause auto-open each.  In Wine, a subsequent
     * command with notify request may cause the initial play
     * notification to be superseded, in turn causing MCI to close the
     * device.  I.e. MCI uses the auto-open notification for itself,
     * that's why it's not available to the app.  On w2k/xp,
     * subsequent commands with notify requests are returned with
     * MCIERR_NOTIFY_ON_AUTO_OPEN and thus don't abort the original
     * command.
     */
1231
    err = mciSendStringA("status tempfile.wav mode notify", buf, sizeof(buf), hwnd);
1232
    todo_wine ok(err==MCIERR_NOTIFY_ON_AUTO_OPEN, "mci status auto-open notify returned %s\n", dbg_mcierr(err));
1233 1234 1235
    if(!err) {
        trace("Wine style MCI auto-close upon notification\n");

1236
        /* "playing" because auto-close comes after the status call. */
1237
        ok(!strcmp(buf,"playing"), "mci auto-open status mode notify, got: %s\n", buf);
1238 1239 1240 1241
        /* fixme:winmm:MMDRV_Exit Closing while ll-driver open
         *  is explained by failure to auto-close a device. */
        test_notification(hwnd,"status notify",MCI_NOTIFY_SUCCESSFUL);
        /* MCI received NOTIFY_SUPERSEDED and auto-closed the device. */
1242 1243

        /* Until this is implemented, force closing the device */
1244
        err = mciSendStringA("close tempfile.wav", NULL, 0, hwnd);
1245
        ok(!err,"mci auto-still-open stop returned %s\n", dbg_mcierr(err));
1246 1247 1248
        Sleep(16);
        test_notification(hwnd,"auto-open",0);
    } else if(err==MCIERR_NOTIFY_ON_AUTO_OPEN) { /* MS style */
1249
        trace("MS style MCI auto-open forbids notification\n");
1250

1251
        err = mciSendStringA("pause tempfile.wav", NULL, 0, hwnd);
1252
        ok(!err,"mci auto-still-open pause returned %s\n", dbg_mcierr(err));
1253

1254
        err = mciSendStringA("status tempfile.wav mode", buf, sizeof(buf), hwnd);
1255
        ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1256 1257 1258
        if(!err) ok(!strcmp(buf,"paused"), "mci auto-open status mode, got: %s\n", buf);

        /* Auto-close */
1259
        err = mciSendStringA("stop tempfile.wav wait", NULL, 0, hwnd);
1260
        ok(!err,"mci auto-still-open stop returned %s\n", dbg_mcierr(err));
1261 1262 1263
        Sleep(16); /* makes sysinfo quantity open below succeed */
    }

1264
    err = mciSendStringA("sysinfo waveaudio quantity open", buf, sizeof(buf), NULL);
1265
    ok(!err,"mci sysinfo waveaudio quantity open after close returned %s\n", dbg_mcierr(err));
1266
    if(!err) ok(!strcmp(buf,"0"), "sysinfo quantity open expected 0 after auto-close, got: %s\n", buf);
1267

1268 1269 1270 1271
    /* w95-WinME (not w2k/XP) switch to C:\ after auto-playing once.  Prevent
     * MCIERR_FILE_NOT_FOUND by using the full path name from the Info file command.
     */
    sprintf(command,"status \"%s\" mode wait",path);
1272
    err = mciSendStringA(command, buf, sizeof(buf), hwnd);
1273
    ok(!err,"mci re-auto-open status mode returned %s\n", dbg_mcierr(err));
1274 1275
    if(!err) ok(!strcmp(buf,"stopped"), "mci re-auto-open status mode, got: %s\n", buf);

1276
    /* This uses auto-open as well. */
1277
    err = mciSendStringA("capability waveaudio outputs", buf, sizeof(buf), NULL);
1278 1279 1280 1281
    ok(!err,"mci capability waveaudio outputs returned %s\n", dbg_mcierr(err));
    /* Wine with no sound selected in winecfg's audio tab fails this test. */
    if(!err) ok(atoi(buf)==ndevs,"Expected %d audio outputs, got %s\n", ndevs, buf);

1282
    err = mciSendStringA("capability waveaudio device type", buf, sizeof(buf), hwnd);
1283
    ok(!err,"mci capability device type returned %s\n", dbg_mcierr(err));
1284 1285 1286
    if(!err) ok(!strcmp(buf,"waveaudio"), "mci capability device type response: %s\n", buf);

    /* waveaudio forbids Pause without Play. */
1287
    sprintf(command,"pause \"%s\"",path);
1288
    err = mciSendStringA(command, NULL, 0, hwnd);
1289
    ok(err==MCIERR_NONAPPLICABLE_FUNCTION,"mci auto-open pause returned %s\n", dbg_mcierr(err));
1290 1291

    ok(0xDEADF00D==intbuf[0] && 0xABADCAFE==intbuf[2],"DWORD buffer corruption\n");
1292 1293
}

1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
static void test_playWaveTypeMpegvideo(void)
{
    MCIERROR err;
    MCIDEVICEID wDeviceID;
    MCI_PLAY_PARMS play_parm;
    MCI_STATUS_PARMS status_parm;
    char buf[1024];
    memset(buf, 0, sizeof(buf));

    err = mciSendStringA("open tempfile.wav type MPEGVideo alias mysound", NULL, 0, NULL);
    ok(err==ok_saved,"mci open tempfile.wav type MPEGVideo returned %s\n", dbg_mcierr(err));
    if(err) {
        skip("Cannot open tempfile.wav type MPEGVideo for playing (%s), skipping\n", dbg_mcierr(err));
        return;
    }

    wDeviceID = mciGetDeviceIDA("mysound");
    ok(wDeviceID == 1, "mciGetDeviceIDA mysound returned %u, expected 1\n", wDeviceID);

    err = mciSendCommandA(wDeviceID, MCI_PLAY, 0, (DWORD_PTR)&play_parm);
    ok(!err,"mciCommand play returned %s\n", dbg_mcierr(err));

    err = mciSendStringA("status mysound mode", buf, sizeof(buf), NULL);
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1318
    ok(!strcmp(buf,"playing"), "mci status mode: %s\n", buf);
1319 1320 1321 1322 1323 1324 1325 1326 1327

    status_parm.dwItem = MCI_STATUS_MODE;
    err = mciSendCommandA(wDeviceID, MCI_STATUS,
                          MCI_STATUS_ITEM,
                          (DWORD_PTR)&status_parm);
    ok(!err,"mciCommand status mode returned %s\n", dbg_mcierr(err));
    ok(status_parm.dwReturn == MCI_MODE_PLAY,
       "mciCommand status mode: %u\n", (DWORD)status_parm.dwReturn);

1328 1329 1330 1331 1332
    err = mciSendStringA("setaudio mysound volume to 1000", NULL, 0, NULL);
    ok(!err,"mci setaudio volume to 1000 returned %s\n", dbg_mcierr(err));

    err = mciSendStringA("status mysound mode", buf, sizeof(buf), NULL);
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1333
    ok(!strcmp(buf,"playing"), "mci status mode: %s\n", buf);
1334 1335

    err = mciSendStringA("setaudio mysound volume to 1001", NULL, 0, NULL);
1336
    ok(err==MCIERR_OUTOFRANGE,"mci setaudio volume to 1001 returned %s\n", dbg_mcierr(err));
1337 1338 1339

    err = mciSendStringA("status mysound mode", buf, sizeof(buf), NULL);
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
1340
    ok(!strcmp(buf,"playing"), "mci status mode: %s\n", buf);
1341

1342 1343 1344 1345
    err = mciSendStringA("close mysound", NULL, 0, NULL);
    ok(!err,"mci close returned %s\n", dbg_mcierr(err));
}

1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374
static void test_asyncWaveTypeMpegvideo(HWND hwnd)
{
    MCIDEVICEID wDeviceID;
    int err;
    char buf[1024];
    memset(buf, 0, sizeof(buf));

    err = mciSendStringA("open tempfile.wav alias mysound notify type mpegvideo", buf, sizeof(buf), hwnd);
    ok(err==ok_saved,"mci open tempfile.wav returned %s\n", dbg_mcierr(err));
    if(err) {
        skip("Cannot open tempfile.wav for playing (%s), skipping\n", dbg_mcierr(err));
        return;
    }
    ok(!strcmp(buf,"1"), "mci open deviceId: %s, expected 1\n", buf);
    wDeviceID = atoi(buf);
    ok(wDeviceID,"mci open DeviceID: %d\n", wDeviceID);
    test_notification(hwnd,"open alias notify",MCI_NOTIFY_SUCCESSFUL);

    err = mciSendStringA("play mysound notify", NULL, 0, hwnd);
    ok(!err,"mci play returned %s\n", dbg_mcierr(err));

    Sleep(500); /* milliseconds */

    err = mciSendStringA("pause mysound wait", NULL, 0, hwnd);
    ok(!err,"mci pause wait returned %s\n", dbg_mcierr(err));

    err = mciSendStringA("status mysound mode notify", buf, sizeof(buf), hwnd);
    ok(!err,"mci status mode returned %s\n", dbg_mcierr(err));
    if(!err) ok(!strcmp(buf,"paused"), "mci status mode: %s\n", buf);
1375
    test_notification(hwnd,"play (superseded)",MCI_NOTIFY_SUPERSEDED);
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    test_notification(hwnd,"status",MCI_NOTIFY_SUCCESSFUL);

    err = mciSendStringA("seek mysound to start wait", NULL, 0, NULL);
    ok(!err,"mci seek to start wait returned %s\n", dbg_mcierr(err));

    err = mciSendStringA("set mysound time format milliseconds", NULL, 0, NULL);
    ok(!err,"mci time format milliseconds returned %s\n", dbg_mcierr(err));

    err = mciSendStringA("play mysound to 1500 notify", NULL, 0, hwnd);
    ok(!err,"mci play returned %s\n", dbg_mcierr(err));
    Sleep(200);
1387
    test_notification(hwnd,"play",0);
1388 1389 1390

    err = mciSendStringA("close mysound wait", NULL, 0, NULL);
    ok(!err,"mci close wait returned %s\n", dbg_mcierr(err));
1391
    test_notification(hwnd,"play (aborted by close)",MCI_NOTIFY_ABORTED);
1392 1393
}

1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
static DWORD CALLBACK thread_cb(void *p)
{
    HANDLE evt = p;
    MCIERROR mr;

    mr = mciSendStringA("play x", NULL, 0, NULL);
    ok(mr == MCIERR_INVALID_DEVICE_NAME, "play gave: 0x%x\n", mr);

    mr = mciSendStringA("close x", NULL, 0, NULL);
    ok(mr == MCIERR_INVALID_DEVICE_NAME, "close gave: 0x%x\n", mr);

    SetEvent(evt);

    return 0;
}

static void test_threads(void)
{
    MCIERROR mr;
    HANDLE evt;

    mr = mciSendStringA("open tempfile.wav alias x", NULL, 0, NULL);
    ok(mr == 0 || mr == ok_saved, "open gave: 0x%x\n", mr);
    if(mr){
        skip("Cannot open tempfile.wav for playing (%s), skipping\n", dbg_mcierr(mr));
        return;
    }

    evt = CreateEventW( NULL, TRUE, FALSE, NULL );

    CloseHandle(CreateThread(NULL, 0, &thread_cb, evt, 0, NULL));

    WaitForSingleObject(evt, INFINITE);

    CloseHandle(evt);

    mr = mciSendStringA("close x", NULL, 0, NULL);
    ok(mr == 0, "close gave: 0x%x\n", mr);
}

1434 1435
START_TEST(mci)
{
1436
    char curdir[MAX_PATH], tmpdir[MAX_PATH];
1437
    MCIERROR err;
1438
    HWND hwnd;
1439 1440 1441 1442 1443

    GetCurrentDirectoryA(MAX_PATH, curdir);
    GetTempPathA(MAX_PATH, tmpdir);
    SetCurrentDirectoryA(tmpdir);

1444 1445
    hwnd = CreateWindowExA(0, "static", "winmm test", WS_POPUP, 0,0,100,100,
                           0, 0, 0, NULL);
1446
    test_mciParser(hwnd);
1447 1448
    test_openCloseWAVE(hwnd);
    test_recordWAVE(hwnd);
1449
    if(waveOutGetNumDevs()){
1450
        test_threads();
1451 1452 1453
        test_playWAVE(hwnd);
        test_asyncWAVE(hwnd);
        test_AutoOpenWAVE(hwnd);
1454
        test_playWaveTypeMpegvideo();
1455
        test_asyncWaveTypeMpegvideo(hwnd);
1456 1457
    }else
        skip("No output devices available, skipping all output tests\n");
1458
    /* Win9X hangs when exiting with something still open. */
1459
    err = mciSendStringA("close all", NULL, 0, hwnd);
1460
    ok(!err,"final close all returned %s\n", dbg_mcierr(err));
1461
    ok(DeleteFileA("tempfile.wav") || ok_saved, "Delete tempfile.wav (cause auto-open?)\n");
1462
    DestroyWindow(hwnd);
1463 1464

    SetCurrentDirectoryA(curdir);
1465
}