0N/A/*
6447N/A * Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.awt.X11;
0N/A
0N/Aimport java.awt.Dimension;
0N/Aimport java.awt.GraphicsEnvironment;
0N/Aimport java.awt.Point;
0N/Aimport java.awt.Rectangle;
0N/A
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashSet;
0N/Aimport java.util.Set;
0N/A
0N/Aimport sun.awt.X11GraphicsConfig;
0N/Aimport sun.awt.X11GraphicsDevice;
0N/Aimport sun.awt.X11GraphicsEnvironment;
0N/A
0N/A/*
0N/A * This class is a collection of utility methods that operate
0N/A * with native windows.
0N/A */
0N/Apublic class XlibUtil
0N/A{
0N/A /**
0N/A * The constructor is made private to eliminate any
0N/A * instances of this class
0N/A */
0N/A private XlibUtil()
0N/A {
0N/A }
0N/A
0N/A /**
0N/A * Xinerama-aware version of XlibWrapper.RootWindow method.
0N/A */
0N/A public static long getRootWindow(int screenNumber)
0N/A {
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A X11GraphicsEnvironment x11ge = (X11GraphicsEnvironment)
0N/A GraphicsEnvironment.getLocalGraphicsEnvironment();
0N/A if (x11ge.runningXinerama())
0N/A {
0N/A // all the Xinerama windows share the same root window
0N/A return XlibWrapper.RootWindow(XToolkit.getDisplay(), 0);
0N/A }
0N/A else
0N/A {
0N/A return XlibWrapper.RootWindow(XToolkit.getDisplay(), screenNumber);
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given window is a root window for the given screen
0N/A */
0N/A static boolean isRoot(long rootCandidate, long screenNumber)
0N/A {
0N/A long root;
0N/A
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A root = XlibWrapper.RootWindow(XToolkit.getDisplay(),
0N/A screenNumber);
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A
0N/A return root == rootCandidate;
0N/A }
0N/A
0N/A /**
0N/A * Returns the bounds of the given window, in absolute coordinates
0N/A */
0N/A static Rectangle getWindowGeometry(long window)
0N/A {
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A int res = XlibWrapper.XGetGeometry(XToolkit.getDisplay(),
0N/A window,
0N/A XlibWrapper.larg1, // root_return
0N/A XlibWrapper.larg2, // x_return
0N/A XlibWrapper.larg3, // y_return
0N/A XlibWrapper.larg4, // width_return
0N/A XlibWrapper.larg5, // height_return
0N/A XlibWrapper.larg6, // border_width_return
0N/A XlibWrapper.larg7); // depth_return
0N/A if (res == 0)
0N/A {
0N/A return null;
0N/A }
0N/A
0N/A int x = Native.getInt(XlibWrapper.larg2);
0N/A int y = Native.getInt(XlibWrapper.larg3);
0N/A long width = Native.getUInt(XlibWrapper.larg4);
0N/A long height = Native.getUInt(XlibWrapper.larg5);
0N/A
0N/A return new Rectangle(x, y, (int)width, (int)height);
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Translates the given point from one window to another. Returns
0N/A * null if the translation is failed
0N/A */
0N/A static Point translateCoordinates(long src, long dst, Point p)
0N/A {
0N/A Point translated = null;
0N/A
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A XTranslateCoordinates xtc =
0N/A new XTranslateCoordinates(src, dst, p.x, p.y);
0N/A try
0N/A {
1216N/A int status = xtc.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());
0N/A if ((status != 0) &&
6447N/A ((XErrorHandlerUtil.saved_error == null) ||
6447N/A (XErrorHandlerUtil.saved_error.get_error_code() == XConstants.Success)))
0N/A {
0N/A translated = new Point(xtc.get_dest_x(), xtc.get_dest_y());
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A xtc.dispose();
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A
0N/A return translated;
0N/A }
0N/A
0N/A /**
0N/A * Translates the given rectangle from one window to another.
0N/A * Returns null if the translation is failed
0N/A */
0N/A static Rectangle translateCoordinates(long src, long dst, Rectangle r)
0N/A {
0N/A Point translatedLoc = translateCoordinates(src, dst, r.getLocation());
0N/A if (translatedLoc == null)
0N/A {
0N/A return null;
0N/A }
0N/A else
0N/A {
0N/A return new Rectangle(translatedLoc, r.getSize());
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the parent for the given window
0N/A */
0N/A static long getParentWindow(long window)
0N/A {
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A XBaseWindow bw = XToolkit.windowToXWindow(window);
0N/A if (bw != null)
0N/A {
0N/A XBaseWindow pbw = bw.getParentWindow();
0N/A if (pbw != null)
0N/A {
0N/A return pbw.getWindow();
0N/A }
0N/A }
0N/A
0N/A XQueryTree qt = new XQueryTree(window);
0N/A try
0N/A {
0N/A if (qt.execute() == 0)
0N/A {
0N/A return 0;
0N/A }
0N/A else
0N/A {
0N/A return qt.get_parent();
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A qt.dispose();
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns all the children for the given window
0N/A */
0N/A static Set<Long> getChildWindows(long window)
0N/A {
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A XBaseWindow bw = XToolkit.windowToXWindow(window);
0N/A if (bw != null)
0N/A {
0N/A return bw.getChildren();
0N/A }
0N/A
0N/A XQueryTree xqt = new XQueryTree(window);
0N/A try
0N/A {
0N/A int status = xqt.execute();
0N/A if (status == 0)
0N/A {
0N/A return Collections.emptySet();
0N/A }
0N/A
0N/A long children = xqt.get_children();
0N/A
0N/A if (children == 0)
0N/A {
0N/A return Collections.emptySet();
0N/A }
0N/A
0N/A int childrenCount = xqt.get_nchildren();
0N/A
0N/A Set<Long> childrenSet = new HashSet<Long>(childrenCount);
0N/A for (int i = 0; i < childrenCount; i++)
0N/A {
0N/A childrenSet.add(Native.getWindow(children, i));
0N/A }
0N/A
0N/A return childrenSet;
0N/A }
0N/A finally
0N/A {
0N/A xqt.dispose();
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given window is a Java window and is an
0N/A * instance of XWindowPeer
0N/A */
0N/A static boolean isXAWTToplevelWindow(long window)
0N/A {
0N/A return XToolkit.windowToXWindow(window) instanceof XWindowPeer;
0N/A }
0N/A
0N/A /**
0N/A * NOTICE: Right now returns only decorated top-levels (not Window)
0N/A */
0N/A static boolean isToplevelWindow(long window)
0N/A {
0N/A if (XToolkit.windowToXWindow(window) instanceof XDecoratedPeer)
0N/A {
0N/A return true;
0N/A }
0N/A
0N/A XToolkit.awtLock();
0N/A try
0N/A {
0N/A WindowPropertyGetter wpg =
0N/A new WindowPropertyGetter(window, XWM.XA_WM_STATE, 0, 1, false,
0N/A XWM.XA_WM_STATE);
0N/A try
0N/A {
1216N/A wpg.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());
0N/A if (wpg.getActualType() == XWM.XA_WM_STATE.getAtom())
0N/A {
0N/A return true;
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A wpg.dispose();
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A finally
0N/A {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * The same as isToplevelWindow(window), but doesn't treat
0N/A * XEmbeddedFramePeer as toplevel.
0N/A */
0N/A static boolean isTrueToplevelWindow(long window)
0N/A {
0N/A if (XToolkit.windowToXWindow(window) instanceof XEmbeddedFramePeer)
0N/A {
0N/A return false;
0N/A }
0N/A
0N/A return isToplevelWindow(window);
0N/A }
0N/A
0N/A static int getWindowMapState(long window)
0N/A {
0N/A XToolkit.awtLock();
0N/A XWindowAttributes wattr = new XWindowAttributes();
0N/A try
0N/A {
6447N/A XErrorHandlerUtil.WITH_XERROR_HANDLER(XErrorHandler.IgnoreBadWindowHandler.getInstance());
0N/A int status = XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(),
0N/A window, wattr.pData);
6447N/A XErrorHandlerUtil.RESTORE_XERROR_HANDLER();
0N/A if ((status != 0) &&
6447N/A ((XErrorHandlerUtil.saved_error == null) ||
6447N/A (XErrorHandlerUtil.saved_error.get_error_code() == XConstants.Success)))
0N/A {
0N/A return wattr.get_map_state();
0N/A }
0N/A }
0N/A finally
0N/A {
0N/A wattr.dispose();
0N/A XToolkit.awtUnlock();
0N/A }
0N/A
216N/A return XConstants.IsUnmapped;
0N/A }
0N/A
0N/A /**
0N/A * XSHAPE extension support.
0N/A */
0N/A
0N/A // The variable is declared static as the XSHAPE extension cannot
0N/A // be disabled at run-time, and thus is available all the time
0N/A // once the check is passed.
0N/A static Boolean isShapingSupported = null;
0N/A
0N/A /**
0N/A * Returns whether the XSHAPE extension available
0N/A * @since 1.7
0N/A */
0N/A static synchronized boolean isShapingSupported() {
0N/A
0N/A if (isShapingSupported == null) {
0N/A XToolkit.awtLock();
0N/A try {
0N/A isShapingSupported =
0N/A XlibWrapper.XShapeQueryExtension(
0N/A XToolkit.getDisplay(),
0N/A XlibWrapper.larg1,
0N/A XlibWrapper.larg2);
0N/A } finally {
0N/A XToolkit.awtUnlock();
0N/A }
0N/A }
0N/A
0N/A return isShapingSupported.booleanValue();
0N/A }
0N/A
0N/A}