Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

keymap.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Keymap for Macway mod
  3. */
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <avr/pgmspace.h>
  7. #include "usb_keyboard.h"
  8. #include "usb_keycodes.h"
  9. #include "matrix.h"
  10. #include "print.h"
  11. #include "debug.h"
  12. #include "util.h"
  13. #include "keymap.h"
  14. #define FN_KEYCODE(fn) (pgm_read_byte(&fn_keycode[(fn)]))
  15. #define FN_LAYER(fn) (pgm_read_byte(&fn_layer[(fn)]))
  16. #define KEYCODE(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)]))
  17. #define KEYMAP( \
  18. R1C1, R1C0, R2C0, R3C0, R4C0, R4C1, R5C1, R5C0, R6C0, R7C0, R8C0, R8C1, R6C1, R0C2, \
  19. R1C2, R1C3, R2C3, R3C3, R4C3, R4C2, R5C2, R5C3, R6C3, R7C3, R8C3, R8C2, R6C2, \
  20. R1C5, R1C4, R2C4, R3C4, R4C4, R4C5, R5C5, R5C4, R6C4, R7C4, R8C4, R8C5, R0C6, \
  21. R6C7, R1C6, R2C6, R3C6, R4C6, R4C7, R5C7, R5C6, R6C6, R7C6, R8C7, R3C2, R3C5, \
  22. R7C5, R2C2, R0C0, R0C7, R2C1, R0C4, R3C7, R2C7, R1C7 \
  23. ) { \
  24. { R0C0, KB_NO, R0C2, KB_NO, R0C4, KB_NO, R0C6, R0C7 }, \
  25. { R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7 }, \
  26. { R2C0, R2C1, R2C2, R2C3, R2C4, KB_NO, R2C6, R2C7 }, \
  27. { R3C0, KB_NO, R3C2, R3C3, R3C4, R3C5, R3C6, R3C7 }, \
  28. { R4C0, R4C1, R4C2, R4C3, R4C4, R4C5, R4C6, R4C7 }, \
  29. { R5C0, R5C1, R5C2, R5C3, R5C4, R5C5, R5C6, R5C7 }, \
  30. { R6C0, R6C1, R6C2, R6C3, R6C4, KB_NO, R6C6, R6C7 }, \
  31. { R7C0, KB_NO, KB_NO, R7C3, R7C4, R7C5, R7C6, KB_NO }, \
  32. { R8C0, R8C1, R8C2, R8C3, R8C4, R8C5, KB_NO, R8C7 } \
  33. }
  34. static int current_layer = 0;
  35. static bool layer_used = false;
  36. /* layer to change into while Fn key pressed */
  37. static const int PROGMEM fn_layer[] = { 0, 1, 2, 3, 4, 0, 2, 3 };
  38. /* keycode to sent when Fn key released without using layer keys. */
  39. static const uint8_t PROGMEM fn_keycode[] = {
  40. KB_NO, // FN_0 [NOT USED]
  41. KB_NO, // FN_1 layer 1
  42. KB_QUOTE, // FN_2 layer 2
  43. KB_SCOLON, // FN_3 layer 3
  44. KB_SPACE, // FN_4 layer 4 [NOT USED]
  45. KB_NO, // FN_5 [NOT USED]
  46. KB_NO, // FN_6 layer 2
  47. KB_NO // FN_7 layer 3
  48. };
  49. static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  50. /* Layer 0: Default Layer
  51. * ,-----------------------------------------------------------.
  52. * |Esc| 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backsp |
  53. * |-----------------------------------------------------------|
  54. * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| |
  55. * |-----------------------------------------------------' |
  56. * |Contro| A| S| D| F| G| H| J| K| L|Fn3|Fn2|Return |
  57. * |-----------------------------------------------------------|
  58. * |Shift | Z| X| C| V| B| N| M| ,| .| /|Shift |Fn1|
  59. * |-----------------------------------------------------------|
  60. * |Fn7|Gui |Alt |Space |Fn6 |\ |` | | |
  61. * `-----------------------------------------------------------'
  62. */
  63. KEYMAP(KB_ESC, KB_1, KB_2, KB_3, KB_4, KB_5, KB_6, KB_7, KB_8, KB_9, KB_0, KB_MINS,KB_EQL, KB_BSPC, \
  64. KB_TAB, KB_Q, KB_W, KB_E, KB_R, KB_T, KB_Y, KB_U, KB_I, KB_O, KB_P, KB_LBRC,KB_RBRC, \
  65. KB_LCTL,KB_A, KB_S, KB_D, KB_F, KB_G, KB_H, KB_J, KB_K, KB_L, FN_3, FN_2, KB_ENT, \
  66. KB_LSFT,KB_Z, KB_X, KB_C, KB_V, KB_B, KB_N, KB_M, KB_COMM,KB_DOT, KB_SLSH,KB_RSFT,FN_1, \
  67. FN_7, KB_LGUI,KB_LALT,KB_SPC, FN_6, KB_BSLS,KB_GRV, KB_NO, KB_NO),
  68. /* Layer 1: HHKB mode (HHKB Fn)
  69. * ,-----------------------------------------------------------.
  70. * |Pow| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Delete |
  71. * |-----------------------------------------------------------|
  72. * |Caps | | | | | | | |Psc|Slk|Pus|Up | | |
  73. * |-----------------------------------------------------' |
  74. * |Contro| | | | | | *| /|Hom|PgU|Lef|Rig|Enter |
  75. * |-----------------------------------------------------------|
  76. * |Shift | | | | | | +| -|End|PgD|Dow|Shift |xxx|
  77. * |-----------------------------------------------------------|
  78. * | |Gui |Alt | |Alt | | | | |
  79. * `-----------------------------------------------------------'
  80. */
  81. KEYMAP(KB_PWR, KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8, KB_F9, KB_F10, KB_F11, KB_F12, KB_DEL, \
  82. KB_CAPS,KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_PSCR,KB_SLCK,KB_BRK, KB_UP, KB_NO, \
  83. KB_LCTL,KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KP_ASTR,KP_SLSH,KB_HOME,KB_PGUP,KB_LEFT,KB_RGHT,KB_ENT, \
  84. KB_LSFT,KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KP_PLUS,KP_MINS,KB_END, KB_PGDN,KB_DOWN,KB_RSFT,FN_1, \
  85. KB_NO, KB_LGUI,KB_LALT,KB_SPC, KB_RALT,KB_NO, KB_NO, KB_NO, KB_NO),
  86. /* Layer 2: Vi mode (Quote/Rmeta)
  87. * ,-----------------------------------------------------------.
  88. * | `| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12| ` |
  89. * |-----------------------------------------------------------|
  90. * | \ |Hom|PgD|Up |PgU|End|Hom|PgD|PgU|End| | | | |
  91. * |-----------------------------------------------------' |
  92. * |Contro| |Lef|Dow|Rig| |Lef|Dow|Up |Rig| |xxx| \ |
  93. * |-----------------------------------------------------------|
  94. * |Shift | | | | | |Hom|PgD|PgU|End| |Shift | |
  95. * |-----------------------------------------------------------|
  96. * | |Gui |Alt |Space |xxxxx| | | | |
  97. * `-----------------------------------------------------------'
  98. */
  99. KEYMAP(KB_GRV, KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8, KB_F9, KB_F10, KB_F11, KB_F12, KB_GRV, \
  100. KB_BSLS,KB_HOME,KB_PGDN,KB_UP, KB_PGUP,KB_END, KB_HOME,KB_PGDN,KB_PGUP,KB_END, KB_NO, KB_NO, KB_NO, \
  101. KB_LCTL,KB_NO, KB_LEFT,KB_DOWN,KB_RGHT,KB_NO, KB_LEFT,KB_DOWN,KB_UP, KB_RGHT,KB_NO, FN_2, KB_BSLS, \
  102. KB_LSFT,KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_NO, KB_RSFT,KB_NO, \
  103. KB_NO, KB_LGUI,KB_LALT,KB_SPC, FN_6, KB_NO, KB_NO, KB_NO, KB_NO),
  104. /* Layer 3: Mouse mode (Semicolon)
  105. * ,-------------------------------------------------------- --.
  106. * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Delete |
  107. * |-----------------------------------------------------------|
  108. * |Tab |MwL|MwU|McU|MwD|MwR|MwL|MwD|MwU|MwR| | | | |
  109. * |-----------------------------------------------------' |
  110. * |Contro|Mb1|Mb2|Mb3| | |McL|McD|McU|McR|xxx| |Return |
  111. * |-----------------------------------------------------------|
  112. * |Shift | | | | | |MwL|MwD|MwU|MwR| |Shift | |
  113. * |-----------------------------------------------------------|
  114. * |xxx|Gui |Alt |Mb1 |Alt | | | | |
  115. * `-----------------------------------------------------------'
  116. * Mc: Mouse Cursor / Mb: Mouse Button / Mw: Mouse Wheel
  117. */
  118. KEYMAP(KB_ESC, KB_F1, KB_F2, KB_F3, KB_F4, KB_F5, KB_F6, KB_F7, KB_F8, KB_F9, KB_F10, KB_F11, KB_F12, KB_DEL, \
  119. KB_TAB, MS_WH_L,MS_WH_U,MS_UP, MS_WH_D,MS_WH_R,MS_WH_L,MS_WH_D,MS_WH_U,MS_WH_R,KB_NO, KB_NO, KB_NO, \
  120. KB_LCTL,KB_NO, MS_LEFT,MS_DOWN,MS_RGHT,KB_NO, MS_LEFT,MS_DOWN,MS_UP, MS_RGHT,FN_3, KB_NO, KB_ENT, \
  121. KB_LSFT,KB_NO, MS_DOWN,KB_NO, KB_NO, KB_NO, MS_BTN2,MS_BTN1,MS_BTN2,MS_BTN3,KB_NO, KB_RSFT,KB_NO, \
  122. FN_7, KB_LGUI,KB_LALT,MS_BTN1,KB_RALT,KB_NO, KB_NO, KB_NO, KB_NO),
  123. /* Layer 4: Matias half keyboard style (Space)
  124. * ,-----------------------------------------------------------.
  125. * |Esc| F1| F2| F3| F4| F5| F6| F7| F8| F9|F10|F11|F12|Delete |
  126. * |-----------------------------------------------------------|
  127. * |Backs| P| O| I| U| Y| T| R| E| W| Q|Tab|Tab| |
  128. * |-----------------------------------------------------' |
  129. * |Contro| ;| L| K| J| H| G| F| D| S| A|Con|Control |
  130. * |-----------------------------------------------------------|
  131. * |Shift | /| .| ,| M| N| B| V| C| X| Z|Shift | |
  132. * |-----------------------------------------------------------|
  133. * | |Gui |Alt |xxxxxxxxxxxxxxxxxxxxxx|Alt | | | | |
  134. * `-----------------------------------------------------------'
  135. */
  136. KEYMAP(KB_MINS,KB_0, KB_9, KB_8, KB_7, KB_6, KB_5, KB_4, KB_3, KB_2, KB_1, KB_NO, KB_NO, KB_ESC, \
  137. KB_BSPC,KB_P, KB_O, KB_I, KB_U, KB_Y, KB_T, KB_R, KB_E, KB_W, KB_Q, KB_TAB, KB_TAB, \
  138. KB_LCTL,KB_SCLN,KB_L, KB_K, KB_J, KB_H, KB_G, KB_F, KB_D, KB_S, KB_A, KB_RCTL,KB_RCTL, \
  139. KB_LSFT,KB_SLSH,KB_DOT, KB_COMM,KB_M, KB_N, KB_B, KB_V, KB_C, KB_X, KB_Z, KB_RSFT,KB_NO, \
  140. KB_NO, KB_LGUI,KB_LALT,FN_4, KB_RALT,KB_NO, KB_NO, KB_NO, KB_NO),
  141. };
  142. uint8_t keymap_get_keycode(int row, int col)
  143. {
  144. return keymap_get_keycodel(current_layer, row, col);
  145. }
  146. uint8_t keymap_get_keycodel(int layer, int row, int col)
  147. {
  148. uint8_t code = KEYCODE(layer, row, col);
  149. // normal key or mouse key
  150. if (IS_KEY(code) || IS_MOUSE(code))
  151. layer_used = true;
  152. return code;
  153. }
  154. inline
  155. int keymap_get_layer(void)
  156. {
  157. return current_layer;
  158. }
  159. inline
  160. int keymap_set_layer(int layer)
  161. {
  162. current_layer = layer;
  163. return current_layer;
  164. }
  165. inline
  166. bool keymap_is_special_mode(int fn_bits)
  167. {
  168. return (keyboard_modifier_keys == (BIT_LCTRL | BIT_LSHIFT | BIT_LALT | BIT_LGUI));
  169. }
  170. void keymap_fn_proc(int fn_bits)
  171. {
  172. // layer switching
  173. static int last_bits = 0;
  174. static uint8_t last_mod = 0;
  175. if (usb_keyboard_has_key() || fn_bits == last_bits) {
  176. // do nothing during press other than Fn key
  177. return;
  178. } else if (fn_bits == 0) {
  179. // send key when Fn key is released without using the layer
  180. if (!layer_used) {
  181. uint8_t code = FN_KEYCODE(biton(last_bits));
  182. if (code != KB_NO) {
  183. if (IS_MOD(code)) {
  184. keyboard_modifier_keys = last_mod | 1<<(code & 0x07);
  185. } else {
  186. keyboard_keys[0] = code;
  187. keyboard_modifier_keys = last_mod;
  188. }
  189. usb_keyboard_send();
  190. usb_keyboard_print();
  191. usb_keyboard_clear();
  192. }
  193. }
  194. last_bits = 0;
  195. last_mod = 0;
  196. layer_used = false;
  197. keymap_set_layer(0); // default layer
  198. } else if ((fn_bits & (fn_bits - 1)) == 0) {
  199. // switch layer when just one Fn Key is pressed
  200. last_bits = fn_bits;
  201. last_mod = keyboard_modifier_keys;
  202. layer_used = false;
  203. keymap_set_layer(FN_LAYER(biton(fn_bits)));
  204. debug("layer: "); phex(current_layer); debug("(");
  205. debug_bin(last_bits); debug(")\n");
  206. debug("last_mod: "); debug_hex(last_mod); debug("\n");
  207. }
  208. }