softbuffer/src/x11.rs

123 lines
3.9 KiB
Rust
Raw Normal View History

2022-12-21 17:26:09 -08:00
//! Implementation of software buffering for X11.
//!
//! This module converts the input buffer into an XImage and then sends it over the wire to be
//! drawn. A more effective implementation would use shared memory instead of the wire. In
//! addition, we may also want to blit to a pixmap instead of a window.
2022-12-20 07:10:11 -07:00
use crate::{GraphicsContextImpl, SwBufError};
use raw_window_handle::{XlibDisplayHandle, XlibWindowHandle};
2022-01-15 08:17:17 -06:00
use std::os::raw::{c_char, c_uint};
2022-01-16 08:59:29 -06:00
use x11_dl::xlib::{Display, Visual, Xlib, ZPixmap, GC};
2022-01-15 08:17:17 -06:00
2022-12-21 17:26:09 -08:00
/// The handle to an X11 drawing context.
2022-01-16 08:59:29 -06:00
pub struct X11Impl {
2022-12-21 17:26:09 -08:00
/// The window handle.
window_handle: XlibWindowHandle,
2022-12-21 17:26:09 -08:00
/// The display handle.
display_handle: XlibDisplayHandle,
2022-12-21 17:26:09 -08:00
/// Reference to the X11 shared library.
2022-01-15 08:17:17 -06:00
lib: Xlib,
2022-12-21 17:26:09 -08:00
/// The graphics context for drawing.
2022-01-15 08:17:17 -06:00
gc: GC,
2022-12-21 17:26:09 -08:00
/// Information about the screen to use for drawing.
2022-01-15 08:17:17 -06:00
visual: *mut Visual,
2022-12-21 17:26:09 -08:00
/// The depth (bits per pixel) of the drawing context.
2022-01-16 08:59:29 -06:00
depth: i32,
2022-01-15 08:17:17 -06:00
}
impl X11Impl {
2022-12-21 17:26:09 -08:00
/// Create a new `X11Impl` from a `XlibWindowHandle` and `XlibDisplayHandle`.
///
/// # Safety
///
/// The `XlibWindowHandle` and `XlibDisplayHandle` must be valid.
pub unsafe fn new(
window_handle: XlibWindowHandle,
display_handle: XlibDisplayHandle,
) -> Result<Self, SwBufError> {
// Try to open the X11 shared library.
2022-01-16 08:59:29 -06:00
let lib = match Xlib::open() {
Ok(lib) => lib,
2022-12-21 17:26:09 -08:00
Err(e) => {
return Err(SwBufError::PlatformError(
Some("Failed to open Xlib".into()),
Some(Box::new(e)),
))
}
2022-01-16 08:59:29 -06:00
};
2022-12-21 17:26:09 -08:00
// Validate the handles to ensure that they aren't incomplete.
if display_handle.display.is_null() {
return Err(SwBufError::IncompleteDisplayHandle);
}
if window_handle.window.is_null() {
return Err(SwBufError::IncompleteWindowHandle);
}
// Get the screen number from the handle.
// NOTE: By default, XlibDisplayHandle sets the screen number to 0. If we see a zero,
// it could mean either screen index zero, or that the screen number was not set. We
// can't tell which, so we'll just assume that the screen number was not set.
let screen = match display_handle.screen {
0 => (lib.XDefaultScreen)(display_handle.display as *mut Display),
screen => screen,
};
// Use the default graphics context, visual and depth for this screen.
let gc = (lib.XDefaultGC)(display_handle.display as *mut Display, screen);
let visual = (lib.XDefaultVisual)(display_handle.display as *mut Display, screen);
let depth = (lib.XDefaultDepth)(display_handle.display as *mut Display, screen);
2022-01-15 08:17:17 -06:00
2022-12-21 17:26:09 -08:00
Ok(Self {
window_handle,
display_handle,
lib,
gc,
visual,
depth,
})
2022-01-15 08:17:17 -06:00
}
}
impl GraphicsContextImpl for X11Impl {
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
2022-12-21 17:26:09 -08:00
// Create the image from the buffer.
2022-01-15 08:17:17 -06:00
let image = (self.lib.XCreateImage)(
self.display_handle.display as *mut Display,
2022-01-15 08:17:17 -06:00
self.visual,
self.depth as u32,
ZPixmap,
0,
(buffer.as_ptr()) as *mut c_char,
2022-01-15 08:17:17 -06:00
width as u32,
height as u32,
32,
2022-01-16 08:59:29 -06:00
(width * 4) as i32,
2022-01-15 08:17:17 -06:00
);
2022-12-21 17:26:09 -08:00
// Draw the image to the window.
2022-01-15 08:17:17 -06:00
(self.lib.XPutImage)(
self.display_handle.display as *mut Display,
self.window_handle.window,
2022-01-15 08:17:17 -06:00
self.gc,
image,
0,
0,
0,
0,
width as c_uint,
2022-01-16 08:59:29 -06:00
height as c_uint,
2022-01-15 08:17:17 -06:00
);
2022-12-21 17:26:09 -08:00
// Delete the image data.
(*image).data = std::ptr::null_mut();
2022-01-15 08:17:17 -06:00
(self.lib.XDestroyImage)(image);
}
2022-01-16 08:59:29 -06:00
}