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