#####################################################
# i3 config
#     ->  https://i3wm.org/docs/userguide.html
#####################################################

# Font (TODO: Unnecessary?)
font pango:Source Han Code JP M 7

# Special Keys
set $m Mod4
set $a Mod1
set $c Control
set $s Shift

# kill focused window
bindsym $m+$s+q kill

# Floating
floating_modifier $m
focus_follows_mouse no

# workspace layout (default|stacking|tabbed)
default_border pixel 1

# class                 border  backgr. text
client.focused          #ffd75f #ffd75f #121212
client.focused_inactive #222222 #222222 #888888
client.unfocused        #222222 #222222 #888888
client.urgent           #2f343a #900000 #ffffff
client.placeholder      #000000 #0c0c0c #ffffff
client.background       #ffffff



# Define names for default workspaces for which we configure key bindings later on.
# We use variables to avoid repeating the names in multiple places.
set $ws0   "0"
set $ws1   "1"
set $ws2   "2"
set $ws3   "3"
set $ws4   "4"
set $ws5   "5"
set $ws6   "6"
set $ws7   "7"
set $ws8   "8"
set $ws9   "9"
set $ws10  "10"

set $ws_music  "6"
set $ws_studio "7"
set $ws_media  "8"
set $ws_util   "9"
set $ws_game   "10"

set $ws_browser "21"
set $ws_console "22"
set $ws_emacs   "23"

{{- if eq .chezmoi.hostname "arch" }}
set $DELL "primary"
set $BenQ "HDMI-0"

workspace $ws0  output $BenQ
workspace $ws1  output $BenQ
workspace $ws2  output $BenQ
workspace $ws3  output $BenQ
workspace $ws4  output $DELL
workspace $ws5  output $DELL
workspace $ws6  output $DELL
workspace $ws7  output $DELL
workspace $ws8  output $BenQ
workspace $ws9  output $BenQ
workspace $ws10 output $DELL
workspace $ws_browser output $DELL
workspace $ws_console output $DELL
workspace $ws_emacs   output $DELL
{{- end }}


# switch to workspace
bindsym $m+Escape workspace $ws0
bindsym $m+a      workspace $ws1
bindsym $m+s      workspace $ws2
bindsym $m+d      workspace $ws3
bindsym $m+f      workspace $ws4
bindsym $m+g      workspace $ws5
bindsym $m+z      workspace $ws6
bindsym $m+x      workspace $ws7
bindsym $m+c      workspace $ws8
bindsym $m+v      workspace $ws9
bindsym $m+b      workspace $ws10

# move focused container to workspace
bindsym $m+$s+Escape move container to workspace $ws0
bindsym $m+$s+a      move container to workspace $ws1
bindsym $m+$s+s      move container to workspace $ws2
bindsym $m+$s+d      move container to workspace $ws3
bindsym $m+$s+f      move container to workspace $ws4
bindsym $m+$s+g      move container to workspace $ws5
bindsym $m+$s+z      move container to workspace $ws6
bindsym $m+$s+x      move container to workspace $ws7
bindsym $m+$s+c      move container to workspace $ws8
bindsym $m+$s+v      move container to workspace $ws9
bindsym $m+$s+b      move container to workspace $ws10

# change focus
bindsym $m+h focus left
bindsym $m+j focus down
bindsym $m+k focus up
bindsym $m+l focus right

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

# move focused window
bindsym $m+$s+h move left 20 px
bindsym $m+$s+Left move left 20 px
bindsym $m+$s+j move down 20 px
bindsym $m+$s+Down move down 20 px
bindsym $m+$s+k move up 20 px
bindsym $m+$s+Up move up 20 px
bindsym $m+$s+l move right 20 px
bindsym $m+$s+Right move right 20 px
bindsym $m+$s+Tab move scratchpad
bindsym $m+Tab scratchpad show

# Change split orientation - horizontal/vertical
bindsym $m+backslash split h
bindsym $m+minus split v

