#
# TODO: this could be made public without duplication by moving into puppet, in /etc/i3/config
#
# i3 config file (v4)
#
# Please see http://i3wm.org/docs/userguide.html for a complete reference!

# TODO:
#
# * assign certain clients to certain windows, to bootstrap session
#   properly: https://i3wm.org/docs/userguide.html#assign_workspace
#
# * consider focus_on_window_activation urgent
#   https://i3wm.org/docs/userguide.html#focus_on_window_activation
#
# * make default layout be "large window on top with two small windows
#   below", like in Xmonad, instead of "just two large windows". i
#   still have trouble adjusting to the "hierarchical container"
#   configuration - way too geeky abstraction that is not really
#   necessary...
#
# * i miss "alt-shift-enter" from xmonad, which would bring the
#   focused window in the primary workspace/container. alt-shift-space
#   (float this window) is a good alternative
#
# * sticky mode for writeroom-mode
#
# * i3 doesn't respect $PATH set in ~/.shenv from .xsession? look for
#   /home/anarcat/bin/ once this is fixed.
#
# * resize keybindings are weird: "up" should move the *boundary* up,
#   not shrink/grow, because that's context-specific

# left "windows" key
set $mod Mod4

# Font for window titles. Will also be used by the bar unless a different font
# is used in the bar {} block below.
# This font is widely installed, provides lots of unicode glyphs, right-to-left
# text rendering and scalability on retina/hidpi displays (thanks to pango).
font pango:Fira mono 10
# Before i3 v4.8, we used to recommend this one as the default:
# font -misc-fixed-medium-r-normal--13-120-75-75-C-70-iso10646-1
# The font above is very space-efficient, that is, it looks good, sharp and
# clear in small sizes. However, its unicode glyph coverage is limited, the old
# X core fonts rendering does not support right-to-left and this being a bitmap
# font, it doesn’t scale on retina/hidpi displays.

# Use Mouse+$mod to drag floating windows to their wanted position
floating_modifier $mod

# guess vertical vs horizontal orientation based on screen size
default_orientation auto

# going to workspace 3 twice brings me back
workspace_auto_back_and_forth no

# wait before canceling urgency to see which window warned
force_display_urgency_hint 500 ms

new_window pixel 1

# start a terminal
bindsym $mod+Return exec i3-sensible-terminal

# kill focused window
#bindsym $mod+Shift+q kill
# xmonad backwards-compat
bindsym $mod+Shift+c kill

# start dmenu (a program launcher)
bindsym $mod+d exec dmenu_run

# There also is the (new) i3-dmenu-desktop which only displays applications
# shipping a .desktop file. It is a wrapper around dmenu, so you need that
# installed.
# bindsym $mod+d exec --no-startup-id i3-dmenu-desktop

# change focus
bindsym $mod+j focus left
bindsym $mod+k focus down
bindsym $mod+l focus up
bindsym $mod+semicolon focus right

# alternatively, you can use the cursor keys:
#bindsym $mod+Left focus left
bindsym $mod+Down focus down
bindsym $mod+Up focus up
#bindsym $mod+Right focus right

#bindsym $mod+Tab focus right
#bindsym $mod+Shift+Tab focus left

# this unfortunately doesn't work
#bindsym $mod+Tab focus next
#bindsym $mod+Shift+Tab focus prev

# https://gitlab.com/anarcat/scripts/blob/master/i3-focus
bindsym $mod+Tab exec /home/anarcat/bin/i3-focus next
bindsym $mod+Shift+Tab exec /home/anarcat/bin/i3-focus prev

# move focused window
bindsym $mod+Shift+j move left
bindsym $mod+Shift+k move down
bindsym $mod+Shift+l move up
bindsym $mod+Shift+semicolon move right

# alternatively, you can use the cursor keys:
bindsym $mod+Shift+Left move left
bindsym $mod+Shift+Down move down
bindsym $mod+Shift+Up move up
bindsym $mod+Shift+Right move right

# split in horizontal orientation
bindsym $mod+h split h

# split in vertical orientation
bindsym $mod+v split v

# enter fullscreen mode for the focused container
bindsym $mod+f fullscreen

