-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
297 lines (233 loc) · 8.5 KB
/
Copy pathmain.py
File metadata and controls
297 lines (233 loc) · 8.5 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import ctypes
from collections import deque
from math import sqrt
import time
import threading
from ctypes import wintypes
import pystray
from pystray import MenuItem as item
from PIL import Image, ImageDraw
from winsdk.windows.devices.sensors import LightSensor
PHYSICAL_MONITOR_DESCRIPTION_SIZE = 128
VCP_BRIGHTNESS_CODE = 0x10
class PHYSICAL_MONITOR(ctypes.Structure):
_fields_ = [
("hPhysicalMonitor", wintypes.HANDLE),
("szPhysicalMonitorDescription", wintypes.WCHAR * PHYSICAL_MONITOR_DESCRIPTION_SIZE),
]
MonitorEnumProc = ctypes.WINFUNCTYPE(
wintypes.BOOL,
wintypes.HMONITOR,
wintypes.HDC,
ctypes.POINTER(wintypes.RECT),
wintypes.LPARAM,
)
user32 = ctypes.WinDLL("user32", use_last_error=True)
dxva2 = ctypes.WinDLL("dxva2", use_last_error=True)
offset = 0
running = True
last_lux = 0.0
lock = threading.Lock()
LOOP_INTERVAL_SEC = 1
AVERAGE_WINDOW_SEC = 60
AVERAGE_HISTORY_SIZE = AVERAGE_WINDOW_SEC // LOOP_INTERVAL_SEC
def lux_to_brightness(lux):
# this needs to be adjusted based on monitor max brightness and user preference
if lux < 1:
return 3
if lux < 600:
return int(sqrt(lux / 600) * 100)
return 100
def _raise_last_error(prefix: str) -> None:
code = ctypes.get_last_error()
raise OSError(f"{prefix} failed (WinError {code})")
def enum_physical_monitors() -> list[PHYSICAL_MONITOR]:
hmonitors = []
@MonitorEnumProc
def _callback(hmonitor, _hdc, _lprc, _lparam):
hmonitors.append(hmonitor)
return True
if not user32.EnumDisplayMonitors(None, None, _callback, 0):
_raise_last_error("EnumDisplayMonitors")
result: list[PHYSICAL_MONITOR] = []
for hmonitor in hmonitors:
count = wintypes.DWORD()
if not dxva2.GetNumberOfPhysicalMonitorsFromHMONITOR(hmonitor, ctypes.byref(count)):
continue
if count.value == 0:
continue
arr = (PHYSICAL_MONITOR * count.value)()
if not dxva2.GetPhysicalMonitorsFromHMONITOR(hmonitor, count, arr):
continue
result.extend(arr)
return result
def set_brightness_percent(handle: wintypes.HANDLE, percent: int) -> None:
percent = max(0, min(100, percent))
# Prefer direct VCP write; many monitors accept this even when read-back fails.
if dxva2.SetVCPFeature(handle, wintypes.BYTE(VCP_BRIGHTNESS_CODE), wintypes.DWORD(percent)):
return
# Fallback path for monitors that only support high-level brightness API.
min_b = wintypes.DWORD()
cur_b = wintypes.DWORD()
max_b = wintypes.DWORD()
if not dxva2.GetMonitorBrightness(handle, ctypes.byref(min_b), ctypes.byref(cur_b), ctypes.byref(max_b)):
_raise_last_error("SetVCPFeature")
target = int(round(min_b.value + ((max_b.value - min_b.value) * (percent / 100.0))))
if not dxva2.SetMonitorBrightness(handle, wintypes.DWORD(target)):
_raise_last_error("SetMonitorBrightness")
def get_brightness_percent(handle: wintypes.HANDLE) -> int:
# Prefer VCP read (works on many external monitors).
code_type = wintypes.DWORD()
cur_v = wintypes.DWORD()
max_v = wintypes.DWORD()
if dxva2.GetVCPFeatureAndVCPFeatureReply(
handle,
wintypes.BYTE(VCP_BRIGHTNESS_CODE),
ctypes.byref(code_type),
ctypes.byref(cur_v),
ctypes.byref(max_v),
):
if max_v.value > 0:
return int(round((cur_v.value / max_v.value) * 100))
# Fallback to high-level brightness API.
min_b = wintypes.DWORD()
cur_b = wintypes.DWORD()
max_b = wintypes.DWORD()
if not dxva2.GetMonitorBrightness(handle, ctypes.byref(min_b), ctypes.byref(cur_b), ctypes.byref(max_b)):
_raise_last_error("GetMonitorBrightness")
span = max_b.value - min_b.value
if span <= 0:
return 0
return int(round(((cur_b.value - min_b.value) / span) * 100))
def brighter(icon, item):
global offset
offset += 10
def darker(icon, item):
global offset
offset -= 10
def reset(icon, item):
global offset
offset = 0
def quit_app(icon, item):
global running
running = False
icon.stop()
def on_reading_changed(sensor, args):
global last_lux
reading = args.reading
if reading:
with lock:
last_lux = reading.illuminance_in_lux
def make_icon_image(brightness: int) -> Image.Image:
value = max(0, min(99, int(round(brightness))))
text = f"{value:02d}"
img = Image.new("RGB", (128, 128), color=(18, 18, 18))
draw = ImageDraw.Draw(img)
on = (245, 245, 245)
off = (48, 48, 48)
digit_map = {
"0": {"a", "b", "c", "d", "e", "f"},
"1": {"b", "c"},
"2": {"a", "b", "g", "e", "d"},
"3": {"a", "b", "g", "c", "d"},
"4": {"f", "g", "b", "c"},
"5": {"a", "f", "g", "c", "d"},
"6": {"a", "f", "g", "e", "c", "d"},
"7": {"a", "b", "c"},
"8": {"a", "b", "c", "d", "e", "f", "g"},
"9": {"a", "b", "c", "d", "f", "g"},
}
def draw_digit(x: int, y: int, w: int, h: int, ch: str) -> None:
t = max(6, w // 7) # segment thickness
m = t // 2
y_mid = y + (h // 2)
segments = {
"a": (x + m, y, x + w - m, y + t),
"d": (x + m, y + h - t, x + w - m, y + h),
"g": (x + m, y_mid - (t // 2), x + w - m, y_mid + (t // 2)),
"f": (x, y + m, x + t, y_mid - m),
"e": (x, y_mid + m, x + t, y + h - m),
"b": (x + w - t, y + m, x + w, y_mid - m),
"c": (x + w - t, y_mid + m, x + w, y + h - m),
}
enabled = digit_map.get(ch, set())
for seg, rect in segments.items():
color = on if seg in enabled else off
draw.rounded_rectangle(rect, radius=t // 3, fill=color)
digit_w = 52
digit_h = 100
gap = 8
total_w = (digit_w * 2) + gap
start_x = (128 - total_w) // 2
start_y = (128 - digit_h) // 2
draw_digit(start_x, start_y, digit_w, digit_h, text[0])
draw_digit(start_x + digit_w + gap, start_y, digit_w, digit_h, text[1])
return img
sensor = LightSensor.get_default()
if sensor is None:
raise SystemExit("No light sensor found")
sensor.report_interval = max(sensor.minimum_report_interval, 250)
sensor.add_reading_changed(on_reading_changed)
monitors = enum_physical_monitors()
if not monitors:
raise SystemExit("No DDC/CI physical monitors found")
print(f"Found {len(monitors)} DDC/CI monitor(s)")
startup_brightness = 50
for mon in monitors:
try:
startup_brightness = max(0, min(100, get_brightness_percent(mon.hPhysicalMonitor)))
break
except Exception:
continue
icon_image = make_icon_image(startup_brightness)
menu = pystray.Menu(
item('Brighter (+10)', brighter),
item('Darker (-10)', darker),
item('Reset offset', reset),
item('Quit', quit_app)
)
icon = pystray.Icon("LuxControl", icon_image, menu=menu)
icon.title = f"LuxControl: {startup_brightness}%"
# Run tray icon in background thread
threading.Thread(target=icon.run, daemon=True).start()
last_icon_brightness = startup_brightness
last_applied_brightness = startup_brightness
target_history: deque[int] = deque([startup_brightness] * AVERAGE_HISTORY_SIZE, maxlen=AVERAGE_HISTORY_SIZE)
try:
while running:
with lock:
lux = last_lux
target_auto = lux_to_brightness(lux)
target_history.append(target_auto)
avg_auto = int(round(sum(target_history) / len(target_history)))
brightness = avg_auto + offset
brightness = max(0, min(100, brightness)) # clamp to valid range
if brightness != last_applied_brightness:
for mon in monitors:
try:
set_brightness_percent(mon.hPhysicalMonitor, brightness)
except Exception as exc:
print(f"Brightness set failed on monitor '{mon.szPhysicalMonitorDescription}': {exc}")
last_applied_brightness = brightness
if brightness != last_icon_brightness:
icon.icon = make_icon_image(brightness)
icon.title = f"LuxControl: {brightness}%"
last_icon_brightness = brightness
print(
"Lux:",
f"{lux:.2f}",
"-> Target:",
target_auto,
"Avg:",
avg_auto,
"Out:",
brightness,
"(offset:",
offset,
")",
)
time.sleep(LOOP_INTERVAL_SEC)
finally:
count = wintypes.DWORD(len(monitors))
arr = (PHYSICAL_MONITOR * len(monitors))(*monitors)
dxva2.DestroyPhysicalMonitors(count, arr)