Steven Smith 1b78bebd94 General cleanup.
* Maintain list position after operations.
* Show current target info in batch operations.
* Fix freeze when suspending to the home menu during a threaded task.
* Other miscellaneous fixes and clean-ups.
2016-04-26 19:30:00 -07:00

155 lines
4.8 KiB
C

#include <malloc.h>
#include <stdio.h>
#include <3ds.h>
#include "section.h"
#include "action/action.h"
#include "task/task.h"
#include "../error.h"
#include "../list.h"
#include "../ui.h"
#include "../../core/linkedlist.h"
#include "../../core/screen.h"
static list_item launch_title = {"Launch Title", COLOR_TEXT, action_launch_title};
static list_item delete_title = {"Delete Title", COLOR_TEXT, action_delete_title};
static list_item extract_smdh = {"Extract SMDH", COLOR_TEXT, action_extract_smdh};
static list_item browse_save_data = {"Browse Save Data", COLOR_TEXT, action_browse_title_save_data};
static list_item import_secure_value = {"Import Secure Value", COLOR_TEXT, action_import_secure_value};
static list_item export_secure_value = {"Export Secure Value", COLOR_TEXT, action_export_secure_value};
static list_item delete_secure_value = {"Delete Secure Value", COLOR_TEXT, action_delete_secure_value};
typedef struct {
Handle cancelEvent;
bool populated;
} titles_data;
typedef struct {
linked_list* items;
list_item* selected;
} titles_action_data;
static void titles_action_draw_top(ui_view* view, void* data, float x1, float y1, float x2, float y2, list_item* selected) {
ui_draw_title_info(view, ((titles_action_data*) data)->selected->data, x1, y1, x2, y2);
}
static void titles_action_update(ui_view* view, void* data, linked_list* items, list_item* selected, bool selectedTouched) {
titles_action_data* actionData = (titles_action_data*) data;
if(hidKeysDown() & KEY_B) {
ui_pop();
list_destroy(view);
free(data);
return;
}
if(selected != NULL && selected->data != NULL && (selectedTouched || (hidKeysDown() & KEY_A))) {
void(*action)(linked_list*, list_item*) = (void(*)(linked_list*, list_item*)) selected->data;
ui_pop();
list_destroy(view);
action(actionData->items, actionData->selected);
free(data);
return;
}
if(linked_list_size(items) == 0) {
linked_list_add(items, &launch_title);
title_info* info = (title_info*) actionData->selected->data;
if(info->mediaType != MEDIATYPE_GAME_CARD) {
linked_list_add(items, &delete_title);
}
if(!info->twl) {
linked_list_add(items, &extract_smdh);
linked_list_add(items, &browse_save_data);
if(info->mediaType != MEDIATYPE_GAME_CARD) {
linked_list_add(items, &import_secure_value);
linked_list_add(items, &export_secure_value);
linked_list_add(items, &delete_secure_value);
}
}
}
}
static void titles_action_open(linked_list* items, list_item* selected) {
titles_action_data* data = (titles_action_data*) calloc(1, sizeof(titles_action_data));
if(data == NULL) {
error_display(NULL, NULL, NULL, "Failed to allocate titles action data.");
return;
}
data->items = items;
data->selected = selected;
list_display("Title Action", "A: Select, B: Return", data, titles_action_update, titles_action_draw_top);
}
static void titles_draw_top(ui_view* view, void* data, float x1, float y1, float x2, float y2, list_item* selected) {
if(selected != NULL && selected->data != NULL) {
ui_draw_title_info(view, selected->data, x1, y1, x2, y2);
}
}
static void titles_update(ui_view* view, void* data, linked_list* items, list_item* selected, bool selectedTouched) {
titles_data* listData = (titles_data*) data;
if(hidKeysDown() & KEY_B) {
if(listData->cancelEvent != 0) {
svcSignalEvent(listData->cancelEvent);
while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) {
svcSleepThread(1000000);
}
listData->cancelEvent = 0;
}
ui_pop();
task_clear_titles(items);
list_destroy(view);
free(listData);
return;
}
if(!listData->populated || (hidKeysDown() & KEY_X)) {
if(listData->cancelEvent != 0) {
svcSignalEvent(listData->cancelEvent);
while(svcWaitSynchronization(listData->cancelEvent, 0) == 0) {
svcSleepThread(1000000);
}
listData->cancelEvent = 0;
}
listData->cancelEvent = task_populate_titles(items);
listData->populated = true;
}
if(selected != NULL && selected->data != NULL && (selectedTouched || (hidKeysDown() & KEY_A))) {
titles_action_open(items, selected);
return;
}
}
void titles_open() {
titles_data* data = (titles_data*) calloc(1, sizeof(titles_data));
if(data == NULL) {
error_display(NULL, NULL, NULL, "Failed to allocate titles data.");
return;
}
list_display("Titles", "A: Select, B: Return, X: Refresh", data, titles_update, titles_draw_top);
}