Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/solaris/native/sun/awt/awt_Robot.c
32287 views
/*1* Copyright (c) 1999, 2014, 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/*26#ifdef HEADLESS27#error This file should not be included in headless library28#endif29*/3031#include "jvm_md.h"32#include <dlfcn.h>3334#include "awt_p.h"35#include "awt_GraphicsEnv.h"36#define XK_MISCELLANY37#include <X11/keysymdef.h>38#include <X11/Intrinsic.h>39#include <X11/Xutil.h>40#include <X11/Xmd.h>41#include <X11/extensions/xtestext1.h>42#include <X11/extensions/XTest.h>43#include <X11/extensions/XInput.h>44#include <X11/extensions/XI.h>45#include <jni.h>46#include <sizecalc.h>47#include "canvas.h"48#include "wsutils.h"49#include "list.h"50#include "multiVis.h"5152#include "java_awt_event_InputEvent.h"5354#if defined(__linux__) || defined(MACOSX)55#include <sys/socket.h>56#endif5758static Bool (*compositeQueryExtension) (Display*, int*, int*);59static Status (*compositeQueryVersion) (Display*, int*, int*);60static Window (*compositeGetOverlayWindow) (Display *, Window);6162extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;6364static jint * masks;65static jint num_buttons;6667static void *xCompositeHandle;6869static const char* XCOMPOSITE = JNI_LIB_NAME("Xcomposite");70static const char* XCOMPOSITE_VERSIONED = VERSIONED_JNI_LIB_NAME("Xcomposite", "1");7172#ifndef HEADLESS73static Bool checkXCompositeFunctions(void) {74return (compositeQueryExtension != NULL &&75compositeQueryVersion != NULL &&76compositeGetOverlayWindow != NULL);77}7879static void initXCompositeFunctions(void) {8081if (xCompositeHandle == NULL) {82xCompositeHandle = dlopen(XCOMPOSITE, RTLD_LAZY | RTLD_GLOBAL);83if (xCompositeHandle == NULL) {84xCompositeHandle = dlopen(XCOMPOSITE_VERSIONED, RTLD_LAZY | RTLD_GLOBAL);85}86}87//*(void **)(&asyncGetCallTraceFunction)88if (xCompositeHandle != NULL) {89*(void **)(&compositeQueryExtension) = dlsym(xCompositeHandle, "XCompositeQueryExtension");90*(void **)(&compositeQueryVersion) = dlsym(xCompositeHandle, "XCompositeQueryVersion");91*(void **)(&compositeGetOverlayWindow) = dlsym(xCompositeHandle, "XCompositeGetOverlayWindow");92}9394if (xCompositeHandle && !checkXCompositeFunctions()) {95dlclose(xCompositeHandle);96}97}9899static int32_t isXTestAvailable() {100int32_t major_opcode, first_event, first_error;101int32_t event_basep, error_basep, majorp, minorp;102int32_t isXTestAvailable;103104/* check if XTest is available */105isXTestAvailable = XQueryExtension(awt_display, XTestExtensionName, &major_opcode, &first_event, &first_error);106if (isXTestAvailable) {107DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XTEST) returns major_opcode = %d, first_event = %d, first_error = %d",108major_opcode, first_event, first_error);109/* check if XTest version is OK */110XTestQueryExtension(awt_display, &event_basep, &error_basep, &majorp, &minorp);111DTRACE_PRINTLN4("RobotPeer: XTestQueryExtension returns event_basep = %d, error_basep = %d, majorp = %d, minorp = %d",112event_basep, error_basep, majorp, minorp);113if (majorp < 2 || (majorp == 2 && minorp < 2)) {114/* bad version*/115DTRACE_PRINTLN2("XRobotPeer: XTEST version is %d.%d \n", majorp, minorp);116if (majorp == 2 && minorp == 1) {117DTRACE_PRINTLN("XRobotPeer: XTEST is 2.1 - no grab is available\n");118} else {119isXTestAvailable = False;120}121} else {122/* allow XTest calls even if someone else has the grab; e.g. during123* a window resize operation. Works only with XTEST2.2*/124XTestGrabControl(awt_display, True);125}126} else {127DTRACE_PRINTLN("RobotPeer: XTEST extension is unavailable");128}129130return isXTestAvailable;131}132133static Bool hasXCompositeOverlayExtension(Display *display) {134135int xoverlay = False;136int eventBase, errorBase;137if (checkXCompositeFunctions() &&138compositeQueryExtension(display, &eventBase, &errorBase))139{140int major = 0;141int minor = 0;142143compositeQueryVersion(display, &major, &minor);144if (major > 0 || minor >= 3) {145xoverlay = True;146}147}148149return xoverlay;150}151152static jboolean isXCompositeDisplay(Display *display, int screenNumber) {153154char NET_WM_CM_Sn[25];155snprintf(NET_WM_CM_Sn, sizeof(NET_WM_CM_Sn), "_NET_WM_CM_S%d\0", screenNumber);156157Atom managerSelection = XInternAtom(display, NET_WM_CM_Sn, 0);158Window owner = XGetSelectionOwner(display, managerSelection);159160return owner != 0;161}162163static XImage *getWindowImage(Display * display, Window window,164int32_t x, int32_t y,165int32_t w, int32_t h) {166XImage *image;167int32_t transparentOverlays;168int32_t numVisuals;169XVisualInfo *pVisuals;170int32_t numOverlayVisuals;171OverlayInfo *pOverlayVisuals;172int32_t numImageVisuals;173XVisualInfo **pImageVisuals;174list_ptr vis_regions; /* list of regions to read from */175list_ptr vis_image_regions ;176int32_t allImage = 0 ;177int32_t format = ZPixmap;178179/* prevent user from moving stuff around during the capture */180XGrabServer(display);181182/*183* The following two functions live in multiVis.c-- they are pretty184* much verbatim taken from the source to the xwd utility from the185* X11 source. This version of the xwd source was somewhat better written186* for reuse compared to Sun's version.187*188* ftp.x.org/pub/R6.3/xc/programs/xwd189*190* We use these functions since they do the very tough job of capturing191* the screen correctly when it contains multiple visuals. They take into192* account the depth/colormap of each visual and produce a capture as a193* 24-bit RGB image so we don't have to fool around with colormaps etc.194*/195196GetMultiVisualRegions(197display,198window,199x, y, w, h,200&transparentOverlays,201&numVisuals,202&pVisuals,203&numOverlayVisuals,204&pOverlayVisuals,205&numImageVisuals,206&pImageVisuals,207&vis_regions,208&vis_image_regions,209&allImage );210211image = ReadAreaToImage(212display,213window,214x, y, w, h,215numVisuals,216pVisuals,217numOverlayVisuals,218pOverlayVisuals,219numImageVisuals,220pImageVisuals,221vis_regions,222vis_image_regions,223format,224allImage );225226/* allow user to do stuff again */227XUngrabServer(display);228229/* make sure the grab/ungrab is flushed */230XSync(display, False);231232return image;233}234#endif235236/*********************************************************************************************/237238// this should be called from XRobotPeer constructor239JNIEXPORT void JNICALL240Java_sun_awt_X11_XRobotPeer_setup (JNIEnv * env, jclass cls, jint numberOfButtons, jintArray buttonDownMasks)241{242#ifndef HEADLESS243int32_t xtestAvailable;244jint *tmp;245int i;246247DTRACE_PRINTLN("RobotPeer: setup()");248249num_buttons = numberOfButtons;250tmp = (*env)->GetIntArrayElements(env, buttonDownMasks, JNI_FALSE);251CHECK_NULL(tmp);252253masks = (jint *)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(jint), num_buttons);254if (masks == (jint *) NULL) {255(*env)->ExceptionClear(env);256(*env)->ReleaseIntArrayElements(env, buttonDownMasks, tmp, 0);257JNU_ThrowOutOfMemoryError((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), NULL);258return;259}260for (i = 0; i < num_buttons; i++) {261masks[i] = tmp[i];262}263(*env)->ReleaseIntArrayElements(env, buttonDownMasks, tmp, 0);264265AWT_LOCK();266xtestAvailable = isXTestAvailable();267DTRACE_PRINTLN1("RobotPeer: XTest available = %d", xtestAvailable);268if (!xtestAvailable) {269JNU_ThrowByName(env, "java/awt/AWTException", "java.awt.Robot requires your X server support the XTEST extension version 2.2");270}271272AWT_UNLOCK();273#endif274}275276277JNIEXPORT void JNICALL278Java_sun_awt_X11_XRobotPeer_getRGBPixelsImpl( JNIEnv *env,279jclass cls,280jobject xgc,281jint x,282jint y,283jint width,284jint height,285jintArray pixelArray) {286#ifndef HEADLESS287XImage *image;288jint *ary; /* Array of jints for sending pixel values back289* to parent process.290*/291Window rootWindow;292AwtGraphicsConfigDataPtr adata;293294DTRACE_PRINTLN6("RobotPeer: getRGBPixelsImpl(%lx, %d, %d, %d, %d, %x)", xgc, x, y, width, height, pixelArray);295296AWT_LOCK();297298/* avoid a lot of work for empty rectangles */299if ((width * height) == 0) {300AWT_UNLOCK();301return;302}303DASSERT(width * height > 0); /* only allow positive size */304305adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);306DASSERT(adata != NULL);307308rootWindow = XRootWindow(awt_display, adata->awt_visInfo.screen);309if (hasXCompositeOverlayExtension(awt_display) &&310isXCompositeDisplay(awt_display, adata->awt_visInfo.screen))311{312rootWindow = compositeGetOverlayWindow(awt_display, rootWindow);313}314315image = getWindowImage(awt_display, rootWindow, x, y, width, height);316317/* Array to use to crunch around the pixel values */318if (!IS_SAFE_SIZE_MUL(width, height) ||319!(ary = (jint *) SAFE_SIZE_ARRAY_ALLOC(malloc, width * height, sizeof (jint))))320{321JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");322XDestroyImage(image);323AWT_UNLOCK();324return;325}326/* convert to Java ARGB pixels */327for (y = 0; y < height; y++) {328for (x = 0; x < width; x++) {329jint pixel = (jint) XGetPixel(image, x, y); /* Note ignore upper330* 32-bits on 64-bit331* OSes.332*/333334pixel |= 0xff000000; /* alpha - full opacity */335336ary[(y * width) + x] = pixel;337}338}339(*env)->SetIntArrayRegion(env, pixelArray, 0, height * width, ary);340free(ary);341342XDestroyImage(image);343344AWT_UNLOCK();345#endif346}347348JNIEXPORT void JNICALL349Java_sun_awt_X11_XRobotPeer_keyPressImpl (JNIEnv *env,350jclass cls,351jint keycode) {352#ifndef HEADLESS353AWT_LOCK();354355DTRACE_PRINTLN1("RobotPeer: keyPressImpl(%i)", keycode);356357XTestFakeKeyEvent(awt_display,358XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),359True,360CurrentTime);361362XSync(awt_display, False);363364AWT_UNLOCK();365#endif366}367368JNIEXPORT void JNICALL369Java_sun_awt_X11_XRobotPeer_keyReleaseImpl (JNIEnv *env,370jclass cls,371jint keycode) {372#ifndef HEADLESS373AWT_LOCK();374375DTRACE_PRINTLN1("RobotPeer: keyReleaseImpl(%i)", keycode);376377XTestFakeKeyEvent(awt_display,378XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),379False,380CurrentTime);381382XSync(awt_display, False);383384AWT_UNLOCK();385#endif386}387388JNIEXPORT void JNICALL389Java_sun_awt_X11_XRobotPeer_mouseMoveImpl (JNIEnv *env,390jclass cls,391jobject xgc,392jint root_x,393jint root_y) {394#ifndef HEADLESS395AwtGraphicsConfigDataPtr adata;396397AWT_LOCK();398399DTRACE_PRINTLN3("RobotPeer: mouseMoveImpl(%lx, %i, %i)", xgc, root_x, root_y);400401adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);402DASSERT(adata != NULL);403404XWarpPointer(awt_display, None, XRootWindow(awt_display, adata->awt_visInfo.screen), 0, 0, 0, 0, root_x, root_y);405XSync(awt_display, False);406407AWT_UNLOCK();408#endif409}410411/*412* Function joining the code of mousePressImpl and mouseReleaseImpl413*/414void mouseAction(JNIEnv *env,415jclass cls,416jint buttonMask,417Bool isMousePress)418{419#ifndef HEADLESS420AWT_LOCK();421422DTRACE_PRINTLN1("RobotPeer: mouseAction(%i)", buttonMask);423DTRACE_PRINTLN1("RobotPeer: mouseAction, press = %d", isMousePress);424425if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK ||426buttonMask & java_awt_event_InputEvent_BUTTON1_DOWN_MASK )427{428XTestFakeButtonEvent(awt_display, 1, isMousePress, CurrentTime);429}430if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK ||431buttonMask & java_awt_event_InputEvent_BUTTON2_DOWN_MASK) &&432(num_buttons >= 2)) {433XTestFakeButtonEvent(awt_display, 2, isMousePress, CurrentTime);434}435if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK ||436buttonMask & java_awt_event_InputEvent_BUTTON3_DOWN_MASK) &&437(num_buttons >= 3)) {438XTestFakeButtonEvent(awt_display, 3, isMousePress, CurrentTime);439}440441if (num_buttons > 3){442int32_t i;443int32_t button = 0;444for (i = 3; i<num_buttons; i++){445if ((buttonMask & masks[i])) {446// arrays starts from zero index => +1447// users wants to affect 4 or 5 button but they are assigned448// to the wheel so => we have to shift it to the right by 2.449button = i + 3;450XTestFakeButtonEvent(awt_display, button, isMousePress, CurrentTime);451}452}453}454455XSync(awt_display, False);456AWT_UNLOCK();457#endif458}459460JNIEXPORT void JNICALL461Java_sun_awt_X11_XRobotPeer_mousePressImpl (JNIEnv *env,462jclass cls,463jint buttonMask) {464mouseAction(env, cls, buttonMask, True);465}466467JNIEXPORT void JNICALL468Java_sun_awt_X11_XRobotPeer_mouseReleaseImpl (JNIEnv *env,469jclass cls,470jint buttonMask) {471mouseAction(env, cls, buttonMask, False);472}473474JNIEXPORT void JNICALL475Java_sun_awt_X11_XRobotPeer_mouseWheelImpl (JNIEnv *env,476jclass cls,477jint wheelAmt) {478/* Mouse wheel is implemented as a button press of button 4 and 5, so it */479/* probably could have been hacked into robot_mouseButtonEvent, but it's */480/* cleaner to give it its own command type, in case the implementation */481/* needs to be changed later. -bchristi, 6/20/01 */482#ifndef HEADLESS483int32_t repeat = abs(wheelAmt);484int32_t button = wheelAmt < 0 ? 4 : 5; /* wheel up: button 4 */485/* wheel down: button 5 */486int32_t loopIdx;487488AWT_LOCK();489490DTRACE_PRINTLN1("RobotPeer: mouseWheelImpl(%i)", wheelAmt);491492for (loopIdx = 0; loopIdx < repeat; loopIdx++) { /* do nothing for */493/* wheelAmt == 0 */494XTestFakeButtonEvent(awt_display, button, True, CurrentTime);495XTestFakeButtonEvent(awt_display, button, False, CurrentTime);496}497XSync(awt_display, False);498499AWT_UNLOCK();500#endif501}502503JNIEXPORT void JNICALL504Java_sun_awt_X11_XRobotPeer_loadNativeLibraries (JNIEnv *env, jclass cls) {505#ifndef HEADLESS506initXCompositeFunctions();507#endif508}509510511