# Sway config based on Regolith i3 setup - Arch Linux version
# Copy this to ~/.config/sway/config and edit it to your liking.
# Read `man 5 sway` for a complete reference.

###############################################################################
# Variables and Basic Setup
###############################################################################

# Logo key. Use Mod1 for Alt.
set $mod Mod4
set $alt Mod1

# Home row direction keys, like vim
set $left h
set $down j
set $up k
set $right l

# Workspace keys
set $ws1_key 1
set $ws2_key 2
set $ws3_key 3
set $ws4_key 4
set $ws5_key 5
set $ws6_key 6
set $ws7_key 7
set $ws8_key 8
set $ws9_key 9
set $ws10_key 0
set $ws_high_key Ctrl

# Workspace numbers
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
set $ws11 11
set $ws12 12
set $ws13 13
set $ws14 14
set $ws15 15
set $ws16 16
set $ws17 17
set $ws18 18
set $ws19 19

# Movement key bindings
set $move_left Shift+h
set $move_down Shift+j
set $move_up Shift+k
set $move_right Shift+l

# Your preferred terminal emulator (Arch Linux alternatives)
set $term kitty
# Your preferred application launcher
set $menu fuzzel
# File manager (Arch alternatives)
set $filemanager thunar

# Defaults (kept here so the config still loads even if the included file is missing)
set $lg_left_monitor_mode 3840x2160@165Hz
set $lg_left_monitor_bar_height 40
set $lg_left_monitor_bar_font pango: FontAwesome, monospace 14

set $right_monitor_x 3840
set $right_monitor_bar_height 24
set $right_monitor_bar_font pango: FontAwesome, monospace 10

# Display/bar profile overrides for 330Hz gamemode
include /home/raga/.config/sway/scripts/display-profile/display-profile.conf

###############################################################################
# Output configuration
###############################################################################

# Default wallpaper (more resolutions are available in /usr/share/backgrounds/sway/)
# output * bg /usr/share/backgrounds/sway/Sway_Wallpaper_Blue_1920x1080.png fill
# output * bg /home/raga/.config/sway/wallpaper.jpg fill

# Multi-monitor setup
# You can get your output names by running: swaymsg -t get_outputs
# DP-2 is the primary monitor on the left, and DP-1 is on the right.
output DP-1 {
#    adaptive_sync on
    render_bit_depth 10

    position 0 0

    # ECO
    #    mode 2560x1440@60Hz

    # FULL
    mode $lg_left_monitor_mode
}

output HDMI-A-1 {
    position $right_monitor_x 0
    mode 2560x1440@144Hz
}

output DP-2 {
    position 0 0
    mode $lg_left_monitor_mode
#    position $right_monitor_x 0

    # ECO
#    mode 2560x1440@60Hz
#    mode 2560x1440@120Hz
#    mode 2560x1440@120Hz

    # FULL
#   modeline 586.59 2560 2568 2600 2640 1440 1529 1537 1543 +hsync -vsync
}

###############################################################################
# Idle configuration
###############################################################################

exec swayidle -w \
         timeout 1500 'swaylock -f -c 000000' \
#         timeout 1800 'swaymsg "output * power off"' resume 'swaymsg "output * power on"' \
         before-sleep 'swaylock -f -c 000000'

###############################################################################
# Input configuration
###############################################################################

# Estonian keyboard layout configuration
# input type:keyboard {
#     xkb_layout "ee"
#     xkb_variant "nodeadkeys"
# }
input * {
    xkb_layout "ee,it"
#    xkb_options "grp:alt_shift_toggle"
}

# Enable touchpad tap, natural scroll
input type:touchpad {
    dwt enabled
    tap enabled
    natural_scroll enabled
    middle_emulation enabled
}

# Disable mouse acceleration for all pointer devices
input type:pointer {
    accel_profile flat
}

###############################################################################
# Window and Application Styling
###############################################################################

