Deluge Firmware 1.3.0
Build date: 2026.11.28
Loading...
Searching...
No Matches
browser.h
1/*
2 * Copyright © 2019-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "definitions_cxx.hpp"
21#include "gui/ui/qwerty_ui.h"
22#include "hid/button.h"
23#include "io/debug/log.h"
24#include "model/favourite/favourite_manager.h"
25#include "storage/file_item.h"
26#include "util/container/array/c_string_array.h"
27
28extern "C" {
29#include "fatfs/ff.h"
30
31FRESULT f_readdir_get_filepointer(DIR* dp, /* Pointer to the open directory object */
32 FILINFO* fno, /* Pointer to file information to return */
33 FilePointer* filePointer);
34}
35
36class Instrument;
37class Output;
38class FileItem;
40class Song;
41
42// FIXME: std::expected<std::pair<bool, FileItem*>, Error>
44 FileItem* fileItem;
45 bool loadedFromFile;
46 Error error;
47};
48
49struct Slot {
50 int16_t slot;
51 int8_t subSlot;
52};
53
54#define CATALOG_SEARCH_LEFT 0
55#define CATALOG_SEARCH_RIGHT 1
56#define CATALOG_SEARCH_BOTH 2
57// Navigation keeps the selected entry while filling the cache in the direction of travel.
58#define CATALOG_SEARCH_LEFT_INCLUSIVE 3
59#define CATALOG_SEARCH_RIGHT_INCLUSIVE 4
60
61#define FILE_ITEMS_MAX_NUM_ELEMENTS 20
62#define FILE_ITEMS_MAX_NUM_ELEMENTS_FOR_NAVIGATION 20 // It "should" be able to be way less than this.
63
64extern char const* allowedFileExtensionsXML[];
65
66class Browser : public QwertyUI {
67public:
68 Browser();
69
70 void close();
71 virtual Error getCurrentFilePath(String* path) = 0;
72 ActionResult buttonAction(deluge::hid::Button b, bool on, bool inCardRoutine) override;
73 ActionResult padAction(int32_t x, int32_t y, int32_t velocity) override;
74 ActionResult verticalEncoderAction(int32_t offset, bool inCardRoutine) override;
75 void currentFileDeleted();
76 Error goIntoFolder(char const* folderName);
77 Error createFolder();
78 Error createFoldersRecursiveIfNotExists(const char* path);
79 void selectEncoderAction(int8_t offset) override;
80 static FileItem* getCurrentFileItem();
82 static void forgetOutput(Output const* output);
83 Error readFileItemsForFolder(char const* filePrefixHere, bool allowFolders, char const** allowedFileExtensionsHere,
84 char const* filenameToStartAt, int32_t newMaxNumFileItems,
85 int32_t newCatalogSearchDirection = CATALOG_SEARCH_BOTH);
86 Error setFileByFullPath(OutputType outputType, char const* fullPath);
87 void sortFileItems();
88 FileItem* getNewFileItem();
89 static void emptyFileItems();
90 static void deleteSomeFileItems(int32_t startAt, int32_t stopAt);
91 static void deleteFolderAndDuplicateItems(Availability instrumentAvailabilityRequirement = Availability::ANY);
92 Error getUnusedSlot(OutputType outputType, String* newName, char const* thingName);
107 static Error surveyNameVariations(char const* stem, String* highestNumbered, uint32_t* takenLetters);
108
113 static bool fileItemsAreComplete() { return !numFileItemsDeletedAtStart && !numFileItemsDeletedAtEnd; }
114 bool opened() override;
115 void cullSomeFileItems();
116 bool checkFP();
117
118 void renderOLED(deluge::hid::display::oled_canvas::Canvas& canvas) override;
119
120 static String currentDir;
121 static CStringArray fileItems;
122 static int32_t numFileItemsDeletedAtStart;
123 static int32_t numFileItemsDeletedAtEnd;
124 // These hold the displayName of the boundary FileItem kept when we cull items off either end of the list to stay
125 // under the memory cap. They must own a *copy* of the name (not borrow the FileItem's buffer): the FileItem they
126 // came from can be destructed - freeing its filename buffer - before these get used as search keys in a later
127 // sortFileItems(). (See the kit-copy use-after-free fix.)
128 static String firstFileItemRemaining;
129 static String lastFileItemRemaining;
130
131 static OutputType outputTypeToLoad;
132 static char const* filenameToStartSearchAt;
133
134 // ui
135 ActionResult exitUI() override {
136 exitAction();
137 return ActionResult::ACTIONED_AND_CAUSED_CHANGE;
138 }
139 bool isFavouritesVisible() override;
140 bool isBanksVisible() override;
141
142protected:
143 Error setEnteredTextFromCurrentFilename();
144 Error goUpOneDirectoryLevel();
145 virtual Error arrivedInNewFolder(int32_t direction, char const* filenameToStartAt = nullptr,
146 char const* defaultDir = nullptr);
147 bool predictExtendedText() override;
148 void goIntoDeleteFileContextMenu();
149 ActionResult mainButtonAction(bool on);
150 virtual void exitAction();
151 virtual ActionResult backButtonAction();
152 virtual void folderContentsReady(int32_t entryDirection) {}
153 virtual void currentFileChanged(int32_t movementDirection) {}
154 void displayText(bool blinkImmediately = false) override;
155 // Keeps the selected file index and top visible browser row inside Browser::fileItems.
156 void clampFileSelectionAndScroll(bool allowNoFileSelection = true);
157 static Slot getSlot(char const* displayName);
160 char const* nameAfterPrefix(char const* name) const;
161 Error readFileItemsFromFolderAndMemory(Song* song, OutputType outputType, char const* filePrefixHere,
162 char const* filenameToStartAt, char const* defaultDirToAlsoTry,
163 bool allowFoldersint,
164 Availability availabilityRequirement = Availability::ANY,
165 int32_t newCatalogSearchDirection = CATALOG_SEARCH_RIGHT);
166 void favouritesChanged();
167
168 static int32_t fileIndexSelected; // If -1, we have not selected any real file/folder. Maybe there are no files, or
169 // maybe we're typing a new name.
170 static int32_t scrollPosVertical;
171 static int32_t
172 numCharsInPrefix; // Only used for deciding Drum names within Kit. Oh and initial text scroll position.
173 static bool qwertyVisible;
174 static bool arrivedAtFileByTyping;
175 static bool allowFoldersSharingNameWithFile;
176 static char const** allowedFileExtensions;
177
178 const uint8_t* fileIcon;
179 const uint8_t* fileIconPt2;
180 int32_t fileIconPt2Width;
181
182 // 7Seg Only
183 static int8_t numberEditPos; // -1 is default
184 static NumericLayerScrollingText* scrollingText;
185 bool shouldWrapFolderContents; // As in, wrap around at the end.
186
187 bool mayDefaultToBrandNewNameOnEntry;
188 bool qwertyAlwaysVisible;
189 // filePrefix is SONG/SYNT/SAMP etc., signifying the portion of the filesystem you're in
190 char const* filePrefix;
191 bool shouldInterpretNoteNamesForThisBrowser;
192};
193
194inline void printInstrumentFileList(const char* where) {
195 D_PRINT("\n");
196 D_PRINT(where);
197 D_PRINT(" List: \n");
198 for (uint32_t idx = 0; idx < Browser::fileItems.getNumElements(); ++idx) {
199 FileItem* fileItem = (FileItem*)Browser::fileItems.getElementAddress(idx);
200 D_PRINTLN(" - %s (%lu)", fileItem->displayName, fileItem->filePointer.sclust);
201 }
202 D_PRINT("\n");
203}
ActionResult exitUI() override
returns whether a UI exited
Definition browser.h:135
static void forgetOutput(Output const *output)
Clears FileItems' pointers to an Output that is about to be freed.
Definition browser.cpp:1573
char const * nameAfterPrefix(char const *name) const
Definition browser.cpp:1454
static Error surveyNameVariations(char const *stem, String *highestNumbered, uint32_t *takenLetters)
Surveys currentDir in one pass for the existing variations of a name.
Definition browser.cpp:864
static bool fileItemsAreComplete()
Whether fileItems holds every file the folder listed, rather than a culled window of them.
Definition browser.h:113
Definition c_string_array.h:23
Definition file_item.h:24
Definition instrument.h:45
Definition numeric_layer_scrolling_text.h:22
Definition output.h:81
Definition song.h:112
Definition d_string.h:41
Definition browser.h:43
Definition browser.h:49