keymap_common.c 5.9 KB

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