keymap_common.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "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 "keymap_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. // The arm-none-eabi compiler generates out of bounds warnings when using the fn_actions directly for some reason
  42. const uint16_t* actions = fn_actions;
  43. switch (keycode) {
  44. case KC_FN0 ... KC_FN31:
  45. action.code = pgm_read_word(&actions[FN_INDEX(keycode)]);
  46. break;
  47. case KC_A ... KC_EXSEL:
  48. case KC_LCTRL ... KC_RGUI:
  49. action.code = ACTION_KEY(keycode);
  50. break;
  51. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  52. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  53. break;
  54. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  55. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  56. break;
  57. case KC_MS_UP ... KC_MS_ACCEL2:
  58. action.code = ACTION_MOUSEKEY(keycode);
  59. break;
  60. case KC_TRNS:
  61. action.code = ACTION_TRANSPARENT;
  62. break;
  63. case QK_MODS ... QK_MODS_MAX: ;
  64. // Has a modifier
  65. // Split it up
  66. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF); // adds modifier to key
  67. break;
  68. case QK_FUNCTION ... QK_FUNCTION_MAX: ;
  69. // Is a shortcut for function action_layer, pull last 12bits
  70. // This means we have 4,096 FN macros at our disposal
  71. action.code = pgm_read_word(&actions[(int)keycode & 0xFFF]);
  72. break;
  73. case QK_MACRO ... QK_MACRO_MAX:
  74. action.code = ACTION_MACRO(keycode & 0xFF);
  75. break;
  76. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  77. action.code = ACTION_LAYER_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  78. break;
  79. case QK_TO ... QK_TO_MAX: ;
  80. // Layer set "GOTO"
  81. when = (keycode >> 0x4) & 0x3;
  82. action_layer = keycode & 0xF;
  83. action.code = ACTION_LAYER_SET(action_layer, when);
  84. break;
  85. case QK_MOMENTARY ... QK_MOMENTARY_MAX: ;
  86. // Momentary action_layer
  87. action_layer = keycode & 0xFF;
  88. action.code = ACTION_LAYER_MOMENTARY(action_layer);
  89. break;
  90. case QK_DEF_LAYER ... QK_DEF_LAYER_MAX: ;
  91. // Set default action_layer
  92. action_layer = keycode & 0xFF;
  93. action.code = ACTION_DEFAULT_LAYER_SET(action_layer);
  94. break;
  95. case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX: ;
  96. // Set toggle
  97. action_layer = keycode & 0xFF;
  98. action.code = ACTION_LAYER_TOGGLE(action_layer);
  99. break;
  100. case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX: ;
  101. // OSL(action_layer) - One-shot action_layer
  102. action_layer = keycode & 0xFF;
  103. action.code = ACTION_LAYER_ONESHOT(action_layer);
  104. break;
  105. case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX: ;
  106. // OSM(mod) - One-shot mod
  107. mod = keycode & 0xFF;
  108. action.code = ACTION_MODS_ONESHOT(mod);
  109. break;
  110. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  111. action.code = ACTION_MODS_TAP_KEY((keycode >> 0x8) & 0xF, keycode & 0xFF);
  112. break;
  113. #ifdef BACKLIGHT_ENABLE
  114. case BL_0 ... BL_15:
  115. action.code = ACTION_BACKLIGHT_LEVEL(keycode - BL_0);
  116. break;
  117. case BL_DEC:
  118. action.code = ACTION_BACKLIGHT_DECREASE();
  119. break;
  120. case BL_INC:
  121. action.code = ACTION_BACKLIGHT_INCREASE();
  122. break;
  123. case BL_TOGG:
  124. action.code = ACTION_BACKLIGHT_TOGGLE();
  125. break;
  126. case BL_STEP:
  127. action.code = ACTION_BACKLIGHT_STEP();
  128. break;
  129. #endif
  130. default:
  131. action.code = ACTION_NO;
  132. break;
  133. }
  134. return action;
  135. }
  136. __attribute__ ((weak))
  137. const uint16_t PROGMEM fn_actions[] = {
  138. };
  139. /* Macro */
  140. __attribute__ ((weak))
  141. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  142. {
  143. return MACRO_NONE;
  144. }
  145. /* Function */
  146. __attribute__ ((weak))
  147. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  148. {
  149. }
  150. /* translates key to keycode */
  151. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  152. {
  153. // Read entire word (16bits)
  154. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  155. }