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.

150 lines
4.3 KiB

  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_LCTRL ... KC_RGUI:
  34. case QK_MOMENTARY ... QK_MOMENTARY_MAX:
  35. case QK_LAYER_MOD ... QK_LAYER_MOD_MAX:
  36. #endif
  37. #ifdef NO_HAPTIC_FN
  38. case KC_FN0 ... KC_FN31:
  39. #endif
  40. #ifdef NO_HAPTIC_ALPHA
  41. case KC_A ... KC_Z:
  42. #endif
  43. #ifdef NO_HAPTIC_PUNCTUATION
  44. case KC_ENTER:
  45. case KC_ESCAPE:
  46. case KC_BSPACE:
  47. case KC_SPACE:
  48. case KC_MINUS:
  49. case KC_EQUAL:
  50. case KC_LBRACKET:
  51. case KC_RBRACKET:
  52. case KC_BSLASH:
  53. case KC_NONUS_HASH:
  54. case KC_SCOLON:
  55. case KC_QUOTE:
  56. case KC_GRAVE:
  57. case KC_COMMA:
  58. case KC_SLASH:
  59. case KC_DOT:
  60. case KC_NONUS_BSLASH:
  61. #endif
  62. #ifdef NO_HAPTIC_LOCKKEYS
  63. case KC_CAPSLOCK:
  64. case KC_SCROLLLOCK:
  65. case KC_NUMLOCK:
  66. #endif
  67. #ifdef NO_HAPTIC_NAV
  68. case KC_PSCREEN:
  69. case KC_PAUSE:
  70. case KC_INSERT:
  71. case KC_DELETE:
  72. case KC_PGDOWN:
  73. case KC_PGUP:
  74. case KC_LEFT:
  75. case KC_UP:
  76. case KC_RIGHT:
  77. case KC_DOWN:
  78. case KC_END:
  79. case KC_HOME:
  80. #endif
  81. #ifdef NO_HAPTIC_NUMERIC
  82. case KC_1 ... KC_0:
  83. #endif
  84. return false;
  85. }
  86. return true;
  87. }
  88. bool process_haptic(uint16_t keycode, keyrecord_t *record) {
  89. if (record->event.pressed) {
  90. switch (keycode) {
  91. case HPT_ON:
  92. haptic_enable();
  93. break;
  94. case HPT_OFF:
  95. haptic_disable();
  96. break;
  97. case HPT_TOG:
  98. haptic_toggle();
  99. break;
  100. case HPT_RST:
  101. haptic_reset();
  102. break;
  103. case HPT_FBK:
  104. haptic_feedback_toggle();
  105. break;
  106. case HPT_BUZ:
  107. haptic_buzz_toggle();
  108. break;
  109. case HPT_MODI:
  110. haptic_mode_increase();
  111. break;
  112. case HPT_MODD:
  113. haptic_mode_decrease();
  114. break;
  115. case HPT_DWLI:
  116. haptic_dwell_increase();
  117. break;
  118. case HPT_DWLD:
  119. haptic_dwell_decrease();
  120. break;
  121. case HPT_CONT:
  122. haptic_toggle_continuous();
  123. break;
  124. case HPT_CONI:
  125. haptic_cont_increase();
  126. break;
  127. case HPT_COND:
  128. haptic_cont_decrease();
  129. break;
  130. }
  131. }
  132. if (haptic_get_enable() && ((!HAPTIC_OFF_IN_LOW_POWER) || (usb_device_state == USB_DEVICE_STATE_CONFIGURED))) {
  133. if (record->event.pressed) {
  134. // keypress
  135. if (haptic_get_feedback() < 2 && get_haptic_enabled_key(keycode, record)) {
  136. haptic_play();
  137. }
  138. } else {
  139. // keyrelease
  140. if (haptic_get_feedback() > 0 && get_haptic_enabled_key(keycode, record)) {
  141. haptic_play();
  142. }
  143. }
  144. }
  145. return true;
  146. }