Skip to content

Commit

Permalink
feat(core): make persist_options always point to a valid array
Browse files Browse the repository at this point in the history
  • Loading branch information
mcdurdin committed Oct 24, 2023
1 parent 8223792 commit 8475b68
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion core/include/keyman/keyman_core_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ typedef struct {
// issue a beep, 0 = no, 1 = yes
km_core_bool do_alert;

// emit the input keystroke to the application, unmodified? 0 = no, 1 = yes
// emit the (unmodified) input keystroke to the application, 0 = no, 1 = yes
km_core_bool emit_keystroke;

// -1=unchanged, 0=off, 1=on
Expand Down
26 changes: 15 additions & 11 deletions core/src/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@ km_core_actions * km::core::action_item_list_to_actions_object(

std::unique_ptr<km_core_actions> actions(new km_core_actions);

// Set actions defaults
// Set actions default values
std::vector<km_core_context_item> output;
std::vector<km_core_option_item> options;
actions->code_points_to_delete = 0;
actions->output = nullptr;
actions->persist_options = nullptr;
actions->do_alert = KM_CORE_FALSE;
actions->emit_keystroke = KM_CORE_FALSE;
actions->new_caps_lock_state = KM_CORE_CAPS_UNCHANGED;

// Clear output pointers, will be set later once we have sizes
actions->output = nullptr;
actions->persist_options = nullptr;

for (; action_items->type != KM_CORE_IT_END; ++action_items) {
assert(action_items->type < KM_CORE_IT_MAX_TYPE_ID);

Expand Down Expand Up @@ -105,9 +107,10 @@ km_core_actions * km::core::action_item_list_to_actions_object(
}
}

output.push_back(KM_CORE_CONTEXT_ITEM_END);

// Strip the markers from the output to be passed to the app
// Strip the markers from the output, and convert to an string of UTF-32

output.push_back(KM_CORE_CONTEXT_ITEM_END);

size_t buf_size;

Expand All @@ -121,14 +124,15 @@ km_core_actions * km::core::action_item_list_to_actions_object(
return nullptr;
}

actions->output = output_usv.release();

// Create an array of the persisted options

if(!options.empty()) {
options.push_back(KM_CORE_OPTIONS_END);
actions->persist_options = new km_core_option_item[options.size()];
std::copy(options.begin(), options.end(), actions->persist_options);
}
options.push_back(KM_CORE_OPTIONS_END);
actions->persist_options = new km_core_option_item[options.size()];
std::copy(options.begin(), options.end(), actions->persist_options);

// We now have a complete set of actions

actions->output = output_usv.release();
return actions.release();
}
26 changes: 21 additions & 5 deletions core/tests/unit/kmnkbd/action_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ void test_two_backspaces() {

assert(actions->code_points_to_delete == 1);
assert(std::u32string(actions->output) == U"");
assert(actions->persist_options == nullptr);
assert(actions->persist_options != nullptr);
assert(actions->persist_options[0].key == nullptr);
assert(actions->persist_options[0].value == nullptr);
assert(actions->persist_options[0].scope == KM_CORE_OPT_UNKNOWN);

assert(actions->do_alert == false);
assert(actions->emit_keystroke == false);
assert(actions->new_caps_lock_state == -1);
Expand All @@ -65,7 +69,10 @@ void test_marker_text_interleaved() {

assert(actions->code_points_to_delete == 0);
assert(std::u32string(actions->output) == U"ABD");
assert(actions->persist_options == nullptr);
assert(actions->persist_options != nullptr);
assert(actions->persist_options[0].key == nullptr);
assert(actions->persist_options[0].value == nullptr);
assert(actions->persist_options[0].scope == KM_CORE_OPT_UNKNOWN);
assert(actions->do_alert == false);
assert(actions->emit_keystroke == false);
assert(actions->new_caps_lock_state == -1);
Expand All @@ -85,7 +92,10 @@ void test_alert() {

assert(actions->code_points_to_delete == 0);
assert(std::u32string(actions->output) == U"");
assert(actions->persist_options == nullptr);
assert(actions->persist_options != nullptr);
assert(actions->persist_options[0].key == nullptr);
assert(actions->persist_options[0].value == nullptr);
assert(actions->persist_options[0].scope == KM_CORE_OPT_UNKNOWN);
assert(actions->do_alert == KM_CORE_TRUE);
assert(actions->emit_keystroke == KM_CORE_FALSE);
assert(actions->new_caps_lock_state == KM_CORE_CAPS_UNCHANGED);
Expand All @@ -105,7 +115,10 @@ void test_emit_keystroke() {

assert(actions->code_points_to_delete == 0);
assert(std::u32string(actions->output) == U"");
assert(actions->persist_options == nullptr);
assert(actions->persist_options != nullptr);
assert(actions->persist_options[0].key == nullptr);
assert(actions->persist_options[0].value == nullptr);
assert(actions->persist_options[0].scope == KM_CORE_OPT_UNKNOWN);
assert(actions->do_alert == KM_CORE_FALSE);
assert(actions->emit_keystroke == KM_CORE_TRUE);
assert(actions->new_caps_lock_state == KM_CORE_CAPS_UNCHANGED);
Expand All @@ -126,7 +139,10 @@ void test_invalidate_context() {

assert(actions->code_points_to_delete == 0);
assert(std::u32string(actions->output) == U"");
assert(actions->persist_options == nullptr);
assert(actions->persist_options != nullptr);
assert(actions->persist_options[0].key == nullptr);
assert(actions->persist_options[0].value == nullptr);
assert(actions->persist_options[0].scope == KM_CORE_OPT_UNKNOWN);
assert(actions->do_alert == KM_CORE_FALSE);
assert(actions->emit_keystroke == KM_CORE_FALSE);
assert(actions->new_caps_lock_state == KM_CORE_CAPS_UNCHANGED);
Expand Down

0 comments on commit 8475b68

Please sign in to comment.