Add sources

This commit is contained in:
Kirill Kirilenko 2021-01-01 15:21:12 +03:00
parent 19d624f45c
commit 3412245ae7
4 changed files with 244 additions and 0 deletions

118
.clang-format Normal file
View file

@ -0,0 +1,118 @@
---
BasedOnStyle: WebKit
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveBitFields: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlines: DontAlign
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllConstructorInitializersOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine: Inline
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: Yes
BinPackArguments: true
BinPackParameters: false
BraceWrapping:
AfterCaseLabel: true
AfterClass: true
AfterControlStatement: Always
AfterEnum: true
AfterFunction: true
AfterNamespace: false
AfterStruct: true
AfterUnion: true
AfterExternBlock: false
BeforeCatch: true
BeforeElse: true
BeforeLambdaBody: true
BeforeWhile: true
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
BreakInheritanceList: BeforeColon
BreakStringLiterals: true
ColumnLimit: 100
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
FixNamespaceComments: true
IncludeBlocks: Regroup
IncludeCategories:
- Regex: '^.*Private[.]h"'
Priority: 0
- Regex: '^".*[.]pb[.]h"'
Priority: 2
- Regex: '^".*[.]h"'
Priority: 1
- Regex: '^<boost/.*>'
Priority: 5
- Regex: '^<.*([.]h|[.]hpp)>'
Priority: 3
- Regex: '^<queue>'
Priority: 6
- Regex: '^<Q.*>'
Priority: 4
- Regex: '^<.*>'
Priority: 6
IndentCaseBlocks: false
IndentCaseLabels: false
IndentExternBlock: NoIndent
IndentGotoLabels: false
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MaxEmptyLinesToKeep: 2
NamespaceIndentation: None
PenaltyExcessCharacter: 10
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInCStyleCastParentheses: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: c++14
StatementMacros: ['Q_UNUSED']
TabWidth: 4
UseCRLF: false
UseTab: ForIndentation
...

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
cmake-build-*
.idea

9
CMakeLists.txt Normal file
View file

@ -0,0 +1,9 @@
cmake_minimum_required(VERSION 3.1)
project(screen-resolution-changer CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
add_executable(screen-resolution-changer WIN32 main.cpp)

115
main.cpp Normal file
View file

@ -0,0 +1,115 @@
#include <Windows.h>
#include <optional>
#include <string>
std::optional<DEVMODE> setScreenResolution(int width, int height)
{
DEVMODE prevMode;
DEVMODE devMode;
ZeroMemory(&devMode, sizeof(DEVMODE));
devMode.dmSize = sizeof(DEVMODE);
if (!EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &devMode))
return std::nullopt;
CopyMemory(&prevMode, &devMode, sizeof(DEVMODE));
devMode.dmPelsWidth = width;
devMode.dmPelsHeight = height;
if (ChangeDisplaySettingsEx(NULL, &devMode, NULL, 0, NULL) == DISP_CHANGE_SUCCESSFUL)
return prevMode;
else
return std::nullopt;
}
bool restoreScreenResolution(DEVMODE& mode)
{
return ChangeDisplaySettingsEx(NULL, &mode, NULL, 0, NULL) == DISP_CHANGE_SUCCESSFUL;
}
bool executeProgram(std::string command)
{
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
if (CreateProcess(NULL, command.data(), NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
{
WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return true;
}
else
return false;
}
void showError(const char* message)
{
MessageBox(NULL, message, "Error", MB_OK);
}
void showUsage()
{
showError(
"Usage:\n"
"screen-resolution-changer.exe <width> <height> <executable> [args...]");
}
int main(int argc, char* argv[])
{
if (argc < 4)
{
showUsage();
return 1;
}
unsigned int width = 0;
unsigned int height = 0;
try
{
width = std::stoi(argv[1]);
height = std::stoi(argv[2]);
}
catch (const std::exception& e)
{
showUsage();
return 1;
}
std::string command = argv[3];
for (int i = 4; i < argc; i++)
command.append(" ").append(argv[i]);
auto prevMode = setScreenResolution(width, height);
if (prevMode)
{
int result = 1;
if (executeProgram(command))
result = 0;
else
{
result = 1;
showError("Failed to start application");
}
if (restoreScreenResolution(*prevMode))
return result;
else
{
showError("Failed to restore screen resolution");
return 1;
}
}
else
{
showError("Failed to change screen resolution");
return 1;
}
}