keymap.c 5.3 KB

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