From 000ad5d55c85ba9a73f11a65f97a8638eecd8440 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Mon, 21 Dec 2020 21:52:56 -0500 Subject: [PATCH] macro_recorder: implement process elevation on linux --- src/main/python/linux_keystroke_recorder.py | 2 + src/main/python/macro_recorder.py | 51 +++++++++++++++++++++ src/main/python/main.py | 18 +++++--- src/main/python/main_window.py | 6 ++- 4 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 src/main/python/linux_keystroke_recorder.py create mode 100644 src/main/python/macro_recorder.py diff --git a/src/main/python/linux_keystroke_recorder.py b/src/main/python/linux_keystroke_recorder.py new file mode 100644 index 0000000..caaef57 --- /dev/null +++ b/src/main/python/linux_keystroke_recorder.py @@ -0,0 +1,2 @@ +def linux_keystroke_recorder(): + print("Recording") diff --git a/src/main/python/macro_recorder.py b/src/main/python/macro_recorder.py new file mode 100644 index 0000000..df96b8a --- /dev/null +++ b/src/main/python/macro_recorder.py @@ -0,0 +1,51 @@ +import sys + +from PyQt5.QtCore import QProcess +from PyQt5.QtWidgets import QPushButton, QWidget +from fbs_runtime.application_context import is_frozen + +from basic_editor import BasicEditor +from vial_device import VialKeyboard + + +class LinuxRecorder: + + def __init__(self): + self.process = None + + def start(self): + self.process = QProcess() + args = [sys.executable] + if is_frozen(): + args += sys.argv[1:] + else: + args += sys.argv + args += ["--linux-recorder"] + + self.process.start("pkexec", args) + self.process.waitForFinished() + print(self.process.readAll()) + + +class MacroRecorder(BasicEditor): + + def __init__(self): + super().__init__() + + self.recorder = LinuxRecorder() + self.recording = False + + btn = QPushButton("Record") + btn.clicked.connect(self.on_record_clicked) + self.addWidget(btn) + + def valid(self): + return isinstance(self.device, VialKeyboard) + + def rebuild(self, device): + super().rebuild(device) + if not self.valid(): + return + + def on_record_clicked(self): + self.recorder.start() diff --git a/src/main/python/main.py b/src/main/python/main.py index b80758d..35fc343 100644 --- a/src/main/python/main.py +++ b/src/main/python/main.py @@ -7,6 +7,7 @@ from fbs_runtime.application_context.PyQt5 import ApplicationContext import sys +from linux_keystroke_recorder import linux_keystroke_recorder from main_window import MainWindow from constants import WINDOW_WIDTH, WINDOW_HEIGHT @@ -46,10 +47,13 @@ class UncaughtHook(QtCore.QObject): if __name__ == '__main__': - appctxt = ApplicationContext() # 1. Instantiate ApplicationContext - qt_exception_hook = UncaughtHook() - window = MainWindow() - window.resize(WINDOW_WIDTH, WINDOW_HEIGHT) - window.show() - exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_() - sys.exit(exit_code) + if len(sys.argv) == 2 and sys.argv[1] == "--linux-recorder": + linux_keystroke_recorder() + else: + appctxt = ApplicationContext() # 1. Instantiate ApplicationContext + qt_exception_hook = UncaughtHook() + window = MainWindow() + window.resize(WINDOW_WIDTH, WINDOW_HEIGHT) + window.show() + exit_code = appctxt.app.exec_() # 2. Invoke appctxt.app.exec_() + sys.exit(exit_code) diff --git a/src/main/python/main_window.py b/src/main/python/main_window.py index 0b33be0..fe0fd12 100644 --- a/src/main/python/main_window.py +++ b/src/main/python/main_window.py @@ -9,6 +9,7 @@ import json from firmware_flasher import FirmwareFlasher from keymap_editor import KeymapEditor from layout_editor import LayoutEditor +from macro_recorder import MacroRecorder from util import tr, find_vial_devices @@ -36,8 +37,9 @@ class MainWindow(QMainWindow): self.layout_editor = LayoutEditor() self.keymap_editor = KeymapEditor(self.layout_editor) self.firmware_flasher = FirmwareFlasher(self) + self.macro_recorder = MacroRecorder() - self.editors = [(self.keymap_editor, "Keymap"), (self.layout_editor, "Layout"), + self.editors = [(self.keymap_editor, "Keymap"), (self.layout_editor, "Layout"), (self.macro_recorder, "Macros"), (self.firmware_flasher, "Firmware updater")] self.tabs = QTabWidget() @@ -116,7 +118,7 @@ class MainWindow(QMainWindow): if self.current_device is not None: self.current_device.open(self.sideload_json if self.current_device.sideload else None) - for e in [self.layout_editor, self.keymap_editor, self.firmware_flasher]: + for e in [self.layout_editor, self.keymap_editor, self.firmware_flasher, self.macro_recorder]: e.rebuild(self.current_device) self.refresh_tabs()