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