aboutsummaryrefslogtreecommitdiff
path: root/users/drashna/pointing/pointing.c
diff options
context:
space:
mode:
Diffstat (limited to 'users/drashna/pointing/pointing.c')
-rw-r--r--users/drashna/pointing/pointing.c136
1 files changed, 136 insertions, 0 deletions
diff --git a/users/drashna/pointing/pointing.c b/users/drashna/pointing/pointing.c
new file mode 100644
index 000000000..0198a8ae2
--- /dev/null
+++ b/users/drashna/pointing/pointing.c
@@ -0,0 +1,136 @@
1/* Copyright 2020 Christopher Courtney, aka Drashna Jael're (@drashna) <drashna@live.com>
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
17#include "pointing.h"
18
19static uint16_t mouse_timer = 0;
20static uint16_t mouse_debounce_timer = 0;
21static uint8_t mouse_keycode_tracker = 0;
22bool tap_toggling = false, enable_acceleration = false;
23
24#ifdef TAPPING_TERM_PER_KEY
25# define TAP_CHECK get_tapping_term(KC_BTN1, NULL)
26#else
27# ifndef TAPPING_TERM
28# define TAPPING_TERM 200
29# endif
30# define TAP_CHECK TAPPING_TERM
31#endif
32
33__attribute__((weak)) report_mouse_t pointing_device_task_keymap(report_mouse_t mouse_report) {
34 return mouse_report;
35}
36
37report_mouse_t pointing_device_task_user(report_mouse_t mouse_report) {
38 int8_t x = mouse_report.x, y = mouse_report.y;
39 mouse_report.x = 0;
40 mouse_report.y = 0;
41
42 if (x != 0 && y != 0) {
43 mouse_timer = timer_read();
44#ifdef OLED_ENABLE
45 oled_timer = timer_read32();
46#endif
47 if (timer_elapsed(mouse_debounce_timer) > TAP_CHECK) {
48 if (enable_acceleration) {
49 x = (x > 0 ? x * x / 16 + x : -x * x / 16 + x);
50 y = (y > 0 ? y * y / 16 + y : -y * y / 16 + y);
51 }
52 mouse_report.x = x;
53 mouse_report.y = y;
54 if (!layer_state_is(_MOUSE)) {
55 layer_on(_MOUSE);
56 }
57 }
58 }
59 return pointing_device_task_keymap(mouse_report);
60}
61
62void matrix_scan_pointing(void) {
63 if (timer_elapsed(mouse_timer) > 650 && layer_state_is(_MOUSE) && !mouse_keycode_tracker && !tap_toggling) {
64 layer_off(_MOUSE);
65 }
66 if (tap_toggling) {
67 if (!layer_state_is(_MOUSE)) {
68 layer_on(_MOUSE);
69 }
70 }
71}
72
73bool process_record_pointing(uint16_t keycode, keyrecord_t* record) {
74 switch (keycode) {
75 case TT(_MOUSE):
76 if (record->event.pressed) {
77 mouse_keycode_tracker++;
78 } else {
79#if TAPPING_TOGGLE != 0
80 if (record->tap.count == TAPPING_TOGGLE) {
81 tap_toggling ^= 1;
82# if TAPPING_TOGGLE == 1
83 if (!tap_toggling) mouse_keycode_tracker -= record->tap.count + 1;
84# else
85 if (!tap_toggling) mouse_keycode_tracker -= record->tap.count;
86# endif
87 } else {
88 mouse_keycode_tracker--;
89 }
90#endif
91 }
92 mouse_timer = timer_read();
93 break;
94 case TG(_MOUSE):
95 if (record->event.pressed) {
96 tap_toggling ^= 1;
97 }
98 break;
99 case MO(_MOUSE):
100#if defined(KEYBOARD_ploopy) || defined(KEYBOARD_handwired_tractyl_manuform)
101 case DPI_CONFIG:
102#elif defined(KEYBOARD_bastardkb_charybdis)
103 case SAFE_RANGE ... (CHARYBDIS_SAFE_RANGE-1):
104#endif
105 case KC_MS_UP ... KC_MS_WH_RIGHT:
106 record->event.pressed ? mouse_keycode_tracker++ : mouse_keycode_tracker--;
107 mouse_timer = timer_read();
108 break;
109 case KC_ACCEL:
110 enable_acceleration = record->event.pressed;
111 record->event.pressed ? mouse_keycode_tracker++ : mouse_keycode_tracker--;
112 mouse_timer = timer_read();
113 break;
114 default:
115 if (IS_NOEVENT(record->event)) break;
116 if ((keycode >= QK_LAYER_TAP && keycode <= QK_LAYER_TAP_MAX) && (((keycode >> 0x8) & 0xF) == _MOUSE)) {
117 record->event.pressed ? mouse_keycode_tracker++ : mouse_keycode_tracker--;
118 mouse_timer = timer_read();
119 break;
120 }
121 if (layer_state_is(_MOUSE) && !mouse_keycode_tracker) {
122 layer_off(_MOUSE);
123 }
124 mouse_keycode_tracker = 0;
125 mouse_debounce_timer = timer_read();
126 break;
127 }
128 return true;
129}
130
131layer_state_t layer_state_set_pointing(layer_state_t state) {
132 if (layer_state_cmp(state, _GAMEPAD) || layer_state_cmp(state, _DIABLO)) {
133 state |= ((layer_state_t)1 << _MOUSE);
134 }
135 return state;
136}