extended_keymap_common.c 5.7 KB

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