fix scheduling, threading and movement bugs
scheduling: - cursor mover created a new java.util.timer for every movement and never cancelled it, leaking a live thread roughly every 10 seconds; timers are replaced by a single shared scheduledexecutorservice with daemon threads, shut down when the automation stops. clicks: - the double click sequence used thread.sleep() inside a timer task, blocking the scheduler thread; it is now scheduled as one-shot actions on the shared scheduler, with the same pacing as before. - stopping the automation now also cancels in-flight movements and pending clicks, instead of letting them run to completion. movement math: - when the destination shared one coordinate with the starting position, the step on the other axis was computed as length/0 = infinity, and the stop condition checked one axis with || instead of &&, so a movement could stop (and click) before reaching its destination. steps are now guarded and the movement stops only when both axes have converged. ui and lifecycle: - the app could not be closed: closing the window only hid it, and ESC unregistered the hotkey without exiting. the window now exits cleanly on close, stopping the automation and unregistering the native hook. - toggleRunning() was called from the jnativehook thread and touched swing components off the EDT; UI updates are now marshalled to the EDT. - the hotkey thrashed: key repeats re-fired the toggle while ctrl+alt was held, and any other key pressed with the combo held toggled it too. it now fires exactly once, when the combo is completed. away-detection (CursorMoveListener) stays intentionally unscheduled: it cannot tell the user's movements apart from the app's own, and will be reworked in a future iteration.
This commit is contained in:
@@ -7,66 +7,61 @@ import wtf.beatrice.autosqueal.util.RunnerUtil;
|
||||
import java.awt.*;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
import java.util.Timer;
|
||||
import java.util.TimerTask;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
public class CursorMover extends TimerTask
|
||||
/**
|
||||
* 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 movement itself is executed by a {@link SingleStepMovementTask}, which
|
||||
* re-schedules itself on the shared scheduler until it reaches its destination.
|
||||
*/
|
||||
public class CursorMover implements Runnable
|
||||
{
|
||||
|
||||
private static final Logger LOGGER = LogManager.getLogger(CursorMover.class);
|
||||
private final Random random;
|
||||
|
||||
private static final int LOOPS_BEFORE_CLICK = 5;
|
||||
|
||||
private final Random random = new SecureRandom();
|
||||
private final ScheduledExecutorService scheduler;
|
||||
|
||||
private int iteration = 0;
|
||||
|
||||
public CursorMover() {
|
||||
random = new SecureRandom();
|
||||
public CursorMover(ScheduledExecutorService scheduler) {
|
||||
this.scheduler = scheduler;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
|
||||
int currentX = MouseInfo.getPointerInfo().getLocation().x;
|
||||
int currentY = MouseInfo.getPointerInfo().getLocation().y;
|
||||
|
||||
LOGGER.info("Starting coordinates: {}, {}", currentX, currentY);
|
||||
Point location = MouseInfo.getPointerInfo().getLocation();
|
||||
LOGGER.info("Starting coordinates: {}, {}", location.x, location.y);
|
||||
|
||||
int destX;
|
||||
int destY;
|
||||
|
||||
SingleStepMovementTask singleStepMovementTask;
|
||||
boolean click;
|
||||
|
||||
if (iteration == LOOPS_BEFORE_CLICK) {
|
||||
destX = RunnerUtil.SCREEN_WIDTH - 5;
|
||||
destY = 5;
|
||||
|
||||
try {
|
||||
singleStepMovementTask = new SingleStepMovementTask(destX, destY, true);
|
||||
} catch (AWTException e) {
|
||||
LOGGER.error(e);
|
||||
return;
|
||||
}
|
||||
click = true;
|
||||
|
||||
iteration = 0;
|
||||
} else {
|
||||
destX = random.nextInt(RunnerUtil.SCREEN_WIDTH);
|
||||
destY = random.nextInt(RunnerUtil.SCREEN_HEIGHT);
|
||||
|
||||
try {
|
||||
singleStepMovementTask = new SingleStepMovementTask(destX, destY, false);
|
||||
} catch (AWTException e) {
|
||||
LOGGER.error(e);
|
||||
return;
|
||||
}
|
||||
click = false;
|
||||
|
||||
iteration++;
|
||||
}
|
||||
|
||||
LOGGER.info("Destination coordinates: {}, {}", destX, destY);
|
||||
|
||||
Timer timer = new Timer();
|
||||
timer.schedule(singleStepMovementTask, 0L, 2L);
|
||||
|
||||
try {
|
||||
SingleStepMovementTask movement = new SingleStepMovementTask(scheduler, destX, destY, click);
|
||||
scheduler.execute(movement);
|
||||
} catch (AWTException ex) {
|
||||
LOGGER.error("Could not start movement task", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user