William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 1 | #include <chrono> |
| 2 | #include <cstdio> |
| 3 | #include <sdeventplus/clock.hpp> |
| 4 | #include <sdeventplus/event.hpp> |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 5 | #include <sdeventplus/source/signal.hpp> |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 6 | #include <sdeventplus/source/time.hpp> |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 7 | #include <stdplus/signal.hpp> |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 8 | #include <string> |
| 9 | #include <utility> |
| 10 | |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 11 | using sdeventplus::Event; |
William A. Kennington III | 406b86e | 2018-09-25 15:23:00 -0700 | [diff] [blame] | 12 | using sdeventplus::source::Enabled; |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 13 | using sdeventplus::source::Signal; |
William A. Kennington III | 406b86e | 2018-09-25 15:23:00 -0700 | [diff] [blame] | 14 | |
| 15 | constexpr auto clockId = sdeventplus::ClockId::RealTime; |
| 16 | using Clock = sdeventplus::Clock<clockId>; |
| 17 | using Time = sdeventplus::source::Time<clockId>; |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 18 | |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 19 | void intCb(Signal& signal, const struct signalfd_siginfo*) |
| 20 | { |
| 21 | printf("Exiting\n"); |
| 22 | signal.get_event().exit(0); |
| 23 | } |
| 24 | |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 25 | int main(int argc, char* argv[]) |
| 26 | { |
| 27 | if (argc != 2) |
| 28 | { |
| 29 | fprintf(stderr, "Usage: %s [seconds]\n", argv[0]); |
| 30 | return 1; |
| 31 | } |
| 32 | |
| 33 | unsigned interval = std::stoul(argv[1]); |
| 34 | fprintf(stderr, "Beating every %u seconds\n", interval); |
| 35 | |
| 36 | auto event = Event::get_default(); |
William A. Kennington III | 406b86e | 2018-09-25 15:23:00 -0700 | [diff] [blame] | 37 | auto hbFunc = [interval](Time& source, Time::TimePoint time) { |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 38 | printf("Beat\n"); |
William A. Kennington III | 406b86e | 2018-09-25 15:23:00 -0700 | [diff] [blame] | 39 | |
| 40 | // Time sources are oneshot and are based on an absolute time |
| 41 | // we need to reconfigure the time source to go off again after the |
| 42 | // configured interval and re-enable it. |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 43 | source.set_time(time + std::chrono::seconds{interval}); |
William A. Kennington III | 406b86e | 2018-09-25 15:23:00 -0700 | [diff] [blame] | 44 | source.set_enabled(Enabled::OneShot); |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 45 | }; |
William A. Kennington III | 406b86e | 2018-09-25 15:23:00 -0700 | [diff] [blame] | 46 | Time time(event, Clock(event).now(), std::chrono::seconds{1}, |
| 47 | std::move(hbFunc)); |
William A. Kennington III | 2584f9d | 2019-03-23 21:05:48 -0700 | [diff] [blame] | 48 | stdplus::signal::block(SIGINT); |
| 49 | Signal signal(event, SIGINT, intCb); |
William A. Kennington III | 7e2e60e | 2018-07-17 14:40:14 -0700 | [diff] [blame] | 50 | return event.loop(); |
| 51 | } |