2012-01-23 06:36:15 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2010 Matt King, Birunthan Mohanathas
|
|
|
|
|
|
|
|
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2011-07-05 13:41:05 +00:00
|
|
|
#include "../../StdAfx.h"
|
|
|
|
#include "../LuaManager.h"
|
|
|
|
#include "../../Rainmeter.h"
|
|
|
|
#include "../../MeterWindow.h"
|
|
|
|
#include "../../MeterString.h"
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
extern Rainmeter* g_Rainmeter;
|
2012-03-09 10:28:25 +00:00
|
|
|
|
2012-09-02 07:26:09 +00:00
|
|
|
#define DECLARE_SELF(L) \
|
|
|
|
void* selfData = lua_touserdata(L, 1); \
|
|
|
|
if (!selfData) return 0; \
|
2013-05-31 14:18:52 +00:00
|
|
|
MeterWindow* self = *(MeterWindow**)selfData;
|
2012-03-09 10:28:25 +00:00
|
|
|
|
|
|
|
static int Bang(lua_State* L)
|
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2013-05-31 14:18:52 +00:00
|
|
|
ConfigParser& parser = self->GetParser();
|
2012-05-08 14:11:22 +00:00
|
|
|
|
|
|
|
std::wstring bang = LuaManager::ToWide(L, 2);
|
|
|
|
|
|
|
|
int top = lua_gettop(L);
|
|
|
|
if (top == 2) // 1 argument
|
|
|
|
{
|
|
|
|
parser.ReplaceVariables(bang);
|
2013-05-31 14:18:52 +00:00
|
|
|
g_Rainmeter->ExecuteCommand(bang.c_str(), self);
|
2012-05-08 14:11:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-08 16:46:18 +00:00
|
|
|
const WCHAR* bangSz = bang.c_str();
|
|
|
|
if (*bangSz == L'!')
|
2012-05-08 14:11:22 +00:00
|
|
|
{
|
2012-05-08 16:46:18 +00:00
|
|
|
++bangSz; // Skip "!"
|
|
|
|
std::vector<std::wstring> args;
|
|
|
|
for (int i = 3; i <= top; ++i)
|
|
|
|
{
|
|
|
|
std::wstring tmpSz = LuaManager::ToWide(L, i);
|
|
|
|
parser.ReplaceVariables(tmpSz);
|
|
|
|
args.push_back(tmpSz);
|
|
|
|
}
|
2012-05-08 14:11:22 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
g_Rainmeter->ExecuteBang(bangSz, args, self);
|
2012-05-08 16:46:18 +00:00
|
|
|
}
|
2012-05-08 14:11:22 +00:00
|
|
|
}
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetMeter(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-08 15:46:36 +00:00
|
|
|
const std::wstring meterName = LuaManager::ToWide(L, 2);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Meter* meter = self->GetMeter(meterName);
|
2012-03-09 10:28:25 +00:00
|
|
|
if (meter)
|
2012-03-08 15:46:36 +00:00
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
*(Meter**)lua_newuserdata(L, sizeof(Meter*)) = meter;
|
|
|
|
lua_getglobal(L, "Meter");
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_setmetatable(L, -2);
|
2012-07-18 15:10:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
2012-03-08 15:46:36 +00:00
|
|
|
}
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-07-18 15:10:25 +00:00
|
|
|
return 1;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetMeasure(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-08 15:46:36 +00:00
|
|
|
const std::wstring measureName = LuaManager::ToWide(L, 2);
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
Measure* measure = self->GetMeasure(measureName);
|
2012-03-09 10:28:25 +00:00
|
|
|
if (measure)
|
|
|
|
{
|
2013-05-31 14:18:52 +00:00
|
|
|
*(Measure**)lua_newuserdata(L, sizeof(Measure*)) = measure;
|
|
|
|
lua_getglobal(L, "Measure");
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_setmetatable(L, -2);
|
2012-07-18 15:10:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
2012-03-09 10:28:25 +00:00
|
|
|
}
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-07-18 15:10:25 +00:00
|
|
|
return 1;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetVariable(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-08 15:46:36 +00:00
|
|
|
std::wstring strTmp = LuaManager::ToWide(L, 2);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-08-05 13:12:36 +00:00
|
|
|
const std::wstring* value = self->GetParser().GetVariable(strTmp);
|
|
|
|
if (value)
|
2012-03-08 15:46:36 +00:00
|
|
|
{
|
2013-01-27 10:17:38 +00:00
|
|
|
LuaManager::PushWide(L, *value);
|
2012-07-18 15:10:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lua_pushnil(L);
|
2012-03-08 15:46:36 +00:00
|
|
|
}
|
2012-03-09 10:28:25 +00:00
|
|
|
|
2012-07-18 15:10:25 +00:00
|
|
|
return 1;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int ReplaceVariables(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-08 15:46:36 +00:00
|
|
|
std::wstring strTmp = LuaManager::ToWide(L, 2);
|
|
|
|
|
|
|
|
self->GetParser().ReplaceVariables(strTmp);
|
2012-08-01 17:40:50 +00:00
|
|
|
self->GetParser().ReplaceMeasures(strTmp);
|
2013-01-27 10:17:38 +00:00
|
|
|
LuaManager::PushWide(L, strTmp);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int ParseFormula(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-08 15:46:36 +00:00
|
|
|
std::wstring strTmp = LuaManager::ToWide(L, 2);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-03-08 15:46:36 +00:00
|
|
|
double result;
|
2012-03-12 18:11:10 +00:00
|
|
|
if (!self->GetParser().ParseFormula(strTmp, &result))
|
2012-03-08 15:46:36 +00:00
|
|
|
{
|
2012-03-12 18:11:10 +00:00
|
|
|
result = lua_tonumber(L, 2);
|
2012-03-08 15:46:36 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 18:11:10 +00:00
|
|
|
lua_pushnumber(L, result);
|
|
|
|
|
|
|
|
return 1;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int MoveWindow(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
int x = (int)lua_tonumber(L, 2);
|
|
|
|
int y = (int)lua_tonumber(L, 3);
|
2012-03-08 15:46:36 +00:00
|
|
|
self->MoveWindow(x, y);
|
|
|
|
|
|
|
|
return 0;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int FadeWindow(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
int from = (int)lua_tonumber(L, 2);
|
|
|
|
int to = (int)lua_tonumber(L, 3);
|
2012-03-08 15:46:36 +00:00
|
|
|
self->FadeWindow(from, to);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-03-08 15:46:36 +00:00
|
|
|
return 0;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetW(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_pushnumber(L, self->GetW());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetH(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_pushnumber(L, self->GetH());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetX(lua_State* L)
|
2011-07-06 20:12:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_pushnumber(L, self->GetX());
|
2011-07-06 20:12:05 +00:00
|
|
|
|
2012-03-08 15:46:36 +00:00
|
|
|
return 1;
|
2011-07-06 20:12:05 +00:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int GetY(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_pushnumber(L, self->GetY());
|
2011-07-05 13:41:05 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:28:25 +00:00
|
|
|
static int MakePathAbsolute(lua_State* L)
|
2011-07-05 13:41:05 +00:00
|
|
|
{
|
2012-09-02 07:26:09 +00:00
|
|
|
DECLARE_SELF(L)
|
2012-03-08 15:46:36 +00:00
|
|
|
std::wstring path = LuaManager::ToWide(L, 2);
|
|
|
|
self->MakePathAbsolute(path);
|
2013-01-27 10:17:38 +00:00
|
|
|
LuaManager::PushWide(L, path);
|
2011-07-05 13:41:05 +00:00
|
|
|
|
2012-03-08 15:46:36 +00:00
|
|
|
return 1;
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LuaManager::RegisterMeterWindow(lua_State* L)
|
|
|
|
{
|
2012-03-09 13:19:49 +00:00
|
|
|
const luaL_Reg functions[] =
|
2012-03-09 10:28:25 +00:00
|
|
|
{
|
|
|
|
{ "Bang", Bang },
|
|
|
|
{ "GetMeter", GetMeter },
|
|
|
|
{ "GetMeasure", GetMeasure },
|
|
|
|
{ "GetVariable", GetVariable },
|
|
|
|
{ "ReplaceVariables", ReplaceVariables },
|
|
|
|
{ "ParseFormula", ParseFormula },
|
|
|
|
{ "MoveWindow", MoveWindow },
|
|
|
|
{ "FadeWindow", FadeWindow },
|
|
|
|
{ "GetW", GetW },
|
|
|
|
{ "GetH", GetH },
|
|
|
|
{ "GetX", GetX },
|
|
|
|
{ "GetY", GetY },
|
|
|
|
{ "MakePathAbsolute", MakePathAbsolute },
|
|
|
|
{ NULL, NULL }
|
|
|
|
};
|
|
|
|
|
2013-05-31 14:18:52 +00:00
|
|
|
luaL_register(L, "MeterWindow", functions);
|
2012-03-09 10:28:25 +00:00
|
|
|
lua_pushvalue(L, -1);
|
|
|
|
lua_setfield(L, -2, "__index");
|
2012-03-10 07:03:19 +00:00
|
|
|
lua_pop(L, 1);
|
2011-07-05 13:41:05 +00:00
|
|
|
}
|