2014-07-30 22:08:05 +00:00
|
|
|
/*
|
|
|
|
* PROJECT: ReactOS system libraries
|
|
|
|
* LICENSE: GPL - See COPYING in the top level directory
|
2015-09-27 18:40:24 +00:00
|
|
|
* FILE: dll/shellext/stobject/csystray.cpp
|
2014-07-30 22:08:05 +00:00
|
|
|
* PURPOSE: Systray shell service object implementation
|
|
|
|
* PROGRAMMERS: David Quintana <gigaherz@gmail.com>
|
2017-10-16 14:11:10 +00:00
|
|
|
* Shriraj Sawant a.k.a SR13 <sr.official@hotmail.com>
|
2014-07-30 22:08:05 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precomp.h"
|
|
|
|
|
2022-06-18 19:19:42 +00:00
|
|
|
#include <regstr.h>
|
2017-12-08 22:15:31 +00:00
|
|
|
#include <undocshell.h>
|
|
|
|
#include <shellutils.h>
|
2014-07-30 22:08:05 +00:00
|
|
|
|
2014-07-31 12:36:40 +00:00
|
|
|
SysTrayIconHandlers_t g_IconHandlers [] = {
|
2019-04-22 22:12:23 +00:00
|
|
|
{ VOLUME_SERVICE_FLAG, Volume_Init, Volume_Shutdown, Volume_Update, Volume_Message },
|
|
|
|
{ HOTPLUG_SERVICE_FLAG, Hotplug_Init, Hotplug_Shutdown, Hotplug_Update, Hotplug_Message },
|
|
|
|
{ POWER_SERVICE_FLAG, Power_Init, Power_Shutdown, Power_Update, Power_Message }
|
2014-07-31 12:36:40 +00:00
|
|
|
};
|
2017-03-23 19:51:53 +00:00
|
|
|
const int g_NumIcons = _countof(g_IconHandlers);
|
2014-07-31 12:36:40 +00:00
|
|
|
|
2022-06-18 19:19:42 +00:00
|
|
|
CSysTray::CSysTray() : dwServicesEnabled(0)
|
|
|
|
{
|
2022-06-18 19:24:41 +00:00
|
|
|
wm_SHELLHOOK = RegisterWindowMessageW(L"SHELLHOOK");
|
2022-06-18 19:19:42 +00:00
|
|
|
wm_DESTROYWINDOW = RegisterWindowMessageW(L"CSysTray_DESTROY");
|
|
|
|
}
|
|
|
|
|
|
|
|
CSysTray::~CSysTray()
|
|
|
|
{
|
|
|
|
}
|
2014-07-30 22:08:05 +00:00
|
|
|
|
2019-04-22 22:12:23 +00:00
|
|
|
VOID CSysTray::GetServicesEnabled()
|
|
|
|
{
|
|
|
|
HKEY hKey;
|
|
|
|
DWORD dwSize;
|
|
|
|
|
2020-03-21 22:46:24 +00:00
|
|
|
/* Enable power, volume and hotplug by default */
|
|
|
|
this->dwServicesEnabled = POWER_SERVICE_FLAG | VOLUME_SERVICE_FLAG | HOTPLUG_SERVICE_FLAG;
|
2019-04-22 22:12:23 +00:00
|
|
|
|
2022-06-18 19:19:42 +00:00
|
|
|
if (RegCreateKeyExW(HKEY_CURRENT_USER, REGSTR_PATH_SYSTRAY,
|
2019-04-22 22:12:23 +00:00
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
REG_OPTION_NON_VOLATILE,
|
|
|
|
KEY_READ,
|
|
|
|
NULL,
|
|
|
|
&hKey,
|
|
|
|
NULL) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
dwSize = sizeof(DWORD);
|
|
|
|
RegQueryValueExW(hKey,
|
|
|
|
L"Services",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
(LPBYTE)&this->dwServicesEnabled,
|
|
|
|
&dwSize);
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VOID CSysTray::EnableService(DWORD dwServiceFlag, BOOL bEnable)
|
|
|
|
{
|
|
|
|
HKEY hKey;
|
|
|
|
|
|
|
|
if (bEnable)
|
|
|
|
this->dwServicesEnabled |= dwServiceFlag;
|
|
|
|
else
|
|
|
|
this->dwServicesEnabled &= ~dwServiceFlag;
|
|
|
|
|
|
|
|
if (RegCreateKeyExW(HKEY_CURRENT_USER,
|
|
|
|
L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\SysTray",
|
|
|
|
0,
|
|
|
|
NULL,
|
|
|
|
REG_OPTION_NON_VOLATILE,
|
|
|
|
KEY_WRITE,
|
|
|
|
NULL,
|
|
|
|
&hKey,
|
|
|
|
NULL) == ERROR_SUCCESS)
|
|
|
|
{
|
|
|
|
RegSetValueExW(hKey,
|
|
|
|
L"Services",
|
|
|
|
0,
|
|
|
|
REG_DWORD,
|
|
|
|
(LPBYTE)&this->dwServicesEnabled,
|
|
|
|
sizeof(DWORD));
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CSysTray::IsServiceEnabled(DWORD dwServiceFlag)
|
|
|
|
{
|
|
|
|
return (this->dwServicesEnabled & dwServiceFlag);
|
|
|
|
}
|
|
|
|
|
2014-12-20 16:12:35 +00:00
|
|
|
HRESULT CSysTray::InitNetShell()
|
|
|
|
{
|
|
|
|
HRESULT hr = CoCreateInstance(CLSID_ConnectionTray, 0, 1u, IID_PPV_ARG(IOleCommandTarget, &pctNetShell));
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
|
|
|
|
return pctNetShell->Exec(&CGID_ShellServiceObject,
|
|
|
|
OLECMDID_NEW,
|
|
|
|
OLECMDEXECOPT_DODEFAULT, NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::ShutdownNetShell()
|
|
|
|
{
|
|
|
|
if (!pctNetShell)
|
|
|
|
return S_FALSE;
|
|
|
|
HRESULT hr = pctNetShell->Exec(&CGID_ShellServiceObject,
|
|
|
|
OLECMDID_SAVE,
|
|
|
|
OLECMDEXECOPT_DODEFAULT, NULL, NULL);
|
|
|
|
pctNetShell.Release();
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:08:05 +00:00
|
|
|
HRESULT CSysTray::InitIcons()
|
|
|
|
{
|
2014-10-31 15:57:56 +00:00
|
|
|
TRACE("Initializing Notification icons...\n");
|
2014-07-30 22:08:05 +00:00
|
|
|
for (int i = 0; i < g_NumIcons; i++)
|
|
|
|
{
|
2019-04-22 22:12:23 +00:00
|
|
|
if (this->dwServicesEnabled & g_IconHandlers[i].dwServiceFlag)
|
|
|
|
{
|
|
|
|
HRESULT hr = g_IconHandlers[i].pfnInit(this);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
}
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 19:24:41 +00:00
|
|
|
MouseKeys_Init(this);
|
|
|
|
|
2014-12-20 16:12:35 +00:00
|
|
|
return InitNetShell();
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::ShutdownIcons()
|
|
|
|
{
|
2014-10-31 15:57:56 +00:00
|
|
|
TRACE("Shutting down Notification icons...\n");
|
2014-07-30 22:08:05 +00:00
|
|
|
for (int i = 0; i < g_NumIcons; i++)
|
|
|
|
{
|
2019-04-22 22:12:23 +00:00
|
|
|
if (this->dwServicesEnabled & g_IconHandlers[i].dwServiceFlag)
|
|
|
|
{
|
2022-06-18 19:19:42 +00:00
|
|
|
this->dwServicesEnabled &= ~g_IconHandlers[i].dwServiceFlag;
|
2019-04-22 22:12:23 +00:00
|
|
|
HRESULT hr = g_IconHandlers[i].pfnShutdown(this);
|
2022-06-18 19:19:42 +00:00
|
|
|
FAILED_UNEXPECTEDLY(hr);
|
2019-04-22 22:12:23 +00:00
|
|
|
}
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2022-06-18 19:24:41 +00:00
|
|
|
MouseKeys_Shutdown(this);
|
|
|
|
|
2014-12-20 16:12:35 +00:00
|
|
|
return ShutdownNetShell();
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::UpdateIcons()
|
|
|
|
{
|
2014-10-31 15:57:56 +00:00
|
|
|
TRACE("Updating Notification icons...\n");
|
2014-07-30 22:08:05 +00:00
|
|
|
for (int i = 0; i < g_NumIcons; i++)
|
|
|
|
{
|
2019-04-22 22:12:23 +00:00
|
|
|
if (this->dwServicesEnabled & g_IconHandlers[i].dwServiceFlag)
|
|
|
|
{
|
|
|
|
HRESULT hr = g_IconHandlers[i].pfnUpdate(this);
|
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
}
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:51:53 +00:00
|
|
|
HRESULT CSysTray::ProcessIconMessage(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult)
|
2014-07-30 22:08:05 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < g_NumIcons; i++)
|
|
|
|
{
|
2017-03-23 19:51:53 +00:00
|
|
|
HRESULT hr = g_IconHandlers[i].pfnMessage(this, uMsg, wParam, lParam, lResult);
|
2014-07-30 22:08:05 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return hr;
|
|
|
|
|
2015-05-19 20:31:31 +00:00
|
|
|
if (hr == S_OK)
|
2014-07-30 22:08:05 +00:00
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
2015-05-19 20:31:31 +00:00
|
|
|
// Not handled by anyone, so return accordingly.
|
|
|
|
return S_FALSE;
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 14:11:10 +00:00
|
|
|
/*++
|
|
|
|
* @name NotifyIcon
|
|
|
|
*
|
|
|
|
* Basically a Shell_NotifyIcon wrapper.
|
|
|
|
* Based on the parameters provided, it changes the current state of the notification icon.
|
|
|
|
*
|
|
|
|
* @param code
|
|
|
|
* Determines whether to add, delete or modify the notification icon (represented by uId).
|
|
|
|
* @param uId
|
|
|
|
* Represents the particular notification icon.
|
|
|
|
* @param hIcon
|
|
|
|
* A handle to an icon for the notification object.
|
|
|
|
* @param szTip
|
|
|
|
* A string for the tooltip of the notification.
|
|
|
|
* @param dwstate
|
|
|
|
* Determines whether to show or hide the notification icon.
|
|
|
|
*
|
|
|
|
* @return The error code.
|
|
|
|
*
|
|
|
|
*--*/
|
|
|
|
HRESULT CSysTray::NotifyIcon(INT code, UINT uId, HICON hIcon, LPCWSTR szTip, DWORD dwstate)
|
2014-07-30 22:08:05 +00:00
|
|
|
{
|
2018-02-04 01:23:28 +00:00
|
|
|
NOTIFYICONDATA nim = { 0 };
|
2014-10-31 15:57:56 +00:00
|
|
|
|
|
|
|
TRACE("NotifyIcon code=%d, uId=%d, hIcon=%p, szTip=%S\n", code, uId, hIcon, szTip);
|
|
|
|
|
2018-02-04 16:21:58 +00:00
|
|
|
nim.cbSize = sizeof(nim);
|
2014-10-31 15:57:56 +00:00
|
|
|
nim.uFlags = NIF_MESSAGE | NIF_ICON | NIF_STATE | NIF_TIP;
|
2014-07-30 22:08:05 +00:00
|
|
|
nim.hIcon = hIcon;
|
|
|
|
nim.uID = uId;
|
|
|
|
nim.uCallbackMessage = uId;
|
2017-10-16 14:11:10 +00:00
|
|
|
nim.dwState = dwstate;
|
|
|
|
nim.dwStateMask = NIS_HIDDEN;
|
2014-07-30 22:08:05 +00:00
|
|
|
nim.hWnd = m_hWnd;
|
|
|
|
nim.uVersion = NOTIFYICON_VERSION;
|
|
|
|
if (szTip)
|
|
|
|
StringCchCopy(nim.szTip, _countof(nim.szTip), szTip);
|
|
|
|
else
|
|
|
|
nim.szTip[0] = 0;
|
|
|
|
BOOL ret = Shell_NotifyIcon(code, &nim);
|
|
|
|
return ret ? S_OK : E_FAIL;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD WINAPI CSysTray::s_SysTrayThreadProc(PVOID param)
|
|
|
|
{
|
|
|
|
CSysTray * st = (CSysTray*) param;
|
|
|
|
return st->SysTrayThreadProc();
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::SysTrayMessageLoop()
|
|
|
|
{
|
|
|
|
BOOL ret;
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
while ((ret = GetMessage(&msg, NULL, 0, 0)) != 0)
|
|
|
|
{
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::SysTrayThreadProc()
|
|
|
|
{
|
|
|
|
WCHAR strFileName[MAX_PATH];
|
|
|
|
GetModuleFileNameW(g_hInstance, strFileName, MAX_PATH);
|
|
|
|
HMODULE hLib = LoadLibraryW(strFileName);
|
|
|
|
|
|
|
|
CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
|
|
|
|
|
|
|
|
Create(NULL);
|
|
|
|
|
|
|
|
HRESULT ret = SysTrayMessageLoop();
|
|
|
|
|
|
|
|
CoUninitialize();
|
|
|
|
|
2022-06-18 19:19:42 +00:00
|
|
|
Release();
|
2014-07-30 22:08:05 +00:00
|
|
|
FreeLibraryAndExitThread(hLib, ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::CreateSysTrayThread()
|
|
|
|
{
|
2014-08-15 10:49:04 +00:00
|
|
|
TRACE("CSysTray Init TODO: Initialize tray icon handlers.\n");
|
2022-06-18 19:19:42 +00:00
|
|
|
AddRef();
|
2014-07-30 22:08:05 +00:00
|
|
|
|
|
|
|
HANDLE hThread = CreateThread(NULL, 0, s_SysTrayThreadProc, this, 0, NULL);
|
|
|
|
|
|
|
|
CloseHandle(hThread);
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT CSysTray::DestroySysTrayWindow()
|
|
|
|
{
|
2022-06-18 19:19:42 +00:00
|
|
|
if (!DestroyWindow())
|
|
|
|
{
|
|
|
|
// Window is from another thread, ask it politely to destroy itself:
|
|
|
|
SendMessage(wm_DESTROYWINDOW);
|
|
|
|
}
|
2014-07-30 22:08:05 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// *** IOleCommandTarget methods ***
|
|
|
|
HRESULT STDMETHODCALLTYPE CSysTray::QueryStatus(const GUID *pguidCmdGroup, ULONG cCmds, OLECMD prgCmds [], OLECMDTEXT *pCmdText)
|
|
|
|
{
|
|
|
|
UNIMPLEMENTED;
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT STDMETHODCALLTYPE CSysTray::Exec(const GUID *pguidCmdGroup, DWORD nCmdID, DWORD nCmdexecopt, VARIANT *pvaIn, VARIANT *pvaOut)
|
|
|
|
{
|
|
|
|
if (!IsEqualGUID(*pguidCmdGroup, CGID_ShellServiceObject))
|
|
|
|
return E_FAIL;
|
|
|
|
|
|
|
|
switch (nCmdID)
|
|
|
|
{
|
|
|
|
case OLECMDID_NEW: // init
|
|
|
|
return CreateSysTrayThread();
|
|
|
|
case OLECMDID_SAVE: // shutdown
|
|
|
|
return DestroySysTrayWindow();
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL CSysTray::ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult, DWORD dwMsgMapID)
|
|
|
|
{
|
|
|
|
HRESULT hr;
|
|
|
|
|
|
|
|
if (hWnd != m_hWnd)
|
|
|
|
return FALSE;
|
|
|
|
|
2022-06-18 19:19:42 +00:00
|
|
|
if (wm_DESTROYWINDOW && uMsg == wm_DESTROYWINDOW)
|
|
|
|
{
|
|
|
|
return DestroyWindow();
|
|
|
|
}
|
2022-06-18 19:24:41 +00:00
|
|
|
|
|
|
|
if (wm_SHELLHOOK && uMsg == wm_SHELLHOOK)
|
|
|
|
{
|
|
|
|
if (wParam == HSHELL_ACCESSIBILITYSTATE && lParam == ACCESS_MOUSEKEYS)
|
|
|
|
{
|
|
|
|
MouseKeys_Update(this);
|
|
|
|
}
|
|
|
|
lResult = 0L;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:08:05 +00:00
|
|
|
switch (uMsg)
|
|
|
|
{
|
2015-05-19 19:58:11 +00:00
|
|
|
case WM_NCCREATE:
|
2015-05-19 20:31:31 +00:00
|
|
|
case WM_NCDESTROY:
|
2015-05-19 19:58:11 +00:00
|
|
|
return FALSE;
|
2017-08-18 14:49:11 +00:00
|
|
|
|
2014-07-30 22:08:05 +00:00
|
|
|
case WM_CREATE:
|
2019-04-22 22:12:23 +00:00
|
|
|
GetServicesEnabled();
|
2014-07-30 22:08:05 +00:00
|
|
|
InitIcons();
|
|
|
|
SetTimer(1, 2000, NULL);
|
2022-06-18 19:24:41 +00:00
|
|
|
RegisterShellHookWindow(hWnd);
|
2014-07-30 22:08:05 +00:00
|
|
|
return TRUE;
|
2017-08-18 14:49:11 +00:00
|
|
|
|
2014-07-30 22:08:05 +00:00
|
|
|
case WM_TIMER:
|
2018-02-28 19:29:29 +00:00
|
|
|
if (wParam == 1)
|
|
|
|
UpdateIcons();
|
|
|
|
else
|
|
|
|
ProcessIconMessage(uMsg, wParam, lParam, lResult);
|
2017-10-16 14:11:10 +00:00
|
|
|
return TRUE;
|
2018-02-28 19:29:29 +00:00
|
|
|
|
2022-06-18 19:24:41 +00:00
|
|
|
case WM_SETTINGCHANGE:
|
|
|
|
if (wParam == SPI_SETMOUSEKEYS)
|
|
|
|
{
|
|
|
|
MouseKeys_Update(this);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-07-30 22:08:05 +00:00
|
|
|
case WM_DESTROY:
|
2014-11-02 20:17:23 +00:00
|
|
|
KillTimer(1);
|
2022-06-18 19:24:41 +00:00
|
|
|
DeregisterShellHookWindow(hWnd);
|
2014-07-30 22:08:05 +00:00
|
|
|
ShutdownIcons();
|
2022-06-18 19:19:42 +00:00
|
|
|
PostQuitMessage(0);
|
2014-07-30 22:08:05 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-08-15 10:49:04 +00:00
|
|
|
TRACE("SysTray message received %u (%08p %08p)\n", uMsg, wParam, lParam);
|
2014-07-30 22:08:05 +00:00
|
|
|
|
2017-03-23 19:51:53 +00:00
|
|
|
hr = ProcessIconMessage(uMsg, wParam, lParam, lResult);
|
2014-07-30 22:08:05 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
return FALSE;
|
|
|
|
|
2015-05-19 20:31:31 +00:00
|
|
|
return (hr == S_OK);
|
2014-07-30 22:08:05 +00:00
|
|
|
}
|