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