Path: blob/master/src/java.desktop/unix/native/libawt_xawt/xawt/XToolkit.c
41153 views
/*1* Copyright (c) 2002, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifdef HEADLESS26#error This file should not be included in headless library27#endif2829#include <X11/Xlib.h>30#include <X11/Xutil.h>31#include <X11/Xos.h>32#include <X11/Xatom.h>3334#include <jvm.h>35#include <jni.h>36#include <jlong.h>37#include <jni_util.h>3839#include "awt_p.h"40#include "awt_Component.h"41#include "awt_MenuComponent.h"42#include "awt_util.h"4344#include "sun_awt_X11_XToolkit.h"45#include "java_awt_SystemColor.h"46#include "java_awt_TrayIcon.h"47#include <X11/extensions/XTest.h>4849#include <unistd.h>5051uint32_t awt_NumLockMask = 0;52Boolean awt_ModLockIsShiftLock = False;5354static int32_t num_buttons = 0;55int32_t getNumButtons();5657extern JavaVM *jvm;5859// Tracing level60static int tracing = 0;61#ifdef PRINT62#undef PRINT63#endif64#ifdef PRINT265#undef PRINT266#endif6768#define PRINT if (tracing) printf69#define PRINT2 if (tracing > 1) printf707172struct ComponentIDs componentIDs;7374struct MenuComponentIDs menuComponentIDs;7576extern Display* awt_init_Display(JNIEnv *env, jobject this);77extern void freeNativeStringArray(char **array, jsize length);78extern char** stringArrayToNative(JNIEnv *env, jobjectArray array, jsize * ret_length);7980/* This function gets called from the static initializer for FileDialog.java81to initialize the fieldIDs for fields that may be accessed from C */8283JNIEXPORT void JNICALL84Java_java_awt_FileDialog_initIDs85(JNIEnv *env, jclass cls)86{8788}8990JNIEXPORT void JNICALL91Java_sun_awt_X11_XToolkit_initIDs92(JNIEnv *env, jclass clazz)93{94jfieldID fid = (*env)->GetStaticFieldID(env, clazz, "numLockMask", "I");95CHECK_NULL(fid);96awt_NumLockMask = (*env)->GetStaticIntField(env, clazz, fid);97DTRACE_PRINTLN1("awt_NumLockMask = %u", awt_NumLockMask);98fid = (*env)->GetStaticFieldID(env, clazz, "modLockIsShiftLock", "I");99CHECK_NULL(fid);100awt_ModLockIsShiftLock = (*env)->GetStaticIntField(env, clazz, fid) != 0 ? True : False;101}102103/*104* Class: sun_awt_X11_XToolkit105* Method: getTrayIconDisplayTimeout106* Signature: ()J107*/108JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getTrayIconDisplayTimeout109(JNIEnv *env, jclass clazz)110{111return (jlong) 2000;112}113114/*115* Class: sun_awt_X11_XToolkit116* Method: getDefaultXColormap117* Signature: ()J118*/119JNIEXPORT jlong JNICALL Java_sun_awt_X11_XToolkit_getDefaultXColormap120(JNIEnv *env, jclass clazz)121{122AWT_LOCK();123AwtGraphicsConfigDataPtr defaultConfig =124getDefaultConfig(DefaultScreen(awt_display));125AWT_UNLOCK();126return (jlong) defaultConfig->awt_cmap;127}128129JNIEXPORT jint JNICALL130DEF_JNI_OnLoad(JavaVM *vm, void *reserved)131{132jvm = vm;133134//Set the gtk backend to x11 on all the systems135putenv("GDK_BACKEND=x11");136137return JNI_VERSION_1_2;138}139140/*141* Class: sun_awt_X11_XToolkit142* Method: nativeLoadSystemColors143* Signature: ([I)V144*/145JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_nativeLoadSystemColors146(JNIEnv *env, jobject this, jintArray systemColors)147{148AWT_LOCK();149AwtGraphicsConfigDataPtr defaultConfig =150getDefaultConfig(DefaultScreen(awt_display));151awtJNI_CreateColorData(env, defaultConfig, 1);152AWT_UNLOCK();153}154155JNIEXPORT void JNICALL156Java_java_awt_Component_initIDs157(JNIEnv *env, jclass cls)158{159jclass keyclass = NULL;160161162componentIDs.x = (*env)->GetFieldID(env, cls, "x", "I");163CHECK_NULL(componentIDs.x);164componentIDs.y = (*env)->GetFieldID(env, cls, "y", "I");165CHECK_NULL(componentIDs.y);166componentIDs.width = (*env)->GetFieldID(env, cls, "width", "I");167CHECK_NULL(componentIDs.width);168componentIDs.height = (*env)->GetFieldID(env, cls, "height", "I");169CHECK_NULL(componentIDs.height);170componentIDs.isPacked = (*env)->GetFieldID(env, cls, "isPacked", "Z");171CHECK_NULL(componentIDs.isPacked);172componentIDs.peer =173(*env)->GetFieldID(env, cls, "peer", "Ljava/awt/peer/ComponentPeer;");174CHECK_NULL(componentIDs.peer);175componentIDs.background =176(*env)->GetFieldID(env, cls, "background", "Ljava/awt/Color;");177CHECK_NULL(componentIDs.background);178componentIDs.foreground =179(*env)->GetFieldID(env, cls, "foreground", "Ljava/awt/Color;");180CHECK_NULL(componentIDs.foreground);181componentIDs.graphicsConfig =182(*env)->GetFieldID(env, cls, "graphicsConfig",183"Ljava/awt/GraphicsConfiguration;");184CHECK_NULL(componentIDs.graphicsConfig);185componentIDs.name =186(*env)->GetFieldID(env, cls, "name", "Ljava/lang/String;");187CHECK_NULL(componentIDs.name);188189/* Use _NoClientCode() methods for trusted methods, so that we190* know that we are not invoking client code on trusted threads191*/192componentIDs.getParent =193(*env)->GetMethodID(env, cls, "getParent_NoClientCode",194"()Ljava/awt/Container;");195CHECK_NULL(componentIDs.getParent);196197componentIDs.getLocationOnScreen =198(*env)->GetMethodID(env, cls, "getLocationOnScreen_NoTreeLock",199"()Ljava/awt/Point;");200CHECK_NULL(componentIDs.getLocationOnScreen);201202keyclass = (*env)->FindClass(env, "java/awt/event/KeyEvent");203CHECK_NULL(keyclass);204205componentIDs.isProxyActive =206(*env)->GetFieldID(env, keyclass, "isProxyActive",207"Z");208CHECK_NULL(componentIDs.isProxyActive);209210componentIDs.appContext =211(*env)->GetFieldID(env, cls, "appContext",212"Lsun/awt/AppContext;");213214(*env)->DeleteLocalRef(env, keyclass);215}216217218JNIEXPORT void JNICALL219Java_java_awt_Container_initIDs220(JNIEnv *env, jclass cls)221{222223}224225226JNIEXPORT void JNICALL227Java_java_awt_Button_initIDs228(JNIEnv *env, jclass cls)229{230231}232233JNIEXPORT void JNICALL234Java_java_awt_Scrollbar_initIDs235(JNIEnv *env, jclass cls)236{237238}239240241JNIEXPORT void JNICALL242Java_java_awt_Window_initIDs243(JNIEnv *env, jclass cls)244{245246}247248JNIEXPORT void JNICALL249Java_java_awt_Frame_initIDs250(JNIEnv *env, jclass cls)251{252253}254255256JNIEXPORT void JNICALL257Java_java_awt_MenuComponent_initIDs(JNIEnv *env, jclass cls)258{259menuComponentIDs.appContext =260(*env)->GetFieldID(env, cls, "appContext", "Lsun/awt/AppContext;");261}262263JNIEXPORT void JNICALL264Java_java_awt_Cursor_initIDs(JNIEnv *env, jclass cls)265{266}267268269JNIEXPORT void JNICALL Java_java_awt_MenuItem_initIDs270(JNIEnv *env, jclass cls)271{272}273274275JNIEXPORT void JNICALL Java_java_awt_Menu_initIDs276(JNIEnv *env, jclass cls)277{278}279280JNIEXPORT void JNICALL281Java_java_awt_TextArea_initIDs282(JNIEnv *env, jclass cls)283{284}285286287JNIEXPORT void JNICALL288Java_java_awt_Checkbox_initIDs289(JNIEnv *env, jclass cls)290{291}292293294JNIEXPORT void JNICALL Java_java_awt_ScrollPane_initIDs295(JNIEnv *env, jclass cls)296{297}298299JNIEXPORT void JNICALL300Java_java_awt_TextField_initIDs301(JNIEnv *env, jclass cls)302{303}304305JNIEXPORT void JNICALL Java_java_awt_Dialog_initIDs (JNIEnv *env, jclass cls)306{307}308309310/* ========================== Begin poll section ================================ */311312// Includes313314#include <sys/time.h>315#include <limits.h>316#include <locale.h>317#include <pthread.h>318319#include <dlfcn.h>320#include <fcntl.h>321322#include <poll.h>323#ifndef POLLRDNORM324#define POLLRDNORM POLLIN325#endif326327// Prototypes328329static void waitForEvents(JNIEnv *, jlong);330static void awt_pipe_init();331static Boolean performPoll(JNIEnv *, jlong);332static void wakeUp();333static void update_poll_timeout(int timeout_control);334static uint32_t get_poll_timeout(jlong nextTaskTime);335336// Defines337338#ifndef bzero339#define bzero(a,b) memset(a, 0, b)340#endif341342#define AWT_POLL_BUFSIZE 100 /* bytes */343#define AWT_READPIPE (awt_pipe_fds[0])344#define AWT_WRITEPIPE (awt_pipe_fds[1])345346#define DEF_AWT_MAX_POLL_TIMEOUT ((uint32_t)500) /* milliseconds */347#define DEF_AWT_FLUSH_TIMEOUT ((uint32_t)100) /* milliseconds */348#define AWT_MIN_POLL_TIMEOUT ((uint32_t)0) /* milliseconds */349350#define TIMEOUT_TIMEDOUT 0351#define TIMEOUT_EVENTS 1352353/* awt_poll_alg - AWT Poll Events Aging Algorithms */354#define AWT_POLL_FALSE 1355#define AWT_POLL_AGING_SLOW 2356#define AWT_POLL_AGING_FAST 3357358#define AWT_POLL_THRESHOLD 1000 // msec, Block if delay is larger359#define AWT_POLL_BLOCK -1 // cause poll() block360361// Static fields362363static int awt_poll_alg = AWT_POLL_AGING_SLOW;364365static uint32_t AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT; /* milliseconds */366static uint32_t AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT; /* milliseconds */367static pthread_t awt_MainThread = 0;368static int32_t awt_pipe_fds[2]; /* fds for wkaeup pipe */369static Boolean awt_pipe_inited = False; /* make sure pipe is initialized before write */370static jlong awt_next_flush_time = 0LL; /* 0 == no scheduled flush */371static jlong awt_last_flush_time = 0LL; /* 0 == no scheduled flush */372static uint32_t curPollTimeout;373static struct pollfd pollFds[2];374static jlong poll_sleep_time = 0LL; // Used for tracing375static jlong poll_wakeup_time = 0LL; // Used for tracing376377// AWT static poll timeout. Zero means "not set", aging algorithm is378// used. Static poll timeout values higher than 50 cause application379// look "slow" - they don't respond to user request fast380// enough. Static poll timeout value less than 10 are usually381// considered by schedulers as zero, so this might cause unnecessary382// CPU consumption by Java. The values between 10 - 50 are suggested383// for single client desktop configurations. For SunRay servers, it384// is highly recomended to use aging algorithm (set static poll timeout385// to 0).386static int32_t static_poll_timeout = 0;387388static Bool isMainThread() {389return awt_MainThread == pthread_self();390}391392/*393* Creates the AWT utility pipe. This pipe exists solely so that394* we can cause the main event thread to wake up from a poll() or395* select() by writing to this pipe.396*/397static void398awt_pipe_init() {399400if (awt_pipe_inited) {401return;402}403404if ( pipe ( awt_pipe_fds ) == 0 )405{406/*407** the write wakes us up from the infinite sleep, which408** then we cause a delay of AWT_FLUSHTIME and then we409** flush.410*/411int32_t flags = 0;412/* set the pipe to be non-blocking */413flags = fcntl ( AWT_READPIPE, F_GETFL, 0 );414fcntl( AWT_READPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );415flags = fcntl ( AWT_WRITEPIPE, F_GETFL, 0 );416fcntl( AWT_WRITEPIPE, F_SETFL, flags | O_NDELAY | O_NONBLOCK );417awt_pipe_inited = True;418}419else420{421AWT_READPIPE = -1;422AWT_WRITEPIPE = -1;423}424425426} /* awt_pipe_init() */427428/**429* Reads environment variables to initialize timeout fields.430*/431static void readEnv() {432char * value;433int tmp_poll_alg;434static Boolean env_read = False;435if (env_read) return;436437env_read = True;438439value = getenv("_AWT_MAX_POLL_TIMEOUT");440if (value != NULL) {441AWT_MAX_POLL_TIMEOUT = atoi(value);442if (AWT_MAX_POLL_TIMEOUT == 0) {443AWT_MAX_POLL_TIMEOUT = DEF_AWT_MAX_POLL_TIMEOUT;444}445}446curPollTimeout = AWT_MAX_POLL_TIMEOUT/2;447448value = getenv("_AWT_FLUSH_TIMEOUT");449if (value != NULL) {450AWT_FLUSH_TIMEOUT = atoi(value);451if (AWT_FLUSH_TIMEOUT == 0) {452AWT_FLUSH_TIMEOUT = DEF_AWT_FLUSH_TIMEOUT;453}454}455456value = getenv("_AWT_POLL_TRACING");457if (value != NULL) {458tracing = atoi(value);459}460461value = getenv("_AWT_STATIC_POLL_TIMEOUT");462if (value != NULL) {463static_poll_timeout = atoi(value);464}465if (static_poll_timeout != 0) {466curPollTimeout = static_poll_timeout;467}468469// non-blocking poll()470value = getenv("_AWT_POLL_ALG");471if (value != NULL) {472tmp_poll_alg = atoi(value);473switch(tmp_poll_alg) {474case AWT_POLL_FALSE:475case AWT_POLL_AGING_SLOW:476case AWT_POLL_AGING_FAST:477awt_poll_alg = tmp_poll_alg;478break;479default:480PRINT("Unknown value of _AWT_POLL_ALG, assuming Slow Aging Algorithm by default");481awt_poll_alg = AWT_POLL_AGING_SLOW;482break;483}484}485}486487/**488* Returns the amount of milliseconds similar to System.currentTimeMillis()489*/490static jlong491awtJNI_TimeMillis(void)492{493struct timeval t;494495gettimeofday(&t, 0);496497return jlong_add(jlong_mul(jint_to_jlong(t.tv_sec), jint_to_jlong(1000)),498jint_to_jlong(t.tv_usec / 1000));499}500501/**502* Updates curPollTimeout according to the aging algorithm.503* @param timeout_control Either TIMEOUT_TIMEDOUT or TIMEOUT_EVENTS504*/505static void update_poll_timeout(int timeout_control) {506PRINT2("tout: %d\n", timeout_control);507508// If static_poll_timeout is set, curPollTimeout has the fixed value509if (static_poll_timeout != 0) return;510511// Update it otherwise512513switch(awt_poll_alg) {514case AWT_POLL_AGING_SLOW:515if (timeout_control == TIMEOUT_TIMEDOUT) {516/* add 1/4 (plus 1, in case the division truncates to 0) */517curPollTimeout += ((curPollTimeout>>2) + 1);518curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);519} else if (timeout_control == TIMEOUT_EVENTS) {520/* subtract 1/4 (plus 1, in case the division truncates to 0) */521if (curPollTimeout > 0) {522curPollTimeout -= ((curPollTimeout>>2) + 1);523curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, curPollTimeout);524}525}526break;527case AWT_POLL_AGING_FAST:528if (timeout_control == TIMEOUT_TIMEDOUT) {529curPollTimeout += ((curPollTimeout>>2) + 1);530curPollTimeout = min(AWT_MAX_POLL_TIMEOUT, curPollTimeout);531if((int)curPollTimeout > AWT_POLL_THRESHOLD || (int)curPollTimeout == AWT_POLL_BLOCK)532curPollTimeout = AWT_POLL_BLOCK;533} else if (timeout_control == TIMEOUT_EVENTS) {534curPollTimeout = max(AWT_MIN_POLL_TIMEOUT, 1);535}536break;537}538}539540/*541* Gets the best timeout for the next call to poll().542*543* @param nextTaskTime -1, if there are no tasks; next time when544* timeout task needs to be run, in millis(of currentTimeMillis)545*/546static uint32_t get_poll_timeout(jlong nextTaskTime)547{548uint32_t ret_timeout = 0;549uint32_t timeout;550uint32_t taskTimeout;551uint32_t flushTimeout;552553jlong curTime = awtJNI_TimeMillis();554timeout = curPollTimeout;555switch(awt_poll_alg) {556case AWT_POLL_AGING_SLOW:557case AWT_POLL_AGING_FAST:558taskTimeout = (nextTaskTime == -1) ? AWT_MAX_POLL_TIMEOUT : (uint32_t)max(0, (int32_t)(nextTaskTime - curTime));559flushTimeout = (awt_next_flush_time > 0) ? (uint32_t)max(0, (int32_t)(awt_next_flush_time - curTime)) : AWT_MAX_POLL_TIMEOUT;560561PRINT2("to: %d, ft: %d, to: %d, tt: %d, mil: %d\n", taskTimeout, flushTimeout, timeout, (int)nextTaskTime, (int)curTime);562563// Adjust timeout to flush_time and task_time564ret_timeout = min(flushTimeout, min(taskTimeout, timeout));565if((int)curPollTimeout == AWT_POLL_BLOCK)566ret_timeout = AWT_POLL_BLOCK;567break;568569case AWT_POLL_FALSE:570ret_timeout = (nextTaskTime > curTime) ?571(nextTaskTime - curTime) :572((nextTaskTime == -1) ? -1 : 0);573break;574}575576return ret_timeout;577578} /* get_poll_timeout() */579580/*581* Waits for X events to appear on the pipe. Returns only when582* it is likely (but not definite) that there are events waiting to583* be processed.584*585* This routine also flushes the outgoing X queue, when the586* awt_next_flush_time has been reached.587*588* If fdAWTPipe is greater or equal than zero the routine also589* checks if there are events pending on the putback queue.590*/591void592waitForEvents(JNIEnv *env, jlong nextTaskTime) {593if (performPoll(env, nextTaskTime)594&& (awt_next_flush_time > 0)595&& (awtJNI_TimeMillis() >= awt_next_flush_time)) {596597XFlush(awt_display);598awt_last_flush_time = awt_next_flush_time;599awt_next_flush_time = 0LL;600}601} /* waitForEvents() */602603JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_waitForEvents (JNIEnv *env, jclass class, jlong nextTaskTime) {604waitForEvents(env, nextTaskTime);605}606607JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1toolkit_1init (JNIEnv *env, jclass class) {608awt_MainThread = pthread_self();609610awt_pipe_init();611readEnv();612}613614JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_awt_1output_1flush (JNIEnv *env, jclass class) {615awt_output_flush();616}617618JNIEXPORT void JNICALL Java_sun_awt_X11_XToolkit_wakeup_1poll (JNIEnv *env, jclass class) {619wakeUp();620}621622/*623* Polls both the X pipe and our AWT utility pipe. Returns624* when there is data on one of the pipes, or the operation times625* out.626*627* Not all Xt events come across the X pipe (e.g., timers628* and alternate inputs), so we must time out every now and629* then to check the Xt event queue.630*631* The fdAWTPipe will be empty when this returns.632*/633static Boolean634performPoll(JNIEnv *env, jlong nextTaskTime) {635static Bool pollFdsInited = False;636static char read_buf[AWT_POLL_BUFSIZE + 1]; /* dummy buf to empty pipe */637638uint32_t timeout = get_poll_timeout(nextTaskTime);639int32_t result;640641if (!pollFdsInited) {642pollFds[0].fd = ConnectionNumber(awt_display);643pollFds[0].events = POLLRDNORM;644pollFds[0].revents = 0;645646pollFds[1].fd = AWT_READPIPE;647pollFds[1].events = POLLRDNORM;648pollFds[1].revents = 0;649pollFdsInited = True;650} else {651pollFds[0].revents = 0;652pollFds[1].revents = 0;653}654655AWT_NOFLUSH_UNLOCK();656657/* ACTUALLY DO THE POLL() */658if (timeout == 0) {659// be sure other threads get a chance660if (!awtJNI_ThreadYield(env)) {661return FALSE;662}663}664665if (tracing) poll_sleep_time = awtJNI_TimeMillis();666result = poll( pollFds, 2, (int32_t) timeout );667if (tracing) poll_wakeup_time = awtJNI_TimeMillis();668PRINT("%d of %d, res: %d\n", (int)(poll_wakeup_time-poll_sleep_time), (int)timeout, result);669670AWT_LOCK();671if (result == 0) {672/* poll() timed out -- update timeout value */673update_poll_timeout(TIMEOUT_TIMEDOUT);674PRINT2("performPoll(): TIMEOUT_TIMEDOUT curPollTimeout = %d \n", curPollTimeout);675}676if (pollFds[1].revents) {677int count;678PRINT("Woke up\n");679/* There is data on the AWT pipe - empty it */680do {681count = read(AWT_READPIPE, read_buf, AWT_POLL_BUFSIZE );682} while (count == AWT_POLL_BUFSIZE );683PRINT2("performPoll(): data on the AWT pipe: curPollTimeout = %d \n", curPollTimeout);684}685if (pollFds[0].revents) {686// Events in X pipe687update_poll_timeout(TIMEOUT_EVENTS);688PRINT2("performPoll(): TIMEOUT_EVENTS curPollTimeout = %d \n", curPollTimeout);689}690return TRUE;691692} /* performPoll() */693694/**695* Schedules next auto-flush event or performs forced flush depending696* on the time of the previous flush.697*/698void awt_output_flush() {699if (awt_next_flush_time == 0) {700JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);701702jlong curTime = awtJNI_TimeMillis(); // current time703jlong l_awt_last_flush_time = awt_last_flush_time; // last time we flushed queue704jlong next_flush_time = l_awt_last_flush_time + AWT_FLUSH_TIMEOUT;705706if (curTime >= next_flush_time) {707// Enough time passed from last flush708PRINT("f1\n");709AWT_LOCK();710XFlush(awt_display);711awt_last_flush_time = curTime;712AWT_NOFLUSH_UNLOCK();713} else {714awt_next_flush_time = next_flush_time;715PRINT("f2\n");716wakeUp();717}718}719}720721722/**723* Wakes-up poll() in performPoll724*/725static void wakeUp() {726static char wakeUp_char = 'p';727if (!isMainThread() && awt_pipe_inited) {728write ( AWT_WRITEPIPE, &wakeUp_char, 1 );729}730}731732733/* ========================== End poll section ================================= */734735/*736* Class: java_awt_KeyboardFocusManager737* Method: initIDs738* Signature: ()V739*/740JNIEXPORT void JNICALL741Java_java_awt_KeyboardFocusManager_initIDs742(JNIEnv *env, jclass cls)743{744}745746/*747* Class: sun_awt_X11_XToolkit748* Method: getEnv749* Signature: (Ljava/lang/String;)Ljava/lang/String;750*/751JNIEXPORT jstring JNICALL Java_sun_awt_X11_XToolkit_getEnv752(JNIEnv *env , jclass clazz, jstring key) {753char *ptr = NULL;754const char *keystr = NULL;755jstring ret = NULL;756757keystr = JNU_GetStringPlatformChars(env, key, NULL);758if (keystr) {759ptr = getenv(keystr);760if (ptr) {761ret = JNU_NewStringPlatform(env, (const char *) ptr);762}763JNU_ReleaseStringPlatformChars(env, key, (const char*)keystr);764}765return ret;766}767768Window get_xawt_root_shell(JNIEnv *env) {769static jclass classXRootWindow = NULL;770static jmethodID methodGetXRootWindow = NULL;771static Window xawt_root_shell = None;772773if (xawt_root_shell == None){774if (classXRootWindow == NULL){775jclass cls_tmp = (*env)->FindClass(env, "sun/awt/X11/XRootWindow");776if (!JNU_IsNull(env, cls_tmp)) {777classXRootWindow = (jclass)(*env)->NewGlobalRef(env, cls_tmp);778(*env)->DeleteLocalRef(env, cls_tmp);779}780}781if( classXRootWindow != NULL) {782methodGetXRootWindow = (*env)->GetStaticMethodID(env, classXRootWindow, "getXRootWindow", "()J");783}784if( classXRootWindow != NULL && methodGetXRootWindow !=NULL ) {785xawt_root_shell = (Window) (*env)->CallStaticLongMethod(env, classXRootWindow, methodGetXRootWindow);786}787if ((*env)->ExceptionCheck(env)) {788(*env)->ExceptionDescribe(env);789(*env)->ExceptionClear(env);790}791}792return xawt_root_shell;793}794795/*796* Class: java_awt_TrayIcon797* Method: initIDs798* Signature: ()V799*/800JNIEXPORT void JNICALL Java_java_awt_TrayIcon_initIDs(JNIEnv *env , jclass clazz)801{802}803804805/*806* Class: java_awt_Cursor807* Method: finalizeImpl808* Signature: ()V809*/810JNIEXPORT void JNICALL811Java_java_awt_Cursor_finalizeImpl(JNIEnv *env, jclass clazz, jlong pData)812{813Cursor xcursor;814815xcursor = (Cursor)pData;816if (xcursor != None) {817AWT_LOCK();818XFreeCursor(awt_display, xcursor);819AWT_UNLOCK();820}821}822823824/*825* Class: sun_awt_X11_XToolkit826* Method: getNumberOfButtonsImpl827* Signature: ()I828*/829JNIEXPORT jint JNICALL Java_sun_awt_X11_XToolkit_getNumberOfButtonsImpl830(JNIEnv * env, jobject cls){831if (num_buttons == 0) {832num_buttons = getNumButtons();833}834return num_buttons;835}836837int32_t getNumButtons() {838int32_t major_opcode, first_event, first_error;839int32_t xinputAvailable;840int32_t numDevices, devIdx, clsIdx;841XDeviceInfo* devices;842XDeviceInfo* aDevice;843XButtonInfo* bInfo;844int32_t local_num_buttons = 0;845846/* 4700242:847* If XTest is asked to press a non-existant mouse button848* (i.e. press Button3 on a system configured with a 2-button mouse),849* then a crash may happen. To avoid this, we use the XInput850* extension to query for the number of buttons on the XPointer, and check851* before calling XTestFakeButtonEvent().852*/853xinputAvailable = XQueryExtension(awt_display, INAME, &major_opcode, &first_event, &first_error);854if (xinputAvailable) {855DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XINPUT) returns major_opcode = %d, first_event = %d, first_error = %d",856major_opcode, first_event, first_error);857devices = XListInputDevices(awt_display, &numDevices);858for (devIdx = 0; devIdx < numDevices; devIdx++) {859aDevice = &(devices[devIdx]);860#ifdef IsXExtensionPointer861if (aDevice->use == IsXExtensionPointer) {862for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) {863if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) {864bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx]));865local_num_buttons = bInfo->num_buttons;866DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons);867break;868}869}870break;871}872#endif873if (local_num_buttons <= 0 ) {874if (aDevice->use == IsXPointer) {875for (clsIdx = 0; clsIdx < aDevice->num_classes; clsIdx++) {876if (aDevice->inputclassinfo[clsIdx].class == ButtonClass) {877bInfo = (XButtonInfo*)(&(aDevice->inputclassinfo[clsIdx]));878local_num_buttons = bInfo->num_buttons;879DTRACE_PRINTLN1("RobotPeer: XPointer has %d buttons", num_buttons);880break;881}882}883break;884}885}886}887888XFreeDeviceList(devices);889}890else {891DTRACE_PRINTLN1("RobotPeer: XINPUT extension is unavailable, assuming %d mouse buttons", num_buttons);892}893if (local_num_buttons == 0 ) {894local_num_buttons = 3;895}896897return local_num_buttons;898}899900/*901* Class: sun_awt_X11_XWindowPeer902* Method: getJvmPID903* Signature: ()I904*/905JNIEXPORT jint JNICALL Java_sun_awt_X11_XWindowPeer_getJvmPID906(JNIEnv *env, jclass cls)907{908/* Return the JVM's PID. */909return getpid();910}911912#ifndef HOST_NAME_MAX913#define HOST_NAME_MAX 1024 /* Overestimated */914#endif915916/*917* Class: sun_awt_X11_XWindowPeer918* Method: getLocalHostname919* Signature: ()Ljava/lang/String;920*/921JNIEXPORT jstring JNICALL Java_sun_awt_X11_XWindowPeer_getLocalHostname922(JNIEnv *env, jclass cls)923{924/* Return the machine's FQDN. */925char hostname[HOST_NAME_MAX + 1];926if (gethostname(hostname, HOST_NAME_MAX + 1) == 0) {927hostname[HOST_NAME_MAX] = '\0';928jstring res = (*env)->NewStringUTF(env, hostname);929return res;930}931932return (jstring)NULL;933}934935936