Skip to content

Commit

Permalink
PowerPlugin -> SysInfo: Moved the last wake/sleep time options to the…
Browse files Browse the repository at this point in the history
… SysInfo plugin.

SysInfo additions:
SysInfoType=USER_LAST_WAKE_TIME
SysInfoType=USER_LAST_SLEEP_TIME

Both return a timestamp that can be used with a Time measure for formatting.
  • Loading branch information
brianferguson committed Jun 27, 2020
1 parent ca32901 commit 7170b86
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 75 deletions.
58 changes: 2 additions & 56 deletions Plugins/PluginPower/PowerPlugin.cpp
Expand Up @@ -33,9 +33,7 @@ enum MeasureType
POWER_LIFETIME,
POWER_PERCENT,
POWER_MHZ,
POWER_HZ,
POWER_LASTWAKETIME,
POWER_LASTSLEEPTIME
POWER_HZ
};

struct MeasureData
Expand All @@ -49,10 +47,8 @@ struct MeasureData
DWORD cachedBatteryLifeTime;

void* rm;

ULONGLONG logonTime;

MeasureData() : type(POWER_UNKNOWN), suppressError(false), updated(false), cachedBatteryLifeTime(0UL), rm(nullptr), logonTime(0ULL) {}
MeasureData() : type(POWER_UNKNOWN), suppressError(false), updated(false), cachedBatteryLifeTime(0UL), rm(nullptr) {}
};

UINT g_NumOfProcessors = 0U;
Expand All @@ -76,23 +72,6 @@ PLUGIN_EXPORT void Initialize(void** data, void* rm)
}

measure->rm = rm;

// Get user logon time (from SysInfo.cpp)
HKEY hKey;
if (RegOpenKey(HKEY_CURRENT_USER, L"Volatile Environment", &hKey) == ERROR_SUCCESS)
{
FILETIME lastWrite;
if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lastWrite) == ERROR_SUCCESS)
{
FileTimeToLocalFileTime(&lastWrite, &lastWrite);

LARGE_INTEGER li;
li.LowPart = lastWrite.dwLowDateTime;
li.HighPart = lastWrite.dwHighDateTime;
measure->logonTime = static_cast<ULONGLONG>(li.QuadPart);
}
RegCloseKey(hKey);
}
}

PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
Expand Down Expand Up @@ -144,14 +123,6 @@ PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
measure->type = POWER_PERCENT;
*maxValue = 100.0;
}
else if (_wcsicmp(L"LASTWAKETIME", value) == 0)
{
measure->type = POWER_LASTWAKETIME;
}
else if (_wcsicmp(L"LASTSLEEPTIME", value) == 0)
{
measure->type = POWER_LASTSLEEPTIME;
}

if (measure->updated)
{
Expand Down Expand Up @@ -195,31 +166,6 @@ PLUGIN_EXPORT double Update(void* data)
}
}
break;

case POWER_LASTWAKETIME:
case POWER_LASTSLEEPTIME:
{
if (g_NumOfProcessors > 0U)
{
double value = 0.0;
ULONGLONG nano = 0ULL;
LONG status = CallNtPowerInformation(
(measure->type == POWER_LASTWAKETIME) ? LastWakeTime : LastSleepTime, nullptr, 0, &nano, sizeof(ULONGLONG));
if (status == NT_STATUS_SUCCESS)
{
value = (double)((measure->logonTime + nano) / 10000000);
}
else if (!measure->suppressError)
{
// NTSTATUS codes:
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55
RmLogF(measure->rm, LOG_ERROR, L"Last wake time error: 0x%08x", status);
measure->suppressError = true;
}
return value;
}
}
break;
}

SYSTEM_POWER_STATUS sps;
Expand Down
4 changes: 2 additions & 2 deletions Plugins/PluginSysInfo/PluginSysInfo.vcxproj
Expand Up @@ -21,8 +21,8 @@
<PreprocessorDefinitions>_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<AdditionalDependencies>Rasapi32.lib;Iphlpapi.lib;netapi32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>netapi32.dll</DelayLoadDLLs>
<AdditionalDependencies>Rasapi32.lib;Iphlpapi.lib;netapi32.lib;PowrProf.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>netapi32.dll;PowrProf.dll;</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand Down
89 changes: 72 additions & 17 deletions Plugins/PluginSysInfo/SysInfo.cpp
Expand Up @@ -7,6 +7,7 @@

#include <algorithm>
#include <windows.h>
#include <Powrprof.h>
#include <Iphlpapi.h>
#include <Netlistmgr.h>
#include <lm.h>
Expand Down Expand Up @@ -68,7 +69,9 @@ enum MeasureType
MEASURE_TIMEZONE_STANDARD_NAME,
MEASURE_TIMEZONE_DAYLIGHT_BIAS,
MEASURE_TIMEZONE_DAYLIGHT_NAME,
MEASURE_USER_LOGONTIME
MEASURE_USER_LOGONTIME,
MEASURE_USER_LAST_WAKE_TIME,
MEASURE_USER_LAST_SLEEP_TIME
};

