Checkpoint/switch/source/main.cpp

191 lines
6.1 KiB
C++
Raw Normal View History

2018-05-09 11:25:01 +02:00
/*
* This file is part of Checkpoint
* Copyright (C) 2017-2018 Bernardo Giordano
*
* This program 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 Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* Additional Terms 7.b and 7.c of GPLv3 apply to this file:
* * Requiring preservation of specified reasonable legal notices or
* author attributions in that material or in the Appropriate Legal
* Notices displayed by works containing it.
* * Prohibiting misrepresentation of the origin of that material,
* or requiring that modified versions of such material be marked in
* reasonable ways as different from the original version.
*/
2018-06-10 22:24:37 +02:00
#include "main.hpp"
u128 g_currentUId = 0;
u8 g_currentUserIndex = 0;
2018-05-09 11:25:01 +02:00
int main(int argc, char** argv)
{
Result res = servicesInit();
if (R_FAILED(res))
{
servicesExit();
return res;
}
res = Threads::create((ThreadFunc)loadTitles);
if (R_FAILED(res))
{
Gui::createError(res, "Failed to create thread.");
}
int selectionTimer = 0;
while(appletMainLoop() && !(hidKeysDown(CONTROLLER_P1_AUTO) & KEY_PLUS))
{
2018-06-10 22:24:37 +02:00
// get the user IDs
std::vector<u128> userIds = Account::ids();
// set g_currentUId to a default user in case we loaded at least one user
if (g_currentUId == 0 && !userIds.empty()) g_currentUId = userIds.at(0);
2018-05-09 11:25:01 +02:00
hidScanInput();
u32 kdown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kdown & KEY_ZL)
{
2018-06-12 21:53:25 +02:00
++g_currentUserIndex %= userIds.size();
g_currentUId = userIds.at(g_currentUserIndex);
2018-06-15 16:13:15 +02:00
Gui::index(TITLES, 0);
}
if (kdown & KEY_ZR)
{
g_currentUserIndex = g_currentUserIndex - 1 < 0 ? userIds.size() - 1 : g_currentUserIndex - 1;
g_currentUId = userIds.at(g_currentUserIndex);
2018-06-15 16:13:15 +02:00
Gui::index(TITLES, 0);
}
// handle touchscreen
touchPosition touch;
hidTouchRead(&touch, 0);
for (u8 i = 0; i < userIds.size(); i++)
{
2018-07-17 18:25:07 +02:00
if (!Gui::backupScroll() &&
hidKeysHeld(CONTROLLER_P1_AUTO) & KEY_TOUCH &&
touch.px >= u32(1280 - (USER_ICON_SIZE + 4) * (i+1)) &&
touch.px <= u32(1280 - (USER_ICON_SIZE + 4) * i) &&
2018-06-12 21:53:25 +02:00
touch.py >= 32 && touch.py <= 32 + USER_ICON_SIZE)
{
g_currentUserIndex = i;
g_currentUId = userIds.at(g_currentUserIndex);
2018-06-15 16:13:15 +02:00
Gui::index(TITLES, 0);
}
}
2018-07-17 21:42:49 +02:00
if (kdown & KEY_A ||
(hidKeysDown(CONTROLLER_P1_AUTO) & KEY_TOUCH &&
(int)touch.px > 540 &&
(int)touch.px < 1046 &&
(int)touch.py > 462 &&
(int)touch.py < 692))
2018-05-09 11:25:01 +02:00
{
Gui::backupScroll(true);
Gui::updateButtonsColor();
2018-05-30 22:56:19 +02:00
Gui::entryType(CELLS);
2018-05-09 11:25:01 +02:00
}
2018-07-17 21:42:49 +02:00
if (kdown & KEY_B ||
(hidKeysDown(CONTROLLER_P1_AUTO) & KEY_TOUCH &&
(int)touch.px > 0 &&
(int)touch.px < 532 &&
(int)touch.py > 28 &&
(int)touch.py < 692))
2018-05-09 11:25:01 +02:00
{
2018-07-17 18:25:07 +02:00
Gui::index(CELLS, 0);
2018-05-09 11:25:01 +02:00
Gui::backupScroll(false);
Gui::updateButtonsColor();
2018-05-30 22:56:19 +02:00
Gui::entryType(TITLES);
2018-05-09 11:25:01 +02:00
Gui::clearSelectedEntries();
}
if (kdown & KEY_X)
{
if (Gui::backupScroll())
{
size_t index = Gui::index(CELLS);
if (index > 0 && Gui::askForConfirmation("Delete selected backup?"))
{
Title title;
2018-06-10 22:24:37 +02:00
getTitle(title, g_currentUId, Gui::index(TITLES));
2018-07-17 18:25:07 +02:00
std::string path = title.fullPath(index);
2018-05-09 11:25:01 +02:00
io::deleteFolderRecursively(path.c_str());
refreshDirectories(title.id());
Gui::index(CELLS, index - 1);
}
}
}
if (kdown & KEY_Y && !(Gui::backupScroll()))
{
Gui::addSelectedEntry(Gui::index(TITLES));
}
if (hidKeysHeld(CONTROLLER_P1_AUTO) & KEY_Y && !(Gui::backupScroll()))
{
selectionTimer++;
}
else
{
selectionTimer = 0;
}
2018-07-16 16:00:34 +02:00
if (selectionTimer > 45)
2018-05-09 11:25:01 +02:00
{
Gui::clearSelectedEntries();
2018-06-10 22:24:37 +02:00
for (size_t i = 0, sz = getTitleCount(g_currentUId); i < sz; i++)
2018-05-09 11:25:01 +02:00
{
Gui::addSelectedEntry(i);
}
selectionTimer = 0;
}
2018-05-13 01:11:01 +02:00
if (Gui::isBackupReleased() || (kdown & KEY_L && Gui::backupScroll()))
2018-05-09 11:25:01 +02:00
{
if (Gui::multipleSelectionEnabled())
{
Gui::resetIndex(CELLS);
std::vector<size_t> list = Gui::selectedEntries();
for (size_t i = 0, sz = list.size(); i < sz; i++)
{
2018-06-10 22:24:37 +02:00
io::backup(list.at(i), g_currentUId);
2018-05-09 11:25:01 +02:00
}
Gui::clearSelectedEntries();
}
else
{
2018-06-10 22:24:37 +02:00
io::backup(Gui::index(TITLES), g_currentUId);
2018-05-09 11:25:01 +02:00
}
}
2018-05-13 01:11:01 +02:00
if (Gui::isRestoreReleased() || (kdown & KEY_R && Gui::backupScroll()))
2018-05-09 11:25:01 +02:00
{
if (Gui::multipleSelectionEnabled())
{
Gui::clearSelectedEntries();
}
else
{
2018-06-10 22:24:37 +02:00
io::restore(Gui::index(TITLES), g_currentUId);
2018-05-09 11:25:01 +02:00
}
}
Gui::updateSelector();
2018-06-10 22:24:37 +02:00
Gui::draw(g_currentUId);
2018-05-09 11:25:01 +02:00
}
Threads::destroy();
servicesExit();
return 0;
}