def main [] { def notify-scheduler [proc_id: int] { print $"Forwarding PID ($proc_id) to scheduler" busctl call com.system76.Scheduler /com/system76/Scheduler com.system76.Scheduler SetForegroundProcess u $"($proc_id)" } print "niri-s76-bridge started" niri msg -j event-stream | lines | reduce --fold [] { |line, windows| let event = $line | from json let event_type = $event | columns | first match $event_type { "WindowsChanged" => { mut acc = $windows for w in $event.WindowsChanged.windows { if $w.pid != null and $w.is_focused { notify-scheduler $w.pid } if $w.pid != null { let existing = $acc | where id == $w.id if ($existing | is-empty) { $acc = ($acc | append {id: $w.id, pid: $w.pid}) } } } $acc } "WindowOpenedOrChanged" => { let w = $event.WindowOpenedOrChanged.window if $w.is_focused and $w.pid != null { notify-scheduler $w.pid } if $w.pid != null { let existing = $windows | where id == $w.id if ($existing | is-empty) { $windows | append {id: $w.id, pid: $w.pid} } else { $windows } } else { $windows } } "WindowFocusChanged" => { let focused_id = $event.WindowFocusChanged.id if $focused_id != null { let match = $windows | where id == $focused_id if not ($match | is-empty) { let focused_pid = $match.0.pid if $focused_pid != null { notify-scheduler $focused_pid } } } $windows } "WindowClosed" => { $windows | where id != $event.WindowClosed.id } _ => $windows } } }