# change container layout (stacked, tabbed, toggle split)
bindsym $mod+s layout stacking
bindsym $mod+w layout tabbed
bindsym $mod+e layout toggle split

# toggle tiling / floating
bindsym $mod+Shift+space floating toggle

# change focus between tiling / floating windows
bindsym $mod+space focus mode_toggle

# focus the parent container
bindsym $mod+a focus parent

# focus the child container
#bindsym $mod+d focus child

# switch to workspace
bindsym $mod+1 workspace 1
bindsym $mod+2 workspace 2
bindsym $mod+3 workspace 3
bindsym $mod+4 workspace 4
bindsym $mod+5 workspace 5
bindsym $mod+6 workspace 6
bindsym $mod+7 workspace 7
bindsym $mod+8 workspace 8
bindsym $mod+9 workspace 9
bindsym $mod+0 workspace 10

# move focused container to workspace
bindsym $mod+Shift+1 move container to workspace 1
bindsym $mod+Shift+2 move container to workspace 2
bindsym $mod+Shift+3 move container to workspace 3
bindsym $mod+Shift+4 move container to workspace 4
bindsym $mod+Shift+5 move container to workspace 5
bindsym $mod+Shift+6 move container to workspace 6
bindsym $mod+Shift+7 move container to workspace 7
bindsym $mod+Shift+8 move container to workspace 8
bindsym $mod+Shift+9 move container to workspace 9
bindsym $mod+Shift+0 move container to workspace 10

bindsym $mod+Right workspace next
bindsym $mod+Left workspace prev
bindsym $mod+Escape workspace back_and_forth

# https://i3wm.org/docs/userguide.html#move_to_outputs
# mod-shift-right/left will also do this, but for a container
bindsym $mod+Control+Right move workspace to output right
bindsym $mod+Control+Left move workspace to output right

bindsym $mod+i sticky toggle

# reload the configuration file
# taken by kill
#bindsym $mod+Shift+c reload
# restart i3 inplace (preserves your layout/session, can be used to upgrade i3)
#bindsym $mod+Shift+r restart
# exit i3 (logs you out of your X session)
#bindsym $mod+Shift+e exec "i3-nagbar -t warning -m 'You pressed the exit shortcut. Do you really want to exit i3? This will end your X session.' -b 'Yes, exit i3' 'i3-msg exit'"
#bindsym $mod+Shift+q exec "i3-nagbar -t warning -m 'You pressed the exit shortcut. Do you really want to exit i3? This will end your X session.' -b 'Yes, exit i3' 'i3-msg exit'"

# all replaced by a dmenu-like single "power" menu
# taken from https://faq.i3wm.org/question/1262/exiting-i3-without-mouse-click.1.html
bindsym $mod+q exec i3-power-menu

# https://faq.i3wm.org/question/6412/focus-nagbar-with-keyboard.1.html
# fails while in taffybar because no "mode" feedback
#bindsym $mod+0 mode "mode_system"
set $mode_system (l)ock, (e)xit, switch_(u)ser, (s)uspend, (h)ibernate, (r)eboot, (Shift+s)hutdown
mode "mode_system" {
bindsym l exec --no-startup-id xset s activate
bindsym s exec --no-startup-id i3exit suspend, mode "default"
bindsym u exec --no-startup-id i3exit switch_user, mode "default"
bindsym e exec --no-startup-id i3exit logout, mode "default"
bindsym h exec --no-startup-id i3exit hibernate, mode "default"
bindsym r exec --no-startup-id i3exit reboot, mode "default"
bindsym Shift+s exec --no-startup-id i3exit shutdown, mode "default"

# exit system mode: "Enter" or "Escape"
bindsym Return mode "default"
bindsym Escape mode "default"
}

# resize window (you can also use the mouse for that)
mode "resize" {
        # These bindings trigger as soon as you enter the resize mode

        # Pressing left will shrink the window’s width.
        # Pressing right will grow the window’s width.
        # Pressing up will shrink the window’s height.
        # Pressing down will grow the window’s height.
        bindsym j resize shrink width 10 px or 10 ppt
        bindsym k resize grow height 10 px or 10 ppt
        bindsym l resize shrink height 10 px or 10 ppt
        bindsym semicolon resize grow width 10 px or 10 ppt

        # same bindings, but for the arrow keys
        bindsym Left resize shrink width 2 px or 1 ppt
        bindsym Down resize shrink height 2 px or 1 ppt
        bindsym Up resize grow height 2 px or 1 ppt
        bindsym Right resize grow width 2 px or 1 ppt

        # back to normal: Enter or Escape
        bindsym Return mode "default"
        bindsym Escape mode "default"
}

