From 29b39f2b9f0977098601b201be1942d299b44da5 Mon Sep 17 00:00:00 2001 From: Peter Johanson Date: Thu, 3 Oct 2024 14:28:05 -0600 Subject: [PATCH] feat: A few enhancements to our RPC messages. * More fine grained status reporting when saving changes. Needed to we can notify the client if we've got errors saving settings due to running out of space, etc. * Return the max layer name lenght in the keymap payload, for the UI to leverage. --- app/src/keymap.c | 45 ++++++++++++++++++++++--------- app/src/studio/keymap_subsystem.c | 29 +++++++++++++++++--- app/west.yml | 2 +- 3 files changed, 59 insertions(+), 17 deletions(-) diff --git a/app/src/keymap.c b/app/src/keymap.c index ad3c7278a21..0ea64b340fd 100644 --- a/app/src/keymap.c +++ b/app/src/keymap.c @@ -432,7 +432,7 @@ int zmk_keymap_check_unsaved_changes(void) { #define LAYER_NAME_SETTINGS_KEY "keymap/l_n/%d" #define LAYER_BINDING_SETTINGS_KEY "keymap/l/%d/%d" -static void save_bindings(void) { +static int save_bindings(void) { for (int l = 0; l < ZMK_KEYMAP_LAYERS_LEN; l++) { uint8_t *pending = zmk_keymap_layer_pending_changes[l]; @@ -461,45 +461,64 @@ static void save_bindings(void) { char setting_name[20]; sprintf(setting_name, LAYER_BINDING_SETTINGS_KEY, l, kp); - settings_save_one(setting_name, &binding_setting, len); + int ret = settings_save_one(setting_name, &binding_setting, len); + if (ret < 0) { + LOG_ERR("Failed to save keymap binding at %d on layer %d (%d)", l, kp, ret); + return ret; + } } } *pending = 0; } + + return 0; } #if IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING) -static void save_layer_orders(void) { - settings_save_one(LAYER_ORDER_SETTINGS_KEY, keymap_layer_orders, - ARRAY_SIZE(keymap_layer_orders)); +static int save_layer_orders(void) { + int ret = settings_save_one(LAYER_ORDER_SETTINGS_KEY, keymap_layer_orders, + ARRAY_SIZE(keymap_layer_orders)); + if (ret < 0) { + return ret; + } + memcpy(settings_layer_orders, keymap_layer_orders, ARRAY_SIZE(keymap_layer_orders)); + return 0; } #endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING) -static void save_layer_names(void) { +static int save_layer_names(void) { for (int id = 0; id < ZMK_KEYMAP_LAYERS_LEN; id++) { if (changed_layer_names & BIT(id)) { char setting_name[14]; sprintf(setting_name, LAYER_NAME_SETTINGS_KEY, id); - settings_save_one(setting_name, zmk_keymap_layer_names[id], - strlen(zmk_keymap_layer_names[id])); + int ret = settings_save_one(setting_name, zmk_keymap_layer_names[id], + strlen(zmk_keymap_layer_names[id])); + if (ret < 0) { + return ret; + } } } changed_layer_names = 0; + return 0; } int zmk_keymap_save_changes(void) { - save_bindings(); + int ret = save_bindings(); + if (ret < 0) { + return ret; + } #if IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING) - save_layer_orders(); + ret = save_layer_orders(); + if (ret < 0) { + return ret; + } #endif // IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING) - save_layer_names(); - - return 0; + return save_layer_names(); } #if IS_ENABLED(CONFIG_ZMK_KEYMAP_LAYER_REORDERING) diff --git a/app/src/studio/keymap_subsystem.c b/app/src/studio/keymap_subsystem.c index 25297b63c8d..c3a78be01ea 100644 --- a/app/src/studio/keymap_subsystem.c +++ b/app/src/studio/keymap_subsystem.c @@ -103,6 +103,7 @@ zmk_studio_Response get_keymap(const zmk_studio_Request *req) { resp.layers.funcs.encode = encode_keymap_layers; + resp.max_layer_name_length = CONFIG_ZMK_KEYMAP_LAYER_NAME_MAX_LEN; resp.available_layers = 0; for (zmk_keymap_layer_index_t index = 0; index < ZMK_KEYMAP_LAYERS_LEN; index++) { @@ -174,25 +175,47 @@ zmk_studio_Response check_unsaved_changes(const zmk_studio_Request *req) { return KEYMAP_RESPONSE(check_unsaved_changes, layout_changes > 0 || keymap_changes > 0); } +static void map_errno_to_save_resp(int err, zmk_keymap_SaveChangesResponse *resp) { + resp->which_result = zmk_keymap_SaveChangesResponse_err_tag; + + switch (err) { + case -ENOTSUP: + resp->result.err = zmk_keymap_SaveChangesErrorCode_SAVE_CHANGES_ERR_NOT_SUPPORTED; + break; + case -ENOSPC: + resp->result.err = zmk_keymap_SaveChangesErrorCode_SAVE_CHANGES_ERR_NO_SPACE; + break; + default: + resp->result.err = zmk_keymap_SaveChangesErrorCode_SAVE_CHANGES_ERR_GENERIC; + break; + } +} + zmk_studio_Response save_changes(const zmk_studio_Request *req) { + zmk_keymap_SaveChangesResponse resp = zmk_keymap_SaveChangesResponse_init_zero; + resp.which_result = zmk_keymap_SaveChangesResponse_ok_tag; + resp.result.ok = true; + LOG_DBG(""); int ret = zmk_physical_layouts_save_selected(); if (ret < 0) { LOG_WRN("Failed to save selected physical layout (%d)", ret); - return ZMK_RPC_SIMPLE_ERR(GENERIC); + map_errno_to_save_resp(ret, &resp); + return KEYMAP_RESPONSE(save_changes, resp); } ret = zmk_keymap_save_changes(); if (ret < 0) { LOG_WRN("Failed to save keymap changes (%d)", ret); - return ZMK_RPC_SIMPLE_ERR(GENERIC); + map_errno_to_save_resp(ret, &resp); + return KEYMAP_RESPONSE(save_changes, resp); } raise_zmk_studio_rpc_notification((struct zmk_studio_rpc_notification){ .notification = KEYMAP_NOTIFICATION(unsaved_changes_status_changed, false)}); - return KEYMAP_RESPONSE(save_changes, true); + return KEYMAP_RESPONSE(save_changes, resp); } zmk_studio_Response discard_changes(const zmk_studio_Request *req) { diff --git a/app/west.yml b/app/west.yml index 5737bd004b1..e6c9758c685 100644 --- a/app/west.yml +++ b/app/west.yml @@ -34,7 +34,7 @@ manifest: path: modules/lib/nanopb remote: zephyrproject-rtos - name: zmk-studio-messages - revision: a79267a9661241a6603b6da3d2b3f71e8023a9d9 + revision: 6cb4c283e76209d59c45fbcb218800cd19e9339d path: modules/msgs/zmk-studio-messages remote: zmkfirmware self: