1/*
2 * Copyright (c) 2003, 2013, 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 sun.util.logging.PlatformLogger;
29
30import java.util.*;
31
32class XProtocol {
33    private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XProtocol");
34
35    private Map<XAtom, XAtomList> atomToList = new HashMap<XAtom, XAtomList>();
36    private Map<XAtom, Long> atomToAnchor = new HashMap<XAtom, Long>();
37
38    volatile boolean firstCheck = true;
39    /*
40     * Check that that the list of protocols specified by WM in property
41     * named LIST_NAME on the root window contains protocol PROTO.
42     */
43    boolean checkProtocol(XAtom listName, XAtom protocol) {
44        XAtomList protocols = atomToList.get(listName);
45
46        if (protocols != null) {
47            return protocols.contains(protocol);
48        }
49
50        protocols = listName.getAtomListPropertyList(XToolkit.getDefaultRootWindow());
51        atomToList.put(listName, protocols);
52        try {
53            return protocols.contains(protocol);
54        } finally {
55            if (firstCheck) {
56                firstCheck = false;
57                if (log.isLoggable(PlatformLogger.Level.FINE)) {
58                    log.fine("{0}:{1} supports {2}", this, listName, protocols);
59                }
60            }
61        }
62    }
63
64    /*
65     * Check for anchor_prop(anchor_type) on root, take the value as the
66     * window id and check if that window exists and has anchor_prop(anchor_type)
67     * with the same value (i.e. pointing back to self).
68     *
69     * Returns the anchor window, as some WM may put interesting stuff in
70     * its properties (e.g. sawfish).
71     */
72    long checkAnchorImpl(XAtom anchorProp, long anchorType) {
73        long root_xref, self_xref;
74
75        XToolkit.awtLock();
76        try {
77            root_xref = anchorProp.get32Property(XToolkit.getDefaultRootWindow(),
78                                                 anchorType);
79        } finally {
80            XToolkit.awtUnlock();
81        }
82        if (root_xref == 0) {
83            return 0;
84        }
85        self_xref = anchorProp.get32Property(root_xref, anchorType);
86        if (self_xref != root_xref) {
87            return 0;
88        }
89        return self_xref;
90    }
91    public long checkAnchor(XAtom anchorProp, long anchorType) {
92        Long val = atomToAnchor.get(anchorProp);
93        if (val != null) {
94            return val.longValue();
95        }
96        long res = checkAnchorImpl(anchorProp, anchorType);
97        atomToAnchor.put(anchorProp, res);
98        return res;
99    }
100    public long checkAnchor(XAtom anchorProp, XAtom anchorType) {
101        return checkAnchor(anchorProp, anchorType.getAtom());
102    }
103
104}
105