softbuffer/src/x11.rs

73 lines
2.2 KiB
Rust
Raw Normal View History

2022-12-20 07:10:11 -07:00
use crate::{GraphicsContextImpl, SwBufError};
use raw_window_handle::{HasRawDisplayHandle, HasRawWindowHandle, 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-01-16 08:59:29 -06:00
pub struct X11Impl {
window_handle: XlibWindowHandle,
display_handle: XlibDisplayHandle,
2022-01-15 08:17:17 -06:00
lib: Xlib,
gc: GC,
visual: *mut Visual,
2022-01-16 08:59:29 -06:00
depth: i32,
2022-01-15 08:17:17 -06:00
}
impl X11Impl {
2022-12-20 07:10:11 -07:00
pub unsafe fn new<W: HasRawWindowHandle + HasRawDisplayHandle>(window_handle: XlibWindowHandle, display_handle: XlibDisplayHandle) -> Result<Self, SwBufError<W>> {
2022-01-16 08:59:29 -06:00
let lib = match Xlib::open() {
Ok(lib) => lib,
2022-12-20 07:10:11 -07: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
};
let screen = (lib.XDefaultScreen)(display_handle.display as *mut Display);
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-01-16 08:59:29 -06:00
Ok(
Self {
window_handle,
display_handle,
2022-01-16 08:59:29 -06:00
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) {
//create image
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
);
//push image to window
(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
);
(*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
}