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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
use std::convert::{TryFrom, TryInto};
use std::os::raw::c_int;
use std::sync::Arc;
use std::time::Duration;

use derivative::Derivative;
use drm_fourcc::{DrmFormat, UnrecognizedFourcc};
use thiserror::Error;
use tokio::sync::{broadcast, mpsc, watch};
use tokio::time::sleep;

use crate::ffi::evdi_cursor_set;
use crate::prelude::*;
use crate::{ffi, OwnedLibcArray};

macro_rules! try_send {
    ($tx:expr, $val:expr) => {
        if $tx.send($val).is_err() {
            warn!("Failed to send: Channel closed");
        }
    };
}

#[derive(Derivative)]
#[derivative(Debug)]
pub struct HandleEvents {
    #[derivative(Debug = "ignore")]
    current_mode: watch::Receiver<Option<Mode>>,
    #[derivative(Debug = "ignore")]
    buffer_updates: broadcast::Sender<BufferId>,
}

impl HandleEvents {
    /// To receive a buffer update you must have been waiting before it was sent by the kernel
    #[instrument]
    pub(crate) async fn await_buffer_update(
        &self,
        id: BufferId,
        timeout: Duration,
    ) -> Result<(), AwaitEventError> {
        tokio::select! {
            _ = sleep(timeout) => {
                warn!("Timeout awaiting buffer update: {:?}", timeout);
                Err(AwaitEventError::Timeout)
            }
            result = self.await_buffer_update_unbounded(id) => result
        }
    }

    async fn await_buffer_update_unbounded(&self, id: BufferId) -> Result<(), AwaitEventError> {
        use broadcast::error::RecvError;
        let mut recv = self.buffer_updates.subscribe();
        loop {
            match recv.recv().await {
                Ok(potential) => {
                    if potential == id {
                        return Ok(());
                    }
                }
                Err(RecvError::Closed) => return Err(AwaitEventError::ChannelClosed),
                Err(RecvError::Lagged(skipped)) => {
                    warn!(skipped, "Lagged receiving buffer update");
                }
            }
        }
    }

    /// The most recent mode received, if a mode has been received.
    pub fn current_mode(&self) -> Option<Mode> {
        *self.current_mode.borrow()
    }

    /// If a mode event has been received, return it. Otherwise wait for a mode event.
    #[instrument]
    pub async fn await_mode(&self, timeout: Duration) -> Result<Mode, AwaitEventError> {
        if let Some(cached) = self.current_mode() {
            return Ok(cached);
        }

        let mut recv = self.current_mode.clone();
        tokio::select! {
            _ = sleep(timeout) => {
                warn!("Timeout awaiting mode: {:?}", timeout);
                Err(AwaitEventError::Timeout)
            },
            ret = recv.changed() => {
                if ret.is_err() {
                    error!("Mode watch channel closed");
                    Err(AwaitEventError::ChannelClosed)
                } else {
                    let mode = self.current_mode().expect("Can never be none after being changed");
                    Ok(mode)
                }
            }
        }
    }

    fn spawn_monitor(
        mut recv: mpsc::Receiver<Event>,
        current_mode_tx: watch::Sender<Option<Mode>>,
        buffer_updates_tx: broadcast::Sender<BufferId>,
    ) {
        tokio::spawn(async move {
            loop {
                match recv.recv().await {
                    Some(event) => match event {
                        Event::ModeChanged(mode) => try_send!(current_mode_tx, Some(mode)),
                        Event::UpdateReady(id) => try_send!(buffer_updates_tx, id),
                        _ => (),
                    },
                    None => {
                        warn!("Event channel closed");
                        return;
                    }
                }
            }
        });
    }

    pub(crate) fn new(recv: mpsc::Receiver<Event>) -> Self {
        let (current_mode_tx, current_mode) = watch::channel(None);
        let (buffer_updates, _) = broadcast::channel(16);

        Self::spawn_monitor(recv, current_mode_tx, buffer_updates.clone());

        Self {
            current_mode,
            buffer_updates,
        }
    }
}

