public XWindowPeer getToplevelXWindow() {
   XBaseWindow bw = this;
   while (bw != null && !(bw instanceof XWindowPeer)) {
     bw = bw.getParentWindow();
   }
   return (XWindowPeer) bw;
 }
 public int getAbsoluteY() {
   XBaseWindow pw = getParentWindow();
   if (pw != null) {
     return pw.getAbsoluteY() + getY();
   } else {
     return getY();
   }
 }
 public int getAbsoluteX() {
   XBaseWindow pw = getParentWindow();
   if (pw != null) {
     return pw.getAbsoluteX() + getX();
   } else {
     // Overridden for top-levels as their (x,y) is Java (x, y), not native location
     return getX();
   }
 }
 /**
  * Dispatches event to the grab Window or event source window depending on whether the grab is
  * active and on the event type
  */
 static void dispatchToWindow(XEvent ev) {
   XBaseWindow target = XAwtState.getGrabWindow();
   if (target == null || !isGrabbedEvent(ev, target)) {
     target = XToolkit.windowToXWindow(ev.get_xany().get_window());
   }
   if (target != null && target.checkInitialised()) {
     target.dispatchEvent(ev);
   }
 }
 static void ungrabInput() {
   XToolkit.awtLock();
   try {
     XBaseWindow grabWindow = XAwtState.getGrabWindow();
     if (grabLog.isLoggable(Level.FINE)) {
       grabLog.log(Level.FINE, "UnGrab input on {0}", new Object[] {String.valueOf(grabWindow)});
     }
     if (grabWindow != null) {
       grabWindow.ungrabInputImpl();
       XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), CurrentTime);
       XlibWrapper.XUngrabKeyboard(XToolkit.getDisplay(), CurrentTime);
       XAwtState.setGrabWindow(null);
       // we need to call XFlush() here to force ungrab
       // see 6384219 for details
       XlibWrapper.XFlush(XToolkit.getDisplay());
     }
   } finally {
     XToolkit.awtUnlock();
   }
 }
  /**
   * We should always grab both keyboard and pointer to control event flow on popups. This also
   * simplifies synthetic grab implementation. The active grab overrides activated automatic grab.
   */
  public boolean grabInput() {
    if (grabLog.isLoggable(Level.FINE)) {
      grabLog.log(Level.FINE, "Grab input on {0}", new Object[] {String.valueOf(this)});
    }

    XToolkit.awtLock();
    try {
      if (XAwtState.getGrabWindow() == this && XAwtState.isManualGrab()) {
        grabLog.fine("    Already Grabbed");
        return true;
      }
      // 6273031: PIT. Choice drop down does not close once it is right clicked to show a popup menu
      // remember previous window having grab and if it's not null ungrab it.
      XBaseWindow prevGrabWindow = XAwtState.getGrabWindow();
      final int eventMask =
          (int)
              (ButtonPressMask
                  | ButtonReleaseMask
                  | EnterWindowMask
                  | LeaveWindowMask
                  | PointerMotionMask
                  | ButtonMotionMask);
      final int ownerEvents = 1;

      int ptrGrab =
          XlibWrapper.XGrabPointer(
              XToolkit.getDisplay(),
              getContentWindow(),
              ownerEvents,
              eventMask,
              GrabModeAsync,
              GrabModeAsync,
              None,
              (XWM.isMotif() ? XToolkit.arrowCursor : None),
              CurrentTime);
      // Check grab results to be consistent with X server grab
      if (ptrGrab != GrabSuccess) {
        XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), CurrentTime);
        XAwtState.setGrabWindow(null);
        grabLog.fine("    Grab Failure - mouse");
        return false;
      }

      int keyGrab =
          XlibWrapper.XGrabKeyboard(
              XToolkit.getDisplay(),
              getContentWindow(),
              ownerEvents,
              GrabModeAsync,
              GrabModeAsync,
              CurrentTime);
      if (keyGrab != GrabSuccess) {
        XlibWrapper.XUngrabPointer(XToolkit.getDisplay(), CurrentTime);
        XlibWrapper.XUngrabKeyboard(XToolkit.getDisplay(), CurrentTime);
        XAwtState.setGrabWindow(null);
        grabLog.fine("    Grab Failure - keyboard");
        return false;
      }
      if (prevGrabWindow != null) {
        prevGrabWindow.ungrabInputImpl();
      }
      XAwtState.setGrabWindow(this);
      grabLog.fine("    Grab - success");
      return true;
    } finally {
      XToolkit.awtUnlock();
    }
  }
  /**
   * Translate coordinates from one window into another. Optimized for XAWT - uses cached data when
   * possible. Preferable over pure XTranslateCoordinates.
   *
   * @return coordinates relative to dst, or null if error happened
   */
  static Point toOtherWindow(long src, long dst, int x, int y) {
    Point rpt = new Point(0, 0);

    // Check if both windows belong to XAWT - then no X calls are necessary

    XBaseWindow srcPeer = XToolkit.windowToXWindow(src);
    XBaseWindow dstPeer = XToolkit.windowToXWindow(dst);

    if (srcPeer != null && dstPeer != null) {
      // (x, y) is relative to src
      rpt.x = x + srcPeer.getAbsoluteX() - dstPeer.getAbsoluteX();
      rpt.y = y + srcPeer.getAbsoluteY() - dstPeer.getAbsoluteY();
    } else if (dstPeer != null && XlibUtil.isRoot(src, dstPeer.getScreenNumber())) {
      // from root into peer
      rpt.x = x - dstPeer.getAbsoluteX();
      rpt.y = y - dstPeer.getAbsoluteY();
    } else if (srcPeer != null && XlibUtil.isRoot(dst, srcPeer.getScreenNumber())) {
      // from peer into root
      rpt.x = x + srcPeer.getAbsoluteX();
      rpt.y = y + srcPeer.getAbsoluteY();
    } else {
      rpt = XlibUtil.translateCoordinates(src, dst, new Point(x, y));
    }
    return rpt;
  }