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.Level.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 if (log.isLoggable(PlatformLogger.Level.FINE)) { 59 log.fine("Current state of the window {0} is {1}", window, old_state); 60 } 61 if ((state & Frame.MAXIMIZED_VERT) != 0) { 62 old_state.add(XA_NET_WM_STATE_MAXIMIZED_VERT); 63 } else { 64 old_state.remove(XA_NET_WM_STATE_MAXIMIZED_VERT); 65 } 66 if ((state & Frame.MAXIMIZED_HORIZ) != 0) { 67 old_state.add(XA_NET_WM_STATE_MAXIMIZED_HORZ); 68 } else { 69 old_state.remove(XA_NET_WM_STATE_MAXIMIZED_HORZ); 70 } 71 if (log.isLoggable(PlatformLogger.Level.FINE)) { 72 log.fine("Setting initial state of the window {0} to {1}", window, old_state); 73 } 74 window.setNETWMState(old_state); 75 } 76 77 private void requestState(XWindowPeer window, int state) { 78 /* 79 * We have to use toggle for maximization because of transitions 80 * from maximization in one direction only to maximization in the 81 * other direction only. 82 */ 83 int old_net_state = getState(window); 84 int max_changed = (state ^ old_net_state) & (Frame.MAXIMIZED_BOTH); 85 86 XClientMessageEvent req = new XClientMessageEvent(); 87 try { 88 switch(max_changed) { 89 case 0: 90 return; 91 case Frame.MAXIMIZED_HORIZ: 92 req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom()); 93 req.set_data(2, 0); 94 break; 95 case Frame.MAXIMIZED_VERT: 96 req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom()); 97 req.set_data(2, 0); 98 break; 99 case Frame.MAXIMIZED_BOTH: 100 req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom()); 101 req.set_data(2, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom()); 102 break; 103 default: 104 return; 105 } 106 if (log.isLoggable(PlatformLogger.Level.FINE)) { 107 log.fine("Requesting state on " + window + " for " + state); 108 } 109 req.set_type((int)XConstants.ClientMessage); 110 req.set_window(window.getWindow()); 111 req.set_message_type(XA_NET_WM_STATE.getAtom()); 112 req.set_format(32); 113 req.set_data(0, _NET_WM_STATE_TOGGLE); 114 XToolkit.awtLock(); 115 try { 116 XlibWrapper.XSendEvent(XToolkit.getDisplay(), 117 XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()), 118 false, 119 XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask, 120 req.pData); 121 } 122 finally { 123 XToolkit.awtUnlock(); 124 } 125 } finally { 126 req.dispose(); 127 } 128 } 129 130 public int getState(XWindowPeer window) { 131 return getStateImpl(window); 132 } 133 134 /* 135 * New "NET" WM spec: _NET_WM_STATE/Atom[] 136 */ 137 int getStateImpl(XWindowPeer window) { 138 XAtomList net_wm_state = window.getNETWMState(); 139 if (net_wm_state.size() == 0) { 140 return Frame.NORMAL; 141 } 142 int java_state = Frame.NORMAL; 143 if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_VERT)) { 144 java_state |= Frame.MAXIMIZED_VERT; 145 } 146 if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_HORZ)) { 147 java_state |= Frame.MAXIMIZED_HORIZ; 148 } 149 return java_state; 150 } 151 152 public boolean isStateChange(XPropertyEvent e) { 153 boolean res = doStateProtocol() && (e.get_atom() == XA_NET_WM_STATE.getAtom()) ; 154 155 if (res) { 156 // Since state change happened, reset our cached state. It will be re-read by getState 157 XWindowPeer wpeer = (XWindowPeer)XToolkit.windowToXWindow(e.get_window()); 158 wpeer.setNETWMState(null); 159 } 160 return res; 161 } 162 163 /* 164 * Work around for 4775545. 165 */ 166 public void unshadeKludge(XWindowPeer window) { 167 XAtomList net_wm_state = window.getNETWMState(); 168 net_wm_state.remove(XA_NET_WM_STATE_SHADED); 169 window.setNETWMState(net_wm_state); 170 } 171 172 /** 173 * XLayerProtocol 174 */ 175 public boolean supportsLayer(int layer) { 176 return ((layer == LAYER_ALWAYS_ON_TOP) || (layer == LAYER_NORMAL)) && doLayerProtocol(); 177 } 178 179 public void requestState(XWindow window, XAtom state, boolean isAdd) { 180 XClientMessageEvent req = new XClientMessageEvent(); 181 try { 182 req.set_type((int)XConstants.ClientMessage); 183 req.set_window(window.getWindow()); 184 req.set_message_type(XA_NET_WM_STATE.getAtom()); 185 req.set_format(32); 186 req.set_data(0, isAdd ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE); 187 req.set_data(1, state.getAtom()); 188 // Fix for 6735584: req.data[2] must be set to 0 when only one property is changed 189 req.set_data(2, 0); 190 if (log.isLoggable(PlatformLogger.Level.FINE)) { 191 log.fine("Setting _NET_STATE atom {0} on {1} for {2}", state, window, Boolean.valueOf(isAdd)); 192 } 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 if (log.isLoggable(PlatformLogger.Level.FINER)) { 218 log.finer("Window visibility is: withdrawn={0}, visible={1}, mapped={2} showing={3}", 219 Boolean.valueOf(window.isWithdrawn()), Boolean.valueOf(window.isVisible()), 220 Boolean.valueOf(window.isMapped()), Boolean.valueOf(window.isShowing())); 221 } 222 if (window.isShowing()) { 223 requestState(window, state, set); 224 } else { 225 XAtomList net_wm_state = window.getNETWMState(); 226 if (log.isLoggable(PlatformLogger.Level.FINER)) { 227 log.finer("Current state on {0} is {1}", window, net_wm_state); 228 } 229 if (!set) { 230 net_wm_state.remove(state); 231 } else { 232 net_wm_state.add(state); 233 } 234 if (log.isLoggable(PlatformLogger.Level.FINE)) { 235 log.fine("Setting states on {0} to {1}", window, 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 public final XAtom XA_NET_WM_WINDOW_TYPE = XAtom.get("_NET_WM_WINDOW_TYPE"); 272 public final XAtom XA_NET_WM_WINDOW_TYPE_NORMAL = XAtom.get("_NET_WM_WINDOW_TYPE_NORMAL"); 273 public final XAtom XA_NET_WM_WINDOW_TYPE_DIALOG = XAtom.get("_NET_WM_WINDOW_TYPE_DIALOG"); 274 public final XAtom XA_NET_WM_WINDOW_TYPE_UTILITY = XAtom.get("_NET_WM_WINDOW_TYPE_UTILITY"); 275 public final XAtom XA_NET_WM_WINDOW_TYPE_POPUP_MENU = XAtom.get("_NET_WM_WINDOW_TYPE_POPUP_MENU"); 276 277 XAtom XA_NET_WM_WINDOW_OPACITY = XAtom.get("_NET_WM_WINDOW_OPACITY"); 278 279 /* For _NET_WM_STATE ClientMessage requests */ 280 final static int _NET_WM_STATE_REMOVE =0; /* remove/unset property */ 281 final static int _NET_WM_STATE_ADD =1; /* add/set property */ 282 final static int _NET_WM_STATE_TOGGLE =2; /* toggle property */ 283 284 boolean supportChecked = false; 285 long NetWindow = 0; 286 void detect() { 287 if (supportChecked) { 288 // TODO: How about detecting WM-restart or exit? 289 return; 290 } 291 NetWindow = checkAnchor(XA_NET_SUPPORTING_WM_CHECK, XAtom.XA_WINDOW); 292 supportChecked = true; 293 if (log.isLoggable(PlatformLogger.Level.FINE)) { 294 log.fine("### " + this + " is active: " + (NetWindow != 0)); 295 } 296 } 297 298 boolean active() { 299 detect(); 300 return NetWindow != 0; 301 } 302 303 boolean doStateProtocol() { 304 boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE); 305 if (stateLog.isLoggable(PlatformLogger.Level.FINER)) { 306 stateLog.finer("doStateProtocol() returns " + res); 307 } 308 return res; 309 } 310 311 boolean doLayerProtocol() { 312 boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_ABOVE); 313 return res; 314 } 315 316 boolean doModalityProtocol() { 317 boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_MODAL); 318 return res; 319 } 320 321 boolean doOpacityProtocol() { 322 boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_WINDOW_OPACITY); 323 return res; 324 } 325 326 boolean isWMName(String name) { 327 if (!active()) { 328 return false; 329 } 330 String net_wm_name_string = getWMName(); 331 if (net_wm_name_string == null) { 332 return false; 333 } 334 if (log.isLoggable(PlatformLogger.Level.FINE)) { 335 log.fine("### WM_NAME = " + net_wm_name_string); 336 } 337 return net_wm_name_string.startsWith(name); 338 } 339 340 String net_wm_name_cache; 341 public String getWMName() { 342 if (!active()) { 343 return null; 344 } 345 346 if (net_wm_name_cache != null) { 347 return net_wm_name_cache; 348 } 349 350 /* 351 * Check both UTF8_STRING and STRING. We only call this function 352 * with ASCII names and UTF8 preserves ASCII bit-wise. wm-spec 353 * mandates UTF8_STRING for _NET_WM_NAME but at least sawfish-1.0 354 * still uses STRING. (mmm, moving targets...). 355 */ 356 String charSet = "UTF8"; 357 byte[] net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XA_UTF8_STRING.getAtom()); 358 if (net_wm_name == null) { 359 net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XAtom.XA_STRING); 360 charSet = "ASCII"; 361 } 362 363 if (net_wm_name == null) { 364 return null; 365 } 366 try { 367 net_wm_name_cache = new String(net_wm_name, charSet); 368 return net_wm_name_cache; 369 } catch (java.io.UnsupportedEncodingException uex) { 370 return null; 371 } 372 } 373 374 /** 375 * Sets _NET_WM_ICON property on the window using the List of XIconInfo 376 * If icons is null or empty list, removes _NET_WM_ICON property 377 */ 378 public void setWMIcons(XWindowPeer window, java.util.List<XIconInfo> icons) { 379 if (window == null) return; 380 381 XAtom iconsAtom = XAtom.get("_NET_WM_ICON"); 382 if (icons == null) { 383 iconsAtom.DeleteProperty(window); 384 return; 385 } 386 387 int length = 0; 388 for (XIconInfo ii : icons) { 389 length += ii.getRawLength(); 390 } 391 int cardinalSize = (XlibWrapper.dataModel == 32) ? 4 : 8; 392 int bufferSize = length * cardinalSize; 393 394 if (bufferSize != 0) { 395 long buffer = XlibWrapper.unsafe.allocateMemory(bufferSize); 396 try { 397 long ptr = buffer; 398 for (XIconInfo ii : icons) { 399 int size = ii.getRawLength() * cardinalSize; 400 if (XlibWrapper.dataModel == 32) { 401 XlibWrapper.copyIntArray(ptr, ii.getIntData(), size); 402 } else { 403 XlibWrapper.copyLongArray(ptr, ii.getLongData(), size); 404 } 405 ptr += size; 406 } 407 iconsAtom.setAtomData(window.getWindow(), XAtom.XA_CARDINAL, buffer, bufferSize/Native.getCard32Size()); 408 } finally { 409 XlibWrapper.unsafe.freeMemory(buffer); 410 } 411 } else { 412 iconsAtom.DeleteProperty(window); 413 } 414 } 415 416 public boolean isWMStateNetHidden(XWindowPeer window) { 417 if (!doStateProtocol()) { 418 return false; 419 } 420 XAtomList state = window.getNETWMState(); 421 return (state != null && state.size() != 0 && state.contains(XA_NET_WM_STATE_HIDDEN)); 422 } 423 } --- EOF ---