gpu_display/sys/
linux.rs

1// Copyright 2022 The ChromiumOS Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use std::path::Path;
6
7use base::AsRawDescriptor;
8use base::RawDescriptor;
9use base::WaitContext;
10
11use crate::gpu_display_wl::DisplayWl;
12use crate::DisplayEventToken;
13use crate::DisplayT;
14use crate::EventDevice;
15use crate::GpuDisplay;
16use crate::GpuDisplayExt;
17use crate::GpuDisplayResult;
18
19pub(crate) trait UnixDisplayT: DisplayT {}
20
21impl GpuDisplayExt for GpuDisplay {
22    fn import_event_device(&mut self, event_device: EventDevice) -> GpuDisplayResult<u32> {
23        let new_event_device_id = self.next_id;
24
25        self.wait_ctx.add(
26            &event_device,
27            DisplayEventToken::EventDevice {
28                event_device_id: new_event_device_id,
29            },
30        )?;
31        self.event_devices.insert(new_event_device_id, event_device);
32
33        self.next_id += 1;
34        Ok(new_event_device_id)
35    }
36
37    fn handle_event_device(&mut self, event_device_id: u32) {
38        if let Some(event_device) = self.event_devices.get(&event_device_id) {
39            // TODO(zachr): decode the event and forward to the device.
40            let _ = event_device.recv_event_encoded();
41        }
42    }
43}
44
45pub trait UnixGpuDisplayExt {
46    /// Opens a fresh connection to the compositor.
47    fn open_wayland<P: AsRef<Path>>(wayland_path: Option<P>) -> GpuDisplayResult<GpuDisplay>;
48}
49
50impl UnixGpuDisplayExt for GpuDisplay {
51    fn open_wayland<P: AsRef<Path>>(wayland_path: Option<P>) -> GpuDisplayResult<GpuDisplay> {
52        let display = match wayland_path {
53            Some(s) => DisplayWl::new(Some(s.as_ref()))?,
54            None => DisplayWl::new(None)?,
55        };
56
57        let wait_ctx = WaitContext::new()?;
58        wait_ctx.add(&display, DisplayEventToken::Display)?;
59
60        Ok(GpuDisplay {
61            inner: Box::new(display),
62            next_id: 1,
63            event_devices: Default::default(),
64            surfaces: Default::default(),
65            wait_ctx,
66        })
67    }
68}
69
70impl AsRawDescriptor for GpuDisplay {
71    fn as_raw_descriptor(&self) -> RawDescriptor {
72        self.wait_ctx.as_raw_descriptor()
73    }
74}