action_util.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. Copyright 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 "host.h"
  15. #include "report.h"
  16. #include "debug.h"
  17. #include "action_util.h"
  18. #include "action_layer.h"
  19. #include "timer.h"
  20. #include "keycode_config.h"
  21. extern keymap_config_t keymap_config;
  22. static uint8_t real_mods = 0;
  23. static uint8_t weak_mods = 0;
  24. static uint8_t macro_mods = 0;
  25. #ifdef USB_6KRO_ENABLE
  26. #define RO_ADD(a, b) ((a + b) % KEYBOARD_REPORT_KEYS)
  27. #define RO_SUB(a, b) ((a - b + KEYBOARD_REPORT_KEYS) % KEYBOARD_REPORT_KEYS)
  28. #define RO_INC(a) RO_ADD(a, 1)
  29. #define RO_DEC(a) RO_SUB(a, 1)
  30. static int8_t cb_head = 0;
  31. static int8_t cb_tail = 0;
  32. static int8_t cb_count = 0;
  33. #endif
  34. // TODO: pointer variable is not needed
  35. //report_keyboard_t keyboard_report = {};
  36. report_keyboard_t *keyboard_report = &(report_keyboard_t){};
  37. extern inline void add_key(uint8_t key);
  38. extern inline void del_key(uint8_t key);
  39. extern inline void clear_keys(void);
  40. #ifndef NO_ACTION_ONESHOT
  41. static int8_t oneshot_mods = 0;
  42. static int8_t oneshot_locked_mods = 0;
  43. int8_t get_oneshot_locked_mods(void) { return oneshot_locked_mods; }
  44. void set_oneshot_locked_mods(int8_t mods) { oneshot_locked_mods = mods; }
  45. void clear_oneshot_locked_mods(void) { oneshot_locked_mods = 0; }
  46. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  47. static int16_t oneshot_time = 0;
  48. bool has_oneshot_mods_timed_out(void) {
  49. return TIMER_DIFF_16(timer_read(), oneshot_time) >= ONESHOT_TIMEOUT;
  50. }
  51. #else
  52. bool has_oneshot_mods_timed_out(void) {
  53. return false;
  54. }
  55. #endif
  56. #endif
  57. /* oneshot layer */
  58. #ifndef NO_ACTION_ONESHOT
  59. /* oneshot_layer_data bits
  60. * LLLL LSSS
  61. * where:
  62. * L => are layer bits
  63. * S => oneshot state bits
  64. */
  65. static int8_t oneshot_layer_data = 0;
  66. inline uint8_t get_oneshot_layer(void) { return oneshot_layer_data >> 3; }
  67. inline uint8_t get_oneshot_layer_state(void) { return oneshot_layer_data & 0b111; }
  68. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  69. static int16_t oneshot_layer_time = 0;
  70. inline bool has_oneshot_layer_timed_out() {
  71. return TIMER_DIFF_16(timer_read(), oneshot_layer_time) >= ONESHOT_TIMEOUT &&
  72. !(get_oneshot_layer_state() & ONESHOT_TOGGLED);
  73. }
  74. #endif
  75. /* Oneshot layer */
  76. void set_oneshot_layer(uint8_t layer, uint8_t state)
  77. {
  78. oneshot_layer_data = layer << 3 | state;
  79. layer_on(layer);
  80. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  81. oneshot_layer_time = timer_read();
  82. #endif
  83. }
  84. void reset_oneshot_layer(void) {
  85. oneshot_layer_data = 0;
  86. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  87. oneshot_layer_time = 0;
  88. #endif
  89. }
  90. void clear_oneshot_layer_state(oneshot_fullfillment_t state)
  91. {
  92. uint8_t start_state = oneshot_layer_data;
  93. oneshot_layer_data &= ~state;
  94. if (!get_oneshot_layer_state() && start_state != oneshot_layer_data) {
  95. layer_off(get_oneshot_layer());
  96. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  97. oneshot_layer_time = 0;
  98. #endif
  99. }
  100. }
  101. bool is_oneshot_layer_active(void)
  102. {
  103. return get_oneshot_layer_state();
  104. }
  105. #endif
  106. void send_keyboard_report(void) {
  107. keyboard_report->mods = real_mods;
  108. keyboard_report->mods |= weak_mods;
  109. keyboard_report->mods |= macro_mods;
  110. #ifndef NO_ACTION_ONESHOT
  111. if (oneshot_mods) {
  112. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  113. if (has_oneshot_mods_timed_out()) {
  114. dprintf("Oneshot: timeout\n");
  115. clear_oneshot_mods();
  116. }
  117. #endif
  118. keyboard_report->mods |= oneshot_mods;
  119. if (has_anykey(keyboard_report)) {
  120. clear_oneshot_mods();
  121. }
  122. }
  123. #endif
  124. host_keyboard_send(keyboard_report);
  125. }
  126. /* modifier */
  127. uint8_t get_mods(void) { return real_mods; }
  128. void add_mods(uint8_t mods) { real_mods |= mods; }
  129. void del_mods(uint8_t mods) { real_mods &= ~mods; }
  130. void set_mods(uint8_t mods) { real_mods = mods; }
  131. void clear_mods(void) { real_mods = 0; }
  132. /* weak modifier */
  133. uint8_t get_weak_mods(void) { return weak_mods; }
  134. void add_weak_mods(uint8_t mods) { weak_mods |= mods; }
  135. void del_weak_mods(uint8_t mods) { weak_mods &= ~mods; }
  136. void set_weak_mods(uint8_t mods) { weak_mods = mods; }
  137. void clear_weak_mods(void) { weak_mods = 0; }
  138. /* macro modifier */
  139. uint8_t get_macro_mods(void) { return macro_mods; }
  140. void add_macro_mods(uint8_t mods) { macro_mods |= mods; }
  141. void del_macro_mods(uint8_t mods) { macro_mods &= ~mods; }
  142. void set_macro_mods(uint8_t mods) { macro_mods = mods; }
  143. void clear_macro_mods(void) { macro_mods = 0; }
  144. /* Oneshot modifier */
  145. #ifndef NO_ACTION_ONESHOT
  146. void set_oneshot_mods(uint8_t mods)
  147. {
  148. oneshot_mods = mods;
  149. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  150. oneshot_time = timer_read();
  151. #endif
  152. }
  153. void clear_oneshot_mods(void)
  154. {
  155. oneshot_mods = 0;
  156. #if (defined(ONESHOT_TIMEOUT) && (ONESHOT_TIMEOUT > 0))
  157. oneshot_time = 0;
  158. #endif
  159. }
  160. uint8_t get_oneshot_mods(void)
  161. {
  162. return oneshot_mods;
  163. }
  164. #endif
  165. /*
  166. * inspect keyboard state
  167. */
  168. uint8_t has_anymod(void)
  169. {
  170. return bitpop(real_mods);
  171. }