extended_keymap_common.c 6.5 KB

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