audio.c 67.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * Wine Driver for EsounD Sound Server
 * http://www.tux.org/~ricdude/EsounD.html
 *
 * Copyright 1994 Martin Ayotte
 *           1999 Eric Pouech (async playing in waveOut/waveIn)
 *	     2000 Eric Pouech (loops in waveOut)
 *	     2004 Zhangrong Huang (EsounD version of this file)
 *
 * 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
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 24 25
 */
/* NOTE:
 *    with esd we cannot stop the audio that is already in
Austin English's avatar
Austin English committed
26
 *    the server's buffer.
27 28 29 30 31 32 33 34 35 36
 *
 * FIXME:
 *	pause in waveOut does not work correctly in loop mode
 *
 *	does something need to be done in for WaveIn DirectSound?
 *
 */

#include "config.h"

Robert Lunnon's avatar
Robert Lunnon committed
37
#include <errno.h>
38 39 40 41 42 43 44 45 46
#include <math.h>
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <fcntl.h>
47 48 49 50 51 52 53
#ifdef HAVE_POLL_H
#include <poll.h>
#endif
#ifdef HAVE_SYS_POLL_H
# include <sys/poll.h>
#endif

54 55 56 57 58 59
#include "windef.h"
#include "winbase.h"
#include "wingdi.h"
#include "winerror.h"
#include "wine/winuser16.h"
#include "mmddk.h"
60
#include "mmreg.h"
61 62
#include "dsound.h"
#include "dsdriver.h"
63 64 65
#include "ks.h"
#include "ksguid.h"
#include "ksmedia.h"
66 67 68 69
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(wave);

70 71 72 73
#ifdef HAVE_SYS_ERRNO_H
#include <sys/errno.h>
#endif

74 75 76 77
#ifdef HAVE_ESD

#include <esd.h>

78 79 80
/* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
#define USE_PIPE_SYNC

81 82 83 84 85 86 87 88 89
/* define if you want to use esd_monitor_stream instead of
 * esd_record_stream for waveIn stream
 */
/*#define WID_USE_ESDMON*/

#define BUFFER_REFILL_THRESHOLD 4

#define MAX_WAVEOUTDRV 	(10)
#define MAX_WAVEINDRV 	(10)
90
#define MAX_CHANNELS	2
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

/* state diagram for waveOut writing:
 *
 * +---------+-------------+---------------+---------------------------------+
 * |  state  |  function   |     event     |            new state	     |
 * +---------+-------------+---------------+---------------------------------+
 * |	     | open()	   |		   | STOPPED		       	     |
 * | PAUSED  | write()	   | 		   | PAUSED		       	     |
 * | STOPPED | write()	   | <thrd create> | PLAYING		  	     |
 * | PLAYING | write()	   | HEADER        | PLAYING		  	     |
 * | (other) | write()	   | <error>       |		       		     |
 * | (any)   | pause()	   | PAUSING	   | PAUSED		       	     |
 * | PAUSED  | restart()   | RESTARTING    | PLAYING (if no thrd => STOPPED) |
 * | (any)   | reset()	   | RESETTING     | STOPPED		      	     |
 * | (any)   | close()	   | CLOSING	   | CLOSED		      	     |
 * +---------+-------------+---------------+---------------------------------+
 */

/* states of the playing device */
#define	WINE_WS_PLAYING		0
#define	WINE_WS_PAUSED		1
#define	WINE_WS_STOPPED		2
#define WINE_WS_CLOSED		3

/* events to be send to device */
enum win_wm_message {
    WINE_WM_PAUSING = WM_USER + 1, WINE_WM_RESTARTING, WINE_WM_RESETTING, WINE_WM_HEADER,
    WINE_WM_UPDATE, WINE_WM_BREAKLOOP, WINE_WM_CLOSING, WINE_WM_STARTING, WINE_WM_STOPPING
};

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#ifdef USE_PIPE_SYNC
#define SIGNAL_OMR(mr) do { int x = 0; write((mr)->msg_pipe[1], &x, sizeof(x)); } while (0)
#define CLEAR_OMR(mr) do { int x = 0; read((mr)->msg_pipe[0], &x, sizeof(x)); } while (0)
#define RESET_OMR(mr) do { } while (0)
#define WAIT_OMR(mr, sleep) \
  do { struct pollfd pfd; pfd.fd = (mr)->msg_pipe[0]; \
       pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
#else
#define SIGNAL_OMR(mr) do { SetEvent((mr)->msg_event); } while (0)
#define CLEAR_OMR(mr) do { } while (0)
#define RESET_OMR(mr) do { ResetEvent((mr)->msg_event); } while (0)
#define WAIT_OMR(mr, sleep) \
  do { WaitForSingleObject((mr)->msg_event, sleep); } while (0)
#endif

136 137
typedef struct {
    enum win_wm_message 	msg;	/* message identifier */
138
    DWORD_PTR                   param;  /* parameter for this message */
139 140 141 142 143 144 145 146 147 148 149 150 151
    HANDLE	                hEvent;	/* if message is synchronous, handle of event for synchro */
} RING_MSG;

/* implement an in-process message ring for better performance
 * (compared to passing thru the server)
 * this ring will be used by the input (resp output) record (resp playback) routine
 */
#define ESD_RING_BUFFER_INCREMENT      64
typedef struct {
    RING_MSG			* messages;
    int                         ring_buffer_size;
    int				msg_tosave;
    int				msg_toget;
152 153 154 155 156
#ifdef USE_PIPE_SYNC
    int                         msg_pipe[2];
#else
    HANDLE                      msg_event;
#endif
157 158 159 160 161 162 163
    CRITICAL_SECTION		msg_crst;
} ESD_MSG_RING;

typedef struct {
    volatile int		state;			/* one of the WINE_WS_ manifest constants */
    WAVEOPENDESC		waveDesc;
    WORD			wFlags;
164
    WAVEFORMATPCMEX             waveFormat;
Francois Gouget's avatar
Francois Gouget committed
165
    WAVEOUTCAPSW		caps;
166 167 168 169 170
    char                        interface_name[32];

    DWORD			dwSleepTime;		/* Num of milliseconds to sleep between filling the dsp buffers */

    /* esd information */
171
    char*			stream_name;		/* a unique name identifying the esd stream */
172
    int				stream_fd;		/* the socket fd we get from esd when opening a stream for playing */
173 174
    int				stream_id;		/* the stream id (to change the volume) */
    int				esd_fd;			/* a socket to communicate with the sound server */
175 176 177 178 179 180 181 182 183 184

    LPWAVEHDR			lpQueuePtr;		/* start of queued WAVEHDRs (waiting to be notified) */
    LPWAVEHDR			lpPlayPtr;		/* start of not yet fully played buffers */
    DWORD			dwPartialOffset;	/* Offset of not yet written bytes in lpPlayPtr */

    LPWAVEHDR			lpLoopPtr;              /* pointer of first buffer in loop, if any */
    DWORD			dwLoops;		/* private copy of loop counter */

    DWORD			dwPlayedTotal;		/* number of bytes actually played since opening */
    DWORD                       dwWrittenTotal;         /* number of bytes written to the audio device since opening */
185 186
    DWORD			dwLastWrite;		/* Time of last write */
    DWORD			dwLatency;		/* Num of milliseconds between when data is sent to the server and when it is played */
187 188 189 190 191 192 193 194 195 196 197

    /* synchronization stuff */
    HANDLE			hStartUpEvent;
    HANDLE			hThread;
    ESD_MSG_RING		msgRing;
} WINE_WAVEOUT;

typedef struct {
    volatile int		state;			/* one of the WINE_WS_ manifest constants */
    WAVEOPENDESC		waveDesc;
    WORD			wFlags;
198
    WAVEFORMATPCMEX             waveFormat;
Francois Gouget's avatar
Francois Gouget committed
199
    WAVEINCAPSW			caps;
200 201 202
    char                        interface_name[32];

    /* esd information */
203
    char*			stream_name;		/* a unique name identifying the esd stream */
204
    int				stream_fd;		/* the socket fd we get from esd when opening a stream for recording */
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    LPWAVEHDR			lpQueuePtr;
    DWORD			dwRecordedTotal;

    /* synchronization stuff */
    HANDLE			hStartUpEvent;
    HANDLE			hThread;
    ESD_MSG_RING		msgRing;
} WINE_WAVEIN;

static WINE_WAVEOUT	WOutDev   [MAX_WAVEOUTDRV];
static WINE_WAVEIN	WInDev    [MAX_WAVEINDRV];

/* These strings used only for tracing */
static const char *wodPlayerCmdString[] = {
    "WINE_WM_PAUSING",
    "WINE_WM_RESTARTING",
    "WINE_WM_RESETTING",
    "WINE_WM_HEADER",
    "WINE_WM_UPDATE",
    "WINE_WM_BREAKLOOP",
    "WINE_WM_CLOSING",
    "WINE_WM_STARTING",
    "WINE_WM_STOPPING",
};

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

/*======================================================================*
 *                  Low level DSOUND implementation			*
 *======================================================================*/

static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
{
    /* we can't perform memory mapping as we don't have a file stream
	interface with esd like we do with oss */
    MESSAGE("This sound card's driver does not support direct access\n");
    MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
    return MMSYSERR_NOTSUPPORTED;
}

static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
{
    memset(desc, 0, sizeof(*desc));
    strcpy(desc->szDesc, "Wine EsounD DirectSound Driver");
    strcpy(desc->szDrvname, "wineesd.drv");
    return MMSYSERR_NOERROR;
}

253 254 255 256
/*======================================================================*
 *                  Low level WAVE implementation			*
 *======================================================================*/

257 258 259
static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
                             WAVEFORMATPCMEX* format)
{
260
    TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
261 262
          lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
          format->Format.nChannels, format->Format.nAvgBytesPerSec);
