Skip to content

Commit

Permalink
glutin-winit: try to inject screen_id into RawDisplayHandle before cr…
Browse files Browse the repository at this point in the history
…eate_display()

- Use tonarino/winit to support creating windows on non-default screens
  • Loading branch information
strohel authored and mbernat committed Oct 29, 2024
1 parent 9c58fbb commit fa4fd24
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ members = [
"glutin_wgl_sys",
"glutin_gles2_sys",
]

[patch.crates-io]
winit = { git = "https://github.com/tonarino/winit.git", branch = "x11-screen-id" }
24 changes: 20 additions & 4 deletions glutin-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use glutin::prelude::*;
#[cfg(wgl_backend)]
use raw_window_handle::HasWindowHandle;

use raw_window_handle::RawWindowHandle;
use raw_window_handle::{RawDisplayHandle, RawWindowHandle};
use winit::error::OsError;
use winit::window::{Window, WindowAttributes};

Expand Down Expand Up @@ -111,6 +111,23 @@ impl DisplayBuilder {
None
};

#[cfg(x11_platform)]
let mut raw_display_handle = event_loop.glutin_display_handle()?.as_raw();
#[cfg(x11_platform)]
if let Some(screen_id) = self.window_attributes.as_ref().and_then(|wa| wa.x11_screen()) {
eprintln!("Injecting screen_id {screen_id} into raw_display_handle.");
match &mut raw_display_handle {
RawDisplayHandle::Xlib(xlib) => xlib.screen = screen_id,
RawDisplayHandle::Xcb(xcb) => xcb.screen = screen_id,
other => {
eprintln!("Cannot inject screen_id to RawDisplayHandle variant {other:?}.")
},
}
}

#[cfg(not(x11_platform))]
let raw_display_handle = event_loop.glutin_display_handle()?.as_raw();

#[cfg(wgl_backend)]
let raw_window_handle = window
.as_ref()
Expand All @@ -119,7 +136,7 @@ impl DisplayBuilder {
#[cfg(not(wgl_backend))]
let raw_window_handle = None;

let gl_display = create_display(event_loop, self.preference, raw_window_handle)?;
let gl_display = create_display(raw_display_handle, 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 Down Expand Up @@ -149,7 +166,7 @@ impl DisplayBuilder {
}

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

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

Expand Down

0 comments on commit fa4fd24

Please sign in to comment.