#!/bin/sh

# Output configuration ─────────────────────────────────────────────────────────

riverctl spawn 'wlr-randr --output DP-2 --mode 1920x1080@60.000000Hz --pos -1920,0 --transform normal --scale 1.000000 --output eDP-1 --mode 1920x1080@60.000000Hz --pos 0,0 --transform normal --scale 1.000000 --output HDMI-A-1 --mode 1920x1080@60.000000Hz --pos 1920,0 --transform normal --scale 1.000000'

# Startup commands ─────────────────────────────────────────────────────────────

riverctl spawn 'pipewire'
riverctl spawn 'mpd'
riverctl spawn 'start_notify'
riverctl spawn 'wl-paste -t text --watch clipman store'
riverctl spawn 'waybar'
riverctl spawn 'swayidle -w before-sleep swaylock'
riverctl spawn 'mpDris2'
riverctl spawn 'wlwpp'
# riverctl spawn 'emacs --daemon'

# Looks ────────────────────────────────────────────────────────────────────────

# Gtk theme
riverctl spawn 'gsettings set org.gnome.desktop.interface gtk-theme Arc-Dark'
riverctl spawn 'gsettings set org.gnome.desktop.interface icon-theme Papirus-Dark'
riverctl spawn 'gsettings set org.gnome.desktop.interface cursor-theme Breeze_Snow'
riverctl spawn 'gsettings set org.gnome.desktop.interface cursor-size 24'

# Cursor theme
riverctl xcursor-theme Breeze_Snow 24

# Set opacity and fade effect
riverctl opacity 1.0 0.95 0.0 0.1 20

# Gaps
riverctl outer-padding 4
riverctl view-padding 4

# Borders
riverctl border-width 2
riverctl border-color-focused '#eceff4'
riverctl border-color-unfocused '#81a1c1'

# Background (if no wallpaper is presented)
riverctl background-color #2e3440

# Bindings ─────────────────────────────────────────────────────────────────────

# Use the "logo" key as the primary modifier
mod="Mod4"

# Terminals
riverctl map normal $mod Return spawn alacritty
riverctl map normal $mod+Shift Return spawn foot

# Launcher
riverctl map normal $mod D spawn wofi --show=drun
riverctl map normal $mod+Control D spawn wofi --show=run

# Web search
riverctl map normal $mod+Mod1 S spawn searchmenu --wofi
riverctl map normal $mod+Control S spawn bukumenu --wofi

# Manga
riverctl map normal $mod+Mod1 M spawn mangamenu --wofi

# Clipboard manager
riverctl map normal $mod+Mod1 D spawn clipboard --wofi
riverctl map normal $mod+Mod1 C spawn clipboard --clear
riverctl map normal $mod C spawn clipman clear --tool wofi

# Dismiss notifications
riverctl map normal $mod+Mod1 N spawn dismiss_notify

# Emacs
riverctl map normal $mod+Mod1 E spawn emacsclient -c -a emacs

# Screenshot
riverctl map normal $mod Print spawn screenshot --full
riverctl map normal None Print spawn screenshot --region
riverctl map normal Mod1 Print spawn screenshot --region-optional
riverctl map normal Control Print spawn screeshot --full-optional

# Mod+Q to close the focused view
riverctl map normal $mod Q close

# Mod+Z to lock the screen
riverctl map normal $mod Z spawn swaylock

# Mod+Alt+Q to exit river
riverctl map normal $mod+Mod1 Q exit

# Mod+J and Mod+K to focus the next/previous view in the layout stack
riverctl map normal $mod J focus-view next
riverctl map normal $mod K focus-view previous

# Mod+Shift+J and Mod+Shift+K to swap the focused view with the next/previous
# view in the layout stack
riverctl map normal $mod+Shift J swap next
riverctl map normal $mod+Shift K swap previous

# Mod+Period and Mod+Comma to focus the next/previous output
riverctl map normal $mod Period focus-output next
riverctl map normal $mod Comma focus-output previous

