audio.c 31.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Audio management UI code
 *
 * Copyright 2004 Chris Morgan
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21
 *
 */

22
#define WIN32_LEAN_AND_MEAN
23 24 25
#define NONAMELESSSTRUCT
#define NONAMELESSUNION

26 27 28 29 30 31 32 33
#include "config.h"
#include "wine/port.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

34
#include <windows.h>
35 36 37 38 39 40
#include <wine/debug.h>
#include <shellapi.h>
#include <objbase.h>
#include <shlguid.h>
#include <shlwapi.h>
#include <shlobj.h>
41
#include <mmsystem.h>
42 43
#include <mmreg.h>
#include <mmddk.h>
44 45 46 47 48 49

#include "winecfg.h"
#include "resource.h"

WINE_DEFAULT_DEBUG_CHANNEL(winecfg);

50 51
#define DRIVER_MASK 0x80000000
#define DEVICE_MASK 0x40000000
52
#define MAX_NAME_LENGTH 64
53

54
typedef DWORD (WINAPI * MessagePtr)(UINT, UINT, DWORD_PTR, DWORD_PTR, DWORD_PTR);
55

56 57 58
static struct DSOUNDACCEL
{
  UINT displayID;
59
  UINT visible;
60
  const char* settingStr;
61 62 63 64 65 66
} DSound_HW_Accels[] = {
  {IDS_ACCEL_FULL,      1, "Full"},
  {IDS_ACCEL_STANDARD,  0, "Standard"},
  {IDS_ACCEL_BASIC,     0, "Basic"},
  {IDS_ACCEL_EMULATION, 1, "Emulation"},
  {0, 0, 0}
67 68
};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static const char* DSound_Rates[] = {
  "48000",
  "44100",
  "22050",
  "16000",
  "11025",
  "8000",
  NULL
};

static const char* DSound_Bits[] = {
  "8",
  "16",
  NULL
};

85 86 87 88
typedef struct
{
  UINT nameID;
  const char *szDriver;
89
  HDRVR hDriver;
90 91
} AUDIO_DRIVER;

92
static AUDIO_DRIVER sAudioDrivers[] = {
93 94
  {IDS_DRIVER_ALSA,      "alsa"},
  {IDS_DRIVER_OSS,       "oss"},
95
  {IDS_DRIVER_COREAUDIO, "coreaudio"},
96 97
  {IDS_DRIVER_JACK,      "jack"},
  {IDS_DRIVER_NAS,       "nas"},
98
  {IDS_DRIVER_ESOUND,    "esd"},
99
  {0, ""}
100 101
};

102 103 104 105 106 107 108 109 110 111 112
/* list of available drivers */
static AUDIO_DRIVER * loadedAudioDrv;

/* local copy of registry setting */
static char curAudioDriver[1024];

/* driver index to configure */
static int toConfigure;

/* display a driver specific configuration dialog */
static void configureAudioDriver(HWND hDlg)
113
{
114
    const AUDIO_DRIVER *pAudioDrv = &loadedAudioDrv[toConfigure];
115

116
    if (strlen(pAudioDrv->szDriver) != 0)
117
    {
118
        HDRVR hdrvr;
119
        char wine_driver[MAX_NAME_LENGTH + 9];
120
        sprintf(wine_driver, "wine%s.drv", pAudioDrv->szDriver);
121
        hdrvr = pAudioDrv->hDriver;
122 123 124 125 126 127 128 129
        if (hdrvr != 0)
        {
            if (SendDriverMessage(hdrvr, DRV_QUERYCONFIGURE, 0, 0) != 0)
            {
                DRVCONFIGINFO dci;
                dci.dwDCISize = sizeof (dci);
                dci.lpszDCISectionName = NULL;
                dci.lpszDCIAliasName = NULL;
130
                SendDriverMessage(hdrvr, DRV_CONFIGURE, 0, (LONG_PTR)&dci);
131 132 133 134
            }
        }
        else
        {
135 136 137 138 139 140 141 142 143 144 145
            WCHAR wine_driverW[MAX_NAME_LENGTH+9];
            WCHAR messageStr[256];
            WCHAR str[1024];
            
            MultiByteToWideChar (CP_ACP, 0, wine_driver, -1, wine_driverW, 
                sizeof (wine_driverW)/sizeof(wine_driverW[0])); 
            
            LoadStringW (GetModuleHandle (NULL), IDS_OPEN_DRIVER_ERROR, messageStr,
                sizeof(messageStr)/sizeof(messageStr[0]));
            wsprintfW (str, messageStr, wine_driverW);
            MessageBoxW (hDlg, str, NULL, MB_OK | MB_ICONERROR);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
        }
    }
}

