From 5d4b2a99de60d100616bd98fb8770901c9997db7 Mon Sep 17 00:00:00 2001 From: innovatedev <64183682+innovatedev@users.noreply.github.com> Date: Sat, 28 May 2022 01:41:34 -0600 Subject: [PATCH] add run when locked option to buttons --- poetry.lock | 13 ++++++++++++- pyproject.toml | 1 + streamdeck_ui/api.py | 10 ++++++++++ streamdeck_ui/gui.py | 25 +++++++++++++++++++++++-- streamdeck_ui/main.ui | 13 ++++++++++++- streamdeck_ui/system_api.py | 16 ++++++++++++++++ streamdeck_ui/ui_main.py | 18 +++++++++++++++--- 7 files changed, 89 insertions(+), 7 deletions(-) create mode 100644 streamdeck_ui/system_api.py diff --git a/poetry.lock b/poetry.lock index 262b53b8..4ac0eec9 100644 --- a/poetry.lock +++ b/poetry.lock @@ -319,6 +319,14 @@ webencodings = "*" doc = ["sphinx", "sphinx-rtd-theme"] test = ["pytest", "pytest-cov", "pytest-flake8", "pytest-isort", "coverage"] +[[package]] +name = "dbus-python" +version = "1.2.18" +description = "Python bindings for libdbus" +category = "main" +optional = false +python-versions = "*" + [[package]] name = "debugpy" version = "1.6.0" @@ -1890,7 +1898,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest- [metadata] lock-version = "1.1" python-versions = ">=3.8,<3.11" -content-hash = "1a0bf29986cbf4d0c101dda4c6119149ecdfa22a4983a40171d5f648830414e2" +content-hash = "d07e93af8953f7e1b62ceb7a0ae31c7db9a8d4078e41a20aa35d4bdc86d1ca28" [metadata.files] appdirs = [ @@ -2098,6 +2106,9 @@ cssselect2 = [ {file = "cssselect2-0.6.0-py3-none-any.whl", hash = "sha256:3a83b2a68370c69c9cd3fcb88bbfaebe9d22edeef2c22d1ff3e1ed9c7fa45ed8"}, {file = "cssselect2-0.6.0.tar.gz", hash = "sha256:5b5d6dea81a5eb0c9ca39f116c8578dd413778060c94c1f51196371618909325"}, ] +dbus-python = [ + {file = "dbus-python-1.2.18.tar.gz", hash = "sha256:92bdd1e68b45596c833307a5ff4b217ee6929a1502f5341bae28fd120acf7260"}, +] debugpy = [ {file = "debugpy-1.6.0-cp310-cp310-macosx_10_15_x86_64.whl", hash = "sha256:eb1946efac0c0c3d411cea0b5ac772fbde744109fd9520fb0c5a51979faf05ad"}, {file = "debugpy-1.6.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl", hash = "sha256:e3513399177dd37af4c1332df52da5da1d0c387e5927dc4c0709e26ee7302e8f"}, diff --git a/pyproject.toml b/pyproject.toml index 78d42687..fce02f83 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,6 +15,7 @@ pyside2 = "^5.15" CairoSVG = "^2.5.2" filetype = "^1.0.10" python-xlib = "^0.31" +dbus-python = "^1.2.18" [tool.poetry.dev-dependencies] vulture = "^1.0" diff --git a/streamdeck_ui/api.py b/streamdeck_ui/api.py index 5ef20fda..26814eae 100644 --- a/streamdeck_ui/api.py +++ b/streamdeck_ui/api.py @@ -363,6 +363,16 @@ def get_button_change_brightness(self, deck_id: str, page: int, button: int) -> """Returns the brightness change set for a particular button""" return self._button_state(deck_id, page, button).get("brightness_change", 0) + def set_button_run_when_locked(self, deck_id: str, page: int, button: int, value: bool) -> None: + """Sets whether the button should run when the system is locked""" + if self.get_button_run_when_locked(deck_id, page, button) != value: + self._button_state(deck_id, page, button)["run_when_locked"] = value + self._save_state() + + def get_button_run_when_locked(self, deck_id: str, page: int, button: int) -> bool: + """Returns whether the button should run when the system is locked""" + return self._button_state(deck_id, page, button).get("run_when_locked", False) + def set_button_command(self, deck_id: str, page: int, button: int, command: str) -> None: """Sets the command associated with the button""" if self.get_button_command(deck_id, page, button) != command: diff --git a/streamdeck_ui/gui.py b/streamdeck_ui/gui.py index 1f281bc5..82d04b90 100644 --- a/streamdeck_ui/gui.py +++ b/streamdeck_ui/gui.py @@ -17,10 +17,12 @@ from streamdeck_ui.api import StreamDeckServer from streamdeck_ui.config import LOGO, STATE_FILE +from streamdeck_ui.system_api import SystemApi from streamdeck_ui.ui_main import Ui_MainWindow from streamdeck_ui.ui_settings import Ui_SettingsDialog api: StreamDeckServer +system_api: SystemApi BUTTON_STYLE = """ QToolButton { @@ -137,12 +139,19 @@ def handle_keypress(ui, deck_id: str, key: int, state: bool) -> None: # TODO: Handle both key down and key up events in future. if state: + page = api.get_page(deck_id) + + run_when_locked = api.get_button_run_when_locked(deck_id, page, key) + is_system_locked = system_api.get_is_system_locked() + + if is_system_locked and not run_when_locked: + print(f"Button not allowed when system is locked") + return if api.reset_dimmer(deck_id): return kb = Controller() - page = api.get_page(deck_id) command = api.get_button_command(deck_id, page, key) if command: @@ -271,6 +280,13 @@ def update_change_brightness(ui, amount: int) -> None: api.set_button_change_brightness(deck_id, _page(ui), selected_button.index, amount) +def update_run_when_locked(ui, value: int) -> None: + if selected_button: + deck_id = _deck_id(ui) + checked = True if value == 2 else False + api.set_button_run_when_locked(deck_id, _page(ui), selected_button.index, checked) + + def update_switch_page(ui, page: int) -> None: if selected_button: deck_id = _deck_id(ui) @@ -396,6 +412,7 @@ def button_clicked(ui, clicked_button, buttons) -> None: ui.keys.setCurrentText(api.get_button_keys(deck_id, _page(ui), button_id)) ui.write.setPlainText(api.get_button_write(deck_id, _page(ui), button_id)) ui.change_brightness.setValue(api.get_button_change_brightness(deck_id, _page(ui), button_id)) + ui.run_when_locked.setChecked(api.get_button_run_when_locked(deck_id, _page(ui), button_id)) ui.switch_page.setValue(api.get_button_switch_page(deck_id, _page(ui), button_id)) api.reset_dimmer(deck_id) else: @@ -409,6 +426,7 @@ def enable_button_configuration(ui, enabled: bool): ui.keys.setEnabled(enabled) ui.write.setEnabled(enabled) ui.change_brightness.setEnabled(enabled) + ui.run_when_locked.setEnabled(enabled) ui.switch_page.setEnabled(enabled) ui.imageButton.setEnabled(enabled) ui.removeButton.setEnabled(enabled) @@ -424,6 +442,7 @@ def reset_button_configuration(ui): ui.keys.clearEditText() ui.write.clear() ui.change_brightness.setValue(0) + ui.run_when_locked.setChecked(False) ui.switch_page.setValue(0) enable_button_configuration(ui, False) @@ -716,6 +735,7 @@ def create_main_window(logo: QIcon, app: QApplication) -> MainWindow: ui.keys.currentTextChanged.connect(partial(update_button_keys, ui)) ui.write.textChanged.connect(partial(update_button_write, ui)) ui.change_brightness.valueChanged.connect(partial(update_change_brightness, ui)) + ui.run_when_locked.stateChanged.connect(partial(update_run_when_locked, ui)) ui.switch_page.valueChanged.connect(partial(update_switch_page, ui)) ui.imageButton.clicked.connect(partial(select_image, main_window)) ui.textButton.clicked.connect(partial(align_text_vertical, main_window)) @@ -797,7 +817,7 @@ def streamdeck_detached(ui, serial_number): def start(_exit: bool = False) -> None: - global api + global api, system_api show_ui = True if "-h" in sys.argv or "--help" in sys.argv: print(f"Usage: {os.path.basename(sys.argv[0])}") @@ -814,6 +834,7 @@ def start(_exit: bool = False) -> None: version = "devel" api = StreamDeckServer() + system_api = SystemApi() if os.path.isfile(STATE_FILE): api.open_config(STATE_FILE) diff --git a/streamdeck_ui/main.ui b/streamdeck_ui/main.ui index 5c0d5556..9ddf8802 100644 --- a/streamdeck_ui/main.ui +++ b/streamdeck_ui/main.ui @@ -428,13 +428,24 @@ + + + Run when locked: + + + + + + + + Write Text: - + diff --git a/streamdeck_ui/system_api.py b/streamdeck_ui/system_api.py new file mode 100644 index 00000000..4467ffdf --- /dev/null +++ b/streamdeck_ui/system_api.py @@ -0,0 +1,16 @@ +import dbus + + +class SystemApi: + + def __init__(self): + self.bus = dbus.SystemBus() + + def get_is_system_locked(self) -> bool: + login1 = self.bus.get_object('org.freedesktop.login1', + '/org/freedesktop/login1/session/auto') + properties_interface = dbus.Interface(login1, 'org.freedesktop.DBus.Properties') + session_properties = properties_interface.GetAll('org.freedesktop.login1.Session') + system_is_locked = session_properties.get('LockedHint') + + return bool(system_is_locked) \ No newline at end of file diff --git a/streamdeck_ui/ui_main.py b/streamdeck_ui/ui_main.py index 3176225c..335dd5e5 100644 --- a/streamdeck_ui/ui_main.py +++ b/streamdeck_ui/ui_main.py @@ -259,15 +259,25 @@ def setupUi(self, MainWindow): self.formLayout.setWidget(5, QFormLayout.FieldRole, self.change_brightness) + self.label_run_when_locked = QLabel(self.groupBox) + self.label_run_when_locked.setObjectName(u"label_run_when_locked") + + self.formLayout.setWidget(6, QFormLayout.LabelRole, self.label_run_when_locked) + + self.run_when_locked = QCheckBox(self.groupBox) + self.run_when_locked.setObjectName(u"run_when_locked") + + self.formLayout.setWidget(6, QFormLayout.FieldRole, self.run_when_locked) + self.label_6 = QLabel(self.groupBox) self.label_6.setObjectName(u"label_6") - self.formLayout.setWidget(6, QFormLayout.LabelRole, self.label_6) + self.formLayout.setWidget(7, QFormLayout.LabelRole, self.label_6) self.write = QPlainTextEdit(self.groupBox) self.write.setObjectName(u"write") - self.formLayout.setWidget(6, QFormLayout.FieldRole, self.write) + self.formLayout.setWidget(7, QFormLayout.FieldRole, self.write) self.horizontalLayout_3 = QHBoxLayout() self.horizontalLayout_3.setObjectName(u"horizontalLayout_3") @@ -323,7 +333,8 @@ def setupUi(self, MainWindow): QWidget.setTabOrder(self.command, self.keys) QWidget.setTabOrder(self.keys, self.switch_page) QWidget.setTabOrder(self.switch_page, self.change_brightness) - QWidget.setTabOrder(self.change_brightness, self.write) + QWidget.setTabOrder(self.change_brightness, self.run_when_locked) + QWidget.setTabOrder(self.run_when_locked, self.write) self.menubar.addAction(self.menuFile.menuAction()) self.menubar.addAction(self.menuHelp.menuAction()) @@ -377,6 +388,7 @@ def retranslateUi(self, MainWindow): self.label_8.setText(QCoreApplication.translate("MainWindow", u"Switch Page:", None)) self.label_7.setText(QCoreApplication.translate("MainWindow", u"Brightness +/-:", None)) + self.label_run_when_locked.setText(QCoreApplication.translate("MainWindow", u"Run When Locked:", None)) self.label_6.setText(QCoreApplication.translate("MainWindow", u"Write Text:", None)) #if QT_CONFIG(tooltip) self.textButton.setToolTip(QCoreApplication.translate("MainWindow", u"Text vertical alignment", None))