refactor(core): Use /omit-if-no-ref/ for behaviors.

* Use lesser-known DT features to skip behaviors not referenced
  in the user keymap
* Update the behaviors to skip code if no nodes found.
* Remove some empty config/data structs where unused in
  behaviors.
This commit is contained in:
Pete Johanson 2021-01-18 01:09:51 -05:00
parent b4d63fb52c
commit b84d29c384
26 changed files with 62 additions and 38 deletions

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
bt: behavior_bluetooth { /omit-if-no-ref/ bt: behavior_bluetooth {
compatible = "zmk,behavior-bluetooth"; compatible = "zmk,behavior-bluetooth";
label = "BLUETOOTH"; label = "BLUETOOTH";
#binding-cells = <2>; #binding-cells = <2>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
ext_power: behavior_ext_power { /omit-if-no-ref/ ext_power: behavior_ext_power {
compatible = "zmk,behavior-ext-power"; compatible = "zmk,behavior-ext-power";
label = "EXT_POWER_BEHAVIOR"; label = "EXT_POWER_BEHAVIOR";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -7,7 +7,7 @@
/ { / {
behaviors { behaviors {
/* DEPRECATED: `cp` will be removed in the future */ /* DEPRECATED: `cp` will be removed in the future */
cp: kp: behavior_key_press { /omit-if-no-ref/ cp: kp: behavior_key_press {
compatible = "zmk,behavior-key-press"; compatible = "zmk,behavior-key-press";
label = "KEY_PRESS"; label = "KEY_PRESS";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
lt: behavior_layer_tap { /omit-if-no-ref/ lt: behavior_layer_tap {
compatible = "zmk,behavior-hold-tap"; compatible = "zmk,behavior-hold-tap";
label = "LAYER_TAP"; label = "LAYER_TAP";
#binding-cells = <2>; #binding-cells = <2>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
mt: behavior_mod_tap { /omit-if-no-ref/ mt: behavior_mod_tap {
compatible = "zmk,behavior-hold-tap"; compatible = "zmk,behavior-hold-tap";
label = "MOD_TAP"; label = "MOD_TAP";
#binding-cells = <2>; #binding-cells = <2>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
mo: behavior_momentary_layer { /omit-if-no-ref/ mo: behavior_momentary_layer {
compatible = "zmk,behavior-momentary-layer"; compatible = "zmk,behavior-momentary-layer";
label = "MO"; label = "MO";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
none: behavior_none { /omit-if-no-ref/ none: behavior_none {
compatible = "zmk,behavior-none"; compatible = "zmk,behavior-none";
label = "NONE"; label = "NONE";
#binding-cells = <0>; #binding-cells = <0>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
out: behavior_outputs { /omit-if-no-ref/ out: behavior_outputs {
compatible = "zmk,behavior-outputs"; compatible = "zmk,behavior-outputs";
label = "OUTPUTS"; label = "OUTPUTS";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -8,13 +8,13 @@
/ { / {
behaviors { behaviors {
reset: behavior_reset { /omit-if-no-ref/ reset: behavior_reset {
compatible = "zmk,behavior-reset"; compatible = "zmk,behavior-reset";
label = "RESET"; label = "RESET";
#binding-cells = <0>; #binding-cells = <0>;
}; };
bootloader: behavior_reset_dfu { /omit-if-no-ref/ bootloader: behavior_reset_dfu {
compatible = "zmk,behavior-reset"; compatible = "zmk,behavior-reset";
label = "BOOTLOADER_RESET"; label = "BOOTLOADER_RESET";
type = <RST_UF2>; type = <RST_UF2>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
rgb_ug: behavior_rgb_underglow { /omit-if-no-ref/ rgb_ug: behavior_rgb_underglow {
compatible = "zmk,behavior-rgb-underglow"; compatible = "zmk,behavior-rgb-underglow";
label = "RGB_UNDERGLOW"; label = "RGB_UNDERGLOW";
#binding-cells = <2>; #binding-cells = <2>;

View file

@ -7,7 +7,7 @@
/ { / {
behaviors { behaviors {
/* DEPRECATED: `inc_dec_cp` will be removed in the future */ /* DEPRECATED: `inc_dec_cp` will be removed in the future */
inc_dec_cp: inc_dec_kp: behavior_sensor_rotate_key_press { /omit-if-no-ref/ inc_dec_cp: inc_dec_kp: behavior_sensor_rotate_key_press {
compatible = "zmk,behavior-sensor-rotate-key-press"; compatible = "zmk,behavior-sensor-rotate-key-press";
label = "ENC_KEY_PRESS"; label = "ENC_KEY_PRESS";
#sensor-binding-cells = <2>; #sensor-binding-cells = <2>;

View file

@ -6,14 +6,14 @@
/ { / {
behaviors { behaviors {
sk: behavior_sticky_key { /omit-if-no-ref/ sk: behavior_sticky_key {
compatible = "zmk,behavior-sticky-key"; compatible = "zmk,behavior-sticky-key";
label = "STICKY_KEY"; label = "STICKY_KEY";
#binding-cells = <1>; #binding-cells = <1>;
release-after-ms = <1000>; release-after-ms = <1000>;
bindings = <&kp>; bindings = <&kp>;
}; };
sl: behavior_sticky_layer { /omit-if-no-ref/ sl: behavior_sticky_layer {
compatible = "zmk,behavior-sticky-key"; compatible = "zmk,behavior-sticky-key";
label = "STICKY_LAYER"; label = "STICKY_LAYER";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
to: behavior_to_layer { /omit-if-no-ref/ to: behavior_to_layer {
compatible = "zmk,behavior-to-layer"; compatible = "zmk,behavior-to-layer";
label = "TO_LAYER"; label = "TO_LAYER";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
tog: behavior_toggle_layer { /omit-if-no-ref/ tog: behavior_toggle_layer {
compatible = "zmk,behavior-toggle-layer"; compatible = "zmk,behavior-toggle-layer";
label = "TOGGLE_LAYER"; label = "TOGGLE_LAYER";
#binding-cells = <1>; #binding-cells = <1>;

View file

@ -6,7 +6,7 @@
/ { / {
behaviors { behaviors {
trans: behavior_transparent { /omit-if-no-ref/ trans: behavior_transparent {
compatible = "zmk,behavior-transparent"; compatible = "zmk,behavior-transparent";
label = "TRANS"; label = "TRANS";
#binding-cells = <0>; #binding-cells = <0>;

View file

@ -17,6 +17,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#include <zmk/ble.h> #include <zmk/ble.h>
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
switch (binding->param1) { switch (binding->param1) {
@ -49,3 +51,5 @@ static const struct behavior_driver_api behavior_bt_driver_api = {
DEVICE_AND_API_INIT(behavior_bt, DT_INST_LABEL(0), behavior_bt_init, NULL, NULL, APPLICATION, DEVICE_AND_API_INIT(behavior_bt, DT_INST_LABEL(0), behavior_bt_init, NULL, NULL, APPLICATION,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_bt_driver_api); CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_bt_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -16,6 +16,8 @@
#include <logging/log.h> #include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
const struct device *ext_power = device_get_binding("EXT_POWER"); const struct device *ext_power = device_get_binding("EXT_POWER");
@ -55,3 +57,5 @@ static const struct behavior_driver_api behavior_ext_power_driver_api = {
DEVICE_AND_API_INIT(behavior_ext_power, DT_INST_LABEL(0), behavior_ext_power_init, NULL, NULL, DEVICE_AND_API_INIT(behavior_ext_power, DT_INST_LABEL(0), behavior_ext_power_init, NULL, NULL,
APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY, &behavior_ext_power_driver_api); APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY, &behavior_ext_power_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -22,7 +22,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_NODE_EXISTS(DT_DRV_INST(0)) #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
#define ZMK_BHV_HOLD_TAP_MAX_HELD 10 #define ZMK_BHV_HOLD_TAP_MAX_HELD 10
#define ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS 40 #define ZMK_BHV_HOLD_TAP_MAX_CAPTURED_EVENTS 40
@ -538,4 +538,4 @@ static struct behavior_hold_tap_data behavior_hold_tap_data;
DT_INST_FOREACH_STATUS_OKAY(KP_INST) DT_INST_FOREACH_STATUS_OKAY(KP_INST)
#endif #endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -15,8 +15,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
struct behavior_none_config {}; #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
struct behavior_none_data {};
static int behavior_none_init(const struct device *dev) { return 0; }; static int behavior_none_init(const struct device *dev) { return 0; };
@ -35,10 +34,7 @@ static const struct behavior_driver_api behavior_none_driver_api = {
.binding_released = on_keymap_binding_released, .binding_released = on_keymap_binding_released,
}; };
static const struct behavior_none_config behavior_none_config = {}; DEVICE_AND_API_INIT(behavior_none, DT_INST_LABEL(0), behavior_none_init, NULL, NULL, APPLICATION,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_none_driver_api);
static struct behavior_none_data behavior_none_data; #endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
DEVICE_AND_API_INIT(behavior_none, DT_INST_LABEL(0), behavior_none_init, &behavior_none_data,
&behavior_none_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&behavior_none_driver_api);

View file

@ -18,6 +18,8 @@
#include <logging/log.h> #include <logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) { struct zmk_behavior_binding_event event) {
switch (binding->param1) { switch (binding->param1) {
@ -42,3 +44,5 @@ static const struct behavior_driver_api behavior_outputs_driver_api = {
DEVICE_AND_API_INIT(behavior_out, DT_INST_LABEL(0), behavior_out_init, NULL, NULL, APPLICATION, DEVICE_AND_API_INIT(behavior_out, DT_INST_LABEL(0), behavior_out_init, NULL, NULL, APPLICATION,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_outputs_driver_api); CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_outputs_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -15,6 +15,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
struct behavior_reset_config { struct behavior_reset_config {
int type; int type;
}; };
@ -45,3 +46,5 @@ static const struct behavior_driver_api behavior_reset_driver_api = {
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_reset_driver_api); CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_reset_driver_api);
DT_INST_FOREACH_STATUS_OKAY(RST_INST) DT_INST_FOREACH_STATUS_OKAY(RST_INST)
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -16,6 +16,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
static int behavior_rgb_underglow_init(const struct device *dev) { return 0; } static int behavior_rgb_underglow_init(const struct device *dev) { return 0; }
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
@ -64,3 +66,5 @@ static const struct behavior_driver_api behavior_rgb_underglow_driver_api = {
DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, NULL, DEVICE_AND_API_INIT(behavior_rgb_underglow, DT_INST_LABEL(0), behavior_rgb_underglow_init, NULL,
NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&behavior_rgb_underglow_driver_api); &behavior_rgb_underglow_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -16,6 +16,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
static int behavior_sensor_rotate_key_press_init(const struct device *dev) { return 0; }; static int behavior_sensor_rotate_key_press_init(const struct device *dev) { return 0; };
static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding, static int on_sensor_binding_triggered(struct zmk_behavior_binding *binding,
@ -63,3 +65,5 @@ static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_
&behavior_sensor_rotate_key_press_driver_api); &behavior_sensor_rotate_key_press_driver_api);
DT_INST_FOREACH_STATUS_OKAY(KP_INST) DT_INST_FOREACH_STATUS_OKAY(KP_INST)
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -15,6 +15,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
static int behavior_to_init(const struct device *dev) { return 0; }; static int behavior_to_init(const struct device *dev) { return 0; };
static int to_keymap_binding_pressed(struct zmk_behavior_binding *binding, static int to_keymap_binding_pressed(struct zmk_behavior_binding *binding,
@ -37,3 +39,5 @@ static const struct behavior_driver_api behavior_to_driver_api = {
DEVICE_AND_API_INIT(behavior_to, DT_INST_LABEL(0), behavior_to_init, NULL, NULL, APPLICATION, DEVICE_AND_API_INIT(behavior_to, DT_INST_LABEL(0), behavior_to_init, NULL, NULL, APPLICATION,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_to_driver_api); CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_to_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -15,6 +15,8 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
struct behavior_tog_config {}; struct behavior_tog_config {};
struct behavior_tog_data {}; struct behavior_tog_data {};
@ -44,3 +46,5 @@ static struct behavior_tog_data behavior_tog_data;
DEVICE_AND_API_INIT(behavior_tog, DT_INST_LABEL(0), behavior_tog_init, &behavior_tog_data, DEVICE_AND_API_INIT(behavior_tog, DT_INST_LABEL(0), behavior_tog_init, &behavior_tog_data,
&behavior_tog_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_tog_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&behavior_tog_driver_api); &behavior_tog_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */

View file

@ -15,8 +15,7 @@
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
struct behavior_transparent_config {}; #if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
struct behavior_transparent_data {};
static int behavior_transparent_init(const struct device *dev) { return 0; }; static int behavior_transparent_init(const struct device *dev) { return 0; };
@ -35,10 +34,8 @@ static const struct behavior_driver_api behavior_transparent_driver_api = {
.binding_released = on_keymap_binding_released, .binding_released = on_keymap_binding_released,
}; };
static const struct behavior_transparent_config behavior_transparent_config = {}; DEVICE_AND_API_INIT(behavior_transparent, DT_INST_LABEL(0), behavior_transparent_init, NULL, NULL,
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&behavior_transparent_driver_api);
static struct behavior_transparent_data behavior_transparent_data; #endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */
DEVICE_AND_API_INIT(behavior_transparent, DT_INST_LABEL(0), behavior_transparent_init,
&behavior_transparent_data, &behavior_transparent_config, APPLICATION,
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_transparent_driver_api);