/* is driver in local copy of driver registry string */
static BOOL isDriverSet(const char * driver)
{
    WINE_TRACE("driver = %s, curAudioDriver = %s\n", driver, curAudioDriver);

    if (strstr(curAudioDriver, driver))
        return TRUE;

    return FALSE;
}

/* add driver to local copy of driver registry string */
static void addDriver(const char * driver)
{
    if (!isDriverSet(driver))
    {
        if (strlen(curAudioDriver))
            strcat(curAudioDriver, ",");
        strcat(curAudioDriver, driver);
169 170 171
    }
}

172 173
/* remove driver from local copy of driver registry string */
static void removeDriver(const char * driver)
174
{
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    char pattern[32], *p;
    int drvlen, listlen;

    strcpy(pattern, ",");
    strcat(pattern, driver);
    strcat(pattern, ",");
    drvlen = strlen(driver);
    listlen = strlen(curAudioDriver);

    p = strstr(curAudioDriver, pattern);
    if (p) /* somewhere in the middle */
        memmove(p, p+drvlen+1, strlen(p+drvlen+1)+1);
    else if (!strncmp(curAudioDriver, pattern+1, drvlen+1)) /* the head */
        memmove(curAudioDriver, curAudioDriver+drvlen+1, listlen-drvlen);
    else if (!strncmp(curAudioDriver+listlen-drvlen-1, pattern, drvlen+1)) /* the tail */
        curAudioDriver[listlen-drvlen-1] = 0;
    else if (!strcmp(curAudioDriver, driver)) /* only one entry (head&tail) */
        curAudioDriver[0] = 0;
    else
        WINE_FIXME("driver '%s' is not in the list, please report!\n", driver);
195 196
}

197 198
static void initAudioDeviceTree(HWND hDlg)
{
199
    AUDIO_DRIVER *pAudioDrv = NULL;
200
    int i, j;
201
    TVINSERTSTRUCTW insert;
202 203
    HTREEITEM root, driver[10];
    HWND tree = NULL;
204 205
    HIMAGELIST hImageList;
    HBITMAP hBitMap;
206
    HCURSOR old_cursor;
207
    WCHAR driver_type[64], dev_type[64];
208 209 210 211 212 213

    tree = GetDlgItem(hDlg, IDC_AUDIO_TREE);

    if (!tree)
        return;

214
    /* set tree view style */
215 216
    SetWindowLong(tree, GWL_STYLE, GetWindowLong(tree, GWL_STYLE) | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);

217 218 219 220 221
    /* state checkbox */
    hImageList = ImageList_Create(16, 16, FALSE, 3, 0);
    hBitMap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_CHECKBOX));
    ImageList_Add(hImageList, hBitMap, NULL);
    DeleteObject(hBitMap);
222
    SendMessageW( tree, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)hImageList );
223 224

    /* root item */
225 226
    LoadStringW (GetModuleHandle (NULL), IDS_SOUNDDRIVERS, driver_type,
        sizeof(driver_type)/sizeof(driver_type[0]));
227 228
    insert.hParent = TVI_ROOT;
    insert.hInsertAfter = TVI_LAST;
229
    insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
230
    insert.u.item.pszText = driver_type;
231
    insert.u.item.cChildren = 1;
232
    root = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
233

234 235 236 237 238
    /* change to the wait cursor because this can take a while if there is a
     * misbehaving driver that takes a long time to open
     */
    old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));

