update deps

This commit is contained in:
Michael Zhang 2023-08-20 04:50:59 -05:00
parent 4678092bbd
commit 2572d86afd
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
28 changed files with 951 additions and 763 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[registries.crates-io]
protocol = "sparse"

1
.envrc Normal file
View file

@ -0,0 +1 @@
use flake

1
.gitignore vendored
View file

@ -1,2 +1,3 @@
/target /target
/Cargo.lock /Cargo.lock
.direnv

View file

@ -22,11 +22,11 @@ xinput = ["x11/xinput"]
[dependencies] [dependencies]
libc = "0.2" libc = "0.2"
x11 = { version = "2.18" } x11 = { version = "2.21" }
thiserror = "1.0.20" thiserror = "1.0.47"
log = "0.4.8" log = "0.4.20"
bitflags = "1.2.1" bitflags = "2.4.0"
derivative = "2.1.1" derivative = "2.2.0"
[build-dependencies] [build-dependencies]
gl_generator = { version = "0.14.0", optional = true } gl_generator = { version = "0.14.0", optional = true }

View file

@ -6,13 +6,13 @@ use std::path::Path;
use gl_generator::{Api, Fallbacks, GlobalGenerator, Profile, Registry}; use gl_generator::{Api, Fallbacks, GlobalGenerator, Profile, Registry};
fn main() { fn main() {
#[cfg(feature = "glx")] #[cfg(feature = "glx")]
{ {
let dest = env::var("OUT_DIR").unwrap(); let dest = env::var("OUT_DIR").unwrap();
let mut file = File::create(&Path::new(&dest).join("bindings.rs")).unwrap(); let mut file = File::create(&Path::new(&dest).join("bindings.rs")).unwrap();
Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, []) Registry::new(Api::Gl, (4, 5), Profile::Core, Fallbacks::All, [])
.write_bindings(GlobalGenerator, &mut file) .write_bindings(GlobalGenerator, &mut file)
.unwrap(); .unwrap();
} }
} }

111
flake.lock Normal file
View file

@ -0,0 +1,111 @@
{
"nodes": {
"fenix": {
"inputs": {
"nixpkgs": "nixpkgs",
"rust-analyzer-src": "rust-analyzer-src"
},
"locked": {
"lastModified": 1692512345,
"narHash": "sha256-ztOkYlG6LgS5AyjcQG3XWX3MRBQCHmTVlW3rhd2UdJs=",
"owner": "nix-community",
"repo": "fenix",
"rev": "75907b374bd1b95fe7ebaa482e4872caae9da081",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "fenix",
"type": "github"
}
},
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1689068808,
"narHash": "sha256-6ixXo3wt24N/melDWjq70UuHQLxGV8jZvooRanIHXw0=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "919d646de7be200f3bf08cb76ae1f09402b6f9b4",
"type": "github"
},
"original": {
"id": "flake-utils",
"type": "indirect"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1692447944,
"narHash": "sha256-fkJGNjEmTPvqBs215EQU4r9ivecV5Qge5cF/QDLVn3U=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "d680ded26da5cf104dd2735a51e88d2d8f487b4d",
"type": "github"
},
"original": {
"owner": "nixos",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 1663551060,
"narHash": "sha256-e2SR4cVx9p7aW/XnVsGsWZBplApA9ZJUjc0fejJhnYo=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "8a5b9ee7b7a2b38267c9481f5c629c015108ab0d",
"type": "github"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"root": {
"inputs": {
"fenix": "fenix",
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs_2"
}
},
"rust-analyzer-src": {
"flake": false,
"locked": {
"lastModified": 1692456871,
"narHash": "sha256-ribQkxEbMMb8vcBMKvcrPHFftMmlaF3HIAbJty9fDeY=",
"owner": "rust-lang",
"repo": "rust-analyzer",
"rev": "83b3ba1b8191c065bb0dacc79e5896f7f21e4611",
"type": "github"
},
"original": {
"owner": "rust-lang",
"ref": "nightly",
"repo": "rust-analyzer",
"type": "github"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

35
flake.nix Normal file
View file

@ -0,0 +1,35 @@
{
inputs = { fenix.url = "github:nix-community/fenix"; };
outputs = { self, nixpkgs, flake-utils, fenix }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [ fenix.overlays.default ];
};
toolchain = pkgs.fenix.stable;
flakePkgs = rec { };
in rec {
packages = flake-utils.lib.flattenTree flakePkgs;
devShell = pkgs.mkShell {
inputsFrom = with packages; [ ];
packages = (with pkgs; [
cargo-watch
cargo-deny
cargo-edit
cargo-expand
pkg-config
xlibsWrapper
# Get the nightly version of rustfmt so we can wrap comments
pkgs.fenix.default.rustfmt
]) ++ (with toolchain; [ cargo clippy rustc rustfmt ]);
};
});
}

3
rustfmt.toml Normal file
View file

@ -0,0 +1,3 @@
max_width = 80
tab_spaces = 2
wrap_comments = true

View file

@ -4,33 +4,33 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
#[allow(missing_docs)] #[allow(missing_docs)]
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum Error { pub enum Error {
#[error("invalid image byte order: {0}")] #[error("invalid image byte order: {0}")]
InvalidByteOrder(i32), InvalidByteOrder(i32),
#[error("invalid event type: {0}")] #[error("invalid event type: {0}")]
InvalidEventType(i32), InvalidEventType(i32),
#[error("failed to get attributes")] #[error("failed to get attributes")]
GetAttributesError, GetAttributesError,
#[error("failed to create cursor")] #[error("failed to create cursor")]
CreateCursorError, CreateCursorError,
#[error("failed to open display")] #[error("failed to open display")]
DisplayOpenError, DisplayOpenError,
#[error("failed to get window")] #[error("failed to get window")]
GetWindowError, GetWindowError,
#[error("failed to translate coordinates")] #[error("failed to translate coordinates")]
TranslateCoordinatesError, TranslateCoordinatesError,
#[error("utf8 decoding error: {0}")] #[error("utf8 decoding error: {0}")]
Utf8(#[from] std::str::Utf8Error), Utf8(#[from] std::str::Utf8Error),
#[error("nul error: {0}")] #[error("nul error: {0}")]
Nul(#[from] std::ffi::NulError), Nul(#[from] std::ffi::NulError),
#[error("error")] #[error("error")]
Error, Error,
} }

View file

@ -4,12 +4,12 @@ use std::os::raw::c_char;
use crate::Result; use crate::Result;
pub unsafe fn string_from_c_char_star(ptr: *mut c_char) -> Result<String> { pub unsafe fn string_from_c_char_star(ptr: *mut c_char) -> Result<String> {
let c_str = CStr::from_ptr(ptr); let c_str = CStr::from_ptr(ptr);
let s = c_str.to_str()?; let s = c_str.to_str()?;
Ok(s.to_owned()) Ok(s.to_owned())
} }
pub fn c_char_star_from_string(s: impl AsRef<str>) -> Result<*mut c_char> { pub fn c_char_star_from_string(s: impl AsRef<str>) -> Result<*mut c_char> {
let c_str = CString::new(s.as_ref().as_bytes())?; let c_str = CString::new(s.as_ref().as_bytes())?;
Ok(c_str.into_raw()) Ok(c_str.into_raw())
} }

View file

@ -3,18 +3,18 @@ use x11::glx;
use crate::xlib::Display; use crate::xlib::Display;
pub struct GlxContext<'a> { pub struct GlxContext<'a> {
pub(super) display: &'a Display, pub(super) display: &'a Display,
pub(super) ctx: glx::GLXContext, pub(super) ctx: glx::GLXContext,
} }
impl<'a> GlxContext<'a> { impl<'a> GlxContext<'a> {
pub fn as_raw(&self) -> glx::GLXContext { pub fn as_raw(&self) -> glx::GLXContext {
self.ctx self.ctx
} }
} }
impl<'a> Drop for GlxContext<'a> { impl<'a> Drop for GlxContext<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { glx::glXDestroyContext(self.display.as_raw(), self.ctx) }; unsafe { glx::glXDestroyContext(self.display.as_raw(), self.ctx) };
} }
} }

