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
use async_process::Command;
use regex::Regex;
use zbus::dbus_interface;

use crate::opts::Opts;

pub struct Screenshot {
    pub opts: Opts,
}

#[dbus_interface(name = "org.gnome.Shell.Screenshot")]
impl Screenshot {
    async fn screenshot(
        &self,
        include_cursor: bool,
        flash: bool,
        filename: &str,
    ) -> (bool, String) {
        if self.opts.debug {
            println!(
                "[ org.gnome.Shell.Screenshot ] called screenshot method with following args:"
            );
            println!(
                "include_cursor: {}, flash: {}, filename: {},",
                include_cursor, flash, filename
            );
        }

        if self.opts.show_warning {
            let _ = Command::new("pw-play")
                .arg("/usr/share/sounds/freedesktop/stereo/screen-capture.oga")
                .output()
                .await;
            let _ = Command::new("zenity")
                .arg("--warning")
                .arg(
                    "--text=\"incoming screenshot, make sure to be on the corresponding worspace\"",
                )
                .output()
                .await;
        }

        let mut grim = Command::new("grim");
        if include_cursor {
            grim.arg("-c");
        }
        let out = grim.arg(filename).output().await.is_ok();

        (out, filename.into())
    }

    async fn screenshot_window(
        &self,
        include_frame: bool,
        include_cursor: bool,
        flash: bool,
        filename: &str,
    ) -> (bool, String) {
        if self.opts.debug {
            println!(
                "[ org.gnome.Shell.Screenshot ] called screenshot_window method with following args:"
            );
            println!(
                "include_frame: {}, include_cursor: {}, flash: {}, filename: {},",
                include_frame, include_cursor, flash, filename
            );
        }

        if self.opts.show_warning {
            let _ = Command::new("pw-play")
                .arg("/usr/share/sounds/freedesktop/stereo/screen-capture.oga")
                .output()
                .await;
            let _ = Command::new("zenity")
                .arg("--warning")
                .arg("--text=\"incoming window capture, make sure to focus the correct window (you have 2 secs once you close this warning)\"")
                .output()
                .await;

            let _ = Command::new("sleep").arg("2").output().await;
        }

        let mut grim = Command::new("grim");
        if include_cursor {
            grim.arg("-c");
        }

        // ask current window coordinates and position
        if let Ok(swaymsg) = Command::new("swaymsg")
            .arg("-t")
            .arg("get_tree")
            .output()
            .await
        {
            if let Ok(tree) = String::from_utf8(swaymsg.stdout) {
                let raw_json: &str = &tree
                    .replace(['\n', '\r', '\t', ' '], "")
                    .split("},{")
                    .filter(|v| v.contains("\"focused\":true"))
                    .collect::<Vec<&str>>()
                    .join("");

                let re = Regex::new(
                    r#""rect".*"x":(\d*),"y":(\d*),"width":(\d*),"height":(\d*).*,"deco_rect":"#,
                )
                .unwrap();
                if let Some(cap) = re.captures(raw_json) {
                    if self.opts.debug {
                        println!(
                            "Focused window coordinates: x: {}, y: {}, w: {}, h: {}",
                            &cap[1], &cap[2], &cap[3], &cap[4]
                        );
                    }
                    grim.arg("-g")
                        .arg(format!("{},{} {}x{}", &cap[1], &cap[2], &cap[3], &cap[4]));
                }
            }
        };

        let out = grim.arg(filename).output().await.is_ok();

        (out, filename.into())
    }

    async fn screenshot_area(
        &self,
        x: i32,
        y: i32,
        width: i32,
        height: i32,
        flash: bool,
        filename: &str,
    ) -> (bool, String) {
        if self.opts.debug {
            println!(
                "[ org.gnome.Shell.Screenshot ] called screenshot_window method with following args:"
            );
            println!(
                "x: {}, y: {}, width: {}, height: {} flash: {}, filename: {},",
                x, y, width, height, flash, filename
            );
        }

        if self.opts.show_warning {
            let _ = Command::new("pw-play")
                .arg("/usr/share/sounds/freedesktop/stereo/screen-capture.oga")
                .output()
                .await;
            let _ = Command::new("zenity")
                .arg("--warning")
                .arg("--text=\"incoming Area capture, Go to the correct workspace\"")
                .output()
                .await;
        }

        let out = Command::new("grim")
            .arg("-g")
            .arg(format!("{x},{y} {width}x{height}"))
            .arg(filename)
            .output()
            .await
            .is_ok();

        (out, filename.into())
    }
}