# enter fullscreen mode for the focused container
bindsym $m+o fullscreen toggle

# Change layout (stacked, tabbed, toggle split)
bindsym $m+y layout stacking
bindsym $m+u layout tabbed
bindsym $m+i layout toggle split

# toggle tiling / floating
bindsym $m+$s+p floating toggle

# change focus between tiling / floating windows
bindsym $m+p focus mode_toggle

# reload the configuration file
bindsym $m+$s+$c+$a+c reload
# restart i3 inplace (preserves your layout/session, can be used to upgrade i3)
bindsym $m+$s+$c+$a+r restart
# exit i3 (logs you out of your X session)
bindsym $m+$s+$c+$a+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'"


# resize window (you can also use the mouse for that)
bindsym --release $m+r mode "resize"
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 h resize shrink width  2 px
  bindsym j resize shrink height 2 px
  bindsym k resize grow   height 2 px
  bindsym l resize grow   width  2 px

  bindsym $s+h resize shrink width  10 px
  bindsym $s+j resize shrink height 10 px
  bindsym $s+k resize grow   height 10 px
  bindsym $s+l resize grow   width  10 px

  # back to normal: Enter or Escape or $m+r
  bindsym Return         mode "default"
  bindsym Escape         mode "default"
  bindsym $c+bracketleft mode "default"
  bindsym $c+c           mode "default"
  bindsym $c+g           mode "default"
  bindsym $m+r           mode "default"
}

bindsym XF86Launch7        resize shrink width  10 px
bindsym XF86Launch8        resize grow   width  10 px

bindsym XF86TouchpadToggle resize shrink height 10 px
bindsym XF86TouchpadOn     resize grow   height 10 px

{{ if eq .chezmoi.hostname "buzz" }}
#bindsym XF86MonBrightnessUp exec --no-startup-id xbacklight +2
#bindsym XF86MonBrightnessDown exec --no-startup-id xbacklight -2
bindsym XF86MonBrightnessUp exec --no-startup-id doas light -A 0.1
bindsym XF86MonBrightnessDown exec --no-startup-id doas light -U 0.1
{{ end }}

bindsym $m+bracketleft  move left;  resize set width 20 ppt
bindsym $m+bracketright move right; resize set width 80 ppt


#############################################
# Launch terminal
#############################################

## Alacritty
bindsym $m+comma exec --no-startup-id launch-console
bindsym $m+$s+comma exec --no-startup-id launch-console tmp
bindsym $m+$s+period exec --no-startup-id launch-console emacs



##############################################
# Control Audio - mpd(mpc) & pulseaudio
##############################################

# Volume control
# bindsym $a+grave     exec --no-startup-id pamixer -t   # toggle mute/unmute
# bindsym $a+Up        exec --no-startup-id pamixer -i 1 # increase volume 1%
# bindsym $a+k         exec --no-startup-id pamixer -i 1 # increase volume 1%
# bindsym $a+Down      exec --no-startup-id pamixer -d 1 # decrease volume 1%
# bindsym $a+j         exec --no-startup-id pamixer -d 1 # decrease volume 1%

bindsym $m+apostrophe exec --no-startup-id goto-window carla.Carla2 carla
bindsym $m+$s+quotedbl exec --no-startup-id launch-pavucontrol
# bindsym $m+$a+n       exec --no-startup-id start-music-console
bindsym $m+$a+j       exec --no-startup-id mpc --host 10.0.2.1 volume -2
bindsym $m+$a+k       exec --no-startup-id mpc --host 10.0.2.1 volume +2
bindsym $m+$a+l       exec --no-startup-id mpc --host 10.0.2.1 seek +00:00:05
bindsym $m+$a+h       exec --no-startup-id mpc --host 10.0.2.1 seek -00:00:05
bindsym $m+$a+m       exec --no-startup-id mpc --host 10.0.2.1 volume 0
bindsym $m+$a+Return  exec --no-startup-id mpc --host 10.0.2.1 toggle
bindsym $m+$a+comma   exec --no-startup-id mpc --host 10.0.2.1 prev
bindsym $m+$a+period  exec --no-startup-id mpc --host 10.0.2.1 next
bindsym $m+$a+slash   exec --no-startup-id nowplaying