View file

@ -3,14 +3,14 @@ use x11::xlib;
use crate::xlib::Window; use crate::xlib::Window;
pub enum GlxDrawable<'a> { pub enum GlxDrawable<'a> {
Window(Window<'a>), Window(Window<'a>),
// TODO: pixmap // TODO: pixmap
} }
impl<'a> GlxDrawable<'a> { impl<'a> GlxDrawable<'a> {
pub fn as_raw(&self) -> xlib::XID { pub fn as_raw(&self) -> xlib::XID {
match self { match self {
GlxDrawable::Window(window) => window.as_raw(), GlxDrawable::Window(window) => window.as_raw(),
}
} }
}
} }

View file

@ -13,54 +13,58 @@ pub use self::context::GlxContext;
pub use self::drawable::GlxDrawable; pub use self::drawable::GlxDrawable;
pub trait GlxExtension { pub trait GlxExtension {
/// Checks if glx is available in this implementation. /// Checks if glx is available in this implementation.
fn query_glx_extension(&self) -> Result<bool>; fn query_glx_extension(&self) -> Result<bool>;
fn create_context( fn create_context(
&self, &self,
visual_info: &VisualInfo, visual_info: &VisualInfo,
share_list: Option<&GlxContext>, share_list: Option<&GlxContext>,
direct: bool, direct: bool,
) -> Result<GlxContext>; ) -> Result<GlxContext>;
fn make_current(&self, drawable: GlxDrawable, ctx: GlxContext) -> Result<()>; fn make_current(&self, drawable: GlxDrawable, ctx: GlxContext) -> Result<()>;
fn choose_visual(&self, screen: i32) -> Result<VisualInfo>; fn choose_visual(&self, screen: i32) -> Result<VisualInfo>;
} }
impl GlxExtension for Display { impl GlxExtension for Display {
fn query_glx_extension(&self) -> Result<bool> { fn query_glx_extension(&self) -> Result<bool> {
let result = unsafe { glx::glXQueryExtension(self.inner, 0 as *mut _, 0 as *mut _) }; let result =
Ok(result == 1) unsafe { glx::glXQueryExtension(self.inner, 0 as *mut _, 0 as *mut _) };
} Ok(result == 1)
}
fn create_context( fn create_context(
&self, &self,
visual_info: &VisualInfo, visual_info: &VisualInfo,
share_list: Option<&GlxContext>, share_list: Option<&GlxContext>,
direct: bool, direct: bool,
) -> Result<GlxContext> { ) -> Result<GlxContext> {
let share_list = share_list let share_list = share_list
.map(|ctx| ctx.as_raw()) .map(|ctx| ctx.as_raw())
.unwrap_or_else(|| 0 as *mut _); .unwrap_or_else(|| 0 as *mut _);
let ctx = unsafe { let ctx = unsafe {
glx::glXCreateContext( glx::glXCreateContext(
self.as_raw(), self.as_raw(),
visual_info.as_raw(), visual_info.as_raw(),
share_list, share_list,
direct.into(), direct.into(),
) )
}; };
Ok(GlxContext { display: self, ctx }) Ok(GlxContext { display: self, ctx })
} }
fn make_current(&self, drawable: GlxDrawable, ctx: GlxContext) -> Result<()> { fn make_current(&self, drawable: GlxDrawable, ctx: GlxContext) -> Result<()> {
unsafe { glx::glXMakeCurrent(self.as_raw(), drawable.as_raw(), ctx.as_raw()) }; unsafe {
Ok(()) glx::glXMakeCurrent(self.as_raw(), drawable.as_raw(), ctx.as_raw())
} };
Ok(())
}
fn choose_visual(&self, screen: i32) -> Result<VisualInfo> { fn choose_visual(&self, screen: i32) -> Result<VisualInfo> {
let visual = unsafe { glx::glXChooseVisual(self.as_raw(), screen, 0 as *mut _) }; let visual =
Ok(VisualInfo::from_raw(visual)) unsafe { glx::glXChooseVisual(self.as_raw(), screen, 0 as *mut _) };
} Ok(VisualInfo::from_raw(visual))
}
} }

View file

@ -13,7 +13,7 @@ pub extern crate x11;
#[cfg(feature = "glx")] #[cfg(feature = "glx")]
mod gl { mod gl {
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
} }
#[cfg(feature = "glx")] #[cfg(feature = "glx")]
pub mod glx; pub mod glx;

View file

@ -1,35 +1,35 @@
/// A rectangle. /// A rectangle.
#[derive(Debug)] #[derive(Debug)]
pub struct Rectangle { pub struct Rectangle {
/// x /// x
pub x: i32, pub x: i32,
/// y /// y
pub y: i32, pub y: i32,
/// width /// width
pub width: u32, pub width: u32,
/// height /// height
pub height: u32, pub height: u32,
} }
impl Rectangle { impl Rectangle {
/// Create a new Rectangle from u32s /// Create a new Rectangle from u32s
pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self { pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
Rectangle { Rectangle {
x, x,
y, y,
width, width,
height, height,
}
} }
}
} }
impl From<(i32, i32, u32, u32)> for Rectangle { impl From<(i32, i32, u32, u32)> for Rectangle {
fn from((x, y, width, height): (i32, i32, u32, u32)) -> Self { fn from((x, y, width, height): (i32, i32, u32, u32)) -> Self {
Rectangle { Rectangle {
x, x,
y, y,
width, width,
height, height,
}
} }
}
} }

View file

@ -4,61 +4,63 @@ use crate::errors::Result;
use crate::xlib::Display; use crate::xlib::Display;
pub struct ScreensInfo { pub struct ScreensInfo {
inner: *mut xinerama::XineramaScreenInfo, inner: *mut xinerama::XineramaScreenInfo,
heads: isize, heads: isize,
} }
impl ScreensInfo { impl ScreensInfo {
pub fn query(display: &Display) -> Result<Self> { pub fn query(display: &Display) -> Result<Self> {
let mut heads = 0; let mut heads = 0;
let inner = unsafe { xinerama::XineramaQueryScreens(display.inner, &mut heads as *mut _) }; let inner = unsafe {
// TODO: check for error xinerama::XineramaQueryScreens(display.inner, &mut heads as *mut _)
Ok(ScreensInfo { };
inner, // TODO: check for error
heads: heads as isize, Ok(ScreensInfo {
}) inner,
} heads: heads as isize,
})
}
pub fn iter(&self) -> ScreenInfoIter { pub fn iter(&self) -> ScreenInfoIter {
ScreenInfoIter(self, 0) ScreenInfoIter(self, 0)
} }
} }
impl Drop for ScreensInfo { impl Drop for ScreensInfo {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { x11::xlib::XFree(self.inner as *mut _) }; unsafe { x11::xlib::XFree(self.inner as *mut _) };
} }
} }
pub struct ScreenInfoIter<'a>(&'a ScreensInfo, isize); pub struct ScreenInfoIter<'a>(&'a ScreensInfo, isize);
pub struct ScreenInfo { pub struct ScreenInfo {
pub x: i16, pub x: i16,
pub y: i16, pub y: i16,
pub width: i16, pub width: i16,
pub height: i16, pub height: i16,
} }
impl<'a> Iterator for ScreenInfoIter<'a> { impl<'a> Iterator for ScreenInfoIter<'a> {
type Item = (i32, ScreenInfo); type Item = (i32, ScreenInfo);
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.1 >= self.0.heads { if self.1 >= self.0.heads {
return None; return None;
}
let screen = unsafe { self.0.inner.offset(self.1) };
self.1 += 1;
unsafe {
Some((
(*screen).screen_number as i32,
ScreenInfo {
x: (*screen).x_org as i16,
y: (*screen).y_org as i16,
width: (*screen).width as i16,
height: (*screen).height as i16,
},
))
}
} }
let screen = unsafe { self.0.inner.offset(self.1) };
self.1 += 1;
unsafe {
Some((
(*screen).screen_number as i32,
ScreenInfo {
x: (*screen).x_org as i16,
y: (*screen).y_org as i16,
width: (*screen).width as i16,
height: (*screen).height as i16,
},
))
}
}
} }