263
    TRACE("Position in bytes=%u\n", position);
264 265 266 267

    switch (lpTime->wType) {
    case TIME_SAMPLES:
        lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
268
        TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
269 270 271
        break;
    case TIME_MS:
        lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
272
        TRACE("TIME_MS=%u\n", lpTime->u.ms);
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        break;
    case TIME_SMPTE:
        lpTime->u.smpte.fps = 30;
        position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
        position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
        lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
        position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
        lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
        lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
        lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
        lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
        lpTime->u.smpte.fps = 30;
        lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
        TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
              lpTime->u.smpte.hour, lpTime->u.smpte.min,
              lpTime->u.smpte.sec, lpTime->u.smpte.frame);
        break;
    default:
        WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
        lpTime->wType = TIME_BYTES;
        /* fall through */
    case TIME_BYTES:
        lpTime->u.cb = position;
296
        TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
        break;
    }
    return MMSYSERR_NOERROR;
}

static BOOL supportedFormat(LPWAVEFORMATEX wf)
{
    TRACE("(%p)\n",wf);
                                                                                
    if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
        return FALSE;
                                                                                
    if (wf->wFormatTag == WAVE_FORMAT_PCM) {
        if (wf->nChannels >= 1 && wf->nChannels <= MAX_CHANNELS) {
            if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
                return TRUE;
        }
    } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
        WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
                                                                                
        if (wf->cbSize == 22 && IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM)) {
            if (wf->nChannels >=1 && wf->nChannels <= MAX_CHANNELS) {
                if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
                    if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
                        return TRUE;
                } else
                    WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
            }
        } else
            WARN("only KSDATAFORMAT_SUBTYPE_PCM supported\n");
    } else
        WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
                                                                                
    return FALSE;
}

333
static void copy_format(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
334 335 336 337 338 339 340 341 342 343
{
    ZeroMemory(wf2, sizeof(wf2));
    if (wf1->wFormatTag == WAVE_FORMAT_PCM)
        memcpy(wf2, wf1, sizeof(PCMWAVEFORMAT));
    else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
        memcpy(wf2, wf1, sizeof(WAVEFORMATPCMEX));
    else
        memcpy(wf2, wf1, sizeof(WAVEFORMATEX) + wf1->cbSize);
}

344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
static char* get_stream_name(const char* direction, unsigned int dev_id)
{
    char exename[MAX_PATH];
    char *basename, *s;
    char* stream_name;

    GetModuleFileNameA(NULL, exename, sizeof(exename));
    exename[sizeof(exename)-1]='\0';
    basename = s = exename;
    while (*s)
    {
        if (*s == '/' || *s == '\\')
            basename = s+1;
        s++;
    }

    stream_name = HeapAlloc(GetProcessHeap(), 0, 4+strlen(basename)+10+strlen(direction)+10+1);
    sprintf(stream_name, "%s (%lu:%s%u)", basename, (unsigned long)getpid(), direction, dev_id);

    return stream_name;
}

366 367 368 369
/******************************************************************
 *		ESD_CloseWaveOutDevice
 *
 */
370
static void	ESD_CloseWaveOutDevice(WINE_WAVEOUT* wwo)
371
{
372
	HeapFree(GetProcessHeap(), 0, wwo->stream_name);
373 374
	esd_close(wwo->stream_fd);
	wwo->stream_fd = -1;
375 376
	if (wwo->esd_fd)
		esd_close(wwo->esd_fd);
377 378 379 380 381 382
}

/******************************************************************
 *		ESD_CloseWaveInDevice
 *
 */
383
static void	ESD_CloseWaveInDevice(WINE_WAVEIN* wwi)
384
{
385
	HeapFree(GetProcessHeap(), 0, wwi->stream_name);
386 387
	esd_close(wwi->stream_fd);
	wwi->stream_fd = -1;
388 389
}

390
static int WAVE_loadcount;
391 392 393
/******************************************************************
 *		ESD_WaveClose
 */
394
static LONG ESD_WaveClose(void)
395 396 397
{
    int iDevice;

398 399 400
    if (--WAVE_loadcount)
        return 1;

401 402 403
    /* close all open devices */
    for(iDevice = 0; iDevice < MAX_WAVEOUTDRV; iDevice++)
    {
404
      if(WOutDev[iDevice].stream_fd != -1)
405 406 407 408 409 410 411
      {
        ESD_CloseWaveOutDevice(&WOutDev[iDevice]);
      }
    }

    for(iDevice = 0; iDevice < MAX_WAVEINDRV; iDevice++)
    {
412
      if(WInDev[iDevice].stream_fd != -1)
413 414 415 416 417 418 419 420 421 422 423 424 425
      {
        ESD_CloseWaveInDevice(&WInDev[iDevice]);
      }
    }

    return 1;
}

/******************************************************************
 *		ESD_WaveInit
 *
 * Initialize internal structures from ESD server info
 */
426
static LONG ESD_WaveInit(void)
427 428
{
    int 	i;
429
    int 	fd;
430 431

    TRACE("called\n");
432 433
    if (WAVE_loadcount++)
        return 1;
434 435

    /* Testing whether the esd host is alive. */
436
    if ((fd = esd_open_sound(NULL)) < 0)
437 438
    {
	WARN("esd_open_sound() failed (%d)\n", errno);
439
	return 0;
440 441 442 443 444 445
    }
    esd_close(fd);

    /* initialize all device handles to -1 */
    for (i = 0; i < MAX_WAVEOUTDRV; ++i)
    {
Francois Gouget's avatar
Francois Gouget committed
446 447
        static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','O','u','t','D','r','i','v','e','r',0};

448
	WOutDev[i].stream_fd = -1;
449 450
	memset(&WOutDev[i].caps, 0, sizeof(WOutDev[i].caps)); /* zero out
							caps values */
Austin English's avatar
Austin English committed
451
	WOutDev[i].caps.wMid = 0x00FF; 	/* Manufacturer ID */
452
    	WOutDev[i].caps.wPid = 0x0001; 	/* Product ID */
Francois Gouget's avatar
Francois Gouget committed
453
    	lstrcpyW(WOutDev[i].caps.szPname, ini);
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
        snprintf(WOutDev[i].interface_name, sizeof(WOutDev[i].interface_name), "wineesd: %d", i);

    	WOutDev[i].caps.vDriverVersion = 0x0100;
    	WOutDev[i].caps.dwFormats = 0x00000000;
    	WOutDev[i].caps.dwSupport = WAVECAPS_VOLUME;

    	WOutDev[i].caps.wChannels = 2;
    	WOutDev[i].caps.dwSupport |= WAVECAPS_LRVOLUME;

    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
    	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
	WOutDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;
    }

    for (i = 0; i < MAX_WAVEINDRV; ++i)
    {
Francois Gouget's avatar
Francois Gouget committed
479 480
        static const WCHAR ini[] = {'E','s','o','u','n','D',' ','W','a','v','e','I','n','D','r','i','v','e','r',0};

481
	WInDev[i].stream_fd = -1;
482 483 484 485
	memset(&WInDev[i].caps, 0, sizeof(WInDev[i].caps)); /* zero out
							caps values */
	WInDev[i].caps.wMid = 0x00FF;
	WInDev[i].caps.wPid = 0x0001;
Francois Gouget's avatar
Francois Gouget committed
486
	lstrcpyW(WInDev[i].caps.szPname, ini);
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
        snprintf(WInDev[i].interface_name, sizeof(WInDev[i].interface_name), "wineesd: %d", i);

	WInDev[i].caps.vDriverVersion = 0x0100;
   	WInDev[i].caps.dwFormats = 0x00000000;

    	WInDev[i].caps.wChannels = 2;

    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M08;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S08;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_4S16;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_4M16;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M08;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S08;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_2M16;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_2S16;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M08;
    	WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S08;
	WInDev[i].caps.dwFormats |= WAVE_FORMAT_1M16;
	WInDev[i].caps.dwFormats |= WAVE_FORMAT_1S16;

	WInDev[i].caps.wReserved1 = 0;
    }
509
    return 1;
510 511 512 513 514 515 516 517 518 519 520 521
}

/******************************************************************
 *		ESD_InitRingMessage
 *
 * Initialize the ring of messages for passing between driver's caller and playback/record
 * thread
 */
static int ESD_InitRingMessage(ESD_MSG_RING* mr)
{
    mr->msg_toget = 0;
    mr->msg_tosave = 0;
522 523 524 525 526 527 528
#ifdef USE_PIPE_SYNC
    if (pipe(mr->msg_pipe) < 0) {
        mr->msg_pipe[0] = -1;
        mr->msg_pipe[1] = -1;
        ERR("could not create pipe, error=%s\n", strerror(errno));
    }
#else
Francois Gouget's avatar
Francois Gouget committed
529
    mr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
530
#endif
531 532 533
    mr->ring_buffer_size = ESD_RING_BUFFER_INCREMENT;
    mr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,mr->ring_buffer_size * sizeof(RING_MSG));
    InitializeCriticalSection(&mr->msg_crst);
534
    mr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ESD_MSG_RING.msg_crst");
535 536 537 538 539 540 541 542 543
    return 0;
}

