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