Reverted trunk to r1085.

This commit is contained in:
Birunthan Mohanathas
2011-12-30 17:18:34 +00:00
parent 9856b5138a
commit 71b8d6395d
14 changed files with 454 additions and 756 deletions

View File

@ -120,8 +120,6 @@
<ImportLibrary>.\x32/Debug/PowerPlugin.lib</ImportLibrary>
<TargetMachine>MachineX86</TargetMachine>
<AdditionalLibraryDirectories>..\..\Library\x32\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Rainmeter.lib;PowrProf.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>PowrProf.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -162,8 +160,6 @@
<ImportLibrary>.\x64/Debug/PowerPlugin.lib</ImportLibrary>
<TargetMachine>MachineX64</TargetMachine>
<AdditionalLibraryDirectories>..\..\Library\x64\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>Rainmeter.lib;PowrProf.lib;%(AdditionalDependencies)</AdditionalDependencies>
<DelayLoadDLLs>PowrProf.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -199,7 +195,7 @@
<Culture>0x0409</Culture>
</ResourceCompile>
<Link>
<AdditionalDependencies>Rainmeter.lib;PowrProf.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rainmeter.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>../../TestBench/x32/Release/Plugins/PowerPlugin.dll</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<AdditionalLibraryDirectories>..\..\Library\x32\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
@ -210,7 +206,6 @@
<MergeSections>.rdata=.text</MergeSections>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<DelayLoadDLLs>PowrProf.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -246,7 +241,7 @@
</ResourceCompile>
<Link>
<AdditionalOptions>/LTCG %(AdditionalOptions)</AdditionalOptions>
<AdditionalDependencies>Rainmeter.lib;PowrProf.lib;%(AdditionalDependencies)</AdditionalDependencies>
<AdditionalDependencies>Rainmeter.lib;%(AdditionalDependencies)</AdditionalDependencies>
<OutputFile>../../TestBench/x64/Release/Plugins/PowerPlugin.dll</OutputFile>
<SuppressStartupBanner>true</SuppressStartupBanner>
<AdditionalLibraryDirectories>..\..\Library\x64\$(Configuration);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
@ -256,7 +251,6 @@
<MergeSections>.rdata=.text</MergeSections>
<OptimizeReferences>true</OptimizeReferences>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<DelayLoadDLLs>PowrProf.dll</DelayLoadDLLs>
</Link>
</ItemDefinitionGroup>
<ItemGroup>

View File

