diff --git a/core/.classpath b/core/.classpath
index 4328dab5..e7a46736 100644
--- a/core/.classpath
+++ b/core/.classpath
@@ -18,8 +18,9 @@
-
+
+
diff --git a/core/src/main/java/it/cavallium/warppi/WarpPI.java b/core/src/main/java/it/cavallium/warppi/WarpPI.java
index 206f8200..fc3424b6 100644
--- a/core/src/main/java/it/cavallium/warppi/WarpPI.java
+++ b/core/src/main/java/it/cavallium/warppi/WarpPI.java
@@ -1,6 +1,8 @@
package it.cavallium.warppi;
import java.io.IOException;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.Future;
import it.cavallium.warppi.Platform.ConsoleUtils;
import it.cavallium.warppi.boot.StartupArguments;
@@ -15,47 +17,45 @@ import it.cavallium.warppi.flow.BehaviorSubject;
import it.cavallium.warppi.flow.Observable;
import it.cavallium.warppi.gui.DisplayManager;
import it.cavallium.warppi.gui.HUD;
-import it.cavallium.warppi.gui.HardwareDisplay;
import it.cavallium.warppi.gui.screens.Screen;
import it.cavallium.warppi.util.ClassUtils;
+import it.cavallium.warppi.util.RunnableWithException;
public class WarpPI {
public static final WarpPI INSTANCE = new WarpPI();
private static Platform platform;
private static boolean running = false;
- private static BehaviorSubject loadPhase = BehaviorSubject.create();
private final BehaviorSubject loaded = BehaviorSubject.create(false);
private Device device;
private WarpPI() {}
- /**
- * Start an instance of the calculator.
- *
- * @param platform
- * Platform implementation
- * @param screen
- * Default screen to show at startup
- * @param hud
- * Head-up display
- * @param args
- * Startup arguments
- * @throws InterruptedException
- * @throws IOException
- */
- public static void start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args)
- throws InterruptedException, IOException {
+ /**
+ * Start an instance of the calculator.
+ *
+ * @param platform
+ * Platform implementation
+ * @param screen
+ * Default screen to show at startup
+ * @param hud
+ * Head-up display
+ * @param args
+ * Startup arguments
+ * @throws InterruptedException
+ * @throws IOException
+ */
+ public static Future start(final Platform platform, final Screen screen, final HUD hud, final StartupArguments args, final RunnableWithException onLoading) throws IOException {
if (WarpPI.running) {
throw new RuntimeException("Already running!");
} else {
WarpPI.running = true;
- WarpPI.INSTANCE.startEngine(platform, screen, hud, args);
+ return WarpPI.INSTANCE.startInstance(platform, screen, hud, args, onLoading);
}
}
- private void startEngine(final Platform platform, final Screen screen,
- final HUD hud, final StartupArguments args)
- throws InterruptedException, IOException {
+ private Future startInstance(final Platform platform, final Screen screen,
+ final HUD hud, final StartupArguments args, final RunnableWithException onLoading)
+ throws IOException {
WarpPI.platform = platform;
platform.getConsoleUtils().out().println("WarpPI Calculator");
initializeEnvironment(args);
@@ -63,15 +63,23 @@ public class WarpPI {
final Thread currentThread = Thread.currentThread();
currentThread.setPriority(Thread.MAX_PRIORITY);
WarpPI.getPlatform().setThreadName(currentThread, "Main thread");
- final DisplayOutputDevice display = platform.getDisplayOutputDevice();
- final BacklightOutputDevice backlight = platform.getBacklightOutputDevice();
- final DisplayManager dm = new DisplayManager(display, backlight, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
- final KeyboardInputDevice keyboard = platform.getKeyboardInputDevice();
- final TouchInputDevice touchscreen = platform.getTouchInputDevice();
- final InputManager im = new InputManager(keyboard, touchscreen);
- device = new Device(dm, im);
- device.setup(() -> WarpPI.loadPhase.onNext(new LoadingStatus()));
+ return CompletableFuture.runAsync(() -> {
+ try {
+ final DisplayOutputDevice display = platform.getDisplayOutputDevice();
+ final BacklightOutputDevice backlight = platform.getBacklightOutputDevice();
+ final DisplayManager dm = new DisplayManager(display, backlight, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
+ final KeyboardInputDevice keyboard = platform.getKeyboardInputDevice();
+ final TouchInputDevice touchscreen = platform.getTouchInputDevice();
+ final InputManager im = new InputManager(keyboard, touchscreen);
+ device = new Device(dm, im);
+ device.setup();
+ onLoading.run();
+ this.loadingCompleted();
+ } catch (Exception ex) {
+ this.loadingFailed(ex);
+ }
+ }).thenRun(this::onShutdown);
}
private void onShutdown() {
@@ -117,10 +125,6 @@ public class WarpPI {
return loaded;
}
- public Observable getLoadPhase() {
- return WarpPI.loadPhase;
- }
-
public Device getHardwareDevice() {
return device;
}
@@ -129,19 +133,13 @@ public class WarpPI {
return WarpPI.platform;
}
- public static class LoadingStatus {
- protected LoadingStatus() {
- }
+ private void loadingCompleted() {
+ WarpPI.INSTANCE.loaded.onNext(true);
+ WarpPI.INSTANCE.device.getDisplayManager().waitForExit();
+ }
- public void done() {
- WarpPI.INSTANCE.loaded.onNext(true);
- WarpPI.INSTANCE.device.getDisplayManager().waitForExit();
- WarpPI.INSTANCE.onShutdown();
- }
-
- public void failed() {
- WarpPI.INSTANCE.onShutdown();
- }
+ private void loadingFailed(Exception e) {
+ e.printStackTrace();
}
}
diff --git a/core/src/main/java/it/cavallium/warppi/boot/Boot.java b/core/src/main/java/it/cavallium/warppi/boot/Boot.java
index ca09e049..1440cac7 100644
--- a/core/src/main/java/it/cavallium/warppi/boot/Boot.java
+++ b/core/src/main/java/it/cavallium/warppi/boot/Boot.java
@@ -1,82 +1,79 @@
-package it.cavallium.warppi.boot;
-
-import java.util.Arrays;
-
-import it.cavallium.warppi.WarpPI;
-import it.cavallium.warppi.WarpPI.LoadingStatus;
-import it.cavallium.warppi.Platform;
-import it.cavallium.warppi.device.PIHardwareDisplay;
-import it.cavallium.warppi.device.input.PIHardwareTouchDevice;
-import it.cavallium.warppi.gui.CalculatorHUD;
-import it.cavallium.warppi.gui.screens.LoadingScreen;
-import it.cavallium.warppi.math.rules.RulesManager;
-import it.cavallium.warppi.util.Error;
-
-public class Boot {
-
- public static void boot(final Platform platform, final String[] args) throws Exception {
- WarpPI.start(platform, new LoadingScreen(), new CalculatorHUD(), Boot.parseStartupArguments(args));
- WarpPI.INSTANCE.getLoadPhase().subscribe(Boot::loadCalculator);
- }
-
- private static void loadCalculator(final LoadingStatus loading) {
- try {
- WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
- //TODO: plugins system: PluginsManager.initialize();
- RulesManager.initialize();
- RulesManager.warmUp();
- loading.done();
- } catch (InterruptedException | Error e) {
- e.printStackTrace();
- }
- loading.failed();
- }
-
- public static StartupArguments parseStartupArguments(final String[] a) {
- final StartupArgumentsImpl args = new StartupArgumentsImpl();
- Arrays.asList(a).stream().parallel().filter((x) -> x != null).map(String::toLowerCase).forEach(arg -> Boot.parseArgument(args, arg));
- return args;
- }
-
- public static void parseArgument(final StartupArgumentsImpl args, final String arg) {
- switch (arg) {
- case "-zoomed":
- args.setZoomed(true);
- break;
- case "-verbose":
- args.setVerboseLoggingEnabled(true);
- break;
- case "-noraspi":
- args.setRaspberryModeAllowed(false);
- break;
- case "nogui":
- args.setNoGUIEngineForced(true);
- break;
- case "ms-dos":
- args.setMSDOSModeEnabled(true);
- break;
- case "html":
- args.setHTMLEngineForced(true);
- break;
- case "gpu":
- args.setGPUEngineForced(true);
- break;
- case "cpu":
- args.setCPUEngineForced(true);
- break;
- case "framebuffer":
- args.setFrameBufferEngineForced(true);
- break;
- case "-debug":
- args.setDebugEnabled(true);
- break;
- case "-uncached":
- args.setUncached(true);
- break;
- default:
- // Not using ConsoleUtils because it isn't initialized at this point.
- System.out.println("Unrecognized argument " + arg);
- break;
- }
- }
-}
+package it.cavallium.warppi.boot;
+
+import java.util.Arrays;
+import java.util.concurrent.Future;
+
+import it.cavallium.warppi.WarpPI;
+import it.cavallium.warppi.Platform;
+import it.cavallium.warppi.device.input.PIHardwareTouchDevice;
+import it.cavallium.warppi.gui.CalculatorHUD;
+import it.cavallium.warppi.gui.screens.LoadingScreen;
+import it.cavallium.warppi.math.rules.RulesManager;
+
+public class Boot {
+
+ public static void boot(final Platform platform, final String[] args) throws Exception {
+ Future execution = WarpPI.start(
+ platform,
+ new LoadingScreen(),
+ new CalculatorHUD(),
+ Boot.parseStartupArguments(args),
+ Boot::loadCalculator);
+ execution.get();
+ }
+
+ private static void loadCalculator() throws Exception {
+ WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().setBrightness(0.2f);
+ //TODO: plugins system: PluginsManager.initialize();
+ RulesManager.initialize();
+ RulesManager.warmUp();
+ }
+
+ public static StartupArguments parseStartupArguments(final String[] a) {
+ final StartupArgumentsImpl args = new StartupArgumentsImpl();
+ Arrays.asList(a).stream().parallel().filter((x) -> x != null).map(String::toLowerCase).forEach(arg -> Boot.parseArgument(args, arg));
+ return args;
+ }
+
+ public static void parseArgument(final StartupArgumentsImpl args, final String arg) {
+ switch (arg) {
+ case "-zoomed":
+ args.setZoomed(true);
+ break;
+ case "-verbose":
+ args.setVerboseLoggingEnabled(true);
+ break;
+ case "-noraspi":
+ args.setRaspberryModeAllowed(false);
+ break;
+ case "nogui":
+ args.setNoGUIEngineForced(true);
+ break;
+ case "ms-dos":
+ args.setMSDOSModeEnabled(true);
+ break;
+ case "html":
+ args.setHTMLEngineForced(true);
+ break;
+ case "gpu":
+ args.setGPUEngineForced(true);
+ break;
+ case "cpu":
+ args.setCPUEngineForced(true);
+ break;
+ case "framebuffer":
+ args.setFrameBufferEngineForced(true);
+ break;
+ case "-debug":
+ args.setDebugEnabled(true);
+ break;
+ case "-uncached":
+ args.setUncached(true);
+ break;
+ default:
+ // Not using ConsoleUtils because it isn't initialized at this point.
+ System.out.println("Unrecognized argument " + arg);
+ break;
+ }
+ }
+}
diff --git a/core/src/main/java/it/cavallium/warppi/device/Device.java b/core/src/main/java/it/cavallium/warppi/device/Device.java
index b7c62fbc..8a8e80d2 100644
--- a/core/src/main/java/it/cavallium/warppi/device/Device.java
+++ b/core/src/main/java/it/cavallium/warppi/device/Device.java
@@ -21,12 +21,8 @@ public class Device {
return inputManager;
}
- public void setup(final Runnable r) {
+ public void setup() {
displayManager.initialize();
- final Thread t = new Thread(r);
- WarpPI.getPlatform().setThreadDaemon(t, false);
- WarpPI.getPlatform().setThreadName(t, "Main thread (after setup)");
- t.start();
}
}
diff --git a/core/src/main/java/it/cavallium/warppi/util/RunnableWithException.java b/core/src/main/java/it/cavallium/warppi/util/RunnableWithException.java
new file mode 100644
index 00000000..a2a1e75e
--- /dev/null
+++ b/core/src/main/java/it/cavallium/warppi/util/RunnableWithException.java
@@ -0,0 +1,5 @@
+package it.cavallium.warppi.util;
+
+public interface RunnableWithException {
+ public void run() throws Exception;
+}
diff --git a/desktop/.classpath b/desktop/.classpath
index d0ba9370..c35053bb 100644
--- a/desktop/.classpath
+++ b/desktop/.classpath
@@ -23,8 +23,9 @@
-
+
+
diff --git a/util/.classpath b/util/.classpath
index 4559ca0b..5e65e726 100644
--- a/util/.classpath
+++ b/util/.classpath
@@ -8,13 +8,14 @@
+
-
-
+
+
diff --git a/util/src/main/java/it/cavallium/warppi/util/Error.java b/util/src/main/java/it/cavallium/warppi/util/Error.java
index 0fd7a345..5ef807a2 100644
--- a/util/src/main/java/it/cavallium/warppi/util/Error.java
+++ b/util/src/main/java/it/cavallium/warppi/util/Error.java
@@ -1,6 +1,6 @@
package it.cavallium.warppi.util;
-public class Error extends java.lang.Throwable {
+public class Error extends java.lang.Exception {
/**
*