// hid_liber custom
// ANSI layout with FN key instead of APP button
// Layer 1 has mediakeys on the nav cluster and keypad numbers for alt-codes
// build and upload: see : doc/keymap.md  use PJRC variant
//  apt install gcc-avr avr-libc dfu-programmer
//
//
// (plug in different keyboard)
// TO ENTER PROGRAM MODE:
// - use a small magnet and run it in the space just above the "up" arrow key
// OR
// - unplug keyboard, hold 'b' + 'space' keys, plug in cable. When it's plugged in, there should be nothing typed when you press the space bar
//
// cd keyboard/hid_liber
// make -f Makefile.pjrc clean && make -f Makefile.pjrc custom && dfu-programmer atmega32u4 erase && dfu-programmer atmega32u4 flash hid_liber_pjrc.hex && dfu-programmer atmega32u4 start
// the 'start' command will start up the keyboard.
//

static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
/* Layer 0: Default ANSI
 *
 * ,---.   ,---------------. ,---------------. ,---------------. ,-----------.
 * |Esc|   |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau|
 * `---'   `---------------' `---------------' `---------------' `-----------'
 * ,-----------------------------------------------------------. ,-----------.
 * |~  |  1|  2|  3|  4|  5|  6|  7|  8|  9|  0|  -|  =|Backsp | |Ins|Hom|PgU|
 * |-----------------------------------------------------------| |-----------|
 * |Tab  |  Q|  W|  E|  R|  T|  Y|  U|  I|  O|  P|  [|  ]|    \| |Del|End|PgD|
 * |-----------------------------------------------------------| `-----------'
 * |Caps  |  A|  S|  D|  F|  G|  H|  J|  K|  L|  ;|  '|Return  |
 * |-----------------------------------------------------------|     ,---.
 * |Shft|iso|  Z|  X|  C|  V|  B|  N|  M|  ,|  .|  /|Shift     |     |Up |
 * |-----------------------------------------------------------| ,-----------.
 * |Ctl|Gui|Alt|          Space                |Alt|Gui|FN1|Ctl| |Lef|Dow|Rig|
 * `-----------------------------------------------------------' `-----------'
 */

// DEFAULT (CAPS is LEFT GUI, normal ALT keys)
  KEYMAP(\
      ESC,   F1,   F2,   F3,   F4,   F5,   F6,   F7,   F8,   F9,  F10,  F11,  F12,             PSCR, SLCK,  BRK, \
      GRV,    1,    2,    3,    4,    5,    6,    7,    8,    9,    0, MINS,  EQL, BSPC,        INS, HOME, PGUP, \
      TAB,    Q,    W,    E,    R,    T,    Y,    U,    I,    O,    P, LBRC, RBRC, BSLS,        DEL,  END, PGDN, \
	 LGUI,    A,    S,    D,    F,    G,    H,    J,    K,    L, SCLN, QUOT,        ENT,                         \
     LSFT, NUBS,    Z,    X,    C,    V,    B,    N,    M, COMM,  DOT, SLSH,       RSFT,               UP,       \
     LCTL,   NO, LALT,              SPC,                         RALT, RGUI,  FN0, RCTL,        LEFT, DOWN, RGHT),




// F1 (CAPS is LEFT GUI, LEFT GUI is RALT for AltGr/international key mapping). RALT key is LALT (alt keys are the "same key")
  KEYMAP(\
      ESC,   F1,   F2,   F3,   F4,   F5,   F6,   F7,   F8,   F9,  F10,  F11,  F12,             PSCR, SLCK,  BRK, \
      GRV,    1,    2,    3,    4,    5,    6,    7,    8,    9,    0, MINS,  EQL, BSPC,        INS, HOME, PGUP, \
      TAB,    Q,    W,    E,    R,    T,    Y,    U,    I,    O,    P, LBRC, RBRC, BSLS,        DEL,  END, PGDN, \
	 LGUI,    A,    S,    D,    F,    G,    H,    J,    K,    L, SCLN, QUOT,        ENT,                         \
     LSFT, NUBS,    Z,    X,    C,    V,    B,    N,    M, COMM,  DOT, SLSH,       RSFT,               UP,       \
     LCTL, RALT, LALT,              SPC,                         LALT, RGUI,  FN0, RCTL,        LEFT, DOWN, RGHT),


