1 /**
2 * A simple example of a repeating timer that prints out a message for
3 * each timer expiration.
4 */
5
6 #include <sdeventplus/clock.hpp>
7 #include <sdeventplus/event.hpp>
8 #include <sdeventplus/source/signal.hpp>
9 #include <sdeventplus/utility/timer.hpp>
10 #include <stdplus/signal.hpp>
11
12 #include <chrono>
13 #include <cstdio>
14 #include <functional>
15 #include <string>
16
17 using sdeventplus::Clock;
18 using sdeventplus::ClockId;
19 using sdeventplus::Event;
20 using sdeventplus::source::Signal;
21
22 constexpr auto clockId = ClockId::RealTime;
23 using Timer = sdeventplus::utility::Timer<clockId>;
24
intCb(Signal & signal,const struct signalfd_siginfo *)25 void intCb(Signal& signal, const struct signalfd_siginfo*)
26 {
27 printf("Exiting\n");
28 signal.get_event().exit(0);
29 }
30
main(int argc,char * argv[])31 int main(int argc, char* argv[])
32 {
33 if (argc != 2)
34 {
35 fprintf(stderr, "Usage: %s [seconds]\n", argv[0]);
36 return 1;
37 }
38
39 unsigned interval = std::stoul(argv[1]);
40 fprintf(stderr, "Beating every %u seconds\n", interval);
41
42 auto event = Event::get_default();
43 Timer timer(
44 event, [](Timer&) { printf("Beat\n"); },
45 std::chrono::seconds{interval});
46 stdplus::signal::block(SIGINT);
47 Signal(event, SIGINT, intCb).set_floating(true);
48 return event.loop();
49 }
50