Skip to content

Commit 5757d17

Browse files
committed
Add command line parsing in GUI mode and headless Windows support
In GUI mode, a file name can be specified to open, and --headless can be added to the command to make it not really open the GUI. In headless mode, we don't really need to listen for hangup signals (since the wrapper script will kill any old instances of GRIP), so we can remove that to make it work on non-unix systems.
1 parent 21c6dc8 commit 5757d17

File tree

2 files changed

+38
-37
lines changed

2 files changed

+38
-37
lines changed

core/src/main/java/edu/wpi/grip/core/Main.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import edu.wpi.grip.core.events.ExceptionEvent;
99
import edu.wpi.grip.core.operations.Operations;
1010
import edu.wpi.grip.core.serialization.Project;
11-
import edu.wpi.grip.core.util.SafeShutdown;
1211
import edu.wpi.grip.generated.CVOperations;
13-
import sun.misc.Signal;
1412

1513
import javax.inject.Inject;
1614
import java.io.File;
@@ -23,22 +21,13 @@
2321
*/
2422
public class Main {
2523

26-
@Inject
27-
private Project project;
28-
@Inject
29-
private PipelineRunner pipelineRunner;
30-
@Inject
31-
private EventBus eventBus;
32-
@Inject
33-
private Logger logger;
24+
@Inject private Project project;
25+
@Inject private PipelineRunner pipelineRunner;
26+
@Inject private EventBus eventBus;
27+
@Inject private Logger logger;
3428

3529
@SuppressWarnings("PMD.SystemPrintln")
3630
public static void main(String[] args) throws IOException, InterruptedException {
37-
// Close GRIP when we get SIGHUP. This signal is sent, for example, when GRIP is run in an SSH session
38-
// and the session is closed.
39-
Signal.handle(new Signal("HUP"), signal -> SafeShutdown.exit(0));
40-
41-
System.out.println("Loading Dependency Injection Framework");
4231
final Injector injector = Guice.createInjector(new GRIPCoreModule());
4332
injector.getInstance(Main.class).start(args);
4433
}

ui/src/main/java/edu/wpi/grip/ui/Main.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
import com.google.inject.Injector;
88
import com.sun.javafx.application.PlatformImpl;
99
import edu.wpi.grip.core.GRIPCoreModule;
10-
import edu.wpi.grip.core.Palette;
1110
import edu.wpi.grip.core.PipelineRunner;
1211
import edu.wpi.grip.core.events.UnexpectedThrowableEvent;
1312
import edu.wpi.grip.core.operations.Operations;
13+
import edu.wpi.grip.core.serialization.Project;
1414
import edu.wpi.grip.core.util.SafeShutdown;
1515
import edu.wpi.grip.generated.CVOperations;
1616
import edu.wpi.grip.ui.util.DPIUtility;
@@ -23,19 +23,19 @@
2323
import javafx.stage.Stage;
2424

2525
import javax.inject.Inject;
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.util.ArrayList;
29+
import java.util.List;
2630
import java.util.logging.Level;
2731
import java.util.logging.Logger;
2832

2933
public class Main extends Application {
3034

31-
@Inject
32-
private EventBus eventBus;
33-
@Inject
34-
private Palette palette;
35-
@Inject
36-
private PipelineRunner pipelineRunner;
37-
@Inject
38-
private Logger logger;
35+
@Inject private EventBus eventBus;
36+
@Inject private PipelineRunner pipelineRunner;
37+
@Inject private Project project;
38+
@Inject private Logger logger;
3939

4040
@VisibleForTesting
4141
protected final Injector injector = Guice.createInjector(new GRIPCoreModule(), new GRIPUIModule());
@@ -58,25 +58,37 @@ public Main() {
5858
@Override
5959
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
6060
public void start(Stage stage) throws Exception {
61-
root = FXMLLoader.load(Main.class.getResource("MainWindow.fxml"), null, null, injector::getInstance);
62-
root.setStyle("-fx-font-size: " + DPIUtility.FONT_SIZE + "px");
61+
// If --headless was specified on the command line, run in headless mode
62+
List<String> parameters = new ArrayList<>(getParameters().getRaw());
63+
64+
if (parameters.contains("--headless")) {
65+
parameters.remove("--headless");
66+
} else {
67+
root = FXMLLoader.load(Main.class.getResource("MainWindow.fxml"), null, null, injector::getInstance);
68+
root.setStyle("-fx-font-size: " + DPIUtility.FONT_SIZE + "px");
69+
70+
// If this isn't here this can cause a deadlock on windows. See issue #297
71+
stage.setOnCloseRequest(event -> SafeShutdown.exit(0, Platform::exit));
72+
stage.setTitle("GRIP Computer Vision Engine");
73+
stage.getIcons().add(new Image("/edu/wpi/grip/ui/icons/grip.png"));
74+
stage.setScene(new Scene(root));
75+
stage.show();
76+
}
6377

6478
Operations.addOperations(eventBus);
6579
CVOperations.addOperations(eventBus);
6680

67-
stage.setOnCloseRequest((event) -> {
68-
// If this isn't here this can cause a deadlock on windows
69-
// See issue #297
70-
SafeShutdown.exit(0, Platform::exit);
71-
});
81+
// If there was a file specified on the command line, open it immediately
82+
if (!parameters.isEmpty()) {
83+
try {
84+
project.open(new File(parameters.get(0)));
85+
} catch (IOException e) {
86+
logger.log(Level.SEVERE, "Error loading file: " + parameters.get(0));
87+
throw e;
88+
}
89+
}
7290

7391
pipelineRunner.startAsync();
74-
75-
stage.setTitle("GRIP Computer Vision Engine");
76-
stage.getIcons().add(new Image("/edu/wpi/grip/ui/icons/grip.png"));
77-
stage.setScene(new Scene(root));
78-
stage.show();
79-
8092
}
8193

8294
public void stop() {

0 commit comments

Comments
 (0)