1 /* 2 * Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 #include <X11/Xlib.h> 27 #include <X11/Xutil.h> 28 #include <X11/Xos.h> 29 #include <X11/Xatom.h> 30 #ifdef __linux__ 31 #include <execinfo.h> 32 #endif 33 34 #include <jvm.h> 35 #include <jni.h> 36 #include <jlong.h> 37 #include <jni_util.h> 38 39 #include "awt_p.h" 40 #include "awt_Component.h" 41 #include "awt_MenuComponent.h" 42 #include "awt_Font.h" 43 #include "awt_util.h" 44 45 #include "sun_awt_X11_XToolkit.h" 46 #include "java_awt_SystemColor.h" 47 #include "java_awt_TrayIcon.h" 48 #include <X11/extensions/XTest.h> 49 50 #include <unistd.h> 51 52 uint32_t awt_NumLockMask = 0; 53 Boolean awt_ModLockIsShiftLock = False; 54 55 static int32_t num_buttons = 0; 56 int32_t getNumButtons(); 57 58 extern JavaVM *jvm; 59 60 // Tracing level 61 static int tracing = 0; 62 #ifdef PRINT 63 #undef PRINT 64 #endif 65 #ifdef PRINT2 66 #undef PRINT2 67 #endif 68 69 #define PRINT if (tracing) printf 70 #define PRINT2 if (tracing > 1) printf 71 72 73 struct ComponentIDs componentIDs; 74 75 struct MenuComponentIDs menuComponentIDs; 76 77 #ifndef HEADLESS 78 79 extern Display* awt_init_Display(JNIEnv *env, jobject this); 80 extern void freeNativeStringArray(char **array, jsize length); 81 extern char** stringArrayToNative(JNIEnv *env, jobjectArray array, jsize * ret_length); 82 83 struct XFontPeerIDs xFontPeerIDs; 84 85 JNIEXPORT void JNICALL 86 Java_sun_awt_X11_XFontPeer_initIDs 87 (JNIEnv *env, jclass cls) 88 { 89 xFontPeerIDs.xfsname = 90 (*env)->GetFieldID(env, cls, "xfsname", "Ljava/lang/String;"); 91 } 92 #endif /* !HEADLESS */ 93 94 /* This function gets called from the static initializer for FileDialog.java 95 to initialize the fieldIDs for fields that may be accessed from C */ 96 97 JNIEXPORT void JNICALL 98 Java_java_awt_FileDialog_initIDs 99 (JNIEnv *env, jclass cls) 100 { 101 102 } 103 104 JNIEXPORT void JNICALL 105 Java_sun_awt_X11_XToolkit_initIDs 106 (JNIEnv *env, jclass clazz) 107 { 108 jfieldID fid = (*env)->GetStaticFieldID(env, clazz, "numLockMask", "I"); 109 CHECK_NULL(fid); 110 awt_NumLockMask = (*env)->GetStaticIntField(env, clazz, fid); 111 DTRACE_PRINTLN1("awt_NumLockMask = %u", awt_NumLockMask); 112 fid = (*env)->GetStaticFieldID(env, clazz, "modLockIsShiftLock", "I"); 113 CHECK_NULL(fid); 114 awt_ModLockIsShiftLock = (*env)->GetStaticIntField(env, clazz, fid) != 0 ? True : False; 115 } 116 117 /* 118 * Class: sun_awt_X11_XToolkit 119 * Method: getTrayIconDisplayTimeout 120 * Signature: ()J 121 */ 122 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getTrayIconDisplayTimeout 123 (JNIEnv *env, jclass clazz) 124 { 125 return (jlong) 2000; 126 } 127 128 /* 129 * Class: sun_awt_X11_XToolkit 130 * Method: getDefaultXColormap 131 * Signature: ()J 132 */ 133 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultXColormap 134 (JNIEnv *env, jclass clazz) 135 { 136 AwtGraphicsConfigDataPtr defaultConfig = 137 getDefaultConfig(DefaultScreen(awt_display)); 138 139 return (jlong) defaultConfig->awt_cmap; 140 } 141 142 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultScreenData 143 (JNIEnv *env, jclass clazz) 144 { 145 return ptr_to_jlong(getDefaultConfig(DefaultScreen(awt_display))); 146 } 147 148 149 JNIEXPORT jint JNICALL 150 DEF_JNI_OnLoad(JavaVM *vm, void *reserved) 151 { 152 jvm = vm; 153 return JNI_VERSION_1_2; 154 } 155 156 /* 157 * Class: sun_awt_X11_XToolkit 158 * Method: nativeLoadSystemColors 159 * Signature: ([I)V 160 */ 161 JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_nativeLoadSystemColors 162 (JNIEnv *env, jobject this, jintArray systemColors) 163 { 164 AwtGraphicsConfigDataPtr defaultConfig = 165 getDefaultConfig(DefaultScreen(awt_display)); 166 awtJNI_CreateColorData(env, defaultConfig, 1); 167 } 168 169 JNIEXPORT void JNICALL 170 Java_java_awt_Component_initIDs 171 (JNIEnv *env, jclass cls) 172 { 173 jclass keyclass = NULL; 174 175 176 componentIDs.x = (*env)->GetFieldID(env, cls, "x", "I"); 177 CHECK_NULL(componentIDs.x); 178 componentIDs.y = (*env)->GetFieldID(env, cls, "y", "I"); 179 CHECK_NULL(componentIDs.y); 180 componentIDs.width = (*env)->GetFieldID(env, cls, "width", "I"); 181 CHECK_NULL(componentIDs.width); 182 componentIDs.height = (*env)->GetFieldID(env, cls, "height", "I"); 183 CHECK_NULL(componentIDs.height); 184 componentIDs.isPacked = (*env)->GetFieldID(env, cls, "isPacked", "Z"); 185 CHECK_NULL(componentIDs.isPacked); 186 componentIDs.peer = 187 (*env)->GetFieldID(env, cls, "peer", "Ljava/awt/peer/ComponentPeer;"); 188 CHECK_NULL(componentIDs.peer); 189 componentIDs.background = 190 (*env)->GetFieldID(env, cls, "background", "Ljava/awt/Color;"); 191 CHECK_NULL(componentIDs.background); 192 componentIDs.foreground = 193 (*env)->GetFieldID(env, cls, "foreground", "Ljava/awt/Color;"); 194 CHECK_NULL(componentIDs.foreground); 195 componentIDs.graphicsConfig = 196 (*env)->GetFieldID(env, cls, "graphicsConfig", 197 "Ljava/awt/GraphicsConfiguration;"); 198 CHECK_NULL(componentIDs.graphicsConfig); 199 componentIDs.name = 200 (*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;"); 201 CHECK_NULL(componentIDs.name); 202 203 /* Use _NoClientCode() methods for trusted methods, so that we 204 * know that we are not invoking client code on trusted threads 205 */ 206 componentIDs.getParent = 207 (*env)->GetMethodID(env, cls, "getParent_NoClientCode", 208 "()Ljava/awt/Container;"); 209 CHECK_NULL(componentIDs.getParent); 210 211 componentIDs.getLocationOnScreen = 212 (*env)->GetMethodID(env, cls, "getLocationOnScreen_NoTreeLock", 213 "()Ljava/awt/Point;"); 214 CHECK_NULL(componentIDs.getLocationOnScreen); 215 216 keyclass = (*env)->FindClass(env, "java/awt/event/KeyEvent"); 217 CHECK_NULL(keyclass); 218 219 componentIDs.isProxyActive = 220 (*env)->GetFieldID(env, keyclass, "isProxyActive", 221 "Z"); 222 CHECK_NULL(componentIDs.isProxyActive); 223 224 componentIDs.appContext = 225 (*env)->GetFieldID(env, cls, "appContext", 226 "Lsun/awt/AppContext;"); 227 228 (*env)->DeleteLocalRef(env, keyclass); 229 } 230 231 232 JNIEXPORT void JNICALL 233 Java_java_awt_Container_initIDs 234 (JNIEnv *env, jclass cls) 235 { 236 237 } 238 239 240 JNIEXPORT void JNICALL 241 Java_java_awt_Button_initIDs 242 (JNIEnv *env, jclass cls) 243 { 244 245 } 246 247 JNIEXPORT void JNICALL 248 Java_java_awt_Scrollbar_initIDs 249 (JNIEnv *env, jclass cls) 250 { 251 252 } 253 254 255 JNIEXPORT void JNICALL 256 Java_java_awt_Window_initIDs 257 (JNIEnv *env, jclass cls) 258 { 259 260 } 261 262 JNIEXPORT void JNICALL 263 Java_java_awt_Frame_initIDs 264 (JNIEnv *env, jclass cls) 265 { 266 267 } 268 269 270 JNIEXPORT void JNICALL 271 Java_java_awt_MenuComponent_initIDs(JNIEnv *env, jclass cls) 272 { 273 menuComponentIDs.appContext = 274 (*env)->GetFieldID(env, cls, "appContext", "Lsun/awt/AppContext;"); 275 } 276 277 JNIEXPORT void JNICALL 278 Java_java_awt_Cursor_initIDs(JNIEnv *env, jclass cls) 279 { 280 } 281 282 283 JNIEXPORT void JNICALL Java_java_awt_MenuItem_initIDs 284 (JNIEnv *env, jclass cls) 285 { 286 } 287 288 289 JNIEXPORT void JNICALL Java_java_awt_Menu_initIDs 290 (JNIEnv *env, jclass cls) 291 { 292 } 293 294 JNIEXPORT void JNICALL 295 Java_java_awt_TextArea_initIDs 296 (JNIEnv *env, jclass cls) 297 { 298 } 299 300 301 JNIEXPORT void JNICALL 302 Java_java_awt_Checkbox_initIDs 303 (JNIEnv *env, jclass cls) 304 { 305 } 306 307 308 JNIEXPORT void JNICALL Java_java_awt_ScrollPane_initIDs 309 (JNIEnv *env, jclass cls) 310 { 311 } 312 313 JNIEXPORT void JNICALL 314 Java_java_awt_TextField_initIDs 315 (JNIEnv *env, jclass cls) 316 { 317 } 318 319 JNIEXPORT jboolean JNICALL AWTIsHeadless() { 320 #ifdef HEADLESS 321 return JNI_TRUE; 322 #else 323 return JNI_FALSE; 324 #endif 325 } 326 327 JNIEXPORT void JNICALL Java_java_awt_Dialog_initIDs (JNIEnv *env, jclass cls) 328 { 329 } 330 331 332 /* ========================== Begin poll section ================================ */ 333 334 // Includes 335 336 #include <sys/time.h> 337 #include <limits.h> 338 #include <locale.h> 339 #include <pthread.h> 340 341 #include <dlfcn.h> 342 #include <fcntl.h> 343 344 #include <poll.h> 345 #ifndef POLLRDNORM 346 #define POLLRDNORM POLLIN 347 #endif 348 349 // Prototypes 350 351 static void waitForEvents(JNIEnv *, jlong); 352 static void awt_pipe_init(); 353 static Boolean performPoll(JNIEnv *, jlong); 354 static void wakeUp(); 355 static void update_poll_timeout(int timeout_control); 356 static uint32_t get_poll_timeout(jlong nextTaskTime); 357 358 // Defines 359 360 #ifndef bzero 361 #define bzero(a,b) memset(a, 0, b) 362 #endif 363 364 #define AWT_POLL_BUFSIZE 100 /* bytes */ 365 #define AWT_READPIPE (awt_pipe_fds[0]) 366 #define AWT_WRITEPIPE (awt_pipe_fds[1]) 367 368 #define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)500) /* milliseconds */ 369 #define DEF_AWT_FLUSH_TIMEOUT ((uint32_t)100) /* milliseconds */ 370 #define AWT_MIN_POLL_TIMEOUT ((uint32_t)0) /* milliseconds */ 371 372 #define TIMEOUT_TIMEDOUT 0 373 #define TIMEOUT_EVENTS 1 374 375 /* awt_poll_alg - AWT Poll Events Aging Algorithms */ 376 #define AWT_POLL_FALSE 1 377 #define AWT_POLL_AGING_SLOW 2 378 #define AWT_POLL_AGING_FAST 3 379 380 #define AWT_POLL_THRESHOLD 1000 // msec, Block if delay is larger 381 #define AWT_POLL_BLOCK -1 // cause poll() block 382 383 // Static fields 384 385 static int awt_poll_alg = AWT_POLL_AGING_SLOW; 386 387 static uint32_t AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT; /* milliseconds */ 388 static uint32_t AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT; /* milliseconds */ 389 static pthread_t awt_MainThread = 0; 390 static int32_t awt_pipe_fds[2]; /* fds for wkaeup pipe */ 391 static Boolean awt_pipe_inited = False; /* make sure pipe is initialized before write */ 392 static jlong awt_next_flush_time = 0LL; /* 0 == no scheduled flush */ 393 static jlong awt_last_flush_time = 0LL; /* 0 == no scheduled flush */ 394 static uint32_t curPollTimeout; 395 static struct pollfd pollFds[2]; 396 static jlong poll_sleep_time = 0LL; // Used for tracing 397 static jlong poll_wakeup_time = 0LL; // Used for tracing 398 399 // AWT static poll timeout. Zero means "not set", aging algorithm is 400 // used. Static poll timeout values higher than 50 cause application 401 // look "slow" - they don't respond to user request fast 402 // enough. Static poll timeout value less than 10 are usually 403 // considered by schedulers as zero, so this might cause unnecessary 404 // CPU consumption by Java. The values between 10 - 50 are suggested 405 // for single client desktop configurations. For SunRay servers, it 406 // is highly recomended to use aging algorithm (set static poll timeout 407 // to 0). 408 static int32_t static_poll_timeout = 0; 409 410 static Bool isMainThread() { 411 return awt_MainThread == pthread_self(); 412 } 413 414 /* 415 * Creates the AWT utility pipe. This pipe exists solely so that 416 * we can cause the main event thread to wake up from a poll() or 417 * select() by writing to this pipe. 418 */ 419 static void 420 awt_pipe_init() { 421 422 if (awt_pipe_inited) { 423 return; 424 } 425 426 if ( pipe ( awt_pipe_fds ) == 0 ) 427 { 428 /* 429 ** the write wakes us up from the infinite sleep, which 430 ** then we cause a delay of AWT_FLUSHTIME and then we 431 ** flush. 432 */ 433 int32_t flags = 0; 434 /* set the pipe to be non-blocking */ 435 flags = fcntl ( AWT_READPIPE, F_GETFL, 0 ); 436 fcntl( AWT_READPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK ); 437 flags = fcntl ( AWT_WRITEPIPE, F_GETFL, 0 ); 438 fcntl( AWT_WRITEPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK ); 439 awt_pipe_inited = True; 440 } 441 else 442 { 443 AWT_READPIPE = -1; 444 AWT_WRITEPIPE = -1; 445 } 446 447 448 } /* awt_pipe_init() */ 449 450 /** 451 * Reads environment variables to initialize timeout fields. 452 */ 453 static void readEnv() { 454 char * value; 455 int tmp_poll_alg; 456 static Boolean env_read = False; 457 if (env_read) return; 458 459 env_read = True; 460 461 value = getenv("_AWT_MAX_POLL_TIMEOUT"); 462 if (value != NULL) { 463 AWT_MAX_POLL_TIMEOUT = atoi(value); 464 if (AWT_MAX_POLL_TIMEOUT == 0) { 465 AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT; 466 } 467 } 468 curPollTimeout = AWT_MAX_POLL_TIMEOUT/2; 469 470 value = getenv("_AWT_FLUSH_TIMEOUT"); 471 if (value != NULL) { 472 AWT_FLUSH_TIMEOUT = atoi(value); 473 if (AWT_FLUSH_TIMEOUT == 0) { 474 AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT; 475 } 476 } 477 478 value = getenv("_AWT_POLL_TRACING"); 479 if (value != NULL) { 480 tracing = atoi(value); 481 } 482 483 value = getenv("_AWT_STATIC_POLL_TIMEOUT"); 484 if (value != NULL) { 485 static_poll_timeout = atoi(value); 486 } 487 if (static_poll_timeout != 0) { 488 curPollTimeout = static_poll_timeout; 489 } 490 491 // non-blocking poll() 492 value = getenv("_AWT_POLL_ALG"); 493 if (value != NULL) { 494 tmp_poll_alg = atoi(value); 495 switch(tmp_poll_alg) { 496 case AWT_POLL_FALSE: 497 case AWT_POLL_AGING_SLOW: 498 case AWT_POLL_AGING_FAST: 499 awt_poll_alg = tmp_poll_alg; 500 break; 501 default: 502 PRINT("Unknown value of _AWT_POLL_ALG, assuming Slow Aging Algorithm by default"); 503 awt_poll_alg = AWT_POLL_AGING_SLOW; 504 break; 505 } 506 } 507 } 508 509 /** 510 * Returns the amount of milliseconds similar to System.currentTimeMillis() 511 */ 512 static jlong 513 awtJNI_TimeMillis(void) 514 { 515 struct timeval t; 516 517 gettimeofday(&t, 0); 518 519 return jlong_add(jlong_mul(jint_to_jlong(t.tv_sec), jint_to_jlong(1000)), 520 jint_to_jlong(t.tv_usec / 1000)); 521 } 522 523 /** 524 * Updates curPollTimeout according to the aging algorithm. 525 * @param timeout_control Either TIMEOUT_TIMEDOUT or TIMEOUT_EVENTS 526 */ 527 static void update_poll_timeout(int timeout_control) { 528 PRINT2("tout: %d\n", timeout_control); 529 530 // If static_poll_timeout is set, curPollTimeout has the fixed value 531 if (static_poll_timeout != 0) return; 532 533 // Update it otherwise 534 535 switch(awt_poll_alg) { 536 case AWT_POLL_AGING_SLOW: 537 if (timeout_control == TIMEOUT_TIMEDOUT) { 538 /* add 1/4 (plus 1, in case the division truncates to 0) */ 539 curPollTimeout += ((curPollTimeout>>2) + 1); 540 curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout); 541 } else if (timeout_control == TIMEOUT_EVENTS) { 542 /* subtract 1/4 (plus 1, in case the division truncates to 0) */ 543 if (curPollTimeout > 0) { 544 curPollTimeout -= ((curPollTimeout>>2) + 1); 545 curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, curPollTimeout); 546 } 547 } 548 break; 549 case AWT_POLL_AGING_FAST: 550 if (timeout_control == TIMEOUT_TIMEDOUT) { 551 curPollTimeout += ((curPollTimeout>>2) + 1); 552 curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout); 553 if((int)curPollTimeout > AWT_POLL_THRESHOLD || (int)curPollTimeout == AWT_POLL_BLOCK) 554 curPollTimeout = AWT_POLL_BLOCK; 555 } else if (timeout_control == TIMEOUT_EVENTS) { 556 curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, 1); 557 } 558 break; 559 } 560 } 561 562 /* 563 * Gets the best timeout for the next call to poll(). 564 * 565 * @param nextTaskTime -1, if there are no tasks; next time when 566 * timeout task needs to be run, in millis(of currentTimeMillis) 567 */ 568 static uint32_t get_poll_timeout(jlong nextTaskTime) 569 { 570 uint32_t ret_timeout = 0; 571 uint32_t timeout; 572 uint32_t taskTimeout; 573 uint32_t flushTimeout; 574 575 jlong curTime = awtJNI_TimeMillis(); 576 timeout = curPollTimeout; 577 switch(awt_poll_alg) { 578 case AWT_POLL_AGING_SLOW: 579 case AWT_POLL_AGING_FAST: 580 taskTimeout = (nextTaskTime == -1) ? AWT_MAX_POLL_TIMEOUT : (uint32_t)max(0, (int32_t)(nextTaskTime - curTime)); 581 flushTimeout = (awt_next_flush_time > 0) ? (uint32_t)max(0, (int32_t)(awt_next_flush_time - curTime)) : AWT_MAX_POLL_TIMEOUT; 582 583 PRINT2("to: %d, ft: %d, to: %d, tt: %d, mil: %d\n", taskTimeout, flushTimeout, timeout, (int)nextTaskTime, (int)curTime); 584 585 // Adjust timeout to flush_time and task_time 586 ret_timeout = min(flushTimeout, min(taskTimeout, timeout)); 587 if((int)curPollTimeout == AWT_POLL_BLOCK) 588 ret_timeout = AWT_POLL_BLOCK; 589 break; 590 591 case AWT_POLL_FALSE: 592 ret_timeout = (nextTaskTime > curTime) ? 593 (nextTaskTime - curTime) : 594 ((nextTaskTime == -1) ? -1 : 0); 595 break; 596 } 597 598 return ret_timeout; 599 600 } /* get_poll_timeout() */ 601 602 /* 603 * Waits for X events to appear on the pipe. Returns only when 604 * it is likely (but not definite) that there are events waiting to 605 * be processed. 606 * 607 * This routine also flushes the outgoing X queue, when the 608 * awt_next_flush_time has been reached. 609 * 610 * If fdAWTPipe is greater or equal than zero the routine also 611 * checks if there are events pending on the putback queue. 612 */ 613 void 614 waitForEvents(JNIEnv *env, jlong nextTaskTime) { 615 if (performPoll(env, nextTaskTime) 616 && (awt_next_flush_time > 0) 617 && (awtJNI_TimeMillis() >= awt_next_flush_time)) { 618 619 XFlush(awt_display); 620 awt_last_flush_time = awt_next_flush_time; 621 awt_next_flush_time = 0LL; 622 } 623 } /* waitForEvents() */ 624 625 JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_waitForEvents (JNIEnv *env, jclass class, jlong nextTaskTime) { 626 waitForEvents(env, nextTaskTime); 627 } 628 629 JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1toolkit_1init (JNIEnv *env, jclass class) { 630 awt_MainThread = pthread_self(); 631 632 awt_pipe_init(); 633 readEnv(); 634 } 635 636 JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1output_1flush (JNIEnv *env, jclass class) { 637 awt_output_flush(); 638 } 639 640 JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_wakeup_1poll (JNIEnv *env, jclass class) { 641 wakeUp(); 642 } 643 644 /* 645 * Polls both the X pipe and our AWT utility pipe. Returns 646 * when there is data on one of the pipes, or the operation times 647 * out. 648 * 649 * Not all Xt events come across the X pipe (e.g., timers 650 * and alternate inputs), so we must time out every now and 651 * then to check the Xt event queue. 652 * 653 * The fdAWTPipe will be empty when this returns. 654 */ 655 static Boolean 656 performPoll(JNIEnv *env, jlong nextTaskTime) { 657 static Bool pollFdsInited = False; 658 static char read_buf[AWT_POLL_BUFSIZE + 1]; /* dummy buf to empty pipe */ 659 660 uint32_t timeout = get_poll_timeout(nextTaskTime); 661 int32_t result; 662 663 if (!pollFdsInited) { 664 pollFds[0].fd = ConnectionNumber(awt_display); 665 pollFds[0].events = POLLRDNORM; 666 pollFds[0].revents = 0; 667 668 pollFds[1].fd = AWT_READPIPE; 669 pollFds[1].events = POLLRDNORM; 670 pollFds[1].revents = 0; 671 pollFdsInited = True; 672 } else { 673 pollFds[0].revents = 0; 674 pollFds[1].revents = 0; 675 } 676 677 AWT_NOFLUSH_UNLOCK(); 678 679 /* ACTUALLY DO THE POLL() */ 680 if (timeout == 0) { 681 // be sure other threads get a chance 682 if (!awtJNI_ThreadYield(env)) { 683 return FALSE; 684 } 685 } 686 687 if (tracing) poll_sleep_time = awtJNI_TimeMillis(); 688 result = poll( pollFds, 2, (int32_t) timeout ); 689 if (tracing) poll_wakeup_time = awtJNI_TimeMillis(); 690 PRINT("%d of %d, res: %d\n", (int)(poll_wakeup_time-poll_sleep_time), (int)timeout, result); 691 692 AWT_LOCK(); 693 if (result == 0) { 694 /* poll() timed out -- update timeout value */ 695 update_poll_timeout(TIMEOUT_TIMEDOUT); 696 PRINT2("performPoll(): TIMEOUT_TIMEDOUT curPollTimeout = %d \n", curPollTimeout); 697 } 698 if (pollFds[1].revents) { 699 int count; 700 PRINT("Woke up\n"); 701 /* There is data on the AWT pipe - empty it */ 702 do { 703 count = read(AWT_READPIPE, read_buf, AWT_POLL_BUFSIZE ); 704 } while (count == AWT_POLL_BUFSIZE ); 705 PRINT2("performPoll(): data on the AWT pipe: curPollTimeout = %d \n", curPollTimeout); 706 } 707 if (pollFds[0].revents) { 708 // Events in X pipe 709 update_poll_timeout(TIMEOUT_EVENTS); 710 PRINT2("performPoll(): TIMEOUT_EVENTS curPollTimeout = %d \n", curPollTimeout); 711 } 712 return TRUE; 713 714 } /* performPoll() */ 715 716 /** 717 * Schedules next auto-flush event or performs forced flush depending 718 * on the time of the previous flush. 719 */ 720 void awt_output_flush() { 721 if (awt_next_flush_time == 0) { 722 JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); 723 724 jlong curTime = awtJNI_TimeMillis(); // current time 725 jlong l_awt_last_flush_time = awt_last_flush_time; // last time we flushed queue 726 jlong next_flush_time = l_awt_last_flush_time + AWT_FLUSH_TIMEOUT; 727 728 if (curTime >= next_flush_time) { 729 // Enough time passed from last flush 730 PRINT("f1\n"); 731 AWT_LOCK(); 732 XFlush(awt_display); 733 awt_last_flush_time = curTime; 734 AWT_NOFLUSH_UNLOCK(); 735 } else { 736 awt_next_flush_time = next_flush_time; 737 PRINT("f2\n"); 738 wakeUp(); 739 } 740 } 741 } 742 743 744 /** 745 * Wakes-up poll() in performPoll 746 */ 747 static void wakeUp() { 748 static char wakeUp_char = 'p'; 749 if (!isMainThread() && awt_pipe_inited) { 750 int ret = write ( AWT_WRITEPIPE, &wakeUp_char, 1 ); 751 (void)ret; 752 } 753 } 754 755 756 /* ========================== End poll section ================================= */ 757 758 /* 759 * Class: java_awt_KeyboardFocusManager 760 * Method: initIDs 761 * Signature: ()V 762 */ 763 JNIEXPORT void JNICALL 764 Java_java_awt_KeyboardFocusManager_initIDs 765 (JNIEnv *env, jclass cls) 766 { 767 } 768 769 /* 770 * Class: sun_awt_X11_XToolkit 771 * Method: getEnv 772 * Signature: (Ljava/lang/String;)Ljava/lang/String; 773 */ 774 JNIEXPORT jstring JNICALL Java_sun_awt_X11_XToolkit_getEnv 775 (JNIEnv *env , jclass clazz, jstring key) { 776 char *ptr = NULL; 777 const char *keystr = NULL; 778 jstring ret = NULL; 779 780 keystr = JNU_GetStringPlatformChars(env, key, NULL); 781 if (keystr) { 782 ptr = getenv(keystr); 783 if (ptr) { 784 ret = JNU_NewStringPlatform(env, (const char *) ptr); 785 } 786 JNU_ReleaseStringPlatformChars(env, key, (const char*)keystr); 787 } 788 return ret; 789 } 790 791 #ifdef __linux__ 792 void print_stack(void) 793 { 794 void *array[10]; 795 size_t size; 796 char **strings; 797 size_t i; 798 799 size = backtrace (array, 10); 800 strings = backtrace_symbols (array, size); 801 802 fprintf (stderr, "Obtained %zd stack frames.\n", size); 803 804 for (i = 0; i < size; i++) 805 fprintf (stderr, "%s\n", strings[i]); 806 807 free (strings); 808 } 809 #endif 810 811 Window get_xawt_root_shell(JNIEnv *env) { 812 static jclass classXRootWindow = NULL; 813 static jmethodID methodGetXRootWindow = NULL; 814 static Window xawt_root_shell = None; 815 816 if (xawt_root_shell == None){ 817 if (classXRootWindow == NULL){ 818 jclass cls_tmp = (*env)->FindClass(env, "sun/awt/X11/XRootWindow"); 819 if (!JNU_IsNull(env, cls_tmp)) { 820 classXRootWindow = (jclass)(*env)->NewGlobalRef(env, cls_tmp); 821 (*env)->DeleteLocalRef(env, cls_tmp); 822 } 823 } 824 if( classXRootWindow != NULL) { 825 methodGetXRootWindow = (*env)->GetStaticMethodID(env, classXRootWindow, "getXRootWindow", "()J"); 826 } 827 if( classXRootWindow != NULL && methodGetXRootWindow !=NULL ) { 828 xawt_root_shell = (Window) (*env)->CallStaticLongMethod(env, classXRootWindow, methodGetXRootWindow); 829 } 830 if ((*env)->ExceptionCheck(env)) { 831 (*env)->ExceptionDescribe(env); 832 (*env)->ExceptionClear(env); 833 } 834 } 835 return xawt_root_shell; 836 } 837 838 /* 839 * Old, compatibility, backdoor for DT. This is a different 840 * implementation. It keeps the signature, but acts on 841 * awt_root_shell, not the frame passed as an argument. Note, that 842 * the code that uses the old backdoor doesn't work correctly with 843 * gnome session proxy that checks for WM_COMMAND when the window is 844 * firts mapped, because DT code calls this old backdoor *after* the 845 * frame is shown or it would get NPE with old AWT (previous 846 * implementation of this backdoor) otherwise. Old style session 847 * managers (e.g. CDE) that check WM_COMMAND only during session 848 * checkpoint should work fine, though. 849 * 850 * NB: The function name looks deceptively like a JNI native method 851 * name. It's not! It's just a plain function. 852 */ 853 854 JNIEXPORT void JNICALL 855 Java_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this, 856 jobject frame, jstring jcommand) 857 { 858 const char *command; 859 XTextProperty text_prop; 860 char *c[1]; 861 int32_t status; 862 Window xawt_root_window; 863 864 AWT_LOCK(); 865 xawt_root_window = get_xawt_root_shell(env); 866 867 if ( xawt_root_window == None ) { 868 AWT_UNLOCK(); 869 JNU_ThrowNullPointerException(env, "AWT root shell is unrealized"); 870 return; 871 } 872 873 command = (char *) JNU_GetStringPlatformChars(env, jcommand, NULL); 874 if (command != NULL) { 875 c[0] = (char *)command; 876 status = XmbTextListToTextProperty(awt_display, c, 1, 877 XStdICCTextStyle, &text_prop); 878 879 if (status == Success || status > 0) { 880 XSetTextProperty(awt_display, xawt_root_window, 881 &text_prop, XA_WM_COMMAND); 882 if (text_prop.value != NULL) 883 XFree(text_prop.value); 884 } 885 JNU_ReleaseStringPlatformChars(env, jcommand, command); 886 } 887 AWT_UNLOCK(); 888 } 889 890 891 /* 892 * New DT backdoor to set WM_COMMAND. New code should use this 893 * backdoor and call it *before* the first frame is shown so that 894 * gnome session proxy can correctly handle it. 895 * 896 * NB: The function name looks deceptively like a JNI native method 897 * name. It's not! It's just a plain function. 898 */ 899 JNIEXPORT void JNICALL 900 Java_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jarray) 901 { 902 jsize length; 903 char ** array; 904 XTextProperty text_prop; 905 int status; 906 Window xawt_root_window; 907 908 AWT_LOCK(); 909 xawt_root_window = get_xawt_root_shell(env); 910 911 if (xawt_root_window == None) { 912 AWT_UNLOCK(); 913 JNU_ThrowNullPointerException(env, "AWT root shell is unrealized"); 914 return; 915 } 916 917 array = stringArrayToNative(env, jarray, &length); 918 919 if (array != NULL) { 920 status = XmbTextListToTextProperty(awt_display, array, length, 921 XStdICCTextStyle, &text_prop); 922 if (status < 0) { 923 switch (status) { 924 case XNoMemory: 925 JNU_ThrowOutOfMemoryError(env, 926 "XmbTextListToTextProperty: XNoMemory"); 927 break; 928 case XLocaleNotSupported: 929 JNU_ThrowInternalError(env, 930 "XmbTextListToTextProperty: XLocaleNotSupported"); 931 break; 932 case XConverterNotFound: 933 JNU_ThrowNullPointerException(env, 934 "XmbTextListToTextProperty: XConverterNotFound"); 935 break; 936 default: 937 JNU_ThrowInternalError(env, 938 "XmbTextListToTextProperty: unknown error"); 939 } 940 } else { 941 XSetTextProperty(awt_display, xawt_root_window, 942 &text_prop, XA_WM_COMMAND); 943 } 944 945 if (text_prop.value != NULL) 946 XFree(text_prop.value); 947 948 freeNativeStringArray(array, length); 949 } 950 AWT_UNLOCK(); 951 } 952 953 /* 954 * Class: java_awt_TrayIcon 955 * Method: initIDs 956 * Signature: ()V 957 */ 958 JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs(JNIEnv *env , jclass clazz) 959 { 960 } 961 962 963 /* 964 * Class: java_awt_Cursor 965 * Method: finalizeImpl 966 * Signature: ()V 967 */ 968 JNIEXPORT void JNICALL 969 Java_java_awt_Cursor_finalizeImpl(JNIEnv *env, jclass clazz, jlong pData) 970 { 971 Cursor xcursor; 972 973 xcursor = (Cursor)pData; 974 if (xcursor != None) { 975 AWT_LOCK(); 976 XFreeCursor(awt_display, xcursor); 977 AWT_UNLOCK(); 978 } 979 } 980 981 982 /* 983 * Class: sun_awt_X11_XToolkit 984 * Method: getNumberOfButtonsImpl 985 * Signature: ()I 986 */ 987 JNIEXPORT jint JNICALL Java_sun_awt_X11_XToolkit_getNumberOfButtonsImpl 988 (JNIEnv * env, jobject cls){ 989 if (num_buttons == 0) { 990 num_buttons = getNumButtons(); 991 } 992 return num_buttons; 993 } 994 995 int32_t getNumButtons() { 996 int32_t major_opcode, first_event, first_error; 997 int32_t xinputAvailable; 998 int32_t numDevices, devIdx, clsIdx; 999 XDeviceInfo* devices; 1000 XDeviceInfo* aDevice; 1001 XButtonInfo* bInfo; 1002 int32_t local_num_buttons = 0; 1003 1004 /* 4700242: 1005 * If XTest is asked to press a non-existant mouse button 1006 * (i.e. press Button3 on a system configured with a 2-button mouse), 1007 * then a crash may happen. To avoid this, we use the XInput 1008 * extension to query for the number of buttons on the XPointer, and check 1009 * before calling XTestFakeButtonEvent(). 1010 */ 1011 xinputAvailable = XQueryExtension(awt_display, INAME, &major_opcode, &first_event, &first_error); 1012 if (xinputAvailable) { 1013 DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XINPUT) returns major_opcode = %d, first_event = %d, first_error = %d", 1014 major_opcode, first_event, first_error); 1015 devices = XListInputDevices(awt_display, &numDevices); 1016 for (devIdx = 0; devIdx < numDevices; devIdx++) { 1017 aDevice = &(devices[devIdx]); 1018 #ifdef IsXExtensionPointer 1019 if (aDevice->use == IsXExtensionPointer) { 1020 for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) { 1021 if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) { 1022 bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx])); 1023 local_num_buttons = bInfo->num_buttons; 1024 DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons); 1025 break; 1026 } 1027 } 1028 break; 1029 } 1030 #endif 1031 if (local_num_buttons <= 0 ) { 1032 if (aDevice->use == IsXPointer) { 1033 for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) { 1034 if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) { 1035 bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx])); 1036 local_num_buttons = bInfo->num_buttons; 1037 DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons); 1038 break; 1039 } 1040 } 1041 break; 1042 } 1043 } 1044 } 1045 1046 XFreeDeviceList(devices); 1047 } 1048 else { 1049 DTRACE_PRINTLN1("RobotPeer: XINPUT extension is unavailable, assuming %d mouse buttons", num_buttons); 1050 } 1051 if (local_num_buttons == 0 ) { 1052 local_num_buttons = 3; 1053 } 1054 1055 return local_num_buttons; 1056 } 1057 1058 /* 1059 * Class: sun_awt_X11_XWindowPeer 1060 * Method: getJvmPID 1061 * Signature: ()I 1062 */ 1063 JNIEXPORT jint JNICALL Java_sun_awt_X11_XWindowPeer_getJvmPID 1064 (JNIEnv *env, jclass cls) 1065 { 1066 /* Return the JVM's PID. */ 1067 return getpid(); 1068 } 1069 1070 #ifndef HOST_NAME_MAX 1071 #define HOST_NAME_MAX 1024 /* Overestimated */ 1072 #endif 1073 1074 /* 1075 * Class: sun_awt_X11_XWindowPeer 1076 * Method: getLocalHostname 1077 * Signature: ()Ljava/lang/String; 1078 */ 1079 JNIEXPORT jstring JNICALL Java_sun_awt_X11_XWindowPeer_getLocalHostname 1080 (JNIEnv *env, jclass cls) 1081 { 1082 /* Return the machine's FQDN. */ 1083 char hostname[HOST_NAME_MAX + 1]; 1084 if (gethostname(hostname, HOST_NAME_MAX + 1) == 0) { 1085 hostname[HOST_NAME_MAX] = '\0'; 1086 jstring res = (*env)->NewStringUTF(env, hostname); 1087 return res; 1088 } 1089 1090 return (jstring)NULL; 1091 }