1 /*
   2  * Copyright (c) 2002, 2016, 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 package sun.awt.X11;
  26 
  27 import java.awt.*;
  28 
  29 import java.awt.event.ComponentEvent;
  30 import java.awt.event.InvocationEvent;
  31 import java.awt.event.WindowEvent;
  32 import java.util.Collections;
  33 import java.util.HashMap;
  34 import java.util.Map;
  35 
  36 import sun.awt.IconInfo;
  37 import sun.util.logging.PlatformLogger;
  38 
  39 import sun.awt.AWTAccessor;
  40 import sun.awt.SunToolkit;
  41 
  42 abstract class XDecoratedPeer extends XWindowPeer {
  43     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XDecoratedPeer");
  44     private static final PlatformLogger insLog = PlatformLogger.getLogger("sun.awt.X11.insets.XDecoratedPeer");
  45     private static final PlatformLogger focusLog = PlatformLogger.getLogger("sun.awt.X11.focus.XDecoratedPeer");
  46     private static final PlatformLogger iconLog = PlatformLogger.getLogger("sun.awt.X11.icon.XDecoratedPeer");
  47 
  48     // Set to true when we get the first ConfigureNotify after being
  49     // reparented - indicates that WM has adopted the top-level.
  50     boolean configure_seen;
  51     boolean insets_corrected;
  52 
  53     XIconWindow iconWindow;
  54     WindowDimensions dimensions;
  55     XContentWindow content;
  56     Insets currentInsets;
  57     XFocusProxyWindow focusProxy;
  58     static final Map<Class<?>,Insets> lastKnownInsets =
  59                                    Collections.synchronizedMap(new HashMap<>());
  60 
  61     XDecoratedPeer(Window target) {
  62         super(target);
  63     }
  64 
  65     XDecoratedPeer(XCreateWindowParams params) {
  66         super(params);
  67     }
  68 
  69     public long getShell() {
  70         return window;
  71     }
  72 
  73     public long getContentWindow() {
  74         return (content == null) ? window : content.getWindow();
  75     }
  76 
  77     void preInit(XCreateWindowParams params) {
  78         super.preInit(params);
  79         winAttr.initialFocus = true;
  80 
  81         currentInsets = new Insets(0,0,0,0);
  82         if (XWM.getWMID() == XWM.UNITY_COMPIZ_WM) {
  83             currentInsets = lastKnownInsets.get(getClass());
  84         }
  85         applyGuessedInsets();
  86 
  87         Rectangle bounds = (Rectangle)params.get(BOUNDS);
  88         dimensions = new WindowDimensions(bounds, getRealInsets(), false);
  89         params.put(BOUNDS, dimensions.getClientRect());
  90         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
  91             insLog.fine("Initial dimensions {0}", dimensions);
  92         }
  93 
  94         // Deny default processing of these events on the shell - proxy will take care of
  95         // them instead
  96         Long eventMask = (Long)params.get(EVENT_MASK);
  97         params.add(EVENT_MASK, Long.valueOf(eventMask.longValue() & ~(XConstants.FocusChangeMask | XConstants.KeyPressMask | XConstants.KeyReleaseMask)));
  98     }
  99 
 100     void postInit(XCreateWindowParams params) {
 101         // The size hints must be set BEFORE mapping the window (see 6895647)
 102         updateSizeHints(dimensions);
 103 
 104         // The super method maps the window if it's visible on the shared level
 105         super.postInit(params);
 106 
 107         // The lines that follow need to be in a postInit, so they
 108         // happen after the X window is created.
 109         initResizability();
 110         XWM.requestWMExtents(getWindow());
 111 
 112         content = XContentWindow.createContent(this);
 113 
 114         if (warningWindow != null) {
 115             warningWindow.toFront();
 116         }
 117         focusProxy = createFocusProxy();
 118     }
 119 
 120     void setIconHints(java.util.List<IconInfo> icons) {
 121         if (!XWM.getWM().setNetWMIcon(this, icons)) {
 122             if (icons.size() > 0) {
 123                 if (iconWindow == null) {
 124                     iconWindow = new XIconWindow(this);
 125                 }
 126                 iconWindow.setIconImages(icons);
 127             }
 128         }
 129     }
 130 
 131     public void updateMinimumSize() {
 132         super.updateMinimumSize();
 133         updateMinSizeHints();
 134     }
 135 
 136     private void updateMinSizeHints() {
 137         if (isResizable()) {
 138             Dimension minimumSize = getTargetMinimumSize();
 139             if (minimumSize != null) {
 140                 Insets insets = getRealInsets();
 141                 int minWidth = minimumSize.width - insets.left - insets.right;
 142                 int minHeight = minimumSize.height - insets.top - insets.bottom;
 143                 if (minWidth < 0) minWidth = 0;
 144                 if (minHeight < 0) minHeight = 0;
 145                 setSizeHints(XUtilConstants.PMinSize | (isLocationByPlatform()?0:(XUtilConstants.PPosition | XUtilConstants.USPosition)),
 146                              getX(), getY(), minWidth, minHeight);
 147                 if (isVisible()) {
 148                     Rectangle bounds = getShellBounds();
 149                     int nw = (bounds.width < minWidth) ? minWidth : bounds.width;
 150                     int nh = (bounds.height < minHeight) ? minHeight : bounds.height;
 151                     if (nw != bounds.width || nh != bounds.height) {
 152                         setShellSize(new Rectangle(0, 0, nw, nh));
 153                     }
 154                 }
 155             } else {
 156                 boolean isMinSizeSet = isMinSizeSet();
 157                 XWM.removeSizeHints(this, XUtilConstants.PMinSize);
 158                 /* Some WMs need remap to redecorate the window */
 159                 if (isMinSizeSet && isShowing() && XWM.needRemap(this)) {
 160                     /*
 161                      * Do the re/mapping at the Xlib level.  Since we essentially
 162                      * work around a WM bug we don't want this hack to be exposed
 163                      * to Intrinsics (i.e. don't mess with grabs, callbacks etc).
 164                      */
 165                     xSetVisible(false);
 166                     XToolkit.XSync();
 167                     xSetVisible(true);
 168                 }
 169             }
 170         }
 171     }
 172 
 173     XFocusProxyWindow createFocusProxy() {
 174         return new XFocusProxyWindow(this);
 175     }
 176 
 177     protected XAtomList getWMProtocols() {
 178         XAtomList protocols = super.getWMProtocols();
 179         protocols.add(wm_delete_window);
 180         protocols.add(wm_take_focus);
 181         return protocols;
 182     }
 183 
 184     public Graphics getGraphics() {
 185         AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
 186         return getGraphics(content.surfaceData,
 187                            compAccessor.getForeground(target),
 188                            compAccessor.getBackground(target),
 189                            compAccessor.getFont(target));
 190     }
 191 
 192     public void setTitle(String title) {
 193         if (log.isLoggable(PlatformLogger.Level.FINE)) {
 194             log.fine("Title is " + title);
 195         }
 196         winAttr.title = title;
 197         updateWMName();
 198     }
 199 
 200     protected String getWMName() {
 201         if (winAttr.title == null || winAttr.title.trim().equals("")) {
 202             return " ";
 203         } else {
 204             return winAttr.title;
 205         }
 206     }
 207 
 208     void updateWMName() {
 209         super.updateWMName();
 210         String name = getWMName();
 211         XToolkit.awtLock();
 212         try {
 213             if (name == null || name.trim().equals("")) {
 214                 name = "Java";
 215             }
 216             XAtom iconNameAtom = XAtom.get(XAtom.XA_WM_ICON_NAME);
 217             iconNameAtom.setProperty(getWindow(), name);
 218             XAtom netIconNameAtom = XAtom.get("_NET_WM_ICON_NAME");
 219             netIconNameAtom.setPropertyUTF8(getWindow(), name);
 220         } finally {
 221             XToolkit.awtUnlock();
 222         }
 223     }
 224 
 225     // NOTE: This method may be called by privileged threads.
 226     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 227     public void handleIconify() {
 228         postEvent(new WindowEvent((Window)target, WindowEvent.WINDOW_ICONIFIED));
 229     }
 230 
 231     // NOTE: This method may be called by privileged threads.
 232     //       DO NOT INVOKE CLIENT CODE ON THIS THREAD!
 233     public void handleDeiconify() {
 234         postEvent(new WindowEvent((Window)target, WindowEvent.WINDOW_DEICONIFIED));
 235     }
 236 
 237     public void handleFocusEvent(XEvent xev) {
 238         super.handleFocusEvent(xev);
 239         XFocusChangeEvent xfe = xev.get_xfocus();
 240 
 241         // If we somehow received focus events forward it instead to proxy
 242         // FIXME: Shouldn't we instead check for inferrior?
 243         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
 244             focusLog.finer("Received focus event on shell: " + xfe);
 245         }
 246 //         focusProxy.xRequestFocus();
 247    }
 248 
 249 /***************************************************************************************
 250  *                             I N S E T S   C O D E
 251  **************************************************************************************/
 252 
 253     protected boolean isInitialReshape() {
 254         return false;
 255     }
 256 
 257     private static Insets difference(Insets i1, Insets i2) {
 258         return new Insets(i1.top-i2.top, i1.left - i2.left, i1.bottom-i2.bottom, i1.right-i2.right);
 259     }
 260 
 261     private static boolean isNull(Insets i) {
 262         return (i == null) || ((i.left | i.top | i.right | i.bottom) == 0);
 263     }
 264 
 265     private static Insets copy(Insets i) {
 266         return new Insets(i.top, i.left, i.bottom, i.right);
 267     }
 268 
 269     // insets which we get from WM (e.g from _NET_FRAME_EXTENTS)
 270     private Insets wm_set_insets;
 271 
 272     private Insets getWMSetInsets(XAtom changedAtom) {
 273         if (isEmbedded()) {
 274             return null;
 275         }
 276 
 277         if (wm_set_insets != null) {
 278             return wm_set_insets;
 279         }
 280 
 281         if (changedAtom == null) {
 282             wm_set_insets = XWM.getInsetsFromExtents(getWindow());
 283         } else {
 284             wm_set_insets = XWM.getInsetsFromProp(getWindow(), changedAtom);
 285         }
 286 
 287         if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
 288             insLog.finer("FRAME_EXTENTS: {0}", wm_set_insets);
 289         }
 290 
 291         if (wm_set_insets != null) {
 292             wm_set_insets = copy(wm_set_insets);
 293         }
 294         return wm_set_insets;
 295     }
 296 
 297     private void resetWMSetInsets() {
 298         wm_set_insets = null;
 299     }
 300 
 301     public void handlePropertyNotify(XEvent xev) {
 302         super.handlePropertyNotify(xev);
 303 
 304         XPropertyEvent ev = xev.get_xproperty();
 305         if (ev.get_atom() == XWM.XA_KDE_NET_WM_FRAME_STRUT.getAtom()
 306             || ev.get_atom() == XWM.XA_NET_FRAME_EXTENTS.getAtom())
 307         {
 308             if (XWM.getWMID() != XWM.UNITY_COMPIZ_WM) {
 309                 getWMSetInsets(XAtom.get(ev.get_atom()));
 310             } else {
 311                 if(!isReparented()) {
 312                     return;
 313                 }
 314                 wm_set_insets = null;
 315                 Insets in = getWMSetInsets(XAtom.get(ev.get_atom()));
 316                 if (isNull(in)) {
 317                     return;
 318                 }
 319                 if (!isEmbedded() && !isTargetUndecorated()) {
 320                     lastKnownInsets.put(getClass(), in);
 321                 }
 322                 if (!in.equals(dimensions.getInsets())) {
 323                     handleCorrectInsets(in);
 324                 }
 325                 insets_corrected = true;
 326             }
 327         }
 328     }
 329 
 330     long reparent_serial = 0;
 331 
 332     public void handleReparentNotifyEvent(XEvent xev) {
 333         XReparentEvent  xe = xev.get_xreparent();
 334         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 335             insLog.fine(xe.toString());
 336         }
 337         reparent_serial = xe.get_serial();
 338         XToolkit.awtLock();
 339         try {
 340             long root = XlibWrapper.RootWindow(XToolkit.getDisplay(), getScreenNumber());
 341 
 342             if (isEmbedded()) {
 343                 setReparented(true);
 344                 insets_corrected = true;
 345                 return;
 346             }
 347             Component t = target;
 348             if (getDecorations() == XWindowAttributesData.AWT_DECOR_NONE) {
 349                 setReparented(true);
 350                 insets_corrected = true;
 351                 reshape(dimensions, SET_SIZE, false);
 352             } else if (xe.get_parent() == root) {
 353                 configure_seen = false;
 354                 insets_corrected = false;
 355 
 356                 /*
 357                  * We can be repareted to root for two reasons:
 358                  *   . setVisible(false)
 359                  *   . WM exited
 360                  */
 361                 if (isVisible()) { /* WM exited */
 362                     /* Work around 4775545 */
 363                     XWM.getWM().unshadeKludge(this);
 364                     insLog.fine("- WM exited");
 365                 } else {
 366                     insLog.fine(" - reparent due to hide");
 367                 }
 368             } else { /* reparented to WM frame, figure out our insets */
 369                 setReparented(true);
 370                 insets_corrected = false;
 371 
 372                 // Check if we have insets provided by the WM
 373                 Insets correctWM = getWMSetInsets(null);
 374                 if (correctWM != null) {
 375                     if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
 376                         insLog.finer("wm-provided insets {0}", correctWM);
 377                     }
 378                     // If these insets are equal to our current insets - no actions are necessary
 379                     Insets dimInsets = dimensions.getInsets();
 380                     if (correctWM.equals(dimInsets)) {
 381                         insLog.finer("Insets are the same as estimated - no additional reshapes necessary");
 382                         no_reparent_artifacts = true;
 383                         insets_corrected = true;
 384                         applyGuessedInsets();
 385                         return;
 386                     }
 387                 } else {
 388                     correctWM = XWM.getWM().getInsets(this, xe.get_window(), xe.get_parent());
 389 
 390                     if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
 391                         if (correctWM != null) {
 392                             insLog.finer("correctWM {0}", correctWM);
 393                         } else {
 394                             insLog.finer("correctWM insets are not available, waiting for configureNotify");
 395                         }
 396                     }
 397                 }
 398 
 399                 if (correctWM != null && XWM.getWMID() != XWM.UNITY_COMPIZ_WM) {
 400                     handleCorrectInsets(correctWM);
 401                 }
 402             }
 403         } finally {
 404             XToolkit.awtUnlock();
 405         }
 406     }
 407 
 408     protected void handleCorrectInsets(Insets correctWM) {
 409         XToolkit.awtLock();
 410         try {
 411             /*
 412              * Ok, now see if we need adjust window size because
 413              * initial insets were wrong (most likely they were).
 414              */
 415             Insets correction = difference(correctWM, currentInsets);
 416             if (insLog.isLoggable(PlatformLogger.Level.FINEST)) {
 417                 insLog.finest("Corrention {0}", correction);
 418             }
 419             if (!isNull(correction)) {
 420                 currentInsets = copy(correctWM);
 421                 applyGuessedInsets();
 422 
 423                 //Fix for 6318109: PIT: Min Size is not honored properly when a
 424                 //smaller size is specified in setSize(), XToolkit
 425                 //update minimum size hints
 426                 updateMinSizeHints();
 427             }
 428             if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
 429                 insLog.finer("Dimensions before reparent: " + dimensions);
 430             }
 431 
 432             dimensions.setInsets(getRealInsets());
 433             insets_corrected = true;
 434 
 435             if (isMaximized()) {
 436                 return;
 437             }
 438 
 439             /*
 440              * If this window has been sized by a pack() we need
 441              * to keep the interior geometry intact.  Since pack()
 442              * computed width and height with wrong insets, we
 443              * must adjust the target dimensions appropriately.
 444              */
 445             if ((getHints().get_flags() & (XUtilConstants.USPosition | XUtilConstants.PPosition)) != 0) {
 446                 reshape(dimensions, SET_BOUNDS, false);
 447             } else {
 448                 reshape(dimensions, SET_SIZE, false);
 449             }
 450         } finally {
 451             XToolkit.awtUnlock();
 452         }
 453     }
 454 
 455     public void handleMoved(WindowDimensions dims) {
 456         Point loc = dims.getLocation();
 457         AWTAccessor.getComponentAccessor().setLocation(target, loc.x, loc.y);
 458         postEvent(new ComponentEvent(target, ComponentEvent.COMPONENT_MOVED));
 459     }
 460 
 461 
 462     protected Insets guessInsets() {
 463         if (isEmbedded() || isTargetUndecorated()) {
 464             return new Insets(0, 0, 0, 0);
 465         } else {
 466             if (!isNull(currentInsets)) {
 467                 /* insets were set on wdata by System Properties */
 468                 return copy(currentInsets);
 469             } else {
 470                 Insets res = getWMSetInsets(null);
 471                 if (res == null) {
 472                     res = XWM.getWM().guessInsets(this);
 473                 }
 474                 return res;
 475             }
 476         }
 477     }
 478 
 479     private void applyGuessedInsets() {
 480         Insets guessed = guessInsets();
 481         currentInsets = copy(guessed);
 482     }
 483 
 484     public void revalidate() {
 485         XToolkit.executeOnEventHandlerThread(target, new Runnable() {
 486                 public void run() {
 487                     target.invalidate();
 488                     target.validate();
 489                 }
 490             });
 491     }
 492 
 493     Insets getRealInsets() {
 494         if (isNull(currentInsets)) {
 495             applyGuessedInsets();
 496         }
 497         return currentInsets;
 498     }
 499 
 500     public Insets getInsets() {
 501         Insets in = copy(getRealInsets());
 502         in.top += getMenuBarHeight();
 503         if (insLog.isLoggable(PlatformLogger.Level.FINEST)) {
 504             insLog.finest("Get insets returns {0}", in);
 505         }
 506         return in;
 507     }
 508 
 509     boolean gravityBug() {
 510         return XWM.configureGravityBuggy();
 511     }
 512 
 513     // The height of area used to display current active input method
 514     int getInputMethodHeight() {
 515         return 0;
 516     }
 517 
 518     void updateSizeHints(WindowDimensions dims) {
 519         Rectangle rec = dims.getClientRect();
 520         checkShellRect(rec);
 521         updateSizeHints(rec.x, rec.y, rec.width, rec.height);
 522     }
 523 
 524     void updateSizeHints() {
 525         updateSizeHints(dimensions);
 526     }
 527 
 528     // Coordinates are that of the target
 529     // Called only on Toolkit thread
 530     public void reshape(WindowDimensions newDimensions, int op,
 531                         boolean userReshape)
 532     {
 533         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 534             insLog.fine("Reshaping " + this + " to " + newDimensions + " op " + op + " user reshape " + userReshape);
 535         }
 536         if (userReshape) {
 537             // We handle only userReshape == true cases. It means that
 538             // if the window manager or any other part of the windowing
 539             // system sets inappropriate size for this window, we can
 540             // do nothing but accept it.
 541             Rectangle newBounds = newDimensions.getBounds();
 542             Insets insets = newDimensions.getInsets();
 543             // Inherit isClientSizeSet from newDimensions
 544             if (newDimensions.isClientSizeSet()) {
 545                 newBounds = new Rectangle(newBounds.x, newBounds.y,
 546                                           newBounds.width - insets.left - insets.right,
 547                                           newBounds.height - insets.top - insets.bottom);
 548             }
 549             newDimensions = new WindowDimensions(newBounds, insets, newDimensions.isClientSizeSet());
 550         }
 551         XToolkit.awtLock();
 552         try {
 553             if (!isReparented() || !isVisible()) {
 554                 if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 555                     insLog.fine("- not reparented({0}) or not visible({1}), default reshape",
 556                            Boolean.valueOf(isReparented()), Boolean.valueOf(visible));
 557                 }
 558 
 559                 // Fix for 6323293.
 560                 // This actually is needed to preserve compatibility with previous releases -
 561                 // some of licensees are expecting componentMoved event on invisible one while
 562                 // its location changes.
 563                 Point oldLocation = getLocation();
 564 
 565                 Point newLocation = new Point(AWTAccessor.getComponentAccessor().getX(target),
 566                                               AWTAccessor.getComponentAccessor().getY(target));
 567 
 568                 if (!newLocation.equals(oldLocation)) {
 569                     handleMoved(newDimensions);
 570                 }
 571 
 572                 dimensions = new WindowDimensions(newDimensions);
 573                 updateSizeHints(dimensions);
 574                 Rectangle client = dimensions.getClientRect();
 575                 checkShellRect(client);
 576                 setShellBounds(client);
 577                 if (content != null &&
 578                     !content.getSize().equals(newDimensions.getSize()))
 579                 {
 580                     reconfigureContentWindow(newDimensions);
 581                 }
 582                 return;
 583             }
 584 
 585             int wm = XWM.getWMID();
 586             updateChildrenSizes();
 587             applyGuessedInsets();
 588 
 589             Rectangle shellRect = newDimensions.getClientRect();
 590 
 591             if (gravityBug()) {
 592                 Insets in = newDimensions.getInsets();
 593                 shellRect.translate(in.left, in.top);
 594             }
 595 
 596             if ((op & NO_EMBEDDED_CHECK) == 0 && isEmbedded()) {
 597                 shellRect.setLocation(0, 0);
 598             }
 599 
 600             checkShellRectSize(shellRect);
 601             if (!isEmbedded()) {
 602                 checkShellRectPos(shellRect);
 603             }
 604 
 605             op = op & ~NO_EMBEDDED_CHECK;
 606 
 607             if (op == SET_LOCATION) {
 608                 setShellPosition(shellRect);
 609             } else if (isResizable()) {
 610                 if (op == SET_BOUNDS) {
 611                     setShellBounds(shellRect);
 612                 } else {
 613                     setShellSize(shellRect);
 614                 }
 615             } else {
 616                 XWM.setShellNotResizable(this, newDimensions, shellRect, true);
 617                 if (op == SET_BOUNDS) {
 618                     setShellPosition(shellRect);
 619                 }
 620             }
 621 
 622             reconfigureContentWindow(newDimensions);
 623         } finally {
 624             XToolkit.awtUnlock();
 625         }
 626     }
 627 
 628     /**
 629      * @param x, y, width, heith - dimensions of the window with insets
 630      */
 631     private void reshape(int x, int y, int width, int height, int operation,
 632                          boolean userReshape)
 633     {
 634         Rectangle newRec;
 635         boolean setClient = false;
 636         WindowDimensions dims = new WindowDimensions(dimensions);
 637         switch (operation & (~NO_EMBEDDED_CHECK)) {
 638           case SET_LOCATION:
 639               // Set location always sets bounds location. However, until the window is mapped we
 640               // should use client coordinates
 641               dims.setLocation(x, y);
 642               break;
 643           case SET_SIZE:
 644               // Set size sets bounds size. However, until the window is mapped we
 645               // should use client coordinates
 646               dims.setSize(width, height);
 647               break;
 648           case SET_CLIENT_SIZE: {
 649               // Sets client rect size. Width and height contain insets.
 650               Insets in = currentInsets;
 651               width -= in.left+in.right;
 652               height -= in.top+in.bottom;
 653               dims.setClientSize(width, height);
 654               break;
 655           }
 656           case SET_BOUNDS:
 657           default:
 658               dims.setLocation(x, y);
 659               dims.setSize(width, height);
 660               break;
 661         }
 662         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 663             insLog.fine("For the operation {0} new dimensions are {1}",
 664                         operationToString(operation), dims);
 665         }
 666 
 667         reshape(dims, operation, userReshape);
 668     }
 669 
 670     // This method gets overriden in XFramePeer & XDialogPeer.
 671     abstract boolean isTargetUndecorated();
 672 
 673     /**
 674      * @see java.awt.peer.ComponentPeer#setBounds
 675      */
 676     public void setBounds(int x, int y, int width, int height, int op) {
 677         // TODO: Rewrite with WindowDimensions
 678         reshape(x, y, width, height, op, true);
 679         validateSurface();
 680     }
 681 
 682     // Coordinates are that of the shell
 683     void reconfigureContentWindow(WindowDimensions dims) {
 684         if (content == null) {
 685             insLog.fine("WARNING: Content window is null");
 686             return;
 687         }
 688         content.setContentBounds(dims);
 689     }
 690 
 691     boolean no_reparent_artifacts = false;
 692     public void handleConfigureNotifyEvent(XEvent xev) {
 693         if (XWM.getWMID() == XWM.UNITY_COMPIZ_WM && !insets_corrected) {
 694             return;
 695         }
 696         assert (SunToolkit.isAWTLockHeldByCurrentThread());
 697         XConfigureEvent xe = xev.get_xconfigure();
 698         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 699             insLog.fine("Configure notify {0}", xe);
 700         }
 701 
 702         // XXX: should really only consider synthetic events, but
 703         if (isReparented()) {
 704             configure_seen = true;
 705         }
 706 
 707         if (!isMaximized()
 708             && (xe.get_serial() == reparent_serial || xe.get_window() != getShell())
 709             && !no_reparent_artifacts)
 710         {
 711             insLog.fine("- reparent artifact, skipping");
 712             return;
 713         }
 714         no_reparent_artifacts = false;
 715 
 716         /**
 717          * When there is a WM we receive some CN before being visible and after.
 718          * We should skip all CN which are before being visible, because we assume
 719          * the gravity is in action while it is not yet.
 720          *
 721          * When there is no WM we receive CN only _before_ being visible.
 722          * We should process these CNs.
 723          */
 724         if (!isVisible() && XWM.getWMID() != XWM.NO_WM) {
 725             insLog.fine(" - not visible, skipping");
 726             return;
 727         }
 728 
 729         /*
 730          * Some window managers configure before we are reparented and
 731          * the send event flag is set! ugh... (Enlighetenment for one,
 732          * possibly MWM as well).  If we haven't been reparented yet
 733          * this is just the WM shuffling us into position.  Ignore
 734          * it!!!! or we wind up in a bogus location.
 735          */
 736         int runningWM = XWM.getWMID();
 737         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 738             insLog.fine("reparented={0}, visible={1}, WM={2}, decorations={3}",
 739                         isReparented(), isVisible(), runningWM, getDecorations());
 740         }
 741         if (!isReparented() && isVisible() && runningWM != XWM.NO_WM
 742                 &&  !XWM.isNonReparentingWM()
 743                 && getDecorations() != XWindowAttributesData.AWT_DECOR_NONE) {
 744             insLog.fine("- visible but not reparented, skipping");
 745             return;
 746         }
 747         //Last chance to correct insets
 748         if (!insets_corrected && getDecorations() != XWindowAttributesData.AWT_DECOR_NONE) {
 749             long parent = XlibUtil.getParentWindow(window);
 750             Insets correctWM = (parent != -1) ? XWM.getWM().getInsets(this, window, parent) : null;
 751             if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
 752                 if (correctWM != null) {
 753                     insLog.finer("Configure notify - insets : " + correctWM);
 754                 } else {
 755                     insLog.finer("Configure notify - insets are still not available");
 756                 }
 757             }
 758             if (correctWM != null) {
 759                 handleCorrectInsets(correctWM);
 760             } else {
 761                 //Only one attempt to correct insets is made (to lower risk)
 762                 //if insets are still not available we simply set the flag
 763                 insets_corrected = true;
 764             }
 765         }
 766 
 767         updateChildrenSizes();
 768 
 769         Point newLocation = getNewLocation(xe, currentInsets.left, currentInsets.top);
 770         WindowDimensions newDimensions =
 771                 new WindowDimensions(newLocation,
 772                                      new Dimension(scaleDown(xe.get_width()),
 773                                                    scaleDown(xe.get_height())),
 774                                      copy(currentInsets), true);
 775 
 776         if (insLog.isLoggable(PlatformLogger.Level.FINER)) {
 777             insLog.finer("Insets are {0}, new dimensions {1}",
 778                      currentInsets, newDimensions);
 779         }
 780 
 781         checkIfOnNewScreen(newDimensions.getBounds());
 782 
 783         Point oldLocation = getLocation();
 784         dimensions = newDimensions;
 785         if (!newLocation.equals(oldLocation)) {
 786             handleMoved(newDimensions);
 787         }
 788         reconfigureContentWindow(newDimensions);
 789         updateChildrenSizes();
 790 
 791         repositionSecurityWarning();
 792     }
 793 
 794     private void checkShellRectSize(Rectangle shellRect) {
 795         shellRect.width = Math.max(MIN_SIZE, shellRect.width);
 796         shellRect.height = Math.max(MIN_SIZE, shellRect.height);
 797     }
 798 
 799     private void checkShellRectPos(Rectangle shellRect) {
 800         int wm = XWM.getWMID();
 801         if (wm == XWM.MOTIF_WM || wm == XWM.CDE_WM) {
 802             if (shellRect.x == 0 && shellRect.y == 0) {
 803                 shellRect.x = shellRect.y = 1;
 804             }
 805         }
 806     }
 807 
 808     private void checkShellRect(Rectangle shellRect) {
 809         checkShellRectSize(shellRect);
 810         checkShellRectPos(shellRect);
 811     }
 812 
 813     public void setShellBounds(Rectangle rec) {
 814         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 815             insLog.fine("Setting shell bounds on " + this + " to " + rec);
 816         }
 817         XToolkit.awtLock();
 818         try {
 819             updateSizeHints(rec.x, rec.y, rec.width, rec.height);
 820             XlibWrapper.XMoveResizeWindow(XToolkit.getDisplay(), getShell(),
 821                                           scaleUp(rec.x), scaleUp(rec.y),
 822                                           scaleUp(rec.width), scaleUp(rec.height));
 823         }
 824         finally {
 825             XToolkit.awtUnlock();
 826         }
 827     }
 828     public void setShellSize(Rectangle rec) {
 829         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 830             insLog.fine("Setting shell size on " + this + " to " + rec);
 831         }
 832         XToolkit.awtLock();
 833         try {
 834             updateSizeHints(rec.x, rec.y, rec.width, rec.height);
 835             XlibWrapper.XResizeWindow(XToolkit.getDisplay(), getShell(),
 836                                       scaleUp(rec.width), scaleUp(rec.height));
 837         }
 838         finally {
 839             XToolkit.awtUnlock();
 840         }
 841     }
 842     public void setShellPosition(Rectangle rec) {
 843         if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 844             insLog.fine("Setting shell position on " + this + " to " + rec);
 845         }
 846         XToolkit.awtLock();
 847         try {
 848             updateSizeHints(rec.x, rec.y, rec.width, rec.height);
 849             XlibWrapper.XMoveWindow(XToolkit.getDisplay(), getShell(),
 850                                     scaleUp(rec.x), scaleUp(rec.y));
 851         }
 852         finally {
 853             XToolkit.awtUnlock();
 854         }
 855     }
 856 
 857     void initResizability() {
 858         setResizable(winAttr.initialResizability);
 859     }
 860     public void setResizable(boolean resizable) {
 861         int fs = winAttr.functions;
 862         if (!isResizable() && resizable) {
 863             currentInsets = new Insets(0, 0, 0, 0);
 864             resetWMSetInsets();
 865             if (!isEmbedded()) {
 866                 setReparented(false);
 867             }
 868             winAttr.isResizable = resizable;
 869             if ((fs & MWMConstants.MWM_FUNC_ALL) != 0) {
 870                 fs &= ~(MWMConstants.MWM_FUNC_RESIZE | MWMConstants.MWM_FUNC_MAXIMIZE);
 871             } else {
 872                 fs |= (MWMConstants.MWM_FUNC_RESIZE | MWMConstants.MWM_FUNC_MAXIMIZE);
 873             }
 874             winAttr.functions = fs;
 875             XWM.setShellResizable(this);
 876         } else if (isResizable() && !resizable) {
 877             currentInsets = new Insets(0, 0, 0, 0);
 878             resetWMSetInsets();
 879             if (!isEmbedded()) {
 880                 setReparented(false);
 881             }
 882             winAttr.isResizable = resizable;
 883             if ((fs & MWMConstants.MWM_FUNC_ALL) != 0) {
 884                 fs |= (MWMConstants.MWM_FUNC_RESIZE | MWMConstants.MWM_FUNC_MAXIMIZE);
 885             } else {
 886                 fs &= ~(MWMConstants.MWM_FUNC_RESIZE | MWMConstants.MWM_FUNC_MAXIMIZE);
 887             }
 888             winAttr.functions = fs;
 889             XWM.setShellNotResizable(this, dimensions, dimensions.getBounds(), false);
 890         }
 891     }
 892 
 893     Rectangle getShellBounds() {
 894         return dimensions.getClientRect();
 895     }
 896 
 897     public Rectangle getBounds() {
 898         return dimensions.getBounds();
 899     }
 900 
 901     public Dimension getSize() {
 902         return dimensions.getSize();
 903     }
 904 
 905     public int getX() {
 906         return dimensions.getLocation().x;
 907     }
 908 
 909     public int getY() {
 910         return dimensions.getLocation().y;
 911     }
 912 
 913     public Point getLocation() {
 914         return dimensions.getLocation();
 915     }
 916 
 917     public int getAbsoluteX() {
 918         // NOTE: returning this peer's location which is shell location
 919         return dimensions.getScreenBounds().x;
 920     }
 921 
 922     public int getAbsoluteY() {
 923         // NOTE: returning this peer's location which is shell location
 924         return dimensions.getScreenBounds().y;
 925     }
 926 
 927     public int getWidth() {
 928         return getSize().width;
 929     }
 930 
 931     public int getHeight() {
 932         return getSize().height;
 933     }
 934 
 935     public final WindowDimensions getDimensions() {
 936         return dimensions;
 937     }
 938 
 939     public Point getLocationOnScreen() {
 940         XToolkit.awtLock();
 941         try {
 942             if (configure_seen) {
 943                 return toGlobal(0,0);
 944             } else {
 945                 Point location = target.getLocation();
 946                 if (insLog.isLoggable(PlatformLogger.Level.FINE)) {
 947                     insLog.fine("getLocationOnScreen {0} not reparented: {1} ",
 948                                 this, location);
 949                 }
 950                 return location;
 951             }
 952         } finally {
 953             XToolkit.awtUnlock();
 954         }
 955     }
 956 
 957 
 958 /***************************************************************************************
 959  *              END            OF             I N S E T S   C O D E
 960  **************************************************************************************/
 961 
 962     protected boolean isEventDisabled(XEvent e) {
 963         switch (e.get_type()) {
 964             // Do not generate MOVED/RESIZED events since we generate them by ourselves
 965           case XConstants.ConfigureNotify:
 966               return true;
 967           case XConstants.EnterNotify:
 968           case XConstants.LeaveNotify:
 969               // Disable crossing event on outer borders of Frame so
 970               // we receive only one set of cross notifications(first set is from content window)
 971               return true;
 972           default:
 973               return super.isEventDisabled(e);
 974         }
 975     }
 976 
 977     int getDecorations() {
 978         return winAttr.decorations;
 979     }
 980 
 981     int getFunctions() {
 982         return winAttr.functions;
 983     }
 984 
 985     public void setVisible(boolean vis) {
 986         if (log.isLoggable(PlatformLogger.Level.FINER)) {
 987             log.finer("Setting {0} to visible {1}", this, Boolean.valueOf(vis));
 988         }
 989         if (vis && !isVisible()) {
 990             XWM.setShellDecor(this);
 991             super.setVisible(vis);
 992             if (winAttr.isResizable) {
 993                 //Fix for 4320050: Minimum size for java.awt.Frame is not being enforced.
 994                 //We need to update frame's minimum size, not to reset it
 995                 XWM.removeSizeHints(this, XUtilConstants.PMaxSize);
 996                 updateMinimumSize();
 997             }
 998         } else {
 999             super.setVisible(vis);
1000         }
1001     }
1002 
1003     protected void suppressWmTakeFocus(boolean doSuppress) {
1004         XAtomList protocols = getWMProtocols();
1005         if (doSuppress) {
1006             protocols.remove(wm_take_focus);
1007         } else {
1008             protocols.add(wm_take_focus);
1009         }
1010         wm_protocols.setAtomListProperty(this, protocols);
1011     }
1012 
1013     public void dispose() {
1014         if (content != null) {
1015             content.destroy();
1016         }
1017         focusProxy.destroy();
1018 
1019         if (iconWindow != null) {
1020             iconWindow.destroy();
1021         }
1022 
1023         super.dispose();
1024     }
1025 
1026     public void handleClientMessage(XEvent xev) {
1027         super.handleClientMessage(xev);
1028         XClientMessageEvent cl = xev.get_xclient();
1029         if ((wm_protocols != null) && (cl.get_message_type() == wm_protocols.getAtom())) {
1030             if (cl.get_data(0) == wm_delete_window.getAtom()) {
1031                 handleQuit();
1032             } else if (cl.get_data(0) == wm_take_focus.getAtom()) {
1033                 handleWmTakeFocus(cl);
1034             }
1035         }
1036     }
1037 
1038     private void handleWmTakeFocus(XClientMessageEvent cl) {
1039         if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1040             focusLog.fine("WM_TAKE_FOCUS on {0}", this);
1041         }
1042 
1043         if (XWM.getWMID() == XWM.UNITY_COMPIZ_WM) {
1044             // JDK-8159460
1045             Window focusedWindow = XKeyboardFocusManagerPeer.getInstance()
1046                     .getCurrentFocusedWindow();
1047             Window activeWindow = XWindowPeer.getDecoratedOwner(focusedWindow);
1048             if (activeWindow != target) {
1049                 requestWindowFocus(cl.get_data(1), true);
1050             } else {
1051                 WindowEvent we = new WindowEvent(focusedWindow,
1052                         WindowEvent.WINDOW_GAINED_FOCUS);
1053                 sendEvent(we);
1054             }
1055         } else {
1056             requestWindowFocus(cl.get_data(1), true);
1057         }
1058     }
1059 
1060     /**
1061      * Requests focus to this decorated top-level by requesting X input focus
1062      * to the shell window.
1063      */
1064     protected void requestXFocus(long time, boolean timeProvided) {
1065         // We have proxied focus mechanism - instead of shell the focus is held
1066         // by "proxy" - invisible mapped window. When we want to set X input focus to
1067         // toplevel set it on proxy instead.
1068         if (focusProxy == null) {
1069             if (focusLog.isLoggable(PlatformLogger.Level.WARNING)) {
1070                 focusLog.warning("Focus proxy is null for " + this);
1071             }
1072         } else {
1073             if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1074                 focusLog.fine("Requesting focus to proxy: " + focusProxy);
1075             }
1076             if (timeProvided) {
1077                 focusProxy.xRequestFocus(time);
1078             } else {
1079                 focusProxy.xRequestFocus();
1080             }
1081         }
1082     }
1083 
1084     XFocusProxyWindow getFocusProxy() {
1085         return focusProxy;
1086     }
1087 
1088     public void handleQuit() {
1089         postEvent(new WindowEvent((Window)target, WindowEvent.WINDOW_CLOSING));
1090     }
1091 
1092     final void dumpMe() {
1093         System.err.println(">>> Peer: " + x + ", " + y + ", " + width + ", " + height);
1094     }
1095 
1096     final void dumpTarget() {
1097         AWTAccessor.ComponentAccessor compAccessor = AWTAccessor.getComponentAccessor();
1098         int getWidth = compAccessor.getWidth(target);
1099         int getHeight = compAccessor.getHeight(target);
1100         int getTargetX = compAccessor.getX(target);
1101         int getTargetY = compAccessor.getY(target);
1102         System.err.println(">>> Target: " + getTargetX + ", " + getTargetY + ", " + getWidth + ", " + getHeight);
1103     }
1104 
1105     final void dumpShell() {
1106         dumpWindow("Shell", getShell());
1107     }
1108     final void dumpContent() {
1109         dumpWindow("Content", getContentWindow());
1110     }
1111     final void dumpParent() {
1112         long parent = XlibUtil.getParentWindow(getShell());
1113         if (parent != 0)
1114         {
1115             dumpWindow("Parent", parent);
1116         }
1117         else
1118         {
1119             System.err.println(">>> NO PARENT");
1120         }
1121     }
1122 
1123     final void dumpWindow(String id, long window) {
1124         XWindowAttributes pattr = new XWindowAttributes();
1125         try {
1126             XToolkit.awtLock();
1127             try {
1128                 int status =
1129                     XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
1130                                                      window, pattr.pData);
1131             }
1132             finally {
1133                 XToolkit.awtUnlock();
1134             }
1135             System.err.println(">>>> " + id + ": " + pattr.get_x()
1136                                + ", " + pattr.get_y() + ", " + pattr.get_width()
1137                                + ", " + pattr.get_height());
1138         } finally {
1139             pattr.dispose();
1140         }
1141     }
1142 
1143     final void dumpAll() {
1144         dumpTarget();
1145         dumpMe();
1146         dumpParent();
1147         dumpShell();
1148         dumpContent();
1149     }
1150 
1151     boolean isMaximized() {
1152         return false;
1153     }
1154 
1155     @Override
1156     boolean isOverrideRedirect() {
1157         return Window.Type.POPUP.equals(getWindowType());
1158     }
1159 
1160     public boolean requestWindowFocus(long time, boolean timeProvided) {
1161         focusLog.fine("Request for decorated window focus");
1162         // If this is Frame or Dialog we can't assure focus request success - but we still can try
1163         // If this is Window and its owner Frame is active we can be sure request succedded.
1164         Window focusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
1165         Window activeWindow = XWindowPeer.getDecoratedOwner(focusedWindow);
1166 
1167         if (focusLog.isLoggable(PlatformLogger.Level.FINER)) {
1168             focusLog.finer("Current window is: active={0}, focused={1}",
1169                        Boolean.valueOf(target == activeWindow),
1170                        Boolean.valueOf(target == focusedWindow));
1171         }
1172 
1173         XWindowPeer toFocus = this;
1174         while (toFocus.nextTransientFor != null) {
1175             toFocus = toFocus.nextTransientFor;
1176         }
1177         if (toFocus == null || !toFocus.focusAllowedFor()) {
1178             // This might change when WM will have property to determine focus policy.
1179             // Right now, because policy is unknown we can't be sure we succedded
1180             return false;
1181         }
1182         if (this == toFocus) {
1183             if (isWMStateNetHidden()) {
1184                 focusLog.fine("The window is unmapped, so rejecting the request");
1185                 return false;
1186             }
1187             if (target == activeWindow && target != focusedWindow) {
1188                 // Happens when an owned window is currently focused
1189                 focusLog.fine("Focus is on child window - transferring it back to the owner");
1190                 handleWindowFocusInSync(-1);
1191                 return true;
1192             }
1193             Window realNativeFocusedWindow = XWindowPeer.getNativeFocusedWindow();
1194             if (focusLog.isLoggable(PlatformLogger.Level.FINEST)) {
1195                 focusLog.finest("Real native focused window: " + realNativeFocusedWindow +
1196                             "\nKFM's focused window: " + focusedWindow);
1197             }
1198 
1199             // A workaround for Metacity. See 6522725, 6613426, 7147075.
1200             if (target == realNativeFocusedWindow && XWM.getWMID() == XWM.METACITY_WM) {
1201                 if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1202                     focusLog.fine("The window is already natively focused.");
1203                 }
1204                 return true;
1205             }
1206         }
1207         if (focusLog.isLoggable(PlatformLogger.Level.FINE)) {
1208             focusLog.fine("Requesting focus to " + (this == toFocus ? "this window" : toFocus));
1209         }
1210 
1211         if (timeProvided) {
1212             toFocus.requestXFocus(time);
1213         } else {
1214             toFocus.requestXFocus();
1215         }
1216         return (this == toFocus);
1217     }
1218 
1219     XWindowPeer actualFocusedWindow = null;
1220     void setActualFocusedWindow(XWindowPeer actualFocusedWindow) {
1221         synchronized(getStateLock()) {
1222             this.actualFocusedWindow = actualFocusedWindow;
1223         }
1224     }
1225 
1226     boolean requestWindowFocus(XWindowPeer actualFocusedWindow,
1227                                long time, boolean timeProvided)
1228     {
1229         setActualFocusedWindow(actualFocusedWindow);
1230         return requestWindowFocus(time, timeProvided);
1231     }
1232     public void handleWindowFocusIn(long serial) {
1233         if (null == actualFocusedWindow) {
1234             super.handleWindowFocusIn(serial);
1235         } else {
1236             /*
1237              * Fix for 6314575.
1238              * If this is a result of clicking on one of the Frame's component
1239              * then 'actualFocusedWindow' shouldn't be focused. A decision of focusing
1240              * it or not should be made after the appropriate Java mouse event (if any)
1241              * is handled by the component where 'actualFocusedWindow' value may be reset.
1242              *
1243              * The fix is based on the empiric fact consisting in that the component
1244              * receives native mouse event nearly at the same time the Frame receives
1245              * WM_TAKE_FOCUS (when FocusIn is generated via XSetInputFocus call) but
1246              * definetely before the Frame gets FocusIn event (when this method is called).
1247              */
1248             postEvent(new InvocationEvent(target, new Runnable() {
1249                 public void run() {
1250                     XWindowPeer fw = null;
1251                     synchronized (getStateLock()) {
1252                         fw = actualFocusedWindow;
1253                         actualFocusedWindow = null;
1254                         if (null == fw || !fw.isVisible() || !fw.isFocusableWindow()) {
1255                             fw = XDecoratedPeer.this;
1256                         }
1257                     }
1258                     fw.handleWindowFocusIn_Dispatch();
1259                 }
1260             }));
1261         }
1262     }
1263 
1264     public void handleWindowFocusOut(Window oppositeWindow, long serial) {
1265         Window actualFocusedWindow = XKeyboardFocusManagerPeer.getInstance().getCurrentFocusedWindow();
1266 
1267         // If the actual focused window is not this decorated window then retain it.
1268         if (actualFocusedWindow != null && actualFocusedWindow != target) {
1269             Window owner = XWindowPeer.getDecoratedOwner(actualFocusedWindow);
1270 
1271             if (owner != null && owner == target) {
1272                 setActualFocusedWindow(AWTAccessor.getComponentAccessor().getPeer(actualFocusedWindow));
1273             }
1274         }
1275         super.handleWindowFocusOut(oppositeWindow, serial);
1276     }
1277 }