Keyboard firmwares for Atmel AVR and Cortex-M
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

layer.c 7.7KB

13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
13 jaren geleden
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. Copyright 2011 Jun Wako <[email protected]>
  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 "host.h"
  16. #include "debug.h"
  17. #include "timer.h"
  18. #include "usb_keycodes.h"
  19. #include "layer.h"
  20. /*
  21. * Parameters:
  22. * SWITCH_DELAY |=======|
  23. * SEND_FN_TERM |================|
  24. *
  25. * Fn key processing cases:
  26. * 1. release Fn after SEND_FN_TERM.
  27. * Layer sw ___________|~~~~~~~~~~~|___
  28. * Fn press ___|~~~~~~~~~~~~~~~~~~~|___
  29. * Fn send ___________________________
  30. *
  31. * 2. release Fn during SEND_FN_TERM.(not layer used)
  32. * Layer sw ___________|~~~~~~|________
  33. * Fn press ___|~~~~~~~~~~~~~~|________
  34. * Fn key send __________________|~|______
  35. * other key press ___________________________
  36. * other key send ___________________________
  37. *
  38. * 3. release Fn during SEND_FN_TERM.(layer used)
  39. * Layer sw ___________|~~~~~~|________
  40. * Fn press ___|~~~~~~~~~~~~~~|________
  41. * Fn key send ___________________________
  42. * Fn send ___________________________
  43. * other key press _____________|~~|__________
  44. * other key send _____________|~~|__________
  45. *
  46. * 4. press other key during SWITCH_DELAY.
  47. * Layer sw ___________________________
  48. * Fn key press ___|~~~~~~~~~|_____________
  49. * Fn key send ______|~~~~~~|_____________
  50. * other key press ______|~~~|________________
  51. * other key send _______|~~|________________
  52. *
  53. * 5. press Fn while press other key.
  54. * Layer sw ___________________________
  55. * Fn key press ___|~~~~~~~~~|_____________
  56. * Fn key send ___|~~~~~~~~~|_____________
  57. * other key press ~~~~~~~|___________________
  58. * other key send ~~~~~~~|___________________
  59. *
  60. * 6. press Fn twice quickly and keep holding down.(repeat)
  61. * Layer sw ___________________________
  62. * Fn key press ___|~|____|~~~~~~~~~~~~~~~~
  63. * Fn key send _____|~|__|~~~~~~~~~~~~~~~~
  64. */
  65. // LAYER_SWITCH_DELAY: prevent from moving to new layer
  66. #ifndef LAYER_SWITCH_DELAY
  67. # define LAYER_SWITCH_DELAY 150
  68. #endif
  69. // LAYER_SEND_FN_TERM: send keycode if release key in this term
  70. #ifndef LAYER_SEND_FN_TERM
  71. # define LAYER_SEND_FN_TERM 500
  72. #endif
  73. uint8_t default_layer = 0;
  74. uint8_t current_layer = 0;
  75. static bool layer_used = false;
  76. static uint8_t new_layer(uint8_t fn_bits);
  77. uint8_t layer_get_keycode(uint8_t row, uint8_t col)
  78. {
  79. uint8_t code = keymap_get_keycode(current_layer, row, col);
  80. // normal key or mouse key
  81. if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
  82. layer_used = true;
  83. }
  84. return code;
  85. }
  86. // bit substract b from a
  87. #define BIT_SUBST(a, b) (a&(a^b))
  88. void layer_switching(uint8_t fn_bits)
  89. {
  90. // layer switching
  91. static uint8_t last_fn = 0;
  92. static uint8_t last_mods = 0;
  93. static uint16_t last_timer = 0;
  94. static uint8_t sent_fn = 0;
  95. if (fn_bits == last_fn) { // Fn state is not changed
  96. if (fn_bits == 0) {
  97. // do nothing
  98. } else {
  99. if (!keymap_fn_keycode(BIT_SUBST(fn_bits, sent_fn)) ||
  100. timer_elapsed(last_timer) > LAYER_SWITCH_DELAY) {
  101. uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn));
  102. if (current_layer != _layer_to_switch) { // not switch layer yet
  103. debug("Fn case: 1,2,3(LAYER_SWITCH_DELAY passed)\n");
  104. debug("Switch Layer: "); debug_hex(current_layer);
  105. current_layer = _layer_to_switch;
  106. layer_used = false;
  107. debug(" -> "); debug_hex(current_layer); debug("\n");
  108. }
  109. } else {
  110. if (host_has_anykey()) { // other keys is pressed
  111. uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn);
  112. if (_fn_to_send) {
  113. debug("Fn case: 4(press other key during SWITCH_DELAY.)\n");
  114. // send only Fn key first
  115. uint8_t tmp_mods = keyboard_report->mods;
  116. host_add_code(keymap_fn_keycode(_fn_to_send));
  117. host_set_mods(last_mods);
  118. host_send_keyboard_report();
  119. host_set_mods(tmp_mods);
  120. host_del_code(keymap_fn_keycode(_fn_to_send));
  121. sent_fn |= _fn_to_send;
  122. }
  123. }
  124. }
  125. // add Fn keys to send
  126. //host_add_code(keymap_fn_keycode(fn_bits&sent_fn)); // TODO: do all Fn keys
  127. }
  128. } else { // Fn state is changed(edge)
  129. uint8_t fn_changed = 0;
  130. debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
  131. debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
  132. debug("last_fn: "); debug_bin(last_fn); debug("\n");
  133. debug("last_mods: "); debug_hex(last_mods); debug("\n");
  134. debug("last_timer: "); debug_hex16(last_timer); debug("\n");
  135. debug("timer_count: "); debug_hex16(timer_count); debug("\n");
  136. // pressed Fn
  137. if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) {
  138. debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
  139. if (host_has_anykey()) {
  140. debug("Fn case: 5(pressed Fn with other key)\n");
  141. sent_fn |= fn_changed;
  142. } else if (fn_changed & sent_fn) { // pressed same Fn in a row
  143. if (timer_elapsed(last_timer) > LAYER_SEND_FN_TERM) {
  144. debug("Fn case: 6(not repeat)\n");
  145. // time passed: not repeate
  146. sent_fn &= ~fn_changed;
  147. } else {
  148. debug("Fn case: 6(repeat)\n");
  149. }
  150. }
  151. }
  152. // released Fn
  153. if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) {
  154. debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
  155. if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
  156. if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) {
  157. debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
  158. // send only Fn key first
  159. uint8_t tmp_mods = keyboard_report->mods;
  160. host_add_code(keymap_fn_keycode(fn_changed));
  161. host_set_mods(last_mods);
  162. host_send_keyboard_report();
  163. host_set_mods(tmp_mods);
  164. host_del_code(keymap_fn_keycode(fn_changed));
  165. sent_fn |= fn_changed;
  166. }
  167. }
  168. debug("Switch Layer(released Fn): "); debug_hex(current_layer);
  169. current_layer = new_layer(BIT_SUBST(fn_bits, sent_fn));
  170. debug(" -> "); debug_hex(current_layer); debug("\n");
  171. }
  172. layer_used = false;
  173. last_fn = fn_bits;
  174. last_mods = keyboard_report->mods;
  175. last_timer = timer_read();
  176. }
  177. // send Fn keys
  178. for (uint8_t i = 0; i < 8; i++) {
  179. if ((sent_fn & fn_bits) & (1<<i)) {
  180. host_add_code(keymap_fn_keycode(1<<i));
  181. }
  182. }
  183. }
  184. inline
  185. static uint8_t new_layer(uint8_t fn_bits)
  186. {
  187. return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
  188. }