diff --git a/build.rs b/build.rs index ca96342..65c090b 100644 --- a/build.rs +++ b/build.rs @@ -2,13 +2,17 @@ use std::env; use std::fs::File; use std::path::Path; +#[cfg(feature = "glx")] use gl_generator::{Api, Fallbacks, GlobalGenerator, Profile, Registry}; fn main() { - let dest = env::var("OUT_DIR").unwrap(); - let mut file = File::create(&Path::new(&dest).join("bindings.rs")).unwrap(); + #[cfg(feature = "glx")] + { + let dest = env::var("OUT_DIR").unwrap(); + let mut file = File::create(&Path::new(&dest).join("bindings.rs")).unwrap(); - Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, []) - .write_bindings(GlobalGenerator, &mut file) - .unwrap(); + Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, []) + .write_bindings(GlobalGenerator, &mut file) + .unwrap(); + } } diff --git a/src/glx/mod.rs b/src/glx/mod.rs index 6ceac75..ccd143e 100644 --- a/src/glx/mod.rs +++ b/src/glx/mod.rs @@ -1,3 +1,6 @@ +//! An extension that bridges X11 with OpenGL, an API used for rendering +//! graphics with the GPU. + mod context; mod drawable; @@ -10,6 +13,7 @@ pub use self::context::GlxContext; pub use self::drawable::GlxDrawable; pub trait GlxExtension { + /// Checks if glx is available in this implementation. fn query_glx_extension(&self) -> Result; fn create_context( diff --git a/src/rect.rs b/src/rect.rs index 1dd4023..b77ad36 100644 --- a/src/rect.rs +++ b/src/rect.rs @@ -22,3 +22,14 @@ impl Rectangle { } } } + +impl From<(i32, i32, u32, u32)> for Rectangle { + fn from((x, y, width, height): (i32, i32, u32, u32)) -> Self { + Rectangle { + x, + y, + width, + height, + } + } +} diff --git a/src/xinerama/mod.rs b/src/xinerama/mod.rs index 8e3b882..9808aac 100644 --- a/src/xinerama/mod.rs +++ b/src/xinerama/mod.rs @@ -1,3 +1,6 @@ +//! An extension that enables applications and window managers to use two or +//! more physical displays as one large virtual display. + mod screen_info; pub use self::screen_info::ScreensInfo; diff --git a/src/xinput/mod.rs b/src/xinput/mod.rs index 8b13789..aab30dd 100644 --- a/src/xinput/mod.rs +++ b/src/xinput/mod.rs @@ -1 +1,2 @@ - +//! Component of x11 related to listing available input devices, querying +//! information about a device and changing input device settings. diff --git a/src/xlib/display.rs b/src/xlib/display.rs index 57e36a0..80415c1 100644 --- a/src/xlib/display.rs +++ b/src/xlib/display.rs @@ -74,7 +74,7 @@ impl Display { let ptr = event.as_mut_ptr(); unsafe { xlib::XNextEvent(self.inner, ptr) }; // TODO: check to make sure this isn't null - let mut event = unsafe { event.assume_init() }; + let event = unsafe { event.assume_init() }; debug!("event: {:?}", event); unsafe { Event::from_raw(event) } } @@ -179,10 +179,10 @@ impl Display { /// Query extension pub fn query_extension(&self, name: impl AsRef) -> Result<()> { let name = ffi::c_char_star_from_string(name)?; - let mut major_opcode_return = ptr::null_mut(); - let mut first_event_return = ptr::null_mut(); - let mut first_error_return = ptr::null_mut(); - let result = unsafe { + let major_opcode_return = ptr::null_mut(); + let first_event_return = ptr::null_mut(); + let first_error_return = ptr::null_mut(); + let _result = unsafe { xlib::XQueryExtension( self.inner, name, @@ -198,7 +198,7 @@ impl Display { /// List extensions pub fn list_extensions(&self) -> Result { - let mut nextensions_return = ptr::null_mut(); + let nextensions_return = ptr::null_mut(); let result = unsafe { xlib::XListExtensions(self.inner, nextensions_return) }; // TODO: check result null // TODO: check nextensions_return diff --git a/src/xlib/mod.rs b/src/xlib/mod.rs index 6865df2..ee1fa23 100644 --- a/src/xlib/mod.rs +++ b/src/xlib/mod.rs @@ -1,9 +1,12 @@ +//! The core of x11 + mod atom; mod cursor; mod display; mod drawable; mod event; mod image; +mod pixmap; mod visual; mod window; @@ -14,4 +17,5 @@ pub use self::drawable::Drawable; pub use self::event::{Event, EventKind}; pub use self::image::{Image, ImageByteOrder, PixBuffer}; pub use self::visual::{Visual, VisualInfo}; -pub use self::window::{EventMask, Window}; +pub use self::window::{EventMask, SetWindowAttributes, Window, WindowAttributes}; +pub use self::pixmap::PixMap; diff --git a/src/xlib/pixmap.rs b/src/xlib/pixmap.rs new file mode 100644 index 0000000..cff4579 --- /dev/null +++ b/src/xlib/pixmap.rs @@ -0,0 +1,20 @@ +use x11::xlib; + +/// Pixmaps are off-screen resources that are used for various operations, for +/// example, defining cursors as tiling patterns or as the source for certain +/// raster operations +/// +/// Most graphics requests can operate either on a window or on a pixmap. A +/// bitmap is a single bit-plane pixmap. Pixmaps can only be used on the screen +/// on which they were created. +#[derive(Debug)] +pub struct PixMap { + pub(crate) display: *mut xlib::Display, + pub(crate) inner: xlib::Pixmap, +} + +impl Drop for PixMap { + fn drop(&mut self) { + unsafe { xlib::XFreePixmap(self.display, self.inner) }; + } +} diff --git a/src/xlib/window.rs b/src/xlib/window.rs index fbc8177..eb1b3ea 100644 --- a/src/xlib/window.rs +++ b/src/xlib/window.rs @@ -1,5 +1,6 @@ use std::mem; -use std::os::raw::c_ulong; +use std::os::raw::{c_long, c_ulong}; +use std::ptr; use x11::xlib; @@ -10,12 +11,13 @@ use super::atom::Atom; use super::display::{Display, GetDisplay}; use super::drawable::Drawable; use super::image::Image; +use super::pixmap::PixMap; /// A wrapper around a window handle. #[derive(Clone)] pub struct Window<'a> { - pub(super) display: &'a Display, - pub(super) inner: xlib::Window, + pub(crate) display: &'a Display, + pub(crate) inner: xlib::Window, } impl<'a> Window<'a> { @@ -24,6 +26,7 @@ impl<'a> Window<'a> { display: &'a Display, parent: Option, location: Rectangle, + set_attributes: SetWindowAttributes, ) -> Result> { let parent = match parent { Some(parent) => parent, @@ -43,7 +46,7 @@ impl<'a> Window<'a> { 0, visual.as_raw(), 0, - 0 as *mut xlib::XSetWindowAttributes, + set_attributes.into_raw(), ) }; Ok(Window { @@ -148,12 +151,6 @@ pub struct WindowAttributes<'a> { pub(self) inner: *mut xlib::XWindowAttributes, } -// impl AsRef for Window { -// fn as_ref(&self) -> &Drawable { -// &self.inner -// } -// } - impl<'a> WindowAttributes<'a> { /// Gets the width of the window pub fn get_x(&self) -> i32 { @@ -184,7 +181,14 @@ impl<'a> WindowAttributes<'a> { } } +impl<'a> Drop for WindowAttributes<'a> { + fn drop(&mut self) { + unsafe { libc::free(self.inner as *mut libc::c_void) }; + } +} + bitflags! { + #[derive(Default)] pub struct EventMask: c_ulong { const NO_EVENT_MASK = 0; const KEY_PRESS_MASK = (1<<0); @@ -214,3 +218,39 @@ bitflags! { const OWNER_GRAB_BUTTON_MASK = (1<<24); } } + +#[derive(Debug, Default)] +pub struct SetWindowAttributes { + pub background_pixmap: Option, + pub background_pixel: c_ulong, + pub border_pixmap: Option, + pub border_pixel: c_ulong, + pub save_under: bool, + pub event_mask: EventMask, + pub do_not_propagate_mask: c_long, + pub override_redirect: bool, +} + +impl SetWindowAttributes { + pub fn into_raw(self) -> *mut xlib::XSetWindowAttributes { + let result = xlib::XSetWindowAttributes { + background_pixmap: unimplemented!(), + background_pixel: self.background_pixel, + border_pixmap: unimplemented!(), + border_pixel: unimplemented!(), + bit_gravity: unimplemented!(), + win_gravity: unimplemented!(), + backing_store: unimplemented!(), + backing_planes: unimplemented!(), + backing_pixel: unimplemented!(), + save_under: self.save_under.into(), + event_mask: self.event_mask.bits() as i64, + do_not_propagate_mask: self.do_not_propagate_mask, + override_redirect: self.override_redirect.into(), + colormap: unimplemented!(), + cursor: unimplemented!(), + }; + + Box::into_raw(Box::new(result)) + } +} diff --git a/src/xrender/mod.rs b/src/xrender/mod.rs index d9a70d2..50da015 100644 --- a/src/xrender/mod.rs +++ b/src/xrender/mod.rs @@ -1 +1,4 @@ +//! An extension that deals with rendering and compositing images with other +//! drawing primitives. + mod picture;