2021-08-03 23:43:45 +02:00
|
|
|
#!/usr/bin/env -S python3 -W ignore::DeprecationWarning
|
2021-08-04 08:31:17 +02:00
|
|
|
# -*- python -*-
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
"""Simple dimmer for xss-lock."""
|
|
|
|
|
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-08-03 23:43:45 +02:00
|
|
|
from gi.repository import Gtk, Gdk, GLib
|
|
|
|
import cairo
|
|
|
|
import argparse
|
|
|
|
|
|
|
|
|
2021-08-05 21:52:37 +02:00
|
|
|
def realize_event(widget):
|
|
|
|
window = widget.get_window()
|
|
|
|
window.set_override_redirect(True)
|
|
|
|
|
|
|
|
|
|
|
|
def draw_event(widget, event, options, elapsed):
|
2021-08-03 23:43:45 +02:00
|
|
|
def dim(once=False):
|
|
|
|
cr = Gdk.cairo_create(window)
|
2021-08-04 11:27:27 +02:00
|
|
|
|
|
|
|
# Background
|
|
|
|
delta = options.max_opacity - options.min_opacity
|
|
|
|
current = elapsed[0] / options.delay
|
|
|
|
opacity = delta * current + options.min_opacity
|
|
|
|
cr.set_source_rgba(0, 0, 0, opacity)
|
2021-08-03 23:43:45 +02:00
|
|
|
cr.set_operator(cairo.OPERATOR_SOURCE)
|
|
|
|
cr.paint()
|
2021-08-04 11:27:27 +02:00
|
|
|
|
|
|
|
# Remaining time
|
|
|
|
remaining = str(round(options.delay - elapsed[0]))
|
|
|
|
wwidth, wheight = widget.get_default_size()
|
|
|
|
cr.set_source_rgba(1, 1, 1, opacity)
|
|
|
|
cr.select_font_face(
|
|
|
|
options.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
|
|
|
|
)
|
|
|
|
cr.set_font_size(wheight // 4)
|
|
|
|
_, _, twidth, theight, _, _ = cr.text_extents(remaining)
|
|
|
|
cr.move_to(wwidth // 2 - twidth // 2, wheight // 2 + theight // 2)
|
|
|
|
cr.show_text(remaining)
|
|
|
|
|
|
|
|
# Rearm timer
|
2021-08-03 23:43:45 +02:00
|
|
|
if not once:
|
2021-08-04 11:27:27 +02:00
|
|
|
elapsed[0] += options.step
|
|
|
|
if elapsed[0] <= options.delay:
|
|
|
|
GLib.timeout_add(options.step * 1000, dim)
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
window = widget.get_window()
|
2021-08-04 11:27:27 +02:00
|
|
|
if not elapsed:
|
2021-08-03 23:43:45 +02:00
|
|
|
# First time we are called.
|
2021-08-04 11:27:27 +02:00
|
|
|
elapsed.append(0)
|
2021-08-03 23:43:45 +02:00
|
|
|
dim()
|
|
|
|
else:
|
2021-08-04 11:27:27 +02:00
|
|
|
# Timers already running, just repaint
|
2021-08-03 23:43:45 +02:00
|
|
|
dim(once=True)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("--min-opacity", type=float, default=0.2)
|
|
|
|
parser.add_argument("--max-opacity", type=float, default=1)
|
2021-08-04 11:27:27 +02:00
|
|
|
parser.add_argument("--step", type=float, default=0.1)
|
2021-08-03 23:43:45 +02:00
|
|
|
parser.add_argument("--delay", type=float, default=10)
|
2021-08-04 11:27:27 +02:00
|
|
|
parser.add_argument("--font", default="DejaVu Sans")
|
2021-08-03 23:43:45 +02:00
|
|
|
options = parser.parse_args()
|
|
|
|
|
|
|
|
display = Gdk.Display.get_default()
|
|
|
|
for i in range(display.get_n_monitors()):
|
|
|
|
geom = display.get_monitor(i).get_geometry()
|
|
|
|
once = []
|
|
|
|
|
|
|
|
window = Gtk.Window()
|
|
|
|
window.set_wmclass("dimmer", "Dimmer")
|
|
|
|
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)
|
|
|
|
|
|
|
|
window.connect("draw", draw_event, options, [])
|
2021-08-06 00:16:58 +02:00
|
|
|
window.connect("delete-event", Gtk.main_quit)
|
2021-08-06 00:19:41 +02:00
|
|
|
window.connect("realize", realize_event)
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
window.show_all()
|
|
|
|
|
|
|
|
Gtk.main()
|