Browse Source

feat(behavior): Add key toggle

xmkb
Caleb Goates 3 years ago committed by Pete Johanson
parent
commit
0d5bb100ba
  1. 1
      app/CMakeLists.txt
  2. 6
      app/Kconfig
  3. 1
      app/dts/behaviors.dtsi
  4. 15
      app/dts/behaviors/key_toggle.dtsi
  5. 8
      app/dts/bindings/behaviors/zmk,behavior-key-toggle.yaml
  6. 5
      app/include/zmk/hid.h
  7. 44
      app/src/behaviors/behavior_key_toggle.c
  8. 50
      app/src/hid.c
  9. 17
      app/tests/keytoggle/behavior_keymap.dtsi
  10. 1
      app/tests/keytoggle/kt-press-release/events.patterns
  11. 2
      app/tests/keytoggle/kt-press-release/keycode_events.snapshot
  12. 10
      app/tests/keytoggle/kt-press-release/native_posix_64.keymap

1
app/CMakeLists.txt

@ -40,6 +40,7 @@ target_sources_ifdef(CONFIG_ZMK_EXT_POWER app PRIVATE src/behaviors/behavior_ext
if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL) if ((NOT CONFIG_ZMK_SPLIT) OR CONFIG_ZMK_SPLIT_ROLE_CENTRAL)
target_sources(app PRIVATE src/hid.c) target_sources(app PRIVATE src/hid.c)
target_sources(app PRIVATE src/behaviors/behavior_key_press.c) target_sources(app PRIVATE src/behaviors/behavior_key_press.c)
target_sources_ifdef(CONFIG_ZMK_BEHAVIOR_KEY_TOGGLE app PRIVATE src/behaviors/behavior_key_toggle.c)
target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c) target_sources(app PRIVATE src/behaviors/behavior_hold_tap.c)
target_sources(app PRIVATE src/behaviors/behavior_sticky_key.c) target_sources(app PRIVATE src/behaviors/behavior_sticky_key.c)
target_sources(app PRIVATE src/behaviors/behavior_caps_word.c) target_sources(app PRIVATE src/behaviors/behavior_caps_word.c)

6
app/Kconfig

@ -330,6 +330,12 @@ config ZMK_BEHAVIORS_QUEUE_SIZE
int "Maximum number of behaviors to allow queueing from a macro or other complex behavior" int "Maximum number of behaviors to allow queueing from a macro or other complex behavior"
default 64 default 64
DT_COMPAT_ZMK_BEHAVIOR_KEY_TOGGLE := zmk,behavior-key-toggle
config ZMK_BEHAVIOR_KEY_TOGGLE
bool
default $(dt_compat_enabled,$(DT_COMPAT_ZMK_BEHAVIOR_KEY_TOGGLE))
endmenu endmenu
menu "Advanced" menu "Advanced"

1
app/dts/behaviors.dtsi

@ -1,4 +1,5 @@
#include <behaviors/key_press.dtsi> #include <behaviors/key_press.dtsi>
#include <behaviors/key_toggle.dtsi>
#include <behaviors/transparent.dtsi> #include <behaviors/transparent.dtsi>
#include <behaviors/none.dtsi> #include <behaviors/none.dtsi>
#include <behaviors/mod_tap.dtsi> #include <behaviors/mod_tap.dtsi>

15
app/dts/behaviors/key_toggle.dtsi

@ -0,0 +1,15 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
/ {
behaviors {
/omit-if-no-ref/ kt: behavior_key_toggle {
compatible = "zmk,behavior-key-toggle";
label = "KEY_TOGGLE";
#binding-cells = <1>;
};
};
};

8
app/dts/bindings/behaviors/zmk,behavior-key-toggle.yaml

@ -0,0 +1,8 @@
# Copyright (c) 2022 The ZMK Contributors
# SPDX-License-Identifier: MIT
description: Key toggle behavior
compatible: "zmk,behavior-key-toggle"
include: one_param.yaml

5
app/include/zmk/hid.h

