a1ive / nwinfo

Hardware information utility for Windows
https://a1ive.github.io/nwinfo/
The Unlicense
65 stars 12 forks source link

Get / Set monitor's brightness value #25

Closed a1ive closed 12 months ago

a1ive commented 1 year ago

SetMonitorBrightness https://learn.microsoft.com/en-us/windows/win32/api/highlevelmonitorconfigurationapi/nf-highlevelmonitorconfigurationapi-setmonitorbrightness SetDeviceGammaRamp https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-setdevicegammaramp IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS https://learn.microsoft.com/en-us/windows/win32/power/ioctl-video-set-display-brightness

https://stackoverflow.com/questions/39262619/setting-brightness-on-windows-10-using-c-winapi https://gist.github.com/pavel-a/dd3a4320176e69a0f6c4b4871e69e56b https://www.nirsoft.net/vc/change_screen_brightness.html

a1ive commented 1 year ago
struct MATCH_DISPLAY_CTX
{
    LPCWSTR name;
    HMONITOR hm;
};

static BOOL MatchDisplayName(HMONITOR hMonitor, HDC hDC, LPRECT lpRect, LPARAM lParam)
{
    struct MATCH_DISPLAY_CTX* ctx = (struct MATCH_DISPLAY_CTX*)lParam;
    MONITORINFOEXW info = { .cbSize = sizeof(MONITORINFOEXW) };
    if (GetMonitorInfoW(hMonitor, (LPMONITORINFO)&info))
    {
        if (wcscmp(ctx->name, info.szDevice) == 0)
        {
            ctx->hm = hMonitor;
            return FALSE;
        }
    }
    return TRUE;
}

HMONITOR
NWL_GetMonitorHandleByHwID(LPCWSTR hwId)
{
    DWORD i, j;
    int len;
    WCHAR displayId[32];
    DISPLAY_DEVICEW displayDevice = { .cb = sizeof(DISPLAY_DEVICEW) };
    // MONITOR\XXXXXXX
    if (wcsncmp(hwId, L"MONITOR\\", 8) != 0)
        return NULL;
    len = swprintf(displayId, 32, L"\\\\?\\DISPLAY#%s#", &hwId[8]);
    if (len <= 0)
        return NULL;
    for (i = 0; EnumDisplayDevicesW(NULL, i, &displayDevice, EDD_GET_DEVICE_INTERFACE_NAME); i++)
    {
        DISPLAY_DEVICEW monitorDevice = { .cb = sizeof(DISPLAY_DEVICEW) };
        for (j = 0; EnumDisplayDevicesW(displayDevice.DeviceName, j, &monitorDevice, EDD_GET_DEVICE_INTERFACE_NAME); j++)
        {
            //MonitorDevice.DeviceID
            if (wcsncmp(monitorDevice.DeviceID, displayId, (size_t)len) == 0)
            {
                struct MATCH_DISPLAY_CTX ctx = { .name = displayDevice.DeviceName, .hm = NULL };
                EnumDisplayMonitors(NULL, NULL, MatchDisplayName, (LPARAM) &ctx);
                return ctx.hm;
            }
        }
    }
    return NULL;
}
a1ive commented 1 year ago
void nwinfo_display(void)
{
    DWORD iDevNum = 0;
    DEVMODEA DevMode = { .dmSize = sizeof(DEVMODEA) };
    DISPLAY_DEVICEA DisplayDevice = { .cb = sizeof(DISPLAY_DEVICEA) };

    while (EnumDisplayDevicesA(NULL, iDevNum, &DisplayDevice, EDD_GET_DEVICE_INTERFACE_NAME))
    {
        DWORD State = DisplayDevice.StateFlags;
        printf("%s\n", DisplayDevice.DeviceName);
        printf("  %s\n", DisplayDevice.DeviceString);
        printf("  Device state: %s%s%s%s%s%s\n",
            (State & DISPLAY_DEVICE_ACTIVE) ? "active" : "deactive",
            (State & DISPLAY_DEVICE_MIRRORING_DRIVER) ? " mirroring" : "",
            (State & DISPLAY_DEVICE_MODESPRUNED) ? " modespruned" : "",
            (State & DISPLAY_DEVICE_PRIMARY_DEVICE) ? " primary" : "",
            (State & DISPLAY_DEVICE_REMOVABLE) ? " removable" : "",
            (State & DISPLAY_DEVICE_VGA_COMPATIBLE) ? " vga" : "");
        if (EnumDisplaySettingsExA(DisplayDevice.DeviceName, ENUM_CURRENT_SETTINGS, &DevMode, 0))
        {
            printf("  Current mode: %ux%u, %u Hz\n", DevMode.dmPelsWidth, DevMode.dmPelsHeight, DevMode.dmDisplayFrequency);
        }
        iDevNum++;
    }
}