RetroArch/frontend/menu/utils/file_browser.c
2013-04-12 12:25:36 +02:00

138 lines
4.6 KiB
C

/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2013 - Hans-Kristian Arntzen
* Copyright (C) 2011-2013 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdint.h>
#include <stdlib.h>
#include "../../../file.h"
#include "file_browser.h"
static bool directory_parse(void *data, const char *path)
{
filebrowser_t *filebrowser = (filebrowser_t*)data;
struct string_list *list = dir_list_new(path,
filebrowser->current_dir.extensions, true);
if(!list)
return false;
dir_list_sort(list, true);
filebrowser->current_dir.ptr = 0;
strlcpy(filebrowser->current_dir.directory_path,
path, sizeof(filebrowser->current_dir.directory_path));
if(filebrowser->list)
dir_list_free(filebrowser->list);
filebrowser->list = list;
return true;
}
void filebrowser_free(void *data)
{
filebrowser_t *filebrowser = (filebrowser_t*)data;
dir_list_free(filebrowser->list);
filebrowser->list = NULL;
filebrowser->current_dir.ptr = 0;
free(filebrowser);
}
void filebrowser_set_root_and_ext(void *data, const char *ext, const char *root_dir)
{
filebrowser_t *filebrowser = (filebrowser_t*)data;
if (ext)
strlcpy(filebrowser->current_dir.extensions, ext,
sizeof(filebrowser->current_dir.extensions));
strlcpy(filebrowser->current_dir.root_dir,
root_dir, sizeof(filebrowser->current_dir.root_dir));
filebrowser_iterate(filebrowser, FILEBROWSER_ACTION_RESET);
}
#define GET_CURRENT_PATH(browser) (browser->list->elems[browser->current_dir.ptr].data)
bool filebrowser_iterate(void *data, unsigned action)
{
filebrowser_t *filebrowser = (filebrowser_t*)data;
bool ret = true;
unsigned entries_to_scroll = 19;
switch(action)
{
case FILEBROWSER_ACTION_UP:
filebrowser->current_dir.ptr--;
if (filebrowser->current_dir.ptr >= filebrowser->list->size)
filebrowser->current_dir.ptr = filebrowser->list->size - 1;
break;
case FILEBROWSER_ACTION_DOWN:
filebrowser->current_dir.ptr++;
if (filebrowser->current_dir.ptr >= filebrowser->list->size)
filebrowser->current_dir.ptr = 0;
break;
case FILEBROWSER_ACTION_LEFT:
if (filebrowser->current_dir.ptr <= 5)
filebrowser->current_dir.ptr = 0;
else
filebrowser->current_dir.ptr -= 5;
break;
case FILEBROWSER_ACTION_RIGHT:
filebrowser->current_dir.ptr = (min(filebrowser->current_dir.ptr + 5,
filebrowser->list->size-1));
break;
case FILEBROWSER_ACTION_SCROLL_UP:
if (filebrowser->current_dir.ptr <= entries_to_scroll)
filebrowser->current_dir.ptr= 0;
else
filebrowser->current_dir.ptr -= entries_to_scroll;
break;
case FILEBROWSER_ACTION_SCROLL_DOWN:
filebrowser->current_dir.ptr = (min(filebrowser->current_dir.ptr +
entries_to_scroll, filebrowser->list->size-1));
break;
case FILEBROWSER_ACTION_OK:
ret = directory_parse(filebrowser, GET_CURRENT_PATH(filebrowser));
break;
case FILEBROWSER_ACTION_CANCEL:
fill_pathname_parent_dir(filebrowser->current_dir.directory_path,
filebrowser->current_dir.directory_path,
sizeof(filebrowser->current_dir.directory_path));
ret = directory_parse(filebrowser, filebrowser->current_dir.directory_path);
break;
case FILEBROWSER_ACTION_RESET:
ret = directory_parse(filebrowser, filebrowser->current_dir.root_dir);
break;
case FILEBROWSER_ACTION_RESET_CURRENT_DIR:
ret = directory_parse(filebrowser, filebrowser->current_dir.directory_path);
break;
case FILEBROWSER_ACTION_PATH_ISDIR:
ret = filebrowser->list->elems[filebrowser->current_dir.ptr].attr.b;
break;
case FILEBROWSER_ACTION_NOOP:
default:
break;
}
strlcpy(filebrowser->current_dir.path, GET_CURRENT_PATH(filebrowser),
sizeof(filebrowser->current_dir.path));
return ret;
}