netripper

netripper

Dec 23rd, 2009
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.95 KB | None | 0 0
  1. // KeypadLedControl.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6.  
  7. // From MSDN: http://msdn.microsoft.com/en-us/library/ms838354.aspx
  8. #define QUERYESCSUPPORT 8
  9. #define SETPOWERMANAGEMENT 6147
  10. #define GETPOWERMANAGEMENT 6148
  11.  
  12. typedef struct VIDEOPOWER_MANAGEMENT {
  13.     ULONG Length;
  14.     ULONG DPMSVersion;
  15.     ULONG PowerState;
  16. } VIDEO_POWER_MANAGEMENT, *PVIDEO_POWER_MANAGEMENT;
  17.  
  18. int _tmain(int argc, _TCHAR* argv[])
  19. {
  20.     // Create an event for ourselves, so that we can be turned off as well
  21.     HANDLE appEvent = CreateEvent(NULL, TRUE, FALSE, L"KeypadLedControlApp");
  22.     if (appEvent == NULL)
  23.     {
  24.         MessageBox(NULL, L"Couldn't create an event for ourselves", L"Error", MB_OK);
  25.         return 1;
  26.     }
  27.  
  28.     // If the app is already running, we set the event so that it will close itself.
  29.     if (GetLastError() == ERROR_ALREADY_EXISTS)
  30.     {
  31.         SetEvent(appEvent);
  32.         CloseHandle(appEvent);
  33.         return 1;
  34.     }
  35.  
  36.     // Get the keyped led event (this one is used for Leo at least)
  37.     HANDLE iconEvent = CreateEvent(NULL, TRUE, FALSE, L"IconLedOnEvent");
  38.     if (iconEvent == NULL)
  39.     {
  40.         // Oops, error creating the event?
  41.         MessageBox(NULL, L"Couldn't create the 'IconLedOnEvent' keypad light event", L"Error", MB_OK);
  42.         return 1;
  43.     }
  44.     if (GetLastError() != ERROR_ALREADY_EXISTS)
  45.     {
  46.         // This even didn't exist yet. Let's see if we can try another event (it's IconLedEvent for Rhodium)
  47.         CloseHandle(iconEvent);
  48.         iconEvent = CreateEvent(NULL, TRUE, FALSE, L"IconLedEvent");
  49.         if (iconEvent == NULL)
  50.         {
  51.             MessageBox(NULL, L"Couldn't create the 'IconLedEvent' keypad light event", L"Error", MB_OK);
  52.             return 1;
  53.         }
  54.         if (GetLastError() != ERROR_ALREADY_EXISTS)
  55.         {
  56.             // This event didn't exist either.
  57.             MessageBox(NULL, L"Sorry, this device is not supported it seems", L"Error", MB_OK);
  58.             CloseHandle(iconEvent);
  59.             return 1;
  60.         }
  61.     }
  62.  
  63.     // Check if the driver supports power management information
  64.     HDC gdc = GetDC(NULL);
  65.     int iESC = GETPOWERMANAGEMENT;
  66.     if (ExtEscape(gdc, QUERYESCSUPPORT, sizeof(int), (LPCSTR)&iESC, 0, NULL) == 0)
  67.     {
  68.         MessageBox(NULL, L"This device does not support checking for LCD state", L"Error", MB_OK);
  69.         CloseHandle(iconEvent);
  70.         ReleaseDC(NULL, gdc);
  71.         return 1;
  72.     }
  73.  
  74.     VIDEO_POWER_MANAGEMENT pm;
  75.     pm.Length = sizeof(pm);
  76.     int ret;
  77.     while (true)
  78.     {
  79.         // Get current power management information
  80.         ret = ExtEscape(gdc, GETPOWERMANAGEMENT, 0, NULL, pm.Length, (LPSTR)&pm);
  81.  
  82.         // If the LCD is on, set the event so that the lights go on
  83.         if (pm.PowerState == 1)
  84.         {
  85.             SetEvent(iconEvent);
  86.         }
  87.        
  88.         // We'll keep on running this app as long as the event hasn't been set (by starting this app again)
  89.         if (WaitForSingleObject(appEvent, 8000) != WAIT_TIMEOUT)
  90.         {
  91.             break;
  92.         }
  93.     }
  94.  
  95.     // Cleanup
  96.     ReleaseDC(NULL, gdc);
  97.     CloseHandle(iconEvent);
  98.     CloseHandle(appEvent);
  99.    
  100.     return 0;
  101. }
  102.  
  103.  
Add Comment
Please, Sign In to add comment