#
# SXHKD module with WM-agnostic keys
#
# Part of my dotfiles: <https://gitlab.com/protesilaos/dotfiles>.
#

### Description
#
# Partial configuration file for the Simple X Hotkey Daemon.  Meant to
# work with `bspwm`.  This is part of my dotfiles, which are managed
# with GNU Stow.  See https://gitlab.com/protesilaos/dotfiles

### Code:

#### Environment

# I am using the prefix `mod4 + e' as a mnemonic for "environment".
#
# The following commands are:
#
# f == bspwm focus mode (my script)
# s == reload sxhkd
# p == reload polybar (NOTE: check the panel names in its config)
# x == reload the Xresources file
mod4 + x ; e ; {f,s,p,t,x}
    { \
    bspwm_focus_mode, \
    pkill -USR1 -x sxhkd && notify-send -i key_bindings "Reloaded key bindings", \
    pkill -x polybar && polybar "$DESKTOP_SESSION" &, \
    xrdb -I "$HOME" -merge "$HOME"/.Xresources \
    }

mod4 + F5
     delight

# Invoke the session management menu (log out, lock, reboot, shutdown).
ctrl + alt + Delete
     poweroptionsmenu

mod4 + Delete
     poweroptionsmenu

#### Keyboard layout

# NOTE 2021-08-27: review commentary about layout toggle.

# We can set the keyboard layouts, as well as the toggle directly via
# `setxkbmap`.  Example of what I used to run:
#
#	setxkbmap -layout 'us,gr' -option 'ctrl:nocaps,compose:menu'
#
# However, doing so means losing out on the possibility to easily find
# out the current/active layout from a script: X knows about "us,gr" in
# that order, not which one is currently typing.  Also, I want another
# key binding that is not available in setxkbmap.
#
# The key chord is based on the Emacs function `toggle-input-method',
# which is bound to C-\
mod4 + backslash
	toggle_kbd_layout

#### Main programs

mod4 + {_, shift +,ctrl + shift + } Return
     {xterm,xterm -e emacsclient -nw -e "(eshell)",xterm -e emacsclient -nw -e "(shell)"}

# Show the desired rofi views.
mod4 + d
     rofi -show drun

mod4 + x ; {x,b}
     rofi -show {drun,window}

#### Miscellaneous tools and media keys


# Screenshots (requires `scrot`).  First one is for the focused window.
# The other is of the entire X session (including all active monitors).
# Note that `scrot` also has an interactive mode that I do not use.
Print
	scrot -u -z '%Y-%m-%d_%H:%M:%S_$wx$h$n.png' -e 'mv $f ~/Pictures' \
    && notify-send -i gnome-screenshot "Window view screenshot" "Saved image to Pictures"
mod4 + Print
	scrot -m -z '%Y-%m-%d_%H:%M:%S_$wx$h.png' -e 'mv $f ~/Pictures' \
    && notify-send -i gnome-screenshot "Screen view screenshot" "Saved image to Pictures"

# Control the laptop's screen brightness (requires `xbacklight`).
XF86MonBrightness{Down,Up}
	xbacklight {-dec 15,-inc 15}


# Specifically for `bongo', my Emacs music manager.
XF86Audio{Play,Prev,Next}
	emacsclient -e { \
    "(bongo-pause/resume)", \
    "(bongo-previous)", \
    "(bongo-next)" \
    }

# Control the [last] currently active player.
mod4 + XF86Audio{Play,Prev,Next}
    playerctl {play-pause,previous,next}

# Speaker/output volume.  Requires `amixer` and dedicated keys.
XF86Audio{Mute,LowerVolume,RaiseVolume}
	amixer set Master {toggle,5%-,5%+}

# Microphone/input volume.  Same requirements as previous definition.
XF86AudioMicMute
	amixer set Capture toggle
mod4 + XF86Audio{Mute,LowerVolume,RaiseVolume}
	amixer set Capture {toggle,5%-,5%+}


#### Notifications

mod4 + x ; d ; {q,Q,c,h}
     dunstctl {close,close-all,context,history-pop}

# 'dunst_toggle_notify' is a shell script of mine.  Check my dotfiles.
mod4 + x ; d ; p
     dunst_toggle_notify


# TODO 2019-11-01: These will need to be reviewed once all key bindings
# are rewritten.  Perhaps it would be nice to produce something that is
# interactive and searchable…
# # Bring up the help text for common keybindings
# mod4 + {F1,Home}
# 	xterm -class "my_float_window" \
# 	-geometry '100x30' -e less "$HOME/.config/sxhkd/cheatsheet_sxhkdrc.txt"
