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 }

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

@ -30,7 +30,8 @@ pub trait GlxExtension {
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 =
unsafe { glx::glXQueryExtension(self.inner, 0 as *mut _, 0 as *mut _) };
Ok(result == 1) Ok(result == 1)
} }
@ -55,12 +56,15 @@ impl GlxExtension for Display {
} }
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 {
glx::glXMakeCurrent(self.as_raw(), drawable.as_raw(), ctx.as_raw())
};
Ok(()) 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 =
unsafe { glx::glXChooseVisual(self.as_raw(), screen, 0 as *mut _) };
Ok(VisualInfo::from_raw(visual)) Ok(VisualInfo::from_raw(visual))
} }
} }

View file

@ -11,7 +11,9 @@ pub struct ScreensInfo {
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 {
xinerama::XineramaQueryScreens(display.inner, &mut heads as *mut _)
};
// TODO: check for error // TODO: check for error
Ok(ScreensInfo { Ok(ScreensInfo {
inner, inner,

View file

@ -13,14 +13,23 @@ pub struct 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(
display: &Display,
val: impl AsRef<str>,
only_if_exists: bool,
) -> Result<Self> {
let val = { let val = {
let v = val.as_ref(); let v = val.as_ref();
let s = CString::new(v).unwrap(); let s = CString::new(v).unwrap();
s.as_ptr() s.as_ptr()
}; };
let inner = let inner = unsafe {
unsafe { xlib::XInternAtom(display.as_raw(), val, if only_if_exists { 1 } else { 0 }) }; xlib::XInternAtom(
display.as_raw(),
val,
if only_if_exists { 1 } else { 0 },
)
};
Ok(Atom { inner }) Ok(Atom { inner })
} }

View file

@ -57,7 +57,8 @@ impl Display {
/// Wrapper around XCreateFontCursor /// Wrapper around XCreateFontCursor
pub fn create_font_cursor(&self, shape: u32) -> Result<Cursor> { pub fn create_font_cursor(&self, shape: u32) -> Result<Cursor> {
let cursor = unsafe { xlib::XCreateFontCursor(self.inner, shape) as xlib::Cursor }; let cursor =
unsafe { xlib::XCreateFontCursor(self.inner, shape) as xlib::Cursor };
if cursor == 0 { if cursor == 0 {
return Err(Error::CreateCursorError); return Err(Error::CreateCursorError);
} }
@ -122,8 +123,8 @@ impl Display {
} }
/// Translate coordinates relative to w1 to coordinates relative to w2. /// Translate coordinates relative to w1 to coordinates relative to w2.
/// If the coordinates are contained in a mapped child of the destination window, the third return /// If the coordinates are contained in a mapped child of the destination
/// value will hold that child window. /// window, the third return value will hold that child window.
pub fn translate_coordinates( pub fn translate_coordinates(
&self, &self,
w1: Window, w1: Window,
@ -168,7 +169,9 @@ impl Display {
pub fn get_input_focus(&self) -> Result<(Window, i32)> { pub fn get_input_focus(&self) -> Result<(Window, i32)> {
let mut focus_return: xlib::Window = 0; let mut focus_return: xlib::Window = 0;
let mut revert_to_return = 0; let mut revert_to_return = 0;
unsafe { xlib::XGetInputFocus(self.inner, &mut focus_return, &mut revert_to_return) }; unsafe {
xlib::XGetInputFocus(self.inner, &mut focus_return, &mut revert_to_return)
};
let window = Window { let window = Window {
display: self, display: self,
inner: focus_return, inner: focus_return,
@ -199,7 +202,8 @@ impl Display {
/// List extensions /// List extensions
pub fn list_extensions(&self) -> Result<ListExtensions> { pub fn list_extensions(&self) -> Result<ListExtensions> {
let nextensions_return = ptr::null_mut(); let nextensions_return = ptr::null_mut();
let result = unsafe { xlib::XListExtensions(self.inner, nextensions_return) }; let result =
unsafe { xlib::XListExtensions(self.inner, nextensions_return) };
// TODO: check result null // TODO: check result null
// TODO: check nextensions_return // TODO: check nextensions_return
let nextensions_return = unsafe { *nextensions_return } as isize; let nextensions_return = unsafe { *nextensions_return } as isize;

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
@ -111,9 +112,10 @@ impl<'a> PixBuffer<'a> {
/// 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
/// aren't actually in the image.
#[inline] #[inline]
pub fn get_pixel(&self, x: u32, y: u32) -> Option<(u8, u8, u8)> { pub fn get_pixel(&self, x: u32, y: u32) -> Option<(u8, u8, u8)> {
let get_pixel = unsafe { (*self.image.inner).funcs.get_pixel }.unwrap(); let get_pixel = unsafe { (*self.image.inner).funcs.get_pixel }.unwrap();

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,15 +14,17 @@ 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> {

View file

@ -5,7 +5,10 @@ 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)]
@ -57,9 +60,12 @@ impl<'a> Window<'a> {
/// 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)
}; };
let result = unsafe { xlib::XGetWindowAttributes(self.display.as_raw(), self.inner, attr) };
match result { match result {
0 => Err(Error::GetAttributesError), 0 => Err(Error::GetAttributesError),
_ => Ok(WindowAttributes { _ => Ok(WindowAttributes {
@ -117,7 +123,13 @@ impl<'a> Window<'a> {
/// 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,
)
};
} }
} }
@ -182,7 +194,7 @@ impl<'a> Drop for WindowAttributes<'a> {
} }
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);