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