# Floating window rules
floating_maximum_size -1 x -1
for_window [app_id="floating_window"] floating enable
for_window [app_id="pavucontrol"] floating enable, move position mouse, sticky enable, border pixel 0
for_window [app_id="Windscribe"] floating enable
for_window [class="steam" instance="steamwebhelper"] floating enable
for_window [class="steam" title="Steam"] floating disable
for_window [app_id="net.davidotek.pupgui2"] floating enable
for_window [app_id="blueman-manager"] floating enable
for_window [app_id="firefox" title=".*Bitwarden.*"] floating enable

# Border configuration
default_floating_border pixel 1
default_border pixel 1

set $bg #000000
set $br #FFFFFF
set $brbug #FF0000

# class                 border  backgr. text    indicator    child_border
client.focused          $br     $br     $br     $brbug       $br
client.focused_inactive $bg     $bg     $br     $bg          $bg
client.unfocused        $bg     $bg     $br     $bg          $bg
client.urgent          	$br     $br     $br     $bg          $br 
client.placeholder     	$br     $br     $br     $bg          $br 

# Hiding borders adjacent to the screen edges.
hide_edge_borders smart

# Enable popup during fullscreen
popup_during_fullscreen smart

# Window focus follows mouse
focus_follows_mouse no

# Auto back and forth workspace switching
workspace_auto_back_and_forth no

# Use Mouse+$mod to drag floating windows
floating_modifier $mod normal

###############################################################################
# Key bindings
###############################################################################

###############################################################################
# Launcher dialogs
###############################################################################

## Keyboard // Toggle Layout // <Alt><Shift> l ##
bindsym $alt+Shift+L input * xkb_switch_layout next

## Launch // Terminal // <> Enter ##
bindsym $mod+Return exec $term

## Launch // Browser // <><Shift> Enter ##
bindsym $mod+Shift+Return exec xdg-open http://

## Launch // Application // <> Space ##
bindsym $mod+space exec $menu

## Navigate // Window by Name // <><Ctrl> Space ##
bindsym $mod+Ctrl+space exec swaymsg -t get_tree | jq -r '..|select(.pid? and .visible?)|.name+" ["+.app_id+"]"' | fuzzel --dmenu | xargs -I {} swaymsg '[title="{}"] focus'

## Launch // File Search // <><Alt> Space ##
bindsym $mod+$alt+space exec $filemanager

###############################################################################
# Session Management
###############################################################################

## Session // Exit App // <><Shift> q ##
bindsym $mod+Shift+q kill

## Session // Terminate App // <><Alt> q ##
bindsym $mod+$alt+q exec --no-startup-id kill -9 $(swaymsg -t get_tree | jq '.. | select(.focused?).pid // empty')

## Session // Reload WM Config // <><Shift> c ##
bindsym $mod+Shift+c reload; exec makoctl reload

## Session // Restart WM // <><Ctrl> r ##
bindsym $mod+Ctrl+r reload

## Session // Logout // <><Shift> e ##
bindsym $mod+Shift+e exec swaynag -t warning -m 'Do you really want to exit sway? This will end your Wayland session.' -B 'Yes, exit sway' 'swaymsg exit'

## Session // Reboot // <><Shift> b ##
#bindsym $mod+Shift+b exec systemctl reboot

## Session // Power Down // <><Shift> p ##
#bindsym $mod+Shift+p exec systemctl poweroff

## Session // Lock Screen // <> Escape ##
bindsym $mod+Escape exec swaylock -f -c 000000

## Session // Sleep // <><Shift> s ##
#bindsym $mod+Shift+s exec systemctl suspend

###############################################################################
# Custom scripts (OSRS mode)
###############################################################################

set $osrs_scripts /home/raga/repos/linux/sway-new-config/scripts/osrs
set $osrs_mode OSRS

