]> git.friedersdorff.com Git - max/tmk_keyboard.git/blob - layer.c
Merge branch 'm0110a'
[max/tmk_keyboard.git] / layer.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include "keymap.h"
19 #include "host.h"
20 #include "debug.h"
21 #include "timer.h"
22 #include "usb_keycodes.h"
23 #include "layer.h"
24
25
26 /*
27  * Parameters:
28  *     ENTER_DELAY         |=======|
29  *     SEND_FN_TERM        |================|
30  *
31  * Fn key processing cases:
32  * 1. release Fn after SEND_FN_TERM.
33  *     Layer sw         ___________|~~~~~~~~~~~|___
34  *     Fn press         ___|~~~~~~~~~~~~~~~~~~~|___
35  *     Fn send          ___________________________
36  *
37  * 2. release Fn during SEND_FN_TERM.(not layer used)
38  *     Layer sw         ___________|~~~~~~|________
39  *     Fn press         ___|~~~~~~~~~~~~~~|________
40  *     Fn key send      __________________|~|______
41  *     other key press  ___________________________
42  *     other key send   ___________________________
43  *
44  * 3. release Fn during SEND_FN_TERM.(layer used)
45  *     Layer sw         ___________|~~~~~~|________
46  *     Fn press         ___|~~~~~~~~~~~~~~|________
47  *     Fn key send      ___________________________
48  *     Fn send          ___________________________
49  *     other key press  _____________|~~|__________
50  *     other key send   _____________|~~|__________
51  *
52  * 4. press other key during ENTER_DELAY.
53  *     Layer sw         ___________________________
54  *     Fn key press     ___|~~~~~~~~~|_____________
55  *     Fn key send      ______|~~~~~~|_____________
56  *     other key press  ______|~~~|________________
57  *     other key send   _______|~~|________________
58  *
59  * 5. press Fn while press other key.
60  *     Layer sw         ___________________________
61  *     Fn key press     ___|~~~~~~~~~|_____________
62  *     Fn key send      ___|~~~~~~~~~|_____________
63  *     other key press  ~~~~~~~|___________________
64  *     other key send   ~~~~~~~|___________________
65  *
66  * 6. press Fn twice quickly and keep holding down.(repeat)
67  *     Layer sw         ___________________________
68  *     Fn key press     ___|~|____|~~~~~~~~~~~~~~~~
69  *     Fn key send      _____|~|__|~~~~~~~~~~~~~~~~
70  */
71
72 // LAYER_ENTER_DELAY: prevent from moving new layer
73 #define LAYER_ENTER_DELAY 150
74
75 // LAYER_SEND_FN_TERM: send keycode if release key in this term
76 #define LAYER_SEND_FN_TERM 500
77
78
79 uint8_t default_layer = 0;
80 uint8_t current_layer = 0;
81
82 static bool layer_used = false;
83 static uint8_t new_layer(uint8_t fn_bits);
84
85
86 uint8_t layer_get_keycode(uint8_t row, uint8_t col)
87 {
88     uint8_t code = keymap_get_keycode(current_layer, row, col);
89     // normal key or mouse key
90     if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
91         layer_used = true;
92     }
93     return code;
94 }
95
96 // bit substract b from a
97 #define BIT_SUBST(a, b) (a&(a^b))
98 void layer_switching(uint8_t fn_bits)
99 {
100     // layer switching
101     static uint8_t last_fn = 0;
102     static uint8_t last_mods = 0;
103     static uint16_t last_timer = 0; 
104     static uint8_t sent_fn = 0;
105
106     if (fn_bits == last_fn) { // Fn state is not changed
107         if (fn_bits == 0) {
108             // do nothing
109         } else {
110             if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
111                 uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn));
112                 if (current_layer != _layer_to_switch) { // not switch layer yet
113                     debug("Fn case: 1,2,3(LAYER_ENTER_DELAY passed)\n");
114                     debug("Switch Layer: "); debug_hex(current_layer);
115                     current_layer = _layer_to_switch;
116                     layer_used = false;
117                     debug(" -> "); debug_hex(current_layer); debug("\n");
118                 }
119             } else {
120                 if (host_has_anykey()) { // other keys is pressed
121                     uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn);
122                     if (_fn_to_send) {
123                         debug("Fn case: 4(send Fn before other key pressed)\n");
124                         // send only Fn key first
125                         host_swap_keyboard_report();
126                         host_clear_keyboard_report();
127                         host_set_mods(last_mods);
128                         host_add_code(keymap_fn_keycode(_fn_to_send));   // TODO: do all Fn keys
129                         host_send_keyboard_report();
130                         host_swap_keyboard_report();
131                         sent_fn |= _fn_to_send;
132                     }
133                 }
134             }
135             // add Fn keys to send
136             //host_add_code(keymap_fn_keycode(fn_bits&sent_fn));  // TODO: do all Fn keys
137         }
138     } else { // Fn state is changed(edge)
139         uint8_t fn_changed = 0;
140
141         debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
142         debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
143         debug("last_fn: "); debug_bin(last_fn); debug("\n");
144         debug("last_mods: "); debug_hex(last_mods); debug("\n");
145         debug("last_timer: "); debug_hex16(last_timer); debug("\n");
146
147         // pressed Fn
148         if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) {
149         debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
150             if (host_has_anykey()) {
151                 debug("Fn case: 5(pressed Fn with other key)\n");
152                 sent_fn |= fn_changed;
153             } else if (fn_changed & sent_fn) { // pressed same Fn in a row
154                 if (timer_elapsed(last_timer) > LAYER_ENTER_DELAY) {
155                     debug("Fn case: 6(not repeat)\n");
156                     // time passed: not repeate
157                     sent_fn &= ~fn_changed;
158                 } else {
159                     debug("Fn case: 6(repeat)\n");
160                 }
161             }
162         }
163         // released Fn
164         if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) {
165         debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
166             if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
167                 if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) {
168                     debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
169                     // send only Fn key first
170                     host_swap_keyboard_report();
171                     host_clear_keyboard_report();
172                     host_set_mods(last_mods);
173                     host_add_code(keymap_fn_keycode(fn_changed));   // TODO: do all Fn keys
174                     host_send_keyboard_report();
175                     host_swap_keyboard_report();
176                     sent_fn |= fn_changed;
177                 }
178             }
179             debug("Switch Layer(released Fn): "); debug_hex(current_layer);
180             current_layer = new_layer(BIT_SUBST(fn_bits, sent_fn));
181             debug(" -> "); debug_hex(current_layer); debug("\n");
182         }
183
184         layer_used = false;
185         last_fn = fn_bits;
186         last_mods = keyboard_report->mods;
187         last_timer = timer_read();
188     }
189     // send Fn keys
190     for (uint8_t i = 0; i < 8; i++) {
191         if ((sent_fn & fn_bits) & (1<<i)) {
192             host_add_code(keymap_fn_keycode(1<<i));
193         }
194     }
195 }
196
197 inline
198 static uint8_t new_layer(uint8_t fn_bits)
199 {
200     return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
201 }