#! /bin/sh

# -- start hotkey daemon
pgrep -x sxhkd > /dev/null || sxhkd &

xrandr --output HDMI-1 --mode 2560x1440 --rate 144
xrandr --output HDMI-1 --primary
xrandr --output HDMI-1 --left-of DP-2

# -- BSPWM Settings
bspc monitor HDMI-1 -d 1 2 3 
bspc monitor DP-2 -d 4 5 6

bspc config border_width         2
bspc config window_gap           4
bspc config split_ratio          0.50

bspc config borderless_monocle   true
bspc config gapless_monocle      true
bspc config single_monocle      true

bspc config presel_feedback_color "#5D5E72"
bspc config normal_border_color "#1e1e2e"
bspc config active_border_color "#1e1e2e"
bspc config focused_border_color "#45475a"

bspc config focus_follows_pointer true
bspc config pointer_follows_focus true
bspc config pointer_follows_monitor true

# -- BSPWM Rules
bspc rule -a kitty-vpn state=center_pseudo_tiled follow=on
bspc rule -a NoiseTorch state=floating center=on

#bspc rule -a "microsoft teams - preview" desktop=4
#bspc rule -a Lutris desktop='^3'
#bspc rule -a battle.net.exe desktop='^3'
#bspc rule -a discord desktop='^6'
#bspc rule -a "VirtualBox Machine" state=floating desktop='^2'
#bspc rule -a Kupfer.py focus=on
#bspc rule -a Screenkey manage=off

bspc rule -a "Conky:Conky:conky*" flag=sticky layer=below state=floating sticky=on

# -- Autostart


# restart window titles daemon
cat /dev/null > $HOME/.cache/bspwm_windows_1.txt
cat /dev/null > $HOME/.cache/bspwm_windows_2.txt
while pgrep -u $UID -f bspwm_window_titles >/dev/null; do pkill -f bspwm_window_titles; done
bspwm_window_titles &

## -- launch polybar
$HOME/.config/polybar/launch.sh

roficlip.py --daemon &


## -- policy kit
#pgrep -x lxsession > /dev/null || lxsession &
pgrep -x lxpolkit > /dev/null || lxpolkit &

## -- launch dunst notification server
pgrep -x dunst > /dev/null || dunst &


## -- set the default cursor to left pointer
xsetroot -cursor_name left_ptr &
## -- start the compositor
#pgrep -x picom > /dev/null || picom --config $HOME/.config/picom/picom.conf --experimental-backends --backend glx &
pgrep -x picom > /dev/null || picom &
## -- udisk utils
#udisksvm >/dev/null &
pgrep -x udiskie > /dev/null || udiskie &

## -- noisetorch noise cancelling
#noisetorch -u
#pgrep -x noisetorch > /dev/null || noisetorch -i sys-devices-pci0000:00-0000:00:01.3-0000:02:00.0-usb2-2\x2d10-2\x2d10:1.0-sound-card3-controlC3.device -s alsa_input.usb-Corsair_CORSAIR_HS70_Pro_Wireless_Gaming_Headset-00.mono-fallback -t 70 &

# -- Autostart of misc
## -- applets
#nm-applet --indicator &
#blueman-applet &
#pgrep -x indicator-sound-switcher > /dev/null || indicator-sound-switcher &
#/home/dom/.local/share/headset-charge-indicator/headset-charge-indicator.py &
#/home/dom/.local/share/media-control-indicator/media-control-indicator.py &



# -- Set background wallpaper
#nitrogen --restore &
feh --bg-scale ~/Pictures/Wallpaper/MonarchOS.png &



###
### remove picom corners in monocle mode
### https://www.reddit.com/r/bspwm/comments/hqt1r3/is_there_a_way_to_disable_picom_rounded_borders/
###
bspc subscribe desktop_layout | while read -r Event
do
  Desktop=$(echo "$Event" | awk '{print $3}')
  State=$(echo "$Event" | awk '{print $4}')
  if [ "$State" = "monocle" ]; then 
    bspc query -N -d $Desktop | while read -r Node
    do
      xprop -id $Node -f _PICOM_ROUNDED 32c -set _PICOM_ROUNDED 1
    done
  elif [ $(bspc config window_gap) -gt 0 ]; then
    bspc query -N -d $Desktop | while read -r Node
    do
      xprop -id $Node -remove _PICOM_ROUNDED
    done
  fi
done &