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