From 9a6aeaa43eebd0015d6bcb0dc66f1b6affcfd126 Mon Sep 17 00:00:00 2001 From: Alfredo Monclus Date: Mon, 28 Oct 2024 08:27:36 -0300 Subject: [PATCH] feat: lockscreen closes #1490 --- docs/Configuration.md | 6 ++ ks_includes/config.py | 15 ++-- ks_includes/widgets/keyboard.py | 5 +- ks_includes/widgets/lockscreen.py | 96 +++++++++++++++++++++++++ panels/shutdown.py | 10 ++- screen.py | 37 ++++++---- styles/base.css | 18 +++++ styles/colorized/images/lock.svg | 93 ++++++++++++++++++++++++ styles/material-dark/images/lock.svg | 40 +++++++++++ styles/material-darker/images/lock.svg | 41 +++++++++++ styles/material-light/images/lock.svg | 40 +++++++++++ styles/z-bolt/images/lock.svg | 97 ++++++++++++++++++++++++++ 12 files changed, 474 insertions(+), 24 deletions(-) create mode 100644 ks_includes/widgets/lockscreen.py create mode 100644 styles/colorized/images/lock.svg create mode 100644 styles/material-dark/images/lock.svg create mode 100644 styles/material-darker/images/lock.svg create mode 100644 styles/material-light/images/lock.svg create mode 100644 styles/z-bolt/images/lock.svg diff --git a/docs/Configuration.md b/docs/Configuration.md index b9b3576a..051ee6ca 100644 --- a/docs/Configuration.md +++ b/docs/Configuration.md @@ -34,6 +34,12 @@ The options listed here are not editable from within the user interface. # Define one or more moonraker power devices that turn on/off with the screensaver (CSV list) # screen_on_devices: example1, example2 # screen_off_devices: example1, example2 + +# Define the password to use when locking the screen, this is not secure +# it's saved as plain text, it's meant to be a deterrent for kids or people at shows +# it will be redacted from the logs. +# default is no password +# lock_password: example_password ``` !!! tip diff --git a/ks_includes/config.py b/ks_includes/config.py index ecd85548..e0900c1e 100644 --- a/ks_includes/config.py +++ b/ks_includes/config.py @@ -168,6 +168,7 @@ class KlipperScreenConfig: strs = ( 'default_printer', 'language', 'print_sort_dir', 'theme', 'screen_blanking_printing', 'font_size', 'print_estimate_method', 'screen_blanking', "screen_on_devices", "screen_off_devices", 'print_view', + "lock_password" ) numbers = ( 'job_complete_timeout', 'job_error_timeout', 'move_speed_xy', 'move_speed_z', @@ -584,14 +585,16 @@ class KlipperScreenConfig: self.config.set(section, name, value) def log_config(self, config): + sensitive_keys = [ + r'(moonraker_api_key\s*(?:=|:)\s*)\S+', + r'(lock_password\s*(?:=|:)\s*)\S+', + ] + config_str = self._build_config_string(config) + for pattern in sensitive_keys: + config_str = re.sub(pattern, r'\1[redacted]', config_str) lines = [ - " " "===== Config File =====", - re.sub( - r'(moonraker_api_key\s*=\s*\S+)', - 'moonraker_api_key = [redacted]', - self._build_config_string(config) - ), + config_str, "=======================" ] logging.info("\n".join(lines)) diff --git a/ks_includes/widgets/keyboard.py b/ks_includes/widgets/keyboard.py index 2545a35f..ad8d4df4 100644 --- a/ks_includes/widgets/keyboard.py +++ b/ks_includes/widgets/keyboard.py @@ -10,7 +10,7 @@ from gi.repository import Gtk, GLib class Keyboard(Gtk.Box): langs = ["de", "en", "fr", "es"] - def __init__(self, screen, close_cb, entry=None): + def __init__(self, screen, close_cb, entry=None, box=None): super().__init__(orientation=Gtk.Orientation.VERTICAL) self.shift = [] self.shift_active = False @@ -20,6 +20,7 @@ class Keyboard(Gtk.Box): self.timeout = self.clear_timeout = None self.entry = entry self.purpose = self.entry.get_input_purpose() + self.box = box or None language = self.detect_language(screen._config.get_main_config().get("language", None)) @@ -218,7 +219,7 @@ class Keyboard(Gtk.Box): if key == "⌫": Gtk.Entry.do_backspace(self.entry) elif key == "↓": - self.close_cb(entry=self.entry) + self.close_cb(entry=self.entry, box=self.box) return elif key == "↑": self.toggle_shift() diff --git a/ks_includes/widgets/lockscreen.py b/ks_includes/widgets/lockscreen.py new file mode 100644 index 00000000..3ac4eb30 --- /dev/null +++ b/ks_includes/widgets/lockscreen.py @@ -0,0 +1,96 @@ +import logging +import gi + +gi.require_version('Gtk', '3.0') +from gi.repository import Gtk + + +class LockScreen: + def __init__(self, screen): + self.screen = screen + self.lock_box = None + self.unlock_box = None + + def lock(self, widget): + self.screen._menu_go_back(None, True) + logging.info("Locked the screen") + close = Gtk.Button() + close.connect("clicked", self.unlock) + self.lock_box = Gtk.Box( + width_request=self.screen.width, height_request=self.screen.height, + halign=Gtk.Align.CENTER + ) + self.lock_box.pack_start(close, True, True, 0) + self.lock_box.get_style_context().add_class("lock") + self.screen.overlay.add_overlay(self.lock_box) + close.grab_focus() + self.lock_box.show_all() + + def unlock(self, widget): + if not self.unlock_box: + self.unlock_box = self.create_unlock_box() + self.screen.overlay.add_overlay(self.unlock_box) + self.unlock_box.show_all() + self.screen.overlay.get_children()[0].hide() + + def create_unlock_box(self): + box = Gtk.Box( + orientation=Gtk.Orientation.VERTICAL, + width_request=self.screen.width, height_request=self.screen.height, + valign=Gtk.Align.CENTER + ) + entry = Gtk.Entry(hexpand=True, vexpand=False, placeholder_text=_("Password")) + entry.set_input_purpose(Gtk.InputPurpose.PASSWORD) + entry.set_visibility(False) + entry.connect("focus-in-event", self.screen.show_keyboard, box, self.relock) + entry.connect("activate", self.unlock_attempt, entry) + entry.get_style_context().add_class("lockscreen_entry") + entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, "view-reveal-symbolic") + entry.connect("icon-press", self.show_pass) + ok = Gtk.Button(label=_("Unlock")) + ok.connect("clicked", self.unlock_attempt, entry) + ok.get_style_context().add_class("lockscreen_button") + entry_row = Gtk.Box(hexpand=True, vexpand=True, valign=Gtk.Align.CENTER) + entry_row.pack_start(entry, True, True, 0) + entry_row.pack_start(ok, False, True, 0) + box.pack_start(entry_row, True, True, 0) + return box + + @staticmethod + def show_pass(entry, icon_pos, event): + entry.grab_focus() + visible = not entry.get_visibility() + entry.set_visibility(visible) + if visible: + entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, "view-conceal-symbolic") + entry.get_style_context().add_class("active") + else: + entry.set_icon_from_icon_name(Gtk.EntryIconPosition.SECONDARY, "view-reveal-symbolic") + entry.get_style_context().remove_class("active") + + def relock(self, entry=None, box=None): + if not self.lock_box: + return + if self.unlock_box: + self.screen.overlay.remove(self.unlock_box) + self.unlock_box = None + self.lock_box.get_children()[0].grab_focus() + self.screen.overlay.get_children()[0].show_all() + + def unlock_attempt(self, widget, entry): + if entry.get_text() != self.screen._config.get_main_config().get("lock_password", ""): + logging.info("Failed unlock") + self.screen.show_popup_message(_("Unlock failed")) + return + self.clear_lock() + + def clear_lock(self): + if self.lock_box: + self.screen.overlay.remove(self.lock_box) + if self.unlock_box: + self.screen.overlay.remove(self.unlock_box) + self.lock_box = None + self.unlock_box = None + self.screen.remove_keyboard() + self.screen.overlay.get_children()[0].show() + logging.info("Unlocked") diff --git a/panels/shutdown.py b/panels/shutdown.py index ad04a01e..0dcb45c3 100644 --- a/panels/shutdown.py +++ b/panels/shutdown.py @@ -24,12 +24,16 @@ class Panel(ScreenPanel): restart_ks = self._gtk.Button("refresh", _("Restart") + " KlipperScreen", "color3") restart_ks.connect("clicked", self._screen.restart_ks) + lock_screen = self._gtk.Button("lock", _("Lock"), "color3") + lock_screen.connect("clicked", self._screen.lock_screen.lock) + self.main = Gtk.Grid(row_homogeneous=True, column_homogeneous=True) if self._printer and self._printer.state not in {'disconnected', 'startup', 'shutdown', 'error'}: - self.main.attach(estop, 0, 0, 1, 1) + self.main.attach(estop, 0, 0, 1, 2) self.main.attach(restart_ks, 1, 0, 1, 1) - self.main.attach(poweroff, 0, 1, 1, 1) - self.main.attach(restart, 1, 1, 1, 1) + self.main.attach(lock_screen, 2, 0, 1, 1) + self.main.attach(poweroff, 1, 1, 1, 1) + self.main.attach(restart, 2, 1, 1, 1) self.content.add(self.main) def reboot_poweroff(self, widget, method): diff --git a/screen.py b/screen.py index d18e3f4c..384d9a90 100755 --- a/screen.py +++ b/screen.py @@ -29,9 +29,11 @@ from ks_includes.KlippyGtk import KlippyGtk from ks_includes.printer import Printer from ks_includes.widgets.keyboard import Keyboard from ks_includes.widgets.prompts import Prompt +from ks_includes.widgets.lockscreen import LockScreen from ks_includes.config import KlipperScreenConfig from panels.base_panel import BasePanel + logging.getLogger("urllib3").setLevel(logging.WARNING) klipperscreendir = pathlib.Path(__file__).parent.resolve() @@ -77,6 +79,7 @@ class KlipperScreen(Gtk.Window): prompt = None tempstore_timeout = None + def __init__(self, args): self.server_info = None try: @@ -150,7 +153,9 @@ class KlipperScreen(Gtk.Window): self.set_icon_from_file(os.path.join(klipperscreendir, "styles", "icon.svg")) self.base_panel = BasePanel(self) self.change_theme(self.theme) - self.add(self.base_panel.main_grid) + self.overlay = Gtk.Overlay() + self.add(self.overlay) + self.overlay.add_overlay(self.base_panel.main_grid) self.show_all() self.update_cursor(self.show_cursor) min_ver = (3, 8) @@ -167,6 +172,7 @@ class KlipperScreen(Gtk.Window): return self.base_panel.activate() self.set_screenblanking_timeout(self._config.get_main_config().get('screen_blanking')) + self.lock_screen = LockScreen(self) self.log_notification("KlipperScreen Started", 1) self.initial_connection() @@ -639,8 +645,7 @@ class KlipperScreen(Gtk.Window): box = Gtk.Box(halign=Gtk.Align.CENTER, width_request=self.width, height_request=self.height) box.pack_start(close, True, True, 0) box.get_style_context().add_class("screensaver") - self.remove(self.base_panel.main_grid) - self.add(box) + self.overlay.add_overlay(box) # Avoid leaving a cursor-handle close.grab_focus() @@ -655,9 +660,8 @@ class KlipperScreen(Gtk.Window): if self.screensaver is None: return False logging.debug("Closing Screensaver") - self.remove(self.screensaver) + self.overlay.remove(self.screensaver) self.screensaver = None - self.add(self.base_panel.main_grid) if self.use_dpms: self.wake_screen() else: @@ -666,8 +670,8 @@ class KlipperScreen(Gtk.Window): logging.info(f"Restoring Dialog {dialog}") dialog.show() self.gtk.set_cursor(self.show_cursor, window=self.get_window()) - self.show_all() self.power_devices(None, self._config.get_main_config().get("screen_on_devices", ""), on=True) + self.lock_screen.relock() def check_dpms_state(self): if not self.use_dpms: @@ -1187,12 +1191,16 @@ class KlipperScreen(Gtk.Window): self.remove_tempstore_timeout() return self.init_tempstore() - def show_keyboard(self, entry=None, event=None): + def show_keyboard(self, entry=None, event=None, box=None, close_cb=None): if entry is None: logging.debug("Error: no entry provided for keyboard") return + if box is None: + box = self.base_panel.content + if close_cb is None: + close_cb = self.remove_keyboard if self.keyboard is not None: - self.remove_keyboard() + self.remove_keyboard(box=box) entry.grab_focus() kbd_grid = Gtk.Grid() kbd_grid.set_size_request(self.gtk.content_width, self.gtk.keyboard_height) @@ -1206,11 +1214,12 @@ class KlipperScreen(Gtk.Window): kbd_grid.set_column_homogeneous(True) kbd_width = 2 if purpose == Gtk.InputPurpose.DIGITS else 3 kbd_grid.attach(Gtk.Box(), 0, 0, 1, 1) - kbd_grid.attach(Keyboard(self, self.remove_keyboard, entry=entry), 1, 0, kbd_width, 1) + kbd = Keyboard(self, close_cb, entry=entry, box=box) + kbd_grid.attach(kbd, 1, 0, kbd_width, 1) kbd_grid.attach(Gtk.Box(), kbd_width + 1, 0, 1, 1) self.keyboard = {"box": kbd_grid} - self.base_panel.content.pack_end(kbd_grid, False, False, 0) - self.base_panel.content.show_all() + box.pack_end(kbd_grid, False, False, 0) + box.show_all() def _show_matchbox_keyboard(self, kbd_grid): env = os.environ.copy() @@ -1240,12 +1249,14 @@ class KlipperScreen(Gtk.Window): } return - def remove_keyboard(self, entry=None, event=None): + def remove_keyboard(self, entry=None, event=None, box=None): if self.keyboard is None: return + if box is None: + box = self.base_panel.content if 'process' in self.keyboard: os.kill(self.keyboard['process'].pid, SIGTERM) - self.base_panel.content.remove(self.keyboard['box']) + box.remove(self.keyboard['box']) self.keyboard = None if entry: entry.set_sensitive(False) # Move the focus diff --git a/styles/base.css b/styles/base.css index 23a08409..c03d2ec7 100644 --- a/styles/base.css +++ b/styles/base.css @@ -563,6 +563,24 @@ popover button { margin: 0; } +.lock +.lock button +.lock button:hover, +.lock button:focus, +.lock button:active { + border: 0; + margin: 0; + background-color: Transparent; +} + +.lockscreen_entry { + margin: 2em; +} + +.lockscreen_button { + padding: 2em; +} + .screensaver, .screensaver button, .screensaver button:hover, diff --git a/styles/colorized/images/lock.svg b/styles/colorized/images/lock.svg new file mode 100644 index 00000000..2fa9a3db --- /dev/null +++ b/styles/colorized/images/lock.svg @@ -0,0 +1,93 @@ + + + + + + + + + + + + + + + diff --git a/styles/material-dark/images/lock.svg b/styles/material-dark/images/lock.svg new file mode 100644 index 00000000..1d032eb9 --- /dev/null +++ b/styles/material-dark/images/lock.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/material-darker/images/lock.svg b/styles/material-darker/images/lock.svg new file mode 100644 index 00000000..32d3ef18 --- /dev/null +++ b/styles/material-darker/images/lock.svg @@ -0,0 +1,41 @@ + + + + + + diff --git a/styles/material-light/images/lock.svg b/styles/material-light/images/lock.svg new file mode 100644 index 00000000..0ac185e2 --- /dev/null +++ b/styles/material-light/images/lock.svg @@ -0,0 +1,40 @@ + + + + + + diff --git a/styles/z-bolt/images/lock.svg b/styles/z-bolt/images/lock.svg new file mode 100644 index 00000000..4668e7af --- /dev/null +++ b/styles/z-bolt/images/lock.svg @@ -0,0 +1,97 @@ + + + + + + image/svg+xml + + folder + + + + + + + folder + Created with Sketch. + + + + + + +