forked from michael/leanshot
Depend on safex11
This commit is contained in:
parent
6be5db5e57
commit
942d959b53
22 changed files with 46 additions and 853 deletions
35
Cargo.lock
generated
35
Cargo.lock
generated
|
@ -152,6 +152,16 @@ dependencies = [
|
|||
"byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "derivative"
|
||||
version = "2.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"syn 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "either"
|
||||
version = "1.5.3"
|
||||
|
@ -232,23 +242,13 @@ version = "0.4.0"
|
|||
dependencies = [
|
||||
"anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"image 0.23.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"leanshot_x11 0.2.0",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"png 0.14.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"safex11 0.0.4",
|
||||
"stderrlog 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "leanshot_x11"
|
||||
version = "0.2.0"
|
||||
dependencies = [
|
||||
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"x11 2.18.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.71"
|
||||
|
@ -434,6 +434,18 @@ dependencies = [
|
|||
"redox_syscall 0.1.56 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "safex11"
|
||||
version = "0.0.4"
|
||||
dependencies = [
|
||||
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"derivative 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"x11 2.18.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "scoped_threadpool"
|
||||
version = "0.1.9"
|
||||
|
@ -668,6 +680,7 @@ dependencies = [
|
|||
"checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8"
|
||||
"checksum deflate 0.7.20 (registry+https://github.com/rust-lang/crates.io-index)" = "707b6a7b384888a70c8d2e8650b3e60170dfc6a67bb4aa67b6dfca57af4bedb4"
|
||||
"checksum deflate 0.8.4 (registry+https://github.com/rust-lang/crates.io-index)" = "e7e5d2a2273fed52a7f947ee55b092c4057025d7a3e04e5ecdbd25d6c3fb1bd7"
|
||||
"checksum derivative 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cb582b60359da160a9477ee80f15c8d784c477e69c217ef2cdd4169c24ea380f"
|
||||
"checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
|
||||
"checksum gif 0.10.3 (registry+https://github.com/rust-lang/crates.io-index)" = "471d90201b3b223f3451cd4ad53e34295f16a1df17b1edf3736d47761c3981af"
|
||||
"checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205"
|
||||
|
|
|
@ -7,12 +7,9 @@ license-file = "LICENSE"
|
|||
edition = "2018"
|
||||
authors = ["Michael Zhang <iptq@protonmail.com>"]
|
||||
|
||||
[workspace]
|
||||
members = [".", "x11"]
|
||||
|
||||
[features]
|
||||
default = ["x11"]
|
||||
x11 = ["leanshot_x11"]
|
||||
x11 = ["safex11"]
|
||||
|
||||
[dependencies]
|
||||
png = "0.14"
|
||||
|
@ -22,4 +19,4 @@ image = "0.23.5"
|
|||
log = "0.4.8"
|
||||
stderrlog = "0.4.3"
|
||||
|
||||
leanshot_x11 = { version = "0.2", path = "x11", optional = true, features = ["xlib", "xinerama"] }
|
||||
safex11 = { version = "0.0", path = "../safex11", optional = true, features = ["xlib", "xinerama"] }
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
extern crate anyhow;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
extern crate leanshot_x11 as x11;
|
||||
extern crate safex11 as x11;
|
||||
|
||||
mod platform;
|
||||
|
||||
|
|
18
src/main.rs
18
src/main.rs
|
@ -13,7 +13,7 @@ pub fn main() -> Result<()> {
|
|||
let opt = Options::from_args();
|
||||
stderrlog::new()
|
||||
.module(module_path!())
|
||||
.module("leanshot_x11")
|
||||
.module("safex11")
|
||||
.verbosity(opt.verbose)
|
||||
.init()
|
||||
.unwrap();
|
||||
|
@ -23,13 +23,13 @@ pub fn main() -> Result<()> {
|
|||
let screen_layout = gui.get_screen_layout()?;
|
||||
debug!("screen layout: {:?}", screen_layout);
|
||||
|
||||
let image = match opt.region {
|
||||
let _image = match opt.region {
|
||||
Region::ActiveWindow => {
|
||||
let window = gui.get_active_window()?;
|
||||
let image = gui.capture_window(window);
|
||||
let _image = gui.capture_window(window);
|
||||
}
|
||||
Region::Fullscreen | Region::Selection => {
|
||||
let mut image = gui.capture_full_screen()?;
|
||||
let _image = gui.capture_full_screen()?;
|
||||
|
||||
// calculate the full size of the image
|
||||
let mut min_x = 0;
|
||||
|
@ -71,11 +71,11 @@ pub fn main() -> Result<()> {
|
|||
|
||||
if let Region::Selection = opt.region {
|
||||
// bring up the interactive selection
|
||||
let rect = gui.show_interactive_selection(&images)?;
|
||||
let _rect = gui.show_interactive_selection(&images)?;
|
||||
}
|
||||
|
||||
// save the image
|
||||
base_image.save(&opt.outfile);
|
||||
base_image.save(&opt.outfile)?;
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -99,5 +99,9 @@ pub struct Options {
|
|||
|
||||
/// Verbosity of output
|
||||
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
|
||||
verbose: usize,
|
||||
pub verbose: usize,
|
||||
|
||||
/// X display connection string to use
|
||||
#[structopt(long = "x-display", default_value = ":0")]
|
||||
pub x_display: String,
|
||||
}
|
||||
|
|
|
@ -26,16 +26,20 @@ impl X11 {
|
|||
}
|
||||
|
||||
impl Platform for X11 {
|
||||
type Window = Window;
|
||||
type Window = x11::x11::xlib::Window;
|
||||
type Image = Image;
|
||||
type ScreenId = i32;
|
||||
|
||||
fn get_active_window(&self) -> Result<Self::Window> {
|
||||
let (window, _) = self.inner.get_input_focus()?;
|
||||
Ok(window)
|
||||
Ok(window.inner)
|
||||
}
|
||||
|
||||
fn capture_window(&self, window: Self::Window) -> Result<Self::Image> {
|
||||
let window = Window {
|
||||
display: &self.inner,
|
||||
inner: window,
|
||||
};
|
||||
let image = window.get_image()?;
|
||||
Ok(Image(image))
|
||||
}
|
||||
|
@ -78,10 +82,11 @@ impl Platform for X11 {
|
|||
&self.inner,
|
||||
Some(root_window),
|
||||
Rectangle::new(0, 0, 500, 500),
|
||||
Default::default(),
|
||||
)?;
|
||||
window.map();
|
||||
|
||||
while true {
|
||||
loop {
|
||||
let event = self.inner.next_event()?;
|
||||
debug!("event: {:?}", event);
|
||||
|
||||
|
|
|
@ -1,18 +0,0 @@
|
|||
[package]
|
||||
name = "leanshot_x11"
|
||||
version = "0.2.0"
|
||||
description = "x11 bindings"
|
||||
license-file = "../LICENSE"
|
||||
authors = ["Michael Zhang <failed.down@gmail.com>"]
|
||||
|
||||
[features]
|
||||
default = []
|
||||
xlib = ["x11/xlib"]
|
||||
xrender = ["x11/xrender"]
|
||||
xinerama = ["x11/xlib", "x11/xinerama"]
|
||||
|
||||
[dependencies]
|
||||
libc = "0.2"
|
||||
x11 = { version = "2.18" }
|
||||
thiserror = "1.0.20"
|
||||
log = "0.4.8"
|
|
@ -1,30 +0,0 @@
|
|||
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
||||
|
||||
/// Any error that can be raised when using this library.
|
||||
#[allow(missing_docs)]
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Error {
|
||||
#[error("invalid image byte order: {0}")]
|
||||
InvalidByteOrder(i32),
|
||||
|
||||
#[error("invalid event type: {0}")]
|
||||
InvalidEventType(i32),
|
||||
|
||||
#[error("failed to get attributes")]
|
||||
GetAttributesError,
|
||||
|
||||
#[error("failed to create cursor")]
|
||||
CreateCursorError,
|
||||
|
||||
#[error("failed to open display")]
|
||||
DisplayOpenError,
|
||||
|
||||
#[error("failed to get window")]
|
||||
GetWindowError,
|
||||
|
||||
#[error("failed to translate coordinates")]
|
||||
TranslateCoordinatesError,
|
||||
|
||||
#[error("error")]
|
||||
Error,
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
//! Higher level bindings to x11.
|
||||
|
||||
#[macro_use]
|
||||
extern crate thiserror;
|
||||
#[macro_use]
|
||||
extern crate log;
|
||||
|
||||
pub extern crate x11;
|
||||
|
||||
#[cfg(feature = "xlib")]
|
||||
pub mod xlib;
|
||||
|
||||
#[cfg(feature = "xrender")]
|
||||
pub mod xrender;
|
||||
|
||||
#[cfg(feature = "xinerama")]
|
||||
pub mod xinerama;
|
||||
|
||||
mod errors;
|
||||
mod rect;
|
||||
|
||||
pub use crate::errors::{Error, Result};
|
||||
pub use crate::rect::Rectangle;
|
|
@ -1,24 +0,0 @@
|
|||
/// A rectangle.
|
||||
#[derive(Debug)]
|
||||
pub struct Rectangle {
|
||||
/// x
|
||||
pub x: i32,
|
||||
/// y
|
||||
pub y: i32,
|
||||
/// width
|
||||
pub width: u32,
|
||||
/// height
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
impl Rectangle {
|
||||
/// Create a new Rectangle from u32s
|
||||
pub fn new(x: i32, y: i32, width: u32, height: u32) -> Self {
|
||||
Rectangle {
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
mod screen_info;
|
||||
|
||||
pub use self::screen_info::ScreensInfo;
|
|
@ -1,64 +0,0 @@
|
|||
use x11::xinerama;
|
||||
|
||||
use crate::errors::Result;
|
||||
use crate::xlib::Display;
|
||||
|
||||
pub struct ScreensInfo {
|
||||
inner: *mut xinerama::XineramaScreenInfo,
|
||||
heads: isize,
|
||||
}
|
||||
|
||||
impl ScreensInfo {
|
||||
pub fn query(display: &Display) -> Result<Self> {
|
||||
let mut heads = 0;
|
||||
let inner = unsafe { xinerama::XineramaQueryScreens(display.inner, &mut heads as *mut _) };
|
||||
// TODO: check for error
|
||||
Ok(ScreensInfo {
|
||||
inner,
|
||||
heads: heads as isize,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> ScreenInfoIter {
|
||||
ScreenInfoIter(self, 0)
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for ScreensInfo {
|
||||
fn drop(&mut self) {
|
||||
unsafe { x11::xlib::XFree(self.inner as *mut _) };
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ScreenInfoIter<'a>(&'a ScreensInfo, isize);
|
||||
|
||||
pub struct ScreenInfo {
|
||||
pub x: i16,
|
||||
pub y: i16,
|
||||
pub width: i16,
|
||||
pub height: i16,
|
||||
}
|
||||
|
||||
impl<'a> Iterator for ScreenInfoIter<'a> {
|
||||
type Item = (i32, ScreenInfo);
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.1 >= self.0.heads {
|
||||
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,
|
||||
},
|
||||
))
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,36 +0,0 @@
|
|||
use std::ffi::CString;
|
||||
|
||||
use x11::xlib;
|
||||
|
||||
use crate::errors::Result;
|
||||
|
||||
use super::display::Display;
|
||||
|
||||
/// A unique string or intger
|
||||
pub struct Atom {
|
||||
inner: xlib::Atom,
|
||||
}
|
||||
|
||||
impl Atom {
|
||||
/// Create a new atom using a string
|
||||
pub fn new(display: &Display, val: impl AsRef<str>, only_if_exists: bool) -> Result<Self> {
|
||||
let val = {
|
||||
let v = val.as_ref();
|
||||
let s = CString::new(v).unwrap();
|
||||
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
|
||||
pub fn from(handle: xlib::Atom) -> Self {
|
||||
Atom { inner: handle }
|
||||
}
|
||||
|
||||
/// Get the handle
|
||||
pub fn as_raw(&self) -> xlib::Atom {
|
||||
self.inner
|
||||
}
|
||||
}
|
|
@ -1,13 +0,0 @@
|
|||
use x11::xlib;
|
||||
|
||||
/// Mouse pointer
|
||||
pub struct Cursor {
|
||||
pub(crate) display: *mut xlib::Display,
|
||||
pub(crate) inner: xlib::Cursor,
|
||||
}
|
||||
|
||||
impl Drop for Cursor {
|
||||
fn drop(&mut self) {
|
||||
unsafe { xlib::XFreeCursor(self.display, self.inner) };
|
||||
}
|
||||
}
|
|
@ -1,181 +0,0 @@
|
|||
use std::ffi::CString;
|
||||
use std::ptr;
|
||||
|
||||
use x11::xlib;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
|
||||
use super::cursor::Cursor;
|
||||
use super::event::Event;
|
||||
use super::visual::Visual;
|
||||
use super::window::Window;
|
||||
|
||||
/// A connection to an X server.
|
||||
pub struct Display {
|
||||
pub(crate) inner: *mut xlib::Display,
|
||||
}
|
||||
|
||||
pub struct Grab(pub(crate) *mut xlib::Display);
|
||||
|
||||
/// Something that's part of a display.
|
||||
pub trait GetDisplay {
|
||||
/// Get the current display
|
||||
fn get_display(&self) -> *mut xlib::Display;
|
||||
}
|
||||
|
||||
impl Display {
|
||||
/// Opens a new connection to an X server.
|
||||
///
|
||||
/// On POSIX-conformant systems, the display name or DISPLAY environment variable can be a string in the format:
|
||||
/// hostname:number.screen_number
|
||||
pub fn connect(display_name: impl AsRef<str>) -> Result<Display> {
|
||||
let display_name = CString::new(display_name.as_ref()).unwrap();
|
||||
let inner = unsafe { xlib::XOpenDisplay(display_name.as_ptr()) };
|
||||
if inner.is_null() {
|
||||
return Err(Error::DisplayOpenError);
|
||||
}
|
||||
Ok(Display { inner })
|
||||
}
|
||||
|
||||
/// Create a Display for an existing connection
|
||||
pub fn from_handle(handle: u64) -> Self {
|
||||
Display {
|
||||
inner: handle as *mut xlib::Display,
|
||||
}
|
||||
}
|
||||
|
||||
/// Grab
|
||||
pub fn grab(&self) -> Grab {
|
||||
unsafe { xlib::XGrabServer(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,
|
||||
inner: cursor,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get the next event, blocks until an event is reached.
|
||||
pub fn next_event(&self) -> Result<Event> {
|
||||
let event = ptr::null_mut();
|
||||
unsafe { xlib::XNextEvent(self.inner, event) };
|
||||
// TODO: check to make sure this isn't null
|
||||
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,
|
||||
inner,
|
||||
};
|
||||
Ok(window)
|
||||
}
|
||||
|
||||
/// Returns the root window for the default screen.
|
||||
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,
|
||||
inner,
|
||||
};
|
||||
Ok(window)
|
||||
}
|
||||
|
||||
/// 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
|
||||
/// 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,
|
||||
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,
|
||||
inner: focus_return,
|
||||
};
|
||||
return Ok((window, revert_to_return));
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Display {
|
||||
fn drop(&mut self) {
|
||||
unsafe { xlib::XCloseDisplay(self.inner) };
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Grab {
|
||||
fn drop(&mut self) {
|
||||
unsafe { xlib::XUngrabServer(self.0) };
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
use x11::xlib;
|
||||
|
||||
use crate::errors::Result;
|
||||
use crate::rect::Rectangle;
|
||||
|
||||
use super::display::GetDisplay;
|
||||
use super::image::Image;
|
||||
|
||||
/// Anything that's drawable
|
||||
pub trait Drawable: GetDisplay {
|
||||
/// Get drawable handle
|
||||
fn as_drawable(&self) -> xlib::Drawable;
|
||||
|
||||
/// Capture a snapshot of this drawable, clipped by rect.
|
||||
fn get_image(&self, rect: Rectangle) -> Result<Image> {
|
||||
let image = unsafe {
|
||||
xlib::XGetImage(
|
||||
self.get_display(),
|
||||
self.as_drawable(),
|
||||
rect.x as i32,
|
||||
rect.y as i32,
|
||||
rect.width,
|
||||
rect.height,
|
||||
0xffffffff,
|
||||
xlib::ZPixmap,
|
||||
)
|
||||
};
|
||||
Ok(Image { inner: image })
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
use x11::xlib;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
|
||||
/// An x11 Event
|
||||
#[derive(Debug)]
|
||||
pub struct Event {
|
||||
inner: *mut xlib::XEvent,
|
||||
kind: EventKind,
|
||||
}
|
||||
|
||||
/// Type of event
|
||||
#[derive(Debug)]
|
||||
pub enum EventKind {
|
||||
KeyPress(KeyEvent),
|
||||
KeyRelease(KeyEvent),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
/// Returns the EventKind of this event
|
||||
pub fn kind(&self) -> &EventKind {
|
||||
&self.kind
|
||||
}
|
||||
|
||||
pub(super) unsafe fn from_raw(event: *mut xlib::XEvent) -> Result<Self> {
|
||||
let inner = event;
|
||||
let event = unsafe { *event };
|
||||
debug!("event type: {:?}", event.type_);
|
||||
let kind = match event.type_ {
|
||||
xlib::KeyPress => EventKind::KeyPress(KeyEvent(event.key)),
|
||||
xlib::KeyRelease => EventKind::KeyRelease(KeyEvent(event.key)),
|
||||
other => return Err(Error::InvalidEventType(other)),
|
||||
};
|
||||
Ok(Event { inner, kind })
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Event {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::free(self.inner as *mut libc::c_void) };
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct KeyEvent(xlib::XKeyEvent);
|
|
@ -1,122 +0,0 @@
|
|||
use std::os::raw::c_ulong;
|
||||
|
||||
use x11::xlib;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
|
||||
/// A handle to an XImage.
|
||||
pub struct Image {
|
||||
pub(super) inner: *mut xlib::XImage,
|
||||
}
|
||||
|
||||
/// Image byte order
|
||||
pub enum ImageByteOrder {
|
||||
/// Least significant byte first
|
||||
LSBFirst,
|
||||
/// Most significant byte first
|
||||
MSBFirst,
|
||||
}
|
||||
|
||||
/// The buffer pointed to by an XImage.
|
||||
pub struct PixBuffer<'a> {
|
||||
/// The raw pointer to the buffer
|
||||
pub buf: *mut u8,
|
||||
|
||||
/// The size of the buffer
|
||||
pub size: usize,
|
||||
|
||||
/// Rgb masks
|
||||
pub masks: (u64, u64, u64),
|
||||
|
||||
pub image: &'a Image,
|
||||
}
|
||||
|
||||
impl Image {
|
||||
/// Get the size (in bytes) of the data buffer.
|
||||
pub fn get_size(&self) -> usize {
|
||||
4 * self.get_width() as usize * self.get_height() as usize
|
||||
}
|
||||
|
||||
/// Get the image width
|
||||
pub fn get_width(&self) -> u32 {
|
||||
unsafe { (*self.inner).width as u32 }
|
||||
}
|
||||
|
||||
/// Get the image height
|
||||
pub fn get_height(&self) -> u32 {
|
||||
unsafe { (*self.inner).height as u32 }
|
||||
}
|
||||
|
||||
/// Get the image depth
|
||||
pub fn get_depth(&self) -> u32 {
|
||||
unsafe { (*self.inner).depth as u32 }
|
||||
}
|
||||
|
||||
/// Get byte order
|
||||
pub fn get_byte_order(&self) -> Result<ImageByteOrder> {
|
||||
let byte_order = unsafe { (*self.inner).byte_order };
|
||||
match byte_order {
|
||||
xlib::LSBFirst => Ok(ImageByteOrder::LSBFirst),
|
||||
xlib::MSBFirst => Ok(ImageByteOrder::MSBFirst),
|
||||
order => Err(Error::InvalidByteOrder(order)),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_rgb_masks(&self) -> (u64, u64, u64) {
|
||||
let red_mask = unsafe { (*self.inner).red_mask };
|
||||
let green_mask = unsafe { (*self.inner).green_mask };
|
||||
let blue_mask = unsafe { (*self.inner).blue_mask };
|
||||
(red_mask, blue_mask, green_mask)
|
||||
}
|
||||
|
||||
/// Produces a PixBuffer
|
||||
pub fn buffer(&self) -> PixBuffer {
|
||||
let size = self.get_size();
|
||||
let buf = unsafe { (*self.inner).data as *mut u8 };
|
||||
let masks = self.get_rgb_masks();
|
||||
PixBuffer {
|
||||
buf,
|
||||
size,
|
||||
masks,
|
||||
image: self,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Image {
|
||||
fn drop(&mut self) {
|
||||
unsafe { xlib::XDestroyImage(self.inner) };
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> PixBuffer<'a> {
|
||||
/// Gets the byte at the index of the data buffer.
|
||||
pub fn get_byte(&self, index: usize) -> Option<u8> {
|
||||
if index > self.size {
|
||||
return None;
|
||||
}
|
||||
Some(unsafe { *self.buf.offset(index as isize) as u8 })
|
||||
}
|
||||
|
||||
/// Gets the rgb value of a particular pixel
|
||||
pub fn get_pixel(&self, x: u32, y: u32) -> Option<(u8, u8, u8)> {
|
||||
// let bytes_per_line = unsafe{(*self.image.inner).bytes_per_line} as u32;
|
||||
// let bits_per_pixel=unsafe{(*self.image.inner).bits_per_pixel} as u32;
|
||||
// let pixel_ptr = (y * bytes_per_line + x * bits_per_pixel) / 8;
|
||||
// let pixel = unsafe{*((*self.image.inner).data.offset(pixel_ptr as isize) as *mut c_ulong)};
|
||||
|
||||
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) };
|
||||
|
||||
// warn!("pixel: {:?} {:#b}", pixel, pixel);
|
||||
let red_mask = unsafe { (*self.image.inner).red_mask };
|
||||
let green_mask = unsafe { (*self.image.inner).green_mask };
|
||||
let blue_mask = unsafe { (*self.image.inner).blue_mask };
|
||||
// warn!("masks: {:b} {:b} {:b}", red_mask, green_mask, blue_mask);
|
||||
let red = ((pixel & unsafe { (*self.image.inner).red_mask }) >> 16) as u8;
|
||||
let green = ((pixel & unsafe { (*self.image.inner).green_mask }) >> 8) as u8;
|
||||
let blue = ((pixel & unsafe { (*self.image.inner).blue_mask }) >> 0) as u8;
|
||||
// warn!("rgb: {:?}", (red, green, blue));
|
||||
Some((red, green, blue))
|
||||
}
|
||||
}
|
|
@ -1,23 +0,0 @@
|
|||
mod atom;
|
||||
mod cursor;
|
||||
mod display;
|
||||
mod drawable;
|
||||
mod event;
|
||||
mod image;
|
||||
mod visual;
|
||||
mod window;
|
||||
|
||||
pub use self::atom::Atom;
|
||||
pub use self::cursor::Cursor;
|
||||
pub use self::display::{Display, GetDisplay};
|
||||
pub use self::drawable::Drawable;
|
||||
pub use self::event::{Event, EventKind};
|
||||
pub use self::image::{Image, ImageByteOrder};
|
||||
pub use self::visual::Visual;
|
||||
pub use self::window::Window;
|
||||
|
||||
pub const XC_crosshair: u32 = 34;
|
||||
pub const XC_ll_angle: u32 = 76;
|
||||
pub const XC_lr_angle: u32 = 78;
|
||||
pub const XC_ul_angle: u32 = 144;
|
||||
pub const XC_ur_angle: u32 = 148;
|
|
@ -1,21 +0,0 @@
|
|||
use x11::xlib;
|
||||
|
||||
use super::display::Display;
|
||||
|
||||
/// A wrapper around a Visual
|
||||
pub struct Visual {
|
||||
pub(super) inner: *mut xlib::Visual,
|
||||
}
|
||||
|
||||
impl Visual {
|
||||
/// Gets the raw handle to the x11 Visual
|
||||
pub fn as_raw(&self) -> *mut xlib::Visual {
|
||||
self.inner
|
||||
}
|
||||
|
||||
/// Gets the default visual
|
||||
pub fn default(display: &Display, screen: i32) -> Self {
|
||||
let inner = unsafe { xlib::XDefaultVisual(display.as_raw(), screen) };
|
||||
Visual { inner }
|
||||
}
|
||||
}
|
|
@ -1,185 +0,0 @@
|
|||
use std::mem;
|
||||
|
||||
use x11::xlib;
|
||||
|
||||
use crate::errors::{Error, Result};
|
||||
use crate::rect::Rectangle;
|
||||
|
||||
use super::atom::Atom;
|
||||
use super::display::{Display, GetDisplay};
|
||||
use super::drawable::Drawable;
|
||||
use super::image::Image;
|
||||
|
||||
/// A wrapper around a window handle.
|
||||
#[derive(Clone, PartialEq, Eq)]
|
||||
pub struct Window {
|
||||
pub(super) display: *mut xlib::Display,
|
||||
pub(super) inner: xlib::Window,
|
||||
}
|
||||
|
||||
impl Window {
|
||||
/// Create a new window
|
||||
pub fn create(
|
||||
display: &Display,
|
||||
parent: Option<Window>,
|
||||
location: Rectangle,
|
||||
) -> Result<Window> {
|
||||
let parent = match parent {
|
||||
Some(parent) => parent,
|
||||
None => display.get_default_root_window()?,
|
||||
};
|
||||
let visual = display.default_visual(0);
|
||||
let window = unsafe {
|
||||
xlib::XCreateWindow(
|
||||
display.as_raw(),
|
||||
parent.as_raw(),
|
||||
location.x as i32,
|
||||
location.y as i32,
|
||||
location.width,
|
||||
location.height,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
visual.as_raw(),
|
||||
0,
|
||||
0 as *mut xlib::XSetWindowAttributes,
|
||||
)
|
||||
};
|
||||
Ok(Window {
|
||||
display: display.as_raw(),
|
||||
inner: window,
|
||||
})
|
||||
}
|
||||
|
||||
/// Create a new Window instance from an existing ID
|
||||
pub fn create_from_handle(display: &Display, id: u64) -> Result<Window> {
|
||||
Ok(Window {
|
||||
display: display.as_raw(),
|
||||
inner: id,
|
||||
})
|
||||
}
|
||||
|
||||
/// Get window attributes.
|
||||
pub fn get_attributes(&self) -> Result<WindowAttributes> {
|
||||
let attr = unsafe {
|
||||
libc::malloc(mem::size_of::<xlib::XWindowAttributes>()) as *mut xlib::XWindowAttributes
|
||||
};
|
||||
let result = unsafe { xlib::XGetWindowAttributes(self.display, self.inner, attr) };
|
||||
match result {
|
||||
0 => Err(Error::GetAttributesError),
|
||||
_ => Ok(WindowAttributes {
|
||||
display: self.display,
|
||||
inner: attr,
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the raw window handle
|
||||
pub fn as_raw(&self) -> xlib::Window {
|
||||
self.inner
|
||||
}
|
||||
|
||||
/// Gets the image from this window using XGetImage.
|
||||
pub fn get_image(&self) -> Result<Image> {
|
||||
let attr = self.get_attributes()?;
|
||||
Drawable::get_image(
|
||||
self,
|
||||
Rectangle::new(
|
||||
attr.get_x(),
|
||||
attr.get_y(),
|
||||
attr.get_width(),
|
||||
attr.get_height(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
/// Change window property
|
||||
// TODO: make it more general
|
||||
pub fn change_property(&self, key: &Atom, val: &Atom) {
|
||||
use std::mem::transmute;
|
||||
let v = val.as_raw();
|
||||
unsafe {
|
||||
xlib::XChangeProperty(
|
||||
self.display,
|
||||
self.inner,
|
||||
key.as_raw(),
|
||||
xlib::XA_ATOM,
|
||||
32,
|
||||
xlib::PropModeReplace,
|
||||
transmute(&v),
|
||||
1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Map to display
|
||||
pub fn map(&self) {
|
||||
unsafe { xlib::XMapWindow(self.display, self.inner) };
|
||||
}
|
||||
}
|
||||
|
||||
impl GetDisplay for Window {
|
||||
fn get_display(&self) -> *mut xlib::Display {
|
||||
self.display
|
||||
}
|
||||
}
|
||||
|
||||
impl Drawable for Window {
|
||||
fn as_drawable(&self) -> xlib::Drawable {
|
||||
self.inner
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for Window {
|
||||
fn drop(&mut self) {
|
||||
unsafe { xlib::XDestroyWindow(self.display, self.inner) };
|
||||
}
|
||||
}
|
||||
|
||||
/// Window Attributes
|
||||
pub struct WindowAttributes {
|
||||
pub(super) display: *mut xlib::Display,
|
||||
pub(self) inner: *mut xlib::XWindowAttributes,
|
||||
}
|
||||
|
||||
// impl AsRef<Drawable> for Window {
|
||||
// fn as_ref(&self) -> &Drawable {
|
||||
// &self.inner
|
||||
// }
|
||||
// }
|
||||
|
||||
impl WindowAttributes {
|
||||
/// Gets the width of the window
|
||||
pub fn get_x(&self) -> i32 {
|
||||
unsafe { (*self.inner).x as i32 }
|
||||
}
|
||||
|
||||
/// Gets the height of the window
|
||||
pub fn get_y(&self) -> i32 {
|
||||
unsafe { (*self.inner).y as i32 }
|
||||
}
|
||||
|
||||
/// Gets the width of the window
|
||||
pub fn get_width(&self) -> u32 {
|
||||
unsafe { (*self.inner).width as u32 }
|
||||
}
|
||||
|
||||
/// Gets the height of the window
|
||||
pub fn get_height(&self) -> u32 {
|
||||
unsafe { (*self.inner).height as u32 }
|
||||
}
|
||||
|
||||
/// Get the root window of this window
|
||||
pub fn get_root(&self) -> Window {
|
||||
Window {
|
||||
display: self.display,
|
||||
inner: unsafe { (*self.inner).root },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WindowAttributes {
|
||||
fn drop(&mut self) {
|
||||
unsafe { libc::free(self.inner as *mut libc::c_void) };
|
||||
}
|
||||
}
|
|
@ -1 +0,0 @@
|
|||
mod picture;
|
|
@ -1,7 +0,0 @@
|
|||
pub struct Picture {}
|
||||
|
||||
impl Picture {}
|
||||
|
||||
impl Drop for Picture {
|
||||
fn drop(&self) {}
|
||||
}
|
Loading…
Reference in a new issue