239
    /* iterate over list of loaded drivers */
240
    for (pAudioDrv = loadedAudioDrv, i = 0; pAudioDrv->nameID; i++, pAudioDrv++) {
241 242
        HDRVR hdrv;
        char name[MAX_PATH];
243
        WCHAR text[MAX_PATH];
244 245

        sprintf(name, "wine%s.drv", pAudioDrv->szDriver);
246 247
        LoadStringW (GetModuleHandle (NULL), pAudioDrv->nameID, text,
            sizeof(text)/sizeof(text[0]));
248

249
        if ((hdrv = pAudioDrv->hDriver))
250
        {
251 252
            HMODULE lib;
            if ((lib = GetDriverModuleHandle(hdrv)))
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
            {
                int num_wod = 0, num_wid = 0, num_mod = 0, num_mid = 0, num_aux = 0, num_mxd = 0;
                MessagePtr wodMessagePtr = (MessagePtr)GetProcAddress(lib, "wodMessage");
                MessagePtr widMessagePtr = (MessagePtr)GetProcAddress(lib, "widMessage");
                MessagePtr modMessagePtr = (MessagePtr)GetProcAddress(lib, "modMessage");
                MessagePtr midMessagePtr = (MessagePtr)GetProcAddress(lib, "midMessage");
                MessagePtr auxMessagePtr = (MessagePtr)GetProcAddress(lib, "auxMessage");
                MessagePtr mxdMessagePtr = (MessagePtr)GetProcAddress(lib, "mxdMessage");

                if (wodMessagePtr)
                    num_wod = wodMessagePtr(0, WODM_GETNUMDEVS, 0, 0, 0);

                if (widMessagePtr)
                    num_wid = widMessagePtr(0, WIDM_GETNUMDEVS, 0, 0, 0);

                if (modMessagePtr)
                    num_mod = modMessagePtr(0, MODM_GETNUMDEVS, 0, 0, 0);

                if (midMessagePtr)
                    num_mid = midMessagePtr(0, MIDM_GETNUMDEVS, 0, 0, 0);

                if (auxMessagePtr)
                    num_aux = auxMessagePtr(0, AUXDM_GETNUMDEVS, 0, 0, 0);

                if (mxdMessagePtr)
                    num_mxd = mxdMessagePtr(0, MXDM_GETNUMDEVS, 0, 0, 0);

                if (num_wod == 0 && num_wid == 0 && num_mod == 0 && num_mid == 0 && num_aux == 0 && num_mxd == 0)
                {
                    insert.hParent = root;
283
                    insert.u.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;
284
                    insert.u.item.pszText = text;
285 286
                    insert.u.item.stateMask = TVIS_STATEIMAGEMASK;
                    insert.u.item.lParam =  i + DRIVER_MASK;
287 288 289 290
                    if (isDriverSet(pAudioDrv->szDriver))
                        insert.u.item.state = INDEXTOSTATEIMAGEMASK(2);
                    else
                        insert.u.item.state = INDEXTOSTATEIMAGEMASK(1);
291

292
                    driver[i] = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
293 294 295 296 297 298
                }
                else
                {
                    HTREEITEM type;

                    insert.hParent = root;
299
                    insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_STATE | TVIF_PARAM;
300 301
                    insert.u.item.pszText = text;
                    insert.u.item.cChildren = 1;
302 303 304 305 306 307 308
                    insert.u.item.stateMask = TVIS_STATEIMAGEMASK;
                    insert.u.item.lParam =  i + DRIVER_MASK;

                    if (isDriverSet(pAudioDrv->szDriver))
                        insert.u.item.state = INDEXTOSTATEIMAGEMASK(2);
                    else
                        insert.u.item.state = INDEXTOSTATEIMAGEMASK(1);
309

310
                    driver[i] = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
311 312 313

                    if (num_wod)
                    {
314 315
                        LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_WAVEOUT, dev_type,
                            sizeof(dev_type)/sizeof(dev_type[0]));
316

317
                        insert.hParent = driver[i];
318
                        insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
319
                        insert.u.item.pszText = dev_type;
320
                        insert.u.item.cChildren = 1;
321

322
                        type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
323 324 325 326 327

                        for (j = 0; j < num_wod; j++)
                        {
                            WAVEOUTCAPSW caps;

328
                            wodMessagePtr(j, WODM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
329 330

                            insert.hParent = type;
331
                            insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
332
                            insert.u.item.pszText = caps.szPname;
333
                            insert.u.item.lParam = j + DEVICE_MASK;
334

335
                            SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
336 337 338 339 340
                        }
                    }

                    if (num_wid)
                    {
341 342
                        LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_WAVEIN, dev_type,
                            sizeof(dev_type)/sizeof(dev_type[0]));
343

344
                        insert.hParent = driver[i];
345
                        insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
346
                        insert.u.item.pszText = dev_type;
347
                        insert.u.item.cChildren = 1;
348

349
                        type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
350 351 352 353 354

                        for (j = 0; j < num_wid; j++)
                        {
                            WAVEINCAPSW caps;

355
                            widMessagePtr(j, WIDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
356 357

                            insert.hParent = type;
358
                            insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
359
                            insert.u.item.pszText = caps.szPname;
360
                            insert.u.item.lParam = j + DEVICE_MASK;
361

362
                            SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
363 364 365 366 367
                        }
                    }

                    if (num_mod)
                    {
368 369
                        LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIDIOUT, dev_type,
                            sizeof(dev_type)/sizeof(dev_type[0]));
370

371
                        insert.hParent = driver[i];
372
                        insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
373
                        insert.u.item.pszText = dev_type;
374
                        insert.u.item.cChildren = 1;
375

376
                        type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
377 378 379 380 381

                        for (j = 0; j < num_mod; j++)
                        {
                            MIDIOUTCAPSW caps;

382
                            modMessagePtr(j, MODM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
383 384

                            insert.hParent = type;
385
                            insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
386
                            insert.u.item.pszText = caps.szPname;
387
                            insert.u.item.lParam = j + DEVICE_MASK;
388

389
                            SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
390 391 392 393 394
                        }
                    }

                    if (num_mid)
                    {
395 396
                        LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIDIIN, dev_type,
                            sizeof(dev_type)/sizeof(dev_type[0]));
397

398
                        insert.hParent = driver[i];
399
                        insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
400
                        insert.u.item.pszText = dev_type;
401
                        insert.u.item.cChildren = 1;
402

403
                        type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
404 405 406 407 408

                        for (j = 0; j < num_mid; j++)
                        {
                            MIDIINCAPSW caps;

409
                            midMessagePtr(j, MIDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
410 411

                            insert.hParent = type;
412
                            insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
413
                            insert.u.item.pszText = caps.szPname;
414
                            insert.u.item.lParam = j + DEVICE_MASK;
415

416
                            SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
417 418 419 420 421
                        }
                    }

                    if (num_aux)
                    {
422 423
                        LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_AUX, dev_type,
                            sizeof(dev_type)/sizeof(dev_type[0]));
424

425
                        insert.hParent = driver[i];
426
                        insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
427
                        insert.u.item.pszText = dev_type;
428
                        insert.u.item.cChildren = 1;
429

430
                        type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
431 432 433 434 435

                        for (j = 0; j < num_aux; j++)
                        {
                            AUXCAPSW caps;

436
                            auxMessagePtr(j, AUXDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
437 438

                            insert.hParent = type;
439
                            insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
440
                            insert.u.item.pszText = caps.szPname;
441
                            insert.u.item.lParam = j + DEVICE_MASK;
442

443
                            SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
444 445 446 447 448
                        }
                    }

                    if (num_mxd)
                    {
449 450
                        LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIXER, dev_type,
                            sizeof(dev_type)/sizeof(dev_type[0]));
451

452
                        insert.hParent = driver[i];
453
                        insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
454
                        insert.u.item.pszText = dev_type;
455
                        insert.u.item.cChildren = 1;
456

457
                        type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
458 459 460 461 462

                        for (j = 0; j < num_mxd; j++)
                        {
                            MIXERCAPSW caps;

463
                            mxdMessagePtr(j, MXDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
464 465

                            insert.hParent = type;
466
                            insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
467
                            insert.u.item.pszText = caps.szPname;
468
                            insert.u.item.lParam = j + DEVICE_MASK;
469

470
                            SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
471 472 473 474 475 476 477
                        }
                    }
                }
            }
        }
    }

