process_haptic.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright 2021 QMK
  2. *
  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. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "haptic.h"
  17. #include "process_haptic.h"
  18. #include "quantum_keycodes.h"
  19. #include "action_tapping.h"
  20. __attribute__((weak)) bool get_haptic_enabled_key(uint16_t keycode, keyrecord_t *record) {
  21. switch (keycode) {
  22. #ifdef NO_HAPTIC_MOD
  23. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  24. if (record->tap.count == 0) return false;
  25. break;
  26. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  27. if (record->tap.count != TAPPING_TOGGLE) return false;
  28. break;
  29. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  30. if (record->tap.count == 0) return false;
  31. break;
  32. case KC_LCTRL ... KC_RGUI:
  33. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  34. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
  35. #endif
  36. #ifdef NO_HAPTIC_FN
  37. case KC_FN0 ... KC_FN31:
  38. #endif
  39. #ifdef NO_HAPTIC_ALPHA
  40. case KC_A ... KC_Z:
  41. #endif
  42. #ifdef NO_HAPTIC_PUNCTUATION
  43. case KC_ENTER:
  44. case KC_ESCAPE:
  45. case KC_BSPACE:
  46. case KC_SPACE:
  47. case KC_MINUS:
  48. case KC_EQUAL:
  49. case KC_LBRACKET:
  50. case KC_RBRACKET:
  51. case KC_BSLASH:
  52. case KC_NONUS_HASH:
  53. case KC_SCOLON:
  54. case KC_QUOTE:
  55. case KC_GRAVE:
  56. case KC_COMMA:
  57. case KC_SLASH:
  58. case KC_DOT:
  59. case KC_NONUS_BSLASH:
  60. #endif
  61. #ifdef NO_HAPTIC_LOCKKEYS
  62. case KC_CAPSLOCK:
  63. case KC_SCROLLLOCK:
  64. case KC_NUMLOCK:
  65. #endif
  66. #ifdef NO_HAPTIC_NAV
  67. case KC_PSCREEN:
  68. case KC_PAUSE:
  69. case KC_INSERT:
  70. case KC_DELETE:
  71. case KC_PGDOWN:
  72. case KC_PGUP:
  73. case KC_LEFT:
  74. case KC_UP:
  75. case KC_RIGHT:
  76. case KC_DOWN:
  77. case KC_END:
  78. case KC_HOME:
  79. #endif
  80. #ifdef NO_HAPTIC_NUMERIC
  81. case KC_1 ... KC_0:
  82. #endif
  83. return false;
  84. }
  85. return true;
  86. }
  87. bool process_haptic(uint16_t keycode, keyrecord_t *record) {
  88. if (record->event.pressed) {
  89. switch (keycode) {
  90. case HPT_ON:
  91. haptic_enable();
  92. break;
  93. case HPT_OFF:
  94. haptic_disable();
  95. break;
  96. case HPT_TOG:
  97. haptic_toggle();
  98. break;
  99. case HPT_RST:
  100. haptic_reset();
  101. break;
  102. case HPT_FBK:
  103. haptic_feedback_toggle();
  104. break;
  105. case HPT_BUZ:
  106. haptic_buzz_toggle();
  107. break;
  108. case HPT_MODI:
  109. haptic_mode_increase();
  110. break;
  111. case HPT_MODD:
  112. haptic_mode_decrease();
  113. break;
  114. case HPT_DWLI:
  115. haptic_dwell_increase();
  116. break;
  117. case HPT_DWLD:
  118. haptic_dwell_decrease();
  119. break;
  120. case HPT_CONT:
  121. haptic_toggle_continuous();
  122. break;
  123. case HPT_CONI:
  124. haptic_cont_increase();
  125. break;
  126. case HPT_COND:
  127. haptic_cont_decrease();
  128. break;
  129. }
  130. }
  131. if (haptic_get_enable()) {
  132. if (record->event.pressed) {
  133. // keypress
  134. if (haptic_get_feedback() < 2 && get_haptic_enabled_key(keycode, record)) {
  135. haptic_play();
  136. }
  137. } else {
  138. // keyrelease
  139. if (haptic_get_feedback() > 0 && get_haptic_enabled_key(keycode, record)) {
  140. haptic_play();
  141. }
  142. }
  143. }
  144. return true;
  145. }