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
.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
;
229 WCHAR colW
[64], speaker_str
[256];
235 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_DRIVER
, format_str
, ARRAY_SIZE(format_str
));
236 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_DRIVER_NONE
, disabled_str
,
237 ARRAY_SIZE(disabled_str
));
238 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_SYSDEFAULT
, sysdefault_str
,
239 ARRAY_SIZE(sysdefault_str
));
241 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
242 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
246 load_devices(devenum
, eRender
, &num_render_devs
, &render_devs
);
247 load_devices(devenum
, eCapture
, &num_capture_devs
, &capture_devs
);
249 PropVariantInit(&pv
);
250 if(get_driver_name(devenum
, &pv
) && pv
.pwszVal
[0] != '\0'){
252 wnsprintfW(display_str
, ARRAY_SIZE(display_str
), format_str
, pv
.pwszVal
);
253 lstrcatW(g_drv_keyW
, pv
.pwszVal
);
255 PropVariantClear(&pv
);
257 IMMDeviceEnumerator_Release(devenum
);
260 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_ADDSTRING
,
261 0, (LPARAM
)sysdefault_str
);
262 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_SETCURSEL
, 0, 0);
263 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_ADDSTRING
,
264 0, (LPARAM
)sysdefault_str
);
265 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_SETCURSEL
, 0, 0);
267 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_ADDSTRING
,
268 0, (LPARAM
)sysdefault_str
);
269 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_SETCURSEL
, 0, 0);
270 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_ADDSTRING
,
271 0, (LPARAM
)sysdefault_str
);
272 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_SETCURSEL
, 0, 0);
275 while (speaker_configs
[i
].text_id
!= 0) {
276 LoadStringW(GetModuleHandleW(NULL
), speaker_configs
[i
].text_id
, speaker_str
,
277 ARRAY_SIZE(speaker_str
));
279 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_ADDSTRING
,
280 0, (LPARAM
)speaker_str
);
285 GetClientRect(GetDlgItem(hDlg
, IDC_LIST_AUDIO_DEVICES
), &rect
);
286 width
= (rect
.right
- rect
.left
) * 3 / 5;
288 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_DEVICE
, colW
, ARRAY_SIZE(colW
));
289 lvcol
.mask
= LVCF_TEXT
| LVCF_WIDTH
| LVCF_SUBITEM
;
290 lvcol
.pszText
= colW
;
291 lvcol
.cchTextMax
= lstrlenW(colW
);
293 SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_INSERTCOLUMNW
, 0, (LPARAM
)&lvcol
);
295 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_SPEAKER_CONFIG
, colW
, ARRAY_SIZE(colW
));
296 lvcol
.pszText
= colW
;
297 lvcol
.cchTextMax
= lstrlenW(colW
);
298 lvcol
.cx
= rect
.right
- rect
.left
- width
;
299 SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_INSERTCOLUMNW
, 1, (LPARAM
)&lvcol
);
301 EnableWindow(GetDlgItem(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
), 0);
304 WCHAR
*reg_out_dev
, *reg_vout_dev
, *reg_in_dev
, *reg_vin_dev
;
306 reg_out_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_out_nameW
, NULL
);
307 reg_vout_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_vout_nameW
, NULL
);
308 reg_in_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_in_nameW
, NULL
);
309 reg_vin_dev
= get_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, reg_vin_nameW
, NULL
);
311 for(i
= 0; i
< num_render_devs
; ++i
){
314 if(!render_devs
[i
].id
)
317 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_ADDSTRING
,
318 0, (LPARAM
)render_devs
[i
].name
.pwszVal
);
319 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_SETITEMDATA
,
320 i
+ 1, (LPARAM
)&render_devs
[i
]);
322 if(reg_out_dev
&& !lstrcmpW(render_devs
[i
].id
, reg_out_dev
)){
323 SendDlgItemMessageW(hDlg
, IDC_AUDIOOUT_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
324 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_SETCURSEL
, render_devs
[i
].speaker_config
, 0);
327 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_ADDSTRING
,
328 0, (LPARAM
)render_devs
[i
].name
.pwszVal
);
329 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_SETITEMDATA
,
330 i
+ 1, (LPARAM
)&render_devs
[i
]);
331 if(reg_vout_dev
&& !lstrcmpW(render_devs
[i
].id
, reg_vout_dev
))
332 SendDlgItemMessageW(hDlg
, IDC_VOICEOUT_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
334 lvitem
.mask
= LVIF_TEXT
| LVIF_PARAM
;
337 lvitem
.pszText
= render_devs
[i
].name
.pwszVal
;
338 lvitem
.cchTextMax
= lstrlenW(lvitem
.pszText
);
339 lvitem
.lParam
= (LPARAM
)&render_devs
[i
];
341 SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_INSERTITEMW
, 0, (LPARAM
)&lvitem
);
343 LoadStringW(GetModuleHandleW(NULL
), speaker_configs
[render_devs
[i
].speaker_config
].text_id
,
344 speaker_str
, ARRAY_SIZE(speaker_str
));
346 lvitem
.mask
= LVIF_TEXT
;
349 lvitem
.pszText
= speaker_str
;
350 lvitem
.cchTextMax
= lstrlenW(lvitem
.pszText
);
352 SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_SETITEMW
, 0, (LPARAM
)&lvitem
);
355 for(i
= 0; i
< num_capture_devs
; ++i
){
356 if(!capture_devs
[i
].id
)
359 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_ADDSTRING
,
360 0, (LPARAM
)capture_devs
[i
].name
.pwszVal
);
361 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_SETITEMDATA
,
362 i
+ 1, (LPARAM
)&capture_devs
[i
]);
363 if(reg_in_dev
&& !lstrcmpW(capture_devs
[i
].id
, reg_in_dev
))
364 SendDlgItemMessageW(hDlg
, IDC_AUDIOIN_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
366 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_ADDSTRING
,
367 0, (LPARAM
)capture_devs
[i
].name
.pwszVal
);
368 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_SETITEMDATA
,
369 i
+ 1, (LPARAM
)&capture_devs
[i
]);
370 if(reg_vin_dev
&& !lstrcmpW(capture_devs
[i
].id
, reg_vin_dev
))
371 SendDlgItemMessageW(hDlg
, IDC_VOICEIN_DEVICE
, CB_SETCURSEL
, i
+ 1, 0);
374 HeapFree(GetProcessHeap(), 0, reg_out_dev
);
375 HeapFree(GetProcessHeap(), 0, reg_vout_dev
);
376 HeapFree(GetProcessHeap(), 0, reg_in_dev
);
377 HeapFree(GetProcessHeap(), 0, reg_vin_dev
);
379 wnsprintfW(display_str
, ARRAY_SIZE(display_str
), format_str
, disabled_str
);
381 SetDlgItemTextW(hDlg
, IDC_AUDIO_DRIVER
, display_str
);
384 static void set_reg_device(HWND hDlg
, int dlgitem
, const WCHAR
*key_name
)
387 struct DeviceInfo
*info
;
389 idx
= SendDlgItemMessageW(hDlg
, dlgitem
, CB_GETCURSEL
, 0, 0);
391 info
= (struct DeviceInfo
*)SendDlgItemMessageW(hDlg
, dlgitem
,
392 CB_GETITEMDATA
, idx
, 0);
394 if(!info
|| info
== (void*)CB_ERR
)
395 set_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, key_name
, NULL
);
397 set_reg_keyW(HKEY_CURRENT_USER
, g_drv_keyW
, key_name
, info
->id
);
400 static void test_sound(void)
402 if(!PlaySoundW(MAKEINTRESOURCEW(IDW_TESTSOUND
), NULL
, SND_RESOURCE
| SND_ASYNC
)){
403 WCHAR error_str
[256], title_str
[256];
405 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_TEST_FAILED
, error_str
,
406 ARRAY_SIZE(error_str
));
407 LoadStringW(GetModuleHandleW(NULL
), IDS_AUDIO_TEST_FAILED_TITLE
, title_str
,
408 ARRAY_SIZE(title_str
));
410 MessageBoxW(NULL
, error_str
, title_str
, MB_OK
| MB_ICONERROR
);
414 static void apply_speaker_configs(void)
417 IMMDeviceEnumerator
*devenum
;
423 hr
= CoCreateInstance(&CLSID_MMDeviceEnumerator
, NULL
,
424 CLSCTX_INPROC_SERVER
, &IID_IMMDeviceEnumerator
, (void**)&devenum
);
427 ERR("Unable to create MMDeviceEnumerator: 0x%08x\n", hr
);
431 PropVariantInit(&pv
);
434 for (i
= 0; i
< num_render_devs
; i
++) {
435 hr
= IMMDeviceEnumerator_GetDevice(devenum
, render_devs
[i
].id
, &dev
);
438 WARN("Could not get MMDevice for %s: 0x%08x\n", wine_dbgstr_w(render_devs
[i
].id
), hr
);
442 hr
= IMMDevice_OpenPropertyStore(dev
, STGM_WRITE
, &ps
);
445 WARN("Could not open property store for %s: 0x%08x\n", wine_dbgstr_w(render_devs
[i
].id
), hr
);
446 IMMDevice_Release(dev
);
450 pv
.ulVal
= speaker_configs
[render_devs
[i
].speaker_config
].speaker_mask
;
452 hr
= IPropertyStore_SetValue(ps
, &PKEY_AudioEndpoint_PhysicalSpeakers
, &pv
);
455 WARN("IPropertyStore_SetValue failed for %s: 0x%08x\n", wine_dbgstr_w(render_devs
[i
].id
), hr
);
457 IPropertyStore_Release(ps
);
458 IMMDevice_Release(dev
);
461 IMMDeviceEnumerator_Release(devenum
);
464 static void listview_changed(HWND hDlg
)
468 idx
= SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_GETNEXTITEM
, -1, LVNI_SELECTED
);
470 EnableWindow(GetDlgItem(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
), 0);
474 SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_SETCURSEL
,
475 render_devs
[idx
].speaker_config
, 0);
477 EnableWindow(GetDlgItem(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
), 1);
481 AudioDlgProc (HWND hDlg
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
485 switch (LOWORD(wParam
)) {
489 case IDC_AUDIOOUT_DEVICE
:
490 if(HIWORD(wParam
) == CBN_SELCHANGE
){
491 set_reg_device(hDlg
, IDC_AUDIOOUT_DEVICE
, reg_out_nameW
);
492 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
495 case IDC_VOICEOUT_DEVICE
:
496 if(HIWORD(wParam
) == CBN_SELCHANGE
){
497 set_reg_device(hDlg
, IDC_VOICEOUT_DEVICE
, reg_vout_nameW
);
498 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
501 case IDC_AUDIOIN_DEVICE
:
502 if(HIWORD(wParam
) == CBN_SELCHANGE
){
503 set_reg_device(hDlg
, IDC_AUDIOIN_DEVICE
, reg_in_nameW
);
504 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
507 case IDC_VOICEIN_DEVICE
:
508 if(HIWORD(wParam
) == CBN_SELCHANGE
){
509 set_reg_device(hDlg
, IDC_VOICEIN_DEVICE
, reg_vin_nameW
);
510 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
513 case IDC_SPEAKERCONFIG_SPEAKERS
:
514 if(HIWORD(wParam
) == CBN_SELCHANGE
){
517 idx
= SendDlgItemMessageW(hDlg
, IDC_SPEAKERCONFIG_SPEAKERS
, CB_GETCURSEL
, 0, 0);
518 dev
= SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_GETNEXTITEM
, -1, LVNI_SELECTED
);
520 if(dev
< num_render_devs
){
521 WCHAR speaker_str
[256];
524 render_devs
[dev
].speaker_config
= idx
;
526 LoadStringW(GetModuleHandleW(NULL
), speaker_configs
[idx
].text_id
,
527 speaker_str
, ARRAY_SIZE(speaker_str
));
529 lvitem
.mask
= LVIF_TEXT
;
532 lvitem
.pszText
= speaker_str
;
533 lvitem
.cchTextMax
= lstrlenW(lvitem
.pszText
);
535 SendDlgItemMessageW(hDlg
, IDC_LIST_AUDIO_DEVICES
, LVM_SETITEMW
, 0, (LPARAM
)&lvitem
);
537 SendMessageW(GetParent(hDlg
), PSM_CHANGED
, 0, 0);
545 set_window_title(hDlg
);
549 switch(((LPNMHDR
)lParam
)->code
) {
551 SetWindowLongPtrW(hDlg
, DWLP_MSGRESULT
, FALSE
);
554 apply_speaker_configs();
556 SetWindowLongPtrW(hDlg
, DWLP_MSGRESULT
, PSNRET_NOERROR
);
560 case LVN_ITEMCHANGED
:
561 listview_changed(hDlg
);