2009-02-10 18:37:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2001 Kimmo Pekkola
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2009-10-07 16:45:14 +00:00
|
|
|
#include "StdAfx.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "MeasurePlugin.h"
|
|
|
|
#include "Rainmeter.h"
|
2010-09-11 19:39:45 +00:00
|
|
|
#include "System.h"
|
2009-02-10 18:37:48 +00:00
|
|
|
#include "Error.h"
|
|
|
|
|
|
|
|
extern CRainmeter* Rainmeter;
|
|
|
|
|
|
|
|
/*
|
|
|
|
** CMeasureMemory
|
|
|
|
**
|
|
|
|
** The constructor
|
|
|
|
**
|
|
|
|
*/
|
2011-02-15 16:26:54 +00:00
|
|
|
CMeasurePlugin::CMeasurePlugin(CMeterWindow* meterWindow, const WCHAR* name) : CMeasure(meterWindow, name),
|
2011-01-29 00:11:01 +00:00
|
|
|
m_Plugin(),
|
|
|
|
m_ID(),
|
|
|
|
InitializeFunc(),
|
|
|
|
UpdateFunc(),
|
|
|
|
UpdateFunc2(),
|
|
|
|
FinalizeFunc(),
|
|
|
|
GetStringFunc(),
|
|
|
|
ExecuteBangFunc()
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
2011-01-29 00:11:01 +00:00
|
|
|
m_MaxValue = 0.0;
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ~CMeasureMemory
|
|
|
|
**
|
|
|
|
** The destructor
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
CMeasurePlugin::~CMeasurePlugin()
|
|
|
|
{
|
|
|
|
if (m_Plugin)
|
|
|
|
{
|
|
|
|
if(FinalizeFunc) FinalizeFunc(m_Plugin, m_ID);
|
|
|
|
FreeLibrary(m_Plugin);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** Update
|
|
|
|
**
|
|
|
|
** Gets the current value from the plugin
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
bool CMeasurePlugin::Update()
|
|
|
|
{
|
|
|
|
if (!CMeasure::PreUpdate()) return false;
|
|
|
|
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
GetCurrentDirectory(MAX_PATH, buffer);
|
|
|
|
|
2010-12-08 02:46:41 +00:00
|
|
|
std::wstring dir = Rainmeter->GetSkinPath();
|
|
|
|
if (m_MeterWindow)
|
|
|
|
{
|
|
|
|
dir += m_MeterWindow->GetSkinName();
|
|
|
|
}
|
|
|
|
SetCurrentDirectory(dir.c_str());
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
if(UpdateFunc)
|
|
|
|
{
|
|
|
|
// Update the plugin
|
|
|
|
m_Value = UpdateFunc(m_ID);
|
|
|
|
}
|
|
|
|
else if(UpdateFunc2)
|
|
|
|
{
|
|
|
|
// Update the plugin
|
|
|
|
m_Value = UpdateFunc2(m_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
SetCurrentDirectory(buffer);
|
|
|
|
|
|
|
|
return PostUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ReadConfig
|
|
|
|
**
|
|
|
|
** Reads the configs and loads & initializes the plugin
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasurePlugin::ReadConfig(CConfigParser& parser, const WCHAR* section)
|
|
|
|
{
|
|
|
|
static UINT id = 1;
|
|
|
|
|
|
|
|
CMeasure::ReadConfig(parser, section);
|
|
|
|
|
2010-08-17 07:19:48 +00:00
|
|
|
// DynamicVariables is now disabled in MeasurePlugin due to a limitation of the re-initialization.
|
|
|
|
// Do not set m_DynamicVariables to "true".
|
|
|
|
m_DynamicVariables = false;
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
m_PluginName = parser.ReadString(section, L"Plugin", L"");
|
|
|
|
|
|
|
|
size_t pos = m_PluginName.rfind(L".");
|
|
|
|
if (pos == std::wstring::npos)
|
|
|
|
{
|
|
|
|
m_PluginName += L".dll";
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = m_PluginName.rfind(L'\\');
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
2010-11-25 22:00:34 +00:00
|
|
|
m_PluginName.insert(0, L"..\\");
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
2010-11-25 22:00:34 +00:00
|
|
|
m_PluginName.insert(0, Rainmeter->GetPluginPath());
|
2009-02-10 18:37:48 +00:00
|
|
|
|
2010-09-11 19:39:45 +00:00
|
|
|
DWORD err = 0;
|
|
|
|
m_Plugin = CSystem::RmLoadLibrary(m_PluginName.c_str(), &err);
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
if(m_Plugin == NULL)
|
|
|
|
{
|
2010-03-20 19:40:30 +00:00
|
|
|
if (CRainmeter::GetDebug())
|
|
|
|
{
|
2010-12-19 23:06:13 +00:00
|
|
|
LogWithArgs(LOG_ERROR, L"Plugin: Unable to load plugin: \"%s\", ErrorCode=%u", m_PluginName.c_str(), err);
|
2010-03-20 19:40:30 +00:00
|
|
|
}
|
|
|
|
|
2009-02-10 18:37:48 +00:00
|
|
|
// Try to load from Rainmeter's folder
|
|
|
|
pos = m_PluginName.rfind(L'\\');
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
{
|
|
|
|
std::wstring pluginName = Rainmeter->GetPath() + m_PluginName.substr(pos + 1);
|
2010-03-20 19:40:30 +00:00
|
|
|
|
2010-09-11 19:39:45 +00:00
|
|
|
err = 0;
|
|
|
|
m_Plugin = CSystem::RmLoadLibrary(pluginName.c_str(), &err);
|
2010-03-20 19:40:30 +00:00
|
|
|
|
|
|
|
if (m_Plugin == NULL)
|
|
|
|
{
|
|
|
|
if (CRainmeter::GetDebug())
|
|
|
|
{
|
2010-12-19 23:06:13 +00:00
|
|
|
LogWithArgs(LOG_ERROR, L"Plugin: Unable to load plugin: \"%s\", ErrorCode=%u", pluginName.c_str(), err);
|
2010-03-20 19:40:30 +00:00
|
|
|
}
|
|
|
|
}
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_Plugin == NULL)
|
|
|
|
{
|
2010-11-25 22:00:34 +00:00
|
|
|
std::wstring error = L"Rainmeter plugin " + m_PluginName;
|
2010-11-25 15:34:49 +00:00
|
|
|
error += L" not found!";
|
|
|
|
throw CError(error, __LINE__, __FILE__);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
InitializeFunc = (INITIALIZE)GetProcAddress(m_Plugin, "Initialize");
|
|
|
|
FinalizeFunc = (FINALIZE)GetProcAddress(m_Plugin, "Finalize");
|
|
|
|
UpdateFunc = (UPDATE)GetProcAddress(m_Plugin, "Update");
|
|
|
|
UpdateFunc2 = (UPDATE2)GetProcAddress(m_Plugin, "Update2");
|
|
|
|
GetStringFunc = (GETSTRING)GetProcAddress(m_Plugin, "GetString");
|
|
|
|
ExecuteBangFunc = (EXECUTEBANG)GetProcAddress(m_Plugin, "ExecuteBang");
|
|
|
|
|
|
|
|
if (UpdateFunc == NULL && UpdateFunc2 == NULL && GetStringFunc == NULL)
|
|
|
|
{
|
|
|
|
FreeLibrary(m_Plugin);
|
2010-11-25 15:34:49 +00:00
|
|
|
|
2010-11-25 22:00:34 +00:00
|
|
|
std::wstring error = L"Rainmeter plugin " + m_PluginName;
|
2010-11-25 15:34:49 +00:00
|
|
|
error += L" doesn't export Update or GetString function!";
|
|
|
|
throw CError(error, __LINE__, __FILE__);
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the plugin
|
|
|
|
m_ID = id++;
|
|
|
|
if(InitializeFunc)
|
|
|
|
{
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
GetCurrentDirectory(MAX_PATH, buffer);
|
|
|
|
|
2010-12-08 02:46:41 +00:00
|
|
|
std::wstring dir = Rainmeter->GetSkinPath();
|
|
|
|
if (m_MeterWindow)
|
|
|
|
{
|
|
|
|
dir += m_MeterWindow->GetSkinName();
|
|
|
|
}
|
|
|
|
SetCurrentDirectory(dir.c_str());
|
2010-09-11 19:39:45 +00:00
|
|
|
|
|
|
|
// Remove current directory from DLL search path
|
|
|
|
CSystem::RmSetDllDirectory(L"");
|
2009-02-10 18:37:48 +00:00
|
|
|
|
|
|
|
double maxValue;
|
|
|
|
maxValue = InitializeFunc(m_Plugin, parser.GetFilename().c_str(), section, m_ID);
|
|
|
|
|
|
|
|
SetCurrentDirectory(buffer);
|
|
|
|
|
|
|
|
std::wstring szMaxValue = parser.ReadString(section, L"MaxValue", L"NotSet");
|
|
|
|
if (szMaxValue == L"NotSet")
|
|
|
|
{
|
|
|
|
m_MaxValue = maxValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(m_MaxValue == 0)
|
|
|
|
{
|
|
|
|
m_MaxValue = 1;
|
|
|
|
m_LogMaxValue = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** GetStringValue
|
|
|
|
**
|
|
|
|
** Gets the string value from the plugin.
|
|
|
|
**
|
|
|
|
*/
|
2011-01-19 15:31:45 +00:00
|
|
|
const WCHAR* CMeasurePlugin::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
|
2009-02-10 18:37:48 +00:00
|
|
|
{
|
|
|
|
if(GetStringFunc)
|
|
|
|
{
|
|
|
|
const WCHAR* ret = GetStringFunc(m_ID, 0);
|
|
|
|
if (ret) return CheckSubstitute(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
return CMeasure::GetStringValue(autoScale, scale, decimals, percentual);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
** ExecuteBang
|
|
|
|
**
|
|
|
|
** Sends a bang to the plugin
|
|
|
|
**
|
|
|
|
*/
|
|
|
|
void CMeasurePlugin::ExecuteBang(const WCHAR* args)
|
|
|
|
{
|
|
|
|
if (ExecuteBangFunc)
|
|
|
|
{
|
|
|
|
ExecuteBangFunc(args, m_ID);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-12-19 23:06:13 +00:00
|
|
|
LogWithArgs(LOG_WARNING, L"[%s] doesn't support bangs.", m_Name.c_str());
|
2009-02-10 18:37:48 +00:00
|
|
|
}
|
|
|
|
}
|