1 /*
   2  * Copyright (c) 1999, 2015, 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 #ifdef HEADLESS
  27     #error This file should not be included in headless library
  28 #endif
  29 
  30 #include "awt_p.h"
  31 #include "awt_GraphicsEnv.h"
  32 #define XK_MISCELLANY
  33 #include <X11/keysymdef.h>
  34 #include <X11/Intrinsic.h>
  35 #include <X11/Xutil.h>
  36 #include <X11/Xmd.h>
  37 #include <X11/extensions/xtestext1.h>
  38 #include <X11/extensions/XTest.h>
  39 #include <X11/extensions/XInput.h>
  40 #include <X11/extensions/XI.h>
  41 #include <jni.h>
  42 #include <sizecalc.h>
  43 #include "robot_common.h"
  44 #include "canvas.h"
  45 #include "wsutils.h"
  46 #include "list.h"
  47 #include "multiVis.h"
  48 #include "gtk_interface.h"
  49 
  50 #if defined(__linux__) || defined(MACOSX)
  51 #include <sys/socket.h>
  52 #endif
  53 
  54 extern struct X11GraphicsConfigIDs x11GraphicsConfigIDs;
  55 
  56 static jint * masks;
  57 static jint num_buttons;
  58 
  59 static int32_t isXTestAvailable() {
  60     int32_t major_opcode, first_event, first_error;
  61     int32_t  event_basep, error_basep, majorp, minorp;
  62     int32_t isXTestAvailable;
  63 
  64     /* check if XTest is available */
  65     isXTestAvailable = XQueryExtension(awt_display, XTestExtensionName, &major_opcode, &first_event, &first_error);
  66     DTRACE_PRINTLN3("RobotPeer: XQueryExtension(XTEST) returns major_opcode = %d, first_event = %d, first_error = %d",
  67                     major_opcode, first_event, first_error);
  68     if (isXTestAvailable) {
  69         /* check if XTest version is OK */
  70         XTestQueryExtension(awt_display, &event_basep, &error_basep, &majorp, &minorp);
  71         DTRACE_PRINTLN4("RobotPeer: XTestQueryExtension returns event_basep = %d, error_basep = %d, majorp = %d, minorp = %d",
  72                         event_basep, error_basep, majorp, minorp);
  73         if (majorp < 2 || (majorp == 2 && minorp < 2)) {
  74             /* bad version*/
  75             DTRACE_PRINTLN2("XRobotPeer: XTEST version is %d.%d \n", majorp, minorp);
  76             if (majorp == 2 && minorp == 1) {
  77                 DTRACE_PRINTLN("XRobotPeer: XTEST is 2.1 - no grab is available\n");
  78             } else {
  79                 isXTestAvailable = False;
  80             }
  81         } else {
  82             /* allow XTest calls even if someone else has the grab; e.g. during
  83              * a window resize operation. Works only with XTEST2.2*/
  84             XTestGrabControl(awt_display, True);
  85         }
  86     } else {
  87         DTRACE_PRINTLN("RobotPeer: XTEST extension is unavailable");
  88     }
  89 
  90     return isXTestAvailable;
  91 }
  92 
  93 
  94 static XImage *getWindowImage(Display * display, Window window,
  95                               int32_t x, int32_t y,
  96                               int32_t w, int32_t h) {
  97     XImage         *image;
  98     int32_t        transparentOverlays;
  99     int32_t        numVisuals;
 100     XVisualInfo    *pVisuals;
 101     int32_t        numOverlayVisuals;
 102     OverlayInfo    *pOverlayVisuals;
 103     int32_t        numImageVisuals;
 104     XVisualInfo    **pImageVisuals;
 105     list_ptr       vis_regions;    /* list of regions to read from */
 106     list_ptr       vis_image_regions ;
 107     int32_t        allImage = 0 ;
 108     int32_t        format = ZPixmap;
 109 
 110     /* prevent user from moving stuff around during the capture */
 111     XGrabServer(display);
 112 
 113     /*
 114      * The following two functions live in multiVis.c-- they are pretty
 115      * much verbatim taken from the source to the xwd utility from the
 116      * X11 source. This version of the xwd source was somewhat better written
 117      * for reuse compared to Sun's version.
 118      *
 119      *        ftp.x.org/pub/R6.3/xc/programs/xwd
 120      *
 121      * We use these functions since they do the very tough job of capturing
 122      * the screen correctly when it contains multiple visuals. They take into
 123      * account the depth/colormap of each visual and produce a capture as a
 124      * 24-bit RGB image so we don't have to fool around with colormaps etc.
 125      */
 126 
 127     GetMultiVisualRegions(
 128         display,
 129         window,
 130         x, y, w, h,
 131         &transparentOverlays,
 132         &numVisuals,
 133         &pVisuals,
 134         &numOverlayVisuals,
 135         &pOverlayVisuals,
 136         &numImageVisuals,
 137         &pImageVisuals,
 138         &vis_regions,
 139         &vis_image_regions,
 140         &allImage );
 141 
 142     image = ReadAreaToImage(
 143         display,
 144         window,
 145         x, y, w, h,
 146         numVisuals,
 147         pVisuals,
 148         numOverlayVisuals,
 149         pOverlayVisuals,
 150         numImageVisuals,
 151         pImageVisuals,
 152         vis_regions,
 153         vis_image_regions,
 154         format,
 155         allImage );
 156 
 157     /* allow user to do stuff again */
 158     XUngrabServer(display);
 159 
 160     /* make sure the grab/ungrab is flushed */
 161     XSync(display, False);
 162 
 163     return image;
 164 }
 165 
 166 /*********************************************************************************************/
 167 
 168 // this should be called from XRobotPeer constructor
 169 JNIEXPORT void JNICALL
 170 Java_sun_awt_X11_XRobotPeer_setup (JNIEnv * env, jclass cls, jint numberOfButtons, jintArray buttonDownMasks)
 171 {
 172     int32_t xtestAvailable;
 173     jint *tmp;
 174     int i;
 175 
 176     DTRACE_PRINTLN("RobotPeer: setup()");
 177 
 178     num_buttons = numberOfButtons;
 179     tmp = (*env)->GetIntArrayElements(env, buttonDownMasks, JNI_FALSE);
 180     CHECK_NULL(tmp);
 181 
 182     masks = (jint *)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(jint), num_buttons);
 183     if (masks == (jint *) NULL) {
 184         (*env)->ExceptionClear(env);
 185         (*env)->ReleaseIntArrayElements(env, buttonDownMasks, tmp, 0);
 186         JNU_ThrowOutOfMemoryError((JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2), NULL);
 187         return;
 188     }
 189     for (i = 0; i < num_buttons; i++) {
 190         masks[i] = tmp[i];
 191     }
 192     (*env)->ReleaseIntArrayElements(env, buttonDownMasks, tmp, 0);
 193 
 194     AWT_LOCK();
 195     xtestAvailable = isXTestAvailable();
 196     DTRACE_PRINTLN1("RobotPeer: XTest available = %d", xtestAvailable);
 197     if (!xtestAvailable) {
 198         JNU_ThrowByName(env, "java/awt/AWTException", "java.awt.Robot requires your X server support the XTEST extension version 2.2");
 199     }
 200 
 201     AWT_UNLOCK();
 202 }
 203 
 204 
 205 JNIEXPORT void JNICALL
 206 Java_sun_awt_X11_XRobotPeer_getRGBPixelsImpl( JNIEnv *env,
 207                              jclass cls,
 208                              jobject xgc,
 209                              jint jx,
 210                              jint jy,
 211                              jint jwidth,
 212                              jint jheight,
 213                              jint scale,
 214                              jintArray pixelArray,
 215                              jboolean isGtkSupported) {
 216     XImage *image;
 217     jint *ary;               /* Array of jints for sending pixel values back
 218                               * to parent process.
 219                               */
 220     Window rootWindow;
 221     XWindowAttributes attr;
 222     AwtGraphicsConfigDataPtr adata;
 223 
 224     DTRACE_PRINTLN6("RobotPeer: getRGBPixelsImpl(%lx, %d, %d, %d, %d, %x)", xgc, jx, jy, jwidth, jheight, pixelArray);
 225 
 226     if (jwidth <= 0 || jheight <= 0) {
 227         return;
 228     }
 229 
 230     adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
 231     DASSERT(adata != NULL);
 232 
 233     AWT_LOCK();
 234 
 235     jint sx = jx * scale;
 236     jint sy = jy * scale;
 237     jint swidth = jwidth * scale;
 238     jint sheight = jheight * scale;
 239 
 240     rootWindow = XRootWindow(awt_display, adata->awt_visInfo.screen);
 241 
 242     if (!XGetWindowAttributes(awt_display, rootWindow, &attr)
 243             || sx + swidth <= attr.x
 244             || attr.x + attr.width <= sx
 245             || sy + sheight <= attr.y
 246             || attr.y + attr.height <= sy) {
 247 
 248         AWT_UNLOCK();
 249         return; // Does not intersect with root window
 250     }
 251 
 252     gboolean gtk_failed = TRUE;
 253     jint _x, _y;
 254 
 255     jint x = MAX(sx, attr.x);
 256     jint y = MAX(sy, attr.y);
 257     jint width = MIN(sx + swidth, attr.x + attr.width) - x;
 258     jint height = MIN(sy + sheight, attr.y + attr.height) - y;
 259 
 260 
 261     int dx = attr.x > sx ? attr.x - sx : 0;
 262     int dy = attr.y > sy ? attr.y - sy : 0;
 263 
 264     int index;
 265 
 266     if (isGtkSupported) {
 267         gtk->gdk_threads_enter();
 268         gtk_failed = gtk->get_drawable_data(env, pixelArray, x, y, width,
 269                                             jwidth, height, dx, dy, scale);
 270         gtk->gdk_threads_leave();
 271     }
 272 
 273     if (gtk_failed) {
 274         image = getWindowImage(awt_display, rootWindow, sx, sy, swidth, sheight);
 275 
 276         ary = (*env)->GetPrimitiveArrayCritical(env, pixelArray, NULL);
 277 
 278         if (!ary) {
 279             XDestroyImage(image);
 280             AWT_UNLOCK();
 281             return;
 282         }
 283 
 284         dx /= scale;
 285         dy /= scale;
 286         width /= scale;
 287         height /= scale;
 288 
 289         /* convert to Java ARGB pixels */
 290         for (_y = 0; _y < height; _y++) {
 291             for (_x = 0; _x < width; _x++) {
 292                 jint pixel = (jint) XGetPixel(image, _x * scale, _y * scale);
 293                                                               /* Note ignore upper
 294                                                                * 32-bits on 64-bit
 295                                                                * OSes.
 296                                                                */
 297                 pixel |= 0xff000000; /* alpha - full opacity */
 298 
 299                 index = (_y + dy) * jwidth + (_x + dx);
 300                 ary[index] = pixel;
 301             }
 302         }
 303 
 304         XDestroyImage(image);
 305         (*env)->ReleasePrimitiveArrayCritical(env, pixelArray, ary, 0);
 306     }
 307     AWT_UNLOCK();
 308 }
 309 
 310 JNIEXPORT void JNICALL
 311 Java_sun_awt_X11_XRobotPeer_keyPressImpl (JNIEnv *env,
 312                          jclass cls,
 313                          jint keycode) {
 314 
 315     AWT_LOCK();
 316 
 317     DTRACE_PRINTLN1("RobotPeer: keyPressImpl(%i)", keycode);
 318 
 319     XTestFakeKeyEvent(awt_display,
 320                       XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
 321                       True,
 322                       CurrentTime);
 323 
 324     XSync(awt_display, False);
 325 
 326     AWT_UNLOCK();
 327 
 328 }
 329 
 330 JNIEXPORT void JNICALL
 331 Java_sun_awt_X11_XRobotPeer_keyReleaseImpl (JNIEnv *env,
 332                            jclass cls,
 333                            jint keycode) {
 334     AWT_LOCK();
 335 
 336     DTRACE_PRINTLN1("RobotPeer: keyReleaseImpl(%i)", keycode);
 337 
 338     XTestFakeKeyEvent(awt_display,
 339                       XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
 340                       False,
 341                       CurrentTime);
 342 
 343     XSync(awt_display, False);
 344 
 345     AWT_UNLOCK();
 346 }
 347 
 348 JNIEXPORT void JNICALL
 349 Java_sun_awt_X11_XRobotPeer_mouseMoveImpl (JNIEnv *env,
 350                           jclass cls,
 351                           jobject xgc,
 352                           jint root_x,
 353                           jint root_y) {
 354 
 355     AwtGraphicsConfigDataPtr adata;
 356 
 357     AWT_LOCK();
 358 
 359     DTRACE_PRINTLN3("RobotPeer: mouseMoveImpl(%lx, %i, %i)", xgc, root_x, root_y);
 360 
 361     adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
 362     DASSERT(adata != NULL);
 363 
 364     XWarpPointer(awt_display, None, XRootWindow(awt_display, adata->awt_visInfo.screen), 0, 0, 0, 0, root_x, root_y);
 365     XSync(awt_display, False);
 366 
 367     AWT_UNLOCK();
 368 }
 369 
 370 /*
 371   * Function joining the code of mousePressImpl and mouseReleaseImpl
 372   */
 373 void mouseAction(JNIEnv *env,
 374                  jclass cls,
 375                  jint buttonMask,
 376                  Bool isMousePress)
 377 {
 378     AWT_LOCK();
 379 
 380     DTRACE_PRINTLN1("RobotPeer: mouseAction(%i)", buttonMask);
 381     DTRACE_PRINTLN1("RobotPeer: mouseAction, press = %d", isMousePress);
 382 
 383     if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK ||
 384         buttonMask & java_awt_event_InputEvent_BUTTON1_DOWN_MASK )
 385     {
 386         XTestFakeButtonEvent(awt_display, 1, isMousePress, CurrentTime);
 387     }
 388     if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK ||
 389          buttonMask & java_awt_event_InputEvent_BUTTON2_DOWN_MASK) &&
 390         (num_buttons >= 2)) {
 391         XTestFakeButtonEvent(awt_display, 2, isMousePress, CurrentTime);
 392     }
 393     if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK ||
 394          buttonMask & java_awt_event_InputEvent_BUTTON3_DOWN_MASK) &&
 395         (num_buttons >= 3)) {
 396         XTestFakeButtonEvent(awt_display, 3, isMousePress, CurrentTime);
 397     }
 398 
 399     if (num_buttons > 3){
 400         int32_t i;
 401         int32_t button = 0;
 402         for (i = 3; i<num_buttons; i++){
 403             if ((buttonMask & masks[i])) {
 404                 // arrays starts from zero index => +1
 405                 // users wants to affect 4 or 5 button but they are assigned
 406                 // to the wheel so => we have to shift it to the right by 2.
 407                 button = i + 3;
 408                 XTestFakeButtonEvent(awt_display, button, isMousePress, CurrentTime);
 409             }
 410         }
 411     }
 412 
 413     XSync(awt_display, False);
 414     AWT_UNLOCK();
 415 }
 416 
 417 JNIEXPORT void JNICALL
 418 Java_sun_awt_X11_XRobotPeer_mousePressImpl (JNIEnv *env,
 419                            jclass cls,
 420                            jint buttonMask) {
 421     mouseAction(env, cls, buttonMask, True);
 422 }
 423 
 424 JNIEXPORT void JNICALL
 425 Java_sun_awt_X11_XRobotPeer_mouseReleaseImpl (JNIEnv *env,
 426                              jclass cls,
 427                              jint buttonMask) {
 428     mouseAction(env, cls, buttonMask, False);
 429 }
 430 
 431 JNIEXPORT void JNICALL
 432 Java_sun_awt_X11_XRobotPeer_mouseWheelImpl (JNIEnv *env,
 433                            jclass cls,
 434                            jint wheelAmt) {
 435 /* Mouse wheel is implemented as a button press of button 4 and 5, so it */
 436 /* probably could have been hacked into robot_mouseButtonEvent, but it's */
 437 /* cleaner to give it its own command type, in case the implementation   */
 438 /* needs to be changed later.  -bchristi, 6/20/01                        */
 439 
 440     int32_t repeat = abs(wheelAmt);
 441     int32_t button = wheelAmt < 0 ? 4 : 5;  /* wheel up:   button 4 */
 442                                                  /* wheel down: button 5 */
 443     int32_t loopIdx;
 444 
 445     AWT_LOCK();
 446 
 447     DTRACE_PRINTLN1("RobotPeer: mouseWheelImpl(%i)", wheelAmt);
 448 
 449     for (loopIdx = 0; loopIdx < repeat; loopIdx++) { /* do nothing for   */
 450                                                      /* wheelAmt == 0    */
 451         XTestFakeButtonEvent(awt_display, button, True, CurrentTime);
 452         XTestFakeButtonEvent(awt_display, button, False, CurrentTime);
 453     }
 454     XSync(awt_display, False);
 455 
 456     AWT_UNLOCK();
 457 }