struct MeasureData
Expand All @@ -78,14 +81,20 @@ struct MeasureData

bool useBestInterface;

MeasureData() : type(), data(), useBestInterface(false) {}
bool suppressError;
bool updated;
void* rm;

MeasureData() : type(), data(), useBestInterface(false), suppressError(false), updated(false), rm(nullptr) {}
};

NLM_CONNECTIVITY GetNetworkConnectivity();
BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
int GetBestInterfaceOrByName(LPCWSTR data, bool& found);

bool g_Initialized = false;
LONGLONG g_LogonTime = 0LL;
constexpr LONG NT_STATUS_SUCCESS = 0x00000000L;

PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
Expand All @@ -101,14 +110,38 @@ PLUGIN_EXPORT void Initialize(void** data, void* rm)

m_Monitors.count = 0;
EnumDisplayMonitors(nullptr, nullptr, MyInfoEnumProc, (LPARAM)(&m_Monitors));

// Get user logon time
HKEY hKey;
if (RegOpenKey(HKEY_CURRENT_USER, L"Volatile Environment", &hKey) == ERROR_SUCCESS)
{
FILETIME lastWrite;
if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lastWrite) == ERROR_SUCCESS)
{
FileTimeToLocalFileTime(&lastWrite, &lastWrite);

LARGE_INTEGER li;
li.LowPart = lastWrite.dwLowDateTime;
li.HighPart = lastWrite.dwHighDateTime;
g_LogonTime = li.QuadPart;
}
RegCloseKey(hKey);
}

g_Initialized = true;
}

measure->rm = rm;
}

PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
MeasureData* measure = (MeasureData*)data;

MeasureType oldType = measure->type;
int oldData = measure->data;
bool oldBest = measure->useBestInterface;

int defaultData = -1;

LPCTSTR type = RmReadString(rm, L"SysInfoType", L"");
Expand Down Expand Up @@ -269,6 +302,14 @@ PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
measure->type = MEASURE_USER_LOGONTIME;
}
else if (_wcsicmp(L"USER_LAST_WAKE_TIME", type) == 0)
{
measure->type = MEASURE_USER_LAST_WAKE_TIME;
}
else if (_wcsicmp(L"USER_LAST_SLEEP_TIME", type) == 0)
{
measure->type = MEASURE_USER_LAST_SLEEP_TIME;
}
else
{
WCHAR buffer[256];
Expand All @@ -293,6 +334,14 @@ PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
measure->data = RmReadInt(rm, L"SysInfoData", defaultData);
}

if (measure->updated)
{
measure->suppressError =
oldType == measure->type &&
oldData == measure->data &&
oldBest == measure->useBestInterface;
}
}

PLUGIN_EXPORT LPCWSTR GetString(void* data)
Expand Down Expand Up @@ -554,6 +603,8 @@ PLUGIN_EXPORT double Update(void* data)
{
MeasureData* measure = (MeasureData*)data;

measure->updated = true;

switch (measure->type)
{
case MEASURE_PAGESIZE:
Expand Down Expand Up @@ -700,25 +751,29 @@ PLUGIN_EXPORT double Update(void* data)
}

case MEASURE_USER_LOGONTIME:
return (double)(g_LogonTime / 10000000);

case MEASURE_USER_LAST_WAKE_TIME:
case MEASURE_USER_LAST_SLEEP_TIME:
{
// Get "last write time" of the current users environment
HKEY hKey;
FILETIME lastWrite;
if (RegOpenKey(HKEY_CURRENT_USER, L"Volatile Environment", &hKey) == ERROR_SUCCESS)
bool isWake = measure->type == MEASURE_USER_LAST_WAKE_TIME;
double value = 0.0;
ULONGLONG nano = 0ULL;
LONG status = CallNtPowerInformation(isWake ? LastWakeTime : LastSleepTime, nullptr, 0, &nano, sizeof(ULONGLONG));
if (status == NT_STATUS_SUCCESS)
{
if (RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &lastWrite) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
FileTimeToLocalFileTime(&lastWrite, &lastWrite);

LARGE_INTEGER li;
li.LowPart = lastWrite.dwLowDateTime;
li.HighPart = lastWrite.dwHighDateTime;
return (double)(li.QuadPart / 10000000);
}
RegCloseKey(hKey);
value = (double)((g_LogonTime + (LONGLONG)nano) / 10000000);
}
else if (!measure->suppressError)
{
// NTSTATUS codes:
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55
RmLogF(measure->rm, LOG_ERROR, L"Last %s time error: 0x%08x", isWake ? L"wake" : L"sleep", status);
measure->suppressError = true;
}
return value;
}

}

return 0.0;
Expand Down

0 comments on commit 7170b86

Please sign in to comment.