Keyboard firmwares for Atmel AVR and Cortex-M
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.

keymap.c 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. Copyright 2013 Jun Wako <[email protected]>
  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. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include "keymap.h"
  15. #include "report.h"
  16. #include "keycode.h"
  17. #include "action_layer.h"
  18. #include "action.h"
  19. #include "action_macro.h"
  20. #include "wait.h"
  21. #include "debug.h"
  22. #include "bootloader.h"
  23. #ifdef BOOTMAGIC_ENABLE
  24. extern keymap_config_t keymap_config;
  25. #endif
  26. static action_t keycode_to_action(uint8_t keycode);
  27. /* converts key to action */
  28. action_t action_for_key(uint8_t layer, keypos_t key)
  29. {
  30. uint8_t keycode = keymap_key_to_keycode(layer, key);
  31. switch (keycode) {
  32. case KC_FN0 ... KC_FN31:
  33. return keymap_fn_to_action(keycode);
  34. #ifdef BOOTMAGIC_ENABLE
  35. case KC_CAPSLOCK:
  36. case KC_LOCKING_CAPS:
  37. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  38. return keycode_to_action(KC_LCTL);
  39. }
  40. return keycode_to_action(keycode);
  41. case KC_LCTL:
  42. if (keymap_config.swap_control_capslock) {
  43. return keycode_to_action(KC_CAPSLOCK);
  44. }
  45. return keycode_to_action(KC_LCTL);
  46. case KC_LALT:
  47. if (keymap_config.swap_lalt_lgui) {
  48. if (keymap_config.no_gui) {
  49. return keycode_to_action(ACTION_NO);
  50. }
  51. return keycode_to_action(KC_LGUI);
  52. }
  53. return keycode_to_action(KC_LALT);
  54. case KC_LGUI:
  55. if (keymap_config.swap_lalt_lgui) {
  56. return keycode_to_action(KC_LALT);
  57. }
  58. if (keymap_config.no_gui) {
  59. return keycode_to_action(ACTION_NO);
  60. }
  61. return keycode_to_action(KC_LGUI);
  62. case KC_RALT:
  63. if (keymap_config.swap_ralt_rgui) {
  64. if (keymap_config.no_gui) {
  65. return keycode_to_action(ACTION_NO);
  66. }
  67. return keycode_to_action(KC_RGUI);
  68. }
  69. return keycode_to_action(KC_RALT);
  70. case KC_RGUI:
  71. if (keymap_config.swap_ralt_rgui) {
  72. return keycode_to_action(KC_RALT);
  73. }
  74. if (keymap_config.no_gui) {
  75. return keycode_to_action(ACTION_NO);
  76. }
  77. return keycode_to_action(KC_RGUI);
  78. case KC_GRAVE:
  79. if (keymap_config.swap_grave_esc) {
  80. return keycode_to_action(KC_ESC);
  81. }
  82. return keycode_to_action(KC_GRAVE);
  83. case KC_ESC:
  84. if (keymap_config.swap_grave_esc) {
  85. return keycode_to_action(KC_GRAVE);
  86. }
  87. return keycode_to_action(KC_ESC);
  88. case KC_BSLASH:
  89. if (keymap_config.swap_backslash_backspace) {
  90. return keycode_to_action(KC_BSPACE);
  91. }
  92. return keycode_to_action(KC_BSLASH);
  93. case KC_BSPACE:
  94. if (keymap_config.swap_backslash_backspace) {
  95. return keycode_to_action(KC_BSLASH);
  96. }
  97. return keycode_to_action(KC_BSPACE);
  98. #endif
  99. default:
  100. return keycode_to_action(keycode);
  101. }
  102. }
  103. /* Macro */
  104. __attribute__ ((weak))
  105. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  106. {
  107. (void)record;
  108. (void)id;
  109. (void)opt;
  110. return MACRO_NONE;
  111. }
  112. /* Function */
  113. __attribute__ ((weak))
  114. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  115. {
  116. (void)record;
  117. (void)id;
  118. (void)opt;
  119. }
  120. /* translates keycode to action */
  121. static action_t keycode_to_action(uint8_t keycode)
  122. {
  123. action_t action = {};
  124. switch (keycode) {
  125. case KC_A ... KC_EXSEL:
  126. case KC_LCTRL ... KC_RGUI:
  127. action.code = ACTION_KEY(keycode);
  128. break;
  129. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  130. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  131. break;
  132. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  133. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  134. break;
  135. case KC_MS_UP ... KC_MS_ACCEL2:
  136. action.code = ACTION_MOUSEKEY(keycode);
  137. break;
  138. case KC_TRNS:
  139. action.code = ACTION_TRANSPARENT;
  140. break;
  141. case KC_BOOTLOADER:
  142. clear_keyboard();
  143. wait_ms(50);
  144. bootloader_jump(); // not return
  145. break;
  146. default:
  147. action.code = ACTION_NO;
  148. break;
  149. }
  150. return action;
  151. }
  152. #ifdef USE_LEGACY_KEYMAP
  153. /*
  154. * Legacy keymap support
  155. * Consider using new keymap API instead.
  156. */
  157. __attribute__ ((weak))
  158. uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  159. {
  160. return keymap_get_keycode(layer, key.row, key.col);
  161. }
  162. /* Legacy keymap support */
  163. __attribute__ ((weak))
  164. action_t keymap_fn_to_action(uint8_t keycode)
  165. {
  166. action_t action = { .code = ACTION_NO };
  167. switch (keycode) {
  168. case KC_FN0 ... KC_FN31:
  169. {
  170. uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
  171. uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
  172. if (key) {
  173. action.code = ACTION_LAYER_TAP_KEY(layer, key);
  174. } else {
  175. action.code = ACTION_LAYER_MOMENTARY(layer);
  176. }
  177. }
  178. return action;
  179. default:
  180. return action;
  181. }
  182. }
  183. #endif