explicit modifiers

master
Stefan Dorn 2016-06-14 09:39:45 +01:00
parent 9c81274348
commit f52da5065a
5 changed files with 710 additions and 707 deletions

View File

@ -30,16 +30,19 @@ class Key
Layers = [ :basic, :punc, :nav, :func ]
Functions = {
# down up
"basic" => [ "&kbfun_press_release", ],
"media" => [ "&kbfun_mediakey_press_release", ],
# down up
"layer" => [ "&kbfun_layer_enable", "&kbfun_layer_disable" ],
"latch" => [ "&kbfun_layer_sticky", ],
# down up
"shifted" => [ "&kbfun_shift_press_release", ],
"ctrled" => [ "&kbfun_control_press_release", ],
"capslock" => [ "&kbfun_2_keys_capslock_press_release", ],
# down up
"basic" => [ "&kbfun_normal_press_release", ],
"media" => [ "&kbfun_mediakey_press_release", ],
# down up
"mod" => [ "&kbfun_modifier_press_release", ],
"sticky_mod" => [ "&kbfun_modifier_sticky", ],
# down up
"layer" => [ "&kbfun_layer_enable", "&kbfun_layer_disable" ],
"sticky" => [ "&kbfun_layer_sticky", ],
# down up
"shifted" => [ "&kbfun_shift_press_release", ],
"ctrled" => [ "&kbfun_control_press_release", ],
"capslock" => [ "&kbfun_capslock_press_release", ],
}
Keys = {
@ -157,15 +160,15 @@ class Key
"insert" => "KEY_Insert",
"menu" => "KEY_Application",
#
"alt" => "KEY_LeftAlt",
"alt_gr" => "KEY_RightAlt",
"umlaut" => "KEY_RightAlt",
"control" => "KEY_LeftControl",
"control_l" => "KEY_LeftControl",
"control_r" => "KEY_RightControl",
"win" => "KEY_LeftGUI",
"shift_l" => "KEY_LeftShift",
"shift_r" => "KEY_RightShift",
"alt" => "MOD_KEY_LeftAlt",
"alt_gr" => "MOD_KEY_RightAlt",
"umlaut" => "MOD_KEY_RightAlt",
"control" => "MOD_KEY_LeftControl",
"control_l" => "MOD_KEY_LeftControl",
"control_r" => "MOD_KEY_RightControl",
"win" => "MOD_KEY_LeftGUI",
"shift_l" => "MOD_KEY_LeftShift",
"shift_r" => "MOD_KEY_RightShift",
"scroll_lock" => "KEY_ScrollLock",
#
"NULL" => "KEY_NULL",
@ -206,13 +209,32 @@ class Key
next
end
# FIXME better defaults
type ||= "basic"
keycode = Keys[key]
up, down = Functions[type] || Functions["basic"]
up, down = Functions[type]
down ||= up
raise "key not found: #{key}" if keycode.nil?
raise "type not found: #{type}" if up.nil? or down.nil?
case type
when "layer", "stitcky"
raise "invalid layer: #{key}" unless key.to_i < Key::Layers.size
when "mod", "sticky_mod"
raise "invalid modifier: #{key}" unless keycode.start_with? "MOD_KEY"
end
case keycode
when /^MOD_KEY/
case type
when "mod", "sticky_mod", "capslock"; # pass
else
raise "invalid modifier keyfunc: #{key}, #{type}"
end
end
@layers[i] = Layer.new(keycode, down, up)
end
@ -281,7 +303,7 @@ keys = [
%w{ tab }, %w{ }, %w{ }, %w{ }, # 1.5
# home
# letter type punc type nav type func type
%w{ umlaut }, %w{ }, %w{ }, %w{ }, # 1.5
%w{ umlaut mod }, %w{ }, %w{ }, %w{ }, # 1.5
%w{ u }, %w{ , }, %w{ left }, %w{ audio_mute media },
%w{ i }, %w{ \{ shifted }, %w{ up }, %w{ audio_vol_up media },
%w{ a }, %w{ ? shifted }, %w{ down }, %w{ audio_vol_down media },
@ -302,7 +324,7 @@ keys = [
%w{ up }, %w{ }, %w{ }, %w{ },
%w{ down }, %w{ }, %w{ }, %w{ },
%w{ right }, %w{ }, %w{ }, %w{ },
%w{ win }, %w{ }, %w{ }, %w{ },
%w{ win mod }, %w{ }, %w{ }, %w{ },
# thumb-top
# letter type punc type nav type func type
%w{ scroll_lock }, %w{ }, %w{ }, %w{ },
@ -310,13 +332,13 @@ keys = [
# thumb-double
# letter type punc type nav type func type
%w{ space }, %w{ }, %w{ }, %w{ },
%w{ control }, %w{ }, %w{ }, %w{ },
%w{ alt }, %w{ }, %w{ }, %w{ },
%w{ control mod }, %w{ }, %w{ }, %w{ },
%w{ alt mod }, %w{ }, %w{ }, %w{ },
# thumb-home
# letter type punc type nav type func type
%w{ space }, %w{ }, %w{ }, %w{ },
%w{ control }, %w{ }, %w{ }, %w{ },
%w{ alt }, %w{ }, %w{ }, %w{ },
%w{ control mod }, %w{ }, %w{ }, %w{ },
%w{ alt mod }, %w{ }, %w{ }, %w{ },
#
# right hand
#
@ -345,7 +367,7 @@ keys = [
%w{ r }, %w{ / }, %w{ 2 }, %w{ f2 },
%w{ t }, %w{ \} shifted }, %w{ 3 }, %w{ f3 },
%w{ d }, %w{ . }, %w{ 4 }, %w{ f4 },
%w{ umlaut }, %w{ }, %w{ }, %w{ }, # 1.5
%w{ umlaut mod }, %w{ }, %w{ }, %w{ }, # 1.5
# bottom
# letter type punc type nav type func type
%w{ enter }, %w{ }, %w{ }, %w{ }, # 1.5
@ -370,12 +392,12 @@ keys = [
# letter type punc type nav type func type
%w{ menu }, %w{ }, %w{ }, %w{ },
%w{ func layer }, %w{ }, %w{ }, %w{ },
%w{ punc latch }, %w{ punc layer }, %w{ NULL }, %w{ NULL },
%w{ punc sticky }, %w{ punc layer }, %w{ NULL }, %w{ NULL },
# thumb-home
# letter type punc type nav type func type
%w{ menu }, %w{ }, %w{ }, %w{ },
%w{ func layer }, %w{ }, %w{ }, %w{ },
%w{ punc latch }, %w{ punc layer }, %w{ NULL }, %w{ NULL },
%w{ punc sticky }, %w{ punc layer }, %w{ NULL }, %w{ NULL },
].each_slice(Key::Layers.size).map do |layers|
Key.new layers

View File

@ -352,3 +352,27 @@ static const uint16_t PROGMEM _media_code_lookup_table[] = {
AC_REFRESH, // MEDIAKEY_BROWSER_REFRESH
AC_BOOKMARKS, // MEDIAKEY_BROWSER_BOOKMARKS
};
// modifiers
#define MOD_KEY_LeftControl 0
#define MOD_KEY_LeftShift 1
#define MOD_KEY_LeftAlt 2
#define MOD_KEY_LeftGUI 3
#define MOD_KEY_RightControl 4
#define MOD_KEY_RightShift 5
#define MOD_KEY_RightAlt 6
#define MOD_KEY_RightGUI 7
static const uint8_t PROGMEM _modifier_lookup_table[] = {
KEY_LeftControl,
KEY_LeftShift,
KEY_LeftAlt,
KEY_LeftGUI,
KEY_RightControl,
KEY_RightShift,
KEY_RightAlt,
KEY_RightGUI,
};
#define MODIFIERS sizeof(_modifier_lookup_table)

File diff suppressed because it is too large Load Diff

View File

@ -254,75 +254,47 @@ keyfunc kb_keyfunc_release (layer l, u8 row, u8 col) { return (keyfunc) pgm_read
// ----------------------------------------------------------------------------
// basic keypresses
void _kbfun_press_release(bool press, keycode key) {
// no-op
if (key == 0) { return; }
void _kbfun_normal_press_release(bool press, keycode key) {
if (key == 0) { return; } // noop
if (press) {
_kbfun_press(key);
for (u8 i=0; i < sizeof(keyboard_keys); i++) {
if (keyboard_keys[i] == 0) {
keyboard_keys[i] = key;
return;
}
}
} else {
_kbfun_release(key);
}
}
void _kbfun_press(keycode key) {
// modifier keys
switch (key) {
case KEY_LeftControl: keyboard_modifier_keys |= (1<<0); return;
case KEY_LeftShift: keyboard_modifier_keys |= (1<<1); return;
case KEY_LeftAlt: keyboard_modifier_keys |= (1<<2); return;
case KEY_LeftGUI: keyboard_modifier_keys |= (1<<3); return;
case KEY_RightControl: keyboard_modifier_keys |= (1<<4); return;
case KEY_RightShift: keyboard_modifier_keys |= (1<<5); return;
case KEY_RightAlt: keyboard_modifier_keys |= (1<<6); return;
case KEY_RightGUI: keyboard_modifier_keys |= (1<<7); return;
}
// all others
for (u8 i=0; i < sizeof(keyboard_keys); i++) {
if (keyboard_keys[i] == 0) {
keyboard_keys[i] = key;
return;
for (u8 i=0; i < sizeof(keyboard_keys); i++) {
if (keyboard_keys[i] == key) {
keyboard_keys[i] = 0;
return;
}
}
}
}
void _kbfun_release(keycode key) {
// modifier keys
switch (key) {
case KEY_LeftControl: keyboard_modifier_keys &= ~(1<<0); return;
case KEY_LeftShift: keyboard_modifier_keys &= ~(1<<1); return;
case KEY_LeftAlt: keyboard_modifier_keys &= ~(1<<2); return;
case KEY_LeftGUI: keyboard_modifier_keys &= ~(1<<3); return;
case KEY_RightControl: keyboard_modifier_keys &= ~(1<<4); return;
case KEY_RightShift: keyboard_modifier_keys &= ~(1<<5); return;
case KEY_RightAlt: keyboard_modifier_keys &= ~(1<<6); return;
case KEY_RightGUI: keyboard_modifier_keys &= ~(1<<7); return;
}
// all others
for (u8 i=0; i < sizeof(keyboard_keys); i++) {
if (keyboard_keys[i] == key) {
keyboard_keys[i] = 0;
return;
void _kbfun_mediakey_press_release(bool press, keycode key) {
media_keycode media_key = _media_code_lookup_table[key];
if (press) {
consumer_key = media_key;
} else {
// only one media key can be pressed at a time, so only clear most recent one
if (media_key == consumer_key) {
consumer_key = 0;
}
}
}
bool _kbfun_is_pressed(keycode key) {
// modifier keys
switch (key) {
case KEY_LeftControl: return (keyboard_modifier_keys & (1<<0));
case KEY_LeftShift: return (keyboard_modifier_keys & (1<<1));
case KEY_LeftAlt: return (keyboard_modifier_keys & (1<<2));
case KEY_LeftGUI: return (keyboard_modifier_keys & (1<<3));
case KEY_RightControl: return (keyboard_modifier_keys & (1<<4));
case KEY_RightShift: return (keyboard_modifier_keys & (1<<5));
case KEY_RightAlt: return (keyboard_modifier_keys & (1<<6));
case KEY_RightGUI: return (keyboard_modifier_keys & (1<<7));
void _kbfun_modifier_press_release(bool press, keycode key) {
if (press) {
keyboard_modifier_keys |= (1<<key);
} else {
keyboard_modifier_keys &= ~(1<<key);
}
}
// all others
bool _kbfun_normal_is_pressed(keycode key) {
for (u8 i=0; i < sizeof(keyboard_keys); i++) {
if (keyboard_keys[i] == key) {
return true;
@ -332,16 +304,12 @@ bool _kbfun_is_pressed(keycode key) {
return false;
}
void _kbfun_mediakey_press_release(bool press, keycode key) {
media_keycode media_key = _media_code_lookup_table[key];
if (press) {
consumer_key = media_key;
} else {
// only one media key can be pressed at a time, so only clear most recent one
if (media_key == consumer_key) {
consumer_key = 0;
}
}
bool _kbfun_mediakey_is_pressed(keycode key) {
return (consumer_key != 0);
}
bool _kbfun_modifier_is_pressed(keycode key) {
return (keyboard_modifier_keys & (1<<key));
}
// ----------------------------------------------------------------------------
@ -365,33 +333,21 @@ void exec_key() {
}
}
bool key_is_modifier(keycode key) {
switch (key) {
case KEY_LeftControl: return true;
case KEY_LeftShift: return true;
case KEY_LeftAlt: return true;
case KEY_LeftGUI: return true;
case KEY_RightControl: return true;
case KEY_RightShift: return true;
case KEY_RightAlt: return true;
case KEY_RightGUI: return true;
default: return false;
}
}
// normal key
void kbfun_press_release() {
if (!key_is_modifier(current_keycode)) {
sticky_done = true;
}
_kbfun_press_release(current_is_pressed, current_keycode);
void kbfun_normal_press_release() {
sticky_done = true;
_kbfun_normal_press_release(current_is_pressed, current_keycode);
}
// media key
void kbfun_mediakey_press_release() {
sticky_done = true;
keycode key = current_keycode;
_kbfun_mediakey_press_release(current_is_pressed, key);
_kbfun_mediakey_press_release(current_is_pressed, current_keycode);
}
// modifier
void kbfun_modifier_press_release() {
_kbfun_modifier_press_release(current_is_pressed, current_keycode);
}
// enable layer
@ -449,17 +405,17 @@ void kbfun_layer_sticky() {
// ----------------------------------------------------------------------------
void _kbfun_combo_press_release(keycode combo_key) {
_kbfun_press_release(current_is_pressed, combo_key);
kbfun_press_release();
_kbfun_modifier_press_release(current_is_pressed, combo_key);
kbfun_normal_press_release();
}
void kbfun_shift_press_release() { _kbfun_combo_press_release(KEY_LeftShift); } // +shift
void kbfun_control_press_release() { _kbfun_combo_press_release(KEY_LeftControl); } // +control
void kbfun_alt_press_release() { _kbfun_combo_press_release(KEY_LeftAlt); } // +alt
void kbfun_win_press_release() { _kbfun_combo_press_release(KEY_LeftGUI); } // +win
void kbfun_shift_press_release() { _kbfun_combo_press_release(MOD_KEY_LeftShift); } // +shift
void kbfun_control_press_release() { _kbfun_combo_press_release(MOD_KEY_LeftControl); } // +control
void kbfun_alt_press_release() { _kbfun_combo_press_release(MOD_KEY_LeftAlt); } // +alt
void kbfun_win_press_release() { _kbfun_combo_press_release(MOD_KEY_LeftGUI); } // +win
// capslock
void kbfun_2_keys_capslock_press_release() {
void kbfun_capslock_press_release() {
static u8 keys_pressed;
static bool lshift_pressed;
static bool rshift_pressed;
@ -469,24 +425,24 @@ void kbfun_2_keys_capslock_press_release() {
if (!current_is_pressed) { keys_pressed--; }
// take care of the key that was actually pressed
_kbfun_press_release(current_is_pressed, key);
_kbfun_modifier_press_release(current_is_pressed, key);
// take care of capslock (only on the press of the 2nd key)
if (keys_pressed == 1 && current_is_pressed) {
// save the state of left and right shift
lshift_pressed = _kbfun_is_pressed(KEY_LeftShift);
rshift_pressed = _kbfun_is_pressed(KEY_RightShift);
lshift_pressed = _kbfun_modifier_is_pressed(MOD_KEY_LeftShift);
rshift_pressed = _kbfun_modifier_is_pressed(MOD_KEY_RightShift);
// disable both
_kbfun_press_release(false, KEY_LeftShift);
_kbfun_press_release(false, KEY_RightShift);
_kbfun_modifier_press_release(false, MOD_KEY_LeftShift);
_kbfun_modifier_press_release(false, MOD_KEY_RightShift);
// press capslock, then release it
_kbfun_press_release(true, KEY_CapsLock); usb_keyboard_send();
_kbfun_press_release(false, KEY_CapsLock); usb_keyboard_send();
_kbfun_normal_press_release(true, KEY_CapsLock); usb_keyboard_send();
_kbfun_normal_press_release(false, KEY_CapsLock); usb_keyboard_send();
// restore the state of left and right shift
if (lshift_pressed) { _kbfun_press_release(true, KEY_LeftShift); }
if (rshift_pressed) { _kbfun_press_release(true, KEY_RightShift); }
if (lshift_pressed) { _kbfun_modifier_press_release(true, MOD_KEY_LeftShift); }
if (rshift_pressed) { _kbfun_modifier_press_release(true, MOD_KEY_RightShift); }
}
if (current_is_pressed) { keys_pressed++; }

View File

@ -1,18 +1,19 @@
/* This file was automatically generated. Do not edit! */
void kbfun_2_keys_capslock_press_release();
void kbfun_capslock_press_release();
void kbfun_win_press_release();
void kbfun_alt_press_release();
void kbfun_control_press_release();
void kbfun_shift_press_release();
void _kbfun_combo_press_release(keycode combo_key);
void kbfun_modifier_press_release();
void kbfun_mediakey_press_release();
void kbfun_press_release();
bool key_is_modifier(keycode key);
void kbfun_normal_press_release();
bool _kbfun_modifier_is_pressed(keycode key);
bool _kbfun_mediakey_is_pressed(keycode key);
bool _kbfun_normal_is_pressed(keycode key);
void _kbfun_modifier_press_release(bool press,keycode key);
void _kbfun_mediakey_press_release(bool press,keycode key);
bool _kbfun_is_pressed(keycode key);
void _kbfun_release(keycode key);
void _kbfun_press(keycode key);
void _kbfun_press_release(bool press,keycode key);
void _kbfun_normal_press_release(bool press,keycode key);
keyfunc kb_keyfunc_release(layer l,u8 row,u8 col);
keyfunc kb_keyfunc_press(layer l,u8 row,u8 col);
void layer_enable_upto(layer max_layer);