| 1 | use calloop::{ |
| 2 | timer::{TimeoutAction, Timer}, |
| 3 | EventLoop, LoopSignal, |
| 4 | }; |
| 5 | |
| 6 | fn main() { |
| 7 | // Create the event loop. The loop is parameterised by the kind of shared |
| 8 | // data you want the callbacks to use. In this case, we want to be able to |
| 9 | // stop the loop when the timer fires, so we provide the loop with a |
| 10 | // LoopSignal, which has the ability to stop the loop from within events. We |
| 11 | // just annotate the type here; the actual data is provided later in the |
| 12 | // run() call. |
| 13 | let mut event_loop: EventLoop<LoopSignal> = |
| 14 | EventLoop::try_new().expect("Failed to initialize the event loop!"); |
| 15 | |
| 16 | // Retrieve a handle. It is used to insert new sources into the event loop |
| 17 | // It can be cloned, allowing you to insert sources from within source |
| 18 | // callbacks. |
| 19 | let handle = event_loop.handle(); |
| 20 | |
| 21 | // Create our event source, a timer, that will expire in 2 seconds |
| 22 | let source = Timer::from_duration(std::time::Duration::from_secs(2)); |
| 23 | |
| 24 | // Inserting an event source takes this general form. It can also be done |
| 25 | // from within the callback of another event source. |
| 26 | handle |
| 27 | .insert_source( |
| 28 | // a type which implements the EventSource trait |
| 29 | source, |
| 30 | // a callback that is invoked whenever this source generates an event |
| 31 | |event, _metadata, shared_data| { |
| 32 | // This callback is given 3 values: |
| 33 | // - the event generated by the source (in our case, timer events are the Instant |
| 34 | // representing the deadline for which it has fired) |
| 35 | // - &mut access to some metadata, specific to the event source (in our case, a |
| 36 | // timer handle) |
| 37 | // - &mut access to the global shared data that was passed to EventLoop::run or |
| 38 | // EventLoop::dispatch (in our case, a LoopSignal object to stop the loop) |
| 39 | // |
| 40 | // The return type is just () because nothing uses it. Some |
| 41 | // sources will expect a Result of some kind instead. |
| 42 | println!("Timeout for {:?} expired!", event); |
| 43 | // notify the event loop to stop running using the signal in the shared data |
| 44 | // (see below) |
| 45 | shared_data.stop(); |
| 46 | // The timer event source requires us to return a TimeoutAction to |
| 47 | // specify if the timer should be rescheduled. In our case we just drop it. |
| 48 | TimeoutAction::Drop |
| 49 | }, |
| 50 | ) |
| 51 | .expect("Failed to insert event source!"); |
| 52 | |
| 53 | // Create the shared data for our loop. |
| 54 | let mut shared_data = event_loop.get_signal(); |
| 55 | |
| 56 | // Actually run the event loop. This will dispatch received events to their |
| 57 | // callbacks, waiting at most 20ms for new events between each invocation of |
| 58 | // the provided callback (pass None for the timeout argument if you want to |
| 59 | // wait indefinitely between events). |
| 60 | // |
| 61 | // This is where we pass the *value* of the shared data, as a mutable |
| 62 | // reference that will be forwarded to all your callbacks, allowing them to |
| 63 | // share some state |
| 64 | event_loop |
| 65 | .run( |
| 66 | std::time::Duration::from_millis(20), |
| 67 | &mut shared_data, |
| 68 | |_shared_data| { |
| 69 | // Finally, this is where you can insert the processing you need |
| 70 | // to do do between each waiting event eg. drawing logic if |
| 71 | // you're doing a GUI app. |
| 72 | }, |
| 73 | ) |
| 74 | .expect("Error during event loop!"); |
| 75 | } |