jdk/src/solaris/classes/sun/awt/X11/XNETProtocol.java
author yan
Mon, 06 Oct 2008 16:45:00 +0400
changeset 1966 12a51fb0db0d
parent 1189 ee54b5ce2113
child 2451 597df8e1d786
permissions -rw-r--r--
5100701: Toolkit.getLockingKeyState() does not work on XToolkit, but works on Motif Summary: Does not work on Motif but works on XToolkit now; implemented using XQueryPointer. Reviewed-by: anthony
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());
1189
ee54b5ce2113 6735584: XNETProtocol:setStateHelper() produces bad _NET_WM_STATE messages
art
parents: 439
diff changeset
   192
                // Fix for 6735584: req.data[2] must be set to 0 when only one property is changed
ee54b5ce2113 6735584: XNETProtocol:setStateHelper() produces bad _NET_WM_STATE messages
art
parents: 439
diff changeset
   193
                req.set_data(2, 0);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                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
   195
                XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    XlibWrapper.XSendEvent(XToolkit.getDisplay(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                           XlibWrapper.RootWindow(XToolkit.getDisplay(), window.getScreenNumber()),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                           false,
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 107
diff changeset
   200
                                           XConstants.SubstructureRedirectMask | XConstants.SubstructureNotifyMask,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                           req.pData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                req.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            XAtomList net_wm_state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            log.log(Level.FINE, "Current state on {0} is {1}", new Object[] {window, net_wm_state});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (!set) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                net_wm_state.remove(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                net_wm_state.add(state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            log.log(Level.FINE, "Setting states on {0} to {1}", new Object[] {window, net_wm_state});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            window.setNETWMState(net_wm_state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        XToolkit.XSync();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    public void setLayer(XWindowPeer window, int layer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        setStateHelper(window, XA_NET_WM_STATE_ABOVE, layer == LAYER_ALWAYS_ON_TOP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    /* New "netwm" spec from www.freedesktop.org */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    XAtom XA_UTF8_STRING = XAtom.get("UTF8_STRING");   /* like STRING but encoding is UTF-8 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    XAtom XA_NET_SUPPORTING_WM_CHECK = XAtom.get("_NET_SUPPORTING_WM_CHECK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    XAtom XA_NET_SUPPORTED = XAtom.get("_NET_SUPPORTED");      /* list of protocols (property of root) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    XAtom XA_NET_WM_NAME = XAtom.get("_NET_WM_NAME");  /* window property */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    XAtom XA_NET_WM_STATE = XAtom.get("_NET_WM_STATE");/* both window property and request */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * _NET_WM_STATE is a list of atoms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * NB: Standard spelling is "HORZ" (yes, without an 'I'), but KDE2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * uses misspelled "HORIZ" (see KDE bug #20229).  This was fixed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 * KDE 2.2.  Under earlier versions of KDE2 horizontal and full
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
 * maximization doesn't work .
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    XAtom XA_NET_WM_STATE_MAXIMIZED_HORZ = XAtom.get("_NET_WM_STATE_MAXIMIZED_HORZ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    XAtom XA_NET_WM_STATE_MAXIMIZED_VERT = XAtom.get("_NET_WM_STATE_MAXIMIZED_VERT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    XAtom XA_NET_WM_STATE_SHADED = XAtom.get("_NET_WM_STATE_SHADED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    XAtom XA_NET_WM_STATE_ABOVE = XAtom.get("_NET_WM_STATE_ABOVE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    XAtom XA_NET_WM_STATE_MODAL = XAtom.get("_NET_WM_STATE_MODAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    XAtom XA_NET_WM_STATE_FULLSCREEN = XAtom.get("_NET_WM_STATE_FULLSCREEN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    XAtom XA_NET_WM_STATE_BELOW = XAtom.get("_NET_WM_STATE_BELOW");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    XAtom XA_NET_WM_STATE_HIDDEN = XAtom.get("_NET_WM_STATE_HIDDEN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    XAtom XA_NET_WM_STATE_SKIP_TASKBAR = XAtom.get("_NET_WM_STATE_SKIP_TASKBAR");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    XAtom XA_NET_WM_STATE_SKIP_PAGER = XAtom.get("_NET_WM_STATE_SKIP_PAGER");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    XAtom XA_NET_WM_WINDOW_TYPE = XAtom.get("_NET_WM_WINDOW_TYPE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    XAtom XA_NET_WM_WINDOW_TYPE_DIALOG = XAtom.get("_NET_WM_WINDOW_TYPE_DIALOG");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
/* For _NET_WM_STATE ClientMessage requests */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    final static int _NET_WM_STATE_REMOVE      =0; /* remove/unset property */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    final static int _NET_WM_STATE_ADD         =1; /* add/set property      */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    final static int _NET_WM_STATE_TOGGLE      =2; /* toggle property       */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    boolean supportChecked = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    long NetWindow = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    void detect() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (supportChecked) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            // TODO: How about detecting WM-restart or exit?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        NetWindow = checkAnchor(XA_NET_SUPPORTING_WM_CHECK, XAtom.XA_WINDOW);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        supportChecked = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (log.isLoggable(Level.FINE)) log.fine("### " + this + " is active: " + (NetWindow != 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    boolean active() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        detect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return NetWindow != 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    boolean doStateProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE);
107
ed0c7cfb3666 6632140: minor refactoring for XWM
son
parents: 2
diff changeset
   279
        stateLog.finer("doStateProtocol() returns " + res);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    boolean doLayerProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_ABOVE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    boolean doModalityProtocol() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        boolean res = active() && checkProtocol(XA_NET_SUPPORTED, XA_NET_WM_STATE_MODAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        return res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    boolean isWMName(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        if (!active()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        String net_wm_name_string = getWMName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        if (net_wm_name_string == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        if (log.isLoggable(Level.FINE)) log.fine("### WM_NAME = " + net_wm_name_string);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        return net_wm_name_string.startsWith(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    String net_wm_name_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    public String getWMName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (!active()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        if (net_wm_name_cache != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            return net_wm_name_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
         * Check both UTF8_STRING and STRING.  We only call this function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
         * with ASCII names and UTF8 preserves ASCII bit-wise.  wm-spec
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * mandates UTF8_STRING for _NET_WM_NAME but at least sawfish-1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         * still uses STRING.  (mmm, moving targets...).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        String charSet = "UTF8";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        byte[] net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XA_UTF8_STRING.getAtom());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (net_wm_name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            net_wm_name = XA_NET_WM_NAME.getByteArrayProperty(NetWindow, XAtom.XA_STRING);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            charSet = "ASCII";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (net_wm_name == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            net_wm_name_cache = new String(net_wm_name, charSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            return net_wm_name_cache;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        } catch (java.io.UnsupportedEncodingException uex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * Sets _NET_WM_ICON property on the window using the List of XIconInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * If icons is null or empty list, removes _NET_WM_ICON property
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    public void setWMIcons(XWindowPeer window, java.util.List<XIconInfo> icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        if (window == null) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        XAtom iconsAtom = XAtom.get("_NET_WM_ICON");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        if (icons == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            iconsAtom.DeleteProperty(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        int length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        for (XIconInfo ii : icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            length += ii.getRawLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        int cardinalSize = (XlibWrapper.dataModel == 32) ? 4 : 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        int bufferSize = length * cardinalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        if (bufferSize != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            long buffer = XlibWrapper.unsafe.allocateMemory(bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                long ptr = buffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                for (XIconInfo ii : icons) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    int size = ii.getRawLength() * cardinalSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    if (XlibWrapper.dataModel == 32) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                        XlibWrapper.copyIntArray(ptr, ii.getIntData(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                        XlibWrapper.copyLongArray(ptr, ii.getLongData(), size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    ptr += size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                iconsAtom.setAtomData(window.getWindow(), XAtom.XA_CARDINAL, buffer, bufferSize/Native.getCard32Size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                XlibWrapper.unsafe.freeMemory(buffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            iconsAtom.DeleteProperty(window);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    public boolean isWMStateNetHidden(XWindowPeer window) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (!doStateProtocol()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        XAtomList state = window.getNETWMState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return (state != null && state.size() != 0 && state.contains(XA_NET_WM_STATE_HIDDEN));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
}