rustautogui 2.5.0

Highly optimized GUI automation library for controlling the mouse and keyboard, with template matching support.
Documentation
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#![allow(clippy::type_complexity)]

#[cfg(not(feature = "lite"))]
use crate::core::template_match;
#[cfg(not(feature = "lite"))]
use crate::data::*;
#[cfg(feature = "opencl")]
use crate::template_match::open_cl::OclVersion;
#[cfg(not(feature = "lite"))]
use crate::{AutoGuiError, ImageProcessingError, MatchMode};
#[cfg(not(feature = "lite"))]
use crate::{DEFAULT_ALIAS, DEFAULT_BCKP_ALIAS};
#[cfg(not(feature = "lite"))]
use image::{ImageBuffer, Luma};
#[cfg(not(feature = "lite"))]
pub use std::{collections::HashMap, env, fmt, fs, path::Path, str::FromStr};
#[cfg(not(feature = "lite"))]
impl crate::RustAutoGui {
    /// Searches for prepared template on screen.
    /// On windows only main monitor search is supported, while on linux, all monitors work.
    /// more details in README
    #[cfg(not(feature = "lite"))]
    #[allow(unused_variables)]
    pub fn find_image_on_screen(
        &mut self,
        precision: f32,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        /// searches for image on screen and returns found locations in vector format
        let image: ImageBuffer<Luma<u8>, Vec<u8>> = self
            .screen
            .grab_screen_image_grayscale(&self.template_data.region)?;

        if self.debug {
            let debug_path = Path::new("debug");
            if !debug_path.exists() {
                match fs::create_dir_all(debug_path) {
                    Ok(_) => {
                        println!("Created a debug folder in your root for saving segmented template images");
                        match image.save("debug/screen_capture.png") {
                            Ok(_) => (),
                            Err(x) => println!("{}", x),
                        };
                    }
                    Err(x) => {
                        println!("Failed to create debug folder");
                        println!("{}", x);
                    }
                };
            }
        };

        #[cfg(target_os = "macos")]
        let locations = match self.run_macos_xcorr_with_backup(image, precision)? {
            Some(x) => x,
            None => return Ok(None),
        };
        #[cfg(not(target_os = "macos"))]
        let locations = match self.run_x_corr(image, precision)? {
            Some(x) => x,
            None => return Ok(None),
        };

        let locations_ajusted: Vec<(u32, u32, f32)> = locations
            .iter()
            .map(|(mut x, mut y, corr)| {
                x = x + self.template_data.region.0 + (self.template_width / 2);
                y = y + self.template_data.region.1 + (self.template_height / 2);
                (x, y, *corr)
            })
            .collect();

        Ok(Some(locations_ajusted))
    }

