1 /*
2  * Copyright (c) 2003, 2005, 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 /**
27  * This class is a placeholder for all internal static objects that represent
28  * system state. We keep our representation up-to-date with actual system
29  * state by tracking events, such as X Focus, Component under cursor etc.
30  * All attributes should be private static with accessors to simpify change
31  * tracking.
32  */
33 package sun.awt.X11;
34 
35 import java.awt.Component;
36 import java.lang.ref.WeakReference;
37 
38 class XAwtState {
39     /**
40      * The mouse is over this component.
41      * If the component is not disabled, it received MOUSE_ENTERED but no MOUSE_EXITED.
42      */
43     private static WeakReference<Component> componentMouseEnteredRef = null;
44 
setComponentMouseEntered(Component component)45     static void setComponentMouseEntered(Component component) {
46         XToolkit.awtLock();
47         try {
48             if (component == null) {
49                 componentMouseEnteredRef = null;
50                 return;
51             }
52             if (component != getComponentMouseEntered()) {
53                 componentMouseEnteredRef = new WeakReference<>(component);
54             }
55         } finally {
56             XToolkit.awtUnlock();
57         }
58     }
59 
getComponentMouseEntered()60     static Component getComponentMouseEntered() {
61         XToolkit.awtLock();
62         try {
63             if (componentMouseEnteredRef == null) {
64                 return null;
65             }
66             return componentMouseEnteredRef.get();
67         } finally {
68             XToolkit.awtUnlock();
69         }
70     }
71 
72     /**
73      * The XBaseWindow is created with OwnerGrabButtonMask
74      * (see X vol. 1, 8.3.3.2) so inside the app Key, Motion, and Button events
75      * are received by the window they actualy happened on, not the grabber.
76      * Then XBaseWindow dispatches them to the grabber. As a result
77      * XAnyEvent.get_window() returns actual window the event is originated,
78      * though the event is dispatched by  the grabber.
79      */
80     private static boolean inManualGrab = false;
81 
isManualGrab()82     static boolean isManualGrab() {
83         return inManualGrab;
84     }
85 
86     private static WeakReference<XBaseWindow> grabWindowRef = null;
87 
88     /**
89      * The X Active Grab overrides any other active grab by the same
90      * client see XGrabPointer, XGrabKeyboard
91      */
setGrabWindow(XBaseWindow grabWindow)92     static void setGrabWindow(XBaseWindow grabWindow) {
93         setGrabWindow(grabWindow, false);
94     }
95 
96     /**
97      * Automatic passive grab doesn't override active grab see XGrabButton
98      */
setAutoGrabWindow(XBaseWindow grabWindow)99     static void setAutoGrabWindow(XBaseWindow grabWindow) {
100         setGrabWindow(grabWindow, true);
101     }
102 
setGrabWindow(XBaseWindow grabWindow, boolean isAutoGrab)103     private static void setGrabWindow(XBaseWindow grabWindow, boolean isAutoGrab) {
104         XToolkit.awtLock();
105         try {
106             if (inManualGrab && isAutoGrab) {
107                 return;
108             }
109             inManualGrab = grabWindow != null && !isAutoGrab;
110             if (grabWindow == null) {
111                 grabWindowRef = null;
112                 return;
113             }
114             if (grabWindow != getGrabWindow()) {
115                 grabWindowRef = new WeakReference<>(grabWindow);
116             }
117         } finally {
118             XToolkit.awtUnlock();
119         }
120     }
121 
getGrabWindow()122     static XBaseWindow getGrabWindow() {
123         XToolkit.awtLock();
124         try {
125             if (grabWindowRef == null) {
126                 return null;
127             }
128             XBaseWindow xbw = grabWindowRef.get();
129             if( xbw != null && xbw.isDisposed() ) {
130                 xbw = null;
131                 grabWindowRef = null;
132             }else if( xbw == null ) {
133                 grabWindowRef = null;
134             }
135             return xbw;
136         } finally {
137             XToolkit.awtUnlock();
138         }
139     }
140 }
141