# config for sway
# Read `man 5 sway` for a complete reference.

# Font in sway
font pango: SFMono Nerd Font 9

# Variables
set $mod Mod4
set $left h
set $down j
set $up k
set $right l

# Color
# old solarized
set $yellow  #b58900
set $orange  #cb4b16
set $red     #dc322f
set $magenta #d33682
set $violet  #6c71c4
set $blue    #268bd2
set $cyan    #2aa198
set $green   #859900

set $baseA3 #fdf6e3
set $baseA2 #eee8d5
set $baseA1 #93a1a1
set $baseA0 #839496
set $baseB0 #657b83
set $baseB1 #586e75
set $baseB2 #073642
set $baseB3 #002b36
set $custom #e1cab3

# new everforest
set $bar_bg #323232
set $green  #93b259
set $orange #f85551

# clientclass           border  backgr. text    indicator
client.focused          $green  $green  $bar_bg $green
client.focused_inactive $bar_bg $bar_bg $baseB0 $bar_bg
client.unfocused        $bar_bg $bar_bg $baseB0 $bar_bg
client.urgent           $orange $orange $baseB3 $orange

# Output configuration
set $laptop eDP-1

output * bg ~/images/wallpaper/wallpaper.png fill

bindswitch --reload --locked lid:on output $laptop disable
bindswitch --reload --locked lid:off output $laptop enable

exec light -S 20

# Idle configuration
# create blurred lock screen
exec_always convert ~/images/wallpaper/wallpaper.png -blur 0x8 /tmp/lock.png

# set lockc and save to script, so other programs can also use it
set $lock 'swaylock -f --image /tmp/lock.png --indicator-idle-visible -F'
exec_always echo $lock > ~/.local/bin/lock.sh
exec_always chmod +x ~/.local/bin/lock.sh

# Lock after 20 minutes
# Lock before suspend
exec swayidle -w \
         timeout 1200 ~/.local/bin/lock.sh \
         before-sleep ~/.local/bin/lock.sh
         #timeout 1320 'systemctl suspend' \


# Input configuration
input type:pointer {
    accel_profile "flat"
}

input type:keyboard {
    # Umlauts on RAlt + {a,u,o,s}
    xkb_layout eu
    # Make Caps Lock an additional Esc
    # https://man.archlinux.org/man/xkeyboard-config.7
    xkb_options caps:escape
    # Keyboard repeats
    repeat_delay 300
    repeat_rate 50
}

# Key bindings
# Basics:
    # Terminal
    bindsym $mod+Return exec alacritty

    # Kill focused window
    bindsym $mod+Shift+Backspace kill

    # wofi
        # show normal applications
        bindsym $mod+d exec wofi --show drun -p 'Open application'

        # show binaries in PATH
        bindsym $mod+Shift+d exec wofi --show run -p 'dmenu'

        # show all pdfs in HOME
        bindsym $mod+p exec \
            fd -I "\.pdf$" $HOME \
            | cut -f 4- -d "/" \
            | wofi -d -p 'Search pdfs' -W 70% \
            | xargs -d '\n' -r xdg-open

        # show shutdown menu
        bindsym $mod+Ctrl+Backspace exec \
            printf "/home/marc/.local/bin/lock.sh\nsystemctl suspend\nswaymsg reload\nshutdown now\nloginctl terminate-user marc" \
            | wofi -d -p "Power" -L 6 \
            | xargs -d '\n' -r /bin/bash -c

        # switch windows
        bindsym $mod+w exec \
            wlrctl window list \
            | awk '{print $1}' \
            | sed 's/://' \
            | wofi --dmenu \
            | xargs -r wlrctl window focus

    # Screenshot
    bindsym $mod+Shift+s exec grim -g "$(slurp)" - | wl-copy
    bindsym $mod+Ctrl+Shift+s exec grim -g "$(slurp)" - | swappy -f -

    # Show notifications
    bindsym $mod+v exec swaync-client -t -sw

    # Brightness
    set $SHOW_LIGHT light -G | cut -d'.' -f1 > $WOBSOCK
    bindsym XF86MonBrightnessUp exec light -A 5 && $SHOW_LIGHT
    bindsym XF86MonBrightnessDown exec light -U 5 && $SHOW_LIGHT

    # Audio
    bindsym XF86AudioPlay exec playerctl play-pause
    bindsym XF86AudioPrev exec playerctl previous
    bindsym XF86AudioNext exec playerctl next
    set $SHOW_VOL pactl get-sink-volume @DEFAULT_SINK@ | head -n 1| awk '{print substr($5, 1, length($5)-1)}' > $WOBSOCK
    bindsym XF86AudioRaiseVolume exec pactl set-sink-volume @DEFAULT_SINK@ +5% && $SHOW_VOL
    bindsym XF86AudioLowerVolume exec pactl set-sink-volume @DEFAULT_SINK@ -5% && $SHOW_VOL
    set $SHOW_MUTE_OUT notify-send "Audio Output" "$(pactl get-sink-mute @DEFAULT_SINK@)" -i speaker
    set $SHOW_MUTE_IN notify-send "Microphone" "$(pactl get-source-mute @DEFAULT_SOURCE@)" -i microphone
    bindsym XF86AudioMicMute exec pactl set-source-mute @DEFAULT_SOURCE@ toggle && $SHOW_MUTE_IN
    bindsym XF86AudioMute exec pactl set-sink-mute @DEFAULT_SINK@ toggle && $SHOW_MUTE_OUT

