⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lumosconfig.cpp

📁 Example of using HTC sensor api to get Light sensor value and set backlight level in modern HTC phon
💻 CPP
字号:
#include "LumosConfig.h"

void showConfigError(const TCHAR* msg)
{
	MessageBox(0, msg, L"Lumos Config", MB_TOPMOST | MB_ICONERROR);
}

void LumosConfig::LoadConfig()
{
	TCHAR* fName = config.GetFilePath(L"settings.txt");
	FILE* fp = _wfopen(fName, L"rt");
	delete[] fName;
	if (!fp)
	{
		showConfigError(L"Can't open settings.txt");
		return;
	}

	TCHAR line[MAX_BUF_SIZE];
	TCHAR *ptr, *ptr2;
	while (fp && config.GetUtf8FileLine(fp, line, MAX_BUF_SIZE))
	{
		ptr = wctrim(line);
		if (wcstrbegins(ptr, L"EXCPRG:") || wcstrbegins(ptr, L"EXCCLS:") || wcstrbegins(ptr, L"EXCCLP:"))
		{
			ParseExcludeRule(ptr);
		}
		else if ((ptr2=wcschr(ptr, L'=')))
		{
			*ptr2 = 0;
			ptr2++;
			ptr = wctrim(ptr);
			ptr2 = wctrim(ptr2);
			ParseValue(ptr, ptr2);
		}
	}
	fclose(fp);

	CheckForGoodValues();
}

void LumosConfig::ParseExcludeRule(TCHAR* line)
{
	// EXCLS: name,value
	TCHAR* ptr = wcschr(line, L':');
	*ptr = 0;
	ptr++;
	TCHAR* ptr2 = wcschr(ptr, L',');
	TCHAR* type = line;

	if (!ptr2)
	{
		TCHAR buf[MAX_BUF_SIZE];
		wcsprintf(buf, L"Exclude rule for %s:%s error, ignoring", type, ptr);
		showConfigError(buf);
		return;
	}

	*ptr2 = 0;
	ptr2++;
	ptr = wctrim(ptr);
	ptr2 = wctrim(ptr2);


	int bLevel = 0;
	
	if (swscanf(ptr2, L"%d", &bLevel) < 1)
	{
		TCHAR buf[MAX_BUF_SIZE];
		wcsprintf(buf, L"Exclude rule for %s:%s,%s error, ignoring", type, ptr, ptr2);
		showConfigError(buf);
		return;
	}

	if (!wcscmp(type, L"EXCPRG"))
	{
		this->exceptions.Add(ptr, new int(bLevel));
	}
	else if (!wcscmp(type, L"EXCCLS"))
	{
		this->exceptions_class.Add(ptr, new int(bLevel));
	}
	else if (!wcscmp(type, L"EXCCLP"))
	{
		this->exceptions_progclass.Add(ptr, new int(bLevel));
	}
	else
	{
		TCHAR buf[MAX_BUF_SIZE];
		wcsprintf(buf, L"Exclude rule for %s:%s,%s error, ignoring", type, ptr, ptr2);
		showConfigError(buf);
		return;
	}
}

bool LumosConfig::SetValueIfNameSame(const TCHAR* oName, TCHAR* name, TCHAR* value, int *valuePalce)
{
	if (!wcscmp(oName, name))
	{
		int val;
		int cnt = swscanf(value, L"%d", &val);
		if (cnt != 1)
		{
			TCHAR buf[MAX_BUF_SIZE];
			wcsprintf(buf, L"Syntax error in %s, using default '%d'", name, (*valuePalce));
			showConfigError(buf);
		}
		else
			*valuePalce = val;
		return true;
	}
	return false;
}

