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