dynamic_keymap.c 14 KB

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