1
0
Fork 0

fix linux

This commit is contained in:
Alex Janka 2023-04-06 14:02:21 +10:00
parent 18403c1d4f
commit 974ed55ea2
3 changed files with 44 additions and 17 deletions

View file

@ -1,7 +1,9 @@
use std::ffi::{c_void, CString}; use std::ffi::{c_void, CString};
use std::os::raw::{c_int, c_ulong}; use std::os::raw::{c_int, c_ulong};
use raw_window_handle::{HasRawWindowHandle, RawWindowHandle}; use raw_window_handle::{
HasRawDisplayHandle, HasRawWindowHandle, RawDisplayHandle, RawWindowHandle,
};
use x11::glx; use x11::glx;
use x11::xlib; use x11::xlib;
@ -80,19 +82,28 @@ impl GlContext {
/// ///
/// Use [Self::get_fb_config_and_visual] to create both of these things. /// Use [Self::get_fb_config_and_visual] to create both of these things.
pub unsafe fn create( pub unsafe fn create(
parent: &impl HasRawWindowHandle, config: FbConfig, parent_window: &impl HasRawWindowHandle, parent_display: &impl HasRawDisplayHandle,
config: FbConfig,
) -> Result<GlContext, GlError> { ) -> Result<GlContext, GlError> {
let handle = if let RawWindowHandle::Xlib(handle) = parent.raw_window_handle() { let window_handle = if let RawWindowHandle::Xlib(handle) = parent_window.raw_window_handle()
{
handle handle
} else { } else {
return Err(GlError::InvalidWindowHandle); return Err(GlError::InvalidWindowHandle);
}; };
if handle.display.is_null() { let display_handle =
if let RawDisplayHandle::Xlib(handle) = parent_display.raw_display_handle() {
handle
} else {
return Err(GlError::InvalidWindowHandle);
};
if display_handle.display.is_null() {
return Err(GlError::InvalidWindowHandle); return Err(GlError::InvalidWindowHandle);
} }
let display = handle.display as *mut xlib::_XDisplay; let display = display_handle.display as *mut xlib::_XDisplay;
errors::XErrorHandler::handle(display, |error_handler| { errors::XErrorHandler::handle(display, |error_handler| {
#[allow(non_snake_case)] #[allow(non_snake_case)]
@ -144,13 +155,13 @@ impl GlContext {
return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed)); return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed));
} }
let res = glx::glXMakeCurrent(display, handle.window, context); let res = glx::glXMakeCurrent(display, window_handle.window, context);
error_handler.check()?; error_handler.check()?;
if res == 0 { if res == 0 {
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
} }
glXSwapIntervalEXT(display, handle.window, config.gl_config.vsync as i32); glXSwapIntervalEXT(display, window_handle.window, config.gl_config.vsync as i32);
error_handler.check()?; error_handler.check()?;
if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 { if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 {
@ -158,7 +169,7 @@ impl GlContext {
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed)); return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
} }
Ok(GlContext { window: handle.window, display, context }) Ok(GlContext { window: window_handle.window, display, context })
}) })
} }

View file

@ -27,6 +27,10 @@ pub(crate) struct RawWindowHandleWrapper {
pub handle: RawWindowHandle, pub handle: RawWindowHandle,
} }
pub(crate) struct RawDisplayHandleWrapper {
pub handle: RawDisplayHandle,
}
impl WindowHandle { impl WindowHandle {
fn new(window_handle: platform::WindowHandle) -> Self { fn new(window_handle: platform::WindowHandle) -> Self {
Self { window_handle, phantom: PhantomData::default() } Self { window_handle, phantom: PhantomData::default() }
@ -148,3 +152,9 @@ unsafe impl HasRawWindowHandle for RawWindowHandleWrapper {
self.handle self.handle
} }
} }
unsafe impl HasRawDisplayHandle for RawDisplayHandleWrapper {
fn raw_display_handle(&self) -> RawDisplayHandle {
self.handle
}
}

View file

@ -15,8 +15,9 @@ use xcb::StructPtr;
use super::XcbConnection; use super::XcbConnection;
use crate::{ use crate::{
Event, MouseButton, MouseCursor, MouseEvent, PhyPoint, PhySize, ScrollDelta, Size, WindowEvent, Event, MouseButton, MouseCursor, MouseEvent, PhyPoint, PhySize, RawDisplayHandleWrapper,
WindowHandler, WindowInfo, WindowOpenOptions, WindowScalePolicy, ScrollDelta, Size, WindowEvent, WindowHandler, WindowInfo, WindowOpenOptions,
WindowScalePolicy,
}; };
use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods}; use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods};
@ -326,13 +327,18 @@ impl Window {
// compared to when raw-gl-context was a separate crate. // compared to when raw-gl-context was a separate crate.
#[cfg(feature = "opengl")] #[cfg(feature = "opengl")]
let gl_context = fb_config.map(|fb_config| { let gl_context = fb_config.map(|fb_config| {
let mut handle = XlibHandle::empty(); let mut window_handle = XlibWindowHandle::empty();
handle.window = window_id as c_ulong; window_handle.window = window_id as c_ulong;
handle.display = xcb_connection.conn.get_raw_dpy() as *mut c_void; let mut display_handle = XlibDisplayHandle::empty();
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Xlib(handle) }; display_handle.display = xcb_connection.conn.get_raw_dpy() as *mut c_void;
let window_handle =
RawWindowHandleWrapper { handle: RawWindowHandle::Xlib(window_handle) };
let display_handle =
RawDisplayHandleWrapper { handle: RawDisplayHandle::Xlib(display_handle) };
// Because of the visual negotation we had to take some extra steps to create this context // Because of the visual negotation we had to take some extra steps to create this context
let context = unsafe { platform::GlContext::create(&handle, fb_config) } let context =
unsafe { platform::GlContext::create(&window_handle, &display_handle, fb_config) }
.expect("Could not create OpenGL context"); .expect("Could not create OpenGL context");
GlContext::new(context) GlContext::new(context)
}); });
@ -716,6 +722,6 @@ fn mouse_id(id: u8) -> MouseButton {
} }
} }
pub fn copy_to_clipboard(data: &str) { pub fn copy_to_clipboard(_data: &str) {
todo!() todo!()
} }