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

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