| 1 | [](https://crates.io/crates/calloop) |
| 2 | [](https://docs.rs/calloop) |
| 3 | [](https://codecov.io/gh/Smithay/calloop) |
| 4 | |
| 5 | # calloop |
| 6 | |
| 7 | Calloop, a Callback-based Event Loop |
| 8 | |
| 9 | This crate provides an `EventLoop` type, which is a small abstraction |
| 10 | over a polling system. The main difference between this crate |
| 11 | and other traditional rust event loops is that it is based on callbacks: |
| 12 | you can register several event sources, each being associated with a callback |
| 13 | closure that will be invoked whenever the associated event source generates |
| 14 | events. |
| 15 | |
| 16 | The main target use of this event loop is thus for apps that expect to spend |
| 17 | most of their time waiting for events and wishes to do so in a cheap and convenient |
| 18 | way. It is not meant for large scale high performance IO. |
| 19 | |
| 20 | ### How to use it |
| 21 | |
| 22 | Below is a quick usage example of calloop. For a more in-depth tutorial, see |
| 23 | the [calloop book](https://smithay.github.io/calloop). |
| 24 | |
| 25 | For simple uses, you can just add event sources with callbacks to the event |
| 26 | loop. For example, here's a runnable program that exits after five seconds: |
| 27 | |
| 28 | ```rust |
| 29 | use calloop::{timer::{Timer, TimeoutAction}, EventLoop, LoopSignal}; |
| 30 | |
| 31 | fn main() { |
| 32 | // Create the event loop. The loop is parameterised by the kind of shared |
| 33 | // data you want the callbacks to use. In this case, we want to be able to |
| 34 | // stop the loop when the timer fires, so we provide the loop with a |
| 35 | // LoopSignal, which has the ability to stop the loop from within events. We |
| 36 | // just annotate the type here; the actual data is provided later in the |
| 37 | // run() call. |
| 38 | let mut event_loop: EventLoop<LoopSignal> = |
| 39 | EventLoop::try_new().expect("Failed to initialize the event loop!"); |
| 40 | |
| 41 | // Retrieve a handle. It is used to insert new sources into the event loop |
| 42 | // It can be cloned, allowing you to insert sources from within source |
| 43 | // callbacks. |
| 44 | let handle = event_loop.handle(); |
| 45 | |
| 46 | // Create our event source, a timer, that will expire in 2 seconds |
| 47 | let source = Timer::from_duration(std::time::Duration::from_secs(2)); |
| 48 | |
| 49 | // Inserting an event source takes this general form. It can also be done |
| 50 | // from within the callback of another event source. |
| 51 | handle |
| 52 | .insert_source( |
| 53 | // a type which implements the EventSource trait |
| 54 | source, |
| 55 | // a callback that is invoked whenever this source generates an event |
| 56 | |event, _metadata, shared_data| { |
| 57 | // This callback is given 3 values: |
| 58 | // - the event generated by the source (in our case, timer events are the Instant |
| 59 | // representing the deadline for which it has fired) |
| 60 | // - &mut access to some metadata, specific to the event source (in our case, a |
| 61 | // timer handle) |
| 62 | // - &mut access to the global shared data that was passed to EventLoop::run or |
| 63 | // EventLoop::dispatch (in our case, a LoopSignal object to stop the loop) |
| 64 | // |
| 65 | // The return type is just () because nothing uses it. Some |
| 66 | // sources will expect a Result of some kind instead. |
| 67 | println!("Timeout for {:?} expired!", event); |
| 68 | // notify the event loop to stop running using the signal in the shared data |
| 69 | // (see below) |
| 70 | shared_data.stop(); |
| 71 | // The timer event source requires us to return a TimeoutAction to |
| 72 | // specify if the timer should be rescheduled. In our case we just drop it. |
| 73 | TimeoutAction::Drop |
| 74 | }, |
| 75 | ) |
| 76 | .expect("Failed to insert event source!"); |
| 77 | |
| 78 | // Create the shared data for our loop. |
| 79 | let mut shared_data = event_loop.get_signal(); |
| 80 | |
| 81 | // Actually run the event loop. This will dispatch received events to their |
| 82 | // callbacks, waiting at most 20ms for new events between each invocation of |
| 83 | // the provided callback (pass None for the timeout argument if you want to |
| 84 | // wait indefinitely between events). |
| 85 | // |
| 86 | // This is where we pass the *value* of the shared data, as a mutable |
| 87 | // reference that will be forwarded to all your callbacks, allowing them to |
| 88 | // share some state |
| 89 | event_loop |
| 90 | .run( |
| 91 | std::time::Duration::from_millis(20), |
| 92 | &mut shared_data, |
| 93 | |_shared_data| { |
| 94 | // Finally, this is where you can insert the processing you need |
| 95 | // to do do between each waiting event eg. drawing logic if |
| 96 | // you're doing a GUI app. |
| 97 | }, |
| 98 | ) |
| 99 | .expect("Error during event loop!"); |
| 100 | } |
| 101 | ``` |
| 102 | |
| 103 | ### Event source types |
| 104 | |
| 105 | The event loop is backed by an OS provided polling selector (epoll on Linux). |
| 106 | |
| 107 | This crate also provide some adapters for common event sources such as: |
| 108 | |
| 109 | - MPSC channels |
| 110 | - Timers |
| 111 | - unix signals on Linux |
| 112 | |
| 113 | As well as generic objects backed by file descriptors. |
| 114 | |
| 115 | It is also possible to insert "idle" callbacks. These callbacks represent computations that |
| 116 | need to be done at some point, but are not as urgent as processing the events. These callbacks |
| 117 | are stored and then executed during `EventLoop::dispatch`, once all events from the sources |
| 118 | have been processed. |
| 119 | |
| 120 | ### Async/Await compatibility |
| 121 | |
| 122 | `calloop` can be used with futures, both as an executor and for monitoring Async IO. |
| 123 | |
| 124 | Activating the `executor` cargo feature will add the `futures` module, which provides |
| 125 | a future executor that can be inserted into an `EventLoop` as yet another `EventSource`. |
| 126 | |
| 127 | IO objects can be made Async-aware via the `LoopHandle::adapt_io` method. Waking up the |
| 128 | futures using these objects is handled by the associated `EventLoop` directly. |
| 129 | |
| 130 | ### Custom event sources |
| 131 | |
| 132 | You can create custom event sources can will be inserted in the event loop by |
| 133 | implementing the `EventSource` trait. This can be done either directly from the file |
| 134 | descriptors of your source of interest, or by wrapping an other event source and further |
| 135 | processing its events. An `EventSource` can register more than one file descriptor and |
| 136 | aggregate them. |
| 137 | |
| 138 | ### Platforms support |
| 139 | |
| 140 | Currently, calloop is tested on Linux, FreeBSD and macOS. |
| 141 | |
| 142 | The following platforms are also enabled at compile time but not tested: Android, NetBSD, |
| 143 | OpenBSD, DragonFlyBSD. |
| 144 | |
| 145 | Those platforms *should* work based on the fact that they have the same polling mechanism as |
| 146 | tested platforms, but some subtle bugs might still occur. |
| 147 | |
| 148 | License: MIT |