add x11 stuff

This commit is contained in:
Michael Zhang 2021-07-28 16:05:13 -05:00
parent a8772d9089
commit 69e226dcf3
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
6 changed files with 92 additions and 11 deletions

1
Cargo.lock generated
View file

@ -185,6 +185,7 @@ name = "leanshot"
version = "0.5.0" version = "0.5.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"cfg-if",
"image", "image",
"leanshot-x11", "leanshot-x11",
"libc", "libc",

View file

@ -10,13 +10,21 @@ authors = ["Michael Zhang <mail@mzhang.io>"]
[workspace] [workspace]
members = ["x11"] members = ["x11"]
[features]
default = ["backend-x11", "backend-x11-glx"]
backend-x11 = []
backend-x11-glx = ["backend-x11"]
backend-xcb = ["xcb-util", "xcb"]
[dependencies] [dependencies]
anyhow = "1.0.38" anyhow = "1.0.38"
image = { version = "0.23.13", default-features = false, features = ["jpeg", "png"] } image = { version = "0.23.13", default-features = false, features = ["jpeg", "png"] }
leanshot-x11 = { path = "x11" } leanshot-x11 = { path = "x11" }
log = "0.4.14" log = "0.4.14"
stderrlog = "0.5.1" stderrlog = "0.5.1"
xcb-util = { version = "0.3.0", features = ["image", "cursor"] }
xcb = "0.9.0"
structopt = "0.3.21" structopt = "0.3.21"
libc = "0.2.86" libc = "0.2.86"
xcb-util = { version = "0.3.0", features = ["image", "cursor"], optional = true }
xcb = { version = "0.9.0", optional = true }
cfg-if = "1.0.0"

47
src/gui_x11.rs Normal file
View file

@ -0,0 +1,47 @@
use std::path::Path;
use anyhow::Result;
use x11::xlib::Display;
use crate::gui_trait::{Capture, Gui};
use crate::rect::Rectangle;
pub struct X11Gui {
conn: Display,
}
impl X11Gui {
pub fn new() -> Result<Self> {
let disp = Display::connect(None)?;
Ok(X11Gui { conn: disp })
}
}
impl Gui for X11Gui {
type Capture = ScreenCapture;
fn capture_entire_screen(&self) -> Result<<Self as Gui>::Capture, anyhow::Error> {
todo!()
}
fn interactive_select(
&self,
_: &<Self as Gui>::Capture,
) -> Result<Option<Rectangle>, anyhow::Error> {
todo!()
}
}
pub struct ScreenCapture {}
impl Capture for ScreenCapture {
fn save_to(&self, _: impl AsRef<Path>) -> Result<(), anyhow::Error> {
todo!()
}
fn save_cropped_to(
&self,
_: impl AsRef<Path>,
_: Option<Rectangle>,
) -> Result<(), anyhow::Error> {
todo!()
}
}

View file

@ -205,12 +205,8 @@ impl Gui for XcbGui {
); );
xproto::poly_rectangle(&self.conn, window_id, window_gc, &[rect.into()]); xproto::poly_rectangle(&self.conn, window_id, window_gc, &[rect.into()]);
let rect2 = Rectangle::new( let rect2 =
rect.x + 1, Rectangle::new(rect.x + 1, rect.y + 1, rect.width - 2, rect.height - 2);
rect.y + 1,
rect.width - 2,
rect.height - 2,
);
xproto::change_gc( xproto::change_gc(
&self.conn, &self.conn,
window_gc, window_gc,

View file

@ -5,8 +5,17 @@ extern crate anyhow;
extern crate leanshot_x11 as x11; extern crate leanshot_x11 as x11;
#[cfg(all(feature = "backend-x11", feature = "backend-xcb"))]
compile_error!("don't enable both x11 and xcb backends");
mod gui_trait; mod gui_trait;
#[cfg(feature = "backend-xcb")]
mod gui_xcb; mod gui_xcb;
#[cfg(feature = "backend-x11")]
mod gui_x11;
mod rect; mod rect;
mod singleton; mod singleton;
@ -18,7 +27,6 @@ use anyhow::Result;
use structopt::StructOpt; use structopt::StructOpt;
use crate::gui_trait::{Capture, Gui}; use crate::gui_trait::{Capture, Gui};
use crate::gui_xcb::XcbGui;
fn main() -> Result<()> { fn main() -> Result<()> {
let opt = Options::from_args(); let opt = Options::from_args();
@ -29,7 +37,21 @@ fn main() -> Result<()> {
.init() .init()
.unwrap(); .unwrap();
let gui = XcbGui::new()?; let gui = get_gui()?;
run(&opt, gui)
}
#[cfg(feature = "backend-xcb")]
fn get_gui() -> Result<crate::gui_xcb::XcbGui> {
crate::gui_xcb::XcbGui::new()
}
#[cfg(feature = "backend-x11")]
fn get_gui() -> Result<crate::gui_x11::X11Gui> {
crate::gui_x11::X11Gui::new()
}
fn run(opt: &Options, gui: impl Gui) -> Result<()> {
let capture = gui.capture_entire_screen()?; let capture = gui.capture_entire_screen()?;
match opt.region { match opt.region {

View file

@ -8,10 +8,16 @@ pub struct Rectangle<T = i16, U = u16> {
impl<T, U> Rectangle<T, U> { impl<T, U> Rectangle<T, U> {
pub fn new(x: T, y: T, width: U, height: U) -> Self { pub fn new(x: T, y: T, width: U, height: U) -> Self {
Rectangle { x, y, width, height } Rectangle {
x,
y,
width,
height,
}
} }
} }
#[cfg(feature = "backend-xcb")]
impl From<xcb::xproto::Rectangle> for Rectangle<i16, u16> { impl From<xcb::xproto::Rectangle> for Rectangle<i16, u16> {
fn from(rect: xcb::xproto::Rectangle) -> Self { fn from(rect: xcb::xproto::Rectangle) -> Self {
Rectangle { Rectangle {
@ -23,6 +29,7 @@ impl From<xcb::xproto::Rectangle> for Rectangle<i16, u16> {
} }
} }
#[cfg(feature = "backend-xcb")]
impl Into<xcb::xproto::Rectangle> for Rectangle<i16, u16> { impl Into<xcb::xproto::Rectangle> for Rectangle<i16, u16> {
fn into(self) -> xcb::xproto::Rectangle { fn into(self) -> xcb::xproto::Rectangle {
xcb::xproto::Rectangle::new(self.x, self.y, self.width, self.height) xcb::xproto::Rectangle::new(self.x, self.y, self.width, self.height)