keymap_common.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. Copyright 2012-2017 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.h"
  15. #include "report.h"
  16. #include "keycode.h"
  17. #include "action_layer.h"
  18. #if defined(__AVR__)
  19. #include <util/delay.h>
  20. #include <stdio.h>
  21. #endif
  22. #include "action.h"
  23. #include "action_macro.h"
  24. #include "debug.h"
  25. #include "backlight.h"
  26. #include "quantum.h"
  27. #ifdef SPLIT_KEYBOARD
  28. #include "split_flags.h"
  29. #endif
  30. #ifdef MIDI_ENABLE
  31. #include "process_midi.h"
  32. #endif
  33. extern keymap_config_t keymap_config;
  34. #include <inttypes.h>
  35. /* converts key to action */
  36. action_t action_for_key(uint8_t layer, keypos_t key)
  37. {
  38. // 16bit keycodes - important
  39. uint16_t keycode = keymap_key_to_keycode(layer, key);
  40. // keycode remapping
  41. keycode = keycode_config(keycode);
  42. action_t action;
  43. uint8_t action_layer, when, mod;
  44. switch (keycode) {
  45. case KC_FN0 ... KC_FN31:
  46. action.code = keymap_function_id_to_action(FN_INDEX(keycode));
  47. break;
  48. case KC_A ... KC_EXSEL:
  49. case KC_LCTRL ... KC_RGUI:
  50. action.code = ACTION_KEY(keycode);
  51. break;
  52. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  53. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  54. break;
  55. case KC_AUDIO_MUTE ... KC_MEDIA_REWIND:
  56. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  57. break;
  58. case KC_MS_UP ... KC_MS_ACCEL2:
  59. action.code = ACTION_MOUSEKEY(keycode);
  60. break;
  61. case KC_TRNS:
  62. action.code = ACTION_TRANSPARENT;
  63. break;
  64. case QK_MODS ... QK_MODS_MAX: ;
  65. // Has a modifier
  66. // Split it up
  67. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
  68. break;
  69. case QK_FUNCTION ... QK_FUNCTION_MAX: ;
  70. // Is a shortcut for function action_layer, pull last 12bits
  71. // This means we have 4,096 FN macros at our disposal
  72. action.code = keymap_function_id_to_action( (int)keycode & 0xFFF );
  73. break;
  74. case QK_MACRO ... QK_MACRO_MAX:
  75. if (keycode & 0x800) // tap macros have upper bit set
  76. action.code = ACTION_MACRO_TAP(keycode & 0xFF);
  77. else
  78. action.code = ACTION_MACRO(keycode & 0xFF);
  79. break;
  80. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  81. action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  82. break;
  83. case QK_TO ... QK_TO_MAX: ;
  84. // Layer set "GOTO"
  85. when = (keycode >> 0x4) & 0x3;
  86. action_layer = keycode & 0xF;
  87. action.code = ACTION_LAYER_SET(action_layer, when);
  88. break;
  89. case QK_MOMENTARY ... QK_MOMENTARY_MAX: ;
  90. // Momentary action_layer
  91. action_layer = keycode & 0xFF;
  92. action.code = ACTION_LAYER_MOMENTARY(action_layer);
  93. break;
  94. case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: ;
  95. // Set default action_layer
  96. action_layer = keycode & 0xFF;
  97. action.code = ACTION_DEFAULT_LAYER_SET(action_layer);
  98. break;
  99. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: ;
  100. // Set toggle
  101. action_layer = keycode & 0xFF;
  102. action.code = ACTION_LAYER_TOGGLE(action_layer);
  103. break;
  104. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: ;
  105. // OSL(action_layer) - One-shot action_layer
  106. action_layer = keycode & 0xFF;
  107. action.code = ACTION_LAYER_ONESHOT(action_layer);
  108. break;
  109. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX: ;
  110. // OSM(mod) - One-shot mod
  111. mod = keycode & 0xFF;
  112. action.code = ACTION_MODS_ONESHOT(mod);
  113. break;
  114. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  115. action.code = ACTION_LAYER_TAP_TOGGLE(keycode & 0xFF);
  116. break;
  117. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
  118. mod = keycode & 0xF;
  119. action_layer = (keycode >> 4) & 0xF;
  120. action.code = ACTION_LAYER_MODS(action_layer, mod);
  121. break;
  122. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  123. mod = mod_config((keycode >> 0x8) & 0x1F);
  124. action.code = ACTION_MODS_TAP_KEY(mod, keycode & 0xFF);
  125. break;
  126. #ifdef BACKLIGHT_ENABLE
  127. case BL_ON:
  128. action.code = ACTION_BACKLIGHT_ON();
  129. #ifdef SPLIT_KEYBOARD
  130. BACKLIT_DIRTY = true;
  131. #endif
  132. break;
  133. case BL_OFF:
  134. action.code = ACTION_BACKLIGHT_OFF();
  135. #ifdef SPLIT_KEYBOARD
  136. BACKLIT_DIRTY = true;
  137. #endif
  138. break;
  139. case BL_DEC:
  140. action.code = ACTION_BACKLIGHT_DECREASE();
  141. #ifdef SPLIT_KEYBOARD
  142. BACKLIT_DIRTY = true;
  143. #endif
  144. break;
  145. case BL_INC:
  146. action.code = ACTION_BACKLIGHT_INCREASE();
  147. #ifdef SPLIT_KEYBOARD
  148. BACKLIT_DIRTY = true;
  149. #endif
  150. break;
  151. case BL_TOGG:
  152. action.code = ACTION_BACKLIGHT_TOGGLE();
  153. #ifdef SPLIT_KEYBOARD
  154. BACKLIT_DIRTY = true;
  155. #endif
  156. break;
  157. case BL_STEP:
  158. action.code = ACTION_BACKLIGHT_STEP();
  159. #ifdef SPLIT_KEYBOARD
  160. BACKLIT_DIRTY = true;
  161. #endif
  162. break;
  163. #endif
  164. #ifdef SWAP_HANDS_ENABLE
  165. case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
  166. action.code = ACTION(ACT_SWAP_HANDS, keycode & 0xff);
  167. break;
  168. #endif
  169. default:
  170. action.code = ACTION_NO;
  171. break;
  172. }
  173. return action;
  174. }
  175. __attribute__ ((weak))
  176. const uint16_t PROGMEM fn_actions[] = {
  177. };
  178. /* Macro */
  179. __attribute__ ((weak))
  180. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  181. {
  182. return MACRO_NONE;
  183. }
  184. /* Function */
  185. __attribute__ ((weak))
  186. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  187. {
  188. }
  189. // translates key to keycode
  190. __attribute__ ((weak))
  191. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  192. {
  193. // Read entire word (16bits)
  194. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  195. }
  196. // translates function id to action
  197. __attribute__ ((weak))
  198. uint16_t keymap_function_id_to_action( uint16_t function_id )
  199. {
  200. // The compiler sees the empty (weak) fn_actions and generates a warning
  201. // This function should not be called in that case, so the warning is too strict
  202. // If this function is called however, the keymap should have overridden fn_actions, and then the compile
  203. // is comparing against the wrong array
  204. #pragma GCC diagnostic push
  205. #pragma GCC diagnostic ignored "-Warray-bounds"
  206. return pgm_read_word(&fn_actions[function_id]);
  207. #pragma GCC diagnostic pop
  208. }