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>
14 WINE_DEFAULT_DEBUG_CHANNEL(stobject
);
22 DWORD g_muteControlID
;
24 UINT g_mmDeviceChange
;
26 static BOOL g_IsMute
= FALSE
;
27 static BOOL g_IsRunning
= FALSE
;
29 static HRESULT __stdcall
Volume_FindMixerControl(CSysTray
* pSysTray
)
36 TRACE("Volume_FindDefaultMixerID\n");
38 result
= waveOutMessage((HWAVEOUT
)WAVE_MAPPER
, DRVM_MAPPER_PREFERRED_GET
, (DWORD_PTR
)&waveOutId
, (DWORD_PTR
)¶m2
);
42 if (waveOutId
== (DWORD
)-1)
44 TRACE("WARNING: waveOut has no default device, trying with first available device...\n", waveOutId
);
50 TRACE("waveOut default device is %d\n", waveOutId
);
52 result
= mixerGetID((HMIXEROBJ
)waveOutId
, &mixerId
, MIXER_OBJECTF_WAVEOUT
);
56 TRACE("mixerId for waveOut default device is %d\n", mixerId
);
64 MIXERCONTROL mixerControl
;
65 MIXERLINECONTROLS mixerLineControls
;
70 if (mixerGetDevCapsW(g_mixerId
, &mixerCaps
, sizeof(mixerCaps
)))
73 if (mixerCaps
.cDestinations
== 0)
76 TRACE("mixerCaps.cDestinations %d\n", mixerCaps
.cDestinations
);
79 for (idx
= 0; idx
< mixerCaps
.cDestinations
; idx
++)
81 mixerLine
.cbStruct
= sizeof(mixerLine
);
82 mixerLine
.dwDestination
= idx
;
83 if (!mixerGetLineInfoW((HMIXEROBJ
)g_mixerId
, &mixerLine
, 0))
85 if (mixerLine
.dwComponentType
>= MIXERLINE_COMPONENTTYPE_DST_SPEAKERS
&&
86 mixerLine
.dwComponentType
<= MIXERLINE_COMPONENTTYPE_DST_HEADPHONES
)
88 TRACE("Destination %d was not speakers or headphones.\n");
92 if (idx
>= mixerCaps
.cDestinations
)
95 TRACE("Valid destination %d found.\n");
97 g_mixerLineID
= mixerLine
.dwLineID
;
99 mixerLineControls
.cbStruct
= sizeof(mixerLineControls
);
100 mixerLineControls
.dwLineID
= mixerLine
.dwLineID
;
101 mixerLineControls
.cControls
= 1;
102 mixerLineControls
.dwControlType
= MIXERCONTROL_CONTROLTYPE_MUTE
;
103 mixerLineControls
.pamxctrl
= &mixerControl
;
104 mixerLineControls
.cbmxctrl
= sizeof(mixerControl
);
106 if (mixerGetLineControlsW((HMIXEROBJ
)g_mixerId
, &mixerLineControls
, MIXER_GETLINECONTROLSF_ONEBYTYPE
))
109 TRACE("Found control id %d for mute: %d\n", mixerControl
.dwControlID
);
111 g_muteControlID
= mixerControl
.dwControlID
;
116 HRESULT
Volume_IsMute()
119 MIXERCONTROLDETAILS mixerControlDetails
;
121 if (g_mixerId
!= (UINT
)-1 && g_muteControlID
!= (DWORD
)-1)
123 BOOL detailsResult
= 0;
124 mixerControlDetails
.cbStruct
= sizeof(mixerControlDetails
);
125 mixerControlDetails
.hwndOwner
= 0;
126 mixerControlDetails
.dwControlID
= g_muteControlID
;
127 mixerControlDetails
.cChannels
= 1;
128 mixerControlDetails
.paDetails
= &detailsResult
;
129 mixerControlDetails
.cbDetails
= sizeof(detailsResult
);
130 if (mixerGetControlDetailsW((HMIXEROBJ
) g_mixerId
, &mixerControlDetails
, 0))
133 TRACE("Obtained mute status %d\n", detailsResult
);
135 g_IsMute
= detailsResult
!= 0;
141 HRESULT STDMETHODCALLTYPE
Volume_Init(_In_ CSysTray
* pSysTray
)
144 WCHAR strTooltip
[128];
146 TRACE("Volume_Init\n");
150 hr
= Volume_FindMixerControl(pSysTray
);
154 g_mmDeviceChange
= RegisterWindowMessageW(L
"winmm_devicechange");
157 g_hIconVolume
= LoadIcon(g_hInstance
, MAKEINTRESOURCE(IDI_VOLUME
));
158 g_hIconMute
= LoadIcon(g_hInstance
, MAKEINTRESOURCE(IDI_VOLMUTE
));
168 icon
= g_hIconVolume
;
170 LoadStringW(g_hInstance
, IDS_VOL_VOLUME
, strTooltip
, _countof(strTooltip
));
171 return pSysTray
->NotifyIcon(NIM_ADD
, ID_ICON_VOLUME
, icon
, strTooltip
);
174 HRESULT STDMETHODCALLTYPE
Volume_Update(_In_ CSysTray
* pSysTray
)
178 TRACE("Volume_Update\n");
180 PrevState
= g_IsMute
;
183 if (PrevState
!= g_IsMute
)
185 WCHAR strTooltip
[128];
190 LoadStringW(g_hInstance
, IDS_VOL_MUTED
, strTooltip
, _countof(strTooltip
));
194 icon
= g_hIconVolume
;
195 LoadStringW(g_hInstance
, IDS_VOL_VOLUME
, strTooltip
, _countof(strTooltip
));
198 return pSysTray
->NotifyIcon(NIM_MODIFY
, ID_ICON_VOLUME
, icon
, strTooltip
);
206 HRESULT STDMETHODCALLTYPE
Volume_Shutdown(_In_ CSysTray
* pSysTray
)
208 TRACE("Volume_Shutdown\n");
212 return pSysTray
->NotifyIcon(NIM_DELETE
, ID_ICON_VOLUME
, NULL
, NULL
);
215 HRESULT
Volume_OnDeviceChange(_In_ CSysTray
* pSysTray
, WPARAM wParam
, LPARAM lParam
)
217 return Volume_FindMixerControl(pSysTray
);
220 static void _RunVolume()
222 // FIXME: ensure we are loading the right one
223 ShellExecuteW(NULL
, NULL
, L
"sndvol32.exe", NULL
, NULL
, SW_SHOWNORMAL
);
226 static void _RunMMCpl()
228 ShellExecuteW(NULL
, NULL
, L
"mmsys.cpl", NULL
, NULL
, SW_NORMAL
);
231 static void _ShowContextMenu(CSysTray
* pSysTray
)
233 WCHAR strAdjust
[128];
235 LoadStringW(g_hInstance
, IDS_VOL_OPEN
, strOpen
, _countof(strOpen
));
236 LoadStringW(g_hInstance
, IDS_VOL_ADJUST
, strAdjust
, _countof(strAdjust
));
238 HMENU hPopup
= CreatePopupMenu();
239 AppendMenuW(hPopup
, MF_STRING
, IDS_VOL_OPEN
, strOpen
);
240 AppendMenuW(hPopup
, MF_STRING
, IDS_VOL_ADJUST
, strAdjust
);
242 DWORD flags
= TPM_RETURNCMD
| TPM_NONOTIFY
| TPM_RIGHTALIGN
| TPM_BOTTOMALIGN
;
244 SetForegroundWindow(pSysTray
->GetHWnd());
247 DWORD id
= TrackPopupMenuEx(hPopup
, flags
,
249 pSysTray
->GetHWnd(), NULL
);
264 HRESULT STDMETHODCALLTYPE
Volume_Message(_In_ CSysTray
* pSysTray
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
, LRESULT
&lResult
)
266 if (uMsg
== g_mmDeviceChange
)
267 return Volume_OnDeviceChange(pSysTray
, wParam
, lParam
);
272 TRACE("Volume_Message: WM_USER+220\n");
276 return Volume_Init(pSysTray
);
278 return Volume_Shutdown(pSysTray
);
283 TRACE("Volume_Message: WM_USER+221\n");
286 lResult
= (LRESULT
)g_IsRunning
;
292 TRACE("Volume_Message uMsg=%d, w=%x, l=%x\n", uMsg
, wParam
, lParam
);
294 Volume_Update(pSysTray
);
302 TRACE("TODO: display volume slider\n");
305 case WM_LBUTTONDBLCLK
:
313 _ShowContextMenu(pSysTray
);
316 case WM_RBUTTONDBLCLK
:
325 TRACE("Volume_Message received for unknown ID %d, ignoring.\n");