wrap slop

This commit is contained in:
Michael Zhang 2018-05-14 18:03:30 -05:00
parent bb94c1beb0
commit 491b096dc5
No known key found for this signature in database
GPG key ID: A1B65B603268116B
8 changed files with 317 additions and 47 deletions

11
Cargo.lock generated
View file

@ -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"

View file

@ -2,9 +2,7 @@
name = "screenshot"
version = "0.1.0"
authors = ["Michael Zhang <failed.down@gmail.com>"]
[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"

View file

@ -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
------------

View file

@ -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");
}

View file

@ -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) {

View file

@ -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};

View file

@ -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<Pixbuf, Error> {
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),
}
}

264
src/slop.rs Normal file
View file

@ -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::<slop_options>(),
64usize,
concat!("Size of: ", stringify!(slop_options))
);
assert_eq!(
::std::mem::align_of::<slop_options>(),
8usize,
concat!("Alignment of ", stringify!(slop_options))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).quiet as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(quiet)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).border as *const _ as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(border)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).padding as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(padding)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).tolerance as *const _ as usize },
12usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(tolerance)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).highlight as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(highlight)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).noopengl as *const _ as usize },
20usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(noopengl)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).nokeyboard as *const _ as usize },
24usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(nokeyboard)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).nodecorations as *const _ as usize },
28usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(nodecorations)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).shaders as *const _ as usize },
32usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(shaders)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).r as *const _ as usize },
40usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(r)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).g as *const _ as usize },
44usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(g)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).b as *const _ as usize },
48usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(b)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).a as *const _ as usize },
52usize,
concat!(
"Offset of field: ",
stringify!(slop_options),
"::",
stringify!(a)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_options>())).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::<slop_selection>(),
24usize,
concat!("Size of: ", stringify!(slop_selection))
);
assert_eq!(
::std::mem::align_of::<slop_selection>(),
4usize,
concat!("Alignment of ", stringify!(slop_selection))
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_selection>())).cancelled as *const _ as usize },
0usize,
concat!(
"Offset of field: ",
stringify!(slop_selection),
"::",
stringify!(cancelled)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_selection>())).x as *const _ as usize },
4usize,
concat!(
"Offset of field: ",
stringify!(slop_selection),
"::",
stringify!(x)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_selection>())).y as *const _ as usize },
8usize,
concat!(
"Offset of field: ",
stringify!(slop_selection),
"::",
stringify!(y)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_selection>())).w as *const _ as usize },
12usize,
concat!(
"Offset of field: ",
stringify!(slop_selection),
"::",
stringify!(w)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_selection>())).h as *const _ as usize },
16usize,
concat!(
"Offset of field: ",
stringify!(slop_selection),
"::",
stringify!(h)
)
);
assert_eq!(
unsafe { &(*(::std::ptr::null::<slop_selection>())).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;
}