Skip to content

Commit

Permalink
glutin-winit: bump winit to 0.30.0
Browse files Browse the repository at this point in the history
Co-Authored-By: marc2332 <[email protected]>
Co-Authored-By: Kirill Chibisov <[email protected]>
Co-Authored-By: Marijn Suijten <[email protected]>
  • Loading branch information
4 people committed Jun 6, 2024
1 parent c68d98f commit aa67fca
Show file tree
Hide file tree
Showing 9 changed files with 365 additions and 222 deletions.
5 changes: 5 additions & 0 deletions glutin-winit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Unreleased

- **Breaking:** Update _winit_ to `0.30`. See [winit's CHANGELOG](https://github.com/rust-windowing/winit/releases/tag/v0.30.0) for more info.
- Add the `GlutinEventLoop` trait to maintain compatibility with the now
deprecated `EventLoop` but also support the new `ActiveEventLoop`.
- Update `DisplayBuilder` to use `WindowAttributes` instead of `WindowBuilder`.

# Version 0.4.2

- **Breaking:** Update _glutin_ to `0.31.0`. See [glutin's CHANGELOG](https://github.com/rust-windowing/glutin/releases/tag/v0.31.0) for more info.
Expand Down
2 changes: 1 addition & 1 deletion glutin-winit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ wayland = ["glutin/wayland", "winit/wayland"]
[dependencies]
glutin = { version = "0.31.0", path = "../glutin", default-features = false }
raw-window-handle = "0.6"
winit = { version = "0.29.2", default-features = false, features = ["rwh_06"] }
winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] }

[build-dependencies]
cfg_aliases = "0.2.1"
44 changes: 44 additions & 0 deletions glutin-winit/src/event_loop.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use raw_window_handle::{DisplayHandle, HandleError, HasDisplayHandle};
use winit::error::OsError;
use winit::event_loop::{ActiveEventLoop, EventLoop};
use winit::window::{Window, WindowAttributes};

use crate::private::Sealed;

/// [`ActiveEventLoop`] is the recommended way to interact with the event
/// loop, but for compatibility purposes [`EventLoop`] is also supported
/// although not recommended anymore as it has been deprecated by Winit.
pub trait GlutinEventLoop: Sealed {
/// Create the window.
///
/// See [`ActiveEventLoop::create_window`] for details.
fn create_window(&self, window_attributes: WindowAttributes) -> Result<Window, OsError>;

/// Get a handle to the display controller of the windowing system.
fn glutin_display_handle(&self) -> Result<DisplayHandle<'_>, HandleError>;
}

impl Sealed for ActiveEventLoop {}

impl GlutinEventLoop for ActiveEventLoop {
fn create_window(&self, window_attributes: WindowAttributes) -> Result<Window, OsError> {
self.create_window(window_attributes)
}

fn glutin_display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
self.display_handle()
}
}

impl<T> Sealed for EventLoop<T> {}

impl<T> GlutinEventLoop for EventLoop<T> {
#[allow(deprecated)]
fn create_window(&self, window_attributes: WindowAttributes) -> Result<Window, OsError> {
self.create_window(window_attributes)
}

fn glutin_display_handle(&self) -> Result<DisplayHandle<'_>, HandleError> {
self.display_handle()
}
}
70 changes: 39 additions & 31 deletions glutin-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
#![deny(missing_docs)]
#![cfg_attr(clippy, deny(warnings))]

mod event_loop;
mod window;

use event_loop::GlutinEventLoop;
pub use window::GlWindow;

use std::error::Error;
Expand All @@ -23,19 +25,25 @@ use glutin::prelude::*;
#[cfg(wgl_backend)]
use raw_window_handle::HasWindowHandle;

use raw_window_handle::{HasDisplayHandle, RawWindowHandle};
use raw_window_handle::RawWindowHandle;
use winit::error::OsError;
use winit::event_loop::EventLoopWindowTarget;
use winit::window::{Window, WindowBuilder};
use winit::window::{Window, WindowAttributes};

#[cfg(glx_backend)]
use winit::platform::x11::register_xlib_error_hook;
#[cfg(x11_platform)]
use winit::platform::x11::WindowBuilderExtX11;
use winit::platform::x11::WindowAttributesExtX11;

#[cfg(all(not(egl_backend), not(glx_backend), not(wgl_backend), not(cgl_backend)))]
compile_error!("Please select at least one api backend");

pub(crate) mod private {
/// Prevent traits from being implemented downstream, since those are used
/// purely for documentation organization and simplify platform api
/// implementation maintenance.
pub trait Sealed {}
}

/// The helper to perform [`Display`] creation and OpenGL platform
/// bootstrapping with the help of [`winit`] with little to no platform specific
/// code.
Expand All @@ -50,7 +58,7 @@ compile_error!("Please select at least one api backend");
#[derive(Default, Debug, Clone)]
pub struct DisplayBuilder {
preference: ApiPreference,
window_builder: Option<WindowBuilder>,
window_attributes: Option<WindowAttributes>,
}

