[STOBJECT]
[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\win32\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 TRACE("Volume_Update\n");
175
176 Volume_IsMute();
177
178 WCHAR strTooltip[128];
179 HICON icon;
180 if (g_IsMute) {
181 icon = g_hIconMute;
182 LoadStringW(g_hInstance, IDS_VOL_MUTED, strTooltip, _countof(strTooltip));
183 }
184 else {
185 icon = g_hIconVolume;
186 LoadStringW(g_hInstance, IDS_VOL_VOLUME, strTooltip, _countof(strTooltip));
187 }
188
189 return pSysTray->NotifyIcon(NIM_MODIFY, ID_ICON_VOLUME, icon, strTooltip);
190 }
191
192 HRESULT STDMETHODCALLTYPE Volume_Shutdown(_In_ CSysTray * pSysTray)
193 {
194 TRACE("Volume_Shutdown\n");
195
196 return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_VOLUME, NULL, NULL);
197 }
198
199 HRESULT Volume_OnDeviceChange(_In_ CSysTray * pSysTray, WPARAM wParam, LPARAM lParam)
200 {
201 return Volume_FindMixerControl(pSysTray);
202 }
203
204 static void _RunVolume()
205 {
206 // FIXME: ensure we are loading the right one
207 ShellExecuteW(NULL, NULL, L"sndvol32.exe", NULL, NULL, SW_SHOWNORMAL);
208 }
209
210 static void _RunMMCpl()
211 {
212 ShellExecuteW(NULL, NULL, L"mmsys.cpl", NULL, NULL, SW_NORMAL);
213 }
214
215 static void _ShowContextMenu(CSysTray * pSysTray)
216 {
217 WCHAR strAdjust[128];
218 WCHAR strOpen[128];
219 LoadStringW(g_hInstance, IDS_VOL_OPEN, strOpen, _countof(strOpen));
220 LoadStringW(g_hInstance, IDS_VOL_ADJUST, strAdjust, _countof(strAdjust));
221
222 HMENU hPopup = CreatePopupMenu();
223 AppendMenuW(hPopup, MF_STRING, IDS_VOL_OPEN, strOpen);
224 AppendMenuW(hPopup, MF_STRING, IDS_VOL_ADJUST, strAdjust);
225
226 DWORD flags = TPM_RETURNCMD | TPM_NONOTIFY | TPM_RIGHTALIGN | TPM_BOTTOMALIGN;
227 DWORD msgPos = GetMessagePos();
228
229 SetForegroundWindow(pSysTray->GetHWnd());
230 DWORD id = TrackPopupMenuEx(hPopup, flags,
231 GET_X_LPARAM(msgPos), GET_Y_LPARAM(msgPos),
232 pSysTray->GetHWnd(), NULL);
233
234 DestroyMenu(hPopup);
235
236 switch (id)
237 {
238 case IDS_VOL_OPEN:
239 _RunVolume();
240 break;
241 case IDS_VOL_ADJUST:
242 _RunMMCpl();
243 break;
244 }
245 }
246
247 HRESULT STDMETHODCALLTYPE Volume_Message(_In_ CSysTray * pSysTray, UINT uMsg, WPARAM wParam, LPARAM lParam)
248 {
249 if (uMsg == g_mmDeviceChange)
250 return Volume_OnDeviceChange(pSysTray, wParam, lParam);
251
252 if (uMsg != ID_ICON_VOLUME)
253 {
254 TRACE("Volume_Message received for unknown ID %d, ignoring.\n");
255 return S_FALSE;
256 }
257
258 TRACE("Volume_Message uMsg=%d, w=%x, l=%x\n", uMsg, wParam, lParam);
259
260 Volume_Update(pSysTray);
261
262 switch (lParam)
263 {
264 case WM_LBUTTONDOWN:
265 break;
266 case WM_LBUTTONUP:
267 TRACE("TODO: display volume slider\n");
268 break;
269 case WM_LBUTTONDBLCLK:
270 _RunVolume();
271 break;
272 case WM_RBUTTONDOWN:
273 break;
274 case WM_RBUTTONUP:
275 _ShowContextMenu(pSysTray);
276 case WM_RBUTTONDBLCLK:
277 break;
278 case WM_MOUSEMOVE:
279 break;
280 }
281
282 return S_OK;
283 }