#!/bin/bash

# hlwm autostart --- Initialisation script for HerbstluftWM
#
# Copyright (c) 2021-2023  Protesilaos Stavrou <info@protesilaos.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
## Commentary:
#
# This file is part of my custom desktop session:
# <https://github.com/protesilaos/dotfiles>.

## Code:

### Essentials

_hc ()
{
    herbstclient "$@"
}

_check ()
{
    command -v "$1" > /dev/null
}

_hc emit_hook reload

# Remove all existing keybindings.  We set them further below via
# SXHKD.  The only exception is the mouse where we want Super+Right
# Click drag to move a floating window and Super+Left Click drag to
# resize any window at its nearest side.
_hc keyunbind --all
_hc mousebind Mod4-Button1 move
_hc mousebind Mod4-Button3 resize

### Tags and monitors

# unlock, just to be sure
_hc unlock

# NOTE that tags must be defined before the monitors, while the
# default_frame_layout has to be set before the tags as it affects all
# new frames.

_hc set default_frame_layout max

tag_names=( {0..9} )

_hc rename default "${tag_names[0]}" || true

for i in "${!tag_names[@]}"
do
    _hc add "${tag_names[$i]}"
done

# Set up virtual monitors.  On my desktop computer, the monitor's
# resolution is 2560x1080 (and what inspired me to set up
# HerbstluftWM).  Also check my script 'herbstluftwm_cycle_monitors'.
[ "$HOSTNAME" = "kronos" ] && _hc set_monitors 640x1080+0+0 1920x1080+640+0

### Configuration

_hc set auto_detect_panels on

_hc set window_gap 0
_hc set frame_gap 0
_hc set frame_border_width 0
_hc set frame_padding 0
_hc set smart_window_surroundings off
_hc set smart_frame_surroundings off
_hc set mouse_recenter_gap 0
_hc set focus_follows_mouse on
_hc set always_show_frame on
_hc set frame_bg_transparent off
_hc set frame_normal_opacity 15
_hc set frame_active_opacity 50

_hc attr theme.inner_width 1
_hc attr theme.border_width 3
_hc attr theme.active.outer_width 3
_hc attr theme.floating.border_width 1
_hc attr theme.floating.outer_width 1

#### Frame title
_hc attr theme.title_height 12
_hc attr theme.title_depth 3
_hc attr theme.title_font "Monospace:size=8:weight=bold" # see my fontconfig setup
_hc attr theme.normal.title_font "Monospace:size=8"
_hc attr theme.title_align left
_hc attr theme.title_when multiple_tabs # only with the max layout

#### Colours
_check herbstluftwm_conf_colors && herbstluftwm_conf_colors

#### Rules
# TODO 2021-11-08: I have kept the defaults here.  Needs review.

_hc unrule -F
_hc rule focus=on # normally focus new clients
_hc rule floatplacement=none
_hc rule fixedsize floating=on
_hc rule windowtype~'_NET_WM_WINDOW_TYPE_(DIALOG|UTILITY|SPLASH)' floating=on
_hc rule windowtype='_NET_WM_WINDOW_TYPE_DIALOG' focus=on floatplacement=center
_hc rule windowtype~'_NET_WM_WINDOW_TYPE_(NOTIFICATION|DOCK|DESKTOP)' manage=off

# Empty frames function as the equivalent of BSPWM's receptacles: they
# receive the next client.  However, this only works for the focused
# tag.
_hc rule index=e

### Autostart

# Common settings used by my BSPWM and Herbstluftwm setups
# (collectively referred to as "Xorg Tiling Window Managers").  I
# configure those window managers to be similar.
_check xtwm_common && xtwm_common
