001: /*
002: * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: /*
027: * This code is ported to XAWT from MAWT based on awt_mgrsel.c
028: * and XSettings.java code written originally by Valeriy Ushakov
029: * Author : Bino George
030: */
031:
032: package sun.awt.X11;
033:
034: import java.util.*;
035: import java.awt.*;
036: import sun.awt.XSettings;
037: import java.util.logging.*;
038:
039: class XAWTXSettings extends XSettings implements XMSelectionListener {
040:
041: private final XAtom xSettingsPropertyAtom = XAtom
042: .get("_XSETTINGS_SETTINGS");
043:
044: private static Logger log = Logger
045: .getLogger("sun.awt.X11.XAWTXSettings");
046:
047: /* The maximal length of the property data. */
048: public static final long MAX_LENGTH = 1000000;
049:
050: XMSelection settings;
051:
052: public XAWTXSettings() {
053: initXSettings();
054:
055: }
056:
057: void initXSettings() {
058: if (log.isLoggable(Level.FINE))
059: log.fine("Initializing XAWT XSettings");
060: settings = new XMSelection("_XSETTINGS");
061: settings.addSelectionListener(this );
062: initPerScreenXSettings();
063: }
064:
065: public void ownerDeath(int screen, XMSelection sel, long deadOwner) {
066: if (log.isLoggable(Level.FINE))
067: log.fine("Owner " + deadOwner + " died for selection "
068: + sel + " screen " + screen);
069: }
070:
071: public void ownerChanged(int screen, XMSelection sel,
072: long newOwner, long data, long timestamp) {
073: if (log.isLoggable(Level.FINE))
074: log.fine("New Owner " + newOwner + " for selection = "
075: + sel + " screen " + screen);
076: }
077:
078: public void selectionChanged(int screen, XMSelection sel,
079: long owner, XPropertyEvent event) {
080: log.fine("Selection changed on sel " + sel + " screen = "
081: + screen + " owner = " + owner + " event = " + event);
082: updateXSettings(screen, owner);
083: }
084:
085: void initPerScreenXSettings() {
086: if (log.isLoggable(Level.FINE))
087: log.fine("Updating Per XSettings changes");
088:
089: /*
090: * As toolkit cannot yet cope with per-screen desktop properties,
091: * only report XSETTINGS changes on the default screen. This
092: * should be "good enough" for most cases.
093: */
094:
095: Map updatedSettings = null;
096: XToolkit.awtLock();
097: try {
098: long display = XToolkit.getDisplay();
099: int screen = (int) XlibWrapper.DefaultScreen(display);
100: updatedSettings = getUpdatedSettings(settings
101: .getOwner(screen));
102: } finally {
103: XToolkit.awtUnlock();
104: }
105: // we must not invoke this under Awt Lock
106: ((XToolkit) Toolkit.getDefaultToolkit()).parseXSettings(0,
107: updatedSettings);
108: }
109:
110: private void updateXSettings(int screen, long owner) {
111: final Map updatedSettings = getUpdatedSettings(owner);
112: // this method is called under awt lock and usually on toolkit thread
113: // but parseXSettings() causes public code execution, so we need to transfer
114: // this to EDT
115: EventQueue.invokeLater(new Runnable() {
116: public void run() {
117: ((XToolkit) Toolkit.getDefaultToolkit())
118: .parseXSettings(0, updatedSettings);
119: }
120: });
121: }
122:
123: private Map getUpdatedSettings(final long owner) {
124: if (log.isLoggable(Level.FINE))
125: log.fine("owner =" + owner);
126: if (0 == owner) {
127: return null;
128: }
129:
130: Map settings = null;
131: try {
132: WindowPropertyGetter getter = new WindowPropertyGetter(
133: owner, xSettingsPropertyAtom, 0, MAX_LENGTH, false,
134: xSettingsPropertyAtom.getAtom());
135: try {
136: int status = getter
137: .execute(XToolkit.IgnoreBadWindowHandler);
138:
139: if (status != XlibWrapper.Success
140: || getter.getData() == 0) {
141: if (log.isLoggable(Level.FINE))
142: log.fine("OH OH : getter failed status = "
143: + status);
144: settings = null;
145: }
146:
147: long ptr = getter.getData();
148:
149: if (log.isLoggable(Level.FINE))
150: log.fine("noItems = " + getter.getNumberOfItems());
151: byte array[] = Native.toBytes(ptr, getter
152: .getNumberOfItems());
153: if (array != null) {
154: settings = update(array);
155: }
156: } finally {
157: getter.dispose();
158: }
159: } catch (Exception e) {
160: e.printStackTrace();
161: }
162: return settings;
163: }
164:
165: }
|