/******************************************************************
 *		ESD_DestroyRingMessage
 *
 */
static int ESD_DestroyRingMessage(ESD_MSG_RING* mr)
{
544 545 546 547
#ifdef USE_PIPE_SYNC
    close(mr->msg_pipe[0]);
    close(mr->msg_pipe[1]);
#else
548
    CloseHandle(mr->msg_event);
549
#endif
550 551
    HeapFree(GetProcessHeap(),0,mr->messages);
    mr->messages=NULL;
552
    mr->msg_crst.DebugInfo->Spare[0] = 0;
553 554 555 556 557 558 559
    DeleteCriticalSection(&mr->msg_crst);
    return 0;
}

/******************************************************************
 *		ESD_AddRingMessage
 *
560
 * Inserts a new message into the ring (should be called from DriverProc derived routines)
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
 */
static int ESD_AddRingMessage(ESD_MSG_RING* mr, enum win_wm_message msg, DWORD param, BOOL wait)
{
    HANDLE      hEvent = INVALID_HANDLE_VALUE;

    EnterCriticalSection(&mr->msg_crst);
    if ((mr->msg_toget == ((mr->msg_tosave + 1) % mr->ring_buffer_size)))
    {
	int old_ring_buffer_size = mr->ring_buffer_size;
	mr->ring_buffer_size += ESD_RING_BUFFER_INCREMENT;
	TRACE("mr->ring_buffer_size=%d\n",mr->ring_buffer_size);
	mr->messages = HeapReAlloc(GetProcessHeap(),0,mr->messages, mr->ring_buffer_size * sizeof(RING_MSG));
	/* Now we need to rearrange the ring buffer so that the new
	   buffers just allocated are in between mr->msg_tosave and
	   mr->msg_toget.
	*/
	if (mr->msg_tosave < mr->msg_toget)
	{
	    memmove(&(mr->messages[mr->msg_toget + ESD_RING_BUFFER_INCREMENT]),
		    &(mr->messages[mr->msg_toget]),
		    sizeof(RING_MSG)*(old_ring_buffer_size - mr->msg_toget)
		    );
	    mr->msg_toget += ESD_RING_BUFFER_INCREMENT;
	}
    }
    if (wait)
    {
Francois Gouget's avatar
Francois Gouget committed
588
        hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        if (hEvent == INVALID_HANDLE_VALUE)
        {
            ERR("can't create event !?\n");
            LeaveCriticalSection(&mr->msg_crst);
            return 0;
        }
        if (mr->msg_toget != mr->msg_tosave && mr->messages[mr->msg_toget].msg != WINE_WM_HEADER)
            FIXME("two fast messages in the queue!!!!\n");

        /* fast messages have to be added at the start of the queue */
        mr->msg_toget = (mr->msg_toget + mr->ring_buffer_size - 1) % mr->ring_buffer_size;

        mr->messages[mr->msg_toget].msg = msg;
        mr->messages[mr->msg_toget].param = param;
        mr->messages[mr->msg_toget].hEvent = hEvent;
    }
    else
    {
        mr->messages[mr->msg_tosave].msg = msg;
        mr->messages[mr->msg_tosave].param = param;
        mr->messages[mr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
        mr->msg_tosave = (mr->msg_tosave + 1) % mr->ring_buffer_size;
    }

    LeaveCriticalSection(&mr->msg_crst);

615 616
    /* signal a new message */
    SIGNAL_OMR(mr);
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
    if (wait)
    {
        /* wait for playback/record thread to have processed the message */
        WaitForSingleObject(hEvent, INFINITE);
        CloseHandle(hEvent);
    }

    return 1;
}

/******************************************************************
 *		ESD_RetrieveRingMessage
 *
 * Get a message from the ring. Should be called by the playback/record thread.
 */
632 633
static int ESD_RetrieveRingMessage(ESD_MSG_RING* mr, enum win_wm_message *msg,
                                   DWORD_PTR *param, HANDLE *hEvent)
634 635 636 637 638 639 640 641 642 643 644 645 646 647
{
    EnterCriticalSection(&mr->msg_crst);

    if (mr->msg_toget == mr->msg_tosave) /* buffer empty ? */
    {
        LeaveCriticalSection(&mr->msg_crst);
	return 0;
    }

    *msg = mr->messages[mr->msg_toget].msg;
    mr->messages[mr->msg_toget].msg = 0;
    *param = mr->messages[mr->msg_toget].param;
    *hEvent = mr->messages[mr->msg_toget].hEvent;
    mr->msg_toget = (mr->msg_toget + 1) % mr->ring_buffer_size;
648
    CLEAR_OMR(mr);
649 650 651 652 653 654 655 656 657 658 659
    LeaveCriticalSection(&mr->msg_crst);
    return 1;
}

/*======================================================================*
 *                  Low level WAVE OUT implementation			*
 *======================================================================*/

/**************************************************************************
 * 			wodNotifyClient			[internal]
 */
660 661
static DWORD wodNotifyClient(WINE_WAVEOUT* wwo, WORD wMsg, DWORD_PTR dwParam1,
                             DWORD_PTR dwParam2)
662
{
663
    TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg, dwParam1, dwParam2);
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685

    switch (wMsg) {
    case WOM_OPEN:
    case WOM_CLOSE:
    case WOM_DONE:
	if (wwo->wFlags != DCB_NULL &&
	    !DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
			    wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2)) {
	    WARN("can't notify client !\n");
	    return MMSYSERR_ERROR;
	}
	break;
    default:
	FIXME("Unknown callback message %u\n", wMsg);
        return MMSYSERR_INVALPARAM;
    }
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				wodUpdatePlayedTotal	[internal]
 *
686 687 688 689 690
 * dwPlayedTotal is used for wodPlayer_NotifyCompletions() and
 * wodGetPosition(), so a byte must only be reported as played once it has
 * reached the speakers. So give our best estimate based on the latency
 * reported by the esd server, and on the elapsed time since the last byte
 * was sent to the server.
691
 */
692
static void wodUpdatePlayedTotal(WINE_WAVEOUT* wwo)
693
{
694
    DWORD elapsed;
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
    if (wwo->dwPlayedTotal == wwo->dwWrittenTotal)
        return;

    /* GetTickCount() wraps every now and then, but these being all unsigned it's ok */
    elapsed = GetTickCount() - wwo->dwLastWrite;
    if (elapsed < wwo->dwLatency)
    {
        wwo->dwPlayedTotal = wwo->dwWrittenTotal - (wwo->dwLatency - elapsed) * wwo->waveFormat.Format.nAvgBytesPerSec / 1000;
        TRACE("written=%u - elapsed=%u -> played=%u\n", wwo->dwWrittenTotal, elapsed, wwo->dwPlayedTotal);
    }
    else
    {
        wwo->dwPlayedTotal = wwo->dwWrittenTotal;
        TRACE("elapsed=%u -> played=written=%u\n", elapsed, wwo->dwPlayedTotal);
    }
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
}

/**************************************************************************
 * 				wodPlayer_BeginWaveHdr          [internal]
 *
 * Makes the specified lpWaveHdr the currently playing wave header.
 * If the specified wave header is a begin loop and we're not already in
 * a loop, setup the loop.
 */
static void wodPlayer_BeginWaveHdr(WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
{
    wwo->lpPlayPtr = lpWaveHdr;

    if (!lpWaveHdr) return;

    if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
	if (wwo->lpLoopPtr) {
	    WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
	    TRACE("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
	} else {
731
            TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	    wwo->lpLoopPtr = lpWaveHdr;
	    /* Windows does not touch WAVEHDR.dwLoops,
	     * so we need to make an internal copy */
	    wwo->dwLoops = lpWaveHdr->dwLoops;
	}
    }
    wwo->dwPartialOffset = 0;
}

/**************************************************************************
 * 				wodPlayer_PlayPtrNext	        [internal]
 *
 * Advance the play pointer to the next waveheader, looping if required.
 */
static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEOUT* wwo)
{
    LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;

    wwo->dwPartialOffset = 0;
    if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
	/* We're at the end of a loop, loop if required */
	if (--wwo->dwLoops > 0) {
	    wwo->lpPlayPtr = wwo->lpLoopPtr;
	} else {
	    /* Handle overlapping loops correctly */
	    if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
		FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
		/* shall we consider the END flag for the closing loop or for
		 * the opening one or for both ???
		 * code assumes for closing loop only
		 */
	    } else {
                lpWaveHdr = lpWaveHdr->lpNext;
            }
            wwo->lpLoopPtr = NULL;
            wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
	}
    } else {
	/* We're not in a loop.  Advance to the next wave header */
	wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
    }

    return lpWaveHdr;
}

