2022-04-23 04:59:50 -04:00
|
|
|
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
|
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2021-09-20 20:25:22 -05:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2021-10-30 22:23:10 -05:00
|
|
|
#include <array>
|
|
|
|
|
|
2021-09-20 20:25:22 -05:00
|
|
|
#include "common/common_types.h"
|
|
|
|
|
|
|
|
|
|
namespace Service::HID {
|
|
|
|
|
|
|
|
|
|
template <typename State>
|
|
|
|
|
struct AtomicStorage {
|
2021-10-22 23:04:06 -05:00
|
|
|
s64 sampling_number;
|
2021-09-20 20:25:22 -05:00
|
|
|
State state;
|
|
|
|
|
};
|
|
|
|
|
|
2021-11-22 22:15:34 -06:00
|
|
|
template <typename State, std::size_t max_buffer_size>
|
2021-09-20 20:25:22 -05:00
|
|
|
struct Lifo {
|
2021-10-22 23:04:06 -05:00
|
|
|
s64 timestamp{};
|
2021-11-14 21:56:54 -06:00
|
|
|
s64 total_buffer_count = static_cast<s64>(max_buffer_size);
|
2021-10-27 18:06:13 -05:00
|
|
|
s64 buffer_tail{};
|
|
|
|
|
s64 buffer_count{};
|
|
|
|
|
std::array<AtomicStorage<State>, max_buffer_size> entries{};
|
2021-09-20 20:25:22 -05:00
|
|
|
|
|
|
|
|
const AtomicStorage<State>& ReadCurrentEntry() const {
|
2021-10-27 18:06:13 -05:00
|
|
|
return entries[buffer_tail];
|
2021-09-20 20:25:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const AtomicStorage<State>& ReadPreviousEntry() const {
|
2021-10-30 22:23:10 -05:00
|
|
|
return entries[GetPreviousEntryIndex()];
|
2021-09-20 20:25:22 -05:00
|
|
|
}
|
|
|
|
|
|
2021-10-30 22:23:10 -05:00
|
|
|
std::size_t GetPreviousEntryIndex() const {
|
2023-11-02 19:14:05 -06:00
|
|
|
return static_cast<size_t>((buffer_tail + max_buffer_size - 1) % max_buffer_size);
|
2021-09-20 20:25:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::size_t GetNextEntryIndex() const {
|
2023-11-02 19:14:05 -06:00
|
|
|
return static_cast<size_t>((buffer_tail + 1) % max_buffer_size);
|
2021-09-20 20:25:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WriteNextEntry(const State& new_state) {
|
2023-11-02 19:14:05 -06:00
|
|
|
if (buffer_count < static_cast<s64>(max_buffer_size) - 1) {
|
2021-10-27 18:06:13 -05:00
|
|
|
buffer_count++;
|
2021-09-20 20:25:22 -05:00
|
|
|
}
|
2021-10-27 18:06:13 -05:00
|
|
|
buffer_tail = GetNextEntryIndex();
|
2021-09-20 20:25:22 -05:00
|
|
|
const auto& previous_entry = ReadPreviousEntry();
|
2021-10-27 18:06:13 -05:00
|
|
|
entries[buffer_tail].sampling_number = previous_entry.sampling_number + 1;
|
|
|
|
|
entries[buffer_tail].state = new_state;
|
2021-09-20 20:25:22 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace Service::HID
|