#!/usr/bin/python3 """Saver module for xsecurelock. It displays a background image, clock and weather. Configuration is done through environment variables: - 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") from gi.repository import Gtk, Gdk, GdkX11, GLib, GdkPixbuf, Gio import cairo def on_win_realize(widget, ctx): """On realization, embed into XSCREENSAVER_WINDOW and remember parent position.""" parent_wid = int(os.getenv("XSCREENSAVER_WINDOW", 0)) if not parent_wid: return parent = GdkX11.X11Window.foreign_new_for_display(widget.get_display(), parent_wid) x, y, w, h = parent.get_geometry() ctx.position = x, y window = widget.get_window() window.resize(w, h) window.reparent(parent, 0, 0) 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, 1) cctx.paint() return x, y = ctx.position wwidth, wheight = widget.get_size() scale = widget.get_scale_factor() bg = ctx.background.new_subpixbuf( x * scale, y * scale, wwidth * scale, wheight * scale ) cctx.save() cctx.scale(1 / scale, 1 / scale) Gdk.cairo_set_source_pixbuf(cctx, bg, 0, 0) cctx.paint() cctx.restore() 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 // 3 - 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.""" if event not in ( Gio.FileMonitorEvent.CHANGES_DONE_HINT, Gio.FileMonitorEvent.RENAMED, ): return try: new_background = GdkPixbuf.Pixbuf.new_from_file(ctx.background_image) except Exception: return ctx.background = new_background 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_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] ctx.window = Gtk.Window() ctx.window.set_app_paintable(True) ctx.window.set_visual(ctx.window.get_screen().get_rgba_visual()) ctx.window.connect("realize", on_win_realize, ctx) 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) monitor.connect("changed", on_background_change, ctx) on_background_change(None, None, None, Gio.FileMonitorEvent.CHANGES_DONE_HINT, ctx) ctx.window.show_all() # Main loop Gtk.main()