keymap_common.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 "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. #include "backlight.h"
  22. #include "keymap_midi.h"
  23. static action_t keycode_to_action(uint16_t keycode);
  24. /* converts key to action */
  25. action_t action_for_key(uint8_t layer, keypos_t key)
  26. {
  27. // 16bit keycodes - important
  28. uint16_t keycode = keymap_key_to_keycode(layer, key);
  29. if (keycode >= 0x0100 && keycode < 0x2000) {
  30. // Has a modifier
  31. action_t action;
  32. // Split it up
  33. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF);
  34. return action;
  35. } else if (keycode >= 0x2000 && keycode < 0x3000) {
  36. // Is a shortcut for function layer, pull last 12bits
  37. return keymap_func_to_action(keycode & 0xFFF);
  38. } else if (keycode >= 0x3000 && keycode < 0x4000) {
  39. action_t action;
  40. action.code = ACTION_MACRO(keycode & 0xFF);
  41. return action;
  42. } else if (keycode >= BL_0 & keycode <= BL_15) {
  43. action_t action;
  44. action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
  45. return action;
  46. } else if (keycode == BL_DEC) {
  47. action_t action;
  48. action.code = ACTION_BACKLIGHT_DECREASE();
  49. return action;
  50. } else if (keycode == BL_INC) {
  51. action_t action;
  52. action.code = ACTION_BACKLIGHT_INCREASE();
  53. return action;
  54. } else if (keycode == BL_TOGG) {
  55. action_t action;
  56. action.code = ACTION_BACKLIGHT_TOGGLE();
  57. return action;
  58. } else if (keycode == BL_STEP) {
  59. action_t action;
  60. action.code = ACTION_BACKLIGHT_STEP();
  61. return action;
  62. } else if (keycode == RESET) {
  63. bootloader_jump();
  64. return;
  65. } else if (keycode == DEBUG) {
  66. print("\nDEBUG: enabled.\n");
  67. debug_enable = true;
  68. return;
  69. } else if (keycode >= 0x6000 && keycode < 0x7000) {
  70. action_t action;
  71. action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
  72. return action;
  73. }
  74. switch (keycode) {
  75. case KC_FN0 ... KC_FN31:
  76. return keymap_fn_to_action(keycode);
  77. #ifdef BOOTMAGIC_ENABLE
  78. case KC_CAPSLOCK:
  79. case KC_LOCKING_CAPS:
  80. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  81. return keycode_to_action(KC_LCTL);
  82. }
  83. return keycode_to_action(keycode);
  84. case KC_LCTL:
  85. if (keymap_config.swap_control_capslock) {
  86. return keycode_to_action(KC_CAPSLOCK);
  87. }
  88. return keycode_to_action(KC_LCTL);
  89. case KC_LALT:
  90. if (keymap_config.swap_lalt_lgui) {
  91. if (keymap_config.no_gui) {
  92. return keycode_to_action(ACTION_NO);
  93. }
  94. return keycode_to_action(KC_LGUI);
  95. }
  96. return keycode_to_action(KC_LALT);
  97. case KC_LGUI:
  98. if (keymap_config.swap_lalt_lgui) {
  99. return keycode_to_action(KC_LALT);
  100. }
  101. if (keymap_config.no_gui) {
  102. return keycode_to_action(ACTION_NO);
  103. }
  104. return keycode_to_action(KC_LGUI);
  105. case KC_RALT:
  106. if (keymap_config.swap_ralt_rgui) {
  107. if (keymap_config.no_gui) {
  108. return keycode_to_action(ACTION_NO);
  109. }
  110. return keycode_to_action(KC_RGUI);
  111. }
  112. return keycode_to_action(KC_RALT);
  113. case KC_RGUI:
  114. if (keymap_config.swap_ralt_rgui) {
  115. return keycode_to_action(KC_RALT);
  116. }
  117. if (keymap_config.no_gui) {
  118. return keycode_to_action(ACTION_NO);
  119. }
  120. return keycode_to_action(KC_RGUI);
  121. case KC_GRAVE:
  122. if (keymap_config.swap_grave_esc) {
  123. return keycode_to_action(KC_ESC);
  124. }
  125. return keycode_to_action(KC_GRAVE);
  126. case KC_ESC:
  127. if (keymap_config.swap_grave_esc) {
  128. return keycode_to_action(KC_GRAVE);
  129. }
  130. return keycode_to_action(KC_ESC);
  131. case KC_BSLASH:
  132. if (keymap_config.swap_backslash_backspace) {
  133. return keycode_to_action(KC_BSPACE);
  134. }
  135. return keycode_to_action(KC_BSLASH);
  136. case KC_BSPACE:
  137. if (keymap_config.swap_backslash_backspace) {
  138. return keycode_to_action(KC_BSLASH);
  139. }
  140. return keycode_to_action(KC_BSPACE);
  141. #endif
  142. default:
  143. return keycode_to_action(keycode);
  144. }
  145. }
  146. /* Macro */
  147. __attribute__ ((weak))
  148. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  149. {
  150. return MACRO_NONE;
  151. }
  152. /* Function */
  153. __attribute__ ((weak))
  154. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  155. {
  156. }
  157. /* translates keycode to action */
  158. static action_t keycode_to_action(uint16_t keycode)
  159. {
  160. action_t action;
  161. switch (keycode) {
  162. case KC_A ... KC_EXSEL:
  163. case KC_LCTRL ... KC_RGUI:
  164. action.code = ACTION_KEY(keycode);
  165. break;
  166. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  167. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  168. break;
  169. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  170. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  171. break;
  172. case KC_MS_UP ... KC_MS_ACCEL2:
  173. action.code = ACTION_MOUSEKEY(keycode);
  174. break;
  175. case KC_TRNS:
  176. action.code = ACTION_TRANSPARENT;
  177. break;
  178. default:
  179. action.code = ACTION_NO;
  180. break;
  181. }
  182. return action;
  183. }
  184. /* translates key to keycode */
  185. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  186. {
  187. // Read entire word (16bits)
  188. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  189. }
  190. /* translates Fn keycode to action */
  191. action_t keymap_fn_to_action(uint16_t keycode)
  192. {
  193. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  194. }
  195. action_t keymap_func_to_action(uint16_t keycode)
  196. {
  197. // For FUNC without 8bit limit
  198. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  199. }