-/* default layer indicates base layer */
-uint8_t default_layer = 0;
-/* current layer indicates active layer at this time */
-uint8_t current_layer = 0;
-
-
-static void process_action(keyrecord_t *record);
-static bool process_tapping(keyrecord_t *record);
-static void waiting_buffer_scan_tap(void);
-
-static void debug_event(keyevent_t event);
-static void debug_record(keyrecord_t record);
-static void debug_action(action_t action);
-static void debug_tapping_key(void);
-static void debug_waiting_buffer(void);
-
-
-/*
- * Tapping
- */
-/* period of tapping(ms) */
-#ifndef TAPPING_TERM
-#define TAPPING_TERM 200
-#endif
-
-/* tap count needed for toggling a feature */
-#ifndef TAPPING_TOGGLE
-#define TAPPING_TOGGLE 5
-#endif
-
-/* stores a key event of current tap. */
-static keyrecord_t tapping_key = {};
-
-#define IS_TAPPING() !IS_NOEVENT(tapping_key.event)
-#define IS_TAPPING_PRESSED() (IS_TAPPING() && tapping_key.event.pressed)
-#define IS_TAPPING_RELEASED() (IS_TAPPING() && !tapping_key.event.pressed)
-#define IS_TAPPING_KEY(k) (IS_TAPPING() && KEYEQ(tapping_key.event.key, (k)))
-#define WITHIN_TAPPING_TERM(e) (TIMER_DIFF_16(e.time, tapping_key.event.time) < TAPPING_TERM)
-
-
-/*
- * Waiting buffer
- *
- * stores key events waiting for settling current tap.
- */
-#define WAITING_BUFFER_SIZE 8
-static keyrecord_t waiting_buffer[WAITING_BUFFER_SIZE] = {};
-
-/* point to empty cell to enq */
-static uint8_t waiting_buffer_head = 0;
-
-/* point to the oldest data cell to deq */
-static uint8_t waiting_buffer_tail = 0;
-
-static bool waiting_buffer_enq(keyrecord_t record)
-{
- if (IS_NOEVENT(record.event)) {
- return true;
- }
-
- if ((waiting_buffer_head + 1) % WAITING_BUFFER_SIZE == waiting_buffer_tail) {
- debug("waiting_buffer_enq: Over flow.\n");
- return false;
- }
-
- waiting_buffer[waiting_buffer_head] = record;
- waiting_buffer_head = (waiting_buffer_head + 1) % WAITING_BUFFER_SIZE;
-
- debug("waiting_buffer_enq: "); debug_waiting_buffer();
- return true;
-}
-
-static void waiting_buffer_clear(void)
-{
- waiting_buffer_head = 0;
- waiting_buffer_tail = 0;
-}
-
-#if TAPPING_TERM >= 500
-static bool waiting_buffer_typed(keyevent_t event)
-{
- for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
- if (KEYEQ(event.key, waiting_buffer[i].event.key) && event.pressed != waiting_buffer[i].event.pressed) {
- return true;
- }
- }
- return false;
-}
-#endif
-
-bool waiting_buffer_has_anykey_pressed(void)
-{
- for (uint8_t i = waiting_buffer_tail; i != waiting_buffer_head; i = (i + 1) % WAITING_BUFFER_SIZE) {
- if (waiting_buffer[i].event.pressed) return true;
- }
- return false;
-}
-
-
-/* Oneshot modifier
- *
- * Problem: Want to capitalize like 'The' but the result tends to be 'THe'.
- * Solution: Oneshot modifier have its effect on only one key coming next.
- * Tap Shift, then type 't', 'h' and 'e'. Not need to hold Shift key.
- *
- * Hold: works as normal modifier.
- * Tap: one shot modifier.
- * 2 Tap: cancel one shot modifier.
- * 5-Tap: toggles enable/disable oneshot feature.
- */
-static struct {
- uint8_t mods;
- uint8_t time;
- bool ready;
- bool disabled;
-} oneshot_state;
-
-static void oneshot_start(uint8_t mods, uint16_t time)
-{
- oneshot_state.mods = mods;
- oneshot_state.time = time;
- oneshot_state.ready = true;
-}
-
-static void oneshot_cancel(void)
-{
- oneshot_state.mods = 0;
- oneshot_state.time = 0;
- oneshot_state.ready = false;
-}
-
-static void oneshot_toggle(void)
-{
- oneshot_state.disabled = !oneshot_state.disabled;
-}
-
-
-/*
- * Layer stack
- */
-#define LAYER_STACK_SIZE 8
-typedef struct {
- uint8_t layer:4;
- uint8_t next:3;
- bool used;
-} layer_item_t;
-
-static uint8_t top_layer = 0;
-// [0] is sentinel and not used. [0] is null item.
-static layer_item_t layer_stack[LAYER_STACK_SIZE] = {};
-
-static bool layer_push(uint8_t layer)
-{
- for (uint8_t i = 1; i < LAYER_STACK_SIZE; i++) {
- if (!layer_stack[i].used) {
- layer_stack[i] = (layer_item_t){ .layer = layer,
- .next = top_layer,
- .used = true };
- top_layer = i;
- return true;
- }
- }
- return false;
-}
-static bool layer_pop(void)
-{
- if (layer_stack[top_layer].used) {
- uint8_t popped = top_layer;
- top_layer = layer_stack[popped].next;
- layer_stack[popped] = (layer_item_t){};
- return true;
- }
- return false;
-}
-static bool layer_remove(uint8_t layer)
-{
- if (layer_stack[top_layer].used && layer_stack[top_layer].layer == layer) {
- layer_pop();
- debug("layer_remove: top_layer\n");
- return true;
- }
-
- for (uint8_t i = top_layer; layer_stack[i].used; i = layer_stack[i].next) {
- debug("layer_remove: ["); debug_dec(i); debug("]");
- debug_dec(layer_stack[i].layer); debug("\n");
- uint8_t removed = layer_stack[i].next;
- if (layer_stack[removed].used && layer_stack[removed].layer == layer) {
- layer_stack[i].next = layer_stack[removed].next;
- layer_stack[removed] = (layer_item_t){};
- debug("layer_remove: removed.\n");
- return true;
- }
- }
- return false;
-}
-static bool layer_remove_then_push(uint8_t layer)
-{
- layer_remove(layer);
- return layer_push(layer);
-}
-static bool layer_remove_or_push(uint8_t layer)
-{
- return (layer_remove(layer)) || layer_push(layer);
-}
-static void debug_layer_stack(void)
-{
- debug("layer_stack: ");
- layer_item_t item = layer_stack[top_layer];
- while (item.used) {
- debug_dec(item.layer);
- debug("["); debug_dec(item.next); debug("]");
- item = layer_stack[item.next];
- }
- debug("\n");
-}
-
-