1 /*
   2  * Copyright 2002-2009 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 #include "sun_awt_X11_XlibWrapper.h"
  27 #include <X11/Xlib.h>
  28 #include <X11/keysym.h>
  29 #include <X11/Xutil.h>
  30 #include <X11/Xos.h>
  31 #include <X11/Xatom.h>
  32 #include <X11/extensions/Xdbe.h>
  33 #include <X11/extensions/shape.h>
  34 #include <string.h>
  35 #include <stdlib.h>
  36 
  37 #include <jni.h>
  38 #include <jni_util.h>
  39 #include <jlong.h>
  40 
  41 #include <awt.h>
  42 #include <jvm.h>
  43 
  44 #include <Region.h>
  45 #include "utility/rect.h"
  46 
  47 #include <X11/XKBlib.h>
  48 #if defined(DEBUG) || defined(INTERNAL_BUILD)
  49 static jmethodID lockIsHeldMID = NULL;
  50 
  51 static void
  52 CheckHaveAWTLock(JNIEnv *env)
  53 {
  54     if (lockIsHeldMID == NULL) {
  55         if (tkClass == NULL) return;
  56         lockIsHeldMID =
  57             (*env)->GetStaticMethodID(env, tkClass,
  58                                       "isAWTLockHeldByCurrentThread", "()Z");
  59     }
  60     if (!(*env)->CallStaticBooleanMethod(env, tkClass, lockIsHeldMID)) {
  61         JNU_ThrowInternalError(env, "Current thread does not hold AWT_LOCK!");
  62     }
  63 }
  64 
  65 #define AWT_CHECK_HAVE_LOCK() CheckHaveAWTLock(env)
  66 #else
  67 #define AWT_CHECK_HAVE_LOCK()
  68 #endif
  69 
  70 
  71 /*
  72  * Class:     XlibWrapper
  73  * Method:    XOpenDisplay
  74  * Signature: (J)J
  75  */
  76 
  77 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XOpenDisplay
  78 (JNIEnv *env, jclass clazz, jlong display_name)
  79 {
  80     Display *dp;
  81     AWT_CHECK_HAVE_LOCK();
  82     dp  =  XOpenDisplay((char *) jlong_to_ptr(display_name));
  83 
  84     return ptr_to_jlong(dp);
  85 }
  86 
  87 JNIEXPORT void JNICALL
  88 Java_sun_awt_X11_XlibWrapper_XCloseDisplay(JNIEnv *env, jclass clazz,
  89                        jlong display) {
  90     AWT_CHECK_HAVE_LOCK();
  91     XCloseDisplay((Display*) jlong_to_ptr(display));
  92 }
  93 
  94 JNIEXPORT jlong JNICALL
  95 Java_sun_awt_X11_XlibWrapper_XDisplayString(JNIEnv *env, jclass clazz,
  96                         jlong display) {
  97     AWT_CHECK_HAVE_LOCK();
  98     return ptr_to_jlong(XDisplayString((Display*) jlong_to_ptr(display)));
  99 }
 100 
 101 JNIEXPORT void JNICALL
 102 Java_sun_awt_X11_XlibWrapper_XSetCloseDownMode(JNIEnv *env, jclass clazz,
 103                            jlong display, jint mode) {
 104     AWT_CHECK_HAVE_LOCK();
 105     XSetCloseDownMode((Display*) jlong_to_ptr(display), (int)mode);
 106 }
 107 
 108 /*
 109  * Class:     sun_awt_X11_XlibWrapper
 110  * Method:    DefaultScreen
 111  * Signature: (J)J
 112  */
 113 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_DefaultScreen (JNIEnv *env, jclass clazz, jlong display) {
 114 
 115     AWT_CHECK_HAVE_LOCK();
 116     return (jlong) DefaultScreen((Display *) jlong_to_ptr(display));
 117 }
 118 
 119 /*
 120  * Class:     sun_awt_X11_XlibWrapper
 121  * Method:    ScreenOfDisplay
 122  * Signature: (JJ)J
 123  */
 124 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_ScreenOfDisplay(JNIEnv *env, jclass clazz, jlong display, jlong screen_number) {
 125     AWT_CHECK_HAVE_LOCK();
 126     return ptr_to_jlong(ScreenOfDisplay((Display *) jlong_to_ptr(display),
 127                                         screen_number));
 128 }
 129 
 130 /*
 131  * Class:     sun_awt_X11_XlibWrapper
 132  * Method:    DoesBackingStore
 133  * Signature: (J)I
 134  */
 135 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_DoesBackingStore(JNIEnv *env, jclass clazz, jlong screen) {
 136     AWT_CHECK_HAVE_LOCK();
 137     return (jint) DoesBackingStore((Screen*) jlong_to_ptr(screen));
 138 }
 139 
 140 /*
 141  * Class:     sun_awt_X11_XlibWrapper
 142  * Method:    DisplayWidth
 143  * Signature: (JJ)J
 144  */
 145 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_DisplayWidth
 146 (JNIEnv *env, jclass clazz, jlong display, jlong screen) {
 147 
 148     AWT_CHECK_HAVE_LOCK();
 149     return (jlong) DisplayWidth((Display *) jlong_to_ptr(display),screen);
 150 
 151 }
 152 
 153 /*
 154  * Class:     sun_awt_X11_XlibWrapper
 155  * Method:    DisplayWidthMM
 156  * Signature: (JJ)J
 157  */
 158 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_DisplayWidthMM
 159 (JNIEnv *env, jclass clazz, jlong display, jlong screen) {
 160     AWT_CHECK_HAVE_LOCK();
 161     return (jlong) DisplayWidthMM((Display *) jlong_to_ptr(display),screen);
 162 }
 163 
 164 /*
 165  * Class:     sun_awt_X11_XlibWrapper
 166  * Method:    DisplayHeight
 167  * Signature: (JJ)J
 168  */
 169 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_DisplayHeight
 170 (JNIEnv *env, jclass clazz, jlong display, jlong screen) {
 171 
 172     AWT_CHECK_HAVE_LOCK();
 173     return (jlong) DisplayHeight((Display *) jlong_to_ptr(display),screen);
 174 }
 175 /*
 176  * Class:     sun_awt_X11_XlibWrapper
 177  * Method:    DisplayHeightMM
 178  * Signature: (JJ)J
 179  */
 180 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_DisplayHeightMM
 181 (JNIEnv *env, jclass clazz, jlong display, jlong screen) {
 182     AWT_CHECK_HAVE_LOCK();
 183     return (jlong) DisplayHeightMM((Display *) jlong_to_ptr(display),screen);
 184 }
 185 
 186 /*
 187  * Class:     sun_awt_X11_XlibWrapper
 188  * Method:    RootWindow
 189  * Signature: (JJ)J
 190  */
 191 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_RootWindow
 192 (JNIEnv *env , jclass clazz, jlong display, jlong screen_number) {
 193     AWT_CHECK_HAVE_LOCK();
 194     return (jlong) RootWindow((Display *) jlong_to_ptr(display), screen_number);
 195 }
 196 
 197 /*
 198  * Class:     sun_awt_X11_XlibWrapper
 199  * Method:    ScreenCount
 200  */
 201 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_ScreenCount
 202 (JNIEnv *env , jclass clazz, jlong display) {
 203     AWT_CHECK_HAVE_LOCK();
 204     return ScreenCount((Display *) jlong_to_ptr(display));
 205 }
 206 
 207 
 208 /*
 209  * Class:     XlibWrapper
 210  * Method:    XCreateWindow
 211  * Signature: (JJIIIIIIJJJJ)J
 212  */
 213 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XCreateWindow
 214   (JNIEnv *env, jclass clazz, jlong display, jlong window,
 215    jint x, jint y, jint w, jint h , jint border_width, jint depth,
 216    jlong wclass, jlong visual, jlong valuemask, jlong attributes)
 217 {
 218     AWT_CHECK_HAVE_LOCK();
 219     return  XCreateWindow((Display *) jlong_to_ptr(display),(Window) window, x, y, w, h,
 220               border_width, depth, wclass, (Visual *) jlong_to_ptr(visual),
 221               valuemask, (XSetWindowAttributes *) jlong_to_ptr(attributes));
 222 
 223 }
 224 
 225 /*
 226  * Class:     XlibWrapper
 227  * Method:    XConvertCase
 228  * Signature: (JJJ)V
 229  */
 230 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XConvertCase
 231   (JNIEnv *env, jclass clazz, jlong keysym,
 232    jlong keysym_lowercase, jlong keysym_uppercase)
 233 {
 234     AWT_CHECK_HAVE_LOCK();
 235     XConvertCase(keysym, (jlong_to_ptr(keysym_lowercase)),
 236                          (jlong_to_ptr(keysym_uppercase)));
 237 }
 238 
 239 
 240 /*
 241  * Class:     XlibWrapper
 242  * Method:    XMapWindow
 243  * Signature: (JJ)V
 244  */
 245 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XMapWindow
 246 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 247 {
 248     AWT_CHECK_HAVE_LOCK();
 249     XMapWindow( (Display *)jlong_to_ptr(display),(Window) window);
 250 
 251 }
 252 
 253 /*
 254  * Class:     XlibWrapper
 255  * Method:    XMapRaised
 256  * Signature: (JJ)V
 257  */
 258 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XMapRaised
 259 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 260 {
 261     AWT_CHECK_HAVE_LOCK();
 262     XMapRaised( (Display *)jlong_to_ptr(display),(Window) window);
 263 
 264 }
 265 
 266 /*
 267  * Class:     XlibWrapper
 268  * Method:    XRaiseWindow
 269  * Signature: (JJ)V
 270  */
 271 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XRaiseWindow
 272 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 273 {
 274 
 275     AWT_CHECK_HAVE_LOCK();
 276     XRaiseWindow( (Display *)jlong_to_ptr(display),(Window) window);
 277 
 278 }
 279 
 280 /*
 281  * Class:     XlibWrapper
 282  * Method:    XLowerWindow
 283  * Signature: (JJ)V
 284  */
 285 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XLowerWindow
 286 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 287 {
 288 
 289     AWT_CHECK_HAVE_LOCK();
 290     XLowerWindow( (Display *)jlong_to_ptr(display),(Window) window);
 291 
 292 }
 293 
 294 /*
 295  * Class:     XlibWrapper
 296  * Method:    XRestackWindows
 297  * Signature: (JJI)V
 298  */
 299 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XRestackWindows
 300 (JNIEnv *env, jclass clazz, jlong display, jlong windows, jint length)
 301 {
 302 
 303     AWT_CHECK_HAVE_LOCK();
 304     XRestackWindows( (Display *) jlong_to_ptr(display), (Window *) jlong_to_ptr(windows), length);
 305 
 306 }
 307 
 308 /*
 309  * Class:     XlibWrapper
 310  * Method:    XConfigureWindow
 311  * Signature: (JJJJ)V
 312  */
 313 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XConfigureWindow
 314 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong value_mask,
 315  jlong values)
 316 {
 317     AWT_CHECK_HAVE_LOCK();
 318     XConfigureWindow((Display*)jlong_to_ptr(display), (Window)window,
 319             (unsigned int)value_mask, (XWindowChanges*)jlong_to_ptr(values));
 320 }
 321 
 322 /*
 323  * Class:     XlibWrapper
 324  * Method:    XSetInputFocus
 325  * Signature: (JJ)V
 326  */
 327 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetInputFocus
 328 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 329 {
 330 
 331     AWT_CHECK_HAVE_LOCK();
 332     XSetInputFocus( (Display *)jlong_to_ptr(display),(Window) window, RevertToPointerRoot, CurrentTime);
 333 
 334 }
 335 /*
 336  * Class:     XlibWrapper
 337  * Method:    XSetInputFocus2
 338  * Signature: (JJJ)V
 339  */
 340 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetInputFocus2
 341 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong time)
 342 {
 343 
 344     AWT_CHECK_HAVE_LOCK();
 345     XSetInputFocus( (Display *)jlong_to_ptr(display),(Window) window, RevertToPointerRoot, time);
 346 
 347 }
 348 
 349 /*
 350  * Class:     XlibWrapper
 351  * Method:    XGetInputFocus
 352  * Signature: (JJ)V
 353  */
 354 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XGetInputFocus
 355 (JNIEnv *env, jclass clazz, jlong display)
 356 {
 357 
 358     Window focusOwner;
 359     int revert_to;
 360     AWT_CHECK_HAVE_LOCK();
 361     XGetInputFocus( (Display *)jlong_to_ptr(display), &focusOwner, &revert_to);
 362     return focusOwner;
 363 }
 364 
 365 
 366 /*
 367  * Class:     XlibWrapper
 368  * Method:    XDestroyWindow
 369  * Signature: (JJ)V
 370  */
 371 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XDestroyWindow
 372 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 373 {
 374     AWT_CHECK_HAVE_LOCK();
 375     XDestroyWindow( (Display *)jlong_to_ptr(display),(Window) window);
 376 }
 377 
 378 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGrabPointer
 379 (JNIEnv *env, jclass clazz, jlong display, jlong window,
 380  jint owner_events, jint event_mask, jint pointer_mode,
 381  jint keyboard_mode, jlong confine_to, jlong cursor, jlong time)
 382 {
 383     AWT_CHECK_HAVE_LOCK();
 384     return XGrabPointer( (Display *)jlong_to_ptr(display), (Window) window,
 385              (Bool) owner_events, (unsigned int) event_mask, (int) pointer_mode,
 386              (int) keyboard_mode, (Window) confine_to, (Cursor) cursor, (Time) time);
 387 }
 388 
 389 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XUngrabPointer
 390 (JNIEnv *env, jclass clazz, jlong display, jlong time)
 391 {
 392     AWT_CHECK_HAVE_LOCK();
 393     XUngrabPointer( (Display *)jlong_to_ptr(display), (Time) time);
 394 }
 395 
 396 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGrabKeyboard
 397 (JNIEnv *env, jclass clazz, jlong display, jlong window,
 398  jint owner_events, jint pointer_mode,
 399  jint keyboard_mode, jlong time)
 400 {
 401     AWT_CHECK_HAVE_LOCK();
 402     return XGrabKeyboard( (Display *)jlong_to_ptr(display), (Window) window,
 403               (Bool) owner_events, (int) pointer_mode,
 404               (int) keyboard_mode, (Time) time);
 405 }
 406 
 407 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XUngrabKeyboard
 408 (JNIEnv *env, jclass clazz, jlong display, jlong time)
 409 {
 410     AWT_CHECK_HAVE_LOCK();
 411     XUngrabKeyboard( (Display *)jlong_to_ptr(display), (Time) time);
 412 }
 413 
 414 JNIEXPORT void JNICALL
 415 Java_sun_awt_X11_XlibWrapper_XGrabServer(JNIEnv *env, jclass clazz,
 416                                          jlong display) {
 417      AWT_CHECK_HAVE_LOCK();
 418      XGrabServer((Display*)jlong_to_ptr(display));
 419 }
 420 
 421 JNIEXPORT void JNICALL
 422 Java_sun_awt_X11_XlibWrapper_XUngrabServer(JNIEnv *env, jclass clazz,
 423                                            jlong display) {
 424      AWT_CHECK_HAVE_LOCK();
 425      XUngrabServer((Display*)jlong_to_ptr(display));
 426      /* Workaround for bug 5039226 */
 427      XSync((Display*)jlong_to_ptr(display), False);
 428 }
 429 
 430 /*
 431  * Class:     XlibWrapper
 432  * Method:    XUnmapWindow
 433  * Signature: (JJ)V
 434  */
 435 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XUnmapWindow
 436 (JNIEnv *env, jclass clazz, jlong display, jlong window)
 437 {
 438 
 439     AWT_CHECK_HAVE_LOCK();
 440     XUnmapWindow( (Display *)jlong_to_ptr(display),(Window) window);
 441 
 442 }
 443 
 444 
 445 
 446 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSelectInput
 447 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong mask)
 448 {
 449     AWT_CHECK_HAVE_LOCK();
 450     XSelectInput((Display *) jlong_to_ptr(display), (Window) window, mask);
 451 }
 452 
 453 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XkbSelectEvents
 454 (JNIEnv *env, jclass clazz, jlong display, jlong device, jlong bits_to_change, jlong values_for_bits)
 455 {
 456     AWT_CHECK_HAVE_LOCK();
 457     XkbSelectEvents((Display *) jlong_to_ptr(display), (unsigned int)device,
 458                    (unsigned long)bits_to_change,
 459                    (unsigned long)values_for_bits);
 460 }
 461 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XkbSelectEventDetails
 462 (JNIEnv *env, jclass clazz, jlong display, jlong device, jlong event_type, jlong bits_to_change, jlong values_for_bits)
 463 {
 464     AWT_CHECK_HAVE_LOCK();
 465     XkbSelectEventDetails((Display *) jlong_to_ptr(display), (unsigned int)device,
 466                    (unsigned int) event_type,
 467                    (unsigned long)bits_to_change,
 468                    (unsigned long)values_for_bits);
 469 }
 470 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XkbQueryExtension
 471 (JNIEnv *env, jclass clazz, jlong display, jlong opcode_rtrn, jlong event_rtrn,
 472               jlong error_rtrn, jlong major_in_out, jlong minor_in_out)
 473 {
 474     AWT_CHECK_HAVE_LOCK();
 475     return XkbQueryExtension( (Display *) jlong_to_ptr(display),
 476                        (int *) jlong_to_ptr(opcode_rtrn),
 477                        (int *) jlong_to_ptr(event_rtrn),
 478                        (int *) jlong_to_ptr(error_rtrn),
 479                        (int *) jlong_to_ptr(major_in_out),
 480                        (int *) jlong_to_ptr(minor_in_out));
 481 }
 482 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XkbLibraryVersion
 483 (JNIEnv *env, jclass clazz, jlong lib_major_in_out, jlong lib_minor_in_out)
 484 {
 485     AWT_CHECK_HAVE_LOCK();
 486     *((int *)lib_major_in_out) =  XkbMajorVersion;
 487     *((int *)lib_minor_in_out) =  XkbMinorVersion;
 488     return  XkbLibraryVersion((int *)jlong_to_ptr(lib_major_in_out), (int *)jlong_to_ptr(lib_minor_in_out));
 489 }
 490 
 491 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XkbGetMap
 492 (JNIEnv *env, jclass clazz, jlong display, jlong which, jlong device_spec)
 493 {
 494     AWT_CHECK_HAVE_LOCK();
 495     return (jlong) XkbGetMap( (Display *) jlong_to_ptr(display),
 496                               (unsigned int) which,
 497                               (unsigned int) device_spec);
 498 }
 499 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XkbGetUpdatedMap
 500 (JNIEnv *env, jclass clazz, jlong display, jlong which, jlong xkb)
 501 {
 502     AWT_CHECK_HAVE_LOCK();
 503     return (jlong) XkbGetUpdatedMap( (Display *) jlong_to_ptr(display),
 504                               (unsigned int) which,
 505                               (XkbDescPtr) jlong_to_ptr(xkb));
 506 }
 507 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XkbFreeKeyboard
 508 (JNIEnv *env, jclass clazz, jlong xkb, jlong which, jboolean free_all)
 509 {
 510     AWT_CHECK_HAVE_LOCK();
 511     XkbFreeKeyboard(jlong_to_ptr(xkb), (unsigned int)which, free_all);
 512 }
 513 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XkbTranslateKeyCode
 514 (JNIEnv *env, jclass clazz, jlong xkb, jint keycode, jlong mods, jlong mods_rtrn, jlong keysym_rtrn)
 515 {
 516     Bool b;
 517     b = XkbTranslateKeyCode((XkbDescPtr)xkb, (unsigned int)keycode, (unsigned int)mods,
 518                               (unsigned int *)jlong_to_ptr(mods_rtrn),
 519                                (KeySym *)jlong_to_ptr(keysym_rtrn));
 520     //printf("native,  input: keycode:0x%0X; mods:0x%0X\n", keycode, mods);
 521     //printf("native, output:  keysym:0x%0X; mods:0x%0X\n", *(unsigned int *)jlong_to_ptr(keysym_rtrn), *(unsigned int *)jlong_to_ptr(mods_rtrn));
 522     return b;
 523 }
 524 
 525 
 526 /*
 527  * Class:     sun_awt_X11_XlibWrapper
 528  * Method:    XNextEvent
 529  * Signature: (JJ)V
 530  */
 531 
 532 
 533 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XNextEvent
 534 (JNIEnv *env, jclass clazz, jlong display, jlong ptr)
 535 {
 536     AWT_CHECK_HAVE_LOCK();
 537     XNextEvent( (Display *) jlong_to_ptr(display), jlong_to_ptr(ptr));
 538 }
 539 
 540 /*
 541  * Class:     sun_awt_X11_XlibWrapper
 542  * Method:    XMaskEvent
 543  * Signature: (JJJ)V
 544  */
 545 
 546 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XMaskEvent
 547   (JNIEnv *env, jclass clazz, jlong display, jlong event_mask, jlong event_return)
 548 {
 549     AWT_CHECK_HAVE_LOCK();
 550     XMaskEvent( (Display *) jlong_to_ptr(display), event_mask, (XEvent *) jlong_to_ptr(event_return));
 551 }
 552 
 553 /*
 554  * Class:     sun_awt_X11_XlibWrapper
 555  * Method:    XWindowEvent
 556  * Signature: (JJJJ)V
 557  */
 558 
 559 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XWindowEvent
 560   (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong event_mask, jlong event_return)
 561 {
 562     AWT_CHECK_HAVE_LOCK();
 563     XWindowEvent( (Display *) jlong_to_ptr(display), (Window)window, event_mask, (XEvent *) jlong_to_ptr(event_return));
 564 }
 565 
 566 /*
 567  * Class:     sun_awt_X11_XlibWrapper
 568  * Method:    XFilterEvent
 569  * Signature: (JJ)Z
 570  */
 571 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XFilterEvent
 572 (JNIEnv *env, jclass clazz, jlong ptr, jlong window)
 573 {
 574     AWT_CHECK_HAVE_LOCK();
 575     return (jboolean) XFilterEvent((XEvent *) jlong_to_ptr(ptr), (Window) window);
 576 }
 577 
 578 /*
 579  * Class:     sun_awt_X11_XlibWrapper
 580  * Method:    XSupportsLocale
 581  * Signature: ()Z
 582  */
 583 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XSupportsLocale
 584 (JNIEnv *env, jclass clazz)
 585 {
 586     AWT_CHECK_HAVE_LOCK();
 587     return (jboolean)XSupportsLocale();
 588 }
 589 
 590 /*
 591  * Class:     sun_awt_X11_XlibWrapper
 592  * Method:    XSetLocaleModifiers
 593  * Signature: (Ljava/lang/String;)Ljava/lang/String;
 594  */
 595 JNIEXPORT jstring JNICALL Java_sun_awt_X11_XlibWrapper_XSetLocaleModifiers
 596 (JNIEnv *env, jclass clazz, jstring jstr)
 597 {
 598     char * modifier_list = NULL;
 599     char * ret = NULL;
 600 
 601     if (!JNU_IsNull(env, jstr)) {
 602         modifier_list = (char *)JNU_GetStringPlatformChars(env, jstr, NULL);
 603     }
 604 
 605     AWT_CHECK_HAVE_LOCK();
 606     if (modifier_list) {
 607         ret = XSetLocaleModifiers(modifier_list);
 608         JNU_ReleaseStringPlatformChars(env, jstr, (const char *) modifier_list);
 609     } else {
 610         ret = XSetLocaleModifiers("");
 611     }
 612 
 613     return (ret != NULL ? JNU_NewStringPlatform(env, ret): NULL);
 614 }
 615 
 616 
 617 /*
 618  * Class:     sun_awt_X11_wrappers_XlibWrapper
 619  * Method:    XPeekEvent
 620  * Signature: (JJ)V
 621  */
 622 
 623 
 624 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XPeekEvent
 625 (JNIEnv *env, jclass clazz, jlong display, jlong ptr)
 626 {
 627     AWT_CHECK_HAVE_LOCK();
 628     XPeekEvent((Display *) jlong_to_ptr(display),jlong_to_ptr(ptr));
 629 }
 630 
 631 
 632 /*
 633  * Class:     sun_awt_X11_XlibWrapper
 634  * Method:    XMoveResizeWindow
 635  * Signature: (JJIIII)V
 636  */
 637 
 638 JNIEXPORT void JNICALL  Java_sun_awt_X11_XlibWrapper_XMoveResizeWindow
 639 (JNIEnv *env, jclass clazz, jlong display, jlong window, jint x , jint y , jint width, jint height) {
 640 
 641     AWT_CHECK_HAVE_LOCK();
 642     XMoveResizeWindow( (Display *) jlong_to_ptr(display), (Window) window, x, y, width, height);
 643 
 644 }
 645 
 646 /*
 647  * Class:     sun_awt_X11_XlibWrapper
 648  * Method:    XResizeWindow
 649  * Signature: (JJII)V
 650  */
 651 
 652 JNIEXPORT void JNICALL  Java_sun_awt_X11_XlibWrapper_XResizeWindow
 653 (JNIEnv *env, jclass clazz, jlong display, jlong window, jint width, jint height)
 654 {
 655     AWT_CHECK_HAVE_LOCK();
 656     XResizeWindow( (Display *) jlong_to_ptr(display),(Window) window,width,height);
 657 }
 658 
 659 /*
 660  * Class:     sun_awt_X11_XlibWrapper
 661  * Method:    XMoveWindow
 662  * Signature: (JJII)V
 663  */
 664 
 665 JNIEXPORT void JNICALL  Java_sun_awt_X11_XlibWrapper_XMoveWindow
 666 (JNIEnv *env, jclass clazz, jlong display, jlong window, jint width, jint height)
 667 {
 668     AWT_CHECK_HAVE_LOCK();
 669     XMoveWindow( (Display *) jlong_to_ptr(display),(Window) window,width,height);
 670 }
 671 
 672 
 673 /*
 674  * Class:     sun_awt_X11_XlibWrapper
 675  * Method:    XSetWindowBackground
 676  * Signature: (JJJ)V
 677  */
 678 
 679 JNIEXPORT void JNICALL  Java_sun_awt_X11_XlibWrapper_XSetWindowBackground
 680 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong background_pixel) {
 681 
 682     AWT_CHECK_HAVE_LOCK();
 683     XSetWindowBackground((Display *) jlong_to_ptr(display),window,background_pixel);
 684 
 685 }
 686 
 687 
 688 /*
 689  * Class:     sun_awt_X11_XlibWrapper
 690  * Method:    XFlush
 691  * Signature: (J)V
 692  */
 693 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XFlush
 694 (JNIEnv *env, jclass clazz, jlong display) {
 695 
 696     AWT_CHECK_HAVE_LOCK();
 697     XFlush((Display *)jlong_to_ptr(display));
 698 }
 699 
 700 /*
 701  * Class:     sun_awt_X11_XlibWrapper
 702  * Method:    XSync
 703  * Signature: (JI)V
 704  */
 705 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSync
 706 (JNIEnv *env, jclass clazz, jlong display, jint discard) {
 707 
 708     AWT_CHECK_HAVE_LOCK();
 709     XSync((Display *) jlong_to_ptr(display), discard);
 710 
 711 }
 712 
 713 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XTranslateCoordinates
 714 (JNIEnv *env, jclass clazz, jlong display, jlong src_w, jlong dest_w,
 715  jlong src_x, jlong src_y, jlong dest_x_return, jlong dest_y_return,
 716  jlong child_return)
 717 {
 718     AWT_CHECK_HAVE_LOCK();
 719     return XTranslateCoordinates( (Display *) jlong_to_ptr(display), src_w, dest_w,
 720                   src_x, src_y,
 721                   (int *) jlong_to_ptr(dest_x_return),
 722                   (int *) jlong_to_ptr(dest_y_return),
 723                   (Window *) jlong_to_ptr(child_return));
 724 }
 725 
 726 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XEventsQueued
 727 (JNIEnv *env, jclass clazz, jlong display, jint mode) {
 728 
 729     AWT_CHECK_HAVE_LOCK();
 730     return XEventsQueued((Display *) jlong_to_ptr(display), mode);
 731 
 732 }
 733 
 734 /*
 735  * Class:     sun_awt_X11_XlibWrapper
 736  * Method:    SetProperty
 737  * Signature: (JJJLjava/lang/String;)V
 738  */
 739 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_SetProperty
 740 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong atom, jstring jstr) {
 741     char *cname;
 742     XTextProperty tp;
 743     int32_t status;
 744 
 745     /*
 746        In case there are direct support of UTF-8 declared, use UTF-8 strings.
 747     */
 748     if (!JNU_IsNull(env, jstr)) {
 749 #ifdef X_HAVE_UTF8_STRING
 750         cname = (char *) (*env)->GetStringUTFChars(env, jstr, JNI_FALSE);
 751 #else
 752         cname = (char *) JNU_GetStringPlatformChars(env, jstr, NULL);
 753 #endif
 754     } else {
 755         cname = "";
 756     }
 757 
 758 
 759     AWT_CHECK_HAVE_LOCK();
 760 
 761 #ifdef X_HAVE_UTF8_STRING
 762     status = Xutf8TextListToTextProperty((Display *)jlong_to_ptr(display), &cname, 1,
 763                                        XStdICCTextStyle, &tp);
 764 #else
 765     status = XmbTextListToTextProperty((Display *)jlong_to_ptr(display), &cname, 1,
 766                                        XStdICCTextStyle, &tp);
 767 #endif
 768 
 769 
 770     if (status == Success || status > 0) {
 771         XChangeProperty((Display *)jlong_to_ptr(display), window, atom, tp.encoding, tp.format, PropModeReplace, tp.value, tp.nitems);
 772         if (tp.value != NULL) {
 773             XFree(tp.value);
 774         }
 775     }
 776 
 777     if (!JNU_IsNull(env, jstr)) {
 778 #ifdef X_HAVE_UTF8_STRING
 779         (*env)->ReleaseStringUTFChars(env, jstr, (const char *) cname);
 780 #else
 781         JNU_ReleaseStringPlatformChars(env, jstr, (const char *) cname);
 782 #endif
 783     }
 784 }
 785 
 786 /*
 787  * Class:     sun_awt_X11_XlibWrapper
 788  * Method:    XChangeProperty
 789  * Signature: (JJJJJJJJJJJ)V
 790  */
 791 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XChangePropertyImpl(
 792     JNIEnv *env, jclass clazz, jlong display, jlong window, jlong property,
 793     jlong type, jint format, jint mode, jlong data, jint nelements)
 794 {
 795     AWT_CHECK_HAVE_LOCK();
 796     XChangeProperty((Display*) jlong_to_ptr(display), (Window) window, (Atom) property,
 797             (Atom) type, format, mode, (unsigned char*) jlong_to_ptr(data),
 798             nelements);
 799 }
 800 /*
 801  * Class:     sun_awt_X11_XlibWrapper
 802  * Method:    XChangePropertyS
 803  * Signature: (JJJJJJJJJLjava/lang/String;)V
 804  */
 805 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XChangePropertyS(
 806     JNIEnv *env, jclass clazz, jlong display, jlong window, jlong property,
 807     jlong type, jint format, jint mode, jstring value)
 808 {
 809     jboolean iscopy;
 810     const char * chars = JNU_GetStringPlatformChars(env, value, &iscopy);
 811     AWT_CHECK_HAVE_LOCK();
 812     XChangeProperty((Display*)jlong_to_ptr(display), window, (Atom)property,
 813                     (Atom)type, format, mode, (unsigned char*)chars, strlen(chars));
 814     if (iscopy) {
 815         JNU_ReleaseStringPlatformChars(env, value, chars);
 816     }
 817 }
 818 
 819 /*
 820  * Class:     sun_awt_X11_XlibWrapper
 821  * Method:    XGetWindowProperty
 822  * Signature: (JJJJJJJJJJJ)J;
 823  */
 824 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGetWindowProperty
 825 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong property, jlong long_offset,
 826  jlong long_length, jlong delete, jlong req_type, jlong actual_type,
 827  jlong actual_format, jlong nitems_ptr, jlong bytes_after, jlong data_ptr)
 828 {
 829     AWT_CHECK_HAVE_LOCK();
 830     return XGetWindowProperty((Display*) jlong_to_ptr(display), window, property, long_offset, long_length,
 831                   delete, (Atom) req_type, (Atom*) jlong_to_ptr(actual_type),
 832                   (int *) jlong_to_ptr(actual_format), (unsigned long *) jlong_to_ptr(nitems_ptr),
 833                   (unsigned long*) jlong_to_ptr(bytes_after), (unsigned char**) jlong_to_ptr(data_ptr));
 834 }
 835 
 836 /*
 837  * Class:     sun_awt_X11_XlibWrapper
 838  * Method:    GetProperty
 839  * Signature: (JJJ)Ljava/lang/String;
 840  */
 841 JNIEXPORT jstring JNICALL Java_sun_awt_X11_XlibWrapper_GetProperty
 842 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong atom)
 843 {
 844     /* Request status */
 845     int status;
 846 
 847     /* Returns of XGetWindowProperty */
 848     Atom actual_type;
 849     int actual_format;
 850     unsigned long nitems;
 851     unsigned long bytes_after;
 852     unsigned char * string;
 853     jstring res;
 854     AWT_CHECK_HAVE_LOCK();
 855     status = XGetWindowProperty((Display*)jlong_to_ptr(display), window,
 856                                 atom, 0, 0xFFFF, False, XA_STRING,
 857                                 &actual_type, &actual_format, &nitems, &bytes_after,
 858                                 &string);
 859     if (status != Success || string == NULL) {
 860     return NULL;
 861     }
 862 
 863     if (actual_type != XA_STRING || actual_format != 8) {
 864     XFree(string);
 865     return NULL;
 866     }
 867 
 868     // Memory leak???
 869     return JNU_NewStringPlatform(env,(char*) string);
 870 }
 871 
 872 /*
 873  * Class:     sun_awt_X11_XlibWrapper
 874  * Method:    InternAtom
 875  * Signature: (JLjava/lang/String;I)J
 876  */
 877 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_InternAtom
 878 (JNIEnv *env, jclass clazz, jlong display, jstring jstr, jint ife) {
 879 
 880     char *cname;
 881     unsigned long atom;
 882 
 883     if (!JNU_IsNull(env, jstr)) {
 884         cname = (char *)JNU_GetStringPlatformChars(env, jstr, NULL);
 885     } else {
 886         cname = "";
 887     }
 888 
 889     AWT_CHECK_HAVE_LOCK();
 890     atom = XInternAtom((Display *) jlong_to_ptr(display), cname, ife);
 891 
 892     if (!JNU_IsNull(env, jstr)) {
 893         JNU_ReleaseStringPlatformChars(env, jstr, (const char *) cname);
 894     }
 895 
 896     return (jlong) atom;
 897 
 898 }
 899 
 900 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XCreateFontCursor
 901 (JNIEnv *env, jclass clazz, jlong display, jint shape) {
 902     AWT_CHECK_HAVE_LOCK();
 903     return XCreateFontCursor((Display *) jlong_to_ptr(display), (int) shape);
 904 }
 905 
 906 
 907 /*
 908  * Class:     sun_awt_X11_XlibWrapper
 909  * Method:    XCreatePixmapCursor
 910  * Signature: (JJJJJII)J
 911  */
 912 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XCreatePixmapCursor
 913 (JNIEnv *env , jclass clazz, jlong display, jlong source, jlong mask, jlong fore, jlong back, jint x , jint y) {
 914 
 915     AWT_CHECK_HAVE_LOCK();
 916     return (jlong) XCreatePixmapCursor((Display *) jlong_to_ptr(display), (Pixmap) source, (Pixmap) mask,
 917                                        (XColor *) jlong_to_ptr(fore), (XColor *) jlong_to_ptr(back), x, y);
 918 }
 919 
 920 
 921 /*
 922  * Class:     sun_awt_X11_XlibWrapper
 923  * Method:    XQueryBestCursor
 924  * Signature: (JJIIJJ)Z
 925  */
 926 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XQueryBestCursor
 927 (JNIEnv *env, jclass clazz, jlong display, jlong drawable, jint width, jint height, jlong width_return, jlong height_return) {
 928 
 929     Status status;
 930 
 931     AWT_CHECK_HAVE_LOCK();
 932     status  =  XQueryBestCursor((Display *) jlong_to_ptr(display), (Drawable) drawable, width,height,
 933                                 (unsigned int *) jlong_to_ptr(width_return), (unsigned int *) jlong_to_ptr(height_return));
 934 
 935     if (status == 0) return JNI_FALSE;
 936     else return JNI_TRUE;
 937 }
 938 
 939 
 940 /*
 941  * Class:     sun_awt_X11_XlibWrapper
 942  * Method:    XFreeCursor
 943  * Signature: (JJ)V
 944  */
 945 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XFreeCursor
 946 (JNIEnv *env, jclass clazz, jlong display, jlong cursor) {
 947 
 948     AWT_CHECK_HAVE_LOCK();
 949     XFreeCursor( (Display *) jlong_to_ptr(display), (Cursor) cursor);
 950 }
 951 
 952 /*
 953  * Class:     sun_awt_X11_XlibWrapper
 954  * Method:    XQueryPointer
 955  * Signature: (JJJJJJJJJ)Z
 956  */
 957 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XQueryPointer
 958 (JNIEnv *env, jclass clazz, jlong display, jlong w, jlong root_return, jlong child_return, jlong root_x_return , jlong root_y_return, jlong win_x_return, jlong win_y_return, jlong mask_return) {
 959 
 960     Bool b;
 961 
 962     AWT_CHECK_HAVE_LOCK();
 963     b = XQueryPointer((Display *) jlong_to_ptr(display),
 964                       (Window) w, (Window *) jlong_to_ptr(root_return), (Window *) jlong_to_ptr(child_return),
 965                       (int *) jlong_to_ptr(root_x_return), (int *) jlong_to_ptr(root_y_return),
 966                       (int *) jlong_to_ptr(win_x_return), (int *) jlong_to_ptr(win_y_return),
 967                       (unsigned int *) jlong_to_ptr(mask_return));
 968     if (b == True) return JNI_TRUE;
 969     else return JNI_FALSE;
 970 
 971 }
 972 
 973 /*
 974  * Class:     sun_awt_X11_XlibWrapper
 975  * Method:    XChangeWindowAttributes
 976  * Signature: (JJJJ)V
 977  */
 978 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XChangeWindowAttributes
 979 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong valuemask, jlong attributes) {
 980 
 981     AWT_CHECK_HAVE_LOCK();
 982     XChangeWindowAttributes((Display *) jlong_to_ptr(display), (Window) window, (unsigned long) valuemask,
 983                             (XSetWindowAttributes *) jlong_to_ptr(attributes));
 984 }
 985 
 986 
 987 /*
 988  * Class:     sun_awt_X11_XlibWrapper
 989  * Method:    XSetTransientFor
 990  * Signature: (JJJ)V
 991  */
 992 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetTransientFor
 993 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong transient_for_window)
 994 {
 995     AWT_CHECK_HAVE_LOCK();
 996     XSetTransientForHint((Display *) jlong_to_ptr(display), window, transient_for_window);
 997 }
 998 
 999 /*
1000  * Class:     sun_awt_X11_XlibWrapper
1001  * Method:    XSetWMHints
1002  * Signature: (JJJ)V
1003  */
1004 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetWMHints
1005 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong hints)
1006 {
1007     AWT_CHECK_HAVE_LOCK();
1008     XSetWMHints((Display *) jlong_to_ptr(display), window, (XWMHints *) jlong_to_ptr(hints));
1009 }
1010 
1011 /*
1012  * Class:     sun_awt_X11_XlibWrapper
1013  * Method:    XGetWMHints
1014  * Signature: (JJJ)V
1015  */
1016 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XGetWMHints
1017 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong hints)
1018 {
1019     XWMHints * get_hints;
1020     AWT_CHECK_HAVE_LOCK();
1021     get_hints = XGetWMHints((Display*)jlong_to_ptr(display), window);
1022     if (get_hints != NULL) {
1023         memcpy(jlong_to_ptr(hints), get_hints, sizeof(XWMHints));
1024         XFree(get_hints);
1025     } else {
1026         memset(jlong_to_ptr(hints), 0, sizeof(XWMHints));
1027     }
1028 }
1029 
1030 /*
1031  * Class:     sun_awt_X11_XlibWrapper
1032  * Method:    XGetPointerMapping
1033  * Signature: (JJI)I
1034  */
1035 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGetPointerMapping
1036 (JNIEnv *env, jclass clazz, jlong display, jlong map, jint buttonNumber)
1037 {
1038     AWT_CHECK_HAVE_LOCK();
1039     return XGetPointerMapping((Display*)jlong_to_ptr(display), (unsigned char*) jlong_to_ptr(map), buttonNumber);
1040 }
1041 
1042 /*
1043  * Class:     sun_awt_X11_XlibWrapper
1044  * Method:    XGetDefault
1045  * Signature: (JJI)I
1046  */
1047 JNIEXPORT jstring JNICALL Java_sun_awt_X11_XlibWrapper_XGetDefault
1048 (JNIEnv *env, jclass clazz, jlong display, jstring program, jstring option)
1049 {
1050     char * c_program = NULL;
1051     char * c_option = NULL;
1052     char * c_res = NULL;
1053 
1054     if (!JNU_IsNull(env, program)) {
1055         c_program = (char *)JNU_GetStringPlatformChars(env, program, NULL);
1056     }
1057     if (!JNU_IsNull(env, option)) {
1058         c_option = (char *)JNU_GetStringPlatformChars(env, option, NULL);
1059     }
1060 
1061     if (c_program == NULL || c_option == NULL) {
1062         if (!JNU_IsNull(env, program)) {
1063             JNU_ReleaseStringPlatformChars(env, program, (const char *) c_program);
1064         }
1065         if (!JNU_IsNull(env, option)) {
1066             JNU_ReleaseStringPlatformChars(env, option, (const char *) c_option);
1067         }
1068         return NULL;
1069     }
1070     AWT_CHECK_HAVE_LOCK();
1071     c_res = XGetDefault((Display*)jlong_to_ptr(display), c_program, c_option);
1072 
1073     if (!JNU_IsNull(env, program)) {
1074         JNU_ReleaseStringPlatformChars(env, program, (const char *) c_program);
1075     }
1076     if (!JNU_IsNull(env, option)) {
1077         JNU_ReleaseStringPlatformChars(env, option, (const char *) c_option);
1078     }
1079 
1080     if (c_res != NULL) {
1081         // Memory leak???
1082         return JNU_NewStringPlatform(env, c_res);
1083     } else {
1084         return NULL;
1085     }
1086 }
1087 
1088 
1089 /*
1090  * Class:     sun_awt_X11_XlibWrapper
1091  * Method:    getScreenOfWindow
1092  * Signature: (JJ)J
1093  */
1094 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_getScreenOfWindow
1095 (JNIEnv *env, jclass clazz, jlong display, jlong window)
1096 {
1097     XWindowAttributes attrs;
1098     memset(&attrs, 0, sizeof(attrs));
1099     AWT_CHECK_HAVE_LOCK();
1100     XGetWindowAttributes((Display *) jlong_to_ptr(display), window, &attrs);
1101     return ptr_to_jlong(attrs.screen);
1102 }
1103 
1104 /*
1105  * Class:     sun_awt_X11_XlibWrapper
1106  * Method:    XScreenNumberOfScreen
1107  * Signature: (J)J
1108  */
1109 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XScreenNumberOfScreen
1110 (JNIEnv *env, jclass clazz, jlong screen)
1111 {
1112     AWT_CHECK_HAVE_LOCK();
1113     if(jlong_to_ptr(screen) == NULL) {
1114         return -1;
1115     }
1116     return XScreenNumberOfScreen((Screen*) jlong_to_ptr(screen));
1117 }
1118 
1119 /*
1120  * Class:     sun_awt_X11_XlibWrapper
1121  * Method:    XIconifyWindow
1122  * Signature: (JJJ)V
1123  */
1124 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XIconifyWindow
1125 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong screenNumber)
1126 {
1127     AWT_CHECK_HAVE_LOCK();
1128     return XIconifyWindow((Display*) jlong_to_ptr(display), window, screenNumber);
1129 }
1130 
1131 /*
1132  * Class:     sun_awt_X11_XlibWrapper
1133  * Method:    XFree
1134  * Signature: (J)V
1135  */
1136 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XFree
1137 (JNIEnv *env, jclass clazz, jlong ptr)
1138 {
1139     AWT_CHECK_HAVE_LOCK();
1140     XFree(jlong_to_ptr(ptr));
1141 }
1142 
1143 /*
1144  * Class:     sun_awt_X11_XlibWrapper
1145  * Method:    XFree
1146  * Signature: (J)V
1147  */
1148 JNIEXPORT jbyteArray JNICALL Java_sun_awt_X11_XlibWrapper_getStringBytes
1149 (JNIEnv *env, jclass clazz, jlong str_ptr)
1150 {
1151     unsigned char * str = (unsigned char*) jlong_to_ptr(str_ptr);
1152     long length = strlen((char*)str);
1153     jbyteArray res = (*env)->NewByteArray(env, length);
1154     void * storage = malloc(length+1);
1155     memcpy(storage, str, length+1);
1156     (*env)->SetByteArrayRegion(env, res, 0, length,
1157                    (const signed char*) storage);
1158     return res;
1159 }
1160 
1161 
1162 /*
1163  * Class:     sun_awt_X11_XlibWrapper
1164  * Method:    ServerVendor
1165  * Signature: (J)Ljava/lang/String;
1166  */
1167 JNIEXPORT jstring JNICALL Java_sun_awt_X11_XlibWrapper_ServerVendor
1168 (JNIEnv *env, jclass clazz, jlong display)
1169 {
1170     AWT_CHECK_HAVE_LOCK();
1171     return JNU_NewStringPlatform(env, ServerVendor((Display*)jlong_to_ptr(display)));
1172 }
1173 /*
1174  * Class:     sun_awt_X11_XlibWrapper
1175  * Method:    VendorRelease
1176  * Signature: (J)I;
1177  */
1178 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_VendorRelease
1179 (JNIEnv *env, jclass clazz, jlong display)
1180 {
1181     AWT_CHECK_HAVE_LOCK();
1182     return VendorRelease((Display*)jlong_to_ptr(display));
1183 }
1184 
1185 JavaVM* jvm = NULL;
1186 static int ToolkitErrorHandler(Display * dpy, XErrorEvent * event) {
1187     if (jvm != NULL) {
1188         JNIEnv * env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
1189         return JNU_CallStaticMethodByName(env, NULL, "sun/awt/X11/XToolkit", "globalErrorHandler", "(JJ)I",
1190                                           ptr_to_jlong(dpy), ptr_to_jlong(event)).i;
1191     } else {
1192         return 0;
1193     }
1194 }
1195 
1196 /*
1197  * Class:     sun_awt_X11_XlibWrapper
1198  * Method:    SetToolkitErrorHandler
1199  * Signature: ()J
1200  */
1201 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_SetToolkitErrorHandler
1202 (JNIEnv *env, jclass clazz)
1203 {
1204     (*env)->GetJavaVM(env, &jvm);
1205     AWT_CHECK_HAVE_LOCK();
1206     return ptr_to_jlong(XSetErrorHandler(ToolkitErrorHandler));
1207 }
1208 
1209 /*
1210  * Class:     sun_awt_X11_XlibWrapper
1211  * Method:    XSetErrorHandler
1212  * Signature: (J)V
1213  */
1214 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetErrorHandler
1215 (JNIEnv *env, jclass clazz, jlong handler)
1216 {
1217     AWT_CHECK_HAVE_LOCK();
1218     XSetErrorHandler((XErrorHandler) jlong_to_ptr(handler));
1219 }
1220 
1221 /*
1222  * Class:     sun_awt_X11_XlibWrapper
1223  * Method:    CallErrorHandler
1224  * Signature: (JJJ)I
1225  */
1226 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_CallErrorHandler
1227 (JNIEnv *env, jclass clazz, jlong handler, jlong display, jlong event_ptr)
1228 {
1229     return (*(XErrorHandler)jlong_to_ptr(handler))((Display*) jlong_to_ptr(display), (XErrorEvent*) jlong_to_ptr(event_ptr));
1230 }
1231 
1232 /*
1233  * Class:     sun_awt_X11_XlibWrapper
1234  * Method:    PrintXErrorEvent
1235  * Signature: (JJ)V
1236  */
1237 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_PrintXErrorEvent
1238 (JNIEnv *env, jclass clazz, jlong display, jlong event_ptr)
1239 {
1240     char msg[128];
1241     char buf[128];
1242 
1243     XErrorEvent* err = (XErrorEvent *)jlong_to_ptr(event_ptr);
1244 
1245     XGetErrorText((Display *)jlong_to_ptr(display), err->error_code, msg, sizeof(msg));
1246     jio_fprintf(stderr, "Xerror %s, XID %x, ser# %d\n", msg, err->resourceid, err->serial);
1247     jio_snprintf(buf, sizeof(buf), "%d", err->request_code);
1248     XGetErrorDatabaseText((Display *)jlong_to_ptr(display), "XRequest", buf, "Unknown", msg, sizeof(msg));
1249     jio_fprintf(stderr, "Major opcode %d (%s)\n", err->request_code, msg);
1250     if (err->request_code > 128) {
1251         jio_fprintf(stderr, "Minor opcode %d\n", err->minor_code);
1252     }
1253 }
1254 
1255 
1256 /*
1257  * Class:     sun_awt_X11_XlibWrapper
1258  * Method:    XInternAtoms
1259  * Signature: (J[Ljava/lang/String;ZJ)I
1260  */
1261 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XInternAtoms
1262 (JNIEnv *env, jclass clazz, jlong display, jobjectArray names_arr, jboolean only_if_exists, jlong atoms)
1263 {
1264 
1265     int length = (*env)->GetArrayLength(env, names_arr);
1266     char ** names = (char**)malloc(length*sizeof(char*));
1267     jboolean copy;
1268     int index, name_index = 0;
1269     int status;
1270     for (index = 0; index < length; index++) {
1271         jstring str = (*env)->GetObjectArrayElement(env, names_arr, index);
1272         if (!JNU_IsNull(env, str)) {
1273             const char * str_char = JNU_GetStringPlatformChars(env, str, NULL);
1274             names[name_index++] = strdup(str_char);
1275             JNU_ReleaseStringPlatformChars(env, str, str_char);
1276             (*env)->DeleteLocalRef(env, str);
1277         }
1278     }
1279     AWT_CHECK_HAVE_LOCK();
1280     status = XInternAtoms((Display*)jlong_to_ptr(display), names, name_index, only_if_exists, (Atom*) jlong_to_ptr(atoms));
1281     for (index = 0; index < length; index++) {
1282         free(names[index]);
1283     }
1284     free(names);
1285     return status;
1286 }
1287 
1288 
1289 
1290 /*
1291  * Class:     sun_awt_X11_XlibWrapper
1292  * Method:    XGetWindowAttributes
1293  * Signature: (JJJ)I
1294  */
1295 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGetWindowAttributes
1296 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong attr_ptr)
1297 {
1298     jint status;
1299     AWT_CHECK_HAVE_LOCK();
1300     memset((XWindowAttributes*) jlong_to_ptr(attr_ptr), 0, sizeof(XWindowAttributes));
1301     status =  XGetWindowAttributes((Display*)jlong_to_ptr(display), window, (XWindowAttributes*) jlong_to_ptr(attr_ptr));
1302     return status;
1303 }
1304 
1305 
1306 /*
1307  * Class:     sun_awt_X11_XlibWrapper
1308  * Method:    XGetGeometry
1309  * Signature: (JJJJJJJJJ)I
1310  */
1311 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGetGeometry
1312 (JNIEnv *env, jclass clazz, jlong display, jlong drawable, jlong root_return,
1313      jlong x_return, jlong y_return, jlong width_return, jlong height_return,
1314      jlong border_width_return, jlong depth_return)
1315 {
1316     jint status;
1317     AWT_CHECK_HAVE_LOCK();
1318     status = XGetGeometry((Display *)jlong_to_ptr(display),
1319                           (Drawable)drawable, (Window *)jlong_to_ptr(root_return),
1320                           (int *)jlong_to_ptr(x_return), (int *)jlong_to_ptr(y_return),
1321                           (unsigned int *)jlong_to_ptr(width_return), (unsigned int *)jlong_to_ptr(height_return),
1322                           (unsigned int *)jlong_to_ptr(border_width_return),
1323                           (unsigned int *)jlong_to_ptr(depth_return));
1324     return status;
1325 }
1326 
1327 
1328 /*
1329  * Class:     sun_awt_X11_XlibWrapper
1330  * Method:    XGetWMNormalHints
1331  * Signature: (JJJJ)I
1332  */
1333 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XGetWMNormalHints
1334 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong hints, jlong supplied_return)
1335 {
1336     AWT_CHECK_HAVE_LOCK();
1337     return XGetWMNormalHints((Display*) jlong_to_ptr(display),
1338                              window,
1339                              (XSizeHints*) jlong_to_ptr(hints),
1340                              (long*) jlong_to_ptr(supplied_return));
1341 }
1342 
1343 /*
1344  * Class:     sun_awt_X11_XlibWrapper
1345  * Method:    XSetWMNormalHints
1346  * Signature: (JJJ)V
1347  */
1348 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetWMNormalHints
1349 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong hints)
1350 {
1351     AWT_CHECK_HAVE_LOCK();
1352     XSetWMNormalHints((Display*) jlong_to_ptr(display), window, (XSizeHints*) jlong_to_ptr(hints));
1353 }
1354 
1355 /*
1356  * Class:     sun_awt_X11_XlibWrapper
1357  * Method:    XDeleteProperty
1358  * Signature: (JJJ)V
1359  */
1360 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XDeleteProperty
1361 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong atom)
1362 {
1363     AWT_CHECK_HAVE_LOCK();
1364     XDeleteProperty((Display*) jlong_to_ptr(display), window, (Atom)atom);
1365 }
1366 
1367 /*
1368  * Class:     sun_awt_X11_XlibWrapper
1369  * Method:    XSendEvent
1370  * Signature: (JJZJJ)V
1371  */
1372 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XSendEvent
1373 (JNIEnv *env, jclass clazz, jlong display, jlong window, jboolean propagate, jlong event_mask, jlong event)
1374 {
1375     AWT_CHECK_HAVE_LOCK();
1376     return XSendEvent((Display*) jlong_to_ptr(display),
1377                       window,
1378                       propagate==JNI_TRUE?True:False,
1379                       (long) event_mask,
1380                       (XEvent*) jlong_to_ptr(event));
1381 }
1382 
1383 
1384 /*
1385  * Class:     sun_awt_X11_XlibWrapper
1386  * Method:    XQueryTree
1387  * Signature: (JJJJJJ)I
1388  */
1389 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XQueryTree
1390 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong root_return, jlong parent_return, jlong children_return, jlong nchildren_return)
1391 {
1392     AWT_CHECK_HAVE_LOCK();
1393     return XQueryTree((Display*) jlong_to_ptr(display),
1394                       window,
1395                       (Window *) jlong_to_ptr(root_return),
1396                       (Window*) jlong_to_ptr(parent_return),
1397                       (Window**) jlong_to_ptr(children_return),
1398                       (unsigned int*) jlong_to_ptr(nchildren_return));
1399 }
1400 
1401 
1402 /*
1403  * Class:     sun_awt_X11_XlibWrapper
1404  * Method:    memcpy
1405  * Signature: (JJJ)V
1406  */
1407 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_memcpy
1408 (JNIEnv *env, jclass clazz, jlong dest_ptr, jlong src_ptr, jlong length)
1409 {
1410     memcpy(jlong_to_ptr(dest_ptr), jlong_to_ptr(src_ptr), length);
1411 }
1412 
1413 
1414 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XSetMinMaxHints
1415 (JNIEnv *env, jclass clazz, jlong display, jlong window, jint x, jint y, jint width, jint height, jlong flags) {
1416     XSizeHints * hints;
1417     AWT_CHECK_HAVE_LOCK();
1418     hints = XAllocSizeHints();
1419     hints->flags = flags;
1420     hints->width = width;
1421     hints->min_width = width;
1422     hints->max_width = width;
1423     hints->height = height;
1424     hints->min_height = height;
1425     hints->max_height = height;
1426     hints->x = x;
1427     hints->y = y;
1428     XSetWMNormalHints((Display*) jlong_to_ptr(display), window, hints);
1429     XFree(hints);
1430 }
1431 
1432 
1433 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XGetVisualInfo
1434 (JNIEnv *env, jclass clazz, jlong display, jlong vinfo_mask, jlong vinfo_template,
1435  jlong nitems_return)
1436 {
1437     AWT_CHECK_HAVE_LOCK();
1438     return ptr_to_jlong(XGetVisualInfo((Display*) jlong_to_ptr(display),
1439                                        (long) vinfo_mask,
1440                                        (XVisualInfo*) jlong_to_ptr(vinfo_template),
1441                                        (int*) jlong_to_ptr(nitems_return)));
1442 }
1443 
1444 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XAllocSizeHints
1445   (JNIEnv *env, jclass clazz)
1446 {
1447     AWT_CHECK_HAVE_LOCK();
1448     return ptr_to_jlong(XAllocSizeHints());
1449 }
1450 
1451 /*
1452  * Class:     sun_awt_X11_XlibWrapper
1453  * Method:    XIconifyWindow
1454  * Signature: (JJJ)V
1455  */
1456 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XBell
1457 (JNIEnv *env, jclass clazz, jlong display, jint percent)
1458 {
1459     AWT_CHECK_HAVE_LOCK();
1460     XBell((Display*)jlong_to_ptr(display), percent);
1461 }
1462 
1463 
1464 /*
1465  * Class:     sun_awt_X11_XlibWrapper
1466  * Method:    XAllocColor
1467  * Signature: (JJJ)Z
1468  */
1469 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XAllocColor
1470 (JNIEnv *env, jclass clazz, jlong display , jlong colormap, jlong xcolor) {
1471 
1472     Status status;
1473     AWT_CHECK_HAVE_LOCK();
1474     status = XAllocColor((Display *) jlong_to_ptr(display), (Colormap) colormap, (XColor *) jlong_to_ptr(xcolor));
1475 
1476     if (status == 0) return JNI_FALSE;
1477     else return JNI_TRUE;
1478 }
1479 
1480 
1481 /*
1482  * Class:     sun_awt_X11_XlibWrapper
1483  * Method:    XCreateBitmapFromData
1484  * Signature: (JJJII)J
1485  */
1486 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XCreateBitmapFromData
1487 (JNIEnv *env, jclass clazz, jlong display, jlong drawable, jlong data, jint width, jint height) {
1488     AWT_CHECK_HAVE_LOCK();
1489 
1490     return (jlong) XCreateBitmapFromData((Display *) jlong_to_ptr(display), (Drawable) drawable,
1491                                          (char *) jlong_to_ptr(data), width, height);
1492 }
1493 
1494 
1495 /*
1496  * Class:     sun_awt_X11_XlibWrapper
1497  * Method:    XFreePixmap
1498  * Signature: (JJ)V
1499  */
1500 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XFreePixmap
1501 (JNIEnv *env, jclass clazz, jlong display, jlong pixmap) {
1502     AWT_CHECK_HAVE_LOCK();
1503     XFreePixmap((Display *)jlong_to_ptr(display), (Pixmap) pixmap);
1504 }
1505 
1506 /*
1507  * Class:     sun_awt_X11_XlibWrapper
1508  * Method:    XReparentWindow
1509  * Signature: (JJJII)V
1510  */
1511 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XReparentWindow
1512 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong parent, jint x, jint y) {
1513     AWT_CHECK_HAVE_LOCK();
1514     XReparentWindow((Display*)jlong_to_ptr(display), window, parent, x, y);
1515 }
1516 
1517 /*
1518  * Class:     sun_awt_X11_XlibWrapper
1519  * Method:    XConvertSelection
1520  * Signature: (JJJJJJ)V
1521  */
1522 JNIEXPORT void JNICALL
1523 Java_sun_awt_X11_XlibWrapper_XConvertSelection(JNIEnv *env, jclass clazz,
1524                            jlong display, jlong selection,
1525                            jlong target, jlong property,
1526                            jlong requestor, jlong time) {
1527     AWT_CHECK_HAVE_LOCK();
1528     XConvertSelection((Display*)jlong_to_ptr(display), selection, target, property, requestor,
1529               time);
1530 }
1531 
1532 /*
1533  * Class:     sun_awt_X11_XlibWrapper
1534  * Method:    XSetSelectionOwner
1535  * Signature: (JJJJ)V
1536  */
1537 JNIEXPORT void JNICALL
1538 Java_sun_awt_X11_XlibWrapper_XSetSelectionOwner(JNIEnv *env, jclass clazz,
1539                         jlong display, jlong selection,
1540                         jlong owner, jlong time) {
1541     AWT_CHECK_HAVE_LOCK();
1542     XSetSelectionOwner((Display*)jlong_to_ptr(display), selection, owner, time);
1543 }
1544 
1545 /*
1546  * Class:     sun_awt_X11_XlibWrapper
1547  * Method:    XGetSelectionOwner
1548  * Signature: (JJ)J
1549  */
1550 JNIEXPORT jlong JNICALL
1551 Java_sun_awt_X11_XlibWrapper_XGetSelectionOwner(JNIEnv *env, jclass clazz,
1552                         jlong display, jlong selection) {
1553     AWT_CHECK_HAVE_LOCK();
1554     return (jlong)XGetSelectionOwner((Display*)jlong_to_ptr(display), selection);
1555 }
1556 
1557 /*
1558  * Class:     sun_awt_X11_XlibWrapper
1559  * Method:    XGetAtomName
1560  * Signature: (JJ)Ljava/lang/String;
1561  */
1562 JNIEXPORT jstring JNICALL
1563 Java_sun_awt_X11_XlibWrapper_XGetAtomName(JNIEnv *env, jclass clazz,
1564                       jlong display, jlong atom)
1565 {
1566     jstring string = NULL;
1567     char* name;
1568     AWT_CHECK_HAVE_LOCK();
1569     name = (char*) XGetAtomName((Display*)jlong_to_ptr(display), atom);
1570 
1571     if (name == NULL) {
1572         fprintf(stderr, "Atom was %d\n", (int)atom);
1573         JNU_ThrowNullPointerException(env, "Failed to retrieve atom name.");
1574         return NULL;
1575     }
1576 
1577     string = (*env)->NewStringUTF(env, (const char *)name);
1578 
1579     XFree(name);
1580 
1581     return string;
1582 }
1583 
1584 /*
1585  * Class:     sun_awt_X11_XlibWrapper
1586  * Method:    XMaxRequestSize
1587  * Signature: (J)J
1588  */
1589 JNIEXPORT jlong JNICALL
1590 Java_sun_awt_X11_XlibWrapper_XMaxRequestSize(JNIEnv *env, jclass clazz,
1591                          jlong display) {
1592     AWT_CHECK_HAVE_LOCK();
1593     return XMaxRequestSize((Display*) jlong_to_ptr(display));
1594 }
1595 
1596 JNIEXPORT jlong JNICALL
1597 Java_sun_awt_X11_XlibWrapper_XAllocWMHints(JNIEnv *env, jclass clazz)
1598 {
1599     AWT_CHECK_HAVE_LOCK();
1600     return ptr_to_jlong(XAllocWMHints());
1601 }
1602 
1603 JNIEXPORT jlong JNICALL
1604 Java_sun_awt_X11_XlibWrapper_XCreatePixmap(JNIEnv *env, jclass clazz, jlong display, jlong drawable, jint width, jint height, jint depth)
1605 {
1606     AWT_CHECK_HAVE_LOCK();
1607     return XCreatePixmap((Display*)jlong_to_ptr(display), (Drawable)drawable, width, height, depth);
1608 }
1609 JNIEXPORT jlong JNICALL
1610 Java_sun_awt_X11_XlibWrapper_XCreateImage
1611   (JNIEnv *env, jclass clazz, jlong display, jlong visual_ptr,
1612    jint depth, jint format, jint offset, jlong data, jint width,
1613    jint height, jint bitmap_pad, jint bytes_per_line)
1614 {
1615     AWT_CHECK_HAVE_LOCK();
1616     return ptr_to_jlong(XCreateImage((Display*) jlong_to_ptr(display), (Visual*) jlong_to_ptr(visual_ptr),
1617                 depth, format, offset, (char*) jlong_to_ptr(data),
1618                 width, height, bitmap_pad, bytes_per_line));
1619 }
1620 JNIEXPORT jlong JNICALL
1621 Java_sun_awt_X11_XlibWrapper_XCreateGC
1622   (JNIEnv *env, jclass clazz, jlong display, jlong drawable,
1623    jlong valuemask, jlong values)
1624 {
1625     AWT_CHECK_HAVE_LOCK();
1626     return ptr_to_jlong(XCreateGC((Display*) jlong_to_ptr(display), (Drawable)drawable, valuemask, (XGCValues*) jlong_to_ptr(values)));
1627 }
1628 
1629 JNIEXPORT void JNICALL
1630 Java_sun_awt_X11_XlibWrapper_XDestroyImage(JNIEnv *env, jclass clazz, jlong image)
1631 {
1632     XImage *img = (XImage*) jlong_to_ptr(image);
1633     AWT_CHECK_HAVE_LOCK();
1634 
1635     // Fix for bug 4903671 :
1636     // We should be careful to not double free the memory pointed to data
1637     // Since we use unsafe to allocate it, we should use unsafe to free it.
1638     // So we should NULL the data pointer before calling XDestroyImage so
1639     // that X does not free the pointer for us.
1640     img->data = NULL;
1641     XDestroyImage(img);
1642 }
1643 JNIEXPORT void JNICALL
1644 Java_sun_awt_X11_XlibWrapper_XPutImage(JNIEnv *env, jclass clazz, jlong display, jlong drawable, jlong gc, jlong image, jint src_x, jint src_y, jint dest_x, jint dest_y, jint width, jint height)
1645 {
1646     AWT_CHECK_HAVE_LOCK();
1647     XPutImage((Display*)jlong_to_ptr(display), (Drawable)drawable, (GC) jlong_to_ptr(gc), (XImage*) jlong_to_ptr(image), src_x, src_y,
1648               dest_x, dest_y, width, height);
1649 }
1650 JNIEXPORT void JNICALL
1651 Java_sun_awt_X11_XlibWrapper_XFreeGC(JNIEnv *env, jclass clazz, jlong display, jlong gc)
1652 {
1653     AWT_CHECK_HAVE_LOCK();
1654     XFreeGC((Display*) jlong_to_ptr(display), (GC) jlong_to_ptr(gc));
1655 }
1656 JNIEXPORT void JNICALL
1657 Java_sun_awt_X11_XlibWrapper_XSetWindowBackgroundPixmap(JNIEnv *env, jclass clazz, jlong display, jlong window, jlong pixmap)
1658 {
1659     AWT_CHECK_HAVE_LOCK();
1660     XSetWindowBackgroundPixmap((Display*) jlong_to_ptr(display), (Window)window, (Pixmap)pixmap);
1661 }
1662 JNIEXPORT void JNICALL
1663 Java_sun_awt_X11_XlibWrapper_XClearWindow(JNIEnv *env, jclass clazz, jlong display, jlong window)
1664 {
1665     AWT_CHECK_HAVE_LOCK();
1666     XClearWindow((Display*) jlong_to_ptr(display), (Window)window);
1667 }
1668 
1669 JNIEXPORT jint JNICALL
1670 Java_sun_awt_X11_XlibWrapper_XGetIconSizes(JNIEnv *env, jclass clazz, jlong display, jlong window, jlong ret_sizes, jlong ret_count)
1671 {
1672     XIconSize** psize = (XIconSize**) jlong_to_ptr(ret_sizes);
1673     int * pcount = (int *) jlong_to_ptr(ret_count);
1674     Status res;
1675     AWT_CHECK_HAVE_LOCK();
1676     res = XGetIconSizes((Display*) jlong_to_ptr(display), (Window)window, psize, pcount);
1677     return res;
1678 }
1679 
1680 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XdbeQueryExtension
1681   (JNIEnv *env, jclass clazz, jlong display, jlong major_version_return,
1682    jlong minor_version_return)
1683 {
1684     AWT_CHECK_HAVE_LOCK();
1685     return XdbeQueryExtension((Display*) jlong_to_ptr(display), (int *) jlong_to_ptr(major_version_return),
1686                   (int *) jlong_to_ptr(minor_version_return));
1687 }
1688 
1689 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_XQueryExtension
1690   (JNIEnv *env, jclass clazz, jlong display, jstring jstr, jlong mop_return,
1691    jlong feve_return, jlong err_return)
1692 {
1693     char *cname;
1694     Boolean bu;
1695     if (!JNU_IsNull(env, jstr)) {
1696         cname = (char *)JNU_GetStringPlatformChars(env, jstr, NULL);
1697     } else {
1698         cname = "";
1699     }
1700 
1701     AWT_CHECK_HAVE_LOCK();
1702     bu = XQueryExtension((Display*) jlong_to_ptr(display), cname, (int *) jlong_to_ptr(mop_return),
1703                 (int *) jlong_to_ptr(feve_return),  (int *) jlong_to_ptr(err_return));
1704     if (!JNU_IsNull(env, jstr)) {
1705         JNU_ReleaseStringPlatformChars(env, jstr, (const char *) cname);
1706     }
1707     return bu ? JNI_TRUE : JNI_FALSE;
1708 }
1709 
1710 JNIEXPORT jboolean JNICALL Java_sun_awt_X11_XlibWrapper_IsKeypadKey
1711   (JNIEnv *env, jclass clazz, jlong keysym)
1712 {
1713     AWT_CHECK_HAVE_LOCK();
1714     if(IsKeypadKey(keysym)) {
1715         return JNI_TRUE;
1716     }
1717     return JNI_FALSE;
1718 }
1719 
1720 JNIEXPORT jlong JNICALL Java_sun_awt_X11_XlibWrapper_XdbeAllocateBackBufferName
1721   (JNIEnv *env, jclass clazz, jlong display, jlong window, jint swap_action)
1722 {
1723     AWT_CHECK_HAVE_LOCK();
1724     return XdbeAllocateBackBufferName((Display*) jlong_to_ptr(display), (Window) window,
1725                       (XdbeSwapAction) swap_action);
1726 }
1727 
1728 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XdbeDeallocateBackBufferName
1729   (JNIEnv *env, jclass clazz, jlong display, jlong buffer)
1730 {
1731     AWT_CHECK_HAVE_LOCK();
1732     return XdbeDeallocateBackBufferName((Display*) jlong_to_ptr(display), (XdbeBackBuffer) buffer);
1733 }
1734 
1735 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XdbeBeginIdiom
1736   (JNIEnv *env, jclass clazz, jlong display)
1737 {
1738     AWT_CHECK_HAVE_LOCK();
1739     return XdbeBeginIdiom((Display*) jlong_to_ptr(display));
1740 }
1741 
1742 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XdbeEndIdiom
1743   (JNIEnv *env, jclass clazz, jlong display)
1744 {
1745     AWT_CHECK_HAVE_LOCK();
1746     return XdbeEndIdiom((Display*) jlong_to_ptr(display));
1747 }
1748 
1749 JNIEXPORT jint JNICALL Java_sun_awt_X11_XlibWrapper_XdbeSwapBuffers
1750   (JNIEnv *env, jclass clazz, jlong display, jlong swap_info, jint num_windows)
1751 {
1752     AWT_CHECK_HAVE_LOCK();
1753     return XdbeSwapBuffers((Display*) jlong_to_ptr(display), (XdbeSwapInfo *) jlong_to_ptr(swap_info), num_windows);
1754 }
1755 JNIEXPORT void JNICALL Java_sun_awt_X11_XlibWrapper_XQueryKeymap
1756 (JNIEnv *env, jclass clazz, jlong display, jlong vector)
1757 {
1758 
1759     AWT_CHECK_HAVE_LOCK();
1760     XQueryKeymap( (Display *) jlong_to_ptr(display), (char *) jlong_to_ptr(vector));
1761 }
1762 
1763 JNIEXPORT jlong JNICALL
1764 Java_sun_awt_X11_XlibWrapper_XKeycodeToKeysym(JNIEnv *env, jclass clazz,
1765                                               jlong display, jint keycode,
1766                                               jint index) {
1767     AWT_CHECK_HAVE_LOCK();
1768     return XKeycodeToKeysym((Display*) jlong_to_ptr(display), (unsigned int)keycode, (int)index);
1769 }
1770 
1771 JNIEXPORT jint JNICALL
1772 Java_sun_awt_X11_XlibWrapper_XkbGetEffectiveGroup(JNIEnv *env, jclass clazz,
1773                                               jlong display) {
1774     XkbStateRec sr;
1775     AWT_CHECK_HAVE_LOCK();
1776     memset(&sr, 0, sizeof(XkbStateRec));
1777     XkbGetState((Display*) jlong_to_ptr(display), XkbUseCoreKbd, &sr);
1778 //    printf("-------------------------------------VVVV\n");
1779 //    printf("                 group:0x%0X\n",sr.group);
1780 //    printf("            base_group:0x%0X\n",sr.base_group);
1781 //    printf("         latched_group:0x%0X\n",sr.latched_group);
1782 //    printf("          locked_group:0x%0X\n",sr.locked_group);
1783 //    printf("                  mods:0x%0X\n",sr.mods);
1784 //    printf("             base_mods:0x%0X\n",sr.base_mods);
1785 //    printf("          latched_mods:0x%0X\n",sr.latched_mods);
1786 //    printf("           locked_mods:0x%0X\n",sr.locked_mods);
1787 //    printf("          compat_state:0x%0X\n",sr.compat_state);
1788 //    printf("             grab_mods:0x%0X\n",sr.grab_mods);
1789 //    printf("      compat_grab_mods:0x%0X\n",sr.compat_grab_mods);
1790 //    printf("           lookup_mods:0x%0X\n",sr.lookup_mods);
1791 //    printf("    compat_lookup_mods:0x%0X\n",sr.compat_lookup_mods);
1792 //    printf("           ptr_buttons:0x%0X\n",sr.ptr_buttons);
1793 //    printf("-------------------------------------^^^^\n");
1794     return (jint)(sr.group);
1795 }
1796 JNIEXPORT jlong JNICALL
1797 Java_sun_awt_X11_XlibWrapper_XkbKeycodeToKeysym(JNIEnv *env, jclass clazz,
1798                                               jlong display, jint keycode,
1799                                               jint group, jint level) {
1800     AWT_CHECK_HAVE_LOCK();
1801     return XkbKeycodeToKeysym((Display*) jlong_to_ptr(display), (unsigned int)keycode, (unsigned int)group, (unsigned int)level);
1802 }
1803 
1804 JNIEXPORT jint JNICALL
1805 Java_sun_awt_X11_XlibWrapper_XKeysymToKeycode(JNIEnv *env, jclass clazz,
1806                                               jlong display, jlong keysym) {
1807     AWT_CHECK_HAVE_LOCK();
1808     return XKeysymToKeycode((Display*) jlong_to_ptr(display), (KeySym)keysym);
1809 }
1810 
1811 JNIEXPORT jlong JNICALL
1812 Java_sun_awt_X11_XlibWrapper_XGetModifierMapping(JNIEnv *env, jclass clazz,
1813                                               jlong display) {
1814     AWT_CHECK_HAVE_LOCK();
1815     return ptr_to_jlong(XGetModifierMapping((Display*) jlong_to_ptr(display)));
1816 }
1817 
1818 JNIEXPORT void JNICALL
1819 Java_sun_awt_X11_XlibWrapper_XFreeModifiermap(JNIEnv *env, jclass clazz,
1820                                               jlong keymap) {
1821     AWT_CHECK_HAVE_LOCK();
1822     XFreeModifiermap((XModifierKeymap*) jlong_to_ptr(keymap));
1823 }
1824 
1825 JNIEXPORT void JNICALL
1826 Java_sun_awt_X11_XlibWrapper_XChangeActivePointerGrab(JNIEnv *env, jclass clazz,
1827                                                       jlong display, jint mask,
1828                                                       jlong cursor, jlong time) {
1829     AWT_CHECK_HAVE_LOCK();
1830     XChangeActivePointerGrab((Display*)jlong_to_ptr(display), (unsigned int)mask,
1831                              (Cursor)cursor, (Time)time);
1832 }
1833 
1834 /******************* Secondary loop support ************************************/
1835 #define AWT_SECONDARY_LOOP_TIMEOUT 250
1836 
1837 static Bool exitSecondaryLoop = True;
1838 
1839 /*
1840  * This predicate procedure allows the Toolkit thread to process specific events
1841  * while it is blocked waiting for the event dispatch thread to process
1842  * a SunDropTargetEvent. We need this to prevent deadlock when the client code
1843  * processing SunDropTargetEvent sets or gets the contents of the system
1844  * clipboard/selection. In this case the event dispatch thread waits for the
1845  * Toolkit thread to process PropertyNotify or SelectionNotify events.
1846  */
1847 static Bool
1848 secondary_loop_event(Display* dpy, XEvent* event, char* arg) {
1849     return (event->type == SelectionNotify ||
1850             event->type == SelectionClear  ||
1851             event->type == PropertyNotify) ? True : False;
1852 }
1853 
1854 
1855 JNIEXPORT jboolean JNICALL
1856 Java_sun_awt_X11_XlibWrapper_XNextSecondaryLoopEvent(JNIEnv *env, jclass clazz,
1857                                                      jlong display, jlong ptr) {
1858     AWT_CHECK_HAVE_LOCK();
1859     exitSecondaryLoop = False;
1860     while (!exitSecondaryLoop) {
1861         if (XCheckIfEvent((Display*) jlong_to_ptr(display), (XEvent*) jlong_to_ptr(ptr), secondary_loop_event, NULL)) {
1862             return JNI_TRUE;
1863         }
1864         AWT_WAIT(AWT_SECONDARY_LOOP_TIMEOUT);
1865     }
1866     return JNI_FALSE;
1867 }
1868 
1869 JNIEXPORT void JNICALL
1870 Java_sun_awt_X11_XlibWrapper_ExitSecondaryLoop(JNIEnv *env, jclass clazz) {
1871     DASSERT(!exitSecondaryLoop);
1872     AWT_CHECK_HAVE_LOCK();
1873     exitSecondaryLoop = True;
1874     AWT_NOTIFY_ALL();
1875 }
1876 /*******************************************************************************/
1877 
1878 JNIEXPORT jobjectArray JNICALL
1879 Java_sun_awt_X11_XlibWrapper_XTextPropertyToStringList(JNIEnv *env,
1880                                                        jclass clazz,
1881                                                        jbyteArray bytes,
1882                                                        jlong encodingAtom) {
1883     XTextProperty tp;
1884     jbyte         *value;
1885 
1886     char**        strings  = (char **)NULL;
1887     int32_t       nstrings = 0;
1888     jobjectArray  ret = NULL;
1889     int32_t       i;
1890     jsize         len;
1891     jboolean      isCopy = JNI_FALSE;
1892     static jclass stringClass = NULL;
1893     jclass        stringClassLocal = NULL;
1894 
1895     AWT_CHECK_HAVE_LOCK();
1896 
1897     if (JNU_IsNull(env, stringClass)) {
1898         stringClassLocal = (*env)->FindClass(env, "java/lang/String");
1899 
1900         if ((*env)->ExceptionCheck(env)) {
1901             (*env)->ExceptionDescribe(env);
1902             (*env)->ExceptionClear(env);
1903             DASSERT(False);
1904         }
1905 
1906         if (JNU_IsNull(env, stringClassLocal)) {
1907             return NULL;
1908         }
1909 
1910         stringClass = (*env)->NewGlobalRef(env, stringClassLocal); /* never freed! */
1911         (*env)->DeleteLocalRef(env, stringClassLocal);
1912 
1913         if (JNU_IsNull(env, stringClass)) {
1914             JNU_ThrowOutOfMemoryError(env, "");
1915             return NULL;
1916         }
1917     }
1918 
1919     /*
1920      * If the length of the byte array is 0 just return a null
1921      */
1922     len = (*env)->GetArrayLength(env, bytes);
1923     if (len == 0) {
1924         return (*env)->NewObjectArray(env, 0, stringClass, NULL);
1925     }
1926 
1927     value = (*env)->GetByteArrayElements(env, bytes, &isCopy);
1928     if (JNU_IsNull(env, value)) {
1929         return NULL;
1930     }
1931 
1932     tp.encoding = encodingAtom;
1933     tp.value    = (unsigned char *)value;
1934     tp.nitems   = len;
1935     tp.format   = 8;
1936 
1937     /*
1938      * Convert the byte stream into a list of X11 strings
1939      */
1940     if (XTextPropertyToStringList(&tp, &strings, &nstrings) == 0) {
1941         (*env)->ReleaseByteArrayElements(env, bytes, value, JNI_ABORT);
1942         return NULL;
1943     }
1944 
1945     (*env)->ReleaseByteArrayElements(env, bytes, value, JNI_ABORT);
1946 
1947     if (nstrings == 0) {
1948         return (*env)->NewObjectArray(env, 0, stringClass, NULL);
1949     }
1950 
1951     ret = (*env)->NewObjectArray(env, nstrings, stringClass, NULL);
1952 
1953     if ((*env)->ExceptionCheck(env)) {
1954         (*env)->ExceptionDescribe(env);
1955         (*env)->ExceptionClear(env);
1956         goto wayout;
1957     }
1958 
1959     if (JNU_IsNull(env, ret)) {
1960         goto wayout;
1961     }
1962 
1963     for (i = 0; i < nstrings; i++) {
1964         jstring string = (*env)->NewStringUTF(env,
1965                                               (const char *)strings[i]);
1966         if ((*env)->ExceptionCheck(env)) {
1967             (*env)->ExceptionDescribe(env);
1968             (*env)->ExceptionClear(env);
1969             goto wayout;
1970         }
1971 
1972         if (JNU_IsNull(env, string)) {
1973             goto wayout;
1974         }
1975 
1976         (*env)->SetObjectArrayElement(env, ret, i, string);
1977 
1978         if ((*env)->ExceptionCheck(env)) {
1979             (*env)->ExceptionDescribe(env);
1980             (*env)->ExceptionClear(env);
1981             goto wayout;
1982         }
1983 
1984         (*env)->DeleteLocalRef(env, string);
1985     }
1986 
1987  wayout:
1988     /*
1989      * Clean up and return
1990      */
1991     XFreeStringList(strings);
1992     return ret;
1993 }
1994 
1995 
1996 JNIEXPORT void JNICALL
1997 Java_sun_awt_X11_XlibWrapper_XPutBackEvent(JNIEnv *env,
1998                                            jclass clazz,
1999                                            jlong display,
2000                                            jlong event) {
2001     XPutBackEvent((Display*)jlong_to_ptr(display), (XEvent*) jlong_to_ptr(event));
2002 }
2003 
2004 JNIEXPORT jlong JNICALL
2005 Java_sun_awt_X11_XlibWrapper_getAddress(JNIEnv *env,
2006                                            jclass clazz,
2007                                            jobject o) {
2008     return ptr_to_jlong(o);
2009 }
2010 
2011 JNIEXPORT void JNICALL
2012 Java_sun_awt_X11_XlibWrapper_copyIntArray(JNIEnv *env,
2013                                            jclass clazz,
2014                                            jlong dest, jobject array, jint size) {
2015     jboolean isCopy = JNI_FALSE;
2016     jint * ints = (*env)->GetIntArrayElements(env, array, &isCopy);
2017     memcpy(jlong_to_ptr(dest), ints, size);
2018     if (isCopy) {
2019         (*env)->ReleaseIntArrayElements(env, array, ints, JNI_ABORT);
2020     }
2021 }
2022 
2023 JNIEXPORT void JNICALL
2024 Java_sun_awt_X11_XlibWrapper_copyLongArray(JNIEnv *env,
2025                                            jclass clazz,
2026                                            jlong dest, jobject array, jint size) {
2027     jboolean isCopy = JNI_FALSE;
2028     jlong * longs = (*env)->GetLongArrayElements(env, array, &isCopy);
2029     memcpy(jlong_to_ptr(dest), longs, size);
2030     if (isCopy) {
2031         (*env)->ReleaseLongArrayElements(env, array, longs, JNI_ABORT);
2032     }
2033 }
2034 
2035 JNIEXPORT jint JNICALL
2036 Java_sun_awt_X11_XlibWrapper_XSynchronize(JNIEnv *env, jclass clazz, jlong display, jboolean onoff)
2037 {
2038     return (jint) XSynchronize((Display*)jlong_to_ptr(display), (onoff == JNI_TRUE ? True : False));
2039 }
2040 
2041 JNIEXPORT jboolean JNICALL
2042 Java_sun_awt_X11_XlibWrapper_XShapeQueryExtension
2043 (JNIEnv *env, jclass clazz, jlong display, jlong event_base_return, jlong error_base_return)
2044 {
2045     jboolean status;
2046 
2047     AWT_CHECK_HAVE_LOCK();
2048 
2049     status = XShapeQueryExtension((Display *)jlong_to_ptr(display),
2050             (int *)jlong_to_ptr(event_base_return), (int *)jlong_to_ptr(error_base_return));
2051     return status;
2052 }
2053 
2054 /*
2055  * Class:     XlibWrapper
2056  * Method:    SetRectangularShape
2057  */
2058 
2059 JNIEXPORT void JNICALL
2060 Java_sun_awt_X11_XlibWrapper_SetRectangularShape
2061 (JNIEnv *env, jclass clazz, jlong display, jlong window,
2062  jint x1, jint y1, jint x2, jint y2,
2063  jobject region)
2064 {
2065     AWT_CHECK_HAVE_LOCK();
2066 
2067     // If all the params are zeros, the shape must be simply reset.
2068     // Otherwise, the shape may be not rectangular.
2069     if (region || x1 || x2 || y1 || y2) {
2070         XRectangle rects[256];
2071         XRectangle *pRect = rects;
2072 
2073         int numrects = RegionToYXBandedRectangles(env, x1, y1, x2, y2, region,
2074                 &pRect, 256);
2075 
2076         XShapeCombineRectangles((Display *)jlong_to_ptr(display), (Window)jlong_to_ptr(window),
2077                 ShapeClip, 0, 0, pRect, numrects, ShapeSet, YXBanded);
2078         XShapeCombineRectangles((Display *)jlong_to_ptr(display), (Window)jlong_to_ptr(window),
2079                 ShapeBounding, 0, 0, pRect, numrects, ShapeSet, YXBanded);
2080 
2081         if (pRect != rects) {
2082             free(pRect);
2083         }
2084     } else {
2085         // Reset the shape to a rectangular form.
2086         XShapeCombineMask((Display *)jlong_to_ptr(display), (Window)jlong_to_ptr(window),
2087                 ShapeClip, 0, 0, None, ShapeSet);
2088         XShapeCombineMask((Display *)jlong_to_ptr(display), (Window)jlong_to_ptr(window),
2089                 ShapeBounding, 0, 0, None, ShapeSet);
2090     }
2091 }
2092 
2093 /*
2094  * Class:     XlibWrapper
2095  * Method:    SetZOrder
2096  */
2097 
2098 JNIEXPORT void JNICALL
2099 Java_sun_awt_X11_XlibWrapper_SetZOrder
2100 (JNIEnv *env, jclass clazz, jlong display, jlong window, jlong above)
2101 {
2102     AWT_CHECK_HAVE_LOCK();
2103 
2104     XWindowChanges wc;
2105     wc.sibling = (Window)jlong_to_ptr(above);
2106 
2107     unsigned int value_mask = CWStackMode;
2108 
2109     if (above == 0) {
2110         wc.stack_mode = Above;
2111     } else {
2112         wc.stack_mode = Below;
2113         value_mask |= CWSibling;
2114     }
2115 
2116     XConfigureWindow((Display *)jlong_to_ptr(display),
2117                      (Window)jlong_to_ptr(window),
2118                      value_mask, &wc );
2119 }
2120 
2121 /*
2122  * Class:     XlibWrapper
2123  * Method:    SetBitmapShape
2124  */
2125 JNIEXPORT void JNICALL
2126 Java_sun_awt_X11_XlibWrapper_SetBitmapShape
2127 (JNIEnv *env, jclass clazz, jlong display, jlong window,
2128  jint width, jint height, jintArray bitmap)
2129 {
2130     jsize len;
2131     jint *values;
2132     jboolean isCopy = JNI_FALSE;
2133     size_t worstBufferSize = (size_t)((width / 2 + 1) * height);
2134     RECT_T * pRect;
2135 
2136     AWT_CHECK_HAVE_LOCK();
2137 
2138     len = (*env)->GetArrayLength(env, bitmap);
2139     if (len == 0 || len < width * height) {
2140         return;
2141     }
2142 
2143     values = (*env)->GetIntArrayElements(env, bitmap, &isCopy);
2144     if (JNU_IsNull(env, values)) {
2145         return;
2146     }
2147 
2148     pRect = (RECT_T *)malloc(worstBufferSize * sizeof(RECT_T));
2149 
2150     /* Note: the values[0] and values[1] are supposed to contain the width
2151      * and height (see XIconInfo.getIntData() for details). So, we do +2.
2152      */
2153     int numrects = BitmapToYXBandedRectangles(32, (int)width, (int)height,
2154             (unsigned char *)(values + 2), pRect);
2155 
2156     XShapeCombineRectangles((Display *)jlong_to_ptr(display), (Window)jlong_to_ptr(window),
2157             ShapeClip, 0, 0, pRect, numrects, ShapeSet, YXBanded);
2158     XShapeCombineRectangles((Display *)jlong_to_ptr(display), (Window)jlong_to_ptr(window),
2159             ShapeBounding, 0, 0, pRect, numrects, ShapeSet, YXBanded);
2160 
2161     free(pRect);
2162 
2163     (*env)->ReleaseIntArrayElements(env, bitmap, values, JNI_ABORT);
2164 }
2165 
2166 typedef struct {
2167     JNIEnv *env;
2168     jobject predicateObject;
2169     jmethodID predicateMID;
2170 
2171     jobject event;
2172 } CheckIfEventStruct;
2173 
2174 static Bool
2175 check_if_event_predicate(Display* dpy, XEvent* event, char* arg) {
2176     CheckIfEventStruct * data = (CheckIfEventStruct *)arg;
2177     //NOTE: event == data->event.pData
2178 
2179     return (Bool)((*(data->env))->CallBooleanMethod(data->env,
2180             data->predicateObject, data->predicateMID,
2181             data->event));
2182 }
2183 
2184 JNIEXPORT jboolean JNICALL
2185 Java_sun_awt_X11_XlibWrapper_CheckIfEvent
2186 (JNIEnv *env, jclass clazz, jlong display, jobject event, jlong event_return, jobject predicate)
2187 {
2188     //NOTE: event_return == event.pData
2189 
2190     AWT_CHECK_HAVE_LOCK();
2191 
2192     CheckIfEventStruct data;
2193 
2194     data.env = env;
2195     data.predicateObject = predicate;
2196     data.predicateMID = (*env)->GetMethodID(env,
2197             (*env)->GetObjectClass(env, predicate),
2198             "doesMatch", "(Lsun/awt/X11/XEvent;)Z");
2199     data.event = event;
2200 
2201     return (jboolean)XCheckIfEvent((Display *)jlong_to_ptr(display),
2202             (XEvent *)jlong_to_ptr(event_return), check_if_event_predicate,
2203             (char*)&data);
2204 }
2205