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

📄 backlight.cpp

📁 Tornado 手机半背光源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// BackLight.cpp : Defines the entry point for the application.
//

#include <windows.h>
#include <windowsx.h>
#include "resource.h"
#include <aygshell.h>

#include <commctrl.h>
#include <pm.h>
#include <winreg.h>
#include <msgqueue.h>
#include <Notify.h>

#include <bthutil.h>

#include "CStr.h"

//*************************************//
// This is a opensource software       //
// if you want to use this source      //
// Please save this comment in it      //
//               Designed by jeewood.  //
//*************************************//

//#define C730W
//#define DEBUG
//#define REBOOT
#define BOOT 2
#define NORMAL 1
#define RECOVERY 0

int Options = NORMAL;
int KeyLock = 0;
bool KeyLockInProcess = false;
long KeyLockDelayTime=30000;
DWORD dwState=0;

#define HALF FALSE
#define ON TRUE
#define ARRAYSIZE(a)   (sizeof(a)/sizeof(*a))

#define VERSION L"ver 1.20"

CStr g_szMessage = 
L"Welcome to use \n \
jwz's Tornado brightness tools\n\n \
here is parameter list,\n \
-D0, \n \
-D1, \n \
-D2, \n \
-D3, (default option.)\n \
-D4, \n \
-H(Help)\n ";

CStr g_szHelp2 = 
L"\
-Boot (use in startup's shortcut for boot loader half light!) \n \
-Silent (this option in shortcut, if jtb.exe \
in memory, will create a shortcut to \\windows\\startup directory). \n \
-[7:30\"/Windows/autoprof.exe\",\"-s Normal\";22:30\"/Windows/autoprof.exe\",\"-s Silent\"] \n \
-Profiles (this option with -Recovery, will restore destroyed Profiles) \n \
-(Bts)[c][d] (Blue tooth toggle) \n \
-Lock (Keyboard lock when screen off) \n \
-Always[120] (Always lock keyboard in given Time out.) \n\n \
Wolf&Lion Software Lab. Presents. \n \
Designed by jeewood. ";

CStr g_szHelp1 = L"\n \
For more information \n \
Please use Option -Help\n \
Designed by jeewood. ";

const TCHAR* g_szAppWndClass = TEXT("jTB ver 1.0");
TCHAR* jTornadoBrightness_reboot_Shortcut = L"\\Windows\\Startup\\jTornadoBrightness_reboot_760211.lnk";

TCHAR g_strModuleFile[MAX_PATH];

HINSTANCE g_hInst = NULL;                     // Local copy of hInstance
HANDLE    g_hEventShutDown = NULL;            
HANDLE    g_hPowerNotificationThread = NULL;  
HWND      g_hSystemState = NULL;
HWND	  g_hDlg = NULL;

DWORD PowerNotificationThread(LPVOID pVoid);

HKEY key = NULL;
DWORD type;
DWORD DX = -1;

TCHAR  POWERONKEY[] =  TEXT("System\\CurrentControlSet\\Control\\Power\\State\\On");

//TCHAR * Apps[60];
//TCHAR * Params[60];

/****************************************************************************

   ActivatePreviousInstance

  ****************************************************************************/
HRESULT ActivatePreviousInstance(
    const TCHAR* pszClass,
    const TCHAR* pszTitle,
    BOOL* pfActivated
    )
{
    HRESULT hr = S_OK;
    int cTries;
    HANDLE hMutex = NULL;

    *pfActivated = FALSE;
    cTries = 5;
    while(cTries > 0)
    {
        hMutex = CreateMutex(NULL, FALSE, L"_JTRONADO_BRIGHTNESS_CONTROL_MUTEX_VARIABLE_IN_JWZ_760211_"); // NOTE: We don't want to own the object.
        if(NULL == hMutex)
        {
            // Something bad happened, fail.
            hr = E_FAIL;
            goto Exit;
        }

        if(GetLastError() == ERROR_ALREADY_EXISTS)
        {
            HWND hwnd;

            CloseHandle(hMutex);
            hMutex = NULL;

            hwnd = FindWindow(pszClass, pszTitle);
            if(NULL == hwnd)
            {
                // It's possible that the other window is in the process of being created...
                Sleep(500);
                hwnd = FindWindow(pszClass, pszTitle);
            }

            if(NULL != hwnd) 
            {

				DestroyWindow(hwnd);

                // We are done.
                *pfActivated = TRUE;
                break;
            }
            cTries--;
        }else{
            break;
        }
    }

    if(cTries <= 0)
    {
        // Someone else owns the mutex but we cannot find
        // their main window to activate.
        hr = E_FAIL;
        goto Exit;
    }

Exit:
    return(hr);
}

