#!/usr/bin/env bash

# create a template for the dell d830
# split into header, deflayer, defcfg and defsrc to allow composition.

# This is sourced by 020_kmonad.sh

# NB 'Fn', Vol up', 'Vol down' and 'Mute' do not give keycodes.
# The Fn key is intercepted by the Embedded Controller ("EC") of your laptop.
# It doesn't produce a normal key symbol
# /usr/bin/acpi_listen
#     button/volumedown VOLDN 00000080 00000000 K
#     button/volumeup VOLUP 00000080 00000000 K
#     button/mute MUTE 00000080 00000000 K
#     button/suspend SUSP 00000080 00000000 K
#     button/battery BAT 00000080 00000000 K
#     Fn F7 == video/switchmode VMOD 00000080 00000000 K
#
# UK ISO Layout
# https://www.toptal.com/developersvdcode
# /usr/bin/xev - for x events
# xev | grep -A2 --line-buffered '^KeyRelease' \
#   | sed -n '/keycode /s/^.*keycode \([0-9]*\).* (.*, \(.*\)).*$/\1 \2/p'
# Once you have the keycode you don't need the Fn to map the key.
#
# /usr/bin/showkey -S  - for kernel level (multimedia keys)
# /usr/bin/xkeycaps - for keymaps and events.
#
# /usr/share/X11/locale/en_US.UTF-8/Compose gives all the available characters
# available through AltGr

## numbersign Non-US Hash key : 1 numbersign/51 asciitilde
#
# * HID Usage ID:  0x070031 (Usage Page 0x07 = Keyboard, Usage ID 0x31 for Keyboard 3 and #)
# * Linux keycode: 51 (maps to KEY_3 or KEY_HASH)
# * USB HID code:  Backslash (via event.code, us position for \ key)
# * X11 keysym:    numbersign
# * Unicode:       U+0023 (#)
# * Location:      On UK/ISO keyboards, left of the enter key.

## backslash Non-US Back Slash : 94 backslash/4 bar
#
# * HID Usage ID:  0x070064 (Usage Page 0x07 = Keyboard, Usage ID 0x64)
# * Linux keycode: 94 (maps to KEY_102ND or KEY_BACKSLASH)
# * USB HID code:  IntlBackslash (via event.code)
# * X11 keysym:    backslash
# * Unicode:       U+005C (character \)
# * Location:      102nd key on ISO keyboards (between Left Shift and Z)

## sterling £ rather than # for shift 3 : 12 3/12 sterling
#
# * Linux keycode: 12 (on UK ISO layout, maps to KEY_3)
# * USB HID code:  Digit3 (via event.code)
# * X11 keysym:    sterling (when layout is set to gb)
# * HID Usage ID:  0x070031 (Keyboard 3 and #, interpreted as £ via layout mapping)
# * Unicode:       U+00A3 (British pound sign)

## EuroSign key as third key on 4 : 3 4/13 dollar/13 EuroSign
#
# * Linux keycode: 13 (maps to KEY_4 on most layouts)
# * USB HID code:  Digit4 (via event.code)
# * X11 keysym:    EuroSign
# * HID Usage ID:  0x070034 (Keyboard 4 and Dollar)
# * Unicode:       U+20AC (€)

