jdk/src/solaris/classes/sun/awt/X11/XAWTXSettings.java
author art
Fri, 15 May 2009 15:40:35 +0400
changeset 2802 d05a9dcc8296
parent 439 3488710b02f8
child 3938 ef327bd847c0
permissions -rw-r--r--
6678385: Random java.lang.StackOverflowError from various JDKs Reviewed-by: stayer
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
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
   * This code is ported to XAWT from MAWT based on awt_mgrsel.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   * and XSettings.java code written originally by Valeriy Ushakov
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
   * Author : Bino George
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
package sun.awt.X11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.awt.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.awt.XSettings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.logging.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
class XAWTXSettings extends XSettings implements XMSelectionListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final XAtom xSettingsPropertyAtom = XAtom.get("_XSETTINGS_SETTINGS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static Logger log = Logger.getLogger("sun.awt.X11.XAWTXSettings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /* The maximal length of the property data. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final long MAX_LENGTH = 1000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    XMSelection settings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public XAWTXSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        initXSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    void initXSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (log.isLoggable(Level.FINE)) log.fine("Initializing XAWT XSettings");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        settings = new XMSelection("_XSETTINGS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        settings.addSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        initPerScreenXSettings();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        settings.removeSelectionListener(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    public void ownerDeath(int screen, XMSelection sel, long deadOwner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (log.isLoggable(Level.FINE)) log.fine("Owner " + deadOwner + " died for selection " + sel + " screen "+ screen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void ownerChanged(int screen, XMSelection sel, long newOwner, long data, long timestamp) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (log.isLoggable(Level.FINE)) log.fine("New Owner "+ newOwner + " for selection = " + sel + " screen " +screen );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    public void selectionChanged(int screen, XMSelection sel, long owner , XPropertyEvent event) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        log.fine("Selection changed on sel " + sel + " screen = " + screen + " owner = " + owner + " event = " + event);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        updateXSettings(screen,owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    void initPerScreenXSettings() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (log.isLoggable(Level.FINE)) log.fine("Updating Per XSettings changes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
         * As toolkit cannot yet cope with per-screen desktop properties,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
         * only report XSETTINGS changes on the default screen.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
         * should be "good enough" for most cases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        Map updatedSettings = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        XToolkit.awtLock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            long display = XToolkit.getDisplay();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            int screen = (int) XlibWrapper.DefaultScreen(display);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            updatedSettings = getUpdatedSettings(settings.getOwner(screen));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            XToolkit.awtUnlock();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        // we must not  invoke this under Awt Lock
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        ((XToolkit)Toolkit.getDefaultToolkit()).parseXSettings(0,updatedSettings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private void updateXSettings(int screen, long owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        final Map updatedSettings = getUpdatedSettings(owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        // this method is called under awt lock and usually on toolkit thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        // but parseXSettings() causes public code execution, so we need to transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        // this to EDT
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        EventQueue.invokeLater( new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                ((XToolkit) Toolkit.getDefaultToolkit()).parseXSettings( 0, updatedSettings);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    private Map getUpdatedSettings(final long owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        if (log.isLoggable(Level.FINE)) log.fine("owner =" + owner);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (0 == owner) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        Map settings = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            WindowPropertyGetter getter =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                new WindowPropertyGetter(owner, xSettingsPropertyAtom, 0, MAX_LENGTH,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                        false, xSettingsPropertyAtom.getAtom() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            try {
2802
d05a9dcc8296 6678385: Random java.lang.StackOverflowError from various JDKs
art
parents: 439
diff changeset
   129
                int status = getter.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
439
3488710b02f8 6623459: Get rid of XConstant, XProtocolConstants and XUtilConstants antipattern
dav
parents: 2
diff changeset
   131
                if (status != XConstants.Success || getter.getData() == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                    if (log.isLoggable(Level.FINE)) log.fine("OH OH : getter failed  status = " + status );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                    settings = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                long ptr = getter.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                if (log.isLoggable(Level.FINE)) log.fine("noItems = " + getter.getNumberOfItems());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                byte array[] = Native.toBytes(ptr,getter.getNumberOfItems());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if (array != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    settings = update(array);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                getter.dispose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        return settings;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
}