xsecurelock: display a clock

This commit is contained in:
Vincent Bernat 2021-12-08 18:14:09 +01:00
parent 2b94a98c91
commit 237fc329bc
3 changed files with 45 additions and 7 deletions

View file

@ -5,12 +5,15 @@
It displays a background image, clock and weather. Configuration is It displays a background image, clock and weather. Configuration is
done through environment variables: 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 os
import types import types
import datetime
import gi import gi
gi.require_version("Gtk", "3.0") gi.require_version("Gtk", "3.0")
@ -35,7 +38,7 @@ def on_win_draw(widget, cctx, ctx):
"""Draw background image.""" """Draw background image."""
cctx.set_operator(cairo.OPERATOR_SOURCE) cctx.set_operator(cairo.OPERATOR_SOURCE)
if not ctx.background: if not ctx.background:
cctx.set_source_rgba(0, 0, 0, opacity) cctx.set_source_rgba(0, 0, 0, 1)
cctx.paint() cctx.paint()
return return
@ -50,6 +53,28 @@ def on_win_draw(widget, cctx, ctx):
cctx.paint() 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): def on_background_change(monitor, f1, f2, event, ctx):
"""Update background when changed.""" """Update background when changed."""
print(f1, f2, event, ctx) print(f1, f2, event, ctx)
@ -66,9 +91,17 @@ def on_background_change(monitor, f1, f2, event, ctx):
ctx.window.queue_draw() 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__": if __name__ == "__main__":
ctx = types.SimpleNamespace() 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.background = None
ctx.position = [0, 0] ctx.position = [0, 0]
@ -84,6 +117,11 @@ if __name__ == "__main__":
ctx.window.connect("draw", on_win_draw, ctx) ctx.window.connect("draw", on_win_draw, ctx)
ctx.window.connect("delete-event", Gtk.main_quit) 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: if ctx.background_image:
gfile = Gio.File.new_for_path(ctx.background_image) gfile = Gio.File.new_for_path(ctx.background_image)
monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None) monitor = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None)

View file

@ -83,12 +83,12 @@ def on_draw(widget, event, options, background, start):
cctx.stroke() cctx.stroke()
def refresh(window, options, start): def on_refresh(window, options, start):
window.queue_draw() window.queue_draw()
elapsed = time.monotonic() - start elapsed = time.monotonic() - start
if elapsed < options.delay: if elapsed < options.delay:
next_step = min(options.step, options.delay - elapsed) 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/ # See: https://easings.net/
easing_functions = { easing_functions = {
@ -171,7 +171,7 @@ if __name__ == "__main__":
window.show_all() window.show_all()
# Schedule refresh with window.queue_draw() # Schedule refresh with window.queue_draw()
refresh(window, options, now) on_refresh(window, options, now)
# Watch for locker window # Watch for locker window
xdisplay = display.Display() xdisplay = display.Display()

View file

@ -36,7 +36,7 @@ case "$1" in
# Then, lock screen # Then, lock screen
env XSECURELOCK_SAVER=$HOME/.config/i3/bin/xsecurelock-saver \ env XSECURELOCK_SAVER=$HOME/.config/i3/bin/xsecurelock-saver \
XSECURELOCK_NO_XRANDR15=1 \ 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_FONT="Iosevka" \
xsecurelock xsecurelock
echo "lock: unlock screen" echo "lock: unlock screen"