Skip to content

Commit b7f90b8

Browse files
committed
Working around some opengl problems
1 parent 5096f41 commit b7f90b8

File tree

8 files changed

+98
-28
lines changed

8 files changed

+98
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Processing Python Mode
22
----------------------
33

4-
[Processing](http://www.processing.org/) is awesome! [Jython](http://www.jython.org/) is awesome! Together, they are TOTALLY AWESOME!
4+
[Processing](http://www.processing.org/) is awesome! [Jython](http://www.jython.org/) is awesome! Together, they are very awesome!
55

66
This project uses Processing 2.0's mode infrastructure to create a seamless blend of Processing and python.
77

src/info/sansgills/mode/python/Communicator.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void destroy(){
5454
*/
5555
public void sendClose(){
5656
toSketch.println("__STOP__"); //hard-coded, what the hell
57-
System.out.println("__STOP__");
5857
toSketch.flush();
5958
}
6059

@@ -69,7 +68,6 @@ public void sendSketch(String[] args){
6968

7069

7170
toSketch.println(out.toString());
72-
System.out.println(out.toString());
7371
toSketch.flush();
7472
}
7573

@@ -91,18 +89,24 @@ public void run() {
9189
String currentLine;
9290

9391
// continually read messages
94-
while ((currentLine = messageReader.readLine()) != null && running) {
92+
while ((currentLine = messageReader.readLine()) != null) {
93+
if(!running){
94+
System.out.println("Messenger thread no longer running");
95+
return;
96+
}
9597
if (currentLine.indexOf("__STOPPED__") != -1) {
9698
// sketch telling us it stopped
9799
runner.parallelStopped();
100+
System.out.println("Stopped recieved.");
98101
return;
99102
}else if(currentLine.indexOf("__STARTED__") != -1){
100103
runner.parallelStarted();
104+
System.out.println("Started recieved.");
101105
} else{
102106
System.err.println(currentLine);
103107
}
104108
}
105-
} catch (Exception e) {}
109+
} catch (Exception e) {e.printStackTrace();}
106110
}
107111
}
108112
}

