extended_keymap_common.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "extended_keymap_common.h"
  15. #include "report.h"
  16. #include "keycode.h"
  17. #include "action_layer.h"
  18. #include "action.h"
  19. #include "action_macro.h"
  20. #include "debug.h"
  21. static action_t keycode_to_action(uint16_t keycode);
  22. /* converts key to action */
  23. action_t action_for_key(uint8_t layer, keypos_t key)
  24. {
  25. uint16_t keycode = keymap_key_to_keycode(layer, key);
  26. // Handle mods in keymap
  27. if (keycode > 0x00FF && keycode < 0x2000) {
  28. action_t action;
  29. action.code = ACTION_MODS_KEY(keycode >> 8, keycode & 0xFF);
  30. return action;
  31. } else if (keycode > 0x1FFF && keycode < 0x3000) {
  32. return keymap_func_to_action(keycode & 0xFFF);
  33. }
  34. switch (keycode) {
  35. case KC_FN0 ... KC_FN31:
  36. return keymap_fn_to_action(keycode);
  37. #ifdef BOOTMAGIC_ENABLE
  38. case KC_CAPSLOCK:
  39. case KC_LOCKING_CAPS:
  40. if (keymap_config.swap_control_capslock || keymap_config.capslock_to_control) {
  41. return keycode_to_action(KC_LCTL);
  42. }
  43. return keycode_to_action(keycode);
  44. case KC_LCTL:
  45. if (keymap_config.swap_control_capslock) {
  46. return keycode_to_action(KC_CAPSLOCK);
  47. }
  48. return keycode_to_action(KC_LCTL);
  49. case KC_LALT:
  50. if (keymap_config.swap_lalt_lgui) {
  51. if (keymap_config.no_gui) {
  52. return keycode_to_action(ACTION_NO);
  53. }
  54. return keycode_to_action(KC_LGUI);
  55. }
  56. return keycode_to_action(KC_LALT);
  57. case KC_LGUI:
  58. if (keymap_config.swap_lalt_lgui) {
  59. return keycode_to_action(KC_LALT);
  60. }
  61. if (keymap_config.no_gui) {
  62. return keycode_to_action(ACTION_NO);
  63. }
  64. return keycode_to_action(KC_LGUI);
  65. case KC_RALT:
  66. if (keymap_config.swap_ralt_rgui) {
  67. if (keymap_config.no_gui) {
  68. return keycode_to_action(ACTION_NO);
  69. }
  70. return keycode_to_action(KC_RGUI);
  71. }
  72. return keycode_to_action(KC_RALT);
  73. case KC_RGUI:
  74. if (keymap_config.swap_ralt_rgui) {
  75. return keycode_to_action(KC_RALT);
  76. }
  77. if (keymap_config.no_gui) {
  78. return keycode_to_action(ACTION_NO);
  79. }
  80. return keycode_to_action(KC_RGUI);
  81. case KC_GRAVE:
  82. if (keymap_config.swap_grave_esc) {
  83. return keycode_to_action(KC_ESC);
  84. }
  85. return keycode_to_action(KC_GRAVE);
  86. case KC_ESC:
  87. if (keymap_config.swap_grave_esc) {
  88. return keycode_to_action(KC_GRAVE);
  89. }
  90. return keycode_to_action(KC_ESC);
  91. case KC_BSLASH:
  92. if (keymap_config.swap_backslash_backspace) {
  93. return keycode_to_action(KC_BSPACE);
  94. }
  95. return keycode_to_action(KC_BSLASH);
  96. case KC_BSPACE:
  97. if (keymap_config.swap_backslash_backspace) {
  98. return keycode_to_action(KC_BSLASH);
  99. }
  100. return keycode_to_action(KC_BSPACE);
  101. #endif
  102. default:
  103. return keycode_to_action(keycode);
  104. }
  105. }
  106. /* Macro */
  107. __attribute__ ((weak))
  108. const macro_t *action_get_macro(keyrecord_t *record, uint8_t id, uint8_t opt)
  109. {
  110. return MACRO_NONE;
  111. }
  112. /* Function */
  113. __attribute__ ((weak))
  114. void action_function(keyrecord_t *record, uint8_t id, uint8_t opt)
  115. {
  116. }
  117. /* translates keycode to action */
  118. static action_t keycode_to_action(uint16_t keycode)
  119. {
  120. action_t action;
  121. switch (keycode) {
  122. case KC_A ... KC_EXSEL:
  123. case KC_LCTRL ... KC_RGUI:
  124. action.code = ACTION_KEY(keycode);
  125. break;
  126. case KC_SYSTEM_POWER ... KC_SYSTEM_WAKE:
  127. action.code = ACTION_USAGE_SYSTEM(KEYCODE2SYSTEM(keycode));
  128. break;
  129. case KC_AUDIO_MUTE ... KC_WWW_FAVORITES:
  130. action.code = ACTION_USAGE_CONSUMER(KEYCODE2CONSUMER(keycode));
  131. break;
  132. case KC_MS_UP ... KC_MS_ACCEL2:
  133. action.code = ACTION_MOUSEKEY(keycode);
  134. break;
  135. case KC_TRNS:
  136. action.code = ACTION_TRANSPARENT;
  137. break;
  138. default:
  139. action.code = ACTION_NO;
  140. break;
  141. }
  142. return action;
  143. }
  144. /* translates key to keycode */
  145. uint16_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  146. {
  147. // return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]);
  148. // This limits it to a byte
  149. return pgm_read_word(&keymaps[(layer)][(key.row)][(key.col)]);
  150. }
  151. /* translates Fn keycode to action */
  152. action_t keymap_fn_to_action(uint16_t keycode)
  153. {
  154. return (action_t){ .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) };
  155. }
  156. /* translates Fn keycode to action */
  157. action_t keymap_func_to_action(uint16_t keycode)
  158. {
  159. return (action_t){ .code = pgm_read_word(&fn_actions[(int)keycode]) };
  160. }