]> git.donarmstrong.com Git - tmk_firmware.git/blob - layer.c
fix for ps2_usb Makefile and circuit
[tmk_firmware.git] / layer.c
1 #include "keymap.h"
2 #include "host.h"
3 #include "debug.h"
4 #include "timer.h"
5 #include "layer.h"
6
7
8 /*
9  * Parameters:
10  *     ENTER_DELAY         |=======|
11  *     SEND_FN_TERM        |================|
12  *
13  * Fn key processing cases:
14  * 1. release Fn after SEND_FN_TERM.
15  *     Layer sw         ___________|~~~~~~~~~~~|___
16  *     Fn press         ___|~~~~~~~~~~~~~~~~~~~|___
17  *     Fn send          ___________________________
18  *
19  * 2. release Fn during SEND_FN_TERM.(not layer used)
20  *     Layer sw         ___________|~~~~~~|________
21  *     Fn press         ___|~~~~~~~~~~~~~~|________
22  *     Fn key send      __________________|~|______
23  *     other key press  ___________________________
24  *     other key send   ___________________________
25  *
26  * 3. release Fn during SEND_FN_TERM.(layer used)
27  *     Layer sw         ___________|~~~~~~|________
28  *     Fn press         ___|~~~~~~~~~~~~~~|________
29  *     Fn key send      ___________________________
30  *     Fn send          ___________________________
31  *     other key press  _____________|~~|__________
32  *     other key send   _____________|~~|__________
33  *
34  * 4. press other key during ENTER_DELAY.
35  *     Layer sw         ___________________________
36  *     Fn key press     ___|~~~~~~~~~|_____________
37  *     Fn key send      ______|~~~~~~|_____________
38  *     other key press  ______|~~~|________________
39  *     other key send   _______|~~|________________
40  *
41  * 5. press Fn while press other key.
42  *     Layer sw         ___________________________
43  *     Fn key press     ___|~~~~~~~~~|_____________
44  *     Fn key send      ___|~~~~~~~~~|_____________
45  *     other key press  ~~~~~~~|___________________
46  *     other key send   ~~~~~~~|___________________
47  *
48  * 6. press Fn twice quickly and keep holding down.(repeat)
49  *     Layer sw         ___________________________
50  *     Fn key press     ___|~|____|~~~~~~~~~~~~~~~~
51  *     Fn key send      _____|~|__|~~~~~~~~~~~~~~~~
52  */
53
54 // LAYER_ENTER_DELAY: prevent from moving new layer
55 #define LAYER_ENTER_DELAY 10
56
57 // LAYER_SEND_FN_TERM: send keycode if release key in this term
58 #define LAYER_SEND_FN_TERM 40
59
60
61 uint8_t default_layer = 0;
62 uint8_t current_layer = 0;
63
64 static bool layer_used = false;
65 static uint8_t new_layer(uint8_t fn_bits);
66
67
68 uint8_t layer_get_keycode(uint8_t row, uint8_t col)
69 {
70     uint8_t code = keymap_get_keycode(current_layer, row, col);
71     // normal key or mouse key
72     if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
73         layer_used = true;
74     }
75     return code;
76 }
77
78 // bit substract b from a
79 #define BIT_SUBT(a, b) (a&(a^b))
80 void layer_switching(uint8_t fn_bits)
81 {
82     // layer switching
83     static uint8_t last_fn = 0;
84     static uint8_t last_mods = 0;
85     static uint16_t last_timer = 0; 
86     static uint8_t sent_fn = 0;
87
88     if (fn_bits == last_fn) { // Fn state is not changed
89         if (fn_bits == 0) {
90             // do nothing
91         } else {
92             if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
93                 uint8_t _layer_to_switch = new_layer(BIT_SUBT(fn_bits, sent_fn));
94                 if (current_layer != _layer_to_switch) { // not switch layer yet
95                     debug("Fn case: 1,2,3(LAYER_ENTER_DELAY passed)\n");
96                     debug("Switch Layer: "); debug_hex(current_layer);
97                     current_layer = _layer_to_switch;
98                     layer_used = false;
99                     debug(" -> "); debug_hex(current_layer); debug("\n");
100                 }
101             } else {
102                 if (host_has_anykey()) { // other keys is pressed
103                     uint8_t _fn_to_send = BIT_SUBT(fn_bits, sent_fn);
104                     if (_fn_to_send) {
105                         debug("Fn case: 4(send Fn before other key pressed)\n");
106                         // send only Fn key first
107                         host_swap_keyboard_report();
108                         host_clear_keyboard_report();
109                         host_set_mods(last_mods);
110                         host_add_code(keymap_fn_keycode(_fn_to_send));   // TODO: do all Fn keys
111                         host_send_keyboard_report();
112                         host_swap_keyboard_report();
113                         sent_fn |= _fn_to_send;
114                     }
115                 }
116             }
117             // add Fn keys to send
118             //host_add_code(keymap_fn_keycode(fn_bits&sent_fn));  // TODO: do all Fn keys
119         }
120     } else { // Fn state is changed(edge)
121         uint8_t fn_changed = 0;
122
123         debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
124         debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
125         debug("last_fn: "); debug_bin(last_fn); debug("\n");
126         debug("last_mods: "); debug_hex(last_mods); debug("\n");
127         debug("last_timer: "); debug_hex16(last_timer); debug("\n");
128
129         // pressed Fn
130         if ((fn_changed = BIT_SUBT(fn_bits, last_fn))) {
131         debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
132             if (host_has_anykey()) {
133                 debug("Fn case: 5(pressed Fn with other key)\n");
134                 sent_fn |= fn_changed;
135             } else if (fn_changed & sent_fn) { // pressed same Fn in a row
136                 if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
137                     debug("Fn case: 6(repate2)\n");
138                     // time passed: not repeate
139                     sent_fn &= ~fn_changed;
140                 } else {
141                     debug("Fn case: 6(repate)\n");
142                 }
143             }
144         }
145         // released Fn
146         if ((fn_changed = BIT_SUBT(last_fn, fn_bits))) {
147         debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
148             if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
149                 //if (!layer_used && BIT_SUBT(fn_changed, sent_fn)) {  // layer not used && Fn not sent
150                 if (BIT_SUBT(fn_changed, sent_fn)) {  // layer not used && Fn not sent
151                     debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
152                     // send only Fn key first
153                     host_swap_keyboard_report();
154                     host_clear_keyboard_report();
155                     host_set_mods(last_mods);
156                     host_add_code(keymap_fn_keycode(fn_changed));   // TODO: do all Fn keys
157                     host_send_keyboard_report();
158                     host_swap_keyboard_report();
159                     sent_fn |= fn_changed;
160                 }
161             }
162             debug("Switch Layer(released Fn): "); debug_hex(current_layer);
163             current_layer = new_layer(BIT_SUBT(fn_bits, sent_fn));
164             layer_used = false;
165             debug(" -> "); debug_hex(current_layer); debug("\n");
166         }
167
168         last_fn = fn_bits;
169         last_mods = keyboard_report->mods;
170         last_timer = timer_read();
171     }
172     // send Fn keys
173     for (uint8_t i = 0; i < 8; i++) {
174         if ((sent_fn & fn_bits) & (1<<i)) {
175             host_add_code(keymap_fn_keycode(1<<i));
176         }
177     }
178 }
179
180 inline
181 static uint8_t new_layer(uint8_t fn_bits)
182 {
183     return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
184 }