# Mod+Shift+{Period,Comma} to send the focused view to the next/previous output
riverctl map normal $mod+Shift Period send-to-output next
riverctl map normal $mod+Shift Comma send-to-output previous

# Mod+E to bump the focused view to the top of the layout stack
riverctl map normal $mod E zoom

# Mod+H and Mod+L to decrease/increase the main factor by 2%
# If using rivertile(1) this determines the width of the main stack.
riverctl map normal $mod H mod-main-factor -0.02
riverctl map normal $mod L mod-main-factor +0.02

# Mod+Shift+H and Mod+Shift+L to increment/decrement the number of
# main views in the layout
riverctl map normal $mod+Shift H mod-main-count +1
riverctl map normal $mod+Shift L mod-main-count -1

# Mod+Alt+{H,J,K,L} to move views
riverctl map normal $mod+Mod1 H move left 100
riverctl map normal $mod+Mod1 J move down 100
riverctl map normal $mod+Mod1 K move up 100
riverctl map normal $mod+Mod1 L move right 100

# Mod+Control+{H,J,K,L} to resize views
riverctl map normal $mod+Control H resize horizontal -100
riverctl map normal $mod+Control J resize vertical 100
riverctl map normal $mod+Control K resize vertical -100
riverctl map normal $mod+Control L resize horizontal 100

# Mod+Alt+Control+{H,J,K,L} to snap views to screen edges
riverctl map normal $mod+Mod1+Control H snap left
riverctl map normal $mod+Mod1+Control J snap down
riverctl map normal $mod+Mod1+Control K snap up
riverctl map normal $mod+Mod1+Control L snap right

# Mod + Left Mouse Button to move views
riverctl map-pointer normal $mod BTN_LEFT move-view

# Mod + Right Mouse Button to resize views
riverctl map-pointer normal $mod BTN_RIGHT resize-view

# Mod+[1-9] to focus tag [0-8]
riverctl map normal $mod 1 set-focused-tags 1
riverctl map normal $mod 2 set-focused-tags 2
riverctl map normal $mod 3 set-focused-tags 4
riverctl map normal $mod 4 set-focused-tags 8
riverctl map normal $mod 5 set-focused-tags 16
riverctl map normal $mod 6 set-focused-tags 32
riverctl map normal $mod 7 set-focused-tags 64
riverctl map normal $mod 8 set-focused-tags 128
riverctl map normal $mod 9 set-focused-tags 256

# Mod+Shift+[1-9] to tag focused view with tag [0-8]
riverctl map normal $mod+Shift 1 set-view-tags 1
riverctl map normal $mod+Shift 2 set-view-tags 2
riverctl map normal $mod+Shift 3 set-view-tags 4
riverctl map normal $mod+Shift 4 set-view-tags 8
riverctl map normal $mod+Shift 5 set-view-tags 16
riverctl map normal $mod+Shift 6 set-view-tags 32
riverctl map normal $mod+Shift 7 set-view-tags 64
riverctl map normal $mod+Shift 8 set-view-tags 128
riverctl map normal $mod+Shift 9 set-view-tags 256

# Mod+Ctrl+[1-9] to toggle focus of tag [0-8]
riverctl map normal $mod+Control 1 toggle-focused-tags 1
riverctl map normal $mod+Control 2 toggle-focused-tags 2
riverctl map normal $mod+Control 3 toggle-focused-tags 4
riverctl map normal $mod+Control 4 toggle-focused-tags 8
riverctl map normal $mod+Control 5 toggle-focused-tags 16
riverctl map normal $mod+Control 6 toggle-focused-tags 32
riverctl map normal $mod+Control 7 toggle-focused-tags 64
riverctl map normal $mod+Control 8 toggle-focused-tags 128
riverctl map normal $mod+Control 9 toggle-focused-tags 256

