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