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