#####################################
# Application Launchers
#####################################

# Browser
bindsym $m+m     exec --no-startup-id open-librewolf
bindsym $m+$s+m  exec --no-startup-id open-librewolf-private
bindsym $m+n     exec --no-startup-id toggl

# Emacs
bindsym $m+period exec --no-startup-id open-emacs
bindsym $m+slash exec --no-startup-id open-emacs-org-capture

# File manager
bindsym $m+grave exec --no-startup-id bring-window pcmanfm pcmanfm

# Dictionary
bindsym $m+$s+slash exec --no-startup-id goldendictxdg.sh

# Rofi
bindsym $m+Return       exec --no-startup-id rofi -show combi
bindsym $m+semicolon    exec --no-startup-id "copyq show"
bindsym $m+$s+semicolon exec --no-startup-id "rofimoji -f emojis html math miscellaneous musical supplemental_punctuation"

# Color pikcer
bindsym $m+$c+$s+a exec --no-startup-id gpick

#screen shot
bindsym $m+$c+$s+s exec --no-startup-id flameshot screen --path "$XDG_PICTURES_DIR/screenshots/" --clipboard
bindsym $m+$c+$s+d exec --no-startup-id flameshot gui
bindsym $c+$s+F11 exec --no-startup-id flameshot gui
bindsym $m+$c+$s+t exec --no-startup-id screencast.sh toggle

bindsym --release $m+BackSpace exec --no-startup-id "$XDG_CONFIG_HOME/polybar/toggle.sh"

# bindsym --release $m+$c+$s+Delete exec --no-startup-id systemctl suspend
bindsym --release $m+$c+$s+$a+Escape exec --no-startup-id systemctl suspend
bindsym --release $m+$s+$c+$a+z exec --no-startup-id slock
#       ^^^^^^^^^ Won't work without this.
# See: https://www.reddit.com/r/i3wm/comments/cae4o8/debian_10_slock_keybind_wont_lock_screen/

bindsym --release $m+$s+$c+$a+f exec --no-startup-id fcitx5-restart


## Notification: dunst
bindsym $m+$c+w exec dunstctl close-all
bindsym $m+$c+h exec dunstctl history-pop


## mozc
bindsym $m+$c+t exec /usr/lib/mozc/mozc_tool --mode=dictionary_tool
bindsym $m+$c+r exec /usr/lib/mozc/mozc_tool --mode=word_register_dialog

## Force window floating
# See: https://i3wm.org/docs/userguide.html#list_of_commands

## 16:9 ratio
#   160 : 90
#   320 : 180
#   480 : 270
#   640 : 360
#   800 : 450
#   960 : 540
#   1120 : 630
#   1280 : 720
#   1440 : 810

for_window [class="my-gtk-app"] floating enable, move position center
for_window [class="Imager" instance="rpi-imager"] float enable, move position center
for_window [class="discord|Signal|thunderbird"] title_window_icon padding 4px
for_window [class="Brave-browser|LibreWolf|firefoxdeveloperedition"] title_window_icon padding 4px
for_window [class="^.*"] title_format "%class | %title" # : %title"
for_window [class="^.*"] border pixel 1
for_window [class="Yad"] floating enable, resize set 640 900, move position center
for_window [class=".*mpv"] floating enable, resize set 1440 810
for_window [class="(p|P)cmanfm"] floating enable, resize set 800 560, move position center
for_window [class="feh"] floating enable #, resize set 800 600
for_window [class="Sxiv"] floating enable #, resize set 800 600
for_window [class="pavucontrol"] floating enable, resize set 720 560
for_window [class="QjackCtl"] floating enable, resize set 420 100, move position right
for_window [class="QjackCtl" title="Graph.*"] floating enable, resize set 1280 1280, move position center
for_window [class="qpwgraph"] floating enable, resize set 1440 960, move position center
for_window [class="Nvidia-settings"] floating enable, resize set 720 560
for_window [class="Bitwarden"] floating enable, resize set 900 560, move position center
for_window [class="Blueman-manager"] floating enable, resize set 900 560, move position center
for_window [class="Anki" title="(Add|Edit).*"] floating enable, resize set 800 600, move position center
for_window [class="Gpick"] floating enable #, resize set 800 600
for_window [class="flameshot"] floating enable
for_window [window_role="GtkFileChooserDialog"] floating enable, resize set 800 600
for_window [window_role="ECompEditorEvent-.*"] floating enable, resize set 800 600
for_window [class="copyq"] floating enable, move position center

