1c5b73ae25e30309113d94fb7abb579e082e5dc7
[reactos.git] / reactos / dll / shellext / stobject / volume.cpp
1 /*
2 * PROJECT: ReactOS system libraries
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: dll/shellext/stobject/volume.cpp
5 * PURPOSE: Volume notification icon handler
6 * PROGRAMMERS: David Quintana <gigaherz@gmail.com>
7 */
8
9 #include "precomp.h"
10
11 #include <mmsystem.h>
12 #include <mmddk.h>
13
14 #define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp))
15 #define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp))
16
17 WINE_DEFAULT_DEBUG_CHANNEL(stobject);
18
19 HICON g_hIconVolume;
20 HICON g_hIconMute;
21
22 HMIXER g_hMixer;
23 UINT g_mixerId;
24 DWORD g_mixerLineID;
25 DWORD g_muteControlID;
26
27 UINT g_mmDeviceChange;
28
29 BOOL g_IsMute = FALSE;
30
31 static HRESULT __stdcall Volume_FindMixerControl(CSysTray * pSysTray)
32 {
33 MMRESULT result;
34 UINT mixerId = 0;
35 DWORD waveOutId = 0;
36 DWORD param2 = 0;
37
38 TRACE("Volume_FindDefaultMixerID\n");
39
40 result = waveOutMessage((HWAVEOUT)WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, (DWORD_PTR)&waveOutId, (DWORD_PTR)&param2);
41 if (result)
42 return E_FAIL;
43
44 if (waveOutId == (DWORD)-1)
45 {
46 TRACE("WARNING: waveOut has no default device, trying with first available device...\n", waveOutId);
47
48 mixerId = 0;
49 }
50 else
51 {
52 TRACE("waveOut default device is %d\n", waveOutId);
53
54 result = mixerGetID((HMIXEROBJ)waveOutId, &mixerId, MIXER_OBJECTF_WAVEOUT);
55 if (result)
56 return E_FAIL;
57
58 TRACE("mixerId for waveOut default device is %d\n", mixerId);
59 }
60
61 g_mixerId = mixerId;
62 return S_OK;
63
64 MIXERCAPS mixerCaps;
65 MIXERLINE mixerLine;
66 MIXERCONTROL mixerControl;
67 MIXERLINECONTROLS mixerLineControls;
68
69 g_mixerLineID = -1;
70 g_muteControlID = -1;
71
72 if (mixerGetDevCapsW(g_mixerId, &mixerCaps, sizeof(mixerCaps)))
73 return E_FAIL;
74
75 if (mixerCaps.cDestinations == 0)
76 return S_FALSE;
77
78 TRACE("mixerCaps.cDestinations %d\n", mixerCaps.cDestinations);
79
80 DWORD idx;
81 for (idx = 0; idx < mixerCaps.cDestinations; idx++)
82 {
83 mixerLine.cbStruct = sizeof(mixerLine);
84 mixerLine.dwDestination = idx;
85 if (!mixerGetLineInfoW((HMIXEROBJ)g_mixerId, &mixerLine, 0))
86 {
87 if (mixerLine.dwComponentType >= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS &&
88 mixerLine.dwComponentType <= MIXERLINE_COMPONENTTYPE_DST_HEADPHONES)
89 break;
90 TRACE("Destination %d was not speakers or headphones.\n");
91 }
92 }
93
94 if (idx >= mixerCaps.cDestinations)
95 return E_FAIL;
96
97 TRACE("Valid destination %d found.\n");
98
99 g_mixerLineID = mixerLine.dwLineID;
100
101 mixerLineControls.cbStruct = sizeof(mixerLineControls);
102 mixerLineControls.dwLineID = mixerLine.dwLineID;
103 mixerLineControls.cControls = 1;
104 mixerLineControls.dwControlType = MIXERCONTROL_CONTROLTYPE_MUTE;
105 mixerLineControls.pamxctrl = &mixerControl;
106 mixerLineControls.cbmxctrl = sizeof(mixerControl);
107
108 if (mixerGetLineControlsW((HMIXEROBJ)g_mixerId, &mixerLineControls, MIXER_GETLINECONTROLSF_ONEBYTYPE))
109 return E_FAIL;
110
111 TRACE("Found control id %d for mute: %d\n", mixerControl.dwControlID);
112
113 g_muteControlID = mixerControl.dwControlID;
114
115 return S_OK;
116 }
117
118 HRESULT Volume_IsMute()
119 {
120 #if 0
121 MIXERCONTROLDETAILS mixerControlDetails;
122
123 if (g_mixerId != (UINT)-1 && g_muteControlID != (DWORD)-1)
124 {
125 BOOL detailsResult = 0;
126 mixerControlDetails.cbStruct = sizeof(mixerControlDetails);
127 mixerControlDetails.hwndOwner = 0;
128 mixerControlDetails.dwControlID = g_muteControlID;
129 mixerControlDetails.cChannels = 1;
130 mixerControlDetails.paDetails = &detailsResult;
131 mixerControlDetails.cbDetails = sizeof(detailsResult);
132 if (mixerGetControlDetailsW((HMIXEROBJ) g_mixerId, &mixerControlDetails, 0))
133 return E_FAIL;
134
135 TRACE("Obtained mute status %d\n", detailsResult);
136
137 g_IsMute = detailsResult != 0;
138 }
139 #endif
140 return S_OK;
141 }
142
143 HRESULT STDMETHODCALLTYPE Volume_Init(_In_ CSysTray * pSysTray)
144 {
145 HRESULT hr;
146
147 TRACE("Volume_Init\n");
148
149 if (!g_hMixer)
150 {
151 hr = Volume_FindMixerControl(pSysTray);
152 if (FAILED(hr))
153 return hr;
154
155 g_mmDeviceChange = RegisterWindowMessageW(L"winmm_devicechange");
156 }
157
158 g_hIconVolume = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_VOLUME));
159 g_hIconMute = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_VOLMUTE));
160
161 Volume_IsMute();
162
163 HICON icon;
164 if (g_IsMute)
165 icon = g_hIconMute;
166 else
167 icon = g_hIconVolume;
168
169 return pSysTray->NotifyIcon(NIM_ADD, ID_ICON_VOLUME, icon, L"Volume Control");
170 }
171
172 HRESULT STDMETHODCALLTYPE Volume_Update(_In_ CSysTray * pSysTray)
173 {
174 BOOL PrevState;
175
176 TRACE("Volume_Update\n");
177
178 PrevState = g_IsMute;
179 Volume_IsMute();
180
181 if (PrevState != g_IsMute)
182 {
183 WCHAR strTooltip[128];
184 HICON icon;
185 if (g_IsMute) {
186 icon = g_hIconMute;
187 LoadStringW(g_hInstance, IDS_VOL_MUTED, strTooltip, _countof(strTooltip));
188 }
189 else {
190 icon = g_hIconVolume;
191 LoadStringW(g_hInstance, IDS_VOL_VOLUME, strTooltip, _countof(strTooltip));
192 }
193
194 return pSysTray->NotifyIcon(NIM_MODIFY, ID_ICON_VOLUME, icon, strTooltip);
195 }
196 else
197 {
198 return S_OK;
199 }
200 }
201
202 HRESULT STDMETHODCALLTYPE Volume_Shutdown(_In_ CSysTray * pSysTray)
203 {
204 TRACE("Volume_Shutdown\n");
205
206 return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_VOLUME, NULL, NULL);
207 }
208
209 HRESULT Volume_OnDeviceChange(_In_ CSysTray * pSysTray, WPARAM wParam, LPARAM lParam)
210 {
211 return Volume_FindMixerControl(pSysTray);
212 }
213
214 static void _RunVolume()
215 {
216 // FIXME: ensure we are loading the right one
217 ShellExecuteW(NULL, NULL, L"sndvol32.exe", NULL, NULL, SW_SHOWNORMAL);
218 }
219
220 static void _RunMMCpl()
221 {
222 ShellExecuteW(NULL, NULL, L"mmsys.cpl", NULL, NULL, SW_NORMAL);
223 }
224
225 static void _ShowContextMenu(CSysTray * pSysTray)
226 {
227 WCHAR strAdjust[128];
228 WCHAR strOpen[128];
229 LoadStringW(g_hInstance, IDS_VOL_OPEN, strOpen, _countof(strOpen));
230 LoadStringW(g_hInstance, IDS_VOL_ADJUST, strAdjust, _countof(strAdjust));
231
232 HMENU hPopup = CreatePopupMenu();
233 AppendMenuW(hPopup, MF_STRING, IDS_VOL_OPEN, strOpen);
234 AppendMenuW(hPopup, MF_STRING, IDS_VOL_ADJUST, strAdjust);
235
236 DWORD flags = TPM_RETURNCMD | TPM_NONOTIFY | TPM_RIGHTALIGN | TPM_BOTTOMALIGN;
237 DWORD msgPos = GetMessagePos();
238
239 SetForegroundWindow(pSysTray->GetHWnd());
240 DWORD id = TrackPopupMenuEx(hPopup, flags,
241 GET_X_LPARAM(msgPos), GET_Y_LPARAM(msgPos),
242 pSysTray->GetHWnd(), NULL);
243
244 DestroyMenu(hPopup);
245
246 switch (id)
247 {
248 case IDS_VOL_OPEN:
249 _RunVolume();
250 break;
251 case IDS_VOL_ADJUST:
252 _RunMMCpl();
253 break;
254 }
255 }
256
257 HRESULT STDMETHODCALLTYPE Volume_Message(_In_ CSysTray * pSysTray, UINT uMsg, WPARAM wParam, LPARAM lParam)
258 {
259 if (uMsg == g_mmDeviceChange)
260 return Volume_OnDeviceChange(pSysTray, wParam, lParam);
261
262 if (uMsg != ID_ICON_VOLUME)
263 {
264 TRACE("Volume_Message received for unknown ID %d, ignoring.\n");
265 return S_FALSE;
266 }
267
268 TRACE("Volume_Message uMsg=%d, w=%x, l=%x\n", uMsg, wParam, lParam);
269
270 Volume_Update(pSysTray);
271
272 switch (lParam)
273 {
274 case WM_LBUTTONDOWN:
275 break;
276 case WM_LBUTTONUP:
277 TRACE("TODO: display volume slider\n");
278 break;
279 case WM_LBUTTONDBLCLK:
280 _RunVolume();
281 break;
282 case WM_RBUTTONDOWN:
283 break;
284 case WM_RBUTTONUP:
285 _ShowContextMenu(pSysTray);
286 case WM_RBUTTONDBLCLK:
287 break;
288 case WM_MOUSEMOVE:
289 break;
290 }
291
292 return S_OK;
293 }