void SetLightOn(){
	if ( RegCreateKeyEx( HKEY_LOCAL_MACHINE, POWERONKEY, 0, 0, 0 , KEY_WRITE, NULL, &key, &type ) == ERROR_SUCCESS )
	{
		RegDeleteValue( key, L"bkl1:");
		RegCloseKey( key );
	}
	
	SetSystemPowerState(L"BacklightOff",POWER_STATE_IDLE,POWER_FORCE);
	SetSystemPowerState(NULL,POWER_STATE_ON,POWER_FORCE);
}


CStr GetProcessExePath( DWORD processId, LPTSTR exeName = NULL )
{
	CStr procName;
	
	HANDLE procHandle = OpenProcess( 0, FALSE, processId );
	if ( procHandle != NULL )
	{
		TCHAR buffer[MAX_PATH];
		::GetModuleFileName( (HMODULE)procHandle, buffer, MAX_PATH );
		procName = buffer;
		CloseHandle( procHandle );
	}
	
	return procName;
}

CStr ActiveProcess()
{
    CStr rc;
	
	HWND  hwnd = ::GetForegroundWindow();
	for ( int retry = 0; hwnd == NULL && retry < 10; retry++ )
	{
		Sleep(10);
		hwnd = ::GetForegroundWindow();
	}
	
	CStr procName;
	DWORD dwProcessID;
	
	::GetWindowThreadProcessId(hwnd,&dwProcessID);
	int e = GetLastError();
	if (dwProcessID)
	{
		procName = GetProcessExePath( dwProcessID );
		
		int pos = procName.ReverseFind( '\\' );
		if ( pos == -1 )
			rc = procName;
		else
			rc = procName.Mid( pos+1 );
	}
    
    return rc;
}

void RemoveNotifications(CStr App, CStr param )
{
    ULONG handleCount;
    CeGetUserNotificationHandles (NULL, 0, &handleCount);
    if ( handleCount > 0 )
    {
        HANDLE *handles = new HANDLE[handleCount];
        BOOL  rc = CeGetUserNotificationHandles (handles, handleCount, &handleCount);
        if ( rc == TRUE )
        {

            for ( UINT i=0; i < handleCount; i++ )
            {
                CE_NOTIFICATION_INFO_HEADER *info;
                DWORD                        size, dwMaxNotifSize;

                CeGetUserNotification(handles[i], 0, &dwMaxNotifSize , NULL);
                if ( dwMaxNotifSize > 0 )
                {
                    BYTE *pNotifBuffer = new BYTE[dwMaxNotifSize];
                    if ( CeGetUserNotification(handles[i], dwMaxNotifSize, &size, pNotifBuffer) )
                    {
                        info = (CE_NOTIFICATION_INFO_HEADER*)pNotifBuffer;
                        CStr app = info->pcent->lpszApplication;
                        CStr par = info->pcent->lpszArguments;
                        if (   app.CompareNoCase( App ) == 0
                            && ((param.GetLength() > 0 && par.CompareNoCase( param ) == 0 ) || param == NULL)
                           )
                        {
                            CeClearUserNotification(handles[i]);
                        }
                    }
                    delete[] pNotifBuffer;
                }
            }
        }
        delete[] handles;
    }
}


