-
Notifications
You must be signed in to change notification settings - Fork 1
/
profiles.py
156 lines (118 loc) · 5.51 KB
/
profiles.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
https://github.com/cronosun/atrofac/blob/master/ADVANCED.md
"""
import os
from subprocess import check_output
from debug import get_logger
log = get_logger("default")
from configuration import main_config, cache_file
from paths import RES_DIR
import time
atrofac_cli = os.path.join(RES_DIR, "atrofac-cli.exe")
ryzenadj_cli = os.path.join(RES_DIR, "ryzenadj-win64", "ryzenadj.exe")
displays_cli = os.path.join(RES_DIR, "ChangeScreenResolution.exe")
PROFILE_DIRTY = False # Tracks whether profile was changed and needs to be applied
REFRESH_RATE_DIRTY = False
def format_atrofac_curve(c):
return f"30c:{c[0]}%,40c:{c[1]}%,50c:{c[2]}%,60c:{c[3]}%,70c:{c[4]}%,80c:{c[5]}%,90c:{c[6]}%,100c:{c[7]}%"
def apply_atrofac_profile(profile_data):
profile = profile_data.get("profile")
cpu_curve = profile_data.get("cpu")
gpu_curve = profile_data.get("gpu")
if not cpu_curve and not cpu_curve:
cmd = f"{atrofac_cli} plan {profile}"
else:
cmd = f"{atrofac_cli} fan --plan {profile}"
if cpu_curve:
cmd += " --cpu "+format_atrofac_curve(cpu_curve)
if gpu_curve:
cmd += " --gpu "+format_atrofac_curve(gpu_curve)
log.info(f"apply_atrofac_profile: {cmd}")
log.info(check_output(cmd, shell=True))
def apply_ryzenadj_profile(profile_data):
log.info(f"set_ryzenadj_profile: {profile_data}")
cmd = f"{ryzenadj_cli}"
for key, value in profile_data.items():
if key != "name":
milliwatts = value
if milliwatts % 1000000 == 0:
milliwatts = milliwatts / 1000;
cmd += f" --{key}={milliwatts}"
log.info(f"apply_ryzenadj_profile cmd: {cmd}")
log.info(check_output(cmd, shell=True))
# print(str(os.system(cmd)))
if cache_file.get("CURRENT_PROFILE") is None:
cache_file.set("CURRENT_PROFILE", 0)
first_launch = True
else:
first_launch = False
# Maximum CPU frequency
# powercfg /setdcvalueindex SCHEME_CURRENT 54533251-82be-4824-96c1-47b60b740d00 75b0ae3f-bce0-45a7-8c89-c9611c25e100 0
def apply_powercfg_profile(profile_data):
cmd = f"powercfg /SETDCVALUEINDEX SCHEME_CURRENT SUB_PROCESSOR PERFBOOSTMODE {main_config['powercfg']['boost_modes'].index(profile_data['boost_mode'])}"
log.info(f"apply_powercfg_profile: {cmd}")
log.info(check_output(cmd, shell=True))
cmd = f"powercfg /SETACVALUEINDEX SCHEME_CURRENT SUB_PROCESSOR PERFBOOSTMODE {main_config['powercfg']['boost_modes'].index(profile_data['boost_mode'])}"
log.info(f"apply_powercfg_profile: {cmd}")
log.info(check_output(cmd, shell=True))
if main_config['powercfg'].get('gpu_instance_path') is not None:
try:
if profile_data.get('dgpu_disabled', False) is True:
cmd = 'pnputil /disable-device "{}"'.format(main_config['powercfg']['gpu_instance_path'])
else:
cmd = 'pnputil /enable-device "{}"'.format(main_config['powercfg']['gpu_instance_path'])
log.info(f"apply_powercfg_profile: {cmd}")
log.info(check_output(cmd, shell=True))
except Exception as e:
log.info(e)
def toggle_profile():
"""
Changes current profile in cache file, does not apply it.
* This distinction is required to be able to toggle between profiles without applying changes until correct profile is selected.
"""
global PROFILE_DIRTY
i = cache_file.get("CURRENT_PROFILE") + 1
if i >= len(main_config['power_presets']):
i = 0
cache_file.set("CURRENT_PROFILE", i)
PROFILE_DIRTY = True
return main_config['power_presets'][i]['name']
def apply_current_profile():
"""
Applies profile currently selected in cache file.
* This distinction is required to be able to toggle between profiles without applying changes until correct profile is selected.
"""
global PROFILE_DIRTY
log.info("Applying current profile...")
CURRENT_PROFILE = cache_file.get("CURRENT_PROFILE")
# HINT This can be made more dynamic and less hardcoded, rely more on yaml config and key names
power_presets = main_config['power_presets'][CURRENT_PROFILE]
for key, value in power_presets.items():
if key == "ryzenadj_presets":
apply_ryzenadj_profile(main_config['ryzenadj_presets'][value])
elif key == "atrofac":
apply_atrofac_profile(value)
elif key == "powercfg":
apply_powercfg_profile(value)
PROFILE_DIRTY = False
return main_config['power_presets'][CURRENT_PROFILE]['name']
def apply_main_display_rate():
global REFRESH_RATE_DIRTY
CURRENT_REFRESH_RATE = cache_file.get("CURRENT_REFRESH_RATE")
log.info(check_output(rf"{displays_cli} /d=0 /f={CURRENT_REFRESH_RATE}"))
REFRESH_RATE_DIRTY = False
def toggle_display_refreshrate():
global REFRESH_RATE_DIRTY
CURRENT_REFRESH_RATE = cache_file.get("CURRENT_REFRESH_RATE")
if CURRENT_REFRESH_RATE is None:
cache_file.set("CURRENT_REFRESH_RATE", main_config['display']['max_refreshrate'])
CURRENT_REFRESH_RATE = main_config['display']['max_refreshrate']
if CURRENT_REFRESH_RATE == main_config['display']['max_refreshrate']:
cache_file.set("CURRENT_REFRESH_RATE", main_config['display']['min_refreshrate'])
elif CURRENT_REFRESH_RATE == main_config['display']['min_refreshrate']:
cache_file.set("CURRENT_REFRESH_RATE", main_config['display']['max_refreshrate'])
REFRESH_RATE_DIRTY = True
return cache_file.get("CURRENT_REFRESH_RATE")
if first_launch is False:
# Apply current profile as set in cache on startup
apply_current_profile()