Compare commits

...

4 Commits

7 changed files with 147 additions and 115 deletions

109
WinPowerDMS/DisplayMode.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "DisplayMode.h"
BOOL DisplayModeEquals(const DISPLAY_MODE* a, const DISPLAY_MODE* b) {
return a->width == b->width && a->height == b->height && a->refresh == b->refresh;
}
// I hate parsing the string here, but the alternative requires extra memory management.
DISPLAY_MODE GetModeFromCB(HWND hComboBox) {
DISPLAY_MODE mode = { 0 };
LRESULT selectedIndex = SendMessage(hComboBox, CB_GETCURSEL, 0, 0);
if (selectedIndex != CB_ERR) {
LRESULT len = SendMessage(hComboBox, CB_GETLBTEXTLEN, selectedIndex, 0);
if (len != CB_ERR) {
LPWSTR text = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*text));
if (text) {
SendMessage(hComboBox, CB_GETLBTEXT, selectedIndex, (LPARAM)text);
swscanf_s(text, L"%dx%d @ %d Hz", &mode.width, &mode.height, &mode.refresh);
HeapFree(GetProcessHeap(), 0, text);
}
}
}
return mode;
}
// returns the result of the ChangeDisplaySettings call that this results in.
LONG ChangeDisplayMode(LPCWSTR displayName, const DISPLAY_MODE* mode, DWORD dwFlags) {
DEVMODEW devMode = {
.dmSize = sizeof(devMode),
.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY,
.dmPelsWidth = mode->width,
.dmPelsHeight = mode->height,
.dmDisplayFrequency = mode->refresh
};
if (displayName) wcscpy_s(devMode.dmDeviceName, sizeof(devMode.dmDeviceName) / sizeof(devMode.dmDeviceName[0]), displayName);
return ChangeDisplaySettings(&devMode, dwFlags);
}
struct MessageBoxParams {
HWND hWnd;
LPCWSTR lpText;
LPCWSTR lpCaption;
UINT uType;
};
static DWORD WINAPI MessageBoxAsync(LPVOID lpParam) {
struct MessageBoxParams* params = lpParam;
return MessageBox(params->hWnd, params->lpText, params->lpCaption, params->uType);
}
struct TestDisplayModeParams {
HWND hDlg;
WCHAR displayName[32];
DISPLAY_MODE mode;
};
static DWORD WINAPI TestDisplayModeThread(LPVOID lpParam) {
struct TestDisplayModeParams* params = lpParam;
DEVMODEW originalMode = { .dmSize = sizeof(originalMode) };
EnumDisplaySettings(params->displayName, ENUM_CURRENT_SETTINGS, &originalMode);
ChangeDisplayMode(params->displayName, &params->mode, CDS_FULLSCREEN);
// Create the message box about the resolution change
WCHAR msgText[96];
swprintf_s(msgText, sizeof(msgText) / sizeof(msgText[0]),
L"Testing %dx%d @ %d Hz\nThe display mode will reset back in 10 seconds.",
params->mode.width, params->mode.height, params->mode.refresh);
struct MessageBoxParams mbParams = {
.hWnd = params->hDlg,
.lpText = msgText,
.lpCaption = L"Resolution Test",
.uType = MB_OK | MB_ICONINFORMATION
};
HANDLE mbThread = CreateThread(NULL, 0, MessageBoxAsync, &mbParams, 0, NULL);
if (mbThread) {
CloseHandle(mbThread);
Sleep(10000);
ChangeDisplaySettings(&originalMode, 0);
}
else {
MessageBox(params->hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR);
}
HeapFree(GetProcessHeap(), 0, params);
return 0;
}
void TestDisplayMode(HWND hDlg, LPCWSTR displayName, DISPLAY_MODE* mode) {
struct TestDisplayModeParams* tdmParams = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdmParams));
if (tdmParams) {
tdmParams->hDlg = hDlg;
if (tdmParams->displayName) wcscpy_s(tdmParams->displayName, sizeof(tdmParams->displayName) / sizeof(tdmParams->displayName[0]), displayName);
tdmParams->mode = *mode;
HANDLE tdmThread = CreateThread(NULL, 0, TestDisplayModeThread, tdmParams, 0, NULL);
if (tdmThread) CloseHandle(tdmThread);
else {
HeapFree(GetProcessHeap(), 0, tdmParams);
MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR);
}
}
else {
MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR);
}
}

