1 /*
   2  * Copyright (c) 2003, 2009, 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 package sun.awt.X11;
  28 
  29 import java.awt.*;
  30 import java.util.logging.Level;
  31 import java.util.logging.Logger;
  32 import java.util.logging.LogManager;
  33 import java.awt.*;
  34 import java.awt.image.*;
  35 import java.util.*;
  36 
  37 class XNETProtocol extends XProtocol implements XStateProtocol, XLayerProtocol {
  38     final static Logger log = Logger.getLogger("sun.awt.X11.XNETProtocol");
  39     private final static Logger iconLog = Logger.getLogger("sun.awt.X11.icon.XNETProtocol");
  40 
  41     /**
  42      * XStateProtocol
  43      */
  44     public boolean supportsState(int state) {
  45         return doStateProtocol() ; // TODO - check for Frame constants
  46     }
  47 
  48     public void setState(XWindowPeer window, int state) {
  49         if (log.isLoggable(Level.FINE)) log.fine("Setting state of " + window + " to " + state);
  50         if (window.isShowing()) {
  51             requestState(window, state);
  52         } else {
  53             setInitialState(window, state);
  54         }
  55     }
  56 
  57     private void setInitialState(XWindowPeer window, int state) {
  58         XAtomList old_state = window.getNETWMState();
  59 
  60         if (log.isLoggable(Level.FINE)) {
  61             log.log(Level.FINE, "Current state of the window {0} is {1}",
  62                     new Object[] {String.valueOf(window), String.valueOf(old_state)});
  63         }
  64         if ((state & Frame.MAXIMIZED_VERT) != 0) {
  65             old_state.add(XA_NET_WM_STATE_MAXIMIZED_VERT);
  66         } else {
  67             old_state.remove(XA_NET_WM_STATE_MAXIMIZED_VERT);
  68         }
  69         if ((state & Frame.MAXIMIZED_HORIZ) != 0) {
  70             old_state.add(XA_NET_WM_STATE_MAXIMIZED_HORZ);
  71         } else {
  72             old_state.remove(XA_NET_WM_STATE_MAXIMIZED_HORZ);
  73         }
  74         if (log.isLoggable(Level.FINE)) {
  75             log.log(Level.FINE, "Setting initial state of the window {0} to {1}",
  76                                 new Object[] {String.valueOf(window), String.valueOf(old_state)});
  77         }
  78         window.setNETWMState(old_state);
  79     }
  80 
  81     private void requestState(XWindowPeer window, int state) {
  82         /*
  83          * We have to use toggle for maximization because of transitions
  84          * from maximization in one direction only to maximization in the
  85          * other direction only.
  86          */
  87         int old_net_state = getState(window);
  88         int max_changed = (state ^ old_net_state) & (Frame.MAXIMIZED_BOTH);
  89 
  90         XClientMessageEvent req = new XClientMessageEvent();
  91         try {
  92             switch(max_changed) {
  93               case 0:
  94                   return;
  95               case Frame.MAXIMIZED_HORIZ:
  96                   req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom());
  97                   req.set_data(2, 0);
  98                   break;
  99               case Frame.MAXIMIZED_VERT:
 100                   req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom());
 101                   req.set_data(2, 0);
 102                   break;
 103               case Frame.MAXIMIZED_BOTH:
 104                   req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom());
 105                   req.set_data(2, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom());
 106                   break;
 107               default:
 108                   return;
 109             }
 110             if (log.isLoggable(Level.FINE)) log.fine("Requesting state on " + window + " for " + state);
 111             req.set_type((int)XlibWrapper.ClientMessage);
 112             req.set_window(window.getWindow());
 113             req.set_message_type(XA_NET_WM_STATE.getAtom());
 114             req.set_format(32);
 115             req.set_data(0, _NET_WM_STATE_TOGGLE);
 116             XToolkit.awtLock();
 117             try {
 118                 XlibWrapper.XSendEvent(XToolkit.getDisplay(),
 119                         XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
 120                         false,
 121                         XlibWrapper.SubstructureRedirectMask | XlibWrapper.SubstructureNotifyMask,
 122                         req.pData);
 123             }
 124             finally {
 125                 XToolkit.awtUnlock();
 126             }
 127         } finally {
 128             req.dispose();
 129         }
 130     }
 131 
 132     public int getState(XWindowPeer window) {
 133         return getStateImpl(window);
 134     }
 135 
 136     /*
 137      * New "NET" WM spec: _NET_WM_STATE/Atom[]
 138      */
 139     int getStateImpl(XWindowPeer window) {
 140         XAtomList net_wm_state = window.getNETWMState();
 141         if (net_wm_state.size() == 0) {
 142             return Frame.NORMAL;
 143         }
 144         int java_state = Frame.NORMAL;
 145         if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_VERT)) {
 146             java_state |= Frame.MAXIMIZED_VERT;
 147         }
 148         if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_HORZ)) {
 149             java_state |= Frame.MAXIMIZED_HORIZ;
 150         }
 151         return java_state;
 152     }
 153 
 154     public boolean isStateChange(XPropertyEvent e) {
 155         boolean res = doStateProtocol() && (e.get_atom() == XA_NET_WM_STATE.getAtom()) ;
 156 
 157         if (res) {
 158             // Since state change happened, reset our cached state.  It will be re-read by getState
 159             XWindowPeer wpeer = (XWindowPeer)XToolkit.windowToXWindow(e.get_window());
 160             wpeer.setNETWMState(null);
 161         }
 162         return res;
 163     }
 164 
 165     /*
 166      * Work around for 4775545.
 167      */
 168     public void unshadeKludge(XWindowPeer window) {
 169         XAtomList net_wm_state = window.getNETWMState();
 170         net_wm_state.remove(XA_NET_WM_STATE_SHADED);
 171         window.setNETWMState(net_wm_state);
 172     }
 173 
 174     /**
 175      * XLayerProtocol
 176      */
 177     public boolean supportsLayer(int layer) {
 178         return ((layer == LAYER_ALWAYS_ON_TOP) || (layer == LAYER_NORMAL)) && doLayerProtocol();
 179     }
 180 
 181     public void requestState(XWindow window, XAtom state, boolean isAdd) {
 182         XClientMessageEvent req = new XClientMessageEvent();
 183         try {
 184             req.set_type((int)XConstants.ClientMessage);
 185             req.set_window(window.getWindow());
 186             req.set_message_type(XA_NET_WM_STATE.getAtom());
 187             req.set_format(32);
 188             req.set_data(0, isAdd ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE);
 189             req.set_data(1, state.getAtom());
 190             // Fix for 6735584: req.data[2] must be set to 0 when only one property is changed
 191             req.set_data(2, 0);
 192             log.log(Level.FINE, "Setting _NET_STATE atom {0} on {1} for {2}", new Object[] {state, window, Boolean.valueOf(isAdd)});
 193             XToolkit.awtLock();
 194             try {
 195                 XlibWrapper.XSendEvent(XToolkit.getDisplay(),
 196                         XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
 197                         false,
 198                         XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask,
 199                         req.pData);
 200             }
 201             finally {
 202                 XToolkit.awtUnlock();
 203             }
 204         } finally {
 205             req.dispose();
 206         }
 207     }
 208 
 209     /**
 210      * Helper function to set/reset one state in NET_WM_STATE
 211      * If window is showing then it uses ClientMessage, otherwise adjusts NET_WM_STATE list
 212      * @param window Window which NET_WM_STATE property is being modified
 213      * @param state State atom to be set/reset
 214      * @param reset Indicates operation, 'set' if false, 'reset' if true
 215      */
 216     private void setStateHelper(XWindowPeer window, XAtom state, boolean set) {
 217         log.log(Level.FINER, "Window visibility is: withdrawn={0}, visible={1}, mapped={2} showing={3}",
 218                 new Object[] {Boolean.valueOf(window.isWithdrawn()), Boolean.valueOf(window.isVisible()),
 219                               Boolean.valueOf(window.isMapped()), Boolean.valueOf(window.isShowing())});
 220         if (window.isShowing()) {
 221             requestState(window, state, set);
 222         } else {
 223             XAtomList net_wm_state = window.getNETWMState();
 224             if (log.isLoggable(Level.FINE)) {
 225                 log.log(Level.FINE, "Current state on {0} is {1}",
 226                         new Object[] {String.valueOf(window), String.valueOf(net_wm_state)});
 227             }
 228             if (!set) {
 229                 net_wm_state.remove(state);
 230             } else {
 231                 net_wm_state.add(state);
 232             }
 233             if (log.isLoggable(Level.FINE)) {
 234                 log.log(Level.FINE, "Setting states on {0} to {1}",
 235                         new Object[] {String.valueOf(window), String.valueOf(net_wm_state)});
 236             }
 237             window.setNETWMState(net_wm_state);
 238         }
 239         XToolkit.XSync();
 240     }
 241 
 242     public void setLayer(XWindowPeer window, int layer) {
 243         setStateHelper(window, XA_NET_WM_STATE_ABOVE, layer == LAYER_ALWAYS_ON_TOP);
 244     }
 245 
 246     /* New "netwm" spec from www.freedesktop.org */
 247     XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING");   /* like STRING but encoding is UTF-8 */
 248     XAtom XA_NET_SUPPORTING_WM_CHECK = XAtom.get("_NET_SUPPORTING_WM_CHECK");
 249     XAtom XA_NET_SUPPORTED = XAtom.get("_NET_SUPPORTED");      /* list of protocols (property of root) */
 250     XAtom XA_NET_WM_NAME = XAtom.get("_NET_WM_NAME");  /* window property */
 251     XAtom XA_NET_WM_STATE = XAtom.get("_NET_WM_STATE");/* both window property and request */
 252 
 253 /*
 254  * _NET_WM_STATE is a list of atoms.
 255  * NB: Standard spelling is "HORZ" (yes, without an 'I'), but KDE2
 256  * uses misspelled "HORIZ" (see KDE bug #20229).  This was fixed in
 257  * KDE 2.2.  Under earlier versions of KDE2 horizontal and full
 258  * maximization doesn't work .
 259  */
 260     XAtom XA_NET_WM_STATE_MAXIMIZED_HORZ = XAtom.get("_NET_WM_STATE_MAXIMIZED_HORZ");
 261     XAtom XA_NET_WM_STATE_MAXIMIZED_VERT = XAtom.get("_NET_WM_STATE_MAXIMIZED_VERT");
 262     XAtom XA_NET_WM_STATE_SHADED = XAtom.get("_NET_WM_STATE_SHADED");
 263     XAtom XA_NET_WM_STATE_ABOVE = XAtom.get("_NET_WM_STATE_ABOVE");
 264     XAtom XA_NET_WM_STATE_MODAL = XAtom.get("_NET_WM_STATE_MODAL");
 265     XAtom XA_NET_WM_STATE_FULLSCREEN = XAtom.get("_NET_WM_STATE_FULLSCREEN");
 266     XAtom XA_NET_WM_STATE_BELOW = XAtom.get("_NET_WM_STATE_BELOW");
 267     XAtom XA_NET_WM_STATE_HIDDEN = XAtom.get("_NET_WM_STATE_HIDDEN");
 268     XAtom XA_NET_WM_STATE_SKIP_TASKBAR = XAtom.get("_NET_WM_STATE_SKIP_TASKBAR");
 269     XAtom XA_NET_WM_STATE_SKIP_PAGER = XAtom.get("_NET_WM_STATE_SKIP_PAGER");
 270 
 271     XAtom XA_NET_WM_WINDOW_TYPE = XAtom.get("_NET_WM_WINDOW_TYPE");
 272     XAtom XA_NET_WM_WINDOW_TYPE_DIALOG = XAtom.get("_NET_WM_WINDOW_TYPE_DIALOG");
 273 
 274     XAtom XA_NET_WM_WINDOW_OPACITY = XAtom.get("_NET_WM_WINDOW_OPACITY");
 275 
 276 /* For _NET_WM_STATE ClientMessage requests */
 277     final static int _NET_WM_STATE_REMOVE      =0; /* remove/unset property */
 278     final static int _NET_WM_STATE_ADD         =1; /* add/set property      */
 279     final static int _NET_WM_STATE_TOGGLE      =2; /* toggle property       */
 280 
 281     boolean supportChecked = false;
 282     long NetWindow = 0;
 283     void detect() {
 284         if (supportChecked) {
 285             // TODO: How about detecting WM-restart or exit?
 286             return;
 287         }
 288         NetWindow = checkAnchor(XA_NET_SUPPORTING_WM_CHECK, XAtom.XA_WINDOW);
 289         supportChecked = true;
 290         if (log.isLoggable(Level.FINE)) log.fine("### " + this + " is active: " + (NetWindow != 0));
 291     }
 292 
 293     boolean active() {
 294         detect();
 295         return NetWindow != 0;
 296     }
 297 
 298     boolean doStateProtocol() {
 299         boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE);
 300         return res;
 301     }
 302 
 303     boolean doLayerProtocol() {
 304         boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_ABOVE);
 305         return res;
 306     }
 307 
 308     boolean doModalityProtocol() {
 309         boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_MODAL);
 310         return res;
 311     }
 312 
 313     boolean doOpacityProtocol() {
 314         boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_WINDOW_OPACITY);
 315         return res;
 316     }
 317 
 318     boolean isWMName(String name) {
 319         if (!active()) {
 320             return false;
 321         }
 322         String net_wm_name_string = getWMName();
 323         if (net_wm_name_string == null) {
 324             return false;
 325         }
 326         if (log.isLoggable(Level.FINE)) log.fine("### WM_NAME = " + net_wm_name_string);
 327         return net_wm_name_string.startsWith(name);
 328     }
 329 
 330     String net_wm_name_cache;
 331     public String getWMName() {
 332         if (!active()) {
 333             return null;
 334         }
 335 
 336         if (net_wm_name_cache != null) {
 337             return net_wm_name_cache;
 338         }
 339 
 340         /*
 341          * Check both UTF8_STRING and STRING.  We only call this function
 342          * with ASCII names and UTF8 preserves ASCII bit-wise.  wm-spec
 343          * mandates UTF8_STRING for _NET_WM_NAME but at least sawfish-1.0
 344          * still uses STRING.  (mmm, moving targets...).
 345          */
 346         String charSet = "UTF8";
 347         byte[] net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XA_UTF8_STRING.getAtom());
 348         if (net_wm_name == null) {
 349             net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XAtom.XA_STRING);
 350             charSet = "ASCII";
 351         }
 352 
 353         if (net_wm_name == null) {
 354             return null;
 355         }
 356         try {
 357             net_wm_name_cache = new String(net_wm_name, charSet);
 358             return net_wm_name_cache;
 359         } catch (java.io.UnsupportedEncodingException uex) {
 360             return null;
 361         }
 362     }
 363 
 364     /**
 365      * Sets _NET_WM_ICON property on the window using the List of XIconInfo
 366      * If icons is null or empty list, removes _NET_WM_ICON property
 367      */
 368     public void setWMIcons(XWindowPeer window, java.util.List<XIconInfo> icons) {
 369         if (window == null) return;
 370 
 371         XAtom iconsAtom = XAtom.get("_NET_WM_ICON");
 372         if (icons == null) {
 373             iconsAtom.DeleteProperty(window);
 374             return;
 375         }
 376 
 377         int length = 0;
 378         for (XIconInfo ii : icons) {
 379             length += ii.getRawLength();
 380         }
 381         int cardinalSize = (XlibWrapper.dataModel == 32) ? 4 : 8;
 382         int bufferSize = length * cardinalSize;
 383 
 384         if (bufferSize != 0) {
 385             long buffer = XlibWrapper.unsafe.allocateMemory(bufferSize);
 386             try {
 387                 long ptr = buffer;
 388                 for (XIconInfo ii : icons) {
 389                     int size = ii.getRawLength() * cardinalSize;
 390                     if (XlibWrapper.dataModel == 32) {
 391                         XlibWrapper.copyIntArray(ptr, ii.getIntData(), size);
 392                     } else {
 393                         XlibWrapper.copyLongArray(ptr, ii.getLongData(), size);
 394                     }
 395                     ptr += size;
 396                 }
 397                 iconsAtom.setAtomData(window.getWindow(), XAtom.XA_CARDINAL, buffer, bufferSize/Native.getCard32Size());
 398             } finally {
 399                 XlibWrapper.unsafe.freeMemory(buffer);
 400             }
 401         } else {
 402             iconsAtom.DeleteProperty(window);
 403         }
 404     }
 405 
 406     public boolean isWMStateNetHidden(XWindowPeer window) {
 407         if (!doStateProtocol()) {
 408             return false;
 409         }
 410         XAtomList state = window.getNETWMState();
 411         return (state != null && state.size() != 0 && state.contains(XA_NET_WM_STATE_HIDDEN));
 412     }
 413 }