Source for gnu.java.awt.peer.x.XGraphicsDevice

   1: /* XGraphicsDevice.java -- GraphicsDevice for X
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: package gnu.java.awt.peer.x;
  39: 
  40: import gnu.classpath.SystemProperties;
  41: import gnu.x11.Display;
  42: 
  43: import java.awt.GraphicsConfiguration;
  44: import java.awt.GraphicsDevice;
  45: import java.lang.reflect.Constructor;
  46: import java.net.Socket;
  47: 
  48: /**
  49:  * This class represents an X Display. The actual connection is established
  50:  * lazily when it is first needed.
  51:  *
  52:  * @author Roman Kennke (kennke@aicas.com)
  53:  */
  54: public class XGraphicsDevice
  55:   extends GraphicsDevice
  56: {
  57: 
  58:   private XGraphicsConfiguration defaultConfiguration;
  59: 
  60:   /**
  61:    * The X display associated with the XGraphicsDevice. This is established
  62:    * when {@link #getDisplay} is first called.
  63:    */
  64:   private Display display;
  65: 
  66:   /**
  67:    * The display name from which the display will be initialized.
  68:    */
  69:   private Display.Name displayName;
  70: 
  71:   /**
  72:    * The event pump for this X Display.
  73:    */
  74:   private XEventPump eventPump;
  75: 
  76:   /**
  77:    * Creates a new XGraphicsDevice.
  78:    */
  79:   XGraphicsDevice(Display.Name dn)
  80:   {
  81:     displayName = dn;
  82:   }
  83: 
  84:   public int getType()
  85:   {
  86:     return TYPE_RASTER_SCREEN;
  87:   }
  88: 
  89:   public String getIDstring()
  90:   {
  91:     // TODO: Implement this.
  92:     throw new UnsupportedOperationException("Not yet implemented.");
  93:   }
  94: 
  95:   public GraphicsConfiguration[] getConfigurations()
  96:   {
  97:     // TODO: Implement this.
  98:     throw new UnsupportedOperationException("Not yet implemented.");
  99:   }
 100: 
 101:   public GraphicsConfiguration getDefaultConfiguration()
 102:   {
 103:     if (defaultConfiguration == null)
 104:       defaultConfiguration = new XGraphicsConfiguration(this);
 105:     return defaultConfiguration;
 106:   }
 107: 
 108:   /**
 109:    * Returns the X Display associated with this XGraphicsDevice.
 110:    * This establishes the connection to the X server on the first invocation.
 111:    *
 112:    * @return the X Display associated with this XGraphicsDevice
 113:    */
 114:   Display getDisplay()
 115:   {
 116:     if (display == null)
 117:       {
 118:         if (displayName.hostname.equals(""))
 119:           displayName.hostname = "localhost";
 120:         if (XToolkit.DEBUG)
 121:           System.err.println("connecting to : " + displayName);
 122:         // Try to connect via unix domain sockets when host == localhost.
 123:         if ((displayName.hostname.equals("localhost")
 124:              || displayName.hostname.equals(""))
 125:           && SystemProperties.getProperty("gnu.xawt.no_local_sockets") == null)
 126:           {
 127:             Socket socket = createLocalSocket();
 128:             if (socket != null)
 129:               {
 130:                 display = new Display(socket, "localhost",
 131:                                       displayName.display_no,
 132:                                       displayName.screen_no);
 133:               }
 134:           }
 135: 
 136:         // The following happens when we are configured to use plain sockets,
 137:         // when the connection is probably remote or when we couldn't load
 138:         // the LocalSocket class stuff.
 139:         if (display == null)
 140:           display = new Display(displayName);
 141: 
 142:         eventPump = new XEventPump(display);
 143:       }
 144:     return display;
 145:   }
 146: 
 147:   XEventPump getEventPump()
 148:   {
 149:     return eventPump;
 150:   }
 151: 
 152:   /**
 153:    * Tries to load the LocalSocket class and initiate a connection to the 
 154:    * local X server.
 155:    */
 156:   private Socket createLocalSocket()
 157:   {
 158:     Socket socket = null;
 159:     try
 160:       {
 161:         // TODO: Is this 100% ok?
 162:         String sockPath = "/tmp/.X11-unix/X" + displayName.display_no;
 163:         Class localSocketAddressClass =
 164:           Class.forName("gnu.java.net.local.LocalSocketAddress");
 165:         Constructor localSocketAddressConstr =
 166:           localSocketAddressClass.getConstructor(new Class[]{ String.class });
 167:         Object addr =
 168:           localSocketAddressConstr.newInstance(new Object[]{ sockPath });
 169:         Class localSocketClass =
 170:           Class.forName("gnu.java.net.local.LocalSocket");
 171:         Constructor localSocketConstructor =
 172:           localSocketClass.getConstructor(new Class[]{localSocketAddressClass});
 173:         Object localSocket =
 174:           localSocketConstructor.newInstance(new Object[]{ addr });
 175:         socket = (Socket) localSocket;
 176:       }
 177:     catch (Exception ex)
 178:       {
 179:         // Whatever goes wrong here, we return null.
 180:       }
 181:     return socket;
 182:   }
 183: }