Debian Codegraph @

src/gnu/io/LPRPort.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*-------------------------------------------------------------------------
2 | RXTX License v 2.1 - LGPL v 2.1 + Linking Over Controlled Interface.
3 | RXTX is a native interface to serial ports in java.
4 | Copyright 1997-2007 by Trent Jarvi [email protected] and others who
5 | actually wrote it. See individual source files for more information.
6 |
7 | A copy of the LGPL v 2.1 may be found at
8 | http://www.gnu.org/licenses/lgpl.txt on March 4th 2007. A copy is
9 | here for your convenience.
10 |
11 | This library is free software; you can redistribute it and/or
12 | modify it under the terms of the GNU Lesser General Public
13 | License as published by the Free Software Foundation; either
14 | version 2.1 of the License, or (at your option) any later version.
15 |
16 | This library is distributed in the hope that it will be useful,
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 | Lesser General Public License for more details.
20 |
21 | An executable that contains no derivative of any portion of RXTX, but
22 | is designed to work with RXTX by being dynamically linked with it,
23 | is considered a "work that uses the Library" subject to the terms and
24 | conditions of the GNU Lesser General Public License.
25 |
26 | The following has been added to the RXTX License to remove
27 | any confusion about linking to RXTX. We want to allow in part what
28 | section 5, paragraph 2 of the LGPL does not permit in the special
29 | case of linking over a controlled interface. The intent is to add a
30 | Java Specification Request or standards body defined interface in the
31 | future as another exception but one is not currently available.
32 |
33 | http://www.fsf.org/licenses/gpl-faq.html#LinkingOverControlledInterface
34 |
35 | As a special exception, the copyright holders of RXTX give you
36 | permission to link RXTX with independent modules that communicate with
37 | RXTX solely through the Sun Microsytems CommAPI interface version 2,
38 | regardless of the license terms of these independent modules, and to copy
39 | and distribute the resulting combined work under terms of your choice,
40 | provided that every copy of the combined work is accompanied by a complete
41 | copy of the source code of RXTX (the version of RXTX used to produce the
42 | combined work), being distributed under the terms of the GNU Lesser General
43 | Public License plus this exception. An independent module is a
44 | module which is not derived from or based on RXTX.
45 |
46 | Note that people who make modified versions of RXTX are not obligated
47 | to grant this special exception for their modified versions; it is
48 | their choice whether to do so. The GNU Lesser General Public License
49 | gives permission to release a modified version without this exception; this
50 | exception also makes it possible to release a modified version which
51 | carries forward this exception.
52 |
53 | You should have received a copy of the GNU Lesser General Public
54 | License along with this library; if not, write to the Free
55 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
56 | All trademarks belong to their respective owners.
57 --------------------------------------------------------------------------*/
58 package gnu.io;
59
60 import java.io.InputStream;
61 import java.io.OutputStream;
62 import java.io.IOException;
63 import java.util.TooManyListenersException;
64 import java.lang.Math;
65
66 /**
67 * LPRPort
68 */
69
70 final class LPRPort extends ParallelPort
71 {
72
73 static {
74 System.loadLibrary( "rxtxParallel" );
75 Initialize();
76 }
77
78 /** Initialize the native library */
79 private native static void Initialize();
80 private final static boolean debug = false;
81
82 /** Open the named port */
83 public LPRPort( String name ) throws PortInUseException
84 {
85 if (debug) System.out.println("LPRPort:LPRPort("+name+")");
86 /*
87 commapi/javadocs/API_users_guide.html specifies that whenever
88 an application tries to open a port in use by another application
89 the PortInUseException will be thrown
90
91 I know some didnt like it this way but I'm not sure how to avoid
92 it. We will just be writing to a bogus fd if we catch the
93 exeption
94
95 Trent
96 */
97 // try {
98 fd = open( name );
99 this.name = name;
100 // } catch ( PortInUseException e ){}
101 if (debug)
102 System.out.println("LPRPort:LPRPort("+name+") fd = " +
103 fd);
104 }
105 private synchronized native int open( String name )
106 throws PortInUseException;
107
108 /** File descriptor */
109 private int fd;
110
111 /** Output stream */
112 private final ParallelOutputStream out = new ParallelOutputStream();
113 public OutputStream getOutputStream() { return out; }
114
115 /** Input stream */
116 private final ParallelInputStream in = new ParallelInputStream();
117 public InputStream getInputStream() { return in; }
118
119 /** return current mode LPT_MODE_SPP, LPT_MODE_PS2, LPT_MODE_EPP,
120 or LPT_MODE_ECP */
121 private int lprmode=LPT_MODE_ANY;
122 public int getMode() { return lprmode; }
123 public int setMode(int mode) throws UnsupportedCommOperationException
124 {
125 try {
126 setLPRMode(mode);
127 } catch(UnsupportedCommOperationException e) {
128 e.printStackTrace();
129 return -1;
130 }
131 lprmode = mode;
132 return(0);
133 }
134 public void restart()
135 {
136 System.out.println("restart() is not implemented");
137 }
138 public void suspend()
139 {
140 System.out.println("suspend() is not implemented");
141 }
142
143 public native boolean setLPRMode(int mode)
144 throws UnsupportedCommOperationException;
145 public native boolean isPaperOut();
146 public native boolean isPrinterBusy();
147 public native boolean isPrinterError();
148 public native boolean isPrinterSelected();
149 public native boolean isPrinterTimedOut();
150
151 /** Close the port */
152 private native void nativeClose();
153 public synchronized void close()
154 {
155 if( fd < 0 ) return;
156 nativeClose();
157 super.close();
158 removeEventListener();
159
160 fd = 0;
161 Runtime.getRuntime().gc();
162 }
163 /** Receive framing control */
164 public void enableReceiveFraming( int f )
165 throws UnsupportedCommOperationException
166 {
167 throw new UnsupportedCommOperationException( "Not supported" );
168 }
169 public void disableReceiveFraming() {}
170 public boolean isReceiveFramingEnabled() { return false; }
171 public int getReceiveFramingByte() { return 0; }
172
173 /** Receive timeout control */
174 private int timeout = 0;
175 public void enableReceiveTimeout( int t )
176 {
177 if( t > 0 ) timeout = t;
178 else timeout = 0;
179 }
180 public void disableReceiveTimeout() { timeout = 0; }
181 public boolean isReceiveTimeoutEnabled() { return timeout > 0; }
182 public int getReceiveTimeout() { return timeout; }
183
184 /** Receive threshold control */
185 private int threshold = 1;
186 public void enableReceiveThreshold( int t )
187 {
188 if( t > 1 ) threshold = t;
189 else threshold = 1;
190 }
191 public void disableReceiveThreshold() { threshold = 1; }
192 public int getReceiveThreshold() { return threshold; }
193 public boolean isReceiveThresholdEnabled() { return threshold > 1; };
194
195 /**
196 Input/output buffers
197 These are native stubs...
198 */
199
200 public native void setInputBufferSize( int size );
201 public native int getInputBufferSize();
202 public native void setOutputBufferSize( int size );
203 public native int getOutputBufferSize();
204
205 public native int getOutputBufferFree();
206 /** Write to the port */
207 protected native void writeByte( int b ) throws IOException;
208 protected native void writeArray( byte b[], int off, int len )
209 throws IOException;
210 protected native void drain() throws IOException;
211
212 /** LPRPort read methods */
213 protected native int nativeavailable() throws IOException;
214 protected native int readByte() throws IOException;
215 protected native int readArray( byte b[], int off, int len )
216 throws IOException;
217
218 /** Parallel Port Event listener */
219 private ParallelPortEventListener PPEventListener;
220
221 /** Thread to monitor data */
222 private MonitorThread monThread;
223
224 /** Process ParallelPortEvents */
225 native void eventLoop();
226
227 public boolean checkMonitorThread()
228 {
229 if(monThread != null)
230 return monThread.isInterrupted();
231 return(true);
232 }
233
234 public synchronized boolean sendEvent( int event, boolean state )
235 {
236 /* Let the native side know its time to die */
237
238 if ( fd == 0 || PPEventListener == null || monThread == null )
239 {
240 return(true);
241 }
242
243 switch( event )
244 {
245 case ParallelPortEvent.PAR_EV_BUFFER:
246 if( monThread.monBuffer ) break;
247 return(false);
248 case ParallelPortEvent.PAR_EV_ERROR:
249 if( monThread.monError ) break;
250 return(false);
251 default:
252 System.err.println("unknown event:"+event);
253 return(false);
254 }
255 ParallelPortEvent e = new ParallelPortEvent(this, event, !state,
256 state );
257 if( PPEventListener != null )
258 PPEventListener.parallelEvent( e );
259 if ( fd == 0 || PPEventListener == null || monThread == null )
260 {
261 return(true);
262 }
263 else
264 {
265 try{Thread.sleep(50);} catch(Exception exc){}
266 return(false);
267 }
268 }
269
270 /** Add an event listener */
271 public synchronized void addEventListener(
272 ParallelPortEventListener lsnr )
273 throws TooManyListenersException
274 {
275 if( PPEventListener != null )
276 throw new TooManyListenersException();
277 PPEventListener = lsnr;
278 monThread = new MonitorThread();
279 monThread.start();
280 }
281
282 /** Remove the parallel port event listener */
283 public synchronized void removeEventListener()
284 {
285 PPEventListener = null;
286 if( monThread != null )
287 {
288 monThread.interrupt();
289 monThread = null;
290 }
291 }
292
293 /** Note: these have to be separate boolean flags because the
294 ParallelPortEvent constants are NOT bit-flags, they are just
295 defined as integers from 1 to 10 -DPL */
296 public synchronized void notifyOnError( boolean enable )
297 {
298 System.out.println("notifyOnError is not implemented yet");
299 monThread.monError = enable;
300 }
301 public synchronized void notifyOnBuffer( boolean enable )
302 {
303 System.out.println("notifyOnBuffer is not implemented yet");
304 monThread.monBuffer = enable;
305 }
306
307
308 /** Finalize the port */
309 protected void finalize()
310 {
311 if ( fd > 0 ) close();
312 }
313
314 /** Inner class for ParallelOutputStream */
315 class ParallelOutputStream extends OutputStream
316 {
317 public synchronized void write( int b ) throws IOException
318 {
319 if ( fd == 0 ) throw new IOException();
320 writeByte( b );
321 }
322 public synchronized void write( byte b[] ) throws IOException
323 {
324 if ( fd == 0 ) throw new IOException();
325 writeArray( b, 0, b.length );
326 }
327 public synchronized void write( byte b[], int off, int len )
328 throws IOException
329 {
330 if ( fd == 0 ) throw new IOException();
331 writeArray( b, off, len );
332 }
333 public synchronized void flush() throws IOException
334 {
335 if ( fd == 0 ) throw new IOException();
336 //drain();
337 }
338 }
339
340 /** Inner class for ParallelInputStream */
341 class ParallelInputStream extends InputStream
342 {
343 public int read() throws IOException
344 {
345 if ( fd == 0 ) throw new IOException();
346 return readByte();
347 }
348 public int read( byte b[] ) throws IOException
349 {
350 if ( fd == 0 ) throw new IOException();
351 return readArray( b, 0, b.length );
352 }
353 public int read( byte b[], int off, int len )
354 throws IOException
355 {
356 if ( fd == 0 ) throw new IOException();
357 return readArray( b, off, len );
358 }
359 public int available() throws IOException
360 {
361 if ( fd == 0 ) throw new IOException();
362 return nativeavailable();
363 }
364 }
365 class MonitorThread extends Thread
366 {
367 private boolean monError = false;
368 private boolean monBuffer = false;
•369 MonitorThread() {
370 setDaemon(true);
371 }
372 public void run()
373 {
374 eventLoop();
375 yield();
376 }
377 }
378 }