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