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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. static action_t keycode_to_action(uint8_t keycode);
  24. /* converts key to action */
  25. action_t action_for_key(uint8_t layer, keypos_t key)
  26. {
  27. uint8_t keycode = keymap_key_to_keycode(layer, key);
  28. switch (keycode) {
  29. case KC_FN0 ... KC_FN31:
  30. return keymap_fn_to_action(keycode);
  31. #ifdef BOOTMAGIC_ENABLE
  32. case KC_CAPSLOCK:
  33. case KC_LOCKING_CAPS:
  34. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  35. return keycode_to_action(KC_LCTL);
  36. }
  37. return keycode_to_action(keycode);
  38. case KC_LCTL:
  39. if (keymap_config.swap_control_capslock) {
  40. return keycode_to_action(KC_CAPSLOCK);
  41. }
  42. return keycode_to_action(KC_LCTL);
  43. case KC_LALT:
  44. if (keymap_config.swap_lalt_lgui) {
  45. if (keymap_config.no_gui) {
  46. return keycode_to_action(ACTION_NO);
  47. }
  48. return keycode_to_action(KC_LGUI);
  49. }
  50. return keycode_to_action(KC_LALT);
  51. case KC_LGUI:
  52. if (keymap_config.swap_lalt_lgui) {
  53. return keycode_to_action(KC_LALT);
  54. }
  55. if (keymap_config.no_gui) {
  56. return keycode_to_action(ACTION_NO);
  57. }
  58. return keycode_to_action(KC_LGUI);
  59. case KC_RALT:
  60. if (keymap_config.swap_ralt_rgui) {
  61. if (keymap_config.no_gui) {
  62. return keycode_to_action(ACTION_NO);
  63. }
  64. return keycode_to_action(KC_RGUI);
  65. }
  66. return keycode_to_action(KC_RALT);
  67. case KC_RGUI:
  68. if (keymap_config.swap_ralt_rgui) {
  69. return keycode_to_action(KC_RALT);
  70. }
  71. if (keymap_config.no_gui) {
  72. return keycode_to_action(ACTION_NO);
  73. }
  74. return keycode_to_action(KC_RGUI);
  75. case KC_GRAVE:
  76. if (keymap_config.swap_grave_esc) {
  77. return keycode_to_action(KC_ESC);
  78. }
  79. return keycode_to_action(KC_GRAVE);
  80. case KC_ESC:
  81. if (keymap_config.swap_grave_esc) {
  82. return keycode_to_action(KC_GRAVE);
  83. }
  84. return keycode_to_action(KC_ESC);
  85. case KC_BSLASH:
  86. if (keymap_config.swap_backslash_backspace) {
  87. return keycode_to_action(KC_BSPACE);
  88. }
  89. return keycode_to_action(KC_BSLASH);
  90. case KC_BSPACE:
  91. if (keymap_config.swap_backslash_backspace) {
  92. return keycode_to_action(KC_BSLASH);
  93. }
  94. return keycode_to_action(KC_BSPACE);
  95. #endif
  96. default:
  97. return keycode_to_action(keycode);
  98. }
  99. }
  100. /* Macro */
  101. __attribute__ ((weak))
  102. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  103. {
  104. return MACRO_NONE;
  105. }
  106. /* Function */
  107. __attribute__ ((weak))
  108. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  109. {
  110. }
  111. /* translates keycode to action */
  112. static action_t keycode_to_action(uint8_t keycode)
  113. {
  114. action_t action = {};
  115. switch (keycode) {
  116. case KC_A ... KC_EXSEL:
  117. case KC_LCTRL ... KC_RGUI:
  118. action.code = ACTION_KEY(keycode);
  119. break;
  120. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  121. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  122. break;
  123. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  124. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  125. break;
  126. case KC_MS_UP ... KC_MS_ACCEL2:
  127. action.code = ACTION_MOUSEKEY(keycode);
  128. break;
  129. case KC_TRNS:
  130. action.code = ACTION_TRANSPARENT;
  131. break;
  132. case KC_BOOTLOADER:
  133. clear_keyboard();
  134. wait_ms(50);
  135. bootloader_jump(); // not return
  136. break;
  137. default:
  138. action.code = ACTION_NO;
  139. break;
  140. }
  141. return action;
  142. }
  143. #ifdef USE_LEGACY_KEYMAP
  144. /*
  145. * Legacy keymap support
  146. * Consider using new keymap API instead.
  147. */
  148. __attribute__ ((weak))
  149. uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  150. {
  151. return keymap_get_keycode(layer, key.row, key.col);
  152. }
  153. /* Legacy keymap support */
  154. __attribute__ ((weak))
  155. action_t keymap_fn_to_action(uint8_t keycode)
  156. {
  157. action_t action = { .code = ACTION_NO };
  158. switch (keycode) {
  159. case KC_FN0 ... KC_FN31:
  160. {
  161. uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
  162. uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
  163. if (key) {
  164. action.code = ACTION_LAYER_TAP_KEY(layer, key);
  165. } else {
  166. action.code = ACTION_LAYER_MOMENTARY(layer);
  167. }
  168. }
  169. return action;
  170. default:
  171. return action;
  172. }
  173. }
  174. #endif