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 {focused: 0, windows: []} { |line, state| let event = $line | from json let event_type = $event | columns | first mut windows = $state.windows mut current = $state.focused match $event_type { "WindowsChanged" => { for w in $event.WindowsChanged.windows { if $w.pid != null and $w.is_focused and $w.pid != $current { notify-scheduler $w.pid $current = $w.pid } if $w.pid != null { let existing = $windows | where id == $w.id if ($existing | is-empty) { $windows = ($windows | append {id: $w.id, pid: $w.pid}) } } } } "WindowOpenedOrChanged" => { let w = $event.WindowOpenedOrChanged.window if $w.is_focused and $w.pid != null and $w.pid != $current { notify-scheduler $w.pid $current = $w.pid } if $w.pid != null { let existing = $state.windows | where id == $w.id if ($existing | is-empty) { $windows = $windows | append {id: $w.id, pid: $w.pid} } } } "WindowFocusChanged" => { let focused_id = $event.WindowFocusChanged.id if $focused_id != null { let match = $state.windows | where id == $focused_id if not ($match | is-empty) { let focused_pid = $match.0.pid if $focused_pid != null and $focused_pid != $current { notify-scheduler $focused_pid $current = $focused_pid } } } } "WindowClosed" => { $windows = $windows | where id != $event.WindowClosed.id } _ => () } {focused: $current, windows: $windows} } }