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

📄 backlight.cpp

📁 Tornado 手机半背光源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		nt.lpszArguments = L"-[]";
		// Call the function to register the notification
		if ( wcslen(nt.lpszApplication) <= 0 || CeSetUserNotificationEx (0, &nt, NULL) == NULL ){
			MessageBox( NULL, L"Couldn't set notification", L"jTB Error", MB_SETFOREGROUND );
			exit(1);
		}
	}
}


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

  OnCreate
  
**************************************************************************************/
LRESULT OnCreate(
				 HWND hwnd,
				 CREATESTRUCT* lParam
				)
{
    // create the menu bar
    SHMENUBARINFO mbi;
    ZeroMemory(&mbi, sizeof(SHMENUBARINFO));
    mbi.cbSize = sizeof(SHMENUBARINFO);
    mbi.hwndParent = hwnd;
    mbi.nToolBarId = IDR_MENUBAR1;
    mbi.hInstRes = g_hInst;
    if(!SHCreateMenuBar(&mbi))
    {
        // Couldn't create the menu bar.  Fail creation of the window.
        return(-1);
    }
	
    return(0); // continue creation of the window
}

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

   WndProc

 **************************************************************************************/
LRESULT CALLBACK WndProc(
    HWND hwnd,
    UINT msg,
    WPARAM wp,
    LPARAM lp
    )
{
    LRESULT lResult = TRUE;
	
	g_hDlg = hwnd;

    switch(msg)
    {
        case WM_CREATE:

			if (Options == NORMAL)
				OnCreate(hwnd, (CREATESTRUCT*)lp);

			// Start up the thread to get the power notifications
            g_hEventShutDown = CreateEvent(NULL, FALSE, FALSE, NULL);
            if (NULL != g_hEventShutDown)  
            {
                g_hPowerNotificationThread = CreateThread(0, 0, PowerNotificationThread, NULL, 0, 0);
				if (g_hPowerNotificationThread == NULL){
					MessageBox(NULL,L"CreateThread failed!",g_szAppWndClass,MB_SETFOREGROUND);
				}
            }

			if (Options == NORMAL) SetTimer(hwnd,1,10,NULL);
			//if (Options == NORMAL)SetWindowPos(hwnd,HWND_BOTTOM,0,0,0,0,SWP_DRAWFRAME);
            break;

        case WM_COMMAND:
            switch (wp)
            {
			case IDOK:
				DestroyWindow(hwnd);
				break;
			default:
				goto DoDefault;
            }
            break;

        case WM_PAINT:
            {
                HDC hdc;
                PAINTSTRUCT ps;
                RECT rect;
				//OEM_CHARSET DEFAULT_CHARSET SYSTEM_FONT
                hdc = BeginPaint(hwnd, &ps);
                GetClientRect(hwnd, &rect);
				
                DrawText(hdc, g_szMessage + g_szHelp1 + VERSION, -1, &rect, DT_TOP);
                EndPaint (hwnd, &ps);
            }
            break;

		case WM_SETFOCUS:

			ShowWindow(hwnd,SW_SHOWMAXIMIZED);

			break;

		case WM_TIMER:

			//MsgBox(dwState);
			switch(wp){
			case 1:
				KillTimer(hwnd,1);
				ShowWindow(hwnd,SW_MINIMIZE);
				break;
			case 2:
				KillTimer(hwnd,2);

				if (dwState == 0x10010000 || dwState == 0x01000000){
					keybd_event(VK_THOME,0,0,0);
					keybd_event(VK_THOME,0,KEYEVENTF_KEYUP,0);
					SetTimer(hwnd,3,10,NULL);
					KeyLockInProcess = true;
				}
				break;
			case 3:
				KillTimer(hwnd,3);
			case 4:
				if (KeyLockInProcess || dwState == 0x10010000 || dwState == 0x01000000){
					keybd_event(VK_APP6,0,0,0);
					keybd_event(VK_APP6,0,KEYEVENTF_KEYUP,0);
					KeyLockInProcess = false;
				}
				break;
			}
			break;

        case WM_DESTROY:

            // Kill the power notification thread
            if (NULL != g_hEventShutDown)
            {
                SetEvent(g_hEventShutDown);

                if (NULL != g_hPowerNotificationThread)
                {
                    WaitForSingleObject(g_hPowerNotificationThread , INFINITE);
                    CloseHandle(g_hPowerNotificationThread);
                }

                CloseHandle(g_hEventShutDown);
            }

			if (Options == RECOVERY) DeleteFile(jTornadoBrightness_reboot_Shortcut);


			PostQuitMessage(0);

            break;
DoDefault:
        default:
			
            lResult = DefWindowProc(hwnd, msg, wp, lp);
            break;
    }

    return(lResult);
}

