Advertisement
Guest User

backlight

a guest
Jan 24th, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Copyright (c) 2016 Anonymous
  3.  
  4. Permission is hereby granted, free of charge, to any person obtaining
  5. a copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sublicense, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11.  
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14.  
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23.  
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <Windows.h>
  27.  
  28. typedef struct _DISPLAY_BRIGHTNESS {
  29.     UCHAR ucDisplayPolicy;
  30.     UCHAR ucACBrightness;
  31.     UCHAR ucDCBrightness;
  32. } DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS;
  33.  
  34. #define DISPLAYPOLICY_AC 0x00000001
  35. #define DISPLAYPOLICY_DC 0x00000002
  36. #define DISPLAYPOLICY_BOTH 0x00000003
  37.  
  38. #define IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS \
  39.     CTL_CODE(FILE_DEVICE_VIDEO, 0x126, METHOD_BUFFERED, FILE_ANY_ACCESS)
  40. #define IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS \
  41.     CTL_CODE(FILE_DEVICE_VIDEO, 0x127, METHOD_BUFFERED, FILE_ANY_ACCESS)
  42. #define IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS \
  43.     CTL_CODE(FILE_DEVICE_VIDEO, 0x125, METHOD_BUFFERED, FILE_ANY_ACCESS)
  44.  
  45. void error(const wchar_t *where)
  46. {
  47.     wchar_t *msg = NULL;
  48.     DWORD dw = GetLastError();
  49.     if(FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
  50.                       | FORMAT_MESSAGE_IGNORE_INSERTS,
  51.                       NULL, dw, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
  52.                       (wchar_t *)&msg, 0, NULL) > 0)
  53.     {
  54.         fwprintf(stderr, L"Error in %ls: %ls", where, msg);
  55.     }
  56.     else
  57.     {
  58.         fwprintf(stderr, L"Error formatting error\n");
  59.     }
  60.     exit((int)dw);
  61. }
  62.  
  63. HANDLE openLcd(void)
  64. {
  65.     HANDLE h = CreateFileW(L"\\\\.\\LCD", GENERIC_READ | GENERIC_WRITE,
  66.                            FILE_SHARE_READ | FILE_SHARE_WRITE,
  67.                            NULL, OPEN_EXISTING, 0, NULL);
  68.     if(h == INVALID_HANDLE_VALUE)
  69.     {
  70.         error(L"CreateFileW");
  71.     }
  72.     return h;
  73. }
  74.  
  75. int getSupported(HANDLE h, unsigned char *vals)
  76. {
  77.     size_t returned;
  78.     if(DeviceIoControl(h, IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS, NULL, 0,
  79.                        (LPVOID)vals, (DWORD)256, (LPDWORD)&returned, NULL) == 0)
  80.     {
  81.         error(L"IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS");
  82.     }
  83.     return returned;
  84. }
  85.  
  86. DISPLAY_BRIGHTNESS getCurrent(HANDLE h)
  87. {
  88.     DISPLAY_BRIGHTNESS db;
  89.     size_t returned;
  90.     if(DeviceIoControl(h, IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS, NULL, 0,
  91.                        (LPVOID)&db, (DWORD)sizeof(db), (LPDWORD)&returned, NULL) == 0
  92.        || returned != sizeof(db))
  93.     {
  94.         error(L"IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS");
  95.     }
  96.     return db;
  97. }
  98.  
  99. void setCurrent(HANDLE h, unsigned char val)
  100. {
  101.     DISPLAY_BRIGHTNESS db = getCurrent(h);
  102.     size_t returned;
  103.     if(db.ucDisplayPolicy == DISPLAYPOLICY_AC)
  104.     {
  105.         db.ucACBrightness = val;
  106.     }
  107.     else
  108.     {
  109.         db.ucDCBrightness = val;
  110.     }
  111.     if(DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (LPVOID)&db, (DWORD)sizeof(db),
  112.                        NULL, 0, (LPDWORD)&returned, NULL) == 0
  113.        || returned != sizeof(db))
  114.     {
  115.         error(L"IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS");
  116.     }
  117. }
  118.  
  119. void queryFull(char *progname)
  120. {
  121.     int i;
  122.     DISPLAY_BRIGHTNESS db;
  123.     HANDLE h = openLcd();
  124.     unsigned char vals[256];
  125.     int count = getSupported(h, vals);
  126.     printf("Supported brigtness values: ");
  127.     for(i = 0; i < count; i++)
  128.     {
  129.         printf("%hhd ", vals[i]);
  130.     }
  131.     printf("\n");
  132.     db = getCurrent(h);
  133.     if(db.ucDisplayPolicy == DISPLAYPOLICY_AC)
  134.     {
  135.         printf("Current: AC power, brightness: %hhd, DC brigtness: %hhd\n",
  136.                db.ucACBrightness, db.ucDCBrightness);
  137.     }
  138.     else
  139.     {
  140.         printf("Current: DC power, brightness: %hhd, AC brigtness: %hhd\n",
  141.                db.ucDCBrightness, db.ucACBrightness);
  142.     }
  143.     printf("Use \"%s q\" to query current brightness, \"%s <value>\" to set current brightness\n",
  144.            progname, progname);
  145. }
  146.  
  147. unsigned char currentValue(DISPLAY_BRIGHTNESS db)
  148. {
  149.     if(db.ucDisplayPolicy == DISPLAYPOLICY_AC)
  150.     {
  151.         return db.ucACBrightness;
  152.     }
  153.     else
  154.     {
  155.         return db.ucDCBrightness;
  156.     }
  157. }
  158.  
  159. void query(void)
  160. {
  161.     HANDLE h = openLcd();
  162.     DISPLAY_BRIGHTNESS db = getCurrent(h);
  163.     printf("%hhd", currentValue(db));
  164. }
  165.  
  166. void set(long value)
  167. {
  168.     int i;
  169.     HANDLE h = openLcd();
  170.     unsigned char vals[256];
  171.     int count = getSupported(h, vals);
  172.     int pos = 0;
  173.     long min = value - vals[0];
  174.     for(i = 1; i < count; i++)
  175.     {
  176.         if(value - vals[i] < min)
  177.         {
  178.             pos = i;
  179.             min = value - vals[i];
  180.         }
  181.     }
  182.     setCurrent(h, vals[pos]);
  183. }
  184.  
  185. int main(int argc, char **argv)
  186. {
  187.     if(argc < 2)
  188.     {
  189.         queryFull(argv[0]);
  190.     }
  191.     else
  192.     {
  193.         if(argv[1][0] == 'q')
  194.         {
  195.             query();
  196.         }
  197.         else
  198.         {
  199.             char *c = argv[1];
  200.             long val = strtol(c, &c, 10);
  201.             if(c == argv[1])
  202.             {
  203.                 queryFull(argv[0]);
  204.             }
  205.             else
  206.             {
  207.                 set(val);
  208.             }
  209.         }
  210.     }
  211.     return 0;
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement