mirror of
https://github.com/vincentbernat/i3wm-configuration.git
synced 2025-06-24 18:58:33 +02:00
xsecurelock: monitor background change
This commit is contained in:
parent
4997db033a
commit
2b94a98c91
1 changed files with 52 additions and 28 deletions
|
@ -10,62 +10,86 @@ done through environment variables:
|
|||
"""
|
||||
|
||||
import os
|
||||
import types
|
||||
import gi
|
||||
|
||||
gi.require_version("Gtk", "3.0")
|
||||
from gi.repository import Gtk, Gdk, GdkX11, GLib, GdkPixbuf
|
||||
from gi.repository import Gtk, Gdk, GdkX11, GLib, GdkPixbuf, Gio
|
||||
import cairo
|
||||
|
||||
|
||||
def on_win_realize(widget, position):
|
||||
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)
|
||||
[*position] = x, y
|
||||
|
||||
|
||||
def on_win_draw(widget, ctx, background, position):
|
||||
def on_win_draw(widget, cctx, ctx):
|
||||
"""Draw background image."""
|
||||
ctx.set_operator(cairo.OPERATOR_SOURCE)
|
||||
if not background:
|
||||
ctx.set_source_rgba(0, 0, 0, opacity)
|
||||
ctx.paint()
|
||||
cctx.set_operator(cairo.OPERATOR_SOURCE)
|
||||
if not ctx.background:
|
||||
cctx.set_source_rgba(0, 0, 0, opacity)
|
||||
cctx.paint()
|
||||
return
|
||||
|
||||
x, y = position
|
||||
x, y = ctx.position
|
||||
wwidth, wheight = widget.get_size()
|
||||
scale = widget.get_scale_factor()
|
||||
bg = background.new_subpixbuf(x * scale, y * scale, wwidth * scale, wheight * scale)
|
||||
ctx.scale(1 / scale, 1 / scale)
|
||||
Gdk.cairo_set_source_pixbuf(ctx, bg, 0, 0)
|
||||
ctx.paint()
|
||||
bg = ctx.background.new_subpixbuf(
|
||||
x * scale, y * scale, wwidth * scale, wheight * scale
|
||||
)
|
||||
cctx.scale(1 / scale, 1 / scale)
|
||||
Gdk.cairo_set_source_pixbuf(cctx, bg, 0, 0)
|
||||
cctx.paint()
|
||||
|
||||
|
||||
def on_background_change(monitor, f1, f2, event, ctx):
|
||||
"""Update background when changed."""
|
||||
print(f1, f2, event, ctx)
|
||||
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:
|
||||
pass
|
||||
ctx.background = new_background
|
||||
ctx.window.queue_draw()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
background_image = os.getenv("XSECURELOCK_BACKGROUND_IMAGE", None)
|
||||
ctx = types.SimpleNamespace()
|
||||
ctx.background_image = os.getenv("XSECURELOCK_BACKGROUND_IMAGE", None)
|
||||
ctx.background = None
|
||||
ctx.position = [0, 0]
|
||||
|
||||
background = None
|
||||
if background_image:
|
||||
try:
|
||||
background = GdkPixbuf.Pixbuf.new_from_file(background_image)
|
||||
ctx.background = GdkPixbuf.Pixbuf.new_from_file(ctx.background_image)
|
||||
except Exception:
|
||||
pass
|
||||
position = [0, 0]
|
||||
|
||||
window = Gtk.Window()
|
||||
window.set_app_paintable(True)
|
||||
window.set_visual(window.get_screen().get_rgba_visual())
|
||||
window.connect("realize", on_win_realize, position)
|
||||
window.connect("draw", on_win_draw, background, position)
|
||||
window.connect("delete-event", Gtk.main_quit)
|
||||
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)
|
||||
|
||||
window.show_all()
|
||||
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)
|
||||
|
||||
ctx.window.show_all()
|
||||
|
||||
# Main loop
|
||||
Gtk.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue