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

📄 backlight.cpp

📁 我自己编译的armv4i wince60模拟器的bps源文件,已经验证可以使用,欢迎下载
💻 CPP
字号:
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
//
//
// Use of this sample source code is subject to the terms of the Microsoft
// license agreement under which you licensed this sample source code. If
// you did not accept the terms of the license agreement, you are not
// authorized to use this sample source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the LICENSE.RTF on your install media or the root of your tools installation.
// THE SAMPLE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//

// DeviceEmulator backlight driver

#include <windows.h>
#include <ceddk.h>
#include <bsp.h>
#include <display_escapes.h>

#define ZONE_BACKLIGHT 0

/*------------------------------------------------------------------
	DllMain
	
	Entry point called when a process hooks up to the DLL.
------------------------------------------------------------------*/

BOOL APIENTRY DllMain(HANDLE hinstDLL, DWORD fdwReason, LPVOID lpv)
{
	switch ( fdwReason )
	{
		case DLL_PROCESS_ATTACH:
			DisableThreadLibraryCalls((HMODULE)hinstDLL);
			break;
		case DLL_PROCESS_DETACH:
			break;
	}
	
	return TRUE;
}


/*------------------------------------------------------------------
	SetBackLightState
------------------------------------------------------------------*/

BOOL
SetBackLightState(BOOL fBacklightOn)
{
    // Call the display driver to change the backlight state
    HDC hdc = CreateDC(NULL, NULL, NULL, NULL);  // This gets us a HDC to the screen.
    if (hdc == NULL) 
    {
        return FALSE;
    }
    int iRet = ExtEscape(hdc, SETBACKLIGHT, sizeof(fBacklightOn), (LPCSTR)&fBacklightOn, 0, NULL);
    DeleteDC(hdc);

    return (iRet > 0) ? TRUE : FALSE;
}


/*------------------------------------------------------------------
	GetBackLightState
------------------------------------------------------------------*/

BOOL
GetBackLightState(PCEDEVICE_POWER_STATE pState)
{
    // Call the display driver to change the backlight state
    HDC hdc = CreateDC(NULL, NULL, NULL, NULL);  // This gets us a HDC to the screen.
    if (hdc == NULL) 
    {
        return FALSE;
    }
    BOOL fBacklightOn;
    int iRet = ExtEscape(hdc, GETBACKLIGHT, 0, NULL, sizeof(fBacklightOn), (LPSTR)&fBacklightOn);
    DeleteDC(hdc);

    if (iRet > 0) 
    {
        *pState = (fBacklightOn) ? D0 : D4;
        return TRUE;
    }
    return FALSE;
}


/*------------------------------------------------------------------
	BKL_Init
	
	Standard stream interface Init function. Does nothing here.
------------------------------------------------------------------*/

DWORD
BKL_Init( DWORD /*registryPath*/ )
{
	return 1;
}


/*------------------------------------------------------------------
	BKL_Deinit
	
	Standard stream interface Deinit function. Does nothing here.
------------------------------------------------------------------*/

BOOL
BKL_Deinit( DWORD /*dwData*/ )
{	
	return TRUE;
}


/*------------------------------------------------------------------
	BKL_Open
	
	Standard stream interface Open function. Does nothing here.
------------------------------------------------------------------*/

extern "C"  DWORD
BKL_Open( 	DWORD dwClientContext,
	  		DWORD /*access*/,
	  		DWORD /*shareMode*/ )  
{
	return dwClientContext;
}


/*------------------------------------------------------------------
	BKL_Close
	
	Standard stream interface Close function. Does nothing here.
------------------------------------------------------------------*/

BOOL
BKL_Close( DWORD /*dwData*/ )
{
	return TRUE;
}


/*------------------------------------------------------------------
	BKL_Write
	
	Standard stream interface Write function. Does nothing here.
------------------------------------------------------------------*/

DWORD
BKL_Write( DWORD	/*dwData*/,	
		   LPCVOID	/*writeBytes*/,	
		   DWORD	/*numBytes*/ )
{
	return 0;
}


/*------------------------------------------------------------------
	BKL_Read
	
	Standard stream interface Read function. Does nothing here.
------------------------------------------------------------------*/

DWORD
BKL_Read( DWORD		/*dwData*/,
		  LPVOID	/*buffer*/,
		  DWORD		/*numRead*/ )
{
	return 0;
}


/*------------------------------------------------------------------
	BKL_IOControl
	
	Standard stream interface IOControl function. Does nothing here.
------------------------------------------------------------------*/