for_window [title="FreeTube Mini-Player:.*"] floating enable, resize set 800 450
for_window [title="FreeTube"] layout tabbed
for_window [class="vlc"] layout tabbed

for_window [class="discord"] layout tabbed

for_window [class="Brave-browser" instance="www.evernote.com__client_web"] layout tabbed
for_window [class="Homebank"] floating enable, resize set 1200 900, move position center
for_window [class="Homebank" title="Budget*"] floating enable, resize set 840 750, move position center

for_window [title="Edit metadata.*" class="calibre.*"] floating enable, resize set 1000 800

for_window [title="Alacritty@tmux:tmp"] floating enable, resize set 800 800, move position center
for_window [title="Alacritty@tmux:emacs"] move position left, resize set width 20 ppt

for_window [class="org.remmina.Remmina"] floating enable, resize set 1200 900, move position center

# Browser
for_window [class="Brave"] layout tabbed
for_window [class="Firefox"] layout tabbed
for_window [class="LibreWolf"] layout tabbed
for_window [class="Chromium"] layout tabbed


# floating music applications
for_window [class="Hydrogen"] floating enable, resize set 1200 750, move position 1940 px 30 px
for_window [class="Hydrogen" title="Mixer"] floating enable, resize set 1200 750, move position 1940 px 790 px
for_window [class="Surge XT"] floating enable, resize set 1200 750, move position 3280 px 30 px
for_window [class="Qsynth"] floating enable, move position 2480 px 1200 px
for_window [class="Drumstick Metronome"] floating enable, resize set 100 100, move position 4000 px 1010 px
for_window [class="FLTK"] floating enable, resize set 1200 560, move position 3160 px 840 px # giada
for_window [class="FLTK" title="Warning"] floating enable, move position center
for_window [class="FLTK" title="Configuration"] floating enable, move position center
for_window [class="TkFDialog" title="Select Audio files"] floating enable, move position center 1000 px 800 px
for_window [class="Tk" title="Ultimate Vocal Remover"] floating enable, move position center
for_window [class="(g|G)metronome"] floating enable, move position center
for_window [class="LibreWolf" title=".*Extension:.*"] floating enable, resize set 720 840, move position center
for_window [class="(m|M)uted"] floating enable, resize set 1200 900, move position center
for_window [class="(a|A)udacity"] floating enable, resize set 1200 600, move position 3280 px 840 px

## assign application to specific workspace
### Tips: Run `xprop` (command line tool) to retrieve class, instance and title.
# Browser
assign [class="LibreWolf"] $ws_browser
assign [class="firefox-developer-edition"] $ws_browser
assign [class="Brave"] $ws_browser
assign [instance="track.toggl.com__timer"] $ws_util
assign [class="(c|C)hromium"] $ws_browser

# Terminal
assign [title="Alacritty@tmux:default"] $ws_console
assign [title="Alacritty@tmux:emacs"] $ws_emacs
assign [class="Emacs"] $ws_emacs


# VirtualBox setting
assign [class="VirtualBox Machine"] $ws4
assign [class="VirtualBox Manager"] $ws4
assign [class="Virt-manager"] $ws4

# Accounting Software
assign [class="Homebank"] $ws5