View file

@ -8,29 +8,38 @@ use super::display::Display;
/// A unique string or intger /// A unique string or intger
pub struct Atom { pub struct Atom {
inner: xlib::Atom, inner: xlib::Atom,
} }
impl Atom { impl Atom {
/// Create a new atom using a string /// Create a new atom using a string
pub fn new(display: &Display, val: impl AsRef<str>, only_if_exists: bool) -> Result<Self> { pub fn new(
let val = { display: &Display,
let v = val.as_ref(); val: impl AsRef<str>,
let s = CString::new(v).unwrap(); only_if_exists: bool,
s.as_ptr() ) -> Result<Self> {
}; let val = {
let inner = let v = val.as_ref();
unsafe { xlib::XInternAtom(display.as_raw(), val, if only_if_exists { 1 } else { 0 }) }; let s = CString::new(v).unwrap();
Ok(Atom { inner }) s.as_ptr()
} };
let inner = unsafe {
xlib::XInternAtom(
display.as_raw(),
val,
if only_if_exists { 1 } else { 0 },
)
};
Ok(Atom { inner })
}
/// Create a new Atom object from an existing handle /// Create a new Atom object from an existing handle
pub fn from(handle: xlib::Atom) -> Self { pub fn from(handle: xlib::Atom) -> Self {
Atom { inner: handle } Atom { inner: handle }
} }
/// Get the handle /// Get the handle
pub fn as_raw(&self) -> xlib::Atom { pub fn as_raw(&self) -> xlib::Atom {
self.inner self.inner
} }
} }

View file

@ -5,13 +5,13 @@ use crate::xlib::{Display, IntoInt};
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Debug)] #[derivative(Debug)]
pub struct Colormap<'a> { pub struct Colormap<'a> {
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
pub display: &'a Display, pub display: &'a Display,
pub inner: xlib::Colormap, pub inner: xlib::Colormap,
} }
impl<'a> IntoInt for Colormap<'a> { impl<'a> IntoInt for Colormap<'a> {
fn into_int(&self) -> u64 { fn into_int(&self) -> u64 {
self.inner self.inner
} }
} }

View file

@ -6,19 +6,19 @@ use crate::xlib::{Display, IntoInt};
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Debug)] #[derivative(Debug)]
pub struct Cursor<'a> { pub struct Cursor<'a> {
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
pub display: &'a Display, pub display: &'a Display,
pub inner: xlib::Cursor, pub inner: xlib::Cursor,
} }
impl<'a> Drop for Cursor<'a> { impl<'a> Drop for Cursor<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { xlib::XFreeCursor(self.display.inner, self.inner) }; unsafe { xlib::XFreeCursor(self.display.inner, self.inner) };
} }
} }
impl<'a> IntoInt for Cursor<'a> { impl<'a> IntoInt for Cursor<'a> {
fn into_int(&self) -> u64 { fn into_int(&self) -> u64 {
self.inner self.inner
} }
} }

View file