bindsym $mod+x mode "resize"

# all my customs keybindings
bindsym $mod+m exec i3-input -F 'mark %s' -l 1 -P 'Mark: '
bindsym $mod+Shift+m exec i3-input -F '[con_mark="%s"] focus' -l 1 -P 'Go to: '

# cargo-culted from https://i3wm.org/docs/userguide.html#_scratchpad
# Make the currently focused window a scratchpad
bindsym $mod+Shift+minus move scratchpad
# Show the first scratchpad window
bindsym $mod+minus scratchpad show

# super-fast shortcuts of death everywhere
bindsym $mod+y exec /home/anarcat/bin/e
# should open the home directory with the preconfigured file
# manager. now what *that* will be is mystery!
bindsym $mod+u exec xdg-open .
#
# the way to find out what xdg-open will do is:
#
# xdg-mime query default $(xdg-mime query filetype $ARG)
#
# directories are `inode/directory`, so to set it:
#
# $ xdg-mime default Thunar.desktop inode/directory
#
# and it is correctly set:
#
# $ xdg-mime query default inode/directory
# Thunar.desktop
bindsym $mod+c exec qalculate

# rofi
bindsym $mod+r exec /home/anarcat/bin/rofi-wrap -show run
bindsym $mod+F2 exec /home/anarcat/bin/rofi-wrap -show ssh  -ssh-client $HOME/bin/ssh-wait-on-fail
# until mosh falls back to SSH (https://github.com/mobile-shell/mosh/issues/731)
bindsym $mod+F3 exec /home/anarcat/bin/rofi-wrap -show ssh -ssh-client mosh
bindsym $mod+g exec /home/anarcat/bin/rofi-wrap -show window
bindsym $mod+p exec /home/anarcat/bin/rofi-wrap -show pass
bindsym $mod+Shift+p exec env PASS_ROFI_MODE=type /home/anarcat/bin/rofi-wrap -show pass

# screensaver panic button
bindsym Pause exec xset s activate
# screenshot
bindsym Print exec /home/anarcat/bin/publish -S --select
bindsym Shift+Print exec /home/anarcat/bin/publish -S --select -t 7
bindsym Control+Print exec /home/anarcat/bin/publish -S
# multimedia keys
bindsym XF86AudioMute exec /home/anarcat/bin/i3-pa-mixer volume mute
# see also https://git.joeyh.name/index.cgi/joey/home.git/tree/bin/mic_mute
bindsym XF86AudioMicMute exec /home/anarcat/bin/i3-pa-mixer volume mute-mic
# also use "shift-mute" as an alternative to mic mute, because there's no such keysym on WASD keyboards
bindsym Shift+XF86AudioMute exec /home/anarcat/bin/i3-pa-mixer volume mute-mic
bindsym XF86AudioLowerVolume exec /home/anarcat/bin/i3-pa-mixer volume down
bindsym XF86AudioRaiseVolume exec /home/anarcat/bin/i3-pa-mixer volume up

# brightness. needs this in /etc/X11/xorg.conf
#
# Section "Device"
#     Identifier  "Card0"
#     Driver      "intel"
#     Option      "Backlight"  "intel_backlight"
# EndSection
bindsym XF86MonBrightnessDown exec xbacklight -dec 5
bindsym XF86MonBrightnessUp exec xbacklight -inc 5

for_window [title="pop-up"] floating enable
for_window [class="Xmessage"] floating enable
for_window [class="Qalculate"] floating enable

