Skip to content

Commit

Permalink
add option to alter all monitors at once
Browse files Browse the repository at this point in the history
  • Loading branch information
lucivpav committed Apr 6, 2024
1 parent 30aeeb8 commit 5df7620
Show file tree
Hide file tree
Showing 11 changed files with 207 additions and 119 deletions.
2 changes: 1 addition & 1 deletion Average.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x64\Release\Monitor.exe /set 1 55
x64\Release\Monitor.exe /set 55
2 changes: 1 addition & 1 deletion Bright.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x64\Release\Monitor.exe /set 1 80
x64\Release\Monitor.exe /set 80
2 changes: 1 addition & 1 deletion Brighter.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x64\Release\Monitor.exe /brighter 1
x64\Release\Monitor.exe /brighter
2 changes: 1 addition & 1 deletion Dark.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x64\Release\Monitor.exe /set 1 20
x64\Release\Monitor.exe /set 20
2 changes: 1 addition & 1 deletion Darker.bat
Original file line number Diff line number Diff line change
@@ -1 +1 @@
x64\Release\Monitor.exe /darker 1
x64\Release\Monitor.exe /darker
38 changes: 0 additions & 38 deletions Monitor/Monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,42 +121,4 @@ void Monitor::setCurrentBrightnessFraction(double fraction)
if (mMinimumBrightness >= mMaximumBrightness)
return;
setCurrentBrightness((mMaximumBrightness - mMinimumBrightness) * fraction + mMinimumBrightness);
}


BOOL CALLBACK MonitorEnumCallback(_In_ HMONITOR hMonitor, _In_ HDC hdcMonitor, _In_ LPRECT lprcMonitor, _In_ LPARAM dwData)
{
std::vector<Monitor>* monitors = reinterpret_cast<std::vector<Monitor>*>(dwData);

// Get the number of physical monitors.
DWORD cPhysicalMonitors;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);

LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
if (bSuccess)
{
// Allocate the array of PHYSICAL_MONITOR structures.
LPPHYSICAL_MONITOR pPhysicalMonitors = new PHYSICAL_MONITOR[cPhysicalMonitors];

if (pPhysicalMonitors != NULL)
{
// Get the array.
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);

// Use the monitor handles.
for (unsigned int i = 0; i < cPhysicalMonitors; ++i)
{
monitors->push_back(Monitor(pPhysicalMonitors[i]));
}
}
}
// Return true to continue enumeration.
return TRUE;
}

std::vector<Monitor> EnumerateMonitors()
{
std::vector<Monitor> monitors;
EnumDisplayMonitors(NULL, NULL, MonitorEnumCallback, reinterpret_cast<LPARAM>(&monitors));
return monitors;
}
6 changes: 1 addition & 5 deletions Monitor/Monitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include <physicalmonitorenumerationapi.h>
#include <highlevelmonitorconfigurationapi.h>

#include <vector>

class Monitor
{
public:
Expand Down Expand Up @@ -39,6 +37,4 @@ class Monitor
int mCurrentContrast = 0;

PHYSICAL_MONITOR mPhysicalMonitor;
};

std::vector<Monitor> EnumerateMonitors();
};
140 changes: 140 additions & 0 deletions Monitor/MonitorManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#include "MonitorManager.h"
#include <iostream>

MonitorManager::MonitorManager()
{
monitors = EnumerateMonitors();
}

std::vector<Monitor> MonitorManager::EnumerateMonitors()
{
std::vector<Monitor> monitors;
EnumDisplayMonitors(NULL, NULL, MonitorEnumCallback, reinterpret_cast<LPARAM>(&monitors));
return monitors;
}

BOOL CALLBACK MonitorManager::MonitorEnumCallback(_In_ HMONITOR hMonitor, _In_ HDC hdcMonitor, _In_ LPRECT lprcMonitor, _In_ LPARAM dwData)
{
std::vector<Monitor>* monitors = reinterpret_cast<std::vector<Monitor>*>(dwData);

// Get the number of physical monitors.
DWORD cPhysicalMonitors;
BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMonitor, &cPhysicalMonitors);

LPPHYSICAL_MONITOR pPhysicalMonitors = NULL;
if (bSuccess)
{
// Allocate the array of PHYSICAL_MONITOR structures.
LPPHYSICAL_MONITOR pPhysicalMonitors = new PHYSICAL_MONITOR[cPhysicalMonitors];

if (pPhysicalMonitors != NULL)
{
// Get the array.
bSuccess = GetPhysicalMonitorsFromHMONITOR(hMonitor, cPhysicalMonitors, pPhysicalMonitors);

// Use the monitor handles.
for (unsigned int i = 0; i < cPhysicalMonitors; ++i)
{
monitors->push_back(Monitor(pPhysicalMonitors[i]));
}
}
}
// Return true to continue enumeration.
return TRUE;
}

