From 491b096dc51bb3877ea4871904e978f46f861750 Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Mon, 14 May 2018 18:03:30 -0500 Subject: [PATCH] wrap slop --- Cargo.lock | 11 +- Cargo.toml | 5 +- README.md | 2 + src/build.rs | 7 +- src/capture.rs | 12 ++- src/main.rs | 3 +- src/selection.rs | 60 ++++++----- src/slop.rs | 264 +++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 317 insertions(+), 47 deletions(-) create mode 100644 src/slop.rs diff --git a/Cargo.lock b/Cargo.lock index 92827db..0d000d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,14 +105,6 @@ dependencies = [ "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "cmake" -version = "0.1.31" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "failure" version = "0.1.1" @@ -363,10 +355,10 @@ version = "0.1.0" dependencies = [ "cairo-rs 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)", - "cmake 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "gdk 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "gdk-pixbuf 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "gio 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "gtk 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.40 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -478,7 +470,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum cc 1.0.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0ebb87d1116151416c0cf66a0e3fb6430cccd120fd6300794b4dfaa050ac40ba" "checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18" "checksum clap 2.31.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f0f16b89cbb9ee36d87483dc939fe9f1e13c05898d56d7b230a0d4dff033a536" -"checksum cmake 0.1.31 (registry+https://github.com/rust-lang/crates.io-index)" = "95470235c31c726d72bf2e1f421adc1e65b9d561bf5529612cbe1a72da1467b3" "checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82" "checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b" "checksum gdk 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "dd30051ff3d908ff2fc7e5776ffe1c699821e043809f294c3a61004f11d6c3a9" diff --git a/Cargo.toml b/Cargo.toml index 11a068f..a958149 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,9 +2,7 @@ name = "screenshot" version = "0.1.0" authors = ["Michael Zhang "] - -[build-dependencies] -cmake = "0.1.31" +build = "src/build.rs" [dependencies] clap = "2.31.2" @@ -12,5 +10,6 @@ cairo-rs = { version = "0.4.1", features = ["png"] } failure = "0.1.1" gdk = "0.8.0" gdk-pixbuf = "0.4.0" +gio = "0.4.0" gtk = { version = "0.4.1", features = ["v3_16"] } time = "0.1.40" diff --git a/README.md b/README.md index 516b32b..4fbf6af 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,8 @@ screenshot Screenshot-capturing utility. Selection isn't fully implemented, so it's disabled for now. +Bindings for `slop` have been generated by `bindgen`. + Installation ------------ diff --git a/src/build.rs b/src/build.rs index fe3a75f..d244485 100644 --- a/src/build.rs +++ b/src/build.rs @@ -1,7 +1,4 @@ -extern crate cmake; - fn main() { - let dst = cmake::build("libscreenshot"); - println!("cargo:rustc-link-lib=static=screenshot"); - println!("cargo:rustc-link-search=native={}/libs", dst.display()); + println!("cargo:rustc-link-lib=slopy"); + println!("cargo:rustc-link-search=native=/usr/lib"); } diff --git a/src/capture.rs b/src/capture.rs index 625b1ce..cbf208d 100644 --- a/src/capture.rs +++ b/src/capture.rs @@ -4,7 +4,7 @@ use cairo::{Context, Format, ImageSurface}; use failure::Error; use gdk::prelude::*; use gdk::{ContextExt, Display, DisplayExt, Screen, ScreenExt, Window as GdkWindow, WindowExt}; -use gdk_pixbuf::Pixbuf; +use gdk_pixbuf::{Pixbuf, PixbufExt}; use gtk::{Clipboard, ClipboardExt}; use time; @@ -48,10 +48,12 @@ pub fn capture(options: Options) -> Result<(), Error> { }?; // launch selection - // let pixbuf = match options.region { - // Region::Selection => select_area(pixbuf)?, - // _ => pixbuf, - // }; + let pixbuf = match options.region { + Region::Selection => select_area(pixbuf)?, + _ => pixbuf, + }; + let width = pixbuf.get_width(); + let height = pixbuf.get_height(); // create and draw to the surface let surface = match ImageSurface::create(Format::Rgb24, width, height) { diff --git a/src/main.rs b/src/main.rs index 03d869a..991d681 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ extern crate clap; extern crate failure; extern crate gdk; extern crate gdk_pixbuf; +extern crate gio; extern crate gtk; extern crate time; @@ -14,8 +15,8 @@ mod capture; mod errors; mod options; mod selection; +mod slop; -use std::path::PathBuf; use std::process::exit; use clap::{App, Arg}; diff --git a/src/selection.rs b/src/selection.rs index 8c9af6f..e0e8f78 100644 --- a/src/selection.rs +++ b/src/selection.rs @@ -1,29 +1,43 @@ +use std::ffi::CString; + use failure::Error; -use gdk::ContextExt; -use gdk_pixbuf::Pixbuf; -use gtk::{ - self, ContainerExt, DrawingArea, GtkWindowExt, Inhibit, WidgetExt, Window as GtkWindow, - WindowType, -}; +use gdk_pixbuf::{Pixbuf, PixbufExt}; + +use errors::ScreenshotError; +use slop::{slop_options, slop_select}; pub fn select_area(pixbuf: Pixbuf) -> Result { - let window = GtkWindow::new(WindowType::Popup); - let mut pixbuf2 = pixbuf.clone(); - let drawing_area = Box::new(DrawingArea::new)(); - drawing_area.connect_draw(move |_, cr| { - cr.set_source_pixbuf(&pixbuf, 0.0, 0.0); - cr.paint(); + let xdisplay = CString::new(":0")?.as_ptr(); - Inhibit(false) - }); - window.add(&drawing_area); + let mut options = slop_options { + quiet: 0, + border: 1.0, + padding: 1.0, + tolerance: 1.0, + highlight: 0, + nokeyboard: 0, + noopengl: 1, + nodecorations: 1, + shaders: &mut 0, + r: 0.7, + g: 0.7, + b: 0.7, + a: 1.0, + xdisplay, + }; + let selection = unsafe { slop_select(&mut options) }; + println!("{:?}", selection); - window.connect_key_release_event(|_, key| { - println!("{:?}", key.get_keyval()); - Inhibit(false) - }); - - window.show_all(); - gtk::main(); - Ok(pixbuf2) + if selection.cancelled == 1 { + bail!(ScreenshotError); + } + match pixbuf.new_subpixbuf( + selection.x.round() as i32, + selection.y.round() as i32, + selection.w.round() as i32, + selection.h.round() as i32, + ) { + Some(pixbuf) => Ok(pixbuf), + None => bail!(ScreenshotError), + } } diff --git a/src/slop.rs b/src/slop.rs new file mode 100644 index 0000000..ca17483 --- /dev/null +++ b/src/slop.rs @@ -0,0 +1,264 @@ +/* automatically generated by rust-bindgen */ + +#![allow(dead_code)] + +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct slop_options { + pub quiet: ::std::os::raw::c_int, + pub border: f32, + pub padding: f32, + pub tolerance: f32, + pub highlight: ::std::os::raw::c_int, + pub noopengl: ::std::os::raw::c_int, + pub nokeyboard: ::std::os::raw::c_int, + pub nodecorations: ::std::os::raw::c_int, + pub shaders: *mut ::std::os::raw::c_char, + pub r: f32, + pub g: f32, + pub b: f32, + pub a: f32, + pub xdisplay: *const ::std::os::raw::c_char, +} +#[test] +fn bindgen_test_layout_slop_options() { + assert_eq!( + ::std::mem::size_of::(), + 64usize, + concat!("Size of: ", stringify!(slop_options)) + ); + assert_eq!( + ::std::mem::align_of::(), + 8usize, + concat!("Alignment of ", stringify!(slop_options)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).quiet as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(quiet) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).border as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(border) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).padding as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(padding) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).tolerance as *const _ as usize }, + 12usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(tolerance) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).highlight as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(highlight) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).noopengl as *const _ as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(noopengl) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).nokeyboard as *const _ as usize }, + 24usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(nokeyboard) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).nodecorations as *const _ as usize }, + 28usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(nodecorations) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).shaders as *const _ as usize }, + 32usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(shaders) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).r as *const _ as usize }, + 40usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(r) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).g as *const _ as usize }, + 44usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(g) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).b as *const _ as usize }, + 48usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(b) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).a as *const _ as usize }, + 52usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(a) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).xdisplay as *const _ as usize }, + 56usize, + concat!( + "Offset of field: ", + stringify!(slop_options), + "::", + stringify!(xdisplay) + ) + ); +} +#[repr(C)] +#[derive(Debug, Copy, Clone)] +pub struct slop_selection { + pub cancelled: ::std::os::raw::c_int, + pub x: f32, + pub y: f32, + pub w: f32, + pub h: f32, + pub id: ::std::os::raw::c_int, +} +#[test] +fn bindgen_test_layout_slop_selection() { + assert_eq!( + ::std::mem::size_of::(), + 24usize, + concat!("Size of: ", stringify!(slop_selection)) + ); + assert_eq!( + ::std::mem::align_of::(), + 4usize, + concat!("Alignment of ", stringify!(slop_selection)) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).cancelled as *const _ as usize }, + 0usize, + concat!( + "Offset of field: ", + stringify!(slop_selection), + "::", + stringify!(cancelled) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).x as *const _ as usize }, + 4usize, + concat!( + "Offset of field: ", + stringify!(slop_selection), + "::", + stringify!(x) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).y as *const _ as usize }, + 8usize, + concat!( + "Offset of field: ", + stringify!(slop_selection), + "::", + stringify!(y) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).w as *const _ as usize }, + 12usize, + concat!( + "Offset of field: ", + stringify!(slop_selection), + "::", + stringify!(w) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).h as *const _ as usize }, + 16usize, + concat!( + "Offset of field: ", + stringify!(slop_selection), + "::", + stringify!(h) + ) + ); + assert_eq!( + unsafe { &(*(::std::ptr::null::())).id as *const _ as usize }, + 20usize, + concat!( + "Offset of field: ", + stringify!(slop_selection), + "::", + stringify!(id) + ) + ); +} +extern "C" { + pub fn slop_options_default() -> slop_options; +} +extern "C" { + pub fn slop_select(options: *mut slop_options) -> slop_selection; +}