1 /*
   2  * Copyright (c) 2002, 2010, 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 package sun.awt.X11;
  27 
  28 import java.security.AccessController;
  29 import sun.security.action.GetPropertyAction;
  30 import sun.misc.*;
  31 
  32 public class XlibWrapper implements XConstants, XUtilConstants, XProtocolConstants,
  33                                     XCursorFontConstants
  34 {
  35     static Unsafe unsafe = Unsafe.getUnsafe();
  36     // strange constants
  37     static final int MAXSIZE = 32767;
  38     static final int MINSIZE = 1;
  39 
  40     // define a private constructor here to prevent this class and all
  41     // its descendants from being created
  42     private XlibWrapper()
  43     {
  44     }
  45 
  46 /*
  47    Display *XOpenDisplay(display_name)
  48    char *display_name;
  49 
  50 */
  51     public final static String eventToString[]=
  52     {"<none:0>", "<none:1>", "KeyPress", "KeyRelease", "ButtonPress", "ButtonRelease",
  53      "MotionNotify", "EnterNotify", "LeaveNotify", "FocusIn", "FocusOut",
  54      "KeymapNotify", "Expose", "GraphicsExpose", "NoExpose", "VisibilityNotify",
  55      "CreateNotify", "DestroyNotify", "UnmapNotify", "MapNotify", "MapRequest",
  56      "ReparentNotify", "ConfigureNotify", "ConfigureRequest", "GravityNotify",
  57      "ResizeRequest", "CirculateNotify", "CirculateRequest", "PropertyNotify",
  58      "SelectionClear", "SelectionRequest", "SelectionNotify", "ColormapNotify",
  59      "ClientMessage", "MappingNotify", "LASTEvent"};
  60 
  61     static native void XFree(long ptr);
  62     static native void memcpy(long dest_ptr, long src_ptr, long length);
  63     static native long getAddress(Object o);
  64     static native void copyIntArray(long dest_ptr, Object array, int size_bytes);
  65     static native void copyLongArray(long dest_ptr, Object array, int size_bytes);
  66 
  67     /**
  68      * Gets byte string from str_ptr and copies it into byte array
  69      * String should be NULL terminated
  70      */
  71     static native byte[] getStringBytes(long str_ptr);
  72 
  73     static  native long XOpenDisplay(long display);
  74 
  75     static  native void XCloseDisplay(long display);
  76 
  77     static  native long XDisplayString(long display);
  78 
  79     static  native void XSetCloseDownMode(long display, int close_mode);
  80 
  81     static  native long DefaultScreen(long display);
  82 
  83     static native long ScreenOfDisplay(long display, long screen_number);
  84 
  85     static native int DoesBackingStore(long screen);
  86 
  87     static native  long DisplayWidth(long display, long screen);
  88     static native  long DisplayWidthMM(long display, long screen);
  89 
  90     static native long DisplayHeight(long display, long screen);
  91     static native long DisplayHeightMM(long display, long screen);
  92 
  93     static  native long RootWindow(long display, long screen_number);
  94     static native int ScreenCount(long display);
  95 
  96 
  97 /*
  98    Window XCreateWindow(display, parent, x, y, width, height,
  99    border_width, depth,
 100    class, visual, valuemask, attributes)
 101    Display *display;
 102    Window parent;
 103    int x, y;
 104    unsigned int width, height;
 105    unsigned int border_width;
 106    int depth;
 107    unsigned int class;
 108    Visual *visual
 109    unsigned long valuemask;
 110    XSetWindowAttributes *attributes;
 111 */
 112 
 113     static native long XCreateWindow(long display, long parent, int x,int  y, int width, int height, int border_width, int depth, long wclass, long visual, long valuemask, long attributes);
 114 
 115     static native void XDestroyWindow(long display, long window);
 116 
 117     static native int XGrabPointer(long display, long grab_window,
 118                                    int owner_events, int event_mask, int pointer_mode,
 119                                    int keyboard_mode, long confine_to, long cursor, long time);
 120 
 121     static native void XUngrabPointer(long display, long time);
 122 
 123     static native int XGrabKeyboard(long display, long grab_window,
 124                                     int owner_events, int pointer_mode,
 125                                     int keyboard_mode, long time);
 126 
 127     static native void XUngrabKeyboard(long display, long time);
 128 
 129     static native void XGrabServer(long display);
 130     static native void XUngrabServer(long display);
 131 
 132 /*
 133 
 134 void XSetWMProperties(display, w, window_name, icon_name,
 135 argv, argc, normal_hints, wm_hints, class_hints)
 136 Display *display;
 137 Window w;
 138 XTextProperty *window_name;
 139 XTextProperty *icon_name;
 140 char **argv;
 141 int argc;
 142 XSizeHints *normal_hints;
 143 XWMHints *wm_hints;
 144 XClassHint *class_hints;
 145 */
 146 
 147 /*
 148 
 149 XMapWindow(display, w)
 150 Display *display;
 151 Window w;
 152 */
 153 
 154     static  native void XMapWindow(long display, long window);
 155     static  native void XMapRaised(long display, long window);
 156     static  native void XRaiseWindow(long display, long window);
 157 
 158     static native void XLowerWindow(long display, long window);
 159     static native void XRestackWindows(long display, long windows, int length);
 160     static native void XConfigureWindow(long display, long window,
 161             long value_mask, long values);
 162     static native void XSetInputFocus(long display, long window);
 163     static native void XSetInputFocus2(long display, long window, long time);
 164     static native long XGetInputFocus(long display);
 165 
 166 /*
 167 
 168 XUnmapWindow(display, w)
 169 Display *display;
 170 Window w;
 171 */
 172 
 173     static  native void XUnmapWindow(long display, long window);
 174 
 175 
 176 
 177 
 178 /*
 179   XSelectInput(display, w, event_mask)
 180   Display *display;
 181   Window w;
 182   long event_mask;
 183 
 184 */
 185     static  native void XSelectInput(long display, long window, long event_mask);
 186 
 187     /*
 188        XNextEvent(display, event_return)
 189        Display *display;
 190        XEvent *event_return;
 191 
 192     */
 193 
 194     static  native void XNextEvent(long display,long ptr);
 195 
 196     /*
 197      XMaskEvent(display, event_mask, event_return)
 198            Display *display;
 199            long event_mask;
 200            XEvent *event_return;
 201      */
 202     static native void XMaskEvent(long display, long event_mask, long event_return);
 203 
 204     static native void XWindowEvent(long display, long window, long event_mask, long event_return);
 205 
 206     /*
 207       Bool XFilterEvent(event, w)
 208            XEvent *event;
 209            Window w;
 210     */
 211     static native boolean XFilterEvent(long ptr, long window);
 212 
 213 /*
 214      Bool XSupportsLocale()
 215 */
 216 static native boolean XSupportsLocale();
 217 
 218 /*
 219      char *XSetLocaleModifiers(modifier_list)
 220            char *modifier_list;
 221 */
 222 static native String XSetLocaleModifiers(String modifier_list);
 223 
 224 
 225     static  native int XTranslateCoordinates(
 226                                              long display, long src_w, long dest_w,
 227                                              long src_x, long src_y,
 228                                              long dest_x_return, long dest_y_return,
 229                                              long child_return);
 230 
 231     /*
 232        XPeekEvent(display, event_return)
 233        Display *display;
 234        XEvent *event_return;
 235 
 236     */
 237 
 238     static  native void XPeekEvent(long display,long ptr);
 239 
 240 /*
 241   XFlush(display)
 242   Display *display;
 243 */
 244 
 245     static native void XFlush(long display);
 246 
 247 /*
 248   XSync(display, discard)
 249   Display *display;
 250   Bool discard;
 251 */
 252 
 253     static native void XSync(long display,int discard);
 254 
 255 
 256 /*    XMoveResizeWindow(display, w, x, y, width, height)
 257       Display *display;
 258       Window w;
 259       int x, y;
 260       unsigned int width, height;
 261 */
 262     static native void XMoveResizeWindow(long display, long window, int x, int y, int width, int height);
 263     static native void XResizeWindow(long display, long window, int width, int height);
 264     static native void XMoveWindow(long display, long window, int x, int y);
 265 
 266     /*
 267      Bool XQueryPointer(display, w, root_return, child_return,
 268      root_x_return, root_y_return,
 269                           win_x_return, win_y_return,
 270      mask_return)
 271            Display *display;
 272            Window w;
 273            Window *root_return, *child_return;
 274            int *root_x_return, *root_y_return;
 275            int *win_x_return, *win_y_return;
 276            unsigned int *mask_return;
 277 */
 278 
 279  static native boolean  XQueryPointer (long display, long window, long root_return, long child_return, long root_x_return, long root_y_return, long win_x_return, long win_y_return, long mask_return);
 280 
 281 /*    XFreeCursor(display, cursor)
 282            Display *display;
 283            Cursor cursor;
 284 */
 285 
 286  static native void XFreeCursor(long display, long cursor);
 287 
 288 /*
 289    XSetWindowBackground(display, w, background_pixel)
 290    Display *display;
 291    Window w;
 292    unsigned long background_pixel;
 293 */
 294 
 295     static native void XSetWindowBackground(long display, long window, long background_pixel);
 296 
 297     static native int XEventsQueued(long display, int mode);
 298 
 299 /*
 300   Atom XInternAtom(display, atom_name, only_if_exists)
 301   Display *display;
 302   char *atom_name;
 303   Bool only_if_exists;
 304 */
 305 
 306     static native int XInternAtoms(long display, String[] names, boolean only_if_exists, long atoms);
 307 
 308     static native void SetProperty(long display, long window, long atom, String str);
 309     static native String GetProperty(long display ,long window, long atom);
 310     static native long InternAtom(long display, String string, int only_if_exists);
 311     static native int XGetWindowProperty(long display, long window, long atom,
 312                                          long long_offset, long long_length,
 313                                          long delete, long req_type, long actualy_type,
 314                                          long actualy_format, long nitems_ptr,
 315                                          long bytes_after, long data_ptr);
 316     native static void XChangePropertyImpl(long display, long window, long atom,
 317                                            long type, int format, int mode, long data,
 318                                            int nelements);
 319     static void XChangeProperty(long display, long window, long atom,
 320                                 long type, int format, int mode, long data,
 321                                 int nelements) {
 322         // TODO: handling of XChangePropertyImpl return value, if not Success - don't cache
 323         if (XPropertyCache.isCachingSupported() &&
 324             XToolkit.windowToXWindow(window) != null &&
 325             WindowPropertyGetter.isCacheableProperty(XAtom.get(atom)) &&
 326             mode == PropModeReplace)
 327         {
 328             int length = (format / 8) * nelements;
 329             XPropertyCache.storeCache(
 330                 new XPropertyCache.PropertyCacheEntry(format,
 331                                                       nelements,
 332                                                       0,
 333                                                       data,
 334                                                       length),
 335                 window,
 336                 XAtom.get(atom));
 337         }
 338         XChangePropertyImpl(display, window, atom, type, format, mode, data, nelements);
 339     }
 340 
 341     static native void XChangePropertyS(long display, long window, long atom,
 342                                        long type, int format, int mode, String value);
 343     static native void XDeleteProperty(long display, long window, long atom);
 344 
 345     static native void XSetTransientFor(long display, long window, long transient_for_window);
 346     static native void XSetWMHints(long display, long window, long wmhints);
 347     static native void XGetWMHints(long display, long window, long wmhints);
 348     static native long XAllocWMHints();
 349     static native int XGetPointerMapping(long display, long map, int buttonNumber);
 350     static native String XGetDefault(long display, String program, String option);
 351     static native long getScreenOfWindow(long display, long window);
 352     static native long XScreenNumberOfScreen(long screen);
 353     static native int XIconifyWindow(long display, long window, long screenNumber);
 354     static native String ServerVendor(long display);
 355     static native int VendorRelease(long display);
 356 
 357     static native void XBell(long display, int percent);
 358 
 359  /*
 360           Cursor XCreateFontCursor(display, shape)
 361            Display *display;
 362            unsigned int shape;
 363 
 364            we always pass int as shape param.
 365            perhaps later we will need to change type of shape to long.
 366 */
 367 
 368     static native int XCreateFontCursor(long display, int shape);
 369 
 370 /*
 371      Pixmap XCreateBitmapFromData(display, d, data, width,
 372      height)
 373            Display *display;
 374            Drawable d;
 375            char *data;
 376            unsigned int width, height;
 377 */
 378 
 379     static native long XCreateBitmapFromData(long display, long drawable, long data, int width, int height);
 380 
 381  /*
 382       XFreePixmap(display, pixmap)
 383            Display *display;
 384            Pixmap pixmap;
 385   */
 386 
 387    static native void XFreePixmap(long display, long pixmap);
 388 
 389   /*
 390      Cursor XCreatePixmapCursor(display, source, mask,
 391      foreground_color, background_color, x, y)
 392            Display *display;
 393            Pixmap source;
 394            Pixmap mask;
 395            XColor *foreground_color;
 396            XColor *background_color;
 397            unsigned int x, y;
 398     */
 399    static native long XCreatePixmapCursor(long display, long source, long mask, long fore, long back, int x, int y);
 400 
 401 
 402     /*
 403          Status XQueryBestCursor(display, d, width, height,
 404      width_return, height_return)
 405            Display *display;
 406            Drawable d;
 407            unsigned int width, height;
 408            unsigned int *width_return, *height_return;
 409 
 410     */
 411 
 412     static native boolean XQueryBestCursor(long display, long drawable, int width, int height, long width_return, long height_return);
 413 
 414 
 415     /*
 416      Status XAllocColor(display, colormap, screen_in_out)
 417            Display *display;
 418            Colormap colormap;
 419            XColor *screen_in_out;
 420   */
 421 
 422     static native boolean XAllocColor( long display, long colormap, long screen_in_out);
 423 
 424 
 425     static native long SetToolkitErrorHandler();
 426     static native void XSetErrorHandler(long handler);
 427     static native int CallErrorHandler(long handler, long display, long event_ptr);
 428 
 429  /*
 430       XChangeWindowAttributes(display, w, valuemask, attributes)
 431            Display *display;
 432            Window w;
 433            unsigned long valuemask;
 434            XSetWindowAttributes *attributes;
 435   */
 436 
 437     static native void XChangeWindowAttributes(long display, long window, long valuemask, long attributes);
 438     static native int XGetWindowAttributes(long display, long window, long attr_ptr);
 439     static native int XGetGeometry(long display, long drawable, long root_return, long x_return, long y_return,
 440                                    long width_return, long height_return, long border_width_return, long depth_return);
 441 
 442     static native int XGetWMNormalHints(long display, long window, long hints, long supplied_return);
 443     static native void XSetWMNormalHints(long display, long window, long hints);
 444     static native void XSetMinMaxHints(long display, long window, int x, int y, int width, int height, long flags);
 445     static native long XAllocSizeHints();
 446 
 447     static native int XSendEvent(long display, long window, boolean propagate, long event_mask, long event);
 448     static native void XPutBackEvent(long display, long event);
 449     static native int XQueryTree(long display, long window, long root_return, long parent_return, long children_return, long nchildren_return);
 450     static native long XGetVisualInfo(long display, long vinfo_mask, long vinfo_template, long nitems_return);
 451     static native void XReparentWindow(long display, long window, long parent, int x, int y);
 452 
 453     static native void XConvertSelection(long display, long selection,
 454                                          long target, long property,
 455                                          long requestor, long time);
 456 
 457     static native void XSetSelectionOwner(long display, long selection,
 458                                           long owner, long time);
 459 
 460     static native long XGetSelectionOwner(long display, long selection);
 461 
 462     static native String XGetAtomName(long display, long atom);
 463 
 464     static native long XMaxRequestSize(long display);
 465 
 466 
 467     static native long XCreatePixmap(long display, long drawable, int width, int height, int depth);
 468     static native long XCreateImage(long display, long visual_ptr, int depth, int format,
 469                                     int offset, long data, int width, int height, int bitmap_pad,
 470                                     int bytes_per_line);
 471     static native void XDestroyImage(long image);
 472     static native void XPutImage(long display, long drawable, long gc, long image,
 473                                  int src_x, int src_y, int dest_x, int dest_y,
 474                                  int width, int height);
 475     static native long XCreateGC(long display, long drawable, long valuemask, long values);
 476     static native void XFreeGC(long display, long gc);
 477     static native void XSetWindowBackgroundPixmap(long display, long window, long pixmap);
 478     static native void XClearWindow(long display, long window);
 479     static native int XGetIconSizes(long display, long window, long ret_sizes, long ret_count);
 480     static native int XdbeQueryExtension(long display, long major_version_return,
 481                                          long minor_version_return);
 482     static native boolean XQueryExtension(long display, String name, long mop_return,
 483                                          long feve_return, long err_return);
 484     static native boolean IsKeypadKey(long keysym);
 485     static native long XdbeAllocateBackBufferName(long display, long window, int swap_action);
 486     static native int XdbeDeallocateBackBufferName(long display, long buffer);
 487     static native int XdbeBeginIdiom(long display);
 488     static native int XdbeEndIdiom(long display);
 489     static native int XdbeSwapBuffers(long display, long swap_info, int num_windows);
 490 
 491     static native long XKeycodeToKeysym(long display, int keycode, int index);
 492 
 493     static native int XKeysymToKeycode(long display, long keysym);
 494 
 495     static native void XConvertCase(long keysym,
 496                                     long keysym_lowercase,
 497                                     long keysym_uppercase);
 498 
 499     static native long XGetModifierMapping(long display);
 500 
 501     static native void XFreeModifiermap(long keymap);
 502 
 503     static native void XChangeActivePointerGrab(long display, int mask,
 504                                                 long cursor, long time);
 505 
 506     /*
 507       int (*XSynchronize(Display *display, Bool onoff))();
 508           display   Specifies the connection to the X server.
 509           onoff     Specifies a Boolean value that indicates whether to enable or disable synchronization.
 510      */
 511     public static native int XSynchronize(long display, boolean onoff);
 512 
 513     /**
 514      * Extracts an X event that can be processed in a secondary loop.
 515      * Should only be called on the toolkit thread.
 516      * Returns false if this secondary event was terminated.
 517      */
 518     static native boolean XNextSecondaryLoopEvent(long display, long ptr);
 519     /**
 520      * Terminates the topmost secondary loop (if any).
 521      * Should never be called on the toolkit thread.
 522      */
 523     static native void ExitSecondaryLoop();
 524 
 525     /**
 526      * Calls XTextPropertyToStringList on the specified byte array and returns
 527      * the array of strings.
 528      */
 529     static native String[] XTextPropertyToStringList(byte[] bytes, long encoding_atom);
 530 
 531     /**
 532      * XSHAPE extension support.
 533      */
 534     static native boolean XShapeQueryExtension(long display, long event_base_return, long error_base_return);
 535     static native void SetRectangularShape(long display, long window,
 536             int lox, int loy, int hix, int hiy,
 537             sun.java2d.pipe.Region region);
 538     /** Each int in the bitmap array is one pixel with a 32-bit color:
 539      *  R, G, B, and Alpha.
 540      */
 541     static native void SetBitmapShape(long display, long window,
 542              int width, int height, int[] bitmap);
 543 
 544 /* Global memory area used for X lib parameter passing */
 545 
 546     final static long lbuffer = unsafe.allocateMemory(64);  // array to hold 8 longs
 547     final static long ibuffer = unsafe.allocateMemory(32);  // array to hold 8 ints
 548 
 549     static final long larg1 = lbuffer;
 550     static final long larg2 = larg1+8;
 551     static final long larg3 = larg2+8;
 552     static final long larg4 = larg3+8;
 553     static final long larg5 = larg4+8;
 554     static final long larg6 = larg5+8;
 555     static final long larg7 = larg6+8;
 556     static final long larg8 = larg7+8;
 557 
 558     static final long iarg1 = ibuffer;
 559     static final long iarg2 = iarg1+4;
 560     static final long iarg3 = iarg2+4;
 561     static final long iarg4 = iarg3+4;
 562     static final long iarg5 = iarg4+4;
 563     static final long iarg6 = iarg5+4;
 564     static final long iarg7 = iarg6+4;
 565     static final long iarg8 = iarg7+4;
 566 
 567 
 568     static int dataModel;
 569     static final boolean isBuildInternal;
 570 
 571     static {
 572         String dataModelProp = AccessController.doPrivileged(new GetPropertyAction("sun.arch.data.model"));
 573         try {
 574             dataModel = Integer.parseInt(dataModelProp);
 575         } catch (Exception e) {
 576             dataModel = 32;
 577         }
 578 
 579         isBuildInternal = getBuildInternal();
 580 
 581 //      System.loadLibrary("mawt");
 582     }
 583 
 584     static int getDataModel() {
 585         return dataModel;
 586     }
 587 
 588     static String hintsToString(long flags) {
 589         StringBuffer buf = new StringBuffer();
 590         if ((flags & PMaxSize) != 0) {
 591             buf.append("PMaxSize ");
 592         }
 593         if ((flags & PMinSize) != 0) {
 594             buf.append("PMinSize ");
 595         }
 596         if ((flags & USSize) != 0) {
 597             buf.append("USSize ");
 598         }
 599         if ((flags & USPosition) != 0) {
 600             buf.append("USPosition ");
 601         }
 602         if ((flags & PPosition) != 0) {
 603             buf.append("PPosition ");
 604         }
 605         if ((flags & PSize) != 0) {
 606             buf.append("PSize ");
 607         }
 608         if ((flags & PWinGravity) != 0) {
 609             buf.append("PWinGravity ");
 610         }
 611         return buf.toString();
 612     }
 613 
 614     private static boolean getBuildInternal() {
 615         String javaVersion = AccessController.doPrivileged(new GetPropertyAction("java.version"));
 616         return javaVersion != null && javaVersion.contains("internal");
 617     }
 618 
 619     static native void PrintXErrorEvent(long display, long event_ptr);
 620 }