keymap_common.c 9.4 KB

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