@ -15,231 +15,235 @@ use super::window::Window;
/// A connection to an X server. /// A connection to an X server.
pub struct Display { pub struct Display {
pub(crate) inner: *mut xlib::Display, pub(crate) inner: *mut xlib::Display,
} }
pub struct Grab(pub(crate) *mut xlib::Display); pub struct Grab(pub(crate) *mut xlib::Display);
/// Something that's part of a display. /// Something that's part of a display.
pub trait GetDisplay { pub trait GetDisplay {
/// Get the current display /// Get the current display
fn get_display(&self) -> *mut xlib::Display; fn get_display(&self) -> *mut xlib::Display;
} }
impl Display { impl Display {
/// Opens a new connection to an X server. /// Opens a new connection to an X server.
/// ///
/// On POSIX-conformant systems, the display name or DISPLAY environment /// On POSIX-conformant systems, the display name or DISPLAY environment
/// variable can be a string in the format: /// variable can be a string in the format:
/// ///
/// hostname:number.screen_number /// hostname:number.screen_number
pub fn connect(display_name: impl AsRef<str>) -> Result<Display> { pub fn connect(display_name: impl AsRef<str>) -> Result<Display> {
let display_name = CString::new(display_name.as_ref()).unwrap(); let display_name = CString::new(display_name.as_ref()).unwrap();
let inner = unsafe { xlib::XOpenDisplay(display_name.as_ptr()) }; let inner = unsafe { xlib::XOpenDisplay(display_name.as_ptr()) };
if inner.is_null() { if inner.is_null() {
return Err(Error::DisplayOpenError); return Err(Error::DisplayOpenError);
}
Ok(Display { inner })
} }
Ok(Display { inner })
}
/// Create a Display for an existing connection /// Create a Display for an existing connection
pub fn from_handle(handle: u64) -> Self { pub fn from_handle(handle: u64) -> Self {
Display { Display {
inner: handle as *mut xlib::Display, inner: handle as *mut xlib::Display,
}
} }
}
/// Grab /// Grab
pub fn grab(&self) -> Grab { pub fn grab(&self) -> Grab {
unsafe { xlib::XGrabServer(self.inner) }; unsafe { xlib::XGrabServer(self.inner) };
Grab(self.inner) Grab(self.inner)
}
/// Wrapper around XCreateFontCursor
pub fn create_font_cursor(&self, shape: u32) -> Result<Cursor> {
let cursor =
unsafe { xlib::XCreateFontCursor(self.inner, shape) as xlib::Cursor };
if cursor == 0 {
return Err(Error::CreateCursorError);
} }
Ok(Cursor {
display: self,
inner: cursor,
})
}
/// Wrapper around XCreateFontCursor /// Get the next event, blocks until an event is reached.
pub fn create_font_cursor(&self, shape: u32) -> Result<Cursor> { pub fn next_event(&self) -> Result<Event> {
let cursor = unsafe { xlib::XCreateFontCursor(self.inner, shape) as xlib::Cursor }; debug!("fishing for next event...");
if cursor == 0 { let mut event = MaybeUninit::uninit();
return Err(Error::CreateCursorError); let ptr = event.as_mut_ptr();
} unsafe { xlib::XNextEvent(self.inner, ptr) };
Ok(Cursor { // TODO: check to make sure this isn't null
display: self, let event = unsafe { event.assume_init() };
inner: cursor, debug!("event: {:?}", event);
}) unsafe { Event::from_raw(event) }
}
/// Returns the number of events that are still pending
pub fn pending(&self) -> Result<i32> {
Ok(unsafe { xlib::XPending(self.inner) })
}
/// Gets the raw X Display handle
pub fn as_raw(&self) -> *mut xlib::Display {
self.inner
}
/// Gets the default visual
pub fn default_visual(&self, screen: i32) -> Visual {
let visual = unsafe { xlib::XDefaultVisual(self.inner, screen) };
Visual { inner: visual }
}
/// Returns the root window for the given screen.
pub fn get_root_window(&self, screen: i32) -> Result<Window> {
let inner = unsafe { xlib::XRootWindow(self.inner, screen) };
if inner == 0 {
return Err(Error::GetWindowError);
} }
let window = Window {
display: self,
inner,
};
Ok(window)
}
/// Get the next event, blocks until an event is reached. /// Returns the root window for the default screen.
pub fn next_event(&self) -> Result<Event> { pub fn get_default_root_window(&self) -> Result<Window> {
debug!("fishing for next event..."); let inner = unsafe { xlib::XDefaultRootWindow(self.inner) };
let mut event = MaybeUninit::uninit(); if inner == 0 {
let ptr = event.as_mut_ptr(); return Err(Error::GetWindowError);
unsafe { xlib::XNextEvent(self.inner, ptr) };
// TODO: check to make sure this isn't null
let event = unsafe { event.assume_init() };
debug!("event: {:?}", event);
unsafe { Event::from_raw(event) }
} }
let window = Window {
display: self,
inner,
};
Ok(window)
}
/// Returns the number of events that are still pending /// Translate coordinates relative to w1 to coordinates relative to w2.
pub fn pending(&self) -> Result<i32> { /// If the coordinates are contained in a mapped child of the destination
Ok(unsafe { xlib::XPending(self.inner) }) /// window, the third return value will hold that child window.
pub fn translate_coordinates(
&self,
w1: Window,
x: i32,
y: i32,
w2: Window,
) -> Result<(i32, i32, Option<Window>)> {
let mut rx = 0;
let mut ry = 0;
let mut child_return: xlib::Window = 0;
let status = unsafe {
xlib::XTranslateCoordinates(
self.inner,
w1.inner,
w2.inner,
x,
y,
&mut rx,
&mut ry,
&mut child_return,
)
};
if status == 0 {
return Err(Error::TranslateCoordinatesError);
} }
let child = match child_return {
0 => None,
val => Some(Window {
display: self,
inner: val,
}),
};
Ok((rx, ry, child))
}
/// Gets the raw X Display handle /// Sync
pub fn as_raw(&self) -> *mut xlib::Display { pub fn sync(&self, discard: bool) {
self.inner unsafe { xlib::XSync(self.inner, if discard { 1 } else { 0 }) };
} }
/// Gets the default visual /// Returns the focus window and the current focus state.
pub fn default_visual(&self, screen: i32) -> Visual { pub fn get_input_focus(&self) -> Result<(Window, i32)> {
let visual = unsafe { xlib::XDefaultVisual(self.inner, screen) }; let mut focus_return: xlib::Window = 0;
Visual { inner: visual } let mut revert_to_return = 0;
} unsafe {
xlib::XGetInputFocus(self.inner, &mut focus_return, &mut revert_to_return)
};
let window = Window {
display: self,
inner: focus_return,
};
return Ok((window, revert_to_return));
}
/// Returns the root window for the given screen. /// Query extension
pub fn get_root_window(&self, screen: i32) -> Result<Window> { pub fn query_extension(&self, name: impl AsRef<str>) -> Result<()> {
let inner = unsafe { xlib::XRootWindow(self.inner, screen) }; let name = ffi::c_char_star_from_string(name)?;
if inner == 0 { let major_opcode_return = ptr::null_mut();
return Err(Error::GetWindowError); let first_event_return = ptr::null_mut();
} let first_error_return = ptr::null_mut();
let window = Window { let _result = unsafe {
display: self, xlib::XQueryExtension(
inner, self.inner,
}; name,
Ok(window) major_opcode_return,
} first_event_return,
first_error_return,
)
};
// TODO: check reuslt
/// Returns the root window for the default screen. unimplemented!()
pub fn get_default_root_window(&self) -> Result<Window> { }
let inner = unsafe { xlib::XDefaultRootWindow(self.inner) };
if inner == 0 {
return Err(Error::GetWindowError);
}
let window = Window {
display: self,
inner,
};
Ok(window)
}
/// Translate coordinates relative to w1 to coordinates relative to w2. /// List extensions
/// If the coordinates are contained in a mapped child of the destination window, the third return pub fn list_extensions(&self) -> Result<ListExtensions> {
/// value will hold that child window. let nextensions_return = ptr::null_mut();
pub fn translate_coordinates( let result =
&self, unsafe { xlib::XListExtensions(self.inner, nextensions_return) };
w1: Window, // TODO: check result null
x: i32, // TODO: check nextensions_return
y: i32, let nextensions_return = unsafe { *nextensions_return } as isize;
w2: Window, Ok(ListExtensions(&self, result, nextensions_return))
) -> Result<(i32, i32, Option<Window>)> { }
let mut rx = 0;
let mut ry = 0;
let mut child_return: xlib::Window = 0;
let status = unsafe {
xlib::XTranslateCoordinates(
self.inner,
w1.inner,
w2.inner,
x,
y,
&mut rx,
&mut ry,
&mut child_return,
)
};
if status == 0 {
return Err(Error::TranslateCoordinatesError);
}
let child = match child_return {
0 => None,
val => Some(Window {
display: self,
inner: val,
}),
};
Ok((rx, ry, child))
}
/// Sync
pub fn sync(&self, discard: bool) {
unsafe { xlib::XSync(self.inner, if discard { 1 } else { 0 }) };
}
/// Returns the focus window and the current focus state.
pub fn get_input_focus(&self) -> Result<(Window, i32)> {
let mut focus_return: xlib::Window = 0;
let mut revert_to_return = 0;
unsafe { xlib::XGetInputFocus(self.inner, &mut focus_return, &mut revert_to_return) };
let window = Window {
display: self,
inner: focus_return,
};
return Ok((window, revert_to_return));
}
/// Query extension
pub fn query_extension(&self, name: impl AsRef<str>) -> Result<()> {
let name = ffi::c_char_star_from_string(name)?;
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,
major_opcode_return,
first_event_return,
first_error_return,
)
};
// TODO: check reuslt
unimplemented!()
}
/// List extensions
pub fn list_extensions(&self) -> Result<ListExtensions> {
let nextensions_return = ptr::null_mut();
let result = unsafe { xlib::XListExtensions(self.inner, nextensions_return) };
// TODO: check result null
// TODO: check nextensions_return
let nextensions_return = unsafe { *nextensions_return } as isize;
Ok(ListExtensions(&self, result, nextensions_return))
}
} }
impl Drop for Display { impl Drop for Display {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { xlib::XCloseDisplay(self.inner) }; unsafe { xlib::XCloseDisplay(self.inner) };
} }
} }
impl Drop for Grab { impl Drop for Grab {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { xlib::XUngrabServer(self.0) }; unsafe { xlib::XUngrabServer(self.0) };
} }
} }
pub struct ListExtensions<'a>(&'a Display, *mut *mut c_char, isize); pub struct ListExtensions<'a>(&'a Display, *mut *mut c_char, isize);
impl<'a> ListExtensions<'a> { impl<'a> ListExtensions<'a> {
pub fn iter(&self) -> ListExtensionsIter { pub fn iter(&self) -> ListExtensionsIter {
ListExtensionsIter(self, 0) ListExtensionsIter(self, 0)
} }
} }
pub struct ListExtensionsIter<'a>(&'a ListExtensions<'a>, isize); pub struct ListExtensionsIter<'a>(&'a ListExtensions<'a>, isize);
impl<'a> Iterator for ListExtensionsIter<'a> { impl<'a> Iterator for ListExtensionsIter<'a> {
type Item = String; type Item = String;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
if self.1 >= (self.0).2 { if self.1 >= (self.0).2 {
return None; return None;
}
// TODO: check for null
let base = unsafe { *(self.0).1.offset(self.1) };
let s = unsafe { ffi::string_from_c_char_star(base).unwrap() };
// TODO: don't unwrap here
Some(s)
} }
// TODO: check for null
let base = unsafe { *(self.0).1.offset(self.1) };
let s = unsafe { ffi::string_from_c_char_star(base).unwrap() };
// TODO: don't unwrap here
Some(s)
}
} }

View file

