Skip to content

Commit 3d53c04

Browse files
committed
Fixed power & gaming threads & reload
1 parent 2e5d56d commit 3d53c04

File tree

3 files changed

+40
-12
lines changed

3 files changed

+40
-12
lines changed

G14Control.pyw

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,42 @@ class power_check_thread(threading.Thread):
6767
self,
6868
):
6969
threading.Thread.__init__(self, daemon=True)
70+
print("power thread started...")
7071

7172
def run(self):
7273
global data
74+
debug = data.config["debug"]
7375
# Only run while loop on startup if auto_power_switch is On (True)
76+
if debug:
77+
print("power thread running...")
7478
if data.auto_power_switch:
79+
if debug:
80+
print("power switch is enabled...")
7581
while data.run_power_thread:
82+
if debug:
83+
print(" run power thread is true")
7684
# Check to user hasn't disabled auto_power_switch
7785
# (i.e. by manually switching plans)
7886
if data.auto_power_switch:
7987
ac = (
8088
psutil.sensors_battery().power_plugged
8189
) # Get the current AC power status
8290
# If on AC power, and not on the default_ac_plan, switch to that plan
83-
if ac and data.current_plan != data.default_ac_plan:
91+
if (
92+
ac
93+
and data.current_plan != data.default_ac_plan
94+
and not data.game_running
95+
):
8496
for plan in data.config["plans"]:
8597
if plan["name"] == data.default_ac_plan:
8698
apply_plan(plan)
8799
break
88100
# If on DC power, and not on the default_dc_plan, switch to that plan
89-
if not ac and data.current_plan != data.default_dc_plan:
101+
if (
102+
not ac
103+
and data.current_plan != data.default_dc_plan
104+
and not data.game_running
105+
):
90106
for plan in data.config["plans"]:
91107
if plan["name"] == data.default_dc_plan:
92108
apply_plan(plan)
@@ -128,11 +144,14 @@ class gaming_check_thread(threading.Thread):
128144
if (
129145
processes & targets
130146
): # Compare 2 lists, if ANY overlap, set game_running to true
131-
game_running = True
147+
data.game_running = True
132148
else:
133-
game_running = False
149+
data.game_running = False
134150
# If game is running and not on the desired gaming plan, switch to that plan
135-
if game_running and data.current_plan != data.default_gaming_plan:
151+
if (
152+
data.game_running
153+
and data.current_plan != data.default_gaming_plan
154+
):
136155
previous_plan = data.current_plan
137156
for plan in data.config["plans"]:
138157
if plan["name"] == data.default_gaming_plan:
@@ -142,7 +161,7 @@ class gaming_check_thread(threading.Thread):
142161
# If game is no longer running, and not on previous plan
143162
# already (if set), then switch back to previous plan
144163
if (
145-
not game_running
164+
not data.game_running
146165
and previous_plan is not None
147166
and previous_plan != data.current_plan
148167
):
@@ -154,10 +173,12 @@ class gaming_check_thread(threading.Thread):
154173
# Check for programs every 10 sec
155174
time.sleep(config["check_power_every"])
156175
else:
176+
data.game_running = False
157177
self.kill()
158178

159179
def kill(self):
160180
thread_id = self.ident
181+
data.game_running = False
161182
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(
162183
thread_id, ctypes.py_object(SystemExit)
163184
)
@@ -204,14 +225,16 @@ def quit_app():
204225
if data.power_thread is not None and data.power_thread.is_alive():
205226
data.power_thread.kill()
206227
while data.power_thread.isAlive():
207-
print("Waiting for power thread to die...")
228+
if data.config["debug"]:
229+
print("Waiting for power thread to die...")
208230
time.sleep(0.25)
209231
print("Power thread was alive, and now is dead.")
210232

211233
if data.gaming_thread is not None and data.gaming_thread.is_alive():
212234
data.gaming_thread.kill()
213235
while data.gaming_thread.isAlive():
214-
print("Waiting for power thread to die...")
236+
if data.config["debug"]:
237+
print("Waiting for gaming thread to die...")
215238
time.sleep(0.25)
216239
print("Gaming thread was alive, and now is dead.")
217240
if device is not None:
@@ -399,8 +422,8 @@ def create_menu(
399422

400423
def reload_config(icon_app, device):
401424
global data
402-
data = G14_Data()
403425
deactivate_powerswitching(False)
426+
data = G14_Data()
404427
if (
405428
data.default_ac_plan is not None
406429
and data.default_dc_plan is not None
@@ -412,6 +435,7 @@ def reload_config(icon_app, device):
412435

413436
if data.auto_power_switch and data.config["power_switch_enabled"]:
414437
data.power_thread = power_check_thread()
438+
data.run_power_thread = True
415439
data.power_thread.start()
416440

417441
if (
@@ -420,6 +444,7 @@ def reload_config(icon_app, device):
420444
and data.config["power_switch_enabled"] is True
421445
):
422446
data.gaming_thread = gaming_check_thread()
447+
data.run_gaming_thread = True
423448
data.gaming_thread.start()
424449

425450
if device is not None:
@@ -452,6 +477,7 @@ def startup(config, icon_app):
452477

453478
if data.power_switch_enabled:
454479
data.power_thread = power_check_thread()
480+
data.run_power_thread = True
455481
data.power_thread.start()
456482

457483
if (
@@ -460,6 +486,7 @@ def startup(config, icon_app):
460486
and data.power_switch_enabled
461487
):
462488
data.gaming_thread = gaming_check_thread()
489+
data.run_gaming_thread = True
463490
data.gaming_thread.start()
464491

465492
device = rog_keyset(config)

G14Data.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __init__(self):
6868
self.power_switch_enabled = self.config["power_switch_enabled"]
6969
self.default_gaming_plan = self.config["default_gaming_plan"]
7070
self.default_gaming_plan_games = self.config["default_gaming_plan_games"]
71-
self.auto_power_switch = self.power_switch_enabled
71+
self.auto_power_switch = bool(self.power_switch_enabled)
7272
self.rog_key = self.config["rog_key"]
7373
self.current_plan = self.default_starting_plan
7474
self.current_windows_plan = self.default_power_plan
@@ -83,6 +83,7 @@ def __init__(self):
8383
)
8484
self.registry_key_loc = r"Software\Microsoft\Windows\CurrentVersion\Run"
8585

86+
self.game_running = False
8687
self.run_gaming_thread = None
8788
self.run_power_thread = None
8889
self.power_thread = None

data/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ start_on_boot: true # Set to True to enable starting on boot which adds a window
44
notification_time: 5 # How long notifications will stay on the screen
55
long_notification_time: 5
66
check_power_every: 10 # Seconds in between checks to get the battery/ac status
7-
default_gaming_plan: "Performance Plus"# Example: "Extreme"
8-
default_gaming_plan_games: ["notepad.exe"] # Example list: ["7zFM.exe", "notepad++.exe"]
7+
default_gaming_plan: "Performance Plus" # Example: "Extreme"
8+
default_gaming_plan_games: [] # Example list: ["7zFM.exe", "notepad++.exe"]
99
default_starting_plan: "Silent (low-speed fan)" # G14control plan
1010
default_ac_plan: "Silent (low-speed fan)" # G14control plan
1111
default_dc_plan: "Silent (fanless)" # G14control plan

0 commit comments

Comments
 (0)