winecfg: Reload winmm for each sound test.
[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 #define COBJMACROS
35 #include <windows.h>
36 #include <wine/debug.h>
37 #include <shellapi.h>
38 #include <objbase.h>
39 #include <shlguid.h>
40 #include <shlwapi.h>
41 #include <shlobj.h>
42 #include <mmsystem.h>
43 #include <mmreg.h>
44 #include <mmddk.h>
45
46 #include "ole2.h"
47 #include "initguid.h"
48 #include "devpkey.h"
49 #include "mmdeviceapi.h"
50 #include "audioclient.h"
51 #include "audiopolicy.h"
52
53 #include "winecfg.h"
54 #include "resource.h"
55
56 WINE_DEFAULT_DEBUG_CHANNEL(winecfg);
57
58 static BOOL get_driver_name(PROPVARIANT *pv)
59 {
60     IMMDeviceEnumerator *devenum;
61     IMMDevice *device;
62     IPropertyStore *ps;
63     HRESULT hr;
64
65     static const WCHAR wine_info_deviceW[] = {'W','i','n','e',' ',
66         'i','n','f','o',' ','d','e','v','i','c','e',0};
67
68     hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
69             CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)&devenum);
70     if(FAILED(hr))
71         return FALSE;
72
73     hr = IMMDeviceEnumerator_GetDevice(devenum, wine_info_deviceW, &device);
74     IMMDeviceEnumerator_Release(devenum);
75     if(FAILED(hr))
76         return FALSE;
77
78     hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
79     if(FAILED(hr)){
80         IMMDevice_Release(device);
81         return FALSE;
82     }
83
84     hr = IPropertyStore_GetValue(ps,
85             (const PROPERTYKEY *)&DEVPKEY_Device_Driver, pv);
86     IPropertyStore_Release(ps);
87     IMMDevice_Release(device);
88     if(FAILED(hr))
89         return FALSE;
90
91     return TRUE;
92 }
93
94 static void initAudioDlg (HWND hDlg)
95 {
96     WCHAR display_str[256], format_str[256], disabled_str[64];
97     PROPVARIANT pv;
98
99     WINE_TRACE("\n");
100
101     LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_DRIVER,
102             format_str, sizeof(format_str) / sizeof(*format_str));
103     LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_DRIVER_NONE,
104             disabled_str, sizeof(disabled_str) / sizeof(*disabled_str));
105
106     PropVariantInit(&pv);
107     if(get_driver_name(&pv) && pv.u.pwszVal[0] != '\0')
108         wnsprintfW(display_str, sizeof(display_str) / sizeof(*display_str),
109                 format_str, pv.u.pwszVal);
110     else
111         wnsprintfW(display_str, sizeof(display_str) / sizeof(*display_str),
112                 format_str, disabled_str);
113     PropVariantClear(&pv);
114
115     SetDlgItemTextW(hDlg, IDC_AUDIO_DRIVER, display_str);
116 }
117
118 static void test_sound(void)
119 {
120     BOOL (WINAPI *pPlaySoundW)(const WCHAR *, HMODULE, DWORD);
121     HMODULE winmm;
122
123     static const WCHAR winmmW[] = {'w','i','n','m','m','.','d','l','l',0};
124
125     winmm = LoadLibraryW(winmmW);
126     if(!winmm){
127         WINE_ERR("LoadLibrary failed: %u\n", GetLastError());
128         return;
129     }
130
131     pPlaySoundW = (void*)GetProcAddress(winmm, "PlaySoundW");
132     if(!pPlaySoundW){
133         WINE_ERR("GetProcAddress failed: %u\n", GetLastError());
134         FreeLibrary(winmm);
135         return;
136     }
137
138     if(!pPlaySoundW(MAKEINTRESOURCEW(IDW_TESTSOUND), NULL, SND_RESOURCE | SND_SYNC)){
139         WCHAR error_str[256], title_str[256];
140
141         LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_TEST_FAILED,
142                 error_str, sizeof(error_str) / sizeof(*error_str));
143         LoadStringW(GetModuleHandle(NULL), IDS_AUDIO_TEST_FAILED_TITLE,
144                 title_str, sizeof(title_str) / sizeof(*title_str));
145
146         MessageBoxW(NULL, error_str, title_str, MB_OK | MB_ICONERROR);
147     }
148
149     FreeLibrary(winmm);
150 }
151
152 INT_PTR CALLBACK
153 AudioDlgProc (HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
154 {
155   switch (uMsg) {
156       case WM_COMMAND:
157         switch (LOWORD(wParam)) {
158           case IDC_AUDIO_TEST:
159               test_sound();
160               break;
161         }
162       case WM_SHOWWINDOW:
163         set_window_title(hDlg);
164         break;
165
166       case WM_NOTIFY:
167         switch(((LPNMHDR)lParam)->code) {
168             case PSN_KILLACTIVE:
169               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, FALSE);
170               break;
171             case PSN_APPLY:
172               apply();
173               SetWindowLongPtr(hDlg, DWLP_MSGRESULT, PSNRET_NOERROR);
174               break;
175             case PSN_SETACTIVE:
176               break;
177         }
178         break;
179       case WM_INITDIALOG:
180         initAudioDlg(hDlg);
181         break;
182   }
183
184   return FALSE;
185 }