extern "C" BOOL BKL_IOControl(
    DWORD hOpenContext,
    DWORD dwCode,
    PBYTE pBufIn,
    DWORD dwLenIn,
    PBYTE pBufOut,
    DWORD dwLenOut,
    PDWORD pdwActualOut )
{
    DWORD dwErr = ERROR_INVALID_PARAMETER;

    RETAILMSG(ZONE_BACKLIGHT,(TEXT("BKL_IOControl IOCTL code  = %d\r\n"), dwCode));

    switch (dwCode) {
    case IOCTL_POWER_CAPABILITIES:  // determines device-specific capabilities
        RETAILMSG(ZONE_BACKLIGHT, (TEXT("IOCTL_POWER_CAPABILITIES\r\n")));
        if (pBufOut && dwLenOut >= sizeof (POWER_CAPABILITIES) && pdwActualOut) 
        {
            __try 
            {
                PPOWER_CAPABILITIES PowerCaps = (PPOWER_CAPABILITIES)pBufOut;
 
                // Right now supports D0 (permanently on) and D4(off) only.
 
                memset(PowerCaps, 0, sizeof(*PowerCaps));
                PowerCaps->DeviceDx = 0x11; //support D0, D4
                *pdwActualOut = sizeof(*PowerCaps);
                
                dwErr = ERROR_SUCCESS;
            }
            __except(EXCEPTION_EXECUTE_HANDLER) 
            {
                RETAILMSG(ZONE_BACKLIGHT, (TEXT("exception in ioctl\r\n")));
            }
        }
        break;
 
    case IOCTL_POWER_QUERY: // determines whether changing power state is feasible
            RETAILMSG(ZONE_BACKLIGHT,(TEXT("BKL: Received IOCTL_POWER_QUERY\r\n")));
            if (pBufOut && dwLenOut >= sizeof(CEDEVICE_POWER_STATE)) 
            {
                // Return a good status on any valid query, since we are always ready to
                // change power states (if asked for state we don't support, we move to next highest, eg D3->D4).
                __try 
                {
                    CEDEVICE_POWER_STATE ReqDx = *(PCEDEVICE_POWER_STATE)pBufOut;
 
                    if (VALID_DX(ReqDx)) 
                    {
                        // This is a valid Dx state so return a good status.
                        dwErr = ERROR_SUCCESS;
                    }
 
                    RETAILMSG(ZONE_BACKLIGHT, (TEXT("IOCTL_POWER_QUERY %s\r\n"), dwErr == ERROR_SUCCESS ? (TEXT("succeeded")) : (TEXT("failed")) ));
                }
                __except(EXCEPTION_EXECUTE_HANDLER) 
                {
                    RETAILMSG(ZONE_BACKLIGHT, (TEXT("Exception in ioctl\r\n")));
                }
            }
            break;
 
        break;
 
    case IOCTL_POWER_SET: // requests a change from one device power state to another
            RETAILMSG(ZONE_BACKLIGHT,(TEXT("BKL: Received IOCTL_POWER_SET\r\n")));
            if (pBufOut && dwLenOut >= sizeof(CEDEVICE_POWER_STATE)) 
            {
                __try 
                {
                    CEDEVICE_POWER_STATE ReqDx = *(PCEDEVICE_POWER_STATE)pBufOut;
 
                    if (VALID_DX(ReqDx)) 
                    {
                        if(ReqDx == D1 ||ReqDx == D2 || ReqDx == D3) 
                        {
                            ReqDx = D4;
                        }
 
                        if (SetBackLightState(D0 == ReqDx ? TRUE : FALSE)) 
                        {
                            *(PCEDEVICE_POWER_STATE) pBufOut = ReqDx;
                            *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
 
                            dwErr = ERROR_SUCCESS;
                            RETAILMSG(ZONE_BACKLIGHT, (TEXT("IOCTL_POWER_SET to D%u \r\n"), ReqDx));
                        }
                        else
                        {
                            dwErr = GetLastError();
                            RETAILMSG(ZONE_BACKLIGHT, (TEXT("IOCTL_POWER_SET failed to switch to D%u\r\n"), ReqDx));
                        }
                    }
                    else 
                    {
                        RETAILMSG(ZONE_BACKLIGHT, (TEXT("Invalid state request D%u\r\n"), ReqDx));
                    }
                }
                __except(EXCEPTION_EXECUTE_HANDLER) 
                {
                    RETAILMSG(ZONE_BACKLIGHT, (TEXT("Exception in ioctl\r\n")));
                }
            }
            break;
    
        break;
 
    case IOCTL_POWER_GET: // gets the current device power state
           RETAILMSG(ZONE_BACKLIGHT,(TEXT("BKL: Received IOCTL_POWER_GET\r\n")));
            if (pBufOut != NULL && dwLenOut >= sizeof(CEDEVICE_POWER_STATE)) 
            {
                __try 
                {
                    if (GetBackLightState((PCEDEVICE_POWER_STATE)pBufOut)) 
                    {
                        dwErr = ERROR_SUCCESS;
 
                        RETAILMSG(ZONE_BACKLIGHT, (TEXT("IOCTL_POWER_GET: passing back %u\r\n"), *(PCEDEVICE_POWER_STATE)pBufOut));
                    }
                    else
                    {
                        dwErr = GetLastError();
                        RETAILMSG(ZONE_BACKLIGHT, (TEXT("IOCTL_POWER_GET: failed to get backlight state\r\n")));
                    }
                }
                __except(EXCEPTION_EXECUTE_HANDLER) 
                {
                    RETAILMSG(ZONE_BACKLIGHT, (TEXT("Exception in ioctl\r\n")));
                }
            }
 
        break;

    default:
        break;
    }

    if (dwErr) 
    {
        RETAILMSG(ZONE_BACKLIGHT, (TEXT("Ioctl failed - err=%d\r\n"), dwErr));
        return FALSE;
    }
    return TRUE;
}


/*------------------------------------------------------------------
	BKL_PowerUp
	
	Standard stream interface PowerUp function. Does nothing here.
------------------------------------------------------------------*/

extern "C"  void
BKL_PowerUp( DWORD /*hContext*/ )
{	
	// do nothing
}


/*------------------------------------------------------------------
	BKL_PowerDown
	
	Standard stream interface PowerDown function. Does nothing here.
------------------------------------------------------------------*/
extern "C"  void
BKL_PowerDown( DWORD /*hContext*/ )
{	
	// do nothing
}


/*------------------------------------------------------------------
	BKL_Seek
	
	Standard stream interface Seek function. Does nothing here.
------------------------------------------------------------------*/

extern "C"  DWORD
BKL_Seek( DWORD /*hOpenContext*/, 
		  long /*amount*/, 
		  WORD /*type*/ )
{
	// Do nothing, return fail
	return -1;
}


⌨️ 快捷键说明

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