keymap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /* Copyright 2022 Forrest Cahoon <forrest.cahoon@gmail.com>
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>.
  12. */
  13. // Milliseconds the backlight is off (or on) when blinking to show
  14. // caps lock is on. In other words, half the time of a full blink cycle.
  15. #define CAPS_LOCK_BLINK_MS 350
  16. #include QMK_KEYBOARD_H
  17. enum layer {
  18. _QWERTY,
  19. _PG, // for "programming"
  20. _MS, // for "mouse"
  21. };
  22. enum custom_keycodes {
  23. PG_ARRO = SAFE_RANGE,
  24. PG_BANG,
  25. PG_ENDC,
  26. PG_EQEQ,
  27. PG_EQL,
  28. PG_LBRK,
  29. PG_LPAR,
  30. PG_MINS,
  31. PG_NEEQ,
  32. PG_RBRK,
  33. PG_RPAR,
  34. PG_SLAS,
  35. PG_VBAR,
  36. MS_BTN,
  37. };
  38. // clang-format off
  39. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  40. [_QWERTY] = LAYOUT(
  41. // ┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐
  42. MS_BTN, KC_ESC, KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC,
  43. // ├────────┼───┬────┴────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤
  44. MO(_PG), KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS,
  45. // ├────────┼───┼─────────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┼────────┴────────┤
  46. MO(_MS), KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT,
  47. // ├────────┼───┴─────────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┴────┬────────┬───┘
  48. TG(_PG), KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, TG(_MS),
  49. // ├────────┼───┬──────────┬──┴──────┬─┴────────┼────────┼────────┼────────┤ ├────────┴─┬──────┴──────┬─┴────────┼────────┴─┬─────────┬─┴────────┤
  50. TG(_MS), KC_LGUI, KC_LCTL, KC_LALT, MO(_PG), KC_SPC, KC_SPC, KC_SPC, KC_SPC, MO(_PG), KC_RALT, KC_RCTL, KC_RGUI
  51. // └────────┴───┴──────────┴─────────┴──────────┴────────┴────────┴────────┘ └──────────┴─────────────┴──────────┴──────────┴─────────┴──────────┘
  52. ),
  53. [_PG] = LAYOUT(
  54. // ┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐
  55. KC_MUTE, QK_BOOT, _______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_DEL,
  56. // ├────────┼───┬────┴────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤
  57. _______, _______, _______, PG_SLAS, PG_LPAR, PG_RPAR, PG_NEEQ, _______, KC_HOME, KC_UP, KC_PGUP, KC_INS, _______, _______, _______,
  58. // ├────────┼───┼─────────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┼────────┴────────┤
  59. _______, _______, PG_ENDC, PG_VBAR, PG_LBRK, PG_RBRK, PG_EQEQ, _______, KC_LEFT, KC_DOWN, KC_RGHT, _______, _______, KC_BSPC,
  60. // ├────────┼───┴─────────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┴────┬────────┬───┘
  61. _______, _______, _______, PG_MINS, PG_EQL, PG_BANG, PG_ARRO, _______, KC_END, KC_DOWN, KC_PGDN, KC_DEL, _______, _______,
  62. // ├────────┼───┬──────────┬──┴──────┬─┴────────┼────────┼────────┼────────┤ ├────────┴─┬──────┴──────┬─┴────────┼────────┴─┬─────────┬─┴────────┤
  63. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  64. // └────────┴───┴──────────┴─────────┴──────────┴────────┴────────┴────────┘ └──────────┴─────────────┴──────────┴──────────┴─────────┴──────────┘
  65. ),
  66. [_MS] = LAYOUT(
  67. // ┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐ ┌────────┬────────┬────────┬────────┬────────┬────────┬────────┬────────┐
  68. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______,
  69. // ├────────┼───┬────┴────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┼────────┼────────┤
  70. _______, _______, _______, _______, _______, _______, _______, _______, KC_BTN1, KC_MS_U, KC_BTN2, _______, _______, _______, _______,
  71. // ├────────┼───┼─────────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┼────────┴────────┤
  72. _______, _______, _______, _______, _______, _______, _______, _______, KC_MS_L, KC_MS_D, KC_MS_R, KC_BTN3, _______, _______,
  73. // ├────────┼───┴─────────────┼────────┼────────┼────────┼────────┼────────┤ ├────────┼────────┼────────┼────────┼────────┼────────┴────┬────────┬───┘
  74. _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_MS_D, _______, _______, _______, _______,
  75. // ├────────┼───┬──────────┬──┴──────┬─┴────────┼────────┼────────┼────────┤ ├────────┴─┬──────┴──────┬─┴────────┼────────┴─┬─────────┬─┴────────┤
  76. _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______
  77. // └────────┴───┴──────────┴─────────┴──────────┴────────┴────────┴────────┘ └──────────┴─────────────┴──────────┴──────────┴─────────┴──────────┘
  78. ),
  79. };
  80. // clang-format on
  81. const key_override_t pg_lbrk_none = ko_make_with_layers_and_negmods(0, PG_LBRK, KC_LEFT_CURLY_BRACE, ~0, ~0);
  82. const key_override_t pg_lbrk_shift = ko_make_with_layers_and_negmods(MOD_MASK_SHIFT, PG_LBRK, KC_LEFT_BRACKET, ~0, ~MOD_MASK_SHIFT);
  83. const key_override_t pg_lpar_none = ko_make_with_layers_and_negmods(0, PG_LPAR, KC_LEFT_PAREN, ~0, ~0);
  84. const key_override_t pg_lpar_shift = ko_make_with_layers_and_negmods(MOD_MASK_SHIFT, PG_LPAR, KC_LEFT_ANGLE_BRACKET, ~0, ~MOD_MASK_SHIFT);
  85. const key_override_t pg_rbrk_none = ko_make_with_layers_and_negmods(0, PG_RBRK, KC_RIGHT_CURLY_BRACE, ~0, ~0);
  86. const key_override_t pg_rbrk_shift = ko_make_with_layers_and_negmods(MOD_MASK_SHIFT, PG_RBRK, KC_RIGHT_BRACKET, ~0, ~MOD_MASK_SHIFT);
  87. const key_override_t pg_rpar_none = ko_make_with_layers_and_negmods(0, PG_RPAR, KC_RIGHT_PAREN, ~0, ~0);
  88. const key_override_t pg_rpar_shift = ko_make_with_layers_and_negmods(MOD_MASK_SHIFT, PG_RPAR, KC_RIGHT_ANGLE_BRACKET, ~0, ~MOD_MASK_SHIFT);
  89. const key_override_t pg_slas_none = ko_make_with_layers_and_negmods(0, PG_SLAS, KC_SLASH, ~0, ~0);
  90. const key_override_t pg_slas_shift = ko_make_with_layers_and_negmods(MOD_MASK_SHIFT, PG_SLAS, KC_BACKSLASH, ~0, ~MOD_MASK_SHIFT);
  91. const key_override_t ms_btn_none = ko_make_with_layers_and_negmods(0, MS_BTN, KC_MS_BTN1, ~0, ~0);
  92. const key_override_t ms_btn_shift = ko_make_with_layers_and_negmods(MOD_MASK_SHIFT, MS_BTN, KC_MS_BTN2, ~0, ~MOD_MASK_SHIFT);
  93. // clang-format off
  94. const key_override_t **key_overrides = (const key_override_t *[]){
  95. &pg_lbrk_none, &pg_lbrk_shift,
  96. &pg_lpar_none, &pg_lpar_shift,
  97. &pg_rbrk_none, &pg_rbrk_shift,
  98. &pg_rpar_none, &pg_rpar_shift,
  99. &pg_slas_none, &pg_slas_shift,
  100. &ms_btn_none, &ms_btn_shift,
  101. NULL};
  102. // clang-format on
  103. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  104. uint8_t std_mods = get_mods();
  105. uint8_t oneshot_mods = get_oneshot_mods();
  106. uint8_t all_mods = std_mods | oneshot_mods;
  107. bool shift_on = all_mods & MOD_MASK_SHIFT;
  108. bool ctrl_on = all_mods & MOD_MASK_CTRL;
  109. // None of our special keycodes use ALT or GUI mods (so far), so we
  110. // check once here at the top and let other code handle it if any of
  111. // these are set.
  112. if (all_mods & (MOD_MASK_ALT | MOD_MASK_GUI)) return true;
  113. switch (keycode) {
  114. case KC_CAPS:
  115. // On caps lock release, if caps lock just got turned off, ensure that
  116. // backlight is enabled when it stops blinking.
  117. if (!record->event.pressed) {
  118. if (!host_keyboard_led_state().caps_lock) {
  119. backlight_enable();
  120. // In this case, we want to continue processing
  121. return true;
  122. }
  123. }
  124. break;
  125. case PG_ARRO:
  126. if (record->event.pressed) {
  127. clear_mods();
  128. clear_oneshot_mods();
  129. if (shift_on) {
  130. if (!ctrl_on) SEND_STRING(" ");
  131. SEND_STRING("=>");
  132. if (!ctrl_on) SEND_STRING(" ");
  133. } else {
  134. if (ctrl_on) SEND_STRING(" ");
  135. SEND_STRING("->");
  136. if (ctrl_on) SEND_STRING(" ");
  137. }
  138. set_mods(std_mods);
  139. set_oneshot_mods(oneshot_mods);
  140. return false;
  141. }
  142. break;
  143. case PG_BANG:
  144. if (record->event.pressed) {
  145. clear_mods();
  146. clear_oneshot_mods();
  147. if (shift_on) {
  148. if (!ctrl_on) SEND_STRING(" ");
  149. SEND_STRING("*");
  150. if (!ctrl_on) SEND_STRING(" ");
  151. } else {
  152. if (ctrl_on) SEND_STRING(" ");
  153. SEND_STRING("!");
  154. if (!ctrl_on) SEND_STRING(" ");
  155. }
  156. set_mods(std_mods);
  157. set_oneshot_mods(oneshot_mods);
  158. return false;
  159. }
  160. break;
  161. case PG_ENDC:
  162. if (record->event.pressed) {
  163. clear_mods();
  164. clear_oneshot_mods();
  165. SEND_STRING(SS_TAP(X_END));
  166. if (!shift_on) SEND_STRING(";");
  167. if (!ctrl_on) SEND_STRING(SS_TAP(X_ENTER));
  168. set_mods(std_mods);
  169. set_oneshot_mods(oneshot_mods);
  170. return false;
  171. }
  172. break;
  173. case PG_EQEQ:
  174. if (record->event.pressed) {
  175. clear_mods();
  176. clear_oneshot_mods();
  177. if (!ctrl_on) SEND_STRING(" ");
  178. shift_on ? SEND_STRING("==") : SEND_STRING("===");
  179. if (!ctrl_on) SEND_STRING(" ");
  180. set_mods(std_mods);
  181. set_oneshot_mods(oneshot_mods);
  182. return false;
  183. }
  184. break;
  185. case PG_EQL:
  186. if (record->event.pressed) {
  187. clear_mods();
  188. clear_oneshot_mods();
  189. if (!ctrl_on) SEND_STRING(" ");
  190. shift_on ? SEND_STRING("+") : SEND_STRING("=");
  191. if (!ctrl_on) SEND_STRING(" ");
  192. set_mods(std_mods);
  193. set_oneshot_mods(oneshot_mods);
  194. return false;
  195. }
  196. break;
  197. case PG_MINS:
  198. if (record->event.pressed) {
  199. clear_mods();
  200. clear_oneshot_mods();
  201. if (shift_on) {
  202. if (ctrl_on) SEND_STRING(" ");
  203. SEND_STRING("_");
  204. if (ctrl_on) SEND_STRING(" ");
  205. } else {
  206. if (!ctrl_on) SEND_STRING(" ");
  207. SEND_STRING("-");
  208. if (!ctrl_on) SEND_STRING(" ");
  209. }
  210. set_mods(std_mods);
  211. set_oneshot_mods(oneshot_mods);
  212. return false;
  213. }
  214. break;
  215. case PG_NEEQ:
  216. if (record->event.pressed) {
  217. clear_mods();
  218. clear_oneshot_mods();
  219. if (!ctrl_on) SEND_STRING(" ");
  220. shift_on ? SEND_STRING("!=") : SEND_STRING("!==");
  221. if (!ctrl_on) SEND_STRING(" ");
  222. set_mods(std_mods);
  223. set_oneshot_mods(oneshot_mods);
  224. return false;
  225. }
  226. break;
  227. case PG_VBAR:
  228. if (record->event.pressed) {
  229. clear_mods();
  230. clear_oneshot_mods();
  231. if (!ctrl_on) SEND_STRING(" ");
  232. shift_on ? SEND_STRING("&") : SEND_STRING("|");
  233. if (!ctrl_on) SEND_STRING(" ");
  234. set_mods(std_mods);
  235. set_oneshot_mods(oneshot_mods);
  236. return false;
  237. }
  238. break;
  239. }
  240. return true;
  241. }
  242. // I have only one encoder on my FoldKB and I want it to always be a scroll wheel.
  243. bool encoder_update_user(uint8_t index, bool clockwise) {
  244. if (clockwise) {
  245. tap_code(KC_MS_WH_DOWN);
  246. } else {
  247. tap_code(KC_MS_WH_UP);
  248. }
  249. return false;
  250. }
  251. static uint16_t caps_lock_blink_timer = 0;
  252. // This is called after every matrix scan. We make the backlight blink
  253. // when caps lock is on here. To ensure the backlight is in the on state
  254. // when caps lock is turned off, we also watch for the relase of the caps
  255. // lock key in process_record_user().
  256. void housekeeping_task_user(void) {
  257. if (host_keyboard_led_state().caps_lock) {
  258. if (timer_elapsed(caps_lock_blink_timer) >= CAPS_LOCK_BLINK_MS) {
  259. backlight_toggle();
  260. caps_lock_blink_timer = timer_read();
  261. }
  262. }
  263. }
  264. // The backlight level shows which layer we are in:
  265. // Mouse layer: dim | QWERTY layer: medium | Programming layer: bright
  266. layer_state_t layer_state_set_user(layer_state_t state) {
  267. switch (get_highest_layer(state)) {
  268. case _MS:
  269. backlight_level(1);
  270. break;
  271. case _PG:
  272. backlight_level(3);
  273. break;
  274. default:
  275. backlight_level(2);
  276. break;
  277. }
  278. return state;
  279. }
  280. void keyboard_post_init(void) {
  281. backlight_level(2);
  282. }