| 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 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#if defined(__MWERKS__) |
| 71 |
#include "LPRPort.h" |
| 72 |
#else |
| 73 |
#ifndef WIN32 |
| 74 |
# include "config.h" |
| 75 |
#endif |
| 76 |
|
| 77 |
|
| 78 |
#include "gnu_io_LPRPort.h" |
| 79 |
#endif |
| 80 |
#include <time.h> |
| 81 |
#include <stdio.h> |
| 82 |
#include <string.h> |
| 83 |
#ifndef WIN32 |
| 84 |
# include <unistd.h> |
| 85 |
# include <sys/ioctl.h> |
| 86 |
# include <sys/errno.h> |
| 87 |
# include <sys/param.h> |
| 88 |
#else |
| 89 |
# include <errno.h> |
| 90 |
# include "win32termios.h" |
| 91 |
#endif |
| 92 |
#include <sys/types.h> |
| 93 |
#include <sys/stat.h> |
| 94 |
#include <stdlib.h> |
| 95 |
|
| 96 |
#ifdef HAVE_TERMIOS_H |
| 97 |
# include <termios.h> |
| 98 |
#endif |
| 99 |
#ifdef HAVE_SYS_FCNTL_H |
| 100 |
# include <sys/fcntl.h> |
| 101 |
#endif |
| 102 |
#ifdef HAVE_SYS_FILE_H |
| 103 |
# include <sys/file.h> |
| 104 |
#endif |
| 105 |
#ifdef HAVE_SYS_SIGNAL_H |
| 106 |
# include <sys/signal.h> |
| 107 |
#endif |
| 108 |
#ifdef HAVE_SYS_TIME_H |
| 109 |
# include <sys/time.h> |
| 110 |
#endif |
| 111 |
#if defined(__linux__) |
| 112 |
# include <linux/lp.h> |
| 113 |
#endif |
| •114 |
#if defined(__FreeBSD_kernel__) |
| 115 |
|
| 116 |
# include <dev/ppbus/lpt.h> |
| 117 |
#endif |
| 118 |
#ifdef __unixware__ |
| 119 |
# include <sys/filio.h> |
| 120 |
#endif |
| 121 |
|
| 122 |
extern int errno; |
| 123 |
|
| 124 |
#include "ParallelImp.h" |
| 125 |
|
| 126 |
#define LPRPort(foo) Java_gnu_io_LPRPort_ ## foo |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
JNIEXPORT jint JNICALL LPRPort(getOutputBufferFree)(JNIEnv *env, |
| 138 |
jclass jclazz) |
| 139 |
{ |
| 140 |
printf("getOutputBufferFree is not implemented yet\n"); |
| 141 |
|
| 142 |
return(0); |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
JNIEXPORT jboolean JNICALL LPRPort(setLPRMode)(JNIEnv *env, |
| 161 |
jclass jclazz, jint mode) |
| 162 |
{ |
| 163 |
switch(mode) |
| 164 |
{ |
| 165 |
case LPT_MODE_ANY: |
| 166 |
break; |
| 167 |
case LPT_MODE_SPP: |
| 168 |
case LPT_MODE_PS2: |
| 169 |
case LPT_MODE_EPP: |
| 170 |
case LPT_MODE_ECP: |
| 171 |
case LPT_MODE_NIBBLE: |
| 172 |
default: |
| 173 |
throw_java_exception( env, UNSUPPORTED_COMM_OPERATION, |
| 174 |
"nativeSetSerialPortParams", |
| 175 |
"setLPRMode was unable to proced the requested \ |
| 176 |
mode" |
| 177 |
); |
| 178 |
} |
| 179 |
return(JNI_TRUE); |
| 180 |
} |
| 181 |
|
| 182 |
#if defined (WIN32) |
| 183 |
|
| 184 |
#define CTL_CODE( DeviceType, Function, Method, Access ) (((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method)) |
| 185 |
|
| 186 |
#define FILE_DEVICE_PARALLEL_PORT 0x00000016 |
| 187 |
#define METHOD_BUFFERED 0 |
| 188 |
#define FILE_ANY_ACCESS 0 |
| 189 |
|
| 190 |
#define IOCTL_PAR_QUERY_INFORMATION CTL_CODE(FILE_DEVICE_PARALLEL_PORT,1,METHOD_BUFFERED,FILE_ANY_ACCESS) |
| 191 |
|
| 192 |
#define PARALLEL_INIT 0x1 |
| 193 |
#define PARALLEL_AUTOFEED 0x2 |
| 194 |
#define PARALLEL_PAPER_EMPTY 0x4 |
| 195 |
#define PARALLEL_OFF_LINE 0x8 |
| 196 |
#define PARALLEL_POWER_OFF 0x10 |
| 197 |
#define PARALLEL_NOT_CONNECTED 0x20 |
| 198 |
#define PARALLEL_BUSY 0x40 |
| 199 |
#define PARALLEL_SELECTED 0x80 |
| 200 |
|
| 201 |
int getWin32ParallelStatusFlags(int fd){ |
| 202 |
int status = 0; |
| 203 |
DWORD count; |
| 204 |
if(!DeviceIoControl( (HANDLE)fd, IOCTL_PAR_QUERY_INFORMATION, NULL,0, &status, sizeof(status), &count, 0) && count == 1){ |
| 205 |
YACK(); |
| 206 |
return 0; |
| 207 |
} |
| 208 |
#ifdef DEBUG |
| 209 |
printf("getWin32ParallelStatusFlags: %d\n", status ); |
| 210 |
#endif |
| 211 |
return status; |
| 212 |
} |
| 213 |
|
| 214 |
jboolean getWin32ParallelStatus(int fd, int flag){ |
| 215 |
return( (getWin32ParallelStatusFlags(fd) & flag) ? JNI_TRUE : JNI_FALSE); |
| 216 |
} |
| 217 |
#endif |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
JNIEXPORT jboolean JNICALL LPRPort(isPaperOut)(JNIEnv *env, |
| 228 |
jobject jobj) |
| 229 |
{ |
| 230 |
|
| 231 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 232 |
#if defined (__linux__) |
| 233 |
int status; |
| 234 |
ioctl(fd, LPGETSTATUS,&status); |
| 235 |
return( status & LP_NOPA ? JNI_TRUE : JNI_FALSE ); |
| 236 |
#elif defined (WIN32) |
| 237 |
return getWin32ParallelStatus( fd, PARALLEL_PAPER_EMPTY); |
| 238 |
#else |
| 239 |
|
| 240 |
printf("ParallelImp.c LPGETSTATUS not defined\n"); |
| 241 |
return(JNI_TRUE); |
| 242 |
#endif |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
JNIEXPORT jboolean JNICALL LPRPort(isPrinterBusy)(JNIEnv *env, |
| 253 |
jobject jobj) |
| 254 |
{ |
| 255 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 256 |
int status; |
| 257 |
#if defined (__linux__) |
| 258 |
ioctl(fd, LPGETSTATUS, &status); |
| 259 |
#elif defined (WIN32) |
| 260 |
return getWin32ParallelStatus( fd, PARALLEL_BUSY); |
| 261 |
#else |
| 262 |
|
| 263 |
printf("ParallelImp.c LPGETSTATUS not defined\n"); |
| 264 |
#endif |
| 265 |
#if defined(__linux__) |
| 266 |
return( status & LP_BUSY ? JNI_TRUE : JNI_FALSE ); |
| 267 |
#endif |
| •268 |
#if defined(__FreeBSD_kernel__) |
| 269 |
return( status & EBUSY ? JNI_TRUE : JNI_FALSE ); |
| 270 |
#endif |
| 271 |
return(JNI_FALSE); |
| 272 |
} |
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
JNIEXPORT jboolean JNICALL LPRPort(isPrinterError)(JNIEnv *env, |
| 282 |
jobject jobj) |
| 283 |
{ |
| 284 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 285 |
#if defined (__linux__) |
| 286 |
int status; |
| 287 |
ioctl(fd, LPGETSTATUS, &status); |
| 288 |
return( status & LP_ERR ? JNI_TRUE : JNI_FALSE ); |
| 289 |
#elif defined (WIN32) |
| 290 |
return getWin32ParallelStatus( fd, PARALLEL_PAPER_EMPTY | |
| 291 |
PARALLEL_OFF_LINE | |
| 292 |
PARALLEL_POWER_OFF | |
| 293 |
PARALLEL_NOT_CONNECTED | |
| 294 |
PARALLEL_BUSY); |
| 295 |
#else |
| 296 |
|
| 297 |
printf("ParallelImp.c LPGETSTATUS not defined\n"); |
| 298 |
return(JNI_FALSE); |
| 299 |
#endif |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
JNIEXPORT jboolean JNICALL LPRPort(isPrinterSelected)(JNIEnv *env, |
| 310 |
jobject jobj) |
| 311 |
{ |
| 312 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 313 |
#if defined (__linux__) |
| 314 |
int status; |
| 315 |
ioctl(fd, LPGETSTATUS, &status); |
| 316 |
return( status & LP_SELEC ? JNI_TRUE : JNI_FALSE ); |
| 317 |
#elif defined (WIN32) |
| 318 |
return getWin32ParallelStatus( fd, PARALLEL_SELECTED); |
| 319 |
#else |
| 320 |
|
| 321 |
printf("ParallelImp.c LPGETSTATUS not defined\n"); |
| 322 |
return(JNI_FALSE); |
| 323 |
#endif |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
JNIEXPORT jboolean JNICALL LPRPort(isPrinterTimedOut)(JNIEnv *env, |
| 336 |
jobject jobj) |
| 337 |
{ |
| 338 |
#if defined(__linux__) |
| 339 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 340 |
int status; |
| 341 |
ioctl(fd, LPGETSTATUS, &status); |
| 342 |
return( status & LP_BUSY ? JNI_TRUE : JNI_FALSE ); |
| 343 |
#endif |
| •344 |
#if defined(__FreeBSD_kernel__) |
| 345 |
printf("ParallelImp.c LPGETSTATUS not defined\n"); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
#endif |
| 350 |
return( JNI_FALSE ); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
JNIEXPORT void JNICALL LPRPort(Initialize)( JNIEnv *env, |
| 363 |
jclass jclazz ) |
| 364 |
{ |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
#if !defined(WIN32) |
| 370 |
struct sigaction handler; |
| 371 |
sigaction( SIGIO, NULL, &handler ); |
| 372 |
if( !handler.sa_handler ) signal( SIGIO, SIG_IGN ); |
| 373 |
#endif |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
JNIEXPORT jint JNICALL LPRPort(open)( JNIEnv *env, jobject jobj, |
| 390 |
jstring jstr ) |
| 391 |
{ |
| 392 |
|
| 393 |
const char *filename = (*env)->GetStringUTFChars( env, jstr, 0 ); |
| 394 |
#ifdef WIN32 |
| 395 |
int fd = (int)CreateFile( filename, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0 ); |
| 396 |
#else |
| 397 |
int fd = open( filename, O_RDWR | O_NONBLOCK ); |
| 398 |
#endif |
| 399 |
(*env)->ReleaseStringUTFChars( env, jstr, NULL ); |
| 400 |
if( fd < 0 ) goto fail; |
| 401 |
return (jint)fd; |
| 402 |
|
| 403 |
fail: |
| 404 |
throw_java_exception_system_msg( env, PORT_IN_USE_EXCEPTION, "open" ); |
| 405 |
return -1; |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
JNIEXPORT void JNICALL LPRPort(nativeClose)( JNIEnv *env, |
| 418 |
jobject jobj ) |
| 419 |
{ |
| 420 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 421 |
|
| 422 |
#ifdef WIN32 |
| 423 |
CloseHandle( (HANDLE)fd ); |
| 424 |
#else |
| 425 |
close( fd ); |
| 426 |
#endif |
| 427 |
return; |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
JNIEXPORT void JNICALL LPRPort(writeByte)( JNIEnv *env, |
| 439 |
jobject jobj, jint ji ) |
| 440 |
{ |
| 441 |
unsigned char byte = (unsigned char)ji; |
| 442 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 443 |
|
| 444 |
#ifdef WIN32 |
| 445 |
DWORD countWritten; |
| 446 |
if( WriteFile( (HANDLE)fd, &byte, sizeof( unsigned char ), &countWritten, NULL ) < 0 ) return; |
| 447 |
#else |
| 448 |
if( write( fd, &byte, sizeof( unsigned char ) ) >= 0 ) return; |
| 449 |
#endif |
| 450 |
throw_java_exception_system_msg( env, IO_EXCEPTION, "writeByte" ); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
JNIEXPORT void JNICALL LPRPort(writeArray)( JNIEnv *env, |
| 465 |
jobject jobj, jbyteArray jbarray, jint offset, jint count ) |
| 466 |
{ |
| 467 |
#ifdef WIN32 |
| 468 |
DWORD countWritten; |
| 469 |
COMMTIMEOUTS timeouts; |
| 470 |
int errorCount = 0; |
| 471 |
#endif |
| 472 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 473 |
jbyte *body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 474 |
unsigned char *bytes = (unsigned char *)malloc( count ); |
| 475 |
int i; |
| 476 |
for( i = 0; i < count; i++ ) bytes[ i ] = body[ i + offset ]; |
| 477 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 478 |
#ifdef WIN32 |
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
GetCommTimeouts( (HANDLE)fd, &timeouts ); |
| 485 |
timeouts.WriteTotalTimeoutMultiplier = 0; |
| 486 |
timeouts.WriteTotalTimeoutConstant = 2000; |
| 487 |
SetCommTimeouts( (HANDLE)fd, &timeouts ); |
| 488 |
while( count > 0 ){ |
| 489 |
if(!WriteFile( (HANDLE)fd, bytes, count, &countWritten, NULL ) && countWritten == 0){ |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
if(errorCount++ < 20){ |
| 495 |
Sleep( 20 ); |
| 496 |
continue; |
| 497 |
} |
| 498 |
throw_java_exception_system_msg( env, IO_EXCEPTION, "writeArray" ); |
| 499 |
break; |
| 500 |
} |
| 501 |
errorCount = 0; |
| 502 |
bytes += countWritten; |
| 503 |
count -= countWritten; |
| 504 |
} |
| 505 |
#else |
| 506 |
if( write( fd, bytes, count ) < 0 ) |
| 507 |
throw_java_exception_system_msg( env, IO_EXCEPTION, "writeArray" ); |
| 508 |
#endif |
| 509 |
free( bytes ); |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
int read_byte_array( int fd, unsigned char *buffer, int length, int threshold, |
| 530 |
int timeout ) |
| 531 |
{ |
| 532 |
int ret, left, bytes = 0; |
| 533 |
fd_set rfds; |
| 534 |
struct timeval sleep; |
| 535 |
|
| 536 |
FD_ZERO( &rfds ); |
| 537 |
FD_SET( fd, &rfds ); |
| 538 |
sleep.tv_sec = timeout / 1000; |
| 539 |
sleep.tv_usec = 1000 * ( timeout % 1000 ); |
| 540 |
left = length; |
| 541 |
|
| 542 |
while( bytes < length && bytes < threshold ) |
| 543 |
{ |
| 544 |
if( timeout > 0 ) |
| 545 |
{ |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
do { |
| 551 |
ret=select( fd + 1, &rfds, NULL, NULL, &sleep ); |
| 552 |
} while(ret < 0 && errno ==EINTR); |
| 553 |
if( ret == 0 ) break; |
| 554 |
if( ret < 0 ) return -1; |
| 555 |
} |
| 556 |
#if defined(WIN32) |
| 557 |
if(!ReadFile( (HANDLE)fd, buffer + bytes, left, (DWORD *)&ret, NULL )){ |
| 558 |
YACK(); |
| 559 |
ret = -1; |
| 560 |
} |
| 561 |
#else |
| 562 |
ret = read( fd, buffer + bytes, left ); |
| 563 |
#endif |
| 564 |
if( ret == 0 ) break; |
| 565 |
if( ret < 0 ) return -1; |
| 566 |
bytes += ret; |
| 567 |
left -= ret; |
| 568 |
} |
| 569 |
return bytes; |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
JNIEXPORT jint JNICALL LPRPort(readByte)( JNIEnv *env, |
| 582 |
jobject jobj ) |
| 583 |
{ |
| 584 |
int bytes, fd, timeout; |
| 585 |
unsigned char buffer[ 1 ]; |
| 586 |
|
| 587 |
fd = get_java_var( env, jobj,"fd","I" ); |
| 588 |
timeout = get_java_var( env, jobj, "timeout","I"); |
| 589 |
|
| 590 |
bytes = read_byte_array( fd, buffer, 1, 1, timeout ); |
| 591 |
if( bytes < 0 ) |
| 592 |
{ |
| 593 |
throw_java_exception_system_msg( env, IO_EXCEPTION, "readByte" ); |
| 594 |
|
| 595 |
return -1; |
| 596 |
} |
| 597 |
return (bytes ? (jint)buffer[ 0 ] : -1); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
JNIEXPORT jint JNICALL LPRPort(readArray)( JNIEnv *env, |
| 612 |
jobject jobj, jbyteArray jbarray, jint offset, jint length ) |
| 613 |
{ |
| 614 |
int bytes, i, fd, threshold, timeout; |
| 615 |
jbyte *body; |
| 616 |
unsigned char *buffer; |
| 617 |
fd = get_java_var( env, jobj,"fd","I" ); |
| 618 |
threshold = get_java_var( env, jobj,"threshold","I" ); |
| 619 |
timeout = get_java_var( env, jobj,"threshold","I" ); |
| 620 |
|
| 621 |
if( (size_t) length < 1 || (size_t) length > SSIZE_MAX ) |
| 622 |
{ |
| 623 |
throw_java_exception( env, IO_EXCEPTION, "readArray", |
| 624 |
"Invalid length" ); |
| 625 |
return -1; |
| 626 |
} |
| 627 |
|
| 628 |
buffer = (unsigned char *)malloc( sizeof( unsigned char ) * length ); |
| 629 |
if( buffer == 0 ) |
| 630 |
{ |
| 631 |
throw_java_exception( env, IO_EXCEPTION, "readArray", |
| 632 |
"Unable to allocate buffer" ); |
| 633 |
|
| 634 |
return -1; |
| 635 |
} |
| 636 |
|
| 637 |
bytes = read_byte_array( fd, buffer, length, threshold, timeout ); |
| 638 |
if( bytes < 0 ) |
| 639 |
{ |
| 640 |
free( buffer ); |
| 641 |
throw_java_exception_system_msg( env, IO_EXCEPTION, "readArray" ); |
| 642 |
|
| 643 |
return -1; |
| 644 |
} |
| 645 |
|
| 646 |
body = (*env)->GetByteArrayElements( env, jbarray, 0 ); |
| 647 |
for( i = 0; i < bytes; i++ ) body[ i + offset ] = buffer[ i ]; |
| 648 |
(*env)->ReleaseByteArrayElements( env, jbarray, body, 0 ); |
| 649 |
free( buffer ); |
| 650 |
return (bytes ? bytes : -1); |
| 651 |
} |
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
JNIEXPORT jint JNICALL LPRPort(nativeavailable)( JNIEnv *env, |
| 664 |
jobject jobj ) |
| 665 |
{ |
| 666 |
int fd = get_java_var( env, jobj,"fd","I" ); |
| 667 |
int result; |
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
#ifdef FIORDCHK |
| 681 |
result = ioctl(fd, FIORDCHK, 0); |
| 682 |
#else |
| 683 |
if( ioctl( fd, FIONREAD, &result ) < 0 ) |
| 684 |
{ |
| 685 |
goto fail; |
| 686 |
} |
| 687 |
#endif |
| 688 |
if (result == -1) { |
| 689 |
goto fail; |
| 690 |
} |
| 691 |
if( result ) |
| 692 |
{ |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
return (jint)result; |
| 702 |
fail: |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
throw_java_exception_system_msg( env, IO_EXCEPTION, "nativeavailable" ); |
| 708 |
return (jint)result; |
| 709 |
} |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
JNIEXPORT void JNICALL LPRPort(setHWFC)( JNIEnv *env, |
| 721 |
jobject jobj, jboolean state ) |
| 722 |
{ |
| 723 |
|
| 724 |
return; |
| 725 |
} |
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
JNIEXPORT void JNICALL LPRPort(eventLoop)( JNIEnv *env, |
| 747 |
jobject jobj ) |
| 748 |
{ |
| 749 |
int fd, ret; |
| 750 |
unsigned int pflags = 0; |
| 751 |
fd_set rfds; |
| 752 |
struct timeval sleep; |
| 753 |
jboolean interrupted = 0; |
| 754 |
|
| 755 |
fd = get_java_var( env, jobj,"fd","I" ); |
| 756 |
|
| 757 |
interrupted = is_interrupted(env, jobj); |
| 758 |
|
| 759 |
FD_ZERO( &rfds ); |
| 760 |
while( !interrupted ) |
| 761 |
{ |
| 762 |
FD_SET( fd, &rfds ); |
| 763 |
|
| 764 |
sleep.tv_sec = 1; |
| 765 |
sleep.tv_usec = 0; |
| 766 |
do { |
| 767 |
ret = select( fd + 1, &rfds, NULL, NULL, &sleep ); |
| 768 |
} |
| 769 |
while (ret < 0 && errno == EINTR); |
| 770 |
if( ret < 0 ) break; |
| 771 |
interrupted = is_interrupted(env, jobj); |
| 772 |
if(interrupted) { |
| 773 |
return; |
| 774 |
} |
| 775 |
|
| 776 |
#if defined(LPGETSTATUS) |
| 777 |
ioctl( fd, LPGETSTATUS, &pflags ); |
| 778 |
#elif defined(WIN32) |
| 779 |
pflags = getWin32ParallelStatusFlags(fd); |
| 780 |
#else |
| 781 |
|
| 782 |
printf("ParallelImp.c LPGETSTATUS is undefined!\n"); |
| 783 |
#endif |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
#if defined(PARALLEL_BUSY) |
| 791 |
if (pflags & PARALLEL_BUSY) |
| 792 |
send_event( env, jobj, PAR_EV_ERROR, JNI_TRUE ); |
| 793 |
#elif defined(LP_BUSY) |
| 794 |
if (pflags&LP_BUSY) |
| 795 |
send_event( env, jobj, PAR_EV_ERROR, JNI_TRUE ); |
| 796 |
#elif defined(EBUSY) |
| 797 |
if (pflags&EBUSY) |
| 798 |
send_event( env, jobj, PAR_EV_ERROR, JNI_TRUE ); |
| 799 |
#endif |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
#if defined (__linux__) |
| 808 |
|
| 809 |
if (pflags&LP_NOPA) |
| 810 |
send_event( env, jobj, PAR_EV_ERROR, 1 ); |
| 811 |
if (pflags&LP_SELEC) |
| 812 |
send_event( env, jobj, PAR_EV_ERROR, 1 ); |
| 813 |
if (pflags&LP_ERR) |
| 814 |
send_event( env, jobj, PAR_EV_ERROR, 1 ); |
| 815 |
#else |
| 816 |
|
| 817 |
printf("ParallelImp.c LPGETSTATUS is undefined!\n"); |
| 818 |
#endif |
| 819 |
usleep(1000); |
| 820 |
} |
| 821 |
return; |
| 822 |
} |
| 823 |
|
| 824 |
JNIEXPORT void JNICALL LPRPort(setInputBufferSize)(JNIEnv *env, |
| 825 |
jobject jobj, jint size ) |
| 826 |
{ |
| 827 |
#ifdef DEBUG |
| 828 |
printf("setInputBufferSize is not implemented\n"); |
| 829 |
#endif |
| 830 |
} |
| 831 |
JNIEXPORT jint JNICALL LPRPort(getInputBufferSize)(JNIEnv *env, |
| 832 |
jobject jobj) |
| 833 |
{ |
| 834 |
#ifdef DEBUG |
| 835 |
printf("getInputBufferSize is not implemented\n"); |
| 836 |
#endif |
| 837 |
return(1); |
| 838 |
} |
| 839 |
JNIEXPORT void JNICALL LPRPort(setOutputBufferSize)(JNIEnv *env, |
| 840 |
jobject jobj, jint size ) |
| 841 |
{ |
| 842 |
#ifdef DEBUG |
| 843 |
printf("setOutputBufferSize is not implemented\n"); |
| 844 |
#endif |
| 845 |
} |
| 846 |
JNIEXPORT jint JNICALL LPRPort(getOutputBufferSize)(JNIEnv *env, |
| 847 |
jobject jobj) |
| 848 |
{ |
| 849 |
#ifdef DEBUG |
| 850 |
printf("getOutputBufferSize is not implemented\n"); |
| 851 |
#endif |
| 852 |
return(1); |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
void throw_java_exception( JNIEnv *env, char *exc, char *foo, char *msg ) |
| 868 |
{ |
| 869 |
#define MSG_SIZE 128 |
| 870 |
char buf[ 128 ]; |
| 871 |
jclass clazz = (*env)->FindClass( env, exc ); |
| 872 |
if( !clazz ) |
| 873 |
{ |
| 874 |
(*env)->ExceptionDescribe( env ); |
| 875 |
(*env)->ExceptionClear( env ); |
| 876 |
return; |
| 877 |
} |
| 878 |
|
| 879 |
if(MSG_SIZE < (strlen( msg ) + strlen(foo) + 5)){ |
| 880 |
msg[ MSG_SIZE - strlen(foo) - 5] = 0; |
| 881 |
} |
| 882 |
#if defined(_GNU_SOURCE) |
| 883 |
snprintf( buf, 60, "%s in %s", msg, foo ); |
| 884 |
#else |
| 885 |
sprintf( buf,"%s in %s", msg, foo ); |
| 886 |
#endif |
| 887 |
(*env)->ThrowNew( env, clazz, buf ); |
| 888 |
|
| 889 |
(*env)->DeleteLocalRef( env, clazz ); |
| 890 |
} |
| 891 |
|
| 892 |
void throw_java_exception_system_msg( JNIEnv *env, char *exc, char *foo ) |
| 893 |
{ |
| 894 |
#ifdef WIN32 |
| 895 |
char *allocTextBuf; |
| 896 |
FormatMessage ( |
| 897 |
FORMAT_MESSAGE_ALLOCATE_BUFFER | |
| 898 |
FORMAT_MESSAGE_FROM_SYSTEM, |
| 899 |
NULL, |
| 900 |
GetLastError(), |
| 901 |
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 902 |
(LPSTR)&allocTextBuf, |
| 903 |
16, |
| 904 |
NULL ); |
| 905 |
throw_java_exception( env, exc, foo, allocTextBuf ); |
| 906 |
LocalFree(allocTextBuf); |
| 907 |
#else |
| 908 |
throw_java_exception( env, exc, foo, strerror( errno ) ); |
| 909 |
#endif |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
void report_error(char *msg) |
| 921 |
{ |
| 922 |
#ifndef DEBUG_MW |
| •923 |
fprintf(stderr, "%s", msg); |
| 924 |
#else |
| 925 |
mexWarnMsgTxt( msg ); |
| 926 |
#endif |
| 927 |
} |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
void report(char *msg) |
| 939 |
{ |
| 940 |
#ifdef DEBUG |
| •941 |
fprintf(stderr, "%s", msg); |
| 942 |
#endif |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
jboolean is_interrupted(JNIEnv *env, jobject jobj) |
| 955 |
{ |
| 956 |
jmethodID foo; |
| 957 |
jclass jclazz; |
| 958 |
int result; |
| 959 |
|
| 960 |
(*env)->ExceptionClear(env); |
| 961 |
|
| 962 |
jclazz = (*env)->GetObjectClass( env, jobj ); |
| 963 |
if(jclazz == NULL) return JNI_TRUE; |
| 964 |
|
| 965 |
foo = (*env)->GetMethodID( env, jclazz, "checkMonitorThread", "()Z"); |
| 966 |
if(foo == NULL) return JNI_TRUE; |
| 967 |
|
| 968 |
result = (*env)->CallBooleanMethod( env, jobj, foo ); |
| 969 |
|
| 970 |
#ifdef DEBUG |
| 971 |
if((*env)->ExceptionOccurred(env)) { |
| 972 |
report ("an error occured calling sendEvent()\n"); |
| 973 |
(*env)->ExceptionDescribe(env); |
| 974 |
(*env)->ExceptionClear(env); |
| 975 |
} |
| 976 |
#endif |
| 977 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 978 |
|
| 979 |
return(result); |
| 980 |
} |
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
int send_event(JNIEnv *env, jobject jobj, jint type, int flag) |
| 991 |
{ |
| 992 |
int result; |
| 993 |
jmethodID foo; |
| 994 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 995 |
|
| 996 |
if(jclazz == NULL) return JNI_TRUE; |
| 997 |
foo = (*env)->GetMethodID( env, jclazz, "sendEvent", "(IZ)Z" ); |
| 998 |
|
| 999 |
(*env)->ExceptionClear(env); |
| 1000 |
|
| 1001 |
result = (*env)->CallBooleanMethod( env, jobj, foo, type, |
| 1002 |
flag > 0 ? JNI_TRUE : JNI_FALSE ); |
| 1003 |
|
| 1004 |
#ifdef DEBUG |
| 1005 |
if((*env)->ExceptionOccurred(env)) { |
| 1006 |
report ("an error occured calling sendEvent()\n"); |
| 1007 |
(*env)->ExceptionDescribe(env); |
| 1008 |
(*env)->ExceptionClear(env); |
| 1009 |
} |
| 1010 |
#endif |
| 1011 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 1012 |
return(result); |
| 1013 |
} |
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
int get_java_var( JNIEnv *env, jobject jobj, char *id, char *type ) |
| 1025 |
{ |
| 1026 |
int result = 0; |
| 1027 |
jclass jclazz = (*env)->GetObjectClass( env, jobj ); |
| 1028 |
jfieldID jfd = (*env)->GetFieldID( env, jclazz, id, type ); |
| 1029 |
if( !jfd ) |
| 1030 |
{ |
| 1031 |
(*env)->ExceptionDescribe( env ); |
| 1032 |
(*env)->ExceptionClear( env ); |
| 1033 |
return result; |
| 1034 |
} |
| 1035 |
result = (int)( (*env)->GetIntField( env, jobj, jfd ) ); |
| 1036 |
|
| 1037 |
(*env)->DeleteLocalRef( env, jclazz ); |
| 1038 |
return result; |
| 1039 |
} |