Files
hypr-xp/bin/xp-screensaver
T

199 lines
7.1 KiB
Python
Executable File

#!/usr/bin/env python3
"""Classic Windows XP Screen Savers: Windows XP Logo (logon.scr), Starfield, Mystify."""
import collections
import math
import os
import random
import signal
import sys
import time
from pathlib import Path
os.environ.setdefault("DCONF_PROFILE", "/home/carsten/.config/hypr/dconf/profile")
import cairo
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_version("GdkPixbuf", "2.0")
gi.require_version("GtkLayerShell", "0.1")
from gi.repository import Gtk, Gdk, GdkPixbuf, GLib, GtkLayerShell
CONF_PATH = Path("/home/carsten/.config/hypr/xp-screensaver.conf")
LOGO_PATH = "/usr/local/share/wintc/logonui/logo.png"
def read_saved_mode():
if CONF_PATH.exists():
val = CONF_PATH.read_text(encoding="utf-8").strip().lower()
if val in ("none", "xp-logo", "starfield", "mystify"):
return val
return "xp-logo"
class XPScreenSaver(Gtk.Window):
def __init__(self, mode):
super().__init__(type=Gtk.WindowType.TOPLEVEL)
self.mode = mode
self.start_time = time.monotonic()
self.init_pointer = None
self.set_decorated(False)
GtkLayerShell.init_for_window(self)
GtkLayerShell.set_layer(self, GtkLayerShell.Layer.OVERLAY)
GtkLayerShell.set_namespace(self, "xp-screensaver")
for edge in (GtkLayerShell.Edge.TOP, GtkLayerShell.Edge.BOTTOM, GtkLayerShell.Edge.LEFT, GtkLayerShell.Edge.RIGHT):
GtkLayerShell.set_anchor(self, edge, True)
GtkLayerShell.set_exclusive_zone(self, -1)
GtkLayerShell.set_keyboard_mode(self, GtkLayerShell.KeyboardMode.EXCLUSIVE)
self.da = Gtk.DrawingArea()
self.da.add_events(Gdk.EventMask.POINTER_MOTION_MASK | Gdk.EventMask.BUTTON_PRESS_MASK)
self.da.connect("draw", self.on_draw)
self.da.connect("motion-notify-event", self.on_motion)
self.da.connect("button-press-event", lambda *_: Gtk.main_quit())
self.add(self.da)
self.connect("key-press-event", lambda *_: Gtk.main_quit())
self.connect("realize", self._hide_cursor)
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM, lambda: (Gtk.main_quit(), False)[1])
# Mode state
self.logo_pb = None
if os.path.exists(LOGO_PATH):
try:
self.logo_pb = GdkPixbuf.Pixbuf.new_from_file(LOGO_PATH)
except Exception:
pass
self.logo_pos = (0.42, 0.42)
self.last_jump = 0.0
self.stars = [[random.uniform(-1, 1), random.uniform(-1, 1), random.uniform(0.05, 1.0)] for _ in range(180)]
# Two Mystify polygons (4 vertices each)
self.polys = []
for col in ((0.0, 0.85, 1.0), (1.0, 0.35, 0.85)):
pts = [[random.uniform(0.1, 0.9), random.uniform(0.1, 0.9),
random.choice((-1, 1)) * random.uniform(0.004, 0.009),
random.choice((-1, 1)) * random.uniform(0.004, 0.009)] for _ in range(4)]
self.polys.append({"color": col, "pts": pts, "trail": collections.deque(maxlen=10)})
interval = 33 if mode in ("starfield", "mystify") else 250
GLib.timeout_add(interval, self.on_tick)
def _hide_cursor(self, _w):
w = self.get_window()
if w:
blank = Gdk.Cursor.new_for_display(w.get_display(), Gdk.CursorType.BLANK_CURSOR)
w.set_cursor(blank)
def on_motion(self, _w, ev):
if time.monotonic() - self.start_time < 0.6:
self.init_pointer = (ev.x, ev.y)
return False
if self.init_pointer is None:
self.init_pointer = (ev.x, ev.y)
return False
dx = ev.x - self.init_pointer[0]
dy = ev.y - self.init_pointer[1]
if dx * dx + dy * dy > 144:
Gtk.main_quit()
return False
def on_tick(self):
if self.mode == "xp-logo":
now = time.monotonic()
if now - self.last_jump >= 4.5:
self.last_jump = now
self.logo_pos = (random.uniform(0.08, 0.78), random.uniform(0.08, 0.78))
self.da.queue_draw()
elif self.mode == "starfield":
for s in self.stars:
s[2] -= 0.018
if s[2] <= 0.02:
s[0] = random.uniform(-1, 1)
s[1] = random.uniform(-1, 1)
s[2] = 1.0
self.da.queue_draw()
elif self.mode == "mystify":
for poly in self.polys:
snap = []
for p in poly["pts"]:
p[0] += p[2]
p[1] += p[3]
if p[0] <= 0.01 or p[0] >= 0.99:
p[2] = -p[2]
p[0] = max(0.01, min(0.99, p[0]))
if p[1] <= 0.01 or p[1] >= 0.99:
p[3] = -p[3]
p[1] = max(0.01, min(0.99, p[1]))
snap.append((p[0], p[1]))
poly["trail"].append(snap)
self.da.queue_draw()
return True
def on_draw(self, _w, cr):
a = self.da.get_allocation()
w, h = a.width, a.height
cr.set_source_rgb(0.0, 0.0, 0.0)
cr.rectangle(0, 0, w, h)
cr.fill()
if self.mode == "xp-logo":
lx = int(self.logo_pos[0] * w)
ly = int(self.logo_pos[1] * h)
if self.logo_pb:
Gdk.cairo_set_source_pixbuf(cr, self.logo_pb, lx, ly)
cr.paint()
cr.set_source_rgb(0.85, 0.88, 0.95)
cr.select_font_face("Tahoma", cairo.FONT_SLANT_NORMAL, cairo.FONT_WEIGHT_NORMAL)
cr.set_font_size(14)
cr.move_to(lx + 18, ly + 104)
cr.show_text("Professional")
elif self.mode == "starfield":
cx, cy = w / 2.0, h / 2.0
for x, y, z in self.stars:
sx = cx + (x / z) * cx * 0.7
sy = cy + (y / z) * cy * 0.7
if 0 <= sx < w and 0 <= sy < h:
b = max(0.25, 1.0 - z)
sz = max(1.2, (1.0 - z) * 3.4)
cr.set_source_rgb(b, b, b)
cr.rectangle(sx, sy, sz, sz)
cr.fill()
elif self.mode == "mystify":
cr.set_line_width(1.4)
for poly in self.polys:
r, g, b = poly["color"]
tr = poly["trail"]
n = len(tr)
for idx, snap in enumerate(tr):
alpha = (idx + 1) / float(max(1, n))
cr.set_source_rgba(r, g, b, alpha * 0.9)
for i, (px, py) in enumerate(snap):
if i == 0:
cr.move_to(px * w, py * h)
else:
cr.line_to(px * w, py * h)
cr.close_path()
cr.stroke()
def main():
mode = read_saved_mode()
for i, arg in enumerate(sys.argv[1:]):
if arg == "--mode" and i + 2 < len(sys.argv):
mode = sys.argv[i + 2].lower()
if mode == "none":
return
win = XPScreenSaver(mode)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()