Code cleanup, startup bugfixes and method result caching

This commit is contained in:
Andrea Cavalli 2018-08-28 02:38:27 +02:00
parent 094b2df460
commit 35fc49768c
33 changed files with 706 additions and 335 deletions

BIN
math-rules-cache.zip618 Normal file

Binary file not shown.

BIN
math-rules-cache.zip648 Normal file

Binary file not shown.

BIN
math-rules-cache.zip72 Normal file

Binary file not shown.

BIN
math-rules-cache.zip868 Normal file

Binary file not shown.

162
pom.xml
View File

@ -14,14 +14,16 @@
<src.dir>src/main/java</src.dir>
<src.dir2>src/main/java</src.dir2>
<src.resdir>src/main/resources</src.resdir>
<buildprops.exclude1>veryverylongandparticularpath/abcd</buildprops.exclude1>
<buildprops.exclude1>veryverylongandparticularpath/abcd</buildprops.exclude1>
</properties>
<repositories>
<repository>
<id>teavm-dev</id>
<url>https://dl.bintray.com/konsoletyper/teavm</url>
<snapshots> <enabled>true</enabled> </snapshots>
<layout>default</layout>
<url>https://dl.bintray.com/konsoletyper/teavm</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<layout>default</layout>
</repository>
<repository>
<id>oss-snapshots-repo</id>
@ -32,43 +34,42 @@
<updatePolicy>always</updatePolicy>
</snapshots>
</repository>
<repository>
<id>jitpack.io</id> <!-- JitPack allows github repo to be used as a maven repo -->
<url>https://jitpack.io</url> <!-- For documentation: http://jitpack.io/ -->
</repository>
<repository>
<id>jitpack.io</id> <!-- JitPack allows github repo to be used as a maven repo -->
<url>https://jitpack.io</url> <!-- For documentation: http://jitpack.io/ -->
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>1_teavm-dev</id>
<url>https://dl.bintray.com/konsoletyper/teavm</url>
<snapshots> <enabled>true</enabled> </snapshots>
<layout>default</layout>
</pluginRepository>
<pluginRepository>
<id>1_teavm-dev</id>
<url>https://dl.bintray.com/konsoletyper/teavm</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<layout>default</layout>
</pluginRepository>
</pluginRepositories>
<profiles>
<profile>
<id>aarch64</id>
<activation>
<!--<os>
<family>Linux</family>
<arch>aarch64</arch>
</os>-->
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<buildprops.exclude1>org/warp/picalculator/gui/graphicengine/gpu/*</buildprops.exclude1>
</properties>
</profile>
<profile>
<id>jarprofile</id>
<properties>
<src.dir>jar-specific</src.dir>
<src.resdir>src/main/rules</src.resdir>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<profile>
<id>aarch64</id>
<activation>
<!--<os> <family>Linux</family> <arch>aarch64</arch> </os> -->
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<buildprops.exclude1>org/warp/picalculator/gui/graphicengine/gpu/*</buildprops.exclude1>
</properties>
</profile>
<profile>
<id>jarprofile</id>
<properties>
<src.dir>jar-specific</src.dir>
<src.resdir>src/main/rules</src.resdir>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<dependencies>
<dependency>
<groupId>org.jogamp.jogl</groupId>
<artifactId>jogl-all-main</artifactId>
@ -79,27 +80,27 @@
<artifactId>gluegen-rt-main</artifactId>
<version>2.3.2</version>
</dependency>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
</dependency>
</dependencies>
<dependency>
<groupId>com.pi4j</groupId>
<artifactId>pi4j-core</artifactId>
<version>1.2-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.15</version>
</dependency>
<dependency>
<groupId>net.lingala.zip4j</groupId>
<artifactId>zip4j</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jdt.core.compiler</groupId>
<artifactId>ecj</artifactId>
<version>4.6.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Set a compiler level -->
@ -111,19 +112,19 @@
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<excludes>
<exclude>${buildprops.exclude1}</exclude>
</excludes>
<excludes>
<exclude>${buildprops.exclude1}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profile>
<profile>
<id>jsprofile</id>
<properties>
<src.dir>js-specific</src.dir>
<src.dir2>src/main/rules</src.dir2>
<src.dir2>src/main/rules</src.dir2>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
@ -132,7 +133,7 @@
<dependency>
<groupId>org.teavm</groupId>
<artifactId>teavm-classlib</artifactId>
<version>0.6.0-dev-529</version>
<version>0.6.0-dev-559</version>
</dependency>
</dependencies>
<build>
@ -177,7 +178,7 @@
<plugin>
<groupId>org.teavm</groupId>
<artifactId>teavm-maven-plugin</artifactId>
<version>0.6.0-dev-529</version>
<version>0.6.0-dev-559</version>
<executions>
<execution>
<goals>
@ -200,8 +201,14 @@
<dependencies>
<!--<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<!--<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
<version>4.12</version> <scope>test</scope> </dependency> -->
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-jcache</artifactId>
<version>8.2.4.Final</version>
</dependency>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
@ -214,15 +221,12 @@
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!-- <dependency>
<groupId>ar.com.hjg</groupId>
<artifactId>pngj</artifactId>
<version>2.1.0</version>
</dependency>-->
<!-- <dependency> <groupId>ar.com.hjg</groupId> <artifactId>pngj</artifactId>
<version>2.1.0</version> </dependency> -->
</dependencies>
<build>
<resources>
@ -314,10 +318,10 @@
</plugin>
</plugins>
</build>
<!-- <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration>
<failOnError>false</failOnError> <source>1.8</source> <target>1.8</target>
<archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>libs/</classpathPrefix>
<mainClass>org.warp.picalculator.Main</mainClass> </manifest> </archive>
<!-- <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId> <version>2.4</version> <configuration>
<failOnError>false</failOnError> <source>1.8</source> <target>1.8</target>
<archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>libs/</classpathPrefix>
<mainClass>org.warp.picalculator.Main</mainClass> </manifest> </archive>
</configuration> </plugin> </plugins> </build> -->
</project>

View File

@ -27,7 +27,7 @@ import java.util.stream.Stream;
import org.apache.commons.io.IOUtils;
import org.warp.picalculator.ClassUtils;
import org.warp.picalculator.Main;
import org.warp.picalculator.boot.Main;
public class StorageUtils {
public static final boolean exists(Path f) {

View File

@ -0,0 +1,29 @@
package org.warp.picalculator.deps.nio;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Collection;
import java.util.List;
import org.warp.picalculator.math.Function;
public class DFiles {
public static boolean deleteIfExists(DPath path) throws IOException {
return Files.deleteIfExists(path.toPath());
}
public static boolean exists(DPath f) {
return Files.exists(f.toPath());
}
public static DPath createTempFile(String prefix, String suffix) throws IOException {
return new DPath(Files.createTempFile(prefix, suffix));
}
public static List<String> readAllLines(DPath p) throws IOException {
return Files.readAllLines(p.toPath());
}
}

View File

@ -0,0 +1,20 @@
package org.warp.picalculator.deps.nio;
import java.nio.file.Path;
public class DPath {
private final Path p;
public DPath(Path p) {
this.p = p;
}
Path toPath() {
return p;
}
public DPath toAbsolutePath() {
return new DPath(p.toAbsolutePath());
}
}

View File

@ -0,0 +1,11 @@
package org.warp.picalculator.deps.nio;
import java.nio.file.Paths;
public class DPaths {
public static DPath get(String first, String... more) {
return new DPath(Paths.get(first, more));
}
}

View File

@ -17,7 +17,6 @@ import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.Collection;

View File

@ -0,0 +1,31 @@
package org.warp.picalculator.deps.nio;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.warp.picalculator.deps.nio.DPaths.DPathImpl;
public class DFiles {
public static List<String> readAllLines(DPath osRelease) throws IOException {
return FileUtils.readLines(osRelease.toFile(), Charset.defaultCharset());
}
public static boolean exists(DPath f) {
return f.toFile().exists();
}
public static DPath createTempFile(String prefix, String suffix) throws IOException {
return new DPathImpl(File.createTempFile(prefix, suffix));
}
public static void deleteIfExists(DPath f) throws IOException {
if (f.toFile().exists()) {
f.toFile().delete();
}
}
}

View File

@ -0,0 +1,12 @@
package org.warp.picalculator.deps.nio;
import java.io.File;
import java.nio.file.Path;
public interface DPath {
DPath toAbsolutePath();
File toFile();
}

View File

@ -0,0 +1,32 @@
package org.warp.picalculator.deps.nio;
import java.io.File;
public class DPaths {
public static DPath get(String first, String... more) {
return new DPathImpl("/etc", "os-release");
}
protected static class DPathImpl implements DPath {
protected final File realFile;
private DPathImpl(String main, String... more) {
realFile = new File(String.join(File.separator, main));
}
DPathImpl(File f) {
realFile = f;
}
@Override
public File toFile() {
return realFile;
}
@Override
public DPath toAbsolutePath() {
return new DPathImpl(realFile.getAbsoluteFile());
}
}
}

View File

@ -0,0 +1,41 @@
package org.warp.picalculator;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.function.Supplier;
public class CacheUtils {
private static final Map<String, Object> cache = Collections.synchronizedMap(new HashMap<>());
private static final Map<String, Long> time = Collections.synchronizedMap(new HashMap<>());
@SuppressWarnings("unchecked")
public static <T> T get(String entryName, long expireDelta, Supplier<T> function) {
refreshEntry(entryName);
synchronized(cache) {
if (cache.containsKey(entryName)) {
return (T) cache.get(entryName);
} else {
time.put(entryName, System.currentTimeMillis() + expireDelta);
T result = function.get();
cache.put(entryName, result);
return result;
}
}
}
private static void refreshEntry(String entryName) {
synchronized(time) {
synchronized(cache) {
if (time.getOrDefault(entryName, 0L) <= System.currentTimeMillis()) {
time.remove(entryName);
cache.remove(entryName);
}
}
}
}
}

View File

@ -2,6 +2,7 @@ package org.warp.picalculator;
import java.io.IOException;
import org.warp.picalculator.boot.Main;
import org.warp.picalculator.device.PIHardwareDisplay;
import org.warp.picalculator.gui.CalculatorHUD;
import org.warp.picalculator.gui.screens.KeyboardDebugScreen;
@ -9,6 +10,6 @@ import org.warp.picalculator.gui.screens.KeyboardDebugScreen;
public class KeyboardTest {
public static void main(String[] args) throws InterruptedException, Error, IOException {
new Main(new KeyboardDebugScreen(), new PIHardwareDisplay(), new CalculatorHUD(), args);
new PICalculator(new KeyboardDebugScreen(), new PIHardwareDisplay(), new CalculatorHUD(), Main.parseStartupArguments(args));
}
}

View File

@ -1,192 +0,0 @@
package org.warp.picalculator;
import java.io.IOException;
import org.warp.picalculator.deps.DGpio;
import org.warp.picalculator.deps.DSystem;
import org.warp.picalculator.device.HardwareDevice;
import org.warp.picalculator.device.HardwareTouchDevice;
import org.warp.picalculator.device.InputManager;
import org.warp.picalculator.device.Keyboard;
import org.warp.picalculator.device.PIHardwareDisplay;
import org.warp.picalculator.device.PIHardwareTouchDevice;
import org.warp.picalculator.gui.CalculatorHUD;
import org.warp.picalculator.gui.DisplayManager;
import org.warp.picalculator.gui.HUD;
import org.warp.picalculator.gui.HardwareDisplay;
import org.warp.picalculator.gui.screens.LoadingScreen;
import org.warp.picalculator.gui.screens.Screen;
import org.warp.picalculator.math.rules.RulesManager;
public class Main {
public static Main instance;
public static String[] args;
public Main(String[] args) throws InterruptedException, Error, IOException {
this(new LoadingScreen(), new PIHardwareDisplay(), new CalculatorHUD(), args);
}
public Main(Screen screen, HardwareDisplay disp, HUD hud, String[] args) throws InterruptedException, Error, IOException {
System.out.println("WarpPI Calculator");
instance = this;
Main.args = args;
// ClassUtils.classLoader = this.getClass();
beforeStart();
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
PlatformUtils.setThreadName(Thread.currentThread(), "Main thread");
DisplayManager dm = new DisplayManager(disp, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
Keyboard k = new Keyboard();
HardwareTouchDevice touch = new PIHardwareTouchDevice(false,false,false);
InputManager im = new InputManager(k, touch);
HardwareDevice hardwareDevice = new HardwareDevice(dm, im);
hardwareDevice.setup(() -> {
try {
HardwareDevice.INSTANCE.getDisplayManager().setBrightness(0.2f);
RulesManager.initialize();
RulesManager.warmUp();
if (screen instanceof LoadingScreen) {
((LoadingScreen) screen).loaded = true;
}
HardwareDevice.INSTANCE.getDisplayManager().waitForExit();
} catch (InterruptedException | Error e) {
e.printStackTrace();
}
ConsoleUtils.out.println(1, "Shutdown...");
beforeShutdown();
ConsoleUtils.out.println(1, "");
ConsoleUtils.out.println(1, "Closed.");
DSystem.exit(0);
});
}
public void beforeStart() throws IOException {
boolean isRaspi = false;
try {
isRaspi = DGpio.getBoardType() != DGpio.UnknownBoardType;
} catch (final Exception e) {}
if (Utils.isRunningOnRaspberry() && !Utils.isInArray("-noraspi", args) && isRaspi) {
DGpio.wiringPiSetupPhys();
DGpio.pinMode(12, DGpio.PWM_OUTPUT);
} else {
StaticVars.screenPos = new int[] { 0, 0 };
StaticVars.debugOn = true;
}
Utils.debugThirdScreen = StaticVars.debugOn & false;
for (final String arg : args) {
if (arg.equalsIgnoreCase("2x")) {
StaticVars.debugWindow2x = true;
}
if (arg.equalsIgnoreCase("headless")) {
Utils.headlessOverride = true;
}
if (arg.equalsIgnoreCase("headless-8")) {
Utils.headlessOverride = true;
Utils.forceEngine = "console-8";
}
if (arg.equalsIgnoreCase("headless-256")) {
Utils.headlessOverride = true;
Utils.forceEngine = "console-256";
}
if (arg.equalsIgnoreCase("headless-24bit")) {
Utils.headlessOverride = true;
Utils.forceEngine = "console-24bit";
}
if (arg.equalsIgnoreCase("cpu")) {
Utils.forceEngine = "cpu";
}
if (arg.equalsIgnoreCase("gpu")) {
Utils.forceEngine = "gpu";
}
if (arg.equalsIgnoreCase("fb")) {
Utils.forceEngine = "fb";
}
if (arg.equalsIgnoreCase("nogui")) {
Utils.forceEngine = "nogui";
}
if (arg.equalsIgnoreCase("html")) {
Utils.forceEngine = "html";
}
if (arg.equalsIgnoreCase("verbose") || arg.equalsIgnoreCase("debug")) {
StaticVars.outputLevel = ConsoleUtils.OUTPUTLEVEL_DEBUG_VERBOSE;
}
if (arg.equalsIgnoreCase("uncached")) {
Utils.debugCache = true;
}
if (arg.equalsIgnoreCase("ms-dos")) {
Utils.headlessOverride = true;
Utils.msDosMode = true;
}
}
}
public void beforeShutdown() {
Keyboard.stopKeyboard();
}
public static void main(String[] args) throws InterruptedException, Error, IOException {
/*
* TEST: Comparing BigIntegerMath.divisors() vs programmingpraxis' Number.getFactors() function
*
long time1;
long time2;
final int max = 10000;
final long HCN = 720720L;
final long LCN = 104911L;
final BigInteger[] bigintegers = new BigInteger[max];
bigintegers[0] = BigInteger.valueOf(HCN);
for (int i = 0; i < max; i++) {
bigintegers[i] = bigintegers[0];
}
final Number[] numbers = new Number[max];
final MathContext mc = new MathContext();
numbers[0] = new Number(mc, HCN);
for (int i = 0; i < max; i++) {
numbers[i] = numbers[0];
}
Vector<BigInteger> empty = null;
LinkedList<BigInteger> empty2 = null;
time1 = System.currentTimeMillis();
for(int i = 0; i < max; i++) {
empty = BigIntegerMath.divisors(bigintegers[i]);
}
time2 = System.currentTimeMillis();
System.out.println("BigIntegerMath HCN: "+(time2-time1)+" ("+empty.toString()+")");
bigintegers[0] = BigInteger.valueOf(LCN);
for (int i = 0; i < max; i++) {
bigintegers[i] = bigintegers[0];
}
time1 = System.currentTimeMillis();
for(int i = 0; i < max; i++) {
empty = BigIntegerMath.divisors(bigintegers[i]);
}
time2 = System.currentTimeMillis();
System.out.println("BigIntegerMath LCN: "+(time2-time1)+" ("+empty.toString()+")");
time1 = System.currentTimeMillis();
for(int i = 0; i < max; i++) {
empty2 = numbers[i].getFactors();
}
time2 = System.currentTimeMillis();
System.out.println("BigIntegerMath HCN: "+(time2-time1)+" ("+empty2.toString()+")");
numbers[0] = new Number(mc, LCN);
for (int i = 0; i < max; i++) {
numbers[i] = numbers[0];
}
time1 = System.currentTimeMillis();
for(int i = 0; i < max; i++) {
empty2 = numbers[i].getFactors();
}
time2 = System.currentTimeMillis();
System.out.println("BigIntegerMath LCN: "+(time2-time1)+" ("+empty2.toString()+")");
if(true) {
System.exit(0);;
}
*/
new Main(args);
}
}

