server: Don't cache sockets until they are pollable.
[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   {IDS_DRIVER_JACK,      "jack"},
97   {IDS_DRIVER_NAS,       "nas"},
98   {IDS_DRIVER_ESOUND,    "esd"},
99   {IDS_DRIVER_AUDIOIO,   "audioio"},
100   {0, ""}
101 };
102
103 /* list of available drivers */
104 static AUDIO_DRIVER * loadedAudioDrv;
105
106 /* local copy of registry setting */
107 static char curAudioDriver[1024];
108
109 /* driver index to configure */
110 static int toConfigure;
111
112 /* display a driver specific configuration dialog */
113 static void configureAudioDriver(HWND hDlg)
114 {
115     const AUDIO_DRIVER *pAudioDrv = &loadedAudioDrv[toConfigure];
116
117     if (strlen(pAudioDrv->szDriver) != 0)
118     {
119         HDRVR hdrvr;
120         char wine_driver[MAX_NAME_LENGTH + 9];
121         sprintf(wine_driver, "wine%s.drv", pAudioDrv->szDriver);
122         hdrvr = pAudioDrv->hDriver;
123         if (hdrvr != 0)
124         {
125             if (SendDriverMessage(hdrvr, DRV_QUERYCONFIGURE, 0, 0) != 0)
126             {
127                 DRVCONFIGINFO dci;
128                 dci.dwDCISize = sizeof (dci);
129                 dci.lpszDCISectionName = NULL;
130                 dci.lpszDCIAliasName = NULL;
131                 SendDriverMessage(hdrvr, DRV_CONFIGURE, 0, (LONG_PTR)&dci);
132             }
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     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 = pAudioDrv->hDriver))
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         }
477     }
478
479     /* restore the original cursor */
480     SetCursor(old_cursor);
481
482     SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_SELECTITEM, 0, 0);
483     SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)root);
484     for (j = 0; j < i; j++)
485         SendDlgItemMessage(hDlg, IDC_AUDIO_TREE, TVM_EXPAND, TVE_EXPAND, (LPARAM)driver[j]);
486 }
487
488 /* find all drivers that can be loaded */
489 static void findAudioDrivers(void)
490 {
491     int numFound = 0;
492     AUDIO_DRIVER *pAudioDrv = NULL;
493     HCURSOR old_cursor;
494
495     /* delete an existing list */
496     HeapFree(GetProcessHeap(), 0, loadedAudioDrv);
497     loadedAudioDrv = 0;
498
499     /* change to the wait cursor because this can take a while if there is a
500      * misbehaving driver that takes a long time to open
501      */
502     old_cursor = SetCursor(LoadCursor(0, IDC_WAIT));
503
504     for (pAudioDrv = sAudioDrivers; pAudioDrv->nameID; pAudioDrv++)
505     {
506         if (strlen(pAudioDrv->szDriver))
507         {
508             HDRVR hdrv;
509             char driver[MAX_PATH];
510
511             sprintf(driver, "wine%s.drv", pAudioDrv->szDriver);
512
513             hdrv = pAudioDrv->hDriver;
514             if (!pAudioDrv->hDriver && (hdrv = OpenDriverA(driver, 0, 0))) {
515                 HMODULE lib = GetDriverModuleHandle(hdrv);
516                 MessagePtr wodMessagePtr = (MessagePtr)GetProcAddress(lib, "wodMessage");
517                 MessagePtr widMessagePtr = (MessagePtr)GetProcAddress(lib, "widMessage");
518                 MessagePtr modMessagePtr = (MessagePtr)GetProcAddress(lib, "modMessage");
519                 MessagePtr midMessagePtr = (MessagePtr)GetProcAddress(lib, "midMessage");
520                 MessagePtr auxMessagePtr = (MessagePtr)GetProcAddress(lib, "auxMessage");
521                 MessagePtr mxdMessagePtr = (MessagePtr)GetProcAddress(lib, "mxdMessage");
522
523                 pAudioDrv->hDriver = hdrv;
524
525                 if (wodMessagePtr)
526                     wodMessagePtr(0, DRVM_INIT, 0, 0, 0);
527
528                 if (widMessagePtr)
529                     widMessagePtr(0, DRVM_INIT, 0, 0, 0);
530
531                 if (modMessagePtr)
532                     modMessagePtr(0, DRVM_INIT, 0, 0, 0);
533
534                 if (midMessagePtr)
535                     midMessagePtr(0, DRVM_INIT, 0, 0, 0);
536
537                 if (auxMessagePtr)
538                     auxMessagePtr(0, DRVM_INIT, 0, 0, 0);
539
540                 if (mxdMessagePtr)
541                     mxdMessagePtr(0, DRVM_INIT, 0, 0, 0);
542             }
543             if (hdrv)
544             {
545                 if (loadedAudioDrv)
546                     loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
547                 else
548                     loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));
549
550                 CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
551                 numFound++;
552             }
553         }
554     }
555
556     /* restore the original cursor */
557     SetCursor(old_cursor);
558
559     /* terminate list with empty driver */
560     if (numFound) {
561         loadedAudioDrv = HeapReAlloc(GetProcessHeap(), 0, loadedAudioDrv, (numFound + 1) * sizeof(AUDIO_DRIVER));
562         CopyMemory(&loadedAudioDrv[numFound], pAudioDrv, sizeof(AUDIO_DRIVER));
563     } else {
564         loadedAudioDrv = HeapAlloc(GetProcessHeap(), 0, sizeof(AUDIO_DRIVER));
565         CopyMemory(&loadedAudioDrv[0], pAudioDrv, sizeof(AUDIO_DRIVER));
566     }
567 }
568
569 /* check local copy of registry string for unloadable drivers */
570 static void checkRegistrySetting(HWND hDlg)
571 {
572     const AUDIO_DRIVER *pAudioDrv;
573     char * token, * tokens;
574
575     tokens = HeapAlloc(GetProcessHeap(), 0, strlen(curAudioDriver)+1);
576     strcpy(tokens, curAudioDriver);
577
578 start_over:
579     token = strtok(tokens, ",");
580     while (token != NULL)
581     {
582         BOOL found = FALSE;
583         for (pAudioDrv = loadedAudioDrv; pAudioDrv->nameID; pAudioDrv++)
584         {
585             if (strcmp(token, pAudioDrv->szDriver) == 0)
586             {
587                 found = TRUE;
588                 break;
589             }
590         }
591         if (found == FALSE)
592         {
593             WCHAR tokenW[MAX_NAME_LENGTH+1];
594             WCHAR messageStr[256];
595             WCHAR str[1024];
596             WCHAR caption[64];
597             
598             MultiByteToWideChar (CP_ACP, 0, token, -1, tokenW, sizeof(tokenW)/sizeof(tokenW[0]));
599             
600             LoadStringW (GetModuleHandle (NULL), IDS_UNAVAILABLE_DRIVER, messageStr,
601                 sizeof(messageStr)/sizeof(messageStr[0]));
602             wsprintfW (str, messageStr, tokenW);
603             LoadStringW (GetModuleHandle (NULL), IDS_WARNING, caption,
604                 sizeof(caption)/sizeof(caption[0]));
605             if (MessageBoxW (hDlg, str, caption, MB_ICONWARNING | MB_YESNOCANCEL) == IDYES)
606             {
607                 removeDriver(token);
608                 strcpy(tokens, curAudioDriver);
609                 goto start_over;
610             }
611         }
612         token = strtok(NULL, ",");
613     }
614     HeapFree(GetProcessHeap(), 0, tokens);
615 }
616
617 static void selectDriver(HWND hDlg, const char * driver)
618 {
619     WCHAR text[1024];
620     WCHAR caption[64];
621
622     strcpy(curAudioDriver, driver);
623     set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);
624
625     if (LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_MISSING, text, sizeof(text)/sizeof(text[0])))
626     {
627         if (LoadStringW(GetModuleHandle(NULL), IDS_WINECFG_TITLE, caption, sizeof(caption)/sizeof(caption[0])))
628             MessageBoxW(hDlg, text, caption, MB_OK | MB_ICONINFORMATION);
629     }
630    
631     SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
632 }
633
634 static void initAudioDlg (HWND hDlg)
635 {
636     int i, j, found;
637     char* buf = NULL;
638
639     WINE_TRACE("\n");
640
641     /* make a list of all drivers that can be loaded */
642     findAudioDrivers();
643
644     /* get current registry setting if available */
645     buf = get_reg_key(config_key, "Drivers", "Audio", NULL);
646
647     /* check for first time install and set a default driver
648      * select first available driver, and if that fails: none
649      */
650     if (buf == NULL)
651     {
652         /* select first available driver */
653         if (*loadedAudioDrv->szDriver)
654             selectDriver(hDlg, loadedAudioDrv->szDriver);
655     }
656     else /* make a local copy of the current registry setting */
657         strcpy(curAudioDriver, buf);
658
659     WINE_TRACE("curAudioDriver = %s\n", curAudioDriver);
660
661     /* check for drivers that can't be loaded */
662     checkRegistrySetting(hDlg);
663
664     initAudioDeviceTree(hDlg);
665
666     SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_RESETCONTENT, 0, 0);
667     buf = get_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration", "Full");
668
669     j = found = 0;
670     for (i = 0; 0 != DSound_HW_Accels[i].displayID; ++i) {
671       WCHAR accelStr[64];
672       int match;
673
674       match = (strcmp(buf, DSound_HW_Accels[i].settingStr) == 0);
675       if (match)
676       {
677         DSound_HW_Accels[i].visible = 1;
678         found = 1;
679       }
680
681       if (DSound_HW_Accels[i].visible)
682       {
683         LoadStringW (GetModuleHandle (NULL), DSound_HW_Accels[i].displayID,
684                      accelStr, sizeof(accelStr)/sizeof(accelStr[0]));
685         SendDlgItemMessageW (hDlg, IDC_DSOUND_HW_ACCEL, CB_ADDSTRING, 0, (LPARAM)accelStr);
686         if (match)
687           SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_SETCURSEL, j, 0);
688         j++;
689       }
690     }
691     if (!found) {
692       WINE_ERR("Invalid Direct Sound HW Accel read from registry (%s)\n", buf);
693     }
694     HeapFree(GetProcessHeap(), 0, buf);
695
696     SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_RESETCONTENT, 0, 0);
697     for (i = 0; NULL != DSound_Rates[i]; ++i) {
698       SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_ADDSTRING, 0, (LPARAM) DSound_Rates[i]);
699     }
700     buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", "44100");
701     for (i = 0; NULL != DSound_Rates[i]; ++i) {
702       if (strcmp(buf, DSound_Rates[i]) == 0) {
703         SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_SETCURSEL, i, 0);
704         break ;
705       }
706     }
707
708     SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_RESETCONTENT, 0, 0);
709     for (i = 0; NULL != DSound_Bits[i]; ++i) {
710       SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_ADDSTRING, 0, (LPARAM) DSound_Bits[i]);
711     }
712     buf = get_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", "16");
713     for (i = 0; NULL != DSound_Bits[i]; ++i) {
714       if (strcmp(buf, DSound_Bits[i]) == 0) {
715         SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_SETCURSEL, i, 0);
716         break ;
717       }
718     }
719     HeapFree(GetProcessHeap(), 0, buf);
720 }
721
722 INT_PTR CALLBACK
723 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
724 {
725   switch (uMsg) {
726       case WM_COMMAND:
727         switch (LOWORD(wParam)) {
728           case IDC_AUDIO_CONFIGURE:
729              configureAudioDriver(hDlg);
730              break;
731           case IDC_AUDIO_TEST:
732              if(!PlaySound(MAKEINTRESOURCE(IDW_TESTSOUND), NULL, SND_RESOURCE | SND_SYNC))
733                 MessageBox(NULL, "Audio test failed!", "Error", MB_OK | MB_ICONERROR);
734              break;
735           case IDC_AUDIO_CONTROL_PANEL:
736              MessageBox(NULL, "Launching audio control panel not implemented yet!", "Fixme", MB_OK | MB_ICONERROR);
737              break;
738           case IDC_DSOUND_HW_ACCEL:
739             if (HIWORD(wParam) == CBN_SELCHANGE) {
740               int selected_dsound_accel;
741               int i, j = 0;
742
743               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
744               selected_dsound_accel = SendDlgItemMessage(hDlg, IDC_DSOUND_HW_ACCEL, CB_GETCURSEL, 0, 0);
745               for (i = 0; DSound_HW_Accels[i].settingStr; ++i)
746               {
747                 if (DSound_HW_Accels[i].visible)
748                 {
749                   if (j == selected_dsound_accel)
750                   {
751                     set_reg_key(config_key, keypath("DirectSound"), "HardwareAcceleration",
752                       DSound_HW_Accels[i].settingStr);
753                     break;
754                   }
755                   j++;
756                 }
757               }
758             }
759             break;
760           case IDC_DSOUND_RATES:
761             if (HIWORD(wParam) == CBN_SELCHANGE) {
762               int selected_dsound_rate;
763               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
764               selected_dsound_rate = SendDlgItemMessage(hDlg, IDC_DSOUND_RATES, CB_GETCURSEL, 0, 0);
765               set_reg_key(config_key, keypath("DirectSound"), "DefaultSampleRate", DSound_Rates[selected_dsound_rate]);
766             }
767             break;
768           case IDC_DSOUND_BITS:
769             if (HIWORD(wParam) == CBN_SELCHANGE) {
770               int selected_dsound_bits;
771               SendMessage(GetParent(hDlg), PSM_CHANGED, 0, 0);
772               selected_dsound_bits = SendDlgItemMessage(hDlg, IDC_DSOUND_BITS, CB_GETCURSEL, 0, 0);
773               set_reg_key(config_key, keypath("DirectSound"), "DefaultBitsPerSample", DSound_Bits[selected_dsound_bits]);
774             }
775             break;
776         }
777         break;
778
779       case WM_SHOWWINDOW:
780         set_window_title(hDlg);
781         break;
782
783       case WM_NOTIFY:
784         switch(((LPNMHDR)lParam)->code) {
785             case PSN_KILLACTIVE:
786               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
787               break;
788             case PSN_APPLY:
789               set_reg_key(config_key, "Drivers", "Audio", curAudioDriver);
790               apply();
791               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
792               break;
793             case PSN_SETACTIVE:
794               break;
795             case NM_CLICK:
796               if (((LPNMHDR)lParam)->idFrom == IDC_AUDIO_TREE)
797               {
798                   TVHITTESTINFO ht;
799                   DWORD dwPos = GetMessagePos();
800                   HWND tree = ((LPNMHDR)lParam)->hwndFrom;
801                   ZeroMemory(&ht, sizeof(ht));
802                   ht.pt.x = (short)LOWORD(dwPos);
803                   ht.pt.y = (short)HIWORD(dwPos);
804                   MapWindowPoints(HWND_DESKTOP, tree, &ht.pt, 1);
805                   SendMessageW( tree, TVM_HITTEST, 0, (LPARAM)&ht );
806                   if (TVHT_ONITEMSTATEICON & ht.flags)
807                   {
808                       TVITEM tvItem;
809                       int index;
810                       ZeroMemory(&tvItem, sizeof(tvItem));
811                       tvItem.hItem = ht.hItem;
812                       SendMessageW( tree, TVM_GETITEMW, 0, (LPARAM) &tvItem );
813
814                       index = TreeView_GetItemState(tree, ht.hItem, TVIS_STATEIMAGEMASK);
815                       if (index == INDEXTOSTATEIMAGEMASK(1))
816                       {
817                           TreeView_SetItemState(tree, ht.hItem, INDEXTOSTATEIMAGEMASK(2), TVIS_STATEIMAGEMASK);
818                           addDriver(loadedAudioDrv[tvItem.lParam & 0xff].szDriver);
819                           SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
820                       }
821                       else if (index == INDEXTOSTATEIMAGEMASK(2))
822                       {
823                           TreeView_SetItemState(tree, ht.hItem, INDEXTOSTATEIMAGEMASK(1), TVIS_STATEIMAGEMASK);
824                           removeDriver(loadedAudioDrv[tvItem.lParam & 0xff].szDriver);
825                           SendMessage(GetParent(hDlg), PSM_CHANGED, (WPARAM) hDlg, 0); /* enable apply button */
826                       }
827                   }
828               }
829               break;
830             case NM_RCLICK:
831               if (((LPNMHDR)lParam)->idFrom == IDC_AUDIO_TREE)
832               {
833                   TVHITTESTINFO ht;
834                   DWORD dwPos = GetMessagePos();
835                   HWND tree = ((LPNMHDR)lParam)->hwndFrom;
836                   POINT pt;
837                   ZeroMemory(&ht, sizeof(ht));
838                   pt.x = (short)LOWORD(dwPos);
839                   pt.y = (short)HIWORD(dwPos);
840                   ht.pt = pt;
841                   MapWindowPoints(HWND_DESKTOP, tree, &ht.pt, 1);
842                   SendMessageW( tree, TVM_HITTEST, 0, (LPARAM)&ht );
843                   if (TVHT_ONITEMLABEL & ht.flags)
844                   {
845                       TVITEM tvItem;
846                       ZeroMemory(&tvItem, sizeof(tvItem));
847                       tvItem.hItem = ht.hItem;
848                       tvItem.mask = TVIF_PARAM;
849                       tvItem.lParam = -1;
850                       if (TreeView_GetItem(tree, &tvItem))
851                       {
852                           if (tvItem.lParam & DRIVER_MASK)
853                           {
854                               if (hPopupMenus)
855                               {
856                                   TrackPopupMenu(GetSubMenu(hPopupMenus, 0), TPM_RIGHTBUTTON, pt.x, pt.y, 0, tree, NULL);
857                                   toConfigure = tvItem.lParam & ~DRIVER_MASK;
858                               }
859                           }
860                           else if (tvItem.lParam & DEVICE_MASK)
861                           {
862                               /* FIXME TBD */
863                           }
864
865                       }
866                   }
867               }
868         }
869         break;
870
871   case WM_INITDIALOG:
872     initAudioDlg(hDlg);
873     break;
874   }
875
876   return FALSE;
877 }