upload
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

keymap.c 5.7KB

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