import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import org.pushingpixels.substance.api.*;
import org.pushingpixels.substance.api.skin.*;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.*;
import java.text.SimpleDateFormat;
import java.awt.datatransfer.StringSelection;
import org.jnativehook.GlobalScreen;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyAdapter;
class main {
static Pt moveDist = pt(400, 300);
static int minWidth = 300;
public static void main(final String[] args) throws Exception { substance(); autoUpdate();
mouseMover().endAfterInterference = false;
while (licensed()) {
if (mouseMover().blocked()) {
consoleStatus("BLOCKED");
sleepSeconds(1);
continue;
} else
consoleStatus("");
long time = sysNow();
BufferedImage img = shootScreen2();
Rect r = ocr_findTerminalBackground(new RGBImage(img));
writeBesideMemoryView(elapsedMS(time) + " ms");
if (rectWidth(r) < minWidth) continue;
Rect title = shrinkRect(40, 4, new Rect(r.x, r.y-16, r.w, 16));
//containsColor(clipBufferedImage(img, title), 0x454545)
boolean isTitle = containsColor(clipBufferedImage(img, new Rect(centerOfRect(title), 10, 10)), 0x454545);
if (isTitle) {
//print(title);
Pt p = centerOfRect(title);
Pt p2 = new Pt(p.x+random(-moveDist.x, moveDist.x), p.y+random(-moveDist.y, moveDist.y));
Rect usable = shrinkRect(300, 20, new Rect(usableWindowArea()));
usable.h = max(40, usable.h-300);
print("Usable: " + usable + ", p2: " + p2);
p2 = limitPtToRect(p2, usable);
if (eq(p, p2)) continue;
if (robotDragMouse(toPoint(p), 100, toPoint(p2))) {
infoBox("Moving the terminal around 'cause I'm bored.");
print("Dragging " + p + " to " + p2);
sleep(3000);
}
}
}
}
static void consoleStatus(String status) {
consoleTitleStatus(status);
}
static void autoUpdate(double interval) {
autoRestart(interval);
}
static void autoUpdate() {
autoRestart();
}
static void sleepSeconds(double s) {
if (s > 0) sleep(round(s*1000));
}
static JLabel writeBesideMemoryView_label;
static void writeBesideMemoryView(final String text) {
{ swing(new Runnable() { public void run() { try {
if (writeBesideMemoryView_label == null)
putBesideMemoryView(writeBesideMemoryView_label = jlabel());
String s = trim(text);
setText(writeBesideMemoryView_label, empty(s) ? "" : " " + s + " | ");
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (writeBesideMemoryView_label == null)\r\n putBesideMemoryView(writeBesi..."; }}); }
}
static Pt pt(int x, int y) {
return new Pt(x, y);
}
static int rectWidth(Rect r) {
return r == null ? 0 : r.w;
}
static JWindow infoBox(String text) {
return infoMessage(text);
}
static JWindow infoBox(String text, double seconds) {
return infoMessage(text, seconds);
}
static JWindow infoBox(Throwable e) {
return infoMessage(e);
}
static Point toPoint(Pt p) {
return p == null ? null : new Point(p.x, p.y);
}
static MouseMover mouseMover_instance;
static MouseMover mouseMover() {
if (mouseMover_instance == null) { swing(new Runnable() { public void run() { try {
if (mouseMover_instance == null) {
mouseMover_instance = new MouseMover();
mouseMover_instance.enable();
}
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "if (mouseMover_instance == null) {\r\n mouseMover_instance = new MouseMove..."; }}); }
return mouseMover_instance;
}
static void cleanMeUp_mouseMover() {
cleanUp(mouseMover_instance);
}
static Pt limitPtToRect(Pt p, Rect r) {
if (p == null) return null;
if (r == null) return p;
return new Pt(
max(r.x, min(r.x2(), p.x)),
max(r.y, min(r.y2(), p.y)));
}
static volatile boolean sleep_noSleep;
static void sleep(long ms) {
ping();
if (ms < 0) return;
// allow spin locks
if (isAWTThread() && ms > 100) throw fail("Should not sleep on AWT thread");
try {
Thread.sleep(ms);
} catch (Exception e) { throw new RuntimeException(e); }
}
static void sleep() { try {
if (sleep_noSleep) throw fail("nosleep");
print("Sleeping.");
sleepQuietly();
} catch (Exception __e) { throw rethrow(__e); } }
static Random random_random = new Random();
static int random(int n) {
return n <= 0 ? 0 : random_random.nextInt(n);
}
static double random(double max) {
return random()*max;
}
static double random() {
return random_random.nextInt(100001)/100000.0;
}
static double random(double min, double max) {
return min+random()*(max-min);
}
// min <= value < max
static int random(int min, int max) {
return min+random(max-min);
}
static A random(List l) {
return oneOf(l);
}
static A random(Collection c) {
if (c instanceof List) return random((List) c);
int i = random(l(c));
return collectionGet(c, i);
}
static long sysNow() {
return System.nanoTime()/1000000;
}
static boolean containsColor(BufferedImage img, int color) {
if (img == null) return false;
color |= 0xFF000000; // alpha
int w = img.getWidth(), h = img.getHeight();
for (int x = 0; x < w; x++)
for (int y = 0; y < h; y++) {
int rgba = img.getRGB(x, y);
if (rgba == color) return true;
}
return false;
}
static Rectangle usableWindowArea() {
return maxWindowBounds();
}
static BufferedImage clipBufferedImage(BufferedImage src, Rectangle clip) {
return clipBufferedImage(src, new Rect(clip));
}
static BufferedImage clipBufferedImage(BufferedImage src, Rect r) {
if (src == null) return null;
// fixClipRect
r = intersectRects(r, new Rect(0, 0, src.getWidth(), src.getHeight()));
if (rectEmpty(r)) return null; // can't make zero-sized BufferedImage
return src.getSubimage(r.x, r.y, r.w, r.h);
}
static BufferedImage clipBufferedImage(BufferedImage src, int x, int y, int w, int h) {
return clipBufferedImage(src, new Rect(x, y, w, h));
}
static volatile boolean licensed_yes = true;
static boolean licensed() {
ping();
return licensed_yes;
}
static void licensed_off() {
licensed_yes = false;
}
static int max(int a, int b) { return Math.max(a, b); }
static int max(int a, int b, int c) { return max(max(a, b), c); }
static long max(int a, long b) { return Math.max((long) a, b); }
static long max(long a, long b) { return Math.max(a, b); }
static double max(int a, double b) { return Math.max((double) a, b); }
static float max(float a, float b) { return Math.max(a, b); }
static double max(double a, double b) { return Math.max(a, b); }
static int max(Collection c) {
int x = Integer.MIN_VALUE;
for (int i : c) x = max(x, i);
return x;
}
static double max(double[] c) {
if (c.length == 0) return Double.MIN_VALUE;
double x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static float max(float[] c) {
if (c.length == 0) return Float.MAX_VALUE;
float x = c[0];
for (int i = 1; i < c.length; i++) x = Math.max(x, c[i]);
return x;
}
static byte max(byte[] c) {
byte x = -128;
for (byte d : c) if (d > x) x = d;
return x;
}
static short max(short[] c) {
short x = -0x8000;
for (short d : c) if (d > x) x = d;
return x;
}
static int max(int[] c) {
int x = Integer.MIN_VALUE;
for (int d : c) if (d > x) x = d;
return x;
}
static void substance() {
substanceLAF();
}
static void substance(String skinName) {
substanceLAF(skinName);
}
static Map _registerThread_threads = newWeakHashMap();
static Thread _registerThread(Thread t) {
_registerThread_threads.put(t, true);
return t;
}
static void _registerThread() { _registerThread(Thread.currentThread()); }
static boolean shootScreen_useScrot = true; // Use program "scrot" to make full-screen screenshots (on Linux); prevents the bug https://bugs.openjdk.java.net/browse/JDK-7168628
static BufferedImage shootScreen2() {
return shootScreen2(screenRectangle());
}
static BufferedImage shootScreen2(Rectangle area) { try {
ping();
if (shootScreen_useScrot && eq(area, screenRectangle())) {
if (!isOnPATH("scrot")) shootScreen2_fallback();
if (shootScreen_useScrot) {
String filename = "screenshot-" + randomID(12) + ".png";
File f = linux_fileInRamDisk(filename);
if (f == null) f = prepareProgramFile(filename);
try {
String cmd = "scrot " + bashQuote(f);
String out = backtick(cmd);
if (f.exists())
return loadPNG(f);
shootScreen2_fallback();
} catch (Throwable e) {
print(e);
shootScreen2_fallback();
} finally {
f.delete();
}
}
}
return new Robot().createScreenCapture(area);
} catch (Exception __e) { throw rethrow(__e); } }
static BufferedImage shootScreen2(Rect area) {
return shootScreen2(area.getRectangle());
}
static BufferedImage shootScreen2(int x, int y, int w, int h) {
return shootScreen2(new Rectangle(x, y, w, h));
}
// internal
static void shootScreen2_fallback() {
if (shootScreen_useScrot) {
if (isLinux()) print("Scrot failed. Reverting to internal screenshots.");
shootScreen_useScrot = false;
}
}
static boolean eq(Object a, Object b) {
return a == null ? b == null : a == b || a.equals(b);
}
static boolean robotDragMouse(final Point a, int delay, final Point b) { try {
mouseMover().enable();
if (mouseMover().blocked()) return false;
mouseMover().moveMouse(new Pt(a.x, a.y));
final int button = InputEvent.BUTTON1_DOWN_MASK;
final Robot robot = new Robot();
mouseMover().pressButton();
swingLater(delay, new Runnable() { public void run() { try {
mouseMover().moveMouse(new Pt(b.x, b.y));
mouseMover().releaseButton();
} catch (Exception __e) { throw rethrow(__e); } } public String toString() { return "mouseMover().moveMouse(Pt(b.x, b.y));\r\n mouseMover().releaseButton();"; }});
return true;
} catch (Exception __e) { throw rethrow(__e); } }
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile StringBuffer print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
//static int print_maxLineLength = 0; // 0 = unset
static boolean print_silent; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal