2019-12-22 23:49:51 +01:00
|
|
|
// Copyright 2019 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include "core/hle/service/time/steady_clock_core.h"
|
|
|
|
#include "core/hle/service/time/system_clock_context_update_callback.h"
|
|
|
|
#include "core/hle/service/time/system_clock_core.h"
|
|
|
|
|
|
|
|
namespace Service::Time::Clock {
|
|
|
|
|
2021-05-16 07:46:30 +02:00
|
|
|
SystemClockCore::SystemClockCore(SteadyClockCore& steady_clock_core_)
|
|
|
|
: steady_clock_core{steady_clock_core_} {
|
2019-12-22 23:49:51 +01:00
|
|
|
context.steady_time_point.clock_source_id = steady_clock_core.GetClockSourceId();
|
|
|
|
}
|
|
|
|
|
|
|
|
SystemClockCore ::~SystemClockCore() = default;
|
|
|
|
|
|
|
|
ResultCode SystemClockCore::GetCurrentTime(Core::System& system, s64& posix_time) const {
|
|
|
|
posix_time = 0;
|
|
|
|
|
|
|
|
const SteadyClockTimePoint current_time_point{steady_clock_core.GetCurrentTimePoint(system)};
|
|
|
|
|
|
|
|
SystemClockContext clock_context{};
|
2021-05-21 07:05:04 +02:00
|
|
|
if (const ResultCode result{GetClockContext(system, clock_context)}; result != ResultSuccess) {
|
2019-12-22 23:49:51 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (current_time_point.clock_source_id != clock_context.steady_time_point.clock_source_id) {
|
|
|
|
return ERROR_TIME_MISMATCH;
|
|
|
|
}
|
|
|
|
|
|
|
|
posix_time = clock_context.offset + current_time_point.time_point;
|
|
|
|
|
2021-05-21 07:05:04 +02:00
|
|
|
return ResultSuccess;
|
2019-12-22 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ResultCode SystemClockCore::SetCurrentTime(Core::System& system, s64 posix_time) {
|
|
|
|
const SteadyClockTimePoint current_time_point{steady_clock_core.GetCurrentTimePoint(system)};
|
|
|
|
const SystemClockContext clock_context{posix_time - current_time_point.time_point,
|
|
|
|
current_time_point};
|
|
|
|
|
2021-05-21 07:05:04 +02:00
|
|
|
if (const ResultCode result{SetClockContext(clock_context)}; result != ResultSuccess) {
|
2019-12-22 23:49:51 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return Flush(clock_context);
|
|
|
|
}
|
|
|
|
|
2021-04-26 15:11:33 +02:00
|
|
|
ResultCode SystemClockCore::Flush(const SystemClockContext& clock_context) {
|
2019-12-22 23:49:51 +01:00
|
|
|
if (!system_clock_context_update_callback) {
|
2021-05-21 07:05:04 +02:00
|
|
|
return ResultSuccess;
|
2019-12-22 23:49:51 +01:00
|
|
|
}
|
2021-04-26 15:11:33 +02:00
|
|
|
return system_clock_context_update_callback->Update(clock_context);
|
2019-12-22 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2021-04-26 15:11:33 +02:00
|
|
|
ResultCode SystemClockCore::SetSystemClockContext(const SystemClockContext& clock_context) {
|
2021-05-21 07:05:04 +02:00
|
|
|
if (const ResultCode result{SetClockContext(clock_context)}; result != ResultSuccess) {
|
2019-12-22 23:49:51 +01:00
|
|
|
return result;
|
|
|
|
}
|
2021-04-26 15:11:33 +02:00
|
|
|
return Flush(clock_context);
|
2019-12-22 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SystemClockCore::IsClockSetup(Core::System& system) const {
|
|
|
|
SystemClockContext value{};
|
2021-05-21 07:05:04 +02:00
|
|
|
if (GetClockContext(system, value) == ResultSuccess) {
|
2019-12-22 23:49:51 +01:00
|
|
|
const SteadyClockTimePoint steady_clock_time_point{
|
|
|
|
steady_clock_core.GetCurrentTimePoint(system)};
|
|
|
|
return steady_clock_time_point.clock_source_id == value.steady_time_point.clock_source_id;
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Service::Time::Clock
|