/**************************************************************************
 * 			     wodPlayer_NotifyWait               [internal]
 * Returns the number of milliseconds to wait before attempting to notify
 * completion of the specified wavehdr.
 * This is based on the number of bytes remaining to be written in the
 * wave.
 */
static DWORD wodPlayer_NotifyWait(const WINE_WAVEOUT* wwo, LPWAVEHDR lpWaveHdr)
{
    DWORD dwMillis;

    if(lpWaveHdr->reserved < wwo->dwPlayedTotal)
    {
	dwMillis = 1;
    }
    else
    {
794
        dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->waveFormat.Format.nAvgBytesPerSec;
795 796 797
	if(!dwMillis) dwMillis = 1;
    }

798
    TRACE("dwMillis = %d\n", dwMillis);
799 800 801 802 803 804 805

    return dwMillis;
}


/**************************************************************************
 * 			     wodPlayer_WriteMaxFrags            [internal]
806 807 808 809
 *
 * Esdlib will have set the stream socket buffer size to an appropriate
 * value, so now our job is to keep it full. So write what we can, and
 * return 1 if more can be written and 0 otherwise.
810
 */
811
static int wodPlayer_WriteMaxFrags(WINE_WAVEOUT* wwo)
812 813 814
{
    DWORD dwLength = wwo->lpPlayPtr->dwBufferLength - wwo->dwPartialOffset;
    int written;
815
    DWORD now;
816

817
    TRACE("Writing wavehdr %p.%u[%u]\n",
818 819
          wwo->lpPlayPtr, wwo->dwPartialOffset, wwo->lpPlayPtr->dwBufferLength);

820 821
    written = write(wwo->stream_fd, wwo->lpPlayPtr->lpData + wwo->dwPartialOffset, dwLength);
    if (written <= 0)
822
    {
823 824 825
        /* the esd buffer is full or some error occurred */
        TRACE("write(%u) failed, errno=%d\n", dwLength, errno);
        return 0;
826
    }
827 828
    now = GetTickCount();
    TRACE("Wrote %d bytes out of %u, %ums since last\n", written, dwLength, now-wwo->dwLastWrite);
829

830
    wwo->dwLastWrite = now;
831
    wwo->dwWrittenTotal += written; /* update stats on this wave device */
832 833 834 835 836 837
    if (written == dwLength)
    {
        /* We're done with this wavehdr, skip to the next one */
        wodPlayer_PlayPtrNext(wwo);
        return 1;
    }
838

839 840 841
    /* Remove the amount written and wait a bit before trying to write more */
    wwo->dwPartialOffset += written;
    return 0;
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
}


/**************************************************************************
 * 				wodPlayer_NotifyCompletions	[internal]
 *
 * Notifies and remove from queue all wavehdrs which have been played to
 * the speaker (ie. they have cleared the audio device).  If force is true,
 * we notify all wavehdrs and remove them all from the queue even if they
 * are unplayed or part of a loop.
 */
static DWORD wodPlayer_NotifyCompletions(WINE_WAVEOUT* wwo, BOOL force)
{
    LPWAVEHDR		lpWaveHdr;

    if (wwo->lpQueuePtr) {
858
	TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p), reserved=(%ld), dwWrittenTotal=(%d), force=(%d)\n",
859 860 861 862 863 864 865
	      wwo->lpQueuePtr,
	      wwo->lpPlayPtr,
	      wwo->lpLoopPtr,
	      wwo->lpQueuePtr->reserved,
	      wwo->dwWrittenTotal,
	      force);
    } else {
866
	TRACE("lpWaveHdr=(%p), lpPlayPtr=(%p), lpLoopPtr=(%p),  dwWrittenTotal=(%d), force=(%d)\n",
867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
	      wwo->lpQueuePtr,
	      wwo->lpPlayPtr,
	      wwo->lpLoopPtr,
	      wwo->dwWrittenTotal,
	      force);
    }

    /* Start from lpQueuePtr and keep notifying until:
     * - we hit an unwritten wavehdr
     * - we hit the beginning of a running loop
     * - we hit a wavehdr which hasn't finished playing
     */
    while ((lpWaveHdr = wwo->lpQueuePtr) &&
           (force ||
            (lpWaveHdr != wwo->lpPlayPtr &&
             lpWaveHdr != wwo->lpLoopPtr &&
	     lpWaveHdr->reserved <= wwo->dwWrittenTotal))) {

	wwo->lpQueuePtr = lpWaveHdr->lpNext;

	lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
	lpWaveHdr->dwFlags |= WHDR_DONE;

890
        wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911
    }
    return  (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
        wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
}

/**************************************************************************
 * 				wodPlayer_Reset			[internal]
 *
 * wodPlayer helper. Resets current output stream.
 */
static	void	wodPlayer_Reset(WINE_WAVEOUT* wwo, BOOL reset)
{
    wodUpdatePlayedTotal(wwo);

    wodPlayer_NotifyCompletions(wwo, FALSE); /* updates current notify list */

    /* we aren't able to flush any data that has already been written */
    /* to esd, otherwise we would do the flushing here */

    if (reset) {
        enum win_wm_message     msg;
912
        DWORD_PTR               param;
913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
        HANDLE                  ev;

	/* remove any buffer */
	wodPlayer_NotifyCompletions(wwo, TRUE);

	wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
	wwo->state = WINE_WS_STOPPED;
	wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;

        wwo->dwPartialOffset = 0;        /* Clear partial wavehdr */

        /* remove any existing message in the ring */
        EnterCriticalSection(&wwo->msgRing.msg_crst);

        /* return all pending headers in queue */
        while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
        {
	    TRACE("flushing msg\n");
            if (msg != WINE_WM_HEADER)
            {
                FIXME("shouldn't have headers left\n");
                SetEvent(ev);
                continue;
            }
            ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
            ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;

            wodNotifyClient(wwo, WOM_DONE, param, 0);
        }
942
        RESET_OMR(&wwo->msgRing);
943 944 945 946
        LeaveCriticalSection(&wwo->msgRing.msg_crst);
    } else {
        if (wwo->lpLoopPtr) {
            /* complicated case, not handled yet (could imply modifying the loop counter */
947
            FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965
            wwo->lpPlayPtr = wwo->lpLoopPtr;
            wwo->dwPartialOffset = 0;
            wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
        } else {
	    /* the data already written is going to be played, so take */
	    /* this fact into account here */
	    wwo->dwPlayedTotal = wwo->dwWrittenTotal;
        }
	wwo->state = WINE_WS_PAUSED;
    }
}

/**************************************************************************
 * 		      wodPlayer_ProcessMessages			[internal]
 */
static void wodPlayer_ProcessMessages(WINE_WAVEOUT* wwo)
{
    LPWAVEHDR           lpWaveHdr;
966 967 968
    enum win_wm_message msg;
    DWORD_PTR           param;
    HANDLE              ev;
969 970

    while (ESD_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
971
        TRACE("Received %s %lx\n", wodPlayerCmdString[msg - WM_USER - 1], param);
972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
	switch (msg) {
	case WINE_WM_PAUSING:
	    wodPlayer_Reset(wwo, FALSE);
	    SetEvent(ev);
	    break;
	case WINE_WM_RESTARTING:
	    wwo->state = WINE_WS_PLAYING;
	    SetEvent(ev);
	    break;
	case WINE_WM_HEADER:
	    lpWaveHdr = (LPWAVEHDR)param;

	    /* insert buffer at the end of queue */
	    {
		LPWAVEHDR*	wh;
		for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
		*wh = lpWaveHdr;
	    }
            if (!wwo->lpPlayPtr)
                wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
	    if (wwo->state == WINE_WS_STOPPED)
		wwo->state = WINE_WS_PLAYING;
	    break;
	case WINE_WM_RESETTING:
	    wodPlayer_Reset(wwo, TRUE);
	    SetEvent(ev);
	    break;
        case WINE_WM_UPDATE:
            wodUpdatePlayedTotal(wwo);
	    SetEvent(ev);
            break;
        case WINE_WM_BREAKLOOP:
            if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
                /* ensure exit at end of current loop */
                wwo->dwLoops = 1;
            }
	    SetEvent(ev);
            break;
	case WINE_WM_CLOSING:
	    /* sanity check: this should not happen since the device must have been reset before */
	    if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
	    wwo->hThread = 0;
	    wwo->state = WINE_WS_CLOSED;
	    SetEvent(ev);
	    ExitThread(0);
	    /* shouldn't go here */
	default:
	    FIXME("unknown message %d\n", msg);
	    break;
	}
    }
}

/**************************************************************************
 * 			     wodPlayer_FeedDSP			[internal]
 * Feed as much sound data as we can into the DSP and return the number of
 * milliseconds before it will be necessary to feed the DSP again.
 */
