summaryrefslogtreecommitdiff
path: root/users/replicaJunction/features/num_word.c
diff options
context:
space:
mode:
authorJoshua T <replicaJunction@users.noreply.github.com>2021-08-13 20:14:21 -0500
committerGitHub <noreply@github.com>2021-08-13 18:14:21 -0700
commitade989962af613b24687f20f54e2dd0e37240216 (patch)
tree7e6e53b4c4151270f465ca16fc4eeb5270a7870d /users/replicaJunction/features/num_word.c
parent424b9ff7090d8407bd6bc56ff42afff05acecb12 (diff)
[Keymap] clean up userspace, add XD75 / Keyboardio Atreus (#13121)
Co-authored-by: Ryan <fauxpark@gmail.com> Co-authored-by: Drashna Jaelre <drashna@live.com>
Diffstat (limited to 'users/replicaJunction/features/num_word.c')
-rw-r--r--users/replicaJunction/features/num_word.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/users/replicaJunction/features/num_word.c b/users/replicaJunction/features/num_word.c
new file mode 100644
index 0000000000..4cca5c19ae
--- /dev/null
+++ b/users/replicaJunction/features/num_word.c
@@ -0,0 +1,129 @@
+/* Copyright 2021 Joshua T.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "num_word.h"
+
+static uint16_t num_word_timer = 0;
+static bool is_num_word_on = false;
+
+bool is_num_word_enabled(void) {
+ return is_num_word_on;
+}
+
+void enable_num_word(void) {
+ if (is_num_word_on) return;
+ is_num_word_on = true;
+ layer_on(L_NUMBERS);
+}
+
+void disable_num_word(void) {
+ if (!is_num_word_on) return;
+ is_num_word_on = false;
+ layer_off(L_NUMBERS);
+}
+
+void toggle_num_word(void) {
+ if (is_num_word_on) {
+ disable_num_word();
+ }
+ else {
+ enable_num_word();
+ }
+}
+
+bool should_terminate_num_word(uint16_t keycode, const keyrecord_t *record) {
+ switch (keycode) {
+ // Keycodes which should not disable num word mode.
+ // We could probably be more brief with these definitions by using
+ // a couple more ranges, but I believe "explicit is better than
+ // implicit"
+ case KC_1 ... KC_0:
+ case KC_EQL:
+ case KC_SCLN:
+ case KC_MINS:
+ case KC_DOT:
+
+ // Numpad keycodes
+ case KC_P1 ... KC_P0:
+ case KC_PSLS ... KC_PPLS:
+ case KC_PDOT:
+
+ // Misc
+ case KC_UNDS:
+ case KC_BSPC:
+ return false;
+
+ default:
+ if (record->event.pressed) {
+ return true;
+ }
+ return false;
+ }
+
+ // Should be unreachable
+ return false;
+}
+
+
+bool process_record_num_word(uint16_t keycode, const keyrecord_t *record) {
+ // Handle the custom keycodes that go with this feature
+ if (keycode == NUMWORD) {
+ if (record->event.pressed) {
+ enable_num_word();
+ num_word_timer = timer_read();
+ return false;
+ }
+ else {
+ if (timer_elapsed(num_word_timer) > TAPPING_TERM) {
+ // If the user held the key longer than TAPPING_TERM,
+ // consider it a hold, and disable the behavior on
+ // key release.
+ disable_num_word();
+ return false;
+ }
+ }
+ }
+
+ // Other than the custom keycodes, nothing else in this feature will
+ // activate if the behavior is not on, so allow QMK to handle the
+ // event as usual
+ if (!is_num_word_on) return true;
+
+ // Nothing else acts on key release, either
+ if (!record->event.pressed) {
+ return true;
+ }
+
+ // Get the base keycode of a mod or layer tap key
+ switch (keycode) {
+ case QK_MOD_TAP ... QK_MOD_TAP_MAX:
+ case QK_LAYER_TAP ... QK_LAYER_TAP_MAX:
+ case QK_TAP_DANCE ... QK_TAP_DANCE_MAX:
+ // Earlier return if this has not been considered tapped yet
+ if (record->tap.count == 0)
+ return true;
+ keycode = keycode & 0xFF;
+ break;
+ default:
+ break;
+ }
+
+ if (should_terminate_num_word(keycode, record)) {
+ disable_num_word();
+ }
+
+ return true;
+}