void MonitorManager::Show() {
if (monitors.empty()) {
std::cout << "No monitors found." << std::endl;
return;
}
for (int i = 0; i < monitors.size(); i++) {
if (i != 0) std::cout << std::endl;
std::cout << "Monitor " << i << ": " << std::endl;
std::cout << "Current brightness: " << monitors[i].currentBrightness() << std::endl;
std::cout << "Current contrast: " << monitors[i].currentContrast() << std::endl;
}
}

bool MonitorManager::Set(int monitorId, int intensity) {
if (monitorId >= monitors.size() || monitorId < 0) {
std::cerr << "Monitor with id " << monitorId << " not found" << std::endl;
return false;
}
bool result = true;
if (!monitors[monitorId].setCurrentBrightness(intensity)) {
std::cerr << "Error setting brightness for monitor " << monitorId << std::endl;
result = false;
}
if (!monitors[monitorId].setCurrentContrast(intensity)) {
std::cerr << "Error setting contrast for monitor " << monitorId << std::endl;
result = false;
}
return result;
}

bool MonitorManager::BrighterOrDarker(int monitorId, bool brighter) {
if (monitors.empty()) {
std::cout << "No monitors found." << std::endl;
return false;
}
if (monitorId >= monitors.size() || monitorId < 0) {
std::cerr << "Monitor with id " << monitorId << " not found" << std::endl;
return false;
}
const int currentIntensity = monitors[monitorId].currentBrightness();
const int newIntensity = brighter ? min(currentIntensity + 10, 100) : max(currentIntensity - 10, 0);

bool result = true;
if (!monitors[monitorId].setCurrentBrightness(newIntensity)) {
std::cerr << "Error setting brightness for monitor " << monitorId << std::endl;
result = false;
}

if (!monitors[monitorId].setCurrentContrast(newIntensity)) {
std::cerr << "Error setting contrast for monitor " << monitorId << std::endl;
result = false;
}
return result;
}

bool MonitorManager::SetAll(int intensity)
{
if (monitors.empty()) {
std::cout << "No monitors found." << std::endl;
return true;
}
bool result = true;
for (int i = 0; i < monitors.size(); i++) {
result &= Set(i, intensity);
}
return result;
}

bool MonitorManager::Brighter(int monitorId) {
return BrighterOrDarker(monitorId, true);
}

bool MonitorManager::Darker(int monitorId) {
return BrighterOrDarker(monitorId, false);
}

bool MonitorManager::BrighterAll() {
return BrighterOrDarkerAll(true);
}

bool MonitorManager::DarkerAll() {
return BrighterOrDarkerAll(false);
}

bool MonitorManager::BrighterOrDarkerAll(bool brighter) {
if (monitors.empty()) {
std::cout << "No monitors found." << std::endl;
return true;
}
bool result = true;
for (int i = 0; i < monitors.size(); i++) {
result &= BrighterOrDarker(i, brighter);
}
return result;
}
26 changes: 26 additions & 0 deletions Monitor/MonitorManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <vector>

#include "Monitor.h"

class MonitorManager {
public:
MonitorManager();
void Show();

bool Set(int monitorId, int intensity);
bool Brighter(int monitorId);
bool Darker(int monitorId);

bool SetAll(int intensity);
bool BrighterAll();
bool DarkerAll();
private:
std::vector<Monitor> EnumerateMonitors();
BOOL static MonitorEnumCallback(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);
bool BrighterOrDarker(int monitorId, bool brighter);
bool BrighterOrDarkerAll(bool brighter);

std::vector<Monitor> monitors;
};
101 changes: 30 additions & 71 deletions Monitor/main.cpp
Original file line number Diff line number Diff line change
@@ -1,104 +1,63 @@
#include <iostream>
#include "Monitor.h"
#include "MonitorManager.h"

