msxml3: Fixed typo in create_bsc.
[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 before[32], after[32], * start;
177
178     strcpy(before, ",");
179     strcat(before, driver);
180     strcpy(after, driver);
181     strcat(after, ",");
182
183     if ((start = strstr(curAudioDriver, after)))
184     {
185         int len = strlen(after);
186         char * end = curAudioDriver + strlen(curAudioDriver);
187         int i, count = end - start + len;
188         for (i = 0; i < count; i++)
189         {
190             if (start + len >= end)
191                 *start = 0;
192             else
193                 *start = start[len];
194             start++;
195         }
196     }
197     else if ((start = strstr(curAudioDriver, before)))
198     {
199         int len = strlen(before);
200         char * end = curAudioDriver + strlen(curAudioDriver);
201         int i, count = end - start + len;
202         for (i = 0; i < count; i++)
203         {
204             if (start + len >= end)
205                 *start = 0;
206             else
207                 *start = start[len];
208             start++;
209         }
210     }
211     else if (strcmp(curAudioDriver, driver) == 0)
212     {
213         strcpy(curAudioDriver, "");
214     }
215 }
216
217 static void initAudioDeviceTree(HWND hDlg)
218 {
219     const AUDIO_DRIVER *pAudioDrv = NULL;
220     int i, j;
221     TVINSERTSTRUCTW insert;
222     HTREEITEM root, driver[10];
223     HWND tree = NULL;
224     HIMAGELIST hImageList;
225     HBITMAP hBitMap;
226     HCURSOR old_cursor;
227     WCHAR driver_type[64], dev_type[64];
228
229     tree = GetDlgItem(hDlg, IDC_AUDIO_TREE);
230
231     if (!tree)
232         return;
233
234     /* set tree view style */
235     SetWindowLong(tree, GWL_STYLE, GetWindowLong(tree, GWL_STYLE) | TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT);
236
237     /* state checkbox */
238     hImageList = ImageList_Create(16, 16, FALSE, 3, 0);
239     hBitMap = LoadBitmap(GetModuleHandle(NULL), MAKEINTRESOURCE(IDB_CHECKBOX));
240     ImageList_Add(hImageList, hBitMap, NULL);
241     DeleteObject(hBitMap);
242     SendMessageW( tree, TVM_SETIMAGELIST, TVSIL_STATE, (LPARAM)hImageList );
243
244     /* root item */
245     LoadStringW (GetModuleHandle (NULL), IDS_SOUNDDRIVERS, driver_type,
246         sizeof(driver_type)/sizeof(driver_type[0]));
247     insert.hParent = TVI_ROOT;
248     insert.hInsertAfter = TVI_LAST;
249     insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
250     insert.u.item.pszText = driver_type;
251     insert.u.item.cChildren = 1;
252     root = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
253
254     /* change to the wait cursor because this can take a while if there is a
255      * misbehaving driver that takes a long time to open
256      */
257     old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
258
259     /* iterate over list of loaded drivers */
260     for (pAudioDrv = loadedAudioDrv, i = 0; pAudioDrv->nameID; i++, pAudioDrv++) {
261         HDRVR hdrv;
262         char name[MAX_PATH];
263         WCHAR text[MAX_PATH];
264
265         sprintf(name, "wine%s.drv", pAudioDrv->szDriver);
266         LoadStringW (GetModuleHandle (NULL), pAudioDrv->nameID, text,
267             sizeof(text)/sizeof(text[0]));
268
269         if ((hdrv = OpenDriverA(name, 0, 0)))
270         {
271             HMODULE lib;
272             if ((lib = GetDriverModuleHandle(hdrv)))
273             {
274                 int num_wod = 0, num_wid = 0, num_mod = 0, num_mid = 0, num_aux = 0, num_mxd = 0;
275                 MessagePtr wodMessagePtr = (MessagePtr)GetProcAddress(lib, "wodMessage");
276                 MessagePtr widMessagePtr = (MessagePtr)GetProcAddress(lib, "widMessage");
277                 MessagePtr modMessagePtr = (MessagePtr)GetProcAddress(lib, "modMessage");
278                 MessagePtr midMessagePtr = (MessagePtr)GetProcAddress(lib, "midMessage");
279                 MessagePtr auxMessagePtr = (MessagePtr)GetProcAddress(lib, "auxMessage");
280                 MessagePtr mxdMessagePtr = (MessagePtr)GetProcAddress(lib, "mxdMessage");
281
282                 if (wodMessagePtr)
283                     num_wod = wodMessagePtr(0, WODM_GETNUMDEVS, 0, 0, 0);
284
285                 if (widMessagePtr)
286                     num_wid = widMessagePtr(0, WIDM_GETNUMDEVS, 0, 0, 0);
287
288                 if (modMessagePtr)
289                     num_mod = modMessagePtr(0, MODM_GETNUMDEVS, 0, 0, 0);
290
291                 if (midMessagePtr)
292                     num_mid = midMessagePtr(0, MIDM_GETNUMDEVS, 0, 0, 0);
293
294                 if (auxMessagePtr)
295                     num_aux = auxMessagePtr(0, AUXDM_GETNUMDEVS, 0, 0, 0);
296
297                 if (mxdMessagePtr)
298                     num_mxd = mxdMessagePtr(0, MXDM_GETNUMDEVS, 0, 0, 0);
299
300                 if (num_wod == 0 && num_wid == 0 && num_mod == 0 && num_mid == 0 && num_aux == 0 && num_mxd == 0)
301                 {
302                     insert.hParent = root;
303                     insert.u.item.mask = TVIF_TEXT | TVIF_STATE | TVIF_PARAM;
304                     insert.u.item.pszText = text;
305                     insert.u.item.stateMask = TVIS_STATEIMAGEMASK;
306                     insert.u.item.lParam =  i + DRIVER_MASK;
307
308                     driver[i] = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
309                 }
310                 else
311                 {
312                     HTREEITEM type;
313
314                     insert.hParent = root;
315                     insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_STATE | TVIF_PARAM;
316                     insert.u.item.pszText = text;
317                     insert.u.item.cChildren = 1;
318                     insert.u.item.stateMask = TVIS_STATEIMAGEMASK;
319                     insert.u.item.lParam =  i + DRIVER_MASK;
320
321                     if (isDriverSet(pAudioDrv->szDriver))
322                         insert.u.item.state = INDEXTOSTATEIMAGEMASK(2);
323                     else
324                         insert.u.item.state = INDEXTOSTATEIMAGEMASK(1);
325
326                     driver[i] = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
327
328                     if (num_wod)
329                     {
330                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_WAVEOUT, dev_type,
331                             sizeof(dev_type)/sizeof(dev_type[0]));
332
333                         insert.hParent = driver[i];
334                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
335                         insert.u.item.pszText = dev_type;
336                         insert.u.item.cChildren = 1;
337
338                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
339
340                         for (j = 0; j < num_wod; j++)
341                         {
342                             WAVEOUTCAPSW caps;
343
344                             wodMessagePtr(j, WODM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
345
346                             insert.hParent = type;
347                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
348                             insert.u.item.pszText = caps.szPname;
349                             insert.u.item.lParam = j + DEVICE_MASK;
350
351                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
352                         }
353                     }
354
355                     if (num_wid)
356                     {
357                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_WAVEIN, dev_type,
358                             sizeof(dev_type)/sizeof(dev_type[0]));
359
360                         insert.hParent = driver[i];
361                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
362                         insert.u.item.pszText = dev_type;
363                         insert.u.item.cChildren = 1;
364
365                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
366
367                         for (j = 0; j < num_wid; j++)
368                         {
369                             WAVEINCAPSW caps;
370
371                             widMessagePtr(j, WIDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
372
373                             insert.hParent = type;
374                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
375                             insert.u.item.pszText = caps.szPname;
376                             insert.u.item.lParam = j + DEVICE_MASK;
377
378                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
379                         }
380                     }
381
382                     if (num_mod)
383                     {
384                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIDIOUT, dev_type,
385                             sizeof(dev_type)/sizeof(dev_type[0]));
386
387                         insert.hParent = driver[i];
388                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
389                         insert.u.item.pszText = dev_type;
390                         insert.u.item.cChildren = 1;
391
392                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
393
394                         for (j = 0; j < num_mod; j++)
395                         {
396                             MIDIOUTCAPSW caps;
397
398                             modMessagePtr(j, MODM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
399
400                             insert.hParent = type;
401                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
402                             insert.u.item.pszText = caps.szPname;
403                             insert.u.item.lParam = j + DEVICE_MASK;
404
405                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
406                         }
407                     }
408
409                     if (num_mid)
410                     {
411                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIDIIN, dev_type,
412                             sizeof(dev_type)/sizeof(dev_type[0]));
413
414                         insert.hParent = driver[i];
415                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
416                         insert.u.item.pszText = dev_type;
417                         insert.u.item.cChildren = 1;
418
419                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
420
421                         for (j = 0; j < num_mid; j++)
422                         {
423                             MIDIINCAPSW caps;
424
425                             midMessagePtr(j, MIDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
426
427                             insert.hParent = type;
428                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
429                             insert.u.item.pszText = caps.szPname;
430                             insert.u.item.lParam = j + DEVICE_MASK;
431
432                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
433                         }
434                     }
435
436                     if (num_aux)
437                     {
438                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_AUX, dev_type,
439                             sizeof(dev_type)/sizeof(dev_type[0]));
440
441                         insert.hParent = driver[i];
442                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
443                         insert.u.item.pszText = dev_type;
444                         insert.u.item.cChildren = 1;
445
446                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
447
448                         for (j = 0; j < num_aux; j++)
449                         {
450                             AUXCAPSW caps;
451
452                             auxMessagePtr(j, AUXDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
453
454                             insert.hParent = type;
455                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
456                             insert.u.item.pszText = caps.szPname;
457                             insert.u.item.lParam = j + DEVICE_MASK;
458
459                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
460                         }
461                     }
462
463                     if (num_mxd)
464                     {
465                         LoadStringW (GetModuleHandle (NULL), IDS_DEVICES_MIXER, dev_type,
466                             sizeof(dev_type)/sizeof(dev_type[0]));
467
468                         insert.hParent = driver[i];
469                         insert.u.item.mask = TVIF_TEXT | TVIF_CHILDREN;
470                         insert.u.item.pszText = dev_type;
471                         insert.u.item.cChildren = 1;
472
473                         type = (HTREEITEM)SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
474
475                         for (j = 0; j < num_mxd; j++)
476                         {
477                             MIXERCAPSW caps;
478
479                             mxdMessagePtr(j, MXDM_GETDEVCAPS, 0, (DWORD_PTR)&caps, sizeof(caps));
480
481                             insert.hParent = type;
482                             insert.u.item.mask = TVIF_TEXT | TVIF_PARAM;
483                             insert.u.item.pszText = caps.szPname;
484                             insert.u.item.lParam = j + DEVICE_MASK;
485
486                             SendDlgItemMessageW (hDlg, IDC_AUDIO_TREE, TVM_INSERTITEMW, 0, (LPARAM)&insert);
487                         }
488                     }
489                 }
490             }
491             CloseDriver(hdrv, 0, 0);
492         }
493     }
494
495     /* restore the original cursor */
496     SetCursor(old_cursor);
497
498     SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_SELECTITEM, 0, 0);
499     SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)root);
500     for (j = 0; j < i; j++)
501         SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)driver[j]);
502 }
503
504 /* find all drivers that can be loaded */
505 static void findAudioDrivers(void)
506 {
507     int numFound = 0;
508     const AUDIO_DRIVER *pAudioDrv = NULL;
509     HCURSOR old_cursor;
510
511     /* delete an existing list */
512     HeapFree(GetProcessHeap(), 0, loadedAudioDrv);
513     loadedAudioDrv = 0;
514
515     /* change to the wait cursor because this can take a while if there is a
516      * misbehaving driver that takes a long time to open
517      */
518     old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
519
520     for (pAudioDrv = sAudioDrivers; pAudioDrv->nameID; pAudioDrv++)
521     {
522         if (strlen(pAudioDrv->szDriver))
523         {
524             HDRVR hdrv;
525             char driver[MAX_PATH];
526
527             sprintf(driver, "wine%s.drv", pAudioDrv->szDriver);
528
529             if ((hdrv = OpenDriverA(driver, 0, 0)))
530             {
531                 CloseDriver(hdrv, 0, 0);
532
533                 if (loadedAudioDrv)
534                     loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
535                 else
536                     loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));
537
538                 CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
539                 numFound++;
540             }
541         }
542     }
543
544     /* restore the original cursor */
545     SetCursor(old_cursor);
546
547     /* terminate list with empty driver */
548     if (numFound) {
549         loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
550         CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
551     } else
552         loadedAudioDrv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(AUDIO_DRIVER));
553 }
554
555 /* check local copy of registry string for unloadable drivers */
556 static void checkRegistrySetting(HWND hDlg)
557 {
558     const AUDIO_DRIVER *pAudioDrv;
559     char * token, * tokens = strdup(curAudioDriver);
560
561 start_over:
562     token = strtok(tokens, ",");
563     while (token != NULL)
564     {
565         BOOL found = FALSE;
566         for (pAudioDrv = loadedAudioDrv; pAudioDrv->nameID; pAudioDrv++)
567         {
568             if (strcmp(token, pAudioDrv->szDriver) == 0)
569             {
570                 found = TRUE;
571                 break;
572             }
573         }
574         if (found == FALSE)
575         {
576             WCHAR tokenW[MAX_NAME_LENGTH+1];
577             WCHAR messageStr[256];
578             WCHAR str[1024];
579             WCHAR caption[64];
580             
581             MultiByteToWideChar (CP_ACP, 0, token, -1, tokenW, sizeof(tokenW)/sizeof(tokenW[0]));
582             
583             LoadStringW (GetModuleHandle (NULL), IDS_UNAVAILABLE_DRIVER, messageStr,
584                 sizeof(messageStr)/sizeof(messageStr[0]));
585             wsprintfW (str, messageStr, tokenW);
586             LoadStringW (GetModuleHandle (NULL), IDS_WARNING, caption,
587                 sizeof(caption)/sizeof(caption[0]));
588             if (MessageBoxW (hDlg, str, caption, MB_ICONWARNING | MB_YESNOCANCEL) == IDYES)
589             {
590                 removeDriver(token);
591                 strcpy(tokens, curAudioDriver);
592                 goto start_over;
593             }
594         }
595         token = strtok(NULL, ",");
596     }
597     free(tokens);
598 }
599
600 static void selectDriver(HWND hDlg, const char * driver)
601 {
602     WCHAR text[1024];
603     WCHAR caption[64];
604
605     strcpy(curAudioDriver, driver);
606     set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);
607
608     if (LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_MISSING, text, sizeof(text)/sizeof(text[0])))
609     {
610         if (LoadStringW(GetModuleHandle(NULL), IDS_WINECFG_TITLE, caption, sizeof(caption)/sizeof(caption[0])))
611             MessageBoxW(hDlg, text, caption, MB_OK | MB_ICONINFORMATION);
612     }
613    
614     SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
615 }
616
617 static void initAudioDlg (HWND hDlg)
618 {
619     int i;
620     char* buf = NULL;
621
622     WINE_TRACE("\n");
623
624     /* make a list of all drivers that can be loaded */
625     findAudioDrivers();
626
627     /* get current registry setting if available */
628     buf = get_reg_key(config_key, "Drivers", "Audio", NULL);
629
630     /* check for first time install and set a default driver
631      * select first available driver, and if that fails: none
632      */
633     if (buf == NULL)
634     {
635         /* select first available driver */
636         if (*loadedAudioDrv->szDriver)
637             selectDriver(hDlg, loadedAudioDrv->szDriver);
638     }
639     else /* make a local copy of the current registry setting */
640         strcpy(curAudioDriver, buf);
641
642     WINE_TRACE("curAudioDriver = %s\n", curAudioDriver);
643
644     /* check for drivers that can't be loaded */
645     checkRegistrySetting(hDlg);
646
647     initAudioDeviceTree(hDlg);
648
649     SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_RESETCONTENT, 0, 0);
650     for (i = 0; 0 != DSound_HW_Accels[i].displayID; ++i) {
651       WCHAR accelStr[64];
652       LoadStringW (GetModuleHandle (NULL), DSound_HW_Accels[i].displayID, accelStr,
653           sizeof(accelStr)/sizeof(accelStr[0]));
654       SendDlgItemMessageW (hDlg, IDC_DSOUND_HW_ACCEL, CB_ADDSTRING, 0, (LPARAM)accelStr);
655     }
656     buf = get_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration", "Full");
657     for (i = 0; NULL != DSound_HW_Accels[i].settingStr; ++i) {
658       if (strcmp(buf, DSound_HW_Accels[i].settingStr) == 0) {
659         SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_SETCURSEL, i, 0);
660         break ;
661       }
662     }
663     if (NULL == DSound_HW_Accels[i].settingStr) {
664       WINE_ERR("Invalid Direct Sound HW Accel read from registry (%s)\n", buf);
665     }
666     HeapFree(GetProcessHeap(), 0, buf);
667
668     SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_RESETCONTENT, 0, 0);
669     for (i = 0; NULL != DSound_Rates[i]; ++i) {
670       SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_ADDSTRING, 0, (LPARAM) DSound_Rates[i]);
671     }
672     buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", "44100");
673     for (i = 0; NULL != DSound_Rates[i]; ++i) {
674       if (strcmp(buf, DSound_Rates[i]) == 0) {
675         SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_SETCURSEL, i, 0);
676         break ;
677       }
678     }
679
680     SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_RESETCONTENT, 0, 0);
681     for (i = 0; NULL != DSound_Bits[i]; ++i) {
682       SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_ADDSTRING, 0, (LPARAM) DSound_Bits[i]);
683     }
684     buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", "16");
685     for (i = 0; NULL != DSound_Bits[i]; ++i) {
686       if (strcmp(buf, DSound_Bits[i]) == 0) {
687         SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_SETCURSEL, i, 0);
688         break ;
689       }
690     }
691
692     buf = get_reg_key(config_key, keypath("DirectSound"), "EmulDriver", "N");
693     if (IS_OPTION_TRUE(*buf))
694       CheckDlgButton(hDlg, IDC_DSOUND_DRV_EMUL, BST_CHECKED);
695     else
696       CheckDlgButton(hDlg, IDC_DSOUND_DRV_EMUL, BST_UNCHECKED);
697     HeapFree(GetProcessHeap(), 0, buf);
698 }
699
700 INT_PTR CALLBACK
701 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
702 {
703   switch (uMsg) {
704       case WM_COMMAND:
705         switch (LOWORD(wParam)) {
706           case IDC_AUDIO_CONFIGURE:
707              configureAudioDriver(hDlg);
708              break;
709           case IDC_AUDIO_TEST:
710              if(!PlaySound(MAKEINTRESOURCE(IDW_TESTSOUND), NULL, SND_RESOURCE | SND_SYNC))
711                 MessageBox(NULL, "Audio test failed!", "Error", MB_OK | MB_ICONERROR);
712              break;
713           case IDC_AUDIO_CONTROL_PANEL:
714              MessageBox(NULL, "Launching audio control panel not implemented yet!", "Fixme", MB_OK | MB_ICONERROR);
715              break;
716           case IDC_DSOUND_HW_ACCEL:
717             if (HIWORD(wParam) == CBN_SELCHANGE) {
718               int selected_dsound_accel;
719               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
720               selected_dsound_accel = SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_GETCURSEL, 0, 0);
721               set_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration", 
722                  DSound_HW_Accels[selected_dsound_accel].settingStr);
723             }
724             break;
725           case IDC_DSOUND_RATES:
726             if (HIWORD(wParam) == CBN_SELCHANGE) {
727               int selected_dsound_rate;
728               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
729               selected_dsound_rate = SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_GETCURSEL, 0, 0);
730               set_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", DSound_Rates[selected_dsound_rate]);
731             }
732             break;
733           case IDC_DSOUND_BITS:
734             if (HIWORD(wParam) == CBN_SELCHANGE) {
735               int selected_dsound_bits;
736               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
737               selected_dsound_bits = SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_GETCURSEL, 0, 0);
738               set_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", DSound_Bits[selected_dsound_bits]);
739             }
740             break;
741           case IDC_DSOUND_DRV_EMUL:
742             if (HIWORD(wParam) == BN_CLICKED) {
743               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
744               if (IsDlgButtonChecked(hDlg, IDC_DSOUND_DRV_EMUL) == BST_CHECKED)
745                 set_reg_key(config_key, keypath("DirectSound"), "EmulDriver", "Y");
746               else
747                 set_reg_key(config_key, keypath("DirectSound"), "EmulDriver", "N");
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 }