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 "gtk2_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         GdkPixbuf *pixbuf;
 268         (*fp_gdk_threads_enter)();
 269         GdkWindow *root = (*fp_gdk_get_default_root_window)();
 270 
 271         pixbuf = (*fp_gdk_pixbuf_get_from_drawable)(NULL, root, NULL,
 272                                                     x, y, 0, 0, width, height);
 273         if (pixbuf && scale != 1) {
 274             GdkPixbuf *scaledPixbuf;
 275             x /= scale;
 276             y /= scale;
 277             width /= scale;
 278             height /= scale;
 279             dx /= scale;
 280             dy /= scale;
 281             scaledPixbuf = (*fp_gdk_pixbuf_scale_simple)(pixbuf, width, height,
 282                                                          GDK_INTERP_BILINEAR);
 283             (*fp_g_object_unref)(pixbuf);
 284             pixbuf = scaledPixbuf;
 285         }
 286         
 287         if (pixbuf) {
 288             int nchan = (*fp_gdk_pixbuf_get_n_channels)(pixbuf);
 289             int stride = (*fp_gdk_pixbuf_get_rowstride)(pixbuf);
 290 
 291             if ((*fp_gdk_pixbuf_get_width)(pixbuf) == width
 292                     && (*fp_gdk_pixbuf_get_height)(pixbuf) == height
 293                     && (*fp_gdk_pixbuf_get_bits_per_sample)(pixbuf) == 8
 294                     && (*fp_gdk_pixbuf_get_colorspace)(pixbuf) == GDK_COLORSPACE_RGB
 295                     && nchan >= 3
 296                     ) {
 297                 guchar *p, *pix = (*fp_gdk_pixbuf_get_pixels)(pixbuf);
 298                 
 299                 ary = (*env)->GetPrimitiveArrayCritical(env, pixelArray, NULL);
 300                 if (!ary) {
 301                     (*fp_g_object_unref)(pixbuf);
 302                     (*fp_gdk_threads_leave)();
 303                     AWT_UNLOCK();
 304                     return;
 305                 }
 306 
 307                 for (_y = 0; _y < height; _y++) {
 308                     for (_x = 0; _x < width; _x++) {
 309                         p = pix + _y * stride + _x * nchan;
 310 
 311                         index = (_y + dy) * jwidth + (_x + dx);
 312                         ary[index] = 0xff000000
 313                                         | (p[0] << 16)
 314                                         | (p[1] << 8)
 315                                         | (p[2]);
 316 
 317                     }
 318                 }
 319                 (*env)->ReleasePrimitiveArrayCritical(env, pixelArray, ary, 0);
 320                 if ((*env)->ExceptionCheck(env)) {
 321                     (*fp_g_object_unref)(pixbuf);
 322                     (*fp_gdk_threads_leave)();
 323                     AWT_UNLOCK();
 324                     return;
 325                 }
 326                 gtk_failed = FALSE;
 327             }
 328             (*fp_g_object_unref)(pixbuf);
 329         }
 330         (*fp_gdk_threads_leave)();
 331     }
 332 
 333     if (gtk_failed) {
 334         image = getWindowImage(awt_display, rootWindow, sx, sy, swidth, sheight);
 335 
 336         ary = (*env)->GetPrimitiveArrayCritical(env, pixelArray, NULL);
 337 
 338         if (!ary) {
 339             XDestroyImage(image);
 340             AWT_UNLOCK();
 341             return;
 342         }
 343 
 344         dx /= scale;
 345         dy /= scale;
 346         width /= scale;
 347         height /= scale;
 348         
 349         /* convert to Java ARGB pixels */
 350         for (_y = 0; _y < height; _y++) {
 351             for (_x = 0; _x < width; _x++) {
 352                 jint pixel = (jint) XGetPixel(image, _x * scale, _y * scale);
 353                                                               /* Note ignore upper
 354                                                                * 32-bits on 64-bit
 355                                                                * OSes.
 356                                                                */
 357                 pixel |= 0xff000000; /* alpha - full opacity */
 358 
 359                 index = (_y + dy) * jwidth + (_x + dx);
 360                 ary[index] = pixel;
 361             }
 362         }
 363 
 364         XDestroyImage(image);
 365         (*env)->ReleasePrimitiveArrayCritical(env, pixelArray, ary, 0);
 366     }
 367     AWT_UNLOCK();
 368 }
 369 
 370 JNIEXPORT void JNICALL
 371 Java_sun_awt_X11_XRobotPeer_keyPressImpl (JNIEnv *env,
 372                          jclass cls,
 373                          jint keycode) {
 374 
 375     AWT_LOCK();
 376 
 377     DTRACE_PRINTLN1("RobotPeer: keyPressImpl(%i)", keycode);
 378 
 379     XTestFakeKeyEvent(awt_display,
 380                       XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
 381                       True,
 382                       CurrentTime);
 383 
 384     XSync(awt_display, False);
 385 
 386     AWT_UNLOCK();
 387 
 388 }
 389 
 390 JNIEXPORT void JNICALL
 391 Java_sun_awt_X11_XRobotPeer_keyReleaseImpl (JNIEnv *env,
 392                            jclass cls,
 393                            jint keycode) {
 394     AWT_LOCK();
 395 
 396     DTRACE_PRINTLN1("RobotPeer: keyReleaseImpl(%i)", keycode);
 397 
 398     XTestFakeKeyEvent(awt_display,
 399                       XKeysymToKeycode(awt_display, awt_getX11KeySym(keycode)),
 400                       False,
 401                       CurrentTime);
 402 
 403     XSync(awt_display, False);
 404 
 405     AWT_UNLOCK();
 406 }
 407 
 408 JNIEXPORT void JNICALL
 409 Java_sun_awt_X11_XRobotPeer_mouseMoveImpl (JNIEnv *env,
 410                           jclass cls,
 411                           jobject xgc,
 412                           jint root_x,
 413                           jint root_y) {
 414 
 415     AwtGraphicsConfigDataPtr adata;
 416 
 417     AWT_LOCK();
 418 
 419     DTRACE_PRINTLN3("RobotPeer: mouseMoveImpl(%lx, %i, %i)", xgc, root_x, root_y);
 420 
 421     adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
 422     DASSERT(adata != NULL);
 423 
 424     XWarpPointer(awt_display, None, XRootWindow(awt_display, adata->awt_visInfo.screen), 0, 0, 0, 0, root_x, root_y);
 425     XSync(awt_display, False);
 426 
 427     AWT_UNLOCK();
 428 }
 429 
 430 /*
 431   * Function joining the code of mousePressImpl and mouseReleaseImpl
 432   */
 433 void mouseAction(JNIEnv *env,
 434                  jclass cls,
 435                  jint buttonMask,
 436                  Bool isMousePress)
 437 {
 438     AWT_LOCK();
 439 
 440     DTRACE_PRINTLN1("RobotPeer: mouseAction(%i)", buttonMask);
 441     DTRACE_PRINTLN1("RobotPeer: mouseAction, press = %d", isMousePress);
 442 
 443     if (buttonMask & java_awt_event_InputEvent_BUTTON1_MASK ||
 444         buttonMask & java_awt_event_InputEvent_BUTTON1_DOWN_MASK )
 445     {
 446         XTestFakeButtonEvent(awt_display, 1, isMousePress, CurrentTime);
 447     }
 448     if ((buttonMask & java_awt_event_InputEvent_BUTTON2_MASK ||
 449          buttonMask & java_awt_event_InputEvent_BUTTON2_DOWN_MASK) &&
 450         (num_buttons >= 2)) {
 451         XTestFakeButtonEvent(awt_display, 2, isMousePress, CurrentTime);
 452     }
 453     if ((buttonMask & java_awt_event_InputEvent_BUTTON3_MASK ||
 454          buttonMask & java_awt_event_InputEvent_BUTTON3_DOWN_MASK) &&
 455         (num_buttons >= 3)) {
 456         XTestFakeButtonEvent(awt_display, 3, isMousePress, CurrentTime);
 457     }
 458 
 459     if (num_buttons > 3){
 460         int32_t i;
 461         int32_t button = 0;
 462         for (i = 3; i<num_buttons; i++){
 463             if ((buttonMask & masks[i])) {
 464                 // arrays starts from zero index => +1
 465                 // users wants to affect 4 or 5 button but they are assigned
 466                 // to the wheel so => we have to shift it to the right by 2.
 467                 button = i + 3;
 468                 XTestFakeButtonEvent(awt_display, button, isMousePress, CurrentTime);
 469             }
 470         }
 471     }
 472 
 473     XSync(awt_display, False);
 474     AWT_UNLOCK();
 475 }
 476 
 477 JNIEXPORT void JNICALL
 478 Java_sun_awt_X11_XRobotPeer_mousePressImpl (JNIEnv *env,
 479                            jclass cls,
 480                            jint buttonMask) {
 481     mouseAction(env, cls, buttonMask, True);
 482 }
 483 
 484 JNIEXPORT void JNICALL
 485 Java_sun_awt_X11_XRobotPeer_mouseReleaseImpl (JNIEnv *env,
 486                              jclass cls,
 487                              jint buttonMask) {
 488     mouseAction(env, cls, buttonMask, False);
 489 }
 490 
 491 JNIEXPORT void JNICALL
 492 Java_sun_awt_X11_XRobotPeer_mouseWheelImpl (JNIEnv *env,
 493                            jclass cls,
 494                            jint wheelAmt) {
 495 /* Mouse wheel is implemented as a button press of button 4 and 5, so it */
 496 /* probably could have been hacked into robot_mouseButtonEvent, but it's */
 497 /* cleaner to give it its own command type, in case the implementation   */
 498 /* needs to be changed later.  -bchristi, 6/20/01                        */
 499 
 500     int32_t repeat = abs(wheelAmt);
 501     int32_t button = wheelAmt < 0 ? 4 : 5;  /* wheel up:   button 4 */
 502                                                  /* wheel down: button 5 */
 503     int32_t loopIdx;
 504 
 505     AWT_LOCK();
 506 
 507     DTRACE_PRINTLN1("RobotPeer: mouseWheelImpl(%i)", wheelAmt);
 508 
 509     for (loopIdx = 0; loopIdx < repeat; loopIdx++) { /* do nothing for   */
 510                                                      /* wheelAmt == 0    */
 511         XTestFakeButtonEvent(awt_display, button, True, CurrentTime);
 512         XTestFakeButtonEvent(awt_display, button, False, CurrentTime);
 513     }
 514     XSync(awt_display, False);
 515 
 516     AWT_UNLOCK();
 517 }