void CommandLine(CStr tCmd){
	CStr tCmdStr = tCmd.Get(L"-[",L"]",5);

	//MessageBox(NULL,tCmdStr,tCmd,MB_SETFOREGROUND);

	if (tCmdStr.CompareNoCase(L"r]")==0)
		RemoveNotifications(g_strModuleFile,L"-[]");
	else if (tCmdStr.Compare(L"]")==0){
		RunAtTime(tCmdStr,TRUE);
		exit(0);
	}else if (tCmdStr.GetLength()>1){
		if ( RegCreateKeyEx( HKEY_CLASSES_ROOT, L"CLSID", 0, 0, 0 , KEY_WRITE, NULL, &key, &type ) == ERROR_SUCCESS )
		{
			RegSetValueEx( key, L"Notification", NULL, REG_SZ, (BYTE*)(LPCTSTR)tCmdStr, (tCmdStr.GetLength()+1)*sizeof(TCHAR));
			RegCloseKey( key );
		}
		RunAtTime(tCmdStr);
	}

	tCmd.MakeLower();

	if (tCmd.Find(L"-bts")!=-1){
		DWORD BthMode;
		if (BthGetMode(&BthMode)==ERROR_SUCCESS){
			//MsgBox(BthMode);
			if (tCmd.Find(L"-off")!=-1){
				if (BthMode!=BTH_POWER_OFF) BthSetMode(BTH_POWER_OFF);
			}else if (tCmd.Find(L"-on")!=-1){
				if (BthMode==BTH_POWER_OFF){
					if (tCmd.Find(L"-btsd")!=-1)
						BthSetMode(BTH_DISCOVERABLE);
					else
						BthSetMode(BTH_CONNECTABLE);
				}
			}else if (BthMode!=BTH_POWER_OFF)
				BthSetMode(BTH_POWER_OFF);
			else{
				if (tCmd.Find(L"-btsd")!=-1)
					BthSetMode(BTH_DISCOVERABLE);
				else
					BthSetMode(BTH_CONNECTABLE);
			}
		}
		exit(1);
	}	

	if (tCmd.Find(L"-l")!=-1){ //Auto keylock
		KeyLock = 1;
		if (tCmd.Find(L"-a")!=-1){
			KeyLock = 2;
			CStr rc = tCmd.Get(L"[",L"]",L"-a",0);
			KeyLockDelayTime = _wtol(rc);
			if(KeyLockDelayTime>0)
				KeyLockDelayTime *= 1000;
			else KeyLockDelayTime = 500;
		}			
	}

	if (tCmd.Find(L"-b")!=-1){ //BOOT
		Options = BOOT;
	}else if (tCmd.Find(L"-s")!=-1){ //Silent
		Options = RECOVERY;
	}else if (tCmd.Find(L"-recovery")!=-1){
		SetLightOn();
		
		DeleteFile(jTornadoBrightness_reboot_Shortcut);

		if (tCmd.Find(L"-h")!=-1)
			MessageBox(NULL,L"Restore Backlight to original, \n\n you need to reboot your system!",g_szAppWndClass,MB_SETFOREGROUND);

		if (tCmd.Find(L"-p")!=-1){
			Run(L"\\Windows\\Autoprof.exe",L"-s Normal");
		}

		exit(0);
	}
	
	if (tCmd.Find(L"-d0")!=-1)
		DX = 0;
	else if (tCmd.Find(L"-d1")!=-1)
		DX = 1;
	else if (tCmd.Find(L"-d2")!=-1)
		DX = 2;
	else if (tCmd.Find(L"-d3")!=-1)
		DX = 3;
	else if (tCmd.Find(L"-d4")!=-1)
		DX = 4;

	
	if (tCmd.Find(L"-h")!=-1) {
		MessageBox(NULL,g_szMessage + g_szHelp2 + VERSION,g_szAppWndClass, MB_SETFOREGROUND);
		
		if (DX==-1) exit(0);
	}
	//tCmdStr.Format(L"DX:%d,Options:%d,DelayTime:%d",DX,Options,KeyLockDelayTime);
	//MessageBox(NULL,tCmdStr,tCmd,MB_SETFOREGROUND);

}

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

  WinMain

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

