2018-04-18 10:30:34 +01:00
|
|
|
#include <switch.h>
|
|
|
|
#include <algorithm>
|
2018-06-19 19:07:31 +01:00
|
|
|
#include <array>
|
2018-04-18 23:24:40 +01:00
|
|
|
#include <cstdio>
|
2018-04-18 00:41:57 +01:00
|
|
|
#include "ldr_launch_queue.hpp"
|
2018-06-19 19:07:31 +01:00
|
|
|
#include "meta_tools.hpp"
|
2018-04-18 00:41:57 +01:00
|
|
|
|
2018-06-19 19:07:31 +01:00
|
|
|
static std::array<LaunchQueue::LaunchItem, LAUNCH_QUEUE_SIZE> g_launch_queue = {0};
|
2018-04-18 00:41:57 +01:00
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
Result LaunchQueue::add(u64 tid, const char *args, u64 arg_size) {
|
|
|
|
if(arg_size > LAUNCH_QUEUE_ARG_SIZE_MAX) {
|
|
|
|
return 0x209;
|
2018-04-18 10:30:34 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
int idx = get_free_index(tid);
|
|
|
|
if(idx == LAUNCH_QUEUE_FULL)
|
|
|
|
return 0x409;
|
2018-04-18 01:05:19 +01:00
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
g_launch_queue[idx].tid = tid;
|
|
|
|
g_launch_queue[idx].arg_size = arg_size;
|
2018-04-18 01:04:41 +01:00
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
std::copy(args, args + arg_size, g_launch_queue[idx].args);
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
2018-04-21 06:58:42 +01:00
|
|
|
Result LaunchQueue::add_copy(u64 tid_base, u64 tid) {
|
2018-05-14 23:54:12 +01:00
|
|
|
int idx = get_index(tid_base);
|
2018-04-21 06:58:42 +01:00
|
|
|
if (idx == LAUNCH_QUEUE_FULL) {
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return add(tid, g_launch_queue[idx].args, g_launch_queue[idx].arg_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
Result LaunchQueue::add_item(const LaunchItem *item) {
|
|
|
|
if(item->arg_size > LAUNCH_QUEUE_ARG_SIZE_MAX) {
|
|
|
|
return 0x209;
|
2018-04-18 00:41:57 +01:00
|
|
|
}
|
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
int idx = get_free_index(item->tid);
|
|
|
|
if(idx == LAUNCH_QUEUE_FULL)
|
|
|
|
return 0x409;
|
2018-04-18 00:41:57 +01:00
|
|
|
|
2018-04-19 06:00:10 +01:00
|
|
|
g_launch_queue[idx] = *item;
|
|
|
|
return 0x0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int LaunchQueue::get_index(u64 tid) {
|
2018-06-19 19:07:31 +01:00
|
|
|
auto it = std::find_if(g_launch_queue.begin(), g_launch_queue.end(), member_equals_fn(&LaunchQueue::LaunchItem::tid, tid));
|
|
|
|
if (it == g_launch_queue.end()) {
|
|
|
|
return LAUNCH_QUEUE_FULL;
|
2018-04-19 06:00:10 +01:00
|
|
|
}
|
2018-06-19 19:07:31 +01:00
|
|
|
return std::distance(g_launch_queue.begin(), it);
|
2018-04-19 06:00:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int LaunchQueue::get_free_index(u64 tid) {
|
|
|
|
for(unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
|
|
|
if(g_launch_queue[i].tid == tid || g_launch_queue[i].tid == 0x0) {
|
|
|
|
return i;
|
2018-04-18 01:04:41 +01:00
|
|
|
}
|
2018-04-18 00:41:57 +01:00
|
|
|
}
|
2018-04-19 06:00:10 +01:00
|
|
|
return LAUNCH_QUEUE_FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LaunchQueue::contains(u64 tid) {
|
|
|
|
return get_index(tid) != LAUNCH_QUEUE_FULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LaunchQueue::clear() {
|
|
|
|
for (unsigned int i = 0; i < LAUNCH_QUEUE_SIZE; i++) {
|
|
|
|
g_launch_queue[i].tid = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-04-22 02:52:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
LaunchQueue::LaunchItem *LaunchQueue::get_item(u64 tid) {
|
2018-06-19 19:07:31 +01:00
|
|
|
int idx = get_index(tid);
|
|
|
|
if (idx == LAUNCH_QUEUE_FULL) {
|
2018-04-22 02:52:49 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return &g_launch_queue[idx];
|
2018-05-04 00:24:34 +01:00
|
|
|
}
|