void LumosConfig::ParseValue(TCHAR* name, TCHAR* value)
{
	bool done = false;

#define NAME_PARSE(n) if (!done) done = SetValueIfNameSame(L#n, name, value, &(this->n))
	NAME_PARSE(BACKLIGHT_TOLERANCE);
	NAME_PARSE(DIM_TO_MINIMUM_BELOW);
	NAME_PARSE(DIM_TO_MINIMUM_PLUS_ONE_BELOW);
	NAME_PARSE(EXCEPTION_ONLY_ACTIVE_WINDOW);
	NAME_PARSE(EXTRA_POLLS_BEFORE_SET);
	NAME_PARSE(MAX_BACKLIGHT);
	NAME_PARSE(MAX_BL_LUMENS);
	NAME_PARSE(MIN_BACKLIGHT);
	NAME_PARSE(MIN_BL_LUMENS);
	NAME_PARSE(POLLING_INTERVAL);
	NAME_PARSE(USE_ON_AC_POWER);
#undef NAME_PARSE
}

void LumosConfig::CheckForGoodValues()
{
	if (MIN_BACKLIGHT > MAX_BACKLIGHT)
	{
		showConfigError(L"Your MIN_BACKLIGHT and MAX_BACKLIGHT settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		MIN_BACKLIGHT = 2;
		MAX_BACKLIGHT = 8;
	}
	if (MIN_BACKLIGHT == MAX_BACKLIGHT)
	{
		showConfigError(L"Your MIN_BACKLIGHT and MAX_BACKLIGHT settings must not be equal, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		MIN_BACKLIGHT = 2;
		MAX_BACKLIGHT = 8;
	}
	if (MIN_BL_LUMENS > MAX_BL_LUMENS)
	{
		showConfigError(L"Your MIN_BL_LUMENS and MAX_BL_LUMENS settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		MIN_BL_LUMENS = 0;
		MAX_BL_LUMENS = 700;
	}
	if (MIN_BL_LUMENS == MAX_BL_LUMENS)
	{
		showConfigError(L"Your MIN_BL_LUMENS and MAX_BL_LUMENS settings must not be equal, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		MIN_BL_LUMENS = 0;
		MAX_BL_LUMENS = 700;
	}
	if ((MAX_BL_LUMENS - MIN_BL_LUMENS) < (MAX_REPORTED_BACKLIGHT - MIN_REPORTED_BACKLIGHT))
	{
		showConfigError(L"Logical error - Range of sensor must be same or larger than range of backlight supported by device. MAX_BL_LUMENS - MIN_BL_LUMENS > MAX_REPORTED_BACKLIGHT - MIN_REPORTED_BACKLIGHT. Please recalibrate your sensor, reverting to defaults.");
		MIN_BL_LUMENS = 0;
		MAX_BL_LUMENS = 700;
	}
	if (POLLING_INTERVAL < 1)
	{
		showConfigError(L"Your POLLING_INTERVAL settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		POLLING_INTERVAL = 0x4e2;
	}
	if (POLLING_INTERVAL > 0x1770)
	{
		showConfigError(L"Sorry but maximum POLLING_INTERVAL is 6 seconds (6000) in order to be able to manage the service properly.");
		POLLING_INTERVAL = 0x1770;
	}
	if (EXTRA_POLLS_BEFORE_SET < 0)
	{
		showConfigError(L"Your EXTRA_POLLS_BEFORE_SET settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		EXTRA_POLLS_BEFORE_SET = 1;
	}
	if (BACKLIGHT_TOLERANCE < 0)
	{
		showConfigError(L"Your BACKLIGHT_TOLERANCE settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		BACKLIGHT_TOLERANCE = 0;
	}
	if ((USE_ON_AC_POWER != 0) && (USE_ON_AC_POWER != 1))
	{
		showConfigError(L"Your USE_ON_AC_POWER settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		USE_ON_AC_POWER = 1;
	}
	if ((EXCEPTION_ONLY_ACTIVE_WINDOW != 0) && (EXCEPTION_ONLY_ACTIVE_WINDOW != 1))
	{
		showConfigError(L"Your EXCEPTION_ONLY_ACTIVE_WINDOW settings don't make any sense, reverting to default. Please start LumosWizard and apply to automatically fix this problem.");
		EXCEPTION_ONLY_ACTIVE_WINDOW = 0;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -