extended_keymap_common.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. Copyright 2012,2013 Jun Wako <wakojun@gmail.com>
  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 "extended_keymap_common.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(uint16_t keycode);
  22. /* converts key to action */
  23. action_t action_for_key(uint8_t layer, keypos_t key)
  24. {
  25. // 16bit keycodes - important
  26. uint16_t keycode = keymap_key_to_keycode(layer, key);
  27. if (keycode > 0x00FF && keycode < 0x2000) {
  28. // Has a modifier
  29. action_t action;
  30. // Split it up
  31. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF);
  32. return action;
  33. } else if (keycode > 0x1FFF && keycode < 0x3000) {
  34. // Is a shortcut for function layer, pull last 12bits
  35. return keymap_func_to_action(keycode & 0xFFF);
  36. }
  37. switch (keycode) {
  38. case KC_FN0 ... KC_FN31:
  39. return keymap_fn_to_action(keycode);
  40. #ifdef BOOTMAGIC_ENABLE
  41. case KC_CAPSLOCK:
  42. case KC_LOCKING_CAPS:
  43. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  44. return keycode_to_action(KC_LCTL);
  45. }
  46. return keycode_to_action(keycode);
  47. case KC_LCTL:
  48. if (keymap_config.swap_control_capslock) {
  49. return keycode_to_action(KC_CAPSLOCK);
  50. }
  51. return keycode_to_action(KC_LCTL);
  52. case KC_LALT:
  53. if (keymap_config.swap_lalt_lgui) {
  54. if (keymap_config.no_gui) {
  55. return keycode_to_action(ACTION_NO);
  56. }
  57. return keycode_to_action(KC_LGUI);
  58. }
  59. return keycode_to_action(KC_LALT);
  60. case KC_LGUI:
  61. if (keymap_config.swap_lalt_lgui) {
  62. return keycode_to_action(KC_LALT);
  63. }
  64. if (keymap_config.no_gui) {
  65. return keycode_to_action(ACTION_NO);
  66. }
  67. return keycode_to_action(KC_LGUI);
  68. case KC_RALT:
  69. if (keymap_config.swap_ralt_rgui) {
  70. if (keymap_config.no_gui) {
  71. return keycode_to_action(ACTION_NO);
  72. }
  73. return keycode_to_action(KC_RGUI);
  74. }
  75. return keycode_to_action(KC_RALT);
  76. case KC_RGUI:
  77. if (keymap_config.swap_ralt_rgui) {
  78. return keycode_to_action(KC_RALT);
  79. }
  80. if (keymap_config.no_gui) {
  81. return keycode_to_action(ACTION_NO);
  82. }
  83. return keycode_to_action(KC_RGUI);
  84. case KC_GRAVE:
  85. if (keymap_config.swap_grave_esc) {
  86. return keycode_to_action(KC_ESC);
  87. }
  88. return keycode_to_action(KC_GRAVE);
  89. case KC_ESC:
  90. if (keymap_config.swap_grave_esc) {
  91. return keycode_to_action(KC_GRAVE);
  92. }
  93. return keycode_to_action(KC_ESC);
  94. case KC_BSLASH:
  95. if (keymap_config.swap_backslash_backspace) {
  96. return keycode_to_action(KC_BSPACE);
  97. }
  98. return keycode_to_action(KC_BSLASH);
  99. case KC_BSPACE:
  100. if (keymap_config.swap_backslash_backspace) {
  101. return keycode_to_action(KC_BSLASH);
  102. }
  103. return keycode_to_action(KC_BSPACE);
  104. #endif
  105. default:
  106. return keycode_to_action(keycode);
  107. }
  108. }
  109. /* Macro */
  110. __attribute__ ((weak))
  111. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  112. {
  113. return MACRO_NONE;
  114. }
  115. /* Function */
  116. __attribute__ ((weak))
  117. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  118. {
  119. }
  120. /* translates keycode to action */
  121. static action_t keycode_to_action(uint16_t keycode)
  122. {
  123. action_t action;
  124. switch (keycode) {
  125. case KC_A ... KC_EXSEL:
  126. case KC_LCTRL ... KC_RGUI:
  127. action.code = ACTION_KEY(keycode);
  128. break;
  129. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  130. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  131. break;
  132. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  133. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  134. break;
  135. case KC_MS_UP ... KC_MS_ACCEL2:
  136. action.code = ACTION_MOUSEKEY(keycode);
  137. break;
  138. case KC_TRNS:
  139. action.code = ACTION_TRANSPARENT;
  140. break;
  141. default:
  142. action.code = ACTION_NO;
  143. break;
  144. }
  145. return action;
  146. }
  147. /* translates key to keycode */
  148. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  149. {
  150. // Read entire word (16bits)
  151. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  152. }
  153. /* translates Fn keycode to action */
  154. action_t keymap_fn_to_action(uint16_t keycode)
  155. {
  156. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  157. }
  158. /* translates Fn keycode to action */
  159. action_t keymap_func_to_action(uint16_t keycode)
  160. {
  161. // For FUNC without 8bit limit
  162. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  163. }