1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use std::cell::RefCell;
use std::io::{Error as IoError, Result as IoResult};
use std::os::raw::{c_int, c_void};
use std::os::unix::io::RawFd;
use std::rc::Rc;
use std::sync::Arc;
use std::sync::atomic;

use wayland_commons::{downcast_impl, Implementation};

use display::DisplayInner;
use sources::{FdEvent, FdInterest, IdleSource, SignalEvent, Source, TimerEvent};

#[cfg(feature = "native_lib")]
use wayland_sys::server::*;

pub(crate) struct EventLoopInner {
    #[cfg(feature = "native_lib")]
    wlevl: *mut wl_event_loop,
    pub(crate) inner: Option<Rc<DisplayInner>>,
}

/// An event loop
///
/// This is an event loop primitive provided by the wayland C libraries.
/// It is notably used for processing messages from the different clients
/// of your server, but additionnal event sources can be associated to it.
///
/// You can also create other event loops (for a multithreaded server for example),
/// however the wayland clients can only be processed from the original event loop
/// created at the same time as the display.
///
/// The event loops *cannot* be moved accross threads, so make sure you create them
/// on the thread you want to use them.
pub struct EventLoop {
    // EventLoop is *not* Send
    inner: Rc<EventLoopInner>,
    stop_signal: Arc<atomic::AtomicBool>,
}

/// An event loop token
///
/// This token allows some manipulations of the event loop, mainly
/// inserting new event sources in it.
///
/// These token are light and clone-able, allowing easy access to these
/// functions without needing to share access to the main `EventLoop` object.
#[derive(Clone)]
pub struct LoopToken {
    pub(crate) inner: Rc<EventLoopInner>,
}

/// An event loop signal
///
/// This handle can be cloned and be send accross threads, and allows you to
/// signal the event loop to stop running if you use the `EventLoop::run()`
/// method.
pub struct LoopSignal {
    inner: Arc<atomic::AtomicBool>,
}

impl LoopSignal {
    /// Signal the event loop to stop running
    pub fn stop(&self) {
        self.inner.store(true, atomic::Ordering::Release);
    }
}

impl EventLoop {
    /// Create a new event loop
    pub fn new() -> EventLoop {
        #[cfg(not(feature = "native_lib"))]
        {
            unimplemented!()
        }
        #[cfg(feature = "native_lib")]
        {
            let ptr = unsafe { ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_event_loop_create,) };
            EventLoop {
                inner: Rc::new(EventLoopInner {
                    wlevl: ptr,
                    inner: None,
                }),
                stop_signal: Arc::new(atomic::AtomicBool::new(false)),
            }
        }
    }

    #[cfg(feature = "native_lib")]
    pub(crate) unsafe fn display_new(disp_inner: Rc<DisplayInner>, ptr: *mut wl_event_loop) -> EventLoop {
        EventLoop {
            inner: Rc::new(EventLoopInner {
                wlevl: ptr,
                inner: Some(disp_inner),
            }),
            stop_signal: Arc::new(atomic::AtomicBool::new(false)),
        }
    }

    /// Retrieve a `LoopToken` associated to this event loop
    pub fn token(&self) -> LoopToken {
        LoopToken {
            inner: self.inner.clone(),
        }
    }

    /// Retrieve a `LoopSignal` associated to this event loop
    pub fn signal(&self) -> LoopSignal {
        LoopSignal {
            inner: self.stop_signal.clone(),
        }
    }

    /// Dispatch pending requests to their respective handlers
    ///
    /// If no request is pending, will block at most `timeout` ms if specified,
    /// or indefinitely if `timeout` is `None`.
    ///
    /// Returns the number of requests dispatched or an error.
    pub fn dispatch(&mut self, timeout: Option<u32>) -> IoResult<u32> {
        #[cfg(feature = "native_lib")]
        {
            use std::i32;
            let timeout = match timeout {
                None => -1,
                Some(v) if v >= (i32::MAX as u32) => i32::MAX,
                Some(v) => (v as i32),
            };
            let ret = unsafe {
                ffi_dispatch!(
                    WAYLAND_SERVER_HANDLE,
                    wl_event_loop_dispatch,
                    self.inner.wlevl,
                    timeout
                )
            };
            if ret >= 0 {
                Ok(ret as u32)
            } else {
                Err(IoError::last_os_error())
            }
        }
    }

    /// Runs the event loop
    ///
    /// This method will call repetitively the dispatch method,
    /// until one of the handlers call the `stop` method of an associated
    /// `LoopSignal`.
    ///
    /// If this event loop is attached to a display, it will also
    /// flush the events to the clients between two calls to
    /// `dispatch()`.
    ///
    /// Note that this method will block indefinitely on waiting events,
    /// as such, if you need to avoid a complete block even if no events
    /// are received, you should use the `dispatch()` method instead and
    /// set a timeout.
    pub fn run(&mut self) -> IoResult<()> {
        self.stop_signal.store(false, atomic::Ordering::Release);
        loop {
            if let Some(ref display_inner) = self.inner.inner {
                unsafe {
                    ffi_dispatch!(
                        WAYLAND_SERVER_HANDLE,
                        wl_display_flush_clients,
                        display_inner.ptr
                    )
                };
            }
            self.dispatch(None)?;
            if self.stop_signal.load(atomic::Ordering::Acquire) {
                return Ok(());
            }
        }
    }
}

