blob: b95733969f0dfb1f6050a2e94253c34aadcea79a [file] [log] [blame]
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07001#include <sdeventplus/clock.hpp>
2#include <sdeventplus/event.hpp>
William A. Kennington III2584f9d2019-03-23 21:05:48 -07003#include <sdeventplus/source/signal.hpp>
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07004#include <sdeventplus/source/time.hpp>
William A. Kennington III2584f9d2019-03-23 21:05:48 -07005#include <stdplus/signal.hpp>
Patrick Williamsa8c11e32023-05-10 07:50:56 -05006
7#include <chrono>
8#include <cstdio>
William A. Kennington III7e2e60e2018-07-17 14:40:14 -07009#include <string>
10#include <utility>
11
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070012using sdeventplus::Event;
William A. Kennington III406b86e2018-09-25 15:23:00 -070013using sdeventplus::source::Enabled;
William A. Kennington III2584f9d2019-03-23 21:05:48 -070014using sdeventplus::source::Signal;
William A. Kennington III406b86e2018-09-25 15:23:00 -070015
16constexpr auto clockId = sdeventplus::ClockId::RealTime;
17using Clock = sdeventplus::Clock<clockId>;
18using Time = sdeventplus::source::Time<clockId>;
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070019
William A. Kennington III2584f9d2019-03-23 21:05:48 -070020void intCb(Signal& signal, const struct signalfd_siginfo*)
21{
22 printf("Exiting\n");
23 signal.get_event().exit(0);
24}
25
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070026int main(int argc, char* argv[])
27{
28 if (argc != 2)
29 {
30 fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
31 return 1;
32 }
33
34 unsigned interval = std::stoul(argv[1]);
35 fprintf(stderr, "Beating every %u seconds\n", interval);
36
37 auto event = Event::get_default();
William A. Kennington III406b86e2018-09-25 15:23:00 -070038 auto hbFunc = [interval](Time& source, Time::TimePoint time) {
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070039 printf("Beat\n");
William A. Kennington III406b86e2018-09-25 15:23:00 -070040
41 // Time sources are oneshot and are based on an absolute time
42 // we need to reconfigure the time source to go off again after the
43 // configured interval and re-enable it.
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070044 source.set_time(time + std::chrono::seconds{interval});
William A. Kennington III406b86e2018-09-25 15:23:00 -070045 source.set_enabled(Enabled::OneShot);
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070046 };
William A. Kennington III406b86e2018-09-25 15:23:00 -070047 Time time(event, Clock(event).now(), std::chrono::seconds{1},
48 std::move(hbFunc));
William A. Kennington III2584f9d2019-03-23 21:05:48 -070049 stdplus::signal::block(SIGINT);
William A. Kennington III1b49b6f2019-03-23 21:07:03 -070050 Signal(event, SIGINT, intCb).set_floating(true);
William A. Kennington III7e2e60e2018-07-17 14:40:14 -070051 return event.loop();
52}