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