#[derive(Debug, Error)]
pub enum AwaitEventError {
    #[error("Timeout")]
    Timeout,
    #[error("Event channel closed")]
    ChannelClosed,
}

#[derive(Clone, Debug)]
pub(crate) enum Event {
    DpmsModeChanged(DpmsMode),
    ModeChanged(Mode),
    /// An update for a buffer requested earlier is ready to be consumed.
    UpdateReady(BufferId),
    CrtcStateChanged(CrtcState),
    CursorChange(CursorChange),
    CursorMove(CursorMove),
    I2CRequest(DdcCiData),
}

#[derive(Debug, Copy, Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde_crate::Serialize, serde_crate::Deserialize),
    serde(crate = "serde_crate")
)]
pub enum DpmsMode {
    // Values from <https://displaylink.github.io/evdi/details/#dpms-mode-change>
    On = 0,
    Standby = 1,
    Suspend = 2,
    Off = 3,
}

impl From<c_int> for DpmsMode {
    fn from(sys: i32) -> Self {
        match sys {
            0 => DpmsMode::On,
            1 => DpmsMode::Standby,
            2 => DpmsMode::Suspend,
            3 => DpmsMode::Off,
            _ => panic!("Unexpected DPMS mode {}", sys),
        }
    }
}

#[derive(Debug, Copy, Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde_crate::Serialize, serde_crate::Deserialize),
    serde(crate = "serde_crate")
)]
pub struct Mode {
    pub width: u32,
    pub height: u32,
    /// Max updates per second.
    pub refresh_rate: u32,
    pub bits_per_pixel: u32,
    /// Based on the current source code of libevdi I believe this can only be
    /// one of the formats returned by
    /// [`drm_mode_legacy_fb_format`][legacy_fn_github], which is essentially
    /// limited to RGB-like formats (ARGB8888, XRGB8888, RGB565, and a few
    /// more).
    ///
    /// [legacy_fn_github]: https://github.com/torvalds/linux/blob/master/drivers/gpu/drm/drm_fourcc.c#L46
    pub pixel_format: Result<DrmFormat, UnrecognizedFourcc>,
}

impl Mode {
    pub fn stride(&self) -> u32 {
        self.bits_per_pixel / 8 * self.width
    }
}

impl From<ffi::evdi_mode> for Mode {
    fn from(sys: ffi::evdi_mode) -> Self {
        Self {
            width: sys.width as u32,
            height: sys.height as u32,
            refresh_rate: sys.refresh_rate as u32,
            bits_per_pixel: sys.bits_per_pixel as u32,
            pixel_format: sys.pixel_format.try_into(),
        }
    }
}

/// A value [forwarded from the kernel][u-doc].
///
/// [u-doc]: https://displaylink.github.io/evdi/details/#crtc-state-change
#[derive(Debug, Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde_crate::Serialize, serde_crate::Deserialize),
    serde(crate = "serde_crate")
)]
pub struct CrtcState(pub i32);

impl From<c_int> for CrtcState {
    fn from(sys: i32) -> Self {
        Self(sys)
    }
}

/// This notification is sent for an update of cursor buffer or shape. It is also raised when
/// cursor is enabled or disabled (when cursor is moved on and off the screen).
#[derive(Debug, Clone)]
pub struct CursorChange {
    pub enabled: bool,
    pub hotspot_x: i32,
    pub hotspot_y: i32,
    pub width: u32,
    pub height: u32,
    pub stride: u32,
    pub pixel_format: Result<DrmFormat, UnrecognizedFourcc>,
    buffer: Arc<OwnedLibcArray<u32>>,
}