static DWORD wodPlayer_FeedDSP(WINE_WAVEOUT* wwo)
{
    wodUpdatePlayedTotal(wwo);

    /* Feed wavehdrs until we run out of wavehdrs or DSP space */
1035
    while (wwo->lpPlayPtr)
1036
    {
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
        if (wwo->dwPartialOffset != 0)
            TRACE("feeding from partial wavehdr\n");
        else
        {
            /* Note the value that dwPlayedTotal will return when this
             * wavehdr finishes playing, for the completion notifications.
             */
            wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
            TRACE("new wavehdr: reserved=(%ld) dwWrittenTotal=(%d) dwBufferLength=(%d)\n",
                  wwo->lpPlayPtr->reserved, wwo->dwWrittenTotal,
                  wwo->lpPlayPtr->dwBufferLength);
        }
        if (!wodPlayer_WriteMaxFrags(wwo))
        {
            /* the buffer is full, wait a bit */
            return wwo->dwSleepTime;
        }
1054 1055
    }

1056 1057
    TRACE("Ran out of wavehdrs or nothing to play\n");
    return INFINITE;
1058 1059 1060 1061 1062 1063 1064 1065
}


/**************************************************************************
 * 				wodPlayer			[internal]
 */
static	DWORD	CALLBACK	wodPlayer(LPVOID pmt)
{
1066
    WORD          uDevID = (DWORD_PTR)pmt;
1067
    WINE_WAVEOUT* wwo = &WOutDev[uDevID];
1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
    DWORD         dwNextFeedTime = INFINITE;   /* Time before DSP needs feeding */
    DWORD         dwNextNotifyTime = INFINITE; /* Time before next wave completion */
    DWORD         dwSleepTime;

    wwo->state = WINE_WS_STOPPED;
    SetEvent(wwo->hStartUpEvent);

    for (;;) {
        /** Wait for the shortest time before an action is required.  If there
         *  are no pending actions, wait forever for a command.
         */
        dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
1080
        TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
1081
        WAIT_OMR(&wwo->msgRing, dwSleepTime);
1082 1083 1084 1085 1086 1087 1088 1089
	wodPlayer_ProcessMessages(wwo);
	if (wwo->state == WINE_WS_PLAYING) {
	    dwNextFeedTime = wodPlayer_FeedDSP(wwo);
	    dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
	} else {
	    dwNextFeedTime = dwNextNotifyTime = INFINITE;
	}
    }
1090 1091

    return 0;
1092 1093 1094 1095 1096
}

/**************************************************************************
 * 			wodGetDevCaps				[internal]
 */
Francois Gouget's avatar
Francois Gouget committed
1097
static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
1098
{
1099
    TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121

    if (lpCaps == NULL) return MMSYSERR_NOTENABLED;

    if (wDevID >= MAX_WAVEOUTDRV) {
	TRACE("MAX_WAVOUTDRV reached !\n");
	return MMSYSERR_BADDEVICEID;
    }

    memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				wodOpen				[internal]
 */
static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
{
    WINE_WAVEOUT*	wwo;
    /* output to esound... */
    int			out_bits = ESD_BITS8, out_channels = ESD_MONO, out_rate;
    int			out_mode = ESD_STREAM, out_func = ESD_PLAY;
    esd_format_t	out_format;
1122
    int			mode;
1123

1124
    TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
    if (lpDesc == NULL) {
	WARN("Invalid Parameter !\n");
	return MMSYSERR_INVALPARAM;
    }
    if (wDevID >= MAX_WAVEOUTDRV) {
	TRACE("MAX_WAVOUTDRV reached !\n");
	return MMSYSERR_BADDEVICEID;
    }

    /* if this device is already open tell the app that it is allocated */
1135
    if(WOutDev[wDevID].stream_fd != -1)
1136 1137 1138 1139 1140 1141
    {
      TRACE("device already allocated\n");
      return MMSYSERR_ALLOCATED;
    }

    /* only PCM format is supported so far... */
1142
    if (!supportedFormat(lpDesc->lpFormat)) {
1143
        WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1144 1145 1146
             lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
             lpDesc->lpFormat->nSamplesPerSec);
        return WAVERR_BADFORMAT;
1147 1148 1149
    }

    if (dwFlags & WAVE_FORMAT_QUERY) {
1150
	TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
	     lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
	     lpDesc->lpFormat->nSamplesPerSec);
	return MMSYSERR_NOERROR;
    }

    wwo = &WOutDev[wDevID];

    /* direct sound not supported, ignore the flag */
    dwFlags &= ~WAVE_DIRECTSOUND;

    wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);

1163
    wwo->waveDesc = *lpDesc;
1164
    copy_format(lpDesc->lpFormat, &wwo->waveFormat);
1165

1166
    if (wwo->waveFormat.Format.wBitsPerSample == 0) {
1167
	WARN("Resetting zeroed wBitsPerSample\n");
1168 1169 1170 1171
	wwo->waveFormat.Format.wBitsPerSample = 8 *
	    (wwo->waveFormat.Format.nAvgBytesPerSec /
	     wwo->waveFormat.Format.nSamplesPerSec) /
	    wwo->waveFormat.Format.nChannels;
1172 1173
    }

1174
    if (wwo->waveFormat.Format.wBitsPerSample == 8)
1175
	out_bits = ESD_BITS8;
1176
    else if (wwo->waveFormat.Format.wBitsPerSample == 16)
1177 1178
	out_bits = ESD_BITS16;

1179
    if (wwo->waveFormat.Format.nChannels == 1)
1180
	out_channels = ESD_MONO;
1181
    else if (wwo->waveFormat.Format.nChannels == 2)
1182 1183 1184
	out_channels = ESD_STEREO;

    out_format = out_bits | out_channels | out_mode | out_func;
1185
    out_rate = (int) wwo->waveFormat.Format.nSamplesPerSec;
1186 1187
	TRACE("esd output format = 0x%08x, rate = %d\n", out_format, out_rate);

1188
    wwo->stream_name = get_stream_name("out", wDevID);
1189 1190
    wwo->stream_fd = esd_play_stream(out_format, out_rate, NULL, wwo->stream_name);
    TRACE("wwo->stream_fd=%d\n", wwo->stream_fd);
1191 1192 1193 1194 1195
    if(wwo->stream_fd < 0)
    {
        HeapFree(GetProcessHeap(), 0, wwo->stream_name);
        return MMSYSERR_ALLOCATED;
    }
1196

1197
    wwo->stream_id = 0;
1198 1199 1200
    wwo->dwPlayedTotal = 0;
    wwo->dwWrittenTotal = 0;

1201 1202 1203 1204 1205 1206 1207
    wwo->esd_fd = esd_open_sound(NULL);
    if (wwo->esd_fd >= 0)
    {
        wwo->dwLatency = 1000 * esd_get_latency(wwo->esd_fd) * 4 / wwo->waveFormat.Format.nAvgBytesPerSec;
    }
    else
    {
1208
        WARN("esd_open_sound() failed\n");
1209 1210 1211 1212 1213
        /* just do a rough guess at the latency and continue anyway */
        wwo->dwLatency = 1000 * (2 * ESD_BUF_SIZE) / out_rate;
    }
    TRACE("dwLatency = %ums\n", wwo->dwLatency);

1214 1215 1216 1217 1218 1219 1220 1221 1222
    /* ESD_BUF_SIZE is the socket buffer size in samples. Set dwSleepTime
     * to a fraction of that so it never get empty.
     */
    wwo->dwSleepTime = 1000 * ESD_BUF_SIZE / out_rate / 3;

    /* Set the stream socket to O_NONBLOCK, so we can stop playing smoothly */
    mode = fcntl(wwo->stream_fd, F_GETFL);
    mode |= O_NONBLOCK;
    fcntl(wwo->stream_fd, F_SETFL, mode);
1223 1224 1225 1226 1227

    ESD_InitRingMessage(&wwo->msgRing);

    /* create player thread */
    if (!(dwFlags & WAVE_DIRECTSOUND)) {
Francois Gouget's avatar
Francois Gouget committed
1228
	wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1229
        wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID,
1230
                                    0, NULL);
1231 1232 1233 1234 1235 1236 1237
	WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
	CloseHandle(wwo->hStartUpEvent);
    } else {
	wwo->hThread = INVALID_HANDLE_VALUE;
    }
    wwo->hStartUpEvent = INVALID_HANDLE_VALUE;

1238
    TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
1239 1240 1241
	  wwo->waveFormat.Format.wBitsPerSample, wwo->waveFormat.Format.nAvgBytesPerSec,
	  wwo->waveFormat.Format.nSamplesPerSec, wwo->waveFormat.Format.nChannels,
	  wwo->waveFormat.Format.nBlockAlign);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255

    return wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
}

/**************************************************************************
 * 				wodClose			[internal]
 */
static DWORD wodClose(WORD wDevID)
{
    DWORD		ret = MMSYSERR_NOERROR;
    WINE_WAVEOUT*	wwo;

    TRACE("(%u);\n", wDevID);

1256
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }

    wwo = &WOutDev[wDevID];
    if (wwo->lpQueuePtr) {
	WARN("buffers still playing !\n");
	ret = WAVERR_STILLPLAYING;
    } else {
	TRACE("imhere[3-close]\n");
	if (wwo->hThread != INVALID_HANDLE_VALUE) {
	    ESD_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
	}

        ESD_DestroyRingMessage(&wwo->msgRing);

	ESD_CloseWaveOutDevice(wwo);	/* close the stream and clean things up */

	ret = wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
    }
    return ret;
}

