From f717a69fd8aadb8fb1ed4d13e03a107bb4a824d0 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Sun, 30 Sep 2018 07:25:18 -0500 Subject: [PATCH] fixed i think --- Cargo.toml | 7 ++----- Dockerfile | 2 -- Dockerfile.check | 8 -------- ci/build-release.sh | 2 +- ci/check.sh | 4 ++-- imlib2/imlib2-sys/build.rs | 2 +- imlib2/src/image.rs | 18 +++++++++++++++++- src/gui.rs | 36 ++++++++++++++++++++++-------------- src/{lib.rs => main.rs} | 0 9 files changed, 45 insertions(+), 34 deletions(-) delete mode 100644 Dockerfile.check rename src/{lib.rs => main.rs} (100%) diff --git a/Cargo.toml b/Cargo.toml index 725982f..5c3d443 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,15 +1,12 @@ [package] -name = "screenshot" -description = "Screenshot capture utility." +name = "leanshot" +description = "Screenshot capture for Linux." version = "0.3.0" authors = ["Michael Zhang "] [workspace] members = [".", "imlib2", "imlib2/imlib2-sys", "xlib"] -[lib] -crate-type = ["dylib", "rlib"] - [dependencies] failure = "0.1" gl = "0.10" diff --git a/Dockerfile b/Dockerfile index 95a73fe..c2d9140 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,5 +4,3 @@ RUN sudo apt-get update -y && sudo apt-get install -y mesa-common-dev libx11-dev COPY . ./ RUN sudo chown -R rust:rust . - -CMD cargo build --release diff --git a/Dockerfile.check b/Dockerfile.check deleted file mode 100644 index d0ac70a..0000000 --- a/Dockerfile.check +++ /dev/null @@ -1,8 +0,0 @@ -FROM ekidd/rust-musl-builder - -RUN sudo apt-get update -y && sudo apt-get install -y libx11-dev libimlib2-dev - -COPY . ./ -RUN sudo chown -R rust:rust . - -CMD cargo build diff --git a/ci/build-release.sh b/ci/build-release.sh index 1d1536e..f33be69 100755 --- a/ci/build-release.sh +++ b/ci/build-release.sh @@ -20,7 +20,7 @@ case `uname -s` in Linux) echo "Building static binaries using ekidd/rust-musl-builder" docker build -t build-"$1"-image . - docker run -it --name build-"$1" build-"$1"-image + docker run -it --name build-"$1" build-"$1"-image bash -c 'cargo build --release' docker cp build-"$1":/home/rust/src/target/x86_64-unknown-linux-musl/release/"$1" "$1" docker rm build-"$1" docker rmi build-"$1"-image diff --git a/ci/check.sh b/ci/check.sh index a55f7a8..ad80965 100755 --- a/ci/check.sh +++ b/ci/check.sh @@ -19,8 +19,8 @@ set -euo pipefail case `uname -s` in Linux) echo "Building static binaries using ekidd/rust-musl-builder" - docker build -f Dockerfile.check -t build-"$1"-image . - docker run -it --name build-"$1" build-"$1"-image + docker build -f Dockerfile -t build-"$1"-image . + docker run -it --name build-"$1" build-"$1"-image bash -c 'cargo check' docker rm build-"$1" docker rmi build-"$1"-image ;; diff --git a/imlib2/imlib2-sys/build.rs b/imlib2/imlib2-sys/build.rs index 3e62340..1616c77 100644 --- a/imlib2/imlib2-sys/build.rs +++ b/imlib2/imlib2-sys/build.rs @@ -16,5 +16,5 @@ fn main() { .generate() .expect("Unable to generate bindings") .write_to_file(out_path.join("bindings.rs")) - .expect("Unable to write bindings"); + .expect("Unable to write bindings"); } diff --git a/imlib2/src/image.rs b/imlib2/src/image.rs index febbc96..a99de23 100644 --- a/imlib2/src/image.rs +++ b/imlib2/src/image.rs @@ -3,8 +3,8 @@ use std::path::Path; use imlib2_sys as im; -use Error; use xlib::Drawable; +use Error; /// A simple wrapper around Imlib_Image pub struct Image { @@ -37,6 +37,22 @@ impl Image { Ok(Image { inner: image }) } + /// Get width + pub fn get_width(&self) -> i32 { + unsafe { + im::imlib_context_set_image(self.inner); + im::imlib_image_get_width() + } + } + + /// Get height + pub fn get_height(&self) -> i32 { + unsafe { + im::imlib_context_set_image(self.inner); + im::imlib_image_get_height() + } + } + /// Save this image pub fn save_image(&self, file: impl AsRef) -> Result<(), Error> { let mut error = 0; diff --git a/src/gui.rs b/src/gui.rs index a2e96b2..9b2619a 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -23,6 +23,7 @@ impl GUI { let mut height = attr.get_height(); let root = attr.get_root(); let (mut x, mut y, _) = self.display.translate_coordinates(window, 0, 0, root)?; + println!("{} {}", window.as_raw(), root.as_raw()); imlib2::context_set_display(&self.display); let visual = Visual::default(&self.display, 0); @@ -30,7 +31,16 @@ impl GUI { match opt.region { Region::Selection => { - let region = self.interactive_select(window)?; + let capture = Image2::create_from_drawable( + window, + 0, + x, + y, + width as i32, + height as i32, + true, + )?; + let region = self.interactive_select(capture)?; x = region.x; y = region.y; width = region.width; @@ -52,7 +62,7 @@ impl GUI { } /// Brings up an interactive selection GUI. - pub fn interactive_select(&self, window: Window) -> Result { + pub fn interactive_select(&self, capture: Image2) -> Result { // let window = SelectWindow::new(&self.display); // let root = self.display.get_default_root_window()?; @@ -84,12 +94,13 @@ impl GUI { use nanovg::{Color, Image, ImagePattern, PathOptions, StrokeOptions}; use std::{f32::consts, slice}; - let attr = window.get_attributes()?; - let width = attr.get_width(); - let height = attr.get_height(); - let root = attr.get_root(); - println!("{} {}", width, height); - let (x, y, _) = self.display.translate_coordinates(window, 0, 0, root)?; + // let attr = window.get_attributes()?; + // let width = attr.get_width(); + // let height = attr.get_height(); + // let root = attr.get_root(); + // let (x, y, _) = self.display.translate_coordinates(window, 0, 0, root)?; + let width = capture.get_width(); + let height = capture.get_height(); let mut evl = EventsLoop::new(); let mon = evl.get_primary_monitor(); @@ -114,13 +125,10 @@ impl GUI { // println!("size={:?} pos={:?} outer={:?}", win.get_inner_size(), win.get_inner_position(), win.get_outer_size()); // println!("{:?}", win.get_hidpi_factor()); - imlib2::context_set_display(&self.display); - let visual = Visual::default(&self.display, 0); - imlib2::context_set_visual(&visual); - - let img = Image2::create_from_drawable(window, 0, x, y, width as i32, height as i32, true)?; - imlib2::context_set_image(&img); + // let img = Image2::create_from_drawable(window, 0, 0, 0, width as i32, height as i32, true)?; + imlib2::context_set_image(&capture); let len = (width * height) as usize; + // println!("{}", window.as_raw()); let raw_data = unsafe { slice::from_raw_parts(imlib2::image_get_data(), len) }; // convert ARGB to RGBA diff --git a/src/lib.rs b/src/main.rs similarity index 100% rename from src/lib.rs rename to src/main.rs