478 479 480
    /* restore the original cursor */
    SetCursor(old_cursor);

481 482 483 484 485 486
    SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_SELECTITEM, 0, 0);
    SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)root);
    for (j = 0; j < i; j++)
        SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)driver[j]);
}

487 488
/* find all drivers that can be loaded */
static void findAudioDrivers(void)
489
{
490
    int numFound = 0;
491
    AUDIO_DRIVER *pAudioDrv = NULL;
492
    HCURSOR old_cursor;
493 494

    /* delete an existing list */
495 496
    HeapFree(GetProcessHeap(), 0, loadedAudioDrv);
    loadedAudioDrv = 0;
497

498 499 500 501 502
    /* change to the wait cursor because this can take a while if there is a
     * misbehaving driver that takes a long time to open
     */
    old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));

503
    for (pAudioDrv = sAudioDrivers; pAudioDrv->nameID; pAudioDrv++)
504 505 506 507 508 509 510 511
    {
        if (strlen(pAudioDrv->szDriver))
        {
            HDRVR hdrv;
            char driver[MAX_PATH];

            sprintf(driver, "wine%s.drv", pAudioDrv->szDriver);

512 513 514 515 516 517 518 519 520 521 522
            hdrv = pAudioDrv->hDriver;
            if (!pAudioDrv->hDriver && (hdrv = OpenDriverA(driver, 0, 0))) {
                HMODULE lib = GetDriverModuleHandle(hdrv);
                MessagePtr wodMessagePtr = (MessagePtr)GetProcAddress(lib, "wodMessage");
                MessagePtr widMessagePtr = (MessagePtr)GetProcAddress(lib, "widMessage");
                MessagePtr modMessagePtr = (MessagePtr)GetProcAddress(lib, "modMessage");
                MessagePtr midMessagePtr = (MessagePtr)GetProcAddress(lib, "midMessage");
                MessagePtr auxMessagePtr = (MessagePtr)GetProcAddress(lib, "auxMessage");
                MessagePtr mxdMessagePtr = (MessagePtr)GetProcAddress(lib, "mxdMessage");

                pAudioDrv->hDriver = hdrv;
523

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
                if (wodMessagePtr)
                    wodMessagePtr(0, DRVM_INIT, 0, 0, 0);

                if (widMessagePtr)
                    widMessagePtr(0, DRVM_INIT, 0, 0, 0);

                if (modMessagePtr)
                    modMessagePtr(0, DRVM_INIT, 0, 0, 0);

                if (midMessagePtr)
                    midMessagePtr(0, DRVM_INIT, 0, 0, 0);

                if (auxMessagePtr)
                    auxMessagePtr(0, DRVM_INIT, 0, 0, 0);

                if (mxdMessagePtr)
                    mxdMessagePtr(0, DRVM_INIT, 0, 0, 0);
            }
            if (hdrv)
            {
544 545 546 547 548 549 550 551 552 553 554
                if (loadedAudioDrv)
                    loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
                else
                    loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));

                CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
                numFound++;
            }
        }
    }

