WarpPI/desktop/src/main/java/it/cavallium/warppi/desktop/DesktopPlatform.java

341 lines
8.9 KiB
Java
Raw Normal View History

2018-09-04 12:12:41 +02:00
package it.cavallium.warppi.desktop;
import java.io.File;
2018-09-11 01:12:54 +02:00
import java.io.IOException;
2018-09-04 12:12:41 +02:00
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
2019-08-09 20:51:31 +02:00
import java.util.List;
import java.util.ArrayList;
2019-11-01 18:04:01 +01:00
import java.util.function.Consumer;
2018-09-04 12:12:41 +02:00
2019-11-01 18:04:01 +01:00
import it.cavallium.warppi.event.TouchEvent;
2019-10-25 13:58:49 +02:00
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLDisplayOutputDevice;
import it.cavallium.warppi.gui.graphicengine.impl.jogl.JOGLEngine;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingDeviceState;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingTouchInputDevice;
2018-09-11 01:12:54 +02:00
import org.apache.commons.io.FileUtils;
2019-02-27 23:29:03 +01:00
import it.cavallium.warppi.WarpPI;
import it.cavallium.warppi.boot.StartupArguments;
import it.cavallium.warppi.device.DeviceStateDevice;
2019-02-27 23:29:03 +01:00
import it.cavallium.warppi.device.display.BacklightOutputDevice;
import it.cavallium.warppi.device.display.DisplayOutputDevice;
import it.cavallium.warppi.device.display.NoDisplaysAvailableException;
import it.cavallium.warppi.device.display.NullBacklightOutputDevice;
2019-02-27 23:29:03 +01:00
import it.cavallium.warppi.device.input.KeyboardInputDevice;
import it.cavallium.warppi.device.input.TouchInputDevice;
import it.cavallium.warppi.Platform;
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingDisplayOutputDevice;
2018-09-22 10:29:40 +02:00
import it.cavallium.warppi.gui.graphicengine.impl.swing.SwingEngine;
import it.cavallium.warppi.util.CacheUtils;
import it.cavallium.warppi.util.Error;
2018-09-04 12:12:41 +02:00
public class DesktopPlatform implements Platform {
private final DesktopConsoleUtils cu;
private final DesktopGpio gi;
private final DesktopStorageUtils su;
2018-10-15 23:10:44 +02:00
private final ImageUtils pu;
2018-09-04 12:12:41 +02:00
private final String on;
private final DesktopSettings settings;
2018-10-06 16:37:44 +02:00
private Boolean runningOnRaspberryOverride = null;
private StartupArguments args;
private DisplayOutputDevice displayOutputDevice;
private DeviceStateDevice deviceStateDevice;
2019-10-25 13:58:49 +02:00
private TouchInputDevice touchInputDevice;
private KeyboardInputDevice keyboardInputDevice;
2018-09-04 12:12:41 +02:00
public DesktopPlatform() {
cu = new DesktopConsoleUtils();
gi = new DesktopGpio();
su = new DesktopStorageUtils();
2018-10-15 23:10:44 +02:00
pu = new DesktopImageUtils();
2018-09-04 12:12:41 +02:00
on = System.getProperty("os.name").toLowerCase();
settings = new DesktopSettings();
2018-09-04 12:12:41 +02:00
}
2018-09-22 11:17:30 +02:00
2018-09-04 12:12:41 +02:00
@Override
public ConsoleUtils getConsoleUtils() {
return cu;
}
@Override
public Gpio getGpio() {
return gi;
}
@Override
public StorageUtils getStorageUtils() {
return su;
}
@Override
2018-10-15 23:10:44 +02:00
public ImageUtils getImageUtils() {
2018-09-04 12:12:41 +02:00
return pu;
}
@Override
public DesktopSettings getSettings() {
return settings;
}
2018-09-04 12:12:41 +02:00
@Override
2018-09-22 11:17:30 +02:00
public void setThreadName(final Thread t, final String name) {
2018-09-04 12:12:41 +02:00
t.setName(name);
}
@Override
2018-09-22 11:17:30 +02:00
public void setThreadDaemon(final Thread t) {
2018-09-04 12:12:41 +02:00
t.setDaemon(true);
}
@Override
2018-09-22 11:17:30 +02:00
public void setThreadDaemon(final Thread t, final boolean value) {
2018-09-04 12:12:41 +02:00
t.setDaemon(value);
}
@Override
2018-09-22 11:17:30 +02:00
public void exit(final int value) {
2018-09-04 12:12:41 +02:00
System.exit(value);
}
@Override
public void gc() {
System.gc();
}
@Override
public boolean isJavascript() {
return false;
}
@Override
public String getOsName() {
return on;
}
@Override
2018-09-22 11:17:30 +02:00
public void alphaChanged(final boolean val) {
2019-02-27 23:29:03 +01:00
final DisplayOutputDevice currentEngine = WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().display;
2018-09-22 11:17:30 +02:00
if (currentEngine instanceof SwingEngine)
2018-09-22 10:29:40 +02:00
((SwingEngine) currentEngine).setAlphaChanged(val);
2018-09-04 12:12:41 +02:00
}
@Override
2018-09-22 11:17:30 +02:00
public void shiftChanged(final boolean val) {
2019-02-27 23:29:03 +01:00
final DisplayOutputDevice currentEngine = WarpPI.INSTANCE.getHardwareDevice().getDisplayManager().display;
2018-09-22 11:17:30 +02:00
if (currentEngine instanceof SwingEngine)
2018-09-22 10:29:40 +02:00
((SwingEngine) currentEngine).setShiftChanged(val);
2018-09-04 12:12:41 +02:00
}
@Override
public Semaphore newSemaphore() {
return new DesktopSemaphore(0);
}
@Override
2018-09-22 11:17:30 +02:00
public Semaphore newSemaphore(final int i) {
2018-09-04 12:12:41 +02:00
return new DesktopSemaphore(i);
}
@Override
2018-09-22 11:17:30 +02:00
public URLClassLoader newURLClassLoader(final URL[] urls) {
2018-09-04 12:12:41 +02:00
return new DesktopURLClassLoader(urls);
}
@Override
2018-09-22 11:17:30 +02:00
public void throwNewExceptionInInitializerError(final String text) {
2018-09-04 12:12:41 +02:00
throw new ExceptionInInitializerError();
}
@Override
2018-09-22 11:17:30 +02:00
public String[] stacktraceToString(final Error e) {
2018-09-04 12:12:41 +02:00
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
}
@Override
2019-08-09 20:51:31 +02:00
public List<String> getRuleFilePaths() throws IOException {
2019-08-09 21:08:22 +02:00
final File dslRulesPath = getStorageUtils().get("rules/");
2019-08-09 20:51:31 +02:00
List<String> paths = new ArrayList<>();
if (dslRulesPath.exists()) {
for (final File file : getStorageUtils().walk(dslRulesPath)) {
final String path = file.toString();
if (path.endsWith(".rules")) {
paths.add(path);
}
}
}
return paths;
2018-09-04 12:12:41 +02:00
}
2018-10-06 16:37:44 +02:00
@Override
public void setRunningOnRaspberry(boolean b) {
if (isRunningOnRaspberry()) {
runningOnRaspberryOverride = b;
} else {
runningOnRaspberryOverride = false;
}
}
2018-09-11 01:12:54 +02:00
@Override
public boolean isRunningOnRaspberry() {
if (runningOnRaspberryOverride != null)
return runningOnRaspberryOverride;
2018-09-11 01:12:54 +02:00
return CacheUtils.get("isRunningOnRaspberry", 24 * 60 * 60 * 1000, () -> {
2019-02-27 23:29:03 +01:00
if (WarpPI.getPlatform().isJavascript())
2018-09-11 01:12:54 +02:00
return false;
2019-02-27 23:29:03 +01:00
if (WarpPI.getPlatform().getOsName().equals("Linux"))
2018-09-11 01:12:54 +02:00
try {
final File osRelease = new File("/etc", "os-release");
return FileUtils.readLines(osRelease, "UTF-8").stream().map(String::toLowerCase).anyMatch(line -> line.contains("raspbian") && line.contains("name"));
2018-09-22 11:17:30 +02:00
} catch (final IOException readException) {
2018-09-11 01:12:54 +02:00
return false;
}
2018-09-22 11:17:30 +02:00
else
2018-09-11 01:12:54 +02:00
return false;
});
}
2019-02-27 23:29:03 +01:00
@Override
public TouchInputDevice getTouchInputDevice() {
2019-10-25 13:58:49 +02:00
return touchInputDevice;
2019-02-27 23:29:03 +01:00
}
@Override
public KeyboardInputDevice getKeyboardInputDevice() {
2019-10-25 13:58:49 +02:00
return keyboardInputDevice;
2019-02-27 23:29:03 +01:00
}
@Override
public DisplayOutputDevice getDisplayOutputDevice() {
return this.displayOutputDevice;
2019-02-27 23:29:03 +01:00
}
@Override
public BacklightOutputDevice getBacklightOutputDevice() {
return new NullBacklightOutputDevice();
}
@Override
public DeviceStateDevice getDeviceStateDevice() {
return this.deviceStateDevice;
}
@Override
public void setArguments(StartupArguments args) {
this.args = args;
this.chooseDevices();
}
private void chooseDevices() {
List<DisplayOutputDevice> availableDevices = new ArrayList<>();
List<DisplayOutputDevice> guiDevices = List.of(new SwingDisplayOutputDevice(), new JOGLDisplayOutputDevice());
List<DisplayOutputDevice> consoleDevices = List.of();
if (args.isMSDOSModeEnabled() || args.isNoGUIEngineForced()) {
availableDevices.addAll(consoleDevices);
}
if (!args.isNoGUIEngineForced()) {
availableDevices.addAll(guiDevices);
}
if (availableDevices.size() == 0) {
throw new NoDisplaysAvailableException();
}
for (DisplayOutputDevice device : availableDevices) {
if (device instanceof SwingDisplayOutputDevice) {
if (args.isCPUEngineForced()) {
this.displayOutputDevice = device;
2019-11-01 18:04:01 +01:00
break;
}
} else if (device instanceof JOGLDisplayOutputDevice) {
if (args.isGPUEngineForced()) {
this.displayOutputDevice = device;
break;
}
}
}
if (this.displayOutputDevice == null) this.displayOutputDevice = availableDevices.get(0);
2019-10-25 13:58:49 +02:00
if (displayOutputDevice instanceof SwingDisplayOutputDevice) {
this.touchInputDevice = new SwingTouchInputDevice((SwingEngine) displayOutputDevice.getGraphicEngine());
//TODO: implement a keyboard input device
this.keyboardInputDevice = new KeyboardInputDevice() {
@Override
public void initialize() {
}
};
this.deviceStateDevice = new SwingDeviceState((SwingEngine) displayOutputDevice.getGraphicEngine());
} else if (displayOutputDevice instanceof JOGLDisplayOutputDevice) {
2019-11-01 18:04:01 +01:00
//TODO: implement a touch input device
this.touchInputDevice = new TouchInputDevice() {
@Override
public boolean getSwappedAxes() {
return false;
}
@Override
public boolean getInvertedX() {
return false;
}
@Override
public boolean getInvertedY() {
return false;
}
@Override
public void listenTouchEvents(Consumer<TouchEvent> touchEventListener) {
}
@Override
public void initialize() {
}
};
//TODO: implement a keyboard input device
this.keyboardInputDevice = new KeyboardInputDevice() {
@Override
public void initialize() {
}
};
this.deviceStateDevice = new DeviceStateDevice() {
@Override
public void initialize() {
}
@Override
public void waitForExit() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void powerOff() {
}
}; //TODO: Implement
2019-10-25 13:58:49 +02:00
}
2019-02-27 23:29:03 +01:00
}
2018-09-04 12:12:41 +02:00
}