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: import gnu.x11.EscherServerConnectionException;
  43: 
  44: import java.awt.GraphicsConfiguration;
  45: import java.awt.GraphicsDevice;
  46: import java.lang.reflect.Constructor;
  47: import java.net.Socket;
  48: 
  49: /**
  50:  * This class represents an X Display. The actual connection is established
  51:  * lazily when it is first needed.
  52:  *
  53:  * @author Roman Kennke (kennke@aicas.com)
  54:  */
  55: public class XGraphicsDevice
  56:   extends GraphicsDevice
  57: {
  58: 
  59:   private XGraphicsConfiguration defaultConfiguration;
  60: 
  61:   /**
  62:    * The X display associated with the XGraphicsDevice. This is established
  63:    * when {@link #getDisplay} is first called.
  64:    */
  65:   private Display display;
  66: 
  67:   /**
  68:    * The display name from which the display will be initialized.
  69:    */
  70:   private Display.Name displayName;
  71: 
  72:   /**
  73:    * The event pump for this X Display.
  74:    */
  75:   private XEventPump eventPump;
  76: 
  77:   /**
  78:    * Creates a new XGraphicsDevice.
  79:    */
  80:   XGraphicsDevice(Display.Name dn)
  81:   {
  82:     displayName = dn;
  83:   }
  84: 
  85:   public int getType()
  86:   {
  87:     return TYPE_RASTER_SCREEN;
  88:   }
  89: 
  90:   public String getIDstring()
  91:   {
  92:     // TODO: Implement this.
  93:     throw new UnsupportedOperationException("Not yet implemented.");
  94:   }
  95: 
  96:   public GraphicsConfiguration[] getConfigurations()
  97:   {
  98:     // TODO: Implement this.
  99:     throw new UnsupportedOperationException("Not yet implemented.");
 100:   }
 101: 
 102:   public GraphicsConfiguration getDefaultConfiguration()
 103:   {
 104:     if (defaultConfiguration == null)
 105:       defaultConfiguration = new XGraphicsConfiguration(this);
 106:     return defaultConfiguration;
 107:   }
 108: 
 109:   /**
 110:    * Returns the X Display associated with this XGraphicsDevice.
 111:    * This establishes the connection to the X server on the first invocation.
 112:    *
 113:    * @return the X Display associated with this XGraphicsDevice
 114:    */
 115:   Display getDisplay()
 116:   {
 117:     if (display == null)
 118:       {
 119:         if (displayName.hostname.equals(""))
 120:           displayName.hostname = "localhost";
 121:         if (XToolkit.DEBUG)
 122:           System.err.println("connecting to : " + displayName);
 123:         // Try to connect via unix domain sockets when host == localhost.
 124:         if ((displayName.hostname.equals("localhost")
 125:              || displayName.hostname.equals(""))
 126:           && SystemProperties.getProperty("gnu.xawt.no_local_sockets") == null)
 127:           {
 128:             Socket socket = createLocalSocket();
 129:             if (socket != null)
 130:               {
 131:                 try
 132:                   {
 133:                     display = new Display(socket, "localhost",
 134:                                           displayName.display_no,
 135:                                           displayName.screen_no);
 136:                   }
 137:                 catch (EscherServerConnectionException e)
 138:                   {
 139:                     throw new RuntimeException(e.getCause());
 140:                   }
 141:               }
 142:           }
 143: 
 144:         // The following happens when we are configured to use plain sockets,
 145:         // when the connection is probably remote or when we couldn't load
 146:         // the LocalSocket class stuff.
 147:         if (display == null)
 148:           {
 149:             try
 150:               {
 151:                 display = new Display(displayName);
 152:               }
 153:             catch (EscherServerConnectionException e)
 154:               {
 155:                 throw new RuntimeException(e.getCause());
 156:               }
 157:           }
 158: 
 159:         eventPump = new XEventPump(display);
 160:       }
 161:     return display;
 162:   }
 163: 
 164:   XEventPump getEventPump()
 165:   {
 166:     return eventPump;
 167:   }
 168: 
 169:   /**
 170:    * Tries to load the LocalSocket class and initiate a connection to the
 171:    * local X server.
 172:    */
 173:   private Socket createLocalSocket()
 174:   {
 175:     Socket socket = null;
 176:     try
 177:       {
 178:         // TODO: Is this 100% ok?
 179:         String sockPath = "/tmp/.X11-unix/X" + displayName.display_no;
 180:         Class localSocketAddressClass =
 181:           Class.forName("gnu.java.net.local.LocalSocketAddress");
 182:         Constructor localSocketAddressConstr =
 183:           localSocketAddressClass.getConstructor(new Class[]{ String.class });
 184:         Object addr =
 185:           localSocketAddressConstr.newInstance(new Object[]{ sockPath });
 186:         Class localSocketClass =
 187:           Class.forName("gnu.java.net.local.LocalSocket");
 188:         Constructor localSocketConstructor =
 189:           localSocketClass.getConstructor(new Class[]{localSocketAddressClass});
 190:         Object localSocket =
 191:           localSocketConstructor.newInstance(new Object[]{ addr });
 192:         socket = (Socket) localSocket;
 193:       }
 194:     catch (Exception ex)
 195:       {
 196:         // Whatever goes wrong here, we return null.
 197:       }
 198:     return socket;
 199:   }
 200: }