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>
19 DWORD g_muteControlID
;
21 UINT g_mmDeviceChange
;
23 static BOOL g_IsMute
= FALSE
;
24 static BOOL g_IsRunning
= FALSE
;
26 static HRESULT __stdcall
Volume_FindMixerControl(CSysTray
* pSysTray
)
33 TRACE("Volume_FindDefaultMixerID\n");
35 result
= waveOutMessage((HWAVEOUT
)UlongToHandle(WAVE_MAPPER
), DRVM_MAPPER_PREFERRED_GET
, (DWORD_PTR
)&waveOutId
, (DWORD_PTR
)¶m2
);
39 if (waveOutId
== (DWORD
)-1)
41 TRACE("WARNING: waveOut has no default device, trying with first available device...\n", waveOutId
);
47 TRACE("waveOut default device is %d\n", waveOutId
);
49 result
= mixerGetID((HMIXEROBJ
)UlongToHandle(waveOutId
), &mixerId
, MIXER_OBJECTF_WAVEOUT
);
53 TRACE("mixerId for waveOut default device is %d\n", mixerId
);
61 MIXERCONTROL mixerControl
;
62 MIXERLINECONTROLS mixerLineControls
;
67 if (mixerGetDevCapsW(g_mixerId
, &mixerCaps
, sizeof(mixerCaps
)))
70 if (mixerCaps
.cDestinations
== 0)
73 TRACE("mixerCaps.cDestinations %d\n", mixerCaps
.cDestinations
);
76 for (idx
= 0; idx
< mixerCaps
.cDestinations
; idx
++)
78 mixerLine
.cbStruct
= sizeof(mixerLine
);
79 mixerLine
.dwDestination
= idx
;
80 if (!mixerGetLineInfoW((HMIXEROBJ
)UlongToHandle(g_mixerId
), &mixerLine
, 0))
82 if (mixerLine
.dwComponentType
>= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
&&
83 mixerLine
.dwComponentType
<= MIXERLINE_COMPONENTTYPE_DST_HEADPHONES
)
85 TRACE("Destination %d was not speakers or headphones.\n");
89 if (idx
>= mixerCaps
.cDestinations
)
92 TRACE("Valid destination %d found.\n");
94 g_mixerLineID
= mixerLine
.dwLineID
;
96 mixerLineControls
.cbStruct
= sizeof(mixerLineControls
);
97 mixerLineControls
.dwLineID
= mixerLine
.dwLineID
;
98 mixerLineControls
.cControls
= 1;
99 mixerLineControls
.dwControlType
= MIXERCONTROL_CONTROLTYPE_MUTE
;
100 mixerLineControls
.pamxctrl
= &mixerControl
;
101 mixerLineControls
.cbmxctrl
= sizeof(mixerControl
);
103 if (mixerGetLineControlsW((HMIXEROBJ
)UlongToHandle(g_mixerId
), &mixerLineControls
, MIXER_GETLINECONTROLSF_ONEBYTYPE
))
106 TRACE("Found control id %d for mute: %d\n", mixerControl
.dwControlID
);
108 g_muteControlID
= mixerControl
.dwControlID
;
113 HRESULT
Volume_IsMute()
116 MIXERCONTROLDETAILS mixerControlDetails
;
118 if (g_mixerId
!= (UINT
)-1 && g_muteControlID
!= (DWORD
)-1)
120 BOOL detailsResult
= 0;
121 mixerControlDetails
.cbStruct
= sizeof(mixerControlDetails
);
122 mixerControlDetails
.hwndOwner
= 0;
123 mixerControlDetails
.dwControlID
= g_muteControlID
;
124 mixerControlDetails
.cChannels
= 1;
125 mixerControlDetails
.paDetails
= &detailsResult
;
126 mixerControlDetails
.cbDetails
= sizeof(detailsResult
);
127 if (mixerGetControlDetailsW((HMIXEROBJ
) g_mixerId
, &mixerControlDetails
, 0))
130 TRACE("Obtained mute status %d\n", detailsResult
);
132 g_IsMute
= detailsResult
!= 0;
138 HRESULT STDMETHODCALLTYPE
Volume_Init(_In_ CSysTray
* pSysTray
)
141 WCHAR strTooltip
[128];
143 TRACE("Volume_Init\n");
147 hr
= Volume_FindMixerControl(pSysTray
);
151 g_mmDeviceChange
= RegisterWindowMessageW(L
"winmm_devicechange");
154 g_hIconVolume
= LoadIcon(g_hInstance
, MAKEINTRESOURCE(IDI_VOLUME
));
155 g_hIconMute
= LoadIcon(g_hInstance
, MAKEINTRESOURCE(IDI_VOLMUTE
));
165 icon
= g_hIconVolume
;
167 LoadStringW(g_hInstance
, IDS_VOL_VOLUME
, strTooltip
, _countof(strTooltip
));
168 return pSysTray
->NotifyIcon(NIM_ADD
, ID_ICON_VOLUME
, icon
, strTooltip
);
171 HRESULT STDMETHODCALLTYPE
Volume_Update(_In_ CSysTray
* pSysTray
)
175 TRACE("Volume_Update\n");
177 PrevState
= g_IsMute
;
180 if (PrevState
!= g_IsMute
)
182 WCHAR strTooltip
[128];
187 LoadStringW(g_hInstance
, IDS_VOL_MUTED
, strTooltip
, _countof(strTooltip
));
191 icon
= g_hIconVolume
;
192 LoadStringW(g_hInstance
, IDS_VOL_VOLUME
, strTooltip
, _countof(strTooltip
));
195 return pSysTray
->NotifyIcon(NIM_MODIFY
, ID_ICON_VOLUME
, icon
, strTooltip
);
203 HRESULT STDMETHODCALLTYPE
Volume_Shutdown(_In_ CSysTray
* pSysTray
)
205 TRACE("Volume_Shutdown\n");
209 return pSysTray
->NotifyIcon(NIM_DELETE
, ID_ICON_VOLUME
, NULL
, NULL
);
212 HRESULT
Volume_OnDeviceChange(_In_ CSysTray
* pSysTray
, WPARAM wParam
, LPARAM lParam
)
214 return Volume_FindMixerControl(pSysTray
);
217 static void _RunVolume(BOOL bTray
)
219 ShellExecuteW(NULL
, NULL
, bTray
? L
"sndvol32.exe /t" : L
"sndvol32.exe", NULL
, NULL
, SW_SHOWNORMAL
);
222 static void _RunMMCpl()
224 ShellExecuteW(NULL
, NULL
, L
"mmsys.cpl", NULL
, NULL
, SW_NORMAL
);
227 static void _ShowContextMenu(CSysTray
* pSysTray
)
229 WCHAR strAdjust
[128];
231 LoadStringW(g_hInstance
, IDS_VOL_OPEN
, strOpen
, _countof(strOpen
));
232 LoadStringW(g_hInstance
, IDS_VOL_ADJUST
, strAdjust
, _countof(strAdjust
));
234 HMENU hPopup
= CreatePopupMenu();
235 AppendMenuW(hPopup
, MF_STRING
, IDS_VOL_OPEN
, strOpen
);
236 AppendMenuW(hPopup
, MF_STRING
, IDS_VOL_ADJUST
, strAdjust
);
238 DWORD flags
= TPM_RETURNCMD
| TPM_NONOTIFY
| TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
;
240 SetForegroundWindow(pSysTray
->GetHWnd());
243 DWORD id
= TrackPopupMenuEx(hPopup
, flags
,
245 pSysTray
->GetHWnd(), NULL
);
260 HRESULT STDMETHODCALLTYPE
Volume_Message(_In_ CSysTray
* pSysTray
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
&lResult
)
262 if (uMsg
== g_mmDeviceChange
)
263 return Volume_OnDeviceChange(pSysTray
, wParam
, lParam
);
268 TRACE("Volume_Message: WM_USER+220\n");
272 return Volume_Init(pSysTray
);
274 return Volume_Shutdown(pSysTray
);
279 TRACE("Volume_Message: WM_USER+221\n");
282 lResult
= (LRESULT
)g_IsRunning
;
288 if (wParam
== VOLUME_TIMER_ID
)
290 KillTimer(pSysTray
->GetHWnd(), VOLUME_TIMER_ID
);
296 TRACE("Volume_Message uMsg=%d, w=%x, l=%x\n", uMsg
, wParam
, lParam
);
298 Volume_Update(pSysTray
);
303 SetTimer(pSysTray
->GetHWnd(), VOLUME_TIMER_ID
, GetDoubleClickTime(), NULL
);
309 case WM_LBUTTONDBLCLK
:
310 KillTimer(pSysTray
->GetHWnd(), VOLUME_TIMER_ID
);
318 _ShowContextMenu(pSysTray
);
321 case WM_RBUTTONDBLCLK
:
330 TRACE("Volume_Message received for unknown ID %d, ignoring.\n");