1
1
package info .sansgills .mode .python .wrapper ;
2
2
3
+ import info .sansgills .mode .python .PythonEditor ;
4
+
3
5
import java .awt .GraphicsDevice ;
6
+ import java .awt .Point ;
7
+ import java .io .BufferedReader ;
8
+ import java .io .InputStream ;
9
+ import java .io .InputStreamReader ;
4
10
import java .util .Arrays ;
5
11
import java .util .Scanner ;
6
12
7
13
import org .python .core .*;
8
14
import org .python .util .InteractiveConsole ;
9
15
16
+ import processing .core .PApplet ;
17
+
10
18
/**
11
19
*
12
20
* Class to handle running jython sketches. Packaged separately from the main mode for easy export.
@@ -39,22 +47,38 @@ public class ProcessingJythonWrapper {
39
47
"mouseReleased" , "mouseClicked" , "mouseWheel" , "mouseMoved" ,
40
48
"mouseDragged" , "keyPressed" , "keyReleased" , "keyTyped" };
41
49
50
+ static MessageReceiverThread receiver ;
51
+
42
52
static InteractiveConsole interp ; // python interpreter to feed things to
43
53
static PySystemState sys ; // for modifying python classpath, as yet
44
54
static PythonPApplet constructedApplet ; // Applet we pull from the interpreter
45
55
46
56
static final String objname = "__applet__" ;
47
57
58
+ static boolean parallel ;
59
+
48
60
/**
49
61
*
50
- * First argument is the path to the script; the rest are things to pass to PApplet.
62
+ * First argument is the path to the script; the rest are things to pass to
63
+ * PApplet.
51
64
*
52
65
*/
53
66
public static void main (String [] args ) {
54
- String scriptPath = args [0 ];
55
- String [] params = Arrays .copyOfRange (args , 1 , args .length );
56
- prepare ();
57
- run (scriptPath , params );
67
+ if (args [0 ].indexOf ("__PARALLEL__" ) != -1 ) {
68
+ parallel = true ;
69
+ receiver = new MessageReceiverThread (System .in );
70
+ receiver .start ();
71
+ // running from PDE
72
+ } else {
73
+ parallel = false ;
74
+ String scriptPath = args [0 ];
75
+ String [] params = Arrays .copyOfRange (args , 1 , args .length );
76
+ receiver = new MessageReceiverThread (System .in );
77
+ receiver .start ();
78
+
79
+ prepare ();
80
+ run (scriptPath , params );
81
+ }
58
82
}
59
83
60
84
public static void prepare () {
@@ -68,7 +92,21 @@ public static void prepare() {
68
92
PySystemState .add_package ("processing.opengl" );
69
93
}
70
94
71
-
95
+ /*
96
+ * Called by PythonPApplet when it's exiting
97
+ * Get rid of all references to our applet
98
+ *
99
+ * ...this tangle of callbacks probably makes me a bad person
100
+ */
101
+ static void sketchExiting (){
102
+ if (parallel ) {
103
+ scrub ();
104
+ constructedApplet = null ;
105
+ sys = null ;
106
+ } else {
107
+ System .exit (0 );
108
+ }
109
+ }
72
110
73
111
/*
74
112
* Run.
@@ -95,27 +133,57 @@ public static void run(String scriptPath, String[] params){
95
133
for (String name : sketchFunctions ){
96
134
PyFunction function = (PyFunction ) interp .get (name );
97
135
if (function != null ){
98
- System .out .println ("found " +name );
99
136
constructedApplet .inject (name , function );
100
137
}
101
138
}
102
139
103
140
//run the sketch!
104
- PythonPApplet .runSketch (params , constructedApplet );
141
+ PythonPApplet .runSketchOpen (params , constructedApplet );
105
142
106
143
} catch (Exception e ){
107
144
System .err .println ("Error running sketch: " + e .getMessage ()); //TODO
108
145
e .printStackTrace ();
109
- System .exit (1 );
146
+ if (constructedApplet != null ){
147
+ constructedApplet .exit ();
148
+ }
110
149
}
111
150
}
112
151
113
152
public static void scrub (){
114
153
interp .exec (scrub );
115
154
}
116
155
117
- public static void runSketch (String [] params , PythonPApplet constructedApplet ){
118
- GraphicsDevice displayDevice ;
156
+
157
+
158
+ /*
159
+ * Class to handle receiving messages from the PDE
160
+ * not to be confused with Communicator.MessageReceiverThread; same idea, different location
161
+ */
162
+ private static class MessageReceiverThread extends Thread {
163
+ BufferedReader messageReader ;
164
+ public boolean running ;
119
165
166
+ public MessageReceiverThread (InputStream messageStream ){
167
+ this .messageReader = new BufferedReader (new InputStreamReader (messageStream ));
168
+ this .running = true ;
169
+ }
170
+
171
+ public void run () {
172
+ try {
173
+ String currentLine ;
174
+
175
+ // continually read messages
176
+ while ((currentLine = messageReader .readLine ()) != null && running ) {
177
+ try {
178
+ if (currentLine .indexOf ("__STOP__" ) != -1 ) {
179
+ // PDE is telling us to kill the applet
180
+ if (constructedApplet != null ) {
181
+ constructedApplet .exit ();
182
+ }
183
+ }
184
+ } catch (Exception e ) {}
185
+ }
186
+ } catch (Exception e ) {}
187
+ }
120
188
}
121
189
}
0 commit comments