This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
142 lines (122 loc) · 6.59 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# Multi Brightness Control
# Set brightness values for all monitors
# Created by Segilmez06 - sarpegilmez.com
import sys
import os
import time
import screen_brightness_control as sbc
from infi.systray import SysTrayIcon
# Print welcome message
if "-s" not in sys.argv[1:]:
print("Multi Brightness Control")
# Print help message
if "-h" in sys.argv[1:] or "--help" in sys.argv[1:]:
print("Usage: " + sys.argv[0] + " [options]")
print("")
print("Options:")
print("\t-c <id>\t\t\t\tPrint current brightness of display.")
print("\t-b <value> <id>\t\t\tSet brightness of display.")
print("\t-g \t\t\t\tGet available displays.")
print("\t-s \t\t\t\tDo not print any debug message. Does not affect output of -c argument.")
print("")
print("\t--check-packages\t\tCheck if required packages are installed")
print("\t--skip-count-validation\t\tSkip checking of monitor count")
print("\t--no-extract-icon\t\tDo not extract system tray icon to file")
print("\t--no-systray-icon\t\tDo not add icon to system tray")
print("")
print("\t-h, --help\t\t\tShow this help")
print("\t-v, --version\t\t\tShow version")
sys.exit(0)
# Print version strings
if "-v" in sys.argv[1:] or "--version" in sys.argv[1:]:
print("Version 1.0")
print("Running on Python " + sys.version)
sys.exit(0)
# Check for packages
if "--check-packages" in sys.argv[1:]:
import pkg_resources
import subprocess
if "-s" not in sys.argv[1:]:
print("Checking if need to install packages...")
to_install = {'infi.systray', 'screen_brightness_control'} - {pkg.key for pkg in pkg_resources.working_set}
if to_install:
if "-s" not in sys.argv[1:]:
print(f"{len(to_install)} packages is not installed.")
for pkg in to_install:
if "-s" not in sys.argv[1:]:
print("Installing " + pkg)
subprocess.check_call([sys.executable, "-m", "pip", "install", pkg])
# Print available displays
if "-g" in sys.argv[1:]:
for monitor in sbc.list_monitors():
print(monitor)
sys.exit(0)
# Print current brightness of display with id
elif "-c" in sys.argv[1:]:
pos = sys.argv.index("-c")
display_id = int(sys.argv[pos + 1])
print(sbc.get_brightness(display=display_id))
sys.exit(0)
# Set current brightness of display with id
elif "-b" in sys.argv[1:]:
pos = sys.argv.index("-b")
value = int(sys.argv[pos + 1])
display_id = int(sys.argv[pos + 2])
sbc.set_brightness(value, display=display_id)
sys.exit(0)
def terminate(t):
os.kill(os.getpid(), 9) # Terminate function
def func_no():
pass # Empty function
# Check available display count
if "--skip-count-validation" not in sys.argv[1:]: # Check for available monitor count
wait = 0
monitor_count = len(sbc.list_monitors())
while monitor_count < 0: # Wait for other monitors to be connected
if "-s" not in sys.argv[1:]:
print(f"Attempt {wait}: No external monitors found.")
time.sleep(1)
if wait < 30: # Wait until 30 failed attempts
monitor_count = len(sbc.list_monitors())
wait += 1
else:
if "-s" not in sys.argv[1:]:
print(f"Attempt {wait}: Still no monitors connected. Terminating...")
terminate(None) # No reason to waste system resources if there is no external monitor connected
# Print display count
if "-s" not in sys.argv[1:]:
print(f"{len(sbc.list_monitors())} monitors found.")
# Set current brightness as last value
last = sbc.get_brightness(display=0)
# Print current brightness
if "-s" not in sys.argv[1:]:
print("Primary display brightness is " + str(last) + "%.")
# Extract icon to file
if "--no-extract-icon" not in sys.argv[1:]:
if "-s" not in sys.argv[1:]:
print("Extracting icon to file...")
img = bytearray(b"\x00\x00\x01\x00\x01\x00\x10\x10\x00\x00\x01\x00 \x00\xb2\x01\x00\x00\x16\x00\x00\x00\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x10\x00\x00\x00\x10\x08\x03\x00\x00\x00(-\x0fS\x00\x00\x00\x01sRGB\x01\xd9\xc9,\x7f\x00\x00\x00\tpHYs\x00\x00\x0eM\x00\x00\x0eM\x01F\xd5h\t\x00\x00\x00\x8aPLTE\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xf67h\x8d\x00\x00\x00.tRNS\x00\x8fa\x9dk\xa8C\\\x93\xedxW\xec0i\xdf\xff\xd7\xfb\xf9D\xd0\xb9]\xfc\xfar\x88>\xf4\xeeL\xb3\x907\xe6\xd9)[&\x89\xd3\xcf}q~\x0f\x08\x003\x00\x00\x00\x87IDATx\x9cm\x8f\xd9\x12\x820\x0cEc\x13[T\xd0\xaa\xb8\xa2\x82\x0b\xa0\xa8\xff\xff{\x86\xa6\xad\xe3\x8c\xe7)\xf7L\'\xb9\x05\x10\x06\n~A\xfa\xceC\x1d\x84I\x9c\xd0#6\xe3\t\xe74\x03o\xa63k\xe71\x03\xa8\x85e\x96y\xdc\xb2\xb2\x8e\xb5\xdcC\xdclE\xec\xf6\x88\x05(\xa2\xc3Q\xc4\xa9$2\xeeU%\xe2\x1cw\\\xae}\xbe\xd5!7i{\x7ft\xcf\xb6\xef#\x99\xef\xbfJ\xdf\x90I\xb2P]\xbf\xff\x7f\xceQ\x18?|\x00\x8e\x17\x07\x8d\xd3\xff\x95\x90\x00\x00\x00\x00IEND\xaeB`\x82")
with open("icon.ico", "wb") as image:
image.write(img)
# Add app icon to system tray
if "--no-systray-icon" not in sys.argv[1:]:
menu_options = (("Detected monitors: " + str(monitor_count), None, func_no),) # Add connected monitor count as menu item
if "-s" not in sys.argv[1:]:
print("Starting system tray icon...")
systray = SysTrayIcon("icon.ico", "Dual Brightness Control", menu_options, on_quit=terminate) # Initialize system tray icon
systray.start()
tooltip = "Active: " + str(monitor_count) + "\nBrightness: " + str(last) + '%' # Set tooltip to current value
systray.update(hover_text=tooltip)
# Main loop for adjust in real time
if "-s" not in sys.argv[1:]:
print("Starting main loop...")
while True:
current = sbc.get_brightness(display=0) # Get current brightness of the first monitor
if current != last: # Compare with last value
last = current # Set new value as last value
for i in range(1, monitor_count):
sbc.set_brightness(current, display=i) # Apply new value to all external monitors
if "--no-systray-icon" not in sys.argv[1:]:
tooltip = "Active: " + str(monitor_count) + "\nBrightness: " + str(current) + '%' # Set tooltip to current value
systray.update(hover_text=tooltip)
time.sleep(0.1) # Wait for 100ms to avoid flickering