vincentbernat.i3wm-configur.../bin/xss-dimmer

187 lines
5.9 KiB
Text
Raw Normal View History

2021-08-07 14:11:15 +02:00
#!/usr/bin/env python3
"""Simple dimmer for xss-lock.
It dim the screen using a provided delay and display a countdown. It
will stop itself when the locker window is mapped.
"""
# It assumes we are using a compositor.
2021-08-04 11:27:27 +02:00
import gi
gi.require_version("Gtk", "3.0")
2021-09-06 14:55:51 +02:00
from gi.repository import Gtk, Gdk, GLib, GdkPixbuf
import cairo
import argparse
import threading
import time
2021-09-29 06:56:08 +02:00
import math
from Xlib import display, X
from Xlib.error import BadWindow
from Xlib.protocol.event import MapNotify
def on_xevent(source, condition, xdisplay, locker):
while xdisplay.pending_events():
event = xdisplay.next_event()
if event.type != X.MapNotify:
continue
try:
wmclass = event.window.get_wm_class()
except BadWindow:
continue
if wmclass and wmclass[1] == locker:
Gtk.main_quit()
return False
return True
def on_realize(widget):
window = widget.get_window()
window.set_override_redirect(True)
def on_draw(widget, event, options, background, start):
def _dim():
2021-09-06 14:55:51 +02:00
r = cairo.Region(cairo.RectangleInt(0, 0, *widget.get_size()))
dctx = window.begin_draw_frame(r)
cctx = dctx.get_cairo_context()
dim(cctx)
window.end_draw_frame(dctx)
def dim(cctx, once=False):
2021-09-06 14:55:51 +02:00
x, y = widget.get_position()
wwidth, wheight = widget.get_size()
delta = options.end_opacity - options.start_opacity
elapsed = time.monotonic() - start
2021-09-29 06:56:08 +02:00
current = easing_functions[options.easing_function](elapsed / options.delay)
opacity = delta * current + options.start_opacity
2021-09-06 14:55:51 +02:00
# Background
2021-09-06 21:31:44 +02:00
cctx.set_operator(cairo.OPERATOR_SOURCE)
2021-09-06 14:55:51 +02:00
if not background:
cctx.set_source_rgba(0, 0, 0, opacity)
cctx.paint()
else:
2021-09-06 21:31:44 +02:00
scale = widget.get_scale_factor()
bg = background.new_subpixbuf(x, y, wwidth * scale, wheight * scale)
2021-09-06 21:31:44 +02:00
cctx.save()
cctx.scale(1 / scale, 1 / scale)
2021-09-06 14:55:51 +02:00
Gdk.cairo_set_source_pixbuf(cctx, bg, 0, 0)
cctx.paint_with_alpha(opacity)
2021-09-06 21:31:44 +02:00
cctx.restore()
2021-08-04 11:27:27 +02:00
# Remaining time
if elapsed >= options.delay:
return
remaining = str(round(options.delay - elapsed))
cctx.select_font_face(
2021-08-04 11:27:27 +02:00
options.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
)
cctx.set_font_size(wheight // 4)
_, _, twidth, theight, _, _ = cctx.text_extents(remaining)
text_position = wwidth // 2 - twidth // 2, wheight // 2 + theight // 2
cctx.move_to(*text_position)
cctx.set_source_rgba(1, 1, 1, opacity)
cctx.show_text(remaining)
cctx.move_to(*text_position)
2021-09-06 14:55:51 +02:00
cctx.set_source_rgba(0, 0, 0, opacity * 2)
cctx.set_line_width(4)
cctx.text_path(remaining)
cctx.stroke()
2021-08-04 11:27:27 +02:00
# Rearm timer
if not once:
next_step = min(options.step, options.delay - elapsed)
on_draw.timer = GLib.timeout_add(next_step * 1000, _dim)
window = widget.get_window()
dim(event)
if not hasattr(on_draw, "timer"):
# First time we are called.
dim(event)
else:
2021-08-04 11:27:27 +02:00
# Timers already running, just repaint
dim(event, once=True)
2021-09-29 06:56:08 +02:00
# See: https://easings.net/
easing_functions = {
"none": lambda x: x,
"ease-out-circ": lambda x: math.sqrt(1 - pow(x - 1, 2)),
"ease-out-sine": lambda x: math.sin(x * math.pi / 2),
"ease-out-cubic": lambda x: 1 - pow(1 - x, 3),
"ease-out-quint": lambda x: 1 - pow(1 - x, 5),
"ease-out-expo": lambda x: 1 - pow(2, -10 * x),
"ease-out-quad": lambda x: 1 - (1 - x) * (1 - x),
"ease-out-elastic": (
lambda x: pow(2, -10 * x) * math.sin((x * 10 - 0.75) * (2 * math.pi) / 3) + 1
),
}
if __name__ == "__main__":
now = time.monotonic()
parser = argparse.ArgumentParser()
add = parser.add_argument
add("--start-opacity", type=float, default=0, help="initial opacity")
add("--end-opacity", type=float, default=1, help="final opacity")
add("--step", type=float, default=0.1, help="step for changing opacity")
add("--delay", type=float, default=10, help="delay from start to end")
add("--font", default="DejaVu Sans", help="font for countdown")
add("--locker", default="i3lock", help="quit if window class detected")
2021-09-06 14:55:51 +02:00
add("--background", help="use a background instead of black")
2021-09-29 06:56:08 +02:00
add(
"--easing-function",
default="ease-out-circ",
choices=easing_functions.keys(),
help="easing function for opacity",
)
options = parser.parse_args()
2021-09-06 14:55:51 +02:00
background = None
if options.background:
try:
background = GdkPixbuf.Pixbuf.new_from_file(options.background)
2021-09-06 21:31:44 +02:00
except Exception:
2021-09-06 14:55:51 +02:00
pass
# Setup dimmer windows on each monitor
gdisplay = Gdk.Display.get_default()
for i in range(gdisplay.get_n_monitors()):
geom = gdisplay.get_monitor(i).get_geometry()
window = Gtk.Window()
window.set_app_paintable(True)
window.set_type_hint(Gdk.WindowTypeHint.SPLASHSCREEN)
window.set_visual(window.get_screen().get_rgba_visual())
window.set_default_size(geom.width, geom.height)
window.move(geom.x, geom.y)
window.connect("draw", on_draw, options, background, now)
window.connect("delete-event", Gtk.main_quit)
window.connect("realize", on_realize)
window.show_all()
# Watch for locker window
xdisplay = display.Display()
root = xdisplay.screen().root
root.change_attributes(event_mask=X.SubstructureNotifyMask)
channel = GLib.IOChannel.unix_new(xdisplay.fileno())
channel.set_encoding(None)
channel.set_buffered(False)
GLib.io_add_watch(
channel,
GLib.PRIORITY_DEFAULT,
GLib.IOCondition.IN,
on_xevent,
xdisplay,
options.locker,
)
xdisplay.pending_events() # otherwise, socket is inactive
# Main loop
Gtk.main()