fix away detection, pause while user is present

This commit is contained in:
bea
2026-09-29 11:58:03 +02:00
parent ed90b246d6
commit c95e660c2c
8 changed files with 347 additions and 34 deletions
@@ -8,11 +8,15 @@ import java.awt.*;
import java.security.SecureRandom;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.function.BooleanSupplier;
/**
* 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.
*
* 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.
*
* The movement itself is executed by a {@link SingleStepMovementTask}, which
* re-schedules itself on the shared scheduler until it reaches its destination.
*/
@@ -24,16 +28,25 @@ public class CursorMover implements Runnable
private final Random random = new SecureRandom();
private final ScheduledExecutorService scheduler;
private final RobotMouseTracker robotTracker;
private final BooleanSupplier isUserAway;
private int iteration = 0;
public CursorMover(ScheduledExecutorService scheduler) {
public CursorMover(ScheduledExecutorService scheduler, RobotMouseTracker robotTracker, BooleanSupplier isUserAway) {
this.scheduler = scheduler;
this.robotTracker = robotTracker;
this.isUserAway = isUserAway;
}
@Override
public void run() {
if (!isUserAway.getAsBoolean()) {
LOGGER.debug("User is present, skipping movement");
return;
}
Point location = MouseInfo.getPointerInfo().getLocation();
LOGGER.info("Starting coordinates: {}, {}", location.x, location.y);
@@ -58,7 +71,7 @@ public class CursorMover implements Runnable
LOGGER.info("Destination coordinates: {}, {}", destX, destY);
try {
SingleStepMovementTask movement = new SingleStepMovementTask(scheduler, destX, destY, click);
SingleStepMovementTask movement = new SingleStepMovementTask(scheduler, robotTracker, destX, destY, click);
scheduler.execute(movement);
} catch (AWTException ex) {
LOGGER.error("Could not start movement task", ex);
@@ -0,0 +1,37 @@
package wtf.beatrice.autosqueal.controls;
/**
* Keeps track of the mouse movements the app itself performs, so that other
* components (like the away detection) can tell them apart from the user's.
*
* Written by the movement tasks, read by whoever needs to know.
*/
public class RobotMouseTracker {
private volatile boolean moving = false;
private volatile int lastX = Integer.MIN_VALUE;
private volatile int lastY = Integer.MIN_VALUE;
/** Marks the start of a movement performed by the app. */
public void moveStarted() {
moving = true;
}
/**
* Marks the end of a movement performed by the app, which left the cursor
* at the given coordinates.
*/
public void moveEnded(int x, int y) {
lastX = x;
lastY = y;
moving = false;
}
public boolean isMoving() {
return moving;
}
public boolean isLastPosition(int x, int y) {
return x == lastX && y == lastY;
}
}
@@ -15,6 +15,9 @@ import java.util.concurrent.TimeUnit;
*
* When it gets there, it can optionally perform a double click, scheduled as
* separate one-shot actions so that the scheduler is never blocked.
*
* All the movements it performs are reported to the {@link RobotMouseTracker},
* so that the away detection can tell them apart from the user's.
*/
public class SingleStepMovementTask implements Runnable {
@@ -24,6 +27,7 @@ public class SingleStepMovementTask implements Runnable {
private static final long STEP_DELAY_MILLISECONDS = 2L;
private final ScheduledExecutorService scheduler;
private final RobotMouseTracker robotTracker;
private final Robot robot;
private final int destX;
private final int destY;
@@ -34,9 +38,10 @@ public class SingleStepMovementTask implements Runnable {
private float stepX;
private float stepY;
public SingleStepMovementTask(ScheduledExecutorService scheduler, int destinationX, int destinationY, boolean click) throws AWTException {
public SingleStepMovementTask(ScheduledExecutorService scheduler, RobotMouseTracker robotTracker, int destinationX, int destinationY, boolean click) throws AWTException {
this.scheduler = scheduler;
this.robotTracker = robotTracker;
this.destX = destinationX;
this.destY = destinationY;
this.click = click;
@@ -60,6 +65,20 @@ public class SingleStepMovementTask implements Runnable {
@Override
public void run() {
// any position change from here on is the app's doing
robotTracker.moveStarted();
try {
tick();
} catch (RuntimeException ex) {
// whatever happened, don't leave the tracker stuck on "moving"
LOGGER.error("Unexpected error while moving the cursor", ex);
robotTracker.moveEnded(Math.round(currentX), Math.round(currentY));
}
}
private void tick() {
if (hasReachedDestination()) {
onDestinationReached();
return;
@@ -81,6 +100,7 @@ public class SingleStepMovementTask implements Runnable {
// the scheduler was shut down mid-movement: the automation was
// stopped, so just end the movement chain here
LOGGER.debug("Movement interrupted: scheduler is shut down");
robotTracker.moveEnded(Math.round(currentX), Math.round(currentY));
}
}
@@ -91,6 +111,8 @@ public class SingleStepMovementTask implements Runnable {
private void onDestinationReached() {
LOGGER.info("Reached destination [{}, {}], stopping mover", destX, destY);
robotTracker.moveEnded(destX, destY);
if (click) {
scheduleClickSequence();
}