impl LoopToken {
    /// Add a File Descriptor event source to this event loop
    ///
    /// The interest in read/write capability for this FD must be provided
    /// (and can be changed afterwards using the returned object), and the
    /// associated implementation will be called whenever these capabilities are
    /// satisfied, during the dispatching of this event loop.
    pub fn add_fd_event_source<Impl>(
        &self,
        fd: RawFd,
        interest: FdInterest,
        implementation: Impl,
    ) -> Result<Source<FdEvent>, (IoError, Impl)>
    where
        Impl: Implementation<(), FdEvent> + 'static,
    {
        let data = Box::new(Box::new(implementation) as Box<Implementation<(), FdEvent>>);
        let ret = unsafe {
            ffi_dispatch!(
                WAYLAND_SERVER_HANDLE,
                wl_event_loop_add_fd,
                self.inner.wlevl,
                fd,
                interest.bits(),
                ::sources::event_source_fd_dispatcher,
                &*data as *const _ as *mut c_void
            )
        };
        if ret.is_null() {
            Err((
                IoError::last_os_error(),
                *(downcast_impl(*data).map_err(|_| ()).unwrap()),
            ))
        } else {
            Ok(Source::make(ret, data))
        }
    }

    /// Add a timer event source to this event loop
    ///
    /// It is a countdown, which can be reset using the struct
    /// returned by this function. When the countdown reaches 0,
    /// the implementation is called in the dispatching of
    /// this event loop.
    pub fn add_timer_event_source<Impl>(
        &self,
        implementation: Impl,
    ) -> Result<Source<TimerEvent>, (IoError, Impl)>
    where
        Impl: Implementation<(), TimerEvent> + 'static,
    {
        let data = Box::new(Box::new(implementation) as Box<Implementation<(), TimerEvent>>);
        let ret = unsafe {
            ffi_dispatch!(
                WAYLAND_SERVER_HANDLE,
                wl_event_loop_add_timer,
                self.inner.wlevl,
                ::sources::event_source_timer_dispatcher,
                &*data as *const _ as *mut c_void
            )
        };
        if ret.is_null() {
            Err((
                IoError::last_os_error(),
                *(downcast_impl(*data).map_err(|_| ()).unwrap()),
            ))
        } else {
            Ok(Source::make(ret, data))
        }
    }

    /// Add a signal event source to this event loop
    ///
    /// This will listen for a given unix signal (by setting up
    /// a signalfd for it) and call the implementation whenever
    /// the program receives this signal. Calls are made during the
    /// dispatching of this event loop.
    pub fn add_signal_event_source<Impl>(
        &self,
        signal: ::nix::sys::signal::Signal,
        implementation: Impl,
    ) -> Result<Source<SignalEvent>, (IoError, Impl)>
    where
        Impl: Implementation<(), SignalEvent> + 'static,
    {
        let data = Box::new(Box::new(implementation) as Box<Implementation<(), SignalEvent>>);
        let ret = unsafe {
            ffi_dispatch!(
                WAYLAND_SERVER_HANDLE,
                wl_event_loop_add_signal,
                self.inner.wlevl,
                signal as c_int,
                ::sources::event_source_signal_dispatcher,
                &*data as *const _ as *mut c_void
            )
        };
        if ret.is_null() {
            Err((
                IoError::last_os_error(),
                *(downcast_impl(*data).map_err(|_| ()).unwrap()),
            ))
        } else {
            Ok(Source::make(ret, data))
        }
    }

    /// Add an idle event source to this event loop
    ///
    /// This is a kind of "defer this computation for when there is nothing else to do".
    ///
    /// The provided implementation callback will be called when the event loop has finished
    /// processing all the pending I/O. This callback will be fired exactly once the first
    /// time this condition is met.
    ///
    /// You can cancel or retrieve the implementation after it has fired using the
    /// returned `IdleEventSource`.
    pub fn add_idle_event_source<Impl>(&self, implementation: Impl) -> IdleSource
    where
        Impl: Implementation<(), ()> + 'static,
    {
        let data = Rc::new(RefCell::new((
            Box::new(implementation) as Box<Implementation<(), ()>>,
            false,
        )));
        let ret = unsafe {
            ffi_dispatch!(
                WAYLAND_SERVER_HANDLE,
                wl_event_loop_add_idle,
                self.inner.wlevl,
                ::sources::event_source_idle_dispatcher,
                Rc::into_raw(data.clone()) as *mut _
            )
        };
        IdleSource::make(ret, data)
    }

    /// Checks whether given resource is indeed linked to this
    /// event loop
    #[cfg(feature = "native_lib")]
    pub(crate) unsafe fn matches(&self, resource_ptr: *mut wl_resource) -> bool {
        let client_ptr = ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_resource_get_client, resource_ptr);
        let display_ptr = ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_client_get_display, client_ptr);
        let event_loop_ptr = ffi_dispatch!(
            WAYLAND_SERVER_HANDLE,
            wl_display_get_event_loop,
            display_ptr
        );
        return event_loop_ptr == self.inner.wlevl;
    }
}

impl Drop for EventLoopInner {
    fn drop(&mut self) {
        #[cfg(feature = "native_lib")]
        {
            if self.inner.is_none() {
                // only destroy the event_loop if it's not the one from the display
                unsafe {
                    ffi_dispatch!(WAYLAND_SERVER_HANDLE, wl_event_loop_destroy, self.wlevl);
                }
            }
        }
    }
}