Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Copyright (c) 2016 Anonymous
- Permission is hereby granted, free of charge, to any person obtaining
- a copy of this software and associated documentation files (the
- "Software"), to deal in the Software without restriction, including
- without limitation the rights to use, copy, modify, merge, publish,
- distribute, sublicense, and/or sell copies of the Software, and to
- permit persons to whom the Software is furnished to do so, subject to
- the following conditions:
- The above copyright notice and this permission notice shall be
- included in all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
- #include <stdlib.h>
- #include <stdio.h>
- #include <Windows.h>
- typedef struct _DISPLAY_BRIGHTNESS {
- UCHAR ucDisplayPolicy;
- UCHAR ucACBrightness;
- UCHAR ucDCBrightness;
- } DISPLAY_BRIGHTNESS, *PDISPLAY_BRIGHTNESS;
- #define DISPLAYPOLICY_AC 0x00000001
- #define DISPLAYPOLICY_DC 0x00000002
- #define DISPLAYPOLICY_BOTH 0x00000003
- #define IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS \
- CTL_CODE(FILE_DEVICE_VIDEO, 0x126, METHOD_BUFFERED, FILE_ANY_ACCESS)
- #define IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS \
- CTL_CODE(FILE_DEVICE_VIDEO, 0x127, METHOD_BUFFERED, FILE_ANY_ACCESS)
- #define IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS \
- CTL_CODE(FILE_DEVICE_VIDEO, 0x125, METHOD_BUFFERED, FILE_ANY_ACCESS)
- void error(const wchar_t *where)
- {
- wchar_t *msg = NULL;
- DWORD dw = GetLastError();
- if(FormatMessageW(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM
- | FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL, dw, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
- (wchar_t *)&msg, 0, NULL) > 0)
- {
- fwprintf(stderr, L"Error in %ls: %ls", where, msg);
- }
- else
- {
- fwprintf(stderr, L"Error formatting error\n");
- }
- exit((int)dw);
- }
- HANDLE openLcd(void)
- {
- HANDLE h = CreateFileW(L"\\\\.\\LCD", GENERIC_READ | GENERIC_WRITE,
- FILE_SHARE_READ | FILE_SHARE_WRITE,
- NULL, OPEN_EXISTING, 0, NULL);
- if(h == INVALID_HANDLE_VALUE)
- {
- error(L"CreateFileW");
- }
- return h;
- }
- int getSupported(HANDLE h, unsigned char *vals)
- {
- size_t returned;
- if(DeviceIoControl(h, IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS, NULL, 0,
- (LPVOID)vals, (DWORD)256, (LPDWORD)&returned, NULL) == 0)
- {
- error(L"IOCTL_VIDEO_QUERY_SUPPORTED_BRIGHTNESS");
- }
- return returned;
- }
- DISPLAY_BRIGHTNESS getCurrent(HANDLE h)
- {
- DISPLAY_BRIGHTNESS db;
- size_t returned;
- if(DeviceIoControl(h, IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS, NULL, 0,
- (LPVOID)&db, (DWORD)sizeof(db), (LPDWORD)&returned, NULL) == 0
- || returned != sizeof(db))
- {
- error(L"IOCTL_VIDEO_QUERY_DISPLAY_BRIGHTNESS");
- }
- return db;
- }
- void setCurrent(HANDLE h, unsigned char val)
- {
- DISPLAY_BRIGHTNESS db = getCurrent(h);
- size_t returned;
- if(db.ucDisplayPolicy == DISPLAYPOLICY_AC)
- {
- db.ucACBrightness = val;
- }
- else
- {
- db.ucDCBrightness = val;
- }
- if(DeviceIoControl(h, IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS, (LPVOID)&db, (DWORD)sizeof(db),
- NULL, 0, (LPDWORD)&returned, NULL) == 0
- || returned != sizeof(db))
- {
- error(L"IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS");
- }
- }
- void queryFull(char *progname)
- {
- int i;
- DISPLAY_BRIGHTNESS db;
- HANDLE h = openLcd();
- unsigned char vals[256];
- int count = getSupported(h, vals);
- printf("Supported brigtness values: ");
- for(i = 0; i < count; i++)
- {
- printf("%hhd ", vals[i]);
- }
- printf("\n");
- db = getCurrent(h);
- if(db.ucDisplayPolicy == DISPLAYPOLICY_AC)
- {
- printf("Current: AC power, brightness: %hhd, DC brigtness: %hhd\n",
- db.ucACBrightness, db.ucDCBrightness);
- }
- else
- {
- printf("Current: DC power, brightness: %hhd, AC brigtness: %hhd\n",
- db.ucDCBrightness, db.ucACBrightness);
- }
- printf("Use \"%s q\" to query current brightness, \"%s <value>\" to set current brightness\n",
- progname, progname);
- }
- unsigned char currentValue(DISPLAY_BRIGHTNESS db)
- {
- if(db.ucDisplayPolicy == DISPLAYPOLICY_AC)
- {
- return db.ucACBrightness;
- }
- else
- {
- return db.ucDCBrightness;
- }
- }
- void query(void)
- {
- HANDLE h = openLcd();
- DISPLAY_BRIGHTNESS db = getCurrent(h);
- printf("%hhd", currentValue(db));
- }
- void set(long value)
- {
- int i;
- HANDLE h = openLcd();
- unsigned char vals[256];
- int count = getSupported(h, vals);
- int pos = 0;
- long min = value - vals[0];
- for(i = 1; i < count; i++)
- {
- if(value - vals[i] < min)
- {
- pos = i;
- min = value - vals[i];
- }
- }
- setCurrent(h, vals[pos]);
- }
- int main(int argc, char **argv)
- {
- if(argc < 2)
- {
- queryFull(argv[0]);
- }
- else
- {
- if(argv[1][0] == 'q')
- {
- query();
- }
- else
- {
- char *c = argv[1];
- long val = strtol(c, &c, 10);
- if(c == argv[1])
- {
- queryFull(argv[0]);
- }
- else
- {
- set(val);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement