keymap_common.c 6.5 KB

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