upload
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

keymap_common.c 11KB

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