dynamic_keymap.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /* Copyright 2017 Jason Williams (Wilba)
  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 "keymap.h" // to get keymaps[][][]
  17. #include "eeprom.h"
  18. #include "progmem.h" // to read default from flash
  19. #include "quantum.h" // for send_string()
  20. #include "dynamic_keymap.h"
  21. #ifdef FNV_ENABLE
  22. # include "fnv.h"
  23. #endif
  24. #ifdef VIA_ENABLE
  25. # include "via.h" // for VIA_EEPROM_CONFIG_END
  26. # define DYNAMIC_KEYMAP_EEPROM_START (VIA_EEPROM_CONFIG_END)
  27. #else
  28. # define DYNAMIC_KEYMAP_EEPROM_START (EECONFIG_SIZE)
  29. #endif
  30. #ifdef ENCODER_ENABLE
  31. # include "encoder.h"
  32. #else
  33. # define NUM_ENCODERS 0
  34. #endif
  35. #ifndef DYNAMIC_KEYMAP_LAYER_COUNT
  36. # define DYNAMIC_KEYMAP_LAYER_COUNT 4
  37. #endif
  38. #ifndef DYNAMIC_KEYMAP_MACRO_COUNT
  39. # define DYNAMIC_KEYMAP_MACRO_COUNT 16
  40. #endif
  41. #ifndef TOTAL_EEPROM_BYTE_COUNT
  42. # error Unknown total EEPROM size. Cannot derive maximum for dynamic keymaps.
  43. #endif
  44. #ifndef DYNAMIC_KEYMAP_EEPROM_MAX_ADDR
  45. # define DYNAMIC_KEYMAP_EEPROM_MAX_ADDR (TOTAL_EEPROM_BYTE_COUNT - 1)
  46. #endif
  47. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > (TOTAL_EEPROM_BYTE_COUNT - 1)
  48. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > " STR((TOTAL_EEPROM_BYTE_COUNT - 1))
  49. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR is configured to use more space than what is available for the selected EEPROM driver
  50. #endif
  51. // Due to usage of uint16_t check for max 65535
  52. #if DYNAMIC_KEYMAP_EEPROM_MAX_ADDR > 65535
  53. # pragma message STR(DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) " > 65535"
  54. # error DYNAMIC_KEYMAP_EEPROM_MAX_ADDR must be less than 65536
  55. #endif
  56. // If DYNAMIC_KEYMAP_EEPROM_ADDR not explicitly defined in config.h,
  57. #ifndef DYNAMIC_KEYMAP_EEPROM_ADDR
  58. # define DYNAMIC_KEYMAP_EEPROM_ADDR DYNAMIC_KEYMAP_EEPROM_START
  59. #endif
  60. // Dynamic encoders starts after dynamic keymaps
  61. #ifndef DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR
  62. # define DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR (DYNAMIC_KEYMAP_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2))
  63. #endif
  64. // Dynamic macro starts after dynamic encoders, but only when using ENCODER_MAP
  65. #ifdef ENCODER_MAP_ENABLE
  66. # ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  67. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR + (DYNAMIC_KEYMAP_LAYER_COUNT * NUM_ENCODERS * 2 * 2))
  68. # endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  69. #else // ENCODER_MAP_ENABLE
  70. # ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  71. # define DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR (DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR)
  72. # endif // DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR
  73. #endif // ENCODER_MAP_ENABLE
  74. // Sanity check that dynamic keymaps fit in available EEPROM
  75. // If there's not 100 bytes available for macros, then something is wrong.
  76. // The keyboard should override DYNAMIC_KEYMAP_LAYER_COUNT to reduce it,
  77. // or DYNAMIC_KEYMAP_EEPROM_MAX_ADDR to increase it, *only if* the microcontroller has
  78. // more than the default.
  79. _Static_assert((DYNAMIC_KEYMAP_EEPROM_MAX_ADDR) - (DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR) >= 100, "Dynamic keymaps are configured to use more EEPROM than is available.");
  80. // Dynamic macros are stored after the keymaps and use what is available
  81. // up to and including DYNAMIC_KEYMAP_EEPROM_MAX_ADDR.
  82. #ifndef DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE
  83. # define DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE (DYNAMIC_KEYMAP_EEPROM_MAX_ADDR - DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + 1)
  84. #endif
  85. #ifndef DYNAMIC_KEYMAP_MACRO_DELAY
  86. # define DYNAMIC_KEYMAP_MACRO_DELAY TAP_CODE_DELAY
  87. #endif
  88. uint8_t dynamic_keymap_get_layer_count(void) {
  89. return DYNAMIC_KEYMAP_LAYER_COUNT;
  90. }
  91. void *dynamic_keymap_key_to_eeprom_address(uint8_t layer, uint8_t row, uint8_t column) {
  92. // TODO: optimize this with some left shifts
  93. return ((void *)DYNAMIC_KEYMAP_EEPROM_ADDR) + (layer * MATRIX_ROWS * MATRIX_COLS * 2) + (row * MATRIX_COLS * 2) + (column * 2);
  94. }
  95. uint16_t dynamic_keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t column) {
  96. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return KC_NO;
  97. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  98. // Big endian, so we can read/write EEPROM directly from host if we want
  99. uint16_t keycode = eeprom_read_byte(address) << 8;
  100. keycode |= eeprom_read_byte(address + 1);
  101. return keycode;
  102. }
  103. void dynamic_keymap_set_keycode(uint8_t layer, uint8_t row, uint8_t column, uint16_t keycode) {
  104. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || row >= MATRIX_ROWS || column >= MATRIX_COLS) return;
  105. void *address = dynamic_keymap_key_to_eeprom_address(layer, row, column);
  106. // Big endian, so we can read/write EEPROM directly from host if we want
  107. eeprom_update_byte(address, (uint8_t)(keycode >> 8));
  108. eeprom_update_byte(address + 1, (uint8_t)(keycode & 0xFF));
  109. }
  110. #ifdef ENCODER_MAP_ENABLE
  111. void *dynamic_keymap_encoder_to_eeprom_address(uint8_t layer, uint8_t encoder_id) {
  112. return ((void *)DYNAMIC_KEYMAP_ENCODER_EEPROM_ADDR) + (layer * NUM_ENCODERS * 2 * 2) + (encoder_id * 2 * 2);
  113. }
  114. uint16_t dynamic_keymap_get_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise) {
  115. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return KC_NO;
  116. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  117. // Big endian, so we can read/write EEPROM directly from host if we want
  118. uint16_t keycode = ((uint16_t)eeprom_read_byte(address + (clockwise ? 0 : 2))) << 8;
  119. keycode |= eeprom_read_byte(address + (clockwise ? 0 : 2) + 1);
  120. return keycode;
  121. }
  122. void dynamic_keymap_set_encoder(uint8_t layer, uint8_t encoder_id, bool clockwise, uint16_t keycode) {
  123. if (layer >= DYNAMIC_KEYMAP_LAYER_COUNT || encoder_id >= NUM_ENCODERS) return;
  124. void *address = dynamic_keymap_encoder_to_eeprom_address(layer, encoder_id);
  125. // Big endian, so we can read/write EEPROM directly from host if we want
  126. eeprom_update_byte(address + (clockwise ? 0 : 2), (uint8_t)(keycode >> 8));
  127. eeprom_update_byte(address + (clockwise ? 0 : 2) + 1, (uint8_t)(keycode & 0xFF));
  128. }
  129. #endif // ENCODER_MAP_ENABLE
  130. static uint32_t dynamic_keymap_compute_hash(void) {
  131. #ifdef FNV_ENABLE
  132. Fnv32_t hash = FNV1_32A_INIT;
  133. uint16_t keycode;
  134. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  135. for (int row = 0; row < MATRIX_ROWS; row++) {
  136. for (int column = 0; column < MATRIX_COLS; column++) {
  137. keycode = keycode_at_keymap_location_raw(layer, row, column);
  138. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  139. }
  140. }
  141. # ifdef ENCODER_MAP_ENABLE
  142. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  143. keycode = keycode_at_encodermap_location_raw(layer, encoder, true);
  144. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  145. keycode = keycode_at_encodermap_location_raw(layer, encoder, false);
  146. hash = fnv_32a_buf(&keycode, sizeof(keycode), hash);
  147. }
  148. # endif // ENCODER_MAP_ENABLE
  149. }
  150. return hash;
  151. #else
  152. return 0;
  153. #endif
  154. }
  155. static uint32_t dynamic_keymap_hash(void) {
  156. static uint32_t hash = 0;
  157. static uint8_t s_init = 0;
  158. if (!s_init) {
  159. s_init = 1;
  160. hash = dynamic_keymap_compute_hash();
  161. }
  162. return hash;
  163. }
  164. bool dynamic_keymap_is_valid(void) {
  165. return eeprom_read_dword(EECONFIG_KEYMAP_HASH) == dynamic_keymap_hash();
  166. }
  167. void dynamic_keymap_reset(void) {
  168. // Reset the keymaps in EEPROM to what is in flash.
  169. for (int layer = 0; layer < DYNAMIC_KEYMAP_LAYER_COUNT; layer++) {
  170. for (int row = 0; row < MATRIX_ROWS; row++) {
  171. for (int column = 0; column < MATRIX_COLS; column++) {
  172. if (layer < keymap_layer_count()) {
  173. dynamic_keymap_set_keycode(layer, row, column, keycode_at_keymap_location_raw(layer, row, column));
  174. } else {
  175. dynamic_keymap_set_keycode(layer, row, column, KC_TRANSPARENT);
  176. }
  177. }
  178. }
  179. #ifdef ENCODER_MAP_ENABLE
  180. for (int encoder = 0; encoder < NUM_ENCODERS; encoder++) {
  181. if (layer < encodermap_layer_count()) {
  182. dynamic_keymap_set_encoder(layer, encoder, true, keycode_at_encodermap_location_raw(layer, encoder, true));
  183. dynamic_keymap_set_encoder(layer, encoder, false, keycode_at_encodermap_location_raw(layer, encoder, false));
  184. } else {
  185. dynamic_keymap_set_encoder(layer, encoder, true, KC_TRANSPARENT);
  186. dynamic_keymap_set_encoder(layer, encoder, false, KC_TRANSPARENT);
  187. }
  188. }
  189. #endif // ENCODER_MAP_ENABLE
  190. }
  191. eeprom_update_dword(EECONFIG_KEYMAP_HASH, dynamic_keymap_hash());
  192. }
  193. void dynamic_keymap_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  194. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  195. void * source = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  196. uint8_t *target = data;
  197. for (uint16_t i = 0; i < size; i++) {
  198. if (offset + i < dynamic_keymap_eeprom_size) {
  199. *target = eeprom_read_byte(source);
  200. } else {
  201. *target = 0x00;
  202. }
  203. source++;
  204. target++;
  205. }
  206. }
  207. void dynamic_keymap_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  208. uint16_t dynamic_keymap_eeprom_size = DYNAMIC_KEYMAP_LAYER_COUNT * MATRIX_ROWS * MATRIX_COLS * 2;
  209. void * target = (void *)(DYNAMIC_KEYMAP_EEPROM_ADDR + offset);
  210. uint8_t *source = data;
  211. for (uint16_t i = 0; i < size; i++) {
  212. if (offset + i < dynamic_keymap_eeprom_size) {
  213. eeprom_update_byte(target, *source);
  214. }
  215. source++;
  216. target++;
  217. }
  218. }
  219. uint16_t keycode_at_keymap_location(uint8_t layer_num, uint8_t row, uint8_t column) {
  220. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && row < MATRIX_ROWS && column < MATRIX_COLS) {
  221. return dynamic_keymap_get_keycode(layer_num, row, column);
  222. }
  223. return KC_NO;
  224. }
  225. #ifdef ENCODER_MAP_ENABLE
  226. uint16_t keycode_at_encodermap_location(uint8_t layer_num, uint8_t encoder_idx, bool clockwise) {
  227. if (layer_num < DYNAMIC_KEYMAP_LAYER_COUNT && encoder_idx < NUM_ENCODERS) {
  228. return dynamic_keymap_get_encoder(layer_num, encoder_idx, clockwise);
  229. }
  230. return KC_NO;
  231. }
  232. #endif // ENCODER_MAP_ENABLE
  233. uint8_t dynamic_keymap_macro_get_count(void) {
  234. return DYNAMIC_KEYMAP_MACRO_COUNT;
  235. }
  236. uint16_t dynamic_keymap_macro_get_buffer_size(void) {
  237. return DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE;
  238. }
  239. void dynamic_keymap_macro_get_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  240. void * source = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  241. uint8_t *target = data;
  242. for (uint16_t i = 0; i < size; i++) {
  243. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  244. *target = eeprom_read_byte(source);
  245. } else {
  246. *target = 0x00;
  247. }
  248. source++;
  249. target++;
  250. }
  251. }
  252. void dynamic_keymap_macro_set_buffer(uint16_t offset, uint16_t size, uint8_t *data) {
  253. void * target = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + offset);
  254. uint8_t *source = data;
  255. for (uint16_t i = 0; i < size; i++) {
  256. if (offset + i < DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE) {
  257. eeprom_update_byte(target, *source);
  258. }
  259. source++;
  260. target++;
  261. }
  262. }
  263. void dynamic_keymap_macro_reset(void) {
  264. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  265. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  266. while (p != end) {
  267. eeprom_update_byte(p, 0);
  268. ++p;
  269. }
  270. }
  271. void dynamic_keymap_macro_send(uint8_t id) {
  272. if (id >= DYNAMIC_KEYMAP_MACRO_COUNT) {
  273. return;
  274. }
  275. // Check the last byte of the buffer.
  276. // If it's not zero, then we are in the middle
  277. // of buffer writing, possibly an aborted buffer
  278. // write. So do nothing.
  279. void *p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE - 1);
  280. if (eeprom_read_byte(p) != 0) {
  281. return;
  282. }
  283. // Skip N null characters
  284. // p will then point to the Nth macro
  285. p = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR);
  286. void *end = (void *)(DYNAMIC_KEYMAP_MACRO_EEPROM_ADDR + DYNAMIC_KEYMAP_MACRO_EEPROM_SIZE);
  287. while (id > 0) {
  288. // If we are past the end of the buffer, then the buffer
  289. // contents are garbage, i.e. there were not DYNAMIC_KEYMAP_MACRO_COUNT
  290. // nulls in the buffer.
  291. if (p == end) {
  292. return;
  293. }
  294. if (eeprom_read_byte(p) == 0) {
  295. --id;
  296. }
  297. ++p;
  298. }
  299. // Send the macro string one or three chars at a time
  300. // by making temporary 1 or 3 char strings
  301. char data[4] = {0, 0, 0, 0};
  302. // We already checked there was a null at the end of
  303. // the buffer, so this cannot go past the end
  304. while (1) {
  305. data[0] = eeprom_read_byte(p++);
  306. data[1] = 0;
  307. // Stop at the null terminator of this macro string
  308. if (data[0] == 0) {
  309. break;
  310. }
  311. // If the char is magic (tap, down, up),
  312. // add the next char (key to use) and send a 3 char string.
  313. if (data[0] == SS_TAP_CODE || data[0] == SS_DOWN_CODE || data[0] == SS_UP_CODE) {
  314. data[1] = data[0];
  315. data[0] = SS_QMK_PREFIX;
  316. data[2] = eeprom_read_byte(p++);
  317. if (data[2] == 0) {
  318. break;
  319. }
  320. }
  321. send_string_with_delay(data, DYNAMIC_KEYMAP_MACRO_DELAY);
  322. }
  323. }