Exemple #1
0
void LuaManager::ReportErrors(lua_State* L)
{
	const char* error = lua_tostring(L, -1);
	lua_pop(L, 1);

	LogWithArgs(LOG_ERROR, L"Script: %S", error);
}
Exemple #2
0
/*
** Read the options specified in the ini file.
**
*/
void CMeasureCalc::ReadOptions(CConfigParser& parser, const WCHAR* section)
{
	CMeasure::ReadOptions(parser, section);

	// Store the current values so we know if the value needs to be updated
	int oldLowBound = m_LowBound;
	int oldHighBound = m_HighBound;
	bool oldUpdateRandom = m_UpdateRandom;

	std::wstring oldFormula = m_Formula;
	m_Formula = parser.ReadString(section, L"Formula", L"");

	m_LowBound = parser.ReadInt(section, L"LowBound", 0);
	m_HighBound = parser.ReadInt(section, L"HighBound", 100);
	m_UpdateRandom = 0!=parser.ReadInt(section, L"UpdateRandom", 0);

	if (!m_Initialized ||
		wcscmp(m_Formula.c_str(), oldFormula.c_str()) != 0 ||
		oldLowBound != m_LowBound ||
		oldHighBound != m_HighBound ||
		oldUpdateRandom != m_UpdateRandom)
	{
		if (!m_UpdateRandom)
		{
			FormulaReplace();
		}

		const WCHAR* errMsg = MathParser::Check(m_Formula.c_str());
		if (errMsg != NULL)
		{
			LogWithArgs(LOG_ERROR, L"Calc: %s in [%s]", errMsg, m_Name.c_str());
			m_Formula.clear();
		}
	}
}
Exemple #3
0
/*
** Returns the uptime as string.
**
*/
const WCHAR* CMeasureUptime::GetStringValue()
{
	static WCHAR buffer[MAX_LINE_LENGTH];

	size_t value = (size_t)m_Value;
	size_t time[4];

	time[0] = value % 60;
	time[1] = (value / 60) % 60;
	time[2] = (value / (60 * 60));
	time[3] = (value / (60 * 60 * 24));

	if (!m_AddDaysToHours)
	{
		time[2] %= 24;
	}

	__try
	{
		FormatMessage(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY, m_Format.c_str(), 0, 0, buffer, MAX_LINE_LENGTH, (char**)time);
	}
	__except (EXCEPTION_EXECUTE_HANDLER)
	{
		LogWithArgs(LOG_ERROR, L"Uptime: \"Format=%s\" invalid in [%s]", m_Format.c_str(), m_Name.c_str());
		buffer[0] = 0;
	}

	return CheckSubstitute(buffer);
}
Exemple #4
0
/*
** Updates the calculation
**
*/
void CMeasureCalc::UpdateValue()
{
	const WCHAR* errMsg = MathParser::Parse(m_Formula.c_str(), this, &m_Value);
	if (errMsg != NULL)
	{
		if (!m_ParseError)
		{
			LogWithArgs(LOG_ERROR, L"Calc: %s in [%s]", errMsg, m_Name.c_str());
			m_ParseError = true;
		}
	}
	else
	{
		m_ParseError = false;
	}
}
/*
** Read the options specified in the ini file.
**
*/
void CMeasureRegistry::ReadOptions(CConfigParser& parser, const WCHAR* section)
{
	CMeasure::ReadOptions(parser, section);

	const WCHAR* keyname = parser.ReadString(section, L"RegHKey", L"HKEY_CURRENT_USER").c_str();
	if (_wcsicmp(keyname, L"HKEY_CURRENT_USER") == 0)
	{
		m_HKey = HKEY_CURRENT_USER;
	}
	else if (_wcsicmp(keyname, L"HKEY_LOCAL_MACHINE") == 0)
	{
		m_HKey = HKEY_LOCAL_MACHINE;
	}
	else if (_wcsicmp(keyname, L"HKEY_CLASSES_ROOT") == 0)
	{
		m_HKey = HKEY_CLASSES_ROOT;
	}
	else if (_wcsicmp(keyname, L"HKEY_CURRENT_CONFIG") == 0)
	{
		m_HKey = HKEY_CURRENT_CONFIG;
	}
	else if (_wcsicmp(keyname, L"HKEY_PERFORMANCE_DATA") == 0)
	{
		m_HKey = HKEY_PERFORMANCE_DATA;
	}
	else if (_wcsicmp(keyname, L"HKEY_DYN_DATA") == 0)
	{
		m_HKey = HKEY_DYN_DATA;
	}
	else
	{
		LogWithArgs(LOG_ERROR, L"RegHKey=%s is not valid in [%s]", keyname, m_Name.c_str());
	}

	m_RegKeyName = parser.ReadString(section, L"RegKey", L"");
	m_RegValueName = parser.ReadString(section, L"RegValue", L"");

	if (m_MaxValue == 0.0)
	{
		m_MaxValue = 1.0;
		m_LogMaxValue = true;
	}

	// Try to open the key
	if (m_RegKey) RegCloseKey(m_RegKey);
	RegOpenKeyEx(m_HKey, m_RegKeyName.c_str(), 0, KEY_READ, &m_RegKey);
}
Exemple #6
0
/*
** Converts given time to string.
** This function is a wrapper function for wcsftime.
**
*/
void CMeasureTime::TimeToString(WCHAR* buf, size_t bufLen, const WCHAR* format, const struct tm* time)
{
	if (bufLen > 0)
	{
		_invalid_parameter_handler oldHandler = _set_invalid_parameter_handler(RmNullCRTInvalidParameterHandler);
		_CrtSetReportMode(_CRT_ASSERT, 0);

		errno = 0;
		wcsftime(buf, bufLen, format, time);
		if (errno == EINVAL)
		{
			LogWithArgs(LOG_ERROR, L"Time: \"Format=%s\" invalid in [%s]", format, m_Name.c_str());
			buf[0] = 0;
		}

		_set_invalid_parameter_handler(oldHandler);
	}
}
Exemple #7
0
/*
** Reads the measure specific configs.
**
*/
void CMeasureCPU::ReadConfig(CConfigParser& parser, const WCHAR* section)
{
    CMeasure::ReadConfig(parser, section);

    int processor = parser.ReadInt(section, L"Processor", 0);

    if (processor < 0 || processor > c_NumOfProcessors)
    {
        LogWithArgs(LOG_WARNING, L"CPU: Processor=%i invalid in [%s]", m_Processor, section);
        processor = 0;
    }

    if (processor != m_Processor)
    {
        m_Processor = processor;
        m_OldTime[0] = m_OldTime[1] = 0.0;
    }
}
Exemple #8
0
void LuaManager::ReportErrors(lua_State* L)
{
	const char* error = lua_tostring(L, -1);
	lua_pop(L, 1);

	const char* pos = error + 4; // Skip the drive

	// Get rid of everything up to the filename
	while (*pos != ':')
	{
		if (*pos == '\\')
		{
			error = pos + 1;
		}
		++pos;
	}

	LogWithArgs(LOG_ERROR, L"Script: %S", error);
}
Exemple #9
0
/*
** Executes a custom bang.
**
*/
void CMeasure::Command(const std::wstring& command)
{
	LogWithArgs(LOG_WARNING, L"!CommandMeasure: Not supported by [%s]", m_Name.c_str());
}
Exemple #10
0
/*
** Reads the common configs for all Measures. The inherited classes
** must call the base implementation if they overwrite this method.
**
*/
void CMeasure::ReadConfig(CConfigParser& parser, const WCHAR* section)
{
	// Clear substitutes to prevent from being added more than once.
	if (!m_Substitute.empty())
	{
		m_Substitute.clear();
	}

	m_Invert = 0!=parser.ReadInt(section, L"InvertMeasure", 0);

	if (!m_Initialized)
	{
		m_Disabled = 0!=parser.ReadInt(section, L"Disabled", 0);
	}
	else
	{
		const std::wstring& result = parser.ReadString(section, L"Disabled", L"0");
		if (parser.GetLastReplaced())
		{
			m_Disabled = 0!=parser.ParseInt(result.c_str(), 0);
		}
	}

	int updateDivider = parser.ReadInt(section, L"UpdateDivider", 1);
	if (updateDivider != m_UpdateDivider)
	{
		m_UpdateCounter = m_UpdateDivider = updateDivider;
	}

	m_MinValue = parser.ReadFormula(section, L"MinValue", m_MinValue);
	m_MaxValue = parser.ReadFormula(section, L"MaxValue", m_MaxValue);

	// The ifabove/ifbelow define actions that are ran when the value goes above/below the given number.

	m_IfAboveValue = parser.ReadFormula(section, L"IfAboveValue", 0.0);
	m_IfAboveAction = parser.ReadString(section, L"IfAboveAction", L"", false);

	m_IfBelowValue = parser.ReadFormula(section, L"IfBelowValue", 0.0);
	m_IfBelowAction = parser.ReadString(section, L"IfBelowAction", L"", false);

	m_IfEqualValue = parser.ReadFormula(section, L"IfEqualValue", 0.0);
	m_IfEqualAction = parser.ReadString(section, L"IfEqualAction", L"", false);

	m_AverageSize = parser.ReadUInt(section, L"AverageSize", 0);

	m_DynamicVariables = 0!=parser.ReadInt(section, L"DynamicVariables", 0);

	m_RegExpSubstitute = 0!=parser.ReadInt(section, L"RegExpSubstitute", 0);
	std::wstring subs = parser.ReadString(section, L"Substitute", L"");
	if (!subs.empty())
	{
		if ((subs[0] != L'"' || subs[subs.length() - 1] != L'\'') &&
			(subs[0] != L'\'' || subs[subs.length() - 1] != L'"'))
		{
			// Add quotes since they are removed by the GetProfileString
			subs.insert(0, 1, L'"');
			subs += L'"';
		}
		if (!ParseSubstitute(subs))
		{
			LogWithArgs(LOG_ERROR, L"Measure: Invalid Substitute=%s", subs.c_str());
		}
	}

	const std::wstring& group = parser.ReadString(section, L"Group", L"");
	InitializeGroup(group);
}
Exemple #11
0
/*
** Reads the tables for all net interfaces
**
*/
void CMeasureNet::UpdateIFTable()
{
	bool logging = false;

	if (c_GetIfTable2)
	{
		if (c_Table)
		{
			c_FreeMibTable(c_Table);
			c_Table = NULL;
		}

		if (c_GetIfTable2((MIB_IF_TABLE2**)&c_Table) == NO_ERROR)
		{
			MIB_IF_TABLE2* ifTable = (MIB_IF_TABLE2*)c_Table;

			if (c_NumOfTables != ifTable->NumEntries)
			{
				c_NumOfTables = ifTable->NumEntries;
				logging = true;
			}

			if (Rainmeter->GetDebug() && logging)
			{
				Log(LOG_DEBUG, L"------------------------------");
				LogWithArgs(LOG_DEBUG, L"* NETWORK-INTERFACE: Count=%i", c_NumOfTables);

				for (size_t i = 0; i < c_NumOfTables; ++i)
				{
					const WCHAR* type = L"Other";
					switch (ifTable->Table[i].Type)
					{
					case IF_TYPE_ETHERNET_CSMACD:
						type = L"Ethernet";
						break;
					case IF_TYPE_PPP:
						type = L"PPP";
						break;
					case IF_TYPE_SOFTWARE_LOOPBACK:
						type = L"Loopback";
						break;
					case IF_TYPE_IEEE80211:
						type = L"IEEE802.11";
						break;
					case IF_TYPE_TUNNEL:
						type = L"Tunnel";
						break;
					case IF_TYPE_IEEE1394:
						type = L"IEEE1394";
						break;
					}

					LogWithArgs(LOG_DEBUG, L"%i: %s", (int)i + 1, ifTable->Table[i].Description);
					LogWithArgs(LOG_DEBUG, L"  Alias: %s", ifTable->Table[i].Alias);
					LogWithArgs(LOG_DEBUG, L"  Type=%s(%i), Hardware=%s, Filter=%s",
						type, ifTable->Table[i].Type,
						(ifTable->Table[i].InterfaceAndOperStatusFlags.HardwareInterface == 1) ? L"Yes" : L"No",
						(ifTable->Table[i].InterfaceAndOperStatusFlags.FilterInterface == 1) ? L"Yes" : L"No");
				}
				Log(LOG_DEBUG, L"------------------------------");
			}
		}
		else
		{
			// Something's wrong. Unable to get the table.
			c_Table = NULL;
			c_NumOfTables = 0;
		}
	}
	else
	{
		if (c_Table == NULL)
		{
			// Gotta reserve few bytes for the tables
			DWORD value = 0;
			if (GetNumberOfInterfaces(&value) == NO_ERROR)
			{
				if (c_NumOfTables != value)
				{
					c_NumOfTables = value;
					logging = true;
				}

				if (c_NumOfTables > 0)
				{
					DWORD size = sizeof(MIB_IFTABLE) + sizeof(MIB_IFROW) * c_NumOfTables;
					c_Table = new BYTE[size];
				}
			}
		}

		if (c_Table)
		{
			DWORD ret, size = 0;

			MIB_IFTABLE* ifTable = (MIB_IFTABLE*)c_Table;

			if ((ret = GetIfTable(ifTable, &size, FALSE)) == ERROR_INSUFFICIENT_BUFFER)
			{
				delete [] c_Table;
				c_Table = new BYTE[size];

				ifTable = (MIB_IFTABLE*)c_Table;

				ret = GetIfTable(ifTable, &size, FALSE);
			}

			if (ret == NO_ERROR)
			{
				if (c_NumOfTables != ifTable->dwNumEntries)
				{
					c_NumOfTables = ifTable->dwNumEntries;
					logging = true;
				}

				if (Rainmeter->GetDebug() && logging)
				{
					Log(LOG_DEBUG, L"------------------------------");
					LogWithArgs(LOG_DEBUG, L"* NETWORK-INTERFACE: Count=%i", c_NumOfTables);

					for (size_t i = 0; i < c_NumOfTables; ++i)
					{
						const WCHAR* type = L"";
						switch (ifTable->table[i].dwType)
						{
						case IF_TYPE_ETHERNET_CSMACD:
							type = L"Ethernet";
							break;
						case IF_TYPE_PPP:
							type = L"PPP";
							break;
						case IF_TYPE_SOFTWARE_LOOPBACK:
							type = L"Loopback";
							break;
						case IF_TYPE_IEEE80211:
							type = L"IEEE802.11";
							break;
						case IF_TYPE_TUNNEL:
							type = L"Tunnel";
							break;
						case IF_TYPE_IEEE1394:
							type = L"IEEE1394";
							break;
						default:
							type = L"Other";
							break;
						}

						LogWithArgs(LOG_DEBUG, L"%i: %.*S", (int)i + 1, ifTable->table[i].dwDescrLen, (char*)ifTable->table[i].bDescr);
						LogWithArgs(LOG_DEBUG, L"  Type=%s(%i)", type, ifTable->table[i].dwType);
					}
					Log(LOG_DEBUG, L"------------------------------");
				}
			}
			else
			{
				// Something's wrong. Unable to get the table.
				delete [] c_Table;
				c_Table = NULL;
				c_NumOfTables = 0;
			}
		}
	}
}
Exemple #12
0
/*
** Called when tab is displayed.
**
*/
void CDialogAbout::CTabPlugins::Initialize()
{
	m_Initialized = true;

	// Add columns to the list view
	HWND item = GetDlgItem(m_Window, IDC_ABOUTPLUGINS_ITEMS_LISTVIEW);

	LVCOLUMN lvc;
	lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
	lvc.fmt = LVCFMT_LEFT;  // left-aligned column
	lvc.iSubItem = 0;
	lvc.cx = 140;
	lvc.pszText = GetString(ID_STR_NAME);
	ListView_InsertColumn(item, 0, &lvc);
	lvc.iSubItem = 1;
	lvc.cx = 80;
	lvc.pszText = GetString(ID_STR_VERSION);
	ListView_InsertColumn(item, 1, &lvc);
	lvc.iSubItem = 2;
	lvc.cx = 310;
	lvc.pszText = GetString(ID_STR_AUTHOR);
	ListView_InsertColumn(item, 2, &lvc);

	LVITEM vitem;
	vitem.mask = LVIF_TEXT;
	vitem.iItem = 0;
	vitem.iSubItem = 0;

	// Scan for plugins
	WIN32_FIND_DATA fileData;      // Data structure describes the file found
	HANDLE hSearch;                // Search handle returned by FindFirstFile

	std::wstring files = Rainmeter->GetPluginPath() + L"*.dll";

	// Start searching for .ini files in the given directory.
	hSearch = FindFirstFile(files.c_str(), &fileData);
	int index = 0;
	do
	{
		if (hSearch == INVALID_HANDLE_VALUE) break;    // No more files found

		// Try to get the version and author
		std::wstring tmpSz = Rainmeter->GetPluginPath() + fileData.cFileName;
		const WCHAR* path = tmpSz.c_str();

		vitem.iItem = index;
		vitem.pszText = fileData.cFileName;

		// Try to get version and author from file resources first
		DWORD handle;
		DWORD versionSize = GetFileVersionInfoSize(path, &handle);
		if (versionSize)
		{
			bool found = false;
			void* data = new BYTE[versionSize];
			if (GetFileVersionInfo(path, 0, versionSize, data))
			{
				UINT len;
				struct LANGCODEPAGE
				{
					WORD wLanguage;
					WORD wCodePage;
				} *lcp;

				if (VerQueryValue(data, L"\\VarFileInfo\\Translation", (LPVOID*)&lcp, &len))
				{
					WCHAR key[64];
					LPWSTR value;

					_snwprintf_s(key, _TRUNCATE, L"\\StringFileInfo\\%04x%04x\\ProductName", lcp[0].wLanguage, lcp[0].wCodePage);
					if (VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&value, &len) &&
						wcscmp(value, L"Rainmeter") == 0)
					{
						ListView_InsertItem(item, &vitem);
						++index;
						found = true;

						_snwprintf_s(key, _TRUNCATE, L"\\StringFileInfo\\%04x%04x\\FileVersion", lcp[0].wLanguage, lcp[0].wCodePage);
						if (VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&value, &len))
						{
							ListView_SetItemText(item, vitem.iItem, 1, value);
						}

						_snwprintf_s(key, _TRUNCATE, L"\\StringFileInfo\\%04x%04x\\LegalCopyright", lcp[0].wLanguage, lcp[0].wCodePage);
						if (VerQueryValue(data, (LPTSTR)(LPCTSTR)key, (void**)&value, &len))
						{
							ListView_SetItemText(item, vitem.iItem, 2, value);
						}
					}
				}
			}

			delete [] data;
			if (found) continue;
		}

		// Try old calling GetPluginVersion/GetPluginAuthor for backwards compatibility
		DWORD err = 0;
		HMODULE dll = CSystem::RmLoadLibrary(path, &err, true);
		if (dll)
		{
			ListView_InsertItem(item, &vitem);
			++index;

			GETPLUGINVERSION GetVersionFunc = (GETPLUGINVERSION)GetProcAddress(dll, "GetPluginVersion");
			if (GetVersionFunc)
			{
				UINT version = GetVersionFunc();
				WCHAR buffer[64];
				_snwprintf_s(buffer, _TRUNCATE, L"%u.%u", version / 1000, version % 1000);
				ListView_SetItemText(item, vitem.iItem, 1, buffer);
			}

			GETPLUGINAUTHOR GetAuthorFunc = (GETPLUGINAUTHOR)GetProcAddress(dll, "GetPluginAuthor");
			if (GetAuthorFunc)
			{
				LPCTSTR author = GetAuthorFunc();
				if (author && *author)
				{
					ListView_SetItemText(item, vitem.iItem, 2, (LPWSTR)author);
				}
			}

			FreeLibrary(dll);
		}
		else
		{
			LogWithArgs(LOG_ERROR, L"Unable to load plugin: %s (%u)", tmpSz.c_str(), err);
		}
	}
	while (FindNextFile(hSearch, &fileData));

	FindClose(hSearch);
}
Exemple #13
0
/*
** Creates the given measure. This is the factory method for the measures.
** If new measures are implemented this method needs to be updated.
**
*/
CMeasure* CMeasure::Create(const WCHAR* measure, CMeterWindow* meterWindow, const WCHAR* name)
{
	// Comparison is caseinsensitive

	if (_wcsicmp(L"CPU", measure) == 0)
	{
		return new CMeasureCPU(meterWindow, name);
	}
	else if (_wcsicmp(L"Memory", measure) == 0)
	{
		return new CMeasureMemory(meterWindow, name);
	}
	else if (_wcsicmp(L"NetIn", measure) == 0)
	{
		return new CMeasureNetIn(meterWindow, name);
	}
	else if (_wcsicmp(L"NetOut", measure) == 0)
	{
		return new CMeasureNetOut(meterWindow, name);
	}
	else if (_wcsicmp(L"NetTotal", measure) == 0)
	{
		return new CMeasureNetTotal(meterWindow, name);
	}
	else if (_wcsicmp(L"PhysicalMemory", measure) == 0)
	{
		return new CMeasurePhysicalMemory(meterWindow, name);
	}
	else if (_wcsicmp(L"SwapMemory", measure) == 0)
	{
		return new CMeasureVirtualMemory(meterWindow, name);
	}
	else if (_wcsicmp(L"FreeDiskSpace", measure) == 0)
	{
		return new CMeasureDiskSpace(meterWindow, name);
	}
	else if (_wcsicmp(L"Uptime", measure) == 0)
	{
		return new CMeasureUptime(meterWindow, name);
	}
	else if (_wcsicmp(L"Time", measure) == 0)
	{
		return new CMeasureTime(meterWindow, name);
	}
	else if (_wcsicmp(L"Plugin", measure) == 0)
	{
		return new CMeasurePlugin(meterWindow, name);
	}
	else if (_wcsicmp(L"Registry", measure) == 0)
	{
		return new CMeasureRegistry(meterWindow, name);
	}
	else if (_wcsicmp(L"Calc", measure) == 0)
	{
		return new CMeasureCalc(meterWindow, name);
	}
	else if (_wcsicmp(L"Script", measure) == 0)
	{
		return new CMeasureScript(meterWindow, name);
	}

	LogWithArgs(LOG_ERROR, L"Measure=%s is not valid in [%s]", measure, name);

	return NULL;
}
Exemple #14
0
/*
** Substitutes part of the text
*/
const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
{
	static std::wstring str;

	if (!m_Substitute.empty())
	{
		if (!m_RegExpSubstitute)	// Plain Substitutions only
		{
			str = buffer;

			for (size_t i = 0, isize = m_Substitute.size(); i < isize; i += 2)
			{
				if (!m_Substitute[i].empty())
				{
					MakePlainSubstitute(str, i);
				}
				else if (str.empty())
				{
					// Empty result and empty substitute -> use second
					str = m_Substitute[i + 1];
				}
			}
		}
		else // Contains a RegEx
		{
			std::string utf8str = ConvertToUTF8(buffer);
			int* ovector = new int[OVECCOUNT];

			for (size_t i = 0, isize = m_Substitute.size(); i < isize ; i += 2)
			{
				pcre* re;
				const char* error;
				int erroffset;
				int rc;
				int flags = PCRE_UTF8;
				int offset = 0;

				re = pcre_compile(
					ConvertToUTF8(m_Substitute[i].c_str()).c_str(),   // the pattern
					flags,						// default options
					&error,						// for error message
					&erroffset,					// for error offset
					NULL);						// use default character tables

				if (re == NULL)
				{
					MakePlainSubstitute(str, i);
					LogWithArgs(LOG_NOTICE, L"Substitute: %S", error);
				}
				else
				{
					do
					{
						rc = pcre_exec(
							re,						// the compiled pattern
							NULL,					// no extra data - we didn't study the pattern
							utf8str.c_str(),		// the subject string
							utf8str.length(),		// the length of the subject
							offset,					// start at offset 0 in the subject
							0,						// default options
							ovector,				// output vector for substring information
							OVECCOUNT);				// number of elements in the output vector

						if (rc <= 0)
						{
							break;
						}
						else
						{
							std::string result = ConvertToUTF8(m_Substitute[i + 1].c_str());

							if (rc > 1)
							{
								for (int j = rc - 1 ; j >= 0 ; --j)
								{
									size_t new_start = ovector[2 * j];
									size_t in_length = ovector[2 * j + 1] - ovector[2 * j];

									char tmpName[64];

									size_t cut_length = _snprintf_s(tmpName, _TRUNCATE, "\\%i", j);;
									size_t start = 0, pos;
									do
									{
										pos = result.find(tmpName, start, cut_length);
										if (pos != std::string::npos)
										{
											result.replace(pos, cut_length, utf8str, new_start, in_length);
											start = pos + in_length;
										}
									}
									while (pos != std::string::npos);
								}
							}

							size_t start = ovector[0];
							size_t length = ovector[1] - ovector[0];
							utf8str.replace(start, length, result);
							offset = start + result.length();
						}
					}
					while (true);

					// Release memory used for the compiled pattern
					pcre_free(re);
				}
			}

			delete [] ovector;

			str = ConvertUTF8ToWide(utf8str.c_str());
		}

		return str.c_str();
	}
	else
	{
		return buffer;
	}
}
/*
** Read the options specified in the ini file.
**
*/
void CMeasureScript::ReadOptions(CConfigParser& parser, const WCHAR* section)
{
	CMeasure::ReadOptions(parser, section);

	std::wstring file = parser.ReadString(section, L"ScriptFile", L"");

	if (!file.empty())
	{
		if (m_MeterWindow)
		{
			m_MeterWindow->MakePathAbsolute(file);
		}
		std::string scriptFile = ConvertToAscii(file.c_str());

		if (!m_Initialized ||
			strcmp(scriptFile.c_str(), m_ScriptFile.c_str()) != 0)
		{
			DeleteLuaScript();

			lua_State* L = LuaManager::GetState();
			m_ScriptFile = scriptFile;
			m_LuaScript = new LuaScript(m_ScriptFile.c_str());

			if (m_LuaScript->IsInitialized())
			{
				bool hasInitializeFunction = m_LuaScript->IsFunction(g_InitializeFunctionName);
				m_HasUpdateFunction = m_LuaScript->IsFunction(g_UpdateFunctionName);
				m_HasGetStringFunction = m_LuaScript->IsFunction(g_GetStringFunctionName);  // For backwards compatbility

				if (m_HasGetStringFunction)
				{
					LogWithArgs(LOG_WARNING, L"Script: Using deprecated GetStringValue() in [%s]", m_Name.c_str());
				}

				lua_rawgeti(L, LUA_GLOBALSINDEX, m_LuaScript->GetRef());

				*(CMeterWindow**)lua_newuserdata(L, sizeof(CMeterWindow*)) = m_MeterWindow;
				lua_getglobal(L, "CMeterWindow");
				lua_setmetatable(L, -2);
				lua_setfield(L, -2, "SKIN");

				*(CMeasure**)lua_newuserdata(L, sizeof(CMeasure*)) = this;
				lua_getglobal(L, "CMeasure");
				lua_setmetatable(L, -2);
				lua_setfield(L, -2, "SELF");

				// For backwards compatibility
				lua_getfield(L, -1, "PROPERTIES");
				if (lua_isnil(L, -1) == 0)
				{
					lua_pushnil(L);
					
					// Look in the table for values to read from the section
					while (lua_next(L, -2))
					{
						lua_pop(L, 1);
						const char* strKey = lua_tostring(L, -1);

						std::wstring wstrKey = ConvertToWide(strKey);
						const std::wstring& wstrValue = parser.ReadString(section, wstrKey.c_str(), L"");

						if (!wstrValue.empty())
						{
							std::string strStrVal = ConvertToAscii(wstrValue.c_str());
							const char* strValue = strStrVal.c_str();

							lua_pushstring(L, strValue);
							lua_setfield(L, -3, strKey);
						}
					}
				}

				// Pop PROPERTIES table and our table
				lua_pop(L, 2);

				if (hasInitializeFunction)
				{
					m_LuaScript->RunFunction(g_InitializeFunctionName);
				}
			}
			else
			{
				DeleteLuaScript();
			}
		}
	}
	else
	{
		LogWithArgs(LOG_ERROR, L"Script: File not valid in [%s]", m_Name.c_str());
		DeleteLuaScript();
	}
}
Exemple #16
0
void CMouse::ReadOptions(CConfigParser& parser, const WCHAR* section, CMeterWindow* meterWindow)
{
	DestroyCustomCursor();

	m_LeftDownAction = parser.ReadString(section, L"LeftMouseDownAction", L"", false);
	m_RightDownAction = parser.ReadString(section, L"RightMouseDownAction", L"", false);
	m_MiddleDownAction = parser.ReadString(section, L"MiddleMouseDownAction", L"", false);
	m_X1DownAction = parser.ReadString(section, L"X1MouseDownAction", L"", false);
	m_X2DownAction = parser.ReadString(section, L"X2MouseDownAction", L"", false);
	m_LeftUpAction = parser.ReadString(section, L"LeftMouseUpAction", L"", false);
	m_RightUpAction = parser.ReadString(section, L"RightMouseUpAction", L"", false);
	m_MiddleUpAction = parser.ReadString(section, L"MiddleMouseUpAction", L"", false);
	m_X1UpAction = parser.ReadString(section, L"X1MouseUpAction", L"", false);
	m_X2UpAction = parser.ReadString(section, L"X2MouseUpAction", L"", false);
	m_LeftDoubleClickAction = parser.ReadString(section, L"LeftMouseDoubleClickAction", L"", false);
	m_RightDoubleClickAction = parser.ReadString(section, L"RightMouseDoubleClickAction", L"", false);
	m_MiddleDoubleClickAction = parser.ReadString(section, L"MiddleMouseDoubleClickAction", L"", false);
	m_X1DoubleClickAction = parser.ReadString(section, L"X1MouseDoubleClickAction", L"", false);	
	m_X2DoubleClickAction = parser.ReadString(section, L"X2MouseDoubleClickAction", L"", false);

	m_OverAction = parser.ReadString(section, L"MouseOverAction", L"", false);
	m_LeaveAction = parser.ReadString(section, L"MouseLeaveAction", L"", false);

	m_MouseScrollDownAction = parser.ReadString(section, L"MouseScrollDownAction", L"", false);
	m_MouseScrollUpAction = parser.ReadString(section, L"MouseScrollUpAction", L"", false);
	m_MouseScrollLeftAction = parser.ReadString(section, L"MouseScrollLeftAction", L"", false);
	m_MouseScrollRightAction = parser.ReadString(section, L"MouseScrollRightAction", L"", false);
	if (!m_MouseScrollDownAction.empty() || !m_MouseScrollUpAction.empty() ||
		!m_MouseScrollLeftAction.empty() || !m_MouseScrollRightAction.empty())
	{
		meterWindow->SetHasMouseScrollAction();
	}

	const bool defaultState = (section == L"Rainmeter") ? true : meterWindow->GetMouse().GetCursorState();
	m_CursorState = 0!=parser.ReadInt(section, L"MouseActionCursor", defaultState);

	const WCHAR* defaultMouseCursor = (section == L"Rainmeter") ? L"HAND" : L"";
	const WCHAR* mouseCursor = parser.ReadString(section, L"MouseActionCursorName", defaultMouseCursor).c_str();
	if (_wcsicmp(mouseCursor, L"HAND") == 0)
	{
		m_CursorType = MOUSECURSOR_HAND;
	}
	else if (_wcsicmp(mouseCursor, L"TEXT") == 0)
	{
		m_CursorType = MOUSECURSOR_TEXT;
	}
	else if (_wcsicmp(mouseCursor, L"HELP") == 0)
	{
		m_CursorType = MOUSECURSOR_HELP;
	}
	else if (_wcsicmp(mouseCursor, L"BUSY") == 0)
	{
		m_CursorType = MOUSECURSOR_BUSY;
	}
	else if (_wcsicmp(mouseCursor, L"CROSS") == 0)
	{
		m_CursorType = MOUSECURSOR_CROSS;
	}
	else if (_wcsicmp(mouseCursor, L"PEN") == 0)
	{
		m_CursorType = MOUSECURSOR_PEN;
	}
	else if (wcschr(mouseCursor, L'.'))
	{
		m_CursorType = MOUSECURSOR_CUSTOM;
	}
	else
	{
		// Inherit from [Rainmeter].
		m_CursorType = meterWindow->GetMouse().GetCursorType();
		if (m_CursorType == MOUSECURSOR_CUSTOM)
		{
			mouseCursor = meterWindow->GetParser().ReadString(L"Rainmeter", L"MouseActionCursorName", L"").c_str();
		}
	}

	if (m_CursorType == MOUSECURSOR_CUSTOM)
	{
		std::wstring cursorPath = meterWindow->GetResourcesPath();
		cursorPath += L"Cursors\\";
		cursorPath += mouseCursor;
		m_CustomCursor = LoadCursorFromFile(cursorPath.c_str());
		if (!m_CustomCursor)
		{
			m_CursorType = MOUSECURSOR_ARROW;
			LogWithArgs(LOG_ERROR, L"Invalid cursor: %s", cursorPath.c_str());
		}
	}
}