2020-12-24 09:47:00 -05:00
|
|
|
# SPDX-License-Identifier: GPL-2.0-or-later
|
|
|
|
|
import sys
|
2021-01-09 14:55:31 -05:00
|
|
|
import os
|
2020-12-24 09:47:00 -05:00
|
|
|
|
|
|
|
|
from PyQt5 import QtCore
|
|
|
|
|
from PyQt5.QtCore import pyqtSignal, QProcess
|
|
|
|
|
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QPushButton, QApplication
|
|
|
|
|
from fbs_runtime.application_context import is_frozen
|
|
|
|
|
|
2020-12-26 04:47:01 -05:00
|
|
|
from keycodes import Keycode
|
2020-12-24 09:47:00 -05:00
|
|
|
from macro_key import KeyUp, KeyDown
|
|
|
|
|
from util import tr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LinuxRecorder(QWidget):
|
|
|
|
|
|
|
|
|
|
keystroke = pyqtSignal(object)
|
|
|
|
|
stopped = pyqtSignal()
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
|
|
self.process = QProcess()
|
2020-12-24 23:39:04 -05:00
|
|
|
self.process.readyReadStandardOutput.connect(self.on_output)
|
2020-12-24 09:47:00 -05:00
|
|
|
|
|
|
|
|
self.setWindowFlags(self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint | QtCore.Qt.X11BypassWindowManagerHint)
|
|
|
|
|
|
|
|
|
|
layout = QVBoxLayout()
|
|
|
|
|
btn = QPushButton(tr("MacroRecorder", "Stop recording"))
|
|
|
|
|
btn.clicked.connect(self.on_stop)
|
|
|
|
|
layout.addWidget(btn)
|
|
|
|
|
|
|
|
|
|
self.setLayout(layout)
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
self.show()
|
|
|
|
|
|
|
|
|
|
center = QApplication.desktop().availableGeometry(self).center()
|
|
|
|
|
self.move(center.x() - self.width() * 0.5, 0)
|
|
|
|
|
|
|
|
|
|
args = [sys.executable]
|
2021-01-09 14:55:31 -05:00
|
|
|
if os.getenv("APPIMAGE"):
|
|
|
|
|
args = [os.getenv("APPIMAGE")]
|
|
|
|
|
elif is_frozen():
|
2020-12-24 09:47:00 -05:00
|
|
|
args += sys.argv[1:]
|
|
|
|
|
else:
|
|
|
|
|
args += sys.argv
|
|
|
|
|
args += ["--linux-recorder"]
|
|
|
|
|
|
|
|
|
|
self.process.start("pkexec", args, QProcess.Unbuffered | QProcess.ReadWrite)
|
|
|
|
|
|
|
|
|
|
def on_stop(self):
|
2020-12-24 09:48:11 -05:00
|
|
|
self.stop()
|
|
|
|
|
|
|
|
|
|
def stop(self):
|
2020-12-24 09:47:00 -05:00
|
|
|
self.process.write(b"q")
|
|
|
|
|
self.process.waitForFinished()
|
|
|
|
|
self.process.close()
|
|
|
|
|
self.hide()
|
|
|
|
|
self.stopped.emit()
|
|
|
|
|
|
|
|
|
|
def on_output(self):
|
|
|
|
|
if self.process.canReadLine():
|
|
|
|
|
line = bytes(self.process.readLine()).decode("utf-8")
|
|
|
|
|
action, key = line.strip().split(":")
|
2020-12-26 04:47:01 -05:00
|
|
|
code = Keycode.find_by_recorder_alias(key)
|
2020-12-24 09:47:00 -05:00
|
|
|
if code is not None:
|
2020-12-26 04:47:01 -05:00
|
|
|
action2cls = {"down": KeyDown, "up": KeyUp}
|
|
|
|
|
self.keystroke.emit(action2cls[action](code))
|