forked from iced-rs/iced
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compositor: Add code to extract adapter from x11
- Loading branch information
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use as_raw_xcb_connection::AsRawXcbConnection; | ||
use raw_window_handle::{ | ||
RawDisplayHandle, XcbDisplayHandle, XlibDisplayHandle, | ||
}; | ||
use rustix::fs::{fstat, major, minor}; | ||
use tiny_xlib::Display; | ||
use x11rb::{ | ||
connection::RequestConnection, | ||
protocol::extensions::dri3::{ | ||
ConnectionExt as _, X11_EXTENSION_NAME as DRI3_NAME, | ||
}, | ||
xcb_ffi::XCBConnection, | ||
}; | ||
|
||
pub fn get_x11_device_ids<W: Window>(window: &W) -> Option<(u16, u16)> { | ||
x11rb::xcb_ffi::load_libxcb().ok()?; | ||
|
||
let (conn, screen) = match window | ||
.display_handle() | ||
.map(|handle| handle.as_raw()) | ||
{ | ||
#[allow(unsafe_code)] | ||
Ok(RawDisplayHandle::Xlib(XlibDisplayHandle { | ||
display, | ||
screen, | ||
.. | ||
})) => match display { | ||
Some(ptr) => unsafe { | ||
let xlib_display = | ||
tiny_xlib::Display::from_ptr(ptr.as_ptr()).ok()?; | ||
let conn = XCBConnection::from_raw_xcb_connection( | ||
xlib.as_raw_xcb_connection(), | ||
false, | ||
) | ||
.ok(); | ||
// intentially leak the display, we don't want to close the connection | ||
std::mem::forget(xlib_display); | ||
|
||
(conn?, screen) | ||
}, | ||
None => (XCBConnection::connect(None), screen), | ||
}, | ||
Ok(RawDisplayHandle::Xcb(XcbDisplayHandle { | ||
connection, | ||
screen, | ||
.. | ||
})) => match connection { | ||
Some(ptr) => ( | ||
unsafe { | ||
XCBConnection::from_raw_xcb_connection(ptr.as_ptr(), false) | ||
.ok()? | ||
}, | ||
screen, | ||
), | ||
None => (XCBConnection::connect(None), screen), | ||
}, | ||
_ => { | ||
return None; | ||
} | ||
}; | ||
|
||
// check for DRI3 | ||
let _ = conn.extension_information(DRI3_NAME).ok()??; | ||
// we have dri3, dri3_open exists on any version, so lets skip version checks. | ||
|
||
// provider being NONE tells the X server to use the RandR provider. | ||
let screen = &conn.setup().roots[screen]; | ||
let dri3 = connection | ||
.dri3_open(screen.root, x11rb::NONE)? | ||
.reply() | ||
.ok()?; | ||
let device_fd = dri3.device_fd; | ||
let stat = fstat(file).ok()?; | ||
let dev = stat.st_rdev; | ||
|
||
super::ids_from_dev(dev) | ||
} |