/**
 * A simple example of a repeating timer that prints out a message for
 * each timer expiration.
 */

#include <sdeventplus/clock.hpp>
#include <sdeventplus/event.hpp>
#include <sdeventplus/source/signal.hpp>
#include <sdeventplus/utility/timer.hpp>
#include <stdplus/signal.hpp>

#include <chrono>
#include <cstdio>
#include <functional>
#include <string>

using sdeventplus::Clock;
using sdeventplus::ClockId;
using sdeventplus::Event;
using sdeventplus::source::Signal;

constexpr auto clockId = ClockId::RealTime;
using Timer = sdeventplus::utility::Timer<clockId>;

void intCb(Signal& signal, const struct signalfd_siginfo*)
{
    printf("Exiting\n");
    signal.get_event().exit(0);
}

int main(int argc, char* argv[])
{
    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
        return 1;
    }

    unsigned interval = std::stoul(argv[1]);
    fprintf(stderr, "Beating every %u seconds\n", interval);

    auto event = Event::get_default();
    Timer timer(
        event, [](Timer&) { printf("Beat\n"); },
        std::chrono::seconds{interval});
    stdplus::signal::block(SIGINT);
    Signal(event, SIGINT, intCb).set_floating(true);
    return event.loop();
}