impl DisplayBuilder {
Expand All @@ -65,30 +73,30 @@ impl DisplayBuilder {
self
}

/// The window builder to use when building a window.
/// The window attributes to use when building a window.
///
/// By default no window is created.
pub fn with_window_builder(mut self, window_builder: Option<WindowBuilder>) -> Self {
self.window_builder = window_builder;
pub fn with_window_attributes(mut self, window_attributes: Option<WindowAttributes>) -> Self {
self.window_attributes = window_attributes;
self
}

/// Initialize the OpenGL platform and create a compatible window to use
/// with it when the [`WindowBuilder`] was passed with
/// [`Self::with_window_builder`]. It's optional, since on some
/// with it when the [`WindowAttributes`] was passed with
/// [`Self::with_window_attributes()`]. It's optional, since on some
/// platforms like `Android` it is not available early on, so you want to
/// find configuration and later use it with the [`finalize_window`].
/// But if you don't care about such platform you can always pass
/// [`WindowBuilder`].
/// [`WindowAttributes`].
///
/// # Api-specific
///
/// **WGL:** - [`WindowBuilder`] **must** be passed in
/// [`Self::with_window_builder`] if modern OpenGL(ES) is desired,
/// **WGL:** - [`WindowAttributes`] **must** be passed in
/// [`Self::with_window_attributes()`] if modern OpenGL(ES) is desired,
/// otherwise only builtin functions like `glClear` will be available.
pub fn build<T, Picker>(
pub fn build<Picker>(
mut self,
window_target: &EventLoopWindowTarget<T>,
event_loop: &impl GlutinEventLoop,
template_builder: ConfigTemplateBuilder,
config_picker: Picker,
) -> Result<(Option<Window>, Config), Box<dyn Error>>
Expand All @@ -97,8 +105,8 @@ impl DisplayBuilder {
{
// XXX with WGL backend window should be created first.
#[cfg(wgl_backend)]
let window = if let Some(wb) = self.window_builder.take() {
Some(wb.build(window_target)?)
let window = if let Some(wa) = self.window_attributes.take() {
Some(event_loop.create_window(wa)?)
} else {
None
};
Expand All @@ -111,7 +119,7 @@ impl DisplayBuilder {
#[cfg(not(wgl_backend))]
let raw_window_handle = None;

let gl_display = create_display(window_target, self.preference, raw_window_handle)?;
let gl_display = create_display(event_loop, self.preference, raw_window_handle)?;

// XXX the native window must be passed to config picker when WGL is used
// otherwise very limited OpenGL features will be supported.
Expand All @@ -130,8 +138,8 @@ impl DisplayBuilder {
};

#[cfg(not(wgl_backend))]
let window = if let Some(wb) = self.window_builder.take() {
Some(finalize_window(window_target, wb, &gl_config)?)
let window = if let Some(wa) = self.window_attributes.take() {
Some(finalize_window(event_loop, wa, &gl_config)?)
} else {
None
};
Expand All @@ -140,8 +148,8 @@ impl DisplayBuilder {
}
}

fn create_display<T>(
window_target: &EventLoopWindowTarget<T>,
fn create_display(
event_loop: &impl GlutinEventLoop,
_api_preference: ApiPreference,
_raw_window_handle: Option<RawWindowHandle>,
) -> Result<Display, Box<dyn Error>> {
Expand Down Expand Up @@ -173,7 +181,7 @@ fn create_display<T>(
ApiPreference::FallbackEgl => DisplayApiPreference::WglThenEgl(_raw_window_handle),
};

let handle = window_target.display_handle()?.as_raw();
let handle = event_loop.glutin_display_handle()?.as_raw();
unsafe { Ok(Display::new(handle, _preference)?) }
}

Expand All @@ -183,24 +191,24 @@ fn create_display<T>(
///
/// [`Window`]: winit::window::Window
/// [`Config`]: glutin::config::Config
pub fn finalize_window<T>(
window_target: &EventLoopWindowTarget<T>,
mut builder: WindowBuilder,
pub fn finalize_window(
event_loop: &impl GlutinEventLoop,
mut attributes: WindowAttributes,
gl_config: &Config,
) -> Result<Window, OsError> {
// Disable transparency if the end config doesn't support it.
if gl_config.supports_transparency() == Some(false) {
builder = builder.with_transparent(false);
attributes = attributes.with_transparent(false);
}

#[cfg(x11_platform)]
let builder = if let Some(x11_visual) = gl_config.x11_visual() {
builder.with_x11_visual(x11_visual.visual_id() as _)
let attributes = if let Some(x11_visual) = gl_config.x11_visual() {
attributes.with_x11_visual(x11_visual.visual_id() as _)
} else {
builder
attributes
};

builder.build(window_target)
event_loop.create_window(attributes)
}

/// Simplified version of the [`DisplayApiPreference`] which is used to simplify
Expand Down
4 changes: 2 additions & 2 deletions glutin_examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ glutin = { path = "../glutin", default-features = false }
glutin-winit = { path = "../glutin-winit", default-features = false }
png = { version = "0.17.6", optional = true }
raw-window-handle = "0.6"
winit = { version = "0.29.2", default-features = false, features = ["rwh_05"] }
winit = { version = "0.30.0", default-features = false, features = ["rwh_06"] }

[target.'cfg(target_os = "android")'.dependencies]
winit = { version = "0.29.2", default-features = false, features = ["android-native-activity", "rwh_06"] }
winit = { version = "0.30.0", default-features = false, features = ["android-native-activity", "rwh_06"] }

[build-dependencies]
gl_generator = "0.14"
Expand Down
4 changes: 2 additions & 2 deletions glutin_examples/examples/android.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#![cfg(android_platform)]

use winit::event_loop::EventLoopBuilder;
use winit::event_loop::EventLoop;
use winit::platform::android::EventLoopBuilderExtAndroid;

#[no_mangle]
fn android_main(app: winit::platform::android::activity::AndroidApp) {
let event_loop = EventLoopBuilder::new().with_android_app(app).build().unwrap();
let event_loop = EventLoop::builder().with_android_app(app).build().unwrap();
glutin_examples::main(event_loop).unwrap()
}
Loading

0 comments on commit aa67fca

Please sign in to comment.