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