mirror of
https://github.com/zmkfirmware/zmk.git
synced 2026-04-03 03:45:19 -05:00
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.
This commit is contained in:
@@ -15,26 +15,48 @@
|
||||
#include <dt-bindings/zmk/hid_usage_pages.h>
|
||||
#include <zmk/usb_hid.h>
|
||||
#include <zmk/hog.h>
|
||||
#include <zmk/endpoints.h>
|
||||
#include <zmk/event_manager.h>
|
||||
#include <zmk/events/ble_active_profile_changed.h>
|
||||
#include <zmk/events/usb_conn_state_changed.h>
|
||||
#include <zmk/events/endpoint_changed.h>
|
||||
|
||||
#include <zephyr/logging/log.h>
|
||||
|
||||
LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL);
|
||||
|
||||
#define DEFAULT_TRANSPORT \
|
||||
COND_CODE_1(IS_ENABLED(CONFIG_ZMK_BLE), (ZMK_TRANSPORT_BLE), (ZMK_TRANSPORT_USB))
|
||||
// Name of the subtree for endpoint-related settings
|
||||
#define SETTING_SUBTREE "endpoints"
|
||||
|
||||
// Key of the setting to store the preferred_transport value.
|
||||
#define SETTING_PREFERRED_TRANSPORT_KEY "preferred2"
|
||||
// Full name of the setting to store the preferred_transport value.
|
||||
#define SETTING_PREFERRED_TRANSPORT SETTING_SUBTREE "/" SETTING_PREFERRED_TRANSPORT_KEY
|
||||
|
||||
// Key of the deprecated setting which stored preferred_transport with an older type.
|
||||
#define SETTING_PREFERRED_TRANSPORT_V1_KEY "preferred"
|
||||
// Full name of the deprecated setting which stored preferred_transport with an older type.
|
||||
#define SETTING_PREFERRED_TRANSPORT_V1 SETTING_SUBTREE "/" SETTING_PREFERRED_TRANSPORT_V1_KEY
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_USB)
|
||||
#define DEFAULT_TRANSPORT ZMK_TRANSPORT_USB
|
||||
#elif IS_ENABLED(CONFIG_ZMK_BLE)
|
||||
#define DEFAULT_TRANSPORT ZMK_TRANSPORT_BLE
|
||||
#else
|
||||
#define DEFAULT_TRANSPORT ZMK_TRANSPORT_NONE
|
||||
#endif
|
||||
|
||||
static struct zmk_endpoint_instance current_instance = {};
|
||||
static enum zmk_transport preferred_transport =
|
||||
ZMK_TRANSPORT_USB; /* Used if multiple endpoints are ready */
|
||||
|
||||
// Transport to use if multiple endpoints are ready
|
||||
static enum zmk_transport preferred_transport = DEFAULT_TRANSPORT;
|
||||
|
||||
static void update_current_endpoint(void);
|
||||
|
||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||
static void endpoints_save_preferred_work(struct k_work *work) {
|
||||
settings_save_one("endpoints/preferred", &preferred_transport, sizeof(preferred_transport));
|
||||
settings_save_one(SETTING_PREFERRED_TRANSPORT, &preferred_transport,
|
||||
sizeof(preferred_transport));
|
||||
}
|
||||
|
||||
static struct k_work_delayable endpoints_save_work;
|
||||
@@ -54,6 +76,7 @@ bool zmk_endpoint_instance_eq(struct zmk_endpoint_instance a, struct zmk_endpoin
|
||||
}
|
||||
|
||||
switch (a.transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
case ZMK_TRANSPORT_USB:
|
||||
return true;
|
||||
|
||||
@@ -67,6 +90,9 @@ bool zmk_endpoint_instance_eq(struct zmk_endpoint_instance a, struct zmk_endpoin
|
||||
|
||||
int zmk_endpoint_instance_to_str(struct zmk_endpoint_instance endpoint, char *str, size_t len) {
|
||||
switch (endpoint.transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
return snprintf(str, len, "None");
|
||||
|
||||
case ZMK_TRANSPORT_USB:
|
||||
return snprintf(str, len, "USB");
|
||||
|
||||
@@ -78,11 +104,15 @@ int zmk_endpoint_instance_to_str(struct zmk_endpoint_instance endpoint, char *st
|
||||
}
|
||||
}
|
||||
|
||||
#define INSTANCE_INDEX_OFFSET_USB 0
|
||||
#define INSTANCE_INDEX_OFFSET_BLE ZMK_ENDPOINT_USB_COUNT
|
||||
#define INSTANCE_INDEX_OFFSET_NONE 0
|
||||
#define INSTANCE_INDEX_OFFSET_USB (INSTANCE_INDEX_OFFSET_NONE + ZMK_ENDPOINT_NONE_COUNT)
|
||||
#define INSTANCE_INDEX_OFFSET_BLE (INSTANCE_INDEX_OFFSET_USB + ZMK_ENDPOINT_USB_COUNT)
|
||||
|
||||
int zmk_endpoint_instance_to_index(struct zmk_endpoint_instance endpoint) {
|
||||
switch (endpoint.transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
return INSTANCE_INDEX_OFFSET_NONE;
|
||||
|
||||
case ZMK_TRANSPORT_USB:
|
||||
return INSTANCE_INDEX_OFFSET_USB;
|
||||
|
||||
@@ -94,7 +124,7 @@ int zmk_endpoint_instance_to_index(struct zmk_endpoint_instance endpoint) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zmk_endpoints_select_transport(enum zmk_transport transport) {
|
||||
int zmk_endpoint_set_preferred_transport(enum zmk_transport transport) {
|
||||
LOG_DBG("Selected endpoint transport %d", transport);
|
||||
|
||||
if (preferred_transport == transport) {
|
||||
@@ -110,16 +140,44 @@ int zmk_endpoints_select_transport(enum zmk_transport transport) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int zmk_endpoints_toggle_transport(void) {
|
||||
enum zmk_transport zmk_endpoint_get_preferred_transport(void) { return preferred_transport; }
|
||||
|
||||
int zmk_endpoint_toggle_preferred_transport(void) {
|
||||
enum zmk_transport new_transport =
|
||||
(preferred_transport == ZMK_TRANSPORT_USB) ? ZMK_TRANSPORT_BLE : ZMK_TRANSPORT_USB;
|
||||
return zmk_endpoints_select_transport(new_transport);
|
||||
return zmk_endpoint_set_preferred_transport(new_transport);
|
||||
}
|
||||
|
||||
struct zmk_endpoint_instance zmk_endpoints_selected(void) { return current_instance; }
|
||||
static struct zmk_endpoint_instance get_instance_from_transport(enum zmk_transport transport) {
|
||||
struct zmk_endpoint_instance instance = {.transport = transport};
|
||||
switch (instance.transport) {
|
||||
case ZMK_TRANSPORT_BLE:
|
||||
#if IS_ENABLED(CONFIG_ZMK_BLE)
|
||||
instance.ble.profile_index = zmk_ble_active_profile_index();
|
||||
#endif // IS_ENABLED(CONFIG_ZMK_BLE)
|
||||
break;
|
||||
|
||||
default:
|
||||
// No extra data for this transport.
|
||||
break;
|
||||
}
|
||||
|
||||
return instance;
|
||||
}
|
||||
|
||||
struct zmk_endpoint_instance zmk_endpoint_get_preferred(void) {
|
||||
return get_instance_from_transport(preferred_transport);
|
||||
}
|
||||
|
||||
struct zmk_endpoint_instance zmk_endpoint_get_selected(void) { return current_instance; }
|
||||
|
||||
bool zmk_endpoint_is_connected(void) { return current_instance.transport != ZMK_TRANSPORT_NONE; }
|
||||
|
||||
static int send_keyboard_report(void) {
|
||||
switch (current_instance.transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
return 0;
|
||||
|
||||
case ZMK_TRANSPORT_USB: {
|
||||
#if IS_ENABLED(CONFIG_ZMK_USB)
|
||||
int err = zmk_usb_hid_send_keyboard_report();
|
||||
@@ -154,6 +212,9 @@ static int send_keyboard_report(void) {
|
||||
|
||||
static int send_consumer_report(void) {
|
||||
switch (current_instance.transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
return 0;
|
||||
|
||||
case ZMK_TRANSPORT_USB: {
|
||||
#if IS_ENABLED(CONFIG_ZMK_USB)
|
||||
int err = zmk_usb_hid_send_consumer_report();
|
||||
@@ -186,8 +247,7 @@ static int send_consumer_report(void) {
|
||||
return -ENOTSUP;
|
||||
}
|
||||
|
||||
int zmk_endpoints_send_report(uint16_t usage_page) {
|
||||
|
||||
int zmk_endpoint_send_report(uint16_t usage_page) {
|
||||
LOG_DBG("usage page 0x%02X", usage_page);
|
||||
switch (usage_page) {
|
||||
case HID_USAGE_KEY:
|
||||
@@ -202,8 +262,11 @@ int zmk_endpoints_send_report(uint16_t usage_page) {
|
||||
}
|
||||
|
||||
#if IS_ENABLED(CONFIG_ZMK_POINTING)
|
||||
int zmk_endpoints_send_mouse_report() {
|
||||
int zmk_endpoint_send_mouse_report() {
|
||||
switch (current_instance.transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
return 0;
|
||||
|
||||
case ZMK_TRANSPORT_USB: {
|
||||
#if IS_ENABLED(CONFIG_ZMK_USB)
|
||||
int err = zmk_usb_hid_send_mouse_report();
|
||||
@@ -239,29 +302,106 @@ int zmk_endpoints_send_mouse_report() {
|
||||
|
||||
#if IS_ENABLED(CONFIG_SETTINGS)
|
||||
|
||||
static int endpoints_handle_set(const char *name, size_t len, settings_read_cb read_cb,
|
||||
void *cb_arg) {
|
||||
LOG_DBG("Setting endpoint value %s", name);
|
||||
// Type for the deprecated SETTING_PREFERRED_TRANSPORT_V1 setting. To maintain backwards
|
||||
// compatibility when ZMK_TRANSPORT_NONE was inserted into the beginning of enum zmk_transport, the
|
||||
// setting was moved to SETTING_PREFERRED_TRANSPORT. If the deprecated setting exists, it must be
|
||||
// upgraded to the new type and setting name.
|
||||
enum transport_v1 {
|
||||
TRANSPORT_V1_USB = 0,
|
||||
TRANSPORT_V1_BLE = 1,
|
||||
};
|
||||
|
||||
if (settings_name_steq(name, "preferred", NULL)) {
|
||||
if (len != sizeof(enum zmk_transport)) {
|
||||
LOG_ERR("Invalid endpoint size (got %d expected %d)", len, sizeof(enum zmk_transport));
|
||||
return -EINVAL;
|
||||
}
|
||||
static enum zmk_transport upgrade_transport_v1(enum transport_v1 value) {
|
||||
switch (value) {
|
||||
case TRANSPORT_V1_USB:
|
||||
return ZMK_TRANSPORT_USB;
|
||||
|
||||
int err = read_cb(cb_arg, &preferred_transport, sizeof(enum zmk_transport));
|
||||
if (err <= 0) {
|
||||
LOG_ERR("Failed to read preferred endpoint from settings (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
case TRANSPORT_V1_BLE:
|
||||
return ZMK_TRANSPORT_BLE;
|
||||
};
|
||||
|
||||
update_current_endpoint();
|
||||
LOG_ERR("Invalid transport_v1 value: %d", value);
|
||||
return ZMK_TRANSPORT_USB;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the deprecated SETTING_PREFERRED_TRANSPORT_V1 setting and upgrades it to the new type,
|
||||
* storing the value in SETTING_PREFERRED_TRANSPORT and deleting the original setting.
|
||||
*/
|
||||
static int endpoint_settings_load_preferred_v1(size_t len, settings_read_cb read_cb, void *cb_arg) {
|
||||
enum transport_v1 value;
|
||||
|
||||
if (len != sizeof(value)) {
|
||||
LOG_ERR("Invalid zmk_transport size (got %zu expected %zu)", len, sizeof(value));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ssize_t len_read = read_cb(cb_arg, &value, sizeof(value));
|
||||
if (len_read < 0) {
|
||||
LOG_ERR("Failed to read preferred endpoint v1 from settings (err %d)", len_read);
|
||||
return len_read;
|
||||
}
|
||||
|
||||
preferred_transport = upgrade_transport_v1(value);
|
||||
|
||||
int err = settings_delete(SETTING_PREFERRED_TRANSPORT_V1);
|
||||
if (err != 0) {
|
||||
LOG_ERR("Failed to delete preferred endpoint v1 setting (err %d)", err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = settings_save_one(SETTING_PREFERRED_TRANSPORT, &preferred_transport,
|
||||
sizeof(preferred_transport));
|
||||
if (err == 0) {
|
||||
LOG_INF("Upgraded preferred endpoint setting");
|
||||
} else {
|
||||
LOG_ERR("Failed to save upgraded endpoint value (err %d)", err);
|
||||
}
|
||||
|
||||
return err;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the SETTING_PREFERRED_TRANSPORT setting.
|
||||
*/
|
||||
static int endpoint_settings_load_preferred_v2(size_t len, settings_read_cb read_cb, void *cb_arg) {
|
||||
if (len != sizeof(preferred_transport)) {
|
||||
LOG_ERR("Invalid zmk_transport size (got %zu expected %zu)", len,
|
||||
sizeof(preferred_transport));
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
ssize_t len_read = read_cb(cb_arg, &preferred_transport, sizeof(preferred_transport));
|
||||
if (len_read < 0) {
|
||||
LOG_ERR("Failed to read preferred endpoint from settings (err %d)", len_read);
|
||||
return len_read;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SETTINGS_STATIC_HANDLER_DEFINE(endpoints, "endpoints", NULL, endpoints_handle_set, NULL, NULL);
|
||||
static int endpoint_settings_set(const char *name, size_t len, settings_read_cb read_cb,
|
||||
void *cb_arg) {
|
||||
LOG_DBG("Setting endpoint value %s", name);
|
||||
|
||||
if (settings_name_steq(name, SETTING_PREFERRED_TRANSPORT_KEY, NULL)) {
|
||||
return endpoint_settings_load_preferred_v2(len, read_cb, cb_arg);
|
||||
}
|
||||
|
||||
if (settings_name_steq(name, SETTING_PREFERRED_TRANSPORT_V1_KEY, NULL)) {
|
||||
return endpoint_settings_load_preferred_v1(len, read_cb, cb_arg);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int endpoint_settings_commit(void) {
|
||||
update_current_endpoint();
|
||||
return 0;
|
||||
}
|
||||
|
||||
SETTINGS_STATIC_HANDLER_DEFINE(endpoints, SETTING_SUBTREE, NULL, endpoint_settings_set,
|
||||
endpoint_settings_commit, NULL);
|
||||
|
||||
#endif /* IS_ENABLED(CONFIG_SETTINGS) */
|
||||
|
||||
@@ -282,41 +422,40 @@ static bool is_ble_ready(void) {
|
||||
}
|
||||
|
||||
static enum zmk_transport get_selected_transport(void) {
|
||||
if (is_ble_ready()) {
|
||||
switch (preferred_transport) {
|
||||
case ZMK_TRANSPORT_NONE:
|
||||
LOG_DBG("No endpoint transport selected");
|
||||
return ZMK_TRANSPORT_NONE;
|
||||
|
||||
case ZMK_TRANSPORT_USB:
|
||||
if (is_usb_ready()) {
|
||||
LOG_DBG("Both endpoint transports are ready. Using %d", preferred_transport);
|
||||
return preferred_transport;
|
||||
LOG_DBG("USB is preferred and ready");
|
||||
return ZMK_TRANSPORT_USB;
|
||||
}
|
||||
if (is_ble_ready()) {
|
||||
LOG_DBG("USB is not ready. Falling back to BLE");
|
||||
return ZMK_TRANSPORT_BLE;
|
||||
}
|
||||
break;
|
||||
|
||||
LOG_DBG("Only BLE is ready.");
|
||||
return ZMK_TRANSPORT_BLE;
|
||||
case ZMK_TRANSPORT_BLE:
|
||||
if (is_ble_ready()) {
|
||||
LOG_DBG("BLE is preferred and ready");
|
||||
return ZMK_TRANSPORT_BLE;
|
||||
}
|
||||
if (is_usb_ready()) {
|
||||
LOG_DBG("BLE is not ready. Falling back to USB");
|
||||
return ZMK_TRANSPORT_USB;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (is_usb_ready()) {
|
||||
LOG_DBG("Only USB is ready.");
|
||||
return ZMK_TRANSPORT_USB;
|
||||
}
|
||||
|
||||
LOG_DBG("No endpoint transports are ready.");
|
||||
return DEFAULT_TRANSPORT;
|
||||
LOG_DBG("Preferred endpoint transport is %d but no transports are ready", preferred_transport);
|
||||
return ZMK_TRANSPORT_NONE;
|
||||
}
|
||||
|
||||
static struct zmk_endpoint_instance get_selected_instance(void) {
|
||||
struct zmk_endpoint_instance instance = {.transport = get_selected_transport()};
|
||||
|
||||
switch (instance.transport) {
|
||||
#if IS_ENABLED(CONFIG_ZMK_BLE)
|
||||
case ZMK_TRANSPORT_BLE:
|
||||
instance.ble.profile_index = zmk_ble_active_profile_index();
|
||||
break;
|
||||
#endif // IS_ENABLED(CONFIG_ZMK_BLE)
|
||||
|
||||
default:
|
||||
// No extra data for this transport.
|
||||
break;
|
||||
}
|
||||
|
||||
return instance;
|
||||
return get_instance_from_transport(get_selected_transport());
|
||||
}
|
||||
|
||||
static int zmk_endpoints_init(void) {
|
||||
@@ -329,15 +468,15 @@ static int zmk_endpoints_init(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
void zmk_endpoints_clear_current(void) {
|
||||
void zmk_endpoint_clear_reports(void) {
|
||||
zmk_hid_keyboard_clear();
|
||||
zmk_hid_consumer_clear();
|
||||
#if IS_ENABLED(CONFIG_ZMK_POINTING)
|
||||
zmk_hid_mouse_clear();
|
||||
#endif // IS_ENABLED(CONFIG_ZMK_POINTING)
|
||||
|
||||
zmk_endpoints_send_report(HID_USAGE_KEY);
|
||||
zmk_endpoints_send_report(HID_USAGE_CONSUMER);
|
||||
zmk_endpoint_send_report(HID_USAGE_KEY);
|
||||
zmk_endpoint_send_report(HID_USAGE_CONSUMER);
|
||||
}
|
||||
|
||||
static void update_current_endpoint(void) {
|
||||
@@ -345,7 +484,7 @@ static void update_current_endpoint(void) {
|
||||
|
||||
if (!zmk_endpoint_instance_eq(new_instance, current_instance)) {
|
||||
// Cancel all current keypresses so keys don't stay held on the old endpoint.
|
||||
zmk_endpoints_clear_current();
|
||||
zmk_endpoint_clear_reports();
|
||||
|
||||
current_instance = new_instance;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user