#include <shellutils.h>
SysTrayIconHandlers_t g_IconHandlers [] = {
- { Volume_Init, Volume_Shutdown, Volume_Update, Volume_Message },
- { Hotplug_Init, Hotplug_Shutdown, Hotplug_Update, Hotplug_Message },
- { Power_Init, Power_Shutdown, Power_Update, Power_Message }
+ { 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 }
};
const int g_NumIcons = _countof(g_IconHandlers);
CSysTray::CSysTray() {}
CSysTray::~CSysTray() {}
+VOID CSysTray::GetServicesEnabled()
+{
+ HKEY hKey;
+ DWORD dwSize;
+
+ /* Enable power and volume by default */
+ this->dwServicesEnabled = POWER_SERVICE_FLAG | VOLUME_SERVICE_FLAG;
+
+ if (RegCreateKeyExW(HKEY_CURRENT_USER,
+ L"Software\\Microsoft\\Windows\\CurrentVersion\\Applets\\SysTray",
+ 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);
+}
+
HRESULT CSysTray::InitNetShell()
{
HRESULT hr = CoCreateInstance(CLSID_ConnectionTray, 0, 1u, IID_PPV_ARG(IOleCommandTarget, &pctNetShell));
TRACE("Initializing Notification icons...\n");
for (int i = 0; i < g_NumIcons; i++)
{
- HRESULT hr = g_IconHandlers[i].pfnInit(this);
- if (FAILED(hr))
- return hr;
+ if (this->dwServicesEnabled & g_IconHandlers[i].dwServiceFlag)
+ {
+ HRESULT hr = g_IconHandlers[i].pfnInit(this);
+ if (FAILED(hr))
+ return hr;
+ }
}
return InitNetShell();
TRACE("Shutting down Notification icons...\n");
for (int i = 0; i < g_NumIcons; i++)
{
- HRESULT hr = g_IconHandlers[i].pfnShutdown(this);
- if (FAILED(hr))
- return hr;
+ if (this->dwServicesEnabled & g_IconHandlers[i].dwServiceFlag)
+ {
+ HRESULT hr = g_IconHandlers[i].pfnShutdown(this);
+ if (FAILED(hr))
+ return hr;
+ }
}
return ShutdownNetShell();
TRACE("Updating Notification icons...\n");
for (int i = 0; i < g_NumIcons; i++)
{
- HRESULT hr = g_IconHandlers[i].pfnUpdate(this);
- if (FAILED(hr))
- return hr;
+ if (this->dwServicesEnabled & g_IconHandlers[i].dwServiceFlag)
+ {
+ HRESULT hr = g_IconHandlers[i].pfnUpdate(this);
+ if (FAILED(hr))
+ return hr;
+ }
}
return S_OK;
return FALSE;
case WM_CREATE:
+ GetServicesEnabled();
InitIcons();
SetTimer(1, 2000, NULL);
return TRUE;
// TODO: keep icon handlers here
+ DWORD dwServicesEnabled;
HWND hwndSysTray;
static DWORD WINAPI s_SysTrayThreadProc(PVOID param);
HRESULT InitNetShell();
HRESULT ShutdownNetShell();
+ VOID GetServicesEnabled();
+
public:
HRESULT NotifyIcon(INT code, UINT uId, HICON hIcon, LPCWSTR szTip, DWORD dwstate = 0);
HWND GetHWnd() { return m_hWnd; }
+ VOID EnableService(DWORD dwServiceFlag, BOOL bEnable);
+ BOOL IsServiceEnabled(DWORD dwServiceFlag);
+
protected:
BOOL ProcessWindowMessage(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT &lResult, DWORD dwMsgMapID = 0);
static HICON g_hIconHotplug = NULL;
static LPCWSTR g_strTooltip = L"Safely Remove Hardware and Eject Media";
static WCHAR g_strMenuSel[DISPLAY_NAME_LEN];
-static BOOL g_IsRunning = FALSE;
static BOOL g_IsRemoving = FALSE;
/*++
{
TRACE("Hotplug_Init\n");
g_hIconHotplug = LoadIcon(g_hInstance, MAKEINTRESOURCE(IDI_HOTPLUG_OK));
- g_IsRunning = TRUE;
EnumHotpluggedDevices(g_devList);
return pSysTray->NotifyIcon(NIM_ADD, ID_ICON_HOTPLUG, g_hIconHotplug, g_strTooltip, NIS_HIDDEN);
{
TRACE("Hotplug_Shutdown\n");
- g_IsRunning = FALSE;
-
return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_HOTPLUG, NULL, NULL);
}
case WM_USER + 220:
TRACE("Hotplug_Message: WM_USER+220\n");
- if (wParam == 1)
+ if (wParam == HOTPLUG_SERVICE_FLAG)
{
- if (lParam == FALSE)
+ if (lParam)
+ {
+ pSysTray->EnableService(HOTPLUG_SERVICE_FLAG, TRUE);
return Hotplug_Init(pSysTray);
+ }
else
+ {
+ pSysTray->EnableService(HOTPLUG_SERVICE_FLAG, FALSE);
return Hotplug_Shutdown(pSysTray);
+ }
}
return S_FALSE;
case WM_USER + 221:
TRACE("Hotplug_Message: WM_USER+221\n");
- if (wParam == 1)
+ if (wParam == HOTPLUG_SERVICE_FLAG)
{
- lResult = (LRESULT)g_IsRunning;
+ lResult = (LRESULT)pSysTray->IsServiceEnabled(HOTPLUG_SERVICE_FLAG);
return S_OK;
}
return S_FALSE;
CString g_strTooltip;
static HICON g_hIconBattery = NULL;
-static BOOL g_IsRunning = FALSE;
/*++
{
TRACE("Power_Init\n");
g_hIconBattery = DynamicLoadIcon(g_hInstance);
- g_IsRunning = TRUE;
return pSysTray->NotifyIcon(NIM_ADD, ID_ICON_POWER, g_hIconBattery, g_strTooltip);
}
HRESULT STDMETHODCALLTYPE Power_Shutdown(_In_ CSysTray * pSysTray)
{
TRACE("Power_Shutdown\n");
- g_IsRunning = FALSE;
return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_POWER, NULL, NULL);
}
{
case WM_USER + 220:
TRACE("Power_Message: WM_USER+220\n");
- if (wParam == 1)
+ if (wParam == POWER_SERVICE_FLAG)
{
- if (lParam == FALSE)
+ if (lParam)
+ {
+ pSysTray->EnableService(POWER_SERVICE_FLAG, TRUE);
return Power_Init(pSysTray);
+ }
else
+ {
+ pSysTray->EnableService(POWER_SERVICE_FLAG, FALSE);
return Power_Shutdown(pSysTray);
+ }
}
return S_FALSE;
case WM_USER + 221:
TRACE("Power_Message: WM_USER+221\n");
- if (wParam == 1)
+ if (wParam == POWER_SERVICE_FLAG)
{
- lResult = (LRESULT)g_IsRunning;
+ lResult = (LRESULT)pSysTray->IsServiceEnabled(POWER_SERVICE_FLAG);
return S_OK;
}
return S_FALSE;
#define ID_ICON_HOTPLUG (WM_APP + 0x4CC)
#define ID_ICON_POWER (WM_APP + 0x4CD)
+#define POWER_SERVICE_FLAG 0x00000001
+#define HOTPLUG_SERVICE_FLAG 0x00000002
+#define VOLUME_SERVICE_FLAG 0x00000004
+
#include "csystray.h"
typedef HRESULT(STDMETHODCALLTYPE * PFNSTINIT) (_In_ CSysTray * pSysTray);
struct SysTrayIconHandlers_t
{
+ DWORD dwServiceFlag;
PFNSTINIT pfnInit;
PFNSTSHUTDOWN pfnShutdown;
PFNSTUPDATE pfnUpdate;
UINT g_mmDeviceChange;
static BOOL g_IsMute = FALSE;
-static BOOL g_IsRunning = FALSE;
static HRESULT __stdcall Volume_FindMixerControl(CSysTray * pSysTray)
{
Volume_IsMute();
- g_IsRunning = TRUE;
-
HICON icon;
if (g_IsMute)
icon = g_hIconMute;
{
TRACE("Volume_Shutdown\n");
- g_IsRunning = FALSE;
-
return pSysTray->NotifyIcon(NIM_DELETE, ID_ICON_VOLUME, NULL, NULL);
}
{
case WM_USER + 220:
TRACE("Volume_Message: WM_USER+220\n");
- if (wParam == 4)
+ if (wParam == VOLUME_SERVICE_FLAG)
{
- if (lParam == FALSE)
+ if (lParam)
+ {
+ pSysTray->EnableService(VOLUME_SERVICE_FLAG, TRUE);
return Volume_Init(pSysTray);
+ }
else
+ {
+ pSysTray->EnableService(VOLUME_SERVICE_FLAG, FALSE);
return Volume_Shutdown(pSysTray);
+ }
}
return S_FALSE;
case WM_USER + 221:
TRACE("Volume_Message: WM_USER+221\n");
- if (wParam == 4)
+ if (wParam == VOLUME_SERVICE_FLAG)
{
- lResult = (LRESULT)g_IsRunning;
+ lResult = (LRESULT)pSysTray->IsServiceEnabled(VOLUME_SERVICE_FLAG);
return S_OK;
}
return S_FALSE;