# more Vincent Bernat cargo-culting:
# https://github.com/vincentbernat/i3wm-configuration/blob/master/config
no_focus [window_type="splash"]
#for_window [tiling] border pixel 3
for_window [class="Nm-connection-editor"] floating enable
#for_window [class="Shadow"] fullscreen enable
for_window [window_role="PictureInPicture"] floating enable
for_window [title="Firefox — Sharing Indicator"] border pixel 1, sticky enable, move position 20 ppt -5 px
no_focus [title="Firefox — Sharing Indicator"]
for_window [class="Pavucontrol"] floating enable

#exec --no-startup-id /home/anarcat/bin/i3-load-layouts

# extract colors from Xresources:
# https://i3wm.org/docs/userguide.html#xresources
#
# this actually doesn't work (!?) so we fallback to srcery colors
# here, see: https://github.com/i3/i3/discussions/5051
set_from_resource $black i3wm.color0 #1C1B19
set_from_resource $bright_black i3wm.color8 #918175

set_from_resource $red i3wm.color1 #EF2F27
set_from_resource $bright_red i3wm.color9 #F75341

set_from_resource $green i3wm.color2 #519F50
set_from_resource $bright_green i3wm.color10 #98BC37

set_from_resource $yellow i3wm.color3 #FBB829
set_from_resource $bright_yellow i3wm.color11 #FED06E

set_from_resource $blue i3wm.color4 #2C78BF
set_from_resource $bright_blue i3wm.color12 #68A8E4

set_from_resource $magenta i3wm.color5 #E02C6D
set_from_resource $bright_magenta i3wm.color13 #FF5C8F

set_from_resource $cyan i3wm.color6 #0AAEB3
set_from_resource $bright_cyan i3wm.color14 #53FDE9

set_from_resource $white i3wm.color7 #D0BFA1
set_from_resource $bright_white i3wm.color15 #FCE8C3

# those are not defined in the srcery .Xresources as they fall outside
# of the 16 colors set. but there are other 256 colors in xterm that
# srcery just defaults to. we still give Xresources a chance to
# override those. see also
# https://github.com/srcery-colors/srcery-terminal/blob/ca6ac7406ced8b0b7c4e4b2188d3d5b48668f9c3/README.md
# https://github.com/srcery-colors/srcery-terminal/issues/161
set_from_resource $orange i3wm.color202 #D75F00
set_from_resource $bright_orange i3wm.color208 #FF8700

set_from_resource $xgray1 i3wm.color235 #262626
set_from_resource $xgray2 i3wm.color236 #303030
set_from_resource $xgray3 i3wm.color237 #3A3A3A
set_from_resource $xgray4 i3wm.color238 #444444
set_from_resource $xgray5 i3wm.color239 #4E4E4E

set_from_resource $hard_black i3wm.color233 #121212

# Colors                border        background  text          indicator child_border
client.focused          $bright_black $xgray3     $yellow       $yellow   $bright_black
client.focused_inactive $xgray3       $xgray3     $bright_black $xgray3   $xgray3
client.unfocused        $xgray1       $xgray1     $bright_black $xgray1   $xgray1
client.urgent           $red          $xgray1     $red          $red      $red
client.placeholder      $xgray3       $hard_black $bright_black $xgray3   $xgray3
client.background       $black

# Start i3bar to display a workspace bar (plus the system information i3status
# finds out, if available)
bar {
        # pango-list can help finding fonts here
        font pango:FontAwesome, Fira mono 10
        # a status bar with:
        # window network cpu-health cpu load memory battery volume datetime date
        status_command exec py3status
        # i used bumblebee-status briefly but switched because it used too much CPU
        # https://github.com/tobi-wan-kenobi/bumblebee-status/issues/891
        position top
        # obey Fitt's law, ie. reduce the empty space
        tray_padding 0
        # show tray, and on primary workspace
        tray_output primary
        # colors are documented here:
        # https://i3wm.org/docs/userguide.html#_colors
        # that reuses the colors set above, and might not actually
        # affect the status bar for anything else than i3statu
        colors {
              background $black
              statusline $white
              separator  $white

              # Colors           border        background   text
              focused_workspace  $bright_black $xgray3      $yellow
              active_workspace   $xgray3       $xgray3      $bright_black
              inactive_workspace $xgray3       $xgray1      $bright_black
              urgent_workspace   $red          $red         $black
              binding_mode       $magenta      $magenta     $bright_white
        }
}