# Moving around:
    # Move your focus around
    bindsym $mod+$left focus left
    bindsym $mod+$down focus down
    bindsym $mod+$up focus up
    bindsym $mod+$right focus right

    # Move the focused window with the same, but add Shift
    bindsym $mod+Shift+$left move left
    bindsym $mod+Shift+$down move down
    bindsym $mod+Shift+$up move up
    bindsym $mod+Shift+$right move right

# Workspaces:
    # Declare workspaces
    set $ws1 '1'
    set $ws2 '2'
    set $ws3 '3'
    set $ws4 '4'
    set $ws5 '5'
    set $ws6 '6'
    set $ws7 '7'
    set $ws8 '8'
    set $ws9 '9'
    set $ws10 '10'

    # Switch to workspace
    bindsym $mod+1 workspace number $ws1
    bindsym $mod+2 workspace number $ws2
    bindsym $mod+3 workspace number $ws3
    bindsym $mod+4 workspace number $ws4
    bindsym $mod+5 workspace number $ws5
    bindsym $mod+6 workspace number $ws6
    bindsym $mod+7 workspace number $ws7
    bindsym $mod+8 workspace number $ws8
    bindsym $mod+9 workspace number $ws9
    bindsym $mod+0 workspace number $ws10

    # Move focused container to workspace
    bindsym $mod+Shift+1 move container to workspace number $ws1
    bindsym $mod+Shift+2 move container to workspace number $ws2
    bindsym $mod+Shift+3 move container to workspace number $ws3
    bindsym $mod+Shift+4 move container to workspace number $ws4
    bindsym $mod+Shift+5 move container to workspace number $ws5
    bindsym $mod+Shift+6 move container to workspace number $ws6
    bindsym $mod+Shift+7 move container to workspace number $ws7
    bindsym $mod+Shift+8 move container to workspace number $ws8
    bindsym $mod+Shift+9 move container to workspace number $ws9
    bindsym $mod+Shift+0 move container to workspace number $ws10

    # Move between displays
    bindsym $mod+Control+$left move workspace to output left
    bindsym $mod+Control+$right move workspace to output right

# Layout stuff:
    titlebar_padding 1
    workspace_layout tabbed
    title_align center
    default_border pixel 0
    default_floating_border pixel 0
    # hide title and border, if only one application open
    hide_edge_borders --i3 smart

    # just show gaps between windows
    # gaps inner 5
    # gaps outer -5

    # Make the current focus fullscreen
    bindsym $mod+f fullscreen

    # Resize windows with $mod + mouse
    floating_modifier $mod normal

    # Toggle the current focus between tiling and floating mode
    bindsym $mod+Shift+space floating toggle

    # Swap focus between the tiling area and the floating area
    bindsym $mod+space focus mode_toggle

    # Window rules
    for_window [app_id="firefox" title="Firefox — Sharing Indicator"] kill
    for_window [app_id="firefox"] move container to workspace number $ws2
    for_window [class="discord"] move container to workspace number $ws3
    for_window [app_id="org.telegram.desktop"] move container to workspace number $ws3
    for_window [class="Spotify"] move container to workspace number $ws4
    for_window [class="Joplin"] move container to workspace number $ws5
    for_window [app_id="evolution"] move container to workspace number $ws8
    for_window [app_id="org.keepassxc.KeePassXC"] move container to workspace number $ws9

# Status Bar
bar {
    swaybar_command waybar
}

# Startup programs
exec syncthing
exec swaync
exec fcitx5
exec nm-applet --indicator
exec blueman-applet
exec pasystray

# On-screen progress bar
set $WOBSOCK $XDG_RUNTIME_DIR/wob.sock
exec_always rm -f $WOBSOCK && mkfifo $WOBSOCK && tail -f $WOBSOCK | wob

# Include some defaults
include /etc/sway/config.d/*