impl From<ffi::evdi_cursor_set> for CursorChange {
    fn from(sys: evdi_cursor_set) -> Self {
        let buffer = unsafe { OwnedLibcArray::new(sys.buffer, sys.buffer_length as usize) };

        Self {
            enabled: sys.enabled != 0,
            hotspot_x: sys.hot_x,
            hotspot_y: sys.hot_y,
            width: sys.width,
            height: sys.height,
            stride: sys.stride,
            pixel_format: DrmFormat::try_from(sys.pixel_format),
            buffer: Arc::new(buffer),
        }
    }
}

impl CursorChange {
    pub fn buffer(&self) -> &[u32] {
        self.buffer.as_slice()
    }
}

/// A cursor position change. Raised only when cursor is positioned on virtual screen.
#[derive(Debug, Copy, Clone)]
#[cfg_attr(
    feature = "serde",
    derive(serde_crate::Serialize, serde_crate::Deserialize),
    serde(crate = "serde_crate")
)]
pub struct CursorMove {
    pub x: i32,
    pub y: i32,
}

impl From<ffi::evdi_cursor_move> for CursorMove {
    fn from(sys: ffi::evdi_cursor_move) -> Self {
        Self { x: sys.x, y: sys.y }
    }
}

/// An i2c request has been made to the DDC/CI address (0x37).
//
// The kernel module will wait for a maximum of DDCCI_TIMEOUT_MS (50ms - The default DDC request
// timeout) for a response to this request to be passed back via evdi_ddcci_response.
#[derive(Debug, Clone)]
pub struct DdcCiData {
    flags: u16,
    buffer: Arc<OwnedLibcArray<u8>>,
}

impl From<ffi::evdi_ddcci_data> for DdcCiData {
    fn from(sys: ffi::evdi_ddcci_data) -> Self {
        // Ignore address, as per docs will always be 0x37.
        let buffer = unsafe { OwnedLibcArray::new(sys.buffer, sys.buffer_length as usize) };
        Self {
            flags: sys.flags,
            buffer: Arc::new(buffer),
        }
    }
}

/// DDC/CI data notification
///
/// All documentation of DdciData is my best guess based on reading the source and googling.
/// You may find reading about [general protocol i2c][i2c] that DDC/CI data is transmitted over
/// useful.
///
/// [i2c]: <http://ww1.microchip.com/downloads/en/AppNotes/I2C-Master-Mode-30003191A.pdf>
impl DdcCiData {
    /// Indicates driver intends to read data from the virtual display
    pub fn flag_read_request(&self) -> bool {
        self.flags & Self::FLAG_READ != 0
    }

    /// Indicates driver intends to write data to the virtual display
    pub fn flag_write_request(&self) -> bool {
        self.flags == Self::FLAG_WRITE
    }

    // Truncated to 64 bytes (DDCCI_BUFFER_SIZE)
    pub fn buffer(&self) -> &[u8] {
        self.buffer.as_slice()
    }

    const FLAG_READ: u16 = 1;
    const FLAG_WRITE: u16 = 0;
}

#[cfg(test)]
mod tests {
    use drm_fourcc::DrmFormat;

    use crate::ffi::evdi_mode;
    use crate::test_common::*;

    use super::*;

    #[ltest(atest)]
    async fn can_receive_mode() {
        let handle = handle_fixture();
        let mode = handle.events.await_mode(TIMEOUT).await.unwrap();
        assert!(mode.height > 100);
    }

    #[ltest]
    fn mode_from_sample() {
        let expected = Mode {
            width: 1280,
            height: 800,
            refresh_rate: 60,
            bits_per_pixel: 32,
            pixel_format: Ok(DrmFormat::Xrgb8888),
        };

        let actual: Mode = evdi_mode {
            width: 1280,
            height: 800,
            refresh_rate: 60,
            bits_per_pixel: 32,
            pixel_format: 875713112,
        }
        .into();

        assert_eq!(format!("{:?}", actual), format!("{:?}", expected));
    }
}