src/info/sansgills/mode/python/PythonBuild.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public class PythonBuild {
4141

4242
String classPath, pythonLibs, javaLibs;
4343

44+
boolean usesOpenGL;
45+
4446
//build tracking
4547
private int buildnumber;
4648
private static int buildstotal = 0;
@@ -88,13 +90,14 @@ public void build() throws Exception{
8890
}
8991

9092

93+
private static Pattern whitespace = Pattern.compile("\\s*");
9194
/*
9295
* Turn .pde into valid python
9396
*
9497
* Now with antlr!
9598
*/
9699
private String preprocess(StringBuilder program){
97-
if(Pattern.matches("\\s*", program)){
100+
if(whitespace.matcher(program).matches()){
98101
return "def setup():\n\tpass\n\n"; //empty sketch; TODO fix hack
99102
}
100103
try{
@@ -110,6 +113,8 @@ private String preprocess(StringBuilder program){
110113

111114
String[] libraries = converter.getLibraries();
112115

116+
usesOpenGL = converter.usesOpenGL;
117+
113118
Set<String> knownPythonLibraries = PythonMode.getPythonLibraries();
114119

115120
pythonLibs = "";

src/info/sansgills/mode/python/PythonEditor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public void actionPerformed(ActionEvent e) {
109109
handleStop();
110110
}
111111
});
112+
112113
return buildSketchMenu(new JMenuItem[] { runItem, presentItem, stopItem });
113114
}
114115

src/info/sansgills/mode/python/PythonRunner.java

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,17 @@ public class PythonRunner {
2626
// Threads to redirect output / error streams from process to us
2727
Communicator communicator;
2828

29-
boolean active;
29+
boolean dying, needsReboot;
3030

3131
public PythonRunner(PythonEditor editor) {
3232
this.editor = editor;
33-
active = false;
33+
dying = false;
34+
3435

3536
//make sure our partner process is dead
3637
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
3738
public void run() {
39+
dying = true; //don't try to start again
3840
if (communicator != null) {
3941
communicator.destroy();
4042
}
@@ -49,6 +51,8 @@ public void run() {
4951
* Run the code.
5052
*/
5153
public void launch(PythonBuild build, boolean present) {
54+
needsReboot = build.usesOpenGL;
55+
System.out.println("we need to reboot: "+needsReboot);
5256
ensureParallel();
5357
String[] a = buildSketchArgs(build, present);
5458
System.out.println(a);
@@ -61,36 +65,63 @@ public void launch(PythonBuild build, boolean present) {
6165
public void internalClose() {
6266
//Closed from editor button
6367
ensureParallel();
64-
System.out.println("Closing...");
6568
communicator.sendClose();
6669
}
6770

71+
72+
73+
/*
74+
* Sketch process died; get ready to reboot it
75+
*/
76+
private void prepareReboot(){
77+
System.out.println("rebooting");
78+
if(!dying){
79+
sketchProcess = null;
80+
communicator.destroy();
81+
communicator = null;
82+
}
83+
}
84+
85+
/*
86+
* Force sketch process to restart
87+
*/
88+
public void forceReboot(){
89+
System.out.println("Forcing reboot.");
90+
if(sketchProcess != null) sketchProcess.destroy();
91+
}
92+
93+
6894
/*
6995
* Make sure we've got a process to run the code.
7096
*/
7197
private void ensureParallel(){
7298
if(sketchProcess == null){
7399
sketchProcess = PApplet.exec(buildJavaArgs());
74100
communicator = new Communicator(sketchProcess, this);
101+
new Thread(new Runnable(){
102+
public void run(){
103+
try{
104+
int result = sketchProcess.waitFor();
105+
System.out.println("parallel process killed");
106+
prepareReboot();
107+
}catch(InterruptedException e){
108+
System.err.println("someone interrupted the deathwatch thread");
109+
}
110+
}
111+
}).start();
75112
}
76113
}
77114

78-
115+
/*
116+
* Handle talking to companion process
117+
*/
79118
public void parallelStopped() {
80-
//Closed from sketch window
81-
if(active){
82-
active = false;
83-
System.out.println("Closed");
84-
}else{
85-
System.err.println("something is wrong");
119+
if(needsReboot){
120+
forceReboot();
86121
}
87122
}
88123
public void parallelStarted(){
89-
if(!active){
90-
active = true;
91-
}else{
92-
System.err.println("something is very wrong");
93-
}
124+
94125
}
95126

96127
/*

src/info/sansgills/mode/python/preproc/PyPdeConverter.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class PyPdeConverter extends PyPdeBaseListener {
3333

3434
boolean hasIssues = false;
3535
boolean staticMode = true;
36+
public boolean usesOpenGL = false;
3637

3738
String result;
3839

@@ -65,15 +66,16 @@ public String[] getLibraries(){
6566
//fiddle with certain PApplet variables
6667
//TODO yell at the user when they try to assign to things
6768
@Override
68-
public void enterPrimaryAtom(PyPdeParser.PrimaryAtomContext ctx){
69-
PyPdeParser.IdentifierContext id = ctx.atom().identifier();
69+
public void enterAtom(PyPdeParser.AtomContext ctx){
70+
PyPdeParser.IdentifierContext id = ctx.identifier();
7071
if(id != null){
72+
String text = id.getText();
7173
//hopefully this will work without java string nonsense
72-
if(instanceVars.contains(id.getText())){
74+
if(instanceVars.contains(text)){
7375
//one of our instance variables;
7476
//replace mouseX with __applet__.mouseX, etc.
7577
rewriter.insertBefore(id.IDENTIFIER().getSymbol(), "__applet__."); //woo dirty hacks!
76-
}else if(wonkyVars.contains(id.getText())){
78+
}else if(wonkyVars.contains(text)){
7779
//one of the obnoxious variables with name conflicts
7880
//replace mousePressed with getmousePressed()
7981
//can't do __applet__.mousePressed Jython thinks that refers to the function
@@ -83,6 +85,21 @@ public void enterPrimaryAtom(PyPdeParser.PrimaryAtomContext ctx){
8385
}
8486
}
8587

88+
private static Pattern getArgs = Pattern.compile("OPENGL|P3D|P2D");
89+
@Override
90+
public void enterCall(PyPdeParser.CallContext ctx) {
91+
if (ctx.primary() instanceof PyPdeParser.PrimaryAtomContext) {
92+
System.out.println("found a funcall");
93+
if (ctx.primary().getText().equals("size")) {
94+
System.out.println("found size");
95+
if(getArgs.matcher(ctx.getText()).find()){
96+
usesOpenGL = true;
97+
}
98+
}
99+
}
100+
101+
}
102+
86103

87104
@Override
88105
public void enterComplexSuite(PyPdeParser.ComplexSuiteContext ctx){

src/info/sansgills/mode/python/wrapper/ProcessingJythonWrapper.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ public static void prepare() {
117117
ready = true;
118118
}
119119

120+
public static void sendStarted(){
121+
System.err.println("__STARTED__");
122+
System.out.println("started sent");
123+
}
124+
public static void sendStopped(){
125+
System.err.println("__STOPPED__");
126+
System.out.println("stopped sent");
127+
}
128+
129+
120130
public static void startSketch(String[] args){
121131
if(applet != null){
122132
applet.exit();
@@ -139,7 +149,6 @@ public static void startSketch(String[] args){
139149
//we're good to go, hopefully
140150

141151
runSketch(args);
142-
143152
}
144153

145154

@@ -200,6 +209,7 @@ public static void terminateSketch(){
200209
public static void sketchDisposed(){
201210
scrubContext();
202211
applet = null; //all done
212+
sendStopped(); //signal the mothership
203213
}
204214

205215

@@ -283,7 +293,6 @@ public static void runSketch(final String args[]) {
283293
scriptPath = value;
284294
} else if (param.equals("--pylibs")) {
285295
if (value != null) {
286-
System.out.println(sys.path);
287296
for (String lib : value.split(separator)) {
288297
if (!pythonLibraries.contains(lib)) {
289298
pythonLibraries.add(lib);
@@ -294,7 +303,6 @@ public static void runSketch(final String args[]) {
294303
} else if (param.equals("--javalibs")) {
295304
if (value != null) {
296305
for (String lib : value.split(separator)) {
297-
System.out.println(sys.path);
298306
if (!javaLibraries.contains(lib)) {
299307
javaLibraries.add(lib);
300308
sys.path.append(new PyString(lib));
@@ -522,6 +530,7 @@ public void mousePressed(java.awt.event.MouseEvent e) {
522530
frame.setVisible(true);
523531
}
524532
}
533+
sendStarted(); //signal the mothership
525534
}
526535

527536
/*

src/info/sansgills/mode/python/wrapper/PythonPApplet.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,14 @@ public float getframeRate(){
169169
@Override
170170
public void dispose(){
171171
if (!disposed) {
172+
System.out.println("disposing sketch");
172173
disposed = true;
173174
super.dispose();
174175
frame.dispose();
175176

176177
ProcessingJythonWrapper.sketchDisposed();
178+
179+
System.out.println("done disposing");
177180
}
178181
}
179182

0 commit comments

Comments
 (0)