-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
296 lines (233 loc) · 11.2 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
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
import os
import subprocess
from settings import SettingsManager
from env import env
from jdspproxy import JdspProxy
from utils import compare_versions, flatpak_CMD, get_xauthority
# The decky plugin module is located at decky-loader/plugin
# For easy intellisense checkout the decky-loader code one directory up
# or add the `decky-loader/plugin` path to `python.analysis.extraPaths` in `.vscode/settings.json`
import decky
APPLICATION_ID = "me.timschneeberger.jdsp4linux"
JDSP_LOG_DIR = os.path.join(decky.DECKY_PLUGIN_LOG_DIR, 'jdsp')
JDSP_LOG = os.path.join(JDSP_LOG_DIR, 'jdsp.log')
JDSP_MIN_VER = '2.7.0'
log = decky.logger
settings_manager = SettingsManager(name="settings", settings_directory=os.environ["DECKY_PLUGIN_SETTINGS_DIR"])
default_plugin_settings = {
'enableInDesktop': False
}
class Plugin:
jdsp: JdspProxy = None
jdsp_install_state = False
profiles = {
'currentPreset': '',
'manualPreset': '',
'useManual': False,
'watchedApps': {}
}
# Asyncio-compatible long-running code, executed in a task when the plugin is loaded
async def _main(self):
log.info('Starting plugin backend...')
if not os.path.exists(JDSP_LOG_DIR):
os.makedirs(JDSP_LOG_DIR)
Plugin.load_settings()
Plugin.jdsp = JdspProxy(APPLICATION_ID, log)
if(Plugin.handle_jdsp_install()):
Plugin.jdsp_install_state = True
log.info('Plugin ready')
else:
log.error('Problem with JamesDSP installation')
async def _uninstall(self):
log.info('Uninstalling plugin...')
flatpak_CMD(['kill', APPLICATION_ID], noCheck=True)
try:
log.info('Uninstalling JamesDSP...')
flatpak_CMD(['--user', '-y', 'uninstall', APPLICATION_ID])
log.info("JamesDSP uninstalled sucessfully")
except subprocess.CalledProcessError as e:
log.error('Problem uninstalling JamesDSP')
log.error(e.stderr)
async def _unload(self):
log.info('Unloading plugin...')
flatpak_CMD(['kill', APPLICATION_ID], noCheck=True)
def load_settings():
default_profiles_settings = {setting: Plugin.profiles[setting] for setting in Plugin.profiles.keys() - { 'currentPreset' }}
profiles = settings_manager.getSetting('profiles', default_profiles_settings)
Plugin.profiles.update(profiles)
def save_profile_settings():
settings_manager.setSetting('profiles', {setting: Plugin.profiles[setting] for setting in Plugin.profiles.keys() - { 'currentPreset' }})
def handle_jdsp_install():
log.info('Checking for JamesDSP installation...')
try:
flatpakListRes = flatpak_CMD(['list', '--user', '--app', '--columns=application,version'])
installed_version = ''
for line in flatpakListRes.stdout.split('\n'):
if line.startswith(APPLICATION_ID):
installed_version = line.split()[1]
if installed_version != '':
log.info(f'JamesDSP version {installed_version} is installed')
if compare_versions(installed_version, JDSP_MIN_VER) >= 0: return True
else: log.info(f'Minimum version is {JDSP_MIN_VER}')
else:
log.info('No JamesDSP installation was found')
log.info('Installing JamesDSP flatpak...')
installRes = flatpak_CMD(['--user', '-y', 'install', 'flathub', APPLICATION_ID])
log.info(installRes.stdout)
log.info(f'Updating JamesDSP..')
updateRes = flatpak_CMD(['--user', '-y', 'update', APPLICATION_ID])
log.info(updateRes.stdout)
log.info(f'JamesDSP updated')
return True
except subprocess.CalledProcessError as e:
log.error(e.stderr)
return False
"""
===================================================================================================================================
FRONTEND CALLED METHODS
===================================================================================================================================
------------------------------------------
General plugin methods
------------------------------------------
return any value or dictionary with an 'error' property
Annotation: 'general-frontend-call'
"""
# general-frontend-call
async def start_jdsp(self):
if not Plugin.jdsp_install_state:
return False
log.info(f'Starting JamesDSP... See process logs at {JDSP_LOG}')
xauth = get_xauthority()
new_env = env.copy()
# run without gui
# new_env['QT_QPA_PLATFORM'] = 'offscreen'
if xauth: new_env['XAUTHORITY'] = xauth
flatpak_CMD(['kill', APPLICATION_ID], noCheck=True)
with open(JDSP_LOG, "w") as jdsp_log:
subprocess.Popen(f'flatpak --user run {APPLICATION_ID} --tray', stdout=jdsp_log, stderr=jdsp_log, shell=True, env=new_env, universal_newlines=True)
return True # assume process has started ignoring errors so that the frontend doesn't hang. the jdsp process errors will be logged in its own file
# general-frontend-call
async def kill_jdsp(self):
log.info('Killing JamesDSP')
flatpak_CMD(['kill', APPLICATION_ID], noCheck=True)
# general-frontend-call
async def get_settings(self):
settings = {}
for setting in default_plugin_settings:
settings[setting] = settings_manager.getSetting(setting, default_plugin_settings[setting])
return settings
# general-frontend-call
async def set_setting(self, setting, value):
settings_manager.setSetting(setting, value)
# general-frontend-call
async def flatpak_repair(self):
try:
flatpak_CMD(['repair', '--user'])
return
except subprocess.CalledProcessError as e:
log.error(e.stderr)
return { 'error': e.stderr }
# general-frontend-call
async def set_app_watch(self, appId, watch):
Plugin.profiles['watchedApps'][appId] = watch
Plugin.save_profile_settings()
# general-frontend-call
async def init_profiles(self, globalPreset):
if Plugin.profiles['manualPreset'] == '': # manual preset should be set from first loading settings file, if not then file doesn't exist yet
Plugin.profiles['manualPreset'] = globalPreset
log.info('No settings file detected. Creating one.')
Plugin.save_profile_settings()
presets = Plugin.jdsp.get_presets()
if JdspProxy.has_error(presets):
return { 'error': str(presets) }
return {
'manualPreset': Plugin.profiles['manualPreset'],
'allPresets': presets['jdsp_result'],
'watchedGames': Plugin.profiles['watchedApps'],
'manuallyApply': Plugin.profiles['useManual']
}
# general-frontend-call
async def set_manually_apply_profiles(self, useManual):
Plugin.profiles['useManual'] = useManual
"""
------------------------------------------
JDSP specific methods
------------------------------------------
return a JamesDSP cli result in a dictionary in the format of either...
{ 'jdsp_result': str } or { 'jdsp_error': str }
Annotation: 'jdsp-frontend-call'
"""
# jdsp-frontend-call
async def set_jdsp_param(self, parameter, value):
res = Plugin.jdsp.set_and_commit(parameter, value)
if not JdspProxy.has_error(res):
Plugin.jdsp.save_preset(Plugin.profiles['currentPreset'])
return res
async def set_jdsp_params(self, values):
for parameter, value in values:
res = Plugin.jdsp.set_and_commit(parameter, value)
if JdspProxy.has_error(res):
return res
Plugin.jdsp.save_preset(Plugin.profiles['currentPreset'])
return {'jdsp_result': ''}
# jdsp-frontend-call
async def get_all_jdsp_param(self):
return Plugin.jdsp.get_all()
# jdsp-frontend-call
async def set_jdsp_defaults(self, defaultPreset):
loadres = Plugin.jdsp.load_preset(defaultPreset) # load the default preset settings
if JdspProxy.has_error(loadres): return loadres
saveres = Plugin.jdsp.save_preset(Plugin.profiles['currentPreset']) # save the current preset with current settings
if JdspProxy.has_error(saveres): return saveres
return Plugin.jdsp.get_all()
# jdsp-frontend-call
async def new_jdsp_preset(self, presetName, fromPresetName = None):
if fromPresetName is not None:
loadres = Plugin.jdsp.load_preset(fromPresetName) # load preset settings to copy
if JdspProxy.has_error(loadres): return loadres
saveres = Plugin.jdsp.save_preset(presetName) # save the new preset with current settings
if JdspProxy.has_error(saveres): return saveres
return Plugin.jdsp.load_preset(Plugin.profiles['currentPreset']) # reload the previous preset settings
else:
return Plugin.jdsp.save_preset(presetName)
# jdsp-frontend-call
async def delete_jdsp_preset(self, presetName):
return Plugin.jdsp.delete_preset(presetName)
# jdsp-frontend-call
async def set_profile(self, presetName, isManual):
if isManual:
Plugin.profiles['manualPreset'] = presetName
res = Plugin.jdsp.load_preset(presetName)
if JdspProxy.has_error(res): return res
Plugin.profiles['currentPreset'] = presetName
Plugin.save_profile_settings()
return Plugin.jdsp.get_all()
async def create_default_jdsp_preset(self, defaultName):
config_dir = os.path.expanduser(f'~/.var/app/{APPLICATION_ID}/config/jamesdsp/')
if not os.path.exists(config_dir):
log.info('Creating default preset: audio.conf directory was dot detected')
return Plugin.jdsp.save_preset(defaultName)
conf_file = os.path.join(config_dir, 'audio.conf')
temp_conf = os.path.join(config_dir, 'audio_old.conf')
if os.path.exists(conf_file):
try:
os.rename(conf_file, temp_conf)
await Plugin.start_jdsp(self)
log.info('Creating default preset: Existing audio.conf detected')
Plugin.jdsp.save_preset(defaultName)
if os.path.exists(conf_file):
os.remove(conf_file)
os.rename(temp_conf, conf_file)
await Plugin.start_jdsp(self)
return { 'jdsp_result': ''}
except Exception as e:
log.error('Error trying to create default preset when audio.conf already existed')
if isinstance(e, subprocess.CalledProcessError):
log.error(e.stderr)
raise e
else:
log.info('Creating default preset: No existing audio.conf was detected')
return Plugin.jdsp.save_preset(defaultName)
"""
===================================================================================================================================
"""