Added better error handling

This commit is contained in:
David Johnson 2022-01-16 08:59:29 -06:00
parent e4b1917333
commit dc6fc474be
8 changed files with 136 additions and 105 deletions

View file

@ -1,38 +1,40 @@
use crate::{GraphicsContextImpl, SoftBufferError};
use raw_window_handle::{HasRawWindowHandle, XlibHandle};
use std::os::raw::{c_char, c_uint};
use raw_window_handle::XlibHandle;
use x11_dl::xlib::{Display, GC, Visual, Xlib, ZPixmap};
use crate::GraphicsContextImpl;
use x11_dl::xlib::{Display, Visual, Xlib, ZPixmap, GC};
pub struct X11Impl{
pub struct X11Impl {
handle: XlibHandle,
lib: Xlib,
gc: GC,
visual: *mut Visual,
depth: i32
depth: i32,
}
impl X11Impl {
pub unsafe fn new(handle: XlibHandle) -> Self{
let lib = Xlib::open().unwrap();
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))))
};
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);
Self{
handle,
lib,
gc,
visual,
depth
}
Ok(
Self {
handle,
lib,
gc,
visual,
depth,
}
)
}
}
impl GraphicsContextImpl for X11Impl {
unsafe fn set_buffer(&mut self, buffer: &[u32], width: u16, height: u16) {
//create image
let image = (self.lib.XCreateImage)(
@ -45,13 +47,9 @@ impl GraphicsContextImpl for X11Impl {
width as u32,
height as u32,
32,
(width*4) as i32
(width * 4) as i32,
);
if image.is_null(){
panic!("Image is null!");
}
//push image to window
(self.lib.XPutImage)(
self.handle.display as *mut Display,
@ -63,11 +61,10 @@ impl GraphicsContextImpl for X11Impl {
0,
0,
width as c_uint,
height as c_uint
height as c_uint,
);
(*image).data = std::ptr::null_mut();
(self.lib.XDestroyImage)(image);
}
}
}