time_t FileTimeToUnixTime( const FILETIME &ft )
{
    LONGLONG t;
    t = (LONGLONG)ft.dwLowDateTime + ((LONGLONG)ft.dwHighDateTime<<32);
    t -= (LONGLONG)116444736000000000;
    return (time_t)(t/10000000);
}

time_t SystemTimeToUnixTime( const SYSTEMTIME &st )
{
    FILETIME ft;
    SystemTimeToFileTime( &st, &ft );
    return FileTimeToUnixTime( ft );
}

void UnixTimeToFileTime(time_t t, LPFILETIME pft)
{
    LONGLONG ll;
    ll = Int32x32To64(t, 10000000) + 116444736000000000;
    pft->dwLowDateTime = (DWORD)ll;
    pft->dwHighDateTime = (DWORD)(ll >> 32);
}

void UnixTimeToSystemTime(time_t t, LPSYSTEMTIME pst)
{
    FILETIME ft;
    UnixTimeToFileTime(t, &ft);
    FileTimeToSystemTime(&ft, pst);
}

#ifdef DEBUG
void MsgBox(CStr msg){
	MessageBox(NULL,msg,L"Info",MB_SETFOREGROUND);
}

void MsgBox(CStr msg, CStr title){
	MessageBox(NULL,msg, title,MB_SETFOREGROUND);
}

void MsgBox(long msg){
	CStr t;
	t.Format(L"%d",msg);
	MessageBox(NULL,t,L"Info",MB_SETFOREGROUND);
}

void MsgBox(long msg,long title){
	CStr m,t;
	m.Format(L"%d",msg);
	t.Format(L"%d",title);
	MessageBox(NULL,m,t,MB_SETFOREGROUND);
}
#endif

void Run(CStr App,CStr Param){
    SHELLEXECUTEINFO sei;
    sei.cbSize = sizeof(SHELLEXECUTEINFO);
    sei.fMask = SEE_MASK_NOCLOSEPROCESS;
    sei.hwnd  = NULL;
    sei.lpVerb = L"Open";
    sei.lpFile = (LPCTSTR)App;
    sei.lpDirectory = L"\\";
    sei.lpParameters = Param;
	sei.nShow = SW_SHOW;
    sei.hInstApp = NULL;

    ShellExecuteEx( &sei );
}