@ -17,12 +17,13 @@
*/
#include <windows.h>
#include <Powrprof.h>
#include <math.h>
#include <map>
#include <string>
#include <time.h>
#include <errno.h>
#include <crtdbg.h>
#include "../../Library/RawString.h"
#include <Powrprof.h>
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
typedef struct _PROCESSOR_POWER_INFORMATION
@ -35,7 +36,20 @@ typedef struct _PROCESSOR_POWER_INFORMATION
ULONG CurrentIdleState;
} PROCESSOR_POWER_INFORMATION, *PPROCESSOR_POWER_INFORMATION;
enum MeasureType
typedef LONG (WINAPI *FPCALLNTPOWERINFORMATION)(POWER_INFORMATION_LEVEL, PVOID, ULONG, PVOID, ULONG);
/* The exported functions */
extern "C"
{
__declspec( dllexport ) UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id);
__declspec( dllexport ) void Finalize(HMODULE instance, UINT id);
__declspec( dllexport ) LPCTSTR GetString(UINT id, UINT flags);
__declspec( dllexport ) double Update2(UINT id);
__declspec( dllexport ) UINT GetPluginVersion();
__declspec( dllexport ) LPCTSTR GetPluginAuthor();
}
enum POWER_STATE
{
POWER_UNKNOWN,
POWER_ACLINE,
@ -47,187 +61,272 @@ enum MeasureType
POWER_HZ
};
struct MeasureData
{
MeasureType type;
CRawString format;
MeasureData() : type(POWER_UNKNOWN) {}
};
UINT g_NumOfProcessors = 0;
std::map<UINT, POWER_STATE> g_States;
std::map<UINT, std::wstring> g_Formats;
HINSTANCE hDLL = NULL;
int g_Instances, g_NumOfProcessors = 0;
FPCALLNTPOWERINFORMATION fpCallNtPowerInformation = NULL;
void NullCRTInvalidParameterHandler(const wchar_t* expression, const wchar_t* function, const wchar_t* file, unsigned int line, uintptr_t pReserved)
{
// Do nothing.
}
PLUGIN_EXPORT void Initialize(void** data)
/*
This function is called when the measure is initialized.
The function must return the maximum value that can be measured.
The return value can also be 0, which means that Rainmeter will
track the maximum value automatically. The parameters for this
function are:
instance The instance of this DLL
iniFile The name of the ini-file (usually Rainmeter.ini)
section The name of the section in the ini-file for this measure
id The identifier for the measure. This is used to identify the measures that use the same plugin.
*/
UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id)
{
MeasureData* measure = new MeasureData;
*data = measure;
if (!g_NumOfProcessors)
g_Instances++;
if (hDLL == NULL)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
g_NumOfProcessors = (UINT)si.dwNumberOfProcessors;
}
}
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
MeasureData* measure = (MeasureData*)data;
LPCWSTR value = RmReadString(rm, L"PowerState", L"");
if (_wcsicmp(L"ACLINE", value) == 0)
{
measure->type = POWER_ACLINE;
*maxValue = 1.0;
}
else if (_wcsicmp(L"STATUS", value) == 0)
{
measure->type = POWER_STATUS;
*maxValue = 4.0;
}
else if (_wcsicmp(L"STATUS2", value) == 0)
{
measure->type = POWER_STATUS2;
*maxValue = 255.0;
}
else if (_wcsicmp(L"LIFETIME", value) == 0)
{
measure->type= POWER_LIFETIME;
value = RmReadString(rm, L"Format", L"%H:%M");
measure->format = value;
SYSTEM_POWER_STATUS sps;
if (GetSystemPowerStatus(&sps))
hDLL = LoadLibrary(L"powrprof.dll");
if (hDLL)
{
*maxValue = sps.BatteryFullLifeTime;
fpCallNtPowerInformation = (FPCALLNTPOWERINFORMATION)GetProcAddress(hDLL, "CallNtPowerInformation");
}
}
else if (_wcsicmp(L"MHZ", value) == 0)
POWER_STATE powerState = POWER_UNKNOWN;
SYSTEM_INFO systemInfo = {0};
GetSystemInfo(&systemInfo);
g_NumOfProcessors = (int)systemInfo.dwNumberOfProcessors;
/* Read our own settings from the ini-file */
LPCTSTR type = ReadConfigString(section, L"PowerState", L"");
if (type)
{
measure->type = POWER_MHZ;
if (_wcsicmp(L"ACLINE", type) == 0)
{
powerState = POWER_ACLINE;
}
else if (_wcsicmp(L"STATUS", type) == 0)
{
powerState = POWER_STATUS;
}
else if (_wcsicmp(L"STATUS2", type) == 0)
{
powerState = POWER_STATUS2;
}
else if (_wcsicmp(L"LIFETIME", type) == 0)
{
powerState= POWER_LIFETIME;
LPCTSTR format = ReadConfigString(section, L"Format", L"%H:%M");
if (format)
{
g_Formats[id] = format;
}
}
else if (_wcsicmp(L"MHZ", type) == 0)
{
powerState= POWER_MHZ;
}
else if (_wcsicmp(L"HZ", type) == 0)
{
powerState= POWER_HZ;
}
else if (_wcsicmp(L"PERCENT", type) == 0)
{
powerState = POWER_PERCENT;
}
g_States[id] = powerState;
}
else if (_wcsicmp(L"HZ", value) == 0)
switch(powerState)
{
measure->type = POWER_HZ;
}
else if (_wcsicmp(L"PERCENT", value) == 0)
{
measure->type = POWER_PERCENT;
*maxValue = 100.0;
case POWER_ACLINE:
return 1;
case POWER_STATUS:
return 4;
case POWER_STATUS2:
return 255;
case POWER_LIFETIME:
{
SYSTEM_POWER_STATUS status;
if (GetSystemPowerStatus(&status))
{
return status.BatteryFullLifeTime;
}
}
break;
case POWER_PERCENT:
return 100;
}
return 0;
}
PLUGIN_EXPORT double Update(void* data)
/*
This function is called when new value should be measured.
The function returns the new value.
*/
double Update2(UINT id)
{
MeasureData* measure = (MeasureData*)data;
SYSTEM_POWER_STATUS sps;
if (GetSystemPowerStatus(&sps))
SYSTEM_POWER_STATUS status;
if (GetSystemPowerStatus(&status))
{
switch (measure->type)
std::map<UINT, POWER_STATE>::iterator i = g_States.find(id);
if (i != g_States.end())
{
case POWER_ACLINE:
return sps.ACLineStatus == 1 ? 1.0 : 0.0;
switch ((*i).second)
{
case POWER_ACLINE:
return status.ACLineStatus == 1 ? 1 : 0;
case POWER_STATUS:
if (sps.BatteryFlag & 128)
{
return 0.0; // No battery
}
else if (sps.BatteryFlag & 8)
{
return 1.0; // Charging
}
else if (sps.BatteryFlag & 4)
{
return 2.0; // Critical
}
else if (sps.BatteryFlag & 2)
{
return 3.0; // Low
}
else if (sps.BatteryFlag & 1)
{
return 4.0; // High
}
break;
case POWER_STATUS:
if (status.BatteryFlag & 128)
{
return 0; // No battery
}
else if (status.BatteryFlag & 8)
{
return 1; // Charging
}
else if (status.BatteryFlag & 4)
{
return 2; // Critical
}
else if (status.BatteryFlag & 2)
{
return 3; // Low
}
else if (status.BatteryFlag & 1)
{
return 4; // High
}
break;
case POWER_STATUS2:
return sps.BatteryFlag;
case POWER_STATUS2:
return status.BatteryFlag;
case POWER_LIFETIME:
return sps.BatteryLifeTime;
case POWER_LIFETIME:
return status.BatteryLifeTime;
case POWER_PERCENT:
return sps.BatteryLifePercent == 255 ? 100.0 : sps.BatteryLifePercent;
case POWER_PERCENT:
return status.BatteryLifePercent > 100 ? 100 : status.BatteryLifePercent;
case POWER_MHZ:
case POWER_HZ:
if (g_NumOfProcessors > 0)
{
PROCESSOR_POWER_INFORMATION* ppi = new PROCESSOR_POWER_INFORMATION[g_NumOfProcessors];
memset(ppi, 0, sizeof(PROCESSOR_POWER_INFORMATION) * g_NumOfProcessors);
CallNtPowerInformation(ProcessorInformation, NULL, 0, ppi, sizeof(PROCESSOR_POWER_INFORMATION) * g_NumOfProcessors);
double value = (measure->type == POWER_MHZ) ? ppi[0].CurrentMhz : ppi[0].CurrentMhz * 1000000.0;
delete [] ppi;
return value;
case POWER_MHZ:
case POWER_HZ:
if (fpCallNtPowerInformation && g_NumOfProcessors > 0)
{
PROCESSOR_POWER_INFORMATION* ppi = new PROCESSOR_POWER_INFORMATION[g_NumOfProcessors];
memset(ppi, 0, sizeof(PROCESSOR_POWER_INFORMATION) * g_NumOfProcessors);
fpCallNtPowerInformation(ProcessorInformation, NULL, 0, ppi, sizeof(PROCESSOR_POWER_INFORMATION) * g_NumOfProcessors);
double value = ((*i).second == POWER_MHZ) ? ppi[0].CurrentMhz : ppi[0].CurrentMhz * 1000000.0;
delete [] ppi;
return value;
}
}
}
}
return 0.0;
return 0;
}
PLUGIN_EXPORT LPCWSTR GetString(void* data)
/*
This function is called when the value should be
returned as a string.
*/
LPCTSTR GetString(UINT id, UINT flags)
{
static WCHAR buffer[128];
MeasureData* measure = (MeasureData*)data;
if (measure->type == POWER_LIFETIME)
std::map<UINT, POWER_STATE>::iterator i = g_States.find(id);
if (i != g_States.end())
{
SYSTEM_POWER_STATUS sps;
if (GetSystemPowerStatus(&sps))
if ((*i).second == POWER_LIFETIME)
{
// Change it to time string
if (sps.BatteryLifeTime == -1)
SYSTEM_POWER_STATUS status;
if (GetSystemPowerStatus(&status))
{
return L"Unknown";
}
else
{
tm time = {0};
time.tm_sec = sps.BatteryLifeTime % 60;
time.tm_min = (sps.BatteryLifeTime / 60) % 60;
time.tm_hour = sps.BatteryLifeTime / 60 / 60;
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(NullCRTInvalidParameterHandler);
_CrtSetReportMode(_CRT_ASSERT, 0);
errno = 0;
wcsftime(buffer, 128, measure->format.c_str(), &time);
if (errno == EINVAL)
// Change it to time string
if (status.BatteryLifeTime == -1)
{
buffer[0] = L'\0';
return L"Unknown";
}
else
{
std::map<UINT, std::wstring>::iterator iter = g_Formats.find(id);
if (iter != g_Formats.end())
{
tm time = {0};
time.tm_sec = status.BatteryLifeTime % 60;
time.tm_min = (status.BatteryLifeTime / 60) % 60;
time.tm_hour = status.BatteryLifeTime / 60 / 60;
_set_invalid_parameter_handler(oldHandler);
_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(NullCRTInvalidParameterHandler);
_CrtSetReportMode(_CRT_ASSERT, 0);
return buffer;
errno = 0;
wcsftime(buffer, 128, (*iter).second.c_str(), &time);
if (errno == EINVAL)
{
buffer[0] = L'\0';
}
_set_invalid_parameter_handler(oldHandler);
}
else
{
wsprintf(buffer, L"%i:%02i:%02i", status.BatteryLifeTime / 60 / 60, (status.BatteryLifeTime / 60) % 60, status.BatteryLifeTime % 60);
}
return buffer;
}
}
}
}
return NULL;
}
PLUGIN_EXPORT void Finalize(void* data)
/*
If the measure needs to free resources before quitting.
The plugin can export Finalize function, which is called
when Rainmeter quits (or refreshes).
*/
void Finalize(HMODULE instance, UINT id)
{
MeasureData* measure = (MeasureData*)data;
delete measure;
std::map<UINT, POWER_STATE>::iterator i = g_States.find(id);
if (i != g_States.end())
{
g_States.erase(i);
}
std::map<UINT, std::wstring>::iterator i2 = g_Formats.find(id);
if (i2 != g_Formats.end())
{
g_Formats.erase(i2);
}
g_Instances--;
if (hDLL != NULL && g_Instances == 0)
{
FreeLibrary(hDLL);
hDLL = NULL;
fpCallNtPowerInformation = NULL;
}
}
UINT GetPluginVersion()
{
return 1004;
}
LPCTSTR GetPluginAuthor()
{
return L"Rainy (rainy@iki.fi)";
}

View File

@ -18,72 +18,117 @@
#include <windows.h>
#include <Psapi.h>
#include <algorithm>
#include <vector>
#include "../../Library/RawString.h"
#include <string>
#include <map>
#include "../../Library/Export.h" // Rainmeter's exported functions
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
/* The exported functions */
extern "C"
{
__declspec(dllexport) UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id);
__declspec(dllexport) void Finalize(HMODULE instance, UINT id);
__declspec(dllexport) UINT Update(UINT id);
__declspec(dllexport) UINT GetPluginVersion();
__declspec(dllexport) LPCTSTR GetPluginAuthor();
}
struct MeasureData
{
CRawString processName;
std::wstring processName;
bool isRunning;
MeasureData() : isRunning(false) {}
};
static std::map<UINT, MeasureData> g_Values;
static UINT g_UpdateCount = 0;
static std::vector<MeasureData*> g_Measures;
void CheckProcesses();
PLUGIN_EXPORT void Initialize(void** data)
/*
This function is called when the measure is initialized.
The function must return the maximum value that can be measured.
The return value can also be 0, which means that Rainmeter will
track the maximum value automatically. The parameters for this
function are:
instance The instance of this DLL
iniFile The name of the ini-file (usually Rainmeter.ini)
section The name of the section in the ini-file for this measure
id The identifier for the measure. This is used to identify the measures that use the same plugin.
*/
UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id)
{
MeasureData* measure = new MeasureData;
g_Measures.push_back(measure);
MeasureData data;
*data = measure;
}
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
MeasureData* measure = (MeasureData*)data;
LPCWSTR value = RmReadString(rm, L"ProcessName", L"");
measure->processName = value;
}
PLUGIN_EXPORT double Update(void* data)
{
MeasureData* measure = (MeasureData*)data;
// Updates the measure only once per combined updates of all measures
++g_UpdateCount;
if (g_UpdateCount >= g_Measures.size())
const WCHAR* value = ReadConfigString(section, L"ProcessName", L"");
if (*value)
{
CheckProcesses();
g_UpdateCount = 0;
data.processName = value;
g_Values[id] = std::move(data);
}
return (double)measure->isRunning;
return 1;
}
PLUGIN_EXPORT void Finalize(void* data)
/*
This function is called when new value should be measured.
The function returns the new value.
*/
UINT Update(UINT id)
{
MeasureData* measure = (MeasureData*)data;
std::vector<MeasureData*>::iterator iter = std::find(g_Measures.begin(), g_Measures.end(), measure);
g_Measures.erase(iter);
UINT result = 0;
delete measure;
std::map<UINT, MeasureData>::const_iterator iter = g_Values.find(id);
if (iter != g_Values.end())
{
// Updates the measure only once per combined updates of all measures
++g_UpdateCount;
if (g_UpdateCount >= g_Values.size())
{
CheckProcesses();
g_UpdateCount = 0;
}
result = (UINT)(*iter).second.isRunning;
}
return result;
}
/*
If the measure needs to free resources before quitting.
The plugin can export Finalize function, which is called
when Rainmeter quits (or refreshes).
*/
void Finalize(HMODULE instance, UINT id)
{
std::map<UINT, MeasureData>::iterator iter = g_Values.find(id);
if (iter != g_Values.end())
{
g_Values.erase(iter);
}
}
UINT GetPluginVersion()
{
return 1000;
}
LPCTSTR GetPluginAuthor()
{
return L"Birunthan Mohanathas (poiru.net)";
}
void CheckProcesses()
{
// Set everything to false
std::vector<MeasureData*>::iterator iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
std::map<UINT, MeasureData>::iterator iter = g_Values.begin();
for ( ; iter != g_Values.end(); ++iter)
{
(*iter)->isRunning = false;
(*iter).second.isRunning = false;
}
int bufSize = 256;
@ -106,12 +151,12 @@ void CheckProcesses()
WCHAR buffer[MAX_PATH];
if (GetModuleBaseName(hProcess, NULL, buffer, _countof(buffer)))
{
iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
iter = g_Values.begin();
for ( ; iter != g_Values.end(); ++iter)
{
if (_wcsicmp(buffer, (*iter)->processName.c_str()) == 0)
if (_wcsicmp(buffer, (*iter).second.processName.c_str()) == 0)
{
(*iter)->isRunning = true;
(*iter).second.isRunning = true;
}
}
}