int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPWSTR lpCmdLine,
    int nCmdShow
    )
{

	GetModuleFileName(NULL,g_strModuleFile,MAX_PATH);

	CommandLine(lpCmdLine);

	if (DX < 0 || DX>4 ) DX = 3;

    MSG msg;
    HWND hwnd = NULL;
    BOOL fActivated;
    WNDCLASS wc;
    HWND hwndMain;
    TCHAR szAppTitle[20];

    g_hInst = hInstance;

    if(0 == LoadString(g_hInst, IDS_HELLO_TITLE, szAppTitle, ARRAYSIZE(szAppTitle)))
    {
        return(0);
    }

    if(FAILED(ActivatePreviousInstance(g_szAppWndClass, szAppTitle, &fActivated)) ||
            fActivated)
    {
        return(0);
    }

    // Register our main window's class.
    ZeroMemory(&wc, sizeof(wc));
    wc.style = CS_HREDRAW | CS_VREDRAW ;
    wc.lpfnWndProc = (WNDPROC)WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_APP));
    wc.hInstance = g_hInst;
    wc.hCursor = NULL;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = g_szAppWndClass;
    if(!RegisterClass(&wc))
    {
        return(0);
    }

    // Create the main window.    
    hwndMain = CreateWindow(g_szAppWndClass, szAppTitle,
            WS_CLIPCHILDREN, // Setting this to 0 gives a default style we don't want.  Use a benign style bit instead.
            0, 0, 0, 0,
            NULL, NULL, g_hInst, NULL );
    if(!hwndMain)
    {
        return(0);
    }

	if (Options == NORMAL){
		ShowWindow(hwndMain, SW_SHOW); //nCmdShow
		UpdateWindow(hwndMain);
	}else if (Options == RECOVERY){
		TCHAR tStr[MAX_PATH];
		wsprintf(tStr,L"\"%s\" -recovery",g_strModuleFile);
		SHCreateShortcut(jTornadoBrightness_reboot_Shortcut,tStr);
	}

    // Pump messages until a PostQuitMessage.
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage (&msg);
        DispatchMessage(&msg);
    }

    return msg.wParam;
}

//***************************************************************************
// Function Name: PowerNotificationThread
//
// Purpose: listens for power change notifications
//
HANDLE rgHandles[2] = {0};
HANDLE hPowerNotifications = NULL;