# ## Fn key in blue
# # Moon - Fn F1 ==
#   * Linux keycode: 213
#   * X11 keysym:    XF86Suspend | 0x1008ffa7
# # Battery Fn F3
#   * Linux keycode: 244
#   * X11 keysym:    XF86Battery | 0x1008ff93
# # Screen = Fn F7
#   * Linux keycode:
#   * X11 keysym:
#   * ACPI:          video/switchmode VMOD 00000080 00000000 K
# # CRT/LCD = Fn F8
#   * Linux keycode:
#   * X11 keysym:
#   * ACPI:          video/switchmode VMOD 00000080 00000000
# # SysRq = Fn Prnt Scrn
#   * Linux keycode:
#   * X11 keysym:
#   * ACPI:
# # Scroll Lk = Fn Num Lk
#   * Linux keycode:
#   * X11 keysym:
#   * ACPI:
# # Break = Fn Pause
#   * Linux keycode:
#   * X11 keysym:
#   * ACPI:
# # Auto Brightness = Fn Left Arrow
#   * Linux keycode:  248       should be 244
#   * X11 keysym:     NoSymbol  should be
#   * ACPI:
# # Keyboard Light =  Fn Right Arrow
#   * Linux keycode:
#   * X11 keysym:
#   * ACPI:           conf_print_dell_d830_template_header() {
# # Brightness Up =   Fn Up Arrow
#   * Linux keycode:  233
#   * X11 keysym:     XF86MonBrightnessUp
#   * ACPI:           video/brightnessup BRTUP 00000086 00000000 K
# # Brightness Down = Fn Down Arrow
#   * Linux keycode:  232
#   * X11 keysym:     XF86MonBrightnessDown
#   * ACPI:           video/brightnessdown BRTDN 00000087 00000000 K
# # Plus = Fn /
#   * Linux keycode:  86
#   * X11 keysym:     KP_Add
#   * 77 Num_Lock:    KP_Add
# # Minus = Fn ;
#   * Linux keycode:  82
#   * X11 keysym:     KP_Subtract
#   * 77 Num_Lock:    KP_Subtract:
# # DOT = Fn .
#   * Linux keycode:  91
#   * X11 keysym:     KP_Decimal
#   * 77 Num_Lock:    KP_Decimal
# # Multiply = Fn P
#   * Linux keycode:  63
#   * X11 keysym:     KP_Multiply
#   * 77 Num_Lock:    KP_Multiply
# # Divide = Fn 0
#   * Linux keycode:  106
#   * X11 keysym:     KP_Divide
#   * 77 Num_Lock: :  KP_Divide
# Numbers
# # Zero = Fn M
#   * Linux keycode:  90
#   * X11 keysym:     KP_Insert
#   * 77 Num_Lock:    KP_0
# # One = Fn J
#   * Linux keycode:  87
#   * X11 keysym:     KP_End
#   * 77 Num_Lock:    KP_1
# # Two = Fn K
#   * Linux keycode:  88
#   * X11 keysym:     KP_Down
#   * 77 Num_Lock:    KP_2
# # Three = Fn L
#   * Linux keycode:  89
#   * X11 keysym:     KP_Next
#   * 77 Num_Lock:    LP_3
# # Four = Fn U
#   * Linux keycode:  83
#   * X11 keysym:     KP_Left
#   * 77 Num_Lock:    KP_4
# # Five = Fn I
#   * Linux keycode:  84
#   * X11 keysym:     KP_Begin
#   * 77 Num_Lock:    KP_5
# # Six = Fn O
#   * Linux keycode:  85
#   * X11 keysym:     KP_Right
#   * 77 Num_Lock:    KP_6
# # Seven = Fn 7
#   * Linux keycode:  79
#   * X11 keysym:     KP_Home
#   * 77 Num_Lock:    KP_7
# # Eight = Fn 8
#   * Linux keycode:  80
#   * X11 keysym:     KP_Up
#   * 77 Num_Lock:    KP_8
# # Nine = Fn 9
#   * Linux keycode:  81
#   * X11 keysym:     KP_Prior
#   * 77 Num_Lock:    KP_9

conf_print_dell_d830_template_header() {
  cat <<EOF
#| --------------------------------------------------------------------------

                      KMonad: ISO Dell D830 Template

This file contains UK and 'defsrc' configurations for a Dell D830
with an ISO keyboard.  Copy out the 'defsrc' layer to start your own
keyboard configuration.  Copy out the 'deflayer' template to easily start
a new layer with matching 'transparent' buttons.

-------------------------------------------------------------------------- |#

EOF
}
# conf_print_dell_d830_template_header | tee ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_header_generic() {
  cat <<EOF
#| --------------------------------------------------------------------------

                      KMonad: ISO Dell D830 Template

This file contains UK and 'defsrc' configurations for a Dell D830
with an ISO keyboard.  Copy out the 'defsrc' layer to start your own
keyboard configuration.  Copy out the 'deflayer' template to easily start
a new layer with matching 'transparent' buttons.

EOF
}
# conf_print_dell_d830_template_header_generic | tee ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_empty_generic() {
  cat <<EOF
(deflayer empty
        _    _    _                                _    _    _    _    _    _    _apt-cache search 
  _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _
    _    _    _    _    _    _    _    _    _    _    _    _    _     _
    _    _    _    _    _    _    _    _    _    _    _    _    _     _
    _    _    _    _    _    _    _    _    _    _    _    _          _
    _    _    _    _    _    _    _    _    _    _    _               _
    _    _    _    _              _              _    _               _    
                                                                _    _    _
)
-------------------------------------------------------------------------- |#

