You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
5.5 KiB

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