1 /*
   2  * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26  /*
  27    * This code is ported to XAWT from MAWT based on awt_mgrsel.c
  28    * and XSettings.java code written originally by Valeriy Ushakov
  29    * Author : Bino George
  30    */
  31 
  32 
  33 package sun.awt.X11;
  34 
  35 import java.util.*;
  36 import java.awt.*;
  37 import sun.awt.XSettings;
  38 import sun.util.logging.PlatformLogger;
  39 
  40 
  41 class XAWTXSettings extends XSettings implements XMSelectionListener {
  42 
  43     private final XAtom xSettingsPropertyAtom = XAtom.get("_XSETTINGS_SETTINGS");
  44 
  45     private static PlatformLogger log = PlatformLogger.getLogger("sun.awt.X11.XAWTXSettings");
  46 
  47     /* The maximal length of the property data. */
  48     public static final long MAX_LENGTH = 1000000;
  49 
  50     XMSelection settings;
  51 
  52     public XAWTXSettings() {
  53         initXSettings();
  54 
  55     }
  56 
  57     void initXSettings() {
  58         if (log.isLoggable(PlatformLogger.FINE)) log.fine("Initializing XAWT XSettings");
  59         settings = new XMSelection("_XSETTINGS");
  60         settings.addSelectionListener(this);
  61         initPerScreenXSettings();
  62     }
  63 
  64     void dispose() {
  65         settings.removeSelectionListener(this);
  66     }
  67 
  68     public void ownerDeath(int screen, XMSelection sel, long deadOwner) {
  69         if (log.isLoggable(PlatformLogger.FINE)) log.fine("Owner " + deadOwner + " died for selection " + sel + " screen "+ screen);
  70     }
  71 
  72 
  73     public void ownerChanged(int screen, XMSelection sel, long newOwner, long data, long timestamp) {
  74         if (log.isLoggable(PlatformLogger.FINE)) log.fine("New Owner "+ newOwner + " for selection = " + sel + " screen " +screen );
  75     }
  76 
  77     public void selectionChanged(int screen, XMSelection sel, long owner , XPropertyEvent event) {
  78         log.fine("Selection changed on sel " + sel + " screen = " + screen + " owner = " + owner + " event = " + event);
  79         updateXSettings(screen,owner);
  80     }
  81 
  82     void initPerScreenXSettings() {
  83         if (log.isLoggable(PlatformLogger.FINE)) log.fine("Updating Per XSettings changes");
  84 
  85         /*
  86          * As toolkit cannot yet cope with per-screen desktop properties,
  87          * only report XSETTINGS changes on the default screen.  This
  88          * should be "good enough" for most cases.
  89          */
  90 
  91         Map updatedSettings = null;
  92         XToolkit.awtLock();
  93         try {
  94             long display = XToolkit.getDisplay();
  95             int screen = (int) XlibWrapper.DefaultScreen(display);
  96             updatedSettings = getUpdatedSettings(settings.getOwner(screen));
  97         } finally {
  98             XToolkit.awtUnlock();
  99         }
 100         // we must not  invoke this under Awt Lock
 101         ((XToolkit)Toolkit.getDefaultToolkit()).parseXSettings(0,updatedSettings);
 102     }
 103 
 104     private void updateXSettings(int screen, long owner) {
 105         final Map updatedSettings = getUpdatedSettings(owner);
 106         // this method is called under awt lock and usually on toolkit thread
 107         // but parseXSettings() causes public code execution, so we need to transfer
 108         // this to EDT
 109         EventQueue.invokeLater( new Runnable() {
 110             public void run() {
 111                 ((XToolkit) Toolkit.getDefaultToolkit()).parseXSettings( 0, updatedSettings);
 112             }
 113         });
 114     }
 115 
 116     private Map getUpdatedSettings(final long owner) {
 117         if (log.isLoggable(PlatformLogger.FINE)) log.fine("owner =" + owner);
 118         if (0 == owner) {
 119             return null;
 120         }
 121 
 122         Map settings = null;
 123         try {
 124             WindowPropertyGetter getter =
 125                 new WindowPropertyGetter(owner, xSettingsPropertyAtom, 0, MAX_LENGTH,
 126                         false, xSettingsPropertyAtom.getAtom() );
 127             try {
 128                 int status = getter.execute(XErrorHandler.IgnoreBadWindowHandler.getInstance());
 129 
 130                 if (status != XConstants.Success || getter.getData() == 0) {
 131                     if (log.isLoggable(PlatformLogger.FINE)) log.fine("OH OH : getter failed  status = " + status );
 132                     settings = null;
 133                 }
 134 
 135                 long ptr = getter.getData();
 136 
 137                 if (log.isLoggable(PlatformLogger.FINE)) log.fine("noItems = " + getter.getNumberOfItems());
 138                 byte array[] = Native.toBytes(ptr,getter.getNumberOfItems());
 139                 if (array != null) {
 140                     settings = update(array);
 141                 }
 142             } finally {
 143                 getter.dispose();
 144             }
 145         }
 146         catch (Exception e) {
 147             e.printStackTrace();
 148         }
 149         return settings;
 150     }
 151 
 152 
 153 
 154 }