aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/action.c50
-rw-r--r--common/action.h39
-rw-r--r--common/command.c4
-rw-r--r--common/layer_stack.c108
-rw-r--r--common/layer_switch.c68
-rw-r--r--common/layer_switch.h (renamed from common/layer_stack.h)35
6 files changed, 123 insertions, 181 deletions
diff --git a/common/action.c b/common/action.c
index 9a8d75596..4f0a5f906 100644
--- a/common/action.c
+++ b/common/action.c
@@ -24,7 +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#include "layer_switch.h"
28 28
29 29
30/* default layer indicates base layer */ 30/* default layer indicates base layer */
@@ -213,8 +213,8 @@ static action_t get_action(key_t key)
213 action_t action; 213 action_t action;
214 action.code = ACTION_NO; 214 action.code = ACTION_NO;
215 215
216 /* layer stack */ 216 /* layer_switch */
217 action = layer_stack_get_action(key); 217 action = layer_switch_get_action(key);
218 if (action.code != ACTION_TRANSPARENT) { 218 if (action.code != ACTION_TRANSPARENT) {
219 return action; 219 return action;
220 } 220 }
@@ -531,45 +531,38 @@ static void process_action(keyrecord_t *record)
531 break; 531 break;
532 } 532 }
533 break; 533 break;
534 case ACT_LAYER_STACK: 534 case ACT_LAYER_SWITCH:
535 switch (action.layer.code) { 535 switch (action.layer.code) {
536 case LAYER_MOMENTARY: /* momentary */ 536 case LAYER_MOMENTARY: /* momentary */
537 if (event.pressed) { 537 if (event.pressed) {
538 layer_stack_remove_then_push(action.layer.val); 538 layer_switch_on(action.layer.val);
539 layer_stack_debug();
540 } else { 539 } else {
541 layer_stack_remove(action.layer.val); 540 layer_switch_off(action.layer.val);
542 layer_stack_debug();
543 } 541 }
544 break; 542 break;
545 case LAYER_ON_PRESS: 543 case LAYER_ON_PRESS:
546 if (event.pressed) { 544 if (event.pressed) {
547 layer_stack_remove_or_push(action.layer.val); 545 layer_switch_inv(action.layer.val);
548 layer_stack_debug();
549 } 546 }
550 break; 547 break;
551 case LAYER_ON_RELEASE: 548 case LAYER_ON_RELEASE:
552 if (!event.pressed) { 549 if (!event.pressed) {
553 layer_stack_remove_or_push(action.layer.val); 550 layer_switch_inv(action.layer.val);
554 layer_stack_debug();
555 } 551 }
556 break; 552 break;
557 case LAYER_ON_BOTH: 553 case LAYER_ON_BOTH:
558 layer_stack_remove_or_push(action.layer.val); 554 layer_switch_inv(action.layer.val);
559 layer_stack_debug();
560 break; 555 break;
561 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */ 556 case LAYER_TAP_TOGGLE: /* switch on hold and toggle on several taps */
562 if (event.pressed) { 557 if (event.pressed) {
563 if (tap_count < TAPPING_TOGGLE) { 558 if (tap_count < TAPPING_TOGGLE) {
564 debug("LAYER_STACK: tap toggle(press).\n"); 559 debug("LAYER_SWITCH: tap toggle(press).\n");
565 layer_stack_remove_or_push(action.layer.val); 560 layer_switch_inv(action.layer.val);
566 layer_stack_debug();
567 } 561 }
568 } else { 562 } else {
569 if (tap_count <= TAPPING_TOGGLE) { 563 if (tap_count <= TAPPING_TOGGLE) {
570 debug("LAYER_STACK: tap toggle(release).\n"); 564 debug("LAYER_SWITCH: tap toggle(release).\n");
571 layer_stack_remove_or_push(action.layer.val); 565 layer_switch_inv(action.layer.val);
572 layer_stack_debug();
573 } 566 }
574 } 567 }
575 break; 568 break;
@@ -577,21 +570,19 @@ static void process_action(keyrecord_t *record)
577 // tap key 570 // tap key
578 if (event.pressed) { 571 if (event.pressed) {
579 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 572 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
580 debug("LAYER_STACK: Tap: register_code\n"); 573 debug("LAYER_SWITCH: Tap: register_code\n");
581 register_code(action.layer.code); 574 register_code(action.layer.code);
582 } else { 575 } else {
583 debug("LAYER_STACK: No tap: layer_stack(on press)\n"); 576 debug("LAYER_SWITCH: No tap: layer_switch(on press)\n");
584 layer_stack_remove_or_push(action.layer.val); 577 layer_switch_inv(action.layer.val);
585 layer_stack_debug();
586 } 578 }
587 } else { 579 } else {
588 if (IS_TAPPING_KEY(event.key) && tap_count > 0) { 580 if (IS_TAPPING_KEY(event.key) && tap_count > 0) {
589 debug("LAYER_STACK: Tap: unregister_code\n"); 581 debug("LAYER_SWITCH: Tap: unregister_code\n");
590 unregister_code(action.layer.code); 582 unregister_code(action.layer.code);
591 } else { 583 } else {
592 debug("LAYER_STACK: No tap: layer_stack(on release)\n"); 584 debug("LAYER_SWITCH: No tap: layer_switch(on release)\n");
593 layer_stack_remove_or_push(action.layer.val); 585 layer_switch_inv(action.layer.val);
594 layer_stack_debug();
595 } 586 }
596 } 587 }
597 break; 588 break;
@@ -898,6 +889,7 @@ bool sending_anykey(void)
898 host_last_sysytem_report() || host_last_consumer_report()); 889 host_last_sysytem_report() || host_last_consumer_report());
899} 890}
900 891
892// TODO: rename or reinpl with new layer_switch.c
901void layer_switch(uint8_t new_layer) 893void layer_switch(uint8_t new_layer)
902{ 894{
903 if (current_layer != new_layer) { 895 if (current_layer != new_layer) {
@@ -966,7 +958,7 @@ static void debug_action(action_t action)
966 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break; 958 case ACT_MOUSEKEY: debug("ACT_MOUSEKEY"); break;
967 case ACT_LAYER: debug("ACT_LAYER"); break; 959 case ACT_LAYER: debug("ACT_LAYER"); break;
968 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break; 960 case ACT_LAYER_BIT: debug("ACT_LAYER_BIT"); break;
969 case ACT_LAYER_STACK: debug("ACT_LAYER_STACK"); break; 961 case ACT_LAYER_SWITCH: debug("ACT_LAYER_SWITCH"); break;
970 case ACT_MACRO: debug("ACT_MACRO"); break; 962 case ACT_MACRO: debug("ACT_MACRO"); break;
971 case ACT_COMMAND: debug("ACT_COMMAND"); break; 963 case ACT_COMMAND: debug("ACT_COMMAND"); break;
972 case ACT_FUNCTION: debug("ACT_FUNCTION"); break; 964 case ACT_FUNCTION: debug("ACT_FUNCTION"); break;
diff --git a/common/action.h b/common/action.h
index 96b8ba2ed..1d00e02d5 100644
--- a/common/action.h
+++ b/common/action.h
@@ -163,9 +163,6 @@ bool waiting_buffer_has_anykey_pressed(void);
163 * Layer Actions 163 * Layer Actions
164 * ------------- 164 * -------------
165 * ACT_LAYER(1000): Set layer 165 * ACT_LAYER(1000): Set layer
166 * ACT_LAYER_BIT(1001): Bit-op layer
167 * ACT_LAYER_STACK: Layer stack
168 *
169 * 1000|LLLL|0000 0000 set current layer on press and return to default on release(momentary) 166 * 1000|LLLL|0000 0000 set current layer on press and return to default on release(momentary)
170 * 1000|LLLL|0000 0001 set current layer on press 167 * 1000|LLLL|0000 0001 set current layer on press
171 * 1000|LLLL|0000 0010 set current layer on release 168 * 1000|LLLL|0000 0010 set current layer on release
@@ -175,6 +172,7 @@ bool waiting_buffer_has_anykey_pressed(void);
175 * 1000|DDDD|1111 1111 set default layer on press 172 * 1000|DDDD|1111 1111 set default layer on press
176 * L: 0 means default layer 173 * L: 0 means default layer
177 * 174 *
175 * ACT_LAYER_BIT(1001): Bit-op layer
178 * 1001|BBBB|0000 0000 bit-on current layer on press and bit-off on release(momentary) 176 * 1001|BBBB|0000 0000 bit-on current layer on press and bit-off on release(momentary)
179 * 1001|BBBB|0000 0001 bit-xor current layer on press 177 * 1001|BBBB|0000 0001 bit-xor current layer on press
180 * 1001|BBBB|0000 0010 bit-xor current layer on release 178 * 1001|BBBB|0000 0010 bit-xor current layer on release
@@ -183,12 +181,13 @@ bool waiting_buffer_has_anykey_pressed(void);
183 * 1001|BBBB|1111 0000 bit-xor current layer on hold and toggle on several taps 181 * 1001|BBBB|1111 0000 bit-xor current layer on hold and toggle on several taps
184 * 1001|BBBB|1111 1111 bit-xor default layer on both 182 * 1001|BBBB|1111 1111 bit-xor default layer on both
185 * 183 *
186 * 1011|LLLL|0000 0000 push on press and remove on release(momentary) 184 * ACT_LAYER_SWITCH: Switch
187 * 1011|LLLL|0000 0001 push or remove on press 185 * 1011|LLLL|0000 0000 On on press and Off on release(momentary)
188 * 1011|LLLL|0000 0010 push or remove on release 186 * 1011|LLLL|0000 0001 Invert on press
189 * 1011|LLLL|0000 0011 push or remove on both 187 * 1011|LLLL|0000 0010 Invert on release
190 * 1011|LLLL| keycode push or remove on hold and send key on tap 188 * 1011|LLLL|0000 0011 Invert on both
191 * 1011|LLLL|1111 0000 push or remove on hold and toggle on several taps 189 * 1011|LLLL| keycode Invert on hold and send key on tap
190 * 1011|LLLL|1111 0000 Invert on hold and toggle on several taps
192 * 1011|LLLL|1111 1111 (not used) 191 * 1011|LLLL|1111 1111 (not used)
193 * 192 *
194 * 193 *
@@ -219,7 +218,7 @@ enum action_kind_id {
219 218
220 ACT_LAYER = 0b1000, 219 ACT_LAYER = 0b1000,
221 ACT_LAYER_BIT = 0b1001, 220 ACT_LAYER_BIT = 0b1001,
222 ACT_LAYER_STACK = 0b1011, 221 ACT_LAYER_SWITCH = 0b1011,
223 222
224 ACT_MACRO = 0b1100, 223 ACT_MACRO = 0b1100,
225 ACT_COMMAND = 0b1110, 224 ACT_COMMAND = 0b1110,
@@ -233,7 +232,7 @@ enum action_kind_id {
233#define ACTION(kind, param) ((kind)<<12 | (param)) 232#define ACTION(kind, param) ((kind)<<12 | (param))
234#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F) 233#define MODS4(mods) (((mods)>>4 | (mods)) & 0x0F)
235 234
236/* 235/*
237 * Key 236 * Key
238 */ 237 */
239#define ACTION_KEY(key) ACTION(ACT_LMODS, key) 238#define ACTION_KEY(key) ACTION(ACT_LMODS, key)
@@ -316,17 +315,17 @@ enum layer_codes {
316#define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE) 315#define ACTION_LAYER_BIT_TAP_TOGGLE(bits) ACTION(ACT_LAYER_BIT, (bits)<<8 | LAYER_TAP_TOGGLE)
317#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key)) 316#define ACTION_LAYER_BIT_TAP_KEY(bits, key) ACTION(ACT_LAYER_BIT, (bits)<<8 | (key))
318/* 317/*
319 * Layer Stack 318 * Layer SWITCH
320 */ 319 */
321/* momentary */ 320/* momentary */
322#define ACTION_LAYER_STACK(layer) ACTION_LAYER_STACK_MOMENTARY(layer) 321#define ACTION_LAYER_SWITCH(layer) ACTION_LAYER_SWITCH_MOMENTARY(layer)
323#define ACTION_LAYER_STACK_MOMENTARY(layer) ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_MOMENTARY) 322#define ACTION_LAYER_SWITCH_MOMENTARY(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_MOMENTARY)
324#define ACTION_LAYER_STACK_TOGGLE(layer) ACTION_LAYER_STACK_R(layer) 323#define ACTION_LAYER_SWITCH_TOGGLE(layer) ACTION_LAYER_SWITCH_R(layer)
325#define ACTION_LAYER_STACK_P(layer) ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_ON_PRESS) 324#define ACTION_LAYER_SWITCH_P(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_PRESS)
326#define ACTION_LAYER_STACK_R(layer) ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_ON_RELEASE) 325#define ACTION_LAYER_SWITCH_R(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_RELEASE)
327#define ACTION_LAYER_STACK_B(layer) ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_ON_BOTH) 326#define ACTION_LAYER_SWITCH_B(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_ON_BOTH)
328#define ACTION_LAYER_STACK_TAP_TOGGLE(layer) ACTION(ACT_LAYER_STACK, (layer)<<8 | LAYER_TAP_TOGGLE) 327#define ACTION_LAYER_SWITCH_TAP_TOGGLE(layer) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | LAYER_TAP_TOGGLE)
329#define ACTION_LAYER_STACK_TAP_KEY(layer, key) ACTION(ACT_LAYER_STACK, (layer)<<8 | (key)) 328#define ACTION_LAYER_SWITCH_TAP_KEY(layer, key) ACTION(ACT_LAYER_SWITCH, (layer)<<8 | (key))
330 329
331 330
332/* 331/*
diff --git a/common/command.c b/common/command.c
index c5b9f0431..82f647c8f 100644
--- a/common/command.c
+++ b/common/command.c
@@ -26,8 +26,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
26#include "timer.h" 26#include "timer.h"
27#include "keyboard.h" 27#include "keyboard.h"
28#include "bootloader.h" 28#include "bootloader.h"
29#include "layer_switch.h"
29#include "command.h" 30#include "command.h"
30#include "layer_stack.h"
31 31
32#ifdef MOUSEKEY_ENABLE 32#ifdef MOUSEKEY_ENABLE
33#include "mousekey.h" 33#include "mousekey.h"
@@ -549,6 +549,6 @@ static void switch_default_layer(uint8_t layer)
549 549
550 default_layer = layer; 550 default_layer = layer;
551 current_layer = 0; /* 0 means default_layer */ 551 current_layer = 0; /* 0 means default_layer */
552 layer_stack_clear(); 552 layer_switch_clear();
553 clear_keyboard(); 553 clear_keyboard();
554} 554}
diff --git a/common/layer_stack.c b/common/layer_stack.c
deleted file mode 100644
index 0076bf779..000000000
--- a/common/layer_stack.c
+++ /dev/null
@@ -1,108 +0,0 @@
1#include <stdint.h>
2#include "keyboard.h"
3#include "layer_stack.h"
4#include "debug.h"
5
6
7static uint8_t top_layer = 0;
8
9/* [0] always works as sentinel and not used for store.*/
10static layer_item_t layer_stack[LAYER_STACK_SIZE] = {};
11
12
13void layer_stack_clear(void)
14{
15 for (uint8_t i = 0; i < LAYER_STACK_SIZE; i++) {
16 layer_stack[i] = (layer_item_t){ .layer = 0,
17 .next = 0,
18 .used = false };
19 }
20}
21
22bool layer_stack_push(uint8_t layer)
23{
24 for (uint8_t i = 1; i < LAYER_STACK_SIZE; i++) {
25 if (!layer_stack[i].used) {
26 layer_stack[i] = (layer_item_t){ .layer = layer,
27 .next = top_layer,
28 .used = true };
29 top_layer = i;
30 return true;
31 }
32 }
33 return false;
34}
35
36bool layer_stack_pop(void)
37{
38 if (layer_stack[top_layer].used) {
39 uint8_t popped = top_layer;
40 top_layer = layer_stack[popped].next;
41 layer_stack[popped] = (layer_item_t){};
42 return true;
43 }
44 return false;
45}
46
47bool layer_stack_remove(uint8_t layer)
48{
49 if (layer_stack[top_layer].used && layer_stack[top_layer].layer == layer) {
50 layer_stack_pop();
51 debug("layer_stack_remove: top_layer\n");
52 return true;
53 }
54
55 for (uint8_t i = top_layer; layer_stack[i].used; i = layer_stack[i].next) {
56 debug("layer_stack_remove: ["); debug_dec(i); debug("]");
57 debug_dec(layer_stack[i].layer); debug("\n");
58 uint8_t removed = layer_stack[i].next;
59 if (layer_stack[removed].used && layer_stack[removed].layer == layer) {
60 layer_stack[i].next = layer_stack[removed].next;
61 layer_stack[removed] = (layer_item_t){};
62 debug("layer_stack_remove: removed.\n");
63 return true;
64 }
65 }
66 return false;
67}
68
69bool layer_stack_remove_then_push(uint8_t layer)
70{
71 layer_stack_remove(layer);
72 return layer_stack_push(layer);
73}
74
75bool layer_stack_remove_or_push(uint8_t layer)
76{
77 return (layer_stack_remove(layer)) || layer_stack_push(layer);
78}
79
80void layer_stack_debug(void)
81{
82 debug("layer_stack: ");
83 layer_item_t item = layer_stack[top_layer];
84 while (item.used) {
85 debug_dec(item.layer);
86 debug("["); debug_dec(item.next); debug("] ");
87 item = layer_stack[item.next];
88 }
89 debug("\n");
90}
91
92action_t layer_stack_get_action(key_t key)
93{
94 action_t action;
95 action.code = ACTION_TRANSPARENT;
96
97 /* layer stack */
98 for (layer_item_t i = layer_stack[top_layer]; i.used; i = layer_stack[i.next]) {
99 action = action_for_key(i.layer, key);
100 if (action.code != ACTION_TRANSPARENT) {
101 layer_stack_debug();
102 debug("layer_stack: used. "); debug_dec(i.layer); debug("\n");
103 return action;
104 }
105 debug("layer_stack: through. "); debug_dec(i.layer); debug("\n");
106 }
107 return action;
108}
diff --git a/common/layer_switch.c b/common/layer_switch.c
new file mode 100644
index 000000000..9bc804e64
--- /dev/null
+++ b/common/layer_switch.c
@@ -0,0 +1,68 @@
1#include <stdint.h>
2#include "keyboard.h"
3#include "action.h"
4#include "debug.h"
5#include "layer_switch.h"
6
7
8uint16_t layer_switch_stat = 0;
9
10
11uint16_t layer_switch_stat_get(void)
12{
13 return layer_switch_stat;
14}
15
16void layer_switch_stat_set(uint16_t stat)
17{
18 layer_switch_stat = stat;
19 layer_switch_debug();
20}
21
22void layer_switch_clear(void)
23{
24 layer_switch_stat = 0;
25 layer_switch_debug();
26}
27
28void layer_switch_on(uint8_t layer)
29{
30 layer_switch_stat |= (1<<layer);
31 layer_switch_debug();
32}
33
34void layer_switch_off(uint8_t layer)
35{
36 layer_switch_stat &= ~(1<<layer);
37 layer_switch_debug();
38}
39
40void layer_switch_inv(uint8_t layer)
41{
42 layer_switch_stat ^= (1<<layer);
43 layer_switch_debug();
44}
45
46void layer_switch_debug(void)
47{
48 debug("layer_switch_stat: "); debug_bin16(layer_switch_stat); debug("\n");
49}
50
51action_t layer_switch_get_action(key_t key)
52{
53 action_t action;
54 action.code = ACTION_TRANSPARENT;
55
56 /* higher layer first */
57 for (int8_t i = 15; i >= 0; i--) {
58 if (layer_switch_stat & (1<<i)) {
59 action = action_for_key(i, key);
60 if (action.code != ACTION_TRANSPARENT) {
61 layer_switch_debug();
62 debug("layer_switch: used. "); debug_dec(i); debug("\n");
63 return action;
64 }
65 }
66 }
67 return action;
68}
diff --git a/common/layer_stack.h b/common/layer_switch.h
index 25bf37a5b..9f0695260 100644
--- a/common/layer_stack.h
+++ b/common/layer_switch.h
@@ -14,32 +14,23 @@ GNU General Public License for more details.
14You should have received a copy of the GNU General Public License 14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>. 15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/ 16*/
17#ifndef LAYER_STACK_H 17#ifndef LAYER_SWITCH_H
18#define LAYER_STACK_H 18#define LAYER_SWITCH_H
19 19
20#include <stdint.h> 20#include <stdint.h>
21#include "keyboard.h"
21#include "action.h" 22#include "action.h"
22 23
24uint16_t layer_switch_stat;
23 25
24/* 26uint16_t layer_switch_stat_get(void);
25 * Layer stack 27void layer_switch_stat_set(uint16_t stat);
26 */ 28void layer_switch_clear(void);
27#define LAYER_STACK_SIZE 8 29void layer_switch_on(uint8_t layer);
28typedef struct { 30void layer_switch_off(uint8_t layer);
29 uint8_t layer:4; 31/* invert state */
30 uint8_t next:3; 32void layer_switch_inv(uint8_t layer);
31 bool used; 33void layer_switch_debug(void);
32} layer_item_t; 34action_t layer_switch_get_action(key_t key);
33
34
35void layer_stack_clear(void);
36bool layer_stack_push(uint8_t layer);
37bool layer_stack_pop(void);
38bool layer_stack_remove(uint8_t layer);
39bool layer_stack_remove_then_push(uint8_t layer);
40bool layer_stack_remove_or_push(uint8_t layer);
41void layer_stack_debug(void);
42action_t layer_stack_get_action(key_t key);
43 35
44#endif 36#endif
45