From b84d29c384532443010a7140e8670482f2a6ca02 Mon Sep 17 00:00:00 2001 From: Pete Johanson Date: Mon, 18 Jan 2021 01:09:51 -0500 Subject: [PATCH] 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. --- app/dts/behaviors/bluetooth.dtsi | 2 +- app/dts/behaviors/ext_power.dtsi | 2 +- app/dts/behaviors/key_press.dtsi | 2 +- app/dts/behaviors/layer_tap.dtsi | 2 +- app/dts/behaviors/mod_tap.dtsi | 2 +- app/dts/behaviors/momentary_layer.dtsi | 2 +- app/dts/behaviors/none.dtsi | 2 +- app/dts/behaviors/outputs.dtsi | 2 +- app/dts/behaviors/reset.dtsi | 4 ++-- app/dts/behaviors/rgb_underglow.dtsi | 2 +- app/dts/behaviors/sensor_rotate_key_press.dtsi | 2 +- app/dts/behaviors/sticky_key.dtsi | 4 ++-- app/dts/behaviors/to_layer.dtsi | 2 +- app/dts/behaviors/toggle_layer.dtsi | 2 +- app/dts/behaviors/transparent.dtsi | 2 +- app/src/behaviors/behavior_bt.c | 4 ++++ app/src/behaviors/behavior_ext_power.c | 4 ++++ app/src/behaviors/behavior_hold_tap.c | 4 ++-- app/src/behaviors/behavior_none.c | 12 ++++-------- app/src/behaviors/behavior_outputs.c | 4 ++++ app/src/behaviors/behavior_reset.c | 5 ++++- app/src/behaviors/behavior_rgb_underglow.c | 6 +++++- .../behaviors/behavior_sensor_rotate_key_press.c | 6 +++++- app/src/behaviors/behavior_to_layer.c | 4 ++++ app/src/behaviors/behavior_toggle_layer.c | 4 ++++ app/src/behaviors/behavior_transparent.c | 13 +++++-------- 26 files changed, 62 insertions(+), 38 deletions(-) diff --git a/app/dts/behaviors/bluetooth.dtsi b/app/dts/behaviors/bluetooth.dtsi index 9df974b4..1e9cf21b 100644 --- a/app/dts/behaviors/bluetooth.dtsi +++ b/app/dts/behaviors/bluetooth.dtsi @@ -6,7 +6,7 @@ / { behaviors { - bt: behavior_bluetooth { + /omit-if-no-ref/ bt: behavior_bluetooth { compatible = "zmk,behavior-bluetooth"; label = "BLUETOOTH"; #binding-cells = <2>; diff --git a/app/dts/behaviors/ext_power.dtsi b/app/dts/behaviors/ext_power.dtsi index 471e9fa2..742e33ae 100644 --- a/app/dts/behaviors/ext_power.dtsi +++ b/app/dts/behaviors/ext_power.dtsi @@ -6,7 +6,7 @@ / { behaviors { - ext_power: behavior_ext_power { + /omit-if-no-ref/ ext_power: behavior_ext_power { compatible = "zmk,behavior-ext-power"; label = "EXT_POWER_BEHAVIOR"; #binding-cells = <1>; diff --git a/app/dts/behaviors/key_press.dtsi b/app/dts/behaviors/key_press.dtsi index 98064f69..59a4e12a 100644 --- a/app/dts/behaviors/key_press.dtsi +++ b/app/dts/behaviors/key_press.dtsi @@ -7,7 +7,7 @@ / { behaviors { /* 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"; label = "KEY_PRESS"; #binding-cells = <1>; diff --git a/app/dts/behaviors/layer_tap.dtsi b/app/dts/behaviors/layer_tap.dtsi index cca481a7..b4539b35 100644 --- a/app/dts/behaviors/layer_tap.dtsi +++ b/app/dts/behaviors/layer_tap.dtsi @@ -6,7 +6,7 @@ / { behaviors { - lt: behavior_layer_tap { + /omit-if-no-ref/ lt: behavior_layer_tap { compatible = "zmk,behavior-hold-tap"; label = "LAYER_TAP"; #binding-cells = <2>; diff --git a/app/dts/behaviors/mod_tap.dtsi b/app/dts/behaviors/mod_tap.dtsi index b972aba1..79ce10c2 100644 --- a/app/dts/behaviors/mod_tap.dtsi +++ b/app/dts/behaviors/mod_tap.dtsi @@ -6,7 +6,7 @@ / { behaviors { - mt: behavior_mod_tap { + /omit-if-no-ref/ mt: behavior_mod_tap { compatible = "zmk,behavior-hold-tap"; label = "MOD_TAP"; #binding-cells = <2>; diff --git a/app/dts/behaviors/momentary_layer.dtsi b/app/dts/behaviors/momentary_layer.dtsi index d3b21da9..2dbd88d9 100644 --- a/app/dts/behaviors/momentary_layer.dtsi +++ b/app/dts/behaviors/momentary_layer.dtsi @@ -6,7 +6,7 @@ / { behaviors { - mo: behavior_momentary_layer { + /omit-if-no-ref/ mo: behavior_momentary_layer { compatible = "zmk,behavior-momentary-layer"; label = "MO"; #binding-cells = <1>; diff --git a/app/dts/behaviors/none.dtsi b/app/dts/behaviors/none.dtsi index 1fe2cff7..790f2d61 100644 --- a/app/dts/behaviors/none.dtsi +++ b/app/dts/behaviors/none.dtsi @@ -6,7 +6,7 @@ / { behaviors { - none: behavior_none { + /omit-if-no-ref/ none: behavior_none { compatible = "zmk,behavior-none"; label = "NONE"; #binding-cells = <0>; diff --git a/app/dts/behaviors/outputs.dtsi b/app/dts/behaviors/outputs.dtsi index ee17e1df..88e8f882 100644 --- a/app/dts/behaviors/outputs.dtsi +++ b/app/dts/behaviors/outputs.dtsi @@ -6,7 +6,7 @@ / { behaviors { - out: behavior_outputs { + /omit-if-no-ref/ out: behavior_outputs { compatible = "zmk,behavior-outputs"; label = "OUTPUTS"; #binding-cells = <1>; diff --git a/app/dts/behaviors/reset.dtsi b/app/dts/behaviors/reset.dtsi index cf363425..c720bc88 100644 --- a/app/dts/behaviors/reset.dtsi +++ b/app/dts/behaviors/reset.dtsi @@ -8,13 +8,13 @@ / { behaviors { - reset: behavior_reset { + /omit-if-no-ref/ reset: behavior_reset { compatible = "zmk,behavior-reset"; label = "RESET"; #binding-cells = <0>; }; - bootloader: behavior_reset_dfu { + /omit-if-no-ref/ bootloader: behavior_reset_dfu { compatible = "zmk,behavior-reset"; label = "BOOTLOADER_RESET"; type = ; diff --git a/app/dts/behaviors/rgb_underglow.dtsi b/app/dts/behaviors/rgb_underglow.dtsi index 8b88f8c8..60bdb3aa 100644 --- a/app/dts/behaviors/rgb_underglow.dtsi +++ b/app/dts/behaviors/rgb_underglow.dtsi @@ -6,7 +6,7 @@ / { behaviors { - rgb_ug: behavior_rgb_underglow { + /omit-if-no-ref/ rgb_ug: behavior_rgb_underglow { compatible = "zmk,behavior-rgb-underglow"; label = "RGB_UNDERGLOW"; #binding-cells = <2>; diff --git a/app/dts/behaviors/sensor_rotate_key_press.dtsi b/app/dts/behaviors/sensor_rotate_key_press.dtsi index 075fc38e..d3f084b0 100644 --- a/app/dts/behaviors/sensor_rotate_key_press.dtsi +++ b/app/dts/behaviors/sensor_rotate_key_press.dtsi @@ -7,7 +7,7 @@ / { behaviors { /* 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"; label = "ENC_KEY_PRESS"; #sensor-binding-cells = <2>; diff --git a/app/dts/behaviors/sticky_key.dtsi b/app/dts/behaviors/sticky_key.dtsi index f1c1cdd5..64032085 100644 --- a/app/dts/behaviors/sticky_key.dtsi +++ b/app/dts/behaviors/sticky_key.dtsi @@ -6,14 +6,14 @@ / { behaviors { - sk: behavior_sticky_key { + /omit-if-no-ref/ sk: behavior_sticky_key { compatible = "zmk,behavior-sticky-key"; label = "STICKY_KEY"; #binding-cells = <1>; release-after-ms = <1000>; bindings = <&kp>; }; - sl: behavior_sticky_layer { + /omit-if-no-ref/ sl: behavior_sticky_layer { compatible = "zmk,behavior-sticky-key"; label = "STICKY_LAYER"; #binding-cells = <1>; diff --git a/app/dts/behaviors/to_layer.dtsi b/app/dts/behaviors/to_layer.dtsi index b09616ff..fa8f98bd 100644 --- a/app/dts/behaviors/to_layer.dtsi +++ b/app/dts/behaviors/to_layer.dtsi @@ -6,7 +6,7 @@ / { behaviors { - to: behavior_to_layer { + /omit-if-no-ref/ to: behavior_to_layer { compatible = "zmk,behavior-to-layer"; label = "TO_LAYER"; #binding-cells = <1>; diff --git a/app/dts/behaviors/toggle_layer.dtsi b/app/dts/behaviors/toggle_layer.dtsi index 40453962..ea0b1c19 100644 --- a/app/dts/behaviors/toggle_layer.dtsi +++ b/app/dts/behaviors/toggle_layer.dtsi @@ -6,7 +6,7 @@ / { behaviors { - tog: behavior_toggle_layer { + /omit-if-no-ref/ tog: behavior_toggle_layer { compatible = "zmk,behavior-toggle-layer"; label = "TOGGLE_LAYER"; #binding-cells = <1>; diff --git a/app/dts/behaviors/transparent.dtsi b/app/dts/behaviors/transparent.dtsi index 09f9ea00..81ebb133 100644 --- a/app/dts/behaviors/transparent.dtsi +++ b/app/dts/behaviors/transparent.dtsi @@ -6,7 +6,7 @@ / { behaviors { - trans: behavior_transparent { + /omit-if-no-ref/ trans: behavior_transparent { compatible = "zmk,behavior-transparent"; label = "TRANS"; #binding-cells = <0>; diff --git a/app/src/behaviors/behavior_bt.c b/app/src/behaviors/behavior_bt.c index 3149c8ce..9a171e0f 100644 --- a/app/src/behaviors/behavior_bt.c +++ b/app/src/behaviors/behavior_bt.c @@ -17,6 +17,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event) { 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, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_bt_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ \ No newline at end of file diff --git a/app/src/behaviors/behavior_ext_power.c b/app/src/behaviors/behavior_ext_power.c index 18520f7d..659cde56 100644 --- a/app/src/behaviors/behavior_ext_power.c +++ b/app/src/behaviors/behavior_ext_power.c @@ -16,6 +16,8 @@ #include 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, struct zmk_behavior_binding_event event) { 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, APPLICATION, CONFIG_APPLICATION_INIT_PRIORITY, &behavior_ext_power_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_hold_tap.c b/app/src/behaviors/behavior_hold_tap.c index 8f239dc2..a7185fb0 100644 --- a/app/src/behaviors/behavior_hold_tap.c +++ b/app/src/behaviors/behavior_hold_tap.c @@ -22,7 +22,7 @@ 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_CAPTURED_EVENTS 40 @@ -538,4 +538,4 @@ static struct behavior_hold_tap_data behavior_hold_tap_data; DT_INST_FOREACH_STATUS_OKAY(KP_INST) -#endif \ No newline at end of file +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ \ No newline at end of file diff --git a/app/src/behaviors/behavior_none.c b/app/src/behaviors/behavior_none.c index e0eaa159..8b6172ff 100644 --- a/app/src/behaviors/behavior_none.c +++ b/app/src/behaviors/behavior_none.c @@ -15,8 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_none_config {}; -struct behavior_none_data {}; +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) 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, }; -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; - -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); \ No newline at end of file +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ \ No newline at end of file diff --git a/app/src/behaviors/behavior_outputs.c b/app/src/behaviors/behavior_outputs.c index 3a45b7fd..ccaa7200 100644 --- a/app/src/behaviors/behavior_outputs.c +++ b/app/src/behaviors/behavior_outputs.c @@ -18,6 +18,8 @@ #include 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, struct zmk_behavior_binding_event event) { 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, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_outputs_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_reset.c b/app/src/behaviors/behavior_reset.c index e4b720f7..95363512 100644 --- a/app/src/behaviors/behavior_reset.c +++ b/app/src/behaviors/behavior_reset.c @@ -15,6 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) struct behavior_reset_config { int type; }; @@ -44,4 +45,6 @@ static const struct behavior_driver_api behavior_reset_driver_api = { &behavior_reset_config_##n, APPLICATION, \ CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_reset_driver_api); -DT_INST_FOREACH_STATUS_OKAY(RST_INST) \ No newline at end of file +DT_INST_FOREACH_STATUS_OKAY(RST_INST) + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_rgb_underglow.c b/app/src/behaviors/behavior_rgb_underglow.c index 07f24940..eb4680e1 100644 --- a/app/src/behaviors/behavior_rgb_underglow.c +++ b/app/src/behaviors/behavior_rgb_underglow.c @@ -16,6 +16,8 @@ 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 on_keymap_binding_pressed(struct zmk_behavior_binding *binding, @@ -63,4 +65,6 @@ 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, NULL, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, - &behavior_rgb_underglow_driver_api); \ No newline at end of file + &behavior_rgb_underglow_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_sensor_rotate_key_press.c b/app/src/behaviors/behavior_sensor_rotate_key_press.c index 67a2e710..589a3a57 100644 --- a/app/src/behaviors/behavior_sensor_rotate_key_press.c +++ b/app/src/behaviors/behavior_sensor_rotate_key_press.c @@ -16,6 +16,8 @@ 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 on_sensor_binding_triggered(struct zmk_behavior_binding *binding, @@ -62,4 +64,6 @@ static const struct behavior_driver_api behavior_sensor_rotate_key_press_driver_ CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, \ &behavior_sensor_rotate_key_press_driver_api); -DT_INST_FOREACH_STATUS_OKAY(KP_INST) \ No newline at end of file +DT_INST_FOREACH_STATUS_OKAY(KP_INST) + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_to_layer.c b/app/src/behaviors/behavior_to_layer.c index bb64026d..e68736ef 100644 --- a/app/src/behaviors/behavior_to_layer.c +++ b/app/src/behaviors/behavior_to_layer.c @@ -15,6 +15,8 @@ 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 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, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_to_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_toggle_layer.c b/app/src/behaviors/behavior_toggle_layer.c index 21daa004..c922634d 100644 --- a/app/src/behaviors/behavior_toggle_layer.c +++ b/app/src/behaviors/behavior_toggle_layer.c @@ -15,6 +15,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + struct behavior_tog_config {}; 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, &behavior_tog_config, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT, &behavior_tog_driver_api); + +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ diff --git a/app/src/behaviors/behavior_transparent.c b/app/src/behaviors/behavior_transparent.c index ca3d279f..e9d49b21 100644 --- a/app/src/behaviors/behavior_transparent.c +++ b/app/src/behaviors/behavior_transparent.c @@ -15,8 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); -struct behavior_transparent_config {}; -struct behavior_transparent_data {}; +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) 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, }; -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; - -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); \ No newline at end of file +#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */ \ No newline at end of file