    // for macOS with retina display, two runs are made. One for resized template
    // and if not found , then second for normal sized template
    // since the function recursively calls find_stored_image_on_screen -> run_macos_xcorr_with_backup
    // covers are made to not run it for backup aswell
    #[cfg(not(feature = "lite"))]
    #[cfg(target_os = "macos")]
    fn run_macos_xcorr_with_backup(
        &mut self,
        image: ImageBuffer<Luma<u8>, Vec<u8>>,
        precision: f32,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        let first_match = self.run_x_corr(image, precision);
        // if retina and if this is not already a recursively ran backup
        if ((self.screen.screen_data.scaling_factor_x > 1.0)
            | (self.screen.screen_data.scaling_factor_y > 1.0))
            & (!self.template_data.alias_used.contains(DEFAULT_BCKP_ALIAS))
        {
            match first_match? {
                Some(result) => return Ok(Some(result)),
                None => {
                    let mut bckp_alias = String::new();

                    // if its not a single image search, create a alias_backup hash
                    if self.template_data.alias_used != DEFAULT_ALIAS.to_string() {
                        bckp_alias.push_str(self.template_data.alias_used.as_str());
                        bckp_alias.push('_');
                    }
                    bckp_alias.push_str(DEFAULT_BCKP_ALIAS);
                    // this recursively searches again for backup
                    return self.find_stored_image_on_screen(precision, &bckp_alias);
                }
            }
        }
        first_match
    }
    #[cfg(not(feature = "lite"))]
    /// loops until image is found and returns found values, or until it times out
    pub fn loop_find_image_on_screen(
        &mut self,
        precision: f32,
        timeout: u64,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        if (timeout == 0) & (!self.suppress_warnings) {
            eprintln!(
                "Warning: setting a timeout to 0 on a loop find image initiates an infinite loop"
            )
        }

        let timeout_start = std::time::Instant::now();
        loop {
            if (timeout_start.elapsed().as_secs() > timeout) & (timeout > 0) {
                Err(ImageProcessingError::new(
                    "loop find image timed out. Could not find image",
                ))?;
            }
            let result = self.find_image_on_screen(precision)?;
            match result {
                Some(r) => return Ok(Some(r)),
                None => continue,
            }
        }
    }
    #[cfg(not(feature = "lite"))]
    /// find image stored under provided alias
    pub fn find_stored_image_on_screen(
        &mut self,
        precision: f32,
        alias: &str,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        let (prepared_data, region, match_mode) = self
            .template_data
            .prepared_data_stored
            .get(alias)
            .ok_or(AutoGuiError::AliasError(
                "No template stored with selected alias".to_string(),
            ))?;
        // save to reset after finished
        let backup = BackupData {
            starting_data: self.template_data.prepared_data.clone(),
            starting_region: self.template_data.region,
            starting_match_mode: self.template_data.match_mode.clone(),
            starting_template_height: self.template_height,
            starting_template_width: self.template_width,
            starting_alias_used: self.template_data.alias_used.clone(),
        };

        self.template_data.alias_used = alias.into();
        self.template_data.prepared_data = prepared_data.clone();
        self.screen.screen_data.screen_region_width = region.2;
        self.screen.screen_data.screen_region_height = region.3;
        self.template_data.region = *region;
        self.template_data.match_mode = Some(match_mode.clone());
        match prepared_data {
            PreparedData::FFT(data) => {
                self.template_width = data.template_width;
                self.template_height = data.template_height;
            }
            PreparedData::Segmented(data) => {
                self.template_width = data.template_width;
                self.template_height = data.template_height;
            }
            PreparedData::None => Err(ImageProcessingError::new("No prepared data loaded"))?,
        };
        let points = self.find_image_on_screen(precision)?;
        // reset to starting info
        backup.update_rustautogui(self);

        Ok(points)
    }

