0N/A/*
3261N/A * Copyright (c) 1999, 2010, 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#ifdef HEADLESS
0N/A #error This file should not be included in headless library
0N/A#endif
0N/A
0N/A#include "awt_p.h"
0N/A#include "awt_Component.h"
0N/A#include "awt_GraphicsEnv.h"
0N/A#define XK_MISCELLANY
0N/A#include <X11/keysymdef.h>
0N/A#include <X11/Intrinsic.h>
0N/A#include <X11/Xutil.h>
0N/A#include <X11/Xmd.h>
0N/A#include <X11/extensions/xtestext1.h>
0N/A#include <X11/extensions/XTest.h>
0N/A#include <X11/extensions/XInput.h>
0N/A#include <X11/extensions/XI.h>
0N/A#include <jni.h>
6295N/A#include <sizecalc.h>
0N/A#include "robot_common.h"
0N/A#include "canvas.h"
0N/A#include "wsutils.h"
0N/A#include "list.h"
0N/A#include "multiVis.h"
4632N/A#if defined(__linux__) || defined(MACOSX)
0N/A#include <sys/socket.h>
0N/A#endif
0N/A
0N/Aextern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
0N/A
870N/Astatic jint * masks;
1444N/Astatic jint num_buttons;
0N/A
0N/Astatic int32_t isXTestAvailable() {
0N/A int32_t major_opcode, first_event, first_error;
0N/A int32_t event_basep, error_basep, majorp, minorp;
0N/A int32_t isXTestAvailable;
0N/A
0N/A /* check if XTest is available */
0N/A isXTestAvailable = XQueryExtension(awt_display, XTestExtensionName, &major_opcode, &first_event, &first_error);
0N/A DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XTEST) returns major_opcode = %d, first_event = %d, first_error = %d",
0N/A major_opcode, first_event, first_error);
0N/A if (isXTestAvailable) {
0N/A /* check if XTest version is OK */
0N/A XTestQueryExtension(awt_display, &event_basep, &error_basep, &majorp, &minorp);
0N/A DTRACE_PRINTLN4("RobotPeer: XTestQueryExtension returns event_basep = %d, error_basep = %d, majorp = %d, minorp = %d",
0N/A event_basep, error_basep, majorp, minorp);
0N/A if (majorp < 2 || (majorp == 2 && minorp < 2)) {
0N/A /* bad version*/
0N/A DTRACE_PRINTLN2("XRobotPeer: XTEST version is %d.%d \n", majorp, minorp);
0N/A if (majorp == 2 && minorp == 1) {
0N/A DTRACE_PRINTLN("XRobotPeer: XTEST is 2.1 - no grab is available\n");
0N/A } else {
0N/A isXTestAvailable = False;
0N/A }
0N/A } else {
0N/A /* allow XTest calls even if someone else has the grab; e.g. during
0N/A * a window resize operation. Works only with XTEST2.2*/
0N/A XTestGrabControl(awt_display, True);
0N/A }
0N/A } else {
0N/A DTRACE_PRINTLN("RobotPeer: XTEST extension is unavailable");
0N/A }
0N/A
0N/A return isXTestAvailable;
0N/A}
0N/A
0N/A
0N/Astatic XImage *getWindowImage(Display * display, Window window,
0N/A int32_t x, int32_t y,
0N/A int32_t w, int32_t h) {
0N/A XImage *image;
0N/A int32_t transparentOverlays;
0N/A int32_t numVisuals;
0N/A XVisualInfo *pVisuals;
0N/A int32_t numOverlayVisuals;
0N/A OverlayInfo *pOverlayVisuals;
0N/A int32_t numImageVisuals;
0N/A XVisualInfo **pImageVisuals;
0N/A list_ptr vis_regions; /* list of regions to read from */
0N/A list_ptr vis_image_regions ;
0N/A int32_t allImage = 0 ;
0N/A int32_t format = ZPixmap;
0N/A
0N/A /* prevent user from moving stuff around during the capture */
0N/A XGrabServer(display);
0N/A
0N/A /*
0N/A * The following two functions live in multiVis.c-- they are pretty
0N/A * much verbatim taken from the source to the xwd utility from the
0N/A * X11 source. This version of the xwd source was somewhat better written
0N/A * for reuse compared to Sun's version.
0N/A *
0N/A * ftp.x.org/pub/R6.3/xc/programs/xwd
0N/A *
0N/A * We use these functions since they do the very tough job of capturing
0N/A * the screen correctly when it contains multiple visuals. They take into
0N/A * account the depth/colormap of each visual and produce a capture as a
0N/A * 24-bit RGB image so we don't have to fool around with colormaps etc.
0N/A */
0N/A
0N/A GetMultiVisualRegions(
0N/A display,
0N/A window,
0N/A x, y, w, h,
0N/A &transparentOverlays,
0N/A &numVisuals,
0N/A &pVisuals,
0N/A &numOverlayVisuals,
0N/A &pOverlayVisuals,
0N/A &numImageVisuals,
0N/A &pImageVisuals,
0N/A &vis_regions,
0N/A &vis_image_regions,
0N/A &allImage );
0N/A
0N/A image = ReadAreaToImage(
0N/A display,
0N/A window,
0N/A x, y, w, h,
0N/A numVisuals,
0N/A pVisuals,
0N/A numOverlayVisuals,
0N/A pOverlayVisuals,
0N/A numImageVisuals,
0N/A pImageVisuals,
0N/A vis_regions,
0N/A vis_image_regions,
0N/A format,
0N/A allImage );
0N/A
0N/A /* allow user to do stuff again */
0N/A XUngrabServer(display);
0N/A
0N/A /* make sure the grab/ungrab is flushed */
0N/A XSync(display, False);
0N/A
0N/A return image;
0N/A}
0N/A
0N/A/*********************************************************************************************/
0N/A
1444N/A// this should be called from XRobotPeer constructor
0N/AJNIEXPORT void JNICALL
2859N/AJava_sun_awt_X11_XRobotPeer_setup (JNIEnv * env, jclass cls, jint numberOfButtons, jintArray buttonDownMasks)
2859N/A{
0N/A int32_t xtestAvailable;
2859N/A jint *tmp;
2859N/A int i;
0N/A
1444N/A DTRACE_PRINTLN("RobotPeer: setup()");
1444N/A
1444N/A num_buttons = numberOfButtons;
2859N/A tmp = (*env)->GetIntArrayElements(env, buttonDownMasks, JNI_FALSE);
6295N/A masks = (jint *)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(jint), num_buttons);
870N/A if (masks == (jint *) NULL) {
870N/A JNU_ThrowOutOfMemoryError((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), NULL);
2859N/A (*env)->ReleaseIntArrayElements(env, buttonDownMasks, tmp, 0);
2859N/A return;
870N/A }
1444N/A for (i = 0; i < num_buttons; i++) {
870N/A masks[i] = tmp[i];
870N/A }
2859N/A (*env)->ReleaseIntArrayElements(env, buttonDownMasks, tmp, 0);
870N/A
0N/A AWT_LOCK();
0N/A xtestAvailable = isXTestAvailable();
0N/A DTRACE_PRINTLN1("RobotPeer: XTest available = %d", xtestAvailable);
0N/A if (!xtestAvailable) {
0N/A JNU_ThrowByName(env, "java/awt/AWTException", "java.awt.Robot requires your X server support the XTEST extension version 2.2");
0N/A }
0N/A
870N/A AWT_UNLOCK();
870N/A}
0N/A
0N/A
0N/AJNIEXPORT void JNICALL
547N/AJava_sun_awt_X11_XRobotPeer_getRGBPixelsImpl( JNIEnv *env,
0N/A jclass cls,
0N/A jobject xgc,
0N/A jint x,
0N/A jint y,
0N/A jint width,
0N/A jint height,
0N/A jintArray pixelArray) {
0N/A
0N/A XImage *image;
0N/A jint *ary; /* Array of jints for sending pixel values back
0N/A * to parent process.
0N/A */
4197N/A Window rootWindow;
0N/A AwtGraphicsConfigDataPtr adata;
0N/A
0N/A DTRACE_PRINTLN6("RobotPeer: getRGBPixelsImpl(%lx, %d, %d, %d, %d, %x)", xgc, x, y, width, height, pixelArray);
0N/A
0N/A AWT_LOCK();
0N/A
0N/A /* avoid a lot of work for empty rectangles */
0N/A if ((width * height) == 0) {
0N/A AWT_UNLOCK();
0N/A return;
0N/A }
0N/A DASSERT(width * height > 0); /* only allow positive size */
0N/A
0N/A adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
0N/A DASSERT(adata != NULL);
0N/A
4197N/A rootWindow = XRootWindow(awt_display, adata->awt_visInfo.screen);
4197N/A image = getWindowImage(awt_display, rootWindow, x, y, width, height);
0N/A
0N/A /* Array to use to crunch around the pixel values */
6295N/A if (!IS_SAFE_SIZE_MUL(width, height) ||
6295N/A !(ary = (jint *) SAFE_SIZE_ARRAY_ALLOC(malloc, width * height, sizeof (jint))))
6295N/A {
0N/A JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
0N/A XDestroyImage(image);
0N/A AWT_UNLOCK();
0N/A return;
0N/A }
0N/A /* convert to Java ARGB pixels */
0N/A for (y = 0; y < height; y++) {
0N/A for (x = 0; x < width; x++) {
0N/A jint pixel = (jint) XGetPixel(image, x, y); /* Note ignore upper
0N/A * 32-bits on 64-bit
0N/A * OSes.
0N/A */
0N/A
0N/A pixel |= 0xff000000; /* alpha - full opacity */
0N/A
0N/A ary[(y * width) + x] = pixel;
0N/A }
0N/A }
0N/A (*env)->SetIntArrayRegion(env, pixelArray, 0, height * width, ary);
0N/A free(ary);
0N/A
0N/A XDestroyImage(image);
0N/A
0N/A AWT_UNLOCK();
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
547N/AJava_sun_awt_X11_XRobotPeer_keyPressImpl (JNIEnv *env,
0N/A jclass cls,
0N/A jint keycode) {
0N/A
0N/A AWT_LOCK();
0N/A
0N/A DTRACE_PRINTLN1("RobotPeer: keyPressImpl(%i)", keycode);
0N/A
0N/A XTestFakeKeyEvent(awt_display,
0N/A XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
0N/A True,
0N/A CurrentTime);
0N/A
0N/A XSync(awt_display, False);
0N/A
0N/A AWT_UNLOCK();
0N/A
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
547N/AJava_sun_awt_X11_XRobotPeer_keyReleaseImpl (JNIEnv *env,
0N/A jclass cls,
0N/A jint keycode) {
0N/A AWT_LOCK();
0N/A
0N/A DTRACE_PRINTLN1("RobotPeer: keyReleaseImpl(%i)", keycode);
0N/A
0N/A XTestFakeKeyEvent(awt_display,
0N/A XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
0N/A False,
0N/A CurrentTime);
0N/A
0N/A XSync(awt_display, False);
0N/A
0N/A AWT_UNLOCK();
0N/A}
0N/A
0N/AJNIEXPORT void JNICALL
547N/AJava_sun_awt_X11_XRobotPeer_mouseMoveImpl (JNIEnv *env,
0N/A jclass cls,
0N/A jobject xgc,
0N/A jint root_x,
0N/A jint root_y) {
0N/A
0N/A AwtGraphicsConfigDataPtr adata;
0N/A
0N/A AWT_LOCK();
0N/A
0N/A DTRACE_PRINTLN3("RobotPeer: mouseMoveImpl(%lx, %i, %i)", xgc, root_x, root_y);
0N/A
0N/A adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
0N/A DASSERT(adata != NULL);
0N/A
0N/A XWarpPointer(awt_display, None, XRootWindow(awt_display, adata->awt_visInfo.screen), 0, 0, 0, 0, root_x, root_y);
0N/A XSync(awt_display, False);
0N/A
0N/A AWT_UNLOCK();
0N/A}
0N/A
870N/A/*
870N/A * Function joining the code of mousePressImpl and mouseReleaseImpl
870N/A */
870N/Avoid mouseAction(JNIEnv *env,
870N/A jclass cls,
870N/A jint buttonMask,
870N/A Bool isMousePress)
870N/A{
870N/A AWT_LOCK();
870N/A
870N/A DTRACE_PRINTLN1("RobotPeer: mouseAction(%i)", buttonMask);
870N/A DTRACE_PRINTLN1("RobotPeer: mouseAction, press = %d", isMousePress);
870N/A
870N/A if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK ||
870N/A buttonMask & java_awt_event_InputEvent_BUTTON1_DOWN_MASK )
870N/A {
870N/A XTestFakeButtonEvent(awt_display, 1, isMousePress, CurrentTime);
870N/A }
870N/A if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK ||
870N/A buttonMask & java_awt_event_InputEvent_BUTTON2_DOWN_MASK) &&
870N/A (num_buttons >= 2)) {
870N/A XTestFakeButtonEvent(awt_display, 2, isMousePress, CurrentTime);
870N/A }
870N/A if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK ||
870N/A buttonMask & java_awt_event_InputEvent_BUTTON3_DOWN_MASK) &&
870N/A (num_buttons >= 3)) {
870N/A XTestFakeButtonEvent(awt_display, 3, isMousePress, CurrentTime);
870N/A }
870N/A
870N/A if (num_buttons > 3){
870N/A int32_t i;
870N/A int32_t button = 0;
870N/A for (i = 3; i<num_buttons; i++){
870N/A if ((buttonMask & masks[i])) {
870N/A // arrays starts from zero index => +1
870N/A // users wants to affect 4 or 5 button but they are assigned
870N/A // to the wheel so => we have to shift it to the right by 2.
870N/A button = i + 3;
870N/A XTestFakeButtonEvent(awt_display, button, isMousePress, CurrentTime);
870N/A }
870N/A }
870N/A }
870N/A
870N/A XSync(awt_display, False);
870N/A AWT_UNLOCK();
870N/A}
870N/A
873N/AJNIEXPORT void JNICALL
873N/AJava_sun_awt_X11_XRobotPeer_mousePressImpl (JNIEnv *env,
873N/A jclass cls,
873N/A jint buttonMask) {
873N/A mouseAction(env, cls, buttonMask, True);
873N/A}
873N/A
873N/AJNIEXPORT void JNICALL
873N/AJava_sun_awt_X11_XRobotPeer_mouseReleaseImpl (JNIEnv *env,
873N/A jclass cls,
873N/A jint buttonMask) {
873N/A mouseAction(env, cls, buttonMask, False);
873N/A}
870N/A
0N/AJNIEXPORT void JNICALL
547N/AJava_sun_awt_X11_XRobotPeer_mouseWheelImpl (JNIEnv *env,
0N/A jclass cls,
0N/A jint wheelAmt) {
0N/A/* Mouse wheel is implemented as a button press of button 4 and 5, so it */
0N/A/* probably could have been hacked into robot_mouseButtonEvent, but it's */
0N/A/* cleaner to give it its own command type, in case the implementation */
0N/A/* needs to be changed later. -bchristi, 6/20/01 */
0N/A
0N/A int32_t repeat = abs(wheelAmt);
0N/A int32_t button = wheelAmt < 0 ? 4 : 5; /* wheel up: button 4 */
0N/A /* wheel down: button 5 */
0N/A int32_t loopIdx;
0N/A
0N/A AWT_LOCK();
0N/A
0N/A DTRACE_PRINTLN1("RobotPeer: mouseWheelImpl(%i)", wheelAmt);
0N/A
0N/A for (loopIdx = 0; loopIdx < repeat; loopIdx++) { /* do nothing for */
0N/A /* wheelAmt == 0 */
0N/A XTestFakeButtonEvent(awt_display, button, True, CurrentTime);
0N/A XTestFakeButtonEvent(awt_display, button, False, CurrentTime);
0N/A }
0N/A XSync(awt_display, False);
0N/A
0N/A AWT_UNLOCK();
0N/A}