process_haptic.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #include "usb_device_state.h"
  21. __attribute__((weak)) bool get_haptic_enabled_key(uint16_t keycode, keyrecord_t *record) {
  22. switch (keycode) {
  23. #ifdef NO_HAPTIC_MOD
  24. case QK_MOD_TAP ... QK_MOD_TAP_MAX:
  25. if (record->tap.count == 0) return false;
  26. break;
  27. case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
  28. if (record->tap.count != TAPPING_TOGGLE) return false;
  29. break;
  30. case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
  31. if (record->tap.count == 0) return false;
  32. break;
  33. case KC_LEFT_CTRL ... KC_RIGHT_GUI:
  34. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  35. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
  36. #endif
  37. #ifdef NO_HAPTIC_ALPHA
  38. case KC_A ... KC_Z:
  39. #endif
  40. #ifdef NO_HAPTIC_PUNCTUATION
  41. case KC_ENTER:
  42. case KC_ESCAPE:
  43. case KC_BACKSPACE:
  44. case KC_SPACE:
  45. case KC_MINUS:
  46. case KC_EQUAL:
  47. case KC_LEFT_BRACKET:
  48. case KC_RIGHT_BRACKET:
  49. case KC_BACKSLASH:
  50. case KC_NONUS_HASH:
  51. case KC_SEMICOLON:
  52. case KC_QUOTE:
  53. case KC_GRAVE:
  54. case KC_COMMA:
  55. case KC_SLASH:
  56. case KC_DOT:
  57. case KC_NONUS_BACKSLASH:
  58. #endif
  59. #ifdef NO_HAPTIC_LOCKKEYS
  60. case KC_CAPS_LOCK:
  61. case KC_SCROLL_LOCK:
  62. case KC_NUM_LOCK:
  63. #endif
  64. #ifdef NO_HAPTIC_NAV
  65. case KC_PRINT_SCREEN:
  66. case KC_PAUSE:
  67. case KC_INSERT:
  68. case KC_DELETE:
  69. case KC_PAGE_DOWN:
  70. case KC_PAGE_UP:
  71. case KC_LEFT:
  72. case KC_UP:
  73. case KC_RIGHT:
  74. case KC_DOWN:
  75. case KC_END:
  76. case KC_HOME:
  77. #endif
  78. #ifdef NO_HAPTIC_NUMERIC
  79. case KC_1 ... KC_0:
  80. #endif
  81. return false;
  82. }
  83. return true;
  84. }
  85. bool process_haptic(uint16_t keycode, keyrecord_t *record) {
  86. if (record->event.pressed) {
  87. switch (keycode) {
  88. case QK_HAPTIC_ON:
  89. haptic_enable();
  90. break;
  91. case QK_HAPTIC_OFF:
  92. haptic_disable();
  93. break;
  94. case QK_HAPTIC_TOGGLE:
  95. haptic_toggle();
  96. break;
  97. case QK_HAPTIC_RESET:
  98. haptic_reset();
  99. break;
  100. case QK_HAPTIC_FEEDBACK_TOGGLE:
  101. haptic_feedback_toggle();
  102. break;
  103. case QK_HAPTIC_BUZZ_TOGGLE:
  104. haptic_buzz_toggle();
  105. break;
  106. case QK_HAPTIC_MODE_NEXT:
  107. haptic_mode_increase();
  108. break;
  109. case QK_HAPTIC_MODE_PREVIOUS:
  110. haptic_mode_decrease();
  111. break;
  112. case QK_HAPTIC_DWELL_UP:
  113. haptic_dwell_increase();
  114. break;
  115. case QK_HAPTIC_DWELL_DOWN:
  116. haptic_dwell_decrease();
  117. break;
  118. case QK_HAPTIC_CONTINUOUS_TOGGLE:
  119. haptic_toggle_continuous();
  120. break;
  121. case QK_HAPTIC_CONTINUOUS_UP:
  122. haptic_cont_increase();
  123. break;
  124. case QK_HAPTIC_CONTINUOUS_DOWN:
  125. haptic_cont_decrease();
  126. break;
  127. }
  128. }
  129. if (haptic_get_enable() && ((!HAPTIC_OFF_IN_LOW_POWER) || (usb_device_state == USB_DEVICE_STATE_CONFIGURED))) {
  130. if (record->event.pressed) {
  131. // keypress
  132. if (haptic_get_feedback() < 2 && get_haptic_enabled_key(keycode, record)) {
  133. haptic_play();
  134. }
  135. } else {
  136. // keyrelease
  137. if (haptic_get_feedback() > 0 && get_haptic_enabled_key(keycode, record)) {
  138. haptic_play();
  139. }
  140. }
  141. }
  142. return true;
  143. }