#!/usr/bin/env python
import sys
from os import path, sep, makedirs
from subprocess import Popen, PIPE, STDOUT
from configparser import ConfigParser
from re import search, IGNORECASE
from justpy import Div, I, WebPage, SetRoute, justpy
APP_NAME = "ControlDeck"
def process(args, output=True):
try:
# with shell=True args can be a string
# detached process https://stackoverflow.com/a/65900355/992129 start_new_session
# https://docs.python.org/3/library/subprocess.html#popen-constructor
result = Popen(args, stdout=PIPE, stderr=STDOUT, shell=True, start_new_session=True)
if output:
return result.stdout.read().decode("utf-8").rstrip()
except Exception as e:
print(f"{e} failed!")
def volume(name):
result = process(f'pamixer --get-volume --sink "{name}"')
if search("The sink doesn't exit", result):
result = "--"
elif search("pamixer: command not found", result) is not None:
n = process(r"pactl list sinks short | awk '{print $2}'").split()
v = process(r"pactl list sinks | grep '^[[:space:]]Volume:' | sed -e 's,.* \([0-9][0-9]*\)%.*,\1,'").split()
if name in n:
result = v[n.index(name)]
else:
result = '--'
return result
def volume_decrease(name):
result = process(f'pamixer --get-volume --sink "{name}" --decrease 5')
if search("pamixer: command not found", result) is not None:
process(f'pactl set-sink-volume "{name}" -5%')
#process(f'pactl set-sink-volume "{name}" -5db')
result = volume(name)
return result
def volume_increase(name):
result = process(f'pamixer --get-volume --sink "{name}" --increase 5')
if search("pamixer: command not found", result) is not None:
process(f'pactl set-sink-volume "{name}" +5%')
#process(f'pactl set-sink-volume "{name}" +5db')
result = volume(name)
return result
def config_load():
config = ConfigParser(strict=False)
config_file = "controldeck.conf"
full_config_file_path = path.dirname(path.realpath(__file__)) + sep + config_file
if not path.exists(config_file):
config_folder = path.join(path.expanduser("~"), '.config', APP_NAME.lower())
makedirs(config_folder, exist_ok=True)
full_config_file_path = path.join(config_folder, config_file)
try:
config.read(full_config_file_path)
except Exception as e:
print(f"{e}")
#print(config.sections())
return config
class Button(Div):
command = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.classes = "bg-gray-800 hover:bg-gray-700 text-gray-500 w-20 h-20 m-2 p-1 rounded-lg font-bold flex items-center text-center justify-center select-none"
if self.command is not None:
def click(self, msg):
print(self.command)
# string works only with shell
if isinstance(self.command, (list)):
# e.g.: [['pkill', 'ArdourGUI'], ['systemctl', '--user', 'restart', 'pipewire', 'pipewire-pulse'], ['ardour6', '-n', 'productive-pipewire']]
if isinstance(self.command[0], (list)):
[process(i) for i in self.command]
else:
# e.g.: ['pkill', 'ArdourGUI']
process(self.command)
else:
# e.g.: 'pkill ArdourGUI'
process(self.command)
self.on('click', click)
class ButtonSound(Div):
div = None
name = None
description = None
volume = None
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.classes = "grid-rows-2"
self.div = Div(classes="flex")
Button(inner_html=f'{self.description}
- 5%', click=self.decrease, a=self.div)
Button(inner_html=f'{self.description}
+ 5%', click=self.increase, a=self.div)
self.add(self.div)
self.volume = Div(text=f"Volume: {volume(self.name)}%", classes="text-gray-600 text-center -mt-2", a=self)
async def decrease(self, msg):
self.volume.text = f'Volume: {volume_decrease(self.name)}%'
async def increase(self, msg):
self.volume.text = f'Volume: {volume_increase(self.name)}%'
async def reload(self, msg):
await msg.page.reload()
async def reload_all_instances(self, msg):
"Reload all browser tabs that the page is rendered on"
for page in WebPage.instances.values():
if page.page_type == 'main':
await page.reload()
async def kill_gui(self, msg):
await process("pkill controldeck-gui")
@SetRoute('/')
def application(request):
wp = WebPage(title=APP_NAME, body_classes="bg-gray-900")
wp.page_type = 'main'
wp.head_html = ''
# div = Div(classes="flex flex-wrap", a=wp)
# ButtonSound(name="Stream_sink", description="Stream sink", a=div)
# div2 = Div(classes="flex flex-wrap", a=wp)
# Button(text="Sleep", command='systemctl suspend', a=div2)
menu = Div(classes="fixed bottom-0 right-0 p-1 grid grid-col-1 select-none text-gray-500", a=wp)
I(classes="w-10 h-10 w-1 fa-2x fa-fw fas fa-redo-alt", click=reload, a=menu)
if "gui" in request.query_params:
I(classes="w-10 h-10 w-1 fa-2x fa-fw fas fa-window-close", click=kill_gui, a=menu)
config = config_load()
volume_dict = {}
button_dict = {}
for i in config.sections():
iname = None
iname = search("^([0-9]*.?)volume", i, flags=IGNORECASE)
if iname is not None:
id = iname.group(1)[:-1] # remove dot
try:
volume_dict[id] += [{'description': i[iname.end(0)+1:],
'name': config.get(i, 'name', fallback=None)}]
except KeyError:
volume_dict[id] = [{'description': i[iname.end(0)+1:],
'name': config.get(i, 'name', fallback=None)}]
iname = search("^([0-9]*.?)button", i, flags=IGNORECASE)
if iname is not None:
id = iname.group(1)[:-1] # remove dot
try:
button_dict[id] += [{'text': i[iname.end(0)+1:],
'command': config.get(i, 'command', fallback=None),
'icon': config.get(i, 'icon', fallback=None)}]
except KeyError:
button_dict[id] = [{'text': i[iname.end(0)+1:],
'command': config.get(i, 'command', fallback=None),
'icon': config.get(i, 'icon', fallback=None)}]
var_prefix = "_div"
for i in volume_dict:
var = var_prefix+i
for j in volume_dict[i]:
if var not in vars():
vars()[var] = Div(classes="flex flex-wrap", a=wp)
ButtonSound(name=j['name'], description=j['description'], a=eval(var))
for i in button_dict:
var = var_prefix+i
for j in button_dict[i]:
if var not in vars():
vars()[var] = Div(classes="flex flex-wrap", a=wp)
if j['icon'] is not None:
Button(inner_html=f"", command=j['command'], a=eval(var))
else:
Button(text=j['text'], command=j['command'], a=eval(var))
if not wp.components:
# config not found or empty, therefore insert an empty div to not get an error
Div(text="add elements in controldeck.conf", classes="flex flex-wrap", a=wp)
return wp
def main():
justpy(host="0.0.0.0")
if __name__ == '__main__':
sys.exit(main())