EOF
}
# conf_print_dell_d830_template_deflayer_empty_generic | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_empty() {
  cat <<EOF
(deflayer empty
        _    _    _                                _    _    _    _    _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _
    _    _    _    _    _    _    _    _    _    _    _    _    _     _
    _    _    _    _    _    _    _    _    _    _    _    _    _     _
    _    _    _    _    _    _    _    _    _    _    _    _          _
    _    _    _    _    _    _    _    _    _    _    _               _
    _    _    _    _              _              _    _               _    
                                                                _    _    _
)

EOF
}
# conf_print_dell_d830_template_deflayer_empty | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_defcfg_generic() {
  cat <<EOF
(defcfg
  ;; ** For Linux **
  ;;input  (device-file "/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd")
  input  (device-file "/dev/input/by-path/platform-i8042-serio-0-event-kbd")
  output (uinput-sink "KMonad seniply-iso-wide")

  ;; ** For Windows **
  ;; input  (low-level-hook)
  ;; output (send-event-sink)

  ;; ** For MacOS **
  ;; input  (iokit-name "my-keyboard-product-string")
  ;; output (kext)

  fallthrough true
)

EOF
}
#conf_print_dell_d830_template_defcfg_generic | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_defcfg() {
  cat <<EOF
(defcfg
  input (device-file "/dev/input/by-path/platform-i8042-serio-0-event-kbd")
  output (uinput-sink "Kmonad kbd")
    "/run/current-system/sw/bin/sleep 1 && /run/current-system/sw/bin/setxkbmap -option caps:swapescape,compose:ralt")

  cmp-seq 'ralt'     ;; Set the compose key to 'RightAlt'
  comp-seq-delay '5' ;; 5ms delay between each compose-key sequence press

  fallthrough 'true' ;; Set to true to emit unhandled events
  allow-cmd 'false'  ;; Set to false to disable command execution in KMonad 
)

EOF
}
# conf_print_dell_d830_template_defcfg | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_defsrc() {
  cat <<EOF
(defsrc
      vold volu mute                        gui  kprt nlck pause   ins  home pgup
esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12     del  end  pgdn
  grv  1    2    3    4    5    6    7    8    9    0    -     =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [     ]    
  caps a    s    d    f    g    h    j    k    l    ;    '     \#    ret
  lsft z    x    c    v    b    n    m    ,    .    /               rsft
  lctl fn   lmet lalt           spc            ralt  rctl           up   
                                                              left down rght
)

EOF
}
#conf_print_dell_d830_template_defsrc | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_defalias_extend() {
  cat <<EOF
(defalias
  ext  (layer-toggle extend) ;; Bind 'ext' to the Extend Layer
)

EOF
}
conf_print_dell_d830_template_defalias_extend | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_defalias() {
  cat <<EOF
(defalias
  o
  cpy C-c
  pst C-v
  cut C-x
  udo C-z
  all C-a
  fnd C-f
  bk Back
  fw Forward
)

EOF
}
#conf_print_dell_d830_template_defalias | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

# Using this as tje first layer after the defsrc preserves querty as the default
conf_print_dell_d830_template_deflayer_querty() {
  cat <<EOF
(deflayer querty
      vold volu mute                        gui  kprt nlck pause   ins  home pgup
  esc  f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12     del  end  pgdn
  grv  1    2    3    4    5    6    7    8    9    0    -     =    bspc
  tab  q    w    e    r    t    y    u    i    o    p    [     ]    
  caps a    s    d    f    g    h    j    k    l    ;    '     \#    ret
  lsft z    x    c    v    b    n    m    ,    .    /               rsft
  lctl fn   lmet lalt           spc            ralt  rctl           up   
                                                              left down rght
)

EOF
}
#conf_print_dell_d830_template_deflayer_querty | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_colemak_dh() {
  cat <<EOF
(deflayer colemak-dh
        _    _    _                                _    _    _    _    _    _    _
  esc     f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12   _    _    _
  grv      1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab      q    w    f    p    b    j    l    u    y    ;    [    ]
  @ext     a    r    s    t    g    m    n    e    i    o    '    \\   ret
  lsft  z    x    c    d    v    102d k    h    ,    .    /    rsft
  lctl   _ lmet lalt           spc            ralt rmet               _
                                                                _    _    _
)

