📄 backlight.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2006, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// File: backlight.cpp
//
// Provides the backlight driver implementation for MX27 reference platform.
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// INCLUDE FILES
//------------------------------------------------------------------------------
#include <windows.h>
#include <ceddk.h>
#include <DevLoad.h>
#include <msgqueue.h>
#include <pm.h>
#include <csp.h>
#include <backlight.h>
//------------------------------------------------------------------------------
// External Functions
extern void BSPBacklightInit();
extern void BSPBacklightDeInit();
extern void BSPBacklightSetIntensity(DWORD level);
//------------------------------------------------------------------------------
// GLOBAL DEFINITIONS
//------------------------------------------------------------------------------
#define dim(x) (sizeof(x) / sizeof(x[0]))
// Queue size for Power Manager Notification MsgQ
#define QUEUE_ENTRIES 1
#define QUEUE_SIZE (QUEUE_ENTRIES * (sizeof(POWER_BROADCAST) + MAX_PATH))
//------------------------------------------------------------------------------
// GLOBAL OR STATIC VARIABLES
//------------------------------------------------------------------------------
#ifdef DEBUG
DBGPARAM dpCurSettings = {
L"Backlight", {
L"Info", L"Function", L"Warnings", L"Errors",
L"Init", L"Undefined", L"Undefined", L"Undefined",
L"Undefined", L"Undefined", L"Undefined", L"Undefined",
L"Undefined", L"Undefined", L"Undefined", L"Undefined"
},
0x0008
};
#endif
// Backlight device name
static TCHAR szName[DEVNAME_LEN];
// Current Backlight device power state.
static CRITICAL_SECTION cs;
static CEDEVICE_POWER_STATE CurDx;
// Power Manager notification variables
static HANDLE hMsgQ;
static HANDLE hNotifications;
static bklSettings_t bklSettings;
// Event handling thread
static HANDLE ghInterruptServiceThread;
// thread wait on these handles
static HANDLE hWaitList[bklMaxWaitEvents];
static HANDLE hevDummy;
static HANDLE hUserActivityEvent;
static HANDLE hUserInactivityEvent;
// device on AC/DC status flag
static BOOL fACOnline;
// Interrupt thread loop flag
static BOOL fIntrThreadLoop;
//------------------------------------------------------------------------------
// STATIC FUNCTION PROTOTYPES
//------------------------------------------------------------------------------
static void PrintBacklightSettings(bklSettings_t *pSettings);
static void GetBacklightTimeoutSettings(bklSettings_t *pSettings);
static void GetBacklightLevelSettings(bklSettings_t *pSettings);
//------------------------------------------------------------------------------
// EXPORTED FUNCTIONS
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// FUNCTION: BKL_IntrThread
//
// DESCRIPTION: THis is a thread to handle setting changes from Control panel
// PARAMETERS:
//
// RETURNS:
//
//------------------------------------------------------------------------------
DWORD WINAPI BKL_IntrThread(LPVOID lpParameter)
{
UCHAR buf[QUEUE_SIZE];
SYSTEM_POWER_STATUS_EX2 sps;
DWORD dwTimeout;
DWORD dwStatus;
DWORD result;
BKL_FUNCTION_ENTRY();
if(!CeSetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_NORMAL))
{
DEBUGMSG(ZONE_ERROR, (TEXT("PMAPI!CeSetThreadPriority ERROR:%d\n"), GetLastError()));
}
// Force initial update of timeout values and backlight levels.
SetEvent(hWaitList[bklControlPanelEvent]);
SetEvent(hWaitList[bklLevelChangeEvent]);
while(fIntrThreadLoop)
{
result = WaitForMultipleObjects(dim(hWaitList), hWaitList, FALSE, INFINITE);
if(!fIntrThreadLoop)
break;
switch(result)
{
case (WAIT_OBJECT_0 + bklControlPanelEvent):
// EVENT COME FROM CONTROL PANEL
#ifdef DEBUG
DEBUGMSG(ZONE_INFO, (_T("Backlight settings before:\r\n")));
PrintBacklightSettings(&bklSettings);
#endif
// GET backlight settings
GetBacklightTimeoutSettings(&bklSettings);
#ifdef DEBUG
DEBUGMSG(ZONE_INFO, (_T("Backlight settings after:\r\n")));
PrintBacklightSettings(&bklSettings);
#endif
// If Timeout is disabled, Not need wait Inactive event.
if(fACOnline == TRUE)
{
if(bklSettings.fACTimeoutEnable)
hWaitList[bklUserInactivityEvent] = hUserInactivityEvent;
else
hWaitList[bklUserInactivityEvent] = hevDummy;
}
else
{
if(bklSettings.fBattTimeoutEnable)
hWaitList[bklUserInactivityEvent] = hUserInactivityEvent;
else
hWaitList[bklUserInactivityEvent] = hevDummy;
}
break;
case (WAIT_OBJECT_0 + bklPowerNotificationEvent):
DWORD dwSize;
// Do not block on our message queue.
if(ReadMsgQueue(hMsgQ, &buf, QUEUE_SIZE, (LPDWORD)&dwSize, 0, &dwStatus))
{
GetSystemPowerStatusEx2(&sps, sizeof(sps), TRUE);
DEBUGMSG(ZONE_INFO, (_T("Line status is %s (0x%x)\r\n"),
sps.ACLineStatus == AC_LINE_ONLINE ? _T("AC") :
sps.ACLineStatus == AC_LINE_OFFLINE ? _T("Offline") :
sps.ACLineStatus == AC_LINE_BACKUP_POWER ? _T("Backup") :
sps.ACLineStatus == AC_LINE_UNKNOWN ? _T("Unknown") : _T("???"),
sps.ACLineStatus));
if(sps.ACLineStatus == AC_LINE_ONLINE)
{
DEBUGMSG(ZONE_INFO, (TEXT("fACOnline = TRUE;\r\n")));
if (fACOnline != TRUE)
{
fACOnline = TRUE;
BSPBacklightSetIntensity(bklSettings.dwACBacklightLevel);
}
}
else
{
DEBUGMSG(ZONE_INFO, (TEXT("fACOnline = FALSE;\r\n")));
if (fACOnline == TRUE)
{
fACOnline = FALSE;
BSPBacklightSetIntensity(bklSettings.dwBattBacklightLevel);
}
}
// Force backlight settings update.
SetEvent(hWaitList[bklControlPanelEvent]);
SetEvent(hWaitList[bklLevelChangeEvent]);
}
else
DEBUGMSG(ZONE_ERROR, (TEXT("ReadMsgQueue: ERROR:%d\n"), GetLastError()));
break;
case (WAIT_OBJECT_0 + bklUserInactivityEvent):
if(fACOnline == TRUE)
{
if(bklSettings.fACTimeoutEnable)
dwTimeout = (bklSettings.dwACTimeout) * 1000; //dwTimeout is in milli Second
else
dwTimeout = INFINITE ;
}
else
{
if(bklSettings.fBattTimeoutEnable)
dwTimeout = (bklSettings.dwBattTimeout) * 1000 ; //dwTimeout is in milli Second
else
dwTimeout = INFINITE ;
}
if(dwTimeout != INFINITE)
{
if (WaitForSingleObject(hUserActivityEvent, dwTimeout) == WAIT_TIMEOUT)
{
// shut off backlight
dwStatus = DevicePowerNotify(szName, D4, POWER_NAME);
if(dwStatus != ERROR_SUCCESS)
DEBUGMSG(ZONE_ERROR, (_T("%s: DevicePowerNotify(D4) failed %d\r\n"), szName, dwStatus));
else
{
// Keep it off until some user activity occurs
WaitForSingleObject(hUserActivityEvent, INFINITE);
// shut on backlight
dwStatus = DevicePowerNotify(szName, D0, POWER_NAME);
if(dwStatus != ERROR_SUCCESS)
DEBUGMSG(ZONE_ERROR, (_T("%s: DevicePowerNotify(D0) failed %d\r\n"), szName, dwStatus));
}
}
}
break;
case (WAIT_OBJECT_0 + bklLevelChangeEvent):
// Event for change in backlight level
// Get backlight levels from registry
DEBUGMSG(ZONE_INFO, (TEXT("backlight batt level = %d\r\n"), bklSettings.dwBattBacklightLevel));
DEBUGMSG(ZONE_INFO, (TEXT("backlight ac level = %d\r\n"), bklSettings.dwACBacklightLevel));
GetBacklightLevelSettings(&bklSettings);
// Set backlight levels depending on currently on battery or AC.
if(fACOnline) {
BSPBacklightSetIntensity(bklSettings.dwACBacklightLevel);
}
else {
BSPBacklightSetIntensity(bklSettings.dwBattBacklightLevel);
}
DEBUGMSG(ZONE_INFO, (TEXT("new backlight batt level = %d\r\n"), bklSettings.dwBattBacklightLevel));
DEBUGMSG(ZONE_INFO, (TEXT("new backlight ac level = %d\r\n"), bklSettings.dwACBacklightLevel));
break;
case WAIT_FAILED:
DEBUGMSG(ZONE_ERROR, (TEXT("WAIT_FAILED!\r\n")));
break;
default:
break;
}
} // end while
BKL_FUNCTION_EXIT();
return 0;
}
//------------------------------------------------------------------------------
//
// FUNCTION: BKL_Deinit
//
// DESCRIPTION: Deinitialize the card loader
// PARAMETERS:
//
// RETURNS:
//
//------------------------------------------------------------------------------
extern "C" BOOL BKL_Deinit(DWORD dwContext)
{
DWORD i;
DEBUGMSG(ZONE_FUNCTION, (TEXT("+BKL_Deinit: dwContext = 0x%x\r\n"), dwContext));
// Give the thread a chance to perform any cleanup.
if(ghInterruptServiceThread)
{
fIntrThreadLoop = FALSE;
SetEvent(hWaitList[bklControlPanelEvent]);
CloseHandle(ghInterruptServiceThread);
}
// Stop Power Notifications
if(hNotifications)
StopPowerNotifications(hNotifications);
if(hMsgQ)
CloseMsgQueue(hMsgQ);
// Close wait Handles
for(i = 0; i < dim(hWaitList); i++)
{
if(hWaitList[i])
CloseHandle(hWaitList[i]);
}
if(hevDummy)
CloseHandle(hevDummy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -