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.awt.*; 29 import java.awt.event.*; 30 import java.awt.peer.ComponentPeer; 31 import java.awt.image.ColorModel; 32 33 import java.lang.ref.WeakReference; 34 35 import java.lang.reflect.Method; 36 37 import sun.util.logging.PlatformLogger; 38 39 import sun.awt.*; 40 41 import sun.awt.image.PixelConverter; 42 43 import sun.java2d.SunGraphics2D; 44 import sun.java2d.SurfaceData; 45 46 public class XWindow extends XBaseWindow implements X11ComponentPeer { 47 private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XWindow"); 48 private static PlatformLogger insLog = PlatformLogger.getLogger("sun.awt.X11.insets.XWindow"); 49 private static PlatformLogger eventLog = PlatformLogger.getLogger("sun.awt.X11.event.XWindow"); 50 private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XWindow"); 51 private static PlatformLogger keyEventLog = PlatformLogger.getLogger("sun.awt.X11.kye.XWindow"); 52 /* If a motion comes in while a multi-click is pending, 53 * allow a smudge factor so that moving the mouse by a small 54 * amount does not wipe out the multi-click state variables. 55 */ 56 private final static int AWT_MULTICLICK_SMUDGE = 4; 57 // ButtonXXX events stuff 58 static int rbutton = 0; 59 static int lastX = 0, lastY = 0; 60 static long lastTime = 0; 61 static long lastButton = 0; 62 static WeakReference lastWindowRef = null; 63 static int clickCount = 0; 64 65 // used to check if we need to re-create surfaceData. 66 int oldWidth = -1; 67 int oldHeight = -1; 68 69 protected PropMwmHints mwm_hints; 70 protected static XAtom wm_protocols; 71 protected static XAtom wm_delete_window; 72 protected static XAtom wm_take_focus; 73 74 private boolean stateChanged; // Indicates whether the value on savedState is valid 75 private int savedState; // Holds last known state of the top-level window 76 77 XWindowAttributesData winAttr; 78 79 protected X11GraphicsConfig graphicsConfig; 80 protected AwtGraphicsConfigData graphicsConfigData; 81 82 private boolean reparented; 83 84 XWindow parent; 85 86 Component target; 87 88 private static int JAWT_LOCK_ERROR=0x00000001; 89 private static int JAWT_LOCK_CLIP_CHANGED=0x00000002; 90 private static int JAWT_LOCK_BOUNDS_CHANGED=0x00000004; 91 private static int JAWT_LOCK_SURFACE_CHANGED=0x00000008; 92 private int drawState = JAWT_LOCK_CLIP_CHANGED | 93 JAWT_LOCK_BOUNDS_CHANGED | 94 JAWT_LOCK_SURFACE_CHANGED; 95 96 public static final String TARGET = "target", 97 REPARENTED = "reparented"; // whether it is reparented by default 98 99 SurfaceData surfaceData; 100 101 XRepaintArea paintArea; 102 103 // fallback default font object 104 private static Font defaultFont; 105 106 static synchronized Font getDefaultFont() { 107 if (null == defaultFont) { 108 defaultFont = new Font(Font.DIALOG, Font.PLAIN, 12); 109 } 110 return defaultFont; 111 } 112 113 /* A bitmask keeps the button's numbers as Button1Mask, Button2Mask, Button3Mask 114 * which are allowed to 115 * generate the CLICK event after the RELEASE has happened. 116 * There are conditions that must be true for that sending CLICK event: 117 * 1) button was initially PRESSED 118 * 2) no movement or drag has happened until RELEASE 119 */ 120 private int mouseButtonClickAllowed = 0; 121 122 native int getNativeColor(Color clr, GraphicsConfiguration gc); 123 native void getWMInsets(long window, long left, long top, long right, long bottom, long border); 124 native long getTopWindow(long window, long rootWin); 125 native void getWindowBounds(long window, long x, long y, long width, long height); 126 private native static void initIDs(); 127 128 static { 129 initIDs(); 130 } 131 132 XWindow(XCreateWindowParams params) { 133 super(params); 134 } 135 136 XWindow() { 137 } 138 139 XWindow(long parentWindow, Rectangle bounds) { 140 super(new XCreateWindowParams(new Object[] { 141 BOUNDS, bounds, 142 PARENT_WINDOW, Long.valueOf(parentWindow)})); 143 } 144 145 XWindow(Component target, long parentWindow, Rectangle bounds) { 146 super(new XCreateWindowParams(new Object[] { 147 BOUNDS, bounds, 148 PARENT_WINDOW, Long.valueOf(parentWindow), 149 TARGET, target})); 150 } 151 152 XWindow(Component target, long parentWindow) { 153 this(target, parentWindow, new Rectangle(target.getBounds())); 154 } 155 156 XWindow(Component target) { 157 this(target, (target.getParent() == null) ? 0 : getParentWindowID(target), new Rectangle(target.getBounds())); 158 } 159 160 XWindow(Object target) { 161 this(null, 0, null); 162 } 163 164 /* This create is used by the XEmbeddedFramePeer since it has to create the window 165 as a child of the netscape window. This netscape window is passed in as wid */ 166 XWindow(long parentWindow) { 167 super(new XCreateWindowParams(new Object[] { 168 PARENT_WINDOW, Long.valueOf(parentWindow), 169 REPARENTED, Boolean.TRUE, 170 EMBEDDED, Boolean.TRUE})); 171 } 172 173 protected void initGraphicsConfiguration() { 174 graphicsConfig = (X11GraphicsConfig) target.getGraphicsConfiguration(); 175 graphicsConfigData = new AwtGraphicsConfigData(graphicsConfig.getAData()); 176 } 177 178 void preInit(XCreateWindowParams params) { 179 super.preInit(params); 180 reparented = Boolean.TRUE.equals(params.get(REPARENTED)); 181 182 target = (Component)params.get(TARGET); 183 184 initGraphicsConfiguration(); 185 186 AwtGraphicsConfigData gData = getGraphicsConfigurationData(); 187 X11GraphicsConfig config = (X11GraphicsConfig) getGraphicsConfiguration(); 188 XVisualInfo visInfo = gData.get_awt_visInfo(); 189 params.putIfNull(EVENT_MASK, XConstants.KeyPressMask | XConstants.KeyReleaseMask 190 | XConstants.FocusChangeMask | XConstants.ButtonPressMask | XConstants.ButtonReleaseMask 191 | XConstants.EnterWindowMask | XConstants.LeaveWindowMask | XConstants.PointerMotionMask 192 | XConstants.ButtonMotionMask | XConstants.ExposureMask | XConstants.StructureNotifyMask); 193 194 if (target != null) { 195 params.putIfNull(BOUNDS, new Rectangle(target.getBounds())); 196 } else { 197 params.putIfNull(BOUNDS, new Rectangle(0, 0, MIN_SIZE, MIN_SIZE)); 198 } 199 params.putIfNull(BORDER_PIXEL, Long.valueOf(0)); 200 getColorModel(); // fix 4948833: this call forces the color map to be initialized 201 params.putIfNull(COLORMAP, gData.get_awt_cmap()); 202 params.putIfNull(DEPTH, gData.get_awt_depth()); 203 params.putIfNull(VISUAL_CLASS, Integer.valueOf((int)XConstants.InputOutput)); 204 params.putIfNull(VISUAL, visInfo.get_visual()); 205 params.putIfNull(VALUE_MASK, XConstants.CWBorderPixel | XConstants.CWEventMask | XConstants.CWColormap); 206 Long parentWindow = (Long)params.get(PARENT_WINDOW); 207 if (parentWindow == null || parentWindow.longValue() == 0) { 208 XToolkit.awtLock(); 209 try { 210 int screen = visInfo.get_screen(); 211 if (screen != -1) { 212 params.add(PARENT_WINDOW, XlibWrapper.RootWindow(XToolkit.getDisplay(), screen)); 213 } else { 214 params.add(PARENT_WINDOW, XToolkit.getDefaultRootWindow()); 215 } 216 } finally { 217 XToolkit.awtUnlock(); 218 } 219 } 220 221 paintArea = new XRepaintArea(); 222 if (target != null) { 223 this.parent = getParentXWindowObject(target.getParent()); 224 } 225 226 params.putIfNull(BACKING_STORE, XToolkit.getBackingStoreType()); 227 228 XToolkit.awtLock(); 229 try { 230 if (wm_protocols == null) { 231 wm_protocols = XAtom.get("WM_PROTOCOLS"); 232 wm_delete_window = XAtom.get("WM_DELETE_WINDOW"); 233 wm_take_focus = XAtom.get("WM_TAKE_FOCUS"); 234 } 235 } 236 finally { 237 XToolkit.awtUnlock(); 238 } 239 winAttr = new XWindowAttributesData(); 240 savedState = XUtilConstants.WithdrawnState; 241 } 242 243 void postInit(XCreateWindowParams params) { 244 super.postInit(params); 245 246 setWMClass(getWMClass()); 247 248 surfaceData = graphicsConfig.createSurfaceData(this); 249 Color c; 250 if (target != null && (c = target.getBackground()) != null) { 251 // We need a version of setBackground that does not call repaint !! 252 // and one that does not get overridden. The problem is that in postInit 253 // we call setBackground and we don't have all the stuff initialized to 254 // do a full paint for most peers. So we cannot call setBackground in postInit. 255 // instead we need to call xSetBackground. 256 xSetBackground(c); 257 } 258 } 259 260 public GraphicsConfiguration getGraphicsConfiguration() { 261 if (graphicsConfig == null) { 262 initGraphicsConfiguration(); 263 } 264 return graphicsConfig; 265 } 266 267 public AwtGraphicsConfigData getGraphicsConfigurationData() { 268 if (graphicsConfigData == null) { 269 initGraphicsConfiguration(); 270 } 271 return graphicsConfigData; 272 } 273 274 protected String[] getWMClass() { 275 return new String[] {XToolkit.getCorrectXIDString(getClass().getName()), XToolkit.getAWTAppClassName()}; 276 } 277 278 void setReparented(boolean newValue) { 279 reparented = newValue; 280 } 281 282 boolean isReparented() { 283 return reparented; 284 } 285 286 static long getParentWindowID(Component target) { 287 288 ComponentPeer peer = target.getParent().getPeer(); 289 Component temp = target.getParent(); 290 while (!(peer instanceof XWindow)) 291 { 292 temp = temp.getParent(); 293 peer = temp.getPeer(); 294 } 295 296 if (peer != null && peer instanceof XWindow) 297 return ((XWindow)peer).getContentWindow(); 298 else return 0; 299 } 300 301 302 static XWindow getParentXWindowObject(Component target) { 303 if (target == null) return null; 304 Component temp = target.getParent(); 305 if (temp == null) return null; 306 ComponentPeer peer = temp.getPeer(); 307 if (peer == null) return null; 308 while ((peer != null) && !(peer instanceof XWindow)) 309 { 310 temp = temp.getParent(); 311 peer = temp.getPeer(); 312 } 313 if (peer != null && peer instanceof XWindow) 314 return (XWindow) peer; 315 else return null; 316 } 317 318 319 boolean isParentOf(XWindow win) { 320 if (!(target instanceof Container) || win == null || win.getTarget() == null) { 321 return false; 322 } 323 Container parent = AWTAccessor.getComponentAccessor().getParent(win.target); 324 while (parent != null && parent != target) { 325 parent = AWTAccessor.getComponentAccessor().getParent(parent); 326 } 327 return (parent == target); 328 } 329 330 public Object getTarget() { 331 return target; 332 } 333 public Component getEventSource() { 334 return target; 335 } 336 337 public ColorModel getColorModel(int transparency) { 338 return graphicsConfig.getColorModel (transparency); 339 } 340 341 public ColorModel getColorModel() { 342 if (graphicsConfig != null) { 343 return graphicsConfig.getColorModel (); 344 } 345 else { 346 return XToolkit.getStaticColorModel(); 347 } 348 } 349 350 Graphics getGraphics(SurfaceData surfData, Color afore, Color aback, Font afont) { 351 if (surfData == null) return null; 352 353 Component target = (Component) this.target; 354 355 /* Fix for bug 4746122. Color and Font shouldn't be null */ 356 Color bgColor = aback; 357 if (bgColor == null) { 358 bgColor = SystemColor.window; 359 } 360 Color fgColor = afore; 361 if (fgColor == null) { 362 fgColor = SystemColor.windowText; 363 } 364 Font font = afont; 365 if (font == null) { 366 font = XWindow.getDefaultFont(); 367 } 368 return new SunGraphics2D(surfData, fgColor, bgColor, font); 369 } 370 371 public Graphics getGraphics() { 372 return getGraphics(surfaceData, 373 target.getForeground(), 374 target.getBackground(), 375 target.getFont()); 376 } 377 378 public FontMetrics getFontMetrics(Font font) { 379 return Toolkit.getDefaultToolkit().getFontMetrics(font); 380 } 381 382 public Rectangle getTargetBounds() { 383 return target.getBounds(); 384 } 385 386 /** 387 * Returns true if the event has been handled and should not be 388 * posted to Java. 389 */ 390 boolean prePostEvent(AWTEvent e) { 391 return false; 392 } 393 394 static Method m_sendMessage; 395 static void sendEvent(final AWTEvent e) { 396 // The uses of this method imply that the incoming event is system-generated 397 SunToolkit.setSystemGenerated(e); 398 PeerEvent pe = new PeerEvent(Toolkit.getDefaultToolkit(), new Runnable() { 399 public void run() { 400 AWTAccessor.getAWTEventAccessor().setPosted(e); 401 ((Component)e.getSource()).dispatchEvent(e); 402 } 403 }, PeerEvent.ULTIMATE_PRIORITY_EVENT); 404 if (focusLog.isLoggable(PlatformLogger.Level.FINER) && (e instanceof FocusEvent)) { 405 focusLog.finer("Sending " + e); 406 } 407 XToolkit.postEvent(XToolkit.targetToAppContext(e.getSource()), pe); 408 } 409 410 411 /* 412 * Post an event to the event queue. 413 */ 414 // NOTE: This method may be called by privileged threads. 415 // DO NOT INVOKE CLIENT CODE ON THIS THREAD! 416 void postEvent(AWTEvent event) { 417 XToolkit.postEvent(XToolkit.targetToAppContext(event.getSource()), event); 418 } 419 420 static void postEventStatic(AWTEvent event) { 421 XToolkit.postEvent(XToolkit.targetToAppContext(event.getSource()), event); 422 } 423 424 public void postEventToEventQueue(final AWTEvent event) { 425 //fix for 6239938 : Choice drop-down does not disappear when it loses focus, on XToolkit 426 if (!prePostEvent(event)) { 427 //event hasn't been handled and must be posted to EventQueue 428 postEvent(event); 429 } 430 } 431 432 // overriden in XCanvasPeer 433 protected boolean doEraseBackground() { 434 return true; 435 } 436 437 // We need a version of setBackground that does not call repaint !! 438 // and one that does not get overridden. The problem is that in postInit 439 // we call setBackground and we don't have all the stuff initialized to 440 // do a full paint for most peers. So we cannot call setBackground in postInit. 441 final public void xSetBackground(Color c) { 442 XToolkit.awtLock(); 443 try { 444 winBackground(c); 445 // fix for 6558510: handle sun.awt.noerasebackground flag, 446 // see doEraseBackground() and preInit() methods in XCanvasPeer 447 if (!doEraseBackground()) { 448 return; 449 } 450 // 6304250: XAWT: Items in choice show a blue border on OpenGL + Solaris10 when background color is set 451 // Note: When OGL is enabled, surfaceData.pixelFor() will not 452 // return a pixel value appropriate for passing to 453 // XSetWindowBackground(). Therefore, we will use the ColorModel 454 // for this component in order to calculate a pixel value from 455 // the given RGB value. 456 ColorModel cm = getColorModel(); 457 int pixel = PixelConverter.instance.rgbToPixel(c.getRGB(), cm); 458 XlibWrapper.XSetWindowBackground(XToolkit.getDisplay(), getContentWindow(), pixel); 459 } 460 finally { 461 XToolkit.awtUnlock(); 462 } 463 } 464 465 public void setBackground(Color c) { 466 xSetBackground(c); 467 } 468 469 Color backgroundColor; 470 void winBackground(Color c) { 471 backgroundColor = c; 472 } 473 474 public Color getWinBackground() { 475 Color c = null; 476 477 if (backgroundColor != null) { 478 c = backgroundColor; 479 } else if (parent != null) { 480 c = parent.getWinBackground(); 481 } 482 483 if (c instanceof SystemColor) { 484 c = new Color(c.getRGB()); 485 } 486 487 return c; 488 } 489 490 public boolean isEmbedded() { 491 return embedded; 492 } 493 494 public final void repaint(int x, int y, int width, int height) { 495 if (!isVisible() || getWidth() == 0 || getHeight() == 0) { 496 return; 497 } 498 Graphics g = getGraphics(); 499 if (g != null) { 500 try { 501 g.setClip(x, y, width, height); 502 if (SunToolkit.isDispatchThreadForAppContext(getTarget())) { 503 paint(g); // The native and target will be painted in place. 504 } else { 505 paintPeer(g); 506 postPaintEvent(target, x, y, width, height); 507 } 508 } finally { 509 g.dispose(); 510 } 511 } 512 } 513 514 void repaint() { 515 repaint(0, 0, getWidth(), getHeight()); 516 } 517 518 public void paint(final Graphics g) { 519 // paint peer 520 paintPeer(g); 521 } 522 523 void paintPeer(final Graphics g) { 524 } 525 //used by Peers to avoid flickering withing paint() 526 protected void flush(){ 527 XToolkit.awtLock(); 528 try { 529 XlibWrapper.XFlush(XToolkit.getDisplay()); 530 } finally { 531 XToolkit.awtUnlock(); 532 } 533 } 534 535 public void popup(int x, int y, int width, int height) { 536 // TBD: grab the pointer 537 xSetBounds(x, y, width, height); 538 } 539 540 public void handleExposeEvent(XEvent xev) { 541 super.handleExposeEvent(xev); 542 XExposeEvent xe = xev.get_xexpose(); 543 if (isEventDisabled(xev)) { 544 return; 545 } 546 int x = xe.get_x(); 547 int y = xe.get_y(); 548 int w = xe.get_width(); 549 int h = xe.get_height(); 550 551 Component target = (Component)getEventSource(); 552 AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor(); 553 554 if (!compAccessor.getIgnoreRepaint(target) 555 && compAccessor.getWidth(target) != 0 556 && compAccessor.getHeight(target) != 0) 557 { 558 postPaintEvent(target, x, y, w, h); 559 } 560 } 561 562 public void postPaintEvent(Component target, int x, int y, int w, int h) { 563 PaintEvent event = PaintEventDispatcher.getPaintEventDispatcher(). 564 createPaintEvent(target, x, y, w, h); 565 if (event != null) { 566 postEventToEventQueue(event); 567 } 568 } 569 570 static int getModifiers(int state, int button, int keyCode) { 571 return getModifiers(state, button, keyCode, 0, false); 572 } 573 574 static int getModifiers(int state, int button, int keyCode, int type, boolean wheel_mouse) { 575 int modifiers = 0; 576 577 if (((state & XConstants.ShiftMask) != 0) ^ (keyCode == KeyEvent.VK_SHIFT)) { 578 modifiers |= InputEvent.SHIFT_DOWN_MASK; 579 } 580 if (((state & XConstants.ControlMask) != 0) ^ (keyCode == KeyEvent.VK_CONTROL)) { 581 modifiers |= InputEvent.CTRL_DOWN_MASK; 582 } 583 if (((state & XToolkit.metaMask) != 0) ^ (keyCode == KeyEvent.VK_META)) { 584 modifiers |= InputEvent.META_DOWN_MASK; 585 } 586 if (((state & XToolkit.altMask) != 0) ^ (keyCode == KeyEvent.VK_ALT)) { 587 modifiers |= InputEvent.ALT_DOWN_MASK; 588 } 589 if (((state & XToolkit.modeSwitchMask) != 0) ^ (keyCode == KeyEvent.VK_ALT_GRAPH)) { 590 modifiers |= InputEvent.ALT_GRAPH_DOWN_MASK; 591 } 592 //InputEvent.BUTTON_DOWN_MASK array is starting from BUTTON1_DOWN_MASK on index == 0. 593 // button currently reflects a real button number and starts from 1. (except NOBUTTON which is zero ) 594 595 /* this is an attempt to refactor button IDs in : MouseEvent, InputEvent, XlibWrapper and XWindow.*/ 596 597 //reflects a button number similar to MouseEvent.BUTTON1, 2, 3 etc. 598 for (int i = 0; i < XConstants.buttons.length; i ++){ 599 //modifier should be added if : 600 // 1) current button is now still in PRESSED state (means that user just pressed mouse but not released yet) or 601 // 2) if Xsystem reports that "state" represents that button was just released. This only happens on RELEASE with 1,2,3 buttons. 602 // ONLY one of these conditions should be TRUE to add that modifier. 603 if (((state & XlibUtil.getButtonMask(i + 1)) != 0) != (button == XConstants.buttons[i])){ 604 //exclude wheel buttons from adding their numbers as modifiers 605 if (!wheel_mouse) { 606 modifiers |= InputEvent.getMaskForButton(i+1); 607 } 608 } 609 } 610 return modifiers; 611 } 612 613 static int getXModifiers(AWTKeyStroke stroke) { 614 int mods = stroke.getModifiers(); 615 int res = 0; 616 if ((mods & (InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK)) != 0) { 617 res |= XConstants.ShiftMask; 618 } 619 if ((mods & (InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK)) != 0) { 620 res |= XConstants.ControlMask; 621 } 622 if ((mods & (InputEvent.ALT_DOWN_MASK | InputEvent.ALT_MASK)) != 0) { 623 res |= XToolkit.altMask; 624 } 625 if ((mods & (InputEvent.META_DOWN_MASK | InputEvent.META_MASK)) != 0) { 626 res |= XToolkit.metaMask; 627 } 628 if ((mods & (InputEvent.ALT_GRAPH_DOWN_MASK | InputEvent.ALT_GRAPH_MASK)) != 0) { 629 res |= XToolkit.modeSwitchMask; 630 } 631 return res; 632 } 633 634 /** 635 * Returns true if this event is disabled and shouldn't be passed to Java. 636 * Default implementation returns false for all events. 637 */ 638 static int getRightButtonNumber() { 639 if (rbutton == 0) { // not initialized yet 640 XToolkit.awtLock(); 641 try { 642 rbutton = XlibWrapper.XGetPointerMapping(XToolkit.getDisplay(), XlibWrapper.ibuffer, 3); 643 } 644 finally { 645 XToolkit.awtUnlock(); 646 } 647 } 648 return rbutton; 649 } 650 651 static int getMouseMovementSmudge() { 652 //TODO: It's possible to read corresponding settings 653 return AWT_MULTICLICK_SMUDGE; 654 } 655 656 public void handleButtonPressRelease(XEvent xev) { 657 super.handleButtonPressRelease(xev); 658 XButtonEvent xbe = xev.get_xbutton(); 659 if (isEventDisabled(xev)) { 660 return; 661 } 662 if (eventLog.isLoggable(PlatformLogger.Level.FINE)) { 663 eventLog.fine(xbe.toString()); 664 } 665 long when; 666 int modifiers; 667 boolean popupTrigger = false; 668 int button=0; 669 boolean wheel_mouse = false; 670 int lbutton = xbe.get_button(); 671 /* 672 * Ignore the buttons above 20 due to the bit limit for 673 * InputEvent.BUTTON_DOWN_MASK. 674 * One more bit is reserved for FIRST_HIGH_BIT. 675 */ 676 if (lbutton > SunToolkit.MAX_BUTTONS_SUPPORTED) { 677 return; 678 } 679 int type = xev.get_type(); 680 when = xbe.get_time(); 681 long jWhen = XToolkit.nowMillisUTC_offset(when); 682 683 int x = xbe.get_x(); 684 int y = xbe.get_y(); 685 if (xev.get_xany().get_window() != window) { 686 Point localXY = toLocal(xbe.get_x_root(), xbe.get_y_root()); 687 x = localXY.x; 688 y = localXY.y; 689 } 690 691 if (type == XConstants.ButtonPress) { 692 //Allow this mouse button to generate CLICK event on next ButtonRelease 693 mouseButtonClickAllowed |= XlibUtil.getButtonMask(lbutton); 694 XWindow lastWindow = (lastWindowRef != null) ? ((XWindow)lastWindowRef.get()):(null); 695 /* 696 multiclick checking 697 */ 698 if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) { 699 eventLog.finest("lastWindow = " + lastWindow + ", lastButton " 700 + lastButton + ", lastTime " + lastTime + ", multiClickTime " 701 + XToolkit.getMultiClickTime()); 702 } 703 if (lastWindow == this && lastButton == lbutton && (when - lastTime) < XToolkit.getMultiClickTime()) { 704 clickCount++; 705 } else { 706 clickCount = 1; 707 lastWindowRef = new WeakReference(this); 708 lastButton = lbutton; 709 lastX = x; 710 lastY = y; 711 } 712 lastTime = when; 713 714 715 /* 716 Check for popup trigger !! 717 */ 718 if (lbutton == getRightButtonNumber() || lbutton > 2) { 719 popupTrigger = true; 720 } else { 721 popupTrigger = false; 722 } 723 } 724 725 button = XConstants.buttons[lbutton - 1]; 726 // 4 and 5 buttons are usually considered assigned to a first wheel 727 if (lbutton == XConstants.buttons[3] || 728 lbutton == XConstants.buttons[4]) { 729 wheel_mouse = true; 730 } 731 732 // mapping extra buttons to numbers starting from 4. 733 if ((button > XConstants.buttons[4]) && (!Toolkit.getDefaultToolkit().areExtraMouseButtonsEnabled())){ 734 return; 735 } 736 737 if (button > XConstants.buttons[4]){ 738 button -= 2; 739 } 740 modifiers = getModifiers(xbe.get_state(),button,0, type, wheel_mouse); 741 742 if (!wheel_mouse) { 743 MouseEvent me = new MouseEvent((Component)getEventSource(), 744 type == XConstants.ButtonPress ? MouseEvent.MOUSE_PRESSED : MouseEvent.MOUSE_RELEASED, 745 jWhen,modifiers, x, y, 746 xbe.get_x_root(), 747 xbe.get_y_root(), 748 clickCount,popupTrigger,button); 749 750 postEventToEventQueue(me); 751 752 if ((type == XConstants.ButtonRelease) && 753 ((mouseButtonClickAllowed & XlibUtil.getButtonMask(lbutton)) != 0) ) // No up-button in the drag-state 754 { 755 postEventToEventQueue(me = new MouseEvent((Component)getEventSource(), 756 MouseEvent.MOUSE_CLICKED, 757 jWhen, 758 modifiers, 759 x, y, 760 xbe.get_x_root(), 761 xbe.get_y_root(), 762 clickCount, 763 false, button)); 764 } 765 766 } 767 else { 768 if (xev.get_type() == XConstants.ButtonPress) { 769 MouseWheelEvent mwe = new MouseWheelEvent((Component)getEventSource(),MouseEvent.MOUSE_WHEEL, jWhen, 770 modifiers, 771 x, y, 772 xbe.get_x_root(), 773 xbe.get_y_root(), 774 1,false,MouseWheelEvent.WHEEL_UNIT_SCROLL, 775 3,button==4 ? -1 : 1); 776 postEventToEventQueue(mwe); 777 } 778 } 779 780 /* Update the state variable AFTER the CLICKED event post. */ 781 if (type == XConstants.ButtonRelease) { 782 /* Exclude this mouse button from allowed list.*/ 783 mouseButtonClickAllowed &= ~ XlibUtil.getButtonMask(lbutton); 784 } 785 } 786 787 public void handleMotionNotify(XEvent xev) { 788 super.handleMotionNotify(xev); 789 XMotionEvent xme = xev.get_xmotion(); 790 if (isEventDisabled(xev)) { 791 return; 792 } 793 794 int mouseKeyState = 0; //(xme.get_state() & (XConstants.buttonsMask[0] | XConstants.buttonsMask[1] | XConstants.buttonsMask[2])); 795 796 //this doesn't work for extra buttons because Xsystem is sending state==0 for every extra button event. 797 // we can't correct it in MouseEvent class as we done it with modifiers, because exact type (DRAG|MOVE) 798 // should be passed from XWindow. 799 final int buttonsNumber = XToolkit.getNumberOfButtonsForMask(); 800 801 for (int i = 0; i < buttonsNumber; i++){ 802 // TODO : here is the bug in WM: extra buttons doesn't have state!=0 as they should. 803 if ((i != 4) && (i != 5)) { 804 mouseKeyState = mouseKeyState | (xme.get_state() & XlibUtil.getButtonMask(i + 1)); 805 } 806 } 807 808 boolean isDragging = (mouseKeyState != 0); 809 int mouseEventType = 0; 810 811 if (isDragging) { 812 mouseEventType = MouseEvent.MOUSE_DRAGGED; 813 } else { 814 mouseEventType = MouseEvent.MOUSE_MOVED; 815 } 816 817 /* 818 Fix for 6176814 . Add multiclick checking. 819 */ 820 int x = xme.get_x(); 821 int y = xme.get_y(); 822 XWindow lastWindow = (lastWindowRef != null) ? ((XWindow)lastWindowRef.get()):(null); 823 824 if (!(lastWindow == this && 825 (xme.get_time() - lastTime) < XToolkit.getMultiClickTime() && 826 (Math.abs(lastX - x) < AWT_MULTICLICK_SMUDGE && 827 Math.abs(lastY - y) < AWT_MULTICLICK_SMUDGE))) { 828 clickCount = 0; 829 lastWindowRef = null; 830 mouseButtonClickAllowed = 0; 831 lastTime = 0; 832 lastX = 0; 833 lastY = 0; 834 } 835 836 long jWhen = XToolkit.nowMillisUTC_offset(xme.get_time()); 837 int modifiers = getModifiers(xme.get_state(), 0, 0); 838 boolean popupTrigger = false; 839 840 Component source = (Component)getEventSource(); 841 842 if (xme.get_window() != window) { 843 Point localXY = toLocal(xme.get_x_root(), xme.get_y_root()); 844 x = localXY.x; 845 y = localXY.y; 846 } 847 /* Fix for 5039416. 848 * According to canvas.c we shouldn't post any MouseEvent if mouse is dragging and clickCount!=0. 849 */ 850 if ((isDragging && clickCount == 0) || !isDragging) { 851 MouseEvent mme = new MouseEvent(source, mouseEventType, jWhen, 852 modifiers, x, y, xme.get_x_root(), xme.get_y_root(), 853 clickCount, popupTrigger, MouseEvent.NOBUTTON); 854 postEventToEventQueue(mme); 855 } 856 } 857 858 859 // REMIND: need to implement looking for disabled events 860 public native boolean x11inputMethodLookupString(long event, long [] keysymArray); 861 native boolean haveCurrentX11InputMethodInstance(); 862 863 private boolean mouseAboveMe; 864 865 public boolean isMouseAbove() { 866 synchronized (getStateLock()) { 867 return mouseAboveMe; 868 } 869 } 870 protected void setMouseAbove(boolean above) { 871 synchronized (getStateLock()) { 872 mouseAboveMe = above; 873 } 874 } 875 876 protected void enterNotify(long window) { 877 if (window == getWindow()) { 878 setMouseAbove(true); 879 } 880 } 881 protected void leaveNotify(long window) { 882 if (window == getWindow()) { 883 setMouseAbove(false); 884 } 885 } 886 887 public void handleXCrossingEvent(XEvent xev) { 888 super.handleXCrossingEvent(xev); 889 XCrossingEvent xce = xev.get_xcrossing(); 890 891 if (eventLog.isLoggable(PlatformLogger.Level.FINEST)) { 892 eventLog.finest(xce.toString()); 893 } 894 895 if (xce.get_type() == XConstants.EnterNotify) { 896 enterNotify(xce.get_window()); 897 } else { // LeaveNotify: 898 leaveNotify(xce.get_window()); 899 } 900 901 // Skip event If it was caused by a grab 902 // This is needed because on displays with focus-follows-mouse on MousePress X system generates 903 // two XCrossing events with mode != NormalNotify. First of them notifies that the mouse has left 904 // current component. Second one notifies that it has entered into the same component. 905 // This looks like the window under the mouse has actually changed and Java handle these events 906 // accordingly. This leads to impossibility to make a double click on Component (6404708) 907 XWindowPeer toplevel = getToplevelXWindow(); 908 if (toplevel != null && !toplevel.isModalBlocked()){ 909 if (xce.get_mode() != XConstants.NotifyNormal) { 910 // 6404708 : need update cursor in accordance with skipping Leave/EnterNotify event 911 // whereas it doesn't need to handled further. 912 if (xce.get_type() == XConstants.EnterNotify) { 913 XAwtState.setComponentMouseEntered(getEventSource()); 914 XGlobalCursorManager.nativeUpdateCursor(getEventSource()); 915 } else { // LeaveNotify: 916 XAwtState.setComponentMouseEntered(null); 917 } 918 return; 919 } 920 } 921 // X sends XCrossing to all hierarchy so if the edge of child equals to 922 // ancestor and mouse enters child, the ancestor will get an event too. 923 // From java point the event is bogus as ancestor is obscured, so if 924 // the child can get java event itself, we skip it on ancestor. 925 long childWnd = xce.get_subwindow(); 926 if (childWnd != XConstants.None) { 927 XBaseWindow child = XToolkit.windowToXWindow(childWnd); 928 if (child != null && child instanceof XWindow && 929 !child.isEventDisabled(xev)) 930 { 931 return; 932 } 933 } 934 935 // Remember old component with mouse to have the opportunity to send it MOUSE_EXITED. 936 final Component compWithMouse = XAwtState.getComponentMouseEntered(); 937 if (toplevel != null) { 938 if(!toplevel.isModalBlocked()){ 939 if (xce.get_type() == XConstants.EnterNotify) { 940 // Change XAwtState's component mouse entered to the up-to-date one before requesting 941 // to update the cursor since XAwtState.getComponentMouseEntered() is used when the 942 // cursor is updated (in XGlobalCursorManager.findHeavyweightUnderCursor()). 943 XAwtState.setComponentMouseEntered(getEventSource()); 944 XGlobalCursorManager.nativeUpdateCursor(getEventSource()); 945 } else { // LeaveNotify: 946 XAwtState.setComponentMouseEntered(null); 947 } 948 } else { 949 ((XComponentPeer) AWTAccessor.getComponentAccessor().getPeer(target)) 950 .pSetCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); 951 } 952 } 953 954 if (isEventDisabled(xev)) { 955 return; 956 } 957 958 long jWhen = XToolkit.nowMillisUTC_offset(xce.get_time()); 959 int modifiers = getModifiers(xce.get_state(),0,0); 960 int clickCount = 0; 961 boolean popupTrigger = false; 962 int x = xce.get_x(); 963 int y = xce.get_y(); 964 if (xce.get_window() != window) { 965 Point localXY = toLocal(xce.get_x_root(), xce.get_y_root()); 966 x = localXY.x; 967 y = localXY.y; 968 } 969 970 // This code tracks boundary crossing and ensures MOUSE_ENTER/EXIT 971 // are posted in alternate pairs 972 if (compWithMouse != null) { 973 MouseEvent me = new MouseEvent(compWithMouse, 974 MouseEvent.MOUSE_EXITED, jWhen, modifiers, xce.get_x(), 975 xce.get_y(), xce.get_x_root(), xce.get_y_root(), clickCount, popupTrigger, 976 MouseEvent.NOBUTTON); 977 postEventToEventQueue(me); 978 eventLog.finest("Clearing last window ref"); 979 lastWindowRef = null; 980 } 981 if (xce.get_type() == XConstants.EnterNotify) { 982 MouseEvent me = new MouseEvent(getEventSource(), MouseEvent.MOUSE_ENTERED, 983 jWhen, modifiers, xce.get_x(), xce.get_y(), xce.get_x_root(), xce.get_y_root(), clickCount, 984 popupTrigger, MouseEvent.NOBUTTON); 985 postEventToEventQueue(me); 986 } 987 } 988 989 public void doLayout(int x, int y, int width, int height) {} 990 991 public void handleConfigureNotifyEvent(XEvent xev) { 992 Rectangle oldBounds = getBounds(); 993 994 super.handleConfigureNotifyEvent(xev); 995 if (insLog.isLoggable(PlatformLogger.Level.FINER)) { 996 insLog.finer("Configure, {0}, event disabled: {1}", 997 xev.get_xconfigure(), isEventDisabled(xev)); 998 } 999 if (isEventDisabled(xev)) { 1000 return; 1001 } 1002 1003 // if ( Check if it's a resize, a move, or a stacking order change ) 1004 // { 1005 Rectangle bounds = getBounds(); 1006 if (!bounds.getSize().equals(oldBounds.getSize())) { 1007 postEventToEventQueue(new ComponentEvent(getEventSource(), ComponentEvent.COMPONENT_RESIZED)); 1008 } 1009 if (!bounds.getLocation().equals(oldBounds.getLocation())) { 1010 postEventToEventQueue(new ComponentEvent(getEventSource(), ComponentEvent.COMPONENT_MOVED)); 1011 } 1012 // } 1013 } 1014 1015 public void handleMapNotifyEvent(XEvent xev) { 1016 super.handleMapNotifyEvent(xev); 1017 if (log.isLoggable(PlatformLogger.Level.FINE)) { 1018 log.fine("Mapped {0}", this); 1019 } 1020 if (isEventDisabled(xev)) { 1021 return; 1022 } 1023 ComponentEvent ce; 1024 1025 ce = new ComponentEvent(getEventSource(), ComponentEvent.COMPONENT_SHOWN); 1026 postEventToEventQueue(ce); 1027 } 1028 1029 public void handleUnmapNotifyEvent(XEvent xev) { 1030 super.handleUnmapNotifyEvent(xev); 1031 if (isEventDisabled(xev)) { 1032 return; 1033 } 1034 ComponentEvent ce; 1035 1036 ce = new ComponentEvent(target, ComponentEvent.COMPONENT_HIDDEN); 1037 postEventToEventQueue(ce); 1038 } 1039 1040 private void dumpKeysymArray(XKeyEvent ev) { 1041 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1042 keyEventLog.fine(" "+Long.toHexString(XlibWrapper.XKeycodeToKeysym(XToolkit.getDisplay(), ev.get_keycode(), 0))+ 1043 "\n "+Long.toHexString(XlibWrapper.XKeycodeToKeysym(XToolkit.getDisplay(), ev.get_keycode(), 1))+ 1044 "\n "+Long.toHexString(XlibWrapper.XKeycodeToKeysym(XToolkit.getDisplay(), ev.get_keycode(), 2))+ 1045 "\n "+Long.toHexString(XlibWrapper.XKeycodeToKeysym(XToolkit.getDisplay(), ev.get_keycode(), 3))); 1046 } 1047 } 1048 /** 1049 Return unicode character or 0 if no correspondent character found. 1050 Parameter is a keysym basically from keysymdef.h 1051 XXX: how about vendor keys? Is there some with Unicode value and not in the list? 1052 */ 1053 int keysymToUnicode( long keysym, int state ) { 1054 return XKeysym.convertKeysym( keysym, state ); 1055 } 1056 int keyEventType2Id( int xEventType ) { 1057 return xEventType == XConstants.KeyPress ? java.awt.event.KeyEvent.KEY_PRESSED : 1058 xEventType == XConstants.KeyRelease ? java.awt.event.KeyEvent.KEY_RELEASED : 0; 1059 } 1060 static private long xkeycodeToKeysym(XKeyEvent ev) { 1061 return XKeysym.getKeysym( ev ); 1062 } 1063 private long xkeycodeToPrimaryKeysym(XKeyEvent ev) { 1064 return XKeysym.xkeycode2primary_keysym( ev ); 1065 } 1066 static private int primaryUnicode2JavaKeycode(int uni) { 1067 return (uni > 0? sun.awt.ExtendedKeyCodes.getExtendedKeyCodeForChar(uni) : 0); 1068 //return (uni > 0? uni + 0x01000000 : 0); 1069 } 1070 void logIncomingKeyEvent(XKeyEvent ev) { 1071 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1072 keyEventLog.fine("--XWindow.java:handleKeyEvent:"+ev); 1073 } 1074 dumpKeysymArray(ev); 1075 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1076 keyEventLog.fine("XXXXXXXXXXXXXX javakeycode will be most probably:0x"+ Integer.toHexString(XKeysym.getJavaKeycodeOnly(ev))); 1077 } 1078 } 1079 public void handleKeyPress(XEvent xev) { 1080 super.handleKeyPress(xev); 1081 XKeyEvent ev = xev.get_xkey(); 1082 if (eventLog.isLoggable(PlatformLogger.Level.FINE)) { 1083 eventLog.fine(ev.toString()); 1084 } 1085 if (isEventDisabled(xev)) { 1086 return; 1087 } 1088 handleKeyPress(ev); 1089 } 1090 // called directly from this package, unlike handleKeyRelease. 1091 // un-final it if you need to override it in a subclass. 1092 final void handleKeyPress(XKeyEvent ev) { 1093 long keysym[] = new long[2]; 1094 int unicodeKey = 0; 1095 keysym[0] = XConstants.NoSymbol; 1096 1097 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1098 logIncomingKeyEvent( ev ); 1099 } 1100 if ( //TODO check if there's an active input method instance 1101 // without calling a native method. Is it necessary though? 1102 haveCurrentX11InputMethodInstance()) { 1103 if (x11inputMethodLookupString(ev.pData, keysym)) { 1104 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1105 keyEventLog.fine("--XWindow.java XIM did process event; return; dec keysym processed:"+(keysym[0])+ 1106 "; hex keysym processed:"+Long.toHexString(keysym[0]) 1107 ); 1108 } 1109 return; 1110 }else { 1111 unicodeKey = keysymToUnicode( keysym[0], ev.get_state() ); 1112 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1113 keyEventLog.fine("--XWindow.java XIM did NOT process event, hex keysym:"+Long.toHexString(keysym[0])+"\n"+ 1114 " unicode key:"+Integer.toHexString((int)unicodeKey)); 1115 } 1116 } 1117 }else { 1118 // No input method instance found. For example, there's a Java Input Method. 1119 // Produce do-it-yourself keysym and perhaps unicode character. 1120 keysym[0] = xkeycodeToKeysym(ev); 1121 unicodeKey = keysymToUnicode( keysym[0], ev.get_state() ); 1122 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1123 keyEventLog.fine("--XWindow.java XIM is absent; hex keysym:"+Long.toHexString(keysym[0])+"\n"+ 1124 " unicode key:"+Integer.toHexString((int)unicodeKey)); 1125 } 1126 } 1127 // Keysym should be converted to Unicode, if possible and necessary, 1128 // and Java KeyEvent keycode should be calculated. 1129 // For press we should post pressed & typed Java events. 1130 // 1131 // Press event might be not processed to this time because 1132 // (1) either XIM could not handle it or 1133 // (2) it was Latin 1:1 mapping. 1134 // 1135 // Preserve modifiers to get Java key code for dead keys 1136 boolean isDeadKey = isDeadKey(keysym[0]); 1137 XKeysym.Keysym2JavaKeycode jkc = isDeadKey ? XKeysym.getJavaKeycode(keysym[0]) 1138 : XKeysym.getJavaKeycode(ev); 1139 if( jkc == null ) { 1140 jkc = new XKeysym.Keysym2JavaKeycode(java.awt.event.KeyEvent.VK_UNDEFINED, java.awt.event.KeyEvent.KEY_LOCATION_UNKNOWN); 1141 } 1142 1143 // Take the first keysym from a keysym array associated with the XKeyevent 1144 // and convert it to Unicode. Then, even if a Java keycode for the keystroke 1145 // is undefined, we still have a guess of what has been engraved on a keytop. 1146 int unicodeFromPrimaryKeysym = keysymToUnicode( xkeycodeToPrimaryKeysym(ev) ,0); 1147 1148 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1149 keyEventLog.fine(">>>Fire Event:"+ 1150 (ev.get_type() == XConstants.KeyPress ? "KEY_PRESSED; " : "KEY_RELEASED; ")+ 1151 "jkeycode:decimal="+jkc.getJavaKeycode()+ 1152 ", hex=0x"+Integer.toHexString(jkc.getJavaKeycode())+"; "+ 1153 " legacy jkeycode: decimal="+XKeysym.getLegacyJavaKeycodeOnly(ev)+ 1154 ", hex=0x"+Integer.toHexString(XKeysym.getLegacyJavaKeycodeOnly(ev))+"; " 1155 ); 1156 } 1157 1158 int jkeyToReturn = XKeysym.getLegacyJavaKeycodeOnly(ev); // someway backward compatible 1159 int jkeyExtended = jkc.getJavaKeycode() == java.awt.event.KeyEvent.VK_UNDEFINED ? 1160 primaryUnicode2JavaKeycode( unicodeFromPrimaryKeysym ) : 1161 jkc.getJavaKeycode(); 1162 postKeyEvent( java.awt.event.KeyEvent.KEY_PRESSED, 1163 ev.get_time(), 1164 isDeadKey ? jkeyExtended : jkeyToReturn, 1165 (unicodeKey == 0 ? java.awt.event.KeyEvent.CHAR_UNDEFINED : unicodeKey), 1166 jkc.getKeyLocation(), 1167 ev.get_state(),ev.getPData(), XKeyEvent.getSize(), (long)(ev.get_keycode()), 1168 unicodeFromPrimaryKeysym, 1169 jkeyExtended); 1170 1171 1172 if (unicodeKey > 0 && !isDeadKey) { 1173 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1174 keyEventLog.fine("fire _TYPED on "+unicodeKey); 1175 } 1176 postKeyEvent( java.awt.event.KeyEvent.KEY_TYPED, 1177 ev.get_time(), 1178 java.awt.event.KeyEvent.VK_UNDEFINED, 1179 unicodeKey, 1180 java.awt.event.KeyEvent.KEY_LOCATION_UNKNOWN, 1181 ev.get_state(),ev.getPData(), XKeyEvent.getSize(), (long)0, 1182 unicodeFromPrimaryKeysym, 1183 java.awt.event.KeyEvent.VK_UNDEFINED); 1184 1185 } 1186 1187 1188 } 1189 1190 public void handleKeyRelease(XEvent xev) { 1191 super.handleKeyRelease(xev); 1192 XKeyEvent ev = xev.get_xkey(); 1193 if (eventLog.isLoggable(PlatformLogger.Level.FINE)) { 1194 eventLog.fine(ev.toString()); 1195 } 1196 if (isEventDisabled(xev)) { 1197 return; 1198 } 1199 handleKeyRelease(ev); 1200 } 1201 // un-private it if you need to call it from elsewhere 1202 private void handleKeyRelease(XKeyEvent ev) { 1203 int unicodeKey = 0; 1204 1205 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1206 logIncomingKeyEvent( ev ); 1207 } 1208 // Keysym should be converted to Unicode, if possible and necessary, 1209 // and Java KeyEvent keycode should be calculated. 1210 // For release we should post released event. 1211 // 1212 // Preserve modifiers to get Java key code for dead keys 1213 long keysym = xkeycodeToKeysym(ev); 1214 boolean isDeadKey = isDeadKey(keysym); 1215 XKeysym.Keysym2JavaKeycode jkc = isDeadKey ? XKeysym.getJavaKeycode(keysym) 1216 : XKeysym.getJavaKeycode(ev); 1217 if( jkc == null ) { 1218 jkc = new XKeysym.Keysym2JavaKeycode(java.awt.event.KeyEvent.VK_UNDEFINED, java.awt.event.KeyEvent.KEY_LOCATION_UNKNOWN); 1219 } 1220 if (keyEventLog.isLoggable(PlatformLogger.Level.FINE)) { 1221 keyEventLog.fine(">>>Fire Event:"+ 1222 (ev.get_type() == XConstants.KeyPress ? "KEY_PRESSED; " : "KEY_RELEASED; ")+ 1223 "jkeycode:decimal="+jkc.getJavaKeycode()+ 1224 ", hex=0x"+Integer.toHexString(jkc.getJavaKeycode())+"; "+ 1225 " legacy jkeycode: decimal="+XKeysym.getLegacyJavaKeycodeOnly(ev)+ 1226 ", hex=0x"+Integer.toHexString(XKeysym.getLegacyJavaKeycodeOnly(ev))+"; " 1227 ); 1228 } 1229 // We obtain keysym from IM and derive unicodeKey from it for KeyPress only. 1230 // We used to cache that value and retrieve it on KeyRelease, 1231 // but in case for example of a dead key+vowel pair, a vowel after a deadkey 1232 // might never be cached before. 1233 // Also, switching between keyboard layouts, we might cache a wrong letter. 1234 // That's why we use the same procedure as if there was no IM instance: do-it-yourself unicode. 1235 unicodeKey = keysymToUnicode( xkeycodeToKeysym(ev), ev.get_state() ); 1236 1237 // Take a first keysym from a keysym array associated with the XKeyevent 1238 // and convert it to Unicode. Then, even if Java keycode for the keystroke 1239 // is undefined, we still will have a guess of what was engraved on a keytop. 1240 int unicodeFromPrimaryKeysym = keysymToUnicode( xkeycodeToPrimaryKeysym(ev) ,0); 1241 1242 int jkeyToReturn = XKeysym.getLegacyJavaKeycodeOnly(ev); // someway backward compatible 1243 int jkeyExtended = jkc.getJavaKeycode() == java.awt.event.KeyEvent.VK_UNDEFINED ? 1244 primaryUnicode2JavaKeycode( unicodeFromPrimaryKeysym ) : 1245 jkc.getJavaKeycode(); 1246 postKeyEvent( java.awt.event.KeyEvent.KEY_RELEASED, 1247 ev.get_time(), 1248 isDeadKey ? jkeyExtended : jkeyToReturn, 1249 (unicodeKey == 0 ? java.awt.event.KeyEvent.CHAR_UNDEFINED : unicodeKey), 1250 jkc.getKeyLocation(), 1251 ev.get_state(),ev.getPData(), XKeyEvent.getSize(), (long)(ev.get_keycode()), 1252 unicodeFromPrimaryKeysym, 1253 jkeyExtended); 1254 1255 1256 } 1257 1258 1259 private boolean isDeadKey(long keysym){ 1260 return XKeySymConstants.XK_dead_grave <= keysym && keysym <= XKeySymConstants.XK_dead_semivoiced_sound; 1261 } 1262 1263 /* 1264 * XmNiconic and Map/UnmapNotify (that XmNiconic relies on) are 1265 * unreliable, since mapping changes can happen for a virtual desktop 1266 * switch or MacOS style shading that became quite popular under X as 1267 * well. Yes, it probably should not be this way, as it violates 1268 * ICCCM, but reality is that quite a lot of window managers abuse 1269 * mapping state. 1270 */ 1271 int getWMState() { 1272 if (stateChanged) { 1273 stateChanged = false; 1274 WindowPropertyGetter getter = 1275 new WindowPropertyGetter(window, XWM.XA_WM_STATE, 0, 1, false, 1276 XWM.XA_WM_STATE); 1277 try { 1278 int status = getter.execute(); 1279 if (status != XConstants.Success || getter.getData() == 0) { 1280 return savedState = XUtilConstants.WithdrawnState; 1281 } 1282 1283 if (getter.getActualType() != XWM.XA_WM_STATE.getAtom() && getter.getActualFormat() != 32) { 1284 return savedState = XUtilConstants.WithdrawnState; 1285 } 1286 savedState = (int)Native.getCard32(getter.getData()); 1287 } finally { 1288 getter.dispose(); 1289 } 1290 } 1291 return savedState; 1292 } 1293 1294 /** 1295 * Override this methods to get notifications when top-level window state changes. The state is 1296 * meant in terms of ICCCM: WithdrawnState, IconicState, NormalState 1297 */ 1298 protected void stateChanged(long time, int oldState, int newState) { 1299 } 1300 1301 @Override 1302 public void handlePropertyNotify(XEvent xev) { 1303 super.handlePropertyNotify(xev); 1304 XPropertyEvent ev = xev.get_xproperty(); 1305 if (ev.get_atom() == XWM.XA_WM_STATE.getAtom()) { 1306 // State has changed, invalidate saved value 1307 stateChanged = true; 1308 stateChanged(ev.get_time(), savedState, getWMState()); 1309 } 1310 } 1311 1312 public void reshape(Rectangle bounds) { 1313 reshape(bounds.x, bounds.y, bounds.width, bounds.height); 1314 } 1315 1316 public void reshape(int x, int y, int width, int height) { 1317 if (width <= 0) { 1318 width = 1; 1319 } 1320 if (height <= 0) { 1321 height = 1; 1322 } 1323 this.x = x; 1324 this.y = y; 1325 this.width = width; 1326 this.height = height; 1327 xSetBounds(x, y, width, height); 1328 // Fixed 6322593, 6304251, 6315137: 1329 // XWindow's SurfaceData should be invalidated and recreated as part 1330 // of the process of resizing the window 1331 // see the evaluation of the bug 6304251 for more information 1332 validateSurface(); 1333 layout(); 1334 } 1335 1336 public void layout() {} 1337 1338 boolean isShowing() { 1339 return visible; 1340 } 1341 1342 boolean isResizable() { 1343 return true; 1344 } 1345 1346 boolean isLocationByPlatform() { 1347 return false; 1348 } 1349 1350 void updateSizeHints() { 1351 updateSizeHints(x, y, width, height); 1352 } 1353 1354 void updateSizeHints(int x, int y, int width, int height) { 1355 long flags = XUtilConstants.PSize | (isLocationByPlatform() ? 0 : (XUtilConstants.PPosition | XUtilConstants.USPosition)); 1356 if (!isResizable()) { 1357 if (log.isLoggable(PlatformLogger.Level.FINER)) { 1358 log.finer("Window {0} is not resizable", this); 1359 } 1360 flags |= XUtilConstants.PMinSize | XUtilConstants.PMaxSize; 1361 } else { 1362 if (log.isLoggable(PlatformLogger.Level.FINER)) { 1363 log.finer("Window {0} is resizable", this); 1364 } 1365 } 1366 setSizeHints(flags, x, y, width, height); 1367 } 1368 1369 void updateSizeHints(int x, int y) { 1370 long flags = isLocationByPlatform() ? 0 : (XUtilConstants.PPosition | XUtilConstants.USPosition); 1371 if (!isResizable()) { 1372 if (log.isLoggable(PlatformLogger.Level.FINER)) { 1373 log.finer("Window {0} is not resizable", this); 1374 } 1375 flags |= XUtilConstants.PMinSize | XUtilConstants.PMaxSize | XUtilConstants.PSize; 1376 } else { 1377 if (log.isLoggable(PlatformLogger.Level.FINER)) { 1378 log.finer("Window {0} is resizable", this); 1379 } 1380 } 1381 setSizeHints(flags, x, y, width, height); 1382 } 1383 1384 void validateSurface() { 1385 if ((width != oldWidth) || (height != oldHeight)) { 1386 doValidateSurface(); 1387 1388 oldWidth = width; 1389 oldHeight = height; 1390 } 1391 } 1392 1393 final void doValidateSurface() { 1394 SurfaceData oldData = surfaceData; 1395 if (oldData != null) { 1396 surfaceData = graphicsConfig.createSurfaceData(this); 1397 oldData.invalidate(); 1398 } 1399 } 1400 1401 public SurfaceData getSurfaceData() { 1402 return surfaceData; 1403 } 1404 1405 public void dispose() { 1406 SurfaceData oldData = surfaceData; 1407 surfaceData = null; 1408 if (oldData != null) { 1409 oldData.invalidate(); 1410 } 1411 XToolkit.targetDisposedPeer(target, this); 1412 destroy(); 1413 } 1414 1415 public Point getLocationOnScreen() { 1416 synchronized (target.getTreeLock()) { 1417 Component comp = target; 1418 1419 while (comp != null && !(comp instanceof Window)) { 1420 comp = AWTAccessor.getComponentAccessor().getParent(comp); 1421 } 1422 1423 // applets, embedded, etc - translate directly 1424 // XXX: override in subclass? 1425 if (comp == null || comp instanceof sun.awt.EmbeddedFrame) { 1426 return toGlobal(0, 0); 1427 } 1428 1429 XToolkit.awtLock(); 1430 try { 1431 Object wpeer = XToolkit.targetToPeer(comp); 1432 if (wpeer == null 1433 || !(wpeer instanceof XDecoratedPeer) 1434 || ((XDecoratedPeer)wpeer).configure_seen) 1435 { 1436 return toGlobal(0, 0); 1437 } 1438 1439 // wpeer is an XDecoratedPeer not yet fully adopted by WM 1440 Point pt = toOtherWindow(getContentWindow(), 1441 ((XDecoratedPeer)wpeer).getContentWindow(), 1442 0, 0); 1443 1444 if (pt == null) { 1445 pt = new Point(((XBaseWindow)wpeer).getAbsoluteX(), ((XBaseWindow)wpeer).getAbsoluteY()); 1446 } 1447 pt.x += comp.getX(); 1448 pt.y += comp.getY(); 1449 return pt; 1450 } finally { 1451 XToolkit.awtUnlock(); 1452 } 1453 } 1454 } 1455 1456 1457 static void setBData(KeyEvent e, byte[] data) { 1458 AWTAccessor.getAWTEventAccessor().setBData(e, data); 1459 } 1460 1461 public void postKeyEvent(int id, long when, int keyCode, int keyChar, 1462 int keyLocation, int state, long event, int eventSize, long rawCode, 1463 int unicodeFromPrimaryKeysym, int extendedKeyCode) 1464 1465 { 1466 long jWhen = XToolkit.nowMillisUTC_offset(when); 1467 int modifiers = getModifiers(state, 0, keyCode); 1468 1469 KeyEvent ke = new KeyEvent((Component)getEventSource(), id, jWhen, 1470 modifiers, keyCode, (char)keyChar, keyLocation); 1471 if (event != 0) { 1472 byte[] data = Native.toBytes(event, eventSize); 1473 setBData(ke, data); 1474 } 1475 1476 AWTAccessor.KeyEventAccessor kea = AWTAccessor.getKeyEventAccessor(); 1477 kea.setRawCode(ke, rawCode); 1478 kea.setPrimaryLevelUnicode(ke, (long)unicodeFromPrimaryKeysym); 1479 kea.setExtendedKeyCode(ke, (long)extendedKeyCode); 1480 postEventToEventQueue(ke); 1481 } 1482 1483 static native int getAWTKeyCodeForKeySym(int keysym); 1484 static native int getKeySymForAWTKeyCode(int keycode); 1485 1486 /* These two methods are actually applicable to toplevel windows only. 1487 * However, the functionality is required by both the XWindowPeer and 1488 * XWarningWindow, both of which have the XWindow as a common ancestor. 1489 * See XWM.setMotifDecor() for details. 1490 */ 1491 public PropMwmHints getMWMHints() { 1492 if (mwm_hints == null) { 1493 mwm_hints = new PropMwmHints(); 1494 if (!XWM.XA_MWM_HINTS.getAtomData(getWindow(), mwm_hints.pData, MWMConstants.PROP_MWM_HINTS_ELEMENTS)) { 1495 mwm_hints.zero(); 1496 } 1497 } 1498 return mwm_hints; 1499 } 1500 1501 public void setMWMHints(PropMwmHints hints) { 1502 mwm_hints = hints; 1503 if (hints != null) { 1504 XWM.XA_MWM_HINTS.setAtomData(getWindow(), mwm_hints.pData, MWMConstants.PROP_MWM_HINTS_ELEMENTS); 1505 } 1506 } 1507 1508 protected final void initWMProtocols() { 1509 wm_protocols.setAtomListProperty(this, getWMProtocols()); 1510 } 1511 1512 /** 1513 * Returns list of protocols which should be installed on this window. 1514 * Descendants can override this method to add class-specific protocols 1515 */ 1516 protected XAtomList getWMProtocols() { 1517 // No protocols on simple window 1518 return new XAtomList(); 1519 } 1520 1521 /** 1522 * Indicates if the window is currently in the FSEM. 1523 * Synchronization: state lock. 1524 */ 1525 private boolean fullScreenExclusiveModeState = false; 1526 1527 // Implementation of the X11ComponentPeer 1528 @Override 1529 public void setFullScreenExclusiveModeState(boolean state) { 1530 synchronized (getStateLock()) { 1531 fullScreenExclusiveModeState = state; 1532 } 1533 } 1534 1535 public final boolean isFullScreenExclusiveMode() { 1536 synchronized (getStateLock()) { 1537 return fullScreenExclusiveModeState; 1538 } 1539 } 1540 1541 }