2 * Audio management UI code
4 * Copyright 2004 Chris Morgan
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.
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.
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
22 #define WIN32_LEAN_AND_MEAN
23 #define NONAMELESSUNION
26 #include "wine/port.h"
35 #include <wine/debug.h>
49 #include "mmdeviceapi.h"
50 #include "audioclient.h"
51 #include "audiopolicy.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(winecfg
);
64 static WCHAR g_drv_keyW
[256] = {'S','o','f','t','w','a','r','e','\\',
65 'W','i','n','e','\\','D','r','i','v','e','r','s','\\',0};
67 static const WCHAR reg_out_nameW
[] = {'D','e','f','a','u','l','t','O','u','t','p','u','t',0};
68 static const WCHAR reg_in_nameW
[] = {'D','e','f','a','u','l','t','I','n','p','u','t',0};
69 static const WCHAR reg_vout_nameW
[] = {'D','e','f','a','u','l','t','V','o','i','c','e','O','u','t','p','u','t',0};
70 static const WCHAR reg_vin_nameW
[] = {'D','e','f','a','u','l','t','V','o','i','c','e','I','n','p','u','t',0};
72 static UINT num_render_devs
, num_capture_devs
;
73 static struct DeviceInfo
*render_devs
, *capture_devs
;
81 { IDS_AUDIO_SPEAKER_5POINT1
, KSAUDIO_SPEAKER_5POINT1
},
82 { IDS_AUDIO_SPEAKER_QUAD
, KSAUDIO_SPEAKER_QUAD
},
83 { IDS_AUDIO_SPEAKER_STEREO
, KSAUDIO_SPEAKER_STEREO
},
84 { IDS_AUDIO_SPEAKER_MONO
, KSAUDIO_SPEAKER_MONO
},
88 static BOOL
load_device(IMMDevice
*dev
, struct DeviceInfo
*info
)
95 hr
= IMMDevice_GetId(dev
, &info
->id
);
101 hr
= IMMDevice_OpenPropertyStore(dev
, STGM_READ
, &ps
);
103 CoTaskMemFree(info
->id
);
108 PropVariantInit(&info
->name
);
110 hr
= IPropertyStore_GetValue(ps
,
111 (PROPERTYKEY
*)&DEVPKEY_Device_FriendlyName
, &info
->name
);
113 CoTaskMemFree(info
->id
);
115 IPropertyStore_Release(ps
);
119 PropVariantInit(&pv
);
121 hr
= IPropertyStore_GetValue(ps
,
122 &PKEY_AudioEndpoint_PhysicalSpeakers
, &pv
);
124 info
->speaker_config
= -1;
125 if(SUCCEEDED(hr
) && pv
.vt
== VT_UI4
){
127 while (speaker_configs
[i
].text_id
!= 0) {
128 if ((speaker_configs
[i
].speaker_mask
& pv
.u
.ulVal
) == speaker_configs
[i
].speaker_mask
) {
129 info
->speaker_config
= i
;
136 /* fallback to stereo */
137 if(info
->speaker_config
== -1)
138 info
->speaker_config
= 2;
140 IPropertyStore_Release(ps
);
145 static BOOL
load_devices(IMMDeviceEnumerator
*devenum
, EDataFlow dataflow
,
146 UINT
*ndevs
, struct DeviceInfo
**out
)
148 IMMDeviceCollection
*coll
;
152 hr
= IMMDeviceEnumerator_EnumAudioEndpoints(devenum
, dataflow
,
153 DEVICE_STATE_ACTIVE
, &coll
);
157 hr
= IMMDeviceCollection_GetCount(coll
, ndevs
);
159 IMMDeviceCollection_Release(coll
);
164 *out
= HeapAlloc(GetProcessHeap(), 0,
165 sizeof(struct DeviceInfo
) * (*ndevs
));
167 IMMDeviceCollection_Release(coll
);
171 for(i
= 0; i
< *ndevs
; ++i
){
174 hr
= IMMDeviceCollection_Item(coll
, i
, &dev
);
180 load_device(dev
, &(*out
)[i
]);
182 IMMDevice_Release(dev
);
187 IMMDeviceCollection_Release(coll
);
192 static BOOL
get_driver_name(IMMDeviceEnumerator
*devenum
, PROPVARIANT
*pv
)
198 static const WCHAR wine_info_deviceW
[] = {'W','i','n','e',' ',
199 'i','n','f','o',' ','d','e','v','i','c','e',0};
201 hr
= IMMDeviceEnumerator_GetDevice(devenum
, wine_info_deviceW
, &device
);
205 hr
= IMMDevice_OpenPropertyStore(device
, STGM_READ
, &ps
);
207 IMMDevice_Release(device
);
211 hr
= IPropertyStore_GetValue(ps
,
212 (const PROPERTYKEY
*)&DEVPKEY_Device_Driver
, pv
);
213 IPropertyStore_Release(ps
);
214 IMMDevice_Release(device
);
221 static void initAudioDlg (HWND hDlg
)
223 WCHAR display_str
[256], format_str
[256], sysdefault_str
[256], disabled_str
[64];
224 IMMDeviceEnumerator
*devenum
;
225 BOOL have_driver
= FALSE
;
231 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_DRIVER
,
232 format_str
, sizeof(format_str
) / sizeof(*format_str
));
233 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_DRIVER_NONE
,
234 disabled_str
, sizeof(disabled_str
) / sizeof(*disabled_str
));
235 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_SYSDEFAULT
,
236 sysdefault_str
, sizeof(sysdefault_str
) / sizeof(*sysdefault_str
));
238 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
239 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
243 load_devices(devenum
, eRender
, &num_render_devs
, &render_devs
);
244 load_devices(devenum
, eCapture
, &num_capture_devs
, &capture_devs
);
246 PropVariantInit(&pv
);
247 if(get_driver_name(devenum
, &pv
) && pv
.u
.pwszVal
[0] != '\0'){
249 wnsprintfW(display_str
, sizeof(display_str
) / sizeof(*display_str
),
250 format_str
, pv
.u
.pwszVal
);
251 lstrcatW(g_drv_keyW
, pv
.u
.pwszVal
);
253 PropVariantClear(&pv
);
255 IMMDeviceEnumerator_Release(devenum
);
258 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_ADDSTRING
,
259 0, (LPARAM
)sysdefault_str
);
260 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_SETCURSEL
, 0, 0);
261 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_ADDSTRING
,
262 0, (LPARAM
)sysdefault_str
);
263 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_SETCURSEL
, 0, 0);
265 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_ADDSTRING
,
266 0, (LPARAM
)sysdefault_str
);
267 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_SETCURSEL
, 0, 0);
268 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_ADDSTRING
,
269 0, (LPARAM
)sysdefault_str
);
270 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_SETCURSEL
, 0, 0);
273 while (speaker_configs
[i
].text_id
!= 0) {
274 WCHAR speaker_str
[256];
276 LoadStringW(GetModuleHandleW(NULL
), speaker_configs
[i
].text_id
,
277 speaker_str
, sizeof(speaker_str
) / sizeof(*speaker_str
));
279 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_ADDSTRING
,
280 0, (LPARAM
)speaker_str
);
286 WCHAR
*reg_out_dev
, *reg_vout_dev
, *reg_in_dev
, *reg_vin_dev
;
287 BOOL default_dev_found
= FALSE
;
289 reg_out_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_out_nameW
, NULL
);
290 reg_vout_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_vout_nameW
, NULL
);
291 reg_in_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_in_nameW
, NULL
);
292 reg_vin_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_vin_nameW
, NULL
);
294 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_DEVICE
, CB_SETCURSEL
, i
, 0);
295 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_SETCURSEL
, render_devs
[i
].speaker_config
, 0);
297 for(i
= 0; i
< num_render_devs
; ++i
){
298 if(!render_devs
[i
].id
)
301 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_ADDSTRING
,
302 0, (LPARAM
)render_devs
[i
].name
.u
.pwszVal
);
303 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_SETITEMDATA
,
304 i
+ 1, (LPARAM
)&render_devs
[i
]);
306 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_DEVICE
, CB_ADDSTRING
,
307 0, (LPARAM
)render_devs
[i
].name
.u
.pwszVal
);
309 if(reg_out_dev
&& !lstrcmpW(render_devs
[i
].id
, reg_out_dev
)){
310 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
311 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_DEVICE
, CB_SETCURSEL
, i
, 0);
312 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_SETCURSEL
, render_devs
[i
].speaker_config
, 0);
313 default_dev_found
= TRUE
;
316 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_ADDSTRING
,
317 0, (LPARAM
)render_devs
[i
].name
.u
.pwszVal
);
318 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_SETITEMDATA
,
319 i
+ 1, (LPARAM
)&render_devs
[i
]);
320 if(reg_vout_dev
&& !lstrcmpW(render_devs
[i
].id
, reg_vout_dev
))
321 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
324 if(!default_dev_found
&& num_render_devs
> 0){
325 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_DEVICE
, CB_SETCURSEL
, 0, 0);
326 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_SETCURSEL
, render_devs
[0].speaker_config
, 0);
329 for(i
= 0; i
< num_capture_devs
; ++i
){
330 if(!capture_devs
[i
].id
)
333 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_ADDSTRING
,
334 0, (LPARAM
)capture_devs
[i
].name
.u
.pwszVal
);
335 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_SETITEMDATA
,
336 i
+ 1, (LPARAM
)&capture_devs
[i
]);
337 if(reg_in_dev
&& !lstrcmpW(capture_devs
[i
].id
, reg_in_dev
))
338 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
340 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_ADDSTRING
,
341 0, (LPARAM
)capture_devs
[i
].name
.u
.pwszVal
);
342 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_SETITEMDATA
,
343 i
+ 1, (LPARAM
)&capture_devs
[i
]);
344 if(reg_vin_dev
&& !lstrcmpW(capture_devs
[i
].id
, reg_vin_dev
))
345 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
348 HeapFree(GetProcessHeap(), 0, reg_out_dev
);
349 HeapFree(GetProcessHeap(), 0, reg_vout_dev
);
350 HeapFree(GetProcessHeap(), 0, reg_in_dev
);
351 HeapFree(GetProcessHeap(), 0, reg_vin_dev
);
353 wnsprintfW(display_str
, sizeof(display_str
) / sizeof(*display_str
),
354 format_str
, disabled_str
);
356 SetDlgItemTextW(hDlg
, IDC_AUDIO_DRIVER
, display_str
);
359 static void set_reg_device(HWND hDlg
, int dlgitem
, const WCHAR
*key_name
)
362 struct DeviceInfo
*info
;
364 idx
= SendDlgItemMessageW(hDlg
, dlgitem
, CB_GETCURSEL
, 0, 0);
366 info
= (struct DeviceInfo
*)SendDlgItemMessageW(hDlg
, dlgitem
,
367 CB_GETITEMDATA
, idx
, 0);
369 if(!info
|| info
== (void*)CB_ERR
)
370 set_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, key_name
, NULL
);
372 set_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, key_name
, info
->id
);
375 static void test_sound(void)
377 if(!PlaySoundW(MAKEINTRESOURCEW(IDW_TESTSOUND
), NULL
, SND_RESOURCE
| SND_ASYNC
)){
378 WCHAR error_str
[256], title_str
[256];
380 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_TEST_FAILED
,
381 error_str
, sizeof(error_str
) / sizeof(*error_str
));
382 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_TEST_FAILED_TITLE
,
383 title_str
, sizeof(title_str
) / sizeof(*title_str
));
385 MessageBoxW(NULL
, error_str
, title_str
, MB_OK
| MB_ICONERROR
);
389 static void apply_speaker_configs(void)
392 IMMDeviceEnumerator
*devenum
;
398 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
399 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
402 ERR("Unable to create MMDeviceEnumerator: 0x%08x\n", hr
);
406 PropVariantInit(&pv
);
409 for (i
= 0; i
< num_render_devs
; i
++) {
410 hr
= IMMDeviceEnumerator_GetDevice(devenum
, render_devs
[i
].id
, &dev
);
413 WARN("Could not get MMDevice for %s: 0x%08x\n", wine_dbgstr_w(render_devs
[i
].id
), hr
);
417 hr
= IMMDevice_OpenPropertyStore(dev
, STGM_WRITE
, &ps
);
420 WARN("Could not open property store for %s: 0x%08x\n", wine_dbgstr_w(render_devs
[i
].id
), hr
);
421 IMMDevice_Release(dev
);
425 pv
.u
.ulVal
= speaker_configs
[render_devs
[i
].speaker_config
].speaker_mask
;
427 hr
= IPropertyStore_SetValue(ps
, &PKEY_AudioEndpoint_PhysicalSpeakers
, &pv
);
430 WARN("IPropertyStore_SetValue failed for %s: 0x%08x\n", wine_dbgstr_w(render_devs
[i
].id
), hr
);
432 IPropertyStore_Release(ps
);
433 IMMDevice_Release(dev
);
436 IMMDeviceEnumerator_Release(devenum
);
440 AudioDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
444 switch (LOWORD(wParam
)) {
448 case IDC_AUDIOOUT_DEVICE
:
449 if(HIWORD(wParam
) == CBN_SELCHANGE
){
450 set_reg_device(hDlg
, IDC_AUDIOOUT_DEVICE
, reg_out_nameW
);
451 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
454 case IDC_VOICEOUT_DEVICE
:
455 if(HIWORD(wParam
) == CBN_SELCHANGE
){
456 set_reg_device(hDlg
, IDC_VOICEOUT_DEVICE
, reg_vout_nameW
);
457 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
460 case IDC_AUDIOIN_DEVICE
:
461 if(HIWORD(wParam
) == CBN_SELCHANGE
){
462 set_reg_device(hDlg
, IDC_AUDIOIN_DEVICE
, reg_in_nameW
);
463 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
466 case IDC_VOICEIN_DEVICE
:
467 if(HIWORD(wParam
) == CBN_SELCHANGE
){
468 set_reg_device(hDlg
, IDC_VOICEIN_DEVICE
, reg_vin_nameW
);
469 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
472 case IDC_SPEAKERCONFIG_DEVICE
:
473 if(HIWORD(wParam
) == CBN_SELCHANGE
){
476 idx
= SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_DEVICE
, CB_GETCURSEL
, 0, 0);
478 if(idx
< num_render_devs
){
479 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_SETCURSEL
, render_devs
[idx
].speaker_config
, 0);
483 case IDC_SPEAKERCONFIG_SPEAKERS
:
484 if(HIWORD(wParam
) == CBN_SELCHANGE
){
487 idx
= SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_GETCURSEL
, 0, 0);
488 dev
= SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_DEVICE
, CB_GETCURSEL
, 0, 0);
490 if(dev
< num_render_devs
){
491 render_devs
[dev
].speaker_config
= idx
;
492 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
500 set_window_title(hDlg
);
504 switch(((LPNMHDR
)lParam
)->code
) {
506 SetWindowLongPtrW(hDlg
, DWLP_MSGRESULT
, FALSE
);
509 apply_speaker_configs();
511 SetWindowLongPtrW(hDlg
, DWLP_MSGRESULT
, PSNRET_NOERROR
);