1 /* GdkScreenGraphicsDevice.java -- information about a screen device
2 Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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)
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.
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
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
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. */
39 package gnu.java.awt.peer.gtk;
41 import java.awt.DisplayMode;
42 import java.awt.Frame;
43 import java.awt.GraphicsConfiguration;
44 import java.awt.GraphicsDevice;
45 import java.awt.Rectangle;
46 import java.awt.Window;
47 import java.util.ArrayList;
49 import gnu.classpath.Configuration;
50 import gnu.classpath.Pointer;
52 class GdkScreenGraphicsDevice extends GraphicsDevice
54 private final int native_state = GtkGenericPeer.getUniqueInteger ();
56 private Window fullscreenWindow;
58 private boolean oldWindowDecorationState;
60 private Rectangle oldWindowBounds;
62 private Rectangle bounds;
64 private GdkGraphicsConfiguration[] configurations;
66 /** The <code>GdkGraphicsEnvironment</code> instance that created this
67 * <code>GdkScreenGraphicsDevice</code>. This is only needed for native
68 * methods which need to access the 'native_state' field storing a pointer
69 * to a GdkDisplay object.
71 GdkGraphicsEnvironment env;
73 /** An identifier that is created by Gdk
77 /** The display modes supported by this <code>GdkScreenGraphicsDevice</code>.
78 * If the array is <code>null</code> <code>nativeGetDisplayModes</code> has
81 X11DisplayMode[] displayModes;
83 /** The non-changeable display mode of this <code>GdkScreenGraphicsDevice
84 * </code>. This field gets initialized by the {@link #init()} method. If it
85 * is still <code>null</code> afterwards, the XRandR extension is available
86 * and display mode changes are possible. If it is non-null XRandR is not
87 * available, no display mode changes are possible and no other native
88 * method must be called.
90 DisplayMode fixedDisplayMode;
93 * The pointer to the native screen resource.
95 * This field is manipulated by native code. Don't change or remove
96 * without adjusting the native code.
98 private Pointer screen;
102 if (Configuration.INIT_LOAD_LIBRARY)
104 System.loadLibrary("gtkpeer");
107 GtkToolkit.initializeGlobalIDs();
111 static native void initIDs();
113 GdkScreenGraphicsDevice (GdkGraphicsEnvironment e)
118 configurations = new GdkGraphicsConfiguration[1];
119 configurations[0] = new GdkGraphicsConfiguration(this);
122 /** This method is called from the native side immediately after
123 * the constructor is run.
127 fixedDisplayMode = nativeGetFixedDisplayMode(env);
130 /** Depending on the availability of the XRandR extension the method returns
131 * the screens' non-changeable display mode or null, meaning that XRandR can
132 * handle display mode changes.
134 native DisplayMode nativeGetFixedDisplayMode(GdkGraphicsEnvironment env);
136 public int getType ()
138 // Gdk manages only raster screens.
139 return GraphicsDevice.TYPE_RASTER_SCREEN;
142 public String getIDstring ()
144 if (idString == null)
145 idString = nativeGetIDString();
150 private native String nativeGetIDString();
152 public GraphicsConfiguration[] getConfigurations ()
154 return (GraphicsConfiguration[]) configurations.clone();
157 public GraphicsConfiguration getDefaultConfiguration ()
159 return configurations[0];
164 * Returns the current display mode of this device, or null if unknown.
166 * @return the current display mode
167 * @see #setDisplayMode(DisplayMode)
168 * @see #getDisplayModes()
171 public DisplayMode getDisplayMode()
173 if (fixedDisplayMode != null)
174 return fixedDisplayMode;
178 if (displayModes == null)
179 displayModes = nativeGetDisplayModes(env);
182 int index = nativeGetDisplayModeIndex(env);
183 int rate = nativeGetDisplayModeRate(env);
185 return new DisplayMode(displayModes[index].width,
186 displayModes[index].height,
187 DisplayMode.BIT_DEPTH_MULTI,
191 native int nativeGetDisplayModeIndex(GdkGraphicsEnvironment env);
193 native int nativeGetDisplayModeRate(GdkGraphicsEnvironment env);
195 public DisplayMode[] getDisplayModes()
197 if (fixedDisplayMode != null)
198 return new DisplayMode[] { fixedDisplayMode };
202 if (displayModes == null)
203 displayModes = nativeGetDisplayModes(env);
206 ArrayList<DisplayMode> list = new ArrayList<DisplayMode>();
207 for(int i=0;i<displayModes.length;i++)
208 for(int j=0;j<displayModes[i].rates.length;j++)
209 list.add(new DisplayMode(displayModes[i].width,
210 displayModes[i].height,
211 DisplayMode.BIT_DEPTH_MULTI,
212 displayModes[i].rates[j]));
214 return list.toArray(new DisplayMode[list.size()]);
217 native X11DisplayMode[] nativeGetDisplayModes(GdkGraphicsEnvironment env);
220 * Real fullscreen exclusive mode is not supported.
222 * @return <code>false</code>
225 public boolean isFullScreenSupported()
230 public boolean isDisplayChangeSupported()
232 return fixedDisplayMode == null;
235 public void setDisplayMode(DisplayMode dm)
237 if (fixedDisplayMode != null)
238 throw new UnsupportedOperationException("Cannnot change display mode.");
241 throw new IllegalArgumentException("DisplayMode must not be null.");
245 if (displayModes == null)
246 displayModes = nativeGetDisplayModes(env);
249 for (int i=0; i<displayModes.length; i++)
250 if (displayModes[i].width == dm.getWidth()
251 && displayModes[i].height == dm.getHeight())
255 nativeSetDisplayMode(env,
257 (short) dm.getRefreshRate());
265 throw new IllegalArgumentException("Mode not supported by this device.");
268 native void nativeSetDisplayMode(GdkGraphicsEnvironment env,
269 int index, short rate);
271 /** A class that simply encapsulates the X11 display mode data.
273 static class X11DisplayMode
279 X11DisplayMode(int width, int height, short[] rates)
282 this.height = height;
288 public void setFullScreenWindow(Window w)
290 // Bring old fullscreen window back into its original state.
291 if (fullscreenWindow != null && w != fullscreenWindow)
293 if (fullscreenWindow instanceof Frame)
295 // Decoration state can only be switched when the peer is
296 // non-existent. That means we have to dispose the
298 Frame f = (Frame) fullscreenWindow;
299 if (oldWindowDecorationState != f.isUndecorated())
302 f.setUndecorated(oldWindowDecorationState);
306 fullscreenWindow.setBounds(oldWindowBounds);
308 if (!fullscreenWindow.isVisible())
309 fullscreenWindow.setVisible(true);
312 // If applicable remove decoration, then maximize the window and
313 // bring it to the foreground.
316 if (w instanceof Frame)
319 oldWindowDecorationState = f.isUndecorated();
320 if (!oldWindowDecorationState)
323 f.setUndecorated(true);
327 oldWindowBounds = w.getBounds();
329 DisplayMode dm = getDisplayMode();
331 w.setBounds(0, 0, dm.getWidth(), dm.getHeight());
341 fullscreenWindow = w;
344 public Window getFullScreenWindow()
346 return fullscreenWindow;
349 Rectangle getBounds()
354 bounds = nativeGetBounds();
360 native Rectangle nativeGetBounds();