Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend window.rs example to show window on non-default X screens #1

Draft
wants to merge 1 commit into
base: fill-windows-cherry-pick
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions examples/window.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
#![allow(clippy::single_match)]

use raw_window_handle::{HasRawDisplayHandle, RawDisplayHandle, XlibWindowHandle};
use simple_logger::SimpleLogger;
use winit::{
event::{Event, WindowEvent},
event_loop::EventLoop,
platform::x11::{WindowBuilderExtX11, WindowExtX11},
window::WindowBuilder,
};
use x11_dl::xlib;

#[path = "util/fill.rs"]
mod fill;

fn main() {
SimpleLogger::new().init().unwrap();
let screen_number = std::env::args()
.nth(1)
.expect("please specify screen number to run on as first commandline argument")
.parse()
.expect("could not parse first commandline argument as i32");

SimpleLogger::new().env().init().unwrap();
let event_loop = EventLoop::new();

let window = WindowBuilder::new()
// Extract connection fro X from winit't event loop. 🤷
let display_handle = event_loop.raw_display_handle();
let RawDisplayHandle::Xlib(xlib_display_handle) = display_handle else {
eprintln!("{display_handle:?} was not XlibDisplayHandle");
return;
};
let display: *mut xlib::_XDisplay = xlib_display_handle.display.cast();

// Load Xlib library.
let xlib = xlib::Xlib::open().unwrap();
log::debug!(
"X server we connected to has {} screens (using `XScreenCount(display)`).",
unsafe { (xlib.XScreenCount)(display) }
);

let root_window_id = unsafe { (xlib.XRootWindow)(display, screen_number) };
log::debug!(
"id of root window of screen {} is {}.",
screen_number,
root_window_id
);

let mut root_window_handle = XlibWindowHandle::empty();
root_window_handle.window = root_window_id;
dbg!(root_window_handle);

let window_builder = WindowBuilder::new()
.with_title("A fantastic window!")
.with_inner_size(winit::dpi::LogicalSize::new(128.0, 128.0))
.build(&event_loop)
.unwrap();
.with_inner_size(winit::dpi::LogicalSize::new(500.0, 500.0))
.with_x11_screen(screen_number);
// We need to specify matching root window (every X screen has its own one):
let window_builder =
unsafe { window_builder.with_parent_window(Some(root_window_handle.into())) };
let window = window_builder.build(&event_loop).unwrap();

dbg!(window.xlib_screen_id());

event_loop.run(move |event, _, control_flow| {
control_flow.set_wait();
println!("{event:?}");

match event {
Event::WindowEvent {
Expand Down