void RunAtTime(CStr tCmdStr,BOOL Execute = FALSE){
	int tPos,tCnt,i=0;
	CE_NOTIFICATION_TRIGGER nt;

	if (Execute){
		if ( RegOpenKeyEx( HKEY_CLASSES_ROOT, L"CLSID", 0, REG_ACCESS_READ, &key ) == ERROR_SUCCESS )
		{
			DWORD type, length;
			TCHAR cont[MAX_PATH];
			length = MAX_PATH;
			if ( RegQueryValueEx( key, L"Notification", NULL, &type, (BYTE*)cont, &length ) == ERROR_SUCCESS )
			{
				tCmdStr = cont;
			}
			RegCloseKey( key );
		}
	}
	
	time_t NearestTime=0,MiniTime=0,tTime,cTime;

	SYSTEMTIME TmpTime,CurrTime;

	GetLocalTime(&CurrTime);
	CurrTime.wSecond = 0;
	CurrTime.wMilliseconds = 0;
	CurrTime.wDayOfWeek = 0;
	
	TmpTime = CurrTime;

	cTime = SystemTimeToUnixTime(CurrTime);

	while(tCmdStr.GetLength()>0){
		
		tPos = tCmdStr.Find(L":");
		CStr t;
		if (tPos != -1){	//Get Hour
			t = tCmdStr.Left(tPos);
			tCmdStr = tCmdStr.Mid(tPos+1);
			tCmdStr.TrimLeft();
			tCmdStr.TrimRight();
			TmpTime.wHour = _wtol((LPCTSTR)t);
			if (TmpTime.wHour>23) TmpTime.wHour = 0;
		}
		tPos = tCmdStr.FindOneOf(L" \"\\/");
		if (tPos != -1){	//Get Minute
			t = tCmdStr.Left(tPos);
			tCmdStr = tCmdStr.Mid(tPos);
			tCmdStr.TrimLeft();
			tCmdStr.TrimRight();
			TmpTime.wMinute = _wtol((LPCTSTR)t);
			if (TmpTime.wMinute>59) TmpTime.wMinute = 0;
		}
        
		TmpTime.wSecond = 0;
		TmpTime.wMilliseconds = 0;
		TmpTime.wDayOfWeek = 0;

		tTime = SystemTimeToUnixTime(TmpTime);
		
		if (!MiniTime) MiniTime = tTime; //Initalize MiniTime
		
		if (MiniTime>tTime)	MiniTime = tTime; //Save MiniTime
		
		if (!NearestTime && tTime > cTime) NearestTime = tTime;

		if (tTime>cTime && tTime < NearestTime){  //Get NearestTime from CurrTime
			NearestTime = tTime;
		}

		if (tTime == cTime){ //is Current Application to Execute?
			if ((tPos = tCmdStr.FindOneOf(L",;]"))!=-1){
				CStr App = tCmdStr.Left(tPos);
				CStr param = NULL;
				tCmdStr = tCmdStr.Mid(tPos+1);
				tCmdStr.TrimLeft();
				App.TrimLeft(L"\"");
				App.TrimRight(L"\"");
				if ((tPos = tCmdStr.FindOneOf(L";]"))!=-1){
					param = tCmdStr.Left(tPos);
					tCmdStr = tCmdStr.Mid(tPos+1);
					tCmdStr.TrimLeft();
					param.TrimLeft(L"\"");
					param.TrimRight(L"\"");
					App.MakeLower();
					if (App.Find(L"autoprof.exe")!=-1){
						CStr t=param.Mid(3);
						if (t.CompareNoCase(L"Normal")==0){
							param = L"-s Normal";
						}else if (t.CompareNoCase(L"Silent")==0){
							param = L"-s Silent";
						}else if (t.CompareNoCase(L"Meeting")==0){
							param = L"-s Meeting";
						}else if (t.CompareNoCase(L"Loud")==0){
							param = L"-s Loud";
						}else if (t.CompareNoCase(L"Automatic")==0){
							param = L"-s Automatic";
						}else if (t.CompareNoCase(L"Car")==0){
							param = L"-s Car";
						}else if (t.CompareNoCase(L"Handset")==0){
							param = L"-s Handset";
						}else{
							Execute = false;
						}
					}
				}
				//MsgBox(App,param);
				if (Execute) Run(App,param); //Execute? Yes!
			}
		}else if ((tPos = tCmdStr.FindOneOf(L";]"))!=-1){	//Next Application need to Execute?
			tCmdStr = tCmdStr.Mid(tPos+1);
		}
		tCmdStr.TrimLeft();
	}
	//Fill in CE_NOTIFICATION_TRIGGER structure
	//[7:00 "\Windows\autoprof.exe","-s Normal";22:00"\Windows\autoprof.exe","-s Silent"]

	RemoveNotifications(g_strModuleFile,L"-[]");

	
	if (NearestTime != 0 && NearestTime != cTime){
		UnixTimeToSystemTime(NearestTime,&TmpTime);
	}else{
		MiniTime += 86400;
		UnixTimeToSystemTime(MiniTime,&TmpTime);
	}

	if ((NearestTime>cTime+5) || (MiniTime)>cTime+5){
		memset( &nt, 0, sizeof(CE_NOTIFICATION_TRIGGER) );
		nt.dwSize  = sizeof(CE_NOTIFICATION_TRIGGER);
		nt.dwType  = CNT_TIME;
		nt.stStartTime     = TmpTime;
		nt.stEndTime       = TmpTime;
		nt.lpszApplication = g_strModuleFile;

⌨️ 快捷键说明

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