555 556 557
    /* restore the original cursor */
    SetCursor(old_cursor);

558
    /* terminate list with empty driver */
559 560 561
    if (numFound) {
        loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
        CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
562 563 564 565
    } else {
        loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));
        CopyMemory(&loadedAudioDrv[0], pAudioDrv, sizeof(AUDIO_DRIVER));
    }
566 567 568 569 570 571
}

/* check local copy of registry string for unloadable drivers */
static void checkRegistrySetting(HWND hDlg)
{
    const AUDIO_DRIVER *pAudioDrv;
572 573 574 575
    char * token, * tokens;

    tokens = HeapAlloc(GetProcessHeap(), 0, strlen(curAudioDriver)+1);
    strcpy(tokens, curAudioDriver);
576 577 578 579 580 581

start_over:
    token = strtok(tokens, ",");
    while (token != NULL)
    {
        BOOL found = FALSE;
582
        for (pAudioDrv = loadedAudioDrv; pAudioDrv->nameID; pAudioDrv++)
583 584 585 586 587 588 589 590 591
        {
            if (strcmp(token, pAudioDrv->szDriver) == 0)
            {
                found = TRUE;
                break;
            }
        }
        if (found == FALSE)
        {
592 593 594 595 596 597 598 599 600 601 602 603 604
            WCHAR tokenW[MAX_NAME_LENGTH+1];
            WCHAR messageStr[256];
            WCHAR str[1024];
            WCHAR caption[64];
            
            MultiByteToWideChar (CP_ACP, 0, token, -1, tokenW, sizeof(tokenW)/sizeof(tokenW[0]));
            
            LoadStringW (GetModuleHandle (NULL), IDS_UNAVAILABLE_DRIVER, messageStr,
                sizeof(messageStr)/sizeof(messageStr[0]));
            wsprintfW (str, messageStr, tokenW);
            LoadStringW (GetModuleHandle (NULL), IDS_WARNING, caption,
                sizeof(caption)/sizeof(caption[0]));
            if (MessageBoxW (hDlg, str, caption, MB_ICONWARNING | MB_YESNOCANCEL) == IDYES)
605 606 607 608 609 610 611 612
            {
                removeDriver(token);
                strcpy(tokens, curAudioDriver);
                goto start_over;
            }
        }
        token = strtok(NULL, ",");
    }
613
    HeapFree(GetProcessHeap(), 0, tokens);
614 615
}

