bootmagic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdint.h>
  2. #include <stdbool.h>
  3. #include "wait.h"
  4. #include "matrix.h"
  5. #include "bootloader.h"
  6. #include "debug.h"
  7. #include "keymap.h"
  8. #include "host.h"
  9. #include "action_layer.h"
  10. #include "eeconfig.h"
  11. #include "bootmagic.h"
  12. keymap_config_t keymap_config;
  13. /** \brief Bootmagic
  14. *
  15. * FIXME: needs doc
  16. */
  17. void bootmagic(void) {
  18. /* check signature */
  19. if (!eeconfig_is_enabled()) {
  20. eeconfig_init();
  21. }
  22. /* do scans in case of bounce */
  23. print("bootmagic scan: ... ");
  24. uint8_t scan = 100;
  25. while (scan--) {
  26. matrix_scan();
  27. wait_ms(10);
  28. }
  29. print("done.\n");
  30. /* bootmagic skip */
  31. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SKIP)) {
  32. return;
  33. }
  34. /* eeconfig clear */
  35. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EEPROM_CLEAR)) {
  36. eeconfig_init();
  37. }
  38. /* bootloader */
  39. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_BOOTLOADER)) {
  40. bootloader_jump();
  41. }
  42. /* debug enable */
  43. debug_config.raw = eeconfig_read_debug();
  44. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_ENABLE)) {
  45. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MATRIX)) {
  46. debug_config.matrix = !debug_config.matrix;
  47. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_KEYBOARD)) {
  48. debug_config.keyboard = !debug_config.keyboard;
  49. } else if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEBUG_MOUSE)) {
  50. debug_config.mouse = !debug_config.mouse;
  51. } else {
  52. debug_config.enable = !debug_config.enable;
  53. }
  54. }
  55. eeconfig_update_debug(debug_config.raw);
  56. /* keymap config */
  57. keymap_config.raw = eeconfig_read_keymap();
  58. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_CONTROL_CAPSLOCK)) {
  59. keymap_config.swap_control_capslock = !keymap_config.swap_control_capslock;
  60. }
  61. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_CAPSLOCK_TO_CONTROL)) {
  62. keymap_config.capslock_to_control = !keymap_config.capslock_to_control;
  63. }
  64. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_LALT_LGUI)) {
  65. keymap_config.swap_lalt_lgui = !keymap_config.swap_lalt_lgui;
  66. }
  67. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_RALT_RGUI)) {
  68. keymap_config.swap_ralt_rgui = !keymap_config.swap_ralt_rgui;
  69. }
  70. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_NO_GUI)) {
  71. keymap_config.no_gui = !keymap_config.no_gui;
  72. }
  73. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_GRAVE_ESC)) {
  74. keymap_config.swap_grave_esc = !keymap_config.swap_grave_esc;
  75. }
  76. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_SWAP_BACKSLASH_BACKSPACE)) {
  77. keymap_config.swap_backslash_backspace = !keymap_config.swap_backslash_backspace;
  78. }
  79. if (bootmagic_scan_keycode(BOOTMAGIC_HOST_NKRO)) {
  80. keymap_config.nkro = !keymap_config.nkro;
  81. }
  82. eeconfig_update_keymap(keymap_config.raw);
  83. /* default layer */
  84. uint8_t default_layer = 0;
  85. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_0)) {
  86. default_layer |= (1 << 0);
  87. }
  88. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_1)) {
  89. default_layer |= (1 << 1);
  90. }
  91. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_2)) {
  92. default_layer |= (1 << 2);
  93. }
  94. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_3)) {
  95. default_layer |= (1 << 3);
  96. }
  97. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_4)) {
  98. default_layer |= (1 << 4);
  99. }
  100. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_5)) {
  101. default_layer |= (1 << 5);
  102. }
  103. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_6)) {
  104. default_layer |= (1 << 6);
  105. }
  106. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_DEFAULT_LAYER_7)) {
  107. default_layer |= (1 << 7);
  108. }
  109. if (default_layer) {
  110. eeconfig_update_default_layer(default_layer);
  111. default_layer_set((layer_state_t)default_layer);
  112. } else {
  113. default_layer = eeconfig_read_default_layer();
  114. default_layer_set((layer_state_t)default_layer);
  115. }
  116. /* EE_HANDS handedness */
  117. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_LEFT)) {
  118. eeconfig_update_handedness(true);
  119. }
  120. if (bootmagic_scan_keycode(BOOTMAGIC_KEY_EE_HANDS_RIGHT)) {
  121. eeconfig_update_handedness(false);
  122. }
  123. }
  124. /** \brief Scan Keycode
  125. *
  126. * FIXME: needs doc
  127. */
  128. static bool scan_keycode(uint8_t keycode) {
  129. for (uint8_t r = 0; r < MATRIX_ROWS; r++) {
  130. matrix_row_t matrix_row = matrix_get_row(r);
  131. for (uint8_t c = 0; c < MATRIX_COLS; c++) {
  132. if (matrix_row & ((matrix_row_t)1 << c)) {
  133. if (keycode == keymap_key_to_keycode(0, (keypos_t){.row = r, .col = c})) {
  134. return true;
  135. }
  136. }
  137. }
  138. }
  139. return false;
  140. }
  141. /** \brief Bootmagic Scan Keycode
  142. *
  143. * FIXME: needs doc
  144. */
  145. bool bootmagic_scan_keycode(uint8_t keycode) {
  146. if (!scan_keycode(BOOTMAGIC_KEY_SALT)) return false;
  147. return scan_keycode(keycode);
  148. }