// F2
 /* CAPS + LGUI + RALT restore (basically, back to a normal keyboard) */
 KEYMAP(\
    TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS,       TRNS, TRNS, TRNS, \
    TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, \
    TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, \
    CAPS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS,       TRNS,                   \
    TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS,       TRNS,       TRNS,       \
    TRNS, LGUI, TRNS,                   TRNS,                   RALT, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS),


// F3
  /* dvorak */
  KEYMAP(\
     ESC,   F1,   F2,   F3,   F4,   F5,   F6,   F7,   F8,   F9,  F10,  F11,  F12,             PSCR, SLCK,  BRK, \
     GRV,    1,    2,    3,    4,    5,    6,    7,    8,    9,    0, LBRC, RBRC, BSPC,        INS, HOME, PGUP, \
     TAB, QUOT, COMM,  DOT,    P,    Y,    F,    G,    C,    R,    L, SLSH,  EQL, BSLS,        DEL,  END, PGDN, \
    LCTL,    A,    O,    E,    U,    I,    D,    H,    T,    N,    S, MINS,        ENT,                         \
    LSFT, NUBS, SCLN,    Q,    J,    K,    X,    B,    M,    W,    V,    Z,       RSFT,               UP,       \
    LCTL,   NO, LALT,              SPC,                         RALT, RGUI, FN0, RCTRL,        LEFT, DOWN, RGHT),



// MUST BE LAST POSITION!
/*
 * with (app key) held down
 * 		Media Keys
 * 		Keypad numbers instead of normal numbers
 * 		Calculator (c)
 * 		Sleep (pause)
 * 		LGUI (because sometimes it's disabled)
 */
 KEYMAP(\
	NO,  FN1,  FN2,  FN3,   NO,   NO,   NO,   NO,   NO,   NO,   NO, NO,  NO,              NO,   NO, SLEP, \
	NO,   NO,   NO,   NO, FN20,   NO,   NO,   NO,   NO,   NO,   NO, NO,  NO, NO,        MSEL, MSEL, VOLU, \
	NO,   NO,   NO,   NO,   NO,   NO,   NO, FN17,   NO, FN18,   NO, NO,  NO, NO,        MUTE, MUTE, VOLD, \
  CAPS, FN16, FN19,   NO,   NO,   NO,   NO,   NO,   NO,   NO,   NO, NO,      NO,                         \
  FN21,   NO,   NO,   NO, CALC,   NO,   NO,   NO,   NO,   NO,   NO, NO,    FN22,              MPLY,      \
	NO, LGUI,   NO,               NO,                           NO, NO, FN0, NO,        MPRV, MSTP, MNXT),




};

enum macro_id {
    A, U, O, S, EURO, LSHIFT, RSHIFT
};

static const uint16_t PROGMEM fn_actions[] = {
	// default is layer 0
    [0] = ACTION_LAYER_MOMENTARY(4), // function overlay + media keys + LGUI access
    [1] = ACTION_LAYER_TOGGLE(1),    // F1 - CAPS is LGUI, LGUI is RALT for international layouts, RALT is LALT (so both alt keys are the "same key")
    [2] = ACTION_LAYER_TOGGLE(2),    // F2 - CAPS + LGUI + RALT restore (basically, back to a normal keyboard)
    [3] = ACTION_LAYER_TOGGLE(3),    // F3 - dvorak layer
    [16] = ACTION_MACRO(A),          // Q - ä  (if keyboard is set to international US)
    [17] = ACTION_MACRO(U),          // Y - ü  (if keyboard is set to international US)
    [18] = ACTION_MACRO(O),          // P - ö  (if keyboard is set to international US)
    [19] = ACTION_MACRO(S),          // S - ß  (if keyboard is set to international US)
    [20] = ACTION_MACRO(EURO),       // 5 - €  (if keyboard is set to international US)
    [21] = ACTION_MACRO(LSHIFT),     // 5 - €  (if keyboard is set to international US)
    [22] = ACTION_MACRO(RSHIFT),     // 5 - €  (if keyboard is set to international US)
};
