Skip to content

Commit

Permalink
Change the hdr enable/disable keywords to improve compatibility with …
Browse files Browse the repository at this point in the history
…Sunshine environment variables
  • Loading branch information
Andre Bocchini committed Apr 5, 2024
1 parent 9e30467 commit 42997af
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 62 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ ResolutionSwitcher --width 1920 --height 1080 --refresh 60 --monitor \\.\DISPLAY
Enable HDR on device with identifier `\\.\DISPLAY2`

```shell
ResolutionSwitcher --hdr enable --monitor \\.\DISPLAY2
ResolutionSwitcher --hdr true --monitor \\.\DISPLAY2
```

Disable HDR on the primary device

```shell
ResolutionSwitcher --hdr disable
ResolutionSwitcher --hdr false
```

Display available help information
Expand All @@ -56,22 +56,13 @@ These examples assume the application is installed at `C:\Program Files\Resoluti
### Do Commands

```shell
cmd /C "C:\Program Files\ResolutionSwitcher\ResolutionSwitcher.exe" --width %SUNSHINE_CLIENT_WIDTH% --height %SUNSHINE_CLIENT_HEIGHT% --refresh %SUNSHINE_CLIENT_FPS%
```


```shell
cmd /C "C:\Program Files\ResolutionSwitcher\ResolutionSwitcher.exe" --hdr disable
cmd /C "C:\Program Files\ResolutionSwitcher\ResolutionSwitcher.exe" --width %SUNSHINE_CLIENT_WIDTH% --height %SUNSHINE_CLIENT_HEIGHT% --refresh %SUNSHINE_CLIENT_FPS% --hdr %SUNSHINE_CLIENT_HDR%
```

### Undo Commands

```shell
cmd /C "C:\Program Files\ResolutionSwitcher\ResolutionSwitcher.exe" --width 3840 --height 2160 --refresh 144
```

```shell
cmd /C "C:\Program Files\ResolutionSwitcher\ResolutionSwitcher.exe" --hdr enable
cmd /C "C:\Program Files\ResolutionSwitcher\ResolutionSwitcher.exe" --width 3840 --height 2160 --refresh 144 --hdr false
```

## Building
Expand Down
90 changes: 41 additions & 49 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from sys import exit
from sys import (
stdout,
stderr,
exit
)
from argparse import ArgumentParser
from termcolor import (
colored,
Expand All @@ -17,7 +21,7 @@
)

# Application metadata
VERSION: str = "v3.0.0"
VERSION: str = "v3.0.1"
NAME: str = "ResolutionSwitcher"


Expand Down Expand Up @@ -56,7 +60,7 @@ def argument_parser() -> ArgumentParser:
prog=NAME,
description='Command line tool to change Windows display settings',
usage=f'{NAME} --version | --monitors | --monitor <ID> | --width <width> --height <height> --refresh '
f'<refresh> | hdr <enable/disable>'
f'<refresh> | hdr <true/false>'
)

version_group = p.add_argument_group()
Expand All @@ -77,9 +81,10 @@ def argument_parser() -> ArgumentParser:
return p


def print_message(message: str, color: str = None, attrs: list[str] = None, end: str = None):
def print_message(message: str, color: str = None, attrs: list[str] = None, end: str = None, is_error: bool = False):
file = stderr if is_error else stdout
colored_message = colored(message, color, attrs=attrs)
cprint(colored_message, color, attrs=attrs, end=end)
cprint(colored_message, color, attrs=attrs, end=end, file=file)


def print_success(message: str):
Expand All @@ -102,31 +107,48 @@ def print_error(error: str):
exit(-1)

if args.width or args.height or args.refresh:
if args.width is None:
print_error("Width is required")
exit(-1)
should_change_resolution: bool = args.width is not None and args.height is not None and args.refresh is not None
should_change_hdr: bool = args.hdr is not None

if args.height is None:
print_error("Height is required")
exit(-1)
if not should_change_resolution:
print_error("Width, height and refresh rate are required")

if not should_change_hdr:
exit(-1)

if args.refresh is None:
print_error("Refresh rate is required")
exit(-1)
if should_change_hdr:
if args.hdr.lower() not in ['true', 'false']:
print_error("Valid values for HDR are 'true' or 'false'")
exit(-1)

try:
identifier: str = args.monitor

if identifier is None:
print_message("Monitor ID not specified")
print_message("Will attempt to change settings for primary monitor")
print_message("Will attempt to identify primary monitor")
identifier = get_primary_monitor(all_monitors).identifier()

display_mode: DisplayMode = DisplayMode(args.width, args.height, args.refresh)
if should_change_resolution:
display_mode: DisplayMode = DisplayMode(args.width, args.height, args.refresh)

print_message(f'Attempting to change {identifier} settings to {str(display_mode)}')
set_display_mode_for_device(display_mode, identifier)
print_success("Display settings changed successfully")

print_message(f'Attempting to change {identifier} settings to {str(display_mode)}')
set_display_mode_for_device(display_mode, identifier)
print_success("Display settings changed successfully")
if should_change_hdr:
for target_monitor in all_monitors:
if target_monitor.adapter.identifier == identifier:
if not target_monitor.is_hdr_supported():
print_success(f"{target_monitor.adapter.identifier} does not support HDR")
print_success("Exiting...")
exit(-1)

hdr_state = True if args.hdr.lower() == 'true' else False

print_message(f'Attempting to {"enable" if hdr_state else "disable"} HDR on {identifier}')
set_hdr_state_for_monitor(hdr_state, target_monitor)
print_success(f"HDR {'enabled' if hdr_state else 'disabled'} successfully")

except DisplayAdapterException as e:
print_error(str(e))
Expand All @@ -136,40 +158,10 @@ def print_error(error: str):
print_error(str(e))
exit(-1)

elif args.hdr is not None:
try:
identifier: str = args.monitor

if identifier is None:
print_message("Monitor ID not specified")
print_message("Will attempt to change settings for primary monitor")
identifier = get_primary_monitor(all_monitors).identifier()

if args.hdr.lower() not in ['enable', 'disable']:
print_error("Valid values for HDR are 'enable' or 'disable'")
exit(-1)

for target_monitor in all_monitors:
if target_monitor.adapter.identifier == identifier:
if not target_monitor.is_hdr_supported():
print_success(f"{target_monitor.adapter.identifier} does not support HDR")
print_success("Exiting...")
exit(0)

hdr_state = True if args.hdr.lower() == 'enable' else False

print_message(f'Attempting to {"enable" if hdr_state else "disable"} HDR on {identifier}')
set_hdr_state_for_monitor(hdr_state, target_monitor)
print_success(f"HDR {'enabled' if hdr_state else 'disabled'} successfully")

except HdrException as e:
print_error(f"Error when trying to change HDR state. Failed with error {str(e)}")
exit(-1)

except PrimaryMonitorException as e:
print_error(str(e))
exit(-1)

elif args.monitor is not None:
identifier: str = args.monitor

Expand Down

0 comments on commit 42997af

Please sign in to comment.