dot

vizie's NixOS desktop: Hyprland, CachyOS kernel and gaming tweaks

54 lines1614 bytesPythonexecutable
1#!/usr/bin/env python3
2"""Waybar module: number of notifications in dunst's history, as one JSON line per change.
3
4Prints an empty text when there are none, so Waybar hides the badge. Event-driven (dunst
5announces changes to its history length over D-Bus), with a slow poll as a safety net.
6The number is capped at what the Eww notifications panel shows.
7"""
8import json
9
10import gi
11
12gi.require_version("Gio", "2.0")
13from gi.repository import Gio, GLib # noqa: E402
14
15LIMIT = 10 # keep in sync with eww/scripts/notifications.py
16BUS = "org.freedesktop.Notifications"
17PATH = "/org/freedesktop/Notifications"
18IFACE = "org.dunstproject.cmd0"
19
20last = None
21
22
23def emit(count):
24 global last
25 text = str(min(count, LIMIT)) if count > 0 else ""
26 if text != last:
27 last = text
28 print(json.dumps({"text": text}), flush=True)
29
30
31def read(connection):
32 try:
33 reply = connection.call_sync(
34 BUS, PATH, "org.freedesktop.DBus.Properties", "Get",
35 GLib.Variant("(ss)", (IFACE, "historyLength")),
36 GLib.VariantType("(v)"), Gio.DBusCallFlags.NO_AUTO_START, 2000, None)
37 emit(reply.unpack()[0])
38 except GLib.Error:
39 emit(0) # dunst not running
40 return True
41
42
43def main():
44 connection = Gio.bus_get_sync(Gio.BusType.SESSION, None)
45 connection.signal_subscribe(
46 BUS, "org.freedesktop.DBus.Properties", "PropertiesChanged", PATH, None,
47 Gio.DBusSignalFlags.NONE, lambda *args: read(connection))
48 read(connection)
49 GLib.timeout_add_seconds(5, read, connection)
50 GLib.MainLoop().run()
51
52
53if __name__ == "__main__":
54 main()