all repos — mgba @ f155780eba1e4008f0b0548a92e67cfdf69df218

mGBA Game Boy Advance Emulator

src/util/gui/file-select.c (view raw)

  1/* Copyright (c) 2013-2015 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "file-select.h"
  7
  8#include "util/gui/font.h"
  9#include "util/gui/menu.h"
 10#include "util/vfs.h"
 11
 12#include <stdlib.h>
 13
 14#define ITERATION_SIZE 5
 15#define SCANNING_THRESHOLD_1 50
 16#ifdef _3DS
 17// 3DS is slooooow at opening files
 18#define SCANNING_THRESHOLD_2 10
 19#else
 20#define SCANNING_THRESHOLD_2 50
 21#endif
 22
 23static void _cleanFiles(struct GUIMenuItemList* currentFiles) {
 24	size_t size = GUIMenuItemListSize(currentFiles);
 25	size_t i;
 26	for (i = 1; i < size; ++i) {
 27		free(GUIMenuItemListGetPointer(currentFiles, i)->title);
 28	}
 29	GUIMenuItemListClear(currentFiles);
 30}
 31
 32static void _upDirectory(char* currentPath) {
 33	char* end = strrchr(currentPath, '/');
 34	if (!end) {
 35		return;
 36	}
 37	if (end == currentPath) {
 38		end[1] = '\0';
 39		return;
 40	}
 41	end[0] = '\0';
 42	if (end[1]) {
 43		return;
 44	}
 45	// TODO: What if there was a trailing slash?
 46}
 47
 48static int _strpcmp(const void* a, const void* b) {
 49	return strcasecmp(((const struct GUIMenuItem*) a)->title, ((const struct GUIMenuItem*) b)->title);
 50}
 51
 52static bool _refreshDirectory(struct GUIParams* params, const char* currentPath, struct GUIMenuItemList* currentFiles, bool (*filter)(struct VFile*)) {
 53	_cleanFiles(currentFiles);
 54
 55	struct VDir* dir = VDirOpen(currentPath);
 56	if (!dir) {
 57		return false;
 58	}
 59	*GUIMenuItemListAppend(currentFiles) = (struct GUIMenuItem) { .title = "(Up)" };
 60	size_t i = 0;
 61	size_t items = 0;
 62	struct VDirEntry* de;
 63	while ((de = dir->listNext(dir))) {
 64		++i;
 65		if (!(i % SCANNING_THRESHOLD_1)) {
 66			uint32_t input = 0;
 67			GUIPollInput(params, &input, 0);
 68			if (input & (1 << GUI_INPUT_CANCEL)) {
 69				return false;
 70			}
 71
 72			params->drawStart();
 73			if (params->guiPrepare) {
 74				params->guiPrepare();
 75			}
 76			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_TEXT_LEFT, 0xFFFFFFFF, "%s", currentPath);
 77			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "(scanning for items: %lu)", i);
 78			if (params->guiFinish) {
 79				params->guiFinish();
 80			}
 81			params->drawEnd();
 82		}
 83		const char* name = de->name(de);
 84		if (name[0] == '.') {
 85			continue;
 86		}
 87		*GUIMenuItemListAppend(currentFiles) = (struct GUIMenuItem) { .title = strdup(name) };
 88		++items;
 89	}
 90	qsort(GUIMenuItemListGetPointer(currentFiles, 1), GUIMenuItemListSize(currentFiles) - 1, sizeof(struct GUIMenuItem), _strpcmp);
 91	i = 0;
 92	size_t item = 0;
 93	while (item < GUIMenuItemListSize(currentFiles)) {
 94		++i;
 95		if (!(i % SCANNING_THRESHOLD_2)) {
 96			uint32_t input = 0;
 97			GUIPollInput(params, &input, 0);
 98			if (input & (1 << GUI_INPUT_CANCEL)) {
 99				return false;
100			}
101
102			params->drawStart();
103			if (params->guiPrepare) {
104				params->guiPrepare();
105			}
106			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_TEXT_LEFT, 0xFFFFFFFF, "%s", currentPath);
107			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_TEXT_LEFT, 0xFFFFFFFF, "(scanning item %lu of %lu)", i, items);
108			if (params->guiFinish) {
109				params->guiFinish();
110			}
111			params->drawEnd();
112		}
113		struct VFile* vf = dir->openFile(dir, GUIMenuItemListGetPointer(currentFiles, item)->title, O_RDONLY);
114		if (!vf) {
115			++item;
116			continue;
117		}
118		if (filter && !filter(vf)) {
119			free(GUIMenuItemListGetPointer(currentFiles, item)->title);
120			GUIMenuItemListShift(currentFiles, item, 1);
121		} else {
122			++item;
123		}
124		vf->close(vf);
125	}
126	dir->close(dir);
127
128	return true;
129}
130
131bool GUISelectFile(struct GUIParams* params, char* outPath, size_t outLen, bool (*filter)(struct VFile*)) {
132	struct GUIMenu menu = {
133		.title = params->currentPath,
134		.index = params->fileIndex,
135	};
136	GUIMenuItemListInit(&menu.items, 0);
137	_refreshDirectory(params, params->currentPath, &menu.items, filter);
138
139	while (true) {
140		struct GUIMenuItem item;
141		enum GUIMenuExitReason reason = GUIShowMenu(params, &menu, &item);
142		params->fileIndex = menu.index;
143		if (reason == GUI_MENU_EXIT_CANCEL) {
144			break;
145		}
146		if (reason == GUI_MENU_EXIT_ACCEPT) {
147			if (params->fileIndex == 0) {
148				_upDirectory(params->currentPath);
149				if (!_refreshDirectory(params, params->currentPath, &menu.items, filter)) {
150					break;
151				}
152			} else {
153				size_t len = strlen(params->currentPath);
154				const char* sep = PATH_SEP;
155				if (params->currentPath[len - 1] == *sep) {
156					sep = "";
157				}
158				snprintf(outPath, outLen, "%s%s%s", params->currentPath, sep, item.title);
159
160				struct GUIMenuItemList newFiles;
161				GUIMenuItemListInit(&newFiles, 0);
162				if (!_refreshDirectory(params, outPath, &newFiles, filter)) {
163					_cleanFiles(&newFiles);
164					GUIMenuItemListDeinit(&newFiles);
165					struct VFile* vf = VFileOpen(outPath, O_RDONLY);
166					if (!vf) {
167						continue;
168					}
169					if (!filter || filter(vf)) {
170						vf->close(vf);
171						_cleanFiles(&menu.items);
172						GUIMenuItemListDeinit(&menu.items);
173						return true;
174					}
175					vf->close(vf);
176					break;
177				} else {
178					_cleanFiles(&menu.items);
179					GUIMenuItemListDeinit(&menu.items);
180					menu.items = newFiles;
181					strncpy(params->currentPath, outPath, PATH_MAX);
182				}
183			}
184			params->fileIndex = 0;
185			menu.index = 0;
186		}
187		if (reason == GUI_MENU_EXIT_BACK) {
188			if (strncmp(params->currentPath, params->basePath, PATH_MAX) == 0) {
189				break;
190			}
191			_upDirectory(params->currentPath);
192			if (!_refreshDirectory(params, params->currentPath, &menu.items, filter)) {
193				break;
194			}
195			params->fileIndex = 0;
196			menu.index = 0;
197		}
198	}
199
200	_cleanFiles(&menu.items);
201	GUIMenuItemListDeinit(&menu.items);
202	return false;
203}