-
Notifications
You must be signed in to change notification settings - Fork 14
/
main.rs
180 lines (159 loc) · 5.92 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use std::io::{self, Cursor, Write};
use chrono::Local;
use clap::Parser;
use image::{DynamicImage, ImageFormat};
use log::{error, info};
use runtime_data::RuntimeData;
use smithay_client_toolkit::reexports::client::{globals::registry_queue_init, Connection};
use traits::{Contains, ToLocal};
use types::{Args, Config, ExitState, Monitor, Rect, SaveLocation, Selection};
use wl_clipboard_rs::copy;
mod macros;
mod runtime_data;
mod traits;
mod types;
pub mod window;
mod sctk_impls {
mod compositor_handler;
mod keyboard_handler;
mod layer_shell_handler;
mod output_handler;
mod pointer_handler;
mod provides_registry_state;
mod seat_handler;
mod shm_handler;
}
mod rendering;
fn main() {
let args = Args::parse();
env_logger::init();
if let Some(image) = gui(&args) {
// Save the file if an argument for that is present
if let Some(save_location) = &args.save {
match save_location {
SaveLocation::Path { path } => {
if let Err(why) = image.save(path) {
error!("Error saving image: {}", why);
}
}
SaveLocation::Directory { path } => {
let local = Local::now();
if let Err(why) = image.save(
local
.format(&format!("{}/Watershot_%d-%m-%Y_%H:%M.png", path))
.to_string(),
) {
error!("Error saving image: {}", why);
}
}
}
}
// Save the selected image into the buffer
let mut buf = Cursor::new(Vec::new());
image
.write_to(&mut buf, ImageFormat::Png)
.expect("Failed to write image to buffer as PNG");
let buf = buf.into_inner();
if args.stdout {
if let Err(why) = io::stdout().lock().write_all(&buf) {
error!("Failed to write image content to stdout: {}", why);
}
}
// Fork to serve copy requests
if args.copy {
match unsafe { nix::unistd::fork() } {
Ok(nix::unistd::ForkResult::Parent { .. }) => {
info!("Forked to serve copy requests")
}
Ok(nix::unistd::ForkResult::Child) => {
// Serve copy requests
let mut opts = copy::Options::new();
opts.foreground(true);
opts.copy(
copy::Source::Bytes(buf.into_boxed_slice()),
copy::MimeType::Autodetect,
)
.expect("Failed to serve copied image");
}
Err(why) => println!("Failed to fork: {}", why),
}
}
}
}
fn gui(args: &Args) -> Option<DynamicImage> {
let conn = Connection::connect_to_env();
if conn.is_err() {
log::error!("Could not connect to the Wayland server, make sure you run watershot within a Wayland session!");
std::process::exit(1);
}
let conn = conn.unwrap();
let (globals, mut event_queue) = registry_queue_init(&conn).unwrap();
let qh = event_queue.handle();
let mut runtime_data = RuntimeData::new(&qh, &globals, args.clone());
// Fetch the outputs from the compositor
event_queue.roundtrip(&mut runtime_data).unwrap();
// Has to be iterated first to get the full area size
let sizes = runtime_data
.output_state
.outputs()
.map(|output| {
let info = runtime_data.output_state.info(&output).unwrap();
let size = info
.logical_size
.map(|(w, h)| (w as u32, h as u32))
.expect("Can't determine monitor size!");
let pos = info
.logical_position
.expect("Can't determine monitor position!");
let rect = Rect {
x: pos.0,
y: pos.1,
width: size.0 as i32,
height: size.1 as i32,
};
// Extend the area spanning all monitors with the current monitor
runtime_data.area.extend(&rect);
(rect, output, info)
})
.collect::<Vec<_>>();
runtime_data.scale_factor = runtime_data.image.width() as f32 / runtime_data.area.width as f32;
for (rect, output, info) in sizes {
runtime_data
.monitors
.push(Monitor::new(rect, &qh, &conn, output, info, &runtime_data));
}
event_queue.roundtrip(&mut runtime_data).unwrap();
loop {
event_queue.blocking_dispatch(&mut runtime_data).unwrap();
match runtime_data.exit {
ExitState::ExitOnly => return None,
ExitState::ExitWithSelection(rect) => {
let image = match runtime_data.monitors.into_iter().find_map(|mon| {
if mon.rect.contains(&rect) {
Some(mon)
} else {
None
}
}) {
Some(mon) => {
let rect = rect.to_local(&mon.rect);
mon.image.crop_imm(
rect.x as u32,
rect.y as u32,
rect.width as u32,
rect.height as u32,
)
}
None => runtime_data.image.crop_imm(
(rect.x as f32 * runtime_data.scale_factor) as u32,
(rect.y as f32 * runtime_data.scale_factor) as u32,
(rect.width as f32 * runtime_data.scale_factor) as u32,
(rect.height as f32 * runtime_data.scale_factor) as u32,
),
};
return Some(image);
}
ExitState::None => (),
}
}
}