1/*
2 * Copyright (c) 2003, 2015, 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
26package sun.awt.X11;
27
28import java.awt.Point;
29import java.awt.Window;
30import java.awt.GraphicsEnvironment;
31import java.awt.GraphicsDevice;
32import java.awt.peer.MouseInfoPeer;
33import sun.awt.X11GraphicsDevice;
34
35import sun.awt.AWTAccessor;
36
37public class XMouseInfoPeer implements MouseInfoPeer {
38
39    /**
40     * Package-private constructor to prevent instantiation.
41     */
42    XMouseInfoPeer() {
43    }
44
45    public int fillPointWithCoords(Point point) {
46        long display = XToolkit.getDisplay();
47        GraphicsEnvironment ge = GraphicsEnvironment.
48                                     getLocalGraphicsEnvironment();
49        GraphicsDevice[] gds = ge.getScreenDevices();
50        int gdslen = gds.length;
51
52        XToolkit.awtLock();
53        try {
54            for (int i = 0; i < gdslen; i++) {
55                long screenRoot = XlibWrapper.RootWindow(display, i);
56                boolean pointerFound = XlibWrapper.XQueryPointer(
57                                           display, screenRoot,
58                                           XlibWrapper.larg1,  // root_return
59                                           XlibWrapper.larg2,  // child_return
60                                           XlibWrapper.larg3,  // xr_return
61                                           XlibWrapper.larg4,  // yr_return
62                                           XlibWrapper.larg5,  // xw_return
63                                           XlibWrapper.larg6,  // yw_return
64                                           XlibWrapper.larg7); // mask_return
65                if (pointerFound) {
66                    point.x = Native.getInt(XlibWrapper.larg3);
67                    point.y = Native.getInt(XlibWrapper.larg4);
68                    GraphicsDevice device = gds[i];
69                    if (device instanceof X11GraphicsDevice) {
70                        int scale = ((X11GraphicsDevice) device).getScaleFactor();
71                        point.x = XlibUtil.scaleDown(point.x, scale);
72                        point.y = XlibUtil.scaleDown(point.y, scale);
73                    }
74                    return i;
75                }
76            }
77        } finally {
78            XToolkit.awtUnlock();
79        }
80
81        // this should never happen
82        assert false : "No pointer found in the system.";
83        return 0;
84    }
85
86    @SuppressWarnings("deprecation")
87    public boolean isWindowUnderMouse(Window w) {
88
89        long display = XToolkit.getDisplay();
90
91        // java.awt.Component.findUnderMouseInWindow checks that
92        // the peer is non-null by checking that the component
93        // is showing.
94        XWindow peer = AWTAccessor.getComponentAccessor().getPeer(w);
95        long contentWindow = peer.getContentWindow();
96        long parent = XlibUtil.getParentWindow(contentWindow);
97
98        XToolkit.awtLock();
99        try
100        {
101            boolean windowOnTheSameScreen = XlibWrapper.XQueryPointer(display, parent,
102                                  XlibWrapper.larg1, // root_return
103                                  XlibWrapper.larg8, // child_return
104                                  XlibWrapper.larg3, // root_x_return
105                                  XlibWrapper.larg4, // root_y_return
106                                  XlibWrapper.larg5, // win_x_return
107                                  XlibWrapper.larg6, // win_y_return
108                                  XlibWrapper.larg7); //  mask_return
109            long siblingWindow = Native.getWindow(XlibWrapper.larg8);
110            return (siblingWindow == contentWindow && windowOnTheSameScreen);
111        }
112        finally
113        {
114            XToolkit.awtUnlock();
115        }
116    }
117}
118