DWORD PowerNotificationThread(LPVOID pVoid)
{
    // size of a POWER_BROADCAST message

	HANDLE s_hBacklightReq = NULL;

    DWORD cbPowerMsgSize = sizeof POWER_BROADCAST + (MAX_PATH * sizeof TCHAR);

    // Initialize our MSGQUEUEOPTIONS structure
    MSGQUEUEOPTIONS mqo;
    mqo.dwSize = sizeof(MSGQUEUEOPTIONS); 
    mqo.dwFlags = MSGQUEUE_NOPRECOMMIT;
    mqo.dwMaxMessages = 4;
    mqo.cbMaxMessage = cbPowerMsgSize;
    mqo.bReadAccess = TRUE;              
                                         
    // Create a message queue to receive power notifications
    HANDLE hPowerMsgQ = CreateMsgQueue(NULL, &mqo);
    if (hPowerMsgQ){
		// Request power notifications 
		hPowerNotifications = RequestPowerNotifications(hPowerMsgQ,PBT_TRANSITION); //POWER_NOTIFY_ALL
		if (hPowerNotifications){

			if ( RegCreateKeyEx( HKEY_LOCAL_MACHINE, POWERONKEY, 0, 0, 0 , KEY_WRITE, NULL, &key, &type ) == ERROR_SUCCESS )
			{
				RegSetValueEx( key, L"bkl1:", NULL, REG_DWORD, (BYTE*)&DX, sizeof(DWORD) );
				RegCloseKey( key );
			}
			
			SetSystemPowerState(L"BacklightOff",POWER_STATE_IDLE,POWER_FORCE);
			SetSystemPowerState(NULL,POWER_STATE_ON,POWER_FORCE);
			//DWORD dwState = 0;
			rgHandles[0] = hPowerMsgQ;
			rgHandles[1] = g_hEventShutDown;

			// Wait for a power notification or for the app to exit
			while(WaitForMultipleObjects(2, rgHandles, FALSE, INFINITE) == WAIT_OBJECT_0)
			{
				DWORD cbRead;
				DWORD dwFlags;
				POWER_BROADCAST *ppb = (POWER_BROADCAST*) new BYTE[cbPowerMsgSize];
				// loop through in case there is more than 1 msg 
				while(ReadMsgQueue(hPowerMsgQ, ppb, cbPowerMsgSize, &cbRead, 
								   0, &dwFlags))
				{
					if (ppb->Message == PBT_TRANSITION){

#ifndef C730W
						if (!(dwState & POWER_STATE_ON ) && (ppb->Flags & POWER_STATE_ON))
#endif
						{
							s_hBacklightReq = SetPowerRequirement(L"BKL1:",D1 , POWER_NAME, NULL, 0);
							if (s_hBacklightReq)ReleasePowerRequirement(s_hBacklightReq);
							s_hBacklightReq = NULL;

							KillTimer(g_hDlg,2);
						}
						
						if (KeyLock){
							if ((dwState == 0x10010000) && (ppb->Flags & 0x01000000)){
								int RealLock = 0;//Active Call Count
								if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE, L"System\\State\\Phone", 0, REG_ACCESS_READ, &key ) == ERROR_SUCCESS )
								{
									DWORD type, length = sizeof(DWORD);
									DWORD cont;
									length = MAX_PATH;
									if ( RegQueryValueEx( key, L"Active Call Count", NULL, &type, (BYTE*)&cont, &length ) == ERROR_SUCCESS )
									{
										RealLock = cont;
									}
									RegCloseKey( key );
								}

								//不在桌面
								if (!RealLock && (ActiveProcess().CompareNoCase(L"home.exe")!=0))	RealLock = -1;

								if (!RealLock)//在桌面则立即锁键
									SendMessage(g_hDlg,WM_TIMER,4,NULL);
								else if (RealLock == -1 && KeyLock == 2)//不在桌面,并在1分钟内未按键.
									SetTimer(g_hDlg,2,KeyLockDelayTime,NULL);

							}
						}

						dwState = ppb->Flags;
					}
				}

				delete[] ppb;
			}
		}
        StopPowerNotifications(hPowerNotifications);
	}

    CloseMsgQueue(hPowerMsgQ);

	SetLightOn();

    return NULL;
}


// end BackLight.cpp

⌨️ 快捷键说明

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