changeset 4949:e53a078c2840

7045370: Java Statically Determines Display Size on Linux platforms Summary: Listen to ConfigureNotify events on the root window and update the current screen size accordingly Reviewed-by: art, bae
author anthony
date Wed, 09 Nov 2011 13:43:39 +0300
parents f8a3dff76b48
children 84e473cf4531
files src/share/classes/java/awt/Component.java src/solaris/classes/sun/awt/X11/XToolkit.java src/solaris/classes/sun/awt/X11GraphicsDevice.java src/solaris/native/sun/awt/awt_GraphicsEnv.c
diffstat 4 files changed, 65 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/java/awt/Component.java	Tue Nov 08 14:36:50 2011 +0300
+++ b/src/share/classes/java/awt/Component.java	Wed Nov 09 13:43:39 2011 +0300
@@ -1158,6 +1158,10 @@
     boolean updateGraphicsData(GraphicsConfiguration gc) {
         checkTreeLock();
 
+        if (graphicsConfig == gc) {
+            return false;
+        }
+
         graphicsConfig = gc;
 
         ComponentPeer peer = getPeer();
--- a/src/solaris/classes/sun/awt/X11/XToolkit.java	Tue Nov 08 14:36:50 2011 +0300
+++ b/src/solaris/classes/sun/awt/X11/XToolkit.java	Wed Nov 09 13:43:39 2011 +0300
@@ -51,6 +51,7 @@
 import sun.awt.*;
 import sun.font.FontConfigManager;
 import sun.font.FontManager;
+import sun.java2d.SunGraphicsEnvironment;
 import sun.misc.PerformanceLogger;
 import sun.print.PrintJob2D;
 import sun.security.action.GetBooleanAction;
@@ -109,7 +110,7 @@
     static int awt_multiclick_time;
     static boolean securityWarningEnabled;
 
-    private static int screenWidth = -1, screenHeight = -1; // Dimensions of default screen
+    private static volatile int screenWidth = -1, screenHeight = -1; // Dimensions of default screen
     static long awt_defaultFg; // Pixel
     private static XMouseInfoPeer xPeer;
     private static Method m_removeSourceEvents;
@@ -310,6 +311,19 @@
             System.setProperty("sun.awt.enableExtraMouseButtons", ""+areExtraMouseButtonsEnabled);
 
             saved_error_handler = XlibWrapper.SetToolkitErrorHandler();
+
+            // Detect display mode changes
+            XlibWrapper.XSelectInput(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), XConstants.StructureNotifyMask);
+            XToolkit.addEventDispatcher(XToolkit.getDefaultRootWindow(), new XEventDispatcher() {
+                @Override
+                public void dispatchEvent(XEvent ev) {
+                    if (ev.get_type() == XConstants.ConfigureNotify) {
+                        ((X11GraphicsEnvironment)GraphicsEnvironment.
+                         getLocalGraphicsEnvironment()).
+                            displayChanged();
+                    }
+                }
+            });
         } finally {
             awtUnlock();
         }
@@ -684,29 +698,49 @@
         }
     }
 
-    static int getDefaultScreenWidth() {
-        if (screenWidth == -1) {
-            long display = getDisplay();
+    static {
+        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
+        if (ge instanceof SunGraphicsEnvironment) {
+            ((SunGraphicsEnvironment)ge).addDisplayChangedListener(
+                new DisplayChangedListener() {
+                    @Override
+                    public void displayChanged() {
+                        // 7045370: Reset the cached values
+                        XToolkit.screenWidth = -1;
+                        XToolkit.screenHeight = -1;
+                    }
+
+                    @Override
+                    public void paletteChanged() {}
+            });
+        }
+    }
+
+    private static void initScreenSize() {
+        if (screenWidth == -1 || screenHeight == -1) {
             awtLock();
             try {
-                screenWidth = (int) XlibWrapper.DisplayWidth(display, XlibWrapper.DefaultScreen(display));
+                XWindowAttributes pattr = new XWindowAttributes();
+                try {
+                    XlibWrapper.XGetWindowAttributes(XToolkit.getDisplay(), XToolkit.getDefaultRootWindow(), pattr.pData);
+                    screenWidth  = (int) pattr.get_width();
+                    screenHeight = (int) pattr.get_height();
+                } finally {
+                    pattr.dispose();
+                }
             } finally {
                 awtUnlock();
             }
         }
+    }
+
+    static int getDefaultScreenWidth() {
+        initScreenSize();
         return screenWidth;
     }
 
     static int getDefaultScreenHeight() {
-        if (screenHeight == -1) {
-            long display = getDisplay();
-            awtLock();
-            try {
-                screenHeight = (int) XlibWrapper.DisplayHeight(display, XlibWrapper.DefaultScreen(display));
-            } finally {
-                awtUnlock();
-            }
-        }
+        initScreenSize();
         return screenHeight;
     }
 
--- a/src/solaris/classes/sun/awt/X11GraphicsDevice.java	Tue Nov 08 14:36:50 2011 +0300
+++ b/src/solaris/classes/sun/awt/X11GraphicsDevice.java	Wed Nov 09 13:43:39 2011 +0300
@@ -487,14 +487,9 @@
      * X11GraphicsEnvironment when the display mode has been changed.
      */
     public synchronized void displayChanged() {
-        // reset the list of configs (and default config)
-        defaultConfig = null;
-        configs = null;
-        doubleBufferVisuals = null;
-
-        // reset the native data structures associated with this device (they
-        // will be reinitialized when the GraphicsConfigs are configured)
-        resetNativeData(screen);
+        // On X11 the visuals do not change, and therefore we don't need
+        // to reset the defaultConfig, config, doubleBufferVisuals,
+        // neither do we need to reset the native data.
 
         // pass on to all top-level windows on this screen
         topLevels.notifyListeners();
--- a/src/solaris/native/sun/awt/awt_GraphicsEnv.c	Tue Nov 08 14:36:50 2011 +0300
+++ b/src/solaris/native/sun/awt/awt_GraphicsEnv.c	Wed Nov 09 13:43:39 2011 +0300
@@ -1434,11 +1434,17 @@
                                                         fbrects[screen].height);
         }
         else {
+            XWindowAttributes xwa;
+            memset(&xwa, 0, sizeof(xwa));
+
+            AWT_LOCK ();
+            XGetWindowAttributes(awt_display,
+                    RootWindow(awt_display, adata->awt_visInfo.screen),
+                    &xwa);
+            AWT_UNLOCK ();
+
             bounds = (*env)->NewObject(env, clazz, mid, 0, 0,
-                                   DisplayWidth(awt_display,
-                                                adata->awt_visInfo.screen),
-                                   DisplayHeight(awt_display,
-                                                 adata->awt_visInfo.screen));
+                    xwa.width, xwa.height);
         }
 
         if ((*env)->ExceptionOccurred(env)) {