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.*;
  29 import java.util.HashMap;
  30 import java.awt.event.KeyEvent;
  31 import java.lang.reflect.*;
  32 import sun.awt.AWTAccessor;
  33 
  34 public class XEmbeddingContainer extends XEmbedHelper implements XEventDispatcher {
  35     HashMap<Long, java.awt.peer.ComponentPeer> children = new HashMap<>();
  36 
  37     XEmbeddingContainer() {
  38     }
  39 
  40     XWindow embedder;
  41     void install(XWindow embedder) {
  42         this.embedder = embedder;
  43         XToolkit.addEventDispatcher(embedder.getWindow(), this);
  44     }
  45     void deinstall() {
  46         XToolkit.removeEventDispatcher(embedder.getWindow(), this);
  47     }
  48 
  49     @SuppressWarnings("deprecation")
  50     void add(long child) {
  51         if (checkXEmbed(child)) {
  52             Component proxy = createChildProxy(child);
  53             ((Container)embedder.getTarget()).add("Center", proxy);
  54             if (proxy.getPeer() != null) {
  55                 children.put(Long.valueOf(child), proxy.getPeer());
  56             }
  57         }
  58     }
  59 
  60     Component createChildProxy(long child) {
  61         return new XEmbedChildProxy(this, child);
  62     }
  63     void notifyChildEmbedded(long child) {
  64         sendMessage(child, XEMBED_EMBEDDED_NOTIFY, embedder.getWindow(), XEMBED_VERSION, 0);
  65     }
  66 
  67     void childResized(Component child) {
  68     }
  69 
  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 
  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 
 102     void focusGained(long child) {
 103         sendMessage(child, XEMBED_FOCUS_IN, XEMBED_FOCUS_CURRENT, 0, 0);
 104     }
 105     void focusLost(long child) {
 106         sendMessage(child, XEMBED_FOCUS_OUT);
 107     }
 108 
 109     XEmbedChildProxyPeer getChild(long child) {
 110         return (XEmbedChildProxyPeer)children.get(Long.valueOf(child));
 111     }
 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     }
 123     public void dispatchEvent(XEvent xev) {
 124         switch(xev.get_type()) {
 125           case XConstants.ClientMessage:
 126               handleClientMessage(xev);
 127               break;
 128         }
 129     }
 130 
 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 }