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