/**************************************************************************
 * 				wodWrite			[internal]
 *
 */
static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
{
1286
    TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
1287 1288

    /* first, do the sanity checks... */
1289
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310
        WARN("bad dev ID !\n");
	return MMSYSERR_BADDEVICEID;
    }

    if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
    {
	TRACE("unprepared\n");
	return WAVERR_UNPREPARED;
    }

    if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
    {
	TRACE("still playing\n");
	return WAVERR_STILLPLAYING;
    }

    lpWaveHdr->dwFlags &= ~WHDR_DONE;
    lpWaveHdr->dwFlags |= WHDR_INQUEUE;
    lpWaveHdr->lpNext = 0;

    TRACE("adding ring message\n");
1311 1312
    ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER,
                       (DWORD_PTR)lpWaveHdr, FALSE);
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			wodPause				[internal]
 */
static DWORD wodPause(WORD wDevID)
{
    TRACE("(%u);!\n", wDevID);

1324
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }

    TRACE("imhere[3-PAUSING]\n");
    ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			wodRestart				[internal]
 */
static DWORD wodRestart(WORD wDevID)
{
    TRACE("(%u);\n", wDevID);

1342
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }

    if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
	TRACE("imhere[3-RESTARTING]\n");
	ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
    }

    /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
    /* FIXME: Myst crashes with this ... hmm -MM
       return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
    */

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			wodReset				[internal]
 */
static DWORD wodReset(WORD wDevID)
{
    TRACE("(%u);\n", wDevID);

1367
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }

    TRACE("imhere[3-RESET]\n");
    ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				wodGetPosition			[internal]
 */
static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
{
    WINE_WAVEOUT*	wwo;

1385
    TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
1386

1387
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1388 1389 1390 1391
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }

1392 1393 1394 1395
    if (lpTime == NULL)	{
        WARN("invalid parameter: lpTime == NULL\n");
        return MMSYSERR_INVALPARAM;
    }
1396 1397 1398 1399

    wwo = &WOutDev[wDevID];
    ESD_AddRingMessage(&wwo->msgRing, WINE_WM_UPDATE, 0, TRUE);

1400
    return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->waveFormat);
1401 1402 1403 1404 1405 1406 1407 1408 1409
}

/**************************************************************************
 * 				wodBreakLoop			[internal]
 */
static DWORD wodBreakLoop(WORD wDevID)
{
    TRACE("(%u);\n", wDevID);

1410
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1) {
1411 1412 1413 1414 1415 1416 1417
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }
    ESD_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
    return MMSYSERR_NOERROR;
}

1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
static esd_player_info_t* wod_get_player(WINE_WAVEOUT* wwo, esd_info_t** esd_all_info)
{
    esd_player_info_t* player;

    if (wwo->esd_fd == -1)
    {
        wwo->esd_fd = esd_open_sound(NULL);
        if (wwo->esd_fd < 0)
        {
            WARN("esd_open_sound() failed (%d)\n", errno);
1428
            *esd_all_info = NULL;
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451
            return NULL;
        }
    }

    *esd_all_info = esd_get_all_info(wwo->esd_fd);
    if (!*esd_all_info)
    {
        WARN("esd_get_all_info() failed (%d)\n", errno);
        return NULL;
    }

    for (player = (*esd_all_info)->player_list; player != NULL; player = player->next)
    {
        if (strcmp(player->name, wwo->stream_name) == 0)
        {
            wwo->stream_id = player->source_id;
            return player;
        }
    }

    return NULL;
}

1452 1453 1454 1455 1456
/**************************************************************************
 * 				wodGetVolume			[internal]
 */
static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
{
1457 1458 1459
    esd_info_t* esd_all_info;
    esd_player_info_t* player;
    DWORD ret;
1460

1461 1462 1463 1464 1465
    if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].stream_fd == -1)
    {
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }
1466

1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
    ret = MMSYSERR_ERROR;
    player = wod_get_player(WOutDev+wDevID, &esd_all_info);
    if (player)
    {
        DWORD left, right;
        left = (player->left_vol_scale * 0xFFFF) / ESD_VOLUME_BASE;
        right = (player->right_vol_scale * 0xFFFF) / ESD_VOLUME_BASE;
        TRACE("volume = %u / %u\n", left, right);
        *lpdwVol = left + (right << 16);
        ret = MMSYSERR_NOERROR;
    }
1478

1479 1480
    if (esd_all_info)
        esd_free_all_info(esd_all_info);
1481
    return ret;
1482 1483 1484 1485 1486 1487 1488
}

/**************************************************************************
 * 				wodSetVolume			[internal]
 */
static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
{
1489
    WINE_WAVEOUT* wwo = WOutDev+wDevID;
1490

1491 1492 1493 1494 1495
    if (wDevID >= MAX_WAVEOUTDRV || wwo->stream_fd == -1)
    {
	WARN("bad device ID !\n");
	return MMSYSERR_BADDEVICEID;
    }
1496

1497 1498 1499 1500 1501 1502 1503 1504
    /* stream_id is the ESD's file descriptor for our stream so it's should
     * be non-zero if set.
     */
    if (!wwo->stream_id)
    {
        esd_info_t* esd_all_info;
        /* wod_get_player sets the stream_id as a side effect */
        wod_get_player(wwo, &esd_all_info);
1505 1506
        if (esd_all_info)
            esd_free_all_info(esd_all_info);
1507 1508 1509
    }
    if (!wwo->stream_id)
        return MMSYSERR_ERROR;
1510

1511 1512 1513
    esd_set_stream_pan(wwo->esd_fd, wwo->stream_id,
                       LOWORD(dwParam) * ESD_VOLUME_BASE / 0xFFFF,
                       HIWORD(dwParam) * ESD_VOLUME_BASE / 0xFFFF);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				wodGetNumDevs			[internal]
 */
static	DWORD	wodGetNumDevs(void)
{
    return MAX_WAVEOUTDRV;
}

/**************************************************************************
 *                              wodDevInterfaceSize             [internal]
 */
static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
{
    TRACE("(%u, %p)\n", wDevID, dwParam1);
 
    *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
                                    NULL, 0 ) * sizeof(WCHAR);
    return MMSYSERR_NOERROR;
}
 
/**************************************************************************
 *                              wodDevInterface                 [internal]
 */
static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
{
    if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
                                        NULL, 0 ) * sizeof(WCHAR))
    {
        MultiByteToWideChar(CP_ACP, 0, WOutDev[wDevID].interface_name, -1,
                            dwParam1, dwParam2 / sizeof(WCHAR));
        return MMSYSERR_NOERROR;
    }
    return MMSYSERR_INVALPARAM;
}
 
/**************************************************************************
 * 				wodMessage (WINEESD.@)
 */
DWORD WINAPI ESD_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1557
                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1558
{
1559
    TRACE("(%u, %04X, %08X, %08lX, %08lX);\n",
1560 1561 1562 1563
	  wDevID, wMsg, dwUser, dwParam1, dwParam2);

    switch (wMsg) {
    case DRVM_INIT:
1564
        return ESD_WaveInit();
1565
    case DRVM_EXIT:
1566
        return ESD_WaveClose();
1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
    case DRVM_ENABLE:
    case DRVM_DISABLE:
	/* FIXME: Pretend this is supported */
	return 0;
    case WODM_OPEN:	 	return wodOpen		(wDevID, (LPWAVEOPENDESC)dwParam1,	dwParam2);
    case WODM_CLOSE:	 	return wodClose		(wDevID);
    case WODM_WRITE:	 	return wodWrite		(wDevID, (LPWAVEHDR)dwParam1,		dwParam2);
    case WODM_PAUSE:	 	return wodPause		(wDevID);
    case WODM_GETPOS:	 	return wodGetPosition	(wDevID, (LPMMTIME)dwParam1, 		dwParam2);
    case WODM_BREAKLOOP: 	return wodBreakLoop     (wDevID);
1577 1578
    case WODM_PREPARE:	 	return MMSYSERR_NOTSUPPORTED;
    case WODM_UNPREPARE: 	return MMSYSERR_NOTSUPPORTED;
Francois Gouget's avatar
Francois Gouget committed
1579
    case WODM_GETDEVCAPS:	return wodGetDevCaps	(wDevID, (LPWAVEOUTCAPSW)dwParam1,	dwParam2);
1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608
    case WODM_GETNUMDEVS:	return wodGetNumDevs	();
    case WODM_GETPITCH:	 	return MMSYSERR_NOTSUPPORTED;
    case WODM_SETPITCH:	 	return MMSYSERR_NOTSUPPORTED;
    case WODM_GETPLAYBACKRATE:	return MMSYSERR_NOTSUPPORTED;
    case WODM_SETPLAYBACKRATE:	return MMSYSERR_NOTSUPPORTED;
    case WODM_GETVOLUME:	return wodGetVolume	(wDevID, (LPDWORD)dwParam1);
    case WODM_SETVOLUME:	return wodSetVolume	(wDevID, dwParam1);
    case WODM_RESTART:		return wodRestart	(wDevID);
    case WODM_RESET:		return wodReset		(wDevID);

    case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize       (wDevID, (LPDWORD)dwParam1);
    case DRV_QUERYDEVICEINTERFACE:     return wodDevInterface           (wDevID, (PWCHAR)dwParam1, dwParam2);
    case DRV_QUERYDSOUNDIFACE:	return wodDsCreate	(wDevID, (PIDSDRIVER*)dwParam1);
    case DRV_QUERYDSOUNDDESC:	return wodDsDesc	(wDevID, (PDSDRIVERDESC)dwParam1);
    default:
	FIXME("unknown message %d!\n", wMsg);
    }
    return MMSYSERR_NOTSUPPORTED;
}

