From 35c705222bbd6f81b3957cd6e817d120899b6ea5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Tue, 6 Aug 2024 16:19:04 +0200 Subject: [PATCH] implement initial support for inactivity monitor --- src/main/java/wtf/beatrice/autosqueal/Main.java | 7 +++++++ .../wtf/beatrice/autosqueal/controls/CursorMover.java | 9 +++++++-- .../autosqueal/controls/SingleStepMovementTask.java | 2 +- .../autosqueal/listener/CursorMoveListener.java | 9 +++++++-- .../java/wtf/beatrice/autosqueal/ui/MainWindow.java | 11 ++++++++--- .../java/wtf/beatrice/autosqueal/util/RunnerUtil.java | 6 ++++++ 6 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/main/java/wtf/beatrice/autosqueal/Main.java b/src/main/java/wtf/beatrice/autosqueal/Main.java index 752ca3f..a3cbcab 100644 --- a/src/main/java/wtf/beatrice/autosqueal/Main.java +++ b/src/main/java/wtf/beatrice/autosqueal/Main.java @@ -4,9 +4,12 @@ import com.github.kwhat.jnativehook.GlobalScreen; import com.github.kwhat.jnativehook.NativeHookException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import wtf.beatrice.autosqueal.listener.CursorMoveListener; import wtf.beatrice.autosqueal.listener.KeyPressListener; import wtf.beatrice.autosqueal.ui.MainWindow; +import java.util.Timer; + public class Main { private static final Logger LOGGER = LogManager.getLogger(Main.class); @@ -17,6 +20,10 @@ public class Main { registerJNativeHook(); mainWindow.init(); + + Timer timerRunner = new Timer(); + CursorMoveListener cursorMoveListener = new CursorMoveListener(); + timerRunner.schedule(cursorMoveListener, 0L, 1000L); } private static void registerJNativeHook() { diff --git a/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java b/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java index 5723d31..1f85d06 100644 --- a/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java +++ b/src/main/java/wtf/beatrice/autosqueal/controls/CursorMover.java @@ -15,6 +15,7 @@ public class CursorMover extends TimerTask private static final Logger LOGGER = LogManager.getLogger(CursorMover.class); private final Random random; + private SingleStepMovementTask singleStepMovementTask; private static final int LOOPS_BEFORE_CLICK = 5; @@ -22,6 +23,7 @@ public class CursorMover extends TimerTask public CursorMover() { random = new SecureRandom(); + singleStepMovementTask = null; } @Override @@ -35,8 +37,6 @@ public class CursorMover extends TimerTask int destX; int destY; - SingleStepMovementTask singleStepMovementTask; - if (iteration == LOOPS_BEFORE_CLICK) { destX = RunnerUtil.SCREEN_WIDTH - 5; destY = 5; @@ -67,6 +67,11 @@ public class CursorMover extends TimerTask Timer timer = new Timer(); timer.schedule(singleStepMovementTask, 0L, 2L); + } + public boolean isRunning() { + if (singleStepMovementTask == null) return false; + + return singleStepMovementTask.isRunning(); } } diff --git a/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java b/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java index 96de446..1c80716 100644 --- a/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java +++ b/src/main/java/wtf/beatrice/autosqueal/controls/SingleStepMovementTask.java @@ -20,7 +20,7 @@ public class SingleStepMovementTask extends TimerTask { float stepX = 1; float stepY = 1; - boolean isRunning = true; + private boolean isRunning = true; boolean click; diff --git a/src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java b/src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java index fde5f4a..9ce3839 100644 --- a/src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java +++ b/src/main/java/wtf/beatrice/autosqueal/listener/CursorMoveListener.java @@ -2,6 +2,8 @@ package wtf.beatrice.autosqueal.listener; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import wtf.beatrice.autosqueal.Main; +import wtf.beatrice.autosqueal.util.RunnerUtil; import java.awt.*; import java.util.TimerTask; @@ -25,6 +27,8 @@ public class CursorMoveListener extends TimerTask { @Override public void run() { + if(RunnerUtil.isSquealing()) return; + int newX = MouseInfo.getPointerInfo().getLocation().x; int newY = MouseInfo.getPointerInfo().getLocation().y; @@ -33,14 +37,15 @@ public class CursorMoveListener extends TimerTask { loops = 0; LOGGER.info("User is no longer away!"); } else { - if (loops < 30) { + if (loops < 10) { loops++; } else { LOGGER.info("User is away!"); + Main.getMainWindow().toggleRunning(); } } - isUserAway = loops >= 30; + isUserAway = loops >= 10; oldX = newX; oldY = newY; diff --git a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java index db808e3..427ada3 100644 --- a/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java +++ b/src/main/java/wtf/beatrice/autosqueal/ui/MainWindow.java @@ -50,7 +50,6 @@ public class MainWindow timestampLabel.setBounds(new Rectangle(bordersPx, bordersPx + rescaleHeight + bordersPx, 100, 30)); frame.add(timestampLabel); - frame.setLayout(null); frame.setVisible(true); } @@ -106,8 +105,6 @@ public class MainWindow if (cursorMover == null) { timerRunner = new Timer(); - CursorMoveListener cursorMoveListener = new CursorMoveListener(); - timerRunner.schedule(cursorMoveListener, 0L, 1000L); cursorMover = new CursorMover(); timerRunner.schedule(cursorMover, 1000L, RunnerUtil.SECONDS_BETWEEN_MOVES * 1000L); @@ -134,4 +131,12 @@ public class MainWindow } + @Deprecated + public boolean isRunning() { + if (cursorMover == null) + return false; + + return cursorMover.isRunning(); + } + } diff --git a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java index a0046d9..d5938f2 100644 --- a/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java +++ b/src/main/java/wtf/beatrice/autosqueal/util/RunnerUtil.java @@ -1,5 +1,7 @@ package wtf.beatrice.autosqueal.util; +import wtf.beatrice.autosqueal.Main; + import java.awt.*; public class RunnerUtil { @@ -13,4 +15,8 @@ public class RunnerUtil { public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height; public static final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width; + public static boolean isSquealing() { + return Main.getMainWindow().isRunning(); + } + }