616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
static void selectDriver(HWND hDlg, const char * driver)
{
    WCHAR text[1024];
    WCHAR caption[64];

    strcpy(curAudioDriver, driver);
    set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);

    if (LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_MISSING, text, sizeof(text)/sizeof(text[0])))
    {
        if (LoadStringW(GetModuleHandle(NULL), IDS_WINECFG_TITLE, caption, sizeof(caption)/sizeof(caption[0])))
            MessageBoxW(hDlg, text, caption, MB_OK | MB_ICONINFORMATION);
    }
   
    SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
}

633 634
static void initAudioDlg (HWND hDlg)
{
635
    int i, j, found;
636
    char* buf = NULL;
637

638 639
    WINE_TRACE("\n");

640 641 642
    /* make a list of all drivers that can be loaded */
    findAudioDrivers();

643 644 645 646
    /* get current registry setting if available */
    buf = get_reg_key(config_key, "Drivers", "Audio", NULL);

    /* check for first time install and set a default driver
647
     * select first available driver, and if that fails: none
648 649 650
     */
    if (buf == NULL)
    {
651 652 653
        /* select first available driver */
        if (*loadedAudioDrv->szDriver)
            selectDriver(hDlg, loadedAudioDrv->szDriver);
654 655 656 657 658 659
    }
    else /* make a local copy of the current registry setting */
        strcpy(curAudioDriver, buf);

    WINE_TRACE("curAudioDriver = %s\n", curAudioDriver);

660 661
    /* check for drivers that can't be loaded */
    checkRegistrySetting(hDlg);
662

663
    initAudioDeviceTree(hDlg);
664 665

    SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_RESETCONTENT, 0, 0);
666 667 668
    buf = get_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration", "Full");

    j = found = 0;
