From e4c5b1bbbb3d76a7632c8a9d5c19c5eb8a9a4e1f Mon Sep 17 00:00:00 2001
From: Roland Huber <36362570+darkcruix@users.noreply.github.com>
Date: Sun, 20 Jun 2021 04:28:54 +0200
Subject: [PATCH] Add Per Key exclusions for Haptic Feedback (#12386)

Co-authored-by: Drashna Jaelre <drashna@live.com>
---
 docs/feature_haptic_feedback.md | 26 +++++++++++-
 drivers/haptic/haptic.c         | 71 ++++++++++++++++++++++++++++++++-
 2 files changed, 94 insertions(+), 3 deletions(-)

diff --git a/docs/feature_haptic_feedback.md b/docs/feature_haptic_feedback.md
index a092e784c7..469c9c7981 100644
--- a/docs/feature_haptic_feedback.md
+++ b/docs/feature_haptic_feedback.md
@@ -162,4 +162,28 @@ This will set what sequence HPT_RST will set as the active mode. If not defined,
 
 ### DRV2605L Continuous Haptic Mode
 
-This mode sets continuous haptic feedback with the option to increase or decrease strength. 
+This mode sets continuous haptic feedback with the option to increase or decrease strength.
+
+## Haptic Key Exclusion
+The Haptic Exclusion is implemented as `__attribute__((weak)) bool get_haptic_enabled_key(uint16_t keycode, keyrecord_t *record)` in haptic.c. This allows a re-definition at the required level with the specific requirement / exclusion.
+
+### NO_HAPTIC_MOD
+With the entry of `#define NO_HAPTIC_MOD` in config.h, modifiers from Left Control to Right GUI will not trigger a feedback. This also includes modifiers in a Mod Tap configuration.
+
+### NO_HAPTIC_FN
+With the entry of `#define NO_HAPTIC_FN` in config.h, layer keys will not rigger a feedback.
+
+### NO_HAPTIC_ALPHA
+With the entry of `#define NO_HAPTIC_ALPHA` in config.h, none of the alpha keys (A ... Z) will trigger a feedback.
+
+### NO_HAPTIC_PUNCTUATION
+With the entry of `#define NO_HAPTIC_PUNCTUATION` in config.h, none of the following keys will trigger a feedback: Enter, ESC, Backspace, Space, Minus, Equal, Left Bracket, Right Bracket, Backslash, Non-US Hash, Semicolon, Quote, Grave, Comma, Slash, Dot, Non-US Backslash.
+
+### NO_HAPTIC_LOCKKEYS
+With the entry of `#define NO_HAPTIC_LOCKKEYS` in config.h, none of the following keys will trigger a feedback: Caps Lock, Scroll Lock, Num Lock.
+
+### NO_HAPTIC_NAV
+With the entry of `#define NO_HAPTIC_NAV` in config.h, none of the following keys will trigger a feedback: Print Screen, Pause, Insert, Delete, Page Down, Page Up, Left Arrow, Up Arrow, Right Arrow, Down Arrow, End, Home.
+
+### NO_HAPTIC_NUMERIC
+With the entry of `#define NO_HAPTIC_NUMERIC` in config.h, none of the following keys between 0 and 9 (KC_1 ... KC_0) will trigger a feedback.
\ No newline at end of file
diff --git a/drivers/haptic/haptic.c b/drivers/haptic/haptic.c
index de3f400527..3fab1be1ae 100644
--- a/drivers/haptic/haptic.c
+++ b/drivers/haptic/haptic.c
@@ -291,6 +291,73 @@ void haptic_play(void) {
 #endif
 }
 
+__attribute__((weak)) bool get_haptic_enabled_key(uint16_t keycode, keyrecord_t *record) {
+    switch(keycode) {
+#    ifdef NO_HAPTIC_MOD
+        case QK_MOD_TAP ... QK_MOD_TAP_MAX:
+            if (record->tap.count == 0) return false;
+            break;
+        case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
+            if (record->tap.count != TAPPING_TOGGLE) return false;
+            break;
+        case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
+            if (record->tap.count == 0) return false;
+            break;
+        case KC_LCTRL ... KC_RGUI:
+        case QK_MOMENTARY ... QK_MOMENTARY_MAX:
+#    endif
+#    ifdef NO_HAPTIC_FN
+        case KC_FN0 ... KC_FN31:
+#    endif
+#    ifdef NO_HAPTIC_ALPHA
+        case KC_A ... KC_Z:
+#    endif
+#    ifdef NO_HAPTIC_PUNCTUATION
+        case KC_ENTER:
+        case KC_ESCAPE:
+        case KC_BSPACE:
+        case KC_SPACE:
+        case KC_MINUS:
+        case KC_EQUAL:
+        case KC_LBRACKET:
+        case KC_RBRACKET:
+        case KC_BSLASH:
+        case KC_NONUS_HASH:
+        case KC_SCOLON:
+        case KC_QUOTE:
+        case KC_GRAVE:
+        case KC_COMMA:
+        case KC_SLASH:
+        case KC_DOT:
+        case KC_NONUS_BSLASH:
+#    endif
+#    ifdef NO_HAPTIC_LOCKKEYS
+        case KC_CAPSLOCK:
+        case KC_SCROLLLOCK:
+        case KC_NUMLOCK:
+#    endif
+#    ifdef NO_HAPTIC_NAV
+        case KC_PSCREEN:
+        case KC_PAUSE:
+        case KC_INSERT:
+        case KC_DELETE:
+        case KC_PGDOWN:
+        case KC_PGUP:
+        case KC_LEFT:
+        case KC_UP:
+        case KC_RIGHT:
+        case KC_DOWN:
+        case KC_END:
+        case KC_HOME:
+#    endif
+#    ifdef NO_HAPTIC_NUMERIC
+        case KC_1 ... KC_0:
+#    endif
+         return false;
+    }
+    return true;
+}
+
 bool process_haptic(uint16_t keycode, keyrecord_t *record) {
     if (keycode == HPT_ON && record->event.pressed) {
         haptic_enable();
@@ -335,12 +402,12 @@ bool process_haptic(uint16_t keycode, keyrecord_t *record) {
     if (haptic_config.enable) {
         if (record->event.pressed) {
             // keypress
-            if (haptic_config.feedback < 2) {
+            if (haptic_config.feedback < 2 && get_haptic_enabled_key(keycode, record)) {
                 haptic_play();
             }
         } else {
             // keyrelease
-            if (haptic_config.feedback > 0) {
+            if (haptic_config.feedback > 0 && get_haptic_enabled_key(keycode, record)) {
                 haptic_play();
             }
         }