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
// surfman/surfman/src/platform/unix/wayland/connection.rs
//
//! A wrapper for Wayland connections (displays).

use super::device::{Adapter, Device, NativeDevice};
use super::surface::NativeWidget;
use crate::egl;
use crate::egl::types::{EGLAttrib, EGLDisplay};
use crate::info::GLApi;
use crate::platform::generic::egl::device::EGL_FUNCTIONS;
use crate::platform::generic::egl::ffi::EGL_PLATFORM_WAYLAND_KHR;
use crate::Error;

use euclid::default::Size2D;
use std::os::raw::c_void;
use std::ptr;
use std::sync::Arc;
use wayland_sys::client::{wl_display, wl_proxy, WAYLAND_CLIENT_HANDLE};

/// A connection to the Wayland server.
#[derive(Clone)]
pub struct Connection {
    pub(crate) native_connection: Arc<NativeConnectionWrapper>,
}

pub(crate) struct NativeConnectionWrapper {
    pub(crate) egl_display: EGLDisplay,
    wayland_display: Option<*mut wl_display>,
}

/// An EGL display wrapping a Wayland display.
pub struct NativeConnection(pub EGLDisplay);

unsafe impl Send for Connection {}

impl Connection {
    /// Connects to the default Wayland server.
    #[inline]
    pub fn new() -> Result<Connection, Error> {
        unsafe {
            let wayland_display = (WAYLAND_CLIENT_HANDLE.wl_display_connect)(ptr::null());
            Connection::from_wayland_display(wayland_display, true)
        }
    }

    /// Wraps an existing EGL display in a `Connection`.
    ///
    /// The display is not retained, as there is no way to do this in the EGL API. Therefore, it is
    /// the caller's responsibility to ensure that the EGL display remains alive as long as the
    /// connection is.
    pub unsafe fn from_native_connection(
        native_connection: NativeConnection,
    ) -> Result<Connection, Error> {
        Connection::from_egl_display(native_connection.0, None)
    }

    /// Returns the underlying native connection.
    #[inline]
    pub fn native_connection(&self) -> NativeConnection {
        NativeConnection(self.native_connection.egl_display)
    }

    /// Returns the OpenGL API flavor that this connection supports (OpenGL or OpenGL ES).
    #[inline]
    pub fn gl_api(&self) -> GLApi {
        GLApi::GL
    }

    /// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
    ///
    /// This is an alias for `Connection::create_hardware_adapter()`.
    #[inline]
    pub fn create_adapter(&self) -> Result<Adapter, Error> {
        self.create_hardware_adapter()
    }

    /// Returns the "best" adapter on this system, preferring high-performance hardware adapters.
    #[inline]
    pub fn create_hardware_adapter(&self) -> Result<Adapter, Error> {
        Ok(Adapter::hardware())
    }

    /// Returns the "best" adapter on this system, preferring low-power hardware adapters.
    #[inline]
    pub fn create_low_power_adapter(&self) -> Result<Adapter, Error> {
        Ok(Adapter::low_power())
    }

    /// Returns the "best" adapter on this system, preferring software adapters.
    #[inline]
    pub fn create_software_adapter(&self) -> Result<Adapter, Error> {
        Ok(Adapter::software())
    }

    /// Opens the hardware device corresponding to the given adapter.
    ///
    /// Device handles are local to a single thread.
    #[inline]
    pub fn create_device(&self, adapter: &Adapter) -> Result<Device, Error> {
        Device::new(self, adapter)
    }

    /// Opens the hardware device corresponding to the adapter wrapped in the given native
    /// device.
    ///
    /// This is present for compatibility with other backends.
    #[inline]
    pub unsafe fn create_device_from_native_device(
        &self,
        native_device: NativeDevice,
    ) -> Result<Device, Error> {
        Device::new(self, &native_device.adapter)
    }

    unsafe fn from_wayland_display(
        wayland_display: *mut wl_display,
        is_owned: bool,
    ) -> Result<Connection, Error> {
        if wayland_display.is_null() {
            return Err(Error::ConnectionFailed);
        }

        EGL_FUNCTIONS.with(|egl| {
            let display_attributes = [egl::NONE as EGLAttrib];
            let egl_display = egl.GetPlatformDisplay(
                EGL_PLATFORM_WAYLAND_KHR,
                wayland_display as *mut c_void,
                display_attributes.as_ptr(),
            );
            if egl_display == egl::NO_DISPLAY {
                return Err(Error::DeviceOpenFailed);
            }

            let (mut egl_major_version, mut egl_minor_version) = (0, 0);
            let ok = egl.Initialize(egl_display, &mut egl_major_version, &mut egl_minor_version);
            assert_ne!(ok, egl::FALSE);

            let owned_display = if is_owned {
                Some(wayland_display)
            } else {
                None
            };
            Connection::from_egl_display(egl_display, owned_display)
        })
    }

    fn from_egl_display(
        egl_display: EGLDisplay,
        wayland_display: Option<*mut wl_display>,
    ) -> Result<Connection, Error> {
        Ok(Connection {
            native_connection: Arc::new(NativeConnectionWrapper {
                egl_display,
                wayland_display,
            }),
        })
    }

    /// Opens the display connection corresponding to the given raw display handle.
    #[cfg(feature = "sm-raw-window-handle")]
    pub fn from_raw_display_handle(
        raw_handle: raw_window_handle::RawDisplayHandle,
    ) -> Result<Connection, Error> {
        use raw_window_handle::RawDisplayHandle::Wayland;
        use raw_window_handle::WaylandDisplayHandle;
        unsafe {
            let wayland_display = match raw_handle {
                Wayland(WaylandDisplayHandle { display, .. }) => display as *mut wl_display,
                _ => return Err(Error::IncompatibleRawDisplayHandle),
            };

            Connection::from_wayland_display(wayland_display, false)
        }
    }

    /// Create a native widget from a raw pointer
    pub unsafe fn create_native_widget_from_ptr(
        &self,
        raw: *mut c_void,
        size: Size2D<i32>,
    ) -> NativeWidget {
        NativeWidget {
            wayland_surface: raw as *mut wl_proxy,
            size,
        }
    }

    /// Creates a native widget type from the given `raw_window_handle::HasRawWindowHandle`
    #[cfg(feature = "sm-raw-window-handle")]
    pub fn create_native_widget_from_raw_window_handle(
        &self,
        raw_handle: raw_window_handle::RawWindowHandle,
        window_size: Size2D<i32>,
    ) -> Result<NativeWidget, Error> {
        use raw_window_handle::RawWindowHandle::Wayland;

        let wayland_surface = match raw_handle {
            Wayland(handle) => handle.surface as *mut wl_proxy,
            _ => return Err(Error::IncompatibleNativeWidget),
        };

        Ok(NativeWidget {
            wayland_surface,
            size: window_size,
        })
    }
}

impl Drop for NativeConnectionWrapper {
    fn drop(&mut self) {
        unsafe {
            if let Some(wayland_display) = self.wayland_display {
                (WAYLAND_CLIENT_HANDLE.wl_display_disconnect)(wayland_display);
            }
        }
    }
}

impl NativeConnection {
    /// Returns the current native connection, if applicable.
    #[inline]
    pub fn current() -> Result<NativeConnection, Error> {
        unsafe {
            EGL_FUNCTIONS.with(|egl| {
                let display = egl.GetCurrentDisplay();
                if display != egl::NO_DISPLAY {
                    Ok(NativeConnection(display))
                } else {
                    Err(Error::NoCurrentConnection)
                }
            })
        }
    }
}