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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Simple library that allows for simple method of asking for screenshots from various Linux/BSD desktops

use std::env;
use std::fs;
use std::process::Command;

static SELECTION_TEMPORARY_FILE: &str = "/tmp/selection-tmp.png";

pub enum ScreenshotKind {
    Area,
    Window,
    Full,
}

enum SessionKind {
    Wayland,
    X11,
    Macos,
}

enum DesktopKind {
    GNOME,
    KDE,
    Sway,
    Generic,
    Macos,
}

fn session_type() -> SessionKind {
    return match env::var("XDG_SESSION_TYPE") {
        Ok(ok) => match ok.to_lowercase().as_ref() {
            "wayland" => SessionKind::Wayland,
            _ => SessionKind::X11,
        },
        Err(_) => {
            if !cfg!(target_os = "macos") {
                SessionKind::Macos
            } else {
                SessionKind::X11
            }
        }
    };
}

fn screenshot_tool_selection(session: SessionKind) -> DesktopKind {
    return match session {
        SessionKind::Wayland => match Command::new("grim").arg("--version").spawn() {
            Ok(_) => DesktopKind::Sway,
            Err(_) => match Command::new("spectacle").arg("--version").spawn() {
                Ok(_) => DesktopKind::KDE,
                Err(_) => match Command::new("gnome-screenshot").arg("--version").spawn() {
                    Ok(_) => DesktopKind::GNOME,
                    Err(_) => panic!("Uncompatible Wayland desktop"),
                },
            },
        },
        SessionKind::X11 => match Command::new("spectacle").arg("--version").spawn() {
            Ok(_) => DesktopKind::KDE,
            Err(_) => match Command::new("gnome-screenshot").arg("--version").spawn() {
                Ok(_) => DesktopKind::GNOME,
                Err(_) => match Command::new("scrot").arg("--version").spawn() {
                    Ok(_) => DesktopKind::Generic,
                    Err(_) => panic!("Uncompatible X11 desktop (install scrot)"),
                },
            },
        },
        SessionKind::Macos => DesktopKind::Macos,
    };
}