@ -129,6 +129,8 @@ struct zmk_hid_consumer_report {
zmk_mod_flags_t zmk_hid_get_explicit_mods(); zmk_mod_flags_t zmk_hid_get_explicit_mods();
int zmk_hid_register_mod(zmk_mod_t modifier); int zmk_hid_register_mod(zmk_mod_t modifier);
int zmk_hid_unregister_mod(zmk_mod_t modifier); int zmk_hid_unregister_mod(zmk_mod_t modifier);
bool zmk_hid_mod_is_pressed(zmk_mod_t modifier);
int zmk_hid_register_mods(zmk_mod_flags_t explicit_modifiers); int zmk_hid_register_mods(zmk_mod_flags_t explicit_modifiers);
int zmk_hid_unregister_mods(zmk_mod_flags_t explicit_modifiers); int zmk_hid_unregister_mods(zmk_mod_flags_t explicit_modifiers);
int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t implicit_modifiers); int zmk_hid_implicit_modifiers_press(zmk_mod_flags_t implicit_modifiers);
@ -137,13 +139,16 @@ int zmk_hid_implicit_modifiers_release();
int zmk_hid_keyboard_press(zmk_key_t key); int zmk_hid_keyboard_press(zmk_key_t key);
int zmk_hid_keyboard_release(zmk_key_t key); int zmk_hid_keyboard_release(zmk_key_t key);
void zmk_hid_keyboard_clear(); void zmk_hid_keyboard_clear();
bool zmk_hid_keyboard_is_pressed(zmk_key_t key);
int zmk_hid_consumer_press(zmk_key_t key); int zmk_hid_consumer_press(zmk_key_t key);
int zmk_hid_consumer_release(zmk_key_t key); int zmk_hid_consumer_release(zmk_key_t key);
void zmk_hid_consumer_clear(); void zmk_hid_consumer_clear();
bool zmk_hid_consumer_is_pressed(zmk_key_t key);
int zmk_hid_press(uint32_t usage); int zmk_hid_press(uint32_t usage);
int zmk_hid_release(uint32_t usage); int zmk_hid_release(uint32_t usage);
bool zmk_hid_is_pressed(uint32_t usage);
struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report(); struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report();
struct zmk_hid_consumer_report *zmk_hid_get_consumer_report(); struct zmk_hid_consumer_report *zmk_hid_get_consumer_report();

44
app/src/behaviors/behavior_key_toggle.c

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_key_toggle
#include <device.h>
#include <drivers/behavior.h>
#include <logging/log.h>
#include <zmk/hid.h>
#include <zmk/event_manager.h>
#include <zmk/events/keycode_state_changed.h>
#include <zmk/behavior.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
static int behavior_key_toggle_init(const struct device *dev) { return 0; }
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
LOG_DBG("position %d keycode 0x%02X", event.position, binding->param1);
bool pressed = zmk_hid_is_pressed(binding->param1);
return ZMK_EVENT_RAISE(
zmk_keycode_state_changed_from_encoded(binding->param1, !pressed, event.timestamp));
}
static int on_keymap_binding_released(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
return 0;
}
static const struct behavior_driver_api behavior_key_toggle_driver_api = {
.binding_pressed = on_keymap_binding_pressed,
.binding_released = on_keymap_binding_released,
};
#define KT_INST(n) \
DEVICE_DT_INST_DEFINE(n, behavior_key_toggle_init, NULL, NULL, NULL, APPLICATION, \
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_key_toggle_driver_api);
DT_INST_FOREACH_STATUS_OKAY(KT_INST)

50
app/src/hid.c