# Mod+Shift+Ctrl+[1-9] to toggle tag [0-8] of focused view
riverctl map normal $mod+Shift+Control 1 toggle-view-tags 1
riverctl map normal $mod+Shift+Control 2 toggle-view-tags 2
riverctl map normal $mod+Shift+Control 3 toggle-view-tags 4
riverctl map normal $mod+Shift+Control 4 toggle-view-tags 8
riverctl map normal $mod+Shift+Control 5 toggle-view-tags 16
riverctl map normal $mod+Shift+Control 6 toggle-view-tags 32
riverctl map normal $mod+Shift+Control 7 toggle-view-tags 64
riverctl map normal $mod+Shift+Control 8 toggle-view-tags 128
riverctl map normal $mod+Shift+Control 9 toggle-view-tags 256

# Mod+0 to focus all tags
riverctl map normal $mod 0 set-focused-tags 511
# Mod+Shift+0 to tag focused view with all tags
riverctl map normal $mod+Shift 0 set-view-tags 511

# Mod+Space to toggle float
riverctl map normal $mod Space toggle-float

# Mod+F to toggle fullscreen
riverctl map normal $mod F toggle-fullscreen

# Control+Alt+{H,J,K,L} to change layout orientation
riverctl map normal Mod1+Control H layout rivertile left
riverctl map normal Mod1+Control J layout rivertile bottom
riverctl map normal Mod1+Control K layout rivertile top
riverctl map normal Mod1+Control L layout rivertile right

# Control+Alt+F to change to Full layout
riverctl map normal Mod1+Control F layout full

# Declare a passthrough mode. This mode has only a single mapping to return to
# normal mode. This makes it useful for testing a nested wayland compositor
riverctl declare-mode passthrough

# Mod+F11 to enter passthrough mode
riverctl map normal $mod F11 enter-mode passthrough

# Mod+F11 to return to normal mode
riverctl map passthrough $mod F11 enter-mode normal

# Various media key mapping examples for both normal and locked mode which do
# not have a modifier
for mode in normal locked
do
    # Eject the optical drive
    riverctl map $mode None XF86Eject spawn eject -T

    # Control pulse audio volume
    riverctl map $mode None XF86AudioRaiseVolume spawn 'volumecontrol increase && pkill -x -RTMIN+11 waybar'
    riverctl map $mode None XF86AudioLowerVolume spawn 'volumecontrol decrease && pkill -x -RTMIN+11 waybar'
    riverctl map $mode None XF86AudioMute        spawn 'volumecontrol toggle && pkill -x -RTMIN+11 waybar'

    # Control MPRIS aware media players with playerctl (https://github.com/altdesktop/playerctl)
    riverctl map $mode None XF86AudioMedia spawn playerctl play-pause
    riverctl map $mode None XF86AudioPlay  spawn playerctl play-pause
    riverctl map $mode None XF86AudioPrev  spawn playerctl previous
    riverctl map $mode None XF86AudioNext  spawn playerctl next

    # Control screen backlight brighness
    riverctl map $mode None XF86MonBrightnessUp   spawn brightness up
    riverctl map $mode None XF86MonBrightnessDown spawn brightness down
done

# Rules ────────────────────────────────────────────────────────────────────────

# Set repeat rate
riverctl set-repeat 50 300

# Focus on mouse move
riverctl focus-follows-cursor normal

# Set app-ids of views which should float
riverctl float-filter-add "float"
riverctl float-filter-add "popup"
riverctl float-filter-add "swappy"
riverctl float-filter-add "info.febvre.Komikku"
riverctl float-filter-add "org.inkscape.Inkscape"

# Set app-ids of views which should use client side decorations
riverctl csd-filter-add "swappy"

# Repeat rules for all outputs
i=0
while [ $i -le $(wlr-randr | grep -c "\"*.\"") ]
do
    # Set the layout on startup
    riverctl layout rivertile left

    # New view starts at bottom
    riverctl attach-mode bottom

    riverctl focus-output next
    i=$(( i + 1 ))
done
