3 #include "layer_stack.h"
7 static uint8_t top_layer = 0;
9 /* [0] always works as sentinel and not used for store.*/
10 static layer_item_t layer_stack[LAYER_STACK_SIZE] = {};
12 bool layer_stack_push(uint8_t layer)
14 for (uint8_t i = 1; i < LAYER_STACK_SIZE; i++) {
15 if (!layer_stack[i].used) {
16 layer_stack[i] = (layer_item_t){ .layer = layer,
26 bool layer_stack_pop(void)
28 if (layer_stack[top_layer].used) {
29 uint8_t popped = top_layer;
30 top_layer = layer_stack[popped].next;
31 layer_stack[popped] = (layer_item_t){};
37 bool layer_stack_remove(uint8_t layer)
39 if (layer_stack[top_layer].used && layer_stack[top_layer].layer == layer) {
41 debug("layer_stack_remove: top_layer\n");
45 for (uint8_t i = top_layer; layer_stack[i].used; i = layer_stack[i].next) {
46 debug("layer_stack_remove: ["); debug_dec(i); debug("]");
47 debug_dec(layer_stack[i].layer); debug("\n");
48 uint8_t removed = layer_stack[i].next;
49 if (layer_stack[removed].used && layer_stack[removed].layer == layer) {
50 layer_stack[i].next = layer_stack[removed].next;
51 layer_stack[removed] = (layer_item_t){};
52 debug("layer_stack_remove: removed.\n");
59 bool layer_stack_remove_then_push(uint8_t layer)
61 layer_stack_remove(layer);
62 return layer_stack_push(layer);
65 bool layer_stack_remove_or_push(uint8_t layer)
67 return (layer_stack_remove(layer)) || layer_stack_push(layer);
70 void layer_stack_debug(void)
72 debug("layer_stack: ");
73 layer_item_t item = layer_stack[top_layer];
75 debug_dec(item.layer);
76 debug("["); debug_dec(item.next); debug("]");
77 item = layer_stack[item.next];
84 action_t layer_stack_get_action(key_t key)
87 action.code = ACTION_TRANSPARENT;
90 for (layer_item_t i = layer_stack[top_layer]; i.used; i = layer_stack[i.next]) {
91 action = action_for_key(i.layer, key);
92 if (action.code != ACTION_TRANSPARENT) {
94 debug("layer_stack: used. "); debug_dec(i.layer); debug("\n");
97 debug("layer_stack: through. "); debug_dec(i.layer); debug("\n");