/*======================================================================*
 *                  Low level WAVE IN implementation			*
 *======================================================================*/

/**************************************************************************
 * 				widGetNumDevs			[internal]
 */
static	DWORD	widGetNumDevs(void)
{
1609
    TRACE("%d\n", MAX_WAVEINDRV);
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643
    return MAX_WAVEINDRV;
}

/**************************************************************************
 *                              widDevInterfaceSize             [internal]
 */
static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
{
    TRACE("(%u, %p)\n", wDevID, dwParam1);
 
 
    *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
                                    NULL, 0 ) * sizeof(WCHAR);
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 *                              widDevInterface                 [internal]
 */
static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
{
    if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
                                        NULL, 0 ) * sizeof(WCHAR))
    {
        MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
                            dwParam1, dwParam2 / sizeof(WCHAR));
        return MMSYSERR_NOERROR;
    }
    return MMSYSERR_INVALPARAM;
}

/**************************************************************************
 * 			widNotifyClient			[internal]
 */
1644 1645
static DWORD widNotifyClient(WINE_WAVEIN* wwi, WORD wMsg, DWORD_PTR dwParam1,
                             DWORD_PTR dwParam2)
1646
{
1647
    TRACE("wMsg = 0x%04x dwParm1 = %08lX dwParam2 = %08lX\n", wMsg, dwParam1, dwParam2);
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670

    switch (wMsg) {
    case WIM_OPEN:
    case WIM_CLOSE:
    case WIM_DATA:
	if (wwi->wFlags != DCB_NULL &&
	    !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags,
			    (HDRVR)wwi->waveDesc.hWave, wMsg,
			    wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
	    WARN("can't notify client !\n");
	    return MMSYSERR_ERROR;
	}
	break;
    default:
	FIXME("Unknown callback message %u\n", wMsg);
	return MMSYSERR_INVALPARAM;
    }
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			widGetDevCaps				[internal]
 */
Francois Gouget's avatar
Francois Gouget committed
1671
static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1672
{
1673
    TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690

    if (lpCaps == NULL) return MMSYSERR_NOTENABLED;

    if (wDevID >= MAX_WAVEINDRV) {
	TRACE("MAX_WAVINDRV reached !\n");
	return MMSYSERR_BADDEVICEID;
    }

    memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				widRecorder			[internal]
 */
static	DWORD	CALLBACK	widRecorder(LPVOID pmt)
{
1691
    WORD                uDevID = (DWORD_PTR)pmt;
1692
    WINE_WAVEIN*        wwi = &WInDev[uDevID];
1693 1694 1695
    WAVEHDR*            lpWaveHdr;
    DWORD               dwSleepTime;
    int                 bytesRead;
1696
    enum win_wm_message msg;
1697 1698
    DWORD_PTR           param;
    HANDLE              ev;
1699 1700 1701 1702

    SetEvent(wwi->hStartUpEvent);

    /* make sleep time to be # of ms to record one packet */
1703
    dwSleepTime = (1024 * 1000) / wwi->waveFormat.Format.nAvgBytesPerSec;
1704
    TRACE("sleeptime=%d ms\n", dwSleepTime);
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716

    for(;;) {
	TRACE("wwi->lpQueuePtr=(%p), wwi->state=(%d)\n",wwi->lpQueuePtr,wwi->state);

	/* read all data is esd input buffer. */
	if ((wwi->lpQueuePtr != NULL) && (wwi->state == WINE_WS_PLAYING))
	{
	    lpWaveHdr = wwi->lpQueuePtr;
 
	    TRACE("read as much as we can\n");
	    while(wwi->lpQueuePtr)
	    {
1717
		TRACE("attempt to read %d bytes\n",lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1718
		bytesRead = read(wwi->stream_fd,
1719 1720
			      lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
			      lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1721
		TRACE("bytesRead=%d\n",bytesRead);
1722
		if (bytesRead <= 0) break; /* So we can stop recording smoothly */
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739
 
		lpWaveHdr->dwBytesRecorded	+= bytesRead;
		wwi->dwRecordedTotal		+= bytesRead;

		/* buffer full. notify client */
		if (lpWaveHdr->dwBytesRecorded >= lpWaveHdr->dwBufferLength)
		{
		    /* must copy the value of next waveHdr, because we have no idea of what
		     * will be done with the content of lpWaveHdr in callback
		     */
		    LPWAVEHDR	lpNext = lpWaveHdr->lpNext;

		    TRACE("waveHdr full.\n");
 
		    lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
		    lpWaveHdr->dwFlags |=  WHDR_DONE;
 
1740
                    widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1741 1742 1743 1744 1745 1746
		    lpWaveHdr = wwi->lpQueuePtr = lpNext;
		}
	    }
	}

	/* wait for dwSleepTime or an event in thread's queue */
1747
	WAIT_OMR(&wwi->msgRing, dwSleepTime);
1748 1749 1750

	while (ESD_RetrieveRingMessage(&wwi->msgRing, &msg, &param, &ev))
	{
1751
            TRACE("msg=%s param=0x%lx\n",wodPlayerCmdString[msg - WM_USER - 1], param);
1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797
	    switch(msg) {
	    case WINE_WM_PAUSING:
		wwi->state = WINE_WS_PAUSED;

		/* Put code here to "pause" esd recording
		 */

		SetEvent(ev);
		break;
	    case WINE_WM_STARTING:
		wwi->state = WINE_WS_PLAYING;

		/* Put code here to "start" esd recording
		 */

		SetEvent(ev);
		break;
	    case WINE_WM_HEADER:
		lpWaveHdr = (LPWAVEHDR)param;
		/* insert buffer at end of queue */
		{
		    LPWAVEHDR* wh;
		    int num_headers = 0;
		    for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext))
		    {
			num_headers++;

		    }
		    *wh=lpWaveHdr;
		}
		break;
	    case WINE_WM_STOPPING:
		if (wwi->state != WINE_WS_STOPPED)
		{

		    /* Put code here to "stop" esd recording
		     */

		    /* return current buffer to app */
		    lpWaveHdr = wwi->lpQueuePtr;
		    if (lpWaveHdr)
		    {
			LPWAVEHDR lpNext = lpWaveHdr->lpNext;
		        TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
		        lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
		        lpWaveHdr->dwFlags |= WHDR_DONE;
1798
                        widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814
		        wwi->lpQueuePtr = lpNext;
		    }
		}
		wwi->state = WINE_WS_STOPPED;
		SetEvent(ev);
		break;
	    case WINE_WM_RESETTING:
		wwi->state = WINE_WS_STOPPED;
		wwi->dwRecordedTotal = 0;

		/* return all buffers to the app */
		for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
		    TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
		    lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
		    lpWaveHdr->dwFlags |= WHDR_DONE;

1815
                    widNotifyClient(wwi, WIM_DATA, (DWORD_PTR)lpWaveHdr, 0);
1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
		}
		wwi->lpQueuePtr = NULL; 
		SetEvent(ev);
		break;
	    case WINE_WM_CLOSING:
		wwi->hThread = 0;
		wwi->state = WINE_WS_CLOSED;
		SetEvent(ev);
		ExitThread(0);
		/* shouldn't go here */
	    default:
		FIXME("unknown message %d\n", msg);
		break;
	    }
	}
    }
    ExitThread(0);
    /* just for not generating compilation warnings... should never be executed */
    return 0;
}

/**************************************************************************
 * 				widOpen				[internal]
 */
static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
{
    WINE_WAVEIN*	wwi;
    /* input esound... */
    int			in_bits = ESD_BITS16, in_channels = ESD_STEREO, in_rate;
#ifdef WID_USE_ESDMON
    int			in_mode = ESD_STREAM, in_func = ESD_PLAY;
#else
    int			in_mode = ESD_STREAM, in_func = ESD_RECORD;
#endif
    esd_format_t	in_format;
    int			mode;

1853
    TRACE("(%u, %p %08X);\n",wDevID, lpDesc, dwFlags);
1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864
    if (lpDesc == NULL) {
	WARN("Invalid Parametr (lpDesc == NULL)!\n");
	return MMSYSERR_INVALPARAM;
    }

    if (wDevID >= MAX_WAVEINDRV) {
	TRACE ("MAX_WAVEINDRV reached !\n");
	return MMSYSERR_BADDEVICEID;
    }

    /* if this device is already open tell the app that it is allocated */
1865
    if(WInDev[wDevID].stream_fd != -1)
1866 1867 1868 1869 1870 1871
    {
	TRACE("device already allocated\n");
	return MMSYSERR_ALLOCATED;
    }

    /* only PCM format is support so far... */
1872
    if (!supportedFormat(lpDesc->lpFormat)) {
1873
        WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1874 1875 1876
             lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
             lpDesc->lpFormat->nSamplesPerSec);
        return WAVERR_BADFORMAT;
1877 1878 1879
    }

    if (dwFlags & WAVE_FORMAT_QUERY) {
1880
	TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
	     lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
	     lpDesc->lpFormat->nSamplesPerSec);
	return MMSYSERR_NOERROR;
    }

    wwi = &WInDev[wDevID];

    /* direct sound not supported, ignore the flag */
    dwFlags &= ~WAVE_DIRECTSOUND;

    wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1892 1893

    wwi->waveDesc = *lpDesc;
