keyboard stuff
1// Copyright 2022-2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "process_repeat_key.h"
16#include "repeat_key.h"
17#include "keycodes.h"
18#include "quantum_keycodes.h"
19#include "action_util.h"
20
21// Default implementation of remember_last_key_user().
22__attribute__((weak)) bool remember_last_key_user(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
23 return true;
24}
25
26static bool remember_last_key(uint16_t keycode, keyrecord_t* record, uint8_t* remembered_mods) {
27 switch (keycode) {
28 // Ignore MO, TO, TG, TT, and TL layer switch keys.
29 case QK_MOMENTARY ... QK_MOMENTARY_MAX:
30 case QK_TO ... QK_TO_MAX:
31 case QK_TOGGLE_LAYER ... QK_TOGGLE_LAYER_MAX:
32 case QK_LAYER_TAP_TOGGLE ... QK_LAYER_TAP_TOGGLE_MAX:
33 // Ignore mod keys.
34 case KC_LCTL ... KC_RGUI:
35 case KC_HYPR:
36 case KC_MEH:
37#ifndef NO_ACTION_ONESHOT // Ignore one-shot keys.
38 case QK_ONE_SHOT_LAYER ... QK_ONE_SHOT_LAYER_MAX:
39 case QK_ONE_SHOT_MOD ... QK_ONE_SHOT_MOD_MAX:
40#endif // NO_ACTION_ONESHOT
41#ifdef TRI_LAYER_ENABLE // Ignore Tri Layer keys.
42 case QK_TRI_LAYER_LOWER:
43 case QK_TRI_LAYER_UPPER:
44#endif // TRI_LAYER_ENABLE
45#ifdef LAYER_LOCK_ENABLE // Ignore Layer Lock key.
46 case QK_LAYER_LOCK:
47#endif // LAYER_LOCK_ENABLE
48 return false;
49
50 // Ignore hold events on tap-hold keys.
51#ifndef NO_ACTION_TAPPING
52 case QK_MOD_TAP ... QK_MOD_TAP_MAX:
53# ifndef NO_ACTION_LAYER
54 case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
55# endif // NO_ACTION_LAYER
56 if (record->tap.count == 0) {
57 return false;
58 }
59 break;
60#endif // NO_ACTION_TAPPING
61
62#ifdef SWAP_HANDS_ENABLE
63 case QK_SWAP_HANDS ... QK_SWAP_HANDS_MAX:
64 if (IS_SWAP_HANDS_KEYCODE(keycode) || record->tap.count == 0) {
65 return false;
66 }
67 break;
68#endif // SWAP_HANDS_ENABLE
69
70 case QK_REPEAT_KEY:
71#ifndef NO_ALT_REPEAT_KEY
72 case QK_ALT_REPEAT_KEY:
73#endif // NO_ALT_REPEAT_KEY
74 return false;
75 }
76
77 return remember_last_key_user(keycode, record, remembered_mods);
78}
79
80bool process_last_key(uint16_t keycode, keyrecord_t* record) {
81 if (get_repeat_key_count()) {
82 return true;
83 }
84
85 if (record->event.pressed) {
86 uint8_t remembered_mods = get_mods() | get_weak_mods();
87#ifndef NO_ACTION_ONESHOT
88 remembered_mods |= get_oneshot_mods();
89#endif // NO_ACTION_ONESHOT
90
91 if (remember_last_key(keycode, record, &remembered_mods)) {
92 set_last_record(keycode, record);
93 set_last_mods(remembered_mods);
94 }
95 }
96
97 return true;
98}
99
100bool process_repeat_key(uint16_t keycode, keyrecord_t* record) {
101 if (get_repeat_key_count()) {
102 return true;
103 }
104
105 if (keycode == QK_REPEAT_KEY) {
106 repeat_key_invoke(&record->event);
107 return false;
108#ifndef NO_ALT_REPEAT_KEY
109 } else if (keycode == QK_ALT_REPEAT_KEY) {
110 alt_repeat_key_invoke(&record->event);
111 return false;
112#endif // NO_ALT_REPEAT_KEY
113 }
114
115 return true;
116}