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