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

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. return MACRO_NONE;
  103. }
  104. /* Function */
  105. __attribute__ ((weak))
  106. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  107. {
  108. }
  109. /* translates keycode to action */
  110. static action_t keycode_to_action(uint8_t keycode)
  111. {
  112. action_t action;
  113. switch (keycode) {
  114. case KC_A ... KC_EXSEL:
  115. case KC_LCTRL ... KC_RGUI:
  116. action.code = ACTION_KEY(keycode);
  117. break;
  118. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  119. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  120. break;
  121. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  122. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  123. break;
  124. case KC_MS_UP ... KC_MS_ACCEL2:
  125. action.code = ACTION_MOUSEKEY(keycode);
  126. break;
  127. case KC_TRNS:
  128. action.code = ACTION_TRANSPARENT;
  129. break;
  130. default:
  131. action.code = ACTION_NO;
  132. break;
  133. }
  134. return action;
  135. }
  136. #ifdef USE_LEGACY_KEYMAP
  137. /*
  138. * Legacy keymap support
  139. * Consider using new keymap API instead.
  140. */
  141. __attribute__ ((weak))
  142. uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  143. {
  144. return keymap_get_keycode(layer, key.row, key.col);
  145. }
  146. /* Legacy keymap support */
  147. __attribute__ ((weak))
  148. action_t keymap_fn_to_action(uint8_t keycode)
  149. {
  150. action_t action = { .code = ACTION_NO };
  151. switch (keycode) {
  152. case KC_FN0 ... KC_FN31:
  153. {
  154. uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
  155. uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
  156. if (key) {
  157. action.code = ACTION_LAYER_TAP_KEY(layer, key);
  158. } else {
  159. action.code = ACTION_LAYER_MOMENTARY(layer);
  160. }
  161. }
  162. return action;
  163. default:
  164. return action;
  165. }
  166. }
  167. #endif