forked from Ikcelaks/qmk_sequence_transform
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
116 lines (112 loc) · 4.72 KB
/
utils.c
File metadata and controls
116 lines (112 loc) · 4.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2021 Google LLC
// Copyright 2021 @filterpaper
// Copyright 2023 Pablo Martinez (@elpekenin) <elpekenin@elpekenin.dev>
// Copyright 2024 Guillaume Stordeur <guillaume.stordeur@gmail.com>
// Copyright 2024 Matt Skalecki <ikcelaks@gmail.com>
// Copyright 2024 QKekos <q.kekos.q@gmail.com>
// SPDX-License-Identifier: Apache-2.0
// Original source/inspiration: https://getreuer.info/posts/keyboards/autocorrection
#include "qmk_wrapper.h"
#include "utils.h"
#include "sequence_transform_data.h"
// Note: we bit-pack in "reverse" order to optimize loading
#define PGM_LOADBIT(mem, pos) ((pgm_read_byte(&((mem)[(pos) / 8])) >> ((pos) % 8)) & 0x01)
static const char unshifted_keycode_to_ascii_lut[53] PROGMEM = {
// KC_A KC_B KC_C KC_D
'a', 'b', 'c', 'd',
// KC_E KC_F KC_G KC_H KC_I KC_J KC_K KC_L
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
// KC_M KC_N KC_O KC_P KC_Q KC_R KC_S KC_T
'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
// KC_U KC_V KC_W KC_X KC_Y KC_Z KC_1 KC_2
'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
// KC_3 KC_4 KC_5 KC_6 KC_7 KC_8 KC_9 KC_0
'3', '4', '5', '6', '7', '8', '9', '0',
// KC_ENTR KC_ESC KC_BSPC KC_TAB KC_SPC KC_MINS KC_EQL KC_LBRC
' ', ' ', ' ', ' ', ' ', '-', '=', '[',
// KC_RBRC KC_BSLS KC_NUHS KC_SCLN KC_QUOT KC_GRV KC_COMM KC_DOT
']', '\\', ' ', ';', '\'', '`', ',', '.',
// KC_SLSH
'/'
};
static const char shifted_keycode_to_ascii_lut[53] PROGMEM = {
// KC_A KC_B KC_C KC_D
'A', 'B', 'C', 'D',
// KC_E KC_F KC_G KC_H KC_I KC_J KC_K KC_L
'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
// KC_M KC_N KC_O KC_P KC_Q KC_R KC_S KC_T
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
// KC_U KC_V KC_W KC_X KC_Y KC_Z KC_EXLM KC_AT
'U', 'V', 'W', 'X', 'Y', 'Z', '!', '@',
// KC_HASH KC_DLR KC_PERC KC_CIRC KC_AMPR KC_ASTR KC_LPRN KC_RPRN
'#', '$', '%', '^', '&', '*', '(', ')',
// KC_ENTR KC_ESC KC_BSPC KC_TAB KC_SPC KC_UNDS KC_PLUS KC_LCBR
' ', ' ', ' ', ' ', ' ', '_', '+', '{',
// KC_RCBR KC_PIPE KC_NUHS KC_COLN KC_DQUO KC_GRV KC_LABK KC_RABK
'}', '|', ' ', ':', '"', '~', '<', '>',
// KC_QUES
'?'
};
//////////////////////////////////////////////////////////////////////
bool st_is_seq_token_keycode(uint16_t key)
{
return (key >= SPECIAL_KEY_TRIECODE_0 &&
key < SPECIAL_KEY_TRIECODE_0 + SEQUENCE_TRANSFORM_COUNT);
}
////////////////////////////////////////////////////////////////////////////////
char st_keycode_to_char(uint16_t keycode)
{
if (st_is_seq_token_keycode(keycode)) {
return st_seq_tokens_ascii[keycode - SPECIAL_KEY_TRIECODE_0];
} else if (keycode == KC_SPACE) {
return st_wordbreak_ascii;
}
const bool shifted = keycode & QK_LSFT;
keycode &= 0xFF;
if (keycode >= KC_A && keycode <= KC_SLASH) {
keycode -= KC_A;
return shifted ? pgm_read_byte(&shifted_keycode_to_ascii_lut[keycode]) :
pgm_read_byte(&unshifted_keycode_to_ascii_lut[keycode]);
}
return '?';
}
////////////////////////////////////////////////////////////////////////////////
uint16_t st_char_to_keycode(char c)
{
uint16_t k = pgm_read_byte(&ascii_to_keycode_lut[(uint8_t)c]);
bool is_shifted = PGM_LOADBIT(ascii_to_shift_lut, (uint8_t)c);
if (is_shifted)
k = S(k);
return k;
}
////////////////////////////////////////////////////////////////////////////////
void st_multi_tap(uint16_t keycode, int count)
{
for (int i = 0; i < count; ++i)
tap_code16(keycode);
}
//////////////////////////////////////////////////////////////////////////////////////////
void st_send_key(uint16_t keycode)
{
// Apply shift to sent key if caps word is enabled.
#ifdef CAPS_WORD_ENABLE
if (is_caps_word_on() && IS_ALPHA_KEYCODE(keycode))
add_weak_mods(MOD_BIT(KC_LSFT));
#endif
tap_code16(keycode);
}
//////////////////////////////////////////////////////////////////////
int st_max(int a, int b)
{
return (a > b ? a : b);
}
//////////////////////////////////////////////////////////////////////
int st_min(int a, int b)
{
return (a < b ? a : b);
}
//////////////////////////////////////////////////////////////////////
int st_clamp(int val, int min_val, int max_val)
{
return (val < min_val ? min_val : (val > max_val ? max_val : val));
}