keymap_common.c 12 KB

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