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