    #[cfg(not(feature = "lite"))]
    /// loops until stored image is found and returns found values, or until it times out
    pub fn loop_find_stored_image_on_screen(
        &mut self,
        precision: f32,
        timeout: u64,
        alias: &str,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        if (timeout == 0) & (!self.suppress_warnings) {
            eprintln!(
                "Warning: setting a timeout to 0 on a loop find image initiates an infinite loop"
            )
        }
        let timeout_start = std::time::Instant::now();
        loop {
            if (timeout_start.elapsed().as_secs() > timeout) & (timeout > 0) {
                Err(ImageProcessingError::new(
                    "loop find image timed out. Could not find image",
                ))?;
            }
            let result = self.find_stored_image_on_screen(precision, alias)?;
            match result {
                Some(r) => return Ok(Some(r)),
                None => continue,
            }
        }
    }
    #[cfg(not(feature = "lite"))]
    /// searches for image stored under provided alias and moves mouse to position
    pub fn find_stored_image_on_screen_and_move_mouse(
        &mut self,
        precision: f32,
        moving_time: f32,
        alias: &str,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        let (prepared_data, region, match_mode) = self
            .template_data
            .prepared_data_stored
            .get(alias)
            .ok_or(AutoGuiError::AliasError(
                "No template stored with selected alias".to_string(),
            ))?;
        // save to reset after finished
        let backup = BackupData {
            starting_data: self.template_data.prepared_data.clone(),
            starting_region: self.template_data.region,
            starting_match_mode: self.template_data.match_mode.clone(),
            starting_template_height: self.template_height,
            starting_template_width: self.template_width,
            starting_alias_used: self.template_data.alias_used.clone(),
        };
        self.template_data.alias_used = alias.into();
        self.template_data.prepared_data = prepared_data.clone();
        self.template_data.region = *region;
        self.screen.screen_data.screen_region_width = region.2;
        self.screen.screen_data.screen_region_height = region.3;
        self.template_data.match_mode = Some(match_mode.clone());
        match prepared_data {
            PreparedData::FFT(data) => {
                self.template_width = data.template_width;
                self.template_height = data.template_height;
            }
            PreparedData::Segmented(data) => {
                self.template_width = data.template_width;
                self.template_height = data.template_height;
            }
            PreparedData::None => Err(ImageProcessingError::new("No prepared data loaded"))?,
        };
        let found_points = self.find_image_on_screen_and_move_mouse(precision, moving_time);

        // reset to starting info
        backup.update_rustautogui(self);

        found_points
    }
    #[cfg(not(feature = "lite"))]
    /// loops until stored image is found and moves mouse
    pub fn loop_find_stored_image_on_screen_and_move_mouse(
        &mut self,
        precision: f32,
        moving_time: f32,
        timeout: u64,
        alias: &str,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        if (timeout == 0) & (!self.suppress_warnings) {
            eprintln!(
                "Warning: setting a timeout to 0 on a loop find image initiates an infinite loop"
            )
        }
        let timeout_start = std::time::Instant::now();
        loop {
            if (timeout_start.elapsed().as_secs() > timeout) & (timeout > 0) {
                Err(ImageProcessingError::new(
                    "loop find image timed out. Could not find image",
                ))?;
            }
            let result =
                self.find_stored_image_on_screen_and_move_mouse(precision, moving_time, alias)?;
            match result {
                Some(r) => return Ok(Some(r)),
                None => continue,
            }
        }
    }
    #[cfg(not(feature = "lite"))]
    /// executes find_image_on_screen and moves mouse to the middle of the image.
    pub fn find_image_on_screen_and_move_mouse(
        &mut self,
        precision: f32,
        moving_time: f32,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        /// finds coordinates of the image on the screen and moves mouse to it. Returns None if no image found
        ///  Best used in loops
        let found_locations = self.find_image_on_screen(precision)?;

        let locations = match found_locations.clone() {
            Some(locations) => locations,
            None => return Ok(None),
        };

        let (target_x, target_y, _) = locations[0];

        self.move_mouse_to_pos(target_x, target_y, moving_time)?;

        Ok(Some(locations))
    }
    #[cfg(not(feature = "lite"))]
    /// loops until image is found and returns found values, or until it times out
    pub fn loop_find_image_on_screen_and_move_mouse(
        &mut self,
        precision: f32,
        moving_time: f32,
        timeout: u64,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        if (timeout == 0) & (!self.suppress_warnings) {
            eprintln!(
                "Warning: setting a timeout to 0 on a loop find image initiates an infinite loop"
            )
        }
        let timeout_start = std::time::Instant::now();
        loop {
            if (timeout_start.elapsed().as_secs() > timeout) & (timeout > 0) {
                Err(ImageProcessingError::new(
                    "loop find image timed out. Could not find image",
                ))?;
            }
            let result = self.find_image_on_screen_and_move_mouse(precision, moving_time)?;
            match result {
                Some(e) => return Ok(Some(e)),
                None => continue,
            }
        }
    }

