1 /*
2  * Copyright (c) 2003, 2017, 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.GraphicsDevice;
29 import java.awt.GraphicsEnvironment;
30 import java.awt.Point;
31 import java.awt.Window;
32 import java.awt.peer.MouseInfoPeer;
33 
34 import sun.awt.AWTAccessor;
35 import sun.awt.X11GraphicsDevice;
36 
37 public final class XMouseInfoPeer implements MouseInfoPeer {
38 
39     /**
40      * Package-private constructor to prevent instantiation.
41      */
XMouseInfoPeer()42     XMouseInfoPeer() {
43     }
44 
fillPointWithCoords(Point point)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")
isWindowUnderMouse(Window w)87     public boolean isWindowUnderMouse(Window w) {
88         if (w == null) {
89             return false;
90         }
91         XWindow peer = AWTAccessor.getComponentAccessor().getPeer(w);
92         if (peer == null) {
93             return false;
94         }
95         long display = XToolkit.getDisplay();
96         long contentWindow = peer.getContentWindow();
97         long parent = XlibUtil.getParentWindow(contentWindow);
98 
99         XToolkit.awtLock();
100         try
101         {
102             boolean windowOnTheSameScreen = XlibWrapper.XQueryPointer(display, parent,
103                                   XlibWrapper.larg1, // root_return
104                                   XlibWrapper.larg8, // child_return
105                                   XlibWrapper.larg3, // root_x_return
106                                   XlibWrapper.larg4, // root_y_return
107                                   XlibWrapper.larg5, // win_x_return
108                                   XlibWrapper.larg6, // win_y_return
109                                   XlibWrapper.larg7); //  mask_return
110             long siblingWindow = Native.getWindow(XlibWrapper.larg8);
111             return (siblingWindow == contentWindow && windowOnTheSameScreen);
112         }
113         finally
114         {
115             XToolkit.awtUnlock();
116         }
117     }
118 }
119