0N/A/*
3876N/A * Copyright (c) 2002, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A#include <X11/Xlib.h>
0N/A#include <X11/Xutil.h>
0N/A#include <X11/Xos.h>
0N/A#include <X11/Xatom.h>
0N/A#ifdef __linux__
0N/A#include <execinfo.h>
0N/A#endif
0N/A
0N/A#include <jvm.h>
0N/A#include <jni.h>
0N/A#include <jlong.h>
0N/A#include <jni_util.h>
0N/A
0N/A#include "awt_p.h"
0N/A#include "awt_Component.h"
0N/A#include "awt_MenuComponent.h"
0N/A#include "awt_KeyboardFocusManager.h"
0N/A#include "awt_Font.h"
0N/A
0N/A#include "sun_awt_X11_XToolkit.h"
0N/A#include "java_awt_SystemColor.h"
0N/A#include "java_awt_TrayIcon.h"
1224N/A#include <X11/extensions/XTest.h>
0N/A
4514N/A#include <unistd.h>
4514N/A
0N/Auint32_t awt_NumLockMask = 0;
0N/ABoolean awt_ModLockIsShiftLock = False;
0N/A
1224N/Astatic int32_t num_buttons = 0;
1224N/Aint32_t getNumButtons();
1224N/A
0N/Aextern JavaVM *jvm;
0N/A
0N/A// Tracing level
0N/Astatic int tracing = 0;
0N/A#ifdef PRINT
0N/A#undef PRINT
0N/A#endif
0N/A#ifdef PRINT2
0N/A#undef PRINT2
0N/A#endif
0N/A
0N/A#define PRINT if (tracing) printf
0N/A#define PRINT2 if (tracing > 1) printf
0N/A
0N/A
0N/Astruct ComponentIDs componentIDs;
0N/A
0N/Astruct MenuComponentIDs menuComponentIDs;
0N/A
0N/Astruct KeyboardFocusManagerIDs keyboardFocusManagerIDs;
0N/A
0N/A#ifndef HEADLESS
0N/A
0N/Aextern Display* awt_init_Display(JNIEnv *env, jobject this);
0N/A
0N/Aextern struct MFontPeerIDs mFontPeerIDs;
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_X11_XFontPeer_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A mFontPeerIDs.xfsname =
0N/A (*env)->GetFieldID(env, cls, "xfsname", "Ljava/lang/String;");
0N/A}
0N/A#endif /* !HEADLESS */
0N/A
0N/A/* This function gets called from the static initializer for FileDialog.java
0N/A to initialize the fieldIDs for fields that may be accessed from C */
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_FileDialog_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_X11_XToolkit_initIDs
0N/A (JNIEnv *env, jclass clazz)
0N/A{
0N/A jfieldID fid = (*env)->GetStaticFieldID(env, clazz, "numLockMask", "I");
0N/A awt_NumLockMask = (*env)->GetStaticIntField(env, clazz, fid);
0N/A DTRACE_PRINTLN1("awt_NumLockMask = %u", awt_NumLockMask);
0N/A fid = (*env)->GetStaticFieldID(env, clazz, "modLockIsShiftLock", "I");
0N/A awt_ModLockIsShiftLock = (*env)->GetStaticIntField(env, clazz, fid) != 0 ? True : False;
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_X11_XToolkit
3876N/A * Method: getTrayIconDisplayTimeout
3876N/A * Signature: ()J
3876N/A */
3876N/AJNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getTrayIconDisplayTimeout
3876N/A (JNIEnv *env, jclass clazz)
3876N/A{
3876N/A#ifndef JAVASE_EMBEDDED
3876N/A return (jlong) 2000;
3876N/A#else
3876N/A return (jlong) 10000;
3876N/A#endif
3876N/A}
3876N/A
3876N/A/*
3876N/A * Class: sun_awt_X11_XToolkit
0N/A * Method: getDefaultXColormap
0N/A * Signature: ()J
0N/A */
0N/AJNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultXColormap
0N/A (JNIEnv *env, jclass clazz)
0N/A{
0N/A AwtGraphicsConfigDataPtr defaultConfig =
0N/A getDefaultConfig(DefaultScreen(awt_display));
0N/A
0N/A return (jlong) defaultConfig->awt_cmap;
0N/A}
0N/A
0N/AJNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultScreenData
0N/A (JNIEnv *env, jclass clazz)
0N/A{
0N/A return ptr_to_jlong(getDefaultConfig(DefaultScreen(awt_display)));
0N/A}
0N/A
0N/A
0N/AJNIEXPORT jint JNICALL
0N/AJNI_OnLoad(JavaVM *vm, void *reserved)
0N/A{
0N/A jvm = vm;
0N/A return JNI_VERSION_1_2;
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_X11_XToolkit
0N/A * Method: nativeLoadSystemColors
0N/A * Signature: ([I)V
0N/A */
0N/AJNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_nativeLoadSystemColors
0N/A (JNIEnv *env, jobject this, jintArray systemColors)
0N/A{
0N/A AwtGraphicsConfigDataPtr defaultConfig =
0N/A getDefaultConfig(DefaultScreen(awt_display));
0N/A awtJNI_CreateColorData(env, defaultConfig, 1);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Component_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A jclass keyclass = NULL;
0N/A
0N/A
0N/A componentIDs.x = (*env)->GetFieldID(env, cls, "x", "I");
0N/A componentIDs.y = (*env)->GetFieldID(env, cls, "y", "I");
0N/A componentIDs.width = (*env)->GetFieldID(env, cls, "width", "I");
0N/A componentIDs.height = (*env)->GetFieldID(env, cls, "height", "I");
0N/A componentIDs.isPacked = (*env)->GetFieldID(env, cls, "isPacked", "Z");
0N/A componentIDs.peer =
0N/A (*env)->GetFieldID(env, cls, "peer", "Ljava/awt/peer/ComponentPeer;");
0N/A componentIDs.background =
0N/A (*env)->GetFieldID(env, cls, "background", "Ljava/awt/Color;");
0N/A componentIDs.foreground =
0N/A (*env)->GetFieldID(env, cls, "foreground", "Ljava/awt/Color;");
0N/A componentIDs.graphicsConfig =
0N/A (*env)->GetFieldID(env, cls, "graphicsConfig",
0N/A "Ljava/awt/GraphicsConfiguration;");
0N/A componentIDs.name =
0N/A (*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");
0N/A
0N/A /* Use _NoClientCode() methods for trusted methods, so that we
0N/A * know that we are not invoking client code on trusted threads
0N/A */
0N/A componentIDs.getParent =
0N/A (*env)->GetMethodID(env, cls, "getParent_NoClientCode",
0N/A "()Ljava/awt/Container;");
0N/A
0N/A componentIDs.getLocationOnScreen =
0N/A (*env)->GetMethodID(env, cls, "getLocationOnScreen_NoTreeLock",
0N/A "()Ljava/awt/Point;");
0N/A
0N/A keyclass = (*env)->FindClass(env, "java/awt/event/KeyEvent");
3290N/A if (JNU_IsNull(env, keyclass)) {
3290N/A return;
3290N/A }
0N/A
0N/A componentIDs.isProxyActive =
0N/A (*env)->GetFieldID(env, keyclass, "isProxyActive",
0N/A "Z");
0N/A
0N/A componentIDs.appContext =
0N/A (*env)->GetFieldID(env, cls, "appContext",
0N/A "Lsun/awt/AppContext;");
0N/A
0N/A (*env)->DeleteLocalRef(env, keyclass);
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Container_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Button_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Scrollbar_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Window_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Frame_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_MenuComponent_initIDs(JNIEnv *env, jclass cls)
0N/A{
0N/A menuComponentIDs.appContext =
0N/A (*env)->GetFieldID(env, cls, "appContext", "Lsun/awt/AppContext;");
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Cursor_initIDs(JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL Java_java_awt_MenuItem_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL Java_java_awt_Menu_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_TextArea_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Checkbox_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL Java_java_awt_ScrollPane_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_TextField_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/AJNIEXPORT jboolean JNICALL AWTIsHeadless() {
0N/A#ifdef HEADLESS
0N/A return JNI_TRUE;
0N/A#else
0N/A return JNI_FALSE;
0N/A#endif
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL Java_java_awt_Dialog_initIDs (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/A
0N/A/* ========================== Begin poll section ================================ */
0N/A
0N/A// Includes
0N/A
0N/A#include <sys/time.h>
0N/A#include <limits.h>
0N/A#include <locale.h>
0N/A#include <pthread.h>
0N/A
0N/A#include <dlfcn.h>
0N/A#include <fcntl.h>
0N/A
0N/A#include <poll.h>
0N/A#ifndef POLLRDNORM
0N/A#define POLLRDNORM POLLIN
0N/A#endif
0N/A
0N/A// Prototypes
0N/A
0N/Astatic void waitForEvents(JNIEnv *, jlong);
0N/Astatic void awt_pipe_init();
0N/Astatic void processOneEvent(XtInputMask iMask);
0N/Astatic void performPoll(JNIEnv *, jlong);
0N/Astatic void wakeUp();
0N/Astatic void update_poll_timeout(int timeout_control);
0N/Astatic uint32_t get_poll_timeout(jlong nextTaskTime);
0N/A
0N/A// Defines
0N/A
0N/A#ifndef bzero
0N/A#define bzero(a,b) memset(a, 0, b)
0N/A#endif
0N/A
0N/A#define AWT_POLL_BUFSIZE 100 /* bytes */
0N/A#define AWT_READPIPE (awt_pipe_fds[0])
0N/A#define AWT_WRITEPIPE (awt_pipe_fds[1])
0N/A
3876N/A#ifdef JAVASE_EMBEDDED
3876N/A #define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)4000000000) /* milliseconds */
3876N/A#else
3876N/A #define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)500) /* milliseconds */
3876N/A#endif
3876N/A
0N/A#define DEF_AWT_FLUSH_TIMEOUT ((uint32_t)100) /* milliseconds */
0N/A#define AWT_MIN_POLL_TIMEOUT ((uint32_t)0) /* milliseconds */
0N/A
0N/A#define TIMEOUT_TIMEDOUT 0
0N/A#define TIMEOUT_EVENTS 1
0N/A
3876N/A/* awt_poll_alg - AWT Poll Events Aging Algorithms */
3876N/A#define AWT_POLL_FALSE 1
3876N/A#define AWT_POLL_AGING_SLOW 2
3876N/A#define AWT_POLL_AGING_FAST 3
3876N/A
3876N/A#define AWT_POLL_THRESHOLD 1000 // msec, Block if delay is larger
3876N/A#define AWT_POLL_BLOCK -1 // cause poll() block
3876N/A
0N/A// Static fields
0N/A
3876N/A#ifdef JAVASE_EMBEDDED
3876N/A static int awt_poll_alg = AWT_POLL_AGING_FAST;
3876N/A#else
3876N/A static int awt_poll_alg = AWT_POLL_AGING_SLOW;
3876N/A#endif
3876N/A
0N/Astatic uint32_t AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT; /* milliseconds */
0N/Astatic uint32_t AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT; /* milliseconds */
0N/Astatic pthread_t awt_MainThread = 0;
0N/Astatic int32_t awt_pipe_fds[2]; /* fds for wkaeup pipe */
0N/Astatic Boolean awt_pipe_inited = False; /* make sure pipe is initialized before write */
0N/Astatic jlong awt_next_flush_time = 0LL; /* 0 == no scheduled flush */
0N/Astatic jlong awt_last_flush_time = 0LL; /* 0 == no scheduled flush */
0N/Astatic uint32_t curPollTimeout;
0N/Astatic struct pollfd pollFds[2];
0N/Astatic jlong poll_sleep_time = 0LL; // Used for tracing
0N/Astatic jlong poll_wakeup_time = 0LL; // Used for tracing
0N/A
0N/A// AWT static poll timeout. Zero means "not set", aging algorithm is
0N/A// used. Static poll timeout values higher than 50 cause application
0N/A// look "slow" - they don't respond to user request fast
0N/A// enough. Static poll timeout value less than 10 are usually
0N/A// considered by schedulers as zero, so this might cause unnecessary
0N/A// CPU consumption by Java. The values between 10 - 50 are suggested
0N/A// for single client desktop configurations. For SunRay servers, it
0N/A// is highly recomended to use aging algorithm (set static poll timeout
0N/A// to 0).
0N/Astatic int32_t static_poll_timeout = 0;
0N/A
0N/Astatic Bool isMainThread() {
0N/A return awt_MainThread == pthread_self();
0N/A}
0N/A
0N/A/*
0N/A * Creates the AWT utility pipe. This pipe exists solely so that
0N/A * we can cause the main event thread to wake up from a poll() or
0N/A * select() by writing to this pipe.
0N/A */
0N/Astatic void
0N/Aawt_pipe_init() {
0N/A
0N/A if (awt_pipe_inited) {
0N/A return;
0N/A }
0N/A
0N/A if ( pipe ( awt_pipe_fds ) == 0 )
0N/A {
0N/A /*
0N/A ** the write wakes us up from the infinite sleep, which
0N/A ** then we cause a delay of AWT_FLUSHTIME and then we
0N/A ** flush.
0N/A */
0N/A int32_t flags = 0;
0N/A /* set the pipe to be non-blocking */
0N/A flags = fcntl ( AWT_READPIPE, F_GETFL, 0 );
0N/A fcntl( AWT_READPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );
0N/A flags = fcntl ( AWT_WRITEPIPE, F_GETFL, 0 );
0N/A fcntl( AWT_WRITEPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );
0N/A awt_pipe_inited = True;
0N/A }
0N/A else
0N/A {
0N/A AWT_READPIPE = -1;
0N/A AWT_WRITEPIPE = -1;
0N/A }
0N/A
0N/A
0N/A} /* awt_pipe_init() */
0N/A
0N/A/**
0N/A * Reads environment variables to initialize timeout fields.
0N/A */
0N/Astatic void readEnv() {
0N/A char * value;
3876N/A int tmp_poll_alg;
0N/A static Boolean env_read = False;
0N/A if (env_read) return;
0N/A
0N/A env_read = True;
0N/A
0N/A value = getenv("_AWT_MAX_POLL_TIMEOUT");
0N/A if (value != NULL) {
0N/A AWT_MAX_POLL_TIMEOUT = atoi(value);
0N/A if (AWT_MAX_POLL_TIMEOUT == 0) {
0N/A AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT;
0N/A }
0N/A }
0N/A curPollTimeout = AWT_MAX_POLL_TIMEOUT/2;
0N/A
0N/A value = getenv("_AWT_FLUSH_TIMEOUT");
0N/A if (value != NULL) {
0N/A AWT_FLUSH_TIMEOUT = atoi(value);
0N/A if (AWT_FLUSH_TIMEOUT == 0) {
0N/A AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT;
0N/A }
0N/A }
0N/A
0N/A value = getenv("_AWT_POLL_TRACING");
0N/A if (value != NULL) {
0N/A tracing = atoi(value);
0N/A }
0N/A
0N/A value = getenv("_AWT_STATIC_POLL_TIMEOUT");
0N/A if (value != NULL) {
0N/A static_poll_timeout = atoi(value);
0N/A }
0N/A if (static_poll_timeout != 0) {
0N/A curPollTimeout = static_poll_timeout;
0N/A }
3876N/A
3876N/A // non-blocking poll()
3876N/A value = getenv("_AWT_POLL_ALG");
3876N/A if (value != NULL) {
3876N/A tmp_poll_alg = atoi(value);
3876N/A switch(tmp_poll_alg) {
3876N/A case AWT_POLL_FALSE:
3876N/A case AWT_POLL_AGING_SLOW:
3876N/A case AWT_POLL_AGING_FAST:
3876N/A awt_poll_alg = tmp_poll_alg;
3876N/A break;
3876N/A default:
3876N/A PRINT("Unknown value of _AWT_POLL_ALG, assuming Slow Aging Algorithm by default");
3876N/A awt_poll_alg = AWT_POLL_AGING_SLOW;
3876N/A break;
3876N/A }
3876N/A }
0N/A}
0N/A
0N/A/**
0N/A * Returns the amount of milliseconds similar to System.currentTimeMillis()
0N/A */
0N/Astatic jlong
0N/AawtJNI_TimeMillis(void)
0N/A{
0N/A struct timeval t;
0N/A
0N/A gettimeofday(&t, 0);
0N/A
0N/A return jlong_add(jlong_mul(jint_to_jlong(t.tv_sec), jint_to_jlong(1000)),
0N/A jint_to_jlong(t.tv_usec / 1000));
0N/A}
0N/A
0N/A/**
0N/A * Updates curPollTimeout according to the aging algorithm.
0N/A * @param timeout_control Either TIMEOUT_TIMEDOUT or TIMEOUT_EVENTS
0N/A */
0N/Astatic void update_poll_timeout(int timeout_control) {
0N/A PRINT2("tout: %d\n", timeout_control);
0N/A
0N/A // If static_poll_timeout is set, curPollTimeout has the fixed value
0N/A if (static_poll_timeout != 0) return;
0N/A
0N/A // Update it otherwise
3876N/A
3876N/A switch(awt_poll_alg) {
3876N/A case AWT_POLL_AGING_SLOW:
3876N/A if (timeout_control == TIMEOUT_TIMEDOUT) {
3876N/A /* add 1/4 (plus 1, in case the division truncates to 0) */
3876N/A curPollTimeout += ((curPollTimeout>>2) + 1);
3876N/A curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);
3876N/A } else if (timeout_control == TIMEOUT_EVENTS) {
3876N/A /* subtract 1/4 (plus 1, in case the division truncates to 0) */
3876N/A curPollTimeout -= ((curPollTimeout>>2) + 1);
3876N/A curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, curPollTimeout);
3876N/A }
3876N/A break;
3876N/A case AWT_POLL_AGING_FAST:
3876N/A if (timeout_control == TIMEOUT_TIMEDOUT) {
3876N/A curPollTimeout += ((curPollTimeout>>2) + 1);
3876N/A curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);
3876N/A if((int)curPollTimeout > AWT_POLL_THRESHOLD || (int)curPollTimeout == AWT_POLL_BLOCK)
3876N/A curPollTimeout = AWT_POLL_BLOCK;
3876N/A } else if (timeout_control == TIMEOUT_EVENTS) {
3876N/A curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, 1);
3876N/A }
3876N/A break;
0N/A }
0N/A}
0N/A
0N/A/*
0N/A * Gets the best timeout for the next call to poll().
0N/A *
0N/A * @param nextTaskTime -1, if there are no tasks; next time when
0N/A * timeout task needs to be run, in millis(of currentTimeMillis)
0N/A */
0N/Astatic uint32_t get_poll_timeout(jlong nextTaskTime)
0N/A{
3876N/A uint32_t ret_timeout;
3876N/A uint32_t timeout;
3876N/A uint32_t taskTimeout;
3876N/A uint32_t flushTimeout;
3876N/A
0N/A jlong curTime = awtJNI_TimeMillis();
3876N/A timeout = curPollTimeout;
3876N/A switch(awt_poll_alg) {
3876N/A case AWT_POLL_AGING_SLOW:
3876N/A case AWT_POLL_AGING_FAST:
3876N/A taskTimeout = (nextTaskTime == -1) ? AWT_MAX_POLL_TIMEOUT : (uint32_t)max(0, (int32_t)(nextTaskTime - curTime));
3876N/A flushTimeout = (awt_next_flush_time > 0) ? (uint32_t)max(0, (int32_t)(awt_next_flush_time - curTime)) : AWT_MAX_POLL_TIMEOUT;
3876N/A
3876N/A PRINT2("to: %d, ft: %d, to: %d, tt: %d, mil: %d\n", taskTimeout, flushTimeout, timeout, (int)nextTaskTime, (int)curTime);
0N/A
3876N/A // Adjust timeout to flush_time and task_time
3876N/A ret_timeout = min(flushTimeout, min(taskTimeout, timeout));
3876N/A if((int)curPollTimeout == AWT_POLL_BLOCK)
3876N/A ret_timeout = AWT_POLL_BLOCK;
3876N/A break;
0N/A
3876N/A case AWT_POLL_FALSE:
3876N/A ret_timeout = (nextTaskTime > curTime) ?
3876N/A (nextTaskTime - curTime) :
3876N/A ((nextTaskTime == -1) ? -1 : 0);
3876N/A break;
3876N/A }
3876N/A
3876N/A return ret_timeout;
3876N/A
3876N/A} /* get_poll_timeout() */
0N/A
0N/A/*
0N/A * Waits for X/Xt events to appear on the pipe. Returns only when
0N/A * it is likely (but not definite) that there are events waiting to
0N/A * be processed.
0N/A *
0N/A * This routine also flushes the outgoing X queue, when the
0N/A * awt_next_flush_time has been reached.
0N/A *
0N/A * If fdAWTPipe is greater or equal than zero the routine also
0N/A * checks if there are events pending on the putback queue.
0N/A */
0N/Avoid
0N/AwaitForEvents(JNIEnv *env, jlong nextTaskTime) {
0N/A performPoll(env, nextTaskTime);
0N/A if ((awt_next_flush_time > 0) && (awtJNI_TimeMillis() >= awt_next_flush_time)) {
0N/A XFlush(awt_display);
0N/A awt_last_flush_time = awt_next_flush_time;
0N/A awt_next_flush_time = 0LL;
0N/A }
0N/A} /* waitForEvents() */
0N/A
0N/AJNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_waitForEvents (JNIEnv *env, jclass class, jlong nextTaskTime) {
0N/A waitForEvents(env, nextTaskTime);
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1toolkit_1init (JNIEnv *env, jclass class) {
0N/A awt_MainThread = pthread_self();
0N/A
0N/A awt_pipe_init();
0N/A readEnv();
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1output_1flush (JNIEnv *env, jclass class) {
0N/A awt_output_flush();
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_wakeup_1poll (JNIEnv *env, jclass class) {
0N/A wakeUp();
0N/A}
0N/A
0N/A/*
0N/A * Polls both the X pipe and our AWT utility pipe. Returns
0N/A * when there is data on one of the pipes, or the operation times
0N/A * out.
0N/A *
0N/A * Not all Xt events come across the X pipe (e.g., timers
0N/A * and alternate inputs), so we must time out every now and
0N/A * then to check the Xt event queue.
0N/A *
0N/A * The fdAWTPipe will be empty when this returns.
0N/A */
0N/Astatic void
0N/AperformPoll(JNIEnv *env, jlong nextTaskTime) {
0N/A static Bool pollFdsInited = False;
0N/A static char read_buf[AWT_POLL_BUFSIZE + 1]; /* dummy buf to empty pipe */
0N/A
0N/A uint32_t timeout = get_poll_timeout(nextTaskTime);
0N/A int32_t result;
0N/A
0N/A if (!pollFdsInited) {
0N/A pollFds[0].fd = ConnectionNumber(awt_display);
0N/A pollFds[0].events = POLLRDNORM;
0N/A pollFds[0].revents = 0;
0N/A
0N/A pollFds[1].fd = AWT_READPIPE;
0N/A pollFds[1].events = POLLRDNORM;
0N/A pollFds[1].revents = 0;
0N/A pollFdsInited = True;
0N/A } else {
0N/A pollFds[0].revents = 0;
0N/A pollFds[1].revents = 0;
0N/A }
0N/A
0N/A AWT_NOFLUSH_UNLOCK();
0N/A
0N/A /* ACTUALLY DO THE POLL() */
0N/A if (timeout == 0) {
0N/A // be sure other threads get a chance
0N/A awtJNI_ThreadYield(env);
0N/A }
0N/A
0N/A if (tracing) poll_sleep_time = awtJNI_TimeMillis();
0N/A result = poll( pollFds, 2, (int32_t) timeout );
0N/A if (tracing) poll_wakeup_time = awtJNI_TimeMillis();
0N/A PRINT("%d of %d, res: %d\n", (int)(poll_wakeup_time-poll_sleep_time), (int)timeout, result);
0N/A
0N/A AWT_LOCK();
0N/A if (result == 0) {
0N/A /* poll() timed out -- update timeout value */
0N/A update_poll_timeout(TIMEOUT_TIMEDOUT);
3876N/A PRINT2("%s(): TIMEOUT_TIMEDOUT curPollTimeout = %d \n",
3876N/A performPoll, curPollTimeout);
0N/A }
0N/A if (pollFds[1].revents) {
0N/A int count;
0N/A PRINT("Woke up\n");
0N/A /* There is data on the AWT pipe - empty it */
0N/A do {
0N/A count = read(AWT_READPIPE, read_buf, AWT_POLL_BUFSIZE );
0N/A } while (count == AWT_POLL_BUFSIZE );
3876N/A PRINT2("%s(): data on the AWT pipe: curPollTimeout = %d \n",
3876N/A performPoll, curPollTimeout);
0N/A }
0N/A if (pollFds[0].revents) {
0N/A // Events in X pipe
0N/A update_poll_timeout(TIMEOUT_EVENTS);
3876N/A PRINT2("%s(): TIMEOUT_EVENTS curPollTimeout = %ld \n",
3876N/A performPoll, curPollTimeout);
0N/A }
0N/A return;
0N/A
0N/A} /* performPoll() */
0N/A
0N/A/**
0N/A * Schedules next auto-flush event or performs forced flush depending
0N/A * on the time of the previous flush.
0N/A */
0N/Avoid awt_output_flush() {
0N/A if (awt_next_flush_time == 0) {
0N/A JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
0N/A
0N/A jlong curTime = awtJNI_TimeMillis(); // current time
0N/A jlong l_awt_last_flush_time = awt_last_flush_time; // last time we flushed queue
0N/A jlong next_flush_time = l_awt_last_flush_time + AWT_FLUSH_TIMEOUT;
0N/A
0N/A if (curTime >= next_flush_time) {
0N/A // Enough time passed from last flush
0N/A PRINT("f1\n");
0N/A AWT_LOCK();
0N/A XFlush(awt_display);
0N/A awt_last_flush_time = curTime;
0N/A AWT_NOFLUSH_UNLOCK();
0N/A } else {
0N/A awt_next_flush_time = next_flush_time;
0N/A PRINT("f2\n");
0N/A wakeUp();
0N/A }
0N/A }
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Wakes-up poll() in performPoll
0N/A */
0N/Astatic void wakeUp() {
0N/A static char wakeUp_char = 'p';
0N/A if (!isMainThread() && awt_pipe_inited) {
0N/A write ( AWT_WRITEPIPE, &wakeUp_char, 1 );
0N/A }
0N/A}
0N/A
0N/A
0N/A/* ========================== End poll section ================================= */
0N/A
0N/A/*
0N/A * Class: java_awt_KeyboardFocusManager
0N/A * Method: initIDs
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_KeyboardFocusManager_initIDs
0N/A (JNIEnv *env, jclass cls)
0N/A{
0N/A}
0N/A
0N/A/*
0N/A * Class: sun_awt_X11_XToolkit
0N/A * Method: getEnv
0N/A * Signature: (Ljava/lang/String;)Ljava/lang/String;
0N/A */
0N/AJNIEXPORT jstring JNICALL Java_sun_awt_X11_XToolkit_getEnv
0N/A(JNIEnv *env , jclass clazz, jstring key) {
0N/A char *ptr = NULL;
0N/A const char *keystr = NULL;
0N/A jstring ret = NULL;
0N/A
0N/A keystr = JNU_GetStringPlatformChars(env, key, NULL);
1894N/A if (keystr) {
0N/A ptr = getenv(keystr);
0N/A if (ptr) {
0N/A ret = JNU_NewStringPlatform(env, (const char *) ptr);
0N/A }
0N/A JNU_ReleaseStringPlatformChars(env, key, (const char*)keystr);
0N/A }
0N/A return ret;
0N/A}
0N/A
0N/A#ifdef __linux__
0N/Avoid print_stack(void)
0N/A{
0N/A void *array[10];
0N/A size_t size;
0N/A char **strings;
0N/A size_t i;
0N/A
0N/A size = backtrace (array, 10);
0N/A strings = backtrace_symbols (array, size);
0N/A
0N/A fprintf (stderr, "Obtained %zd stack frames.\n", size);
0N/A
0N/A for (i = 0; i < size; i++)
0N/A fprintf (stderr, "%s\n", strings[i]);
0N/A
0N/A free (strings);
0N/A}
0N/A#endif
0N/A
0N/AWindow get_xawt_root_shell(JNIEnv *env) {
0N/A static jclass classXRootWindow = NULL;
0N/A static jmethodID methodGetXRootWindow = NULL;
0N/A static Window xawt_root_shell = None;
0N/A
0N/A if (xawt_root_shell == None){
0N/A if (classXRootWindow == NULL){
0N/A jclass cls_tmp = (*env)->FindClass(env, "sun/awt/X11/XRootWindow");
3290N/A if (!JNU_IsNull(env, cls_tmp)) {
3290N/A classXRootWindow = (jclass)(*env)->NewGlobalRef(env, cls_tmp);
3290N/A (*env)->DeleteLocalRef(env, cls_tmp);
3290N/A }
0N/A }
0N/A if( classXRootWindow != NULL) {
0N/A methodGetXRootWindow = (*env)->GetStaticMethodID(env, classXRootWindow, "getXRootWindow", "()J");
0N/A }
0N/A if( classXRootWindow != NULL && methodGetXRootWindow !=NULL ) {
0N/A xawt_root_shell = (Window) (*env)->CallStaticLongMethod(env, classXRootWindow, methodGetXRootWindow);
0N/A }
0N/A if ((*env)->ExceptionCheck(env)) {
0N/A (*env)->ExceptionDescribe(env);
0N/A (*env)->ExceptionClear(env);
0N/A }
0N/A }
0N/A return xawt_root_shell;
0N/A}
0N/A
0N/A/*
0N/A * Old, compatibility, backdoor for DT. This is a different
0N/A * implementation. It keeps the signature, but acts on
0N/A * awt_root_shell, not the frame passed as an argument. Note, that
0N/A * the code that uses the old backdoor doesn't work correctly with
0N/A * gnome session proxy that checks for WM_COMMAND when the window is
0N/A * firts mapped, because DT code calls this old backdoor *after* the
0N/A * frame is shown or it would get NPE with old AWT (previous
0N/A * implementation of this backdoor) otherwise. Old style session
0N/A * managers (e.g. CDE) that check WM_COMMAND only during session
0N/A * checkpoint should work fine, though.
0N/A *
0N/A * NB: The function name looks deceptively like a JNI native method
0N/A * name. It's not! It's just a plain function.
0N/A */
0N/A
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_motif_XsessionWMcommand(JNIEnv *env, jobject this,
0N/A jobject frame, jstring jcommand)
0N/A{
0N/A const char *command;
0N/A XTextProperty text_prop;
0N/A char *c[1];
0N/A int32_t status;
0N/A Window xawt_root_window;
0N/A
0N/A AWT_LOCK();
0N/A xawt_root_window = get_xawt_root_shell(env);
0N/A
0N/A if ( xawt_root_window == None ) {
0N/A JNU_ThrowNullPointerException(env, "AWT root shell is unrealized");
0N/A AWT_UNLOCK();
0N/A return;
0N/A }
0N/A
0N/A command = (char *) JNU_GetStringPlatformChars(env, jcommand, NULL);
0N/A c[0] = (char *)command;
0N/A status = XmbTextListToTextProperty(awt_display, c, 1,
0N/A XStdICCTextStyle, &text_prop);
0N/A
0N/A if (status == Success || status > 0) {
0N/A XSetTextProperty(awt_display, xawt_root_window,
0N/A &text_prop, XA_WM_COMMAND);
0N/A if (text_prop.value != NULL)
0N/A XFree(text_prop.value);
0N/A }
0N/A JNU_ReleaseStringPlatformChars(env, jcommand, command);
0N/A AWT_UNLOCK();
0N/A}
0N/A
0N/A
0N/A/*
0N/A * New DT backdoor to set WM_COMMAND. New code should use this
0N/A * backdoor and call it *before* the first frame is shown so that
0N/A * gnome session proxy can correctly handle it.
0N/A *
0N/A * NB: The function name looks deceptively like a JNI native method
0N/A * name. It's not! It's just a plain function.
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_sun_awt_motif_XsessionWMcommand_New(JNIEnv *env, jobjectArray jargv)
0N/A{
0N/A static const char empty[] = "";
0N/A
0N/A int argc;
0N/A const char **cargv;
0N/A XTextProperty text_prop;
0N/A int status;
0N/A int i;
0N/A Window xawt_root_window;
0N/A
0N/A AWT_LOCK();
0N/A xawt_root_window = get_xawt_root_shell(env);
0N/A
0N/A if (xawt_root_window == None) {
0N/A JNU_ThrowNullPointerException(env, "AWT root shell is unrealized");
0N/A AWT_UNLOCK();
0N/A return;
0N/A }
0N/A
0N/A argc = (int)(*env)->GetArrayLength(env, jargv);
0N/A if (argc == 0) {
0N/A AWT_UNLOCK();
0N/A return;
0N/A }
0N/A
0N/A /* array of C strings */
0N/A cargv = (const char **)calloc(argc, sizeof(char *));
0N/A if (cargv == NULL) {
0N/A JNU_ThrowOutOfMemoryError(env, "Unable to allocate cargv");
0N/A AWT_UNLOCK();
0N/A return;
0N/A }
0N/A
0N/A /* fill C array with platform chars of java strings */
0N/A for (i = 0; i < argc; ++i) {
0N/A jstring js;
0N/A const char *cs;
0N/A
0N/A cs = NULL;
0N/A js = (*env)->GetObjectArrayElement(env, jargv, i);
0N/A if (js != NULL) {
0N/A cs = JNU_GetStringPlatformChars(env, js, NULL);
0N/A }
0N/A if (cs == NULL) {
0N/A cs = empty;
0N/A }
0N/A cargv[i] = cs;
0N/A (*env)->DeleteLocalRef(env, js);
0N/A }
0N/A
0N/A /* grr, X prototype doesn't declare cargv as const, thought it really is */
0N/A status = XmbTextListToTextProperty(awt_display, (char **)cargv, argc,
0N/A XStdICCTextStyle, &text_prop);
0N/A if (status < 0) {
0N/A switch (status) {
0N/A case XNoMemory:
0N/A JNU_ThrowOutOfMemoryError(env,
0N/A "XmbTextListToTextProperty: XNoMemory");
0N/A break;
0N/A case XLocaleNotSupported:
0N/A JNU_ThrowInternalError(env,
0N/A "XmbTextListToTextProperty: XLocaleNotSupported");
0N/A break;
0N/A case XConverterNotFound:
0N/A JNU_ThrowNullPointerException(env,
0N/A "XmbTextListToTextProperty: XConverterNotFound");
0N/A break;
0N/A default:
0N/A JNU_ThrowInternalError(env,
0N/A "XmbTextListToTextProperty: unknown error");
0N/A }
0N/A } else {
0N/A
0N/A XSetTextProperty(awt_display, xawt_root_window,
0N/A &text_prop, XA_WM_COMMAND);
0N/A }
0N/A
0N/A for (i = 0; i < argc; ++i) {
0N/A jstring js;
0N/A
0N/A if (cargv[i] == empty)
0N/A continue;
0N/A
0N/A js = (*env)->GetObjectArrayElement(env, jargv, i);
0N/A JNU_ReleaseStringPlatformChars(env, js, cargv[i]);
0N/A (*env)->DeleteLocalRef(env, js);
0N/A }
0N/A if (text_prop.value != NULL)
0N/A XFree(text_prop.value);
0N/A AWT_UNLOCK();
0N/A}
0N/A
0N/A/*
0N/A * Class: java_awt_TrayIcon
0N/A * Method: initIDs
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs(JNIEnv *env , jclass clazz)
0N/A{
0N/A}
0N/A
0N/A
0N/A/*
0N/A * Class: java_awt_Cursor
0N/A * Method: finalizeImpl
0N/A * Signature: ()V
0N/A */
0N/AJNIEXPORT void JNICALL
0N/AJava_java_awt_Cursor_finalizeImpl(JNIEnv *env, jclass clazz, jlong pData)
0N/A{
0N/A Cursor xcursor;
0N/A
0N/A xcursor = (Cursor)pData;
0N/A if (xcursor != None) {
0N/A AWT_LOCK();
0N/A XFreeCursor(awt_display, xcursor);
0N/A AWT_UNLOCK();
0N/A }
0N/A}
1224N/A
1224N/A
1224N/A/*
1224N/A * Class: sun_awt_X11_XToolkit
1224N/A * Method: getNumberOfButtonsImpl
1224N/A * Signature: ()I
1224N/A */
1224N/AJNIEXPORT jint JNICALL Java_sun_awt_X11_XToolkit_getNumberOfButtonsImpl
1224N/A(JNIEnv * env, jobject cls){
1224N/A if (num_buttons == 0) {
1224N/A num_buttons = getNumButtons();
1224N/A }
1224N/A return num_buttons;
1224N/A}
1224N/A
1224N/Aint32_t getNumButtons() {
1224N/A int32_t major_opcode, first_event, first_error;
1224N/A int32_t xinputAvailable;
1224N/A int32_t numDevices, devIdx, clsIdx;
1224N/A XDeviceInfo* devices;
1224N/A XDeviceInfo* aDevice;
1224N/A XButtonInfo* bInfo;
1224N/A int32_t local_num_buttons = 0;
1224N/A
1224N/A /* 4700242:
1224N/A * If XTest is asked to press a non-existant mouse button
1224N/A * (i.e. press Button3 on a system configured with a 2-button mouse),
1224N/A * then a crash may happen. To avoid this, we use the XInput
1224N/A * extension to query for the number of buttons on the XPointer, and check
1224N/A * before calling XTestFakeButtonEvent().
1224N/A */
1224N/A xinputAvailable = XQueryExtension(awt_display, INAME, &major_opcode, &first_event, &first_error);
1224N/A DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XINPUT) returns major_opcode = %d, first_event = %d, first_error = %d",
1224N/A major_opcode, first_event, first_error);
1224N/A if (xinputAvailable) {
1224N/A devices = XListInputDevices(awt_display, &numDevices);
1224N/A for (devIdx = 0; devIdx < numDevices; devIdx++) {
1224N/A aDevice = &(devices[devIdx]);
1225N/A#ifdef IsXExtensionPointer
1224N/A if (aDevice->use == IsXExtensionPointer) {
1224N/A for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) {
1224N/A if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) {
1224N/A bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx]));
1224N/A local_num_buttons = bInfo->num_buttons;
1224N/A DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons);
1224N/A break;
1224N/A }
1224N/A }
1224N/A break;
1224N/A }
1225N/A#endif
1224N/A if (local_num_buttons <= 0 ) {
1224N/A if (aDevice->use == IsXPointer) {
1224N/A for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) {
1224N/A if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) {
1224N/A bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx]));
1224N/A local_num_buttons = bInfo->num_buttons;
1224N/A DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons);
1224N/A break;
1224N/A }
1224N/A }
1224N/A break;
1224N/A }
1224N/A }
1224N/A }
1224N/A
1224N/A XFreeDeviceList(devices);
1224N/A }
1224N/A else {
1224N/A DTRACE_PRINTLN1("RobotPeer: XINPUT extension is unavailable, assuming %d mouse buttons", num_buttons);
1224N/A }
1224N/A if (local_num_buttons == 0 ) {
1224N/A local_num_buttons = 3;
1224N/A }
1224N/A
1224N/A return local_num_buttons;
1224N/A}
4514N/A
4514N/A/*
4514N/A * Class: sun_awt_X11_XWindowPeer
4514N/A * Method: getJvmPID
4514N/A * Signature: ()I
4514N/A */
4514N/AJNIEXPORT jint JNICALL Java_sun_awt_X11_XWindowPeer_getJvmPID
4514N/A(JNIEnv *env, jclass cls)
4514N/A{
4514N/A /* Return the JVM's PID. */
4514N/A return getpid();
4514N/A}
4514N/A
4514N/A#ifndef HOST_NAME_MAX
4514N/A#define HOST_NAME_MAX 1024 /* Overestimated */
4514N/A#endif
4514N/A
4514N/A/*
4514N/A * Class: sun_awt_X11_XWindowPeer
4514N/A * Method: getLocalHostname
4514N/A * Signature: ()Ljava/lang/String;
4514N/A */
4514N/AJNIEXPORT jstring JNICALL Java_sun_awt_X11_XWindowPeer_getLocalHostname
4514N/A(JNIEnv *env, jclass cls)
4514N/A{
4514N/A /* Return the machine's FQDN. */
4514N/A char hostname[HOST_NAME_MAX + 1];
4514N/A if (gethostname(hostname, HOST_NAME_MAX + 1) == 0) {
4514N/A hostname[HOST_NAME_MAX] = '\0';
4514N/A jstring res = (*env)->NewStringUTF(env, hostname);
4514N/A return res;
4514N/A }
4514N/A
4514N/A return (jstring)NULL;
4514N/A}