keymap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. // An Ergodox EZ keymap meant to be used with a bépo layout (FR ergonomic
  2. // layout, dvorak style). The overall design is slightly inspired by the
  3. // TypeMatrix keyboard. Switching between a TypeMatrix and an Ergodox with this
  4. // layout should be relatively easy.
  5. //
  6. // See the README.md file for an image of this keymap.
  7. #include QMK_KEYBOARD_H
  8. #include "keymap_bepo.h"
  9. // The layers that we are defining for this keyboards.
  10. #define BASE 0
  11. #define FN 1
  12. #define MOUSE 2
  13. #define NUMS 3
  14. #define SWAP 4
  15. #define SYSLEDS 5
  16. // The Tap Dance identifiers, used in the TD keycode and tap_dance_actions array.
  17. #define TAP_MACRO 0
  18. // A 'transparent' key code (that falls back to the layers below it).
  19. #define ___ KC_TRANSPARENT
  20. // A 'blocking' key code. Does nothing but prevent falling back to another layer.
  21. #define XXX KC_NO
  22. // Some combined keys (one normal keycode when tapped and one modifier or layer
  23. // toggle when held).
  24. #define ESC_FN LT(FN, KC_ESC) // ESC key and FN layer toggle.
  25. #define M_RSFT MT(MOD_RSFT, BP_M) // 'M' key and right shift modifier.
  26. #define W_RCTL MT(MOD_RCTL, BP_W) // 'W' key and right control modifier.
  27. #define SPC_RALT MT(MOD_RALT, KC_SPC) // SPACE key and right alt modifier.
  28. #define PERC_FN LT(FN, BP_PERC) // '%' key and FN layer toggle.
  29. // The most portable copy/paste keys (windows (mostly), linux, and some terminal emulators).
  30. #define MK_CUT LSFT(KC_DEL) // shift + delete
  31. #define MK_COPY LCTL(KC_INS) // ctrl + insert
  32. #define MK_PASTE LSFT(KC_INS) // shift + insert
  33. // Custom keycodes
  34. enum {
  35. // SAFE_RANGE must be used to tag the first element of the enum.
  36. // DYNAMIC_MACRO_RANGE must always be the last element of the enum if other
  37. // values are added (as its value is used to create a couple of other keycodes
  38. // after it).
  39. DYNAMIC_MACRO_RANGE = SAFE_RANGE,
  40. };
  41. // This file must be included after DYNAMIC_MACRO_RANGE is defined...
  42. #include "dynamic_macro.h"
  43. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  44. // Layer 0: basic keys.
  45. [BASE] = LAYOUT_ergodox(
  46. /* left hand */
  47. BP_DLR, BP_DQUO, BP_LDAQ, BP_RDAQ, BP_LPRN, BP_RPRN, KC_DEL,
  48. KC_TAB, BP_B, BP_EACU, BP_P, BP_O, BP_EGRV, KC_BSPC,
  49. KC_LSFT, BP_A, BP_U, BP_I, BP_E, BP_COMM,
  50. KC_LCTRL, BP_AGRV, BP_Y, BP_X, BP_DOT, BP_K, KC_ENT,
  51. ESC_FN, BP_ECIR, KC_LGUI, KC_LALT, SPC_RALT,
  52. TT(SWAP), KC_MNXT,
  53. KC_MPLY,
  54. TT(FN), TT(NUMS), KC_MPRV,
  55. /* right hand */
  56. KC_DEL, BP_AT, BP_PLUS, BP_MINS, BP_SLSH, BP_ASTR, BP_EQL,
  57. KC_BSPC, BP_DCIR, BP_V, BP_D, BP_L, BP_J, BP_Z,
  58. BP_C, BP_T, BP_S, BP_R, BP_N, M_RSFT,
  59. KC_ENT, BP_QUOT, BP_Q, BP_G, BP_H, BP_F, W_RCTL,
  60. SPC_RALT, KC_LALT, TT(SYSLEDS), BP_CCED, PERC_FN,
  61. KC_LEFT, KC_RIGHT,
  62. KC_UP,
  63. KC_DOWN, TD(TAP_MACRO), TT(MOUSE)),
  64. // Layer 1: function and media keys.
  65. [FN] = LAYOUT_ergodox(
  66. /* left hand */
  67. KC_SLEP, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, ___,
  68. ___, ___, ___, ___, ___, ___, ___,
  69. ___, ___, ___, ___, ___, KC_LSFT,
  70. ___, ___, MK_CUT, MK_COPY, MK_PASTE, KC_LCTRL, ___,
  71. ___, ___, ___, ___, ___,
  72. ___, KC_VOLU,
  73. KC_VOLD,
  74. ___, ___, KC_MUTE,
  75. /* right hand */
  76. ___, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11,
  77. ___, ___, KC_HOME, KC_UP, KC_END, KC_PGUP, KC_F12,
  78. ___, KC_LEFT, KC_DOWN, KC_RIGHT, KC_PGDN, ___,
  79. ___, ___, ___, ___, ___, ___, ___,
  80. ___, ___, ___, ___, ___,
  81. KC_HOME, KC_END,
  82. KC_PGUP,
  83. KC_PGDN, ___, ___),
  84. // Note that any change to the FN layer above must be added to
  85. // the MOUSE layer below (except for the arrow keys).
  86. // Layer 2: Mouse control.
  87. [MOUSE] = LAYOUT_ergodox(
  88. /* left hand */
  89. KC_SLEP, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, ___,
  90. ___, ___, KC_BTN4, KC_MS_U, KC_BTN5, ___, ___,
  91. ___, ___, KC_MS_L, KC_MS_D, KC_MS_R, KC_LSFT,
  92. ___, ___, MK_CUT, MK_COPY, MK_PASTE, KC_LCTRL, ___,
  93. ___, ___, ___, ___, ___,
  94. ___, KC_VOLU,
  95. KC_VOLD,
  96. ___, ___, KC_MUTE,
  97. /* right hand */
  98. ___, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11,
  99. ___, ___, XXX, KC_WH_U, XXX, XXX, KC_F12,
  100. ___, KC_WH_L, KC_WH_D, KC_WH_R, XXX, ___,
  101. ___, ___, KC_ACL0, KC_ACL1, KC_ACL2, ___, ___,
  102. KC_BTN1, KC_BTN2, KC_BTN3, ___, ___,
  103. ___, ___,
  104. ___,
  105. ___, ___, ___),
  106. // Layer 3: Numeric keypad and system keys.
  107. [NUMS] = LAYOUT_ergodox(
  108. /* left hand */
  109. KC_PSCR, KC_INS, KC_PAUS, ___, ___, ___, ___,
  110. ___, ___, ___, ___, ___, ___, ___,
  111. ___, ___, ___, ___, ___, ___,
  112. ___, ___, MK_CUT, MK_COPY, MK_PASTE, ___, ___,
  113. ___, ___, ___, ___, ___,
  114. ___, ___,
  115. ___,
  116. ___, ___, ___,
  117. /* right hand */
  118. ___, ___, ___, ___, ___, ___, KC_NLCK,
  119. ___, KC_PEQL, KC_P7, KC_P8, KC_P9, KC_PMNS, KC_SLCK,
  120. KC_PCMM, KC_P4, KC_P5, KC_P6, KC_PPLS, ___,
  121. KC_PENT, KC_P0, KC_P1, KC_P2, KC_P3, KC_PAST, ___,
  122. ___, ___, ___, KC_PSLS, ___,
  123. ___, ___,
  124. ___,
  125. ___, ___, ___),
  126. // Layer 4: hand swap, all keys are mirrored to the other side of the keyboard
  127. // except for the layer toggle itself (so there is no right arrow when this
  128. // layer is activated).
  129. [SWAP] = LAYOUT_ergodox(
  130. /* left hand */
  131. ___, ___, ___, ___, ___, ___, ___,
  132. ___, ___, ___, ___, ___, ___, ___,
  133. ___, ___, ___, ___, ___, ___,
  134. ___, ___, ___, ___, ___, ___, ___,
  135. ___, ___, ___, ___, ___,
  136. TT(SWAP), ___,
  137. ___,
  138. ___, ___, ___,
  139. /* right hand */
  140. ___, ___, ___, ___, ___, ___, ___,
  141. ___, ___, ___, ___, ___, ___, ___,
  142. ___, ___, ___, ___, ___, ___,
  143. ___, ___, ___, ___, ___, ___, ___,
  144. ___, ___, ___, ___, ___,
  145. ___, TT(SWAP),
  146. ___,
  147. ___, ___, ___),
  148. // Layer 5: The LEDs are showing the "standard" caps/num/scroll lock indicator
  149. // instead of their default which shows the currently active layers (FN, NUMS,
  150. // and MOUSE in that order).
  151. [SYSLEDS] = LAYOUT_ergodox(
  152. /* left hand */
  153. ___, ___, ___, ___, ___, ___, ___,
  154. ___, ___, ___, ___, ___, ___, ___,
  155. ___, ___, ___, ___, ___, ___,
  156. ___, ___, ___, ___, ___, ___, ___,
  157. ___, ___, ___, ___, ___,
  158. ___, ___,
  159. ___,
  160. ___, ___, ___,
  161. /* right hand */
  162. ___, ___, ___, ___, ___, ___, ___,
  163. ___, ___, ___, ___, ___, ___, ___,
  164. ___, ___, ___, ___, ___, ___,
  165. ___, ___, ___, ___, ___, ___, ___,
  166. ___, ___, TT(SYSLEDS), ___, ___,
  167. ___, ___,
  168. ___,
  169. ___, ___, ___),
  170. };
  171. // Whether the macro 1 is currently being recorded.
  172. static bool is_macro1_recording = false;
  173. // The current set of active layers (as a bitmask).
  174. // There is a global 'layer_state' variable but it is set after the call
  175. // to layer_state_set_user().
  176. static uint32_t current_layer_state = 0;
  177. uint32_t layer_state_set_user(uint32_t state);
  178. // Method called at the end of the tap dance on the TAP_MACRO key. That key is
  179. // used to start recording a macro (double tap or more), to stop recording (any
  180. // number of tap), or to play the recorded macro (1 tap).
  181. void macro_tapdance_fn(qk_tap_dance_state_t *state, void *user_data) {
  182. uint16_t keycode;
  183. keyrecord_t record;
  184. dprintf("macro_tap_dance_fn %d\n", state->count);
  185. if (is_macro1_recording) {
  186. keycode = DYN_REC_STOP;
  187. is_macro1_recording = false;
  188. layer_state_set_user(current_layer_state);
  189. } else if (state->count == 1) {
  190. keycode = DYN_MACRO_PLAY1;
  191. } else {
  192. keycode = DYN_REC_START1;
  193. is_macro1_recording = true;
  194. layer_state_set_user(current_layer_state);
  195. }
  196. record.event.pressed = true;
  197. process_record_dynamic_macro(keycode, &record);
  198. record.event.pressed = false;
  199. process_record_dynamic_macro(keycode, &record);
  200. }
  201. // The definition of the tap dance actions:
  202. qk_tap_dance_action_t tap_dance_actions[] = {
  203. // This Tap dance plays the macro 1 on TAP and records it on double tap.
  204. [TAP_MACRO] = ACTION_TAP_DANCE_FN(macro_tapdance_fn),
  205. };
  206. // Runs for each key down or up event.
  207. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  208. if (keycode != TD(TAP_MACRO)) {
  209. // That key is processed by the macro_tapdance_fn. Not ignoring it here is
  210. // mostly a no-op except that it is recorded in the macros (and uses space).
  211. // We can't just return false when the key is a tap dance, because
  212. // process_record_user, is called before the tap dance processing (and
  213. // returning false would eat the tap dance).
  214. if (!process_record_dynamic_macro(keycode, record)) {
  215. return false;
  216. }
  217. }
  218. return true; // Let QMK send the enter press/release events
  219. }
  220. // Runs just one time when the keyboard initializes.
  221. void matrix_init_user(void) {
  222. ergodox_right_led_1_off();
  223. ergodox_right_led_2_off();
  224. ergodox_right_led_3_off();
  225. };
  226. // Runs constantly in the background, in a loop.
  227. void matrix_scan_user(void) {
  228. };
  229. // The state of the LEDs requested by the system, as a bitmask.
  230. static uint8_t sys_led_state = 0;
  231. // Use these masks to read the system LEDs state.
  232. static const uint8_t sys_led_mask_num_lock = 1 << USB_LED_NUM_LOCK;
  233. static const uint8_t sys_led_mask_caps_lock = 1 << USB_LED_CAPS_LOCK;
  234. static const uint8_t sys_led_mask_scroll_lock = 1 << USB_LED_SCROLL_LOCK;
  235. // Value to use to switch LEDs on. The default value of 255 is far too bright.
  236. static const uint8_t max_led_value = 20;
  237. // Whether the given layer (one of the constant defined at the top) is active.
  238. #define LAYER_ON(layer) (current_layer_state & (1<<layer))
  239. void led_1_on(void) {
  240. ergodox_right_led_1_on();
  241. ergodox_right_led_1_set(max_led_value);
  242. }
  243. void led_2_on(void) {
  244. ergodox_right_led_2_on();
  245. ergodox_right_led_2_set(max_led_value);
  246. }
  247. void led_3_on(void) {
  248. ergodox_right_led_3_on();
  249. ergodox_right_led_3_set(max_led_value);
  250. }
  251. void led_1_off(void) {
  252. ergodox_right_led_1_off();
  253. }
  254. void led_2_off(void) {
  255. ergodox_right_led_2_off();
  256. }
  257. void led_3_off(void) {
  258. ergodox_right_led_3_off();
  259. }
  260. // Called when the computer wants to change the state of the keyboard LEDs.
  261. void led_set_user(uint8_t usb_led) {
  262. sys_led_state = usb_led;
  263. if (LAYER_ON(SYSLEDS)) {
  264. if (sys_led_state & sys_led_mask_caps_lock) {
  265. led_1_on();
  266. } else {
  267. led_1_off();
  268. }
  269. if (sys_led_state & sys_led_mask_num_lock) {
  270. led_2_on();
  271. } else {
  272. led_2_off();
  273. }
  274. if (sys_led_state & sys_led_mask_scroll_lock) {
  275. led_3_on();
  276. } else {
  277. led_3_off();
  278. }
  279. }
  280. }
  281. uint32_t layer_state_set_user(uint32_t state) {
  282. current_layer_state = state;
  283. swap_hands = LAYER_ON(SWAP);
  284. if (is_macro1_recording) {
  285. led_1_on();
  286. led_2_on();
  287. led_3_on();
  288. return state;
  289. }
  290. if (LAYER_ON(SYSLEDS)) {
  291. led_set_user(sys_led_state);
  292. return state;
  293. }
  294. if (LAYER_ON(FN)) {
  295. led_1_on();
  296. } else {
  297. led_1_off();
  298. }
  299. if (LAYER_ON(NUMS)) {
  300. led_2_on();
  301. } else {
  302. led_2_off();
  303. }
  304. if (LAYER_ON(MOUSE)) {
  305. led_3_on();
  306. } else {
  307. led_3_off();
  308. }
  309. return state;
  310. };