all repos — mgba @ 4f312a099848ccc657fdf0cb39efbf710895039e

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 <mgba-util/gui/file-select.h>
  7
  8#include <mgba-util/gui/font.h>
  9#include <mgba-util/gui/menu.h>
 10#include <mgba-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((char*) GUIMenuItemListGetPointer(currentFiles, i)->title);
 28	}
 29	GUIMenuItemListClear(currentFiles);
 30}
 31
 32static void _upDirectory(char* currentPath) {
 33	char* end = strrchr(currentPath, '/');
 34	if (!end) {
 35		currentPath[0] = '\0';
 36		return;
 37	}
 38	if (!end[1]) {
 39		// Trailing slash
 40		end[0] = '\0';
 41		return _upDirectory(currentPath);
 42	}
 43	end[1] = '\0';
 44}
 45
 46static int _strpcmp(const void* a, const void* b) {
 47	return strcasecmp(((const struct GUIMenuItem*) a)->title, ((const struct GUIMenuItem*) b)->title);
 48}
 49
 50static bool _refreshDirectory(struct GUIParams* params, const char* currentPath, struct GUIMenuItemList* currentFiles, bool (*filterName)(const char* name), bool (*filterContents)(struct VFile*)) {
 51	_cleanFiles(currentFiles);
 52
 53	struct VDir* dir = VDirOpen(currentPath);
 54	if (!dir) {
 55		return false;
 56	}
 57	*GUIMenuItemListAppend(currentFiles) = (struct GUIMenuItem) { .title = "(Up)" };
 58	size_t i = 0;
 59	size_t items = 0;
 60	struct VDirEntry* de;
 61	while ((de = dir->listNext(dir))) {
 62		++i;
 63		if (!(i % SCANNING_THRESHOLD_1)) {
 64			uint32_t input = 0;
 65			GUIPollInput(params, &input, 0);
 66			if (input & (1 << GUI_INPUT_CANCEL)) {
 67				return false;
 68			}
 69
 70			params->drawStart();
 71			if (params->guiPrepare) {
 72				params->guiPrepare();
 73			}
 74			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_ALIGN_LEFT, 0xFFFFFFFF, "(scanning for items: %"PRIz"u)", i);
 75			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_ALIGN_LEFT, 0xFFFFFFFF, "%s", currentPath);
 76			if (params->guiFinish) {
 77				params->guiFinish();
 78			}
 79			params->drawEnd();
 80		}
 81		const char* name = de->name(de);
 82		if (name[0] == '.') {
 83			continue;
 84		}
 85		if (de->type(de) == VFS_DIRECTORY) {
 86			size_t len = strlen(name) + 2;
 87			char* n2 = malloc(len);
 88			snprintf(n2, len, "%s/", name);
 89			name = n2;
 90		} else {
 91			name = strdup(name);
 92		}
 93		*GUIMenuItemListAppend(currentFiles) = (struct GUIMenuItem) { .title = name, .data = (void*) de->type(de) };
 94		++items;
 95	}
 96	qsort(GUIMenuItemListGetPointer(currentFiles, 1), GUIMenuItemListSize(currentFiles) - 1, sizeof(struct GUIMenuItem), _strpcmp);
 97	i = 0;
 98	size_t item = 0;
 99	while (item < GUIMenuItemListSize(currentFiles)) {
100		++i;
101		if (!(i % SCANNING_THRESHOLD_2)) {
102			uint32_t input = 0;
103			GUIPollInput(params, &input, 0);
104			if (input & (1 << GUI_INPUT_CANCEL)) {
105				return false;
106			}
107
108			params->drawStart();
109			if (params->guiPrepare) {
110				params->guiPrepare();
111			}
112			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font), GUI_ALIGN_LEFT, 0xFFFFFFFF, "(scanning item %"PRIz"u of %"PRIz"u)", i, items);
113			GUIFontPrintf(params->font, 0, GUIFontHeight(params->font) * 2, GUI_ALIGN_LEFT, 0xFFFFFFFF, "%s", currentPath);
114			if (params->guiFinish) {
115				params->guiFinish();
116			}
117			params->drawEnd();
118		}
119		struct GUIMenuItem* testItem = GUIMenuItemListGetPointer(currentFiles, item);
120		if (testItem->data != (void*) VFS_FILE) {
121			++item;
122			continue;
123		}
124		bool failed = false;
125		if (filterName && !filterName(testItem->title)) {
126			failed = true;
127		}
128
129		if (!failed && filterContents) {
130			struct VFile* vf = dir->openFile(dir, testItem->title, O_RDONLY);
131			if (!vf) {
132				failed = true;
133			} else {
134				if (!filterContents(vf)) {
135					failed = true;
136				}
137				vf->close(vf);
138			}
139		}
140
141		if (failed) {
142			free((char*) testItem->title);
143			GUIMenuItemListShift(currentFiles, item, 1);
144		} else {
145			++item;
146		}
147	}
148	dir->close(dir);
149
150	return true;
151}
152
153bool GUISelectFile(struct GUIParams* params, char* outPath, size_t outLen, bool (*filterName)(const char* name), bool (*filterContents)(struct VFile*)) {
154	struct GUIMenu menu = {
155		.title = "Select file",
156		.subtitle = params->currentPath,
157		.index = params->fileIndex,
158	};
159	GUIMenuItemListInit(&menu.items, 0);
160	_refreshDirectory(params, params->currentPath, &menu.items, filterName, filterContents);
161
162	while (true) {
163		struct GUIMenuItem* item;
164		enum GUIMenuExitReason reason = GUIShowMenu(params, &menu, &item);
165		params->fileIndex = menu.index;
166		if (reason == GUI_MENU_EXIT_CANCEL) {
167			break;
168		}
169		if (reason == GUI_MENU_EXIT_ACCEPT) {
170			if (params->fileIndex == 0) {
171				_upDirectory(params->currentPath);
172				if (!_refreshDirectory(params, params->currentPath, &menu.items, filterName, filterContents)) {
173					break;
174				}
175			} else {
176				size_t len = strlen(params->currentPath);
177				const char* sep = PATH_SEP;
178				if (!len || params->currentPath[len - 1] == *sep) {
179					sep = "";
180				}
181				snprintf(outPath, outLen, "%s%s%s", params->currentPath, sep, item->title);
182
183				struct GUIMenuItemList newFiles;
184				GUIMenuItemListInit(&newFiles, 0);
185				if (!_refreshDirectory(params, outPath, &newFiles, filterName, filterContents)) {
186					_cleanFiles(&newFiles);
187					GUIMenuItemListDeinit(&newFiles);
188					_cleanFiles(&menu.items);
189					GUIMenuItemListDeinit(&menu.items);
190					return true;
191				} else {
192					_cleanFiles(&menu.items);
193					GUIMenuItemListDeinit(&menu.items);
194					menu.items = newFiles;
195					strncpy(params->currentPath, outPath, PATH_MAX);
196				}
197			}
198			params->fileIndex = 0;
199			menu.index = 0;
200		}
201		if (reason == GUI_MENU_EXIT_BACK) {
202			if (strncmp(params->currentPath, params->basePath, PATH_MAX) == 0) {
203				break;
204			}
205			_upDirectory(params->currentPath);
206			if (!_refreshDirectory(params, params->currentPath, &menu.items, filterName, filterContents)) {
207				break;
208			}
209			params->fileIndex = 0;
210			menu.index = 0;
211		}
212	}
213
214	_cleanFiles(&menu.items);
215	GUIMenuItemListDeinit(&menu.items);
216	return false;
217}