2021-08-07 14:11:15 +02:00
|
|
|
#!/usr/bin/env python3
|
2021-08-03 23:43:45 +02:00
|
|
|
|
2021-08-07 08:48:01 +02:00
|
|
|
"""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.
|
|
|
|
"""
|
2021-08-03 23:43:45 +02:00
|
|
|
|
2021-08-04 00:04:52 +02:00
|
|
|
# It assumes we are using a compositor.
|
2021-08-03 23:43:45 +02:00
|
|
|
|
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
|
2021-08-03 23:43:45 +02:00
|
|
|
import cairo
|
|
|
|
import argparse
|
2021-08-07 08:48:01 +02:00
|
|
|
import threading
|
2021-09-07 21:52:50 +02:00
|
|
|
import time
|
2021-09-29 06:56:08 +02:00
|
|
|
import math
|
2021-08-07 08:48:01 +02:00
|
|
|
from Xlib import display, X
|
|
|
|
from Xlib.error import BadWindow
|
|
|
|
from Xlib.protocol.event import MapNotify
|
|
|
|
|
|
|
|
|
2021-08-07 09:25:37 +02:00
|
|
|
def on_xevent(source, condition, xdisplay, locker):
|
|
|
|
while xdisplay.pending_events():
|
2021-08-07 08:48:01 +02:00
|
|
|
event = xdisplay.next_event()
|
|
|
|
if event.type != X.MapNotify:
|
|
|
|
continue
|
|
|
|
try:
|
|
|
|
wmclass = event.window.get_wm_class()
|
2021-08-07 14:11:59 +02:00
|
|
|
except BadWindow:
|
2021-08-07 08:48:01 +02:00
|
|
|
continue
|
|
|
|
if wmclass and wmclass[1] == locker:
|
2021-08-07 09:25:37 +02:00
|
|
|
Gtk.main_quit()
|
|
|
|
return False
|
|
|
|
return True
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
|
2021-08-06 21:56:16 +02:00
|
|
|
def on_realize(widget):
|
2021-08-05 21:52:37 +02:00
|
|
|
window = widget.get_window()
|
|
|
|
window.set_override_redirect(True)
|
|
|
|
|
|
|
|
|
2021-09-07 21:52:50 +02:00
|
|
|
def on_draw(widget, event, options, background, start):
|
2021-12-08 00:28:21 +01:00
|
|
|
x, y = widget.get_position()
|
|
|
|
wwidth, wheight = widget.get_size()
|
|
|
|
delta = options.end_opacity - options.start_opacity
|
|
|
|
elapsed = time.monotonic() - start
|
|
|
|
current = easing_functions[options.easing_function](elapsed / options.delay)
|
|
|
|
opacity = delta * current + options.start_opacity
|
|
|
|
cctx = event
|
|
|
|
|
|
|
|
# Background
|
|
|
|
cctx.set_operator(cairo.OPERATOR_SOURCE)
|
|
|
|
if not background:
|
|
|
|
cctx.set_source_rgba(0, 0, 0, opacity)
|
|
|
|
cctx.paint()
|
2021-08-03 23:43:45 +02:00
|
|
|
else:
|
2021-12-08 00:28:21 +01:00
|
|
|
scale = widget.get_scale_factor()
|
|
|
|
bg = background.new_subpixbuf(x, y, wwidth * scale, wheight * scale)
|
|
|
|
cctx.save()
|
|
|
|
cctx.scale(1 / scale, 1 / scale)
|
|
|
|
Gdk.cairo_set_source_pixbuf(cctx, bg, 0, 0)
|
|
|
|
cctx.paint_with_alpha(opacity)
|
|
|
|
cctx.restore()
|
|
|
|
|
|
|
|
# Remaining time
|
|
|
|
remaining = str(round(options.delay - elapsed))
|
|
|
|
cctx.select_font_face(
|
|
|
|
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)
|
|
|
|
cctx.set_source_rgba(0, 0, 0, opacity * 2)
|
|
|
|
cctx.set_line_width(4)
|
|
|
|
cctx.text_path(remaining)
|
|
|
|
cctx.stroke()
|
|
|
|
|
|
|
|
|
|
|
|
def refresh(window, options, start):
|
|
|
|
window.queue_draw()
|
|
|
|
elapsed = time.monotonic() - start
|
|
|
|
if elapsed < options.delay:
|
|
|
|
next_step = min(options.step, options.delay - elapsed)
|
|
|
|
GLib.timeout_add(options.step * 1000, refresh, window, options, start)
|
2021-08-03 23:43:45 +02:00
|
|
|
|
2021-09-29 06:56:08 +02:00
|
|
|
# See: https://easings.net/
|
|
|
|
easing_functions = {
|
|
|
|
"none": lambda x: x,
|
2021-09-29 15:30:26 +02:00
|
|
|
"out-circ": lambda x: math.sqrt(1 - pow(x - 1, 2)),
|
|
|
|
"out-sine": lambda x: math.sin(x * math.pi / 2),
|
|
|
|
"out-cubic": lambda x: 1 - pow(1 - x, 3),
|
|
|
|
"out-quint": lambda x: 1 - pow(1 - x, 5),
|
|
|
|
"out-expo": lambda x: 1 - pow(2, -10 * x),
|
|
|
|
"out-quad": lambda x: 1 - (1 - x) * (1 - x),
|
|
|
|
"out-bounce": (
|
2021-09-29 07:12:03 +02:00
|
|
|
lambda n1, d1: lambda x: n1 * x * x
|
|
|
|
if x < 1 / d1
|
|
|
|
else n1 * pow(x - 1.5 / d1, 2) + 0.75
|
|
|
|
if x < 2 / d1
|
|
|
|
else n1 * pow(x - 2.25 / d1, 2) + 0.9375
|
|
|
|
if (x < 2.5 / d1)
|
|
|
|
else n1 * pow(x - 2.625 / d1, 2) + 0.984375
|
|
|
|
)(7.5625, 2.75),
|
2021-09-29 15:30:26 +02:00
|
|
|
"out-elastic": (
|
2021-09-29 06:56:08 +02:00
|
|
|
lambda x: pow(2, -10 * x) * math.sin((x * 10 - 0.75) * (2 * math.pi) / 3) + 1
|
|
|
|
),
|
2021-09-29 15:30:26 +02:00
|
|
|
"inout-quad": lambda x: 2 * x * x if x < 0.5 else 1 - pow(-2 * x + 2, 2) / 2,
|
|
|
|
"inout-quart": (
|
2021-09-29 07:12:03 +02:00
|
|
|
lambda x: 8 * x * x * x * x if x < 0.5 else 1 - pow(-2 * x + 2, 4) / 2
|
|
|
|
),
|
2021-09-29 15:30:26 +02:00
|
|
|
"inout-expo": (
|
2021-09-29 07:12:03 +02:00
|
|
|
lambda x: pow(2, 20 * x - 10) / 2 if x < 0.5 else (2 - pow(2, -20 * x + 10)) / 2
|
|
|
|
),
|
2021-09-29 15:30:26 +02:00
|
|
|
"inout-bounce": (
|
|
|
|
lambda x: (1 - easing_functions["out-bounce"](1 - 2 * x)) / 2
|
2021-09-29 07:12:03 +02:00
|
|
|
if x < 0.5
|
2021-09-29 15:30:26 +02:00
|
|
|
else (1 + easing_functions["out-bounce"](2 * x - 1)) / 2
|
2021-09-29 07:12:03 +02:00
|
|
|
),
|
2021-09-29 06:56:08 +02:00
|
|
|
}
|
|
|
|
|
2021-08-03 23:43:45 +02:00
|
|
|
if __name__ == "__main__":
|
2021-09-07 21:52:50 +02:00
|
|
|
now = time.monotonic()
|
2021-08-03 23:43:45 +02:00
|
|
|
parser = argparse.ArgumentParser()
|
2021-08-07 08:48:01 +02:00
|
|
|
add = parser.add_argument
|
2021-09-06 17:49:45 +02:00
|
|
|
add("--start-opacity", type=float, default=0, help="initial opacity")
|
|
|
|
add("--end-opacity", type=float, default=1, help="final opacity")
|
2021-08-07 08:48:01 +02:00
|
|
|
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")
|
2021-12-07 20:34:15 +01:00
|
|
|
add("--locker", default="xsecurelock", 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",
|
2021-11-29 23:03:04 +01:00
|
|
|
default="out-circ",
|
2021-09-29 06:56:08 +02:00
|
|
|
choices=easing_functions.keys(),
|
|
|
|
help="easing function for opacity",
|
|
|
|
)
|
2021-08-03 23:43:45 +02:00
|
|
|
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
|
|
|
|
|
2021-08-07 09:25:37 +02:00
|
|
|
# Setup dimmer windows on each monitor
|
2021-08-07 08:48:01 +02:00
|
|
|
gdisplay = Gdk.Display.get_default()
|
|
|
|
for i in range(gdisplay.get_n_monitors()):
|
|
|
|
geom = gdisplay.get_monitor(i).get_geometry()
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
window = Gtk.Window()
|
|
|
|
window.set_app_paintable(True)
|
|
|
|
window.set_type_hint(Gdk.WindowTypeHint.SPLASHSCREEN)
|
|
|
|
window.set_visual(window.get_screen().get_rgba_visual())
|
|
|
|
|
2021-08-05 21:52:37 +02:00
|
|
|
window.set_default_size(geom.width, geom.height)
|
|
|
|
window.move(geom.x, geom.y)
|
|
|
|
|
2021-09-07 21:52:50 +02:00
|
|
|
window.connect("draw", on_draw, options, background, now)
|
2021-08-06 00:16:58 +02:00
|
|
|
window.connect("delete-event", Gtk.main_quit)
|
2021-08-06 21:56:16 +02:00
|
|
|
window.connect("realize", on_realize)
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
window.show_all()
|
|
|
|
|
2021-12-08 00:28:21 +01:00
|
|
|
# Schedule refresh with window.queue_draw()
|
|
|
|
refresh(window, options, now)
|
|
|
|
|
2021-08-07 09:25:37 +02:00
|
|
|
# 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,
|
2021-08-07 08:48:01 +02:00
|
|
|
)
|
2021-08-07 09:25:37 +02:00
|
|
|
xdisplay.pending_events() # otherwise, socket is inactive
|
|
|
|
|
|
|
|
# Main loop
|
2021-08-03 23:43:45 +02:00
|
|
|
Gtk.main()
|