forked from michael/leanshot
yay all fixed
This commit is contained in:
parent
96ad7bf6cf
commit
4470235275
4 changed files with 69 additions and 28 deletions
|
@ -8,7 +8,7 @@ use Error;
|
||||||
|
|
||||||
/// A simple wrapper around Imlib_Image
|
/// A simple wrapper around Imlib_Image
|
||||||
pub struct Image {
|
pub struct Image {
|
||||||
inner: im::Imlib_Image,
|
pub(crate) inner: im::Imlib_Image,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Image {
|
impl Image {
|
||||||
|
|
|
@ -35,3 +35,36 @@ pub fn context_set_image(image: &Image) {
|
||||||
pub fn image_get_data() -> *mut u32 {
|
pub fn image_get_data() -> *mut u32 {
|
||||||
unsafe { imlib2_sys::imlib_image_get_data_for_reading_only() }
|
unsafe { imlib2_sys::imlib_image_get_data_for_reading_only() }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create cropped image
|
||||||
|
pub fn create_cropped_image(x: i32, y: i32, width: u32, height: u32) -> Result<Image, Error> {
|
||||||
|
let inner =
|
||||||
|
unsafe { imlib2_sys::imlib_create_cropped_image(x, y, width as i32, height as i32) };
|
||||||
|
if inner.is_null() {
|
||||||
|
return Err(Error::Error);
|
||||||
|
}
|
||||||
|
Ok(Image { inner })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create scaled cropped image
|
||||||
|
pub fn create_scaled_cropped_image(
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
width: u32,
|
||||||
|
height: u32,
|
||||||
|
) -> Result<Image, Error> {
|
||||||
|
let inner = unsafe {
|
||||||
|
imlib2_sys::imlib_create_cropped_scaled_image(
|
||||||
|
x,
|
||||||
|
y,
|
||||||
|
width as i32,
|
||||||
|
height as i32,
|
||||||
|
width as i32,
|
||||||
|
height as i32,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if inner.is_null() {
|
||||||
|
return Err(Error::Error);
|
||||||
|
}
|
||||||
|
Ok(Image { inner })
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
use errors::ScreenshotError;
|
use errors::ScreenshotError;
|
||||||
|
|
||||||
use gui::GUI;
|
use gui::GUI;
|
||||||
|
use imlib2;
|
||||||
use options::{Options, Region};
|
use options::{Options, Region};
|
||||||
|
|
||||||
/// The main capture routine.
|
/// The main capture routine.
|
||||||
|
@ -13,6 +14,8 @@ pub fn capture(opt: &Options) -> Result<(), ScreenshotError> {
|
||||||
};
|
};
|
||||||
|
|
||||||
let capture = gui.capture_window(&opt, window_to_capture)?;
|
let capture = gui.capture_window(&opt, window_to_capture)?;
|
||||||
|
|
||||||
|
imlib2::context_set_image(&capture);
|
||||||
capture.save_image(&opt.outfile)?;
|
capture.save_image(&opt.outfile)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
59
src/gui.rs
59
src/gui.rs
|
@ -19,33 +19,26 @@ impl GUI {
|
||||||
/// Captures the window and produces an Image.
|
/// Captures the window and produces an Image.
|
||||||
pub fn capture_window(&self, opt: &Options, window: Window) -> Result<Image2, ScreenshotError> {
|
pub fn capture_window(&self, opt: &Options, window: Window) -> Result<Image2, ScreenshotError> {
|
||||||
let attr = window.get_attributes()?;
|
let attr = window.get_attributes()?;
|
||||||
let mut width = attr.get_width();
|
let width = attr.get_width();
|
||||||
let mut height = attr.get_height();
|
let height = attr.get_height();
|
||||||
let root = attr.get_root();
|
let root = attr.get_root();
|
||||||
let (mut x, mut y, _) = self.display.translate_coordinates(window, 0, 0, root)?;
|
let (x, y, _) = self.display.translate_coordinates(window, 0, 0, root)?;
|
||||||
|
|
||||||
imlib2::context_set_display(&self.display);
|
imlib2::context_set_display(&self.display);
|
||||||
let visual = Visual::default(&self.display, 0);
|
let visual = Visual::default(&self.display, 0);
|
||||||
imlib2::context_set_visual(&visual);
|
imlib2::context_set_visual(&visual);
|
||||||
|
|
||||||
match opt.region {
|
if let Region::Selection = opt.region {
|
||||||
Region::Selection => {
|
let capture =
|
||||||
let capture = Image2::create_from_drawable(
|
Image2::create_from_drawable(window, 0, x, y, width as i32, height as i32, true)?;
|
||||||
window,
|
let region = self.interactive_select(&capture)?;
|
||||||
0,
|
imlib2::context_set_image(&capture);
|
||||||
x,
|
return imlib2::create_scaled_cropped_image(
|
||||||
y,
|
region.x,
|
||||||
width as i32,
|
region.y,
|
||||||
height as i32,
|
region.width,
|
||||||
true,
|
region.height,
|
||||||
)?;
|
).map_err(|err| err.into());
|
||||||
let region = self.interactive_select(capture)?;
|
|
||||||
x = region.x;
|
|
||||||
y = region.y;
|
|
||||||
width = region.width;
|
|
||||||
height = region.height;
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Image2::create_from_drawable(window, 0, x, y, width as i32, height as i32, true)
|
Image2::create_from_drawable(window, 0, x, y, width as i32, height as i32, true)
|
||||||
|
@ -61,7 +54,7 @@ impl GUI {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Brings up an interactive selection GUI.
|
/// Brings up an interactive selection GUI.
|
||||||
pub fn interactive_select(&self, capture: Image2) -> Result<Rectangle, ScreenshotError> {
|
pub fn interactive_select(&self, capture: &Image2) -> Result<Rectangle, ScreenshotError> {
|
||||||
// let window = SelectWindow::new(&self.display);
|
// let window = SelectWindow::new(&self.display);
|
||||||
// let root = self.display.get_default_root_window()?;
|
// let root = self.display.get_default_root_window()?;
|
||||||
|
|
||||||
|
@ -88,7 +81,7 @@ impl GUI {
|
||||||
use gl;
|
use gl;
|
||||||
use glutin::{
|
use glutin::{
|
||||||
self,
|
self,
|
||||||
dpi::{PhysicalSize, PhysicalPosition},
|
dpi::{PhysicalPosition, PhysicalSize},
|
||||||
os::unix::{WindowBuilderExt, WindowExt, XWindowType},
|
os::unix::{WindowBuilderExt, WindowExt, XWindowType},
|
||||||
ElementState, Event, EventsLoop, GlContext, GlWindow, KeyboardInput, MouseButton,
|
ElementState, Event, EventsLoop, GlContext, GlWindow, KeyboardInput, MouseButton,
|
||||||
MouseCursor, VirtualKeyCode, WindowBuilder, WindowEvent,
|
MouseCursor, VirtualKeyCode, WindowBuilder, WindowEvent,
|
||||||
|
@ -113,8 +106,9 @@ impl GUI {
|
||||||
.with_decorations(false)
|
.with_decorations(false)
|
||||||
.with_visibility(false)
|
.with_visibility(false)
|
||||||
.with_always_on_top(true)
|
.with_always_on_top(true)
|
||||||
.with_dimensions(PhysicalSize::new(width.into(), height.into()).to_logical(mon.get_hidpi_factor()))
|
.with_dimensions(
|
||||||
.with_fullscreen(Some(mon));
|
PhysicalSize::new(width.into(), height.into()).to_logical(mon.get_hidpi_factor()),
|
||||||
|
).with_fullscreen(Some(mon));
|
||||||
let ctx = glutin::ContextBuilder::new()
|
let ctx = glutin::ContextBuilder::new()
|
||||||
.with_vsync(false)
|
.with_vsync(false)
|
||||||
.with_multisampling(4)
|
.with_multisampling(4)
|
||||||
|
@ -253,7 +247,7 @@ impl GUI {
|
||||||
|
|
||||||
evl.poll_events(|event| match event {
|
evl.poll_events(|event| match event {
|
||||||
Event::WindowEvent { event, .. } => match event {
|
Event::WindowEvent { event, .. } => match event {
|
||||||
WindowEvent::CloseRequested | WindowEvent::Destroyed => running = false,
|
WindowEvent::Destroyed => running = false,
|
||||||
WindowEvent::KeyboardInput {
|
WindowEvent::KeyboardInput {
|
||||||
input:
|
input:
|
||||||
KeyboardInput {
|
KeyboardInput {
|
||||||
|
@ -269,6 +263,12 @@ impl GUI {
|
||||||
rectw = 0.0;
|
rectw = 0.0;
|
||||||
recth = 0.0;
|
recth = 0.0;
|
||||||
} else {
|
} else {
|
||||||
|
unsafe {
|
||||||
|
x11::xlib::XDestroyWindow(
|
||||||
|
self.display.as_raw(),
|
||||||
|
win.get_xlib_window().unwrap(),
|
||||||
|
)
|
||||||
|
};
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -303,6 +303,12 @@ impl GUI {
|
||||||
}
|
}
|
||||||
ElementState::Released => {
|
ElementState::Released => {
|
||||||
if down && rectw.abs() > 0.0 && recth.abs() > 0.0 {
|
if down && rectw.abs() > 0.0 && recth.abs() > 0.0 {
|
||||||
|
unsafe {
|
||||||
|
x11::xlib::XDestroyWindow(
|
||||||
|
self.display.as_raw(),
|
||||||
|
win.get_xlib_window().unwrap(),
|
||||||
|
)
|
||||||
|
};
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
|
@ -315,7 +321,6 @@ impl GUI {
|
||||||
},
|
},
|
||||||
_ => (),
|
_ => (),
|
||||||
});
|
});
|
||||||
|
|
||||||
win.swap_buffers().expect("couldn't swap buffers");
|
win.swap_buffers().expect("couldn't swap buffers");
|
||||||
}
|
}
|
||||||
if rectw.abs() > 0.0 && recth.abs() > 0.0 {
|
if rectw.abs() > 0.0 && recth.abs() > 0.0 {
|
||||||
|
|
Loading…
Reference in a new issue