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