keymap_common.c 11 KB

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