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