Skip to content

Commit

Permalink
Merge branch 'fix-patch-open-crash'
Browse files Browse the repository at this point in the history
  • Loading branch information
danngreen committed Aug 22, 2024
2 parents 5b3ffe5 + eccc77a commit 10b6045
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion firmware/lib/CoreModules
5 changes: 4 additions & 1 deletion firmware/src/gui/pages/patch_selector.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ struct PatchSelectorPage : PageBase {
void prepare_focus() override {
abort_cable(gui_state, notify_queue);

// Don't persist module selection
args.module_id = std::nullopt;

state = State::TryingToRequestPatchList;
hide_spinner();
blur_subdir_panel();
Expand Down Expand Up @@ -243,7 +246,7 @@ struct PatchSelectorPage : PageBase {
lv_obj_clear_state(ui_PatchListRoller, LV_STATE_FOCUSED);
subdir_panel.focus();
} else {
page_list.request_last_page();
page_list.request_new_page_no_history(PageId::MainMenu, args);
}
}

Expand Down
4 changes: 3 additions & 1 deletion firmware/src/gui/pages/patch_view.hh
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ struct PatchViewPage : PageBase {

} else {
abort_cable(gui_state, notify_queue);
page_list.request_last_page();
page_list.request_new_page_no_history(PageId::PatchSel, args);
blur();
}
}
Expand All @@ -292,6 +292,8 @@ struct PatchViewPage : PageBase {

if (file_menu.did_filesystem_change()) {
gui_state.force_refresh_vol = patches.get_view_patch_vol();
displayed_patch_loc_hash = patches.get_view_patch_loc_hash();
args.patch_loc_hash = patches.get_view_patch_loc_hash();
}

if (is_patch_playing && !patch_playloader.is_audio_muted()) {
Expand Down
3 changes: 2 additions & 1 deletion firmware/src/gui/pages/save_dialog.hh
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,8 @@ private:

page->file_name = lv_textarea_get_text(ui_SaveDialogFilename);

std::string fullpath = page->file_path + "/" + page->file_name;
std::string fullpath = page->file_path.length() ? (page->file_path + "/") : "";
fullpath += page->file_name;
if (!fullpath.ends_with(".yml")) {
fullpath.append(".yml");
}
Expand Down
31 changes: 26 additions & 5 deletions firmware/src/patch_file/open_patch_manager.hh
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,28 @@ public:

// Parses and opens the loaded patch, and sets the view patch to point to it
bool open_patch(std::span<char> file_data, PatchLocation const &patch_loc) {
bool patch_is_playing = false;

if (open_patches_.find(patch_loc)) {
if (auto patch = open_patches_.find(patch_loc)) {
open_patches_.dump_open_patches();

if (patch == playing_patch_)
patch_is_playing = true;

pr_warn("open patch found already. p:%d\n", patch_is_playing);
open_patches_.remove(patch_loc);
open_patches_.dump_open_patches();
}

auto *new_patch = open_patches_.emplace_back(patch_loc);

if (!yaml_raw_to_patch(file_data, new_patch->patch)) {
pr_err("Failed to parse\n");
open_patches_.remove_last();

if (patch_is_playing)
playing_patch_ = nullptr;

return false;
}

Expand All @@ -104,16 +116,25 @@ public:

view_patch_ = new_patch;

if (patch_is_playing)
playing_patch_ = new_patch;

return true;
}

//
// playing_patch: (copy of) patch currently playing in the audio thread
//
PatchData *get_playing_patch() {
if (playing_patch_)
return &playing_patch_->patch;
else {
if (playing_patch_) {
if (open_patches_.exists(playing_patch_)) {
pr_dbg("Playing patch exists\n");
return &playing_patch_->patch;
} else {
pr_err("Playing patch not null and not found in open patches!\n");
return nullptr;
}
} else {
return nullptr;
}
}
Expand Down Expand Up @@ -193,7 +214,7 @@ public:

void new_patch() {
std::string name = "Untitled Patch " + std::to_string((uint8_t)std::rand());
std::string filename = "/" + name + ".yml";
std::string filename = name + ".yml";
PatchLocation loc{std::string_view{filename}, Volume::RamDisk};
view_patch_ = open_patches_.emplace_back(loc);
view_patch_->patch.blank_patch(name);
Expand Down
12 changes: 11 additions & 1 deletion firmware/src/patch_file/open_patches.hh
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ struct OpenPatchList {
return num_erased > 0;
}

bool exists(OpenPatch const *patch) {
for (auto const &p : list) {
if (&p == patch)
return true;
}
return false;

// return (std::find(list.begin(), list.end(), patch) != list.end());
}

void remove(std::list<OpenPatch>::iterator item) {
list.erase(item);
}
Expand All @@ -81,7 +91,6 @@ struct OpenPatchList {
return list.size();
}

private:
void dump_open_patches() {
unsigned i = 0;
size_t total_size = 0;
Expand All @@ -100,6 +109,7 @@ private:
pr_dbg("TOTAL: %zu\n", total_size);
}

private:
static size_t patch_size(PatchData const &p) {
auto sz = sizeof(PatchData);
sz += p.module_slugs.size() * sizeof(BrandModuleSlug);
Expand Down

0 comments on commit 10b6045

Please sign in to comment.