void Usage() {
std::cout << "Monitor.exe -- prints the usage information" << std::endl;
std::cout << "Monitor.exe /show -- prints current brightness and contrast for every monitor detected" << std::endl;
std::cout << std::endl;
std::cout << "Monitor.exe /set 1 60 -- sets both brightness and contrast to 60% of monitor with id 1" << std::endl;
std::cout << "Monitor.exe /brighter 1 -- increases the brightness and contrast to +10% of monitor with id 1" << std::endl;
std::cout << "Monitor.exe /darker 1 -- decreases the brightness and contrast to -10% of monitor with id 1" << std::endl;
std::cout << std::endl;
std::cout << "Monitor.exe /set 60 -- sets both brightness and contrast to 60% of all monitors" << std::endl;
std::cout << "Monitor.exe /brighter -- increases the brightness and contrast to +10% of all monitors" << std::endl;
std::cout << "Monitor.exe /darker -- decreases the brightness and contrast to -10% of all monitors" << std::endl;
}

void Show() {
auto monitors = EnumerateMonitors();
if (monitors.empty()) {
std::cout << "No monitors found." << std::endl;
return;
}
for (int i = 0; i < monitors.size(); i++) {
if (i != 0) std::cout << std::endl;
std::cout << "Monitor " << i << ": " << std::endl;
std::cout << "Current brightness: " << monitors[i].currentBrightness() << std::endl;
std::cout << "Current contrast: " << monitors[i].currentContrast() << std::endl;
}
}

bool Set(int monitorId, int intensity) {
auto monitors = EnumerateMonitors();
if (monitorId >= monitors.size() || monitorId < 0) {
std::cerr << "Monitor with id " << monitorId << " not found" << std::endl;
return false;
}
monitors[monitorId].setCurrentBrightness(intensity);
monitors[monitorId].setCurrentContrast(intensity);
return true;
}

bool BrighterOrDarker(int monitorId, bool brighter) {
auto monitors = EnumerateMonitors();
if (monitors.empty()) {
std::cout << "No monitors found." << std::endl;
return false;
}
if (monitorId >= monitors.size() || monitorId < 0) {
std::cerr << "Monitor with id " << monitorId << " not found" << std::endl;
return false;
}
const int currentIntensity = monitors[monitorId].currentBrightness();
const int newIntensity = brighter ? min(currentIntensity + 10, 100) : max(currentIntensity - 10, 0);

bool result = true;
if (!monitors[monitorId].setCurrentBrightness(newIntensity)) {
std::cerr << "Error setting brightness for monitor " << monitorId << std::endl;
result = false;
}

if (!monitors[monitorId].setCurrentContrast(newIntensity)) {
std::cerr << "Error setting contrast for monitor " << monitorId << std::endl;
result = false;
}
return result;
}

bool Brighter(int monitorId) {
return BrighterOrDarker(monitorId, true);
}

bool Darker(int monitorId) {
return BrighterOrDarker(monitorId, false);
int MapOpResultToExitCode(bool opSuccess) {
return opSuccess ? 0 : 1;
}

int main(int argc, char ** argv) {
if (argc == 1) {
Usage();
return 1;
}

MonitorManager manager;

if (argc == 2 && std::string("/show") == std::string(argv[1])) {
Show();
manager.Show();
return 0;
}

if (argc == 4 && std::string("/set") == std::string(argv[1])) {
int monitorId = std::atoi(argv[2]);
int intensity = std::atoi(argv[3]);
return MapOpResultToExitCode(manager.Set(monitorId, intensity));
}

if (argc == 3 && std::string("/brighter") == std::string(argv[1])) {
int monitorId = std::atoi(argv[2]);
if (!Brighter(monitorId))
return 1;
return 0;
return MapOpResultToExitCode(manager.Brighter(monitorId));
}

if (argc == 3 && std::string("/darker") == std::string(argv[1])) {
int monitorId = std::atoi(argv[2]);
if (!Darker(monitorId))
return 1;
return 0;
return MapOpResultToExitCode(manager.Darker(monitorId));
}

if (argc == 4 && std::string("/set") == std::string(argv[1])) {
int monitorId = std::atoi(argv[2]);
int intensity = std::atoi(argv[3]);
if (!Set(monitorId, intensity))
return 1;
return 0;
if (argc == 3 && std::string("/set") == std::string(argv[1])) {
int intensity = std::atoi(argv[2]);
return MapOpResultToExitCode(manager.SetAll(intensity));
}

if (argc == 2 && std::string("/brighter") == std::string(argv[1])) {
return MapOpResultToExitCode(manager.BrighterAll());
}

if (argc == 2 && std::string("/darker") == std::string(argv[1])) {
return MapOpResultToExitCode(manager.DarkerAll());
}

Usage();
Expand Down
Loading

0 comments on commit 5df7620

Please sign in to comment.