@ -8,23 +8,23 @@ use super::image::Image;
/// Anything that's drawable /// Anything that's drawable
pub trait Drawable: GetDisplay { pub trait Drawable: GetDisplay {
/// Get drawable handle /// Get drawable handle
fn as_drawable(&self) -> xlib::Drawable; fn as_drawable(&self) -> xlib::Drawable;
/// Capture a snapshot of this drawable, clipped by rect. /// Capture a snapshot of this drawable, clipped by rect.
fn get_image(&self, rect: Rectangle) -> Result<Image> { fn get_image(&self, rect: Rectangle) -> Result<Image> {
let image = unsafe { let image = unsafe {
xlib::XGetImage( xlib::XGetImage(
self.get_display(), self.get_display(),
self.as_drawable(), self.as_drawable(),
rect.x as i32, rect.x as i32,
rect.y as i32, rect.y as i32,
rect.width, rect.width,
rect.height, rect.height,
0xffffffff, 0xffffffff,
xlib::ZPixmap, xlib::ZPixmap,
) )
}; };
Ok(Image { inner: image }) Ok(Image { inner: image })
} }
} }

View file

@ -5,33 +5,33 @@ use crate::errors::{Error, Result};
/// An x11 Event /// An x11 Event
#[derive(Debug)] #[derive(Debug)]
pub struct Event { pub struct Event {
inner: xlib::XEvent, inner: xlib::XEvent,
kind: EventKind, kind: EventKind,
} }
/// Type of event /// Type of event
#[derive(Debug)] #[derive(Debug)]
pub enum EventKind { pub enum EventKind {
KeyPress(KeyEvent), KeyPress(KeyEvent),
KeyRelease(KeyEvent), KeyRelease(KeyEvent),
} }
impl Event { impl Event {
/// Returns the EventKind of this event /// Returns the EventKind of this event
pub fn kind(&self) -> &EventKind { pub fn kind(&self) -> &EventKind {
&self.kind &self.kind
} }
pub(super) unsafe fn from_raw(event: xlib::XEvent) -> Result<Self> { pub(super) unsafe fn from_raw(event: xlib::XEvent) -> Result<Self> {
let inner = event; let inner = event;
debug!("event type: {:?}", event.type_); debug!("event type: {:?}", event.type_);
let kind = match event.type_ { let kind = match event.type_ {
xlib::KeyPress => EventKind::KeyPress(KeyEvent(event.key)), xlib::KeyPress => EventKind::KeyPress(KeyEvent(event.key)),
xlib::KeyRelease => EventKind::KeyRelease(KeyEvent(event.key)), xlib::KeyRelease => EventKind::KeyRelease(KeyEvent(event.key)),
other => return Err(Error::InvalidEventType(other)), other => return Err(Error::InvalidEventType(other)),
}; };
Ok(Event { inner, kind }) Ok(Event { inner, kind })
} }
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -4,10 +4,11 @@ use crate::errors::{Error, Result};
/// A handle to an XImage /// A handle to an XImage
/// ///
/// Xlib provides some structures to perform operations on images. However, because there are /// Xlib provides some structures to perform operations on images. However,
/// different in-memory representations, working with images is actually totally fucked. If you /// because there are different in-memory representations, working with images
/// want to maintain your sanity, do NOT pry into the raw data buffer of an Image. Instead, index /// is actually totally fucked. If you want to maintain your sanity, do NOT pry
/// into the image using `PixBuffer::get_pixel` which already performs the conversion and returns a /// into the raw data buffer of an Image. Instead, index into the image using
/// `PixBuffer::get_pixel` which already performs the conversion and returns a
/// tuple of RGB values. /// tuple of RGB values.
/// ///
/// Peeking into the raw data buffer of an Image /// Peeking into the raw data buffer of an Image
@ -15,118 +16,119 @@ use crate::errors::{Error, Result};
/// ///
/// Images consist of lines of longs in row-major form. /// Images consist of lines of longs in row-major form.
pub struct Image { pub struct Image {
pub inner: *mut xlib::XImage, pub inner: *mut xlib::XImage,
} }
/// Image byte order /// Image byte order
pub enum ImageByteOrder { pub enum ImageByteOrder {
/// Least significant byte first /// Least significant byte first
LSBFirst, LSBFirst,
/// Most significant byte first /// Most significant byte first
MSBFirst, MSBFirst,
} }
impl Image { impl Image {
/// Get the size (in bytes) of the data buffer. /// Get the size (in bytes) of the data buffer.
#[inline] #[inline]
pub fn get_size(&self) -> usize { pub fn get_size(&self) -> usize {
4 * self.get_width() as usize * self.get_height() as usize 4 * self.get_width() as usize * self.get_height() as usize
} }
/// Get the image width /// Get the image width
#[inline] #[inline]
pub fn get_width(&self) -> u32 { pub fn get_width(&self) -> u32 {
unsafe { (*self.inner).width as u32 } unsafe { (*self.inner).width as u32 }
} }
/// Get the image height /// Get the image height
#[inline] #[inline]
pub fn get_height(&self) -> u32 { pub fn get_height(&self) -> u32 {
unsafe { (*self.inner).height as u32 } unsafe { (*self.inner).height as u32 }
} }
/// Get the image depth /// Get the image depth
#[inline] #[inline]
pub fn get_depth(&self) -> u32 { pub fn get_depth(&self) -> u32 {
unsafe { (*self.inner).depth as u32 } unsafe { (*self.inner).depth as u32 }
} }
/// Get byte order (LSB or MSB) /// Get byte order (LSB or MSB)
#[inline] #[inline]
pub fn get_byte_order(&self) -> Result<ImageByteOrder> { pub fn get_byte_order(&self) -> Result<ImageByteOrder> {
let byte_order = unsafe { (*self.inner).byte_order }; let byte_order = unsafe { (*self.inner).byte_order };
match byte_order { match byte_order {
xlib::LSBFirst => Ok(ImageByteOrder::LSBFirst), xlib::LSBFirst => Ok(ImageByteOrder::LSBFirst),
xlib::MSBFirst => Ok(ImageByteOrder::MSBFirst), xlib::MSBFirst => Ok(ImageByteOrder::MSBFirst),
order => Err(Error::InvalidByteOrder(order)), order => Err(Error::InvalidByteOrder(order)),
}
} }
}
fn get_rgb_masks(&self) -> (u64, u64, u64) { fn get_rgb_masks(&self) -> (u64, u64, u64) {
let red_mask = unsafe { (*self.inner).red_mask }; let red_mask = unsafe { (*self.inner).red_mask };
let green_mask = unsafe { (*self.inner).green_mask }; let green_mask = unsafe { (*self.inner).green_mask };
let blue_mask = unsafe { (*self.inner).blue_mask }; let blue_mask = unsafe { (*self.inner).blue_mask };
(red_mask, blue_mask, green_mask) (red_mask, blue_mask, green_mask)
} }
/// Produces a PixBuffer. /// Produces a PixBuffer.
pub fn buffer(&self) -> PixBuffer { pub fn buffer(&self) -> PixBuffer {
let size = self.get_size(); let size = self.get_size();
let buf = unsafe { (*self.inner).data as *mut u8 }; let buf = unsafe { (*self.inner).data as *mut u8 };
let masks = self.get_rgb_masks(); let masks = self.get_rgb_masks();
PixBuffer { PixBuffer {
buf, buf,
size, size,
masks, masks,
image: self, image: self,
}
} }
}
} }
impl Drop for Image { impl Drop for Image {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { xlib::XDestroyImage(self.inner) }; unsafe { xlib::XDestroyImage(self.inner) };
} }
} }
/// The buffer pointed to by an XImage. /// The buffer pointed to by an XImage.
pub struct PixBuffer<'a> { pub struct PixBuffer<'a> {
/// The raw pointer to the buffer /// The raw pointer to the buffer
pub buf: *mut u8, pub buf: *mut u8,
/// The size of the buffer /// The size of the buffer
pub size: usize, pub size: usize,
/// Rgb masks /// Rgb masks
pub masks: (u64, u64, u64), pub masks: (u64, u64, u64),
pub image: &'a Image, pub image: &'a Image,
} }
impl<'a> PixBuffer<'a> { impl<'a> PixBuffer<'a> {
/// Retrieve a direct reference to the internal buffer. /// Retrieve a direct reference to the internal buffer.
pub fn get_buffer(&self) -> &[u8] { pub fn get_buffer(&self) -> &[u8] {
unsafe { std::slice::from_raw_parts(self.buf, self.size) } unsafe { std::slice::from_raw_parts(self.buf, self.size) }
} }
/// Gets the rgb value of a particular pixel, after performing conversion /// Gets the rgb value of a particular pixel, after performing conversion
/// ///
/// This uses XImage's get_pixel function to retrieve the pixel value, and then the various /// This uses XImage's get_pixel function to retrieve the pixel value, and
/// red/green/blue masks to actually perform the conversions, and fails (returns `None`) if the /// then the various red/green/blue masks to actually perform the
/// (x, y) coordinates given aren't actually in the image. /// conversions, and fails (returns `None`) if the (x, y) coordinates given
#[inline] /// aren't actually in the image.
pub fn get_pixel(&self, x: u32, y: u32) -> Option<(u8, u8, u8)> { #[inline]
let get_pixel = unsafe { (*self.image.inner).funcs.get_pixel }.unwrap(); pub fn get_pixel(&self, x: u32, y: u32) -> Option<(u8, u8, u8)> {
let pixel = unsafe { get_pixel(self.image.inner, x as i32, y as i32) }; let get_pixel = unsafe { (*self.image.inner).funcs.get_pixel }.unwrap();
let pixel = unsafe { get_pixel(self.image.inner, x as i32, y as i32) };
let red_mask = unsafe { (*self.image.inner).red_mask }; let red_mask = unsafe { (*self.image.inner).red_mask };
let green_mask = unsafe { (*self.image.inner).green_mask }; let green_mask = unsafe { (*self.image.inner).green_mask };
let blue_mask = unsafe { (*self.image.inner).blue_mask }; let blue_mask = unsafe { (*self.image.inner).blue_mask };
let red = ((pixel & red_mask) >> 16) as u8; let red = ((pixel & red_mask) >> 16) as u8;
let green = ((pixel & green_mask) >> 8) as u8; let green = ((pixel & green_mask) >> 8) as u8;
let blue = ((pixel & blue_mask) >> 0) as u8; let blue = ((pixel & blue_mask) >> 0) as u8;
Some((red, green, blue)) Some((red, green, blue))
} }
} }

