From dd62c8a2d07a149f71e3c93d945d524ce068db07 Mon Sep 17 00:00:00 2001
From: Marty Oehme <marty.oehme@gmail.com>
Date: Sun, 23 Feb 2025 00:53:49 +0100
Subject: [PATCH] river: Automatically choose layout engine

Prefers filtile as layouting engine but falls back to rivertile if it
does not find it.
---
 desktop/.config/river/init | 49 ++++++++++++++++++++++----------------
 1 file changed, 29 insertions(+), 20 deletions(-)

diff --git a/desktop/.config/river/init b/desktop/.config/river/init
index dcf40a2..093fc0b 100755
--- a/desktop/.config/river/init
+++ b/desktop/.config/river/init
@@ -3,11 +3,24 @@
 mod="Mod4"
 modemod="Mod1"
 term=${TERMINAL:-foot}
-layout="filtile"
 time_to_lockscreen=300
 time_to_screendim=600
 time_to_suspend=900
 
+layout_cmd="rivertile"
+layout_opt="-main-ratio 0.65\
+        -outer-padding 0\
+        -view-padding 6"
+
+if command -v filtile >/dev/null 2>&1; then
+    layout_cmd="filtile"
+    layout_opt="--tags all --output all main-ratio 0.65,\
+    --tags all --output all view-padding 6,\
+    --tags all --output all outer-padding 0,\
+    --tags all --output all smart-padding on,\
+    --tags all --output all smart-padding 0"
+fi
+
 NO_RESTART="$1"
 should_start() { # 1=program binary name
     # not running, start
@@ -129,10 +142,10 @@ riverctl map normal $mod+Shift K swap previous
 riverctl map normal $mod+Shift Return zoom
 
 # change layout orientation
-riverctl map normal $mod Up send-layout-cmd $layout "main-location top"
-riverctl map normal $mod Right send-layout-cmd $layout "main-location right"
-riverctl map normal $mod Down send-layout-cmd $layout "main-location bottom"
-riverctl map normal $mod Left send-layout-cmd $layout "main-location left"
+riverctl map normal $mod Up send-layout-cmd $layout_cmd "main-location top"
+riverctl map normal $mod Right send-layout-cmd $layout_cmd "main-location right"
+riverctl map normal $mod Down send-layout-cmd $layout_cmd "main-location bottom"
+riverctl map normal $mod Left send-layout-cmd $layout_cmd "main-location left"
 
 # snap views to screen edges
 riverctl map normal $mod+Control H snap left
@@ -143,8 +156,8 @@ riverctl map normal $mod+Control L snap right
 # Mod+F to toggle fullscreen
 riverctl map normal $mod F toggle-fullscreen
 # if we are running filtile we also have access to monocle mode
-if [ "$layout" = "filtile" ]; then
-    riverctl map normal $mod+Shift F spawn "riverctl send-layout-cmd $layout monocle"
+if [ "$layout_cmd" = "filtile" ]; then
+    riverctl map normal $mod+Shift F spawn "riverctl send-layout-cmd $layout_cmd monocle"
 fi
 
 riverctl map normal $mod+Shift v toggle-float
@@ -153,8 +166,8 @@ riverctl map normal $mod+Shift v toggle-float
 # Make all connected outputs show the desktop and no windows at all
 riverctl map normal $mod+Shift M spawn 'for i in $(wlopm | wc -l); do riverctl set-focused-tags $((1 << 10)); riverctl focus-output next; done; riverctl set-focused-tags $((1 << 10)); riverctl focus-output next'
 
-riverctl map normal $mod+Shift F10 spawn "riverctl send-layout-cmd $layout '--tags all --output all view-padding 0'"
-riverctl map normal $mod F10 spawn "riverctl send-layout-cmd $layout '--tags all --output all view-padding 6'"
+riverctl map normal $mod+Shift F10 spawn "riverctl send-layout-cmd $layout_cmd '--tags all --output all view-padding 0'"
+riverctl map normal $mod F10 spawn "riverctl send-layout-cmd $layout_cmd '--tags all --output all view-padding 6'"
 
 # Mod + Left Mouse Button to move views
 riverctl map-pointer normal $mod BTN_LEFT move-view
@@ -179,11 +192,11 @@ riverctl map -repeat interact_float $mod J resize vertical 100
 riverctl map -repeat interact_float $mod K resize vertical -100
 riverctl map -repeat interact_float $mod L resize horizontal 100
 # decrease/increase the main ratio of layout
-riverctl map interact_float $mod+Shift H send-layout-cmd $layout "main-ratio -0.05"
-riverctl map interact_float $mod+Shift L send-layout-cmd $layout "main-ratio +0.05"
+riverctl map interact_float $mod+Shift H send-layout-cmd $layout_cmd "main-ratio -0.05"
+riverctl map interact_float $mod+Shift L send-layout-cmd $layout_cmd "main-ratio +0.05"
 # increment/decrement the main layout
-riverctl map interact_float $mod+Shift J send-layout-cmd $layout "main-count +1"
-riverctl map interact_float $mod+Shift K send-layout-cmd $layout "main-count -1"
+riverctl map interact_float $mod+Shift J send-layout-cmd $layout_cmd "main-count +1"
+riverctl map interact_float $mod+Shift K send-layout-cmd $layout_cmd "main-count -1"
 # snap views to screen edges
 riverctl map interact_float $mod+Control H snap left
 riverctl map interact_float $mod+Control J snap down
@@ -334,12 +347,8 @@ should_start wl-paste && riverctl spawn "wl-paste -t text --watch clipman store"
 # killall polkit-gnome-authentication-agent-1
 # /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1 &
 # start layouting engine
-killall $layout
-riverctl spawn "$layout    --tags all --output all main-ratio 0.65,\
-    --tags all --output all view-padding 6,\
-    --tags all --output all outer-padding 0,\
-    --tags all --output all smart-padding on,\
-    --tags all --output all smart-padding 0"
+killall $layout_cmd
+riverctl spawn "$layout_cmd $layout_opt"
 # River will send the process group of the init executable SIGTERM on exit.
-riverctl default-layout $layout
+riverctl default-layout $layout_cmd
 brightnessctl set 70%