2023-08-22 02:52:03 +01:00
|
|
|
import logging
|
|
|
|
|
2023-08-22 02:04:00 +01:00
|
|
|
import PySimpleGUI as psg
|
|
|
|
|
2023-08-22 16:11:24 +01:00
|
|
|
from .builder import Builder
|
2023-08-22 20:41:44 +01:00
|
|
|
from .models import _make_cache, _patch_insert_channels
|
2023-08-22 02:04:00 +01:00
|
|
|
from .nvda import Nvda
|
|
|
|
from .parser import Parser
|
|
|
|
|
2023-08-22 02:52:03 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2023-08-22 02:04:00 +01:00
|
|
|
|
|
|
|
class Window(psg.Window):
|
2023-08-22 16:11:24 +01:00
|
|
|
"""Represents the main window of the application"""
|
|
|
|
|
2023-08-22 02:04:00 +01:00
|
|
|
def __init__(self, title, vm):
|
|
|
|
self.vm = vm
|
|
|
|
self.kind = self.vm.kind
|
2023-08-22 02:52:03 +01:00
|
|
|
self.logger = logger.getChild(type(self).__name__)
|
2023-08-22 02:04:00 +01:00
|
|
|
self.cache = _make_cache(self.vm)
|
|
|
|
self.nvda = Nvda()
|
|
|
|
self.parser = Parser()
|
2023-08-22 16:11:24 +01:00
|
|
|
self.builder = Builder(self, self.vm)
|
|
|
|
layout = self.builder.run()
|
|
|
|
super().__init__(title, layout, finalize=True)
|
2023-08-22 02:52:03 +01:00
|
|
|
self.register_events()
|
2023-08-22 02:04:00 +01:00
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __exit__(self, exc_type, exc_value, traceback):
|
|
|
|
self.close()
|
|
|
|
|
2023-08-22 02:52:03 +01:00
|
|
|
def register_events(self):
|
|
|
|
for i in range(1, self.vm.kind.phys_out + 1):
|
|
|
|
self[f"HARDWARE OUT||A{i}"].bind("<FocusIn>", "||FOCUS IN")
|
2023-08-22 18:13:26 +01:00
|
|
|
for i in range(1, self.kind.phys_out + 1):
|
2023-08-22 05:16:43 +01:00
|
|
|
self[f"ASIO CHECKBOX||IN{i} 0"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"ASIO CHECKBOX||IN{i} 1"].bind("<FocusIn>", "||FOCUS IN")
|
2023-08-22 18:13:26 +01:00
|
|
|
for i in range(1, self.kind.num_strip + 1):
|
|
|
|
if i <= self.kind.phys_in:
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 0"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 1"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
else:
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 0"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 1"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 2"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 3"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 4"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 5"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 6"].bind("<FocusIn>", "||FOCUS IN")
|
|
|
|
self[f"INSERT CHECKBOX||IN{i} 7"].bind("<FocusIn>", "||FOCUS IN")
|
2023-08-22 02:52:03 +01:00
|
|
|
|
2023-08-22 02:04:00 +01:00
|
|
|
def run(self):
|
2023-08-22 05:16:43 +01:00
|
|
|
"""
|
|
|
|
Parses the event string and matches it to events
|
|
|
|
|
|
|
|
Main thread will shutdown once a close or exit event occurs
|
|
|
|
"""
|
2023-08-22 02:04:00 +01:00
|
|
|
while True:
|
|
|
|
event, values = self.read()
|
|
|
|
if event in (psg.WIN_CLOSED, "Exit"):
|
|
|
|
break
|
|
|
|
match self.parser.match.parseString(event):
|
2023-08-22 02:52:03 +01:00
|
|
|
case [["HARDWARE", "OUT"], [key]]:
|
|
|
|
selection = values[f"HARDWARE OUT||{key}"]
|
2023-08-22 05:37:06 +01:00
|
|
|
match selection.split(":"):
|
|
|
|
case [device_name]:
|
|
|
|
device_name = ""
|
|
|
|
self.nvda.speak(f"HARDWARE OUT {key} device deselected")
|
|
|
|
case [driver, device_name]:
|
|
|
|
index = int(key[1]) - 1
|
|
|
|
setattr(self.vm.bus[index].device, driver, device_name.strip())
|
|
|
|
self.nvda.speak(f"{driver} {selection}")
|
2023-08-22 02:52:03 +01:00
|
|
|
case [["HARDWARE", "OUT"], [key], ["FOCUS", "IN"]]:
|
|
|
|
self.nvda.speak(f"HARDWARE OUT {key} in focus")
|
2023-08-22 20:41:44 +01:00
|
|
|
case [["ASIO", "CHECKBOX"], [in_num, channel]]:
|
|
|
|
if int(channel) == 0:
|
2023-08-22 05:16:43 +01:00
|
|
|
index = (2 * int(in_num[-1])) - 2
|
|
|
|
else:
|
|
|
|
index = 2 * int(in_num[-1]) - 1
|
2023-08-22 20:41:44 +01:00
|
|
|
val = values[f"ASIO CHECKBOX||{in_num} {channel}"]
|
2023-08-22 05:16:43 +01:00
|
|
|
self.vm.patch.asio[index].set(val)
|
2023-08-22 20:41:44 +01:00
|
|
|
channel = ("left", "right")[int(channel)]
|
|
|
|
self.nvda.speak(f"Patch ASIO {in_num} {channel} set to {val}")
|
|
|
|
case [["ASIO", "CHECKBOX"], [in_num, channel], ["FOCUS", "IN"]]:
|
|
|
|
channel = ("left", "right")[int(channel)]
|
2023-08-22 05:16:43 +01:00
|
|
|
num = int(in_num[-1])
|
2023-08-22 20:41:44 +01:00
|
|
|
self.nvda.speak(f"Patch ASIO inputs to strips IN#{num} {channel} in focus")
|
|
|
|
case [["INSERT", "CHECKBOX"], [in_num, channel]]:
|
|
|
|
num = int(in_num[-1])
|
|
|
|
if num <= self.kind.phys_in:
|
|
|
|
if int(channel) == 0:
|
|
|
|
index = (2 * int(in_num[-1])) - 2
|
|
|
|
else:
|
|
|
|
index = 2 * int(in_num[-1]) - 1
|
|
|
|
else:
|
|
|
|
index = (2 * self.kind.phys_in) + (8 * (int(in_num[-1]) - self.kind.phys_in - 1)) + int(channel)
|
|
|
|
val = values[f"INSERT CHECKBOX||{in_num} {channel}"]
|
|
|
|
self.vm.patch.insert[index].on = val
|
|
|
|
self.nvda.speak(
|
|
|
|
f"PATCH INSERT {in_num} {_patch_insert_channels[int(channel)]} set to {'on' if val else 'off'}"
|
|
|
|
)
|
|
|
|
case [["INSERT", "CHECKBOX"], [in_num, channel], ["FOCUS", "IN"]]:
|
|
|
|
channel = _patch_insert_channels[int(channel)]
|
|
|
|
num = int(in_num[-1])
|
|
|
|
self.nvda.speak(f"Patch INSERT IN#{num} {channel} in focus")
|
2023-08-22 02:04:00 +01:00
|
|
|
case _:
|
2023-08-22 05:16:43 +01:00
|
|
|
self.logger.error(f"Unknown event {event}")
|
2023-08-22 02:52:03 +01:00
|
|
|
self.logger.debug(self.parser.match.parseString(event))
|
2023-08-22 02:04:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
def request_window_object(title, vm):
|
|
|
|
WINDOW_cls = Window
|
|
|
|
return WINDOW_cls(title, vm)
|