View file

@ -1,8 +1,8 @@
//! The core of x11 //! The core of x11
mod atom; mod atom;
mod cursor;
mod colormap; mod colormap;
mod cursor;
mod display; mod display;
mod drawable; mod drawable;
mod event; mod event;
@ -14,58 +14,60 @@ mod window;
use x11::xlib; use x11::xlib;
pub use self::atom::Atom; pub use self::atom::Atom;
pub use self::colormap::Colormap;
pub use self::cursor::Cursor; pub use self::cursor::Cursor;
pub use self::display::{Display, GetDisplay}; pub use self::display::{Display, GetDisplay};
pub use self::colormap::Colormap;
pub use self::drawable::Drawable; pub use self::drawable::Drawable;
pub use self::event::{Event, EventKind}; pub use self::event::{Event, EventKind};
pub use self::image::{Image, ImageByteOrder, PixBuffer}; pub use self::image::{Image, ImageByteOrder, PixBuffer};
pub use self::pixmap::Pixmap; pub use self::pixmap::Pixmap;
pub use self::visual::{Visual, VisualInfo}; pub use self::visual::{Visual, VisualInfo};
pub use self::window::{EventMask, SetWindowAttributes, Window, WindowAttributes}; pub use self::window::{
EventMask, SetWindowAttributes, Window, WindowAttributes,
};
#[derive(Debug)] #[derive(Debug)]
pub enum Inherit<T> { pub enum Inherit<T> {
Value(T), Value(T),
CopyFromParent, CopyFromParent,
} }
impl<T> Default for Inherit<T> { impl<T> Default for Inherit<T> {
fn default() -> Self { fn default() -> Self {
Inherit::CopyFromParent Inherit::CopyFromParent
} }
} }
pub trait IntoInt { pub trait IntoInt {
fn into_int(&self) -> u64; fn into_int(&self) -> u64;
} }
impl<T: IntoInt> IntoInt for Inherit<T> { impl<T: IntoInt> IntoInt for Inherit<T> {
fn into_int(&self) -> u64 { fn into_int(&self) -> u64 {
match self { match self {
Inherit::Value(t) => t.into_int(), Inherit::Value(t) => t.into_int(),
Inherit::CopyFromParent => xlib::CopyFromParent as u64, Inherit::CopyFromParent => xlib::CopyFromParent as u64,
}
} }
}
} }
#[derive(Debug)] #[derive(Debug)]
pub enum Noneable<T> { pub enum Noneable<T> {
Value(T), Value(T),
None, None,
} }
impl<T> Default for Noneable<T> { impl<T> Default for Noneable<T> {
fn default() -> Self { fn default() -> Self {
Noneable::None Noneable::None
} }
} }
impl<T: IntoInt> IntoInt for Noneable<T> { impl<T: IntoInt> IntoInt for Noneable<T> {
fn into_int(&self) -> u64 { fn into_int(&self) -> u64 {
match self { match self {
Noneable::Value(t) => t.into_int(), Noneable::Value(t) => t.into_int(),
Noneable::None => 0, Noneable::None => 0,
}
} }
}
} }

View file

@ -12,19 +12,19 @@ use crate::xlib::{Display, IntoInt};
#[derive(Derivative)] #[derive(Derivative)]
#[derivative(Debug)] #[derivative(Debug)]
pub struct Pixmap<'a> { pub struct Pixmap<'a> {
#[derivative(Debug = "ignore")] #[derivative(Debug = "ignore")]
pub(crate) display: &'a Display, pub(crate) display: &'a Display,
pub(crate) inner: xlib::Pixmap, pub(crate) inner: xlib::Pixmap,
} }
impl<'a> Drop for Pixmap<'a> { impl<'a> Drop for Pixmap<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { xlib::XFreePixmap(self.display.inner, self.inner) }; unsafe { xlib::XFreePixmap(self.display.inner, self.inner) };
} }
} }
impl<'a> IntoInt for Pixmap<'a> { impl<'a> IntoInt for Pixmap<'a> {
fn into_int(&self) -> u64 { fn into_int(&self) -> u64 {
self.inner self.inner
} }
} }

View file

@ -4,32 +4,32 @@ use super::display::Display;
/// A wrapper around a Visual /// A wrapper around a Visual
pub struct Visual { pub struct Visual {
pub(super) inner: *mut xlib::Visual, pub(super) inner: *mut xlib::Visual,
} }
impl Visual { impl Visual {
/// Gets the raw handle to the x11 Visual /// Gets the raw handle to the x11 Visual
pub fn as_raw(&self) -> *mut xlib::Visual { pub fn as_raw(&self) -> *mut xlib::Visual {
self.inner self.inner
} }
/// Gets the default visual /// Gets the default visual
pub fn default(display: &Display, screen: i32) -> Self { pub fn default(display: &Display, screen: i32) -> Self {
let inner = unsafe { xlib::XDefaultVisual(display.as_raw(), screen) }; let inner = unsafe { xlib::XDefaultVisual(display.as_raw(), screen) };
Visual { inner } Visual { inner }
} }
} }
pub struct VisualInfo { pub struct VisualInfo {
pub(super) inner: *mut xlib::XVisualInfo, pub(super) inner: *mut xlib::XVisualInfo,
} }
impl VisualInfo { impl VisualInfo {
pub fn from_raw(inner: *mut xlib::XVisualInfo) -> Self { pub fn from_raw(inner: *mut xlib::XVisualInfo) -> Self {
VisualInfo { inner } VisualInfo { inner }
} }
pub fn as_raw(&self) -> *mut xlib::XVisualInfo { pub fn as_raw(&self) -> *mut xlib::XVisualInfo {
self.inner self.inner
} }
} }

