| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
final class LPRPort extends ParallelPort |
| 71 |
{ |
| 72 |
|
| 73 |
static { |
| 74 |
System.loadLibrary( "rxtxParallel" ); |
| 75 |
Initialize(); |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
private native static void Initialize(); |
| 80 |
private final static boolean debug = false; |
| 81 |
|
| 82 |
|
| 83 |
public LPRPort( String name ) throws PortInUseException |
| 84 |
{ |
| 85 |
if (debug) System.out.println("LPRPort:LPRPort("+name+")"); |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
fd = open( name ); |
| 99 |
this.name = name; |
| 100 |
|
| 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 |
|
| 109 |
private int fd; |
| 110 |
|
| 111 |
|
| 112 |
private final ParallelOutputStream out = new ParallelOutputStream(); |
| 113 |
public OutputStream getOutputStream() { return out; } |
| 114 |
|
| 115 |
|
| 116 |
private final ParallelInputStream in = new ParallelInputStream(); |
| 117 |
public InputStream getInputStream() { return in; } |
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 197 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 219 |
private ParallelPortEventListener PPEventListener; |
| 220 |
|
| 221 |
|
| 222 |
private MonitorThread monThread; |
| 223 |
|
| 224 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 283 |
public synchronized void removeEventListener() |
| 284 |
{ |
| 285 |
PPEventListener = null; |
| 286 |
if( monThread != null ) |
| 287 |
{ |
| 288 |
monThread.interrupt(); |
| 289 |
monThread = null; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 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 |
|
| 309 |
protected void finalize() |
| 310 |
{ |
| 311 |
if ( fd > 0 ) close(); |
| 312 |
} |
| 313 |
|
| 314 |
|
| 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 |
|
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 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 |
} |