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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright 2013,2016 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. #if defined(__AVR__)
  24. #include <avr/pgmspace.h>
  25. #endif
  26. #ifdef BOOTMAGIC_ENABLE
  27. extern keymap_config_t keymap_config;
  28. #endif
  29. static action_t keycode_to_action(uint8_t keycode);
  30. /* converts key to action */
  31. __attribute__ ((weak))
  32. action_t action_for_key(uint8_t layer, keypos_t key)
  33. {
  34. uint8_t keycode = keymap_key_to_keycode(layer, key);
  35. switch (keycode) {
  36. case KC_FN0 ... KC_FN31:
  37. return keymap_fn_to_action(keycode);
  38. #ifdef BOOTMAGIC_ENABLE
  39. case KC_CAPSLOCK:
  40. case KC_LOCKING_CAPS:
  41. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  42. return keycode_to_action(KC_LCTL);
  43. }
  44. return keycode_to_action(keycode);
  45. case KC_LCTL:
  46. if (keymap_config.swap_control_capslock) {
  47. return keycode_to_action(KC_CAPSLOCK);
  48. }
  49. return keycode_to_action(KC_LCTL);
  50. case KC_LALT:
  51. if (keymap_config.swap_lalt_lgui) {
  52. if (keymap_config.no_gui) {
  53. return keycode_to_action(KC_NO);
  54. }
  55. return keycode_to_action(KC_LGUI);
  56. }
  57. return keycode_to_action(KC_LALT);
  58. case KC_LGUI:
  59. if (keymap_config.swap_lalt_lgui) {
  60. return keycode_to_action(KC_LALT);
  61. }
  62. if (keymap_config.no_gui) {
  63. return keycode_to_action(KC_NO);
  64. }
  65. return keycode_to_action(KC_LGUI);
  66. case KC_RALT:
  67. if (keymap_config.swap_ralt_rgui) {
  68. if (keymap_config.no_gui) {
  69. return keycode_to_action(KC_NO);
  70. }
  71. return keycode_to_action(KC_RGUI);
  72. }
  73. return keycode_to_action(KC_RALT);
  74. case KC_RGUI:
  75. if (keymap_config.swap_ralt_rgui) {
  76. return keycode_to_action(KC_RALT);
  77. }
  78. if (keymap_config.no_gui) {
  79. return keycode_to_action(KC_NO);
  80. }
  81. return keycode_to_action(KC_RGUI);
  82. case KC_GRAVE:
  83. if (keymap_config.swap_grave_esc) {
  84. return keycode_to_action(KC_ESC);
  85. }
  86. return keycode_to_action(KC_GRAVE);
  87. case KC_ESC:
  88. if (keymap_config.swap_grave_esc) {
  89. return keycode_to_action(KC_GRAVE);
  90. }
  91. return keycode_to_action(KC_ESC);
  92. case KC_BSLASH:
  93. if (keymap_config.swap_backslash_backspace) {
  94. return keycode_to_action(KC_BSPACE);
  95. }
  96. return keycode_to_action(KC_BSLASH);
  97. case KC_BSPACE:
  98. if (keymap_config.swap_backslash_backspace) {
  99. return keycode_to_action(KC_BSLASH);
  100. }
  101. return keycode_to_action(KC_BSPACE);
  102. #endif
  103. default:
  104. return keycode_to_action(keycode);
  105. }
  106. }
  107. /* Macro */
  108. __attribute__ ((weak))
  109. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  110. {
  111. (void)record;
  112. (void)id;
  113. (void)opt;
  114. return MACRO_NONE;
  115. }
  116. /* Function */
  117. __attribute__ ((weak))
  118. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  119. {
  120. (void)record;
  121. (void)id;
  122. (void)opt;
  123. }
  124. /* translates keycode to action */
  125. static action_t keycode_to_action(uint8_t keycode)
  126. {
  127. switch (keycode) {
  128. case KC_A ... KC_EXSEL:
  129. case KC_LCTRL ... KC_RGUI:
  130. return (action_t)ACTION_KEY(keycode);
  131. break;
  132. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  133. return (action_t)ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  134. break;
  135. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  136. return (action_t)ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  137. break;
  138. case KC_MS_UP ... KC_MS_ACCEL2:
  139. return (action_t)ACTION_MOUSEKEY(keycode);
  140. break;
  141. case KC_TRNS:
  142. return (action_t)ACTION_TRANSPARENT;
  143. break;
  144. case KC_BOOTLOADER:
  145. clear_keyboard();
  146. wait_ms(50);
  147. bootloader_jump(); // not return
  148. break;
  149. default:
  150. return (action_t)ACTION_NO;
  151. break;
  152. }
  153. return (action_t)ACTION_NO;
  154. }
  155. #ifdef USE_LEGACY_KEYMAP
  156. /*
  157. * Legacy keymap support
  158. * Consider using new keymap API instead.
  159. */
  160. extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  161. extern const uint8_t fn_layer[];
  162. extern const uint8_t fn_keycode[];
  163. __attribute__ ((weak))
  164. uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col)
  165. {
  166. return pgm_read_byte(&keymaps[(layer)][(row)][(col)]);
  167. }
  168. __attribute__ ((weak))
  169. uint8_t keymap_fn_layer(uint8_t index)
  170. {
  171. return pgm_read_byte(&fn_layer[index]);
  172. }
  173. __attribute__ ((weak))
  174. uint8_t keymap_fn_keycode(uint8_t index)
  175. {
  176. return pgm_read_byte(&fn_keycode[index]);
  177. }
  178. __attribute__ ((weak))
  179. uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  180. {
  181. return keymap_get_keycode(layer, key.row, key.col);
  182. }
  183. /* Legacy keymap support */
  184. __attribute__ ((weak))
  185. action_t keymap_fn_to_action(uint8_t keycode)
  186. {
  187. switch (keycode) {
  188. case KC_FN0 ... KC_FN31:
  189. {
  190. uint8_t layer = keymap_fn_layer(FN_INDEX(keycode));
  191. uint8_t key = keymap_fn_keycode(FN_INDEX(keycode));
  192. if (key) {
  193. return (action_t)ACTION_LAYER_TAP_KEY(layer, key);
  194. } else {
  195. return (action_t)ACTION_LAYER_MOMENTARY(layer);
  196. }
  197. }
  198. return (action_t)ACTION_NO;
  199. default:
  200. return (action_t)ACTION_NO;
  201. }
  202. }
  203. #else
  204. /* user keymaps should be defined somewhere */
  205. extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  206. extern const action_t fn_actions[];
  207. __attribute__ ((weak))
  208. uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  209. {
  210. #if defined(__AVR__)
  211. return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
  212. #else
  213. return keymaps[(layer)][(key.row)][(key.col)];
  214. #endif
  215. }
  216. __attribute__ ((weak))
  217. action_t keymap_fn_to_action(uint8_t keycode)
  218. {
  219. #if defined(__AVR__)
  220. return (action_t)pgm_read_word(&fn_actions[FN_INDEX(keycode)]);
  221. #else
  222. return fn_actions[FN_INDEX(keycode)];
  223. #endif
  224. }
  225. #endif