EOF
}
#conf_print_dell_d830_template_deflayer_colemak_dh | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_colemak_dhk() {
  cat <<EOF
(deflayer colemak-dhk
        _    _    _                                _    _    _    _    _    _    _
  esc     f1   f2   f3   f4   f5   f6   f7   f8   f9   f10  f11  f12   _    _    _
  grv      1    2    3    4    5    6    7    8    9    0    -    =    bspc
  tab      q    w    f    p    b    j    l    u    y    ;    [    ]
  @ext     a    r    s    t    g    k    n    e    i    o    '    \\   ret
  lsft  z    x    c    d    v    102d m    h    ,    .    /    rsft
  lctl   _ lmet lalt           spc            ralt rmet              _
                                                              _    _    _
)

EOF
}
#conf_print_dell_d830_template_deflayer_colemak_dhk | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_extend() {
  cat <<EOF
(deflayer extend
        _    _    _                                _    _    _    _    _    _    _
  _        play rewind previoussong nextsong ejectcd refresh brdn brup www mail prog1 prog2
  _        f1   f2   f3   f4   f5   f6   f7   f8   f9  f10   f11  f12  _    _    _
  _        esc  @bk  @fnd @fw  ins  pgup home up   end  menu prnt slck
  _        lalt lmet lsft lctl ralt pgdn lft  down rght del  caps _    _
  _     @udo @cut @cpy  tab  @pst _   pgdn bks  lsft lctl comp _
  _      _    _    _           ret               _    _               _ 
                                                                _    _    _
)

EOF
}
#conf_print_dell_d830_template_deflayer_extend | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_fn() {
  cat <<EOF
  
;; Fn SysRq == sysrq a legacy key shared with the Print Screen key
;; Fn Scroll Lk == slck shared with Num Lk
;; Fn Break == shared with Pause, a Break signal (e.g., for debugging or interrupting code)
;; Fn F1 moon == ssrq sleep/hibernate
;; Fn F3 battery == battery charging on or off (not on linux)
;; Fn F7 screen == display options
;; Fn F8 battery CRT/LCD == toggle between display modes (e.g., laptop screen, external monitor, dual display).

;; Fn 7 == kp7 numpad
;; Fn 8 == kp8 numpad
;; Fn 9 == kp9 numpad
;; Fn 0 == kp/ numpad
;; Fn U == kp4 numpad
;; Fn I == kp5 numpad
;; Fn O == kp6 numpad
;; Fn P == kp* numpad
;; Fn J == kp1 numpad
;; Fn K == kp2 numpad
;; Fn L == kp3 numpad
;; Fn ; == kp- numpad
;; Fn M == kp0 numpad
;; Fn . == kp. numpad
;; Fn / == kp+ numpad 

;; Fn <- left  == screen brightness auto
;; Fn -> right == keyboard light (missing)
;; Fn ↑  up    == increase screen brightness
;; Fn ↓  down  == increase screen brightness

(deflayer fn
;;      vold volu mute                         -  syrq slck pause    -   -  -
   -  ssrq   -   f3    -    -    -   f7   f8    -    -   -   -      -  -   -
   -  -    -    -    -    -    -    kp7    kp8    kp9    kp/    -     -    -
   -  -    -    -    -    -    -    kp4    kp5    kp6    kp*    -     -    
   -  -    -    -    -    -    -    kp1    kp2    kp3    kp-    -     -     -
   -  -    -    -    -    -    -    -    kp0    -    kp.    kp+                -
   -  -    -    -            -              -    -                 up   
                                                              left down rght
)

EOF
}
#conf_print_dell_d830_template_deflayer_fn | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd

conf_print_dell_d830_template_deflayer_empty() {
  cat <<EOF

(deflayer empty
        _    _    _                                _    _    _    _    _    _    _
  _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _    _
    _    _    _    _    _    _    _    _    _    _    _    _    _     _
    _    _    _    _    _    _    _    _    _    _    _    _    _     _
    _    _    _    _    _    _    _    _    _    _    _    _          _
    _    _    _    _    _    _    _    _    _    _    _               _
    _    _    _    _              _              _    _               _    
                                                                _    _    _
)

EOF
}
#conf_print_dell_d830_template_deflayer_empty | tee -a ${DEST}/.config/kmonad/keymap/template/dell_D830_iso.kbd
