You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
208 lines
7.7 KiB
C
208 lines
7.7 KiB
C
14 years ago
|
/*
|
||
|
Copyright 2011 Jun Wako <wakojun@gmail.com>
|
||
|
|
||
|
This program is free software: you can redistribute it and/or modify
|
||
|
it under the terms of the GNU General Public License as published by
|
||
|
the Free Software Foundation, either version 2 of the License, or
|
||
|
(at your option) any later version.
|
||
|
|
||
|
This program is distributed in the hope that it will be useful,
|
||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
GNU General Public License for more details.
|
||
|
|
||
|
You should have received a copy of the GNU General Public License
|
||
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include "keymap.h"
|
||
14 years ago
|
#include "host.h"
|
||
14 years ago
|
#include "debug.h"
|
||
|
#include "timer.h"
|
||
13 years ago
|
#include "usb_keycodes.h"
|
||
14 years ago
|
#include "layer.h"
|
||
|
|
||
14 years ago
|
|
||
14 years ago
|
/*
|
||
14 years ago
|
* Parameters:
|
||
13 years ago
|
* SWITCH_DELAY |=======|
|
||
14 years ago
|
* SEND_FN_TERM |================|
|
||
14 years ago
|
*
|
||
|
* Fn key processing cases:
|
||
14 years ago
|
* 1. release Fn after SEND_FN_TERM.
|
||
14 years ago
|
* Layer sw ___________|~~~~~~~~~~~|___
|
||
|
* Fn press ___|~~~~~~~~~~~~~~~~~~~|___
|
||
|
* Fn send ___________________________
|
||
|
*
|
||
14 years ago
|
* 2. release Fn during SEND_FN_TERM.(not layer used)
|
||
14 years ago
|
* Layer sw ___________|~~~~~~|________
|
||
|
* Fn press ___|~~~~~~~~~~~~~~|________
|
||
|
* Fn key send __________________|~|______
|
||
|
* other key press ___________________________
|
||
|
* other key send ___________________________
|
||
|
*
|
||
14 years ago
|
* 3. release Fn during SEND_FN_TERM.(layer used)
|
||
14 years ago
|
* Layer sw ___________|~~~~~~|________
|
||
|
* Fn press ___|~~~~~~~~~~~~~~|________
|
||
|
* Fn key send ___________________________
|
||
|
* Fn send ___________________________
|
||
|
* other key press _____________|~~|__________
|
||
|
* other key send _____________|~~|__________
|
||
|
*
|
||
13 years ago
|
* 4. press other key during SWITCH_DELAY.
|
||
14 years ago
|
* Layer sw ___________________________
|
||
|
* Fn key press ___|~~~~~~~~~|_____________
|
||
|
* Fn key send ______|~~~~~~|_____________
|
||
|
* other key press ______|~~~|________________
|
||
|
* other key send _______|~~|________________
|
||
|
*
|
||
|
* 5. press Fn while press other key.
|
||
|
* Layer sw ___________________________
|
||
|
* Fn key press ___|~~~~~~~~~|_____________
|
||
|
* Fn key send ___|~~~~~~~~~|_____________
|
||
|
* other key press ~~~~~~~|___________________
|
||
|
* other key send ~~~~~~~|___________________
|
||
14 years ago
|
*
|
||
|
* 6. press Fn twice quickly and keep holding down.(repeat)
|
||
|
* Layer sw ___________________________
|
||
|
* Fn key press ___|~|____|~~~~~~~~~~~~~~~~
|
||
|
* Fn key send _____|~|__|~~~~~~~~~~~~~~~~
|
||
14 years ago
|
*/
|
||
|
|
||
13 years ago
|
// LAYER_SWITCH_DELAY: prevent from moving to new layer
|
||
|
#ifndef LAYER_SWITCH_DELAY
|
||
|
# define LAYER_SWITCH_DELAY 150
|
||
|
#endif
|
||
14 years ago
|
|
||
14 years ago
|
// LAYER_SEND_FN_TERM: send keycode if release key in this term
|
||
13 years ago
|
#ifndef LAYER_SEND_FN_TERM
|
||
|
# define LAYER_SEND_FN_TERM 500
|
||
|
#endif
|
||
14 years ago
|
|
||
|
|
||
14 years ago
|
uint8_t default_layer = 0;
|
||
|
uint8_t current_layer = 0;
|
||
|
|
||
14 years ago
|
static bool layer_used = false;
|
||
14 years ago
|
static uint8_t new_layer(uint8_t fn_bits);
|
||
14 years ago
|
|
||
|
|
||
|
uint8_t layer_get_keycode(uint8_t row, uint8_t col)
|
||
|
{
|
||
|
uint8_t code = keymap_get_keycode(current_layer, row, col);
|
||
|
// normal key or mouse key
|
||
14 years ago
|
if ((IS_KEY(code) || IS_MOUSEKEY(code))) {
|
||
14 years ago
|
layer_used = true;
|
||
14 years ago
|
}
|
||
14 years ago
|
return code;
|
||
|
}
|
||
|
|
||
14 years ago
|
// bit substract b from a
|
||
14 years ago
|
#define BIT_SUBST(a, b) (a&(a^b))
|
||
14 years ago
|
void layer_switching(uint8_t fn_bits)
|
||
|
{
|
||
|
// layer switching
|
||
14 years ago
|
static uint8_t last_fn = 0;
|
||
14 years ago
|
static uint8_t last_mods = 0;
|
||
14 years ago
|
static uint16_t last_timer = 0;
|
||
14 years ago
|
static uint8_t sent_fn = 0;
|
||
14 years ago
|
|
||
14 years ago
|
if (fn_bits == last_fn) { // Fn state is not changed
|
||
|
if (fn_bits == 0) {
|
||
|
// do nothing
|
||
14 years ago
|
} else {
|
||
13 years ago
|
if (!keymap_fn_keycode(BIT_SUBST(fn_bits, sent_fn)) ||
|
||
|
timer_elapsed(last_timer) > LAYER_SWITCH_DELAY) {
|
||
14 years ago
|
uint8_t _layer_to_switch = new_layer(BIT_SUBST(fn_bits, sent_fn));
|
||
14 years ago
|
if (current_layer != _layer_to_switch) { // not switch layer yet
|
||
13 years ago
|
debug("Fn case: 1,2,3(LAYER_SWITCH_DELAY passed)\n");
|
||
14 years ago
|
debug("Switch Layer: "); debug_hex(current_layer);
|
||
|
current_layer = _layer_to_switch;
|
||
|
layer_used = false;
|
||
|
debug(" -> "); debug_hex(current_layer); debug("\n");
|
||
|
}
|
||
|
} else {
|
||
14 years ago
|
if (host_has_anykey()) { // other keys is pressed
|
||
14 years ago
|
uint8_t _fn_to_send = BIT_SUBST(fn_bits, sent_fn);
|
||
14 years ago
|
if (_fn_to_send) {
|
||
13 years ago
|
debug("Fn case: 4(press other key during SWITCH_DELAY.)\n");
|
||
14 years ago
|
// send only Fn key first
|
||
13 years ago
|
uint8_t tmp_mods = keyboard_report->mods;
|
||
|
host_add_code(keymap_fn_keycode(_fn_to_send));
|
||
14 years ago
|
host_set_mods(last_mods);
|
||
|
host_send_keyboard_report();
|
||
13 years ago
|
host_set_mods(tmp_mods);
|
||
|
host_del_code(keymap_fn_keycode(_fn_to_send));
|
||
14 years ago
|
sent_fn |= _fn_to_send;
|
||
|
}
|
||
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
// add Fn keys to send
|
||
14 years ago
|
//host_add_code(keymap_fn_keycode(fn_bits&sent_fn)); // TODO: do all Fn keys
|
||
14 years ago
|
}
|
||
14 years ago
|
} else { // Fn state is changed(edge)
|
||
|
uint8_t fn_changed = 0;
|
||
|
|
||
|
debug("fn_bits: "); debug_bin(fn_bits); debug("\n");
|
||
|
debug("sent_fn: "); debug_bin(sent_fn); debug("\n");
|
||
|
debug("last_fn: "); debug_bin(last_fn); debug("\n");
|
||
|
debug("last_mods: "); debug_hex(last_mods); debug("\n");
|
||
|
debug("last_timer: "); debug_hex16(last_timer); debug("\n");
|
||
13 years ago
|
debug("timer_count: "); debug_hex16(timer_count); debug("\n");
|
||
14 years ago
|
|
||
|
// pressed Fn
|
||
14 years ago
|
if ((fn_changed = BIT_SUBST(fn_bits, last_fn))) {
|
||
13 years ago
|
debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
|
||
14 years ago
|
if (host_has_anykey()) {
|
||
14 years ago
|
debug("Fn case: 5(pressed Fn with other key)\n");
|
||
|
sent_fn |= fn_changed;
|
||
|
} else if (fn_changed & sent_fn) { // pressed same Fn in a row
|
||
13 years ago
|
if (timer_elapsed(last_timer) > LAYER_SEND_FN_TERM) {
|
||
14 years ago
|
debug("Fn case: 6(not repeat)\n");
|
||
14 years ago
|
// time passed: not repeate
|
||
|
sent_fn &= ~fn_changed;
|
||
|
} else {
|
||
14 years ago
|
debug("Fn case: 6(repeat)\n");
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
// released Fn
|
||
14 years ago
|
if ((fn_changed = BIT_SUBST(last_fn, fn_bits))) {
|
||
13 years ago
|
debug("fn_changed: "); debug_bin(fn_changed); debug("\n");
|
||
14 years ago
|
if (timer_elapsed(last_timer) < LAYER_SEND_FN_TERM) {
|
||
14 years ago
|
if (!layer_used && BIT_SUBST(fn_changed, sent_fn)) {
|
||
14 years ago
|
debug("Fn case: 2(send Fn one shot: released Fn during LAYER_SEND_FN_TERM)\n");
|
||
|
// send only Fn key first
|
||
13 years ago
|
uint8_t tmp_mods = keyboard_report->mods;
|
||
|
host_add_code(keymap_fn_keycode(fn_changed));
|
||
14 years ago
|
host_set_mods(last_mods);
|
||
|
host_send_keyboard_report();
|
||
13 years ago
|
host_set_mods(tmp_mods);
|
||
|
host_del_code(keymap_fn_keycode(fn_changed));
|
||
14 years ago
|
sent_fn |= fn_changed;
|
||
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
debug("Switch Layer(released Fn): "); debug_hex(current_layer);
|
||
14 years ago
|
current_layer = new_layer(BIT_SUBST(fn_bits, sent_fn));
|
||
14 years ago
|
debug(" -> "); debug_hex(current_layer); debug("\n");
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
14 years ago
|
layer_used = false;
|
||
14 years ago
|
last_fn = fn_bits;
|
||
14 years ago
|
last_mods = keyboard_report->mods;
|
||
14 years ago
|
last_timer = timer_read();
|
||
14 years ago
|
}
|
||
14 years ago
|
// send Fn keys
|
||
|
for (uint8_t i = 0; i < 8; i++) {
|
||
|
if ((sent_fn & fn_bits) & (1<<i)) {
|
||
14 years ago
|
host_add_code(keymap_fn_keycode(1<<i));
|
||
14 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
inline
|
||
|
static uint8_t new_layer(uint8_t fn_bits)
|
||
|
{
|
||
|
return (fn_bits ? keymap_fn_layer(fn_bits) : default_layer);
|
||
14 years ago
|
}
|