# Music Applications
assign [class="Hydrogen"] $ws_music # Drum machine
assign [class="Surge XT"] $ws_music # Synthesizer
assign [class="FLTK"] $ws_music # Giada (loop machine)
assign [class="muted"] $ws_music # music util tool
assign [class="(a|A)udacity"] $ws_music

# Photo/Music/Video Editing
assign [class="Rawtherapee"] $ws_studio # RAW image editor
assign [class="Ardour"]      $ws_studio # DAW
assign [class="Carla"]       $ws_studio # JACK patch bay
assign [class="Guitarix"]    $ws_studio # Guitar/Bass amp stack
assign [class="kdenlive"]    $ws_studio # video editor

# Media Applications
assign [class="(g|G)podder"] $ws_media
assign [class=".*Celluloid"] $ws_media
assign [class="Blanket"] $ws_media
assign [class="spot"] $ws_media
assign [class="Spotify"] $ws_media
assign [class="vlc"] $ws_media
assign [title="FreeTube Mini-Player:.*"] $ws_media
assign [title="FreeTube"] $ws_media


# Chat & Todo Applications
assign [class="discord"] $ws_util
assign [class="(s|S)lack"] $ws_util
for_window [class="Signal" instance="signal"] move to workspace $ws_util, layout tabbed

# Games
assign [class="steam|steamwebhelper"] $ws0
assign [class="retroarch"] $ws_game
assign [class="steam_app_.*" instance="steam_app_.*"] $ws_game
for_window [class="steam_app_.*" instance="steam_app_.*"] move to workspace $ws_game
assign [class="Hexcells Infinite.x86_64" instance="Hexcells Infinite.x86_64" title="Hexcells Infinite"] $ws_game
assign [class="steam_app_1172470" instance="steam_app_1172470" title="Apex Legends"] $ws_game
## KEY_INSERT_GAME_ASSIGN # Don't change left string. See ~/.config/fish/function/i3-assign-games.fish

#######################################################
# Auto Exec
#######################################################

# Power saving (xidlehook)
exec --no-startup-id powersave-on

# xss-lock (hook systemd-suspend)
exec --no-startup-id xss-lock -- slock

# Auto-hide mouse cursor
exec --no-startup-id unclutter --timeout 1 --ignore-scrolling &

# Key repeat rate
# exec_always --no-startup-id xset r rate 200 60

# compositor
exec_always --no-startup-id picom -b > /dev/null 2>&1

# Notification
exec --no-startup-id dunst

# Watch plug
exec --no-startup-id udiskie &

# Japanese Input
exec --no-startup-id fcitx5 -d

# Clipboard Manager
# exec_always --no-startup-id clipcatd -r
exec_always --no-startup-id copyq

# Wallpaper
exec --no-startup-id nitrogen --restore
exec_always --no-startup-id $HOME/.config/conky/start.sh

# # Launch terminal
# exec --no-startup-id launch-console
exec --no-startup-id emacs --daemon

# Note
# exec --no-startup-id note.sh
# exec --no-startup-id setsid sync-vimwiki.sh > /dev/null &

# Util
exec --no-startup-id goldendictxdg.sh
exec --no-startup-id flameshot > /dev/null 2>&1

# Audio
exec --no-startup-id start-mpd
exec --no-startup-id shairport-sync -o=pa
# JACK/Carla
exec --no-startup-id carla /home/inkch/Carla/play.carxp


# redshift (like f.lux)
exec --no-startup-id redshift

# Polybar
exec --no-startup-id "$XDG_CONFIG_HOME/polybar/toggle.sh"

# Transmission
exec --no-startup-id "transmission-daemon"

{{ if eq .chezmoi.hostname "buzz" }}
exec --no-startup-id "kmonad $XDG_CONFIG_HOME/kmonad/thinkpad.kbd"
{{ end }}


#######################################################
# i3-gaps
#######################################################
gaps inner 0
smart_gaps on
smart_borders on