Skip to content

Commit

Permalink
Fix X11 OpenGL issue
Browse files Browse the repository at this point in the history
  • Loading branch information
greatest-ape committed Nov 23, 2022
1 parent 09cc05f commit 715a7c6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
20 changes: 6 additions & 14 deletions src/gl/x11.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::ffi::{c_void, CString};
use std::os::raw::{c_int, c_ulong};

use raw_window_handle::{HasRawWindowHandle, RawWindowHandle};

use x11::glx;
use x11::xlib;

Expand Down Expand Up @@ -80,19 +78,13 @@ impl GlContext {
///
/// Use [Self::get_fb_config_and_visual] to create both of these things.
pub unsafe fn create(
parent: &impl HasRawWindowHandle, config: FbConfig,
window: c_ulong, display: *mut c_void, config: FbConfig,
) -> Result<GlContext, GlError> {
let handle = if let RawWindowHandle::Xlib(handle) = parent.raw_window_handle() {
handle
} else {
return Err(GlError::InvalidWindowHandle);
};

if handle.display.is_null() {
if display.is_null() {
return Err(GlError::InvalidWindowHandle);
}

let display = handle.display as *mut xlib::_XDisplay;
let display = display as *mut xlib::_XDisplay;

errors::XErrorHandler::handle(display, |error_handler| {
#[allow(non_snake_case)]
Expand Down Expand Up @@ -144,21 +136,21 @@ impl GlContext {
return Err(GlError::CreationFailed(CreationFailedError::ContextCreationFailed));
}

let res = glx::glXMakeCurrent(display, handle.window, context);
let res = glx::glXMakeCurrent(display, window, context);
error_handler.check()?;
if res == 0 {
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
}

glXSwapIntervalEXT(display, handle.window, config.gl_config.vsync as i32);
glXSwapIntervalEXT(display, window, config.gl_config.vsync as i32);
error_handler.check()?;

if glx::glXMakeCurrent(display, 0, std::ptr::null_mut()) == 0 {
error_handler.check()?;
return Err(GlError::CreationFailed(CreationFailedError::MakeCurrentFailed));
}

Ok(GlContext { window: handle.window, display, context })
Ok(GlContext { window, display, context })
})
}

Expand Down
13 changes: 4 additions & 9 deletions src/x11/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ use crate::{
use super::keyboard::{convert_key_press_event, convert_key_release_event, key_mods};

#[cfg(feature = "opengl")]
use crate::{
gl::{platform, GlContext},
window::RawWindowHandleWrapper,
};
use crate::gl::{platform, GlContext};

pub struct WindowHandle {
raw_window_handle: Option<RawWindowHandle>,
Expand Down Expand Up @@ -326,13 +323,11 @@ impl Window {
// compared to when raw-gl-context was a separate crate.
#[cfg(feature = "opengl")]
let gl_context = fb_config.map(|fb_config| {
let mut handle = XlibWindowHandle::empty();
handle.window = window_id as c_ulong;
handle.display = xcb_connection.conn.get_raw_dpy() as *mut c_void;
let handle = RawWindowHandleWrapper { handle: RawWindowHandle::Xlib(handle) };
let window = window_id as c_ulong;
let display = xcb_connection.conn.get_raw_dpy() as *mut c_void;

// 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, display, fb_config) }
.expect("Could not create OpenGL context");
GlContext::new(context)
});
Expand Down

0 comments on commit 715a7c6

Please sign in to comment.