keymap_common.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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); // adds modifier to key
  34. return action;
  35. } else if (keycode >= 0x2000 && keycode < 0x3000) {
  36. // Is a shortcut for function layer, pull last 12bits
  37. // This means we have 4,096 FN macros at our disposal
  38. return keymap_func_to_action(keycode & 0xFFF);
  39. } else if (keycode >= 0x3000 && keycode < 0x4000) {
  40. // When the code starts with 3, it's an action macro.
  41. action_t action;
  42. action.code = ACTION_MACRO(keycode & 0xFF);
  43. return action;
  44. #ifdef BACKLIGHT_ENABLE
  45. } else if (keycode >= BL_0 & keycode <= BL_15) {
  46. action_t action;
  47. action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
  48. return action;
  49. } else if (keycode == BL_DEC) {
  50. action_t action;
  51. action.code = ACTION_BACKLIGHT_DECREASE();
  52. return action;
  53. } else if (keycode == BL_INC) {
  54. action_t action;
  55. action.code = ACTION_BACKLIGHT_INCREASE();
  56. return action;
  57. } else if (keycode == BL_TOGG) {
  58. action_t action;
  59. action.code = ACTION_BACKLIGHT_TOGGLE();
  60. return action;
  61. } else if (keycode == BL_STEP) {
  62. action_t action;
  63. action.code = ACTION_BACKLIGHT_STEP();
  64. return action;
  65. #endif
  66. } else if (keycode == RESET) { // RESET is 0x5000, which is why this is here
  67. bootloader_jump();
  68. return;
  69. } else if (keycode == DEBUG) { // DEBUG is 0x5001
  70. // TODO: Does this actually work?
  71. print("\nDEBUG: enabled.\n");
  72. debug_enable = true;
  73. return;
  74. } else if (keycode >= 0x5000 && keycode < 0x6000) {
  75. // Layer movement shortcuts
  76. // See .h to see constraints/usage
  77. int type = (keycode >> 0x8) & 0xF;
  78. if (type == 0x1) {
  79. // Layer set "GOTO"
  80. int when = (keycode >> 0x4) & 0x3;
  81. int layer = keycode & 0xF;
  82. action_t action;
  83. action.code = ACTION_LAYER_SET(layer, when);
  84. return action;
  85. } else if (type == 0x2) {
  86. // Momentary layer
  87. int layer = keycode & 0xFF;
  88. action_t action;
  89. action.code = ACTION_LAYER_MOMENTARY(layer);
  90. return action;
  91. } else if (type == 0x3) {
  92. // Set default layer
  93. int layer = keycode & 0xFF;
  94. action_t action;
  95. action.code = ACTION_DEFAULT_LAYER_SET(layer);
  96. return action;
  97. } else if (type == 0x4) {
  98. // Set default layer
  99. int layer = keycode & 0xFF;
  100. action_t action;
  101. action.code = ACTION_LAYER_TOGGLE(layer);
  102. return action;
  103. }
  104. #ifdef MIDI_ENABLE
  105. } else if (keycode >= 0x6000 && keycode < 0x7000) {
  106. action_t action;
  107. action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
  108. return action;
  109. #endif
  110. } else if (keycode >= 0x7000 && keycode < 0x8000) {
  111. action_t action;
  112. action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  113. return action;
  114. } else if (keycode >= 0x8000 && keycode < 0x9000) {
  115. action_t action;
  116. action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  117. return action;
  118. #ifdef UNICODE_ENABLE
  119. } else if (keycode >= 0x8000000) {
  120. action_t action;
  121. uint16_t unicode = keycode & ~(0x8000);
  122. action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
  123. return action;
  124. #endif
  125. } else {
  126. }
  127. switch (keycode) {
  128. case KC_FN0 ... KC_FN31:
  129. return keymap_fn_to_action(keycode);
  130. #ifdef BOOTMAGIC_ENABLE
  131. case KC_CAPSLOCK:
  132. case KC_LOCKING_CAPS:
  133. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  134. return keycode_to_action(KC_LCTL);
  135. }
  136. return keycode_to_action(keycode);
  137. case KC_LCTL:
  138. if (keymap_config.swap_control_capslock) {
  139. return keycode_to_action(KC_CAPSLOCK);
  140. }
  141. return keycode_to_action(KC_LCTL);
  142. case KC_LALT:
  143. if (keymap_config.swap_lalt_lgui) {
  144. if (keymap_config.no_gui) {
  145. return keycode_to_action(ACTION_NO);
  146. }
  147. return keycode_to_action(KC_LGUI);
  148. }
  149. return keycode_to_action(KC_LALT);
  150. case KC_LGUI:
  151. if (keymap_config.swap_lalt_lgui) {
  152. return keycode_to_action(KC_LALT);
  153. }
  154. if (keymap_config.no_gui) {
  155. return keycode_to_action(ACTION_NO);
  156. }
  157. return keycode_to_action(KC_LGUI);
  158. case KC_RALT:
  159. if (keymap_config.swap_ralt_rgui) {
  160. if (keymap_config.no_gui) {
  161. return keycode_to_action(ACTION_NO);
  162. }
  163. return keycode_to_action(KC_RGUI);
  164. }
  165. return keycode_to_action(KC_RALT);
  166. case KC_RGUI:
  167. if (keymap_config.swap_ralt_rgui) {
  168. return keycode_to_action(KC_RALT);
  169. }
  170. if (keymap_config.no_gui) {
  171. return keycode_to_action(ACTION_NO);
  172. }
  173. return keycode_to_action(KC_RGUI);
  174. case KC_GRAVE:
  175. if (keymap_config.swap_grave_esc) {
  176. return keycode_to_action(KC_ESC);
  177. }
  178. return keycode_to_action(KC_GRAVE);
  179. case KC_ESC:
  180. if (keymap_config.swap_grave_esc) {
  181. return keycode_to_action(KC_GRAVE);
  182. }
  183. return keycode_to_action(KC_ESC);
  184. case KC_BSLASH:
  185. if (keymap_config.swap_backslash_backspace) {
  186. return keycode_to_action(KC_BSPACE);
  187. }
  188. return keycode_to_action(KC_BSLASH);
  189. case KC_BSPACE:
  190. if (keymap_config.swap_backslash_backspace) {
  191. return keycode_to_action(KC_BSLASH);
  192. }
  193. return keycode_to_action(KC_BSPACE);
  194. #endif
  195. default:
  196. return keycode_to_action(keycode);
  197. }
  198. }
  199. /* Macro */
  200. __attribute__ ((weak))
  201. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  202. {
  203. return MACRO_NONE;
  204. }
  205. /* Function */
  206. __attribute__ ((weak))
  207. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  208. {
  209. }
  210. /* translates keycode to action */
  211. static action_t keycode_to_action(uint16_t keycode)
  212. {
  213. action_t action;
  214. switch (keycode) {
  215. case KC_A ... KC_EXSEL:
  216. case KC_LCTRL ... KC_RGUI:
  217. action.code = ACTION_KEY(keycode);
  218. break;
  219. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  220. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  221. break;
  222. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  223. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  224. break;
  225. case KC_MS_UP ... KC_MS_ACCEL2:
  226. action.code = ACTION_MOUSEKEY(keycode);
  227. break;
  228. case KC_TRNS:
  229. action.code = ACTION_TRANSPARENT;
  230. break;
  231. default:
  232. action.code = ACTION_NO;
  233. break;
  234. }
  235. return action;
  236. }
  237. /* translates key to keycode */
  238. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  239. {
  240. // Read entire word (16bits)
  241. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  242. }
  243. /* translates Fn keycode to action */
  244. action_t keymap_fn_to_action(uint16_t keycode)
  245. {
  246. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  247. }
  248. action_t keymap_func_to_action(uint16_t keycode)
  249. {
  250. // For FUNC without 8bit limit
  251. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  252. }