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 
26 package sun.awt.X11;
27 
28 import java.awt.*;
29 import java.util.HashMap;
30 import java.awt.event.KeyEvent;
31 import sun.awt.AWTAccessor;
32 
33 public class XEmbeddingContainer extends XEmbedHelper implements XEventDispatcher {
34     HashMap<Long, java.awt.peer.ComponentPeer> children = new HashMap<>();
35 
XEmbeddingContainer()36     XEmbeddingContainer() {
37     }
38 
39     XWindow embedder;
install(XWindow embedder)40     void install(XWindow embedder) {
41         this.embedder = embedder;
42         XToolkit.addEventDispatcher(embedder.getWindow(), this);
43     }
deinstall()44     void deinstall() {
45         XToolkit.removeEventDispatcher(embedder.getWindow(), this);
46     }
47 
add(long child)48     void add(long child) {
49         if (checkXEmbed(child)) {
50             Component proxy = createChildProxy(child);
51             ((Container)embedder.getTarget()).add("Center", proxy);
52             XEmbeddedFramePeer peer = AWTAccessor.getComponentAccessor()
53                                                  .getPeer(proxy);
54             if (peer != null) {
55                 children.put(Long.valueOf(child), peer);
56             }
57         }
58     }
59 
createChildProxy(long child)60     Component createChildProxy(long child) {
61         return new XEmbedChildProxy(this, child);
62     }
notifyChildEmbedded(long child)63     void notifyChildEmbedded(long child) {
64         sendMessage(child, XEMBED_EMBEDDED_NOTIFY, embedder.getWindow(), XEMBED_VERSION, 0);
65     }
66 
childResized(Component child)67     void childResized(Component child) {
68     }
69 
checkXEmbed(long child)70     boolean checkXEmbed(long child) {
71         long data = unsafe.allocateMemory(8);
72         try {
73             if (XEmbedInfo.getAtomData(child, data, 2)) {
74                 int protocol = unsafe.getInt(data);
75                 int flags = unsafe.getInt(data);
76                 return true;
77             }
78         } finally {
79             unsafe.freeMemory(data);
80         }
81         return false;
82     }
83 
detachChild(long child)84     void detachChild(long child) {
85         // The embedder can unmap the client and reparent the client window
86         // to the root window. If the client receives an ReparentNotify
87         // event, it should check the parent field of the XReparentEvent
88         // structure. If this is the root window of the window's screen, then
89         // the protocol is finished and there is no further interaction. If
90         // it is a window other than the root window, then the protocol
91         // continues with the new parent acting as the embedder window.
92         XToolkit.awtLock();
93         try {
94             XlibWrapper.XUnmapWindow(XToolkit.getDisplay(), child);
95             XlibWrapper.XReparentWindow(XToolkit.getDisplay(), child, XToolkit.getDefaultRootWindow(), 0, 0);
96         }
97         finally {
98             XToolkit.awtUnlock();
99         }
100     }
101 
focusGained(long child)102     void focusGained(long child) {
103         sendMessage(child, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0);
104     }
focusLost(long child)105     void focusLost(long child) {
106         sendMessage(child, XEMBED_FOCUS_OUT);
107     }
108 
getChild(long child)109     XEmbedChildProxyPeer getChild(long child) {
110         return (XEmbedChildProxyPeer)children.get(Long.valueOf(child));
111     }
handleClientMessage(XEvent xev)112     public void handleClientMessage(XEvent xev) {
113         XClientMessageEvent msg = xev.get_xclient();
114         if (msg.get_message_type() == XEmbed.getAtom()) {
115             switch ((int)msg.get_data(1)) {
116               case XEMBED_REQUEST_FOCUS:
117                   long child = msg.get_data(2); // Unspecified
118                   getChild(child).requestXEmbedFocus();
119                   break;
120             }
121         }
122     }
dispatchEvent(XEvent xev)123     public void dispatchEvent(XEvent xev) {
124         switch(xev.get_type()) {
125           case XConstants.ClientMessage:
126               handleClientMessage(xev);
127               break;
128         }
129     }
130 
forwardKeyEvent(long child, KeyEvent e)131     void forwardKeyEvent(long child, KeyEvent e) {
132         byte[] bdata = AWTAccessor.getAWTEventAccessor().getBData(e);
133         long data = Native.toData(bdata);
134         if (data == 0) {
135             return;
136         }
137         XKeyEvent ke = new XKeyEvent(data);
138         ke.set_window(child);
139         XToolkit.awtLock();
140         try {
141             XlibWrapper.XSendEvent(XToolkit.getDisplay(), child, false, XConstants.NoEventMask, data);
142         }
143         finally {
144             XToolkit.awtUnlock();
145         }
146         XlibWrapper.unsafe.freeMemory(data);
147     }
148 }
149