extended_keymap_common.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }
  65. switch (keycode) {
  66. case KC_FN0 ... KC_FN31:
  67. return keymap_fn_to_action(keycode);
  68. #ifdef BOOTMAGIC_ENABLE
  69. case KC_CAPSLOCK:
  70. case KC_LOCKING_CAPS:
  71. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  72. return keycode_to_action(KC_LCTL);
  73. }
  74. return keycode_to_action(keycode);
  75. case KC_LCTL:
  76. if (keymap_config.swap_control_capslock) {
  77. return keycode_to_action(KC_CAPSLOCK);
  78. }
  79. return keycode_to_action(KC_LCTL);
  80. case KC_LALT:
  81. if (keymap_config.swap_lalt_lgui) {
  82. if (keymap_config.no_gui) {
  83. return keycode_to_action(ACTION_NO);
  84. }
  85. return keycode_to_action(KC_LGUI);
  86. }
  87. return keycode_to_action(KC_LALT);
  88. case KC_LGUI:
  89. if (keymap_config.swap_lalt_lgui) {
  90. return keycode_to_action(KC_LALT);
  91. }
  92. if (keymap_config.no_gui) {
  93. return keycode_to_action(ACTION_NO);
  94. }
  95. return keycode_to_action(KC_LGUI);
  96. case KC_RALT:
  97. if (keymap_config.swap_ralt_rgui) {
  98. if (keymap_config.no_gui) {
  99. return keycode_to_action(ACTION_NO);
  100. }
  101. return keycode_to_action(KC_RGUI);
  102. }
  103. return keycode_to_action(KC_RALT);
  104. case KC_RGUI:
  105. if (keymap_config.swap_ralt_rgui) {
  106. return keycode_to_action(KC_RALT);
  107. }
  108. if (keymap_config.no_gui) {
  109. return keycode_to_action(ACTION_NO);
  110. }
  111. return keycode_to_action(KC_RGUI);
  112. case KC_GRAVE:
  113. if (keymap_config.swap_grave_esc) {
  114. return keycode_to_action(KC_ESC);
  115. }
  116. return keycode_to_action(KC_GRAVE);
  117. case KC_ESC:
  118. if (keymap_config.swap_grave_esc) {
  119. return keycode_to_action(KC_GRAVE);
  120. }
  121. return keycode_to_action(KC_ESC);
  122. case KC_BSLASH:
  123. if (keymap_config.swap_backslash_backspace) {
  124. return keycode_to_action(KC_BSPACE);
  125. }
  126. return keycode_to_action(KC_BSLASH);
  127. case KC_BSPACE:
  128. if (keymap_config.swap_backslash_backspace) {
  129. return keycode_to_action(KC_BSLASH);
  130. }
  131. return keycode_to_action(KC_BSPACE);
  132. #endif
  133. default:
  134. return keycode_to_action(keycode);
  135. }
  136. }
  137. /* Macro */
  138. __attribute__ ((weak))
  139. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  140. {
  141. return MACRO_NONE;
  142. }
  143. /* Function */
  144. __attribute__ ((weak))
  145. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  146. {
  147. }
  148. /* translates keycode to action */
  149. static action_t keycode_to_action(uint16_t keycode)
  150. {
  151. action_t action;
  152. switch (keycode) {
  153. case KC_A ... KC_EXSEL:
  154. case KC_LCTRL ... KC_RGUI:
  155. action.code = ACTION_KEY(keycode);
  156. break;
  157. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  158. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  159. break;
  160. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  161. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  162. break;
  163. case KC_MS_UP ... KC_MS_ACCEL2:
  164. action.code = ACTION_MOUSEKEY(keycode);
  165. break;
  166. case KC_TRNS:
  167. action.code = ACTION_TRANSPARENT;
  168. break;
  169. default:
  170. action.code = ACTION_NO;
  171. break;
  172. }
  173. return action;
  174. }
  175. /* translates key to keycode */
  176. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  177. {
  178. // Read entire word (16bits)
  179. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  180. }
  181. /* translates Fn keycode to action */
  182. action_t keymap_fn_to_action(uint16_t keycode)
  183. {
  184. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  185. }
  186. action_t keymap_func_to_action(uint16_t keycode)
  187. {
  188. // For FUNC without 8bit limit
  189. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  190. }