aboutsummaryrefslogtreecommitdiff
path: root/common/action.c
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2013-02-15 13:47:41 +0900
committertmk <nobody@nowhere>2013-02-15 13:47:41 +0900
commit768ea72f109fee2411c77bf2fabcbede5f98650d (patch)
tree59350a8538ef9a8380250771fb57490759041fec /common/action.c
parentc74ad260fb45b2deec0309d9a9cbac3c0434886b (diff)
downloadqmk_firmware-768ea72f109fee2411c77bf2fabcbede5f98650d.tar.gz
qmk_firmware-768ea72f109fee2411c77bf2fabcbede5f98650d.zip
Add layer_stack files taking apart from action.c
Diffstat (limited to 'common/action.c')
-rw-r--r--common/action.c127
1 files changed, 22 insertions, 105 deletions
diff --git a/common/action.c b/common/action.c
index 38c5933ee..9a8d75596 100644
--- a/common/action.c
+++ b/common/action.c
@@ -24,6 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
24#include "util.h" 24#include "util.h"
25#include "debug.h" 25#include "debug.h"
26#include "action.h" 26#include "action.h"
27#include "layer_stack.h"
27 28
28 29
29/* default layer indicates base layer */ 30/* default layer indicates base layer */
@@ -163,85 +164,6 @@ static void oneshot_toggle(void)
163} 164}
164 165
165 166
166/*
167 * Layer stack
168 */
169#define LAYER_STACK_SIZE 8
170typedef struct {
171 uint8_t layer:4;
172 uint8_t next:3;
173 bool used;
174} layer_item_t;
175
176static uint8_t top_layer = 0;
177// [0] is sentinel and not used. [0] is null item.
178static layer_item_t layer_stack[LAYER_STACK_SIZE] = {};
179
180static bool layer_push(uint8_t layer)
181{
182 for (uint8_t i = 1; i < LAYER_STACK_SIZE; i++) {
183 if (!layer_stack[i].used) {
184 layer_stack[i] = (layer_item_t){ .layer = layer,
185 .next = top_layer,
186 .used = true };
187 top_layer = i;
188 return true;
189 }
190 }
191 return false;
192}
193static bool layer_pop(void)
194{
195 if (layer_stack[top_layer].used) {
196 uint8_t popped = top_layer;
197 top_layer = layer_stack[popped].next;
198 layer_stack[popped] = (layer_item_t){};
199 return true;
200 }
201 return false;
202}
203static bool layer_remove(uint8_t layer)
204{
205 if (layer_stack[top_layer].used && layer_stack[top_layer].layer == layer) {
206 layer_pop();
207 debug("layer_remove: top_layer\n");
208 return true;
209 }
210
211 for (uint8_t i = top_layer; layer_stack[i].used; i = layer_stack[i].next) {
212 debug("layer_remove: ["); debug_dec(i); debug("]");
213 debug_dec(layer_stack[i].layer); debug("\n");
214 uint8_t removed = layer_stack[i].next;
215 if (layer_stack[removed].used && layer_stack[removed].layer == layer) {
216 layer_stack[i].next = layer_stack[removed].next;
217 layer_stack[removed] = (layer_item_t){};
218 debug("layer_remove: removed.\n");
219 return true;
220 }
221 }
222 return false;
223}
224static bool layer_remove_then_push(uint8_t layer)
225{
226 layer_remove(layer);
227 return layer_push(layer);
228}
229static bool layer_remove_or_push(uint8_t layer)
230{
231 return (layer_remove(layer)) || layer_push(layer);
232}
233static void debug_layer_stack(void)
234{
235 debug("layer_stack: ");
236 layer_item_t item = layer_stack[top_layer];
237 while (item.used) {
238 debug_dec(item.layer);
239 debug("["); debug_dec(item.next); debug("]");
240 item = layer_stack[item.next];
241 }
242 debug("\n");
243}
244
245 167
246void action_exec(keyevent_t event) 168void action_exec(keyevent_t event)
247{ 169{
@@ -292,14 +214,9 @@ static action_t get_action(key_t key)
292 action.code = ACTION_NO; 214 action.code = ACTION_NO;
293 215
294 /* layer stack */ 216 /* layer stack */
295 for (layer_item_t i = layer_stack[top_layer]; i.used; i = layer_stack[i.next]) { 217 action = layer_stack_get_action(key);
296 action = action_for_key(i.layer, key); 218 if (action.code != ACTION_TRANSPARENT) {
297 if (action.code != ACTION_TRANSPARENT) { 219 return action;
298 debug_layer_stack();
299 debug("layer_stack: used. "); debug_dec(i.layer); debug("\n");
300 return action;
301 }
302 debug("layer_stack: through. "); debug_dec(i.layer); debug("\n");
303 } 220 }
304 221
305 /* current layer: 0 means default layer */ 222 /* current layer: 0 means default layer */
@@ -618,41 +535,41 @@ static void process_action(keyrecord_t *record)
618 switch (action.layer.code) { 535 switch (action.layer.code) {
619 case LAYER_MOMENTARY: /* momentary */ 536 case LAYER_MOMENTARY: /* momentary */
620 if (event.pressed) { 537 if (event.pressed) {
621 layer_remove_then_push(action.layer.val); 538 layer_stack_remove_then_push(action.layer.val);
622 debug_layer_stack(); 539 layer_stack_debug();
623 } else { 540 } else {
624 layer_remove(action.layer.val); 541 layer_stack_remove(action.layer.val);
625 debug_layer_stack(); 542 layer_stack_debug();
626 } 543 }
627 break; 544 break;
628 case LAYER_ON_PRESS: 545 case LAYER_ON_PRESS:
629 if (event.pressed) { 546 if (event.pressed) {
630 layer_remove_or_push(action.layer.val); 547 layer_stack_remove_or_push(action.layer.val);
631 debug_layer_stack(); 548 layer_stack_debug();
632 } 549 }
633 break; 550 break;
634 case LAYER_ON_RELEASE: 551 case LAYER_ON_RELEASE:
635 if (!event.pressed) { 552 if (!event.pressed) {
636 layer_remove_or_push(action.layer.val); 553 layer_stack_remove_or_push(action.layer.val);
637 debug_layer_stack(); 554 layer_stack_debug();
638 } 555 }
639 break; 556 break;
640 case LAYER_ON_BOTH: 557 case LAYER_ON_BOTH:
641 layer_remove_or_push(action.layer.val); 558 layer_stack_remove_or_push(action.layer.val);
642 debug_layer_stack(); 559 layer_stack_debug();
643 break; 560 break;
644 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ 561 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
645 if (event.pressed) { 562 if (event.pressed) {
646 if (tap_count < TAPPING_TOGGLE) { 563 if (tap_count < TAPPING_TOGGLE) {
647 debug("LAYER_STACK: tap toggle(press).\n"); 564 debug("LAYER_STACK: tap toggle(press).\n");
648 layer_remove_or_push(action.layer.val); 565 layer_stack_remove_or_push(action.layer.val);
649 debug_layer_stack(); 566 layer_stack_debug();
650 } 567 }
651 } else { 568 } else {
652 if (tap_count <= TAPPING_TOGGLE) { 569 if (tap_count <= TAPPING_TOGGLE) {
653 debug("LAYER_STACK: tap toggle(release).\n"); 570 debug("LAYER_STACK: tap toggle(release).\n");
654 layer_remove_or_push(action.layer.val); 571 layer_stack_remove_or_push(action.layer.val);
655 debug_layer_stack(); 572 layer_stack_debug();
656 } 573 }
657 } 574 }
658 break; 575 break;
@@ -664,8 +581,8 @@ static void process_action(keyrecord_t *record)
664 register_code(action.layer.code); 581 register_code(action.layer.code);
665 } else { 582 } else {
666 debug("LAYER_STACK: No tap: layer_stack(on press)\n"); 583 debug("LAYER_STACK: No tap: layer_stack(on press)\n");
667 layer_remove_or_push(action.layer.val); 584 layer_stack_remove_or_push(action.layer.val);
668 debug_layer_stack(); 585 layer_stack_debug();
669 } 586 }
670 } else { 587 } else {
671 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 588 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
@@ -673,8 +590,8 @@ static void process_action(keyrecord_t *record)
673 unregister_code(action.layer.code); 590 unregister_code(action.layer.code);
674 } else { 591 } else {
675 debug("LAYER_STACK: No tap: layer_stack(on release)\n"); 592 debug("LAYER_STACK: No tap: layer_stack(on release)\n");
676 layer_remove_or_push(action.layer.val); 593 layer_stack_remove_or_push(action.layer.val);
677 debug_layer_stack(); 594 layer_stack_debug();
678 } 595 }
679 } 596 }
680 break; 597 break;