#bindsym f12 mode "$osrs_mode"
mode "$osrs_mode" {
    bindsym q exec $osrs_scripts/osrs-drop.sh
    bindsym w exec $osrs_scripts/osrs-click.sh
    bindsym o exec $osrs_scripts/mouse-toggle.sh toggle
    bindsym p exec $osrs_scripts/mouse-toggle.sh
    bindsym a exec kitty -e $osrs_scripts/autoclicker.sh
    #bindsym Escape exec $osrs_scripts/autoclicker-stop.sh

    ## Resize // Exit OSRS Mode // Escape or Enter ##
    bindsym Return mode "default"
    bindsym Escape mode "default"
    bindsym f12 mode "default"
}

###############################################################################
# System Management (Arch Linux alternatives)
###############################################################################

## Modify // Settings // <> c ##
#bindsym $mod+c exec env XDG_CURRENT_DESKTOP=GNOME gnome-control-center

## Modify // Display Settings // <> d ##
#bindsym $mod+d exec wdisplays

## Modify // Wifi Settings // <> w ##
bindsym $mod+w exec nm-connection-editor

## Modify // Bluetooth Settings // <> b ##
bindsym $mod+b exec blueman-manager

## Launch // File Browser // <><Shift> n ##
bindsym $mod+Shift+n exec $filemanager

###############################################################################
# Window and Workspace Navigation
###############################################################################

## Navigate // Relative Parent // <> a ##
bindsym $mod+a focus parent

## Navigate // Relative Child // <> z ##
bindsym $mod+z focus child

## Navigate // Relative Window // <> ↑ ↓ ← → ##
bindsym $mod+Left focus left
bindsym $mod+Down focus down
bindsym $mod+Up focus up
bindsym $mod+Right focus right

## Navigate // Relative Window // <> k j h l ##
bindsym $mod+$left focus left
bindsym $mod+$down focus down
bindsym $mod+$up focus up
bindsym $mod+$right focus right

## Navigate // Workspaces 1-10 // <> 0..9 ##
bindsym $mod+$ws1_key workspace number $ws1
bindsym $mod+$ws2_key workspace number $ws2
bindsym $mod+$ws3_key workspace number $ws3
bindsym $mod+$ws4_key workspace number $ws4
bindsym $mod+$ws5_key workspace number $ws5
bindsym $mod+$ws6_key workspace number $ws6
bindsym $mod+$ws7_key workspace number $ws7
bindsym $mod+$ws8_key workspace number $ws8
bindsym $mod+$ws9_key workspace number $ws9
bindsym $mod+$ws10_key workspace number $ws10

## Navigate // Workspace 11 - 19 // <><Ctrl> 1..9 ##
bindsym $mod+$ws_high_key+$ws1_key workspace number $ws11
bindsym $mod+$ws_high_key+$ws2_key workspace number $ws12
bindsym $mod+$ws_high_key+$ws3_key workspace number $ws13
bindsym $mod+$ws_high_key+$ws4_key workspace number $ws14
bindsym $mod+$ws_high_key+$ws5_key workspace number $ws15
bindsym $mod+$ws_high_key+$ws6_key workspace number $ws16
bindsym $mod+$ws_high_key+$ws7_key workspace number $ws17
bindsym $mod+$ws_high_key+$ws8_key workspace number $ws18
bindsym $mod+$ws_high_key+$ws9_key workspace number $ws19

## Navigate // Next Workspace // <> Tab ##
bindsym $mod+Tab workspace next

## Navigate // Next Workspace // <><Alt> → ##
bindsym $mod+$alt+Right workspace next

## Navigate // Next Workspace on Output // <><Ctrl> Tab ##
bindsym $mod+Ctrl+Tab workspace next_on_output

## Navigate // Next Workspace on Output // <><Ctrl> l ##
bindsym $mod+Ctrl+l workspace next_on_output

## Navigate // Previous Workspace // <><Shift> Tab ##
bindsym $mod+Shift+Tab workspace prev

## Navigate // Previous Workspace // <><Alt> ← ##
bindsym $mod+$alt+Left workspace prev

## Navigate // Previous Workspace on Output // <><Ctrl><Shift> Tab ##
bindsym $mod+Ctrl+Shift+Tab workspace prev_on_output

## Navigate // Previous Workspace on Output // <><Ctrl> h ##
bindsym $mod+Ctrl+h workspace prev_on_output

