forked from ITotalJustice/sys-tune
-
Notifications
You must be signed in to change notification settings - Fork 1
/
gui_browser.cpp
217 lines (187 loc) · 7.16 KB
/
gui_browser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include "gui_browser.hpp"
#include "tune.h"
namespace {
bool ListItemTextCompare(const tsl::elm::ListItem *_lhs, const tsl::elm::ListItem *_rhs) {
return strcasecmp(_lhs->getText().c_str(), _rhs->getText().c_str()) < 0;
};
bool StringTextCompare(std::string _lhs, std::string _rhs) {
return strcasecmp(_lhs.c_str(), _rhs.c_str()) < 0;
};
ALWAYS_INLINE bool EndsWith(const char *name, const char *ext) {
return strcasecmp(name + std::strlen(name) - std::strlen(ext), ext) == 0;
}
constexpr const std::array SupportedTypes = {
#ifdef WANT_MP3
".mp3",
#endif
#ifdef WANT_FLAC
".flac",
#endif
#ifdef WANT_WAV
".wav",
".wave",
#endif
};
bool SupportsType(const char *name) {
for (auto &ext : SupportedTypes)
if (EndsWith(name, ext))
return true;
return false;
}
constexpr const char *const base_path = "/music/";
char path_buffer[FS_MAX_PATH];
}
BrowserGui::BrowserGui()
: m_fs(), has_music(), cwd("/") {
this->m_list = new tsl::elm::List();
/* Open sd card filesystem. */
Result rc = fsOpenSdCardFileSystem(&this->m_fs);
if (R_SUCCEEDED(rc)) {
/* Check if base path /music/ exists. */
FsDir dir;
std::strcpy(this->cwd, base_path);
if (R_SUCCEEDED(fsFsOpenDirectory(&this->m_fs, this->cwd, FsDirOpenMode_ReadFiles, &dir))) {
this->has_music = true;
fsDirClose(&dir);
} else {
this->cwd[1] = '\0';
}
this->scanCwd();
} else {
this->m_list->addItem(new tsl::elm::CategoryHeader("Couldn't open SdCard"));
}
}
BrowserGui::~BrowserGui() {
fsFsClose(&this->m_fs);
}
tsl::elm::Element *BrowserGui::createUI() {
m_frame = new SysTuneOverlayFrame();
m_frame->setDescription("\uE0E1 Back \uE0E0 Add \uE0E2 Add All");
m_frame->setContent(this->m_list);
return m_frame;
}
bool BrowserGui::handleInput(u64 keysDown, u64, const HidTouchState&, HidAnalogStickState, HidAnalogStickState) {
if (keysDown & HidNpadButton_B) {
if (this->has_music && this->cwd[7] == '\0') {
return false;
} else if (this->cwd[1] != '\0') {
this->upCwd();
return true;
}
} else if (keysDown & HidNpadButton_X) {
this->addAllToPlaylist();
return true;
}
return false;
}
void BrowserGui::scanCwd() {
tsl::Gui::removeFocus();
this->m_list->clear();
/* Show absolute folder path. */
this->m_list->addItem(new tsl::elm::CategoryHeader(this->cwd, true));
/* Open directory. */
FsDir dir;
Result rc = fsFsOpenDirectory(&this->m_fs, this->cwd, FsDirOpenMode_ReadDirs | FsDirOpenMode_ReadFiles, &dir);
if (R_FAILED(rc)) {
char result_buffer[0x10];
std::snprintf(result_buffer, 0x10, "2%03X-%04X", R_MODULE(rc), R_DESCRIPTION(rc));
this->m_list->addItem(new tsl::elm::ListItem("something went wrong :/"));
this->m_list->addItem(new tsl::elm::ListItem(result_buffer));
return;
}
tsl::hlp::ScopeGuard dirGuard([&] { fsDirClose(&dir); });
std::vector<tsl::elm::ListItem *> folders, files;
/* Iternate over directory. */
s64 count = 0;
FsDirectoryEntry entry;
while (R_SUCCEEDED(fsDirRead(&dir, &count, 1, &entry)) && count) {
if (entry.type == FsDirEntryType_Dir) {
/* Add directory entries. */
auto item = new tsl::elm::ListItem(entry.name);
item->setClickListener([this, item](u64 down) -> bool {
if (down & HidNpadButton_A) {
std::strncat(this->cwd, item->getText().c_str(), sizeof(this->cwd) - 1);
std::strncat(this->cwd, "/", sizeof(this->cwd) - 1);
this->scanCwd();
return true;
}
return false;
});
folders.push_back(item);
} else if (SupportsType(entry.name)) {
/* Add file entry. */
auto item = new tsl::elm::ListItem(entry.name);
item->setClickListener([this, item](u64 down) -> bool {
if (down & HidNpadButton_A) {
std::snprintf(path_buffer, sizeof(path_buffer), "%s%s", this->cwd, item->getText().c_str());
Result rc = tuneEnqueue(path_buffer, TuneEnqueueType_Back);
if (R_SUCCEEDED(rc)) {
m_frame->setToast("Playlist updated", "Added 1 song to Playlist.");
} else {
m_frame->setToast("Failed to add Track.", "Does the name contain umlauts?");
}
return true;
}
return false;
});
files.push_back(item);
}
}
if (folders.size() == 0 && files.size() == 0) {
this->m_list->addItem(new tsl::elm::CategoryHeader("Empty..."));
return;
}
if (folders.size() > 0) {
std::sort(folders.begin(), folders.end(), ListItemTextCompare);
for (auto element : folders)
this->m_list->addItem(element);
}
if (files.size() > 0) {
this->m_list->addItem(new tsl::elm::CategoryHeader("Files"));
std::sort(files.begin(), files.end(), ListItemTextCompare);
for (auto element : files)
this->m_list->addItem(element);
}
}
void BrowserGui::upCwd() {
size_t length = std::strlen(this->cwd);
if (length <= 1)
return;
for (size_t i = length - 2; i >= 0; i--) {
if (this->cwd[i] == '/') {
this->cwd[i + 1] = '\0';
this->scanCwd();
return;
}
}
}
void BrowserGui::addAllToPlaylist() {
FsDir dir;
Result rc = fsFsOpenDirectory(&this->m_fs, this->cwd, FsDirOpenMode_ReadFiles, &dir);
if (R_FAILED(rc)) {
char result_buffer[0x10];
std::snprintf(result_buffer, 0x10, "2%03X-%04X", R_MODULE(rc), R_DESCRIPTION(rc));
this->m_list->addItem(new tsl::elm::ListItem("something went wrong :/"));
this->m_list->addItem(new tsl::elm::ListItem(result_buffer));
return;
}
tsl::hlp::ScopeGuard dirGuard([&] { fsDirClose(&dir); });
std::vector<std::string> file_list;
s64 songs_added = 0;
s64 count = 0;
FsDirectoryEntry entry;
while (R_SUCCEEDED(fsDirRead(&dir, &count, 1, &entry)) && count){
if (entry.type == FsDirEntryType_File && SupportsType(entry.name)){
file_list.push_back(std::string(entry.name));
count++;
}
}
std::sort(file_list.begin(), file_list.end(), StringTextCompare);
for (auto const & file : file_list) {
std::snprintf(path_buffer, sizeof(path_buffer), "%s%s", this->cwd, file.c_str());
rc = tuneEnqueue(path_buffer, TuneEnqueueType_Back);
if (R_SUCCEEDED(rc)) songs_added++;
}
std::snprintf(path_buffer, sizeof(path_buffer), "Added %ld songs to Playlist.", songs_added);
m_frame->setToast("Playlist updated", path_buffer);
}