jdk/src/solaris/classes/sun/awt/X11/XNETProtocol.java
author dav
Mon, 07 Apr 2008 16:52:51 +0400
changeset 439 3488710b02f8
parent 107 ed0c7cfb3666
child 1189 ee54b5ce2113
permissions -rw-r--r--
6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern Summary: Access to interface's fiels via their name rather then implementation Reviewed-by: volk, son
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
107
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
107
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
    29
import java.awt.Frame;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.logging.Level;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.logging.Logger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
107
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
    33
final class XNETProtocol extends XProtocol implements XStateProtocol, XLayerProtocol
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
    34
{
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
    35
    private final static Logger log = Logger.getLogger("sun.awt.X11.XNETProtocol");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private final static Logger iconLog = Logger.getLogger("sun.awt.X11.icon.XNETProtocol");
107
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
    37
    private static Logger stateLog = Logger.getLogger("sun.awt.X11.states.XNETProtocol");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * XStateProtocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public boolean supportsState(int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        return doStateProtocol() ; // TODO - check for Frame constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public void setState(XWindowPeer window, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if (log.isLoggable(Level.FINE)) log.fine("Setting state of " + window + " to " + state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        if (window.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            requestState(window, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            setInitialState(window, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private void setInitialState(XWindowPeer window, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        XAtomList old_state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        log.log(Level.FINE, "Current state of the window {0} is {1}", new Object[] {window, old_state});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        if ((state & Frame.MAXIMIZED_VERT) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            old_state.add(XA_NET_WM_STATE_MAXIMIZED_VERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            old_state.remove(XA_NET_WM_STATE_MAXIMIZED_VERT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if ((state & Frame.MAXIMIZED_HORIZ) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            old_state.add(XA_NET_WM_STATE_MAXIMIZED_HORZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            old_state.remove(XA_NET_WM_STATE_MAXIMIZED_HORZ);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        log.log(Level.FINE, "Setting initial state of the window {0} to {1}", new Object[] {window, old_state});
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        window.setNETWMState(old_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private void requestState(XWindowPeer window, int state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         * We have to use toggle for maximization because of transitions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
         * from maximization in one direction only to maximization in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
         * other direction only.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        int old_net_state = getState(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        int max_changed = (state ^ old_net_state) & (Frame.MAXIMIZED_BOTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        XClientMessageEvent req = new XClientMessageEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            switch(max_changed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
              case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
              case Frame.MAXIMIZED_HORIZ:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                  req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                  req.set_data(2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
              case Frame.MAXIMIZED_VERT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                  req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                  req.set_data(2, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
              case Frame.MAXIMIZED_BOTH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                  req.set_data(1, XA_NET_WM_STATE_MAXIMIZED_HORZ.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                  req.set_data(2, XA_NET_WM_STATE_MAXIMIZED_VERT.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                  break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
              default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                  return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (log.isLoggable(Level.FINE)) log.fine("Requesting state on " + window + " for " + state);
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 107
diff changeset
   102
            req.set_type((int)XConstants.ClientMessage);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            req.set_window(window.getWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            req.set_message_type(XA_NET_WM_STATE.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            req.set_format(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            req.set_data(0, _NET_WM_STATE_TOGGLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                XlibWrapper.XSendEvent(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                        XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                        false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 107
diff changeset
   112
                        XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                        req.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            req.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public int getState(XWindowPeer window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return getStateImpl(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * New "NET" WM spec: _NET_WM_STATE/Atom[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    int getStateImpl(XWindowPeer window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        XAtomList net_wm_state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (net_wm_state.size() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            return Frame.NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        int java_state = Frame.NORMAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_VERT)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            java_state |= Frame.MAXIMIZED_VERT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (net_wm_state.contains(XA_NET_WM_STATE_MAXIMIZED_HORZ)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            java_state |= Frame.MAXIMIZED_HORIZ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        return java_state;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    public boolean isStateChange(XPropertyEvent e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        boolean res = doStateProtocol() && (e.get_atom() == XA_NET_WM_STATE.getAtom()) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (res) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // Since state change happened, reset our cached state.  It will be re-read by getState
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            XWindowPeer wpeer = (XWindowPeer)XToolkit.windowToXWindow(e.get_window());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            wpeer.setNETWMState(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * Work around for 4775545.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    public void unshadeKludge(XWindowPeer window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        XAtomList net_wm_state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        net_wm_state.remove(XA_NET_WM_STATE_SHADED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        window.setNETWMState(net_wm_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * XLayerProtocol
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public boolean supportsLayer(int layer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return ((layer == LAYER_ALWAYS_ON_TOP) || (layer == LAYER_NORMAL)) && doLayerProtocol();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * Helper function to set/reset one state in NET_WM_STATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * If window is showing then it uses ClientMessage, otherwise adjusts NET_WM_STATE list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @param window Window which NET_WM_STATE property is being modified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @param state State atom to be set/reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @param reset Indicates operation, 'set' if false, 'reset' if true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private void setStateHelper(XWindowPeer window, XAtom state, boolean set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        log.log(Level.FINER, "Window visibility is: withdrawn={0}, visible={1}, mapped={2} showing={3}",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                new Object[] {Boolean.valueOf(window.isWithdrawn()), Boolean.valueOf(window.isVisible()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                              Boolean.valueOf(window.isMapped()), Boolean.valueOf(window.isShowing())});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (window.isShowing()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            XClientMessageEvent req = new XClientMessageEvent();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            try {
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 107
diff changeset
   186
                req.set_type((int)XConstants.ClientMessage);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                req.set_window(window.getWindow());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                req.set_message_type(XA_NET_WM_STATE.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                req.set_format(32);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                req.set_data(0, (!set) ? _NET_WM_STATE_REMOVE : _NET_WM_STATE_ADD);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                req.set_data(1, state.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                log.log(Level.FINE, "Setting _NET_STATE atom {0} on {1} for {2}", new Object[] {state, window, Boolean.valueOf(set)});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                    XlibWrapper.XSendEvent(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                                           XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                           false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 107
diff changeset
   198
                                           XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                           req.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                req.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            XAtomList net_wm_state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            log.log(Level.FINE, "Current state on {0} is {1}", new Object[] {window, net_wm_state});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (!set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                net_wm_state.remove(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                net_wm_state.add(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            log.log(Level.FINE, "Setting states on {0} to {1}", new Object[] {window, net_wm_state});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            window.setNETWMState(net_wm_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        XToolkit.XSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void setLayer(XWindowPeer window, int layer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        setStateHelper(window, XA_NET_WM_STATE_ABOVE, layer == LAYER_ALWAYS_ON_TOP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    /* New "netwm" spec from www.freedesktop.org */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING");   /* like STRING but encoding is UTF-8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    XAtom XA_NET_SUPPORTING_WM_CHECK = XAtom.get("_NET_SUPPORTING_WM_CHECK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    XAtom XA_NET_SUPPORTED = XAtom.get("_NET_SUPPORTED");      /* list of protocols (property of root) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    XAtom XA_NET_WM_NAME = XAtom.get("_NET_WM_NAME");  /* window property */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    XAtom XA_NET_WM_STATE = XAtom.get("_NET_WM_STATE");/* both window property and request */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 * _NET_WM_STATE is a list of atoms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 * NB: Standard spelling is "HORZ" (yes, without an 'I'), but KDE2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * uses misspelled "HORIZ" (see KDE bug #20229).  This was fixed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * KDE 2.2.  Under earlier versions of KDE2 horizontal and full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * maximization doesn't work .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    XAtom XA_NET_WM_STATE_MAXIMIZED_HORZ = XAtom.get("_NET_WM_STATE_MAXIMIZED_HORZ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    XAtom XA_NET_WM_STATE_MAXIMIZED_VERT = XAtom.get("_NET_WM_STATE_MAXIMIZED_VERT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    XAtom XA_NET_WM_STATE_SHADED = XAtom.get("_NET_WM_STATE_SHADED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    XAtom XA_NET_WM_STATE_ABOVE = XAtom.get("_NET_WM_STATE_ABOVE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    XAtom XA_NET_WM_STATE_MODAL = XAtom.get("_NET_WM_STATE_MODAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    XAtom XA_NET_WM_STATE_FULLSCREEN = XAtom.get("_NET_WM_STATE_FULLSCREEN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    XAtom XA_NET_WM_STATE_BELOW = XAtom.get("_NET_WM_STATE_BELOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    XAtom XA_NET_WM_STATE_HIDDEN = XAtom.get("_NET_WM_STATE_HIDDEN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    XAtom XA_NET_WM_STATE_SKIP_TASKBAR = XAtom.get("_NET_WM_STATE_SKIP_TASKBAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    XAtom XA_NET_WM_STATE_SKIP_PAGER = XAtom.get("_NET_WM_STATE_SKIP_PAGER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    XAtom XA_NET_WM_WINDOW_TYPE = XAtom.get("_NET_WM_WINDOW_TYPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    XAtom XA_NET_WM_WINDOW_TYPE_DIALOG = XAtom.get("_NET_WM_WINDOW_TYPE_DIALOG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
/* For _NET_WM_STATE ClientMessage requests */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    final static int _NET_WM_STATE_REMOVE      =0; /* remove/unset property */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    final static int _NET_WM_STATE_ADD         =1; /* add/set property      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    final static int _NET_WM_STATE_TOGGLE      =2; /* toggle property       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    boolean supportChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    long NetWindow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    void detect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (supportChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            // TODO: How about detecting WM-restart or exit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        NetWindow = checkAnchor(XA_NET_SUPPORTING_WM_CHECK, XAtom.XA_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        supportChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (log.isLoggable(Level.FINE)) log.fine("### " + this + " is active: " + (NetWindow != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    boolean active() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        detect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        return NetWindow != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    boolean doStateProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE);
107
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
   277
        stateLog.finer("doStateProtocol() returns " + res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    boolean doLayerProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_ABOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    boolean doModalityProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_MODAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    boolean isWMName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        if (!active()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        String net_wm_name_string = getWMName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (net_wm_name_string == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        if (log.isLoggable(Level.FINE)) log.fine("### WM_NAME = " + net_wm_name_string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        return net_wm_name_string.startsWith(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    String net_wm_name_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    public String getWMName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        if (!active()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        if (net_wm_name_cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            return net_wm_name_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
         * Check both UTF8_STRING and STRING.  We only call this function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
         * with ASCII names and UTF8 preserves ASCII bit-wise.  wm-spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * mandates UTF8_STRING for _NET_WM_NAME but at least sawfish-1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * still uses STRING.  (mmm, moving targets...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        String charSet = "UTF8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        byte[] net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XA_UTF8_STRING.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        if (net_wm_name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XAtom.XA_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            charSet = "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        if (net_wm_name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            net_wm_name_cache = new String(net_wm_name, charSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            return net_wm_name_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        } catch (java.io.UnsupportedEncodingException uex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * Sets _NET_WM_ICON property on the window using the List of XIconInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * If icons is null or empty list, removes _NET_WM_ICON property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    public void setWMIcons(XWindowPeer window, java.util.List<XIconInfo> icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (window == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        XAtom iconsAtom = XAtom.get("_NET_WM_ICON");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (icons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            iconsAtom.DeleteProperty(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        for (XIconInfo ii : icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            length += ii.getRawLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        int cardinalSize = (XlibWrapper.dataModel == 32) ? 4 : 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        int bufferSize = length * cardinalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        if (bufferSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            long buffer = XlibWrapper.unsafe.allocateMemory(bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                long ptr = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                for (XIconInfo ii : icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    int size = ii.getRawLength() * cardinalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if (XlibWrapper.dataModel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        XlibWrapper.copyIntArray(ptr, ii.getIntData(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        XlibWrapper.copyLongArray(ptr, ii.getLongData(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    ptr += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                iconsAtom.setAtomData(window.getWindow(), XAtom.XA_CARDINAL, buffer, bufferSize/Native.getCard32Size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                XlibWrapper.unsafe.freeMemory(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            iconsAtom.DeleteProperty(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public boolean isWMStateNetHidden(XWindowPeer window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (!doStateProtocol()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        XAtomList state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        return (state != null && state.size() != 0 && state.contains(XA_NET_WM_STATE_HIDDEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
}