## Navigate // Scratchpad // <><Ctrl> a ##
#bindsym $mod+Ctrl+a scratchpad show

###############################################################################
# Window and Workspace Configuration
###############################################################################

## Modify // Window Position // <><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

## Modify // Window Position // <><Shift> k j h l ##
bindsym $mod+Shift+$left move left
bindsym $mod+Shift+$down move down
bindsym $mod+Shift+$up move up
bindsym $mod+Shift+$right move right

## Modify // Containing Workspace // <><Ctrl><Shift> ↑ ↓ ← → ##
bindsym $mod+Ctrl+Shift+Left move workspace to output left
bindsym $mod+Ctrl+Shift+Right move workspace to output right
bindsym $mod+Ctrl+Shift+Up move workspace to output up
bindsym $mod+Ctrl+Shift+Down move workspace to output down

## Modify // Containing Workspace // <><Ctrl><Shift> k j h l ##
bindsym $mod+Ctrl+Shift+h move workspace to output left
bindsym $mod+Ctrl+Shift+j move workspace to output down
bindsym $mod+Ctrl+Shift+k move workspace to output up
bindsym $mod+Ctrl+Shift+l move workspace to output right

## Modify // Vertical Window Orientation // <> v ##
#bindsym $mod+v splitv

## Modify // Horizontal Window Orientation // <> g ##
#bindsym $mod+g splith

## Modify // Toggle Window Orientation // <> BackSpace ##
bindsym $mod+BackSpace split toggle

## Modify // Window Fullscreen Toggle // <> f ##
bindsym $mod+f fullscreen toggle

## Modify // Window Floating Toggle // <><Shift> f ##
bindsym $mod+Shift+f floating toggle

## Modify // Move to Scratchpad // <><Ctrl> m ##
#bindsym $mod+Ctrl+m move to scratchpad

## Modify // Tile/Float Focus Toggle // <><Shift> t ##
#bindsym $mod+Shift+t focus mode_toggle

## Modify // Window Layout Mode // <> t ##
#bindsym $mod+t layout toggle tabbed splith splitv

# Move focused container to workspace
## Modify // Move Window to Workspace 1 - 10 // <><Shift> 0..9 ##
bindsym $mod+Shift+$ws1_key move container to workspace number $ws1
bindsym $mod+Shift+$ws2_key move container to workspace number $ws2
bindsym $mod+Shift+$ws3_key move container to workspace number $ws3
bindsym $mod+Shift+$ws4_key move container to workspace number $ws4
bindsym $mod+Shift+$ws5_key move container to workspace number $ws5
bindsym $mod+Shift+$ws6_key move container to workspace number $ws6
bindsym $mod+Shift+$ws7_key move container to workspace number $ws7
bindsym $mod+Shift+$ws8_key move container to workspace number $ws8
bindsym $mod+Shift+$ws9_key move container to workspace number $ws9
bindsym $mod+Shift+$ws10_key move container to workspace number $ws10

## Modify // Move Window to Workspace 11 - 19// <><Ctrl><Shift> 1..9 ##
bindsym $mod+Shift+$ws_high_key+$ws1_key move container to workspace number $ws11
bindsym $mod+Shift+$ws_high_key+$ws2_key move container to workspace number $ws12
bindsym $mod+Shift+$ws_high_key+$ws3_key move container to workspace number $ws13
bindsym $mod+Shift+$ws_high_key+$ws4_key move container to workspace number $ws14
bindsym $mod+Shift+$ws_high_key+$ws5_key move container to workspace number $ws15
bindsym $mod+Shift+$ws_high_key+$ws6_key move container to workspace number $ws16
bindsym $mod+Shift+$ws_high_key+$ws7_key move container to workspace number $ws17
bindsym $mod+Shift+$ws_high_key+$ws8_key move container to workspace number $ws18
bindsym $mod+Shift+$ws_high_key+$ws9_key move container to workspace number $ws19

