Skip to content

Commit 20ec815

Browse files
committed
implement passing of command line options (#2552)
1 parent cf2f7b0 commit 20ec815

File tree

6 files changed

+111
-90
lines changed

6 files changed

+111
-90
lines changed

core/todo.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ X could add -Dapp.root=$APP_ROOT and get via System.getProperty("app.root")
1515
X https://github.com/processing/processing/issues/2181
1616
X textWidth() incorrect with default (JAVA2D) renderer and default font
1717
X https://github.com/processing/processing/issues/2175
18+
X Error on size() when using FX2D due to stage inset issues
19+
X https://github.com/processing/processing/issues/3412
20+
X probably fixes w/ size() removal change
1821

1922
jakub
2023
X Remove alpha filler (hopefully no regression here)
@@ -58,8 +61,6 @@ _ https://github.com/processing/processing/issues/2063
5861

5962

6063
javafx
61-
_ Error on size() when using FX2D due to stage inset issues
62-
_ https://github.com/processing/processing/issues/3412
6364
_ static mode sketches (draw once and halt w/o closing window)
6465
_ fix display handling, line up the device order with AWT
6566
_ https://docs.oracle.com/javafx/2/api/javafx/stage/Screen.html

java/src/processing/mode/java/Commander.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,10 @@ public Commander(String[] args) {
112112
System.exit(1);
113113
}
114114

115-
// File preferencesFile = Base.getSettingsFile("preferences.txt");
116-
// System.out.println("Preferences file at " + preferencesFile.getAbsolutePath());
117-
115+
int argOffset = 0;
118116
for (String arg : args) {
117+
argOffset++;
118+
119119
if (arg.length() == 0) {
120120
// ignore it, just the crappy shell script
121121

@@ -127,18 +127,19 @@ public Commander(String[] args) {
127127

128128
} else if (arg.equals(buildArg)) {
129129
task = BUILD;
130+
break;
130131

131132
} else if (arg.equals(runArg)) {
132133
task = RUN;
134+
break;
133135

134136
} else if (arg.equals(presentArg)) {
135137
task = PRESENT;
136-
137-
// } else if (arg.equals(exportAppletArg)) {
138-
// task = EXPORT_APPLET;
138+
break;
139139

140140
} else if (arg.equals(exportApplicationArg)) {
141141
task = EXPORT;
142+
break;
142143

143144
} else if (arg.equals(noJavaArg)) {
144145
embedJava = false;
@@ -189,6 +190,7 @@ public Commander(String[] args) {
189190
complainAndQuit("I don't know anything about " + arg + ".", true);
190191
}
191192
}
193+
String[] sketchArgs = PApplet.subset(args, argOffset);
192194

193195
// if ((outputPath == null) &&
194196
// (task == PREPROCESS || task == BUILD ||
@@ -268,7 +270,11 @@ public Commander(String[] args) {
268270
success = true;
269271
if (task == RUN || task == PRESENT) {
270272
Runner runner = new Runner(build, this);
271-
runner.launch(task == PRESENT);
273+
if (task == PRESENT) {
274+
runner.present(sketchArgs);
275+
} else {
276+
runner.launch(sketchArgs);
277+
}
272278
}
273279
} else {
274280
success = false;
@@ -369,7 +375,7 @@ static void printCommandLine(PrintStream out) {
369375
out.println();
370376
out.println("--build Preprocess and compile a sketch into .class files.");
371377
out.println("--run Preprocess, compile, and run a sketch.");
372-
out.println("--present Preprocess, compile, and run a sketch full screen.");
378+
out.println("--present Preprocess, compile, and run a sketch in presentation mode.");
373379
out.println();
374380
out.println("--export Export an application.");
375381
out.println("--no-java Do not embed Java. Use at your own risk!");
@@ -378,6 +384,13 @@ static void printCommandLine(PrintStream out) {
378384
// out.println("--bits Must be specified if libraries are used that are");
379385
// out.println(" 32- or 64-bit specific such as the OpenGL library.");
380386
// out.println(" Otherwise specify 0 or leave it out.");
387+
388+
out.println("The --build, --run, --present, or --export must be the final parameter.");
389+
out.println("Additional arguments will be passed to the sketch itself and available");
390+
out.println("in the sketch's 'args' field. To pass options understood by PApplet.main(),");
391+
out.println("write a custom main() method so that the preprocessor does not add one.");
392+
out.println("https://github.com/processing/processing/wiki/Command-Line");
393+
381394
out.println();
382395
}
383396

java/src/processing/mode/java/Debugger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public synchronized void startDebug() {
216216

217217
log(Level.INFO, "launching debuggee runtime");
218218
runtime = new Runner(build, editor);
219-
VirtualMachine vm = runtime.launchDebug(); // non-blocking
219+
VirtualMachine vm = runtime.debug(null); // non-blocking
220220
if (vm == null) {
221221
log(Level.SEVERE, "error 37: launch failed");
222222
}

java/src/processing/mode/java/JavaMode.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,12 @@ public Runner handleLaunch(Sketch sketch, RunnerListener listener,
149149
final Runner runtime = new Runner(build, listener);
150150
new Thread(new Runnable() {
151151
public void run() {
152-
runtime.launch(present); // this blocks until finished
152+
// these block until finished
153+
if (present) {
154+
runtime.present(null);
155+
} else {
156+
runtime.launch(null);
157+
}
153158
}
154159
}).start();
155160
return runtime;
@@ -197,15 +202,20 @@ public Runner handleTweak(Sketch sketch,
197202
if (appletClassName != null) {
198203
final Runner runtime = new Runner(build, listener);
199204
new Thread(new Runnable() {
200-
public void run() {
201-
runtime.launch(present); // this blocks until finished
202-
// next lines are executed when the sketch quits
203-
if (launchInteractive) {
204-
editor.initEditorCode(parser.allHandles, false);
205-
editor.stopTweakMode(parser.allHandles);
206-
}
205+
public void run() {
206+
// these block until finished
207+
if (present) {
208+
runtime.present(null);
209+
} else {
210+
runtime.launch(null);
207211
}
208-
}).start();
212+
// next lines are executed when the sketch quits
213+
if (launchInteractive) {
214+
editor.initEditorCode(parser.allHandles, false);
215+
editor.stopTweakMode(parser.allHandles);
216+
}
217+
}
218+
}).start();
209219

210220
if (launchInteractive) {
211221
// replace editor code with baseCode

java/src/processing/mode/java/runner/Runner.java

Lines changed: 61 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import processing.app.exec.StreamRedirectThread;
2727
import processing.app.ui.Editor;
2828
import processing.core.*;
29+
import processing.data.StringList;
2930
import processing.mode.java.JavaBuild;
3031

3132
import java.awt.GraphicsDevice;
@@ -113,19 +114,28 @@ public Runner(JavaBuild build, RunnerListener listener) throws SketchException {
113114
}
114115

115116

116-
public void launch(boolean presenting) {
117-
if (launchVirtualMachine(presenting)) {
117+
public VirtualMachine launch(String[] args) {
118+
if (launchVirtualMachine(false, args)) {
118119
generateTrace();
119120
}
121+
return vm;
122+
}
123+
124+
125+
public VirtualMachine present(String[] args) {
126+
if (launchVirtualMachine(true, args)) {
127+
generateTrace();
128+
}
129+
return vm;
120130
}
121131

122132

123133
/**
124134
* Simple non-blocking launch of the virtual machine. VM starts suspended.
125135
* @return debuggee VM or null on failure
126136
*/
127-
public VirtualMachine launchDebug() {
128-
if (launchVirtualMachine(false)) { // will return null on failure
137+
public VirtualMachine debug(String[] args) {
138+
if (launchVirtualMachine(false, args)) { // will return null on failure
129139
redirectStreams(vm);
130140
}
131141
return vm;
@@ -153,9 +163,9 @@ public VirtualMachine vm() {
153163
}
154164

155165

156-
public boolean launchVirtualMachine(boolean presenting) {
157-
String[] vmParams = getMachineParams();
158-
String[] sketchParams = getSketchParams(presenting);
166+
public boolean launchVirtualMachine(boolean present, String[] args) {
167+
StringList vmParams = getMachineParams();
168+
StringList sketchParams = getSketchParams(present, args);
159169
// PApplet.printArray(sketchParams);
160170
int port = 8000 + (int) (Math.random() * 1000);
161171
String portStr = String.valueOf(port);
@@ -167,13 +177,13 @@ public boolean launchVirtualMachine(boolean presenting) {
167177
String jdwpArg = "-agentlib:jdwp=transport=dt_socket,address=" + portStr + ",server=y,suspend=y";
168178

169179
// Everyone works the same under Java 7 (also on OS X)
170-
String[] commandArgs = new String[] { Platform.getJavaPath(), jdwpArg };
180+
StringList commandArgs = new StringList();
181+
commandArgs.append(Platform.getJavaPath());
182+
commandArgs.append(jdwpArg);
171183

172-
commandArgs = PApplet.concat(commandArgs, vmParams);
173-
commandArgs = PApplet.concat(commandArgs, sketchParams);
174-
// PApplet.println(commandArgs);
175-
// commandArg.setValue(commandArgs);
176-
launchJava(commandArgs);
184+
commandArgs.append(vmParams);
185+
commandArgs.append(sketchParams);
186+
launchJava(commandArgs.array());
177187

178188
AttachingConnector connector = (AttachingConnector)
179189
findConnector("com.sun.jdi.SocketAttach");
@@ -228,8 +238,8 @@ public boolean launchVirtualMachine(boolean presenting) {
228238
}
229239

230240

231-
protected String[] getMachineParams() {
232-
ArrayList<String> params = new ArrayList<String>();
241+
protected StringList getMachineParams() {
242+
StringList params = new StringList();
233243

234244
//params.add("-Xint"); // interpreted mode
235245
//params.add("-Xprof"); // profiler
@@ -244,73 +254,53 @@ protected String[] getMachineParams() {
244254
for (int i = 0; i < pieces.length; i++) {
245255
String p = pieces[i].trim();
246256
if (p.length() > 0) {
247-
params.add(p);
257+
params.append(p);
248258
}
249259
}
250260
}
251261

252262
// params.add("-Djava.ext.dirs=nuffing");
253263

254264
if (Preferences.getBoolean("run.options.memory")) {
255-
params.add("-Xms" + Preferences.get("run.options.memory.initial") + "m");
256-
params.add("-Xmx" + Preferences.get("run.options.memory.maximum") + "m");
265+
params.append("-Xms" + Preferences.get("run.options.memory.initial") + "m");
266+
params.append("-Xmx" + Preferences.get("run.options.memory.maximum") + "m");
257267
}
258268

259269
if (Platform.isMacOS()) {
260-
params.add("-Xdock:name=" + build.getSketchClassName());
270+
params.append("-Xdock:name=" + build.getSketchClassName());
261271
// params.add("-Dcom.apple.mrj.application.apple.menu.about.name=" +
262272
// sketch.getMainClassName());
263273
}
264274
// sketch.libraryPath might be ""
265275
// librariesClassPath will always have sep char prepended
266-
params.add("-Djava.library.path=" +
267-
build.getJavaLibraryPath() +
268-
File.pathSeparator +
269-
System.getProperty("java.library.path"));
276+
params.append("-Djava.library.path=" +
277+
build.getJavaLibraryPath() +
278+
File.pathSeparator +
279+
System.getProperty("java.library.path"));
270280

271-
params.add("-cp");
272-
params.add(build.getClassPath());
273-
// params.add(sketch.getClassPath() +
274-
// File.pathSeparator +
275-
// Base.librariesClassPath);
281+
params.append("-cp");
282+
params.append(build.getClassPath());
276283

277284
// enable assertions
278285
// http://dev.processing.org/bugs/show_bug.cgi?id=1188
279-
params.add("-ea");
286+
params.append("-ea");
280287
//PApplet.println(PApplet.split(sketch.classPath, ':'));
281288

282-
String outgoing[] = new String[params.size()];
283-
params.toArray(outgoing);
284-
285-
// PApplet.println(outgoing);
286-
// PApplet.println(PApplet.split(outgoing[0], ":"));
287-
// PApplet.println();
288-
// PApplet.println("class path");
289-
// PApplet.println(PApplet.split(outgoing[2], ":"));
290-
291-
return outgoing;
292-
//return (String[]) params.toArray();
293-
294-
// System.out.println("sketch class path");
295-
// PApplet.println(PApplet.split(sketch.classPath, ';'));
296-
// System.out.println();
297-
// System.out.println("libraries class path");
298-
// PApplet.println(PApplet.split(Base.librariesClassPath, ';'));
299-
// System.out.println();
289+
return params;
300290
}
301291

302292

303-
protected String[] getSketchParams(boolean presenting) {
304-
ArrayList<String> params = new ArrayList<String>();
293+
protected StringList getSketchParams(boolean present, String[] args) {
294+
StringList params = new StringList();
305295

306296
// It's dangerous to add your own main() to your code,
307297
// but if you've done it, we'll respect your right to hang yourself.
308298
// http://processing.org/bugs/bugzilla/1446.html
309299
if (build.getFoundMain()) {
310-
params.add(build.getSketchClassName());
300+
params.append(build.getSketchClassName());
311301

312302
} else {
313-
params.add("processing.core.PApplet");
303+
params.append("processing.core.PApplet");
314304

315305
// get the stored device index (starts at 1)
316306
int runDisplay = Preferences.getInteger("run.display");
@@ -366,8 +356,8 @@ protected String[] getSketchParams(boolean presenting) {
366356
// If sketches are to be shown on the same display as the editor,
367357
// provide the editor location so the sketch's main() can place it.
368358
Point editorLocation = editor.getLocation();
369-
params.add(PApplet.ARGS_EDITOR_LOCATION + "=" +
370-
editorLocation.x + "," + editorLocation.y);
359+
params.append(PApplet.ARGS_EDITOR_LOCATION + "=" +
360+
editorLocation.x + "," + editorLocation.y);
371361
} else {
372362
// The sketch's main() will set a location centered on the new
373363
// display. It has to happen in main() because the width/height
@@ -379,37 +369,38 @@ protected String[] getSketchParams(boolean presenting) {
379369
// params.add(PApplet.ARGS_LOCATION + "=" + runX + "," + runY);
380370
}
381371
} else {
382-
params.add(PApplet.ARGS_LOCATION + "=" +
383-
windowLocation.x + "," + windowLocation.y);
372+
params.append(PApplet.ARGS_LOCATION + "=" +
373+
windowLocation.x + "," + windowLocation.y);
384374
}
385-
params.add(PApplet.ARGS_EXTERNAL);
375+
params.append(PApplet.ARGS_EXTERNAL);
386376
}
387377

388-
params.add(PApplet.ARGS_DISPLAY + "=" + runDisplay);
378+
params.append(PApplet.ARGS_DISPLAY + "=" + runDisplay);
389379

390380

391-
if (presenting) {
392-
params.add(PApplet.ARGS_PRESENT);
381+
if (present) {
382+
params.append(PApplet.ARGS_PRESENT);
393383
// if (Preferences.getBoolean("run.present.exclusive")) {
394384
// params.add(PApplet.ARGS_EXCLUSIVE);
395385
// }
396-
params.add(PApplet.ARGS_STOP_COLOR + "=" +
397-
Preferences.get("run.present.stop.color"));
398-
params.add(PApplet.ARGS_WINDOW_COLOR + "=" +
399-
Preferences.get("run.present.bgcolor"));
386+
params.append(PApplet.ARGS_STOP_COLOR + "=" +
387+
Preferences.get("run.present.stop.color"));
388+
params.append(PApplet.ARGS_WINDOW_COLOR + "=" +
389+
Preferences.get("run.present.bgcolor"));
400390
}
401391

402392
// There was a PDE X hack that put this after the class name, but it was
403393
// removed for 3.0a6 because it would break the args passed to sketches.
404-
params.add(PApplet.ARGS_SKETCH_FOLDER + "=" + build.getSketchPath());
394+
params.append(PApplet.ARGS_SKETCH_FOLDER + "=" + build.getSketchPath());
405395

406-
params.add(build.getSketchClassName());
396+
params.append(build.getSketchClassName());
407397
}
408-
409-
// String outgoing[] = new String[params.size()];
410-
// params.toArray(outgoing);
411-
// return outgoing;
412-
return params.toArray(new String[0]);
398+
// Add command-line arguments to be given to the sketch itself
399+
if (args != null) {
400+
params.append(args);
401+
}
402+
// Pass back the whole list
403+
return params;
413404
}
414405

415406

0 commit comments

Comments
 (0)