Parent directory

process_haptic.c

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