keymap_common.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #include "bootloader.h"
  25. #include "eeconfig.h"
  26. extern keymap_config_t keymap_config;
  27. #include <stdio.h>
  28. #include <inttypes.h>
  29. #ifdef AUDIO_ENABLE
  30. #include "audio.h"
  31. #endif /* AUDIO_ENABLE */
  32. static action_t keycode_to_action(uint16_t keycode);
  33. /* converts key to action */
  34. action_t action_for_key(uint8_t layer, keypos_t key)
  35. {
  36. // 16bit keycodes - important
  37. uint16_t keycode = keymap_key_to_keycode(layer, key);
  38. switch (keycode) {
  39. case KC_FN0 ... KC_FN31:
  40. return keymap_fn_to_action(keycode);
  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. default:
  105. return keycode_to_action(keycode);
  106. }
  107. }
  108. /* Macro */
  109. __attribute__ ((weak))
  110. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  111. {
  112. return MACRO_NONE;
  113. }
  114. /* Function */
  115. __attribute__ ((weak))
  116. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  117. {
  118. }
  119. /* translates keycode to action */
  120. static action_t keycode_to_action(uint16_t keycode)
  121. {
  122. action_t action;
  123. switch (keycode) {
  124. case KC_A ... KC_EXSEL:
  125. case KC_LCTRL ... KC_RGUI:
  126. action.code = ACTION_KEY(keycode);
  127. break;
  128. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  129. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  130. break;
  131. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  132. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  133. break;
  134. case KC_MS_UP ... KC_MS_ACCEL2:
  135. action.code = ACTION_MOUSEKEY(keycode);
  136. break;
  137. case KC_TRNS:
  138. action.code = ACTION_TRANSPARENT;
  139. break;
  140. case 0x0100 ... 0x1FFF: ;
  141. // Has a modifier
  142. // Split it up
  143. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
  144. break;
  145. case 0x2000 ... 0x2FFF:
  146. // Is a shortcut for function layer, pull last 12bits
  147. // This means we have 4,096 FN macros at our disposal
  148. return keymap_func_to_action(keycode & 0xFFF);
  149. break;
  150. case 0x3000 ... 0x3FFF: ;
  151. // When the code starts with 3, it's an action macro.
  152. action.code = ACTION_MACRO(keycode & 0xFF);
  153. break;
  154. #ifdef BACKLIGHT_ENABLE
  155. case BL_0 ... BL_15:
  156. action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
  157. break;
  158. case BL_DEC:
  159. action.code = ACTION_BACKLIGHT_DECREASE();
  160. break;
  161. case BL_INC:
  162. action.code = ACTION_BACKLIGHT_INCREASE();
  163. break;
  164. case BL_TOGG:
  165. action.code = ACTION_BACKLIGHT_TOGGLE();
  166. break;
  167. case BL_STEP:
  168. action.code = ACTION_BACKLIGHT_STEP();
  169. break;
  170. #endif
  171. case RESET: ; // RESET is 0x5000, which is why this is here
  172. clear_keyboard();
  173. #ifdef AUDIO_ENABLE
  174. play_goodbye_tone();
  175. #endif
  176. _delay_ms(250);
  177. #ifdef ATREUS_ASTAR
  178. *(uint16_t *)0x0800 = 0x7777; // these two are a-star-specific
  179. #endif
  180. bootloader_jump();
  181. break;
  182. case DEBUG: ; // DEBUG is 0x5001
  183. print("\nDEBUG: enabled.\n");
  184. debug_enable = true;
  185. break;
  186. case 0x5002 ... 0x50FF:
  187. // MAGIC actions (BOOTMAGIC without the boot)
  188. if (!eeconfig_is_enabled()) {
  189. eeconfig_init();
  190. }
  191. /* keymap config */
  192. keymap_config.raw = eeconfig_read_keymap();
  193. if (keycode == MAGIC_SWAP_CONTROL_CAPSLOCK) {
  194. keymap_config.swap_control_capslock = 1;
  195. } else if (keycode == MAGIC_CAPSLOCK_TO_CONTROL) {
  196. keymap_config.capslock_to_control = 1;
  197. } else if (keycode == MAGIC_SWAP_LALT_LGUI) {
  198. keymap_config.swap_lalt_lgui = 1;
  199. } else if (keycode == MAGIC_SWAP_RALT_RGUI) {
  200. keymap_config.swap_ralt_rgui = 1;
  201. } else if (keycode == MAGIC_NO_GUI) {
  202. keymap_config.no_gui = 1;
  203. } else if (keycode == MAGIC_SWAP_GRAVE_ESC) {
  204. keymap_config.swap_grave_esc = 1;
  205. } else if (keycode == MAGIC_SWAP_BACKSLASH_BACKSPACE) {
  206. keymap_config.swap_backslash_backspace = 1;
  207. } else if (keycode == MAGIC_HOST_NKRO) {
  208. keymap_config.nkro = 1;
  209. } else if (keycode == MAGIC_SWAP_ALT_GUI) {
  210. keymap_config.swap_lalt_lgui = 1;
  211. keymap_config.swap_ralt_rgui = 1;
  212. }
  213. /* UNs */
  214. else if (keycode == MAGIC_UNSWAP_CONTROL_CAPSLOCK) {
  215. keymap_config.swap_control_capslock = 0;
  216. } else if (keycode == MAGIC_UNCAPSLOCK_TO_CONTROL) {
  217. keymap_config.capslock_to_control = 0;
  218. } else if (keycode == MAGIC_UNSWAP_LALT_LGUI) {
  219. keymap_config.swap_lalt_lgui = 0;
  220. } else if (keycode == MAGIC_UNSWAP_RALT_RGUI) {
  221. keymap_config.swap_ralt_rgui = 0;
  222. } else if (keycode == MAGIC_UNNO_GUI) {
  223. keymap_config.no_gui = 0;
  224. } else if (keycode == MAGIC_UNSWAP_GRAVE_ESC) {
  225. keymap_config.swap_grave_esc = 0;
  226. } else if (keycode == MAGIC_UNSWAP_BACKSLASH_BACKSPACE) {
  227. keymap_config.swap_backslash_backspace = 0;
  228. } else if (keycode == MAGIC_UNHOST_NKRO) {
  229. keymap_config.nkro = 0;
  230. } else if (keycode == MAGIC_UNSWAP_ALT_GUI) {
  231. keymap_config.swap_lalt_lgui = 0;
  232. keymap_config.swap_ralt_rgui = 0;
  233. }
  234. eeconfig_write_keymap(keymap_config.raw);
  235. break;
  236. case 0x5100 ... 0x5FFF: ;
  237. // Layer movement shortcuts
  238. // See .h to see constraints/usage
  239. int type = (keycode >> 0x8) & 0xF;
  240. if (type == 0x1) {
  241. // Layer set "GOTO"
  242. int when = (keycode >> 0x4) & 0x3;
  243. int layer = keycode & 0xF;
  244. action.code = ACTION_LAYER_SET(layer, when);
  245. } else if (type == 0x2) {
  246. // Momentary layer
  247. int layer = keycode & 0xFF;
  248. action.code = ACTION_LAYER_MOMENTARY(layer);
  249. } else if (type == 0x3) {
  250. // Set default layer
  251. int layer = keycode & 0xFF;
  252. action.code = ACTION_DEFAULT_LAYER_SET(layer);
  253. } else if (type == 0x4) {
  254. // Set default layer
  255. int layer = keycode & 0xFF;
  256. action.code = ACTION_LAYER_TOGGLE(layer);
  257. }
  258. break;
  259. #ifdef MIDI_ENABLE
  260. case 0x6000 ... 0x6FFF:
  261. action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
  262. break;
  263. #endif
  264. case 0x7000 ... 0x7FFF:
  265. action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  266. break;
  267. case 0x8000 ... 0x8FFF:
  268. action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  269. break;
  270. #ifdef UNICODE_ENABLE
  271. case 0x8000000 ... 0x8FFFFFF:
  272. uint16_t unicode = keycode & ~(0x8000);
  273. action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
  274. break;
  275. #endif
  276. default:
  277. action.code = ACTION_NO;
  278. break;
  279. }
  280. return action;
  281. }
  282. /* translates key to keycode */
  283. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  284. {
  285. // Read entire word (16bits)
  286. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  287. }
  288. /* translates Fn keycode to action */
  289. action_t keymap_fn_to_action(uint16_t keycode)
  290. {
  291. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  292. }
  293. action_t keymap_func_to_action(uint16_t keycode)
  294. {
  295. // For FUNC without 8bit limit
  296. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  297. }
  298. void update_tri_layer(uint8_t layer1, uint8_t layer2, uint8_t layer3) {
  299. if (IS_LAYER_ON(layer1) && IS_LAYER_ON(layer2)) {
  300. layer_on(layer3);
  301. } else {
  302. layer_off(layer3);
  303. }
  304. }