669 670
    for (i = 0; 0 != DSound_HW_Accels[i].displayID; ++i) {
      WCHAR accelStr[64];
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687
      int match;

      match = (strcmp(buf, DSound_HW_Accels[i].settingStr) == 0);
      if (match)
      {
        DSound_HW_Accels[i].visible = 1;
        found = 1;
      }

      if (DSound_HW_Accels[i].visible)
      {
        LoadStringW (GetModuleHandle (NULL), DSound_HW_Accels[i].displayID,
                     accelStr, sizeof(accelStr)/sizeof(accelStr[0]));
        SendDlgItemMessageW (hDlg, IDC_DSOUND_HW_ACCEL, CB_ADDSTRING, 0, (LPARAM)accelStr);
        if (match)
          SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_SETCURSEL, j, 0);
        j++;
688 689
      }
    }
690
    if (!found) {
691 692 693 694
      WINE_ERR("Invalid Direct Sound HW Accel read from registry (%s)\n", buf);
    }
    HeapFree(GetProcessHeap(), 0, buf);

695 696 697 698
    SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_RESETCONTENT, 0, 0);
    for (i = 0; NULL != DSound_Rates[i]; ++i) {
      SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_ADDSTRING, 0, (LPARAM) DSound_Rates[i]);
    }
699
    buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", "44100");
700 701 702 703 704 705 706 707 708 709 710
    for (i = 0; NULL != DSound_Rates[i]; ++i) {
      if (strcmp(buf, DSound_Rates[i]) == 0) {
	SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_SETCURSEL, i, 0);
	break ;
      }
    }

    SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_RESETCONTENT, 0, 0);
    for (i = 0; NULL != DSound_Bits[i]; ++i) {
      SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_ADDSTRING, 0, (LPARAM) DSound_Bits[i]);
    }
711
    buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", "16");
712 713 714 715 716 717
    for (i = 0; NULL != DSound_Bits[i]; ++i) {
      if (strcmp(buf, DSound_Bits[i]) == 0) {
	SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_SETCURSEL, i, 0);
	break ;
      }
    }
718
    HeapFree(GetProcessHeap(), 0, buf);
719 720 721 722 723 724 725 726
}

INT_PTR CALLBACK
AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uMsg) {
      case WM_COMMAND:
	switch (LOWORD(wParam)) {
727
          case IDC_AUDIO_CONFIGURE:
728
	     configureAudioDriver(hDlg);
729
	     break;
730
          case IDC_AUDIO_TEST:
731 732
	     if(!PlaySound(MAKEINTRESOURCE(IDW_TESTSOUND), NULL, SND_RESOURCE | SND_SYNC))
                MessageBox(NULL, "Audio test failed!", "Error", MB_OK | MB_ICONERROR);
733
             break;
734 735 736
          case IDC_AUDIO_CONTROL_PANEL:
	     MessageBox(NULL, "Launching audio control panel not implemented yet!", "Fixme", MB_OK | MB_ICONERROR);
             break;
737 738 739
          case IDC_DSOUND_HW_ACCEL:
	    if (HIWORD(wParam) == CBN_SELCHANGE) {
	      int selected_dsound_accel;
740 741
              int i, j = 0;

742 743
	      SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
	      selected_dsound_accel = SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_GETCURSEL, 0, 0);
744 745 746 747 748 749 750 751 752 753 754 755 756
              for (i = 0; DSound_HW_Accels[i].settingStr; ++i)
              {
                if (DSound_HW_Accels[i].visible)
                {
                  if (j == selected_dsound_accel)
                  {
                    set_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration",
                      DSound_HW_Accels[i].settingStr);
                    break;
                  }
                  j++;
                }
              }
757 758
	    }
	    break;
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
          case IDC_DSOUND_RATES:
	    if (HIWORD(wParam) == CBN_SELCHANGE) {
	      int selected_dsound_rate;
	      SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
	      selected_dsound_rate = SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_GETCURSEL, 0, 0);
	      set_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", DSound_Rates[selected_dsound_rate]);
	    }
	    break;
          case IDC_DSOUND_BITS:
	    if (HIWORD(wParam) == CBN_SELCHANGE) {
	      int selected_dsound_bits;
	      SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
	      selected_dsound_bits = SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_GETCURSEL, 0, 0);
	      set_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", DSound_Bits[selected_dsound_bits]);
	    }
	    break;
775 776
	}
	break;
