Skip to content

Commit

Permalink
feat: A few enhancements to our RPC messages.
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
petejohanson authored and rafaelromao committed Oct 8, 2024
1 parent 50b7835 commit 03a138f
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 17 deletions.
45 changes: 32 additions & 13 deletions app/src/keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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)
Expand Down
29 changes: 26 additions & 3 deletions app/src/studio/keymap_subsystem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion app/west.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 03a138f

Please sign in to comment.