Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement icon updating upon SIGUSR1 #252

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion streamdeck_ui/gui.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Defines the QT powered interface for configuring Stream Decks"""
import json
import os
import shlex
import sys
import time
from functools import partial
from signal import SIGUSR1, signal
from subprocess import Popen # nosec - Need to allow users to specify arbitrary commands
from typing import Dict, Optional

Expand All @@ -16,7 +18,7 @@
from PySide2.QtWidgets import QAction, QApplication, QDialog, QFileDialog, QMainWindow, QMenu, QMessageBox, QSizePolicy, QSystemTrayIcon

from streamdeck_ui.api import StreamDeckServer
from streamdeck_ui.config import LOGO, STATE_FILE
from streamdeck_ui.config import CONFIG_FILE_VERSION, LOGO, STATE_FILE
from streamdeck_ui.ui_main import Ui_MainWindow
from streamdeck_ui.ui_settings import Ui_SettingsDialog

Expand Down Expand Up @@ -839,6 +841,39 @@ def start(_exit: bool = False) -> None:

api.start()

def reload_on_sigusr1(signal_received, frame):
# not very DRY, api.open_config would require refactoring to be reusable
with open(STATE_FILE) as state_file:
config = json.loads(state_file.read())
file_version = config.get("streamdeck_ui_version", 0)
if file_version != CONFIG_FILE_VERSION:
raise ValueError("Incompatible version of config file found: " f"{file_version} does not match required version " f"{CONFIG_FILE_VERSION}.")

new_state = {}
for deck_id, deck in config["state"].items():
deck["buttons"] = {int(page_id): {int(button_id): button for button_id, button in buttons.items()} for page_id, buttons in deck.get("buttons", {}).items()}
new_state[deck_id] = deck

changed = False
for deck_key, deck_value in api.state.items():
for page_key, page_value in deck_value["buttons"].items():
for button_key, button_value in page_value.items():
if not button_value:
continue
new_button = new_state[deck_key]["buttons"][page_key][button_key]
if str(button_value) == str(new_button):
continue
icon = new_button.get("icon")
if icon:
api.set_button_icon(deck_key, int(page_key), int(button_key), icon)
changed = True

if changed:
api.state = new_state
redraw_buttons(ui)

signal(SIGUSR1, reload_on_sigusr1)

tray.show()
if show_ui:
main_window.show()
Expand Down