From 6e03a0bb67d0b2fc12004b109cbbd8c5c1448a21 Mon Sep 17 00:00:00 2001 From: Jim Aho Date: Thu, 12 Sep 2024 22:57:19 +0200 Subject: [PATCH 01/20] fix(docs): Fix wording in layers section in keymaps (#2474) --- docs/docs/keymaps/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/keymaps/index.mdx b/docs/docs/keymaps/index.mdx index 0409cecb082..6a5c96988f2 100644 --- a/docs/docs/keymaps/index.mdx +++ b/docs/docs/keymaps/index.mdx @@ -143,7 +143,7 @@ Each layer of your keymap will be nested under the keymap node. Here is an examp Each layer should have: -1. A `bindings` property this will be a list of [behavior bindings](behaviors/index.mdx), one for each key position for the keyboard. +1. A `bindings` property that will be a list of [behavior bindings](behaviors/index.mdx), one for each key position for the keyboard. 1. (Optional) A `sensor-bindings` property that will be a list of behavior bindings for each sensor on the keyboard. (Currently, only encoders are supported as sensor hardware, but in the future devices like trackpoints would be supported the same way) 1. (Optional) A `display-name` property that is a string used by certain features, such as the layer status display widget. From 82a22d731bdd6719844437cddaffc9773b538fcc Mon Sep 17 00:00:00 2001 From: Maxime Vincent Date: Wed, 29 May 2024 14:03:40 +0200 Subject: [PATCH 02/20] pre-commit: use versioned clang-format hook (v18.1.8) --- .pre-commit-config.yaml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e0666ea8d2b..352ad723277 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -5,10 +5,11 @@ repos: hooks: - id: remove-tabs exclude: "vendor-prefixes\\.txt$" - - repo: https://github.com/pocc/pre-commit-hooks - rev: v1.3.5 + - repo: https://github.com/pre-commit/mirrors-clang-format + rev: v18.1.8 hooks: - id: clang-format + types_or: [c++, c] args: - -i - repo: https://github.com/pre-commit/mirrors-prettier From 67d595f29f7495ca32fa8e413e74e22d23bb993a Mon Sep 17 00:00:00 2001 From: Maxime Vincent Date: Fri, 13 Sep 2024 21:39:26 +0200 Subject: [PATCH 03/20] pre-commit: re-format using clang-format hook (v18.1.8) --- .../arm/corneish_zen/widgets/battery_status.c | 2 +- .../nice_view/widgets/peripheral_status.c | 2 +- app/boards/shields/nice_view/widgets/status.c | 2 +- app/include/drivers/behavior.h | 13 +++++++--- app/include/zmk/virtual_key_position.h | 2 +- app/src/behaviors/behavior_caps_word.c | 5 +--- app/src/behaviors/behavior_tap_dance.c | 2 +- app/src/display/widgets/battery_status.c | 2 +- app/src/endpoints.c | 4 +-- app/src/hid.c | 12 +++------ app/src/keymap.c | 13 +++++----- app/src/kscan_sideband_behaviors.c | 3 ++- app/src/physical_layouts.c | 4 ++- app/src/sensors.c | 26 +++++++------------ app/src/studio/rpc.c | 8 ++---- app/src/workqueue.c | 4 +-- 16 files changed, 45 insertions(+), 59 deletions(-) diff --git a/app/boards/arm/corneish_zen/widgets/battery_status.c b/app/boards/arm/corneish_zen/widgets/battery_status.c index 39b811b53f3..622e39df6f0 100644 --- a/app/boards/arm/corneish_zen/widgets/battery_status.c +++ b/app/boards/arm/corneish_zen/widgets/battery_status.c @@ -68,7 +68,7 @@ void battery_status_update_cb(struct battery_status_state state) { static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) { const struct zmk_battery_state_changed *ev = as_zmk_battery_state_changed(eh); - return (struct battery_status_state) { + return (struct battery_status_state){ .level = (ev != NULL) ? ev->state_of_charge : zmk_battery_state_of_charge(), #if IS_ENABLED(CONFIG_USB_DEVICE_STACK) .usb_present = zmk_usb_is_powered(), diff --git a/app/boards/shields/nice_view/widgets/peripheral_status.c b/app/boards/shields/nice_view/widgets/peripheral_status.c index b9da19969cb..e9002b33e75 100644 --- a/app/boards/shields/nice_view/widgets/peripheral_status.c +++ b/app/boards/shields/nice_view/widgets/peripheral_status.c @@ -71,7 +71,7 @@ static void battery_status_update_cb(struct battery_status_state state) { } static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) { - return (struct battery_status_state) { + return (struct battery_status_state){ .level = zmk_battery_state_of_charge(), #if IS_ENABLED(CONFIG_USB_DEVICE_STACK) .usb_present = zmk_usb_is_powered(), diff --git a/app/boards/shields/nice_view/widgets/status.c b/app/boards/shields/nice_view/widgets/status.c index 061b7127cee..f5095cbf1d8 100644 --- a/app/boards/shields/nice_view/widgets/status.c +++ b/app/boards/shields/nice_view/widgets/status.c @@ -212,7 +212,7 @@ static void battery_status_update_cb(struct battery_status_state state) { static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) { const struct zmk_battery_state_changed *ev = as_zmk_battery_state_changed(eh); - return (struct battery_status_state) { + return (struct battery_status_state){ .level = (ev != NULL) ? ev->state_of_charge : zmk_battery_state_of_charge(), #if IS_ENABLED(CONFIG_USB_DEVICE_STACK) .usb_present = zmk_usb_is_powered(), diff --git a/app/include/drivers/behavior.h b/app/include/drivers/behavior.h index 7c99f04ed74..56c26a0155c 100644 --- a/app/include/drivers/behavior.h +++ b/app/include/drivers/behavior.h @@ -122,7 +122,9 @@ struct zmk_behavior_local_id_map { #if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA) #define ZMK_BEHAVIOR_METADATA_INITIALIZER(node_id) \ - { .display_name = DT_PROP_OR(node_id, display_name, DEVICE_DT_NAME(node_id)), } + { \ + .display_name = DT_PROP_OR(node_id, display_name, DEVICE_DT_NAME(node_id)), \ + } #else @@ -132,10 +134,15 @@ struct zmk_behavior_local_id_map { #endif // IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA) #define ZMK_BEHAVIOR_REF_INITIALIZER(node_id, _dev) \ - { .device = _dev, .metadata = ZMK_BEHAVIOR_METADATA_INITIALIZER(node_id), } + { \ + .device = _dev, \ + .metadata = ZMK_BEHAVIOR_METADATA_INITIALIZER(node_id), \ + } #define ZMK_BEHAVIOR_LOCAL_ID_MAP_INITIALIZER(node_id, _dev) \ - { .device = _dev, } + { \ + .device = _dev, \ + } #define ZMK_BEHAVIOR_REF_DEFINE(name, node_id, _dev) \ static const STRUCT_SECTION_ITERABLE(zmk_behavior_ref, name) = \ diff --git a/app/include/zmk/virtual_key_position.h b/app/include/zmk/virtual_key_position.h index b8f20683d7b..563f951eba7 100644 --- a/app/include/zmk/virtual_key_position.h +++ b/app/include/zmk/virtual_key_position.h @@ -17,7 +17,7 @@ /** * Gets the sensor number from the virtual key position. */ -#define ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(vkp) ((vkp)-ZMK_KEYMAP_LEN) +#define ZMK_SENSOR_POSITION_FROM_VIRTUAL_KEY_POSITION(vkp) ((vkp) - ZMK_KEYMAP_LEN) /** * Gets the virtual key position to use for the combo with the given index. diff --git a/app/src/behaviors/behavior_caps_word.c b/app/src/behaviors/behavior_caps_word.c index bf74a4b3dd3..c3255f12c15 100644 --- a/app/src/behaviors/behavior_caps_word.c +++ b/app/src/behaviors/behavior_caps_word.c @@ -169,10 +169,7 @@ static int behavior_caps_word_init(const struct device *dev) { #define CAPS_WORD_LABEL(i, _n) DT_INST_LABEL(i) #define PARSE_BREAK(i) \ - { \ - .page = ZMK_HID_USAGE_PAGE(i), .id = ZMK_HID_USAGE_ID(i), \ - .implicit_modifiers = SELECT_MODS(i) \ - } + {.page = ZMK_HID_USAGE_PAGE(i), .id = ZMK_HID_USAGE_ID(i), .implicit_modifiers = SELECT_MODS(i)} #define BREAK_ITEM(i, n) PARSE_BREAK(DT_INST_PROP_BY_IDX(n, continue_list, i)) diff --git a/app/src/behaviors/behavior_tap_dance.c b/app/src/behaviors/behavior_tap_dance.c index ce57b70fc4b..61e755850d9 100644 --- a/app/src/behaviors/behavior_tap_dance.c +++ b/app/src/behaviors/behavior_tap_dance.c @@ -245,7 +245,7 @@ static int behavior_tap_dance_init(const struct device *dev) { #define _TRANSFORM_ENTRY(idx, node) ZMK_KEYMAP_EXTRACT_BINDING(idx, node) #define TRANSFORMED_BINDINGS(node) \ - { LISTIFY(DT_INST_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, (, ), DT_DRV_INST(node)) } + {LISTIFY(DT_INST_PROP_LEN(node, bindings), _TRANSFORM_ENTRY, (, ), DT_DRV_INST(node))} #define KP_INST(n) \ static struct zmk_behavior_binding \ diff --git a/app/src/display/widgets/battery_status.c b/app/src/display/widgets/battery_status.c index bec6964ba94..22e73fafa9e 100644 --- a/app/src/display/widgets/battery_status.c +++ b/app/src/display/widgets/battery_status.c @@ -65,7 +65,7 @@ void battery_status_update_cb(struct battery_status_state state) { static struct battery_status_state battery_status_get_state(const zmk_event_t *eh) { const struct zmk_battery_state_changed *ev = as_zmk_battery_state_changed(eh); - return (struct battery_status_state) { + return (struct battery_status_state){ .level = (ev != NULL) ? ev->state_of_charge : zmk_battery_state_of_charge(), #if IS_ENABLED(CONFIG_USB_DEVICE_STACK) .usb_present = zmk_usb_is_powered(), diff --git a/app/src/endpoints.c b/app/src/endpoints.c index 652438531f0..b17a664646d 100644 --- a/app/src/endpoints.c +++ b/app/src/endpoints.c @@ -116,9 +116,7 @@ int zmk_endpoints_toggle_transport(void) { return zmk_endpoints_select_transport(new_transport); } -struct zmk_endpoint_instance zmk_endpoints_selected(void) { - return current_instance; -} +struct zmk_endpoint_instance zmk_endpoints_selected(void) { return current_instance; } static int send_keyboard_report(void) { switch (current_instance.transport) { diff --git a/app/src/hid.c b/app/src/hid.c index 24572ad325b..df0715ee487 100644 --- a/app/src/hid.c +++ b/app/src/hid.c @@ -435,18 +435,12 @@ void zmk_hid_mouse_clear(void) { memset(&mouse_report.body, 0, sizeof(mouse_repo #endif // IS_ENABLED(CONFIG_ZMK_MOUSE) -struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report(void) { - return &keyboard_report; -} +struct zmk_hid_keyboard_report *zmk_hid_get_keyboard_report(void) { return &keyboard_report; } -struct zmk_hid_consumer_report *zmk_hid_get_consumer_report(void) { - return &consumer_report; -} +struct zmk_hid_consumer_report *zmk_hid_get_consumer_report(void) { return &consumer_report; } #if IS_ENABLED(CONFIG_ZMK_MOUSE) -struct zmk_hid_mouse_report *zmk_hid_get_mouse_report(void) { - return &mouse_report; -} +struct zmk_hid_mouse_report *zmk_hid_get_mouse_report(void) { return &mouse_report; } #endif // IS_ENABLED(CONFIG_ZMK_MOUSE) diff --git a/app/src/keymap.c b/app/src/keymap.c index dabe338d07d..c9845479cb5 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -40,11 +40,9 @@ static zmk_keymap_layer_id_t _zmk_keymap_layer_default = 0; #endif #define TRANSFORMED_LAYER(node) \ - { \ - COND_CODE_1( \ - DT_NODE_HAS_PROP(node, bindings), \ - (LISTIFY(DT_PROP_LEN(node, bindings), ZMK_KEYMAP_EXTRACT_BINDING, (, ), node)), ()) \ - } + {COND_CODE_1(DT_NODE_HAS_PROP(node, bindings), \ + (LISTIFY(DT_PROP_LEN(node, bindings), ZMK_KEYMAP_EXTRACT_BINDING, (, ), node)), \ + ())} #if ZMK_KEYMAP_HAS_SENSORS #define _TRANSFORM_SENSOR_ENTRY(idx, layer) \ @@ -841,11 +839,12 @@ static int keymap_handle_set(const char *name, size_t len, settings_read_cb read binding_setting.behavior_local_id); } - zmk_keymap[layer][key_position] = (struct zmk_behavior_binding) { + zmk_keymap[layer][key_position] = (struct zmk_behavior_binding){ #if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_LOCAL_IDS_IN_BINDINGS) .local_id = binding_setting.behavior_local_id, #endif - .behavior_dev = name, .param1 = binding_setting.param1, + .behavior_dev = name, + .param1 = binding_setting.param1, .param2 = binding_setting.param2, }; } diff --git a/app/src/kscan_sideband_behaviors.c b/app/src/kscan_sideband_behaviors.c index 602cae12af4..4ef5b052f6f 100644 --- a/app/src/kscan_sideband_behaviors.c +++ b/app/src/kscan_sideband_behaviors.c @@ -183,7 +183,8 @@ static const struct kscan_driver_api ksbb_api = { #define ENTRY(e) \ { \ - .row = DT_PROP(e, row), .column = DT_PROP(e, column), \ + .row = DT_PROP(e, row), \ + .column = DT_PROP(e, column), \ .binding = ZMK_KEYMAP_EXTRACT_BINDING(0, e), \ } diff --git a/app/src/physical_layouts.c b/app/src/physical_layouts.c index 00cfa29e780..a7f6507818e 100644 --- a/app/src/physical_layouts.c +++ b/app/src/physical_layouts.c @@ -139,7 +139,9 @@ struct zmk_kscan_event { uint32_t state; }; -static struct zmk_kscan_msg_processor { struct k_work work; } msg_processor; +static struct zmk_kscan_msg_processor { + struct k_work work; +} msg_processor; K_MSGQ_DEFINE(physical_layouts_kscan_msgq, sizeof(struct zmk_kscan_event), CONFIG_ZMK_KSCAN_EVENT_QUEUE_SIZE, 4); diff --git a/app/src/sensors.c b/app/src/sensors.c index 4dcda44d1d1..90ea1903940 100644 --- a/app/src/sensors.c +++ b/app/src/sensors.c @@ -26,28 +26,22 @@ struct sensors_item_cfg { }; #define _SENSOR_ITEM(idx, node) \ - { \ - .dev = DEVICE_DT_GET_OR_NULL(node), \ - .trigger = {.type = SENSOR_TRIG_DATA_READY, .chan = SENSOR_CHAN_ROTATION}, \ - .config = &configs[idx], .sensor_index = idx \ - } + {.dev = DEVICE_DT_GET_OR_NULL(node), \ + .trigger = {.type = SENSOR_TRIG_DATA_READY, .chan = SENSOR_CHAN_ROTATION}, \ + .config = &configs[idx], \ + .sensor_index = idx} #define SENSOR_ITEM(idx, _i) _SENSOR_ITEM(idx, ZMK_KEYMAP_SENSORS_BY_IDX(idx)) #define PLUS_ONE(n) +1 #define ZMK_KEYMAP_SENSORS_CHILD_COUNT (0 DT_FOREACH_CHILD(ZMK_KEYMAP_SENSORS_NODE, PLUS_ONE)) #define SENSOR_CHILD_ITEM(node) \ - { \ - .triggers_per_rotation = \ - DT_PROP_OR(node, triggers_per_rotation, \ - DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, \ - CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION)) \ - } + {.triggers_per_rotation = \ + DT_PROP_OR(node, triggers_per_rotation, \ + DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, \ + CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION))} #define SENSOR_CHILD_DEFAULTS(idx, arg) \ - { \ - .triggers_per_rotation = \ - DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, \ - CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION) \ - } + {.triggers_per_rotation = DT_PROP_OR(ZMK_KEYMAP_SENSORS_NODE, triggers_per_rotation, \ + CONFIG_ZMK_KEYMAP_SENSORS_DEFAULT_TRIGGERS_PER_ROTATION)} static struct zmk_sensor_config configs[] = { #if ZMK_KEYMAP_SENSORS_CHILD_COUNT > 0 diff --git a/app/src/studio/rpc.c b/app/src/studio/rpc.c index 8dfb025d5f8..bd980eb4605 100644 --- a/app/src/studio/rpc.c +++ b/app/src/studio/rpc.c @@ -77,9 +77,7 @@ static enum studio_framing_state rpc_framing_state; static K_MUTEX_DEFINE(rpc_transport_mutex); static struct zmk_rpc_transport *selected_transport; -struct ring_buf *zmk_rpc_get_rx_buf(void) { - return &rpc_rx_buf; -} +struct ring_buf *zmk_rpc_get_rx_buf(void) { return &rpc_rx_buf; } void zmk_rpc_rx_notify(void) { k_sem_give(&rpc_rx_sem); } @@ -118,9 +116,7 @@ static pb_istream_t pb_istream_for_rx_ring_buf() { RING_BUF_DECLARE(rpc_tx_buf, CONFIG_ZMK_STUDIO_RPC_TX_BUF_SIZE); -struct ring_buf *zmk_rpc_get_tx_buf(void) { - return &rpc_tx_buf; -} +struct ring_buf *zmk_rpc_get_tx_buf(void) { return &rpc_tx_buf; } static bool rpc_tx_buffer_write(pb_ostream_t *stream, const uint8_t *buf, size_t count) { void *user_data = stream->state; diff --git a/app/src/workqueue.c b/app/src/workqueue.c index e6e55c87c14..2decc5aad7e 100644 --- a/app/src/workqueue.c +++ b/app/src/workqueue.c @@ -13,9 +13,7 @@ K_THREAD_STACK_DEFINE(lowprio_q_stack, CONFIG_ZMK_LOW_PRIORITY_THREAD_STACK_SIZE static struct k_work_q lowprio_work_q; -struct k_work_q *zmk_workqueue_lowprio_work_q(void) { - return &lowprio_work_q; -} +struct k_work_q *zmk_workqueue_lowprio_work_q(void) { return &lowprio_work_q; } static int workqueue_init(void) { static const struct k_work_queue_config queue_config = {.name = "Low Priority Work Queue"}; From 92745903c98876f78a8a6da5890b7a6ff93563ec Mon Sep 17 00:00:00 2001 From: Abe Nonym Date: Mon, 16 Sep 2024 18:06:34 +0200 Subject: [PATCH 04/20] fix(ble): Properly compile with clear bonds on start --- app/src/ble.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/ble.c b/app/src/ble.c index 2308de52024..9ecfb7ec394 100644 --- a/app/src/ble.c +++ b/app/src/ble.c @@ -671,7 +671,7 @@ static int zmk_ble_complete_startup(void) { char setting_name[32]; sprintf(setting_name, "ble/peripheral_addresses/%d", i); - err = settings_delete(setting_name); + int err = settings_delete(setting_name); if (err) { LOG_ERR("Failed to delete setting: %d", err); } From 32d0a4bf4102148d394dc5a7acc962e71c5ed000 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Mon, 16 Sep 2024 14:13:51 -0600 Subject: [PATCH 05/20] fix: Fix wakeup from kscan wrapped in sideband. * If using a sideband kscan device to wrap a native kscan device, ensure the inner kscan device is also wakeup enabled to it can be waken from a deep sleep state by key press. --- app/src/kscan_sideband_behaviors.c | 7 +++++++ app/src/physical_layouts.c | 7 +++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/kscan_sideband_behaviors.c b/app/src/kscan_sideband_behaviors.c index 4ef5b052f6f..9f38e0fbcfa 100644 --- a/app/src/kscan_sideband_behaviors.c +++ b/app/src/kscan_sideband_behaviors.c @@ -111,6 +111,9 @@ static int ksbb_enable(const struct device *dev) { pm_device_runtime_get(config->kscan); } #elif IS_ENABLED(CONFIG_PM_DEVICE) + if (pm_device_wakeup_is_capable(config->kscan)) { + pm_device_wakeup_enable(config->kscan, true); + } pm_device_action_run(config->kscan, PM_DEVICE_ACTION_RESUME); #endif // IS_ENABLED(CONFIG_PM_DEVICE) @@ -132,6 +135,10 @@ static int ksbb_disable(const struct device *dev) { pm_device_runtime_put(config->kscan); } #elif IS_ENABLED(CONFIG_PM_DEVICE) + if (pm_device_wakeup_is_capable(config->kscan) && !pm_device_wakeup_is_enabled(dev) && + pm_device_wakeup_is_enabled(config->kscan)) { + pm_device_wakeup_enable(config->kscan, false); + } pm_device_action_run(config->kscan, PM_DEVICE_ACTION_SUSPEND); #endif // IS_ENABLED(CONFIG_PM_DEVICE) diff --git a/app/src/physical_layouts.c b/app/src/physical_layouts.c index a7f6507818e..ac627f46533 100644 --- a/app/src/physical_layouts.c +++ b/app/src/physical_layouts.c @@ -393,10 +393,9 @@ static int zmk_physical_layouts_init(void) { #if IS_ENABLED(CONFIG_PM_DEVICE) for (int l = 0; l < ARRAY_SIZE(layouts); l++) { const struct zmk_physical_layout *pl = layouts[l]; - if (pl->kscan) { - if (pm_device_wakeup_is_capable(pl->kscan)) { - pm_device_wakeup_enable(pl->kscan, true); - } + if (pl->kscan && pm_device_wakeup_is_capable(pl->kscan) && + !pm_device_wakeup_enable(pl->kscan, true)) { + LOG_WRN("Failed to wakeup enable %s", pl->kscan->name); } } #endif // IS_ENABLED(CONFIG_PM_DEVICE) From a9167b2275a17970169a84c5041441ef0b36d0f7 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Mon, 16 Sep 2024 22:59:47 -0600 Subject: [PATCH 06/20] fix: Reduce RAM usage in the keymap * When not building with runtime keymap support, make the keymap const. --- app/src/keymap.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/keymap.c b/app/src/keymap.c index c9845479cb5..041ee11311f 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -83,7 +83,7 @@ static uint8_t keymap_layer_orders[ZMK_KEYMAP_LAYERS_LEN]; (DT_INST_FOREACH_CHILD_SEP(0, TRANSFORMED_LAYER, (, ))), \ (DT_INST_FOREACH_CHILD_STATUS_OKAY_SEP(0, TRANSFORMED_LAYER, (, ))))}; -KEYMAP_VAR(zmk_keymap, ) +KEYMAP_VAR(zmk_keymap, COND_CODE_1(IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE), (), (const))) #if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) @@ -238,8 +238,6 @@ zmk_keymap_get_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t bind static uint8_t zmk_keymap_layer_pending_changes[ZMK_KEYMAP_LAYERS_LEN][PENDING_ARRAY_SIZE]; -#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) - int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t binding_idx, struct zmk_behavior_binding binding) { if (binding_idx >= ZMK_KEYMAP_LEN) { @@ -248,11 +246,9 @@ int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t ASSERT_LAYER_VAL(layer_id, -EINVAL) -#if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) uint8_t *pending = zmk_keymap_layer_pending_changes[layer_id]; WRITE_BIT(pending[binding_idx / 8], binding_idx % 8, 1); -#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) // TODO: Need a mutex to protect access to the keymap data? memcpy(&zmk_keymap[layer_id][binding_idx], &binding, sizeof(binding)); @@ -260,6 +256,14 @@ int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t return 0; } +#else + +int zmk_keymap_set_layer_binding_at_idx(zmk_keymap_layer_id_t layer_id, uint8_t binding_idx, + struct zmk_behavior_binding binding) { + return -ENOTSUP; +} + +#endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) #if IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING) #if IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) From c8c8835a19642d745785c62ae7ab5e74ab88ad24 Mon Sep 17 00:00:00 2001 From: Andrew Kannan Date: Tue, 17 Sep 2024 20:59:05 -0400 Subject: [PATCH 07/20] fix(studio): Update position map querying --- app/src/physical_layouts.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/src/physical_layouts.c b/app/src/physical_layouts.c index ac627f46533..e77a290ed13 100644 --- a/app/src/physical_layouts.c +++ b/app/src/physical_layouts.c @@ -81,7 +81,8 @@ DT_FOREACH_CHILD_SEP(DT_INST(0, POS_MAP_COMPAT), ZMK_POS_MAP_LEN_CHECK, (;)); #define ZMK_POS_MAP_ENTRY(node_id) \ { \ .layout = COND_CODE_1( \ - DT_HAS_COMPAT_STATUS_OKAY(DT_PHANDLE(node_id, physical_layout)), \ + UTIL_AND(DT_NODE_HAS_COMPAT(DT_PHANDLE(node_id, physical_layout), DT_DRV_COMPAT), \ + DT_NODE_HAS_STATUS(DT_PHANDLE(node_id, physical_layout), okay)), \ (&_CONCAT(_zmk_physical_layout_, DT_PHANDLE(node_id, physical_layout))), (NULL)), \ .positions = DT_PROP(node_id, positions), \ } From 62900c62bed78c6583a91d9cd0185800b6be3bd9 Mon Sep 17 00:00:00 2001 From: XiNGRZ Date: Sun, 15 Sep 2024 01:51:32 +0800 Subject: [PATCH 08/20] fix(studio): Ensure null-termination of layer name read from settings This fixes the string leak when a layer name is changed to a longer one, but is discarded and reverted to the original shorter one from ZMK Studio. --- app/src/keymap.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/app/src/keymap.c b/app/src/keymap.c index 041ee11311f..41e57024b77 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -791,12 +791,14 @@ static int keymap_handle_set(const char *name, size_t len, settings_read_cb read LOG_WRN("Found layer name for invalid layer ID %d", layer); } - int err = read_cb(cb_arg, zmk_keymap_layer_names[layer], + int ret = read_cb(cb_arg, zmk_keymap_layer_names[layer], MIN(len, CONFIG_ZMK_KEYMAP_LAYER_NAME_MAX_LEN - 1)); - if (err <= 0) { - LOG_ERR("Failed to handle keymap layer name from settings (err %d)", err); - return err; + if (ret <= 0) { + LOG_ERR("Failed to handle keymap layer name from settings (err %d)", ret); + return ret; } + + zmk_keymap_layer_names[layer][ret] = 0; } else if (settings_name_steq(name, "l", &next) && next) { char *endptr; uint8_t layer = strtoul(next, &endptr, 10); From 6ae07d222a23f1b3887432d35a647bf15a4baf0d Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 18 Sep 2024 13:08:35 -0600 Subject: [PATCH 09/20] feat: Bit of extra studio keymap logging. --- app/src/studio/keymap_subsystem.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/src/studio/keymap_subsystem.c b/app/src/studio/keymap_subsystem.c index aa4b9799206..95d89d3f375 100644 --- a/app/src/studio/keymap_subsystem.c +++ b/app/src/studio/keymap_subsystem.c @@ -175,11 +175,13 @@ zmk_studio_Response save_changes(const zmk_studio_Request *req) { int ret = zmk_physical_layouts_save_selected(); if (ret < 0) { + LOG_WRN("Failed to save selected physical layout (0x%02x)", ret); return ZMK_RPC_SIMPLE_ERR(GENERIC); } ret = zmk_keymap_save_changes(); if (ret < 0) { + LOG_WRN("Failed to save keymap changes (0x%02x)", ret); return ZMK_RPC_SIMPLE_ERR(GENERIC); } From 1baf18d671b8e2e68aaecab8d16967014ad9d2bd Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 19 Sep 2024 01:15:13 -0600 Subject: [PATCH 10/20] chore: Better errno formatting in logs. --- app/src/studio/keymap_subsystem.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/studio/keymap_subsystem.c b/app/src/studio/keymap_subsystem.c index 95d89d3f375..d734afb6029 100644 --- a/app/src/studio/keymap_subsystem.c +++ b/app/src/studio/keymap_subsystem.c @@ -175,13 +175,13 @@ zmk_studio_Response save_changes(const zmk_studio_Request *req) { int ret = zmk_physical_layouts_save_selected(); if (ret < 0) { - LOG_WRN("Failed to save selected physical layout (0x%02x)", ret); + LOG_WRN("Failed to save selected physical layout (%d)", ret); return ZMK_RPC_SIMPLE_ERR(GENERIC); } ret = zmk_keymap_save_changes(); if (ret < 0) { - LOG_WRN("Failed to save keymap changes (0x%02x)", ret); + LOG_WRN("Failed to save keymap changes (%d)", ret); return ZMK_RPC_SIMPLE_ERR(GENERIC); } From de38676afda510305d9af057e536954d6626f0ee Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 18 Sep 2024 10:30:22 -0600 Subject: [PATCH 11/20] fix(core): Warn only with layouts + chosen transform * Instead of erroring out, simply ignore physical layouts if we detect a chosen matrix transform, and warn instead. --- app/include/zmk/matrix.h | 4 ---- app/src/physical_layouts.c | 21 +++++++++++++++++++-- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/include/zmk/matrix.h b/app/include/zmk/matrix.h index e38f5a4967c..436f86f5d18 100644 --- a/app/include/zmk/matrix.h +++ b/app/include/zmk/matrix.h @@ -13,10 +13,6 @@ #if DT_HAS_COMPAT_STATUS_OKAY(zmk_physical_layout) -#if ZMK_MATRIX_HAS_TRANSFORM -#error "To use physical layouts, remove the chosen `zmk,matrix-transform` value." -#endif - #define ZMK_PHYSICAL_LAYOUT_BYTE_ARRAY(node_id) \ uint8_t _CONCAT(prop_, node_id)[DT_PROP_LEN(DT_PHANDLE(node_id, transform), map)]; diff --git a/app/src/physical_layouts.c b/app/src/physical_layouts.c index e77a290ed13..c71b427a4dd 100644 --- a/app/src/physical_layouts.c +++ b/app/src/physical_layouts.c @@ -24,7 +24,10 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #define DT_DRV_COMPAT zmk_physical_layout -#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) +#define USE_PHY_LAYOUTS \ + (DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) && !DT_HAS_CHOSEN(zmk_matrix_transform)) + +#if USE_PHY_LAYOUTS #define ZKPA_INIT(i, n) \ (const struct zmk_key_physical_attrs) { \ @@ -99,6 +102,13 @@ static const struct zmk_physical_layout *const layouts[] = { #elif DT_HAS_CHOSEN(zmk_matrix_transform) +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +#warning \ + "Ignoring the physical layouts and using the chosen matrix transform. Consider setting a chosen physical layout instead." + +#endif + ZMK_MATRIX_TRANSFORM_EXTERN(DT_CHOSEN(zmk_matrix_transform)); static const struct zmk_physical_layout _CONCAT(_zmk_physical_layout_, chosen) = { @@ -111,6 +121,13 @@ static const struct zmk_physical_layout *const layouts[] = { #elif DT_HAS_CHOSEN(zmk_kscan) +#if DT_HAS_COMPAT_STATUS_OKAY(DT_DRV_COMPAT) + +#warning \ + "Ignoring the physical layouts and using the chosen kscan with a synthetic transform. Consider setting a chosen physical layout instead." + +#endif + ZMK_MATRIX_TRANSFORM_DEFAULT_EXTERN(); static const struct zmk_physical_layout _CONCAT(_zmk_physical_layout_, chosen) = { .display_name = "Default", @@ -252,7 +269,7 @@ static int8_t saved_selected_index = -1; int zmk_physical_layouts_select_initial(void) { const struct zmk_physical_layout *initial; -#if DT_HAS_CHOSEN(zmk_physical_layout) +#if USE_PHY_LAYOUTS && DT_HAS_CHOSEN(zmk_physical_layout) initial = &_CONCAT(_zmk_physical_layout_, DT_CHOSEN(zmk_physical_layout)); #else initial = layouts[0]; From cca637d66e36a85706d1807d44a5fecbc66bc5f7 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 19 Sep 2024 01:34:20 -0600 Subject: [PATCH 12/20] fix: Properly calculate highest active layer for display. --- .../arm/corneish_zen/widgets/layer_status.c | 9 +++++---- app/boards/shields/nice_view/widgets/status.c | 7 ++++--- app/include/zmk/keymap.h | 2 +- app/src/display/widgets/layer_status.c | 7 ++++--- app/src/keymap.c | 17 ++++++++++++----- 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/app/boards/arm/corneish_zen/widgets/layer_status.c b/app/boards/arm/corneish_zen/widgets/layer_status.c index 86418318092..82de72c99fd 100644 --- a/app/boards/arm/corneish_zen/widgets/layer_status.c +++ b/app/boards/arm/corneish_zen/widgets/layer_status.c @@ -19,13 +19,13 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); struct layer_status_state { - uint8_t index; + zmk_keymap_layer_index_t index; const char *label; }; static void set_layer_symbol(lv_obj_t *label, struct layer_status_state state) { const char *layer_label = state.label; - uint8_t active_layer_index = state.index; + zmk_keymap_layer_index_t active_layer_index = state.index; if (layer_label == NULL) { char text[6] = {}; @@ -44,8 +44,9 @@ static void layer_status_update_cb(struct layer_status_state state) { } static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { - uint8_t index = zmk_keymap_highest_layer_active(); - return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)}; + zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){ + .index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))}; } ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, diff --git a/app/boards/shields/nice_view/widgets/status.c b/app/boards/shields/nice_view/widgets/status.c index f5095cbf1d8..fa0223551c4 100644 --- a/app/boards/shields/nice_view/widgets/status.c +++ b/app/boards/shields/nice_view/widgets/status.c @@ -36,7 +36,7 @@ struct output_status_state { }; struct layer_status_state { - uint8_t index; + zmk_keymap_layer_index_t index; const char *label; }; @@ -277,8 +277,9 @@ static void layer_status_update_cb(struct layer_status_state state) { } static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { - uint8_t index = zmk_keymap_highest_layer_active(); - return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)}; + zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){ + .index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))}; } ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, diff --git a/app/include/zmk/keymap.h b/app/include/zmk/keymap.h index 50b358ba92a..9b278a6050f 100644 --- a/app/include/zmk/keymap.h +++ b/app/include/zmk/keymap.h @@ -34,7 +34,7 @@ zmk_keymap_layer_id_t zmk_keymap_layer_index_to_id(zmk_keymap_layer_index_t laye zmk_keymap_layer_id_t zmk_keymap_layer_default(void); zmk_keymap_layers_state_t zmk_keymap_layer_state(void); bool zmk_keymap_layer_active(zmk_keymap_layer_id_t layer); -zmk_keymap_layer_id_t zmk_keymap_highest_layer_active(void); +zmk_keymap_layer_index_t zmk_keymap_highest_layer_active(void); int zmk_keymap_layer_activate(zmk_keymap_layer_id_t layer); int zmk_keymap_layer_deactivate(zmk_keymap_layer_id_t layer); int zmk_keymap_layer_toggle(zmk_keymap_layer_id_t layer); diff --git a/app/src/display/widgets/layer_status.c b/app/src/display/widgets/layer_status.c index 19e25d93312..d341ccd3223 100644 --- a/app/src/display/widgets/layer_status.c +++ b/app/src/display/widgets/layer_status.c @@ -18,7 +18,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); static sys_slist_t widgets = SYS_SLIST_STATIC_INIT(&widgets); struct layer_status_state { - uint8_t index; + zmk_keymap_layer_index_t index; const char *label; }; @@ -44,8 +44,9 @@ static void layer_status_update_cb(struct layer_status_state state) { } static struct layer_status_state layer_status_get_state(const zmk_event_t *eh) { - uint8_t index = zmk_keymap_highest_layer_active(); - return (struct layer_status_state){.index = index, .label = zmk_keymap_layer_name(index)}; + zmk_keymap_layer_index_t index = zmk_keymap_highest_layer_active(); + return (struct layer_status_state){ + .index = index, .label = zmk_keymap_layer_name(zmk_keymap_layer_index_to_id(index))}; } ZMK_DISPLAY_WIDGET_LISTENER(widget_layer_status, struct layer_status_state, layer_status_update_cb, diff --git a/app/src/keymap.c b/app/src/keymap.c index 41e57024b77..e49acb579c5 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -182,13 +182,20 @@ bool zmk_keymap_layer_active(zmk_keymap_layer_id_t layer) { return zmk_keymap_layer_active_with_state(layer, _zmk_keymap_layer_state); }; -zmk_keymap_layer_id_t zmk_keymap_highest_layer_active(void) { - for (uint8_t layer = ZMK_KEYMAP_LAYERS_LEN - 1; layer > 0; layer--) { - if (zmk_keymap_layer_active(layer)) { - return layer; +zmk_keymap_layer_index_t zmk_keymap_highest_layer_active(void) { + for (int layer_idx = ZMK_KEYMAP_LAYERS_LEN - 1; + layer_idx >= LAYER_ID_TO_INDEX(_zmk_keymap_layer_default); layer_idx--) { + zmk_keymap_layer_id_t layer_id = LAYER_INDEX_TO_ID(layer_idx); + + if (layer_id == ZMK_KEYMAP_LAYER_ID_INVAL) { + continue; + } + if (zmk_keymap_layer_active(layer_id)) { + return LAYER_ID_TO_INDEX(layer_id); } } - return zmk_keymap_layer_default(); + + return LAYER_ID_TO_INDEX(zmk_keymap_layer_default()); } int zmk_keymap_layer_activate(zmk_keymap_layer_id_t layer) { return set_layer_state(layer, true); }; From 11f600d9e52233a038b86069c99c5f7947b7b925 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Fri, 20 Sep 2024 16:37:10 -0600 Subject: [PATCH 13/20] fix(display): Check layer name length too. --- app/boards/arm/corneish_zen/widgets/layer_status.c | 2 +- app/src/display/widgets/layer_status.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/boards/arm/corneish_zen/widgets/layer_status.c b/app/boards/arm/corneish_zen/widgets/layer_status.c index 82de72c99fd..002ce46b48d 100644 --- a/app/boards/arm/corneish_zen/widgets/layer_status.c +++ b/app/boards/arm/corneish_zen/widgets/layer_status.c @@ -27,7 +27,7 @@ static void set_layer_symbol(lv_obj_t *label, struct layer_status_state state) { const char *layer_label = state.label; zmk_keymap_layer_index_t active_layer_index = state.index; - if (layer_label == NULL) { + if (layer_label == NULL || strlen(layer_label) == 0) { char text[6] = {}; sprintf(text, " %i", active_layer_index); diff --git a/app/src/display/widgets/layer_status.c b/app/src/display/widgets/layer_status.c index d341ccd3223..64cb7c3d599 100644 --- a/app/src/display/widgets/layer_status.c +++ b/app/src/display/widgets/layer_status.c @@ -23,7 +23,7 @@ struct layer_status_state { }; static void set_layer_symbol(lv_obj_t *label, struct layer_status_state state) { - if (state.label == NULL) { + if (state.label == NULL || strlen(state.label) == 0) { char text[8] = {}; snprintf(text, sizeof(text), LV_SYMBOL_KEYBOARD " %i", state.index); From 9e36ebd52587c0364562057466a458fdcfcdc685 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Tue, 17 Jan 2023 19:05:04 +0100 Subject: [PATCH 14/20] feat(split): Make locality work nested behavior invocations Co-authored-by: Tokazio --- app/include/zmk/behavior.h | 14 ++++ app/include/zmk/behavior_queue.h | 4 +- app/include/zmk/split/bluetooth/service.h | 1 + app/src/behavior.c | 67 ++++++++++++++++++ app/src/behavior_queue.c | 17 +++-- app/src/behaviors/behavior_hold_tap.c | 24 ++++--- app/src/behaviors/behavior_macro.c | 15 ++-- app/src/behaviors/behavior_mod_morph.c | 4 +- .../behaviors/behavior_sensor_rotate_common.c | 4 +- app/src/behaviors/behavior_sticky_key.c | 15 ++-- app/src/behaviors/behavior_tap_dance.c | 15 ++-- app/src/keymap.c | 70 ++----------------- app/src/split/bluetooth/central.c | 1 + 13 files changed, 146 insertions(+), 105 deletions(-) diff --git a/app/include/zmk/behavior.h b/app/include/zmk/behavior.h index d45bbfffe75..0940fc6eeef 100644 --- a/app/include/zmk/behavior.h +++ b/app/include/zmk/behavior.h @@ -26,6 +26,7 @@ struct zmk_behavior_binding_event { int layer; uint32_t position; int64_t timestamp; + uint8_t source; }; /** @@ -42,6 +43,19 @@ struct zmk_behavior_binding_event { */ const struct device *zmk_behavior_get_binding(const char *name); +/** + * @brief Invoke a behavior given its binding and invoking event details. + * + * @param src_binding Behavior binding to invoke. + * @param event The binding event struct containing details of the event that invoked it. + * @param pressed Whether the binding is pressed or released. + * + * @retval 0 If successful. + * @retval Negative errno code if failure. + */ +int zmk_behavior_invoke_binding(const struct zmk_behavior_binding *src_binding, + struct zmk_behavior_binding_event event, bool pressed); + /** * @brief Get a local ID for a behavior from its @p name field. * diff --git a/app/include/zmk/behavior_queue.h b/app/include/zmk/behavior_queue.h index 307482e7cd4..781f582e095 100644 --- a/app/include/zmk/behavior_queue.h +++ b/app/include/zmk/behavior_queue.h @@ -10,5 +10,5 @@ #include #include -int zmk_behavior_queue_add(uint32_t position, const struct zmk_behavior_binding behavior, - bool press, uint32_t wait); +int zmk_behavior_queue_add(uint32_t position, uint8_t source, + const struct zmk_behavior_binding behavior, bool press, uint32_t wait); diff --git a/app/include/zmk/split/bluetooth/service.h b/app/include/zmk/split/bluetooth/service.h index 112cd552942..1c9e75226ad 100644 --- a/app/include/zmk/split/bluetooth/service.h +++ b/app/include/zmk/split/bluetooth/service.h @@ -20,6 +20,7 @@ struct sensor_event { struct zmk_split_run_behavior_data { uint8_t position; + uint8_t source; uint8_t state; uint32_t param1; uint32_t param2; diff --git a/app/src/behavior.c b/app/src/behavior.c index e69cdf88702..f24f0223634 100644 --- a/app/src/behavior.c +++ b/app/src/behavior.c @@ -17,11 +17,18 @@ #endif +#include +#if ZMK_BLE_IS_CENTRAL +#include +#endif + #include #include #include #include +#include + #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -49,6 +56,66 @@ const struct device *z_impl_behavior_get_binding(const char *name) { return NULL; } +static int invoke_locally(struct zmk_behavior_binding *binding, + struct zmk_behavior_binding_event event, bool pressed) { + if (pressed) { + return behavior_keymap_binding_pressed(binding, event); + } else { + return behavior_keymap_binding_released(binding, event); + } +} + +int zmk_behavior_invoke_binding(const struct zmk_behavior_binding *src_binding, + struct zmk_behavior_binding_event event, bool pressed) { + // We want to make a copy of this, since it may be converted from + // relative to absolute before being invoked + struct zmk_behavior_binding binding = *src_binding; + + const struct device *behavior = zmk_behavior_get_binding(binding.behavior_dev); + + if (!behavior) { + LOG_WRN("No behavior assigned to %d on layer %d", event.position, event.layer); + return 1; + } + + int err = behavior_keymap_binding_convert_central_state_dependent_params(&binding, event); + if (err) { + LOG_ERR("Failed to convert relative to absolute behavior binding (err %d)", err); + return err; + } + + enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL; + err = behavior_get_locality(behavior, &locality); + if (err) { + LOG_ERR("Failed to get behavior locality %d", err); + return err; + } + + switch (locality) { + case BEHAVIOR_LOCALITY_CENTRAL: + return invoke_locally(&binding, event, pressed); + case BEHAVIOR_LOCALITY_EVENT_SOURCE: +#if ZMK_BLE_IS_CENTRAL + if (event.source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) { + return invoke_locally(&binding, event, pressed); + } else { + return zmk_split_bt_invoke_behavior(event.source, &binding, event, pressed); + } +#else + return invoke_locally(&binding, event, pressed); +#endif + case BEHAVIOR_LOCALITY_GLOBAL: +#if ZMK_BLE_IS_CENTRAL + for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { + zmk_split_bt_invoke_behavior(i, &binding, event, pressed); + } +#endif + return invoke_locally(&binding, event, pressed); + } + + return -ENOTSUP; +} + #if IS_ENABLED(CONFIG_ZMK_BEHAVIOR_METADATA) int zmk_behavior_get_empty_param_metadata(const struct device *dev, diff --git a/app/src/behavior_queue.c b/app/src/behavior_queue.c index 1511e755d4f..19275fe1516 100644 --- a/app/src/behavior_queue.c +++ b/app/src/behavior_queue.c @@ -5,6 +5,7 @@ */ #include +#include #include #include @@ -14,6 +15,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct q_item { uint32_t position; + uint8_t source; struct zmk_behavior_binding binding; bool press : 1; uint32_t wait : 31; @@ -31,13 +33,13 @@ static void behavior_queue_process_next(struct k_work *work) { LOG_DBG("Invoking %s: 0x%02x 0x%02x", item.binding.behavior_dev, item.binding.param1, item.binding.param2); - struct zmk_behavior_binding_event event = {.position = item.position, - .timestamp = k_uptime_get()}; + struct zmk_behavior_binding_event event = { + .position = item.position, .timestamp = k_uptime_get(), .source = item.source}; if (item.press) { - behavior_keymap_binding_pressed(&item.binding, event); + zmk_behavior_invoke_binding(&item.binding, event, true); } else { - behavior_keymap_binding_released(&item.binding, event); + zmk_behavior_invoke_binding(&item.binding, event, false); } LOG_DBG("Processing next queued behavior in %dms", item.wait); @@ -49,9 +51,10 @@ static void behavior_queue_process_next(struct k_work *work) { } } -int zmk_behavior_queue_add(uint32_t position, const struct zmk_behavior_binding binding, bool press, - uint32_t wait) { - struct q_item item = {.press = press, .binding = binding, .wait = wait}; +int zmk_behavior_queue_add(uint32_t position, uint8_t source, + const struct zmk_behavior_binding binding, bool press, uint32_t wait) { + struct q_item item = { + .press = press, .binding = binding, .wait = wait, .position = position, .source = source}; const int ret = k_msgq_put(&zmk_behavior_queue_msgq, &item, K_NO_WAIT); if (ret < 0) { diff --git a/app/src/behaviors/behavior_hold_tap.c b/app/src/behaviors/behavior_hold_tap.c index c45ee803f53..7280451a063 100644 --- a/app/src/behaviors/behavior_hold_tap.c +++ b/app/src/behaviors/behavior_hold_tap.c @@ -18,7 +18,6 @@ #include #include #include -#include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -77,6 +76,7 @@ struct behavior_hold_tap_data { // this data is specific for each hold-tap struct active_hold_tap { int32_t position; + uint8_t source; uint32_t param_hold; uint32_t param_tap; int64_t timestamp; @@ -250,14 +250,16 @@ static struct active_hold_tap *find_hold_tap(uint32_t position) { return NULL; } -static struct active_hold_tap *store_hold_tap(uint32_t position, uint32_t param_hold, - uint32_t param_tap, int64_t timestamp, +static struct active_hold_tap *store_hold_tap(uint32_t position, uint8_t source, + uint32_t param_hold, uint32_t param_tap, + int64_t timestamp, const struct behavior_hold_tap_config *config) { for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { if (active_hold_taps[i].position != ZMK_BHV_HOLD_TAP_POSITION_NOT_USED) { continue; } active_hold_taps[i].position = position; + active_hold_taps[i].source = source; active_hold_taps[i].status = STATUS_UNDECIDED; active_hold_taps[i].config = config; active_hold_taps[i].param_hold = param_hold; @@ -400,45 +402,49 @@ static int press_hold_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, + .source = hold_tap->source, }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev, .param1 = hold_tap->param_hold}; - return behavior_keymap_binding_pressed(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, true); } static int press_tap_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, + .source = hold_tap->source, }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev, .param1 = hold_tap->param_tap}; store_last_hold_tapped(hold_tap); - return behavior_keymap_binding_pressed(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, true); } static int release_hold_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, + .source = hold_tap->source, }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev, .param1 = hold_tap->param_hold}; - return behavior_keymap_binding_released(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, false); } static int release_tap_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, + .source = hold_tap->source, }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev, .param1 = hold_tap->param_tap}; - return behavior_keymap_binding_released(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, false); } static int press_binding(struct active_hold_tap *hold_tap) { @@ -597,8 +603,8 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding, return ZMK_BEHAVIOR_OPAQUE; } - struct active_hold_tap *hold_tap = - store_hold_tap(event.position, binding->param1, binding->param2, event.timestamp, cfg); + struct active_hold_tap *hold_tap = store_hold_tap(event.position, event.source, binding->param1, + binding->param2, event.timestamp, cfg); if (hold_tap == NULL) { LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?", ZMK_BHV_HOLD_TAP_MAX_HELD); diff --git a/app/src/behaviors/behavior_macro.c b/app/src/behaviors/behavior_macro.c index b535ed8be07..adf3fa65747 100644 --- a/app/src/behaviors/behavior_macro.c +++ b/app/src/behaviors/behavior_macro.c @@ -158,7 +158,8 @@ static void replace_params(struct behavior_macro_trigger_state *state, state->param2_source = PARAM_SOURCE_BINDING; } -static void queue_macro(uint32_t position, const struct zmk_behavior_binding bindings[], +static void queue_macro(uint32_t position, uint8_t source, + const struct zmk_behavior_binding bindings[], struct behavior_macro_trigger_state state, const struct zmk_behavior_binding *macro_binding) { LOG_DBG("Iterating macro bindings - starting: %d, count: %d", state.start_index, state.count); @@ -169,14 +170,14 @@ static void queue_macro(uint32_t position, const struct zmk_behavior_binding bin switch (state.mode) { case MACRO_MODE_TAP: - zmk_behavior_queue_add(position, binding, true, state.tap_ms); - zmk_behavior_queue_add(position, binding, false, state.wait_ms); + zmk_behavior_queue_add(position, source, binding, true, state.tap_ms); + zmk_behavior_queue_add(position, source, binding, false, state.wait_ms); break; case MACRO_MODE_PRESS: - zmk_behavior_queue_add(position, binding, true, state.wait_ms); + zmk_behavior_queue_add(position, source, binding, true, state.wait_ms); break; case MACRO_MODE_RELEASE: - zmk_behavior_queue_add(position, binding, false, state.wait_ms); + zmk_behavior_queue_add(position, source, binding, false, state.wait_ms); break; default: LOG_ERR("Unknown macro mode: %d", state.mode); @@ -197,7 +198,7 @@ static int on_macro_binding_pressed(struct zmk_behavior_binding *binding, .start_index = 0, .count = state->press_bindings_count}; - queue_macro(event.position, cfg->bindings, trigger_state, binding); + queue_macro(event.position, event.source, cfg->bindings, trigger_state, binding); return ZMK_BEHAVIOR_OPAQUE; } @@ -208,7 +209,7 @@ static int on_macro_binding_released(struct zmk_behavior_binding *binding, const struct behavior_macro_config *cfg = dev->config; struct behavior_macro_state *state = dev->data; - queue_macro(event.position, cfg->bindings, state->release_state, binding); + queue_macro(event.position, event.source, cfg->bindings, state->release_state, binding); return ZMK_BEHAVIOR_OPAQUE; } diff --git a/app/src/behaviors/behavior_mod_morph.c b/app/src/behaviors/behavior_mod_morph.c index 303f96a7d05..6698f24886f 100644 --- a/app/src/behaviors/behavior_mod_morph.c +++ b/app/src/behaviors/behavior_mod_morph.c @@ -51,7 +51,7 @@ static int on_mod_morph_binding_pressed(struct zmk_behavior_binding *binding, } else { data->pressed_binding = (struct zmk_behavior_binding *)&cfg->normal_binding; } - return behavior_keymap_binding_pressed(data->pressed_binding, event); + return zmk_behavior_invoke_binding(data->pressed_binding, event, true); } static int on_mod_morph_binding_released(struct zmk_behavior_binding *binding, @@ -67,7 +67,7 @@ static int on_mod_morph_binding_released(struct zmk_behavior_binding *binding, struct zmk_behavior_binding *pressed_binding = data->pressed_binding; data->pressed_binding = NULL; int err; - err = behavior_keymap_binding_released(pressed_binding, event); + err = zmk_behavior_invoke_binding(pressed_binding, event, false); zmk_hid_masked_modifiers_clear(); return err; } diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index 94bf40c18d4..677443ee290 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -90,8 +90,8 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi LOG_DBG("Sensor binding: %s", binding->behavior_dev); for (int i = 0; i < triggers; i++) { - zmk_behavior_queue_add(event.position, triggered_binding, true, cfg->tap_ms); - zmk_behavior_queue_add(event.position, triggered_binding, false, 0); + zmk_behavior_queue_add(event.position, event.source, triggered_binding, true, cfg->tap_ms); + zmk_behavior_queue_add(event.position, event.source, triggered_binding, false, 0); } return ZMK_BEHAVIOR_OPAQUE; diff --git a/app/src/behaviors/behavior_sticky_key.c b/app/src/behaviors/behavior_sticky_key.c index 61c86fb7d21..a77ba4d08c4 100644 --- a/app/src/behaviors/behavior_sticky_key.c +++ b/app/src/behaviors/behavior_sticky_key.c @@ -40,6 +40,7 @@ struct behavior_sticky_key_config { struct active_sticky_key { uint32_t position; + uint8_t source; uint32_t param1; uint32_t param2; const struct behavior_sticky_key_config *config; @@ -55,8 +56,8 @@ struct active_sticky_key { struct active_sticky_key active_sticky_keys[ZMK_BHV_STICKY_KEY_MAX_HELD] = {}; -static struct active_sticky_key *store_sticky_key(uint32_t position, uint32_t param1, - uint32_t param2, +static struct active_sticky_key *store_sticky_key(uint32_t position, uint8_t source, + uint32_t param1, uint32_t param2, const struct behavior_sticky_key_config *config) { for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) { struct active_sticky_key *const sticky_key = &active_sticky_keys[i]; @@ -65,6 +66,7 @@ static struct active_sticky_key *store_sticky_key(uint32_t position, uint32_t pa continue; } sticky_key->position = position; + sticky_key->source = source; sticky_key->param1 = param1; sticky_key->param2 = param2; sticky_key->config = config; @@ -101,8 +103,9 @@ static inline int press_sticky_key_behavior(struct active_sticky_key *sticky_key struct zmk_behavior_binding_event event = { .position = sticky_key->position, .timestamp = timestamp, + .source = sticky_key->source, }; - return behavior_keymap_binding_pressed(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, true); } static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_key, @@ -115,10 +118,11 @@ static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_k struct zmk_behavior_binding_event event = { .position = sticky_key->position, .timestamp = timestamp, + .source = sticky_key->source, }; clear_sticky_key(sticky_key); - return behavior_keymap_binding_released(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, false); } static inline void on_sticky_key_timeout(struct active_sticky_key *sticky_key) { @@ -149,7 +153,8 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding, stop_timer(sticky_key); release_sticky_key_behavior(sticky_key, event.timestamp); } - sticky_key = store_sticky_key(event.position, binding->param1, binding->param2, cfg); + sticky_key = + store_sticky_key(event.position, event.source, binding->param1, binding->param2, cfg); if (sticky_key == NULL) { LOG_ERR("unable to store sticky key, did you press more than %d sticky_key?", ZMK_BHV_STICKY_KEY_MAX_HELD); diff --git a/app/src/behaviors/behavior_tap_dance.c b/app/src/behaviors/behavior_tap_dance.c index 61e755850d9..606a16393cd 100644 --- a/app/src/behaviors/behavior_tap_dance.c +++ b/app/src/behaviors/behavior_tap_dance.c @@ -35,6 +35,7 @@ struct active_tap_dance { // Tap Dance Data int counter; uint32_t position; + uint8_t source; uint32_t param1; uint32_t param2; bool is_pressed; @@ -59,13 +60,15 @@ static struct active_tap_dance *find_tap_dance(uint32_t position) { return NULL; } -static int new_tap_dance(uint32_t position, const struct behavior_tap_dance_config *config, +static int new_tap_dance(uint32_t position, uint8_t source, + const struct behavior_tap_dance_config *config, struct active_tap_dance **tap_dance) { for (int i = 0; i < ZMK_BHV_TAP_DANCE_MAX_HELD; i++) { struct active_tap_dance *const ref_dance = &active_tap_dances[i]; if (ref_dance->position == ZMK_BHV_TAP_DANCE_POSITION_FREE) { ref_dance->counter = 0; ref_dance->position = position; + ref_dance->source = source; ref_dance->config = config; ref_dance->release_at = 0; ref_dance->is_pressed = true; @@ -108,8 +111,9 @@ static inline int press_tap_dance_behavior(struct active_tap_dance *tap_dance, i struct zmk_behavior_binding_event event = { .position = tap_dance->position, .timestamp = timestamp, + .source = tap_dance->source, }; - return behavior_keymap_binding_pressed(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, true); } static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance, @@ -118,9 +122,10 @@ static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance, struct zmk_behavior_binding_event event = { .position = tap_dance->position, .timestamp = timestamp, + .source = tap_dance->source, }; clear_tap_dance(tap_dance); - return behavior_keymap_binding_released(&binding, event); + return zmk_behavior_invoke_binding(&binding, event, false); } static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding, @@ -130,7 +135,7 @@ static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding, struct active_tap_dance *tap_dance; tap_dance = find_tap_dance(event.position); if (tap_dance == NULL) { - if (new_tap_dance(event.position, cfg, &tap_dance) == -ENOMEM) { + if (new_tap_dance(event.position, event.source, cfg, &tap_dance) == -ENOMEM) { LOG_ERR("Unable to create new tap dance. Insufficient space in active_tap_dances[]."); return ZMK_BEHAVIOR_OPAQUE; } @@ -261,4 +266,4 @@ static int behavior_tap_dance_init(const struct device *dev) { DT_INST_FOREACH_STATUS_OKAY(KP_INST) -#endif \ No newline at end of file +#endif diff --git a/app/src/keymap.c b/app/src/keymap.c index e49acb579c5..d4a4ab1f7de 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -7,7 +7,6 @@ #include #include #include -#include #include LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); @@ -18,11 +17,6 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include -#include -#if ZMK_BLE_IS_CENTRAL -#include -#endif - #include #include #include @@ -585,76 +579,20 @@ int zmk_keymap_reset_settings(void) { return -ENOTSUP; } #endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_SETTINGS_STORAGE) -int invoke_locally(struct zmk_behavior_binding *binding, struct zmk_behavior_binding_event event, - bool pressed) { - if (pressed) { - return behavior_keymap_binding_pressed(binding, event); - } else { - return behavior_keymap_binding_released(binding, event); - } -} - int zmk_keymap_apply_position_state(uint8_t source, zmk_keymap_layer_id_t layer_id, uint32_t position, bool pressed, int64_t timestamp) { - // We want to make a copy of this, since it may be converted from - // relative to absolute before being invoked - - ASSERT_LAYER_VAL(layer_id, -EINVAL); - - struct zmk_behavior_binding binding = zmk_keymap[layer_id][position]; - const struct device *behavior; + struct zmk_behavior_binding *binding = &zmk_keymap[layer_id][position]; struct zmk_behavior_binding_event event = { .layer = layer_id, .position = position, .timestamp = timestamp, + .source = source, }; LOG_DBG("layer_id: %d position: %d, binding name: %s", layer_id, position, - binding.behavior_dev); - - behavior = zmk_behavior_get_binding(binding.behavior_dev); - - if (!behavior) { - LOG_WRN("No behavior assigned to %d on layer %d", position, layer_id); - return 1; - } + binding->behavior_dev); - int err = behavior_keymap_binding_convert_central_state_dependent_params(&binding, event); - if (err) { - LOG_ERR("Failed to convert relative to absolute behavior binding (err %d)", err); - return err; - } - - enum behavior_locality locality = BEHAVIOR_LOCALITY_CENTRAL; - err = behavior_get_locality(behavior, &locality); - if (err) { - LOG_ERR("Failed to get behavior locality %d", err); - return err; - } - - switch (locality) { - case BEHAVIOR_LOCALITY_CENTRAL: - return invoke_locally(&binding, event, pressed); - case BEHAVIOR_LOCALITY_EVENT_SOURCE: -#if ZMK_BLE_IS_CENTRAL - if (source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) { - return invoke_locally(&binding, event, pressed); - } else { - return zmk_split_bt_invoke_behavior(source, &binding, event, pressed); - } -#else - return invoke_locally(&binding, event, pressed); -#endif - case BEHAVIOR_LOCALITY_GLOBAL: -#if ZMK_BLE_IS_CENTRAL - for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { - zmk_split_bt_invoke_behavior(i, &binding, event, pressed); - } -#endif - return invoke_locally(&binding, event, pressed); - } - - return -ENOTSUP; + return zmk_behavior_invoke_binding(binding, event, pressed); } int zmk_keymap_position_state_changed(uint8_t source, uint32_t position, bool pressed, diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 0f4cd78b531..9c459bf1b1d 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -816,6 +816,7 @@ int zmk_split_bt_invoke_behavior(uint8_t source, struct zmk_behavior_binding *bi .param1 = binding->param1, .param2 = binding->param2, .position = event.position, + .source = event.source, .state = state ? 1 : 0, }}; const size_t payload_dev_size = sizeof(payload.behavior_dev); From 4fdfb01b6aa3715ae9e28391991f8bb1b604db4d Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Fri, 9 Aug 2024 00:07:21 -0700 Subject: [PATCH 15/20] feat(split): Make combos invoke behaviors with locality TODO: Currently the source is hardcoded to central for source local behaviors --- app/src/combo.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/src/combo.c b/app/src/combo.c index 3f78878f01f..a990e2f2a60 100644 --- a/app/src/combo.c +++ b/app/src/combo.c @@ -291,21 +291,23 @@ static int release_pressed_keys() { static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestamp) { struct zmk_behavior_binding_event event = { .position = combo->virtual_key_position, + .source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, .timestamp = timestamp, }; last_combo_timestamp = timestamp; - return behavior_keymap_binding_pressed(&combo->behavior, event); + return zmk_behavior_invoke_binding(&combo->behavior, event, true); } static inline int release_combo_behavior(struct combo_cfg *combo, int32_t timestamp) { struct zmk_behavior_binding_event event = { .position = combo->virtual_key_position, + .source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, .timestamp = timestamp, }; - return behavior_keymap_binding_released(&combo->behavior, event); + return zmk_behavior_invoke_binding(&combo->behavior, event, false); } static void move_pressed_keys_to_active_combo(struct active_combo *active_combo) { From b249135742ebba10c07fa899b50cbb260c155a45 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Fri, 9 Aug 2024 11:27:36 -0700 Subject: [PATCH 16/20] feat(sensors): Make sensors always trigger on central (for now) --- app/src/behaviors/behavior_sensor_rotate_common.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index 677443ee290..e8fd7c37a23 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -6,6 +6,7 @@ #include #include +#include #include "behavior_sensor_rotate_common.h" @@ -90,8 +91,10 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi LOG_DBG("Sensor binding: %s", binding->behavior_dev); for (int i = 0; i < triggers; i++) { - zmk_behavior_queue_add(event.position, event.source, triggered_binding, true, cfg->tap_ms); - zmk_behavior_queue_add(event.position, event.source, triggered_binding, false, 0); + zmk_behavior_queue_add(event.position, ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, + triggered_binding, true, cfg->tap_ms); + zmk_behavior_queue_add(event.position, ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, + triggered_binding, false, 0); } return ZMK_BEHAVIOR_OPAQUE; From fb18a4d871485b7e184f7a13d3095cf624b2b433 Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Sat, 17 Aug 2024 21:03:43 -0700 Subject: [PATCH 17/20] refactor: Condition source props on CONFIG_ZMK_SPLIT --- app/include/zmk/behavior.h | 2 ++ app/include/zmk/behavior_queue.h | 2 +- app/src/behavior.c | 2 +- app/src/behavior_queue.c | 20 +++++++++++--- app/src/behaviors/behavior_hold_tap.c | 26 ++++++++++++++----- app/src/behaviors/behavior_macro.c | 14 +++++----- .../behaviors/behavior_sensor_rotate_common.c | 11 +++++--- app/src/behaviors/behavior_sticky_key.c | 17 ++++++++---- app/src/behaviors/behavior_tap_dance.c | 16 +++++++++--- app/src/combo.c | 8 ++++-- app/src/keymap.c | 2 ++ 11 files changed, 86 insertions(+), 34 deletions(-) diff --git a/app/include/zmk/behavior.h b/app/include/zmk/behavior.h index 0940fc6eeef..5028d320257 100644 --- a/app/include/zmk/behavior.h +++ b/app/include/zmk/behavior.h @@ -26,7 +26,9 @@ struct zmk_behavior_binding_event { int layer; uint32_t position; int64_t timestamp; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) uint8_t source; +#endif }; /** diff --git a/app/include/zmk/behavior_queue.h b/app/include/zmk/behavior_queue.h index 781f582e095..b942bd28958 100644 --- a/app/include/zmk/behavior_queue.h +++ b/app/include/zmk/behavior_queue.h @@ -10,5 +10,5 @@ #include #include -int zmk_behavior_queue_add(uint32_t position, uint8_t source, +int zmk_behavior_queue_add(const struct zmk_behavior_binding_event *event, const struct zmk_behavior_binding behavior, bool press, uint32_t wait); diff --git a/app/src/behavior.c b/app/src/behavior.c index f24f0223634..9b20c706265 100644 --- a/app/src/behavior.c +++ b/app/src/behavior.c @@ -95,7 +95,7 @@ int zmk_behavior_invoke_binding(const struct zmk_behavior_binding *src_binding, case BEHAVIOR_LOCALITY_CENTRAL: return invoke_locally(&binding, event, pressed); case BEHAVIOR_LOCALITY_EVENT_SOURCE: -#if ZMK_BLE_IS_CENTRAL +#if ZMK_BLE_IS_CENTRAL // source is a member of event because CONFIG_ZMK_SPLIT is enabled if (event.source == ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL) { return invoke_locally(&binding, event, pressed); } else { diff --git a/app/src/behavior_queue.c b/app/src/behavior_queue.c index 19275fe1516..86837f42332 100644 --- a/app/src/behavior_queue.c +++ b/app/src/behavior_queue.c @@ -15,7 +15,9 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); struct q_item { uint32_t position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) uint8_t source; +#endif struct zmk_behavior_binding binding; bool press : 1; uint32_t wait : 31; @@ -34,7 +36,12 @@ static void behavior_queue_process_next(struct k_work *work) { item.binding.param2); struct zmk_behavior_binding_event event = { - .position = item.position, .timestamp = k_uptime_get(), .source = item.source}; + .position = item.position, + .timestamp = k_uptime_get(), +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + .source = item.source +#endif + }; if (item.press) { zmk_behavior_invoke_binding(&item.binding, event, true); @@ -51,10 +58,17 @@ static void behavior_queue_process_next(struct k_work *work) { } } -int zmk_behavior_queue_add(uint32_t position, uint8_t source, +int zmk_behavior_queue_add(const struct zmk_behavior_binding_event *event, const struct zmk_behavior_binding binding, bool press, uint32_t wait) { struct q_item item = { - .press = press, .binding = binding, .wait = wait, .position = position, .source = source}; + .press = press, + .binding = binding, + .wait = wait, + .position = event->position, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + .source = event->source, +#endif + }; const int ret = k_msgq_put(&zmk_behavior_queue_msgq, &item, K_NO_WAIT); if (ret < 0) { diff --git a/app/src/behaviors/behavior_hold_tap.c b/app/src/behaviors/behavior_hold_tap.c index 7280451a063..3df3bc86436 100644 --- a/app/src/behaviors/behavior_hold_tap.c +++ b/app/src/behaviors/behavior_hold_tap.c @@ -76,7 +76,9 @@ struct behavior_hold_tap_data { // this data is specific for each hold-tap struct active_hold_tap { int32_t position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) uint8_t source; +#endif uint32_t param_hold; uint32_t param_tap; int64_t timestamp; @@ -250,21 +252,22 @@ static struct active_hold_tap *find_hold_tap(uint32_t position) { return NULL; } -static struct active_hold_tap *store_hold_tap(uint32_t position, uint8_t source, +static struct active_hold_tap *store_hold_tap(struct zmk_behavior_binding_event *event, uint32_t param_hold, uint32_t param_tap, - int64_t timestamp, const struct behavior_hold_tap_config *config) { for (int i = 0; i < ZMK_BHV_HOLD_TAP_MAX_HELD; i++) { if (active_hold_taps[i].position != ZMK_BHV_HOLD_TAP_POSITION_NOT_USED) { continue; } - active_hold_taps[i].position = position; - active_hold_taps[i].source = source; + active_hold_taps[i].position = event->position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + active_hold_taps[i].source = event->source; +#endif active_hold_taps[i].status = STATUS_UNDECIDED; active_hold_taps[i].config = config; active_hold_taps[i].param_hold = param_hold; active_hold_taps[i].param_tap = param_tap; - active_hold_taps[i].timestamp = timestamp; + active_hold_taps[i].timestamp = event->timestamp; active_hold_taps[i].position_of_first_other_key_pressed = -1; return &active_hold_taps[i]; } @@ -402,7 +405,9 @@ static int press_hold_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = hold_tap->source, +#endif }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev, @@ -414,7 +419,9 @@ static int press_tap_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = hold_tap->source, +#endif }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev, @@ -427,7 +434,9 @@ static int release_hold_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = hold_tap->source, +#endif }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->hold_behavior_dev, @@ -439,7 +448,9 @@ static int release_tap_binding(struct active_hold_tap *hold_tap) { struct zmk_behavior_binding_event event = { .position = hold_tap->position, .timestamp = hold_tap->timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = hold_tap->source, +#endif }; struct zmk_behavior_binding binding = {.behavior_dev = hold_tap->config->tap_behavior_dev, @@ -603,8 +614,9 @@ static int on_hold_tap_binding_pressed(struct zmk_behavior_binding *binding, return ZMK_BEHAVIOR_OPAQUE; } - struct active_hold_tap *hold_tap = store_hold_tap(event.position, event.source, binding->param1, - binding->param2, event.timestamp, cfg); + struct active_hold_tap *hold_tap = + store_hold_tap(&event, binding->param1, binding->param2, cfg); + if (hold_tap == NULL) { LOG_ERR("unable to store hold-tap info, did you press more than %d hold-taps?", ZMK_BHV_HOLD_TAP_MAX_HELD); diff --git a/app/src/behaviors/behavior_macro.c b/app/src/behaviors/behavior_macro.c index adf3fa65747..c16fb69a9e9 100644 --- a/app/src/behaviors/behavior_macro.c +++ b/app/src/behaviors/behavior_macro.c @@ -158,7 +158,7 @@ static void replace_params(struct behavior_macro_trigger_state *state, state->param2_source = PARAM_SOURCE_BINDING; } -static void queue_macro(uint32_t position, uint8_t source, +static void queue_macro(struct zmk_behavior_binding_event *event, const struct zmk_behavior_binding bindings[], struct behavior_macro_trigger_state state, const struct zmk_behavior_binding *macro_binding) { @@ -170,14 +170,14 @@ static void queue_macro(uint32_t position, uint8_t source, switch (state.mode) { case MACRO_MODE_TAP: - zmk_behavior_queue_add(position, source, binding, true, state.tap_ms); - zmk_behavior_queue_add(position, source, binding, false, state.wait_ms); + zmk_behavior_queue_add(event, binding, true, state.tap_ms); + zmk_behavior_queue_add(event, binding, false, state.wait_ms); break; case MACRO_MODE_PRESS: - zmk_behavior_queue_add(position, source, binding, true, state.wait_ms); + zmk_behavior_queue_add(event, binding, true, state.wait_ms); break; case MACRO_MODE_RELEASE: - zmk_behavior_queue_add(position, source, binding, false, state.wait_ms); + zmk_behavior_queue_add(event, binding, false, state.wait_ms); break; default: LOG_ERR("Unknown macro mode: %d", state.mode); @@ -198,7 +198,7 @@ static int on_macro_binding_pressed(struct zmk_behavior_binding *binding, .start_index = 0, .count = state->press_bindings_count}; - queue_macro(event.position, event.source, cfg->bindings, trigger_state, binding); + queue_macro(&event, cfg->bindings, trigger_state, binding); return ZMK_BEHAVIOR_OPAQUE; } @@ -209,7 +209,7 @@ static int on_macro_binding_released(struct zmk_behavior_binding *binding, const struct behavior_macro_config *cfg = dev->config; struct behavior_macro_state *state = dev->data; - queue_macro(event.position, event.source, cfg->bindings, state->release_state, binding); + queue_macro(&event, cfg->bindings, state->release_state, binding); return ZMK_BEHAVIOR_OPAQUE; } diff --git a/app/src/behaviors/behavior_sensor_rotate_common.c b/app/src/behaviors/behavior_sensor_rotate_common.c index e8fd7c37a23..278f1cb2be7 100644 --- a/app/src/behaviors/behavior_sensor_rotate_common.c +++ b/app/src/behaviors/behavior_sensor_rotate_common.c @@ -90,11 +90,14 @@ int zmk_behavior_sensor_rotate_common_process(struct zmk_behavior_binding *bindi LOG_DBG("Sensor binding: %s", binding->behavior_dev); +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + // set this value so that it always triggers on central, can be handled more properly later + event.source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL; +#endif + for (int i = 0; i < triggers; i++) { - zmk_behavior_queue_add(event.position, ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, - triggered_binding, true, cfg->tap_ms); - zmk_behavior_queue_add(event.position, ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, - triggered_binding, false, 0); + zmk_behavior_queue_add(&event, triggered_binding, true, cfg->tap_ms); + zmk_behavior_queue_add(&event, triggered_binding, false, 0); } return ZMK_BEHAVIOR_OPAQUE; diff --git a/app/src/behaviors/behavior_sticky_key.c b/app/src/behaviors/behavior_sticky_key.c index a77ba4d08c4..3faeec53a39 100644 --- a/app/src/behaviors/behavior_sticky_key.c +++ b/app/src/behaviors/behavior_sticky_key.c @@ -40,7 +40,9 @@ struct behavior_sticky_key_config { struct active_sticky_key { uint32_t position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) uint8_t source; +#endif uint32_t param1; uint32_t param2; const struct behavior_sticky_key_config *config; @@ -56,7 +58,7 @@ struct active_sticky_key { struct active_sticky_key active_sticky_keys[ZMK_BHV_STICKY_KEY_MAX_HELD] = {}; -static struct active_sticky_key *store_sticky_key(uint32_t position, uint8_t source, +static struct active_sticky_key *store_sticky_key(struct zmk_behavior_binding_event *event, uint32_t param1, uint32_t param2, const struct behavior_sticky_key_config *config) { for (int i = 0; i < ZMK_BHV_STICKY_KEY_MAX_HELD; i++) { @@ -65,8 +67,10 @@ static struct active_sticky_key *store_sticky_key(uint32_t position, uint8_t sou sticky_key->timer_cancelled) { continue; } - sticky_key->position = position; - sticky_key->source = source; + sticky_key->position = event->position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + sticky_key->source = event->source; +#endif sticky_key->param1 = param1; sticky_key->param2 = param2; sticky_key->config = config; @@ -103,7 +107,9 @@ static inline int press_sticky_key_behavior(struct active_sticky_key *sticky_key struct zmk_behavior_binding_event event = { .position = sticky_key->position, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = sticky_key->source, +#endif }; return zmk_behavior_invoke_binding(&binding, event, true); } @@ -118,7 +124,9 @@ static inline int release_sticky_key_behavior(struct active_sticky_key *sticky_k struct zmk_behavior_binding_event event = { .position = sticky_key->position, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = sticky_key->source, +#endif }; clear_sticky_key(sticky_key); @@ -153,8 +161,7 @@ static int on_sticky_key_binding_pressed(struct zmk_behavior_binding *binding, stop_timer(sticky_key); release_sticky_key_behavior(sticky_key, event.timestamp); } - sticky_key = - store_sticky_key(event.position, event.source, binding->param1, binding->param2, cfg); + sticky_key = store_sticky_key(&event, binding->param1, binding->param2, cfg); if (sticky_key == NULL) { LOG_ERR("unable to store sticky key, did you press more than %d sticky_key?", ZMK_BHV_STICKY_KEY_MAX_HELD); diff --git a/app/src/behaviors/behavior_tap_dance.c b/app/src/behaviors/behavior_tap_dance.c index 606a16393cd..5423f93f7dc 100644 --- a/app/src/behaviors/behavior_tap_dance.c +++ b/app/src/behaviors/behavior_tap_dance.c @@ -35,7 +35,9 @@ struct active_tap_dance { // Tap Dance Data int counter; uint32_t position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) uint8_t source; +#endif uint32_t param1; uint32_t param2; bool is_pressed; @@ -60,15 +62,17 @@ static struct active_tap_dance *find_tap_dance(uint32_t position) { return NULL; } -static int new_tap_dance(uint32_t position, uint8_t source, +static int new_tap_dance(struct zmk_behavior_binding_event *event, const struct behavior_tap_dance_config *config, struct active_tap_dance **tap_dance) { for (int i = 0; i < ZMK_BHV_TAP_DANCE_MAX_HELD; i++) { struct active_tap_dance *const ref_dance = &active_tap_dances[i]; if (ref_dance->position == ZMK_BHV_TAP_DANCE_POSITION_FREE) { ref_dance->counter = 0; - ref_dance->position = position; - ref_dance->source = source; + ref_dance->position = event->position; +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + ref_dance->source = event->source; +#endif ref_dance->config = config; ref_dance->release_at = 0; ref_dance->is_pressed = true; @@ -111,7 +115,9 @@ static inline int press_tap_dance_behavior(struct active_tap_dance *tap_dance, i struct zmk_behavior_binding_event event = { .position = tap_dance->position, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = tap_dance->source, +#endif }; return zmk_behavior_invoke_binding(&binding, event, true); } @@ -122,7 +128,9 @@ static inline int release_tap_dance_behavior(struct active_tap_dance *tap_dance, struct zmk_behavior_binding_event event = { .position = tap_dance->position, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = tap_dance->source, +#endif }; clear_tap_dance(tap_dance); return zmk_behavior_invoke_binding(&binding, event, false); @@ -135,7 +143,7 @@ static int on_tap_dance_binding_pressed(struct zmk_behavior_binding *binding, struct active_tap_dance *tap_dance; tap_dance = find_tap_dance(event.position); if (tap_dance == NULL) { - if (new_tap_dance(event.position, event.source, cfg, &tap_dance) == -ENOMEM) { + if (new_tap_dance(&event, cfg, &tap_dance) == -ENOMEM) { LOG_ERR("Unable to create new tap dance. Insufficient space in active_tap_dances[]."); return ZMK_BEHAVIOR_OPAQUE; } diff --git a/app/src/combo.c b/app/src/combo.c index a990e2f2a60..c3334bdb754 100644 --- a/app/src/combo.c +++ b/app/src/combo.c @@ -291,8 +291,10 @@ static int release_pressed_keys() { static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestamp) { struct zmk_behavior_binding_event event = { .position = combo->virtual_key_position, - .source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + .source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, +#endif }; last_combo_timestamp = timestamp; @@ -303,8 +305,10 @@ static inline int press_combo_behavior(struct combo_cfg *combo, int32_t timestam static inline int release_combo_behavior(struct combo_cfg *combo, int32_t timestamp) { struct zmk_behavior_binding_event event = { .position = combo->virtual_key_position, - .source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) + .source = ZMK_POSITION_STATE_CHANGE_SOURCE_LOCAL, +#endif }; return zmk_behavior_invoke_binding(&combo->behavior, event, false); diff --git a/app/src/keymap.c b/app/src/keymap.c index d4a4ab1f7de..af94bbfd3f7 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -586,7 +586,9 @@ int zmk_keymap_apply_position_state(uint8_t source, zmk_keymap_layer_id_t layer_ .layer = layer_id, .position = position, .timestamp = timestamp, +#if IS_ENABLED(CONFIG_ZMK_SPLIT) .source = source, +#endif }; LOG_DBG("layer_id: %d position: %d, binding name: %s", layer_id, position, From 8166527ea35923d8be6278b9d1727285dd504d3c Mon Sep 17 00:00:00 2001 From: Cem Aksoylar Date: Fri, 9 Aug 2024 11:31:09 -0700 Subject: [PATCH 18/20] fix(docs): Remove split locality issue note --- docs/docs/features/split-keyboards.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/docs/docs/features/split-keyboards.md b/docs/docs/features/split-keyboards.md index 8c69d51a8db..ff9397f7dd4 100644 --- a/docs/docs/features/split-keyboards.md +++ b/docs/docs/features/split-keyboards.md @@ -86,11 +86,6 @@ These behaviors only affect the keyboard part that they are invoked from: - [Reset behaviors](../keymaps/behaviors/reset.md) -:::warning[Nesting behaviors with locality] -Currently there is [an issue](https://github.com/zmkfirmware/zmk/issues/1494) preventing both global and source locality behaviors from working as expected if they are invoked from another behavior, such as a hold-tap, tap dance or a mod-morph. -For this reason it is recommended that these behaviors are placed directly on a keymap layer. -::: - :::note[Peripheral invocation] Peripherals must be paired and connected to the central in order to be able to activate these behaviors, even if it is possible to trigger the behavior using only keys on a particular peripheral. This is because the key bindings are processed on the central side which would then instruct the peripheral side to run the behavior's effect. From f992352936f6556da8b343a34e75ac61514d0ecf Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Mon, 23 Sep 2024 10:28:56 -0600 Subject: [PATCH 19/20] chore: Formatting fix. --- app/src/behavior_queue.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/src/behavior_queue.c b/app/src/behavior_queue.c index 86837f42332..82c58ca8548 100644 --- a/app/src/behavior_queue.c +++ b/app/src/behavior_queue.c @@ -35,11 +35,10 @@ static void behavior_queue_process_next(struct k_work *work) { LOG_DBG("Invoking %s: 0x%02x 0x%02x", item.binding.behavior_dev, item.binding.param1, item.binding.param2); - struct zmk_behavior_binding_event event = { - .position = item.position, - .timestamp = k_uptime_get(), + struct zmk_behavior_binding_event event = {.position = item.position, + .timestamp = k_uptime_get(), #if IS_ENABLED(CONFIG_ZMK_SPLIT) - .source = item.source + .source = item.source #endif }; From 33e3b02ddb0509a38b39dc565f21fdc17553ff0c Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Wed, 31 Jul 2024 11:54:15 -0600 Subject: [PATCH 20/20] feat: Split physical layout selection sync. * Ensure the split peripherals have the same selected physical layout on connection and change. --- app/include/zmk/physical_layouts.h | 7 +++ app/include/zmk/split/bluetooth/uuid.h | 1 + app/src/physical_layouts.c | 11 +++- app/src/split/bluetooth/central.c | 82 +++++++++++++++++++++++++- app/src/split/bluetooth/service.c | 43 +++++++++++++- 5 files changed, 141 insertions(+), 3 deletions(-) diff --git a/app/include/zmk/physical_layouts.h b/app/include/zmk/physical_layouts.h index e78602e3811..33004af738f 100644 --- a/app/include/zmk/physical_layouts.h +++ b/app/include/zmk/physical_layouts.h @@ -8,6 +8,13 @@ #include #include +#include + +struct zmk_physical_layout_selection_changed { + uint8_t selection; +}; + +ZMK_EVENT_DECLARE(zmk_physical_layout_selection_changed); struct zmk_key_physical_attrs { int16_t width; diff --git a/app/include/zmk/split/bluetooth/uuid.h b/app/include/zmk/split/bluetooth/uuid.h index dccdfc804c5..4a653c73b83 100644 --- a/app/include/zmk/split/bluetooth/uuid.h +++ b/app/include/zmk/split/bluetooth/uuid.h @@ -18,3 +18,4 @@ #define ZMK_SPLIT_BT_CHAR_RUN_BEHAVIOR_UUID ZMK_BT_SPLIT_UUID(0x00000002) #define ZMK_SPLIT_BT_CHAR_SENSOR_STATE_UUID ZMK_BT_SPLIT_UUID(0x00000003) #define ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID ZMK_BT_SPLIT_UUID(0x00000004) +#define ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID ZMK_BT_SPLIT_UUID(0x00000005) diff --git a/app/src/physical_layouts.c b/app/src/physical_layouts.c index c71b427a4dd..59077ede27e 100644 --- a/app/src/physical_layouts.c +++ b/app/src/physical_layouts.c @@ -22,6 +22,8 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include +ZMK_EVENT_IMPL(zmk_physical_layout_selection_changed); + #define DT_DRV_COMPAT zmk_physical_layout #define USE_PHY_LAYOUTS \ @@ -247,7 +249,14 @@ int zmk_physical_layouts_select(uint8_t index) { return -EINVAL; } - return zmk_physical_layouts_select_layout(layouts[index]); + int ret = zmk_physical_layouts_select_layout(layouts[index]); + + if (ret >= 0) { + raise_zmk_physical_layout_selection_changed( + (struct zmk_physical_layout_selection_changed){.selection = index}); + } + + return ret; } int zmk_physical_layouts_get_selected(void) { diff --git a/app/src/split/bluetooth/central.c b/app/src/split/bluetooth/central.c index 9c459bf1b1d..21ff611f568 100644 --- a/app/src/split/bluetooth/central.c +++ b/app/src/split/bluetooth/central.c @@ -30,6 +30,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include #include +#include static int start_scanning(void); @@ -56,6 +57,7 @@ struct peripheral_slot { #if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) uint16_t update_hid_indicators; #endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) + uint16_t selected_physical_layout_handle; uint8_t position_state[POSITION_STATE_DATA_LEN]; uint8_t changed_positions[POSITION_STATE_DATA_LEN]; }; @@ -141,6 +143,7 @@ int release_peripheral_slot(int index) { // Clean up previously discovered handles; slot->subscribe_params.value_handle = 0; slot->run_behavior_handle = 0; + slot->selected_physical_layout_handle = 0; #if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) slot->update_hid_indicators = 0; #endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) @@ -392,6 +395,46 @@ static int split_central_subscribe(struct bt_conn *conn, struct bt_gatt_subscrib return err; } +static int update_peripheral_selected_layout(struct peripheral_slot *slot, uint8_t layout_idx) { + if (slot->state != PERIPHERAL_SLOT_STATE_CONNECTED) { + return -ENOTCONN; + } + + if (slot->selected_physical_layout_handle == 0) { + // It appears that sometimes the peripheral is considered connected + // before the GATT characteristics have been discovered. If this is + // the case, the selected_physical_layout_handle will not yet be set. + return -EAGAIN; + } + + if (bt_conn_get_security(slot->conn) < BT_SECURITY_L2) { + return -EAGAIN; + } + + int err = bt_gatt_write_without_response(slot->conn, slot->selected_physical_layout_handle, + &layout_idx, sizeof(layout_idx), true); + + if (err < 0) { + LOG_ERR("Failed to write physical layout index to peripheral (err %d)", err); + } + + return err; +} + +static void update_peripherals_selected_physical_layout(struct k_work *_work) { + uint8_t layout_idx = zmk_physical_layouts_get_selected(); + for (int i = 0; i < ZMK_SPLIT_BLE_PERIPHERAL_COUNT; i++) { + if (peripherals[i].state != PERIPHERAL_SLOT_STATE_CONNECTED) { + continue; + } + + update_peripheral_selected_layout(&peripherals[i], layout_idx); + } +} + +K_WORK_DEFINE(update_peripherals_selected_layouts_work, + update_peripherals_selected_physical_layout); + static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn, const struct bt_gatt_attr *attr, struct bt_gatt_discover_params *params) { @@ -442,6 +485,11 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn, slot->discover_params.uuid = NULL; slot->discover_params.start_handle = attr->handle + 2; slot->run_behavior_handle = bt_gatt_attr_value_handle(attr); + } else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, + BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID))) { + LOG_DBG("Found select physical layout handle"); + slot->selected_physical_layout_handle = bt_gatt_attr_value_handle(attr); + k_work_submit(&update_peripherals_selected_layouts_work); #if IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) } else if (!bt_uuid_cmp(((struct bt_gatt_chrc *)attr->user_data)->uuid, BT_UUID_DECLARE_128(ZMK_SPLIT_BT_UPDATE_HID_INDICATORS_UUID))) { @@ -467,7 +515,8 @@ static uint8_t split_central_chrc_discovery_func(struct bt_conn *conn, #endif /* IS_ENABLED(CONFIG_ZMK_SPLIT_BLE_CENTRAL_BATTERY_LEVEL_FETCHING) */ } - bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle; + bool subscribed = slot->run_behavior_handle && slot->subscribe_params.value_handle && + slot->selected_physical_layout_handle; #if ZMK_KEYMAP_HAS_SENSORS subscribed = subscribed && slot->sensor_subscribe_params.value_handle; @@ -739,9 +788,30 @@ static void split_central_disconnected(struct bt_conn *conn, uint8_t reason) { start_scanning(); } +static void split_central_security_changed(struct bt_conn *conn, bt_security_t level, + enum bt_security_err err) { + struct peripheral_slot *slot = peripheral_slot_for_conn(conn); + if (!slot || !slot->selected_physical_layout_handle) { + return; + } + + if (err > 0) { + LOG_DBG("Skipping updating the physical layout for peripheral with security error"); + return; + } + + if (level < BT_SECURITY_L2) { + LOG_DBG("Skipping updating the physical layout for peripheral with insufficient security"); + return; + } + + k_work_submit(&update_peripherals_selected_layouts_work); +} + static struct bt_conn_cb conn_callbacks = { .connected = split_central_connected, .disconnected = split_central_disconnected, + .security_changed = split_central_security_changed, }; K_THREAD_STACK_DEFINE(split_central_split_run_q_stack, @@ -898,3 +968,13 @@ static int zmk_split_bt_central_init(void) { } SYS_INIT(zmk_split_bt_central_init, APPLICATION, CONFIG_ZMK_BLE_INIT_PRIORITY); + +static int zmk_split_bt_central_listener_cb(const zmk_event_t *eh) { + if (as_zmk_physical_layout_selection_changed(eh)) { + k_work_submit(&update_peripherals_selected_layouts_work); + } + return ZMK_EV_EVENT_BUBBLE; +} + +ZMK_LISTENER(zmk_split_bt_central, zmk_split_bt_central_listener_cb); +ZMK_SUBSCRIPTION(zmk_split_bt_central, zmk_physical_layout_selection_changed); \ No newline at end of file diff --git a/app/src/split/bluetooth/service.c b/app/src/split/bluetooth/service.c index 505eb363cd8..9529d51613c 100644 --- a/app/src/split/bluetooth/service.c +++ b/app/src/split/bluetooth/service.c @@ -19,6 +19,7 @@ LOG_MODULE_DECLARE(zmk, CONFIG_ZMK_LOG_LEVEL); #include #include #include +#include #include #include @@ -138,6 +139,42 @@ static ssize_t split_svc_update_indicators(struct bt_conn *conn, const struct bt #endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) +static uint8_t selected_phys_layout = 0; + +static void split_svc_select_phys_layout_callback(struct k_work *work) { + LOG_DBG("Selecting physical layout after GATT write of %d", selected_phys_layout); + zmk_physical_layouts_select(selected_phys_layout); +} + +static K_WORK_DEFINE(split_svc_select_phys_layout_work, split_svc_select_phys_layout_callback); + +static ssize_t split_svc_select_phys_layout(struct bt_conn *conn, const struct bt_gatt_attr *attr, + const void *buf, uint16_t len, uint16_t offset, + uint8_t flags) { + if (offset + len > sizeof(uint8_t) || len == 0) { + return BT_GATT_ERR(BT_ATT_ERR_INVALID_OFFSET); + } + + selected_phys_layout = *(uint8_t *)buf; + + k_work_submit(&split_svc_select_phys_layout_work); + + return len; +} + +static ssize_t split_svc_get_selected_phys_layout(struct bt_conn *conn, + const struct bt_gatt_attr *attrs, void *buf, + uint16_t len, uint16_t offset) { + int selected_ret = zmk_physical_layouts_get_selected(); + if (selected_ret < 0) { + return BT_GATT_ERR(BT_ATT_ERR_VALUE_NOT_ALLOWED); + } + + uint8_t selected = (uint8_t)selected_ret; + + return bt_gatt_attr_read(conn, attrs, buf, len, offset, &selected, sizeof(selected)); +} + BT_GATT_SERVICE_DEFINE( split_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SERVICE_UUID)), BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_CHAR_POSITION_STATE_UUID), @@ -160,7 +197,11 @@ BT_GATT_SERVICE_DEFINE( BT_GATT_CHRC_WRITE_WITHOUT_RESP, BT_GATT_PERM_WRITE_ENCRYPT, NULL, split_svc_update_indicators, NULL), #endif // IS_ENABLED(CONFIG_ZMK_SPLIT_PERIPHERAL_HID_INDICATORS) -); + BT_GATT_CHARACTERISTIC(BT_UUID_DECLARE_128(ZMK_SPLIT_BT_SELECT_PHYS_LAYOUT_UUID), + BT_GATT_CHRC_WRITE | BT_GATT_CHRC_READ, + BT_GATT_PERM_WRITE_ENCRYPT | BT_GATT_PERM_READ_ENCRYPT, + split_svc_get_selected_phys_layout, split_svc_select_phys_layout, + NULL), ); K_THREAD_STACK_DEFINE(service_q_stack, CONFIG_ZMK_SPLIT_BLE_PERIPHERAL_STACK_SIZE);