From 8fbc2560dfa5a507bed3101a74dba3865085fcaf Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sun, 9 Jan 2022 12:28:04 +0100 Subject: [PATCH] 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. --- bin/xsecurelock-saver | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/bin/xsecurelock-saver b/bin/xsecurelock-saver index 7a80ea3..bdfebbb 100755 --- a/bin/xsecurelock-saver +++ b/bin/xsecurelock-saver @@ -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)