# Move focused container to workspace and follow
## Modify // Carry Window to Workspace 1 - 10// <><Alt> 0..9 ##
bindsym $mod+$alt+$ws1_key move container to workspace number $ws1; workspace number $ws1
bindsym $mod+$alt+$ws2_key move container to workspace number $ws2; workspace number $ws2
bindsym $mod+$alt+$ws3_key move container to workspace number $ws3; workspace number $ws3
bindsym $mod+$alt+$ws4_key move container to workspace number $ws4; workspace number $ws4
bindsym $mod+$alt+$ws5_key move container to workspace number $ws5; workspace number $ws5
bindsym $mod+$alt+$ws6_key move container to workspace number $ws6; workspace number $ws6
bindsym $mod+$alt+$ws7_key move container to workspace number $ws7; workspace number $ws7
bindsym $mod+$alt+$ws8_key move container to workspace number $ws8; workspace number $ws8
bindsym $mod+$alt+$ws9_key move container to workspace number $ws9; workspace number $ws9
bindsym $mod+$alt+$ws10_key move container to workspace number $ws10; workspace number $ws10

## Modify // Carry Window to Workspace 11 - 19 // <><Alt><Ctrl> 1..9 ##
bindsym $mod+$alt+$ws_high_key+$ws1_key move container to workspace number $ws11; workspace number $ws11
bindsym $mod+$alt+$ws_high_key+$ws2_key move container to workspace number $ws12; workspace number $ws12
bindsym $mod+$alt+$ws_high_key+$ws3_key move container to workspace number $ws13; workspace number $ws13
bindsym $mod+$alt+$ws_high_key+$ws4_key move container to workspace number $ws14; workspace number $ws14
bindsym $mod+$alt+$ws_high_key+$ws5_key move container to workspace number $ws15; workspace number $ws15
bindsym $mod+$alt+$ws_high_key+$ws6_key move container to workspace number $ws16; workspace number $ws16
bindsym $mod+$alt+$ws_high_key+$ws7_key move container to workspace number $ws17; workspace number $ws17
bindsym $mod+$alt+$ws_high_key+$ws8_key move container to workspace number $ws18; workspace number $ws18
bindsym $mod+$alt+$ws_high_key+$ws9_key move container to workspace number $ws19; workspace number $ws19

# Layout bindings (matching i3 defaults)
#bindsym $mod+s layout stacking
#bindsym $mod+e layout toggle split

# Move the currently focused window to the scratchpad
#bindsym $mod+Shift+minus move scratchpad

# Show the next scratchpad window or hide the focused scratchpad window.
#bindsym $mod+minus scratchpad show

###############################################################################
# Resize Mode
###############################################################################

## Resize // Enter Resize Mode // <> r ##
mode "Resize Mode" {
        # These bindings trigger as soon as you enter the resize mode

        ## Resize // Resize Window // ↑ ↓ ← → ##
        bindsym Left resize shrink width 6 px or 6 ppt
        bindsym Down resize grow height 6 px or 6 ppt
        bindsym Up resize shrink height 6 px or 6 ppt
        bindsym Right resize grow width 6 px or 6 ppt

        bindsym Shift+Left resize shrink width 48 px or 48 ppt
        bindsym Shift+Down resize grow height 48 px or 48 ppt
        bindsym Shift+Up resize shrink height 48 px or 48 ppt
        bindsym Shift+Right resize grow width 48 px or 48 ppt

        ## Resize // Resize Window // k j h l ##
        bindsym $left resize shrink width 6 px or 6 ppt
        bindsym $down resize grow height 6 px or 6 ppt
        bindsym $up resize shrink height 6 px or 6 ppt
        bindsym $right resize grow width 6 px or 6 ppt

        bindsym Shift+$left resize shrink width 48 px or 48 ppt
        bindsym Shift+$down resize grow height 48 px or 48 ppt
        bindsym Shift+$up resize shrink height 48 px or 48 ppt
        bindsym Shift+$right resize grow width 48 px or 48 ppt

        ## Resize // Exit Resize Mode // Escape or Enter ##
        bindsym Return mode "default"
        bindsym Escape mode "default"
        bindsym $mod+r mode "default"
}
bindsym $mod+r mode "Resize Mode"

