keymap_common.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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 "action.h"
  19. #include "action_macro.h"
  20. #include "debug.h"
  21. #include "backlight.h"
  22. #include "keymap_midi.h"
  23. static action_t keycode_to_action(uint16_t keycode);
  24. uint16_t hextokeycode(int hex) {
  25. if (hex == 0x0) {
  26. return KC_0;
  27. } else if (hex < 0xA) {
  28. return KC_1 + (hex - 0x1);
  29. } else {
  30. return KC_A + (hex - 0xA);
  31. }
  32. }
  33. /* converts key to action */
  34. action_t action_for_key(uint8_t layer, keypos_t key)
  35. {
  36. // 16bit keycodes - important
  37. uint16_t keycode = keymap_key_to_keycode(layer, key);
  38. if (keycode >= 0x0100 && keycode < 0x2000) {
  39. // Has a modifier
  40. action_t action;
  41. // Split it up
  42. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF);
  43. return action;
  44. } else if (keycode >= 0x2000 && keycode < 0x3000) {
  45. // Is a shortcut for function layer, pull last 12bits
  46. return keymap_func_to_action(keycode & 0xFFF);
  47. } else if (keycode >= 0x3000 && keycode < 0x4000) {
  48. action_t action;
  49. action.code = ACTION_MACRO(keycode & 0xFF);
  50. return action;
  51. } else if (keycode >= BL_0 & keycode <= BL_15) {
  52. action_t action;
  53. action.code = ACTION_BACKLIGHT_LEVEL(keycode & 0x000F);
  54. return action;
  55. } else if (keycode == BL_DEC) {
  56. action_t action;
  57. action.code = ACTION_BACKLIGHT_DECREASE();
  58. return action;
  59. } else if (keycode == BL_INC) {
  60. action_t action;
  61. action.code = ACTION_BACKLIGHT_INCREASE();
  62. return action;
  63. } else if (keycode == BL_TOGG) {
  64. action_t action;
  65. action.code = ACTION_BACKLIGHT_TOGGLE();
  66. return action;
  67. } else if (keycode == BL_STEP) {
  68. action_t action;
  69. action.code = ACTION_BACKLIGHT_STEP();
  70. return action;
  71. } else if (keycode == RESET) {
  72. bootloader_jump();
  73. return;
  74. } else if (keycode == DEBUG) {
  75. print("\nDEBUG: enabled.\n");
  76. debug_enable = true;
  77. return;
  78. } else if (keycode >= 0x6000 && keycode < 0x7000) {
  79. action_t action;
  80. action.code = ACTION_FUNCTION_OPT(keycode & 0xFF, (keycode & 0x0F00) >> 8);
  81. return action;
  82. } else if (keycode >= 0x8000 && keycode < 0x9000) {
  83. action_t action;
  84. uint16_t unicode = keycode & ~(0x8000);
  85. action.code = ACTION_FUNCTION_OPT(unicode & 0xFF, (unicode & 0xFF00) >> 8);
  86. return action;
  87. }
  88. switch (keycode) {
  89. case KC_FN0 ... KC_FN31:
  90. return keymap_fn_to_action(keycode);
  91. #ifdef BOOTMAGIC_ENABLE
  92. case KC_CAPSLOCK:
  93. case KC_LOCKING_CAPS:
  94. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  95. return keycode_to_action(KC_LCTL);
  96. }
  97. return keycode_to_action(keycode);
  98. case KC_LCTL:
  99. if (keymap_config.swap_control_capslock) {
  100. return keycode_to_action(KC_CAPSLOCK);
  101. }
  102. return keycode_to_action(KC_LCTL);
  103. case KC_LALT:
  104. if (keymap_config.swap_lalt_lgui) {
  105. if (keymap_config.no_gui) {
  106. return keycode_to_action(ACTION_NO);
  107. }
  108. return keycode_to_action(KC_LGUI);
  109. }
  110. return keycode_to_action(KC_LALT);
  111. case KC_LGUI:
  112. if (keymap_config.swap_lalt_lgui) {
  113. return keycode_to_action(KC_LALT);
  114. }
  115. if (keymap_config.no_gui) {
  116. return keycode_to_action(ACTION_NO);
  117. }
  118. return keycode_to_action(KC_LGUI);
  119. case KC_RALT:
  120. if (keymap_config.swap_ralt_rgui) {
  121. if (keymap_config.no_gui) {
  122. return keycode_to_action(ACTION_NO);
  123. }
  124. return keycode_to_action(KC_RGUI);
  125. }
  126. return keycode_to_action(KC_RALT);
  127. case KC_RGUI:
  128. if (keymap_config.swap_ralt_rgui) {
  129. return keycode_to_action(KC_RALT);
  130. }
  131. if (keymap_config.no_gui) {
  132. return keycode_to_action(ACTION_NO);
  133. }
  134. return keycode_to_action(KC_RGUI);
  135. case KC_GRAVE:
  136. if (keymap_config.swap_grave_esc) {
  137. return keycode_to_action(KC_ESC);
  138. }
  139. return keycode_to_action(KC_GRAVE);
  140. case KC_ESC:
  141. if (keymap_config.swap_grave_esc) {
  142. return keycode_to_action(KC_GRAVE);
  143. }
  144. return keycode_to_action(KC_ESC);
  145. case KC_BSLASH:
  146. if (keymap_config.swap_backslash_backspace) {
  147. return keycode_to_action(KC_BSPACE);
  148. }
  149. return keycode_to_action(KC_BSLASH);
  150. case KC_BSPACE:
  151. if (keymap_config.swap_backslash_backspace) {
  152. return keycode_to_action(KC_BSLASH);
  153. }
  154. return keycode_to_action(KC_BSPACE);
  155. #endif
  156. default:
  157. return keycode_to_action(keycode);
  158. }
  159. }
  160. /* Macro */
  161. __attribute__ ((weak))
  162. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  163. {
  164. return MACRO_NONE;
  165. }
  166. /* Function */
  167. __attribute__ ((weak))
  168. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  169. {
  170. }
  171. /* translates keycode to action */
  172. static action_t keycode_to_action(uint16_t keycode)
  173. {
  174. action_t action;
  175. switch (keycode) {
  176. case KC_A ... KC_EXSEL:
  177. case KC_LCTRL ... KC_RGUI:
  178. action.code = ACTION_KEY(keycode);
  179. break;
  180. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  181. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  182. break;
  183. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  184. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  185. break;
  186. case KC_MS_UP ... KC_MS_ACCEL2:
  187. action.code = ACTION_MOUSEKEY(keycode);
  188. break;
  189. case KC_TRNS:
  190. action.code = ACTION_TRANSPARENT;
  191. break;
  192. default:
  193. action.code = ACTION_NO;
  194. break;
  195. }
  196. return action;
  197. }
  198. /* translates key to keycode */
  199. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  200. {
  201. // Read entire word (16bits)
  202. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  203. }
  204. /* translates Fn keycode to action */
  205. action_t keymap_fn_to_action(uint16_t keycode)
  206. {
  207. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  208. }
  209. action_t keymap_func_to_action(uint16_t keycode)
  210. {
  211. // For FUNC without 8bit limit
  212. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  213. }