complete reinfra to prevent weird race scenarios

This commit is contained in:
Daylin Morgan 2024-10-21 18:48:36 -05:00
parent f6c5fc3c48
commit f73bbbd0d3
Signed by: daylin
GPG key ID: 950D13E9719334AD
4 changed files with 58 additions and 24 deletions

View file

@ -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,39 @@ 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..<getMonitors().len:
let (output, code) = execCmdEx("eww open bar" & $i)
if code != 0:
notify("eww failed:\n" & output)
proc maybeTriggerSwww(last: var DateTime) =
const swwwTimeout = 10 # seconds
let current = now()
if (current - last).inSeconds > 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

View file

@ -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],
)

View file

@ -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

View file

@ -45,3 +45,4 @@ proc persistentSwww*() =
setImg(gallery.sample(), monitor)
sleep 5 * oneMinute