    #[cfg(not(feature = "lite"))]
    fn run_x_corr(
        &mut self,
        image: ImageBuffer<Luma<u8>, Vec<u8>>,
        precision: f32,
    ) -> Result<Option<Vec<(u32, u32, f32)>>, AutoGuiError> {
        let match_mode = self.template_data.match_mode.clone().ok_or(ImageProcessingError::new("No template chosen and no template data prepared. Please run load_and_prepare_template before searching image on screen"))?;
        let found_locations: Vec<(u32, u32, f32)> = match match_mode {
            MatchMode::FFT => {
                println!("Running FFT mode");
                let data = match &self.template_data.prepared_data {
                    PreparedData::FFT(data) => data,
                    _ => Err(ImageProcessingError::new(
                        "error in prepared data type. Matchmode does not match prepare data type",
                    ))?,
                };
                let found_locations: Vec<(u32, u32, f64)> =
                    template_match::fft_ncc::fft_ncc(&image, precision, data);
                found_locations
                    .into_iter()
                    .map(|(x, y, value)| (x, y, value as f32))
                    .collect()
            }
            MatchMode::Segmented => {
                println!("Running Segmented mode");
                let data = match &self.template_data.prepared_data {
                    PreparedData::Segmented(data) => data,
                    _ => Err(ImageProcessingError::new(
                        "error in prepared data type. Matchmode does not match prepare data type",
                    ))?,
                };
                template_match::segmented_ncc::fast_ncc_template_match(
                    &image,
                    precision,
                    data,
                    &self.debug,
                )
            }
            #[cfg(feature = "opencl")]
            MatchMode::SegmentedOcl => {
                let data = match &self.template_data.prepared_data {
                    PreparedData::Segmented(data) => data,
                    _ => Err(ImageProcessingError::new(
                        "error in prepared data type. Matchmode does not match prepare data type",
                    ))?,
                };
                let gpu_memory_pointers = self
                    .opencl_data
                    .ocl_buffer_storage
                    .get(&self.template_data.alias_used)
                    .ok_or(ImageProcessingError::new("Error , no OCL data prepared"))?;
                template_match::open_cl::gui_opencl_ncc_template_match(
                    &self.opencl_data.ocl_queue,
                    &self.opencl_data.ocl_program,
                    self.opencl_data.ocl_workgroup_size,
                    &self.opencl_data.ocl_kernel_storage[&self.template_data.alias_used],
                    gpu_memory_pointers,
                    precision,
                    &image,
                    data,
                    OclVersion::V1,
                )?
            }
            #[cfg(feature = "opencl")]
            MatchMode::SegmentedOclV2 => {
                let data = match &self.template_data.prepared_data {
                    PreparedData::Segmented(data) => data,
                    _ => Err(ImageProcessingError::new(
                        "error in prepared data type. Matchmode does not match prepare data type",
                    ))?,
                };
                let gpu_memory_pointers = self
                    .opencl_data
                    .ocl_buffer_storage
                    .get(&self.template_data.alias_used)
                    .ok_or(ImageProcessingError::new("Error , no OCL data prepared"))?;
                template_match::open_cl::gui_opencl_ncc_template_match(
                    &self.opencl_data.ocl_queue,
                    &self.opencl_data.ocl_program,
                    self.opencl_data.ocl_workgroup_size,
                    &self.opencl_data.ocl_kernel_storage[&self.template_data.alias_used],
                    gpu_memory_pointers,
                    precision,
                    &image,
                    data,
                    OclVersion::V2,
                )?
            }
        };
        if !found_locations.is_empty() {
            if self.debug {
                let x =
                    found_locations[0].0 + (self.template_width / 2) + self.template_data.region.0;
                let y =
                    found_locations[0].1 + (self.template_height / 2) + self.template_data.region.1;
                let corr = found_locations[0].2;
                let corrected_found_location = (x, y, corr);

                println!(
                    "Location found at x: {}, y {}, corr {} ",
                    corrected_found_location.0,
                    corrected_found_location.1,
                    corrected_found_location.2
                )
            }
            Ok(Some(found_locations))
        } else {
            Ok(None)
        }
    }
}