winecfg: Hide basic and normal acceleration options by default.
[wine] / programs / winecfg / audio.c
1 /*
2  * Audio management UI code
3  *
4  * Copyright 2004 Chris Morgan
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #define WIN32_LEAN_AND_MEAN
23 #define NONAMELESSSTRUCT
24 #define NONAMELESSUNION
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <assert.h>
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <string.h>
33
34 #include <windows.h>
35 #include <wine/debug.h>
36 #include <shellapi.h>
37 #include <objbase.h>
38 #include <shlguid.h>
39 #include <shlwapi.h>
40 #include <shlobj.h>
41 #include <mmsystem.h>
42 #include <mmreg.h>
43 #include <mmddk.h>
44
45 #include "winecfg.h"
46 #include "resource.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
49
50 #define DRIVER_MASK 0x80000000
51 #define DEVICE_MASK 0x40000000
52 #define MAX_NAME_LENGTH 64
53
54 typedef DWORD (WINAPI * MessagePtr)(UINT, UINT, DWORD, DWORD, DWORD);
55
56 static struct DSOUNDACCEL
57 {
58   UINT displayID;
59   UINT visible;
60   const char* settingStr;
61 } DSound_HW_Accels[] = {
62   {IDS_ACCEL_FULL,      1, "Full"},
63   {IDS_ACCEL_STANDARD,  0, "Standard"},
64   {IDS_ACCEL_BASIC,     0, "Basic"},
65   {IDS_ACCEL_EMULATION, 1, "Emulation"},
66   {0, 0, 0}
67 };
68
69 static const char* DSound_Rates[] = {
70   "48000",
71   "44100",
72   "22050",
73   "16000",
74   "11025",
75   "8000",
76   NULL
77 };
78
79 static const char* DSound_Bits[] = {
80   "8",
81   "16",
82   NULL
83 };
84
85 typedef struct
86 {
87   UINT nameID;
88   const char *szDriver;
89 } AUDIO_DRIVER;
90
91 static const AUDIO_DRIVER sAudioDrivers[] = {
92   {IDS_DRIVER_ALSA,      "alsa"},
93   {IDS_DRIVER_OSS,       "oss"},
94   {IDS_DRIVER_COREAUDIO, "coreaudio"},
95   {IDS_DRIVER_JACK,      "jack"},
96   {IDS_DRIVER_NAS,       "nas"},
97   {IDS_DRIVER_ESOUND,    "esd"},
98   {IDS_DRIVER_AUDIOIO,   "audioio"},
99   {0, ""}
100 };
101
102 /* list of available drivers */
103 static AUDIO_DRIVER * loadedAudioDrv;
104
105 /* local copy of registry setting */
106 static char curAudioDriver[1024];
107
108 /* driver index to configure */
109 static int toConfigure;
110
111 /* display a driver specific configuration dialog */
112 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 = OpenDriverA(wine_driver, 0, 0);
122         if (hdrvr != 0)
123         {
124             if (SendDriverMessage(hdrvr, DRV_QUERYCONFIGURE, 0, 0) != 0)
125             {
126                 DRVCONFIGINFO dci;
127                 LONG lRes;
128                 dci.dwDCISize = sizeof (dci);
129                 dci.lpszDCISectionName = NULL;
130                 dci.lpszDCIAliasName = NULL;
131                 lRes = SendDriverMessage(hdrvr, DRV_CONFIGURE, 0, (LONG_PTR)&dci);
132             }
133             CloseDriver(hdrvr, 0, 0);
134         }
135         else
136         {
137             WCHAR wine_driverW[MAX_NAME_LENGTH+9];
138             WCHAR messageStr[256];
139             WCHAR str[1024];
140             
141             MultiByteToWideChar (CP_ACP, 0, wine_driver, -1, wine_driverW, 
142                 sizeof (wine_driverW)/sizeof(wine_driverW[0])); 
143             
144             LoadStringW (GetModuleHandle (NULL), IDS_OPEN_DRIVER_ERROR, messageStr,
145                 sizeof(messageStr)/sizeof(messageStr[0]));
146             wsprintfW (str, messageStr, wine_driverW);
147             MessageBoxW (hDlg, str, NULL, MB_OK | MB_ICONERROR);
148         }
149     }
150 }
151
152 /* is driver in local copy of driver registry string */
153 static BOOL isDriverSet(const char * driver)
154 {
155     WINE_TRACE("driver = %s, curAudioDriver = %s\n", driver, curAudioDriver);
156
157     if (strstr(curAudioDriver, driver))
158         return TRUE;
159
160     return FALSE;
161 }
162
163 /* add driver to local copy of driver registry string */
164 static void addDriver(const char * driver)
165 {
166     if (!isDriverSet(driver))
167     {
168         if (strlen(curAudioDriver))
169             strcat(curAudioDriver, ",");
170         strcat(curAudioDriver, driver);
171     }
172 }
173
174 /* remove driver from local copy of driver registry string */
175 static void removeDriver(const char * driver)
176 {
177     char pattern[32], *p;
178     int drvlen, listlen;
179
180     strcpy(pattern, ",");
181     strcat(pattern, driver);
182     strcat(pattern, ",");
183     drvlen = strlen(driver);
184     listlen = strlen(curAudioDriver);
185
186     p = strstr(curAudioDriver, pattern);
187     if (p) /* somewhere in the middle */
188         memmove(p, p+drvlen+1, strlen(p+drvlen+1)+1);
189     else if (!strncmp(curAudioDriver, pattern+1, drvlen+1)) /* the head */
190         memmove(curAudioDriver, curAudioDriver+drvlen+1, listlen-drvlen);
191     else if (!strncmp(curAudioDriver+listlen-drvlen-1, pattern, drvlen+1)) /* the tail */
192         curAudioDriver[listlen-drvlen-1] = 0;
193     else if (!strcmp(curAudioDriver, driver)) /* only one entry (head&tail) */
194         curAudioDriver[0] = 0;
195     else
196         WINE_FIXME("driver '%s' is not in the list, please report!\n", driver);
197 }
198
199 static void initAudioDeviceTree(HWND hDlg)
200 {
201     const AUDIO_DRIVER *pAudioDrv = NULL;
202     int i, j;
203     TVINSERTSTRUCTW insert;
204     HTREEITEM root, driver[10];
205     HWND tree = NULL;
206     HIMAGELIST hImageList;
207     HBITMAP hBitMap;
208     HCURSOR old_cursor;
209     WCHAR driver_type[64], dev_type[64];
210
211     tree = GetDlgItem(hDlg, IDC_AUDIO_TREE);
212
213     if (!tree)
214         return;
215
216     /* set tree view style */
217     SetWindowLong(tree, GWL_STYLE, GetWindowLong(tree, GWL_STYLE) | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);
218
219     /* state checkbox */
220     hImageList = ImageList_Create(16, 16, FALSE, 3, 0);
221     hBitMap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_CHECKBOX));
222     ImageList_Add(hImageList, hBitMap, NULL);
223     DeleteObject(hBitMap);
224     SendMessageW( tree, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)hImageList );
225
226     /* root item */
227     LoadStringW (GetModuleHandle (NULL), IDS_SOUNDDRIVERS, driver_type,
228         sizeof(driver_type)/sizeof(driver_type[0]));
229     insert.hParent = TVI_ROOT;
230     insert.hInsertAfter = TVI_LAST;
231     insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
232     insert.u.item.pszText = driver_type;
233     insert.u.item.cChildren = 1;
234     root = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
235
236     /* change to the wait cursor because this can take a while if there is a
237      * misbehaving driver that takes a long time to open
238      */
239     old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
240
241     /* iterate over list of loaded drivers */
242     for (pAudioDrv = loadedAudioDrv, i = 0; pAudioDrv->nameID; i++, pAudioDrv++) {
243         HDRVR hdrv;
244         char name[MAX_PATH];
245         WCHAR text[MAX_PATH];
246
247         sprintf(name, "wine%s.drv", pAudioDrv->szDriver);
248         LoadStringW (GetModuleHandle (NULL), pAudioDrv->nameID, text,
249             sizeof(text)/sizeof(text[0]));
250
251         if ((hdrv = OpenDriverA(name, 0, 0)))
252         {
253             HMODULE lib;
254             if ((lib = GetDriverModuleHandle(hdrv)))
255             {
256                 int num_wod = 0, num_wid = 0, num_mod = 0, num_mid = 0, num_aux = 0, num_mxd = 0;
257                 MessagePtr wodMessagePtr = (MessagePtr)GetProcAddress(lib, "wodMessage");
258                 MessagePtr widMessagePtr = (MessagePtr)GetProcAddress(lib, "widMessage");
259                 MessagePtr modMessagePtr = (MessagePtr)GetProcAddress(lib, "modMessage");
260                 MessagePtr midMessagePtr = (MessagePtr)GetProcAddress(lib, "midMessage");
261                 MessagePtr auxMessagePtr = (MessagePtr)GetProcAddress(lib, "auxMessage");
262                 MessagePtr mxdMessagePtr = (MessagePtr)GetProcAddress(lib, "mxdMessage");
263
264                 if (wodMessagePtr)
265                     num_wod = wodMessagePtr(0, WODM_GETNUMDEVS, 0, 0, 0);
266
267                 if (widMessagePtr)
268                     num_wid = widMessagePtr(0, WIDM_GETNUMDEVS, 0, 0, 0);
269
270                 if (modMessagePtr)
271                     num_mod = modMessagePtr(0, MODM_GETNUMDEVS, 0, 0, 0);
272
273                 if (midMessagePtr)
274                     num_mid = midMessagePtr(0, MIDM_GETNUMDEVS, 0, 0, 0);
275
276                 if (auxMessagePtr)
277                     num_aux = auxMessagePtr(0, AUXDM_GETNUMDEVS, 0, 0, 0);
278
279                 if (mxdMessagePtr)
280                     num_mxd = mxdMessagePtr(0, MXDM_GETNUMDEVS, 0, 0, 0);
281
282                 if (num_wod == 0 && num_wid == 0 && num_mod == 0 && num_mid == 0 && num_aux == 0 && num_mxd == 0)
283                 {
284                     insert.hParent = root;
285                     insert.u.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;
286                     insert.u.item.pszText = text;
287                     insert.u.item.stateMask = TVIS_STATEIMAGEMASK;
288                     insert.u.item.lParam =  i + DRIVER_MASK;
289                     if (isDriverSet(pAudioDrv->szDriver))
290                         insert.u.item.state = INDEXTOSTATEIMAGEMASK(2);
291                     else
292                         insert.u.item.state = INDEXTOSTATEIMAGEMASK(1);
293
294                     driver[i] = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
295                 }
296                 else
297                 {
298                     HTREEITEM type;
299
300                     insert.hParent = root;
301                     insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_STATE | TVIF_PARAM;
302                     insert.u.item.pszText = text;
303                     insert.u.item.cChildren = 1;
304                     insert.u.item.stateMask = TVIS_STATEIMAGEMASK;
305                     insert.u.item.lParam =  i + DRIVER_MASK;
306
307                     if (isDriverSet(pAudioDrv->szDriver))
308                         insert.u.item.state = INDEXTOSTATEIMAGEMASK(2);
309                     else
310                         insert.u.item.state = INDEXTOSTATEIMAGEMASK(1);
311
312                     driver[i] = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
313
314                     if (num_wod)
315                     {
316                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_WAVEOUT, dev_type,
317                             sizeof(dev_type)/sizeof(dev_type[0]));
318
319                         insert.hParent = driver[i];
320                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
321                         insert.u.item.pszText = dev_type;
322                         insert.u.item.cChildren = 1;
323
324                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
325
326                         for (j = 0; j < num_wod; j++)
327                         {
328                             WAVEOUTCAPSW caps;
329
330                             wodMessagePtr(j, WODM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
331
332                             insert.hParent = type;
333                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
334                             insert.u.item.pszText = caps.szPname;
335                             insert.u.item.lParam = j + DEVICE_MASK;
336
337                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
338                         }
339                     }
340
341                     if (num_wid)
342                     {
343                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_WAVEIN, dev_type,
344                             sizeof(dev_type)/sizeof(dev_type[0]));
345
346                         insert.hParent = driver[i];
347                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
348                         insert.u.item.pszText = dev_type;
349                         insert.u.item.cChildren = 1;
350
351                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
352
353                         for (j = 0; j < num_wid; j++)
354                         {
355                             WAVEINCAPSW caps;
356
357                             widMessagePtr(j, WIDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
358
359                             insert.hParent = type;
360                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
361                             insert.u.item.pszText = caps.szPname;
362                             insert.u.item.lParam = j + DEVICE_MASK;
363
364                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
365                         }
366                     }
367
368                     if (num_mod)
369                     {
370                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIDIOUT, dev_type,
371                             sizeof(dev_type)/sizeof(dev_type[0]));
372
373                         insert.hParent = driver[i];
374                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
375                         insert.u.item.pszText = dev_type;
376                         insert.u.item.cChildren = 1;
377
378                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
379
380                         for (j = 0; j < num_mod; j++)
381                         {
382                             MIDIOUTCAPSW caps;
383
384                             modMessagePtr(j, MODM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
385
386                             insert.hParent = type;
387                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
388                             insert.u.item.pszText = caps.szPname;
389                             insert.u.item.lParam = j + DEVICE_MASK;
390
391                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
392                         }
393                     }
394
395                     if (num_mid)
396                     {
397                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIDIIN, dev_type,
398                             sizeof(dev_type)/sizeof(dev_type[0]));
399
400                         insert.hParent = driver[i];
401                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
402                         insert.u.item.pszText = dev_type;
403                         insert.u.item.cChildren = 1;
404
405                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
406
407                         for (j = 0; j < num_mid; j++)
408                         {
409                             MIDIINCAPSW caps;
410
411                             midMessagePtr(j, MIDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
412
413                             insert.hParent = type;
414                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
415                             insert.u.item.pszText = caps.szPname;
416                             insert.u.item.lParam = j + DEVICE_MASK;
417
418                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
419                         }
420                     }
421
422                     if (num_aux)
423                     {
424                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_AUX, dev_type,
425                             sizeof(dev_type)/sizeof(dev_type[0]));
426
427                         insert.hParent = driver[i];
428                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
429                         insert.u.item.pszText = dev_type;
430                         insert.u.item.cChildren = 1;
431
432                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
433
434                         for (j = 0; j < num_aux; j++)
435                         {
436                             AUXCAPSW caps;
437
438                             auxMessagePtr(j, AUXDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
439
440                             insert.hParent = type;
441                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
442                             insert.u.item.pszText = caps.szPname;
443                             insert.u.item.lParam = j + DEVICE_MASK;
444
445                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
446                         }
447                     }
448
449                     if (num_mxd)
450                     {
451                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIXER, dev_type,
452                             sizeof(dev_type)/sizeof(dev_type[0]));
453
454                         insert.hParent = driver[i];
455                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
456                         insert.u.item.pszText = dev_type;
457                         insert.u.item.cChildren = 1;
458
459                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
460
461                         for (j = 0; j < num_mxd; j++)
462                         {
463                             MIXERCAPSW caps;
464
465                             mxdMessagePtr(j, MXDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
466
467                             insert.hParent = type;
468                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
469                             insert.u.item.pszText = caps.szPname;
470                             insert.u.item.lParam = j + DEVICE_MASK;
471
472                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
473                         }
474                     }
475                 }
476             }
477             CloseDriver(hdrv, 0, 0);
478         }
479     }
480
481     /* restore the original cursor */
482     SetCursor(old_cursor);
483
484     SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_SELECTITEM, 0, 0);
485     SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)root);
486     for (j = 0; j < i; j++)
487         SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)driver[j]);
488 }
489
490 /* find all drivers that can be loaded */
491 static void findAudioDrivers(void)
492 {
493     int numFound = 0;
494     const AUDIO_DRIVER *pAudioDrv = NULL;
495     HCURSOR old_cursor;
496
497     /* delete an existing list */
498     HeapFree(GetProcessHeap(), 0, loadedAudioDrv);
499     loadedAudioDrv = 0;
500
501     /* change to the wait cursor because this can take a while if there is a
502      * misbehaving driver that takes a long time to open
503      */
504     old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
505
506     for (pAudioDrv = sAudioDrivers; pAudioDrv->nameID; pAudioDrv++)
507     {
508         if (strlen(pAudioDrv->szDriver))
509         {
510             HDRVR hdrv;
511             char driver[MAX_PATH];
512
513             sprintf(driver, "wine%s.drv", pAudioDrv->szDriver);
514
515             if ((hdrv = OpenDriverA(driver, 0, 0)))
516             {
517                 CloseDriver(hdrv, 0, 0);
518
519                 if (loadedAudioDrv)
520                     loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
521                 else
522                     loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));
523
524                 CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
525                 numFound++;
526             }
527         }
528     }
529
530     /* restore the original cursor */
531     SetCursor(old_cursor);
532
533     /* terminate list with empty driver */
534     if (numFound) {
535         loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
536         CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
537     } else {
538         loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));
539         CopyMemory(&loadedAudioDrv[0], pAudioDrv, sizeof(AUDIO_DRIVER));
540     }
541 }
542
543 /* check local copy of registry string for unloadable drivers */
544 static void checkRegistrySetting(HWND hDlg)
545 {
546     const AUDIO_DRIVER *pAudioDrv;
547     char * token, * tokens;
548
549     tokens = HeapAlloc(GetProcessHeap(), 0, strlen(curAudioDriver)+1);
550     strcpy(tokens, curAudioDriver);
551
552 start_over:
553     token = strtok(tokens, ",");
554     while (token != NULL)
555     {
556         BOOL found = FALSE;
557         for (pAudioDrv = loadedAudioDrv; pAudioDrv->nameID; pAudioDrv++)
558         {
559             if (strcmp(token, pAudioDrv->szDriver) == 0)
560             {
561                 found = TRUE;
562                 break;
563             }
564         }
565         if (found == FALSE)
566         {
567             WCHAR tokenW[MAX_NAME_LENGTH+1];
568             WCHAR messageStr[256];
569             WCHAR str[1024];
570             WCHAR caption[64];
571             
572             MultiByteToWideChar (CP_ACP, 0, token, -1, tokenW, sizeof(tokenW)/sizeof(tokenW[0]));
573             
574             LoadStringW (GetModuleHandle (NULL), IDS_UNAVAILABLE_DRIVER, messageStr,
575                 sizeof(messageStr)/sizeof(messageStr[0]));
576             wsprintfW (str, messageStr, tokenW);
577             LoadStringW (GetModuleHandle (NULL), IDS_WARNING, caption,
578                 sizeof(caption)/sizeof(caption[0]));
579             if (MessageBoxW (hDlg, str, caption, MB_ICONWARNING | MB_YESNOCANCEL) == IDYES)
580             {
581                 removeDriver(token);
582                 strcpy(tokens, curAudioDriver);
583                 goto start_over;
584             }
585         }
586         token = strtok(NULL, ",");
587     }
588     HeapFree(GetProcessHeap(), 0, tokens);
589 }
590
591 static void selectDriver(HWND hDlg, const char * driver)
592 {
593     WCHAR text[1024];
594     WCHAR caption[64];
595
596     strcpy(curAudioDriver, driver);
597     set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);
598
599     if (LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_MISSING, text, sizeof(text)/sizeof(text[0])))
600     {
601         if (LoadStringW(GetModuleHandle(NULL), IDS_WINECFG_TITLE, caption, sizeof(caption)/sizeof(caption[0])))
602             MessageBoxW(hDlg, text, caption, MB_OK | MB_ICONINFORMATION);
603     }
604    
605     SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
606 }
607
608 static void initAudioDlg (HWND hDlg)
609 {
610     int i, j, found;
611     char* buf = NULL;
612
613     WINE_TRACE("\n");
614
615     /* make a list of all drivers that can be loaded */
616     findAudioDrivers();
617
618     /* get current registry setting if available */
619     buf = get_reg_key(config_key, "Drivers", "Audio", NULL);
620
621     /* check for first time install and set a default driver
622      * select first available driver, and if that fails: none
623      */
624     if (buf == NULL)
625     {
626         /* select first available driver */
627         if (*loadedAudioDrv->szDriver)
628             selectDriver(hDlg, loadedAudioDrv->szDriver);
629     }
630     else /* make a local copy of the current registry setting */
631         strcpy(curAudioDriver, buf);
632
633     WINE_TRACE("curAudioDriver = %s\n", curAudioDriver);
634
635     /* check for drivers that can't be loaded */
636     checkRegistrySetting(hDlg);
637
638     initAudioDeviceTree(hDlg);
639
640     SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_RESETCONTENT, 0, 0);
641     buf = get_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration", "Full");
642
643     j = found = 0;
644     for (i = 0; 0 != DSound_HW_Accels[i].displayID; ++i) {
645       WCHAR accelStr[64];
646       int match;
647
648       match = (strcmp(buf, DSound_HW_Accels[i].settingStr) == 0);
649       if (match)
650       {
651         DSound_HW_Accels[i].visible = 1;
652         found = 1;
653       }
654
655       if (DSound_HW_Accels[i].visible)
656       {
657         LoadStringW (GetModuleHandle (NULL), DSound_HW_Accels[i].displayID,
658                      accelStr, sizeof(accelStr)/sizeof(accelStr[0]));
659         SendDlgItemMessageW (hDlg, IDC_DSOUND_HW_ACCEL, CB_ADDSTRING, 0, (LPARAM)accelStr);
660         if (match)
661           SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_SETCURSEL, j, 0);
662         j++;
663       }
664     }
665     if (!found) {
666       WINE_ERR("Invalid Direct Sound HW Accel read from registry (%s)\n", buf);
667     }
668     HeapFree(GetProcessHeap(), 0, buf);
669
670     SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_RESETCONTENT, 0, 0);
671     for (i = 0; NULL != DSound_Rates[i]; ++i) {
672       SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_ADDSTRING, 0, (LPARAM) DSound_Rates[i]);
673     }
674     buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", "44100");
675     for (i = 0; NULL != DSound_Rates[i]; ++i) {
676       if (strcmp(buf, DSound_Rates[i]) == 0) {
677         SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_SETCURSEL, i, 0);
678         break ;
679       }
680     }
681
682     SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_RESETCONTENT, 0, 0);
683     for (i = 0; NULL != DSound_Bits[i]; ++i) {
684       SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_ADDSTRING, 0, (LPARAM) DSound_Bits[i]);
685     }
686     buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", "16");
687     for (i = 0; NULL != DSound_Bits[i]; ++i) {
688       if (strcmp(buf, DSound_Bits[i]) == 0) {
689         SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_SETCURSEL, i, 0);
690         break ;
691       }
692     }
693     HeapFree(GetProcessHeap(), 0, buf);
694 }
695
696 INT_PTR CALLBACK
697 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
698 {
699   switch (uMsg) {
700       case WM_COMMAND:
701         switch (LOWORD(wParam)) {
702           case IDC_AUDIO_CONFIGURE:
703              configureAudioDriver(hDlg);
704              break;
705           case IDC_AUDIO_TEST:
706              if(!PlaySound(MAKEINTRESOURCE(IDW_TESTSOUND), NULL, SND_RESOURCE | SND_SYNC))
707                 MessageBox(NULL, "Audio test failed!", "Error", MB_OK | MB_ICONERROR);
708              break;
709           case IDC_AUDIO_CONTROL_PANEL:
710              MessageBox(NULL, "Launching audio control panel not implemented yet!", "Fixme", MB_OK | MB_ICONERROR);
711              break;
712           case IDC_DSOUND_HW_ACCEL:
713             if (HIWORD(wParam) == CBN_SELCHANGE) {
714               int selected_dsound_accel;
715               int i, j = 0;
716
717               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
718               selected_dsound_accel = SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_GETCURSEL, 0, 0);
719               for (i = 0; DSound_HW_Accels[i].settingStr; ++i)
720               {
721                 if (DSound_HW_Accels[i].visible)
722                 {
723                   if (j == selected_dsound_accel)
724                   {
725                     set_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration",
726                       DSound_HW_Accels[i].settingStr);
727                     break;
728                   }
729                   j++;
730                 }
731               }
732             }
733             break;
734           case IDC_DSOUND_RATES:
735             if (HIWORD(wParam) == CBN_SELCHANGE) {
736               int selected_dsound_rate;
737               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
738               selected_dsound_rate = SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_GETCURSEL, 0, 0);
739               set_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", DSound_Rates[selected_dsound_rate]);
740             }
741             break;
742           case IDC_DSOUND_BITS:
743             if (HIWORD(wParam) == CBN_SELCHANGE) {
744               int selected_dsound_bits;
745               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
746               selected_dsound_bits = SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_GETCURSEL, 0, 0);
747               set_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", DSound_Bits[selected_dsound_bits]);
748             }
749             break;
750         }
751         break;
752
753       case WM_SHOWWINDOW:
754         set_window_title(hDlg);
755         break;
756
757       case WM_NOTIFY:
758         switch(((LPNMHDR)lParam)->code) {
759             case PSN_KILLACTIVE:
760               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
761               break;
762             case PSN_APPLY:
763               set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);
764               apply();
765               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
766               break;
767             case PSN_SETACTIVE:
768               break;
769             case NM_CLICK:
770               if (((LPNMHDR)lParam)->idFrom == IDC_AUDIO_TREE)
771               {
772                   TVHITTESTINFO ht;
773                   DWORD dwPos = GetMessagePos();
774                   HWND tree = ((LPNMHDR)lParam)->hwndFrom;
775                   ZeroMemory(&ht, sizeof(ht));
776                   ht.pt.x = (short)LOWORD(dwPos);
777                   ht.pt.y = (short)HIWORD(dwPos);
778                   MapWindowPoints(HWND_DESKTOP, tree, &ht.pt, 1);
779                   SendMessageW( tree, TVM_HITTEST, 0, (LPARAM)&ht );
780                   if (TVHT_ONITEMSTATEICON & ht.flags)
781                   {
782                       TVITEM tvItem;
783                       int index;
784                       ZeroMemory(&tvItem, sizeof(tvItem));
785                       tvItem.hItem = ht.hItem;
786                       SendMessageW( tree, TVM_GETITEMW, 0, (LPARAM) &tvItem );
787
788                       index = TreeView_GetItemState(tree, ht.hItem, TVIS_STATEIMAGEMASK);
789                       if (index == INDEXTOSTATEIMAGEMASK(1))
790                       {
791                           TreeView_SetItemState(tree, ht.hItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK);
792                           addDriver(loadedAudioDrv[tvItem.lParam & 0xff].szDriver);
793                           SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
794                       }
795                       else if (index == INDEXTOSTATEIMAGEMASK(2))
796                       {
797                           TreeView_SetItemState(tree, ht.hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK);
798                           removeDriver(loadedAudioDrv[tvItem.lParam & 0xff].szDriver);
799                           SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
800                       }
801                   }
802               }
803               break;
804             case NM_RCLICK:
805               if (((LPNMHDR)lParam)->idFrom == IDC_AUDIO_TREE)
806               {
807                   TVHITTESTINFO ht;
808                   DWORD dwPos = GetMessagePos();
809                   HWND tree = ((LPNMHDR)lParam)->hwndFrom;
810                   POINT pt;
811                   ZeroMemory(&ht, sizeof(ht));
812                   pt.x = (short)LOWORD(dwPos);
813                   pt.y = (short)HIWORD(dwPos);
814                   ht.pt = pt;
815                   MapWindowPoints(HWND_DESKTOP, tree, &ht.pt, 1);
816                   SendMessageW( tree, TVM_HITTEST, 0, (LPARAM)&ht );
817                   if (TVHT_ONITEMLABEL & ht.flags)
818                   {
819                       TVITEM tvItem;
820                       ZeroMemory(&tvItem, sizeof(tvItem));
821                       tvItem.hItem = ht.hItem;
822                       tvItem.mask = TVIF_PARAM;
823                       tvItem.lParam = -1;
824                       if (TreeView_GetItem(tree, &tvItem))
825                       {
826                           if (tvItem.lParam & DRIVER_MASK)
827                           {
828                               if (hPopupMenus)
829                               {
830                                   TrackPopupMenu(GetSubMenu(hPopupMenus, 0), TPM_RIGHTBUTTON, pt.x, pt.y, 0, tree, NULL);
831                                   toConfigure = tvItem.lParam & ~DRIVER_MASK;
832                               }
833                           }
834                           else if (tvItem.lParam & DEVICE_MASK)
835                           {
836                               /* FIXME TBD */
837                           }
838
839                       }
840                   }
841               }
842         }
843         break;
844
845   case WM_INITDIALOG:
846     initAudioDlg(hDlg);
847     break;
848   }
849
850   return FALSE;
851 }