From 237fc329bc2ce4502d6324131d8c35e9f7a92978 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Wed, 8 Dec 2021 18:14:09 +0100 Subject: [PATCH] xsecurelock: display a clock --- bin/xsecurelock-saver | 44 ++++++++++++++++++++++++++++++++++++++++--- bin/xss-dimmer | 6 +++--- bin/xss-lock | 2 +- 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/bin/xsecurelock-saver b/bin/xsecurelock-saver index 4edc9ed..244b2f4 100755 --- a/bin/xsecurelock-saver +++ b/bin/xsecurelock-saver @@ -5,12 +5,15 @@ It displays a background image, clock and weather. Configuration is done through environment variables: - - XSECURELOCK_BACKGROUND_IMAGE: path to the background image to use + - XSECURELOCK_SAVER_IMAGE: path to the background image to use + - XSECURELOCK_SAVER_FONT: font family to use to display clock + - XSECURELOCK_SAVER_FONT_SIZE: font size to use to display clock """ import os import types +import datetime import gi gi.require_version("Gtk", "3.0") @@ -35,7 +38,7 @@ def on_win_draw(widget, cctx, ctx): """Draw background image.""" cctx.set_operator(cairo.OPERATOR_SOURCE) if not ctx.background: - cctx.set_source_rgba(0, 0, 0, opacity) + cctx.set_source_rgba(0, 0, 0, 1) cctx.paint() return @@ -50,6 +53,28 @@ def on_win_draw(widget, cctx, ctx): cctx.paint() +def on_overlay_draw(widget, cctx, ctx): + """Draw overlay.""" + # Clock + cctx.set_operator(cairo.OPERATOR_SOURCE) + wwidth, wheight = widget.get_parent().get_size() + now = datetime.datetime.now().strftime("%H:%M") + cctx.select_font_face( + ctx.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD + ) + cctx.set_font_size(ctx.font_size) + _, _, twidth, theight, _, _ = cctx.text_extents("00:00") + text_position = wwidth // 2 - twidth // 2, wheight // 2 - theight // 2 + cctx.move_to(*text_position) + cctx.set_source_rgba(1, 1, 1, 0.8) + cctx.show_text(now) + cctx.move_to(*text_position) + cctx.set_source_rgb(0, 0, 0) + cctx.set_line_width(2) + cctx.text_path(now) + cctx.stroke() + + def on_background_change(monitor, f1, f2, event, ctx): """Update background when changed.""" print(f1, f2, event, ctx) @@ -66,9 +91,17 @@ def on_background_change(monitor, f1, f2, event, ctx): ctx.window.queue_draw() +def on_clock_change(ctx): + ctx.overlay.queue_draw() + now = datetime.datetime.now() + GLib.timeout_add((60 - now.second) * 1000, on_clock_change, ctx) + + if __name__ == "__main__": ctx = types.SimpleNamespace() - ctx.background_image = os.getenv("XSECURELOCK_BACKGROUND_IMAGE", None) + ctx.background_image = os.getenv("XSECURELOCK_SAVER_IMAGE", None) + ctx.font_size = int(os.getenv("XSECURELOCK_SAVER_FONT_SIZE", 120)) + ctx.font_family = os.getenv("XSECURELOCK_SAVER_FONT", "Iosevka Aile") ctx.background = None ctx.position = [0, 0] @@ -84,6 +117,11 @@ if __name__ == "__main__": ctx.window.connect("draw", on_win_draw, ctx) ctx.window.connect("delete-event", Gtk.main_quit) + ctx.overlay = Gtk.DrawingArea() + ctx.overlay.connect("draw", on_overlay_draw, ctx) + ctx.window.add(ctx.overlay) + on_clock_change(ctx) + if ctx.background_image: gfile = Gio.File.new_for_path(ctx.background_image) monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None) diff --git a/bin/xss-dimmer b/bin/xss-dimmer index d6ce98f..5001e38 100755 --- a/bin/xss-dimmer +++ b/bin/xss-dimmer @@ -83,12 +83,12 @@ def on_draw(widget, event, options, background, start): cctx.stroke() -def refresh(window, options, start): +def on_refresh(window, options, start): window.queue_draw() elapsed = time.monotonic() - start if elapsed < options.delay: next_step = min(options.step, options.delay - elapsed) - GLib.timeout_add(options.step * 1000, refresh, window, options, start) + GLib.timeout_add(options.step * 1000, on_refresh, window, options, start) # See: https://easings.net/ easing_functions = { @@ -171,7 +171,7 @@ if __name__ == "__main__": window.show_all() # Schedule refresh with window.queue_draw() - refresh(window, options, now) + on_refresh(window, options, now) # Watch for locker window xdisplay = display.Display() diff --git a/bin/xss-lock b/bin/xss-lock index c62c2f0..ec85a75 100755 --- a/bin/xss-lock +++ b/bin/xss-lock @@ -36,7 +36,7 @@ case "$1" in # Then, lock screen env XSECURELOCK_SAVER=$HOME/.config/i3/bin/xsecurelock-saver \ XSECURELOCK_NO_XRANDR15=1 \ - XSECURELOCK_BACKGROUND_IMAGE=$XDG_RUNTIME_DIR/i3/current-wallpaper.png \ + XSECURELOCK_SAVER_IMAGE=$XDG_RUNTIME_DIR/i3/current-wallpaper.png \ XSECURELOCK_FONT="Iosevka" \ xsecurelock echo "lock: unlock screen"