2021-12-07 20:34:15 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
"""Saver module for xsecurelock.
|
|
|
|
|
|
|
|
It displays a background image, clock and weather. Configuration is
|
|
|
|
done through environment variables:
|
|
|
|
|
2021-12-08 18:14:09 +01:00
|
|
|
- XSECURELOCK_SAVER_IMAGE: path to the background image to use
|
2021-12-09 12:51:29 +01:00
|
|
|
- XSECURELOCK_SAVER_WEATHER: path to weather text
|
|
|
|
- XSECURELOCK_SAVER_FONT: font family to use to display clock and weather
|
|
|
|
- XSECURELOCK_SAVER_CLOCK_FONT_SIZE: font size to use to display clock
|
|
|
|
- XSECURELOCK_SAVER_WEATHER_FONT_SIZE: font size to use to display weather
|
2021-12-07 20:34:15 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
|
2021-12-10 09:35:22 +01:00
|
|
|
# In case I want to put a video instead of an image:
|
|
|
|
# https://gist.github.com/NBonaparte/89fb1b645c99470bc0f6. Check
|
|
|
|
# `bin/wallpaper' for some sources.
|
|
|
|
|
2021-12-07 20:34:15 +01:00
|
|
|
import os
|
2021-12-08 17:19:46 +01:00
|
|
|
import types
|
2021-12-08 18:14:09 +01:00
|
|
|
import datetime
|
2021-12-09 12:51:29 +01:00
|
|
|
import re
|
2022-01-09 12:28:04 +01:00
|
|
|
import socket
|
2022-01-13 21:38:33 +01:00
|
|
|
import time
|
|
|
|
import cairo
|
|
|
|
import gi
|
2021-12-07 20:34:15 +01:00
|
|
|
|
|
|
|
gi.require_version("Gtk", "3.0")
|
2021-12-08 17:19:46 +01:00
|
|
|
from gi.repository import Gtk, Gdk, GdkX11, GLib, GdkPixbuf, Gio
|
2021-12-07 20:34:15 +01:00
|
|
|
|
|
|
|
|
2021-12-08 17:19:46 +01:00
|
|
|
def on_win_realize(widget, ctx):
|
2021-12-07 20:34:15 +01:00
|
|
|
"""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()
|
2021-12-08 17:19:46 +01:00
|
|
|
ctx.position = x, y
|
2021-12-07 20:34:15 +01:00
|
|
|
window = widget.get_window()
|
|
|
|
window.resize(w, h)
|
|
|
|
window.reparent(parent, 0, 0)
|
|
|
|
|
|
|
|
|
2021-12-08 17:19:46 +01:00
|
|
|
def on_win_draw(widget, cctx, ctx):
|
2021-12-07 20:34:15 +01:00
|
|
|
"""Draw background image."""
|
2021-12-13 21:20:58 +01:00
|
|
|
x, y = ctx.position
|
|
|
|
wwidth, wheight = widget.get_size()
|
|
|
|
scale = widget.get_scale_factor()
|
|
|
|
bg = None
|
|
|
|
|
|
|
|
if ctx.background:
|
|
|
|
bg = ctx.background.new_subpixbuf(
|
|
|
|
x * scale, y * scale, wwidth * scale, wheight * scale
|
|
|
|
)
|
|
|
|
|
2021-12-08 17:19:46 +01:00
|
|
|
cctx.set_operator(cairo.OPERATOR_SOURCE)
|
2021-12-13 21:20:58 +01:00
|
|
|
if not bg:
|
2021-12-08 18:14:09 +01:00
|
|
|
cctx.set_source_rgba(0, 0, 0, 1)
|
2021-12-08 17:19:46 +01:00
|
|
|
cctx.paint()
|
2021-12-07 20:34:15 +01:00
|
|
|
return
|
|
|
|
|
2021-12-08 19:43:26 +01:00
|
|
|
cctx.save()
|
2021-12-08 17:19:46 +01:00
|
|
|
cctx.scale(1 / scale, 1 / scale)
|
|
|
|
Gdk.cairo_set_source_pixbuf(cctx, bg, 0, 0)
|
|
|
|
cctx.paint()
|
2021-12-08 19:43:26 +01:00
|
|
|
cctx.restore()
|
2021-12-08 17:19:46 +01:00
|
|
|
|
|
|
|
|
2021-12-08 18:14:09 +01:00
|
|
|
def on_overlay_draw(widget, cctx, ctx):
|
2021-12-10 14:09:00 +01:00
|
|
|
"""Draw overlay (clock and weather)."""
|
2022-01-09 12:28:04 +01:00
|
|
|
if not ctx.leader:
|
|
|
|
return
|
2021-12-08 18:14:09 +01:00
|
|
|
wwidth, wheight = widget.get_parent().get_size()
|
2021-12-14 07:33:34 +01:00
|
|
|
cctx.set_operator(cairo.OPERATOR_OVER)
|
|
|
|
|
|
|
|
def draw(what):
|
|
|
|
x, y = cctx.get_current_point()
|
|
|
|
cctx.set_source_rgba(0, 0, 0, 0.3)
|
2021-12-26 15:31:38 +01:00
|
|
|
cctx.move_to(x + 2, y + 2)
|
2021-12-14 07:33:34 +01:00
|
|
|
cctx.show_text(what)
|
|
|
|
cctx.set_source_rgb(1, 1, 1)
|
|
|
|
cctx.move_to(x, y)
|
2021-12-13 23:23:38 +01:00
|
|
|
cctx.show_text(what)
|
2021-12-13 22:49:43 +01:00
|
|
|
|
|
|
|
# Clock
|
2021-12-09 13:05:50 +01:00
|
|
|
if ctx.clock:
|
2021-12-13 18:22:49 +01:00
|
|
|
time, date = ctx.clock
|
|
|
|
|
|
|
|
# Time
|
2021-12-13 21:07:00 +01:00
|
|
|
cctx.select_font_face(
|
|
|
|
ctx.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
|
|
|
|
)
|
2021-12-09 13:05:50 +01:00
|
|
|
cctx.set_font_size(ctx.clock_font_size)
|
2021-12-13 18:22:49 +01:00
|
|
|
_, _, twidth, theight, _, _ = cctx.text_extents(re.sub(r"\d", "8", time))
|
2021-12-13 22:49:43 +01:00
|
|
|
cctx.move_to(wwidth // 2 - twidth // 2, wheight // 3)
|
2021-12-14 07:33:34 +01:00
|
|
|
draw(time)
|
2021-12-13 18:22:49 +01:00
|
|
|
|
|
|
|
# Date
|
2021-12-13 21:07:00 +01:00
|
|
|
cctx.select_font_face(
|
|
|
|
ctx.font_family, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL
|
|
|
|
)
|
2021-12-13 18:22:49 +01:00
|
|
|
cctx.set_font_size(ctx.clock_font_size // 3)
|
|
|
|
_, _, twidth, theight, _, _ = cctx.text_extents(date)
|
2021-12-13 22:49:43 +01:00
|
|
|
cctx.move_to(wwidth // 2 - twidth // 2, wheight // 3 + theight * 1.5)
|
2021-12-13 23:23:38 +01:00
|
|
|
draw(date)
|
2021-12-08 18:14:09 +01:00
|
|
|
|
2021-12-09 12:51:29 +01:00
|
|
|
# Weather
|
|
|
|
# We can have polybar markups in it. We assume %{Tx} means to use
|
2024-05-12 16:22:08 +02:00
|
|
|
# Weather Icons and we ignore font color change. The parsing is
|
2021-12-09 12:51:29 +01:00
|
|
|
# quite basic.
|
2022-01-09 12:28:04 +01:00
|
|
|
if ctx.weather:
|
2021-12-09 13:05:50 +01:00
|
|
|
data = re.sub(r"%{F[#\d+-]+?}", "", ctx.weather)
|
|
|
|
data = re.split(r"(%{T[1-9-]})", data)
|
|
|
|
font = ctx.font_family
|
|
|
|
cctx.move_to(20, wheight - 20)
|
|
|
|
for chunk in data:
|
|
|
|
if chunk == "%{T-}":
|
|
|
|
font = ctx.font_family
|
|
|
|
continue
|
|
|
|
elif chunk.startswith("%{T"):
|
2024-05-12 16:22:08 +02:00
|
|
|
font = "Weather Icons"
|
2021-12-09 13:05:50 +01:00
|
|
|
continue
|
|
|
|
elif not chunk:
|
|
|
|
continue
|
|
|
|
cctx.select_font_face(
|
|
|
|
font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL
|
|
|
|
)
|
|
|
|
cctx.set_font_size(ctx.weather_font_size)
|
2021-12-13 23:23:38 +01:00
|
|
|
draw(chunk)
|
2021-12-09 13:05:50 +01:00
|
|
|
|
2021-12-09 12:51:29 +01:00
|
|
|
|
2021-12-08 17:19:46 +01:00
|
|
|
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:
|
2021-12-08 21:14:37 +01:00
|
|
|
return
|
2021-12-08 17:19:46 +01:00
|
|
|
ctx.background = new_background
|
|
|
|
ctx.window.queue_draw()
|
2021-12-07 20:34:15 +01:00
|
|
|
|
|
|
|
|
2021-12-08 18:14:09 +01:00
|
|
|
def on_clock_change(ctx):
|
2021-12-10 14:09:00 +01:00
|
|
|
"""Clock may have changed. Update it.
|
|
|
|
|
|
|
|
We are checking more often than once a minute because we want to
|
|
|
|
update the clock swiftly after suspend. An alternative would be to
|
|
|
|
listen to PrepareForSleep signal from org.freedesktop.login1, but
|
|
|
|
this is more complex.
|
|
|
|
|
|
|
|
"""
|
2021-12-08 18:14:09 +01:00
|
|
|
now = datetime.datetime.now()
|
2021-12-09 17:52:44 +01:00
|
|
|
new_clock = now.strftime("%H:%M")
|
|
|
|
if new_clock != ctx.clock:
|
2021-12-13 18:22:49 +01:00
|
|
|
ctx.clock = (new_clock, now.strftime("%A %-d %B"))
|
2021-12-09 17:52:44 +01:00
|
|
|
ctx.overlay.queue_draw()
|
2022-01-09 12:28:04 +01:00
|
|
|
if ctx.leader is None:
|
|
|
|
# Do leader "election"
|
2022-01-13 21:38:33 +01:00
|
|
|
if ctx.position != (0, 0):
|
|
|
|
time.sleep(0.2)
|
2022-01-09 12:28:04 +01:00
|
|
|
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
|
|
|
|
try:
|
|
|
|
s.bind("\0xsecurelock-saver")
|
|
|
|
except OSError:
|
|
|
|
ctx.leader = False
|
|
|
|
else:
|
|
|
|
ctx.leader = s
|
|
|
|
if ctx.leader:
|
|
|
|
GLib.timeout_add(min(60 - now.second, 3) * 1000, on_clock_change, ctx)
|
2021-12-08 18:14:09 +01:00
|
|
|
|
|
|
|
|
2021-12-09 12:51:29 +01:00
|
|
|
def on_weather_change(monitor, f1, f2, event, ctx):
|
2021-12-10 14:09:00 +01:00
|
|
|
"""Weather file has changed."""
|
2021-12-09 12:51:29 +01:00
|
|
|
if event not in (
|
|
|
|
Gio.FileMonitorEvent.CHANGES_DONE_HINT,
|
|
|
|
Gio.FileMonitorEvent.RENAMED,
|
|
|
|
):
|
|
|
|
return
|
2021-12-09 13:05:50 +01:00
|
|
|
try:
|
|
|
|
with open(ctx.weather_file) as wfile:
|
|
|
|
ctx.weather = wfile.read()
|
|
|
|
ctx.overlay.queue_draw()
|
|
|
|
except Exception:
|
|
|
|
pass
|
2021-12-09 12:51:29 +01:00
|
|
|
|
|
|
|
|
2021-12-07 20:34:15 +01:00
|
|
|
if __name__ == "__main__":
|
2021-12-08 17:19:46 +01:00
|
|
|
ctx = types.SimpleNamespace()
|
2021-12-08 18:14:09 +01:00
|
|
|
ctx.background_image = os.getenv("XSECURELOCK_SAVER_IMAGE", None)
|
2021-12-09 12:51:29 +01:00
|
|
|
ctx.clock_font_size = int(os.getenv("XSECURELOCK_SAVER_CLOCK_FONT_SIZE", 120))
|
|
|
|
ctx.weather_font_size = int(os.getenv("XSECURELOCK_SAVER_CLOCK_FONT_SIZE", 40))
|
|
|
|
ctx.weather_file = os.getenv("XSECURELOCK_SAVER_WEATHER", None)
|
2021-12-08 18:14:09 +01:00
|
|
|
ctx.font_family = os.getenv("XSECURELOCK_SAVER_FONT", "Iosevka Aile")
|
2021-12-08 17:19:46 +01:00
|
|
|
ctx.background = None
|
2021-12-13 18:22:49 +01:00
|
|
|
ctx.weather = None
|
|
|
|
ctx.clock = None
|
2021-12-31 22:16:18 +01:00
|
|
|
ctx.position = (0, 0)
|
2022-01-09 12:28:04 +01:00
|
|
|
ctx.leader = None
|
2021-12-08 17:19:46 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-12-08 18:14:09 +01:00
|
|
|
ctx.overlay = Gtk.DrawingArea()
|
|
|
|
ctx.overlay.connect("draw", on_overlay_draw, ctx)
|
|
|
|
ctx.window.add(ctx.overlay)
|
|
|
|
|
2021-12-09 13:05:50 +01:00
|
|
|
gio_event_args = (None, None, None, Gio.FileMonitorEvent.CHANGES_DONE_HINT)
|
2021-12-08 17:19:46 +01:00
|
|
|
if ctx.background_image:
|
|
|
|
gfile = Gio.File.new_for_path(ctx.background_image)
|
2021-12-26 16:44:57 +01:00
|
|
|
monitor1 = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None)
|
|
|
|
monitor1.connect("changed", on_background_change, ctx)
|
2021-12-09 13:05:50 +01:00
|
|
|
on_background_change(*gio_event_args, ctx)
|
2021-12-09 12:51:29 +01:00
|
|
|
if ctx.weather_file:
|
|
|
|
gfile = Gio.File.new_for_path(ctx.weather_file)
|
2021-12-26 16:44:57 +01:00
|
|
|
monitor2 = gfile.monitor_file(Gio.FileMonitorFlags.WATCH_MOVES, None)
|
|
|
|
monitor2.connect("changed", on_weather_change, ctx)
|
2021-12-09 13:05:50 +01:00
|
|
|
GLib.timeout_add(1000, on_weather_change, *gio_event_args, ctx)
|
|
|
|
GLib.timeout_add(1000, on_clock_change, ctx)
|
2021-12-08 17:19:46 +01:00
|
|
|
|
|
|
|
ctx.window.show_all()
|
2021-12-07 20:34:15 +01:00
|
|
|
|
|
|
|
# Main loop
|
|
|
|
Gtk.main()
|