unlocker: refactor to base on QDialog

main
Ilya Zhuravlev 2021-02-02 09:49:43 -05:00
parent 3fd6291685
commit 2a0a7b17ce
5 changed files with 42 additions and 47 deletions

View File

@ -206,7 +206,7 @@ class FirmwareFlasher(BasicEditor):
# keep track of which keyboard we should restore saved layout to
self.uid_restore = self.device.keyboard.get_uid()
Unlocker.get().perform_unlock(self.device.keyboard)
Unlocker.unlock(self.device.keyboard)
self.log("Restarting in bootloader mode...")
self.device.keyboard.reset()

View File

@ -210,7 +210,7 @@ class Keyboard:
key = (layer, row, col)
if self.layout[key] != code:
if code == RESET_KEYCODE:
Unlocker.get().perform_unlock(self)
Unlocker.unlock(self)
self.usb_send(self.dev, struct.pack(">BBBBH", CMD_VIA_SET_KEYCODE, layer, row, col, code), retries=20)
self.layout[key] = code
@ -222,7 +222,7 @@ class Keyboard:
key = (layer, index, direction)
if self.encoder_layout[key] != code:
if code == RESET_KEYCODE:
Unlocker.get().perform_unlock(self)
Unlocker.unlock(self)
self.usb_send(self.dev, struct.pack(">BBBBBH", CMD_VIA_VIAL_PREFIX, CMD_VIAL_SET_ENCODER,
layer, index, direction, code), retries=20)
@ -302,7 +302,7 @@ class Keyboard:
# only do that if it's different from current macros
macro = base64.b64decode(data["macro"])
if macro != self.macro:
Unlocker.get().perform_unlock(self)
Unlocker.unlock(self)
self.set_macro(macro)
self.lock()

View File

@ -330,6 +330,6 @@ class MacroRecorder(BasicEditor):
self.deserialize(self.keyboard.macro)
def on_save(self):
Unlocker.get().perform_unlock(self.device.keyboard)
Unlocker.unlock(self.device.keyboard)
self.keyboard.set_macro(self.serialize())
self.on_change()

View File

@ -54,7 +54,7 @@ class MainWindow(QMainWindow):
self.editors = [(self.keymap_editor, "Keymap"), (self.layout_editor, "Layout"), (self.macro_recorder, "Macros"),
(self.firmware_flasher, "Firmware updater")]
self.unlocker = Unlocker(self.layout_editor)
Unlocker.global_layout_editor = self.layout_editor
self.tabs = QTabWidget()
self.refresh_tabs()
@ -221,7 +221,7 @@ class MainWindow(QMainWindow):
# if unlock process was interrupted, we must finish it first
if isinstance(self.current_device, VialKeyboard) and self.current_device.keyboard.get_unlock_in_progress():
Unlocker.get().perform_unlock(self.current_device.keyboard)
Unlocker.unlock(self.current_device.keyboard)
self.current_device.keyboard.reload()
for e in [self.layout_editor, self.keymap_editor, self.firmware_flasher, self.macro_recorder]:
@ -282,7 +282,7 @@ class MainWindow(QMainWindow):
def unlock_keyboard(self):
if isinstance(self.current_device, VialKeyboard):
self.unlocker.perform_unlock(self.current_device.keyboard)
Unlocker.unlock(self.current_device.keyboard)
def lock_keyboard(self):
if isinstance(self.current_device, VialKeyboard):
@ -290,7 +290,7 @@ class MainWindow(QMainWindow):
def reboot_to_bootloader(self):
if isinstance(self.current_device, VialKeyboard):
self.unlocker.perform_unlock(self.current_device.keyboard)
Unlocker.unlock(self.current_device.keyboard)
self.current_device.keyboard.reset()
def change_keyboard_layout(self, index):

View File

@ -1,18 +1,19 @@
# SPDX-License-Identifier: GPL-2.0-or-later
import time
from PyQt5.QtCore import QCoreApplication, Qt
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QProgressBar
from PyQt5.QtCore import QCoreApplication, Qt, QTimer
from PyQt5.QtWidgets import QVBoxLayout, QLabel, QProgressBar, QDialog, QDialogButtonBox
from keyboard_widget import KeyboardWidget
from util import tr
class Unlocker(QWidget):
class Unlocker(QDialog):
def __init__(self, layout_editor):
def __init__(self, layout_editor, keyboard):
super().__init__()
self.keyboard = None
self.keyboard = keyboard
layout = QVBoxLayout()
@ -36,19 +37,18 @@ class Unlocker(QWidget):
self.setLayout(layout)
self.setWindowFlags(Qt.Dialog | Qt.WindowTitleHint | Qt.CustomizeWindowHint)
Unlocker.obj = self
self.update_reference()
self.timer = QTimer()
self.timer.timeout.connect(self.unlock_poller)
self.perform_unlock()
@classmethod
def get(cls):
return cls.obj
def update_reference(self, keyboard):
def update_reference(self):
""" Updates keycap reference image """
self.keyboard_reference.set_keys(keyboard.keys, keyboard.encoders)
self.keyboard_reference.set_keys(self.keyboard.keys, self.keyboard.encoders)
# use "active" background to indicate keys to hold
lock_keys = keyboard.get_unlock_keys()
lock_keys = self.keyboard.get_unlock_keys()
for w in self.keyboard_reference.widgets:
if (w.desc.row, w.desc.col) in lock_keys:
w.setActive(True)
@ -57,37 +57,32 @@ class Unlocker(QWidget):
self.keyboard_reference.update()
self.keyboard_reference.updateGeometry()
def perform_unlock(self, keyboard):
# if it's already unlocked, don't need to do anything
unlock = keyboard.get_unlock_status()
if unlock == 1:
return
def unlock_poller(self):
data = self.keyboard.unlock_poll()
unlocked = data[0]
unlock_counter = data[2]
self.update_reference(keyboard)
self.progress.setMaximum(max(self.progress.maximum(), unlock_counter))
self.progress.setValue(self.progress.maximum() - unlock_counter)
if unlocked == 1:
self.accept()
def perform_unlock(self):
self.progress.setMaximum(1)
self.progress.setValue(0)
self.show()
self.keyboard = keyboard
self.keyboard.unlock_start()
self.timer.start(200)
while True:
data = self.keyboard.unlock_poll()
unlocked = data[0]
unlock_counter = data[2]
@classmethod
def unlock(cls, keyboard):
if keyboard.get_unlock_status() == 1:
return True
self.progress.setMaximum(max(self.progress.maximum(), unlock_counter))
self.progress.setValue(self.progress.maximum() - unlock_counter)
dlg = cls(cls.global_layout_editor, keyboard)
return bool(dlg.exec_())
if unlocked == 1:
break
QCoreApplication.processEvents()
time.sleep(0.2)
# ok all done, the keyboard is now set to insecure state
self.hide()
def closeEvent(self, ev):
ev.ignore()
def keyPressEvent(self, ev):
""" Ignore all key presses, e.g. Esc should not close the window """
pass