keymap_common.c 8.8 KB

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