forked from timre13/sdl_file_chooser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSelector.cpp
More file actions
568 lines (515 loc) · 19.3 KB
/
Selector.cpp
File metadata and controls
568 lines (515 loc) · 19.3 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#include "Selector.hpp"
#include <cmath>
#include <dirent.h>
#include <iostream>
#include <stdint.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <functional>
#include <cstring>
#include <sstream>
#include <chrono>
// no std::filesystem for GCC 7.5
void Selector::renderCounter(SDL_Renderer* renderer, int currentOption, int totalOptions)
{
SDL_Color textColor = {255, 255, 255, 255};
if (!counterFont)
{
std::cerr << "Counter font not initialized" << std::endl;
return;
}
string counterText = std::to_string(currentOption) + "/" + std::to_string(totalOptions);
SDL_Surface* textSurface = TTF_RenderText_Solid(counterFont, counterText.c_str(), textColor);
if (!textSurface)
{
std::cerr << "Failed to create text surface: " << TTF_GetError() << std::endl;
return;
}
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
if (!textTexture)
{
std::cerr << "Failed to create texture: " << SDL_GetError() << std::endl;
SDL_FreeSurface(textSurface);
return;
}
int windowWidth, windowHeight;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
int textWidth = textSurface->w;
int textHeight = textSurface->h;
SDL_Rect dstRect = {windowWidth - textWidth - 10, windowHeight - textHeight - 10, textWidth,
textHeight};
SDL_RenderCopy(renderer, textTexture, nullptr, &dstRect);
SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
}
// Number of files displayed per page
Mix_Chunk* Selector::loadClickSound(const vec_string& paths)
{
// Initialize audio only once before attempting to load sounds
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
std::cerr << "SDL_mixer could not initialize! SDL_mixer Error: " << Mix_GetError() << '\n';
std::exit(2);
}
// Try loading the sound from the specified paths
for (const auto& path : paths)
{
Mix_Chunk* sound = Mix_LoadWAV(path.c_str());
if (sound != nullptr)
return sound; // Return the successfully loaded sound
// std::cerr << "Failed to load click sound from " << path << "! SDL_mixer
// Error: " << Mix_GetError() << '\n';
}
// If no sound file could be loaded, close the audio
Mix_CloseAudio(); // Close the audio system if no sound was loaded
return nullptr; // Return nullptr if no path succeeded
}
static bool isRegularFile(const std::string& path)
{
struct stat st;
if (stat(path.c_str(), &st) != 0) return false;
return S_ISREG(st.st_mode);
}
static bool isDirectory(const std::string& path)
{
struct stat st;
if (stat(path.c_str(), &st) != 0) return false;
return S_ISDIR(st.st_mode);
}
void Selector::getFileList(string directory, bool recursive)
{
auto matchFilters = [&](const std::string& filename) {
string lower = filename;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
if (filters.empty()) return true;
for (const auto& f : filters)
{
string lf = f;
std::transform(lf.begin(), lf.end(), lf.begin(), ::tolower);
if (lower.find(lf) != string::npos) return true;
}
return false;
};
std::function<void(const std::string&)> scan = [&](const std::string& dir) {
DIR* d = opendir(dir.c_str());
if (!d) return;
struct dirent* ent;
while ((ent = readdir(d)) != nullptr)
{
const char* name = ent->d_name;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) continue;
std::string full = dir;
if (!full.empty() && full.back() != '/') full += '/';
full += name;
if (isDirectory(full))
{
if (recursive) scan(full);
}
else if (isRegularFile(full))
{
if (matchFilters(name)) fileList.push_back(full);
}
}
closedir(d);
};
fileList.clear();
scan(directory);
std::sort(fileList.begin(), fileList.end());
}
void Selector::drawFileList()
{
// Match previous look: original code rendered font size ~100 then divided by 5
const float baseDiv = 5.0f;
int rawLine = listFont ? TTF_FontLineSkip(listFont) : 100;
if (rawLine <= 0) rawLine = 100;
int lineAdvance = static_cast<int>(rawLine / baseDiv);
if (lineAdvance < 12) lineAdvance = 12; // minimal spacing guard
int baseY = 500;
for (int i = 0; i < static_cast<int>(fileList.size()); ++i)
{
int y = baseY - chosenFileI * lineAdvance + i * lineAdvance;
if (y < 1000 && y > 0)
{
SDL_Surface* textSurface = TTF_RenderText_Blended(listFont ? listFont : font, fileList[i].c_str(),
{255, 255, 255, static_cast<uint8_t>(255 - std::abs(500 - y) / 2)});
SDL_Rect sourceRect{0, 0, textSurface->w, textSurface->h};
// Scale down to match previous visual size convention
SDL_Rect targetRect{0, y, static_cast<int>(textSurface->w / baseDiv),
static_cast<int>(textSurface->h / baseDiv)};
SDL_Texture* textTexture{SDL_CreateTextureFromSurface(renderer, textSurface)};
SDL_RenderCopy(renderer, textTexture, &sourceRect, &targetRect);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
// Counter will be rendered once per frame, outside the loop
}
}
}
void Selector::drawTitle(const string& title)
{
int lineHeight = 40; // Height between lines (adjustable according to font size)
int yOffset = 10; // Vertical position of first line
// Divide title with line breaks
std::istringstream stream(title);
string line;
while (std::getline(stream, line, '\n'))
{
SDL_Surface* textSurface = TTF_RenderText_Solid(font, line.c_str(), {255, 255, 255, 255});
SDL_Rect sourceRect{0, 0, textSurface->w, textSurface->h};
SDL_Rect targetRect{
10, yOffset, textSurface->w / 3, textSurface->h / 3}; // Adjust text size here
SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_RenderCopy(renderer, textTexture, &sourceRect, &targetRect);
SDL_DestroyTexture(textTexture);
SDL_FreeSurface(textSurface);
yOffset += lineHeight; // Adjust offset for next line
}
}
void Selector::drawSelector()
{
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
int windowWidth, windowHeight;
SDL_GetWindowSize(window, &windowWidth, &windowHeight);
const float baseDiv = 5.0f; // Keep consistent with drawFileList scaling
int rawLine = listFont ? TTF_FontLineSkip(listFont) : 100;
if (rawLine <= 0) rawLine = 100;
int lineAdvance = static_cast<int>(rawLine / baseDiv);
if (lineAdvance < 12) lineAdvance = 12;
int baseY = 500;
SDL_Rect selectorRect{0, baseY, windowWidth, lineAdvance};
SDL_RenderFillRect(renderer, &selectorRect);
}
void Selector::drawBackground()
{
if (backgroundTexture)
SDL_RenderCopy(renderer, backgroundTexture, NULL,
NULL); // Full-window image
}
// ╒═════════════════════════════════════════════════════════╕
// PUBLIC
// ╘═════════════════════════════════════════════════════════╛
Selector::Selector(string title, string backgroundImage, int listFontSize)
: title(title)
, backgroundTexture(nullptr)
, listFont(nullptr)
, counterFont(nullptr)
, listFontSize(listFontSize)
, chosenFileI(0)
{
#ifdef TRIMUI
// Disable STDOUT to silent trimui's sdl logs
int stdout_fd = dup(STDOUT_FILENO);
freopen("/dev/null", "w", stdout);
#endif
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_GAMECONTROLLER | SDL_INIT_AUDIO);
TTF_Init();
// Load the "click" sound
vec_string soundPaths = {"/mnt/SDCARD/System/usr/trimui/res/sound/click.wav", "click.wav",
"/usr/trimui/res/sound/click.wav"};
clickSound = loadClickSound(soundPaths);
if (clickSound == nullptr)
std::cerr << "No valid click sound file found.\n";
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800,
1000, SDL_WINDOW_ALLOW_HIGHDPI);
if (!window)
{
std::cerr << "Unable to create window" << '\n';
std::exit(2);
}
// Prefer VSYNC to cap refresh and reduce CPU usage (fallback to default if it fails)
renderer = SDL_CreateRenderer(
window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (!renderer)
{
std::cerr << "VSYNC renderer failed: " << SDL_GetError() << "\nFalling back...\n";
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer)
{
std::cerr << "Unable to create renderer" << '\n';
std::exit(2);
}
}
#ifdef TRIMUI
// Restore STDOUT
fflush(stdout);
dup2(stdout_fd, STDOUT_FILENO);
close(stdout_fd);
#endif
controller = NULL;
if (SDL_NumJoysticks() > 0)
{
controller = SDL_GameControllerOpen(0);
if (!controller)
std::cerr << "Failed to open controller: " << SDL_GetError() << '\n';
} else
{
std::cerr << "No joystick found\n";
}
// Load background image if supplied
if (!backgroundImage.empty())
{
SDL_Surface* bgSurface = IMG_Load(backgroundImage.c_str());
if (bgSurface)
{
backgroundTexture = SDL_CreateTextureFromSurface(renderer, bgSurface);
SDL_FreeSurface(bgSurface);
} else
{
std::cerr << "Failed to load background image: " << IMG_GetError() << '\n';
}
}
char buff[1024];
ssize_t len = readlink("/proc/self/exe", buff, sizeof(buff) - 1);
font_path = "";
if (len != -1)
{
buff[len] = '\0';
// Derive from executable directory without std::filesystem
std::string path(buff);
size_t pos = path.find_last_of('/');
if (pos != std::string::npos)
font_path = path.substr(0, pos + 1) + "Anonymous_Pro.ttf";
}
if (font_path.empty())
{
// Fallback to current working directory
font_path = "Anonymous_Pro.ttf";
}
font = TTF_OpenFont(font_path.c_str(), 100);
if (!font || font_path == "")
{
std::cerr << "Unable to open font file." << '\n';
std::exit(2);
}
// Use provided listFontSize or default to 100 (old base size) if invalid/non-positive
if (this->listFontSize <= 0) this->listFontSize = 100;
listFont = TTF_OpenFont(font_path.c_str(), this->listFontSize);
if (!listFont)
{
std::cerr << "Failed to load list font, falling back to title font size: "
<< TTF_GetError() << std::endl;
listFont = font; // fall back; do not close twice
}
// Create a small cached font for the counter
counterFont = TTF_OpenFont(font_path.c_str(), 24);
if (!counterFont)
std::cerr << "Failed to load counter font: " << TTF_GetError() << std::endl;
}
Selector::~Selector()
{
if (clickSound)
{
Mix_FreeChunk(clickSound);
clickSound = nullptr;
}
if (font && font != listFont)
TTF_CloseFont(font);
if (listFont && listFont != font)
TTF_CloseFont(listFont);
if (counterFont)
TTF_CloseFont(counterFont);
if (backgroundTexture)
SDL_DestroyTexture(backgroundTexture);
if (controller)
SDL_GameControllerClose(controller);
if (renderer)
SDL_DestroyRenderer(renderer);
if (window)
SDL_DestroyWindow(window);
Mix_CloseAudio();
Mix_Quit(); // Exit SDL_mixer
TTF_Quit();
SDL_Quit();
}
void Selector::setCustom(vec_string customChoices)
{
chosenFileI = 0;
fileList = customChoices; // Use the custom choices provided
isCustom = 1;
/* drawTitle("Loading...");
SDL_RenderPresent(renderer); */
}
void Selector::setFolder(string directory, bool recursive, vec_string filters)
{
this->filters = filters;
drawTitle("Loading...");
SDL_RenderPresent(renderer);
getFileList(directory, recursive); // Call with filters and recursive flag
isCustom = 0;
}
int Selector::run()
{
auto lastDpadPressTime = std::chrono::steady_clock::now();
const int scrollIntervalMs = 220; // Adjust scrolling speed here
bool dpadDownPressed = false;
bool dpadUpPressed = false;
bool needsRedraw = true; // Redraw only on changes
while (1)
{
SDL_Event event;
// Wait up to ~16ms for an event to reduce busy-waiting; then drain queue
bool shouldExit = false;
int exitCode = -1;
if (SDL_WaitEventTimeout(&event, 16))
{
// Handle the event we waited for
do
{
switch (event.type)
{
case SDL_QUIT:
shouldExit = true;
exitCode = -1;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE:
shouldExit = true;
exitCode = -1;
break;
case SDLK_DOWN:
case SDLK_s:
if (chosenFileI < static_cast<int>(fileList.size()) - 1)
{
++chosenFileI;
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
break;
case SDLK_UP:
case SDLK_w:
if (chosenFileI > 0)
{
--chosenFileI;
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
break;
case SDLK_RETURN:
shouldExit = true;
exitCode = -1;
break;
}
break;
case SDL_CONTROLLERBUTTONDOWN:
switch (event.cbutton.button)
{
case SDL_CONTROLLER_BUTTON_DPAD_DOWN:
dpadDownPressed = true;
lastDpadPressTime = std::chrono::steady_clock::now();
if (chosenFileI < static_cast<int>(fileList.size()) - 1)
{
++chosenFileI;
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
break;
case SDL_CONTROLLER_BUTTON_DPAD_UP:
dpadUpPressed = true;
lastDpadPressTime = std::chrono::steady_clock::now();
if (chosenFileI > 0)
{
--chosenFileI;
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
break;
case SDL_CONTROLLER_BUTTON_B:
shouldExit = true;
exitCode = 1;
break;
case SDL_CONTROLLER_BUTTON_A:
shouldExit = true;
exitCode = -1;
break;
case SDL_CONTROLLER_BUTTON_LEFTSHOULDER: // L1
if (chosenFileI > 0)
{
chosenFileI -= filesPerPage;
if (chosenFileI < 0)
chosenFileI = 0; // Prevent underflow
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
break;
case SDL_CONTROLLER_BUTTON_RIGHTSHOULDER: // R1
if (chosenFileI < static_cast<int>(fileList.size()) - 1)
{
chosenFileI += filesPerPage;
if (chosenFileI >= static_cast<int>(fileList.size()))
chosenFileI = static_cast<int>(fileList.size()) - 1; // Prevent overflow
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
break;
}
break;
case SDL_WINDOWEVENT:
// Redraw on expose, resize, etc.
needsRedraw = true;
break;
case SDL_CONTROLLERBUTTONUP:
if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_DOWN)
dpadDownPressed = false;
else if (event.cbutton.button == SDL_CONTROLLER_BUTTON_DPAD_UP)
dpadUpPressed = false;
break;
}
} while (SDL_PollEvent(&event));
}
auto currentTime = std::chrono::steady_clock::now();
auto timeSinceLastPress =
std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - lastDpadPressTime)
.count();
if (dpadDownPressed && timeSinceLastPress >= scrollIntervalMs)
{
lastDpadPressTime = currentTime;
if (chosenFileI < static_cast<int>(fileList.size()) - 1)
{
++chosenFileI;
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
} else if (dpadUpPressed && timeSinceLastPress >= scrollIntervalMs)
{
lastDpadPressTime = currentTime;
if (chosenFileI > 0)
{
--chosenFileI;
if (clickSound) Mix_PlayChannel(-1, clickSound, 0);
needsRedraw = true;
}
}
if (needsRedraw)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
drawBackground();
drawSelector();
drawTitle(title);
drawFileList();
// Render selection counter once per frame
renderCounter(renderer, chosenFileI + 1, static_cast<int>(fileList.size()));
SDL_RenderPresent(renderer);
needsRedraw = false;
}
if (shouldExit)
{
// Force a redraw before exit to ensure the last selection is visible
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
drawBackground();
drawSelector();
drawTitle(title);
drawFileList();
renderCounter(renderer, chosenFileI + 1, static_cast<int>(fileList.size()));
SDL_RenderPresent(renderer);
return exitCode;
}
}
}
string Selector::get()
{
return chosenFileI < 0 ? "" : fileList[chosenFileI];
}