-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitorPy.py
More file actions
908 lines (827 loc) · 40.1 KB
/
MonitorPy.py
File metadata and controls
908 lines (827 loc) · 40.1 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
#!/usr/bin/env python3
"""
MonitorPy - Monitor Control System Tray Application for Windows 11/10
Requires: pip install pystray pillow monitorcontrol pyinstaller
"""
import tkinter as tk
from tkinter import ttk, messagebox
import pystray
from PIL import Image, ImageDraw
import threading
import sys
import os
from pathlib import Path
import time
try:
from monitorcontrol import get_monitors
except ImportError:
print("Please install monitorcontrol: pip install monitorcontrol")
messagebox.showerror("Missing Dependency", "Please install monitorcontrol: pip install monitorcontrol")
sys.exit(1)
class MonitorController:
def __init__(self):
self.monitors = []
self.monitor = None
self.root = None
self.brightness_var = None
self.contrast_var = None
self.tray_icon = None
self.current_brightness = []
self.current_contrast = []
self.monitor_connected = False
self.selected_monitor_index = 0
self.preset_1 = {"brightness": 100, "contrast": 70}
self.preset_2 = {"brightness": 30, "contrast": 50}
self.discover_monitors()
def discover_monitors(self):
print("Discovering monitors...")
# messagebox.showinfo("Discovering Monitors", "Discovering monitors...")
self.monitors = get_monitors()
self.monitor_infos = []
win32_manufacturers = self._get_monitor_manufacturers_win32com()
for i, monitor in enumerate(self.monitors):
info = {"monitor": monitor, "name": f"Monitor {i+1}", "brightness": 50, "contrast": 50, "manufacturer": None}
try:
with monitor:
caps = monitor.get_vcp_capabilities()
if isinstance(caps, dict) and "model" in caps:
info["name"] = caps["model"]
info["brightness"] = monitor.get_luminance()
info["contrast"] = monitor.get_contrast()
except Exception:
info["name"] += " (No DDC/CI)"
# Assign manufacturer from Registry if available
# Only use manufacturers list up to the number of actual monitors
if i < len(win32_manufacturers) and win32_manufacturers[i]:
info["manufacturer"] = win32_manufacturers[i]
print(f"Monitor {i}: {info['name']} -> {info['manufacturer']}")
self.monitor_infos.append(info)
self.monitor = self.monitors[0] if self.monitors else None
self.monitor_connected = bool(self.monitors)
if self.monitor_infos:
self.current_brightness = self.monitor_infos[0]["brightness"]
self.current_contrast = self.monitor_infos[0]["contrast"]
def _get_monitor_manufacturers_win32com(self):
"""Get monitor manufacturers using EDID data from Windows Registry."""
manufacturers = []
try:
import winreg
print("Reading monitor EDID data from Registry...")
# Map of 3-letter manufacturer codes to full names
mfg_codes = {
'ACI': 'Asus', 'AOC': 'AOC', 'API': 'Acer', 'APP': 'Apple',
'AUO': 'AUOptronics', 'BNQ': 'BenQ', 'CMO': 'Chimei',
'CPQ': 'Compaq', 'DEL': 'Dell', 'DPC': 'Delta',
'DWD': 'Daewoo', 'EIZ': 'Eizo', 'ELS': 'Elsa',
'EMC': 'eMachines', 'ENC': 'Eizo', 'ENE': 'Eaton',
'EWD': 'Eizo', 'FUJ': 'Fujitsu', 'GSM': 'LG',
'HEC': 'Hectronic', 'HIT': 'Hitachi', 'HPN': 'HP',
'HSL': 'Hansol', 'HSJ': 'Hansol', 'HWP': 'HP', 'IBM': 'IBM',
'ICN': 'Incore', 'IDT': 'Idenitum', 'IFS': 'Infocus',
'ILK': 'Ilk', 'ITE': 'Itelvision', 'IVY': 'Iiyama',
'JVC': 'JVC', 'KFC': 'Kingfisher', 'KOE': 'Kaohsiung',
'KYE': 'Genius', 'LEN': 'Lenovo', 'LGD': 'LG Display',
'LHP': 'LG Philips', 'LPL': 'LG Philips', 'LSI': 'Lacie',
'MAG': 'Mag', 'MAX': 'Maxi', 'MEC': 'Mec',
'MEI': 'Panasonic', 'MIR': 'Miro', 'MIS': 'Mitsubishi',
'MON': 'Moniterm', 'MSI': 'MSI', 'MTO': 'Mitsubishi',
'NCI': 'NCR', 'NEC': 'NEC', 'NET': 'Netronix',
'NEW': 'Newvision', 'NLX': 'NLX', 'NOK': 'Nokia',
'NVD': 'Nvidia', 'NVI': 'Nvidia', 'OPT': 'Optoma',
'OQI': 'OPTIQUEST', 'ORN': 'Orion', 'OTC': 'Optec',
'OUI': 'Oui', 'PAC': 'Pac', 'PAR': 'Paragon',
'PBN': 'Philbin', 'PEG': 'Pegatron', 'PEL': 'Pelco',
'PHA': 'Photon', 'PHL': 'Philips', 'PHO': 'Photon',
'PKG': 'Procom', 'PLY': 'Polyester', 'PMM': 'Pamm',
'PRT': 'Proart', 'PSI': 'Protech', 'PTS': 'Protech',
'PUI': 'Princeton', 'PVI': 'Princeton', 'PVM': 'Panasonic',
'QDS': 'Quanta', 'REL': 'Relisys', 'REN': 'Renesas',
'REV': 'Revlon', 'RNT': 'Rentron', 'ROL': 'Rolex',
'RTK': 'Samsung', 'RTS': 'RTS', 'SAM': 'Samsung',
'SAT': 'Sanyo', 'SED': 'Sedar', 'SEI': 'Seiko',
'SEL': 'Seleco', 'SEM': 'Semantix', 'SEN': 'Sencore',
'SHP': 'Sharp', 'SIE': 'Siemens', 'SIG': 'Sigma',
'SKM': 'Skelm', 'SKY': 'Skyworth', 'SNI': 'Sansui',
'SNY': 'Sony', 'SOU': 'SOUNIQUE', 'SPT': 'Sceptre',
'SRC': 'Serica', 'STD': 'Std', 'STN': 'Satcom',
'STX': 'Streaming', 'SUN': 'Sun', 'SVO': 'Shinova',
'SYN': 'Synaptics', 'SYS': 'System', 'TAJ': 'Taj',
'TEC': 'Teconics', 'TEK': 'Tektronix', 'TEM': 'Tem',
'TEN': 'Tennics', 'TER': 'Terratec', 'TES': 'Tesis',
'TEX': 'Texaco', 'THE': 'Theben', 'THS': 'Thisware',
'THX': 'THX', 'TIC': 'Ticta', 'TIE': 'Tiernan',
'TIN': 'Tinnos', 'TIP': 'Tiptel', 'TIS': 'Tisys',
'TIV': 'Tiviewave', 'TOE': 'Toe', 'TOU': 'Touch',
'TRL': 'Tranquillity', 'TSB': 'Tsenso', 'TSN': 'Tseng',
'TST': 'Tensai', 'TWH': 'Taiwan', 'TYN': 'Tynamics',
'UBI': 'Ubisoft', 'UMC': 'Umcharacter', 'UMT': 'Unitech',
'UNI': 'Unipac', 'UPS': 'UPSHIFT', 'URD': 'Urdama',
'USA': 'USA', 'USB': 'USB', 'UTC': 'UTC',
'UTE': 'Utech', 'VAI': 'Vaio', 'VDT': 'Videoterm',
'VES': 'Vestel', 'VIT': 'Vitec', 'VIZ': 'Vizaudio',
'VLV': 'Velvision', 'VOB': 'VOBO', 'VOD': 'Vodacom',
'VON': 'Voni', 'VPC': 'VPC', 'VPI': 'Viper',
'VRN': 'Vernon', 'VUL': 'Vultron', 'WAY': 'Waytech',
'WDC': 'Western Digital', 'WEB': 'Webtronics', 'WEI': 'Wei',
'WET': 'Wetzel', 'WFD': 'Winfast', 'WFM': 'Winfirst',
'WHT': 'White', 'WIZ': 'Wizardry', 'WMC': 'Withc',
'WYS': 'Wyse', 'XAI': 'XAIRA', 'XER': 'Xerox',
'XIN': 'Xingbar', 'XON': 'Xonix', 'XOR': 'Xor',
'YAH': 'Yahoo', 'YAM': 'Yamaha', 'YES': 'Yesman',
'YMH': 'Yamaha', 'ZAP': 'Zaplight', 'ZEI': 'Zeitel',
'ZEN': 'Zenith', 'ZEO': 'Zeon', 'ZET': 'Zeta',
'ZGE': 'Zigote', 'ZTE': 'ZTE', 'ZXL': 'Zaxel',
}
reg_path = r"SYSTEM\CurrentControlSet\Enum\DISPLAY"
try:
key = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, reg_path)
display_count = 0
for i in range(winreg.QueryInfoKey(key)[0]):
try:
subkey_name = winreg.EnumKey(key, i)
subkey = winreg.OpenKey(key, subkey_name)
# EDID is in nested subkeys, enumerate them
for j in range(winreg.QueryInfoKey(subkey)[0]):
try:
nested_name = winreg.EnumKey(subkey, j)
nested_key = winreg.OpenKey(subkey, nested_name)
# Try Device Parameters subkey
try:
dev_params = winreg.OpenKey(nested_key, "Device Parameters")
edid_raw = winreg.QueryValueEx(dev_params, "EDID")[0]
display_count += 1
if edid_raw and len(edid_raw) >= 10:
# EDID format: Manufacturer ID is at bytes 8-9 (0-indexed)
mfg_bytes = edid_raw[8:10]
mfg_code_int = (mfg_bytes[0] << 8) | mfg_bytes[1]
# Decode the 3-letter code
char1 = chr(64 + ((mfg_code_int >> 10) & 0x1F))
char2 = chr(64 + ((mfg_code_int >> 5) & 0x1F))
char3 = chr(64 + (mfg_code_int & 0x1F))
mfg_code = char1 + char2 + char3
mfg_name = mfg_codes.get(mfg_code, f"Unknown ({mfg_code})")
manufacturers.append(mfg_name)
print(f"Monitor {display_count}: {mfg_name} ({mfg_code})")
else:
manufacturers.append(None)
winreg.CloseKey(dev_params)
except FileNotFoundError:
# Device Parameters subkey doesn't exist, try EDID directly
try:
edid_raw = winreg.QueryValueEx(nested_key, "EDID")[0]
display_count += 1
if edid_raw and len(edid_raw) >= 10:
mfg_bytes = edid_raw[8:10]
mfg_code_int = (mfg_bytes[0] << 8) | mfg_bytes[1]
char1 = chr(64 + ((mfg_code_int >> 10) & 0x1F))
char2 = chr(64 + ((mfg_code_int >> 5) & 0x1F))
char3 = chr(64 + (mfg_code_int & 0x1F))
mfg_code = char1 + char2 + char3
mfg_name = mfg_codes.get(mfg_code, f"Unknown ({mfg_code})")
manufacturers.append(mfg_name)
print(f"Monitor {display_count}: {mfg_name} ({mfg_code})")
else:
manufacturers.append(None)
except Exception:
pass
winreg.CloseKey(nested_key)
except Exception:
pass
winreg.CloseKey(subkey)
except Exception:
pass
winreg.CloseKey(key)
except Exception as e:
print(f"Registry access error: {e}")
print(f"Total EDID manufacturers found: {len(manufacturers)}")
except Exception as e:
print(f"EDID parsing error: {e}")
return manufacturers
# --- Autostart (Windows) support ---
def _get_startup_paths(self):
"""Return possible startup file paths (lnk and vbs) in the user's Startup folder."""
appdata = os.environ.get('APPDATA')
if not appdata:
return None, None
startup = os.path.join(appdata, r"Microsoft\Windows\Start Menu\Programs\Startup")
lnk = os.path.join(startup, 'MonitorPy.lnk')
vbs = os.path.join(startup, 'MonitorPy.vbs')
return lnk, vbs
def is_autostart_enabled(self):
lnk, vbs = self._get_startup_paths()
try:
return (lnk and os.path.exists(lnk)) or (vbs and os.path.exists(vbs))
except Exception:
return False
def enable_autostart(self):
lnk, vbs = self._get_startup_paths()
# Prefer creating a .lnk via Windows Script Host. Fall back to a .vbs launcher if pywin32 isn't available.
try:
try:
from win32com.client import Dispatch
except Exception:
Dispatch = None
if Dispatch and lnk:
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(lnk)
if getattr(sys, 'frozen', False):
shortcut.Targetpath = sys.executable
shortcut.WorkingDirectory = os.path.dirname(sys.executable)
shortcut.Arguments = ''
else:
pythonw = sys.executable
# prefer pythonw to avoid a console when launching the script
if pythonw.lower().endswith('python.exe'):
pythonw = pythonw[:-len('python.exe')] + 'pythonw.exe'
script = os.path.abspath(sys.argv[0])
shortcut.Targetpath = pythonw
shortcut.Arguments = f'"{script}"'
shortcut.WorkingDirectory = os.path.dirname(script)
try:
ico = os.path.join(os.path.dirname(sys.executable), 'python.exe')
shortcut.IconLocation = ico
except Exception:
pass
shortcut.save()
return True
# Fallback: create a .vbs in Startup that launches pythonw + script without showing a console
if vbs:
if getattr(sys, 'frozen', False):
exe = sys.executable
cmd = f'"{exe}"'
else:
pythonw = sys.executable
if pythonw.lower().endswith('python.exe'):
pythonw = pythonw[:-len('python.exe')] + 'pythonw.exe'
script = os.path.abspath(sys.argv[0])
cmd = f'"{pythonw}" "{script}"'
content = (
'Set WshShell = CreateObject("WScript.Shell")\n'
f'WshShell.Run {cmd}, 0\n'
)
try:
with open(vbs, 'w', encoding='utf-8') as f:
f.write(content)
return True
except Exception:
return False
except Exception:
return False
def disable_autostart(self):
lnk, vbs = self._get_startup_paths()
ok = False
try:
if lnk and os.path.exists(lnk):
try:
os.remove(lnk)
ok = True
except Exception:
pass
if vbs and os.path.exists(vbs):
try:
os.remove(vbs)
ok = True
except Exception:
pass
except Exception:
pass
return ok
def toggle_autostart(self, icon=None, item=None):
# Ensure runs on Tk main thread when invoked from tray
if self.root and threading.current_thread() is not threading.main_thread():
if self.root:
self.root.after(0, lambda: self.toggle_autostart(None, None))
return
try:
if self.is_autostart_enabled():
self.disable_autostart()
else:
self.enable_autostart()
finally:
# Refresh tray menu to update checked state
if self.tray_icon:
try:
self.tray_icon.menu = self.create_tray_menu()
except Exception:
pass
def get_monitor_names(self):
"""Get monitor names with optional manufacturer."""
return [info["name"] for info in self.monitor_infos]
def get_monitor_display_names(self):
"""Get formatted monitor names with manufacturer info."""
names = []
for info in self.monitor_infos:
if info["manufacturer"]:
display_name = f"{info['manufacturer']} {info['name']}"
else:
display_name = info["name"]
names.append(display_name)
return names
# --- get/set brightness and contrast with error handling ---
def get_brightness(self):
return self.current_brightness
def set_brightness(self, value):
if self.monitor:
try:
with self.monitor:
self.monitor.set_luminance(int(value))
except Exception:
pass
self.current_brightness = int(value)
self.monitor_infos[self.selected_monitor_index]["brightness"] = int(value)
def get_contrast(self):
return self.current_contrast
def set_contrast(self, value):
if self.monitor:
try:
with self.monitor:
self.monitor.set_contrast(int(value))
except Exception:
pass
self.current_contrast = int(value)
self.monitor_infos[self.selected_monitor_index]["contrast"] = int(value)
# --- Keyboard shortcut helpers for brightness and contrast ---
def _clamp(self, v, lo=0, hi=100):
try:
v = int(v)
except Exception:
return lo
return max(lo, min(hi, v))
def change_brightness_by(self, delta):
cur = self._clamp(self.get_brightness())
new = self._clamp(cur + int(delta))
self.set_brightness(new)
if self.brightness_var:
try:
self.brightness_var.set(new)
self.brightness_label.config(text=f"{new}%")
except Exception:
pass
def increase_brightness(self, event=None):
self.change_brightness_by(getattr(self, 'brightness_step', 5))
def decrease_brightness(self, event=None):
self.change_brightness_by(-getattr(self, 'brightness_step', 5))
def change_contrast_by(self, delta):
cur = self._clamp(self.get_contrast())
new = self._clamp(cur + int(delta))
self.set_contrast(new)
if self.contrast_var:
try:
self.contrast_var.set(new)
self.contrast_label.config(text=f"{new}%")
except Exception:
pass
def increase_contrast(self, event=None):
self.change_contrast_by(getattr(self, 'contrast_step', 5))
def decrease_contrast(self, event=None):
self.change_contrast_by(-getattr(self, 'contrast_step', 5))
def create_image(self, width, height, color1, color2):
image = Image.new('RGB', (width, height), color1)
dc = ImageDraw.Draw(image)
# Draw monitor frame (white rectangle border)
frame_x1, frame_y1 = 0, 0
frame_x2, frame_y2 = width - 1, height - 1
dc.rectangle([frame_x1, frame_y1, frame_x2, frame_y2], fill=None, outline='white', width=3)
# Draw inner monitor bezel
bezel_x1, bezel_y1 = 2, 2
bezel_x2, bezel_y2 = width - 3, height - 3
dc.rectangle([bezel_x1, bezel_y1, bezel_x2, bezel_y2], fill=None, outline='white', width=1)
# Draw diagonally split rectangle - left side white, right side black
x1, y1 = 4, 4
x2, y2 = width - 5, height - 5
# Left/top-left triangle: white
dc.polygon([(x1, y1), (x2, y1), (x1, y2)], fill=color2)
# Right/bottom-right triangle: black
dc.polygon([(x2, y1), (x2, y2), (x1, y2)], fill=color1)
# Draw outline
dc.rectangle([x1, y1, x2, y2], fill=None, outline='white', width=1)
return image
def show_control_window(self, icon=None, item=None):
"""Toggle the control window visibility and position it near the tray icon"""
# If invoked from pystray's thread, schedule on Tk main thread to avoid freezes
if self.root and threading.current_thread() is not threading.main_thread():
if self.root:
self.root.after(0, lambda: self.show_control_window(None, None))
return
if self.root and self.root.winfo_exists() and self.root.state() != 'withdrawn':
self.root.withdraw()
return
if not self.root or not self.root.winfo_exists():
self.create_control_window()
else:
self.position_near_tray()
self.root.deiconify()
self.root.lift()
self.root.focus_force()
try:
# start polling for outside clicks
self.root.after(100, self._outside_click_check)
except Exception:
pass
def position_near_tray(self):
self.root.update_idletasks()
width = self.root.winfo_width()
height = self.root.winfo_height()
screen_width = self.root.winfo_screenwidth()
screen_height = self.root.winfo_screenheight()
try:
import ctypes
class RECT(ctypes.Structure):
_fields_ = [("left", ctypes.c_long),
("top", ctypes.c_long),
("right", ctypes.c_long),
("bottom", ctypes.c_long)]
SPI_GETWORKAREA = 0x0030
rect = RECT()
ctypes.windll.user32.SystemParametersInfoW(SPI_GETWORKAREA, 0, ctypes.byref(rect), 0)
if rect.top > 0:
x = screen_width - width - 20
y = rect.top + 10
elif rect.bottom < screen_height:
x = screen_width - width - 20
y = rect.bottom - height - 10
elif rect.left > 0:
x = rect.left + 10
y = screen_height - height - 60
elif rect.right < screen_width:
x = rect.right - width - 10
y = screen_height - height - 60
else:
x = screen_width - width - 20
y = screen_height - height - 60
except Exception:
x = screen_width - width - 20
y = screen_height - height - 60
self.root.geometry(f"+{x}+{y}")
def _outside_click_check(self):
"""Periodically check whether the user clicked outside the control window.
If a left-button click occurs outside the window bounds, hide the window.
Uses Win32 GetCursorPos and GetAsyncKeyState to detect clicks outside the app.
Also accounts for the edit presets (Day and Night) window.
"""
try:
if not self.root or not self.root.winfo_exists() or self.root.state() == 'withdrawn':
return
import ctypes
class POINT(ctypes.Structure):
_fields_ = [("x", ctypes.c_long), ("y", ctypes.c_long)]
pt = POINT()
if not ctypes.windll.user32.GetCursorPos(ctypes.byref(pt)):
# Failed to get cursor position; schedule next check
self.root.after(100, self._outside_click_check)
return
mx, my = pt.x, pt.y
rx = self.root.winfo_rootx()
ry = self.root.winfo_rooty()
rw = self.root.winfo_width()
rh = self.root.winfo_height()
inside = (rx <= mx <= rx + rw) and (ry <= my <= ry + rh)
# Also check if click is inside the edit window
edit_win = getattr(self, 'edit_win', None)
if edit_win and edit_win.winfo_exists() and edit_win.state() != 'withdrawn':
ex = edit_win.winfo_rootx()
ey = edit_win.winfo_rooty()
ew = edit_win.winfo_width()
eh = edit_win.winfo_height()
inside_edit = (ex <= mx <= ex + ew) and (ey <= my <= ey + eh)
inside = inside or inside_edit
# VK_LBUTTON == 0x01. GetAsyncKeyState returns negative if down (high bit set).
lbutton = ctypes.windll.user32.GetAsyncKeyState(0x01) & 0x8000
# If left button is down and cursor is outside both windows, hide.
if lbutton and not inside:
try:
self.hide_window()
except Exception:
pass
return
except Exception:
# Ignore errors and continue polling
pass
finally:
try:
# keep polling while the window exists
if self.root and self.root.winfo_exists():
self.root.after(100, self._outside_click_check)
except Exception:
pass
def create_control_window(self):
self.root = tk.Tk()
self.root.title("MonitorPy")
self.root.geometry("320x200")
self.root.resizable(False, False)
self.root.attributes('-toolwindow', True)
self.root.protocol("WM_DELETE_WINDOW", self.hide_window)
self.root.overrideredirect(True)
main_frame = ttk.Frame(self.root, padding="8")
main_frame.pack(fill=tk.BOTH, expand=True)
top_frame = ttk.Frame(main_frame)
top_frame.pack(fill=tk.X)
ttk.Label(top_frame, text="MonitorPy", font=("Segoe UI", 10, "bold")).pack(side=tk.LEFT)
ttk.Button(top_frame, text="Edit", command=self.open_edit_presets_window, width=len("Edit")).pack(side=tk.RIGHT, padx=1)
ttk.Button(top_frame, text="Night", command=self.quick_preset_2, width=len("Preset 2")).pack(side=tk.RIGHT, padx=1)
ttk.Button(top_frame, text="Day", command=self.quick_preset_1, width=len("Preset 1")).pack(side=tk.RIGHT, padx=1)
ttk.Label(main_frame, text="Select Monitor").pack(anchor=tk.W)
self.monitor_listbox = tk.Listbox(main_frame, height=3)
self.monitor_listbox.pack(fill=tk.X, pady=3)
for i, name in enumerate(self.get_monitor_display_names()):
self.monitor_listbox.insert(tk.END, name)
self.monitor_listbox.bind("<<ListboxSelect>>", self.on_monitor_select)
# Restore last selection if available
if hasattr(self, 'last_selected_monitor_index'):
self.selected_monitor_index = self.last_selected_monitor_index
else:
self.selected_monitor_index = 0
self.monitor_listbox.select_set(self.selected_monitor_index)
sliders_frame = ttk.Frame(main_frame)
sliders_frame.pack(fill=tk.X, pady=8)
ttk.Label(sliders_frame, text="Brightness (Ctrl+F10/F11)", font=("Segoe UI", 8)).grid(row=0, column=0, sticky=tk.W)
brightness_value = getattr(self, 'last_brightness', self.get_brightness())
self.brightness_var = tk.IntVar(value=brightness_value)
self.brightness_scale = ttk.Scale(sliders_frame, from_=0, to=100, variable=self.brightness_var,
command=self.on_brightness_change, orient=tk.HORIZONTAL, length=140)
self.brightness_scale.grid(row=1, column=0, padx=(0,10))
self.brightness_label = ttk.Label(sliders_frame, text=f"{self.brightness_var.get()}%")
self.brightness_label.grid(row=2, column=0, sticky=tk.E)
ttk.Label(sliders_frame, text="Contrast (Shift+F10/F11)", font=("Segoe UI", 8)).grid(row=0, column=1, sticky=tk.W)
contrast_value = getattr(self, 'last_contrast', self.get_contrast())
self.contrast_var = tk.IntVar(value=contrast_value)
self.contrast_scale = ttk.Scale(sliders_frame, from_=0, to=100, variable=self.contrast_var,
command=self.on_contrast_change, orient=tk.HORIZONTAL, length=140)
self.contrast_scale.grid(row=1, column=1, padx=(0,10))
self.contrast_label = ttk.Label(sliders_frame, text=f"{self.contrast_var.get()}%")
self.contrast_label.grid(row=2, column=1, sticky=tk.E)
self.position_near_tray()
self.root.focus_force()
# keyboard shortcut defaults and bindings
self.brightness_step = getattr(self, 'brightness_step', 5)
self.contrast_step = getattr(self, 'contrast_step', 5)
try:
self.root.bind_all('<Control-F11>', self.increase_brightness)
self.root.bind_all('<Control-F10>', self.decrease_brightness)
self.root.bind_all('<Shift-F11>', self.increase_contrast)
self.root.bind_all('<Shift-F10>', self.decrease_contrast)
except Exception:
pass
try:
# begin outside-click watcher
self.root.after(100, self._outside_click_check)
except Exception:
pass
def on_monitor_select(self, event=None):
sel = self.monitor_listbox.curselection()
if sel:
self.selected_monitor_index = sel[0]
info = self.monitor_infos[self.selected_monitor_index]
self.monitor = info["monitor"]
self.current_brightness = info["brightness"]
self.current_contrast = info["contrast"]
self.update_controls()
if self.tray_icon:
self.tray_icon.menu = self.create_tray_menu()
def update_controls(self):
self.brightness_var.set(self.get_brightness())
self.brightness_label.config(text=f"{self.brightness_var.get()}%")
self.contrast_var.set(self.get_contrast())
self.contrast_label.config(text=f"{self.contrast_var.get()}%")
def on_brightness_change(self, value):
if self.monitor:
self.set_brightness(int(float(value)))
self.brightness_label.config(text=f"{int(float(value))}%")
def on_contrast_change(self, value):
if self.monitor:
self.set_contrast(int(float(value)))
self.contrast_label.config(text=f"{int(float(value))}%")
def open_download_link(self):
import webbrowser
webbrowser.open("https://github.com/paulomarconi/MonitorPy")
def hide_window(self):
if self.root:
self.root.withdraw()
def open_edit_presets_window(self, icon=None, item=None):
"""Open a modal to edit preset values for Preset 1 and Preset 2"""
# If invoked from the tray (pystray) thread, schedule on Tk main thread
if self.root and threading.current_thread() is not threading.main_thread():
if self.root:
self.root.after(0, lambda: self.open_edit_presets_window(None, None))
return
if not self.root:
return
# If an edit window already exists, bring it to front instead of
# creating a new one (ensure single-instance editor window).
existing = getattr(self, 'edit_win', None)
try:
if existing and existing.winfo_exists():
try:
existing.deiconify()
existing.lift()
existing.focus_force()
except Exception:
pass
return
except Exception:
pass
edit_win = tk.Toplevel(self.root)
# remember the edit window so we can reuse it
self.edit_win = edit_win
edit_win.title("Edit Presets")
edit_win.resizable(False, False)
# Match main GUI style: no border, no title or close button
try:
edit_win.attributes('-toolwindow', True)
except Exception:
pass
try:
edit_win.overrideredirect(True)
except Exception:
pass
# Only set transient to the main root if the root is visible.
# If the root is withdrawn (hidden), making the Toplevel transient
# to a hidden root can prevent it from being shown. Create the
# Toplevel without transient in that case so it will appear.
try:
if self.root and self.root.winfo_exists() and self.root.state() != 'withdrawn':
edit_win.transient(self.root)
except Exception:
pass
frame = ttk.Frame(edit_win, padding=8)
frame.pack(fill=tk.BOTH, expand=True)
# Preset 1
ttk.Label(frame, text="Day").grid(row=0, column=0, columnspan=2, sticky=tk.W)
reset_b_var = tk.IntVar(value=self.preset_1.get("brightness", 100))
reset_c_var = tk.IntVar(value=self.preset_1.get("contrast", 70))
ttk.Label(frame, text="Brightness:").grid(row=1, column=0, sticky=tk.W, pady=(4,0))
reset_b_entry = ttk.Entry(frame, textvariable=reset_b_var, width=6)
reset_b_entry.grid(row=1, column=1, sticky=tk.W, pady=(4,0))
ttk.Label(frame, text="Contrast:").grid(row=2, column=0, sticky=tk.W)
reset_c_entry = ttk.Entry(frame, textvariable=reset_c_var, width=6)
reset_c_entry.grid(row=2, column=1, sticky=tk.W)
# Preset 2
ttk.Separator(frame).grid(row=3, column=0, columnspan=2, sticky=tk.EW, pady=6)
ttk.Label(frame, text="Night").grid(row=4, column=0, columnspan=2, sticky=tk.W)
p_b_var = tk.IntVar(value=self.preset_2.get("brightness", 30))
p_c_var = tk.IntVar(value=self.preset_2.get("contrast", 50))
ttk.Label(frame, text="Brightness:").grid(row=5, column=0, sticky=tk.W, pady=(4,0))
p_b_entry = ttk.Entry(frame, textvariable=p_b_var, width=6)
p_b_entry.grid(row=5, column=1, sticky=tk.W, pady=(4,0))
ttk.Label(frame, text="Contrast:").grid(row=6, column=0, sticky=tk.W)
p_c_entry = ttk.Entry(frame, textvariable=p_c_var, width=6)
p_c_entry.grid(row=6, column=1, sticky=tk.W)
btn_frame = ttk.Frame(frame)
btn_frame.grid(row=7, column=0, columnspan=2, pady=(8,0))
def save_presets():
try:
rb = int(reset_b_var.get())
rc = int(reset_c_var.get())
pb = int(p_b_var.get())
pc = int(p_c_var.get())
except Exception:
messagebox.showerror("Invalid value", "Preset values must be integers 0-100")
return
for v in (rb, rc, pb, pc):
if v < 0 or v > 100:
messagebox.showerror("Invalid range", "Values must be between 0 and 100")
return
self.preset_1["brightness"] = rb
self.preset_1["contrast"] = rc
self.preset_2["brightness"] = pb
self.preset_2["contrast"] = pc
# Update controls if visible
if self.brightness_var:
self.brightness_label.config(text=f"{self.brightness_var.get()}%")
try:
edit_win.destroy()
finally:
# clear reference so a new window can be created later
self.edit_win = None
ttk.Button(btn_frame, text="Save", command=save_presets, width=len("Save")).pack(side=tk.RIGHT, padx=4)
def close_edit_window():
try:
edit_win.destroy()
finally:
self.edit_win = None
ttk.Button(btn_frame, text="Cancel", command=close_edit_window, width=len("Cancel")).pack(side=tk.RIGHT)
edit_win.protocol("WM_DELETE_WINDOW", close_edit_window)
# Ensure main GUI is visible and positioned near tray so we can
# place the editor window adjacent to it.
try:
if self.root.state() == 'withdrawn':
self.root.deiconify()
self.position_near_tray()
self.root.update_idletasks()
except Exception:
pass
# Position editor to left or right of main GUI depending on GUI x
try:
self.root.update_idletasks()
edit_win.update_idletasks()
screen_width = self.root.winfo_screenwidth()
rx = self.root.winfo_rootx()
ry = self.root.winfo_rooty()
rw = self.root.winfo_width()
rh = self.root.winfo_height()
ew = edit_win.winfo_reqwidth()
eh = edit_win.winfo_reqheight()
# If GUI is on right half of screen, put editor to the left
if (rx + rw/2) > (screen_width / 2):
ex = rx - ew - 8
else:
ex = rx + rw + 8
# Align tops; clamp to screen
ey = ry
if ex < 0:
ex = 8
if ey < 0:
ey = 8
edit_win.geometry(f"+{ex}+{ey}")
edit_win.deiconify()
edit_win.lift()
edit_win.focus_force()
except Exception:
pass
def quit_app(self, icon=None, item=None):
# Ensure quit runs on the Tk main thread when triggered from tray
if self.root and threading.current_thread() is not threading.main_thread():
if self.root:
self.root.after(0, lambda: self.quit_app(None, None))
return
if self.root:
self.root.quit()
if self.tray_icon:
self.tray_icon.stop()
def create_tray_menu(self):
monitor_names = self.get_monitor_display_names()
current_monitor_name = monitor_names[self.selected_monitor_index] if monitor_names else "No monitor"
return pystray.Menu(
pystray.MenuItem("MonitorPy v1.0.2 | Site", lambda icon, item: self.open_download_link(), enabled=True),
pystray.MenuItem(f"Current Monitor: {current_monitor_name}", None, enabled=False),
pystray.MenuItem("Show Controls", self.show_control_window, default=True),
pystray.MenuItem("Edit Presets", lambda icon, item: self.open_edit_presets_window()),
pystray.MenuItem("Day", self.quick_preset_1),
pystray.MenuItem("Night", self.quick_preset_2),
pystray.MenuItem(
"Autostart on Windows startup",
lambda icon, item: self.toggle_autostart(),
checked=lambda item: self.is_autostart_enabled()
),
pystray.MenuItem("Exit", self.quit_app)
)
def quick_preset_1(self):
# If invoked from non-main thread (tray), schedule on Tk thread
if self.root and threading.current_thread() is not threading.main_thread():
if self.root:
self.root.after(0, self.quick_preset_1)
return
self.set_brightness(self.preset_1.get("brightness", 100))
self.set_contrast(self.preset_1.get("contrast", 70))
if self.brightness_var:
self.brightness_var.set(self.preset_1.get("brightness", 100))
self.brightness_label.config(text=f"{self.brightness_var.get()}%")
if self.contrast_var:
self.contrast_var.set(self.preset_1.get("contrast", 70))
self.contrast_label.config(text=f"{self.contrast_var.get()}%")
def quick_preset_2(self):
"""Set preset 1"""
# If invoked from non-main thread (tray), schedule on Tk thread
if self.root and threading.current_thread() is not threading.main_thread():
if self.root:
self.root.after(0, self.quick_preset_2)
return
self.set_brightness(self.preset_2.get("brightness", 30))
self.set_contrast(self.preset_2.get("contrast", 50))
if self.brightness_var:
self.brightness_var.set(self.preset_2.get("brightness", 30))
self.brightness_label.config(text=f"{self.brightness_var.get()}%")
if self.contrast_var:
self.contrast_var.set(self.preset_2.get("contrast", 50))
self.contrast_label.config(text=f"{self.contrast_var.get()}%")
def run(self):
icon_image = self.create_image(64, 64, 'black', 'white')
self.tray_icon = pystray.Icon(
"monitor_control",
icon_image,
f"Monitor Control - {'Connected' if self.monitor_connected else 'Disconnected'}",
self.create_tray_menu()
)
tray_thread = threading.Thread(target=self.tray_icon.run, daemon=True)
tray_thread.start()
self.create_control_window()
self.root.withdraw()
def check_interrupt():
try:
pass
except KeyboardInterrupt:
self.quit_app()
self.root.after(100, check_interrupt)
self.root.after(100, check_interrupt)
try:
self.root.mainloop()
except KeyboardInterrupt:
self.quit_app()
if __name__ == "__main__":
try:
app = MonitorController()
app.run()
except Exception as e:
print(f"Error starting application: {e}")
messagebox.showerror("Error", f"Failed to start application: {e}")
sys.exit(1)