#!/bin/sh
# env
# Written in 2019-2020,2022 by Lucas
# CC0 1.0 Universal/Public domain - No rights reserved
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty. You should have received a copy of the CC0 Public Domain
# Dedication along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.

if [ -f ~/.config/herbstluftwm/theme ]; then
	. ~/.config/herbstluftwm/theme
fi

hc() { herbstclient "$@"; }

hc emit_hook reload

# remove all existing keybindings
hc keyunbind --all

# keybindings
Mod=Mod4

hc keybind $Mod-Shift-q	quit
hc keybind $Mod-Shift-r	reload
hc keybind $Mod-Shift-c	close

# programs
hc keybind $Mod-g		spawn sh -c 'xclip -o | plumb'
hc keybind $Mod-Control-g	spawn sh -c 'xclip -sel clip -o | plumb'
hc keybind $Mod-Shift-g		spawn sh -c 'xclip -o | plumb -T'
hc keybind $Mod-Control-Shift-g	spawn sh -c 'xclip -sel clip -o | plumb -T'
hc keybind $Mod-p		spawn dmenu_run \
    -p run -fn "$hlwm_face" \
    -nb "$hlwm_background" -nf "$hlwm_foreground" \
    -sb "$hlwm_background" -sf "$hlwm_color14"
hc keybind $Mod-Shift-Return	spawn xterm
hc keybind $Mod-Control-Return	spawn xterm -geometry 80x40
hc keybind $Mod-Control-l	spawn xlock
hc keybind $Mod-Control-z	spawn sh -c 'xlock & doas /usr/sbin/ZZZ'
hc keybind $Mod-Print		spawn screenshot
hc keybind $Mod-Shift-Print	spawn screenshot -s

# basic movement
# focusing clients
hc keybind $Mod-Left	focus left
hc keybind $Mod-Down	focus down
hc keybind $Mod-Up	focus up
hc keybind $Mod-Right	focus right

# moving clients
hc keybind $Mod-Shift-Left	shift left
hc keybind $Mod-Shift-Down	shift down
hc keybind $Mod-Shift-Up	shift up
hc keybind $Mod-Shift-Right	shift right

# splitting frames
# create an empty frame at the specified direction
hc keybind $Mod-u	split bottom 0.5
hc keybind $Mod-o	split right 0.5
# let the current frame explode into subframes
hc keybind $Mod-Control-space	split explode

# resizing frames
resizestep=0.05
hc keybind $Mod-Control-Left	resize left +$resizestep
hc keybind $Mod-Control-Down	resize down +$resizestep
hc keybind $Mod-Control-Up	resize up +$resizestep
hc keybind $Mod-Control-Right	resize right +$resizestep

# tags
hc try rename default 1
for tag in 1:1 2:2 3:3 4:4 5:5 6:6 7:7 8:8 9:9 0:0; do
	name=${tag%:*}
	key=${tag#*:}
	hc try add $name
	hc keybind $Mod-$key use $name
	hc keybind $Mod-Shift-$key move $name
done
hc load 2 "(split horizontal:0.5:1 (clients max:0) (clients max:0))"
hc load 3 "(clients max:0)"

# cycle through tags
hc keybind $Mod-period	use_index +1 --skip-visible
hc keybind $Mod-comma	use_index -1 --skip-visible

# layouting
hc keybind $Mod-v	set_layout vertical
hc keybind $Mod-h	set_layout horizontal
hc keybind $Mod-m	set_layout max
hc keybind $Mod-r	remove
hc keybind $Mod-s	floating toggle
hc keybind $Mod-f	fullscreen toggle
hc keybind $Mod-Shift-s	pseudotile toggle
# The following cycles through the available layouts within a frame, but skips
# layouts, if the layout change wouldn't affect the actual window positions.
# I.e. if there are two windows within a frame, the grid layout is skipped.
hc keybind $Mod-space                                                           \
            or , and . compare tags.focus.curframe_wcount = 2                   \
                     . cycle_layout +1 vertical horizontal max vertical grid    \
               , cycle_layout +1

# mouse
hc mouseunbind --all
hc mousebind $Mod-Button1	move
hc mousebind $Mod-Button2	zoom
hc mousebind $Mod-Button3	resize

# focus
hc keybind $Mod-BackSpace	cycle_monitor
hc keybind $Mod-Tab		cycle_all +1
hc keybind $Mod-j		cycle +1
hc keybind $Mod-Shift-Tab	cycle_all -1
hc keybind $Mod-k		cycle -1
hc keybind $Mod-c		cycle
hc keybind $Mod-i		jumpto urgent

# theme
hc attr theme.tiling.reset	1
hc attr theme.floating.reset	1
hc attr theme.border_width	2
hc attr theme.inner_width	0
hc attr theme.outer_width	0
hc attr theme.background_color	"$hlwm_background"
hc attr theme.active.color	"$hlwm_color11"
hc attr theme.normal.color	"$hlwm_color8"
hc attr theme.urgent.color	"$hlwm_color9"

hc set frame_gap			0
hc set frame_padding			4
hc set window_gap			0
hc set frame_bg_transparent		1
hc set frame_transparent_width		0
hc set frame_border_width		0
hc set mouse_recenter_gap		0
hc set always_show_frame		0
hc set smart_frame_surroundings		0
hc set smart_window_surroundings	0
hc set pseudotile_center_threshold	0

# rules
hc unrule -F
#hc rule class=XTerm tag=3 # move all xterms to tag 3
hc rule focus=on # normally focus new clients
#hc rule focus=off # normally do not focus new clients
# give focus to most common terminals
#hc rule class~"(.*[Rr]xvt.*|.*[Tt]erm|Konsole)" focus=on
hc rule windowtype~"_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)" pseudotile=on
hc rule windowtype="_NET_WM_WINDOW_TYPE_DIALOG" focus=on
hc rule windowtype~"_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK|DESKTOP)" manage=off

hc rule class=XTerm pseudotile=on
hc rule class~"[Ff]irefox.*" pseudotile=on tag=3
hc rule class~"[Ff]irefox.*" instance=Navigator fullscreen=on tag=3
hc rule class=tor-browser-default pseudotile=on tag=3
hc rule class=tor-browser-default instance=Navigator fullscreen=on tag=3
hc rule class=SshAskpass pseudotile=on
hc rule class=Snes9x-gtk pseudotile=on
hc rule class=Gajim pseudotile=on tag=7

hc set tree_style "╾│ ├└╼─┐"
#hc set tree_style "-| |'>-."

# unlock, just to be sure
hc unlock

hc detect_monitors

hc list_monitors | while read -r line; do
	m=${line%%:*}
	sh ~/.config/herbstluftwm/herbstpanel.sh "$m" &
done