Files
hypr-xp/bin/xp-volume-popup

178 lines
5.7 KiB
Python
Executable File

#!/usr/bin/env python3
"""Classic Windows XP sndvol32 tray Volume slider popup (GTK3 + GtkLayerShell)."""
import fcntl
import os
import re
import signal
import subprocess
import sys
os.environ.setdefault("DCONF_PROFILE", "/home/carsten/.config/hypr/dconf/profile")
PID_FILE = f"/run/user/{os.getuid()}/xp-volume-popup.pid"
SOUND_SCRIPT = "/home/carsten/.config/hypr/scripts/xp-sound.sh"
# Toggle behavior: if already open, close it and exit
if os.path.exists(PID_FILE):
try:
old_pid = int(open(PID_FILE).read().strip())
if old_pid != os.getpid():
os.kill(old_pid, signal.SIGTERM)
os.unlink(PID_FILE)
sys.exit(0)
except (OSError, ValueError):
pass
open(PID_FILE, "w").write(str(os.getpid()))
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("Gdk", "3.0")
gi.require_version("GtkLayerShell", "0.1")
from gi.repository import Gtk, Gdk, GLib, GtkLayerShell
POPUP_CSS = b"""
window#xp-vol-win {
background-color: transparent;
}
.xp-vol-outer {
background-color: #ECE9D8;
border: 1px solid #003C74;
border-radius: 2px;
box-shadow: 2px 3px 6px rgba(0, 0, 0, 0.35);
margin: 4px;
}
.xp-vol-bevel {
background-color: #ECE9D8;
border-top: 1px solid #FFFFFF;
border-left: 1px solid #FFFFFF;
border-right: 1px solid #ACA899;
border-bottom: 1px solid #ACA899;
padding: 8px 10px 8px 10px;
}
.xp-vol-title {
font-family: "Tahoma", sans-serif;
font-size: 8.5pt;
color: #000000;
}
.xp-vol-pct {
font-family: "Tahoma", sans-serif;
font-size: 8pt;
color: #444444;
}
"""
def get_volume_state():
try:
out = subprocess.check_output(["wpctl", "get-volume", "@DEFAULT_AUDIO_SINK@"], text=True, timeout=1.0)
m = re.search(r"([0-9.]+)", out)
pct = int(round(float(m.group(1)) * 100)) if m else 50
muted = "[MUTED]" in out
return max(0, min(100, pct)), muted
except Exception:
return 50, False
class XPVolumePopup(Gtk.Window):
def __init__(self):
super().__init__(type=Gtk.WindowType.TOPLEVEL)
self.set_name("xp-vol-win")
self.set_decorated(False)
self.set_resizable(False)
self.set_app_paintable(True)
visual = self.get_screen().get_rgba_visual()
if visual:
self.set_visual(visual)
GtkLayerShell.init_for_window(self)
GtkLayerShell.set_layer(self, GtkLayerShell.Layer.OVERLAY)
GtkLayerShell.set_namespace(self, "xp-volume-popup")
GtkLayerShell.set_anchor(self, GtkLayerShell.Edge.BOTTOM, True)
GtkLayerShell.set_anchor(self, GtkLayerShell.Edge.RIGHT, True)
GtkLayerShell.set_margin(self, GtkLayerShell.Edge.BOTTOM, 32)
GtkLayerShell.set_margin(self, GtkLayerShell.Edge.RIGHT, 86)
GtkLayerShell.set_keyboard_mode(self, GtkLayerShell.KeyboardMode.ON_DEMAND)
prov = Gtk.CssProvider()
prov.load_from_data(POPUP_CSS)
Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), prov, Gtk.STYLE_PROVIDER_PRIORITY_USER)
outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)
outer.get_style_context().add_class("xp-vol-outer")
bevel = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6)
bevel.get_style_context().add_class("xp-vol-bevel")
outer.pack_start(bevel, True, True, 0)
lbl = Gtk.Label(label="Volume")
lbl.get_style_context().add_class("xp-vol-title")
bevel.pack_start(lbl, False, False, 0)
pct, muted = get_volume_state()
self.syncing = True
self.scale = Gtk.Scale.new_with_range(Gtk.Orientation.VERTICAL, 0, 100, 2)
self.scale.set_inverted(True)
self.scale.set_draw_value(False)
self.scale.set_size_request(36, 118)
self.scale.set_value(pct)
self.scale.connect("value-changed", self._on_vol_changed)
self.scale.connect("button-release-event", self._on_vol_released)
bevel.pack_start(self.scale, True, True, 0)
self.pct_lbl = Gtk.Label(label=f"{pct}%")
self.pct_lbl.get_style_context().add_class("xp-vol-pct")
bevel.pack_start(self.pct_lbl, False, False, 0)
self.mute_cb = Gtk.CheckButton(label="Mute")
self.mute_cb.set_active(muted)
self.mute_cb.connect("toggled", self._on_mute_toggled)
bevel.pack_start(self.mute_cb, False, False, 0)
self.syncing = False
self.add(outer)
self.connect("key-press-event", lambda _w, ev: self.quit() if ev.keyval == Gdk.KEY_Escape else False)
self.connect("focus-out-event", lambda *_: self.quit())
GLib.unix_signal_add(GLib.PRIORITY_DEFAULT, signal.SIGTERM, lambda: (self.quit(), False)[1])
def _on_vol_changed(self, scale):
if self.syncing:
return
val = int(round(scale.get_value()))
self.pct_lbl.set_text(f"{val}%")
subprocess.Popen(["wpctl", "set-volume", "@DEFAULT_AUDIO_SINK@", f"{val}%"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def _on_vol_released(self, _w, _ev):
if not self.mute_cb.get_active():
subprocess.Popen([SOUND_SCRIPT, "ding"],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
return False
def _on_mute_toggled(self, cb):
if self.syncing:
return
state = "1" if cb.get_active() else "0"
subprocess.Popen(["wpctl", "set-mute", "@DEFAULT_AUDIO_SINK@", state],
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def quit(self):
try:
os.unlink(PID_FILE)
except OSError:
pass
Gtk.main_quit()
return False
def main():
win = XPVolumePopup()
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()