1 /* 2 * Copyright (c) 2002, 2009, 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 java.security.PrivilegedAction; 30 import sun.security.action.GetPropertyAction; 31 import sun.misc.*; 32 33 final public class XlibWrapper 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 == XConstants.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 static native boolean IsXsunKPBehavior(long display); 357 static native boolean IsSunKeyboard(long display); 358 static native boolean IsKanaKeyboard(long display); 359 360 static native void XBell(long display, int percent); 361 362 /* 363 Cursor XCreateFontCursor(display, shape) 364 Display *display; 365 unsigned int shape; 366 367 we always pass int as shape param. 368 perhaps later we will need to change type of shape to long. 369 */ 370 371 static native int XCreateFontCursor(long display, int shape); 372 373 /* 374 Pixmap XCreateBitmapFromData(display, d, data, width, 375 height) 376 Display *display; 377 Drawable d; 378 char *data; 379 unsigned int width, height; 380 */ 381 382 static native long XCreateBitmapFromData(long display, long drawable, long data, int width, int height); 383 384 /* 385 XFreePixmap(display, pixmap) 386 Display *display; 387 Pixmap pixmap; 388 */ 389 390 static native void XFreePixmap(long display, long pixmap); 391 392 /* 393 Cursor XCreatePixmapCursor(display, source, mask, 394 foreground_color, background_color, x, y) 395 Display *display; 396 Pixmap source; 397 Pixmap mask; 398 XColor *foreground_color; 399 XColor *background_color; 400 unsigned int x, y; 401 */ 402 static native long XCreatePixmapCursor(long display, long source, long mask, long fore, long back, int x, int y); 403 404 405 /* 406 Status XQueryBestCursor(display, d, width, height, 407 width_return, height_return) 408 Display *display; 409 Drawable d; 410 unsigned int width, height; 411 unsigned int *width_return, *height_return; 412 413 */ 414 415 static native boolean XQueryBestCursor(long display, long drawable, int width, int height, long width_return, long height_return); 416 417 418 /* 419 Status XAllocColor(display, colormap, screen_in_out) 420 Display *display; 421 Colormap colormap; 422 XColor *screen_in_out; 423 */ 424 425 static native boolean XAllocColor( long display, long colormap, long screen_in_out); 426 427 428 static native long SetToolkitErrorHandler(); 429 static native void XSetErrorHandler(long handler); 430 static native int CallErrorHandler(long handler, long display, long event_ptr); 431 432 /* 433 XChangeWindowAttributes(display, w, valuemask, attributes) 434 Display *display; 435 Window w; 436 unsigned long valuemask; 437 XSetWindowAttributes *attributes; 438 */ 439 440 static native void XChangeWindowAttributes(long display, long window, long valuemask, long attributes); 441 static native int XGetWindowAttributes(long display, long window, long attr_ptr); 442 static native int XGetGeometry(long display, long drawable, long root_return, long x_return, long y_return, 443 long width_return, long height_return, long border_width_return, long depth_return); 444 445 static native int XGetWMNormalHints(long display, long window, long hints, long supplied_return); 446 static native void XSetWMNormalHints(long display, long window, long hints); 447 static native void XSetMinMaxHints(long display, long window, int x, int y, int width, int height, long flags); 448 static native long XAllocSizeHints(); 449 450 static native int XSendEvent(long display, long window, boolean propagate, long event_mask, long event); 451 static native void XPutBackEvent(long display, long event); 452 static native int XQueryTree(long display, long window, long root_return, long parent_return, long children_return, long nchildren_return); 453 static native long XGetVisualInfo(long display, long vinfo_mask, long vinfo_template, long nitems_return); 454 static native void XReparentWindow(long display, long window, long parent, int x, int y); 455 456 static native void XConvertSelection(long display, long selection, 457 long target, long property, 458 long requestor, long time); 459 460 static native void XSetSelectionOwner(long display, long selection, 461 long owner, long time); 462 463 static native long XGetSelectionOwner(long display, long selection); 464 465 static native String XGetAtomName(long display, long atom); 466 467 static native long XMaxRequestSize(long display); 468 469 470 static native long XCreatePixmap(long display, long drawable, int width, int height, int depth); 471 static native long XCreateImage(long display, long visual_ptr, int depth, int format, 472 int offset, long data, int width, int height, int bitmap_pad, 473 int bytes_per_line); 474 static native void XDestroyImage(long image); 475 static native void XPutImage(long display, long drawable, long gc, long image, 476 int src_x, int src_y, int dest_x, int dest_y, 477 int width, int height); 478 static native long XCreateGC(long display, long drawable, long valuemask, long values); 479 static native void XFreeGC(long display, long gc); 480 static native void XSetWindowBackgroundPixmap(long display, long window, long pixmap); 481 static native void XClearWindow(long display, long window); 482 static native int XGetIconSizes(long display, long window, long ret_sizes, long ret_count); 483 static native int XdbeQueryExtension(long display, long major_version_return, 484 long minor_version_return); 485 static native boolean XQueryExtension(long display, String name, long mop_return, 486 long feve_return, long err_return); 487 static native boolean IsKeypadKey(long keysym); 488 static native long XdbeAllocateBackBufferName(long display, long window, int swap_action); 489 static native int XdbeDeallocateBackBufferName(long display, long buffer); 490 static native int XdbeBeginIdiom(long display); 491 static native int XdbeEndIdiom(long display); 492 static native int XdbeSwapBuffers(long display, long swap_info, int num_windows); 493 494 static native void XQueryKeymap(long display, long vector); 495 static native long XKeycodeToKeysym(long display, int keycode, int index); 496 497 static native int XKeysymToKeycode(long display, long keysym); 498 499 // xkb-related 500 static native int XkbGetEffectiveGroup(long display); 501 static native long XkbKeycodeToKeysym(long display, int keycode, int group, int level); 502 static native void XkbSelectEvents(long display, long device, long bits_to_change, long values_for_bits); 503 static native void XkbSelectEventDetails(long display, long device, long event_type, 504 long bits_to_change, long values_for_bits); 505 static native boolean XkbQueryExtension(long display, long opcode_rtrn, long event_rtrn, 506 long error_rtrn, long major_in_out, long minor_in_out); 507 static native boolean XkbLibraryVersion(long lib_major_in_out, long lib_minor_in_out); 508 static native long XkbGetMap(long display, long which, long device_spec); 509 static native long XkbGetUpdatedMap(long display, long which, long xkb); 510 static native void XkbFreeKeyboard(long xkb, long which, boolean free_all); 511 static native boolean XkbTranslateKeyCode(long xkb, int keycode, long mods, long mods_rtrn, long keysym_rtrn); 512 513 514 static native void XConvertCase(long keysym, 515 long keysym_lowercase, 516 long keysym_uppercase); 517 518 static native long XGetModifierMapping(long display); 519 static native void XFreeModifiermap(long keymap); 520 static native void XRefreshKeyboardMapping(long event); 521 522 523 static native void XChangeActivePointerGrab(long display, int mask, 524 long cursor, long time); 525 526 /* 527 int (*XSynchronize(Display *display, Bool onoff))(); 528 display Specifies the connection to the X server. 529 onoff Specifies a Boolean value that indicates whether to enable or disable synchronization. 530 */ 531 public static native int XSynchronize(long display, boolean onoff); 532 533 /** 534 * Extracts an X event that can be processed in a secondary loop. 535 * Should only be called on the toolkit thread. 536 * Returns false if this secondary event was terminated. 537 */ 538 static native boolean XNextSecondaryLoopEvent(long display, long ptr); 539 /** 540 * Terminates the topmost secondary loop (if any). 541 * Should never be called on the toolkit thread. 542 */ 543 static native void ExitSecondaryLoop(); 544 545 /** 546 * Calls XTextPropertyToStringList on the specified byte array and returns 547 * the array of strings. 548 */ 549 static native String[] XTextPropertyToStringList(byte[] bytes, long encoding_atom); 550 551 /** 552 * XSHAPE extension support. 553 */ 554 static native boolean XShapeQueryExtension(long display, long event_base_return, long error_base_return); 555 static native void SetRectangularShape(long display, long window, 556 int lox, int loy, int hix, int hiy, 557 sun.java2d.pipe.Region region); 558 /** Each int in the bitmap array is one pixel with a 32-bit color: 559 * R, G, B, and Alpha. 560 */ 561 static native void SetBitmapShape(long display, long window, 562 int width, int height, int[] bitmap); 563 564 static native void SetZOrder(long display, long window, long above); 565 566 /* Global memory area used for X lib parameter passing */ 567 568 final static long lbuffer = unsafe.allocateMemory(64); // array to hold 8 longs 569 final static long ibuffer = unsafe.allocateMemory(32); // array to hold 8 ints 570 571 static final long larg1 = lbuffer; 572 static final long larg2 = larg1+8; 573 static final long larg3 = larg2+8; 574 static final long larg4 = larg3+8; 575 static final long larg5 = larg4+8; 576 static final long larg6 = larg5+8; 577 static final long larg7 = larg6+8; 578 static final long larg8 = larg7+8; 579 580 static final long iarg1 = ibuffer; 581 static final long iarg2 = iarg1+4; 582 static final long iarg3 = iarg2+4; 583 static final long iarg4 = iarg3+4; 584 static final long iarg5 = iarg4+4; 585 static final long iarg6 = iarg5+4; 586 static final long iarg7 = iarg6+4; 587 static final long iarg8 = iarg7+4; 588 589 590 static int dataModel; 591 static final boolean isBuildInternal; 592 593 static { 594 String dataModelProp = AccessController.doPrivileged( 595 new GetPropertyAction("sun.arch.data.model")); 596 try { 597 dataModel = Integer.parseInt(dataModelProp); 598 } catch (Exception e) { 599 dataModel = 32; 600 } 601 602 isBuildInternal = getBuildInternal(); 603 604 // System.loadLibrary("mawt"); 605 } 606 607 static int getDataModel() { 608 return dataModel; 609 } 610 611 static String hintsToString(long flags) { 612 StringBuffer buf = new StringBuffer(); 613 if ((flags & XUtilConstants.PMaxSize) != 0) { 614 buf.append("PMaxSize "); 615 } 616 if ((flags & XUtilConstants.PMinSize) != 0) { 617 buf.append("PMinSize "); 618 } 619 if ((flags & XUtilConstants.USSize) != 0) { 620 buf.append("USSize "); 621 } 622 if ((flags & XUtilConstants.USPosition) != 0) { 623 buf.append("USPosition "); 624 } 625 if ((flags & XUtilConstants.PPosition) != 0) { 626 buf.append("PPosition "); 627 } 628 if ((flags & XUtilConstants.PSize) != 0) { 629 buf.append("PSize "); 630 } 631 if ((flags & XUtilConstants.PWinGravity) != 0) { 632 buf.append("PWinGravity "); 633 } 634 return buf.toString(); 635 } 636 static String getEventToString( int type ) { 637 if( (type >= 0) && (type < eventToString.length)) { 638 return eventToString[type]; 639 }else if( type == XToolkit.getXKBBaseEventCode() ) { 640 //XXX TODO various xkb types 641 return "XkbEvent"; 642 } 643 return eventToString[0]; 644 } 645 646 private static boolean getBuildInternal() { 647 String javaVersion = AccessController.doPrivileged( 648 new GetPropertyAction("java.version")); 649 return javaVersion != null && javaVersion.contains("internal"); 650 } 651 652 static native void PrintXErrorEvent(long display, long event_ptr); 653 }