feed the automation settings into the movers
This commit is contained in:
@@ -2,7 +2,7 @@ package wtf.beatrice.autosqueal.controls;
|
||||
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import wtf.beatrice.autosqueal.util.RunnerUtil;
|
||||
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
|
||||
|
||||
import java.awt.*;
|
||||
import java.security.SecureRandom;
|
||||
@@ -10,10 +10,11 @@ import java.util.Random;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.function.BooleanSupplier;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Periodically starts a new movement of the mouse cursor to a random position,
|
||||
* or to the top-right corner of the screen for a double click.
|
||||
* or to the top-right corner of the main screen for a double click.
|
||||
*
|
||||
* The movements only happen when the user is away: while the user is actively
|
||||
* using the machine, they are skipped, so the app never fights for the mouse.
|
||||
@@ -21,26 +22,50 @@ import java.util.function.BooleanSupplier;
|
||||
* the user is still away at every step, and gives the mouse back when they
|
||||
* are not.
|
||||
*
|
||||
* The movement itself is executed by a {@link SingleStepMovementTask}, which
|
||||
* runs on its own virtual thread until it reaches its destination.
|
||||
* Everything that varies — how often to move, how fast, whether and how
|
||||
* often to click, which screens to travel — comes from the settings, which
|
||||
* are read live at every movement. The movement itself is executed by a
|
||||
* {@link SingleStepMovementTask}, which runs on its own virtual thread
|
||||
* until it reaches its destination.
|
||||
*/
|
||||
public class CursorMover implements Runnable
|
||||
{
|
||||
public class CursorMover implements Runnable {
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(CursorMover.class);
|
||||
|
||||
private static final int LOOPS_BEFORE_CLICK = 5;
|
||||
/** Distance from the screen edge, so the corner click lands inside the screen. */
|
||||
private static final int CORNER_OFFSET_PIXELS = 5;
|
||||
|
||||
private final Random random = new SecureRandom();
|
||||
private final Random random;
|
||||
private final ExecutorService movementExecutor;
|
||||
private final RobotMouseTracker robotTracker;
|
||||
private final BooleanSupplier isUserAway;
|
||||
private final AutoSquealConfig config;
|
||||
private final Supplier<Rectangle> movementBounds;
|
||||
private final Supplier<Rectangle> primaryScreenBounds;
|
||||
private final Supplier<Point> currentPosition;
|
||||
|
||||
/** Movements started since the last corner click. */
|
||||
private int iteration = 0;
|
||||
|
||||
public CursorMover(ExecutorService movementExecutor, RobotMouseTracker robotTracker, BooleanSupplier isUserAway) {
|
||||
public CursorMover(ExecutorService movementExecutor, RobotMouseTracker robotTracker,
|
||||
BooleanSupplier isUserAway, AutoSquealConfig config,
|
||||
Supplier<Rectangle> movementBounds, Supplier<Rectangle> primaryScreenBounds) {
|
||||
this(movementExecutor, robotTracker, isUserAway, config, movementBounds, primaryScreenBounds,
|
||||
new SecureRandom(), () -> MouseInfo.getPointerInfo().getLocation());
|
||||
}
|
||||
|
||||
CursorMover(ExecutorService movementExecutor, RobotMouseTracker robotTracker,
|
||||
BooleanSupplier isUserAway, AutoSquealConfig config,
|
||||
Supplier<Rectangle> movementBounds, Supplier<Rectangle> primaryScreenBounds,
|
||||
Random random, Supplier<Point> currentPosition) {
|
||||
this.movementExecutor = movementExecutor;
|
||||
this.robotTracker = robotTracker;
|
||||
this.isUserAway = isUserAway;
|
||||
this.config = config;
|
||||
this.movementBounds = movementBounds;
|
||||
this.primaryScreenBounds = primaryScreenBounds;
|
||||
this.random = random;
|
||||
this.currentPosition = currentPosition;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -51,34 +76,38 @@ public class CursorMover implements Runnable
|
||||
return;
|
||||
}
|
||||
|
||||
Point location = MouseInfo.getPointerInfo().getLocation();
|
||||
Point location = currentPosition.get();
|
||||
LOGGER.info("Starting coordinates: {}, {}", location.x, location.y);
|
||||
|
||||
int destX;
|
||||
int destY;
|
||||
boolean click;
|
||||
|
||||
if (iteration == LOOPS_BEFORE_CLICK) {
|
||||
destX = RunnerUtil.SCREEN_WIDTH - 5;
|
||||
destY = 5;
|
||||
// one movement every N is a corner double click, so that the
|
||||
// notification area gets exercised too. the corner is always the
|
||||
// main screen's top-right one, whatever screens the cursor may
|
||||
// otherwise travel to.
|
||||
if (config.isClickEnabled() && iteration == config.getClickEveryNMoves() - 1) {
|
||||
Rectangle primary = primaryScreenBounds.get();
|
||||
destX = primary.x + primary.width - CORNER_OFFSET_PIXELS;
|
||||
destY = primary.y + CORNER_OFFSET_PIXELS;
|
||||
click = true;
|
||||
|
||||
iteration = 0;
|
||||
} else {
|
||||
destX = random.nextInt(RunnerUtil.SCREEN_WIDTH);
|
||||
destY = random.nextInt(RunnerUtil.SCREEN_HEIGHT);
|
||||
Rectangle bounds = movementBounds.get();
|
||||
destX = bounds.x + random.nextInt(Math.max(1, bounds.width));
|
||||
destY = bounds.y + random.nextInt(Math.max(1, bounds.height));
|
||||
click = false;
|
||||
|
||||
iteration++;
|
||||
}
|
||||
|
||||
LOGGER.info("Destination coordinates: {}, {}", destX, destY);
|
||||
|
||||
try {
|
||||
SingleStepMovementTask movement = new SingleStepMovementTask(robotTracker, destX, destY, click, isUserAway);
|
||||
SingleStepMovementTask movement = new SingleStepMovementTask(
|
||||
robotTracker, destX, destY, click, isUserAway,
|
||||
config.getStepDelayMilliseconds(), location);
|
||||
movementExecutor.execute(movement);
|
||||
} catch (AWTException ex) {
|
||||
LOGGER.error("Could not start movement task", ex);
|
||||
} catch (RejectedExecutionException ex) {
|
||||
LOGGER.debug("Movement not started: automation is shutting down");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user