View File

@ -0,0 +1,86 @@
package org.warp.picalculator;
import java.io.IOException;
import org.warp.picalculator.boot.StartupArguments;
import org.warp.picalculator.deps.DGpio;
import org.warp.picalculator.deps.DSystem;
import org.warp.picalculator.device.HardwareDevice;
import org.warp.picalculator.device.HardwareTouchDevice;
import org.warp.picalculator.device.InputManager;
import org.warp.picalculator.device.Keyboard;
import org.warp.picalculator.device.PIHardwareDisplay;
import org.warp.picalculator.device.PIHardwareTouchDevice;
import org.warp.picalculator.gui.CalculatorHUD;
import org.warp.picalculator.gui.DisplayManager;
import org.warp.picalculator.gui.HUD;
import org.warp.picalculator.gui.HardwareDisplay;
import org.warp.picalculator.gui.screens.LoadingScreen;
import org.warp.picalculator.gui.screens.Screen;
import org.warp.picalculator.math.rules.RulesManager;
public class PICalculator {
public static PICalculator instance;
public PICalculator(StartupArguments args) throws InterruptedException, IOException {
this(new LoadingScreen(), new PIHardwareDisplay(), new CalculatorHUD(), args);
}
public PICalculator(Screen screen, HardwareDisplay disp, HUD hud, StartupArguments args) throws InterruptedException, IOException {
ConsoleUtils.out.println("WarpPI Calculator");
initializeEnvironment(args);
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
PlatformUtils.setThreadName(Thread.currentThread(), "Main thread");
DisplayManager dm = new DisplayManager(disp, hud, screen, "WarpPI Calculator by Andrea Cavalli (@Cavallium)");
Keyboard k = new Keyboard();
HardwareTouchDevice touch = new PIHardwareTouchDevice(false,false,false);
InputManager im = new InputManager(k, touch);
HardwareDevice hardwareDevice = new HardwareDevice(dm, im);
hardwareDevice.setup(() -> {
try {
HardwareDevice.INSTANCE.getDisplayManager().setBrightness(0.2f);
RulesManager.initialize();
RulesManager.warmUp();
if (screen instanceof LoadingScreen) {
((LoadingScreen) screen).loaded = true;
}
HardwareDevice.INSTANCE.getDisplayManager().waitForExit();
} catch (InterruptedException | Error e) {
e.printStackTrace();
}
ConsoleUtils.out.println(1, "Shutdown...");
beforeShutdown();
ConsoleUtils.out.println(1, "");
ConsoleUtils.out.println(1, "Closed.");
DSystem.exit(0);
});
}
public void initializeEnvironment(StartupArguments args) throws IOException {
instance = this;
ClassUtils.classLoader = this.getClass();
StaticVars.startupArguments = args;
checkDeviceType();
if (Utils.isRunningOnRaspberry() && args.isRaspberryModeAllowed()) {
DGpio.wiringPiSetupPhys();
DGpio.pinMode(12, DGpio.PWM_OUTPUT);
} else {
StaticVars.screenPos = new int[] { 0, 0 };
StaticVars.debugOn = true;
}
Utils.debugThirdScreen = StaticVars.debugOn & false;
StaticVars.debugWindow2x = args.isZoomed();
if (args.isVerboseLoggingEnabled() || args.isDebugEnabled()) {
StaticVars.outputLevel = ConsoleUtils.OUTPUTLEVEL_DEBUG_VERBOSE;
}
}
private void checkDeviceType() {
// TODO Auto-generated method stub
}
public void beforeShutdown() {
Keyboard.stopKeyboard();
}
}

