1 /*
   2  * Copyright (c) 2005, 2008, 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.awt.peer.SystemTrayPeer;
  30 import sun.awt.SunToolkit;
  31 import sun.awt.AppContext;
  32 import sun.awt.AWTAccessor;
  33 import sun.util.logging.PlatformLogger;
  34 
  35 public class XSystemTrayPeer implements SystemTrayPeer, XMSelectionListener {
  36     private static final PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XSystemTrayPeer");
  37 
  38     SystemTray target;
  39     static XSystemTrayPeer peerInstance; // there is only one SystemTray peer per application
  40 
  41     private volatile boolean available;
  42     private final XMSelection selection = new XMSelection("_NET_SYSTEM_TRAY");
  43 
  44     private static final int SCREEN = 0;
  45     private static final String SYSTEM_TRAY_PROPERTY_NAME = "systemTray";
  46     private static final XAtom _NET_SYSTEM_TRAY = XAtom.get("_NET_SYSTEM_TRAY_S" + SCREEN);
  47     private static final XAtom _XEMBED_INFO = XAtom.get("_XEMBED_INFO");
  48     private static final XAtom _NET_SYSTEM_TRAY_OPCODE = XAtom.get("_NET_SYSTEM_TRAY_OPCODE");
  49     private static final XAtom _NET_WM_ICON = XAtom.get("_NET_WM_ICON");
  50     private static final long SYSTEM_TRAY_REQUEST_DOCK = 0;
  51 
  52     XSystemTrayPeer(SystemTray target) {
  53         this.target = target;
  54         peerInstance = this;
  55 
  56         selection.addSelectionListener(this);
  57 
  58         long selection_owner = selection.getOwner(SCREEN);
  59         available = (selection_owner != XConstants.None);
  60 
  61         log.fine(" check if system tray is available. selection owner: " + selection_owner);
  62     }
  63 
  64     public void ownerChanged(int screen, XMSelection sel, long newOwner, long data, long timestamp) {
  65         if (screen != SCREEN) {
  66             return;
  67         }
  68         if (!available) {
  69             available = true;
  70             firePropertyChange(SYSTEM_TRAY_PROPERTY_NAME, null, target);
  71         } else {
  72             removeTrayPeers();
  73         }
  74         createTrayPeers();
  75     }
  76 
  77     public void ownerDeath(int screen, XMSelection sel, long deadOwner) {
  78         if (screen != SCREEN) {
  79             return;
  80         }
  81         if (available) {
  82             available = false;
  83             firePropertyChange(SYSTEM_TRAY_PROPERTY_NAME, target, null);
  84             removeTrayPeers();
  85         }
  86     }
  87 
  88     public void selectionChanged(int screen, XMSelection sel, long owner, XPropertyEvent event) {
  89     }
  90 
  91     public Dimension getTrayIconSize() {
  92         return new Dimension(XTrayIconPeer.TRAY_ICON_HEIGHT, XTrayIconPeer.TRAY_ICON_WIDTH);
  93     }
  94 
  95     boolean isAvailable() {
  96         return available;
  97     }
  98 
  99     void dispose() {
 100         selection.removeSelectionListener(this);
 101     }
 102 
 103     // ***********************************************************************
 104     // ***********************************************************************
 105 
 106     void addTrayIcon(XTrayIconPeer tiPeer) throws AWTException {
 107         long selection_owner = selection.getOwner(SCREEN);
 108 
 109         log.fine(" send SYSTEM_TRAY_REQUEST_DOCK message to owner: " + selection_owner);
 110 
 111         if (selection_owner == XConstants.None) {
 112             throw new AWTException("TrayIcon couldn't be displayed.");
 113         }
 114 
 115         long tray_window = tiPeer.getWindow();
 116         long data[] = new long[] {XEmbedHelper.XEMBED_VERSION, XEmbedHelper.XEMBED_MAPPED};
 117         long data_ptr = Native.card32ToData(data);
 118 
 119         _XEMBED_INFO.setAtomData(tray_window, data_ptr, data.length);
 120 
 121         sendMessage(selection_owner, SYSTEM_TRAY_REQUEST_DOCK, tray_window, 0, 0);
 122     }
 123 
 124     void sendMessage(long win, long msg, long data1, long data2, long data3) {
 125         XClientMessageEvent xev = new XClientMessageEvent();
 126 
 127         try {
 128             xev.set_type(XConstants.ClientMessage);
 129             xev.set_window(win);
 130             xev.set_format(32);
 131             xev.set_message_type(_NET_SYSTEM_TRAY_OPCODE.getAtom());
 132             xev.set_data(0, 0);
 133             xev.set_data(1, msg);
 134             xev.set_data(2, data1);
 135             xev.set_data(3, data2);
 136             xev.set_data(4, data3);
 137 
 138             XToolkit.awtLock();
 139             try {
 140                 XlibWrapper.XSendEvent(XToolkit.getDisplay(), win, false,
 141                                        XConstants.NoEventMask, xev.pData);
 142             } finally {
 143                 XToolkit.awtUnlock();
 144             }
 145         } finally {
 146             xev.dispose();
 147         }
 148     }
 149 
 150     static XSystemTrayPeer getPeerInstance() {
 151         return peerInstance;
 152     }
 153 
 154     private void firePropertyChange(final String propertyName,
 155                                     final Object oldValue,
 156                                     final Object newValue) {
 157         Runnable runnable = new Runnable() {
 158                 public void run() {
 159                     AWTAccessor.getSystemTrayAccessor()
 160                         .firePropertyChange(target, propertyName, oldValue, newValue);
 161                 }
 162             };
 163         invokeOnEachAppContext(runnable);
 164     }
 165 
 166     private void createTrayPeers() {
 167         Runnable runnable = new Runnable() {
 168                 public void run() {
 169                     TrayIcon[] icons = target.getTrayIcons();
 170                     try {
 171                         for (TrayIcon ti : icons) {
 172                             AWTAccessor.getTrayIconAccessor().addNotify(ti);
 173                         }
 174                     } catch (AWTException e) {
 175                     }
 176                 }
 177             };
 178         invokeOnEachAppContext(runnable);
 179     }
 180 
 181     private void removeTrayPeers() {
 182         Runnable runnable = new Runnable() {
 183                 public void run() {
 184                     TrayIcon[] icons = target.getTrayIcons();
 185                     for (TrayIcon ti : icons) {
 186                         AWTAccessor.getTrayIconAccessor().removeNotify(ti);
 187                     }
 188                 }
 189             };
 190         invokeOnEachAppContext(runnable);
 191     }
 192 
 193     private void invokeOnEachAppContext(Runnable runnable) {
 194         for (AppContext appContext : AppContext.getAppContexts()) {
 195             SunToolkit.invokeLaterOnAppContext(appContext, runnable);
 196         }
 197     }
 198 
 199 }