2022-01-16 08:59:29 -06:00
|
|
|
use crate::{GraphicsContextImpl, SoftBufferError};
|
|
|
|
|
use raw_window_handle::{HasRawWindowHandle, XlibHandle};
|
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 {
|
2022-01-15 08:17:17 -06:00
|
|
|
handle: XlibHandle,
|
|
|
|
|
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-01-16 08:59:29 -06:00
|
|
|
pub unsafe fn new<W: HasRawWindowHandle>(handle: XlibHandle) -> Result<Self, SoftBufferError<W>> {
|
|
|
|
|
let lib = match Xlib::open() {
|
|
|
|
|
Ok(lib) => lib,
|
|
|
|
|
Err(e) => return Err(SoftBufferError::PlatformError(Some("Failed to open Xlib".into()), Some(Box::new(e))))
|
|
|
|
|
};
|
2022-01-15 08:17:17 -06:00
|
|
|
let screen = (lib.XDefaultScreen)(handle.display as *mut Display);
|
|
|
|
|
let gc = (lib.XDefaultGC)(handle.display as *mut Display, screen);
|
|
|
|
|
let visual = (lib.XDefaultVisual)(handle.display as *mut Display, screen);
|
|
|
|
|
let depth = (lib.XDefaultDepth)(handle.display as *mut Display, screen);
|
|
|
|
|
|
2022-01-16 08:59:29 -06:00
|
|
|
Ok(
|
|
|
|
|
Self {
|
|
|
|
|
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) {
|
|
|
|
|
//create image
|
|
|
|
|
let image = (self.lib.XCreateImage)(
|
|
|
|
|
self.handle.display as *mut Display,
|
|
|
|
|
self.visual,
|
|
|
|
|
self.depth as u32,
|
|
|
|
|
ZPixmap,
|
|
|
|
|
0,
|
2022-01-15 08:33:24 -06:00
|
|
|
(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.handle.display as *mut Display,
|
|
|
|
|
self.handle.window,
|
|
|
|
|
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-01-15 08:33:24 -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
|
|
|
}
|