View File

@ -1,5 +1,7 @@
package org.warp.picalculator;
import org.warp.picalculator.boot.StartupArguments;
public class StaticVars {
public static final boolean enableVBO = true;
public static final String calculatorName = "WarpPI";
@ -13,6 +15,7 @@ public class StaticVars {
public static int outputLevel = 0;
public static boolean debugWindow2x = false;
public static float windowZoom = 2;
public static StartupArguments startupArguments;
private StaticVars() {

View File

@ -15,14 +15,20 @@ import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.nevec.rjm.BigDecimalMath;
import org.nevec.rjm.Rational;
import org.warp.picalculator.deps.DGpio;
import org.warp.picalculator.deps.StorageUtils;
import org.warp.picalculator.deps.nio.DFiles;
import org.warp.picalculator.deps.nio.DPath;
import org.warp.picalculator.deps.nio.DPaths;
import org.warp.picalculator.device.HardwareDevice;
import org.warp.picalculator.gui.DisplayManager;
import org.warp.picalculator.gui.graphicengine.BinaryFont;
@ -53,32 +59,17 @@ public class Utils {
public static final RoundingMode scaleMode2 = RoundingMode.HALF_UP;
public static boolean debugThirdScreen;
public static boolean headlessOverride = false;
public static String forceEngine;
public static boolean msDosMode;
public static boolean debugCache;
public static boolean newtMode = true;
public static boolean isInArray(String ch, String[] a) {
boolean contains = false;
for (final String c : a) {
if (c.equals(ch)) {
contains = true;
break;
}
}
return contains;
public static <T> boolean isInArray(T ch, T[] a) {
return Arrays.stream(a).anyMatch(item -> ch.equals(item));
}
public static boolean isInArray(char ch, char[] a) {
boolean contains = false;
for (final char c : a) {
if (c == ch) {
contains = true;
break;
}
if (c == ch) return true;
}
return contains;
return false;
}
private static final String[] regexNormalSymbols = new String[] { "\\", ".", "[", "]", "{", "}", "(", ")", "*", "+", "-", "?", "^", "$", "|" };
@ -646,24 +637,30 @@ public class Utils {
} // for
System.out.println("============");
}
public static boolean isRunningOnRaspberry() {
if (PlatformUtils.osName.equals("Linux")) {
final File file = new File("/etc", "os-release");
try (FileInputStream fis = new FileInputStream(file); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis))) {
String string;
while ((string = bufferedReader.readLine()) != null) {
if (string.toLowerCase().contains("raspbian")) {
if (string.toLowerCase().contains("name")) {
return true;
}
}
}
return CacheUtils.get("isRunningOnRaspberry", 24*60*60*1000, () -> {
if (PlatformUtils.isJavascript) return false;
try {
// Check if it's a raspberry using pi4j
return DGpio.getBoardType() != DGpio.UnknownBoardType;
} catch (final Exception e) {
e.printStackTrace();
// Check if it's a raspberry using other methods
if (PlatformUtils.osName.equals("Linux")) {
try {
final DPath osRelease = DPaths.get("/etc", "os-release");
return DFiles.readAllLines(osRelease).stream()
.map(String::toLowerCase)
.anyMatch(line -> line.contains("raspbian") && line.contains("name"));
} catch (IOException readException) {
return false;
}
} else {
return false;
}
}
}
return false;
});
}
public static boolean isWindows() {
@ -684,7 +681,7 @@ public class Utils {
}
}
public static Path getJarDirectory() {
return Paths.get("").toAbsolutePath();
public static DPath getJarDirectory() {
return DPaths.get("").toAbsolutePath();
}
}

View File

@ -0,0 +1,80 @@
package org.warp.picalculator.boot;
import java.io.IOException;
import java.util.Arrays;
import java.util.stream.Stream;
import org.warp.picalculator.ConsoleUtils;
import org.warp.picalculator.PICalculator;
import org.warp.picalculator.Utils;
import org.warp.picalculator.device.PIHardwareDisplay;
import org.warp.picalculator.gui.CalculatorHUD;
import org.warp.picalculator.gui.screens.LoadingScreen;
public class Main {
public static void main(String[] args) throws Exception {
new PICalculator(parseStartupArguments(args));
}
public static StartupArguments parseStartupArguments(final String[] a) {
final StartupArgumentsImpl args = new StartupArgumentsImpl();
Arrays.asList(a).stream().parallel().map(String::toLowerCase).forEach(arg -> {
switch(arg) {
case "2x":
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 "headless-8":
args.setHeadless8EngineForced(true);
break;
case "headless-256":
args.setHeadless256EngineForced(true);
break;
case "headless-24bit":
args.setHeadless24bitEngineForced(true);
break;
case "headless":
args.setHeadlessEngineForced(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:
ConsoleUtils.out.print(ConsoleUtils.OUTPUTLEVEL_NODEBUG, "Unrecognized argument " + arg);
break;
}
});
args.setHeadlessEngineForced(args.isHeadlessEngineForced() ||
args.isHeadless8EngineForced() ||
args.isHeadless256EngineForced() ||
args.isHeadless24bitEngineForced()
);
return args;
}
}

View File

@ -0,0 +1,21 @@
package org.warp.picalculator.boot;
public interface StartupArguments {
public boolean isRaspberryModeAllowed();
public boolean isZoomed();
public boolean isHeadlessEngineForced();
public boolean isHeadless8EngineForced();
public boolean isHeadless256EngineForced();
public boolean isHeadless24bitEngineForced();
public boolean isCPUEngineForced();
public boolean isGPUEngineForced();
public boolean isFrameBufferEngineForced();
public boolean isNoGUIEngineForced();
public boolean isHTMLEngineForced();
public boolean isMSDOSModeEnabled();
public boolean isVerboseLoggingEnabled();
public boolean isDebugEnabled();
public boolean isUncached();
public boolean isEngineForced();
}

View File

@ -0,0 +1,175 @@
package org.warp.picalculator.boot;
public class StartupArgumentsImpl implements StartupArguments {
StartupArgumentsImpl() {
isRaspberryModeAllowed = true;
}
private boolean isRaspberryModeAllowed;
private boolean isZoomed;
private boolean isHeadlessEngineForced;
private boolean isHeadless8EngineForced;
private boolean isHeadless256EngineForced;
private boolean isHeadless24bitEngineForced;
private boolean isCPUEngineForced;
private boolean isGPUEngineForced;
private boolean isFrameBufferEngineForced;
private boolean isNoGUIEngineForced;
private boolean isHTMLEngineForced;
private boolean isMSDOSModeEnabled;
private boolean isVerboseLoggingEnabled;
private boolean isDebugEnabled;
private boolean isUncached;
@Override
public boolean isRaspberryModeAllowed() {
return isRaspberryModeAllowed;
}
@Override
public boolean isZoomed() {
return isZoomed;
}
@Override
public boolean isHeadlessEngineForced() {
return isHeadlessEngineForced ||
isHeadless8EngineForced ||
isHeadless256EngineForced ||
isHeadless24bitEngineForced;
}
@Override
public boolean isHeadless8EngineForced() {
return isHeadless8EngineForced;
}
@Override
public boolean isHeadless256EngineForced() {
return isHeadless256EngineForced;
}
@Override
public boolean isHeadless24bitEngineForced() {
return isHeadless24bitEngineForced;
}
@Override
public boolean isCPUEngineForced() {
return isCPUEngineForced;
}
@Override
public boolean isGPUEngineForced() {
return isGPUEngineForced;
}
@Override
public boolean isFrameBufferEngineForced() {
return isFrameBufferEngineForced;
}
@Override
public boolean isNoGUIEngineForced() {
return isNoGUIEngineForced;
}
@Override
public boolean isHTMLEngineForced() {
return isHTMLEngineForced;
}
@Override
public boolean isMSDOSModeEnabled() {
return isMSDOSModeEnabled;
}
@Override
public boolean isVerboseLoggingEnabled() {
return isVerboseLoggingEnabled;
}
@Override
public boolean isDebugEnabled() {
return isDebugEnabled;
}
@Override
public boolean isUncached() {
return isUncached;
}
@Override
public boolean isEngineForced() {
return isCPUEngineForced
|| isFrameBufferEngineForced
|| isGPUEngineForced
|| isHeadless24bitEngineForced
|| isHeadless256EngineForced
|| isHeadless8EngineForced
|| isHTMLEngineForced
|| isNoGUIEngineForced;
}
void setRaspberryModeAllowed(boolean isRaspberryModeAllowed) {
this.isRaspberryModeAllowed = isRaspberryModeAllowed;
}
void setZoomed(boolean isZoomed) {
this.isZoomed = isZoomed;
}
void setHeadlessEngineForced(boolean isHeadlessEngineForced) {
this.isHeadlessEngineForced = isHeadlessEngineForced;
}
void setHeadless8EngineForced(boolean isHeadless8EngineForced) {
this.isHeadless8EngineForced = isHeadless8EngineForced;
}
void setHeadless256EngineForced(boolean isHeadless256EngineForced) {
this.isHeadless256EngineForced = isHeadless256EngineForced;
}
void setHeadless24bitEngineForced(boolean isHeadless24bitEngineForced) {
this.isHeadless24bitEngineForced = isHeadless24bitEngineForced;
}
void setCPUEngineForced(boolean isCPUEngineForced) {
this.isCPUEngineForced = isCPUEngineForced;
}
void setGPUEngineForced(boolean isGPUEngineForced) {
this.isGPUEngineForced = isGPUEngineForced;
}
void setFrameBufferEngineForced(boolean isFrameBufferEngineForced) {
this.isFrameBufferEngineForced = isFrameBufferEngineForced;
}
void setNoGUIEngineForced(boolean isNoGUIEngineForced) {
this.isNoGUIEngineForced = isNoGUIEngineForced;
}
void setHTMLEngineForced(boolean isHTMLEngineForced) {
this.isHTMLEngineForced = isHTMLEngineForced;
}
void setMSDOSModeEnabled(boolean isMSDOSModeEnabled) {
this.isMSDOSModeEnabled = isMSDOSModeEnabled;
}
void setVerboseLoggingEnabled(boolean isVerboseLoggingEnabled) {
this.isVerboseLoggingEnabled = isVerboseLoggingEnabled;
}
void setDebugEnabled(boolean isDebugEnabled) {
this.isDebugEnabled = isDebugEnabled;
}
void setUncached(boolean isUncached) {
this.isUncached = isUncached;
}
}

View File

@ -5,11 +5,11 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.UUID;
import org.warp.picalculator.StaticVars;
import org.warp.picalculator.deps.nio.DFiles;
import org.warp.picalculator.deps.nio.DPaths;
public class CacheFile {
private String path;
@ -21,9 +21,9 @@ public class CacheFile {
public CacheFile() {
do {
path = UUID.randomUUID().toString() + ".ser";
} while (Files.exists(Paths.get(path)));
} while (DFiles.exists(DPaths.get(path)));
try {
Files.createTempFile(StaticVars.calculatorNameLOWER, "");
DFiles.createTempFile(StaticVars.calculatorNameLOWER, "");
} catch (final IOException e) {
e.printStackTrace();
}
@ -80,7 +80,7 @@ public class CacheFile {
public void dispose() {
closeStreams();
try {
Files.deleteIfExists(Paths.get(path));
DFiles.deleteIfExists(DPaths.get(path));
} catch (final IOException e) {
e.printStackTrace();
}

View File

@ -1,5 +1,6 @@
package org.warp.picalculator.device;
import org.warp.picalculator.PlatformUtils;
import org.warp.picalculator.gui.DisplayManager;
public class HardwareDevice {
@ -25,8 +26,8 @@ public class HardwareDevice {
displayManager.initialize();
inputManager.getKeyboard().startKeyboard();
Thread t = new Thread(r);
t.setDaemon(false);
t.setName("Main thread (after setup)");
PlatformUtils.setDaemon(t, false);
PlatformUtils.setThreadName(t, "Main thread (after setup)");
t.start();
}

View File

@ -12,7 +12,7 @@ import java.util.logging.Logger;
import javax.imageio.ImageIO;
import org.warp.picalculator.ClassUtils;
import org.warp.picalculator.Main;
import org.warp.picalculator.PICalculator;
import org.warp.picalculator.Utils;
import org.warp.picalculator.deps.DSystem;

View File

@ -191,10 +191,10 @@ public class CPUEngine implements GraphicEngine {
@Override
public boolean isSupported() {
if (Utils.forceEngine != null && Utils.forceEngine != "cpu") {
if (StaticVars.startupArguments.isEngineForced() && StaticVars.startupArguments.isCPUEngineForced() == false) {
return false;
}
return (Utils.headlessOverride || GraphicsEnvironment.isHeadless()) == false;
return (StaticVars.startupArguments.isHeadlessEngineForced() || GraphicsEnvironment.isHeadless()) == false;
}
@Override

View File

@ -7,6 +7,7 @@ import java.nio.MappedByteBuffer;
import java.util.concurrent.Semaphore;
import org.warp.picalculator.MmapByteBuffer;
import org.warp.picalculator.StaticVars;
import org.warp.picalculator.TestJNI;
import org.warp.picalculator.Utils;
import org.warp.picalculator.gui.graphicengine.BinaryFont;
@ -174,10 +175,10 @@ public class FBEngine implements GraphicEngine {
@Override
public boolean isSupported() {
if (Utils.forceEngine != null && Utils.forceEngine != "fb") {
if (StaticVars.startupArguments.isEngineForced() && StaticVars.startupArguments.isFrameBufferEngineForced() == false) {
return false;
}
if (Utils.headlessOverride) {
if (StaticVars.startupArguments.isHeadlessEngineForced()) {
return false;
}
/*

View File

@ -162,10 +162,10 @@ public class GPUEngine implements GraphicEngine {
@Override
public boolean isSupported() {
if (Utils.forceEngine != null && Utils.forceEngine != "gpu") {
if (StaticVars.startupArguments.isEngineForced() && StaticVars.startupArguments.isGPUEngineForced() == false) {
return false;
}
if (Utils.headlessOverride) {
if (StaticVars.startupArguments.isHeadlessEngineForced()) {
return false;
}
boolean available = false;

View File

@ -64,7 +64,7 @@ public class Headless24bitEngine implements org.warp.picalculator.gui.graphiceng
public void create(Runnable onInitialized) {
StaticVars.outputLevel = -1;
AnsiConsole.systemInstall();
if (Utils.isWindows() && !Utils.msDosMode) {
if (Utils.isWindows() && !StaticVars.startupArguments.isMSDOSModeEnabled()) {
win = true;
WindowsSupport.setConsoleMode(0x0200);
final Thread t = new Thread(() -> {
@ -319,7 +319,13 @@ public class Headless24bitEngine implements org.warp.picalculator.gui.graphiceng
@Override
public boolean isSupported() {
if (Utils.msDosMode || (Utils.forceEngine != null && Utils.forceEngine != "console-24bit")) {
if (
StaticVars.startupArguments.isMSDOSModeEnabled()
|| (
StaticVars.startupArguments.isEngineForced()
&& StaticVars.startupArguments.isHeadless24bitEngineForced() == false
)
) {
return false;
}
return true;

View File

@ -63,7 +63,7 @@ public class Headless256Engine implements org.warp.picalculator.gui.graphicengin
public void create(Runnable onInitialized) {
StaticVars.outputLevel = -1;
AnsiConsole.systemInstall();
if (Utils.isWindows() && !Utils.msDosMode) {
if (Utils.isWindows() && !StaticVars.startupArguments.isMSDOSModeEnabled()) {
win = true;
WindowsSupport.setConsoleMode(0x0200);
final Thread t = new Thread(() -> {
@ -276,7 +276,13 @@ public class Headless256Engine implements org.warp.picalculator.gui.graphicengin
@Override
public boolean isSupported() {
if (Utils.msDosMode || (Utils.forceEngine != null && Utils.forceEngine != "console-256")) {
if (
StaticVars.startupArguments.isMSDOSModeEnabled()
|| (
StaticVars.startupArguments.isEngineForced()
&& StaticVars.startupArguments.isHeadless256EngineForced() == false
)
) {
return false;
}
return true;

View File

@ -63,7 +63,7 @@ public class Headless8Engine implements org.warp.picalculator.gui.graphicengine.
public void create(Runnable onInitialized) {
StaticVars.outputLevel = -1;
AnsiConsole.systemInstall();
if (Utils.isWindows() && !Utils.msDosMode) {
if (Utils.isWindows() && !StaticVars.startupArguments.isMSDOSModeEnabled()) {
win = true;
WindowsSupport.setConsoleMode(0x0200);
final Thread t = new Thread(() -> {
@ -276,7 +276,13 @@ public class Headless8Engine implements org.warp.picalculator.gui.graphicengine.
@Override
public boolean isSupported() {
if (Utils.forceEngine != null && Utils.forceEngine != "console-8") {
if (
StaticVars.startupArguments.isMSDOSModeEnabled()
|| (
StaticVars.startupArguments.isEngineForced()
&& StaticVars.startupArguments.isHeadless8EngineForced() == false
)
) {
return false;
}
return true;

View File

@ -1,5 +1,6 @@
package org.warp.picalculator.gui.graphicengine.headless8;
import org.warp.picalculator.StaticVars;
import org.warp.picalculator.Utils;
import org.warp.picalculator.gui.graphicengine.Renderer;
@ -19,7 +20,7 @@ public class Headless8Renderer implements Renderer {
public static final String[] colorANSI = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "0;1", "1;1", "2;1", "3;1", "4;1", "5;1", "6;1", "7;1" };
public static final String ANSI_RESET = "\u001B[0m";
public static final char FILL = Utils.msDosMode ? 0xDB : '█';
public static final char FILL = StaticVars.startupArguments.isMSDOSModeEnabled() ? 0xDB : '█';
private int hexColor(int red, int green, int blue) {
final int r1 = red;

View File

@ -11,7 +11,6 @@ import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.ArrayList;
import java.util.List;
@ -21,6 +20,7 @@ import org.apache.commons.io.IOUtils;
import org.warp.picalculator.ConsoleUtils;
import org.warp.picalculator.Error;
import org.warp.picalculator.PlatformUtils;
import org.warp.picalculator.StaticVars;
import org.warp.picalculator.Utils;
import org.warp.picalculator.ZipUtils;
import org.warp.picalculator.deps.StorageUtils;
@ -101,7 +101,7 @@ public class RulesManager {
tDir.delete();
}
ZipUtils.unzip(cacheFilePath.toString(), tDir.getParent().toString(), "");
useCache = !Utils.debugCache;
useCache = !StaticVars.startupArguments.isUncached();
} catch (final Exception ex) {
ex.printStackTrace();
}
@ -189,7 +189,7 @@ public class RulesManager {
}
StorageUtils.write(tFileJava, javaCode.getBytes("UTF-8"), DStandardOpenOption.WRITE, DStandardOpenOption.CREATE);
final boolean compiled = DJDTCompiler.compile(new String[] { "-nowarn", "-1.8", tFileJava.toString() }, new PrintWriter(System.out), new PrintWriter(System.err));
if (Utils.debugCache) {
if (StaticVars.startupArguments.isUncached()) {
tFileJava.deleteOnExit();
} else {
tFileJava.delete();