66 lines
2.7 KiB
Python
66 lines
2.7 KiB
Python
|
import re
|
||
|
import subprocess
|
||
|
from configparser import ConfigParser
|
||
|
from pathlib import Path
|
||
|
|
||
|
import dearpygui.dearpygui as dpg
|
||
|
|
||
|
from vrutil.utils import write_as_sudo
|
||
|
|
||
|
_PULSE_DAEMON_CONF_FILE = '/etc/pulse/daemon.conf'
|
||
|
_DEFAULT_SAMPLE_RATE_KEY = 'default-sample-rate'
|
||
|
_DEFAULT_SAMPLE_RATE = '48000'
|
||
|
|
||
|
|
||
|
class ValeIndexSettings:
|
||
|
"""Valve Index related settings"""
|
||
|
|
||
|
def __init__(self):
|
||
|
self._default_sample_rate_value = None
|
||
|
self._default_sample_rate_button = None
|
||
|
|
||
|
def create_views(self):
|
||
|
with dpg.collapsing_header(label='Valve Index', default_open=True):
|
||
|
with dpg.tree_node(label='Pulse Audio'):
|
||
|
if Path(_PULSE_DAEMON_CONF_FILE).exists():
|
||
|
with dpg.group(horizontal=True):
|
||
|
dpg.add_text('%s =' % _DEFAULT_SAMPLE_RATE_KEY)
|
||
|
self._default_sample_rate_value = dpg.add_text(self._get_default_sample_rate())
|
||
|
self._default_sample_rate_button = dpg.add_button(
|
||
|
label=f'Set to `{_DEFAULT_SAMPLE_RATE}`',
|
||
|
show=self._can_update_sample_rate(),
|
||
|
callback=self._set_default_sample_rate,
|
||
|
)
|
||
|
else:
|
||
|
dpg.add_text(f'"{_PULSE_DAEMON_CONF_FILE}" not found')
|
||
|
|
||
|
def reload(self):
|
||
|
dpg.set_value(self._default_sample_rate_value, self._get_default_sample_rate())
|
||
|
dpg.configure_item(self._default_sample_rate_button, show=self._can_update_sample_rate())
|
||
|
|
||
|
def _can_update_sample_rate(self):
|
||
|
return self._get_default_sample_rate() != _DEFAULT_SAMPLE_RATE
|
||
|
|
||
|
@staticmethod
|
||
|
def _get_default_sample_rate():
|
||
|
config = ConfigParser()
|
||
|
with open(_PULSE_DAEMON_CONF_FILE, 'r') as f:
|
||
|
config.read_string(f'[root]\n{f.read()}')
|
||
|
if config.has_option('root', _DEFAULT_SAMPLE_RATE_KEY):
|
||
|
return config.get('root', _DEFAULT_SAMPLE_RATE_KEY)
|
||
|
return None
|
||
|
|
||
|
def _set_default_sample_rate(self):
|
||
|
with open(_PULSE_DAEMON_CONF_FILE, 'r') as f:
|
||
|
config = f.read()
|
||
|
if self._get_default_sample_rate() is None:
|
||
|
# If the option is not yet included in the config file, we append it at the bottom.
|
||
|
new_config = f'{config}\n{_DEFAULT_SAMPLE_RATE_KEY} = {_DEFAULT_SAMPLE_RATE}'
|
||
|
else:
|
||
|
# If the option is already included, we need to find and replace it.
|
||
|
rex = re.compile(f'^[ \\t]*{_DEFAULT_SAMPLE_RATE_KEY}[ \\t]*=[ \\t]*\\d+[ \\t]*$', flags=re.MULTILINE)
|
||
|
new_config = re.sub(rex, f'{_DEFAULT_SAMPLE_RATE_KEY} = {_DEFAULT_SAMPLE_RATE}', config)
|
||
|
write_as_sudo(_PULSE_DAEMON_CONF_FILE, new_config)
|
||
|
subprocess.call(['pulseaudio', '-k'])
|
||
|
self.reload()
|