rainmeter-studio/Library/MeasurePlugin.cpp

255 lines
5.3 KiB
C++
Raw Normal View History

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.
*/
#include "StdAfx.h"
2009-02-10 18:37:48 +00:00
#include "MeasurePlugin.h"
#include "Rainmeter.h"
2011-12-30 16:16:22 +00:00
#include "Export.h"
#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),
m_Plugin(),
2011-12-30 16:16:22 +00:00
m_ReloadFunc(),
m_ID(),
2011-12-30 16:16:22 +00:00
m_UpdateFunc(),
m_GetStringFunc(),
m_ExecuteBangFunc()
2009-02-10 18:37:48 +00:00
{
m_MaxValue = 0.0;
2009-02-10 18:37:48 +00:00
}
/*
** ~CMeasureMemory
**
** The destructor
**
*/
2011-03-29 19:21:57 +00:00
CMeasurePlugin::~CMeasurePlugin()
2009-02-10 18:37:48 +00:00
{
if (m_Plugin)
{
2011-12-30 16:16:22 +00:00
FARPROC finalizeFunc = GetProcAddress(m_Plugin, "Finalize");
if (IsNewApi())
{
((NEWFINALIZE)finalizeFunc)(m_PluginData);
}
else if (finalizeFunc)
{
((FINALIZE)finalizeFunc)(m_Plugin, m_ID);
}
2009-02-10 18:37:48 +00:00
FreeLibrary(m_Plugin);
}
}
/*
** Update
**
** Gets the current value from the plugin
**
*/
bool CMeasurePlugin::Update()
{
if (!CMeasure::PreUpdate()) return false;
2011-12-30 16:16:22 +00:00
if (IsNewApi())
2009-02-10 18:37:48 +00:00
{
2011-12-30 16:16:22 +00:00
m_Value = ((NEWUPDATE)m_UpdateFunc)(m_PluginData);
2009-02-10 18:37:48 +00:00
}
2011-12-30 16:16:22 +00:00
else if (m_UpdateFunc)
2009-02-10 18:37:48 +00:00
{
2011-12-30 16:16:22 +00:00
if (m_Update2)
{
m_Value = ((UPDATE2)m_UpdateFunc)(m_ID);
}
else
{
m_Value = ((UPDATE)m_UpdateFunc)(m_ID);
}
2009-02-10 18:37:48 +00:00
}
// Reset to default
CSystem::ResetWorkingDirectory();
2009-02-10 18:37:48 +00:00
return PostUpdate();
}
/*
** ReadConfig
**
** Reads the configs and loads & initializes the plugin
**
*/
void CMeasurePlugin::ReadConfig(CConfigParser& parser, const WCHAR* section)
{
2011-12-30 16:16:22 +00:00
static UINT id = 0;
2009-02-10 18:37:48 +00:00
CMeasure::ReadConfig(parser, section);
if (m_Initialized)
{
2011-12-30 16:16:22 +00:00
if (IsNewApi())
{
((NEWRELOAD)m_ReloadFunc)(m_PluginData, this, &m_MaxValue);
}
// DynamicVariables doesn't work with old plugins
return;
}
2010-08-17 07:19:48 +00:00
2009-02-10 18:37:48 +00:00
m_PluginName = parser.ReadString(section, L"Plugin", L"");
2011-12-04 22:18:40 +00:00
size_t pos = m_PluginName.rfind(L'.');
2011-03-29 19:21:57 +00:00
if (pos == std::wstring::npos)
2009-02-10 18:37:48 +00:00
{
m_PluginName += L".dll";
}
pos = m_PluginName.rfind(L'\\');
2011-03-29 19:21:57 +00:00
if (pos != std::wstring::npos)
2009-02-10 18:37:48 +00:00
{
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
2011-12-30 16:16:22 +00:00
m_Plugin = CSystem::RmLoadLibrary(m_PluginName.c_str(), NULL);
2011-03-29 19:21:57 +00:00
if (m_Plugin == NULL)
2009-02-10 18:37:48 +00:00
{
// Try to load from Rainmeter's folder
pos = m_PluginName.rfind(L'\\');
2011-03-29 19:21:57 +00:00
if (pos != std::wstring::npos)
2009-02-10 18:37:48 +00:00
{
2011-07-18 00:32:09 +00:00
std::wstring pluginName = Rainmeter->GetPath();
pluginName.append(m_PluginName, pos + 1, m_PluginName.length() - (pos + 1));
2011-12-30 16:16:22 +00:00
m_Plugin = CSystem::RmLoadLibrary(pluginName.c_str(), NULL);
2009-02-10 18:37:48 +00:00
}
if (m_Plugin == NULL)
{
std::wstring error = L"Plugin: \"" + m_PluginName;
error += L"\" not found";
2011-11-09 09:27:06 +00:00
throw CError(error);
2009-02-10 18:37:48 +00:00
}
}
2011-03-29 19:21:57 +00:00
2011-12-30 16:16:22 +00:00
FARPROC initializeFunc = GetProcAddress(m_Plugin, "Initialize");
m_ReloadFunc = GetProcAddress(m_Plugin, "Reload");
m_UpdateFunc = GetProcAddress(m_Plugin, "Update");
m_GetStringFunc = GetProcAddress(m_Plugin, "GetString");
m_ExecuteBangFunc = GetProcAddress(m_Plugin, "ExecuteBang");
2009-02-10 18:37:48 +00:00
2011-12-30 16:16:22 +00:00
// Remove current directory from DLL search path
SetDllDirectory(L"");
2010-11-25 15:34:49 +00:00
2011-12-30 16:16:22 +00:00
if (IsNewApi())
{
((NEWINITIALIZE)initializeFunc)(&m_PluginData);
((NEWRELOAD)m_ReloadFunc)(m_PluginData, this, &m_MaxValue);
2009-02-10 18:37:48 +00:00
}
2011-12-30 16:16:22 +00:00
else
2009-02-10 18:37:48 +00:00
{
2011-12-30 16:16:22 +00:00
m_ID = id;
2011-09-29 17:14:51 +00:00
2011-12-30 16:16:22 +00:00
if (!m_UpdateFunc)
{
m_UpdateFunc = GetProcAddress(m_Plugin, "Update2");
m_Update2 = true;
}
2009-02-10 18:37:48 +00:00
2011-12-30 16:16:22 +00:00
double maxValue = ((INITIALIZE)initializeFunc)(m_Plugin, parser.GetFilename().c_str(), section, m_ID);
2009-02-10 18:37:48 +00:00
const std::wstring& szMaxValue = parser.ReadString(section, L"MaxValue", L"");
if (szMaxValue.empty())
2009-02-10 18:37:48 +00:00
{
m_MaxValue = maxValue;
}
2011-12-30 16:16:22 +00:00
if (m_MaxValue == 0)
{
m_MaxValue = 1;
m_LogMaxValue = true;
}
2009-02-10 18:37:48 +00:00
}
2011-12-30 16:16:22 +00:00
// Reset to default
SetDllDirectory(L"");
CSystem::ResetWorkingDirectory();
++id;
2009-02-10 18:37:48 +00:00
}
/*
** GetStringValue
**
** Gets the string value from the plugin.
**
*/
const WCHAR* CMeasurePlugin::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
2009-02-10 18:37:48 +00:00
{
2011-12-30 16:16:22 +00:00
if (m_GetStringFunc)
2009-02-10 18:37:48 +00:00
{
2011-12-30 16:16:22 +00:00
const WCHAR* ret;
if (IsNewApi())
{
ret = ((NEWGETSTRING)m_GetStringFunc)(m_PluginData);
}
else
{
ret = ((GETSTRING)m_GetStringFunc)(m_ID, 0);
}
2009-02-10 18:37:48 +00:00
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)
{
2011-12-30 16:16:22 +00:00
if (m_ExecuteBangFunc)
2009-02-10 18:37:48 +00:00
{
2011-12-30 16:16:22 +00:00
if (IsNewApi())
{
((NEWEXECUTEBANG)m_ExecuteBangFunc)(m_PluginData, args);
}
else
{
((EXECUTEBANG)m_ExecuteBangFunc)(args, m_ID);
}
2009-02-10 18:37:48 +00:00
}
else
{
CMeasure::ExecuteBang(args);
2009-02-10 18:37:48 +00:00
}
2011-12-30 16:16:22 +00:00
}