bootmagic_full.c 5.0 KB

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