Answered notify when display is on or off

  • Thursday, June 18, 2009 2:57 AM
     
     
    Is there a way to get notified when the display is on or off? I can get notified when pen is down by waiting the event of "penisdown" on my diamond but can't find out any way to check the state of display.

All Replies

  • Thursday, June 18, 2009 5:03 AM
     
     
    Hi,

    I think you are talking about suspend mode. Joel made very good sample on power state modes..

    http://www.codeproject.com/KB/mobile/WiMoPower1.aspx


    Regards,
    Malleswar

  • Thursday, June 18, 2009 8:03 AM
     
     Answered
    Hi,

    try this code

    typedef enum tagPowerState {
       BacklightOff,
       On,
       Resuming,
       ScreenOff,
       Suspended,
       Unattended,
       UserIdle,

       Unknown
    } PowerState;

    // see http://msdn.microsoft.com/en-us/library/aa930499.aspx for details
    PowerState cast(LPCTSTR szPowerState)
    {
       if (!_tcsicmp(_T("BacklightOff"), szPowerState)) {
          return BacklightOff;
       } else if (!_tcsicmp(_T("On"), szPowerState)) {
          return On;
       } else if (!_tcsicmp(_T("Resuming"), szPowerState)) {
          return Resuming;
       } else if (!_tcsicmp(_T("ScreenOff"), szPowerState)) {
          return ScreenOff;
       } else if (!_tcsicmp(_T("Suspended"), szPowerState)) {
          return Suspended;
       } else if (!_tcsicmp(_T("Unattended"), szPowerState)) {
          return Unattended;
       } else if (!_tcsicmp(_T("UserIdle"), szPowerState)) {
          return UserIdle;
       } 

       return Unknown;
    }

    HANDLE         g_hMsgQ;
    HANDLE         g_hPowerNotification;
    HANDLE         g_hThreadPowerEvent;
    PowerState     g_powerState;

     // start monitor power changes.
     MSGQUEUEOPTIONS msgOptions = {0};  
     msgOptions.dwSize = sizeof(MSGQUEUEOPTIONS);
     msgOptions.dwFlags = 0;
     msgOptions.dwMaxMessages = 20;
     msgOptions.cbMaxMessage = sizeof(POWER_BROADCAST) + MAX_PATH;
     msgOptions.bReadAccess = TRUE;

     g_hMsgQ = CreateMsgQueue(NULL, &msgOptions);
     if (g_hMsgQ) {
          g_hPowerNotification = RequestPowerNotifications(g_hMsgQ, PBT_TRANSITION);
          if (!g_hPowerNotification) {
             DEBUGMSG(1, (TEXT("RequestPowerNotifications error:0x%08x\r\n"), GetLastError()));
             return FALSE;
          }

          g_hThreadPowerEvent = CreateThread(NULL, 0, ThreadProcPowerEvent, NULL, 0, NULL);
          if (!g_hThreadPowerEvent) {
             DEBUGMSG(1, (TEXT("CreateThread error:0x%08x\r\n"), GetLastError()));
             return FALSE;
          }
       } else {
          DEBUGMSG(1, (TEXT("CreateMsgQueue error:0x%08x\r\n"), GetLastError()));
          return FALSE;
       }


    // do something here...

    // release resourcea
    CloseMsgQueue(g_hMsgQ);
    StopPowerNotifications(g_hPowerNotification);
    CloseHandle(g_hThreadPowerEvent);



    #include "tpcshell.h"
    #pragma comment(lib, "Aygshell.lib")
    DWORD WINAPI ThreadProcPowerEvent(LPVOID lpParam)
    {
       POWER_BROADCAST pwrInfo[1024];
       DWORD dwRead;
       DWORD dwFlags;  
       DWORD dwWait;
       BOOL bRet;

       for (;;) {
          dwWait = WaitForSingleObject(g_hMsgQ, INFINITE);
          if (WAIT_OBJECT_0 == dwWait) {
             bRet = ReadMsgQueue(g_hMsgQ, &pwrInfo, sizeof(pwrInfo), &dwRead, INFINITE, &dwFlags);
             if (bRet) {
                DEBUGMSG(1, (TEXT("POWER_BROADCAST info:\r\n")));
                DEBUGMSG(1, (TEXT("Message: 0x%08x\r\n"), pwrInfo->Message));
                DEBUGMSG(1, (TEXT("Flags: 0x%08x\r\n"), pwrInfo->Flags));
                DEBUGMSG(1, (TEXT("SystemPowerState: %s\r\n"), pwrInfo->SystemPowerState));

                g_powerState = cast(pwrInfo->SystemPowerState);       

                if (g_powerState == BacklightOff || g_powerState == ScreenOff || g_powerState == UserIdle) {
                   SetSystemPowerState(NULL, POWER_STATE_ON, 0);
                }
             } else {
                DEBUGMSG(1, (TEXT("ReadMsgQueue error: 0x%08x\r\n"), GetLastError()));
             }
          } else {
             // an error occurred or the message queue has been released.
             DEBUGMSG(1, (TEXT("ReadMsgQueue ret: 0x%08x\r\n"), dwWait));
             break;
          }
       }
      
       DEBUGMSG(1, (TEXT("ThreadProcPowerEvent thread exiting\r\n")));
       return 0;
    }
    • Marked As Answer by MicOne Monday, June 22, 2009 1:47 AM
    •  
  • Tuesday, June 23, 2009 7:05 AM
     
     
    Thanks ten0s, the code works well. By the way, the name of suspend event for my diamond is not the same as the MS doc, which spells just "suspend"