1894
    copy_format(lpDesc->lpFormat, &wwi->waveFormat);
1895

1896
    if (wwi->waveFormat.Format.wBitsPerSample == 0) {
1897
	WARN("Resetting zerod wBitsPerSample\n");
1898 1899 1900 1901
	wwi->waveFormat.Format.wBitsPerSample = 8 *
	    (wwi->waveFormat.Format.nAvgBytesPerSec /
	     wwi->waveFormat.Format.nSamplesPerSec) /
	    wwi->waveFormat.Format.nChannels;
1902 1903
    }

1904
    if (wwi->waveFormat.Format.wBitsPerSample == 8)
1905
	in_bits = ESD_BITS8;
1906
    else if (wwi->waveFormat.Format.wBitsPerSample == 16)
1907 1908
	in_bits = ESD_BITS16;

1909
    if (wwi->waveFormat.Format.nChannels == 1)
1910
	in_channels = ESD_MONO;
1911
    else if (wwi->waveFormat.Format.nChannels == 2)
1912 1913 1914
	in_channels = ESD_STEREO;

    in_format = in_bits | in_channels | in_mode | in_func;
1915
    in_rate = (int) wwi->waveFormat.Format.nSamplesPerSec;
1916 1917
	TRACE("esd input format = 0x%08x, rate = %d\n", in_format, in_rate);

1918
    wwi->stream_name = get_stream_name("in", wDevID);
1919
#ifdef WID_USE_ESDMON
1920
    wwi->stream_fd = esd_monitor_stream(in_format, in_rate, NULL, wwi->stream_name);
1921
#else
1922
    wwi->stream_fd = esd_record_stream(in_format, in_rate, NULL, wwi->stream_name);
1923
#endif
1924
    TRACE("wwi->stream_fd=%d\n",wwi->stream_fd);
1925 1926 1927 1928 1929
    if(wwi->stream_fd < 0)
    {
        HeapFree(GetProcessHeap(), 0, wwi->stream_name);
        return MMSYSERR_ALLOCATED;
    }
1930 1931 1932 1933 1934 1935 1936
    wwi->state = WINE_WS_STOPPED;

    if (wwi->lpQueuePtr) {
	WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
	wwi->lpQueuePtr = NULL;
    }

1937 1938
    /* Set the socket to O_NONBLOCK, so we can stop recording smoothly */
    mode = fcntl(wwi->stream_fd, F_GETFL);
1939
    mode |= O_NONBLOCK;
1940
    fcntl(wwi->stream_fd, F_SETFL, mode);
1941 1942 1943 1944 1945 1946 1947 1948

    wwi->dwRecordedTotal = 0;
    wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);

    ESD_InitRingMessage(&wwi->msgRing);

    /* create recorder thread */
    if (!(dwFlags & WAVE_DIRECTSOUND)) {
Francois Gouget's avatar
Francois Gouget committed
1949
	wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1950
        wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD_PTR)wDevID,
1951
                                    0, NULL);
1952 1953 1954 1955 1956 1957 1958
	WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
	CloseHandle(wwi->hStartUpEvent);
    } else {
	wwi->hThread = INVALID_HANDLE_VALUE;
    }
    wwi->hStartUpEvent = INVALID_HANDLE_VALUE;

1959
    TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
1960 1961 1962
	  wwi->waveFormat.Format.wBitsPerSample, wwi->waveFormat.Format.nAvgBytesPerSec,
	  wwi->waveFormat.Format.nSamplesPerSec, wwi->waveFormat.Format.nChannels,
	  wwi->waveFormat.Format.nBlockAlign);
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997
    return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
}

/**************************************************************************
 * 				widClose			[internal]
 */
static DWORD widClose(WORD wDevID)
{
    WINE_WAVEIN*	wwi;

    TRACE("(%u);\n", wDevID);
    if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
	WARN("can't close !\n");
	return MMSYSERR_INVALHANDLE;
    }

    wwi = &WInDev[wDevID];

    if (wwi->lpQueuePtr != NULL) {
	WARN("still buffers open !\n");
	return WAVERR_STILLPLAYING;
    }

    ESD_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
    ESD_CloseWaveInDevice(wwi);
    wwi->state = WINE_WS_CLOSED;
    ESD_DestroyRingMessage(&wwi->msgRing);
    return widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
}

/**************************************************************************
 * 				widAddBuffer		[internal]
 */
static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
{
1998
    TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017

    if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
	WARN("can't do it !\n");
	return MMSYSERR_INVALHANDLE;
    }
    if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
	TRACE("never been prepared !\n");
	return WAVERR_UNPREPARED;
    }
    if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
	TRACE("header already in use !\n");
	return WAVERR_STILLPLAYING;
    }

    lpWaveHdr->dwFlags |= WHDR_INQUEUE;
    lpWaveHdr->dwFlags &= ~WHDR_DONE;
    lpWaveHdr->dwBytesRecorded = 0;
    lpWaveHdr->lpNext = NULL;

2018 2019
    ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER,
                       (DWORD_PTR)lpWaveHdr, FALSE);
2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			widStart				[internal]
 */
static DWORD widStart(WORD wDevID)
{
    TRACE("(%u);\n", wDevID);
    if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
	WARN("can't start recording !\n");
	return MMSYSERR_INVALHANDLE;
    }

    ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			widStop					[internal]
 */
static DWORD widStop(WORD wDevID)
{
    TRACE("(%u);\n", wDevID);
    if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
	WARN("can't stop !\n");
	return MMSYSERR_INVALHANDLE;
    }

    ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);

    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 			widReset				[internal]
 */
static DWORD widReset(WORD wDevID)
{
    TRACE("(%u);\n", wDevID);
    if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].state == WINE_WS_CLOSED) {
	WARN("can't reset !\n");
	return MMSYSERR_INVALHANDLE;
    }
    ESD_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
    return MMSYSERR_NOERROR;
}

/**************************************************************************
 * 				widMessage (WINEESD.6)
 */
DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2072
                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2073
{
2074
    TRACE("(%u, %04X, %08X, %08lX, %08lX);\n",
2075 2076 2077
	  wDevID, wMsg, dwUser, dwParam1, dwParam2);
    switch (wMsg) {
    case DRVM_INIT:
2078
        return ESD_WaveInit();
2079
    case DRVM_EXIT:
2080
        return ESD_WaveClose();
2081 2082 2083 2084 2085 2086 2087
    case DRVM_ENABLE:
    case DRVM_DISABLE:
	/* FIXME: Pretend this is supported */
	return 0;
    case WIDM_OPEN:	 	return widOpen		(wDevID, (LPWAVEOPENDESC)dwParam1,	dwParam2);
    case WIDM_CLOSE:		return widClose		(wDevID);
    case WIDM_ADDBUFFER:	return widAddBuffer	(wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2088 2089
    case WIDM_PREPARE:		return MMSYSERR_NOTSUPPORTED;
    case WIDM_UNPREPARE:	return MMSYSERR_NOTSUPPORTED;
Francois Gouget's avatar
Francois Gouget committed
2090
    case WIDM_GETDEVCAPS:	return widGetDevCaps	(wDevID, (LPWAVEINCAPSW)dwParam1,	dwParam2);
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108
    case WIDM_GETNUMDEVS:	return widGetNumDevs	();
    case WIDM_RESET:		return widReset		(wDevID);
    case WIDM_START:		return widStart		(wDevID);
    case WIDM_STOP:		return widStop		(wDevID);
    case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize       (wDevID, (LPDWORD)dwParam1);
    case DRV_QUERYDEVICEINTERFACE:     return widDevInterface           (wDevID, (PWCHAR)dwParam1, dwParam2);
    default:
	FIXME("unknown message %d!\n", wMsg);
    }
    return MMSYSERR_NOTSUPPORTED;
}

#else /* !HAVE_ESD */

/**************************************************************************
 * 				wodMessage (WINEESD.@)
 */
DWORD WINAPI ESD_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2109
                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2110
{
2111
    FIXME("(%u, %04X, %08X, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2112 2113 2114 2115 2116 2117 2118
    return MMSYSERR_NOTENABLED;
}

/**************************************************************************
 * 				widMessage (WINEESD.6)
 */
DWORD WINAPI ESD_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
2119
                            DWORD_PTR dwParam1, DWORD_PTR dwParam2)
2120
{
2121
    FIXME("(%u, %04X, %08X, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2122 2123 2124 2125
    return MMSYSERR_NOTENABLED;
}

#endif /* HAVE_ESD */