dynamic_keymap.c 14 KB

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