- Updated Power plugin and Process plugin to use new API

- Removed CSPluginTemplate
This commit is contained in:
Birunthan Mohanathas
2011-12-30 16:24:22 +00:00
parent 7b3b0277ec
commit e8b89aadfa
3 changed files with 188 additions and 340 deletions

View File

@ -18,117 +18,72 @@
#include <windows.h>
#include <Psapi.h>
#include <string>
#include <map>
#include <algorithm>
#include <vector>
#include "../../Library/RawString.h"
#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
{
std::wstring processName;
CRawString 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();
/*
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)
PLUGIN_EXPORT void Initialize(void** data)
{
MeasureData data;
MeasureData* measure = new MeasureData;
g_Measures.push_back(measure);
const WCHAR* value = ReadConfigString(section, L"ProcessName", L"");
if (*value)
*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())
{
data.processName = value;
g_Values[id] = std::move(data);
CheckProcesses();
g_UpdateCount = 0;
}
return 1;
return (double)measure->isRunning;
}
/*
This function is called when new value should be measured.
The function returns the new value.
*/
UINT Update(UINT id)
PLUGIN_EXPORT void Finalize(void* data)
{
UINT result = 0;
MeasureData* measure = (MeasureData*)data;
std::vector<MeasureData*>::iterator iter = std::find(g_Measures.begin(), g_Measures.end(), measure);
g_Measures.erase(iter);
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)";
delete measure;
}
void CheckProcesses()
{
// Set everything to false
std::map<UINT, MeasureData>::iterator iter = g_Values.begin();
for ( ; iter != g_Values.end(); ++iter)
std::vector<MeasureData*>::iterator iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
{
(*iter).second.isRunning = false;
(*iter)->isRunning = false;
}
int bufSize = 256;
@ -151,12 +106,12 @@ void CheckProcesses()
WCHAR buffer[MAX_PATH];
if (GetModuleBaseName(hProcess, NULL, buffer, _countof(buffer)))
{
iter = g_Values.begin();
for ( ; iter != g_Values.end(); ++iter)
iter = g_Measures.begin();
for ( ; iter != g_Measures.end(); ++iter)
{
if (_wcsicmp(buffer, (*iter).second.processName.c_str()) == 0)
if (_wcsicmp(buffer, (*iter)->processName.c_str()) == 0)
{
(*iter).second.isRunning = true;
(*iter)->isRunning = true;
}
}
}