keycode_config.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Copyright 2016 Jack Humbert
  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 "keycode_config.h"
  17. keymap_config_t keymap_config;
  18. /** \brief keycode_config
  19. *
  20. * This function is used to check a specific keycode against the bootmagic config,
  21. * and will return the corrected keycode, when appropriate.
  22. */
  23. __attribute__((weak)) uint16_t keycode_config(uint16_t keycode) {
  24. #ifdef MAGIC_ENABLE
  25. switch (keycode) {
  26. case KC_CAPS_LOCK:
  27. case KC_LOCKING_CAPS_LOCK:
  28. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  29. return KC_LEFT_CTRL;
  30. } else if (keymap_config.swap_escape_capslock) {
  31. return KC_ESCAPE;
  32. }
  33. return keycode;
  34. case KC_LEFT_CTRL:
  35. if (keymap_config.swap_control_capslock) {
  36. return KC_CAPS_LOCK;
  37. }
  38. if (keymap_config.swap_lctl_lgui) {
  39. if (keymap_config.no_gui) {
  40. return KC_NO;
  41. }
  42. return KC_LEFT_GUI;
  43. }
  44. return KC_LEFT_CTRL;
  45. case KC_LEFT_ALT:
  46. if (keymap_config.swap_lalt_lgui) {
  47. if (keymap_config.no_gui) {
  48. return KC_NO;
  49. }
  50. return KC_LEFT_GUI;
  51. }
  52. return KC_LEFT_ALT;
  53. case KC_LEFT_GUI:
  54. if (keymap_config.swap_lalt_lgui) {
  55. return KC_LEFT_ALT;
  56. }
  57. if (keymap_config.swap_lctl_lgui) {
  58. return KC_LEFT_CTRL;
  59. }
  60. if (keymap_config.no_gui) {
  61. return KC_NO;
  62. }
  63. return KC_LEFT_GUI;
  64. case KC_RIGHT_CTRL:
  65. if (keymap_config.swap_rctl_rgui) {
  66. if (keymap_config.no_gui) {
  67. return KC_NO;
  68. }
  69. return KC_RIGHT_GUI;
  70. }
  71. return KC_RIGHT_CTRL;
  72. case KC_RIGHT_ALT:
  73. if (keymap_config.swap_ralt_rgui) {
  74. if (keymap_config.no_gui) {
  75. return KC_NO;
  76. }
  77. return KC_RIGHT_GUI;
  78. }
  79. return KC_RIGHT_ALT;
  80. case KC_RIGHT_GUI:
  81. if (keymap_config.swap_ralt_rgui) {
  82. return KC_RIGHT_ALT;
  83. }
  84. if (keymap_config.swap_rctl_rgui) {
  85. return KC_RIGHT_CTRL;
  86. }
  87. if (keymap_config.no_gui) {
  88. return KC_NO;
  89. }
  90. return KC_RIGHT_GUI;
  91. case KC_GRAVE:
  92. if (keymap_config.swap_grave_esc) {
  93. return KC_ESCAPE;
  94. }
  95. return KC_GRAVE;
  96. case KC_ESCAPE:
  97. if (keymap_config.swap_grave_esc) {
  98. return KC_GRAVE;
  99. } else if (keymap_config.swap_escape_capslock) {
  100. return KC_CAPS_LOCK;
  101. }
  102. return KC_ESCAPE;
  103. case KC_BACKSLASH:
  104. if (keymap_config.swap_backslash_backspace) {
  105. return KC_BACKSPACE;
  106. }
  107. return KC_BACKSLASH;
  108. case KC_BACKSPACE:
  109. if (keymap_config.swap_backslash_backspace) {
  110. return KC_BACKSLASH;
  111. }
  112. return KC_BACKSPACE;
  113. default:
  114. return keycode;
  115. }
  116. #else
  117. return keycode;
  118. #endif // MAGIC_ENABLE
  119. }
  120. /** \brief mod_config
  121. *
  122. * This function checks the mods passed to it against the bootmagic config,
  123. * and will remove or replace mods, based on that.
  124. */
  125. __attribute__((weak)) uint8_t mod_config(uint8_t mod) {
  126. #ifdef MAGIC_ENABLE
  127. /**
  128. * Note: This function is for the 5-bit packed mods, NOT the full 8-bit mods.
  129. * More info about the mods can be seen in modifiers.h.
  130. */
  131. if (keymap_config.swap_lalt_lgui) {
  132. /** If both modifiers pressed or neither pressed, do nothing
  133. * Otherwise swap the values
  134. * Note: The left mods are ANDed with the right-hand values to check
  135. * if they were pressed with the right hand bit set
  136. */
  137. if (((mod & MOD_RALT) == MOD_LALT) ^ ((mod & MOD_RGUI) == MOD_LGUI)) {
  138. mod ^= (MOD_LALT | MOD_LGUI);
  139. }
  140. }
  141. if (keymap_config.swap_ralt_rgui) {
  142. if (((mod & MOD_RALT) == MOD_RALT) ^ ((mod & MOD_RGUI) == MOD_RGUI)) {
  143. /* lefthand values to preserve the right hand bit */
  144. mod ^= (MOD_LALT | MOD_LGUI);
  145. }
  146. }
  147. if (keymap_config.swap_lctl_lgui) {
  148. /* left mods ANDed with right-hand values to check for right hand bit */
  149. if (((mod & MOD_RCTL) == MOD_LCTL) ^ ((mod & MOD_RGUI) == MOD_LGUI)) {
  150. mod ^= (MOD_LCTL | MOD_LGUI);
  151. }
  152. }
  153. if (keymap_config.swap_rctl_rgui) {
  154. if (((mod & MOD_RCTL) == MOD_RCTL) ^ ((mod & MOD_RGUI) == MOD_RGUI)) {
  155. /* lefthand values to preserve the right hand bit */
  156. mod ^= (MOD_LCTL | MOD_LGUI);
  157. }
  158. }
  159. if (keymap_config.no_gui) {
  160. if (mod & MOD_LGUI) {
  161. mod &= ~MOD_RGUI;
  162. }
  163. }
  164. #endif // MAGIC_ENABLE
  165. return mod;
  166. }