pub fn screenshot_area(file: String, freeze: bool) {
    match screenshot_tool_selection(session_type()) {
        DesktopKind::GNOME => gnome(ScreenshotKind::Area, file, freeze),
        DesktopKind::KDE => kde(ScreenshotKind::Area, file),
        DesktopKind::Sway => sway(ScreenshotKind::Area, file),
        DesktopKind::Generic => scrot(ScreenshotKind::Area, file, freeze),
        DesktopKind::Macos => mac(ScreenshotKind::Area, file),
    }
}
pub fn screenshot_window(file: String) {
    match screenshot_tool_selection(session_type()) {
        DesktopKind::GNOME => gnome(ScreenshotKind::Window, file, false),
        DesktopKind::KDE => kde(ScreenshotKind::Window, file),
        DesktopKind::Sway => sway(ScreenshotKind::Window, file),
        DesktopKind::Generic => scrot(ScreenshotKind::Window, file, false),
        DesktopKind::Macos => mac(ScreenshotKind::Window, file),
    }
}
pub fn screenshot_full(file: String) {
    match screenshot_tool_selection(session_type()) {
        DesktopKind::GNOME => gnome(ScreenshotKind::Full, file, false),
        DesktopKind::KDE => kde(ScreenshotKind::Full, file),
        DesktopKind::Sway => sway(ScreenshotKind::Full, file),
        DesktopKind::Generic => scrot(ScreenshotKind::Full, file, false),
        DesktopKind::Macos => mac(ScreenshotKind::Full, file),
    }
}
fn gnome(option: ScreenshotKind, file: String, freeze: bool) {
    match option {
        ScreenshotKind::Area => {
            let mut feh = match Command::new("feh").arg("--version").spawn() {
                Ok(_) => {
                    Command::new("gnome-screenshot")
                        .args(&["-f", SELECTION_TEMPORARY_FILE])
                        .output()
                        .expect("gnome-screenshot did not launch");
                    Command::new("feh")
                        .args(&[SELECTION_TEMPORARY_FILE, "-F"])
                        .spawn()
                        .expect("'feh' did not launch to pause screen for selection")
                }
                Err(_) => Command::new("sh")
                    .arg("-c")
                    .arg("echo Feh does not exist")
                    .spawn()
                    .unwrap(),
            };
            Command::new("gnome-screenshot")
                .args(&["-a", "-f", &file])
                .output()
                .expect("gnome-screenshot did not launch");
            if freeze {
                match fs::remove_file(SELECTION_TEMPORARY_FILE) {
                    Ok(ok) => ok,
                    Err(_) => eprintln!("Unable to remove temporary selection file"),
                };
                match feh.kill() {
                    Ok(ok) => ok,
                    Err(_) => eprintln!("Unable to kill feh, must have already been closed"),
                };
            }
        }
        ScreenshotKind::Window => {
            Command::new("gnome-screenshot")
                .args(&["-w", "-e", "shadow", "-f", &file])
                .output()
                .expect("gnome-screenshot did not launch");
        }
        ScreenshotKind::Full => {
            Command::new("gnome-screenshot")
                .args(&["-f", &file])
                .output()
                .expect("gnome-screenshot did not launch");
        }
    };
}
fn kde(option: ScreenshotKind, file: String) {
    match option {
        ScreenshotKind::Area => {
            Command::new("spectacle")
                .args(&["-rbno", &file])
                .output()
                .expect("spectacle did not launch");
        }
        ScreenshotKind::Window => {
            Command::new("spectacle")
                .args(&["-abno", &file])
                .output()
                .expect("spectacle did not launch");
        }
        ScreenshotKind::Full => {
            Command::new("spectacle")
                .args(&["-fbno", &file])
                .output()
                .expect("spectacle did not launch");
        }
    };
}
fn sway(option: ScreenshotKind, file: String) {
    match option {
        ScreenshotKind::Area => {
            let slurp = Command::new("slurp")
                .output()
                .expect("slurp did not launch");
            Command::new("grim")
                .args(&["-g", &String::from_utf8(slurp.stdout).unwrap(), &file])
                .output()
                .expect("grim did not launch");
        }
        ScreenshotKind::Window => {
            let slurp = Command::new("slurp")
                .output()
                .expect("slurp did not launch");
            Command::new("grim")
                .args(&["-g", &String::from_utf8(slurp.stdout).unwrap(), &file])
                .output()
                .expect("grim did not launch");
        }
        ScreenshotKind::Full => {
            Command::new("grim")
                .arg(&file)
                .output()
                .expect("grim did not launch");
        }
    };
}
fn mac(option: ScreenshotKind, file: String) {
    match option {
        ScreenshotKind::Area => {
            Command::new("screencapture")
                .args(&["-s", &file])
                .output()
                .expect("screencapture did not launch");
        }
        ScreenshotKind::Window => {
            Command::new("screencapture")
                .args(&["-w", &file])
                .output()
                .expect("screencapture did not launch");
        }
        ScreenshotKind::Full => {
            Command::new("screencapture")
                .args(&["-S", &file])
                .output()
                .expect("screencapture did not launch");
        }
    };
}
fn scrot(option: ScreenshotKind, file: String, freeze: bool) {
    match option {
        ScreenshotKind::Area => {
            let mut feh = match Command::new("feh").arg("--version").spawn() {
                Ok(_) => {
                    Command::new("scrot")
                        .arg(SELECTION_TEMPORARY_FILE)
                        .output()
                        .expect("scrot did not launch");
                    Command::new("feh")
                        .args(&[SELECTION_TEMPORARY_FILE, "-F"])
                        .spawn()
                        .expect("'feh' did not launch to pause screen for selection")
                }
                Err(_) => Command::new("sh")
                    .arg("-c")
                    .arg("echo Feh does not exist")
                    .spawn()
                    .unwrap(),
            };
            Command::new("scrot")
                .args(&["--select", &file])
                .output()
                .expect("scrot did not launch");
            if freeze {
                match fs::remove_file(SELECTION_TEMPORARY_FILE) {
                    Ok(ok) => ok,
                    Err(_) => eprintln!("Unable to remove temporary selection file"),
                };
                match feh.kill() {
                    Ok(ok) => ok,
                    Err(_) => eprintln!("Unable to kill feh, must have already been closed"),
                };
            }
        }
        ScreenshotKind::Window => {
            Command::new("scrot")
                .args(&["--border", "--focused", &file])
                .output()
                .expect("gnome-screenshot did not launch");
        }
        ScreenshotKind::Full => {
            Command::new("scrot")
                .args(&[&file])
                .output()
                .expect("gnome-screenshot did not launch");
        }
    };
}