777 778 779 780

      case WM_SHOWWINDOW:
        set_window_title(hDlg);
        break;
781

782 783 784
      case WM_NOTIFY:
	switch(((LPNMHDR)lParam)->code) {
	    case PSN_KILLACTIVE:
785
	      SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
786 787
	      break;
	    case PSN_APPLY:
788
	      set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);
789
              apply();
790
	      SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
791 792 793
	      break;
	    case PSN_SETACTIVE:
	      break;
794 795 796 797 798 799 800
            case NM_CLICK:
              if (((LPNMHDR)lParam)->idFrom == IDC_AUDIO_TREE)
              {
                  TVHITTESTINFO ht;
                  DWORD dwPos = GetMessagePos();
                  HWND tree = ((LPNMHDR)lParam)->hwndFrom;
                  ZeroMemory(&ht, sizeof(ht));
801 802
                  ht.pt.x = (short)LOWORD(dwPos);
                  ht.pt.y = (short)HIWORD(dwPos);
803
                  MapWindowPoints(HWND_DESKTOP, tree, &ht.pt, 1);
804
                  SendMessageW( tree, TVM_HITTEST, 0, (LPARAM)&ht );
805 806 807 808 809 810
                  if (TVHT_ONITEMSTATEICON & ht.flags)
                  {
                      TVITEM tvItem;
                      int index;
                      ZeroMemory(&tvItem, sizeof(tvItem));
                      tvItem.hItem = ht.hItem;
811
                      SendMessageW( tree, TVM_GETITEMW, 0, (LPARAM) &tvItem );
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836

                      index = TreeView_GetItemState(tree, ht.hItem, TVIS_STATEIMAGEMASK);
                      if (index == INDEXTOSTATEIMAGEMASK(1))
                      {
                          TreeView_SetItemState(tree, ht.hItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK);
                          addDriver(loadedAudioDrv[tvItem.lParam & 0xff].szDriver);
                          SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
                      }
                      else if (index == INDEXTOSTATEIMAGEMASK(2))
                      {
                          TreeView_SetItemState(tree, ht.hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK);
                          removeDriver(loadedAudioDrv[tvItem.lParam & 0xff].szDriver);
                          SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
                      }
                  }
              }
              break;
            case NM_RCLICK:
              if (((LPNMHDR)lParam)->idFrom == IDC_AUDIO_TREE)
              {
                  TVHITTESTINFO ht;
                  DWORD dwPos = GetMessagePos();
                  HWND tree = ((LPNMHDR)lParam)->hwndFrom;
                  POINT pt;
                  ZeroMemory(&ht, sizeof(ht));
837 838
                  pt.x = (short)LOWORD(dwPos);
                  pt.y = (short)HIWORD(dwPos);
839 840
                  ht.pt = pt;
                  MapWindowPoints(HWND_DESKTOP, tree, &ht.pt, 1);
841
                  SendMessageW( tree, TVM_HITTEST, 0, (LPARAM)&ht );
842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
                  if (TVHT_ONITEMLABEL & ht.flags)
                  {
                      TVITEM tvItem;
                      ZeroMemory(&tvItem, sizeof(tvItem));
                      tvItem.hItem = ht.hItem;
                      tvItem.mask = TVIF_PARAM;
                      tvItem.lParam = -1;
                      if (TreeView_GetItem(tree, &tvItem))
                      {
                          if (tvItem.lParam & DRIVER_MASK)
                          {
                              if (hPopupMenus)
                              {
                                  TrackPopupMenu(GetSubMenu(hPopupMenus, 0), TPM_RIGHTBUTTON, pt.x, pt.y, 0, tree, NULL);
                                  toConfigure = tvItem.lParam & ~DRIVER_MASK;
                              }
                          }
                          else if (tvItem.lParam & DEVICE_MASK)
                          {
                              /* FIXME TBD */
                          }

                      }
                  }
              }
867 868 869 870 871 872 873 874 875 876
	}
	break;

  case WM_INITDIALOG:
    initAudioDlg(hDlg);
    break;
  }

  return FALSE;
}