< prev index next >

src/java.desktop/unix/native/libawt_xawt/awt/awt_Robot.c

Print this page




 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                              jintArray pixelArray,
 214                              jboolean isGtkSupported) {
 215     XImage *image;
 216     jint *ary;               /* Array of jints for sending pixel values back
 217                               * to parent process.
 218                               */
 219     Window rootWindow;
 220     XWindowAttributes attr;
 221     AwtGraphicsConfigDataPtr adata;
 222 
 223     DTRACE_PRINTLN6("RobotPeer: getRGBPixelsImpl(%lx, %d, %d, %d, %d, %x)", xgc, jx, jy, jwidth, jheight, pixelArray);
 224 
 225     if (jwidth <= 0 || jheight <= 0) {
 226         return;
 227     }
 228 
 229     adata = (AwtGraphicsConfigDataPtr) JNU_GetLongFieldAsPtr(env, xgc, x11GraphicsConfigIDs.aData);
 230     DASSERT(adata != NULL);
 231 
 232     AWT_LOCK();
 233 





 234     rootWindow = XRootWindow(awt_display, adata->awt_visInfo.screen);
 235 
 236     if (!XGetWindowAttributes(awt_display, rootWindow, &attr)
 237             || jx + jwidth <= attr.x
 238             || attr.x + attr.width <= jx
 239             || jy + jheight <= attr.y
 240             || attr.y + attr.height <= jy) {
 241 
 242         AWT_UNLOCK();
 243         return; // Does not intersect with root window
 244     }
 245 
 246     gboolean gtk_failed = TRUE;
 247     jint _x, _y;
 248 
 249     jint x = MAX(jx, attr.x);
 250     jint y = MAX(jy, attr.y);
 251     jint width = MIN(jx + jwidth, attr.x + attr.width) - x;
 252     jint height = MIN(jy + jheight, attr.y + attr.height) - y;
 253 
 254 
 255     int dx = attr.x > jx ? attr.x - jx : 0;
 256     int dy = attr.y > jy ? attr.y - jy : 0;
 257 
 258     int index;
 259 
 260     if (isGtkSupported) {
 261         GdkPixbuf *pixbuf;
 262         (*fp_gdk_threads_enter)();
 263         GdkWindow *root = (*fp_gdk_get_default_root_window)();
 264 
 265         pixbuf = (*fp_gdk_pixbuf_get_from_drawable)(NULL, root, NULL,
 266                                                     x, y, 0, 0, width, height);













 267 
 268         if (pixbuf) {
 269             int nchan = (*fp_gdk_pixbuf_get_n_channels)(pixbuf);
 270             int stride = (*fp_gdk_pixbuf_get_rowstride)(pixbuf);
 271 
 272             if ((*fp_gdk_pixbuf_get_width)(pixbuf) == width
 273                     && (*fp_gdk_pixbuf_get_height)(pixbuf) == height
 274                     && (*fp_gdk_pixbuf_get_bits_per_sample)(pixbuf) == 8
 275                     && (*fp_gdk_pixbuf_get_colorspace)(pixbuf) == GDK_COLORSPACE_RGB
 276                     && nchan >= 3
 277                     ) {
 278                 guchar *p, *pix = (*fp_gdk_pixbuf_get_pixels)(pixbuf);
 279 
 280                 ary = (*env)->GetPrimitiveArrayCritical(env, pixelArray, NULL);
 281                 if (!ary) {
 282                     (*fp_g_object_unref)(pixbuf);
 283                     (*fp_gdk_threads_leave)();
 284                     AWT_UNLOCK();
 285                     return;
 286                 }


 295                                         | (p[1] << 8)
 296                                         | (p[2]);
 297 
 298                     }
 299                 }
 300                 (*env)->ReleasePrimitiveArrayCritical(env, pixelArray, ary, 0);
 301                 if ((*env)->ExceptionCheck(env)) {
 302                     (*fp_g_object_unref)(pixbuf);
 303                     (*fp_gdk_threads_leave)();
 304                     AWT_UNLOCK();
 305                     return;
 306                 }
 307                 gtk_failed = FALSE;
 308             }
 309             (*fp_g_object_unref)(pixbuf);
 310         }
 311         (*fp_gdk_threads_leave)();
 312     }
 313 
 314     if (gtk_failed) {
 315         image = getWindowImage(awt_display, rootWindow, x, y, width, height);
 316 
 317         ary = (*env)->GetPrimitiveArrayCritical(env, pixelArray, NULL);
 318 
 319         if (!ary) {
 320             XDestroyImage(image);
 321             AWT_UNLOCK();
 322             return;
 323         }
 324 





 325         /* convert to Java ARGB pixels */
 326         for (_y = 0; _y < height; _y++) {
 327             for (_x = 0; _x < width; _x++) {
 328                 jint pixel = (jint) XGetPixel(image, _x, _y); /* Note ignore upper

 329                                                                * 32-bits on 64-bit
 330                                                                * OSes.
 331                                                                */
 332                 pixel |= 0xff000000; /* alpha - full opacity */
 333 
 334                 index = (_y + dy) * jwidth + (_x + dx);
 335                 ary[index] = pixel;
 336             }
 337         }
 338 
 339         XDestroyImage(image);
 340         (*env)->ReleasePrimitiveArrayCritical(env, pixelArray, ary, 0);
 341     }
 342     AWT_UNLOCK();
 343 }
 344 
 345 JNIEXPORT void JNICALL
 346 Java_sun_awt_X11_XRobotPeer_keyPressImpl (JNIEnv *env,
 347                          jclass cls,
 348                          jint keycode) {




 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                 }


 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) {


< prev index next >