###############################################################################
# Media keys and utilities
###############################################################################

# Volume control (works with both PulseAudio and PipeWire)
bindsym --locked XF86AudioMute exec pactl set-sink-mute @DEFAULT_SINK@ toggle && ~/.config/sway/scripts/sway/volume-notify.sh
bindsym --locked XF86AudioLowerVolume exec pactl set-sink-volume @DEFAULT_SINK@ -5% && ~/.config/sway/scripts/sway/volume-notify.sh
bindsym --locked XF86AudioRaiseVolume exec pactl set-sink-volume @DEFAULT_SINK@ +5% && ~/.config/sway/scripts/sway/volume-notify.sh
bindsym --locked XF86AudioMicMute exec pactl set-source-mute @DEFAULT_SOURCE@ toggle

# Brightness control
bindsym --locked XF86MonBrightnessDown exec brightnessctl set 5%-
bindsym --locked XF86MonBrightnessUp exec brightnessctl set 5%+

# Screenshots
bindsym Print exec grim - | swappy -f -

bindsym $mod+Print exec grim -g "$(slurp)" - | swappy -f -

## Modify // Toggle Bar // <> i ##
bindsym $mod+i bar mode toggle

###############################################################################
# Bar Configuration
###############################################################################

bar {
    output DP-1
    #output DP-2
    position bottom
    mode dock
    height $lg_left_monitor_bar_height
    font $lg_left_monitor_bar_font
    separator_symbol " "
    strip_workspace_numbers yes
    workspace_min_width 36

    status_command i3status-rs ~/.config/sway/i3status-rust.toml || while date +'%Y-%m-%d %l:%M:%S %p'; do sleep 1; done

    colors {
        background #000000
        statusline #ffffff
        separator #666666

        #                  border  backgr. text
        focused_workspace  #000000 #ffffff #000000
        active_workspace   #ffffff #000000 #ffffff
        inactive_workspace #000000 #000000 #ffffff
        urgent_workspace   #000000 #ff0000 #ffffff
    }
}

bar {
    #output DP-2
    output HDMI-A-1
    position bottom
    mode dock
    height $right_monitor_bar_height
    font $right_monitor_bar_font
    separator_symbol " "
    strip_workspace_numbers yes
    workspace_min_width 36

    status_command i3status-rs ~/.config/sway/i3status-rust.toml || while date +'%Y-%m-%d %l:%M:%S %p'; do sleep 1; done

    colors {
        background #000000
        statusline #ffffff
        separator #666666

        #                  border  backgr. text
        focused_workspace  #000000 #ffffff #000000
        active_workspace   #ffffff #000000 #ffffff
        inactive_workspace #000000 #000000 #ffffff
        urgent_workspace   #000000 #ff0000 #ffffff
    }
}

###############################################################################
# Autostart Applications
###############################################################################

# Start background applications
exec --no-startup-id systemctl --user import-environment XDG_CURRENT_DESKTOP DISPLAY WAYLAND_DISPLAY
exec --no-startup-id hash dbus-update-activation-environment 2>/dev/null && \
     dbus-update-activation-environment --systemd XDG_CURRENT_DESKTOP DISPLAY WAYLAND_DISPLAY

# Start essential services (Arch Linux)
exec mako # Notification daemon
exec lxqt-policykit-agent # Authentication agent (comment out if not needed)

exec --no-startup-id blueman-applet
exec --no-startup-id nm-applet
exec --no-startup-id home_dotfiles/.config/sway/scripts/i3status/set-random-bg.sh

# Include additional config files if they exist
include /etc/sway/config.d/* 

#bindsym $mod+x exec "$(file="/tmp/click"; if test "0" = "$(cat "$file")"; then printf '1\n' > "$file"; else printf '0\n' > "$file"; fi)"

seat seat0 hide_cursor 3000