View file

@ -5,184 +5,196 @@ use x11::xlib;
use crate::errors::{Error, Result}; use crate::errors::{Error, Result};
use crate::rect::Rectangle; use crate::rect::Rectangle;
use crate::xlib::{Colormap, Atom, Cursor, Display, Drawable, GetDisplay, Image, Inherit, IntoInt, Noneable, Pixmap}; use crate::xlib::{
Atom, Colormap, Cursor, Display, Drawable, GetDisplay, Image, Inherit,
IntoInt, Noneable, Pixmap,
};
/// A wrapper around a window handle. /// A wrapper around a window handle.
#[derive(Clone)] #[derive(Clone)]
pub struct Window<'a> { pub struct Window<'a> {
pub display: &'a Display, pub display: &'a Display,
pub inner: xlib::Window, pub inner: xlib::Window,
} }
impl<'a> Window<'a> { impl<'a> Window<'a> {
/// Create a new window /// Create a new window
pub fn create( pub fn create(
display: &'a Display, display: &'a Display,
parent: Option<Window>, parent: Option<Window>,
location: Rectangle, location: Rectangle,
set_attributes: SetWindowAttributes, set_attributes: SetWindowAttributes,
) -> Result<Window<'a>> { ) -> Result<Window<'a>> {
let parent = match parent { let parent = match parent {
Some(parent) => parent, Some(parent) => parent,
None => display.get_default_root_window()?, None => display.get_default_root_window()?,
}; };
let visual = display.default_visual(0); let visual = display.default_visual(0);
let window = unsafe { let window = unsafe {
xlib::XCreateWindow( xlib::XCreateWindow(
display.as_raw(), display.as_raw(),
parent.as_raw(), parent.as_raw(),
location.x as i32, location.x as i32,
location.y as i32, location.y as i32,
location.width, location.width,
location.height, location.height,
0, 0,
0, 0,
0, 0,
visual.as_raw(), visual.as_raw(),
0, 0,
set_attributes.into_raw(), set_attributes.into_raw(),
) )
}; };
Ok(Window { Ok(Window {
display, display,
inner: window, inner: window,
}) })
} }
/// Create a new Window instance from an existing ID /// Create a new Window instance from an existing ID
pub fn create_from_handle(display: &Display, id: u64) -> Result<Window> { pub fn create_from_handle(display: &Display, id: u64) -> Result<Window> {
Ok(Window { display, inner: id }) Ok(Window { display, inner: id })
} }
/// Get window attributes. /// Get window attributes.
pub fn get_attributes(&self) -> Result<WindowAttributes> { pub fn get_attributes(&self) -> Result<WindowAttributes> {
let attr = unsafe { let attr = unsafe {
libc::malloc(mem::size_of::<xlib::XWindowAttributes>()) as *mut xlib::XWindowAttributes libc::malloc(mem::size_of::<xlib::XWindowAttributes>())
}; as *mut xlib::XWindowAttributes
let result = unsafe { xlib::XGetWindowAttributes(self.display.as_raw(), self.inner, attr) }; };
match result { let result = unsafe {
0 => Err(Error::GetAttributesError), xlib::XGetWindowAttributes(self.display.as_raw(), self.inner, attr)
_ => Ok(WindowAttributes { };
display: self.display, match result {
inner: attr, 0 => Err(Error::GetAttributesError),
}), _ => Ok(WindowAttributes {
} display: self.display,
inner: attr,
}),
} }
}
/// Get the raw window handle /// Get the raw window handle
pub fn as_raw(&self) -> xlib::Window { pub fn as_raw(&self) -> xlib::Window {
self.inner self.inner
} }
/// Gets the image from this window using XGetImage. /// Gets the image from this window using XGetImage.
pub fn get_image(&self) -> Result<Image> { pub fn get_image(&self) -> Result<Image> {
let attr = self.get_attributes()?; let attr = self.get_attributes()?;
Drawable::get_image( Drawable::get_image(
self, self,
Rectangle::new( Rectangle::new(
attr.get_x(), attr.get_x(),
attr.get_y(), attr.get_y(),
attr.get_width(), attr.get_width(),
attr.get_height(), attr.get_height(),
), ),
) )
} }
/// Change window property /// Change window property
// TODO: make it more general // TODO: make it more general
pub fn change_property(&self, key: &Atom, val: &Atom) { pub fn change_property(&self, key: &Atom, val: &Atom) {
use std::mem::transmute; use std::mem::transmute;
let v = val.as_raw(); let v = val.as_raw();
unsafe { unsafe {
xlib::XChangeProperty( xlib::XChangeProperty(
self.display.as_raw(), self.display.as_raw(),
self.inner, self.inner,
key.as_raw(), key.as_raw(),
xlib::XA_ATOM, xlib::XA_ATOM,
32, 32,
xlib::PropModeReplace, xlib::PropModeReplace,
transmute(&v), transmute(&v),
1, 1,
); );
}
} }
}
/// "Maps", or shows a window to a display. /// "Maps", or shows a window to a display.
pub fn map(&self) { pub fn map(&self) {
unsafe { xlib::XMapWindow(self.display.as_raw(), self.inner) }; unsafe { xlib::XMapWindow(self.display.as_raw(), self.inner) };
} }
/// Requests for events from the server matching a particular event mask. /// Requests for events from the server matching a particular event mask.
/// ///
/// If this function is not called, events will not be reported. /// If this function is not called, events will not be reported.
pub fn select_input(&self, event_mask: EventMask) { pub fn select_input(&self, event_mask: EventMask) {
debug!("event mask: {:?}", event_mask); debug!("event mask: {:?}", event_mask);
unsafe { xlib::XSelectInput(self.display.as_raw(), self.inner, event_mask.bits as i64) }; unsafe {
} xlib::XSelectInput(
self.display.as_raw(),
self.inner,
event_mask.bits() as i64,
)
};
}
} }
impl<'a> GetDisplay for Window<'a> { impl<'a> GetDisplay for Window<'a> {
fn get_display(&self) -> *mut xlib::Display { fn get_display(&self) -> *mut xlib::Display {
self.display.as_raw() self.display.as_raw()
} }
} }
impl<'a> Drawable for Window<'a> { impl<'a> Drawable for Window<'a> {
fn as_drawable(&self) -> xlib::Drawable { fn as_drawable(&self) -> xlib::Drawable {
self.inner self.inner
} }
} }
impl<'a> Drop for Window<'a> { impl<'a> Drop for Window<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { xlib::XDestroyWindow(self.display.as_raw(), self.inner) }; unsafe { xlib::XDestroyWindow(self.display.as_raw(), self.inner) };
} }
} }
/// Window Attributes /// Window Attributes
pub struct WindowAttributes<'a> { pub struct WindowAttributes<'a> {
pub(super) display: &'a Display, pub(super) display: &'a Display,
pub(self) inner: *mut xlib::XWindowAttributes, pub(self) inner: *mut xlib::XWindowAttributes,
} }
impl<'a> WindowAttributes<'a> { impl<'a> WindowAttributes<'a> {
/// Gets the width of the window /// Gets the width of the window
pub fn get_x(&self) -> i32 { pub fn get_x(&self) -> i32 {
unsafe { (*self.inner).x as i32 } unsafe { (*self.inner).x as i32 }
} }
/// Gets the height of the window /// Gets the height of the window
pub fn get_y(&self) -> i32 { pub fn get_y(&self) -> i32 {
unsafe { (*self.inner).y as i32 } unsafe { (*self.inner).y as i32 }
} }
/// Gets the width of the window /// Gets the width of the window
pub fn get_width(&self) -> u32 { pub fn get_width(&self) -> u32 {
unsafe { (*self.inner).width as u32 } unsafe { (*self.inner).width as u32 }
} }
/// Gets the height of the window /// Gets the height of the window
pub fn get_height(&self) -> u32 { pub fn get_height(&self) -> u32 {
unsafe { (*self.inner).height as u32 } unsafe { (*self.inner).height as u32 }
} }
/// Get the root window of this window /// Get the root window of this window
pub fn get_root(&self) -> Window { pub fn get_root(&self) -> Window {
Window { Window {
display: self.display, display: self.display,
inner: unsafe { (*self.inner).root }, inner: unsafe { (*self.inner).root },
}
} }
}
} }
impl<'a> Drop for WindowAttributes<'a> { impl<'a> Drop for WindowAttributes<'a> {
fn drop(&mut self) { fn drop(&mut self) {
unsafe { libc::free(self.inner as *mut libc::c_void) }; unsafe { libc::free(self.inner as *mut libc::c_void) };
} }
} }
bitflags! { bitflags! {
#[derive(Default)] #[derive(Default, Debug)]
pub struct EventMask: c_ulong { pub struct EventMask: c_ulong {
const NO_EVENT_MASK = 0; const NO_EVENT_MASK = 0;
const KEY_PRESS_MASK = (1<<0); const KEY_PRESS_MASK = (1<<0);
@ -215,111 +227,111 @@ bitflags! {
#[derive(Debug)] #[derive(Debug)]
pub struct SetWindowAttributes<'a> { pub struct SetWindowAttributes<'a> {
pub background_pixmap: BackgroundPixmap<'a>, pub background_pixmap: BackgroundPixmap<'a>,
pub background_pixel: c_ulong, pub background_pixel: c_ulong,
pub border_pixmap: Inherit<Pixmap<'a>>, pub border_pixmap: Inherit<Pixmap<'a>>,
pub border_pixel: c_ulong, pub border_pixel: c_ulong,
pub bit_gravity: i32, pub bit_gravity: i32,
pub win_gravity: i32, pub win_gravity: i32,
pub backing_store: BackingStore, pub backing_store: BackingStore,
pub backing_planes: u64, pub backing_planes: u64,
pub backing_pixel: u64, pub backing_pixel: u64,
pub save_under: bool, pub save_under: bool,
pub event_mask: EventMask, pub event_mask: EventMask,
pub do_not_propagate_mask: c_long, pub do_not_propagate_mask: c_long,
pub override_redirect: bool, pub override_redirect: bool,
pub colormap: Inherit<Colormap<'a>>, pub colormap: Inherit<Colormap<'a>>,
pub cursor: Noneable<Cursor<'a>>, pub cursor: Noneable<Cursor<'a>>,
} }
impl<'a> Default for SetWindowAttributes<'a> { impl<'a> Default for SetWindowAttributes<'a> {
fn default() -> Self { fn default() -> Self {
SetWindowAttributes { SetWindowAttributes {
background_pixmap: BackgroundPixmap::default(), background_pixmap: BackgroundPixmap::default(),
background_pixel: 0, background_pixel: 0,
border_pixmap: Inherit::default(), border_pixmap: Inherit::default(),
border_pixel: 0, border_pixel: 0,
bit_gravity: xlib::ForgetGravity, bit_gravity: xlib::ForgetGravity,
win_gravity: xlib::NorthWestGravity, win_gravity: xlib::NorthWestGravity,
backing_store: BackingStore::default(), backing_store: BackingStore::default(),
backing_planes: 0xffffffff, backing_planes: 0xffffffff,
backing_pixel: 0, backing_pixel: 0,
save_under: false, save_under: false,
event_mask: EventMask::empty(), event_mask: EventMask::empty(),
do_not_propagate_mask: 0, do_not_propagate_mask: 0,
override_redirect: false, override_redirect: false,
colormap: Inherit::default(), colormap: Inherit::default(),
cursor: Noneable::default(), cursor: Noneable::default(),
}
} }
}
} }
impl<'a> SetWindowAttributes<'a> { impl<'a> SetWindowAttributes<'a> {
pub fn into_raw(self) -> *mut xlib::XSetWindowAttributes { pub fn into_raw(self) -> *mut xlib::XSetWindowAttributes {
let result = xlib::XSetWindowAttributes { let result = xlib::XSetWindowAttributes {
background_pixmap: self.background_pixmap.into_int(), background_pixmap: self.background_pixmap.into_int(),
background_pixel: self.background_pixel, background_pixel: self.background_pixel,
border_pixmap: self.border_pixmap.into_int(), border_pixmap: self.border_pixmap.into_int(),
border_pixel: self.border_pixel, border_pixel: self.border_pixel,
bit_gravity: self.bit_gravity, bit_gravity: self.bit_gravity,
win_gravity: self.win_gravity, win_gravity: self.win_gravity,
backing_store: self.backing_store.into_raw(), backing_store: self.backing_store.into_raw(),
backing_planes: self.backing_planes, backing_planes: self.backing_planes,
backing_pixel: self.backing_pixel, backing_pixel: self.backing_pixel,
save_under: self.save_under.into(), save_under: self.save_under.into(),
event_mask: self.event_mask.bits() as i64, event_mask: self.event_mask.bits() as i64,
do_not_propagate_mask: self.do_not_propagate_mask, do_not_propagate_mask: self.do_not_propagate_mask,
override_redirect: self.override_redirect.into(), override_redirect: self.override_redirect.into(),
colormap: self.colormap.into_int(), colormap: self.colormap.into_int(),
cursor: self.cursor.into_int(), cursor: self.cursor.into_int(),
}; };
Box::into_raw(Box::new(result)) Box::into_raw(Box::new(result))
} }
} }
#[derive(Debug)] #[derive(Debug)]
pub enum BackgroundPixmap<'a> { pub enum BackgroundPixmap<'a> {
Background(Pixmap<'a>), Background(Pixmap<'a>),
None, None,
ParentRelative, ParentRelative,
} }
impl<'a> Default for BackgroundPixmap<'a> { impl<'a> Default for BackgroundPixmap<'a> {
fn default() -> Self { fn default() -> Self {
BackgroundPixmap::None BackgroundPixmap::None
} }
} }
impl<'a> IntoInt for BackgroundPixmap<'a> { impl<'a> IntoInt for BackgroundPixmap<'a> {
fn into_int(&self) -> u64 { fn into_int(&self) -> u64 {
match self { match self {
BackgroundPixmap::Background(pixmap) => pixmap.inner, BackgroundPixmap::Background(pixmap) => pixmap.inner,
BackgroundPixmap::None => 0, BackgroundPixmap::None => 0,
BackgroundPixmap::ParentRelative => xlib::ParentRelative as u64, BackgroundPixmap::ParentRelative => xlib::ParentRelative as u64,
}
} }
}
} }
#[derive(Debug)] #[derive(Debug)]
pub enum BackingStore { pub enum BackingStore {
NotUseful, NotUseful,
WhenMapped, WhenMapped,
Always, Always,
} }
impl Default for BackingStore { impl Default for BackingStore {
fn default() -> Self { fn default() -> Self {
BackingStore::NotUseful BackingStore::NotUseful
} }
} }
impl BackingStore { impl BackingStore {
fn into_raw(self) -> i32 { fn into_raw(self) -> i32 {
match self { match self {
BackingStore::NotUseful => xlib::NotUseful, BackingStore::NotUseful => xlib::NotUseful,
BackingStore::WhenMapped => xlib::WhenMapped, BackingStore::WhenMapped => xlib::WhenMapped,
BackingStore::Always => xlib::Always, BackingStore::Always => xlib::Always,
}
} }
}
} }

View file

@ -3,5 +3,5 @@ pub struct Picture {}
impl Picture {} impl Picture {}
impl Drop for Picture { impl Drop for Picture {
fn drop(&mut self) {} fn drop(&mut self) {}
} }