generated from SteamDeckHomebrew/decky-plugin-template
-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
244 lines (198 loc) · 7.63 KB
/
main.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
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
import ac_power
import decky_plugin
import plugin_update
import time
import file_timeout
import advanced_options
import power_utils
import cpu_utils
import os
from plugin_settings import merge_tdp_profiles, get_saved_settings, get_tdp_profile, get_active_tdp_profile, per_game_profiles_enabled, set_setting as persist_setting
from gpu_utils import get_gpu_frequency_range
import plugin_utils
import migrations
import steam_info
import device_utils
class Plugin:
async def log_info(self, info):
decky_plugin.logger.info(info)
async def is_steam_running(self):
return steam_info.is_steam_running()
async def get_power_control_info(self):
response = {
'powerControlsEnabled': False,
"eppOptions": [],
"powerGovernorOptions": [],
"scalingDriver": '',
'supportsCpuBoost': False
}
try:
with file_timeout.time_limit(5):
pstate_status = cpu_utils.get_pstate_status()
response['pstateStatus'] = pstate_status
if pstate_status == 'passive' and device_utils.is_intel():
cpu_utils.set_pstate_active()
response['pstateStatus'] = 'active'
response['supportsSmt'] = cpu_utils.supports_smt()
response['scalingDriver'] = cpu_utils.get_scaling_driver()
response['supportsCpuBoost'] = cpu_utils.supports_cpu_boost()
response['powerControlsEnabled'] = power_utils.power_controls_enabled()
if response['powerControlsEnabled']:
response['eppOptions'] = power_utils.get_available_epp_options()
response['powerGovernorOptions'] = power_utils.get_available_governor_options()
except Exception as e:
decky_plugin.logger.error(f'{__name__} get_power_control_info {e}')
return response
async def get_settings(self):
try:
settings = get_saved_settings()
try:
with file_timeout.time_limit(5):
settings['advancedOptions'] = advanced_options.get_advanced_options()
settings['supportsCustomAcPowerManagement'] = ac_power.supports_custom_ac_power_management()
settings['cpuVendor'] = device_utils.get_cpu_manufacturer()
if device_utils.is_intel():
# hardcode min/max TDP values for Intel
min_tdp, max_tdp = cpu_utils.get_intel_tdp_limits()
settings['maxTdp'] = max_tdp
settings['minTdp'] = min_tdp
gpu_min, gpu_max = get_gpu_frequency_range()
if (gpu_min and gpu_max):
settings['minGpuFrequency'] = gpu_min
settings['maxGpuFrequency'] = gpu_max
except Exception as e:
decky_plugin.logger.error(f"main#get_settings failed to get info {e}")
settings['pluginVersionNum'] = f'{decky_plugin.DECKY_PLUGIN_VERSION}'
return settings
except Exception as e:
decky_plugin.logger.error(f"get_settings failed to get settings {e}")
async def set_setting(self, name: str, value):
try:
if name == "advanced":
advanced_options.handle_advanced_option_change(value)
return persist_setting(name, value)
except Exception as e:
decky_plugin.logger.error(f"error failed to set_setting {name}={value} {e}")
async def set_values_for_game_id(self, gameId):
plugin_utils.set_values_for_game_id(gameId)
async def set_steam_patch_values_for_game_id(self, gameId):
enabled = per_game_profiles_enabled()
plugin_utils.set_steam_patch_values_for_game_id(gameId, enabled)
async def persist_tdp(self, tdp, gameId):
plugin_utils.persist_tdp(tdp, gameId)
async def persist_gpu(self, minGpuFrequency, maxGpuFrequency, gameId):
plugin_utils.persist_gpu(minGpuFrequency, maxGpuFrequency, gameId)
async def set_power_governor(self, powerGovernorInfo, gameId):
scaling_driver = powerGovernorInfo.get('scalingDriver')
powerGovernor = powerGovernorInfo.get('powerGovernor')
tdp_profiles = {
f'{gameId}': {
'powerControls': {
f'{scaling_driver}': {
'powerGovernor': powerGovernor
}
}
}
}
merge_tdp_profiles(tdp_profiles)
tdp_profile = get_tdp_profile(gameId)
if tdp_profile:
plugin_utils.set_power_governor_for_tdp_profile(tdp_profile)
async def set_epp(self, eppInfo, gameId):
scaling_driver = eppInfo.get('scalingDriver')
epp = eppInfo.get('epp')
tdp_profiles = {
f'{gameId}': {
'powerControls': {
f'{scaling_driver}': {
'epp': epp
}
}
}
}
merge_tdp_profiles(tdp_profiles)
tdp_profile = get_tdp_profile(gameId)
if tdp_profile and scaling_driver and epp:
plugin_utils.set_epp_for_tdp_profile(tdp_profile)
async def persist_smt(self, smt, gameId):
tdp_profiles = {
f'{gameId}': {
'smt': smt
}
}
merge_tdp_profiles(tdp_profiles)
tdp_profile = get_tdp_profile(gameId)
cpu_utils.set_smt(smt)
time.sleep(0.3)
plugin_utils.set_power_governor_for_tdp_profile(tdp_profile)
async def on_suspend(self):
cpu_utils.set_smt(True)
async def persist_cpu_boost(self, cpuBoost, gameId):
tdp_profiles = {
f'{gameId}': {
'cpuBoost': cpuBoost
}
}
merge_tdp_profiles(tdp_profiles)
return plugin_utils.set_values_for_game_id(gameId)
async def get_latest_version_num(self):
return plugin_update.get_latest_version()
async def poll_tdp(self, currentGameId: str):
settings = get_saved_settings()
tdp_profile = get_tdp_profile('default')
if settings.get('enableTdpProfiles'):
tdp_profile = get_tdp_profile(currentGameId) or tdp_profile
try:
with file_timeout.time_limit(3):
plugin_utils.set_values_for_tdp_profile(tdp_profile)
except Exception as e:
decky_plugin.logger.error(f'main#poll_tdp file timeout {e}')
return False
return True
async def save_tdp(self, tdpProfiles, currentGameId, advanced):
try:
merge_tdp_profiles(tdpProfiles)
persist_setting('advanced', advanced)
tdp_profile = get_active_tdp_profile(currentGameId)
try:
with file_timeout.time_limit(3):
plugin_utils.set_values_for_tdp_profile(tdp_profile)
except Exception as e:
decky_plugin.logger.error(f'main#save_tdp file timeout {e}')
except Exception as e:
decky_plugin.logger.error(e)
async def set_max_tdp(self):
settings = get_saved_settings()
max_tdp = settings.get('maxTdp')
if (max_tdp and max_tdp > 10):
cpu_utils.set_tdp(max_tdp)
async def ota_update(self):
# trigger ota update
try:
with file_timeout.time_limit(15):
return plugin_update.ota_update()
except Exception as e:
decky_plugin.logger.error(e)
async def supports_custom_ac_power_management(self):
return ac_power.supports_custom_ac_power_management()
async def get_ac_power_status(self):
ac_power_path = ac_power.custom_ac_power_management_path()
if ac_power_path and os.path.exists(ac_power_path):
with open(ac_power_path, 'r') as file:
current_status = file.read().strip()
file.close()
return current_status
return None
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
decky_plugin.logger.info("SimpleDeckyTDP Starting")
# Function called first during the unload process, utilize this to handle your plugin being removed
async def _unload(self):
decky_plugin.logger.info("SimpleDeckyTDP Unloading")
pass
# Migrations that should be performed before entering `_main()`.
async def _migration(self):
decky_plugin.logger.info("Migrating")
# migrations.migrate_smt()
# migrations.disable_steam_patch()
migrations.migrate_gpu_mode()