keymap_common.c 9.3 KB

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