@ -4,6 +4,7 @@
* SPDX-License-Identifier: MIT * SPDX-License-Identifier: MIT
*/ */
#include "zmk/keys.h"
#include <logging/log.h> #include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
@ -55,6 +56,11 @@ int zmk_hid_unregister_mod(zmk_mod_t modifier) {
return current == GET_MODIFIERS ? 0 : 1; return current == GET_MODIFIERS ? 0 : 1;
} }
bool zmk_hid_mod_is_pressed(zmk_mod_t modifier) {
zmk_mod_flags_t mod_flag = 1 << modifier;
return (zmk_hid_get_explicit_mods() & mod_flag) == mod_flag;
}
int zmk_hid_register_mods(zmk_mod_flags_t modifiers) { int zmk_hid_register_mods(zmk_mod_flags_t modifiers) {
int ret = 0; int ret = 0;
for (zmk_mod_t i = 0; i < 8; i++) { for (zmk_mod_t i = 0; i < 8; i++) {
@ -96,6 +102,13 @@ static inline int deselect_keyboard_usage(zmk_key_t usage) {
return 0; return 0;
} }
static inline bool check_keyboard_usage(zmk_key_t usage) {
if (usage > ZMK_HID_KEYBOARD_NKRO_MAX_USAGE) {
return false;
}
return keyboard_report.body.keys[usage / 8] & (1 << (usage % 8));
}
#elif IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_HKRO) #elif IS_ENABLED(CONFIG_ZMK_HID_REPORT_TYPE_HKRO)
#define TOGGLE_KEYBOARD(match, val) \ #define TOGGLE_KEYBOARD(match, val) \
@ -119,6 +132,15 @@ static inline int deselect_keyboard_usage(zmk_key_t usage) {
return 0; return 0;
} }
static inline int check_keyboard_usage(zmk_key_t usage) {
for (int idx = 0; idx < CONFIG_ZMK_HID_KEYBOARD_REPORT_SIZE; idx++) {
if (keyboard_report.body.keys[idx] == usage) {
return true;
}
}
return false;
}
#else #else
#error "A proper HID report type must be selected" #error "A proper HID report type must be selected"
#endif #endif
@ -164,6 +186,13 @@ int zmk_hid_keyboard_release(zmk_key_t code) {
return 0; return 0;
}; };
bool zmk_hid_keyboard_is_pressed(zmk_key_t code) {
if (code >= HID_USAGE_KEY_KEYBOARD_LEFTCONTROL && code <= HID_USAGE_KEY_KEYBOARD_RIGHT_GUI) {
return zmk_hid_mod_is_pressed(code - HID_USAGE_KEY_KEYBOARD_LEFTCONTROL);
}
return check_keyboard_usage(code);
}
void zmk_hid_keyboard_clear() { memset(&keyboard_report.body, 0, sizeof(keyboard_report.body)); } void zmk_hid_keyboard_clear() { memset(&keyboard_report.body, 0, sizeof(keyboard_report.body)); }
int zmk_hid_consumer_press(zmk_key_t code) { int zmk_hid_consumer_press(zmk_key_t code) {
@ -176,6 +205,17 @@ int zmk_hid_consumer_release(zmk_key_t code) {
return 0; return 0;
}; };
void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer_report.body)); }
bool zmk_hid_consumer_is_pressed(zmk_key_t key) {
for (int idx = 0; idx < CONFIG_ZMK_HID_CONSUMER_REPORT_SIZE; idx++) {
if (consumer_report.body.keys[idx] == key) {
return true;
}
}
return false;
}
int zmk_hid_press(uint32_t usage) { int zmk_hid_press(uint32_t usage) {
switch (ZMK_HID_USAGE_PAGE(usage)) { switch (ZMK_HID_USAGE_PAGE(usage)) {
case HID_USAGE_KEY: case HID_USAGE_KEY:
@ -196,7 +236,15 @@ int zmk_hid_release(uint32_t usage) {
return -EINVAL; return -EINVAL;
} }
void zmk_hid_consumer_clear() { memset(&consumer_report.body, 0, sizeof(consumer_report.body)); } bool zmk_hid_is_pressed(uint32_t usage) {
switch (ZMK_HID_USAGE_PAGE(usage)) {
case HID_USAGE_KEY:
return zmk_hid_keyboard_is_pressed(ZMK_HID_USAGE_ID(usage));
case HID_USAGE_CONSUMER:
return zmk_hid_consumer_is_pressed(ZMK_HID_USAGE_ID(usage));
}
return false;
}
struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() { struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report() {
return &keyboard_report; return &keyboard_report;

17
app/tests/keytoggle/behavior_keymap.dtsi

@ -0,0 +1,17 @@
#include <dt-bindings/zmk/keys.h>
#include <behaviors.dtsi>
#include <dt-bindings/zmk/kscan_mock.h>
/ {
keymap {
compatible = "zmk,keymap";
label ="Default keymap";
default_layer {
bindings = <
&kt B &none
&none &none
>;
};
};
};

1
app/tests/keytoggle/kt-press-release/events.patterns

@ -0,0 +1 @@
s/.*hid_listener_keycode_//p

2
app/tests/keytoggle/kt-press-release/keycode_events.snapshot

@ -0,0 +1,2 @@
pressed: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00
released: usage_page 0x07 keycode 0x05 implicit_mods 0x00 explicit_mods 0x00

10
app/tests/keytoggle/kt-press-release/native_posix_64.keymap

@ -0,0 +1,10 @@
#include "../behavior_keymap.dtsi"
&kscan {
events = <
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
ZMK_MOCK_PRESS(0,0,10)
ZMK_MOCK_RELEASE(0,0,10)
>;
};
Loading…
Cancel
Save