19
WinPowerDMS/DisplayMode.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
typedef struct {
DWORD width;
DWORD height;
DWORD refresh;
} DISPLAY_MODE;
BOOL DisplayModeEquals(const DISPLAY_MODE* a, const DISPLAY_MODE* b);
DISPLAY_MODE GetModeFromCB(HWND hComboBox);
// returns the result of the ChangeDisplaySettings call that this results in.
LONG ChangeDisplayMode(LPCWSTR displayName, const DISPLAY_MODE* mode, DWORD dwFlags);
void TestDisplayMode(HWND hDlg, LPCWSTR displayName, DISPLAY_MODE* mode);

View File

@ -8,6 +8,7 @@
#include <commctrl.h>
#include "resource.h"
#include "DisplayMode.h"
// Link Common Controls v6 for visual styling
#pragma comment(linker,"\"/manifestdependency:type='win32' \
@ -17,114 +18,6 @@ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#define WM_TRAYICON (WM_USER + 1)
#define TRAY_ICON_ID 1001
typedef struct {
DWORD width;
DWORD height;
DWORD refresh;
} DISPLAY_MODE;
BOOL DisplayModeEquals(const DISPLAY_MODE* a, const DISPLAY_MODE* b) {
return a->width == b->width && a->height == b->height && a->refresh == b->refresh;
}
#define MODE_SELECTED(mode) (!DisplayModeEquals(&mode, &(DISPLAY_MODE) { 0 }))
// I hate parsing the string here, but the alternative requires extra memory management.
static DISPLAY_MODE GetModeFromCB(HWND hComboBox) {
DISPLAY_MODE mode = { 0 };
LRESULT selectedIndex = SendMessage(hComboBox, CB_GETCURSEL, 0, 0);
if (selectedIndex != CB_ERR) {
LRESULT len = SendMessage(hComboBox, CB_GETLBTEXTLEN, selectedIndex, 0);
if (len != CB_ERR) {
LPWSTR text = HeapAlloc(GetProcessHeap(), 0, len * sizeof(*text));
if (text) {
SendMessage(hComboBox, CB_GETLBTEXT, selectedIndex, (LPARAM) text);
swscanf_s(text, L"%dx%d @ %d Hz", &mode.width, &mode.height, &mode.refresh);
HeapFree(GetProcessHeap(), 0, text);
}
}
}
return mode;
}
// returns the result of the ChangeDisplaySettings call that this results in.
static LONG ChangeDisplayMode(const DISPLAY_MODE* mode, DWORD dwFlags) {
DEVMODE devMode = {
.dmSize = sizeof(devMode),
.dmFields = DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY,
.dmPelsWidth = mode->width,
.dmPelsHeight = mode->height,
.dmDisplayFrequency = mode->refresh
};
return ChangeDisplaySettings(&devMode, dwFlags);
}
struct MessageBoxParams {
HWND hWnd;
LPCWSTR lpText;
LPCWSTR lpCaption;
UINT uType;
};
static DWORD WINAPI MessageBoxAsync(LPVOID lpParam) {
struct MessageBoxParams* params = lpParam;
return MessageBox(params->hWnd, params->lpText, params->lpCaption, params->uType);
}
struct TestDisplayModeParams {
HWND hDlg;
DISPLAY_MODE mode;
};
static DWORD WINAPI TestDisplayModeThread(LPVOID lpParam) {
struct TestDisplayModeParams* params = lpParam;
DEVMODE originalMode = { .dmSize = sizeof(originalMode) };
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &originalMode);
ChangeDisplayMode(&params->mode, CDS_FULLSCREEN);
// Create the message box about the resolution change
WCHAR msgText[96];
swprintf_s(msgText, sizeof(msgText) / sizeof(msgText[0]),
L"Testing %dx%d @ %d Hz\nThe display mode will reset back in 10 seconds.",
params->mode.width, params->mode.height, params->mode.refresh);
struct MessageBoxParams mbParams = {
.hWnd = params->hDlg,
.lpText = msgText,
.lpCaption = L"Resolution Test",
.uType = MB_OK | MB_ICONINFORMATION
};
HANDLE mbThread = CreateThread(NULL, 0, MessageBoxAsync, &mbParams, 0, NULL);
if (mbThread) {
CloseHandle(mbThread);
Sleep(10000);
ChangeDisplaySettings(&originalMode, 0);
}
else {
MessageBox(params->hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR);
}
HeapFree(GetProcessHeap(), 0, params);
return 0;
}
static void TestDisplayMode(HWND hDlg, DISPLAY_MODE* mode) {
struct TestDisplayModeParams* tdmParams = HeapAlloc(GetProcessHeap(), 0, sizeof(*tdmParams));
if (tdmParams) {
tdmParams->hDlg = hDlg;
tdmParams->mode = *mode;
HANDLE tdmThread = CreateThread(NULL, 0, TestDisplayModeThread, tdmParams, 0, NULL);
if (tdmThread) CloseHandle(tdmThread);
else {
HeapFree(GetProcessHeap(), 0, tdmParams);
MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR);
}
}
else {
MessageBox(hDlg, L"Failed to test resolution.", L"Error", MB_OK | MB_ICONERROR);
}
}
typedef struct {
DISPLAY_MODE modeBatt;
DISPLAY_MODE modeAC;
@ -194,7 +87,7 @@ static BOOL LoadPrefs(void) {
}
static DISPLAY_MODE GetCurrentDisplayMode(void) {
DEVMODE currentMode = { .dmSize = sizeof(currentMode) };
DEVMODEW currentMode = { .dmSize = sizeof(currentMode) };
EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &currentMode);
return (DISPLAY_MODE) {
.width = currentMode.dmPelsWidth,
@ -211,7 +104,7 @@ static INT_PTR CALLBACK PrefsDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPA
switch (uMsg) {
case WM_INITDIALOG: {
// devMode object that will be enumerated
DEVMODE devMode;
DEVMODEW devMode;
ZeroMemory(&devMode, sizeof(devMode));
devMode.dmSize = sizeof(devMode);
@ -248,7 +141,7 @@ static INT_PTR CALLBACK PrefsDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPA
SendMessage(hCheckStartup, BM_SETCHECK, userPrefs.runAtStartup ? BST_CHECKED : BST_UNCHECKED, 0);
return TRUE;
}
case WM_COMMAND: {
switch (LOWORD(wParam)) {
case IDC_BUTTON_APPLY:
@ -265,12 +158,12 @@ static INT_PTR CALLBACK PrefsDialogProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPA
}
case IDC_BUTTON_TEST_BATT: {
DISPLAY_MODE mode = GetModeFromCB(hComboBatt);
TestDisplayMode(hDlg, &mode);
TestDisplayMode(hDlg, NULL, &mode);
return TRUE;
}
case IDC_BUTTON_TEST_AC: {
DISPLAY_MODE mode = GetModeFromCB(hComboAC);
TestDisplayMode(hDlg, &mode);
TestDisplayMode(hDlg, NULL, &mode);
return TRUE;
}
break;
@ -329,7 +222,7 @@ static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l
if (GetSystemPowerStatus(&powerStatus) &&
(DisplayModeEquals(&currentMode, &userPrefs.modeBatt) || DisplayModeEquals(&currentMode, &userPrefs.modeAC)))
{
ChangeDisplayMode(powerStatus.ACLineStatus ? &userPrefs.modeAC : &userPrefs.modeBatt, CDS_UPDATEREGISTRY);
ChangeDisplayMode(NULL, powerStatus.ACLineStatus ? &userPrefs.modeAC : &userPrefs.modeBatt, CDS_UPDATEREGISTRY);
}
}
break;

Binary file not shown.

View File

@ -144,9 +144,11 @@
</Manifest>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="DisplayMode.c" />
<ClCompile Include="WinPowerDMS.c" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="DisplayMode.h" />
<ClInclude Include="resource.h" />
</ItemGroup>
<ItemGroup>

View File

@ -18,11 +18,17 @@
<ClCompile Include="WinPowerDMS.c">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="DisplayMode.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="DisplayMode.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="WinPowerDMS.rc">

View File

@ -11,6 +11,9 @@
#define IDC_BUTTON_APPLY 1005
#define IDC_LIST1 1006
#define IDC_CHECK_STARTUP 1007
#define IDC_COMBO_DISPLAY 1008
#define IDC_CHECK_ENABLE_DISPLAY 1009
#define IDC_CHECK_DISPLAY 1009
// Next default values for new objects
//
@ -18,7 +21,7 @@
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 107
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1008
#define _APS_NEXT_CONTROL_VALUE 1012
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif