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-08-06 21:57:00 +02:00
|
|
|
from gi.repository import Gtk, Gdk, GLib
|
2021-08-03 23:43:45 +02:00
|
|
|
import cairo
|
|
|
|
import argparse
|
2021-08-07 08:48:01 +02:00
|
|
|
import threading
|
|
|
|
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()
|
|
|
|
except error.BadWindow:
|
|
|
|
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-08-06 21:56:16 +02:00
|
|
|
def on_draw(widget, event, options, elapsed):
|
2021-08-07 14:10:43 +02:00
|
|
|
def _dim():
|
|
|
|
r = cairo.Region(cairo.RectangleInt(0, 0, *widget.get_default_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-08-04 11:27:27 +02:00
|
|
|
# Background
|
2021-08-07 08:48:01 +02:00
|
|
|
delta = options.end_opacity - options.start_opacity
|
2021-08-04 11:27:27 +02:00
|
|
|
current = elapsed[0] / options.delay
|
2021-08-07 08:48:01 +02:00
|
|
|
opacity = delta * current + options.start_opacity
|
2021-08-07 14:10:43 +02:00
|
|
|
cctx.set_source_rgba(0, 0, 0, opacity)
|
|
|
|
cctx.set_operator(cairo.OPERATOR_SOURCE)
|
|
|
|
cctx.paint()
|
2021-08-04 11:27:27 +02:00
|
|
|
|
|
|
|
# Remaining time
|
|
|
|
remaining = str(round(options.delay - elapsed[0]))
|
|
|
|
wwidth, wheight = widget.get_default_size()
|
2021-08-07 14:10:43 +02:00
|
|
|
cctx.set_source_rgba(1, 1, 1, opacity)
|
|
|
|
cctx.select_font_face(
|
2021-08-04 11:27:27 +02:00
|
|
|
options.font, cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_BOLD
|
|
|
|
)
|
2021-08-07 14:10:43 +02:00
|
|
|
cctx.set_font_size(wheight // 4)
|
|
|
|
_, _, twidth, theight, _, _ = cctx.text_extents(remaining)
|
|
|
|
cctx.move_to(wwidth // 2 - twidth // 2, wheight // 2 + theight // 2)
|
|
|
|
cctx.show_text(remaining)
|
2021-08-04 11:27:27 +02:00
|
|
|
|
|
|
|
# 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:
|
2021-08-07 14:10:43 +02:00
|
|
|
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-07 14:10:43 +02:00
|
|
|
dim(event)
|
2021-08-03 23:43:45 +02:00
|
|
|
else:
|
2021-08-04 11:27:27 +02:00
|
|
|
# Timers already running, just repaint
|
2021-08-07 14:10:43 +02:00
|
|
|
dim(event, once=True)
|
2021-08-03 23:43:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
2021-08-07 08:48:01 +02:00
|
|
|
add = parser.add_argument
|
|
|
|
add("--start-opacity", type=float, default=0.2, 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-08-03 23:43:45 +02:00
|
|
|
options = parser.parse_args()
|
|
|
|
|
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
|
|
|
once = []
|
|
|
|
|
|
|
|
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-08-06 21:56:16 +02:00
|
|
|
window.connect("draw", on_draw, options, [])
|
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-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()
|