track user presence by time instead of poll count

This commit is contained in:
bea
2026-10-01 23:32:08 +02:00
parent 8b869fe4e9
commit f89af040b9
4 changed files with 198 additions and 104 deletions
@@ -4,6 +4,8 @@ import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException; import com.github.kwhat.jnativehook.NativeHookException;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
import wtf.beatrice.autosqueal.config.ConfigStore;
import wtf.beatrice.autosqueal.listener.KeyPressListener; import wtf.beatrice.autosqueal.listener.KeyPressListener;
import wtf.beatrice.autosqueal.ui.MainWindow; import wtf.beatrice.autosqueal.ui.MainWindow;
@@ -13,7 +15,7 @@ import javax.swing.UnsupportedLookAndFeelException;
public class Main { public class Main {
private static final Logger LOGGER = LogManager.getLogger(Main.class); private static final Logger LOGGER = LogManager.getLogger(Main.class);
private static final MainWindow mainWindow = new MainWindow(); private static MainWindow mainWindow;
/** Whether the hook and its listener are registered, so retries don't add duplicates. */ /** Whether the hook and its listener are registered, so retries don't add duplicates. */
private static boolean hookRegistered = false; private static boolean hookRegistered = false;
@@ -21,6 +23,9 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
LOGGER.info("Hello world!"); LOGGER.info("Hello world!");
AutoSquealConfig config = new ConfigStore().load();
mainWindow = new MainWindow(config);
// ⌘Q on macOS, the IDE stop button and signals all bypass // ⌘Q on macOS, the IDE stop button and signals all bypass
// windowClosing: make cleanup run on every exit path instead // windowClosing: make cleanup run on every exit path instead
Runtime.getRuntime().addShutdownHook(new Thread(mainWindow::cleanup, "autosqueal-shutdown")); Runtime.getRuntime().addShutdownHook(new Thread(mainWindow::cleanup, "autosqueal-shutdown"));
@@ -2,41 +2,50 @@ package wtf.beatrice.autosqueal.listener;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
import wtf.beatrice.autosqueal.controls.RobotMouseTracker; import wtf.beatrice.autosqueal.controls.RobotMouseTracker;
import java.awt.*; import java.awt.*;
import java.util.function.LongSupplier;
import java.util.function.Supplier; import java.util.function.Supplier;
/** /**
* Watches the cursor position and tracks whether the user is away: if the cursor * Watches the cursor position and tracks whether the user is away: if the
* stays still for long enough — ignoring the movements the app performs on its * user has not touched mouse or keyboard for long enough — ignoring the
* own — the user is considered away. Keyboard activity counts as presence, * movements the app performs on its own — the user is considered away.
* too: a user who is typing is using the machine, even if the mouse never moves. * Keyboard activity counts as presence, too: a user who is typing is using
* the machine, even if the mouse never moves.
*
* The away state is time-based: how long the user has been idle is compared
* against the threshold at every poll, so a threshold changed on the fly
* applies from the very next poll.
*/ */
public class CursorMoveListener implements Runnable { public class CursorMoveListener implements Runnable {
private static final Logger LOGGER = LogManager.getLogger(CursorMoveListener.class); private static final Logger LOGGER = LogManager.getLogger(CursorMoveListener.class);
/** Number of consecutive still polls after which the user is considered away. */
private static final int LOOPS_BEFORE_AWAY = 30;
private final RobotMouseTracker robotTracker; private final RobotMouseTracker robotTracker;
private final Supplier<Point> currentPosition; private final Supplier<Point> currentPosition;
private final AutoSquealConfig config;
private final LongSupplier clock;
private int lastSeenX; private int lastSeenX;
private int lastSeenY; private int lastSeenY;
private boolean firstPoll = true; private boolean firstPoll = true;
private int loops = 0; private long lastUserActivityTime;
private volatile boolean userAway = false; private volatile boolean userAway = false;
private volatile boolean keyboardActivity = false; private volatile boolean keyboardActivity = false;
public CursorMoveListener(RobotMouseTracker robotTracker) { public CursorMoveListener(RobotMouseTracker robotTracker, AutoSquealConfig config) {
this(robotTracker, () -> MouseInfo.getPointerInfo().getLocation()); this(robotTracker, config, () -> MouseInfo.getPointerInfo().getLocation(), System::currentTimeMillis);
} }
CursorMoveListener(RobotMouseTracker robotTracker, Supplier<Point> currentPosition) { CursorMoveListener(RobotMouseTracker robotTracker, AutoSquealConfig config,
Supplier<Point> currentPosition, LongSupplier clock) {
this.robotTracker = robotTracker; this.robotTracker = robotTracker;
this.config = config;
this.currentPosition = currentPosition; this.currentPosition = currentPosition;
this.clock = clock;
} }
/** /**
@@ -56,13 +65,15 @@ public class CursorMoveListener implements Runnable {
this.keyboardActivity = false; this.keyboardActivity = false;
Point location = currentPosition.get(); Point location = currentPosition.get();
long now = clock.getAsLong();
if (firstPoll) { if (firstPoll) {
// seed the comparison baseline, so that the first poll is not // seed the comparison baseline and the activity clock, so that
// mistaken for a movement // the first poll is neither a movement nor a full idle period
firstPoll = false; firstPoll = false;
lastSeenX = location.x; lastSeenX = location.x;
lastSeenY = location.y; lastSeenY = location.y;
lastUserActivityTime = now;
return; return;
} }
@@ -70,18 +81,16 @@ public class CursorMoveListener implements Runnable {
boolean userMoved = keyboardActivity || (positionChanged && !isAppMovement(location)); boolean userMoved = keyboardActivity || (positionChanged && !isAppMovement(location));
if (userMoved) { if (userMoved) {
if (userAway) { lastUserActivityTime = now;
LOGGER.info("User is no longer away!");
}
loops = 0;
} else if (loops < LOOPS_BEFORE_AWAY) {
loops++;
if (loops == LOOPS_BEFORE_AWAY) {
LOGGER.info("User is away!");
}
} }
userAway = loops >= LOOPS_BEFORE_AWAY; boolean away = now - lastUserActivityTime >= config.getAwayThresholdSeconds() * 1000L;
if (away && !userAway) {
LOGGER.info("User is away!");
} else if (!away && userAway) {
LOGGER.info("User is no longer away!");
}
userAway = away;
lastSeenX = location.x; lastSeenX = location.x;
lastSeenY = location.y; lastSeenY = location.y;
@@ -4,6 +4,7 @@ import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import wtf.beatrice.autosqueal.Main; import wtf.beatrice.autosqueal.Main;
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
import wtf.beatrice.autosqueal.controls.CursorMover; import wtf.beatrice.autosqueal.controls.CursorMover;
import wtf.beatrice.autosqueal.controls.RobotMouseTracker; import wtf.beatrice.autosqueal.controls.RobotMouseTracker;
import wtf.beatrice.autosqueal.listener.CursorMoveListener; import wtf.beatrice.autosqueal.listener.CursorMoveListener;
@@ -35,10 +36,22 @@ public class MainWindow
private final JFrame frame = new JFrame(); private final JFrame frame = new JFrame();
private JButton toggleButton; private JButton toggleButton;
private final AutoSquealConfig config;
// the away detection tracks the user, not the automation: it lives from // the away detection tracks the user, not the automation: it lives from
// the window's creation and is polled only while the automation runs // the window's creation and is polled only while the automation runs
private final RobotMouseTracker robotTracker = new RobotMouseTracker(); private final RobotMouseTracker robotTracker;
private final CursorMoveListener awayDetector = new CursorMoveListener(robotTracker); private final CursorMoveListener awayDetector;
/**
* Creates the window around the given settings: the components read
* them live, so a change applies without a restart.
*/
public MainWindow(AutoSquealConfig config) {
this.config = config;
this.robotTracker = new RobotMouseTracker();
this.awayDetector = new CursorMoveListener(robotTracker, config);
}
private ScheduledExecutorService periodicScheduler; private ScheduledExecutorService periodicScheduler;
private ExecutorService movementExecutor; private ExecutorService movementExecutor;
@@ -1,12 +1,11 @@
package wtf.beatrice.autosqueal.listener; package wtf.beatrice.autosqueal.listener;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import wtf.beatrice.autosqueal.config.AutoSquealConfig;
import wtf.beatrice.autosqueal.controls.RobotMouseTracker; import wtf.beatrice.autosqueal.controls.RobotMouseTracker;
import java.awt.*; import java.awt.*;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue; import java.util.Queue;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@@ -14,73 +13,93 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class CursorMoveListenerTest class CursorMoveListenerTest
{ {
private static final int LOOPS_BEFORE_AWAY = 30; private static final int AWAY_THRESHOLD_SECONDS = 30;
private final RobotMouseTracker robotTracker = new RobotMouseTracker(); private final RobotMouseTracker robotTracker = new RobotMouseTracker();
/** The fake clock the listener polls with, in milliseconds. */
private long now = 1_000_000L;
/** Builds a listener that reads scripted cursor positions, one per poll. */ /** Builds a listener that reads scripted cursor positions, one per poll. */
private CursorMoveListener scriptedListener(Point... positions) { private CursorMoveListener scriptedListener(Point... positions) {
Queue<Point> positionQueue = new ArrayDeque<>(java.util.List.of(positions)); Queue<Point> positionQueue = new ArrayDeque<>(java.util.List.of(positions));
return new CursorMoveListener(robotTracker, positionQueue::remove); return new CursorMoveListener(robotTracker, new AutoSquealConfig(),
positionQueue::remove, () -> now);
} }
private static Point at(int x, int y) { private static Point at(int x, int y) {
return new Point(x, y); return new Point(x, y);
} }
/** Polls, pretending that the given seconds have passed since the previous poll. */
private void poll(CursorMoveListener listener, int secondsSincePreviousPoll) {
now += secondsSincePreviousPoll * 1000L;
listener.run();
}
@Test @Test
void userIsAwayAfterThirtyStillSeconds() { void userIsAwayAfterThirtyStillSeconds() {
Point[] still = new Point[LOOPS_BEFORE_AWAY + 2]; Point[] still = new Point[AWAY_THRESHOLD_SECONDS + 3];
for (int i = 0; i < still.length; i++) { for (int i = 0; i < still.length; i++) {
still[i] = at(100, 100); still[i] = at(100, 100);
} }
CursorMoveListener listener = scriptedListener(still); CursorMoveListener listener = scriptedListener(still);
// the first poll seeds the baseline and does not count // the first poll seeds the baseline and does not count
for (int i = 0; i < LOOPS_BEFORE_AWAY; i++) { poll(listener, 1);
listener.run();
// 29 still seconds: the user is still considered present
for (int i = 0; i < AWAY_THRESHOLD_SECONDS - 1; i++) {
poll(listener, 1);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
} }
listener.run(); // the 30th still second: the user is away
poll(listener, 1);
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
} }
@Test @Test
void firstPollDoesNotCountAsMovement() { void firstPollDoesNotCountAsMovementOrIdleTime() {
// the very first position read must seed the baseline: starting anywhere // the very first position read must seed the baseline and the
// else would have counted as a user movement and reset the away timer // activity clock: starting anywhere else would have counted the
// first poll as a movement, or as a full idle period
CursorMoveListener listener = scriptedListener(at(500, 300), at(500, 300)); CursorMoveListener listener = scriptedListener(at(500, 300), at(500, 300));
listener.run(); poll(listener, 5);
listener.run(); poll(listener, 10);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
// still at (500, 300), so the baseline was seeded correctly // still at (500, 300), and only 10 idle seconds: the seeding poll
// consumed the first 5 seconds
} }
@Test @Test
void userMovementResetsTheAwayTimer() { void userMovementResetsTheAwayTimer() {
List<Point> positions = new ArrayList<>(); Queue<Point> positions = new ArrayDeque<>();
positions.add(at(100, 100)); // baseline positions.add(at(100, 100)); // baseline
for (int i = 0; i < 5; i++) positions.add(at(100, 100)); // 5 still polls for (int i = 0; i < 3; i++) positions.add(at(100, 100)); // 3 still seconds
positions.add(at(140, 100)); // the user moves the mouse positions.add(at(140, 100)); // the user moves the mouse
for (int i = 0; i < LOOPS_BEFORE_AWAY; i++) positions.add(at(140, 100)); // then it stays put for (int i = 0; i < AWAY_THRESHOLD_SECONDS; i++) positions.add(at(140, 100)); // then it stays put
CursorMoveListener listener = new CursorMoveListener(robotTracker, new AutoSquealConfig(),
positions::remove, () -> now);
CursorMoveListener listener = scriptedListener(positions.toArray(Point[]::new)); poll(listener, 1); // seed the baseline
for (int i = 0; i < 3; i++) {
listener.run(); // seed the baseline poll(listener, 1);
for (int i = 0; i < 5; i++) listener.run(); // loops = 5 }
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
listener.run(); // the user moved: the away timer resets poll(listener, 1); // the user moved: the away timer resets
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
// ...and it still takes 30 more still polls to be away again // ...and it still takes 30 more still seconds to be away again
for (int i = 0; i < LOOPS_BEFORE_AWAY - 1; i++) listener.run(); // loops = 29 for (int i = 0; i < AWAY_THRESHOLD_SECONDS - 1; i++) {
poll(listener, 1);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
}
listener.run(); // loops = 30 poll(listener, 1);
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
} }
@@ -92,19 +111,21 @@ class CursorMoveListenerTest
// always think the user was present // always think the user was present
// every poll finds the cursor where the app last left it // every poll finds the cursor where the app last left it
List<Point> positions = new ArrayList<>(); Queue<Point> appPositions = new ArrayDeque<>();
positions.add(at(100, 100)); // baseline appPositions.add(at(100, 100));
for (int i = 1; i <= 40; i++) positions.add(at(100 + i, 100)); for (int i = 1; i <= 40; i++) {
appPositions.add(at(100 + i, 100));
}
CursorMoveListener listener = new CursorMoveListener(robotTracker, new AutoSquealConfig(),
appPositions::remove, () -> now);
CursorMoveListener listener = scriptedListener(positions.toArray(Point[]::new)); poll(listener, 1); // seed the baseline
listener.run(); // seed the baseline
for (int i = 1; i <= 40; i++) { for (int i = 1; i <= 40; i++) {
// the app just finished a movement, leaving the cursor exactly // the app just finished a movement, leaving the cursor exactly
// where this poll will find it // where this poll will find it
robotTracker.moveEnded(100 + i, 100); robotTracker.moveEnded(100 + i, 100);
listener.run(); poll(listener, 1);
} }
// every position change was the app's own doing: the user is away // every position change was the app's own doing: the user is away
@@ -115,16 +136,17 @@ class CursorMoveListenerTest
void ongoingAppMovementsDoNotKeepTheUserPresent() { void ongoingAppMovementsDoNotKeepTheUserPresent() {
// same as above, but the polls happen while the app is mid-movement, // same as above, but the polls happen while the app is mid-movement,
// with the cursor transiently at intermediate positions // with the cursor transiently at intermediate positions
Point[] positions = new Point[41]; Queue<Point> positions = new ArrayDeque<>();
positions[0] = at(100, 100); positions.add(at(100, 100));
for (int i = 1; i < positions.length; i++) { for (int i = 1; i <= 40; i++) {
positions[i] = at(100 + (i / 2), 100); positions.add(at(100 + (i / 2), 100));
} }
CursorMoveListener listener = scriptedListener(positions); CursorMoveListener listener = new CursorMoveListener(robotTracker, new AutoSquealConfig(),
positions::remove, () -> now);
robotTracker.moveStarted(); robotTracker.moveStarted();
for (int i = 1; i < positions.length; i++) { for (int i = 0; i <= 40; i++) {
listener.run(); poll(listener, 1);
} }
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
@@ -132,48 +154,55 @@ class CursorMoveListenerTest
@Test @Test
void keyboardActivityResetsTheAwayTimer() { void keyboardActivityResetsTheAwayTimer() {
List<Point> positions = new ArrayList<>(); Queue<Point> positions = new ArrayDeque<>();
positions.add(at(100, 100)); // baseline positions.add(at(100, 100)); // baseline
for (int i = 0; i < 29; i++) positions.add(at(100, 100)); // 29 still polls for (int i = 0; i < 60; i++) {
positions.add(at(100, 100)); // the user presses a key positions.add(at(100, 100)); // the mouse never moves
for (int i = 0; i < LOOPS_BEFORE_AWAY; i++) positions.add(at(100, 100)); // then the mouse stays put }
CursorMoveListener listener = new CursorMoveListener(robotTracker, new AutoSquealConfig(),
positions::remove, () -> now);
CursorMoveListener listener = scriptedListener(positions.toArray(Point[]::new)); poll(listener, 1); // seed the baseline
for (int i = 0; i < AWAY_THRESHOLD_SECONDS - 1; i++) {
listener.run(); // seed the baseline poll(listener, 1);
for (int i = 0; i < 29; i++) listener.run(); // loops = 29 }
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
// the user is typing without moving the mouse: the timer resets // the user is typing without moving the mouse: the timer resets
listener.reportKeyboardActivity(); listener.reportKeyboardActivity();
listener.run(); poll(listener, 1);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
// ...and it still takes 30 still polls to be away again // ...and it still takes 30 still seconds to be away again
for (int i = 0; i < LOOPS_BEFORE_AWAY - 1; i++) listener.run(); // loops = 29 for (int i = 0; i < AWAY_THRESHOLD_SECONDS - 1; i++) {
poll(listener, 1);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
}
listener.run(); // loops = 30 poll(listener, 1);
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
} }
@Test @Test
void keyboardActivityBringsTheUserBackWhileAway() { void keyboardActivityBringsTheUserBackWhileAway() {
Point[] positions = new Point[LOOPS_BEFORE_AWAY + 3]; Queue<Point> positions = new ArrayDeque<>();
for (int i = 0; i < positions.length; i++) { positions.add(at(100, 100));
positions[i] = at(100, 100); for (int i = 0; i < AWAY_THRESHOLD_SECONDS + 3; i++) {
positions.add(at(100, 100));
} }
CursorMoveListener listener = scriptedListener(positions); CursorMoveListener listener = new CursorMoveListener(robotTracker, new AutoSquealConfig(),
positions::remove, () -> now);
for (int i = 0; i <= LOOPS_BEFORE_AWAY; i++) { // baseline + 30 still polls poll(listener, 1); // seed the baseline
listener.run(); for (int i = 0; i < AWAY_THRESHOLD_SECONDS; i++) {
poll(listener, 1);
} }
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
// the user comes back at the keyboard: the away state must end, // the user comes back at the keyboard: the away state must end,
// even though the mouse never moved // even though the mouse never moved
listener.reportKeyboardActivity(); listener.reportKeyboardActivity();
listener.run(); poll(listener, 1);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
} }
@@ -181,34 +210,72 @@ class CursorMoveListenerTest
void userComingBackIsDetectedWhileTheAppIsWiggling() { void userComingBackIsDetectedWhileTheAppIsWiggling() {
// the app keeps wiggling while the user is away; when the user comes // the app keeps wiggling while the user is away; when the user comes
// back and moves the mouse, the away state must end // back and moves the mouse, the away state must end
Queue<Point> positions = new ArrayDeque<>();
// polls: baseline, 30 still polls, two app wiggles, then the user's move positions.add(at(100, 100));
Point[] positions = new Point[LOOPS_BEFORE_AWAY + 4]; for (int i = 0; i < AWAY_THRESHOLD_SECONDS; i++) {
for (int i = 0; i <= LOOPS_BEFORE_AWAY; i++) { positions.add(at(100, 100));
positions[i] = at(100, 100);
} }
positions[LOOPS_BEFORE_AWAY + 1] = at(150, 100); positions.add(at(150, 100)); // app wiggle
positions[LOOPS_BEFORE_AWAY + 2] = at(200, 100); positions.add(at(200, 100)); // app wiggle
positions[LOOPS_BEFORE_AWAY + 3] = at(333, 100); positions.add(at(333, 100)); // the user's move
CursorMoveListener listener = scriptedListener(positions); CursorMoveListener listener = new CursorMoveListener(robotTracker, new AutoSquealConfig(),
positions::remove, () -> now);
// baseline + 30 still polls: the user goes away poll(listener, 1); // seed the baseline
for (int i = 0; i <= LOOPS_BEFORE_AWAY; i++) { for (int i = 0; i < AWAY_THRESHOLD_SECONDS; i++) {
listener.run(); poll(listener, 1);
} }
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
// the app wiggles: each movement is discounted, the user stays "away" // the app wiggles: each movement is discounted, the user stays "away"
robotTracker.moveEnded(150, 100); robotTracker.moveEnded(150, 100);
listener.run(); poll(listener, 1);
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
robotTracker.moveEnded(200, 100); robotTracker.moveEnded(200, 100);
listener.run(); poll(listener, 1);
assertTrue(listener.isUserAway()); assertTrue(listener.isUserAway());
// the user comes back and grabs the mouse // the user comes back and grabs the mouse
listener.run(); poll(listener, 1);
assertFalse(listener.isUserAway());
}
@Test
void thresholdChangesApplyFromTheNextPoll() {
// the threshold is read live: a change applies without a restart
AutoSquealConfig config = new AutoSquealConfig();
config.setAwayThresholdSeconds(10);
Queue<Point> positions = new ArrayDeque<>();
for (int i = 0; i < 20; i++) {
positions.add(at(100, 100));
}
CursorMoveListener listener = new CursorMoveListener(robotTracker, config,
positions::remove, () -> now);
poll(listener, 1); // seed the baseline
// 9 still seconds: not away with a 10 seconds threshold
for (int i = 0; i < 9; i++) {
poll(listener, 1);
assertFalse(listener.isUserAway());
}
// 10th second: away
poll(listener, 1);
assertTrue(listener.isUserAway());
// the user tightens the threshold to 5: the next poll re-evaluates
// and the user is still away (already idle for more than 5 seconds)
config.setAwayThresholdSeconds(5);
poll(listener, 1);
assertTrue(listener.isUserAway());
// the user loosens the threshold to 600: the same idle time is no
// longer enough, so the user is present again — and back to away
// only after 600 seconds of stillness
config.setAwayThresholdSeconds(600);
poll(listener, 1);
assertFalse(listener.isUserAway()); assertFalse(listener.isUserAway());
} }
} }