ReactOS 0.4.15-dev-7093-g01a8288
volume.cpp
Go to the documentation of this file.
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 <mmddk.h>
12
15
16HMIXER g_hMixer;
20
22
24
26{
28 UINT mixerId = 0;
29 DWORD waveOutId = 0;
30 DWORD param2 = 0;
31
32 TRACE("Volume_FindDefaultMixerID\n");
33
35 if (result)
36 return E_FAIL;
37
38 if (waveOutId == (DWORD)-1)
39 {
40 TRACE("WARNING: waveOut has no default device, trying with first available device...\n", waveOutId);
41
42 mixerId = 0;
43 }
44 else
45 {
46 TRACE("waveOut default device is %d\n", waveOutId);
47
48 result = mixerGetID((HMIXEROBJ)UlongToHandle(waveOutId), &mixerId, MIXER_OBJECTF_WAVEOUT);
49 if (result)
50 return E_FAIL;
51
52 TRACE("mixerId for waveOut default device is %d\n", mixerId);
53 }
54
55 g_mixerId = mixerId;
56 return S_OK;
57
58 MIXERCAPS mixerCaps;
59 MIXERLINE mixerLine;
60 MIXERCONTROL mixerControl;
61 MIXERLINECONTROLS mixerLineControls;
62
63 g_mixerLineID = -1;
64 g_muteControlID = -1;
65
66 if (mixerGetDevCapsW(g_mixerId, &mixerCaps, sizeof(mixerCaps)))
67 return E_FAIL;
68
69 if (mixerCaps.cDestinations == 0)
70 return S_FALSE;
71
72 TRACE("mixerCaps.cDestinations %d\n", mixerCaps.cDestinations);
73
74 DWORD idx;
75 for (idx = 0; idx < mixerCaps.cDestinations; idx++)
76 {
77 mixerLine.cbStruct = sizeof(mixerLine);
78 mixerLine.dwDestination = idx;
79 if (!mixerGetLineInfoW((HMIXEROBJ)UlongToHandle(g_mixerId), &mixerLine, 0))
80 {
83 break;
84 TRACE("Destination %d was not speakers or headphones.\n");
85 }
86 }
87
88 if (idx >= mixerCaps.cDestinations)
89 return E_FAIL;
90
91 TRACE("Valid destination %d found.\n");
92
93 g_mixerLineID = mixerLine.dwLineID;
94
95 mixerLineControls.cbStruct = sizeof(mixerLineControls);
96 mixerLineControls.dwLineID = mixerLine.dwLineID;
97 mixerLineControls.cControls = 1;
99 mixerLineControls.pamxctrl = &mixerControl;
100 mixerLineControls.cbmxctrl = sizeof(mixerControl);
101
103 return E_FAIL;
104
105 TRACE("Found control id %d for mute: %d\n", mixerControl.dwControlID);
106
107 g_muteControlID = mixerControl.dwControlID;
108
109 return S_OK;
110}
111
113{
114 MIXERCONTROLDETAILS mixerControlDetails;
115
116 if (g_mixerId != (UINT)-1 && g_muteControlID != (DWORD)-1)
117 {
118 BOOL detailsResult = 0;
119 mixerControlDetails.cbStruct = sizeof(mixerControlDetails);
120 mixerControlDetails.hwndOwner = 0;
121 mixerControlDetails.dwControlID = g_muteControlID;
122 mixerControlDetails.cChannels = 1;
123 mixerControlDetails.paDetails = &detailsResult;
124 mixerControlDetails.cbDetails = sizeof(detailsResult);
125 if (mixerGetControlDetailsW((HMIXEROBJ)UlongToHandle(g_mixerId), &mixerControlDetails, 0))
126 return E_FAIL;
127
128 TRACE("Obtained mute status %d\n", detailsResult);
129
130 g_IsMute = detailsResult != 0;
131 }
132
133 return S_OK;
134}
135
137{
138 HRESULT hr;
139 WCHAR strTooltip[128];
140
141 TRACE("Volume_Init\n");
142
143 if (!g_hMixer)
144 {
145 hr = Volume_FindMixerControl(pSysTray);
146 if (FAILED(hr))
147 return hr;
148
149 g_mmDeviceChange = RegisterWindowMessageW(L"winmm_devicechange");
150 }
151
154
156
157 HICON icon;
158 if (g_IsMute)
159 icon = g_hIconMute;
160 else
161 icon = g_hIconVolume;
162
163 LoadStringW(g_hInstance, IDS_VOL_VOLUME, strTooltip, _countof(strTooltip));
164 return pSysTray->NotifyIcon(NIM_ADD, ID_ICON_VOLUME, icon, strTooltip);
165}
166
168{
169 BOOL PrevState;
170
171 TRACE("Volume_Update\n");
172
173 PrevState = g_IsMute;
175
176 if (PrevState != g_IsMute)
177 {
178 WCHAR strTooltip[128];
179 HICON icon;
180 if (g_IsMute)
181 {
182 icon = g_hIconMute;
183 LoadStringW(g_hInstance, IDS_VOL_MUTED, strTooltip, _countof(strTooltip));
184 }
185 else
186 {
187 icon = g_hIconVolume;
188 LoadStringW(g_hInstance, IDS_VOL_VOLUME, strTooltip, _countof(strTooltip));
189 }
190
191 return pSysTray->NotifyIcon(NIM_MODIFY, ID_ICON_VOLUME, icon, strTooltip);
192 }
193 else
194 {
195 return S_OK;
196 }
197}
198
200{
201 TRACE("Volume_Shutdown\n");
202
203 return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_VOLUME, NULL, NULL);
204}
205
207{
208 return Volume_FindMixerControl(pSysTray);
209}
210
211static void _RunVolume(BOOL bTray)
212{
214 NULL,
215 L"sndvol32.exe",
216 bTray ? L"/t" : NULL,
217 NULL,
219}
220
221static void _RunMMCpl()
222{
223 ShellExecuteW(NULL, NULL, L"mmsys.cpl", NULL, NULL, SW_NORMAL);
224}
225
226static void _ShowContextMenu(CSysTray * pSysTray)
227{
228 WCHAR strAdjust[128];
229 WCHAR strOpen[128];
230 LoadStringW(g_hInstance, IDS_VOL_OPEN, strOpen, _countof(strOpen));
231 LoadStringW(g_hInstance, IDS_VOL_ADJUST, strAdjust, _countof(strAdjust));
232
233 HMENU hPopup = CreatePopupMenu();
234 AppendMenuW(hPopup, MF_STRING, IDS_VOL_OPEN, strOpen);
235 AppendMenuW(hPopup, MF_STRING, IDS_VOL_ADJUST, strAdjust);
237
239 POINT pt;
240 SetForegroundWindow(pSysTray->GetHWnd());
242
243 DWORD id = TrackPopupMenuEx(hPopup, flags,
244 pt.x, pt.y,
245 pSysTray->GetHWnd(), NULL);
246
247 DestroyMenu(hPopup);
248
249 switch (id)
250 {
251 case IDS_VOL_OPEN:
253 break;
254 case IDS_VOL_ADJUST:
255 _RunMMCpl();
256 break;
257 }
258}
259
261{
262 if (uMsg == g_mmDeviceChange)
263 return Volume_OnDeviceChange(pSysTray, wParam, lParam);
264
265 switch (uMsg)
266 {
267 case WM_USER + 220:
268 TRACE("Volume_Message: WM_USER+220\n");
270 {
271 if (lParam)
272 {
273 pSysTray->EnableService(VOLUME_SERVICE_FLAG, TRUE);
274 return Volume_Init(pSysTray);
275 }
276 else
277 {
278 pSysTray->EnableService(VOLUME_SERVICE_FLAG, FALSE);
279 return Volume_Shutdown(pSysTray);
280 }
281 }
282 return S_FALSE;
283
284 case WM_USER + 221:
285 TRACE("Volume_Message: WM_USER+221\n");
287 {
288 lResult = (LRESULT)pSysTray->IsServiceEnabled(VOLUME_SERVICE_FLAG);
289 return S_OK;
290 }
291 return S_FALSE;
292
293 case WM_TIMER:
294 if (wParam == VOLUME_TIMER_ID)
295 {
296 KillTimer(pSysTray->GetHWnd(), VOLUME_TIMER_ID);
298 }
299 break;
300
301 case ID_ICON_VOLUME:
302 TRACE("Volume_Message uMsg=%d, w=%x, l=%x\n", uMsg, wParam, lParam);
303
304 Volume_Update(pSysTray);
305
306 switch (lParam)
307 {
308 case WM_LBUTTONDOWN:
309 SetTimer(pSysTray->GetHWnd(), VOLUME_TIMER_ID, GetDoubleClickTime(), NULL);
310 break;
311
312 case WM_LBUTTONUP:
313 break;
314
315 case WM_LBUTTONDBLCLK:
316 KillTimer(pSysTray->GetHWnd(), VOLUME_TIMER_ID);
318 break;
319
320 case WM_RBUTTONDOWN:
321 break;
322
323 case WM_RBUTTONUP:
324 _ShowContextMenu(pSysTray);
325 break;
326
327 case WM_RBUTTONDBLCLK:
328 break;
329
330 case WM_MOUSEMOVE:
331 break;
332 }
333 return S_OK;
334
335 default:
336 TRACE("Volume_Message received for unknown ID %d, ignoring.\n");
337 return S_FALSE;
338 }
339
340 return S_FALSE;
341}
HINSTANCE g_hInstance
Definition: MainWindow.cpp:18
#define UlongToHandle(ul)
Definition: basetsd.h:97
#define STDMETHODCALLTYPE
Definition: bdasup.h:9
HWND GetHWnd()
Definition: csystray.h:54
WPARAM wParam
Definition: combotst.c:138
LPARAM lParam
Definition: combotst.c:139
#define E_FAIL
Definition: ddrawi.h:102
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
unsigned int idx
Definition: utils.c:41
#define ID_ICON_VOLUME
Definition: precomp.h:38
#define VOLUME_TIMER_ID
Definition: precomp.h:84
#define VOLUME_SERVICE_FLAG
Definition: precomp.h:40
#define IDS_VOL_VOLUME
Definition: resource.h:32
#define IDS_VOL_ADJUST
Definition: resource.h:33
#define IDS_VOL_OPEN
Definition: resource.h:34
#define IDI_VOLMUTE
Definition: resource.h:30
#define IDI_VOLUME
Definition: resource.h:29
#define IDS_VOL_MUTED
Definition: resource.h:36
#define pt(x, y)
Definition: drawing.c:79
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLbitfield flags
Definition: glext.h:7161
GLuint64EXT * result
Definition: glext.h:11304
#define S_OK
Definition: intsafe.h:52
#define FAILED(hr)
Definition: intsafe.h:51
#define DRVM_MAPPER_PREFERRED_GET
Definition: mmsys.h:39
#define MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
Definition: mmsystem.h:319
UINT MMRESULT
Definition: mmsystem.h:962
#define MIXER_OBJECTF_WAVEOUT
Definition: mmsystem.h:302
#define MIXERLINE_COMPONENTTYPE_DST_HEADPHONES
Definition: mmsystem.h:320
#define WAVE_MAPPER
Definition: mmsystem.h:187
#define MIXER_GETLINECONTROLSF_ONEBYTYPE
Definition: mmsystem.h:410
#define MIXERCONTROL_CONTROLTYPE_MUTE
Definition: mmsystem.h:384
static HICON
Definition: imagelist.c:84
static const CLSID *static CLSID *static const GUID VARIANT VARIANT *static IServiceProvider DWORD *static HMENU
Definition: ordinal.c:63
#define _In_
Definition: ms_sal.h:308
unsigned int UINT
Definition: ndis.h:50
#define L(x)
Definition: ntvdm.h:50
#define LRESULT
Definition: ole.h:14
#define NIM_DELETE
Definition: shellapi.h:93
#define NIM_MODIFY
Definition: shellapi.h:92
#define NIM_ADD
Definition: shellapi.h:91
HINSTANCE WINAPI ShellExecuteW(HWND hwnd, LPCWSTR lpVerb, LPCWSTR lpFile, LPCWSTR lpParameters, LPCWSTR lpDirectory, INT nShowCmd)
Definition: shlexec.cpp:2379
HRESULT hr
Definition: shlfolder.c:183
#define _countof(array)
Definition: sndvol32.h:68
#define TRACE(s)
Definition: solgame.cpp:4
DWORD cDestinations
Definition: mmsystem.h:1219
DWORD dwDestination
Definition: mmsystem.h:1233
DWORD dwComponentType
Definition: mmsystem.h:1238
DWORD dwLineID
Definition: mmsystem.h:1235
DWORD cbStruct
Definition: mmsystem.h:1232
LPMIXERCONTROLA pamxctrl
Definition: mmsystem.h:1338
uint32_t DWORD_PTR
Definition: typedefs.h:65
#define __stdcall
Definition: typedefs.h:25
DWORD g_muteControlID
Definition: volume.cpp:19
HRESULT Volume_IsMute()
Definition: volume.cpp:112
static BOOL g_IsMute
Definition: volume.cpp:23
HRESULT STDMETHODCALLTYPE Volume_Init(_In_ CSysTray *pSysTray)
Definition: volume.cpp:136
UINT g_mmDeviceChange
Definition: volume.cpp:21
HRESULT STDMETHODCALLTYPE Volume_Message(_In_ CSysTray *pSysTray, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult)
Definition: volume.cpp:260
HRESULT Volume_OnDeviceChange(_In_ CSysTray *pSysTray, WPARAM wParam, LPARAM lParam)
Definition: volume.cpp:206
static void _RunVolume(BOOL bTray)
Definition: volume.cpp:211
DWORD g_mixerLineID
Definition: volume.cpp:18
HICON g_hIconVolume
Definition: volume.cpp:13
static HRESULT __stdcall Volume_FindMixerControl(CSysTray *pSysTray)
Definition: volume.cpp:25
UINT g_mixerId
Definition: volume.cpp:17
HMIXER g_hMixer
Definition: volume.cpp:16
HRESULT STDMETHODCALLTYPE Volume_Shutdown(_In_ CSysTray *pSysTray)
Definition: volume.cpp:199
static void _RunMMCpl()
Definition: volume.cpp:221
static void _ShowContextMenu(CSysTray *pSysTray)
Definition: volume.cpp:226
HRESULT STDMETHODCALLTYPE Volume_Update(_In_ CSysTray *pSysTray)
Definition: volume.cpp:167
HICON g_hIconMute
Definition: volume.cpp:14
LONG_PTR LPARAM
Definition: windef.h:208
LONG_PTR LRESULT
Definition: windef.h:209
UINT_PTR WPARAM
Definition: windef.h:207
#define S_FALSE
Definition: winerror.h:2357
UINT WINAPI mixerGetLineControlsW(HMIXEROBJ hmix, LPMIXERLINECONTROLSW lpmlcW, DWORD fdwControls)
Definition: winmm.c:571
UINT WINAPI mixerGetDevCapsW(UINT_PTR uDeviceID, LPMIXERCAPSW lpCaps, UINT uSize)
Definition: winmm.c:314
UINT WINAPI mixerGetControlDetailsW(HMIXEROBJ hmix, LPMIXERCONTROLDETAILS lpmcdW, DWORD fdwDetails)
Definition: winmm.c:423
UINT WINAPI mixerGetLineInfoW(HMIXEROBJ hmix, LPMIXERLINEW lpmliW, DWORD fdwInfo)
Definition: winmm.c:592
UINT WINAPI mixerGetID(HMIXEROBJ hmix, LPUINT lpid, DWORD fdwID)
Definition: winmm.c:404
UINT WINAPI waveOutMessage(HWAVEOUT hWaveOut, UINT uMessage, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
Definition: winmm.c:2538
#define SW_SHOWNORMAL
Definition: winuser.h:769
HMENU WINAPI CreatePopupMenu(void)
Definition: menu.c:838
BOOL WINAPI SetMenuDefaultItem(_In_ HMENU, _In_ UINT, _In_ UINT)
int WINAPI LoadStringW(_In_opt_ HINSTANCE hInstance, _In_ UINT uID, _Out_writes_to_(cchBufferMax, return+1) LPWSTR lpBuffer, _In_ int cchBufferMax)
#define WM_LBUTTONDBLCLK
Definition: winuser.h:1777
#define TPM_BOTTOMALIGN
Definition: winuser.h:2384
#define MF_STRING
Definition: winuser.h:138
BOOL WINAPI SetForegroundWindow(_In_ HWND)
BOOL WINAPI GetCursorPos(_Out_ LPPOINT)
Definition: cursoricon.c:2639
#define WM_RBUTTONUP
Definition: winuser.h:1779
#define WM_RBUTTONDBLCLK
Definition: winuser.h:1780
#define WM_MOUSEMOVE
Definition: winuser.h:1774
#define TPM_RIGHTALIGN
Definition: winuser.h:2377
#define TPM_NONOTIFY
Definition: winuser.h:2385
#define WM_LBUTTONDOWN
Definition: winuser.h:1775
BOOL WINAPI TrackPopupMenuEx(_In_ HMENU, _In_ UINT, _In_ int, _In_ int, _In_ HWND, _In_opt_ LPTPMPARAMS)
UINT WINAPI GetDoubleClickTime(void)
Definition: ntwrapper.h:314
UINT_PTR WINAPI SetTimer(_In_opt_ HWND, _In_ UINT_PTR, _In_ UINT, _In_opt_ TIMERPROC)
#define WM_RBUTTONDOWN
Definition: winuser.h:1778
#define WM_TIMER
Definition: winuser.h:1741
#define LoadIcon
Definition: winuser.h:5812
UINT WINAPI RegisterWindowMessageW(_In_ LPCWSTR)
#define WM_LBUTTONUP
Definition: winuser.h:1776
BOOL WINAPI DestroyMenu(_In_ HMENU)
#define WM_USER
Definition: winuser.h:1894
#define TPM_RETURNCMD
Definition: winuser.h:2386
BOOL WINAPI KillTimer(_In_opt_ HWND, _In_ UINT_PTR)
#define MAKEINTRESOURCE
Definition: winuser.h:591
#define SW_NORMAL
Definition: winuser.h:768
BOOL WINAPI AppendMenuW(_In_ HMENU, _In_ UINT, _In_ UINT_PTR, _In_opt_ LPCWSTR)
__wchar_t WCHAR
Definition: xmlstorage.h:180