zmk/app/src/behaviors/behavior_outputs.c
Joel Spadin 6e7e0de2b6
feat(endpoints): add "no endpoint" value (#3140)
feat(endpoints): add "no endpoint" value

This adds ZMK_TRANSPORT_NONE, which can be set as the preferred
endpoint transport if you wish to prevent the keyboard from sending any
output. More usefully, it also is used to indicate that the preferred
endpoint is not available and it could not fall back to an available
one. To go along with this, many endpoint functions are renamed for
consistency, and a few new functions are added:

- zmk_endpoint_get_preferred_transport() returns the value that was set
  with zmk_endpoint_set_preferred_transport().

- zmk_endpoint_get_preferred() returns the endpoint that will be used
  if it is available. This endpoint always has the same transport as
  zmk_endpoint_get_preferred_transport().

- zmk_endpoint_is_connected() is a shortcut to check if the keyboard is
  actually connected to an endpoint.

This change is based on #2572 but without the option to disable endpoint
fallback. It does refactor code to allow adding that feature later.

fix(endpoints): Add endpoint setting upgrade

Adding ZMK_TRANSPORT_NONE at the start of enum zmk_transport results in
the preferred transport setting no longer representing the same values
when it is saved with earlier firmware and loaded with newer firmware.

To fix this, the "endpoints/preferred" setting is now deprecated and
replaced by "endpoints/preferred2". If the old setting is present, it
is interpreted as the old enum type, upgraded to the new type, and saved
immediately to the new setting. The old setting is then deleted.

To avoid this happening again in the future, enum zmk_transport now has
explicit values assigned to identifier, and a comment is also added to
explain that existing values must not be changed.

fix: Set default transport according to enabled transports

The default value for preferred_transport is now set to USB only if
CONFIG_ZMK_USB is enabled. If not, it falls back to BLE if
CONFIG_ZMK_BLE is enabled, then to "none" if nothing is enabled.
2026-02-12 01:51:42 -05:00

85 lines
2.4 KiB
C

/*
* Copyright (c) 2020 The ZMK Contributors
*
* SPDX-License-Identifier: MIT
*/
#define DT_DRV_COMPAT zmk_behavior_outputs
#include <zephyr/device.h>
#include <zephyr/devicetree.h>
#include <drivers/behavior.h>
#include <dt-bindings/zmk/outputs.h>
#include <zmk/behavior.h>
#include <zmk/endpoints.h>
#include <zephyr/logging/log.h>
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT)
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
static const struct behavior_parameter_value_metadata std_values[] = {
{
.value = OUT_TOG,
.display_name = "Toggle Outputs",
.type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE,
},
#if IS_ENABLED(CONFIG_ZMK_USB)
{
.value = OUT_USB,
.display_name = "USB Output",
.type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE,
},
#endif // IS_ENABLED(CONFIG_ZMK_USB)
#if IS_ENABLED(CONFIG_ZMK_BLE)
{
.value = OUT_BLE,
.display_name = "BLE Output",
.type = BEHAVIOR_PARAMETER_VALUE_TYPE_VALUE,
},
#endif // IS_ENABLED(CONFIG_ZMK_BLE)
};
static const struct behavior_parameter_metadata_set std_set = {
.param1_values = std_values,
.param1_values_len = ARRAY_SIZE(std_values),
};
static const struct behavior_parameter_metadata metadata = {
.sets_len = 1,
.sets = &std_set,
};
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
static int on_keymap_binding_pressed(struct zmk_behavior_binding *binding,
struct zmk_behavior_binding_event event) {
switch (binding->param1) {
case OUT_TOG:
return zmk_endpoint_toggle_preferred_transport();
case OUT_USB:
return zmk_endpoint_set_preferred_transport(ZMK_TRANSPORT_USB);
case OUT_BLE:
return zmk_endpoint_set_preferred_transport(ZMK_TRANSPORT_BLE);
default:
LOG_ERR("Unknown output command: %d", binding->param1);
}
return -ENOTSUP;
}
static const struct behavior_driver_api behavior_outputs_driver_api = {
.binding_pressed = on_keymap_binding_pressed,
#if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
.parameter_metadata = &metadata,
#endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA)
};
BEHAVIOR_DT_INST_DEFINE(0, NULL, NULL, NULL, NULL, POST_KERNEL, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT,
&behavior_outputs_driver_api);
#endif /* DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) */