diff --git a/API/src/main/java/org/sikuli/script/Region.java b/API/src/main/java/org/sikuli/script/Region.java index 3f4ecfb6e..7262cbb4c 100644 --- a/API/src/main/java/org/sikuli/script/Region.java +++ b/API/src/main/java/org/sikuli/script/Region.java @@ -80,6 +80,7 @@ private static void log(int level, String message, Object... args) { * Flag, if an observer is running on this region {@link Settings} */ private boolean observing = false; + private boolean observingInBackground = false; private float observeScanRate = Settings.ObserveScanRate; private int repeatWaitTime = Settings.RepeatWaitTime; /** @@ -3517,8 +3518,10 @@ private boolean observeDo(double secs) { return false; } if (observing) { - Debug.error("Region: observe: already running for this region. Only one allowed!"); - return false; + if (!observingInBackground) { + Debug.error("Region: observe: already running for this region. Only one allowed!"); + return false; + } } log(lvl, "observe: starting in " + this.toStringShort() + " for " + secs + " seconds"); int MaxTimePerScan = (int) (1000.0 / observeScanRate); @@ -3563,7 +3566,7 @@ private boolean observeDo(double secs) { } /** - * start an observer in this region for the given time that runs in background for details about the observe event + * start an observer in this region for the given time that runs in background - for details about the observe event * handler: {@link ObserverCallBack} for details about APPEAR/VANISH/CHANGE events: {@link ObserveEvent} * * @param secs time in seconds the observer should run @@ -3574,12 +3577,24 @@ public boolean observeInBackground(double secs) { Debug.error("Region: observeInBackground: already running for this region. Only one allowed!"); return false; } + observing = true; + observingInBackground = true; Thread observeThread = new Thread(new ObserverThread(secs)); observeThread.start(); log(lvl, "observeInBackground now running"); return true; } + /** + * start an observer in this region that runs in background forever - for details about the observe event + * handler: {@link ObserverCallBack} for details about APPEAR/VANISH/CHANGE events: {@link ObserveEvent} + * + * @return false if not possible, true otherwise + */ + public boolean observeInBackground() { + return observeInBackground(Double.MAX_VALUE); + } + private class ObserverThread implements Runnable { private double time; @@ -3600,6 +3615,7 @@ public void run() { public void stopObserver() { log(lvl, "observe: request to stop observer for " + this.toStringShort()); observing = false; + observingInBackground = false; } /** diff --git a/API/src/main/java/org/sikuli/script/Sikulix.java b/API/src/main/java/org/sikuli/script/Sikulix.java index 42e63662c..3b39ac19e 100644 --- a/API/src/main/java/org/sikuli/script/Sikulix.java +++ b/API/src/main/java/org/sikuli/script/Sikulix.java @@ -130,7 +130,28 @@ public static void main(String[] args) throws FindFailed { boolean playing = false; if (args.length > 0 && "play".equals(args[0])) { playing = true; -//-------- playground +//-------- playground Observe +// Region reg = new Region(0,0,80,80); +// reg.highlight(2); +// reg.onChange(new ObserverCallBack(){ +// @Override +// public void changed(ObserveEvent evt) { +// if (evt.getCount() > 3) { +// p("in handler: %d - stopping", evt.getCount()); +// evt.stopObserver(); +// } else { +// p("in handler: %d", evt.getCount()); +// } +// } +// }); +// reg.observeInBackground(); +// int n = 0; +// while (reg.isObserving()) { +// p("%d - observing", n); +// reg.wait(1.0); +// n++; +// } +//-------- playground VNC // VNCScreen vs = vncStart("192.168.2.63", 5900, "vnc", 0, 0); // p("%s", vs); // vs.wait(3.0);