xsecurelock: only display clock and weather on one screen

Relying on position is weak. Use an abstract Unix socket to take some
kind of lock and declare a leader.
This commit is contained in:
Vincent Bernat 2022-01-09 12:28:04 +01:00
parent 943445d31f
commit 8fbc2560df

View file

@ -22,6 +22,7 @@ import types
import datetime
import re
import gi
import socket
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GdkX11, GLib, GdkPixbuf, Gio
@ -68,6 +69,8 @@ def on_win_draw(widget, cctx, ctx):
def on_overlay_draw(widget, cctx, ctx):
"""Draw overlay (clock and weather)."""
if not ctx.leader:
return
wwidth, wheight = widget.get_parent().get_size()
cctx.set_operator(cairo.OPERATOR_OVER)
@ -106,7 +109,7 @@ def on_overlay_draw(widget, cctx, ctx):
# We can have polybar markups in it. We assume %{Tx} means to use
# Font Awesome 6 and we ignore font color change. The parsing is
# quite basic.
if ctx.weather and ctx.position == (0, 0):
if ctx.weather:
data = re.sub(r"%{F[#\d+-]+?}", "", ctx.weather)
data = re.split(r"(%{T[1-9-]})", data)
font = ctx.font_family
@ -156,7 +159,17 @@ def on_clock_change(ctx):
if new_clock != ctx.clock:
ctx.clock = (new_clock, now.strftime("%A %-d %B"))
ctx.overlay.queue_draw()
GLib.timeout_add(min(60 - now.second, 3) * 1000, on_clock_change, ctx)
if ctx.leader is None:
# Do leader "election"
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)
def on_weather_change(monitor, f1, f2, event, ctx):
@ -185,6 +198,7 @@ if __name__ == "__main__":
ctx.weather = None
ctx.clock = None
ctx.position = (0, 0)
ctx.leader = None
ctx.window = Gtk.Window()
ctx.window.set_app_paintable(True)