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