dynamic_keymap.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright 2017 Jason Williams (Wilba)
  2. * Copyright 2024-2025 Nick Brassel (@tzarc)
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "dynamic_keymap.h"
  18. #include "keymap_introspection.h"
  19. #include "action.h"
  20. #include "send_string.h"
  21. #include "keycodes.h"
  22. #include "nvm_eeconfig.h"
  23. #include "nvm_dynamic_keymap.h"
  24. #ifdef FNV_ENABLE
  25. # include "fnv.h"
  26. #endif
  27. #ifdef ENCODER_ENABLE
  28. # include "encoder.h"
  29. #else
  30. # define NUM_ENCODERS 0
  31. #endif
  32. #ifndef DYNAMIC_KEYMAP_MACRO_DELAY
  33. # define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY
  34. #endif
  35. uint8_t dynamic_keymap_get_layer_count(void) {
  36. return DYNAMIC_KEYMAP_LAYER_COUNT;
  37. }
  38. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  39. return nvm_dynamic_keymap_read_keycode(layer, row, column);
  40. }
  41. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  42. nvm_dynamic_keymap_update_keycode(layer, row, column, keycode);
  43. }
  44. #ifdef ENCODER_MAP_ENABLE
  45. uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
  46. return nvm_dynamic_keymap_read_encoder(layer, encoder_id, clockwise);
  47. }
  48. void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
  49. nvm_dynamic_keymap_update_encoder(layer, encoder_id, clockwise, keycode);
  50. }
  51. #endif // ENCODER_MAP_ENABLE
  52. static uint32_t dynamic_keymap_compute_hash(void) {
  53. #ifdef FNV_ENABLE
  54. Fnv32_t hash = FNV1_32A_INIT;
  55. uint16_t keycode;
  56. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  57. for (int row = 0; row < MATRIX_ROWS; row++) {
  58. for (int column = 0; column < MATRIX_COLS; column++) {
  59. keycode = keycode_at_keymap_location_raw(layer, row, column);
  60. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  61. }
  62. }
  63. # ifdef ENCODER_MAP_ENABLE
  64. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  65. keycode = keycode_at_encodermap_location_raw(layer, encoder, true);
  66. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  67. keycode = keycode_at_encodermap_location_raw(layer, encoder, false);
  68. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  69. }
  70. # endif // ENCODER_MAP_ENABLE
  71. }
  72. return hash;
  73. #else
  74. return 0;
  75. #endif
  76. }
  77. static uint32_t dynamic_keymap_hash(void) {
  78. static uint32_t hash = 0;
  79. static uint8_t s_init = 0;
  80. if (!s_init) {
  81. s_init = 1;
  82. hash = dynamic_keymap_compute_hash();
  83. }
  84. return hash;
  85. }
  86. bool dynamic_keymap_is_valid(void) {
  87. return nvm_eeconfig_read_keymap_hash() == dynamic_keymap_hash();
  88. }
  89. void dynamic_keymap_reset(void) {
  90. // Erase the keymaps, if necessary.
  91. nvm_dynamic_keymap_erase();
  92. // Reset the keymaps in EEPROM to what is in flash.
  93. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  94. for (int row = 0; row < MATRIX_ROWS; row++) {
  95. for (int column = 0; column < MATRIX_COLS; column++) {
  96. dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column));
  97. }
  98. }
  99. #ifdef ENCODER_MAP_ENABLE
  100. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  101. dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true));
  102. dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false));
  103. }
  104. #endif // ENCODER_MAP_ENABLE
  105. }
  106. nvm_eeconfig_update_keymap_hash(dynamic_keymap_hash());
  107. }
  108. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  109. nvm_dynamic_keymap_read_buffer(offset, size, data);
  110. }
  111. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  112. nvm_dynamic_keymap_update_buffer(offset, size, data);
  113. }
  114. uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  115. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && row < MATRIX_ROWS && column < MATRIX_COLS) {
  116. return dynamic_keymap_get_keycode(layer_num, row, column);
  117. }
  118. return KC_NO;
  119. }
  120. #ifdef ENCODER_MAP_ENABLE
  121. uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  122. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && encoder_idx < NUM_ENCODERS) {
  123. return dynamic_keymap_get_encoder(layer_num, encoder_idx, clockwise);
  124. }
  125. return KC_NO;
  126. }
  127. #endif // ENCODER_MAP_ENABLE
  128. uint8_t dynamic_keymap_macro_get_count(void) {
  129. return DYNAMIC_KEYMAP_MACRO_COUNT;
  130. }
  131. uint16_t dynamic_keymap_macro_get_buffer_size(void) {
  132. return (uint16_t)nvm_dynamic_keymap_macro_size();
  133. }
  134. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  135. nvm_dynamic_keymap_macro_read_buffer(offset, size, data);
  136. }
  137. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  138. nvm_dynamic_keymap_macro_update_buffer(offset, size, data);
  139. }
  140. static uint8_t dynamic_keymap_read_byte(uint32_t offset) {
  141. uint8_t d;
  142. nvm_dynamic_keymap_macro_read_buffer(offset, 1, &d);
  143. return d;
  144. }
  145. typedef struct send_string_nvm_state_t {
  146. uint32_t offset;
  147. } send_string_nvm_state_t;
  148. char send_string_get_next_nvm(void *arg) {
  149. send_string_nvm_state_t *state = (send_string_nvm_state_t *)arg;
  150. char ret = dynamic_keymap_read_byte(state->offset);
  151. state->offset++;
  152. return ret;
  153. }
  154. void dynamic_keymap_macro_reset(void) {
  155. // Erase the macros, if necessary.
  156. nvm_dynamic_keymap_macro_erase();
  157. nvm_dynamic_keymap_macro_reset();
  158. }
  159. void dynamic_keymap_macro_send(uint8_t id) {
  160. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  161. return;
  162. }
  163. // Check the last byte of the buffer.
  164. // If it's not zero, then we are in the middle
  165. // of buffer writing, possibly an aborted buffer
  166. // write. So do nothing.
  167. if (dynamic_keymap_read_byte(nvm_dynamic_keymap_macro_size() - 1) != 0) {
  168. return;
  169. }
  170. // Skip N null characters
  171. // p will then point to the Nth macro
  172. uint32_t offset = 0;
  173. uint32_t end = nvm_dynamic_keymap_macro_size();
  174. while (id > 0) {
  175. // If we are past the end of the buffer, then there is
  176. // no Nth macro in the buffer.
  177. if (offset == end) {
  178. return;
  179. }
  180. if (dynamic_keymap_read_byte(offset) == 0) {
  181. --id;
  182. }
  183. ++offset;
  184. }
  185. send_string_nvm_state_t state = {.offset = offset};
  186. send_string_with_delay_impl(send_string_get_next_nvm, &state, DYNAMIC_KEYMAP_MACRO_DELAY);
  187. }