keymap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright 2022 spx01 (@spx01)
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include QMK_KEYBOARD_H
  17. #include "print.h"
  18. enum CustomKeycodes {
  19. CK_HEXRGB = SAFE_RANGE,
  20. /* esc when shift is held, grave otherwise; particularly useful for windows' task manager shortcut */
  21. CK_ESCG,
  22. };
  23. enum Layers {
  24. _LAYER1,
  25. _LAYER2,
  26. };
  27. const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  28. // clang-format off
  29. [_LAYER1] = LAYOUT_65_ansi_blocker(
  30. QK_GESC, 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, KC_HOME,
  31. 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, KC_PGUP,
  32. LT(_LAYER2, 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, KC_PGDN,
  33. KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, KC_UP, KC_END,
  34. KC_LCTL, KC_LGUI, KC_LALT, KC_SPC, KC_RALT, MO(1), KC_LEFT, KC_DOWN, KC_RIGHT),
  35. [_LAYER2] = LAYOUT_65_ansi_blocker(
  36. CK_ESCG, 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, KC_INS,
  37. KC_TRNS, KC_TRNS, KC_TRNS, EE_CLR, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_PSCR, KC_TRNS, KC_TRNS, QK_BOOT, KC_TRNS,
  38. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  39. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_MUTE, KC_TRNS, KC_TRNS, CK_HEXRGB, KC_MPLY, KC_VOLU, KC_TRNS,
  40. KC_TRNS, KC_TRNS, KC_TRNS, RGB_TOG, KC_TRNS, KC_TRNS, KC_MPRV, KC_VOLD, KC_MNXT),
  41. /* [] = LAYOUT_65_ansi_blocker(
  42. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  43. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  44. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  45. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS,
  46. KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS, KC_TRNS), */
  47. // clang-format on
  48. };
  49. /* exported by the rgb_static effect */
  50. void RGB_STATIC_save_eeprom(void);
  51. void RGB_STATIC_reset(void);
  52. extern uint8_t g_rgb_static_color[3];
  53. /* returns number corresponding to hex digit represented by keycode or -1 if keycode isn't a valid hex digit */
  54. static int8_t key_hexdigit(uint16_t keycode) {
  55. if (keycode >= KC_A && keycode <= KC_F) {
  56. return keycode - KC_A + 10;
  57. }
  58. if (keycode >= KC_1 && keycode <= KC_0) {
  59. int8_t res = keycode - KC_1 + 1;
  60. /* mod 10 considering res is between 1 and 10 */
  61. res *= res != 10;
  62. return res;
  63. }
  64. return -1;
  65. }
  66. typedef struct {
  67. bool active;
  68. uint8_t color[3];
  69. uint8_t count;
  70. } RGBHexState;
  71. static RGBHexState hexrgb;
  72. /* handles input mode for an rgb value */
  73. static void hexrgb_input(uint16_t keycode) {
  74. /* only check for special keys when input mode has just been triggered */
  75. if (hexrgb.count == 0) {
  76. switch (keycode) {
  77. case HEXRGB_SAVE_KC:
  78. RGB_STATIC_save_eeprom();
  79. hexrgb.active = false;
  80. return;
  81. case HEXRGB_RESET_KC:
  82. RGB_STATIC_reset();
  83. hexrgb.active = false;
  84. return;
  85. default:
  86. }
  87. }
  88. int8_t digit = key_hexdigit(keycode);
  89. /* exit input mode if an invalid key has been pressed */
  90. if (digit == -1) {
  91. hexrgb.count = 0;
  92. hexrgb.active = false;
  93. return;
  94. }
  95. /* append digit to current color */
  96. uint8_t idx = hexrgb.count / 2;
  97. hexrgb.color[idx] <<= 4;
  98. hexrgb.color[idx] |= digit;
  99. ++hexrgb.count;
  100. /* done with input */
  101. if (hexrgb.count == 6) {
  102. hexrgb.active = false;
  103. hexrgb.count = 0;
  104. /* copy color to rgb_static's buffer */
  105. for (int8_t i = 0; i < 3; ++i) {
  106. g_rgb_static_color[i] = hexrgb.color[i];
  107. hexrgb.color[i] = 0;
  108. }
  109. }
  110. }
  111. void keyboard_post_init_user(void) {
  112. #if CUSTOM_DEBUG
  113. debug_enable = true;
  114. debug_matrix = true;
  115. #endif
  116. }
  117. bool process_record_user(uint16_t keycode, keyrecord_t *record) {
  118. /* shift status last time ck_escg was pressed so that the correct key can be released */
  119. static bool ck_escg_last_shifted;
  120. if (hexrgb.active && record->event.pressed) {
  121. hexrgb_input(keycode);
  122. /* while in input mode nothing passes through */
  123. return false;
  124. }
  125. switch (keycode) {
  126. case CK_HEXRGB:
  127. hexrgb.active = record->event.pressed;
  128. return false;
  129. case CK_ESCG:
  130. /* if pressed, inject key, otherwise delete it */
  131. if (record->event.pressed) {
  132. bool shifted = get_mods() & MOD_BIT(KC_LSFT);
  133. add_key(shifted ? KC_ESC : KC_GRV);
  134. ck_escg_last_shifted = shifted;
  135. } else {
  136. del_key(ck_escg_last_shifted ? KC_ESC : KC_GRV);
  137. }
  138. send_keyboard_report();
  139. return false;
  140. default:
  141. }
  142. return true;
  143. }