77import winreg
88import psutil
99import shutil
10- from typing import Union , Dict , SupportsInt , AnyStr
10+ from typing import Union , Dict , SupportsInt , AnyStr , Tuple
11+ import ctypes
1112
1213from pathlib import Path
1314from pynput import keyboard
1617
1718Keys = keyboard .Key
1819last_btn : Keys
19- TIME_STEP = 2 # seconds
20+ TIME_STEP = 1 # seconds
2021STARTUP_SWITCH = True
2122
2223PATH_APPDATA_LOCAL = Path (os .getenv ("LOCALAPPDATA" )).resolve ()
@@ -53,7 +54,7 @@ def on_release(key):
5354 if key == Keys .backspace and last_btn == Keys .shift_r :
5455 reschanger .set_display_defaults ()
5556 write_logs (Exception ("Program termination caused by user" ))
56- exit ( )
57+ os . _exit ( - 1 )
5758
5859
5960def cur_power_state ():
@@ -96,19 +97,27 @@ async def switch_rate(current_state, prss: ScreenSettings, psss: ScreenSettings)
9697async def srr_loop (time_step , prss : ScreenSettings , psss : ScreenSettings ):
9798 last_state = cur_power_state ()
9899 while True :
99- try :
100- await asyncio .sleep (time_step )
101- current_state = cur_power_state ()
102- if last_state == current_state :
103- continue
104- else :
105- await switch_rate (current_state , prss , psss )
106- last_state = current_state
107- except KeyboardInterrupt as k :
108- write_logs (k )
109- exit ()
110- except Exception as e :
111- write_logs (e )
100+ await asyncio .sleep (time_step )
101+ current_state = cur_power_state ()
102+ if last_state == current_state :
103+ continue
104+ else :
105+ await switch_rate (current_state , prss , psss )
106+ last_state = current_state
107+
108+
109+ def load_config () -> Tuple [ScreenSettings , ScreenSettings ]:
110+ with open (PATH_TO_PROGRAM / "config.json" , "r" ) as config :
111+ stream = config .read ()
112+ params = json .JSONDecoder ().decode (stream )
113+
114+ powersave_dict = params ["powersave-state" ]
115+ powersave_state = ScreenSettings (** powersave_dict )
116+
117+ performance_dict = params ["performance-state" ]
118+ performance_state = ScreenSettings (** performance_dict )
119+
120+ return powersave_state , performance_state
112121
113122
114123async def main ():
@@ -130,29 +139,16 @@ async def main():
130139 params = cur_monitor_specs ()
131140 json .dump (params , config , indent = 4 )
132141
133- with open (PATH_TO_PROGRAM / "config.json" , "r" ) as config :
134- stream = config .read ()
135- params = json .JSONDecoder ().decode (stream )
136-
137- powersave_dict = params ["powersave-state" ]
138- powersave_state = ScreenSettings (** powersave_dict )
142+ powersave_state , performance_state = load_config ()
139143
140- performance_dict = params ["performance-state" ]
141- performance_state = ScreenSettings (** performance_dict )
142-
143- fatalities = 0
144-
145- while fatalities <= 5 :
146- try :
147- with keyboard .Listener (on_press = on_press , on_release = on_release ):
148- await switch_rate (cur_power_state (), performance_state , powersave_state )
149- await srr_loop (TIME_STEP , performance_state , powersave_state )
150- except KeyboardInterrupt as k :
151- write_logs (k )
152- exit ()
153- except Exception as e :
154- write_logs (e )
155- fatalities += 1
144+ try :
145+ with keyboard .Listener (on_press = on_press , on_release = on_release ):
146+ await switch_rate (cur_power_state (), performance_state , powersave_state )
147+ await srr_loop (TIME_STEP , performance_state , powersave_state )
148+ except Exception as e :
149+ ctypes .windll .user32 .MessageBoxW (None , f"The SRR program terminated with the following error:\n { repr (e )} " ,
150+ "Error" , 0 )
151+ write_logs (e )
156152
157153
158154if __name__ == '__main__' :
0 commit comments