204a9490a9671dbd6e1631e6511b97279345e2dc
[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_VOL_BTN), FALSE);
236 EnableWindow(GetDlgItem(hwndDlg, IDC_ADVANCED2_BTN), FALSE);
237 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconNoHW);
238 return;
239 }
240
241 if (mixerOpen(&pGlobalData->hMixer, 0, PtrToUlong(hwndDlg), 0, MIXER_OBJECTF_MIXER | CALLBACK_WINDOW) != MMSYSERR_NOERROR)
242 {
243 MessageBox(hwndDlg, _T("Cannot open mixer"), NULL, MB_OK);
244 return;
245 }
246
247 ZeroMemory(&mxc, sizeof(MIXERCAPS));
248 if (mixerGetDevCaps(PtrToUint(pGlobalData->hMixer), &mxc, sizeof(MIXERCAPS)) != MMSYSERR_NOERROR)
249 {
250 MessageBox(hwndDlg, _T("mixerGetDevCaps failed"), NULL, MB_OK);
251 return;
252 }
253
254 GetMuteControl(pGlobalData);
255 GetMuteState(pGlobalData);
256 if (pGlobalData->muteVal)
257 {
258 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_CHECKED, (LPARAM)0);
259 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconMuted);
260 }
261 else
262 {
263 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_UNCHECKED, (LPARAM)0);
264 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconUnMuted);
265 }
266
267 GetVolumeControl(pGlobalData);
268 GetVolumeValue(pGlobalData);
269
270 SendDlgItemMessage(hwndDlg, IDC_DEVICE_NAME, WM_SETTEXT, 0, (LPARAM)mxc.szPname);
271 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETRANGE, (WPARAM)TRUE,
272 (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeMaximum/VOLUME_DIVIDER));
273 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETPAGESIZE, (WPARAM)FALSE, (LPARAM)1);
274 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETSEL, (WPARAM)FALSE,
275 (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeValue/VOLUME_DIVIDER));
276 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)pGlobalData->volumeValue/VOLUME_DIVIDER);
277 }
278
279
280 VOID
281 LaunchSoundControl(HWND hwndDlg)
282 {
283 if ((INT_PTR)ShellExecuteW(NULL, L"open", L"sndvol32.exe", NULL, NULL, SW_SHOWNORMAL) > 32)
284 return;
285 MessageBox(hwndDlg, _T("Cannot run sndvol32.exe"), NULL, MB_OK);
286 }
287
288 /* Volume property page dialog callback */
289 //static INT_PTR CALLBACK
290 INT_PTR CALLBACK
291 VolumeDlgProc(HWND hwndDlg,
292 UINT uMsg,
293 WPARAM wParam,
294 LPARAM lParam)
295 {
296 static IMGINFO ImgInfo;
297 PGLOBAL_DATA pGlobalData;
298 UNREFERENCED_PARAMETER(lParam);
299 UNREFERENCED_PARAMETER(wParam);
300
301 pGlobalData = (PGLOBAL_DATA)GetWindowLongPtr(hwndDlg, DWLP_USER);
302
303
304
305 switch(uMsg)
306 {
307 case MM_MIXM_LINE_CHANGE:
308 {
309 GetMuteState(pGlobalData);
310 if (pGlobalData->muteVal)
311 {
312 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_CHECKED, (LPARAM)0);
313 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconMuted);
314 }
315 else
316 {
317 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_UNCHECKED, (LPARAM)0);
318 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconUnMuted);
319 }
320 break;
321 }
322 case MM_MIXM_CONTROL_CHANGE:
323 {
324 GetVolumeValue(pGlobalData);
325 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETSEL, (WPARAM)FALSE, (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeValue/VOLUME_DIVIDER));
326 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)pGlobalData->volumeValue/VOLUME_DIVIDER);
327 break;
328 }
329 case WM_INITDIALOG:
330 {
331 pGlobalData = (GLOBAL_DATA*) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(GLOBAL_DATA));
332 SetWindowLongPtr(hwndDlg, DWLP_USER, (LONG_PTR)pGlobalData);
333
334 pGlobalData->hIconUnMuted = LoadImage(hApplet, MAKEINTRESOURCE(IDI_CPLICON), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
335 pGlobalData->hIconMuted = LoadImage(hApplet, MAKEINTRESOURCE(IDI_MUTED_ICON), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
336 pGlobalData->hIconNoHW = LoadImage(hApplet, MAKEINTRESOURCE(IDI_NO_HW), IMAGE_ICON, 32, 32, LR_DEFAULTCOLOR);
337
338 InitImageInfo(&ImgInfo);
339 InitVolumeControls(hwndDlg, pGlobalData);
340 break;
341 }
342
343 case WM_DRAWITEM:
344 {
345 LPDRAWITEMSTRUCT lpDrawItem;
346 lpDrawItem = (LPDRAWITEMSTRUCT) lParam;
347 if(lpDrawItem->CtlID == IDC_SPEAKIMG)
348 {
349 HDC hdcMem;
350 LONG left;
351
352 /* Position image in centre of dialog */
353 left = (lpDrawItem->rcItem.right - ImgInfo.cxSource) / 2;
354
355 hdcMem = CreateCompatibleDC(lpDrawItem->hDC);
356 if (hdcMem != NULL)
357 {
358 SelectObject(hdcMem, ImgInfo.hBitmap);
359 BitBlt(lpDrawItem->hDC,
360 left,
361 lpDrawItem->rcItem.top,
362 lpDrawItem->rcItem.right - lpDrawItem->rcItem.left,
363 lpDrawItem->rcItem.bottom - lpDrawItem->rcItem.top,
364 hdcMem,
365 0,
366 0,
367 SRCCOPY);
368 DeleteDC(hdcMem);
369 }
370 }
371 break;
372 }
373
374 case WM_COMMAND:
375 {
376 switch (LOWORD(wParam))
377 {
378 case IDC_MUTE_CHECKBOX:
379 SwitchMuteState(pGlobalData);
380 if (pGlobalData->muteVal)
381 {
382 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_CHECKED, (LPARAM)0);
383 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconMuted);
384 }
385 else
386 {
387 SendDlgItemMessage(hwndDlg, IDC_MUTE_CHECKBOX, BM_SETCHECK, (WPARAM)BST_UNCHECKED, (LPARAM)0);
388 SendDlgItemMessage(hwndDlg, IDC_MUTE_ICON, STM_SETIMAGE, IMAGE_ICON, (LPARAM)pGlobalData->hIconUnMuted);
389 }
390 break;
391 case IDC_ADVANCED_BTN:
392 LaunchSoundControl(hwndDlg);
393 break;
394 }
395 break;
396 }
397
398 case WM_HSCROLL:
399 {
400 HWND hVolumeTrackbar = GetDlgItem(hwndDlg, IDC_VOLUME_TRACKBAR);
401 if (hVolumeTrackbar == (HWND)lParam)
402 {
403 pGlobalData->volumeValue = (DWORD)SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_GETPOS, 0, 0)*VOLUME_DIVIDER;
404 SetVolumeValue(pGlobalData);
405 SendDlgItemMessage(hwndDlg, IDC_VOLUME_TRACKBAR, TBM_SETSEL, (WPARAM)TRUE,
406 (LPARAM)MAKELONG(pGlobalData->volumeMinimum, pGlobalData->volumeValue/VOLUME_DIVIDER));
407 }
408 break;
409 }
410
411 case WM_DESTROY:
412 mixerClose(pGlobalData->hMixer);
413 DestroyIcon(pGlobalData->hIconMuted);
414 DestroyIcon(pGlobalData->hIconUnMuted);
415 DestroyIcon(pGlobalData->hIconNoHW);
416 HeapFree(GetProcessHeap(), 0, pGlobalData);
417 break;
418 }
419
420 return FALSE;
421 }