From 64df9e1171989229cc81cd65c6c3d3467ab878f3 Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Mon, 21 Oct 2024 18:48:36 -0500 Subject: [PATCH] complete reinfra to prevent weird race scenarios --- src/hyprland.nim | 69 +++++++++++++++++++++++++++++++++++++----------- src/hyprman.nim | 15 +++++------ src/lib.nim | 3 ++- src/swww.nim | 1 + 4 files changed, 64 insertions(+), 24 deletions(-) diff --git a/src/hyprland.nim b/src/hyprland.nim index 0ce09fc..3a7f1c8 100644 --- a/src/hyprland.nim +++ b/src/hyprland.nim @@ -1,6 +1,8 @@ -import std/[os, strformat, strutils, streams, tables, net, sugar] +import std/[ + os, osproc, strformat, strutils, + streams, tables, net, sugar, times +] import ./[lib, swww] -export tables import yaml, jsony type @@ -119,20 +121,16 @@ const redrawEvents = [ "movewindowv2", # emitted when a window is moved to a workspace WINDOWADDRESS,WORKSPACEID,WORKSPACENAME ] -proc handleHyprEvent(event: string) = - let - s = event.split(">>", 1) - event = s[0] +const monitorChangeEvents = [ + "monitorremoved", # emitted when a monitor is removed (disconnected) MONITORNAME + "monitoraddedv2", # emitted when a monitor is added (connected) MONITORID,MONITORNAME,MONITORDESCRIPTION +] - if event in ["monitoraddedv2", "monitorremoved"]: - # TODO: open as many bars as necessary depending on num monitors - notify("monitor added") - oneShotSwww() +proc parseEvent(line: string): (string, string) = + let s = line.split(">>", 1) + result = (s[0], s[1]) - if event in redrawEvents: - writeEwwClasses() - -proc watchHyprland*() = +proc streamEwwClasses*() = let socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP) try: socket.connectUnix(hyprSocket2) @@ -144,4 +142,45 @@ proc watchHyprland*() = while true: var line: string socket.readLine line - handleHyprEvent line + let (e, _) = parseEvent(line) + if e in redrawEvents: + writeEwwClasses() + + +proc handleHyprEvent(e: string) = + if e in monitorChangeEvents: + notify("detected monitor change") + oneShotSwww() + for i in 0.. swwwTimeout: + oneShotSwww() + last = current + +proc watchHyprland*() = + let socket = newSocket(AF_UNIX, SOCK_STREAM, IPPROTO_IP) + try: + socket.connectUnix(hyprSocket2) + except OSError: + raise newException( + HyprlandDefect, "Could not connect to Hyprland IPC UNIX path; is Hyprland running?" + ) + var lastSwww= now() + while true: + var line: string + socket.readLine line + let (e, _) = parseEvent(line) + handleHyprEvent e + maybeTriggerSwww lastSwww + echo lastSwww + + + + + diff --git a/src/hyprman.nim b/src/hyprman.nim index de06a6d..5370c46 100644 --- a/src/hyprman.nim +++ b/src/hyprman.nim @@ -1,12 +1,11 @@ ## hyprman, the hyprland companion -import std/[os, osproc, strformat] +import std/[osproc, strformat] import hwylterm/cligen, cligen import ./[ hyprland, lib, mako, - swww ] hwylCli(clCfg) @@ -19,13 +18,13 @@ proc start() = if code != 0: notify(fmt"failed to open eww bar{i}") -proc watch() = +proc eww() = ## watch hyprland events for eww class changes - watchHyprland() + streamEwwClasses() -proc swww() = - ## set swww to cycle through wallpapers - persistentSwww() +proc watch() = + ## handle monitor changes on hyprland + watchHyprland() when isMainModule: const @@ -38,6 +37,6 @@ when isMainModule: dispatchMulti( [makoCmd, usage = clCfg.use, help = makoHelp, cmdName = "mako",], [start , usage = clCfg.use], + [eww , usage = clCfg.use], [watch , usage = clCfg.use], - [swww , usage = clCfg.use], ) diff --git a/src/lib.nim b/src/lib.nim index ad96496..6a4a196 100644 --- a/src/lib.nim +++ b/src/lib.nim @@ -5,8 +5,9 @@ export tables import yaml proc notify*(message: string) = - var cmd = "notify-send --app-name=hyprman --transient hyprman " + var cmd = "notify-send --app-name=hyprman --transient hyprman \"" cmd.add message + cmd.add "\"" discard execCmd(cmd) type diff --git a/src/swww.nim b/src/swww.nim index 35f819a..4bc4790 100644 --- a/src/swww.nim +++ b/src/swww.nim @@ -45,3 +45,4 @@ proc persistentSwww*() = setImg(gallery.sample(), monitor) sleep 5 * oneMinute +