00bc9b513e3a7906164133d240d2144cf3efe8d8
[reactos.git] / reactos / dll / cpl / mmsys / volume.c
1 /*
2 * PROJECT: ReactOS Multimedia Control Panel
3 * FILE: dll/cpl/mmsys/mmsys.c
4 * PURPOSE: ReactOS Multimedia Control Panel
5 * PROGRAMMER: Thomas Weidenmueller <w3seek@reactos.com>
6 * Johannes Anderwald <janderwald@reactos.com>
7 * Dmitry Chapyshev <dmitry@reactos.org>
8 */
9
10 #include "mmsys.h"
11
12 #include <shellapi.h>
13
14 #define VOLUME_DIVIDER 0xFFF
15
16 typedef struct _IMGINFO
17 {
18 HBITMAP hBitmap;
19 INT cxSource;
20 INT cySource;
21 } IMGINFO, *PIMGINFO;
22
23
24 typedef struct _GLOBAL_DATA
25 {
26 HMIXER hMixer;
27 HICON hIconMuted;
28 HICON hIconUnMuted;
29 HICON hIconNoHW;
30
31 LONG muteVal;
32 DWORD muteControlID;
33
34 DWORD volumeControlID;
35 DWORD volumeMinimum;
36 DWORD volumeMaximum;
37 DWORD volumeValue;
38
39 } GLOBAL_DATA, *PGLOBAL_DATA;
40
41
42 static VOID
43 InitImageInfo(PIMGINFO ImgInfo)
44 {
45 BITMAP bitmap;
46
47 ZeroMemory(ImgInfo, sizeof(*ImgInfo));
48
49 ImgInfo->hBitmap = LoadImage(hApplet,
50 MAKEINTRESOURCE(IDB_SPEAKIMG),
51 IMAGE_BITMAP,
52 0,
53 0,
54 LR_DEFAULTCOLOR);
55
56 if (ImgInfo->hBitmap != NULL)
57 {
58 GetObject(ImgInfo->hBitmap, sizeof(BITMAP), &bitmap);
59
60 ImgInfo->cxSource = bitmap.bmWidth;
61 ImgInfo->cySource = bitmap.bmHeight;
62 }
63 }
64
65
66 VOID
67 GetMuteControl(PGLOBAL_DATA pGlobalData)
68 {
69 MIXERLINE mxln;
70 MIXERCONTROL mxc;
71 MIXERLINECONTROLS mxlctrl;
72
73 if (pGlobalData->hMixer == NULL)
74 return;
75
76 mxln.cbStruct = sizeof(MIXERLINE);
77 mxln.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
78
79 if (mixerGetLineInfo((HMIXEROBJ)pGlobalData->hMixer, &mxln, MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE)
80 != MMSYSERR_NOERROR) return;
81
82 mxlctrl.cbStruct = sizeof(MIXERLINECONTROLS);
83 mxlctrl.dwLineID = mxln.dwLineID;
84 mxlctrl.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
85 mxlctrl.cControls = 1;
86 mxlctrl.cbmxctrl = sizeof(MIXERCONTROL);
87 mxlctrl.pamxctrl = &mxc;
88
89 if (mixerGetLineControls((HMIXEROBJ)pGlobalData->hMixer, &mxlctrl, MIXER_OBJECTF_HMIXER | MIXER_GETLINECONTROLSF_ONEBYTYPE)
90 != MMSYSERR_NOERROR) return;
91
92 pGlobalData->muteControlID = mxc.dwControlID;
93 }
94
95
96 VOID
97 GetMuteState(PGLOBAL_DATA pGlobalData)
98 {
99 MIXERCONTROLDETAILS_BOOLEAN mxcdMute;
100 MIXERCONTROLDETAILS mxcd;
101
102 if (pGlobalData->hMixer == NULL)
103 return;
104
105 mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
106 mxcd.dwControlID = pGlobalData->muteControlID;
107 mxcd.cChannels = 1;
108 mxcd.cMultipleItems = 0;
109 mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
110 mxcd.paDetails = &mxcdMute;
111
112 if (mixerGetControlDetails((HMIXEROBJ)pGlobalData->hMixer, &mxcd, MIXER_OBJECTF_HMIXER | MIXER_GETCONTROLDETAILSF_VALUE)
113 != MMSYSERR_NOERROR)
114 return;
115
116 pGlobalData->muteVal = mxcdMute.fValue;
117 }
118
119
120 VOID
121 SwitchMuteState(PGLOBAL_DATA pGlobalData)
122 {
123 MIXERCONTROLDETAILS_BOOLEAN mxcdMute;
124 MIXERCONTROLDETAILS mxcd;
125
126 mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
127 mxcd.dwControlID = pGlobalData->muteControlID;
128 mxcd.cChannels = 1;
129 mxcd.cMultipleItems = 0;
130 mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_BOOLEAN);
131 mxcd.paDetails = &mxcdMute;
132
133 mxcdMute.fValue = !pGlobalData->muteVal;
134 if (mixerSetControlDetails((HMIXEROBJ)pGlobalData->hMixer, &mxcd, MIXER_OBJECTF_HMIXER | MIXER_SETCONTROLDETAILSF_VALUE)
135 != MMSYSERR_NOERROR)
136 return;
137
138 pGlobalData->muteVal = mxcdMute.fValue;
139 }
140
141
142 VOID
143 GetVolumeControl(PGLOBAL_DATA pGlobalData)
144 {
145 MIXERLINE mxln;
146 MIXERCONTROL mxc;
147 MIXERLINECONTROLS mxlc;
148
149 if (pGlobalData->hMixer == NULL)
150 return;
151
152 mxln.cbStruct = sizeof(MIXERLINE);
153 mxln.dwComponentType = MIXERLINE_COMPONENTTYPE_DST_SPEAKERS;
154 if (mixerGetLineInfo((HMIXEROBJ)pGlobalData->hMixer, &mxln, MIXER_OBJECTF_HMIXER | MIXER_GETLINEINFOF_COMPONENTTYPE)
155 != MMSYSERR_NOERROR)
156 return;
157
158 mxlc.cbStruct = sizeof(MIXERLINECONTROLS);
159 mxlc.dwLineID = mxln.dwLineID;
160 mxlc.dwControlType = MIXERCONTROL_CONTROLTYPE_VOLUME;
161 mxlc.cControls = 1;
162 mxlc.cbmxctrl = sizeof(MIXERCONTROL);
163 mxlc.pamxctrl = &mxc;
164 if (mixerGetLineControls((HMIXEROBJ)pGlobalData->hMixer, &mxlc, MIXER_OBJECTF_HMIXER | MIXER_GETLINECONTROLSF_ONEBYTYPE)
165 != MMSYSERR_NOERROR)
166 return;
167
168 pGlobalData->volumeMinimum = mxc.Bounds.dwMinimum;
169 pGlobalData->volumeMaximum = mxc.Bounds.dwMaximum;
170 pGlobalData->volumeControlID = mxc.dwControlID;
171 }
172
173
174 VOID
175 GetVolumeValue(PGLOBAL_DATA pGlobalData)
176 {
177 MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
178 MIXERCONTROLDETAILS mxcd;
179
180 if (pGlobalData->hMixer == NULL)
181 return;
182
183 mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
184 mxcd.dwControlID = pGlobalData->volumeControlID;
185 mxcd.cChannels = 1;
186 mxcd.cMultipleItems = 0;
187 mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
188 mxcd.paDetails = &mxcdVolume;
189
190 if (mixerGetControlDetails((HMIXEROBJ)pGlobalData->hMixer, &mxcd, MIXER_OBJECTF_HMIXER | MIXER_GETCONTROLDETAILSF_VALUE)
191 != MMSYSERR_NOERROR)
192 return;
193
194 pGlobalData->volumeValue = mxcdVolume.dwValue;
195 }
196
197
198 VOID
199 SetVolumeValue(PGLOBAL_DATA pGlobalData){
200 MIXERCONTROLDETAILS_UNSIGNED mxcdVolume;
201 MIXERCONTROLDETAILS mxcd;
202
203 if (pGlobalData->hMixer == NULL)
204 return;
205
206 mxcdVolume.dwValue = pGlobalData->volumeValue;
207 mxcd.cbStruct = sizeof(MIXERCONTROLDETAILS);
208 mxcd.dwControlID = pGlobalData->volumeControlID;
209 mxcd.cChannels = 1;
210 mxcd.cMultipleItems = 0;
211 mxcd.cbDetails = sizeof(MIXERCONTROLDETAILS_UNSIGNED);
212 mxcd.paDetails = &mxcdVolume;
213
214 if (mixerSetControlDetails((HMIXEROBJ)pGlobalData->hMixer, &mxcd, MIXER_OBJECTF_HMIXER | MIXER_SETCONTROLDETAILSF_VALUE)
215 != MMSYSERR_NOERROR)
216 return;
217
218 pGlobalData->volumeValue = mxcdVolume.dwValue;
219 }
220
221
222 VOID
223 InitVolumeControls(HWND hwndDlg, PGLOBAL_DATA pGlobalData)
224 {
225 UINT NumMixers;
226 MIXERCAPS mxc;
227
228 NumMixers = mixerGetNumDevs();
229 if (!NumMixers)
230 {
231 EnableWindow(GetDlgItem(hwndDlg, IDC_VOLUME_TRACKBAR), FALSE);
232 EnableWindow(GetDlgItem(hwndDlg, IDC_MUTE_CHECKBOX), FALSE);
233 EnableWindow(GetDlgItem(hwndDlg, IDC_ICON_IN_TASKBAR), FALSE);
234 EnableWindow(GetDlgItem(hwndDlg, IDC_ADVANCED_BTN), FALSE);
235 EnableWindow(GetDlgItem(hwndDlg, IDC_SPEAKER_SET_BTN), FALSE);
236 EnableWindow(GetDlgItem(hwndDlg, IDC_SPEAKER_VOL_BTN), FALSE);
237 EnableWindow(GetDlgItem(hwndDlg, IDC_ADVANCED2_BTN), FALSE);
238 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconNoHW);
239 return;
240 }
241
242 if (mixerOpen(&pGlobalData->hMixer, 0, PtrToUlong(hwndDlg), 0, MIXER_OBJECTF_MIXER | CALLBACK_WINDOW) != MMSYSERR_NOERROR)
243 {
244 MessageBox(hwndDlg, _T("Cannot open mixer"), NULL, MB_OK);
245 return;
246 }
247
248 ZeroMemory(&mxc, sizeof(MIXERCAPS));
249 if (mixerGetDevCaps(PtrToUint(pGlobalData->hMixer), &mxc, sizeof(MIXERCAPS)) != MMSYSERR_NOERROR)
250 {
251 MessageBox(hwndDlg, _T("mixerGetDevCaps failed"), NULL, MB_OK);
252 return;
253 }
254
255 GetMuteControl(pGlobalData);
256 GetMuteState(pGlobalData);
257 if (pGlobalData->muteVal)
258 {
259 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_CHECKED, (LPARAM)0);
260 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconMuted);
261 }
262 else
263 {
264 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_UNCHECKED, (LPARAM)0);
265 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconUnMuted);
266 }
267
268 GetVolumeControl(pGlobalData);
269 GetVolumeValue(pGlobalData);
270
271 SendDlgItemMessage(hwndDlg, IDC_DEVICE_NAME, WM_SETTEXT, 0, (LPARAM)mxc.szPname);
272 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETRANGE, (WPARAM)TRUE,
273 (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeMaximum/VOLUME_DIVIDER));
274 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETPAGESIZE, (WPARAM)FALSE, (LPARAM)1);
275 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETSEL, (WPARAM)FALSE,
276 (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeValue/VOLUME_DIVIDER));
277 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)pGlobalData->volumeValue/VOLUME_DIVIDER);
278 }
279
280
281 VOID
282 LaunchSoundControl(HWND hwndDlg)
283 {
284 if ((INT_PTR)ShellExecuteW(NULL, L"open", L"sndvol32.exe", NULL, NULL, SW_SHOWNORMAL) > 32)
285 return;
286 MessageBox(hwndDlg, _T("Cannot run sndvol32.exe"), NULL, MB_OK);
287 }
288
289 /* Volume property page dialog callback */
290 //static INT_PTR CALLBACK
291 INT_PTR CALLBACK
292 VolumeDlgProc(HWND hwndDlg,
293 UINT uMsg,
294 WPARAM wParam,
295 LPARAM lParam)
296 {
297 static IMGINFO ImgInfo;
298 PGLOBAL_DATA pGlobalData;
299 UNREFERENCED_PARAMETER(lParam);
300 UNREFERENCED_PARAMETER(wParam);
301
302 pGlobalData = (PGLOBAL_DATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
303
304
305
306 switch(uMsg)
307 {
308 case MM_MIXM_LINE_CHANGE:
309 {
310 GetMuteState(pGlobalData);
311 if (pGlobalData->muteVal)
312 {
313 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_CHECKED, (LPARAM)0);
314 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconMuted);
315 }
316 else
317 {
318 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_UNCHECKED, (LPARAM)0);
319 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconUnMuted);
320 }
321 break;
322 }
323 case MM_MIXM_CONTROL_CHANGE:
324 {
325 GetVolumeValue(pGlobalData);
326 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETSEL, (WPARAM)FALSE, (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeValue/VOLUME_DIVIDER));
327 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)pGlobalData->volumeValue/VOLUME_DIVIDER);
328 break;
329 }
330 case WM_INITDIALOG:
331 {
332 pGlobalData = (GLOBAL_DATA*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(GLOBAL_DATA));
333 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pGlobalData);
334
335 pGlobalData->hIconUnMuted = LoadImage(hApplet, MAKEINTRESOURCE(IDI_CPLICON), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
336 pGlobalData->hIconMuted = LoadImage(hApplet, MAKEINTRESOURCE(IDI_MUTED_ICON), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
337 pGlobalData->hIconNoHW = LoadImage(hApplet, MAKEINTRESOURCE(IDI_NO_HW), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
338
339 InitImageInfo(&ImgInfo);
340 InitVolumeControls(hwndDlg, pGlobalData);
341 break;
342 }
343
344 case WM_DRAWITEM:
345 {
346 LPDRAWITEMSTRUCT lpDrawItem;
347 lpDrawItem = (LPDRAWITEMSTRUCT) lParam;
348 if(lpDrawItem->CtlID == IDC_SPEAKIMG)
349 {
350 HDC hdcMem;
351 LONG left;
352
353 /* Position image in centre of dialog */
354 left = (lpDrawItem->rcItem.right - ImgInfo.cxSource) / 2;
355
356 hdcMem = CreateCompatibleDC(lpDrawItem->hDC);
357 if (hdcMem != NULL)
358 {
359 SelectObject(hdcMem, ImgInfo.hBitmap);
360 BitBlt(lpDrawItem->hDC,
361 left,
362 lpDrawItem->rcItem.top,
363 lpDrawItem->rcItem.right - lpDrawItem->rcItem.left,
364 lpDrawItem->rcItem.bottom - lpDrawItem->rcItem.top,
365 hdcMem,
366 0,
367 0,
368 SRCCOPY);
369 DeleteDC(hdcMem);
370 }
371 }
372 break;
373 }
374
375 case WM_COMMAND:
376 {
377 switch (LOWORD(wParam))
378 {
379 case IDC_MUTE_CHECKBOX:
380 SwitchMuteState(pGlobalData);
381 if (pGlobalData->muteVal)
382 {
383 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_CHECKED, (LPARAM)0);
384 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconMuted);
385 }
386 else
387 {
388 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_UNCHECKED, (LPARAM)0);
389 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconUnMuted);
390 }
391 break;
392 case IDC_ADVANCED_BTN:
393 LaunchSoundControl(hwndDlg);
394 break;
395 }
396 break;
397 }
398
399 case WM_HSCROLL:
400 {
401 HWND hVolumeTrackbar = GetDlgItem(hwndDlg, IDC_VOLUME_TRACKBAR);
402 if (hVolumeTrackbar == (HWND)lParam)
403 {
404 pGlobalData->volumeValue = (DWORD)SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_GETPOS, 0, 0)*VOLUME_DIVIDER;
405 SetVolumeValue(pGlobalData);
406 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETSEL, (WPARAM)TRUE,
407 (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeValue/VOLUME_DIVIDER));
408 }
409 break;
410 }
411
412 case WM_DESTROY:
413 mixerClose(pGlobalData->hMixer);
414 DestroyIcon(pGlobalData->hIconMuted);
415 DestroyIcon(pGlobalData->hIconUnMuted);
416 DestroyIcon(pGlobalData->hIconNoHW);
417 HeapFree(GetProcessHeap(), 0, pGlobalData);
418 break;
419 }
420
421 return FALSE;
422 }