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.java.net.local.LocalSocket;
  42: import gnu.java.net.local.LocalSocketAddress;
  43: import gnu.x11.Connection;
  44: import gnu.x11.Display;
  45: 
  46: import java.awt.AWTError;
  47: import java.awt.GraphicsConfiguration;
  48: import java.awt.GraphicsDevice;
  49: import java.net.SocketException;
  50: 
  51: /**
  52:  * This class represents an X Display. The actual connection is established
  53:  * lazily when it is first needed.
  54:  *
  55:  * @author Roman Kennke (kennke@aicas.com)
  56:  */
  57: public class XGraphicsDevice
  58:   extends GraphicsDevice
  59: {
  60: 
  61:   private XGraphicsConfiguration defaultConfiguration;
  62: 
  63:   /**
  64:    * The X display associated with the XGraphicsDevice. This is established
  65:    * when {@link #getDisplay} is first called.
  66:    */
  67:   private Display display;
  68: 
  69:   /**
  70:    * The display name from which the display will be initialized.
  71:    */
  72:   private Display.Name displayName;
  73: 
  74:   /**
  75:    * The event pump for this X Display.
  76:    */
  77:   private XEventPump eventPump;
  78: 
  79:   /**
  80:    * Creates a new XGraphicsDevice.
  81:    */
  82:   XGraphicsDevice(Display.Name dn)
  83:   {
  84:     displayName = dn;
  85:   }
  86: 
  87:   public int getType()
  88:   {
  89:     return TYPE_RASTER_SCREEN;
  90:   }
  91: 
  92:   public String getIDstring()
  93:   {
  94:     // TODO: Implement this.
  95:     throw new UnsupportedOperationException("Not yet implemented.");
  96:   }
  97: 
  98:   public GraphicsConfiguration[] getConfigurations()
  99:   {
 100:     // TODO: Implement this.
 101:     throw new UnsupportedOperationException("Not yet implemented.");
 102:   }
 103: 
 104:   public GraphicsConfiguration getDefaultConfiguration()
 105:   {
 106:     if (defaultConfiguration == null)
 107:       defaultConfiguration = new XGraphicsConfiguration(this);
 108:     return defaultConfiguration;
 109:   }
 110: 
 111:   /**
 112:    * Returns the X Display associated with this XGraphicsDevice.
 113:    * This establishes the connection to the X server on the first invocation.
 114:    *
 115:    * @return the X Display associated with this XGraphicsDevice
 116:    */
 117:   Display getDisplay()
 118:   {
 119:     if (display == null)
 120:       {
 121:         if (displayName.hostname.equals(""))
 122:           displayName.hostname = "localhost";
 123:         if (XToolkit.DEBUG)
 124:           System.err.println("connecting to : " + displayName);
 125:         // Try to connect via unix domain sockets when host == localhost.
 126:         if ((displayName.hostname.equals("localhost")
 127:              || displayName.hostname.equals(""))
 128:           && SystemProperties.getProperty("gnu.xawt.no_local_sockets") == null)
 129:           {
 130:             // TODO: Is this 100% ok?
 131:             String sockPath = "/tmp/.X11-unix/X" + displayName.display_no;
 132:             LocalSocketAddress addr = new LocalSocketAddress(sockPath);
 133:             try
 134:               {
 135:                 if (XToolkit.DEBUG)
 136:                   System.err.println("connecting to local socket: "
 137:                                      + sockPath);
 138:                 LocalSocket socket = new LocalSocket(addr);
 139:                 display = new Display(socket, "localhost",
 140:                                       displayName.display_no,
 141:                                       displayName.screen_no);
 142:                 display.connection.send_mode = Connection.ASYNCHRONOUS;
 143:                 if (XToolkit.DEBUG)
 144:                   System.err.println("connected to local socket");
 145:               }
 146:             catch (SocketException ex)
 147:               {
 148:                 AWTError err = new AWTError("could not connect to X server");
 149:                 err.initCause(ex);
 150:                 throw err;
 151:               }
 152:           }
 153:         else
 154:           {
 155:             display = new Display(displayName);
 156:           }
 157:         eventPump = new XEventPump(display);
 158:       }
 159:     return display;
 160:   }
 161: 
 162:   XEventPump getEventPump()
 163:   {
 164:     return eventPump;
 165:   }
 166: }