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

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