Further XRandR fixes, it now catches throwable in populate(), if there are any problems in parsing the xrandr output we revert to LWJGL's regular behaviour. All output now goes to LWJGLUtil.log() instead of the out.println and err.println. Credit to Ryanm for patch.

This commit is contained in:
kappa1 2010-05-21 22:12:57 +00:00
parent fc21a7735d
commit a54c5c40e1
1 changed files with 163 additions and 126 deletions

View File

@ -1,33 +1,28 @@
/* /*
* Copyright (c) 2002-2010 LWJGL Project * Copyright (c) 2002-2010 LWJGL Project All rights reserved.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are * modification, are permitted provided that the following conditions
* met: * are met: * Redistributions of source code must retain the above
* * copyright notice, this list of conditions and the following
* * Redistributions of source code must retain the above copyright * disclaimer. * Redistributions in binary form must reproduce the
* notice, this list of conditions and the following disclaimer. * above copyright notice, this list of conditions and the following
* * disclaimer in the documentation and/or other materials provided
* * Redistributions in binary form must reproduce the above copyright * with the distribution. * Neither the name of 'LWJGL' nor the names
* notice, this list of conditions and the following disclaimer in the * of its contributors may be used to endorse or promote products
* documentation and/or other materials provided with the distribution. * derived from this software without specific prior written
* * permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* * Neither the name of 'LWJGL' nor the names of * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* its contributors may be used to endorse or promote products derived * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* from this software without specific prior written permission. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/ */
package org.lwjgl.opengl; package org.lwjgl.opengl;
@ -42,25 +37,30 @@ import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.lwjgl.LWJGLUtil;
/** /**
* Utility for working with the xrandr commmand-line utility. Assumes * Utility for working with the xrandr commmand-line utility. Assumes
* xrandr v1.2 or higher. * xrandr v1.2 or higher.
* *
* @author ryanm * @author ryanm
*/ */
public class XRandR { public class XRandR
{
private static Screen[] current; private static Screen[] current;
private static Map /* <String, Screen[]> */screens; private static Map /* <String, Screen[]> */screens;
private static void populate() { private static void populate()
if (screens == null) { {
if( screens == null )
{
screens = new HashMap/* <String, Screen[]> */(); screens = new HashMap/* <String, Screen[]> */();
// ProcessBuilder pb = new ProcessBuilder( "xrandr", "-q" ); // ProcessBuilder pb = new ProcessBuilder( "xrandr", "-q" );
// pb.redirectErrorStream(); // pb.redirectErrorStream();
try { try
{
// Process p= pb.start(); // Process p= pb.start();
Process p = Runtime.getRuntime().exec( new String[] { "xrandr", "-q" } ); Process p = Runtime.getRuntime().exec( new String[] { "xrandr", "-q" } );
@ -70,13 +70,16 @@ public class XRandR {
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line; String line;
while ((line = br.readLine()) != null) { while( ( line = br.readLine() ) != null )
{
line = line.trim(); line = line.trim();
String[] sa = line.split( "\\s+" ); String[] sa = line.split( "\\s+" );
if (sa[1].equals("connected")) { if( sa[ 1 ].equals( "connected" ) )
{
// found a new screen block // found a new screen block
if (name != null) { if( name != null )
{
screens.put( name, possibles.toArray( new Screen[ possibles.size() ] ) ); screens.put( name, possibles.toArray( new Screen[ possibles.size() ] ) );
possibles.clear(); possibles.clear();
} }
@ -84,7 +87,9 @@ public class XRandR {
// record the current config // record the current config
parseScreen( currentList, name, sa[ 2 ] ); parseScreen( currentList, name, sa[ 2 ] );
} else if (Pattern.matches("\\d*x\\d*", sa[0])) { }
else if( Pattern.matches( "\\d*x\\d*", sa[ 0 ] ) )
{
// found a new mode line // found a new mode line
parseScreen( possibles, name, sa[ 0 ] ); parseScreen( possibles, name, sa[ 0 ] );
} }
@ -93,8 +98,12 @@ public class XRandR {
screens.put( name, possibles.toArray( new Screen[ possibles.size() ] ) ); screens.put( name, possibles.toArray( new Screen[ possibles.size() ] ) );
current = ( Screen[] ) currentList.toArray( new Screen[ currentList.size() ] ); current = ( Screen[] ) currentList.toArray( new Screen[ currentList.size() ] );
} catch (IOException e) { }
e.printStackTrace(); catch( Throwable e )
{
LWJGLUtil.log( "Exception in XRandR.populate(): " + e.getMessage() );
screens.clear();
current = new Screen[ 0 ];
} }
} }
} }
@ -103,18 +112,23 @@ public class XRandR {
* @return The current screen configuration, or an empty array if * @return The current screen configuration, or an empty array if
* xrandr is not supported * xrandr is not supported
*/ */
public static Screen[] getConfiguration() { public static Screen[] getConfiguration()
{
populate(); populate();
return (Screen[]) current.clone(); return current.clone();
} }
/** /**
* @param screens * @param screens
* The desired screen set, may not be <code>null</code> * The desired screen set, may not be <code>null</code>
* @throws IllegalArgumentException
* if no screens are specified
*/ */
public static void setConfiguration(Screen[]/* ... */screens) { public static void setConfiguration( Screen[]/* ... */screens )
if (screens.length == 0) { {
if( screens.length == 0 )
{
throw new IllegalArgumentException( "Must specify at least one screen" ); throw new IllegalArgumentException( "Must specify at least one screen" );
} }
@ -122,16 +136,20 @@ public class XRandR {
cmd.add( "xrandr" ); cmd.add( "xrandr" );
// switch off those in the current set not in the new set // switch off those in the current set not in the new set
for (int i = 0; i < current.length; i++) { for( int i = 0; i < current.length; i++ )
{
boolean found = false; boolean found = false;
for (int j = 0; j < screens.length; j++) { for( int j = 0; j < screens.length; j++ )
if (screens[j].name.equals(current[i].name)) { {
if( screens[ j ].name.equals( current[ i ].name ) )
{
found = true; found = true;
break; break;
} }
} }
if (!found) { if( !found )
{
cmd.add( "--output" ); cmd.add( "--output" );
cmd.add( current[ i ].name ); cmd.add( current[ i ].name );
cmd.add( "--off" ); cmd.add( "--off" );
@ -139,33 +157,39 @@ public class XRandR {
} }
// set up new set // set up new set
for (int i = 0; i < screens.length; i++) { for( int i = 0; i < screens.length; i++ )
{
screens[ i ].getArgs( cmd ); screens[ i ].getArgs( cmd );
} }
try { try
{
// ProcessBuilder pb = new ProcessBuilder( cmd ); // ProcessBuilder pb = new ProcessBuilder( cmd );
// pb.redirectErrorStream(); // pb.redirectErrorStream();
// Process p = pb.start(); // Process p = pb.start();
Process p = Runtime.getRuntime().exec((String[]) cmd.toArray(new String[cmd.size()])); Process p =
Runtime.getRuntime().exec( ( String[] ) cmd.toArray( new String[ cmd.size() ] ) );
// no output is expected, but check anyway // no output is expected, but check anyway
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) );
String line; String line;
while ((line = br.readLine()) != null) { while( ( line = br.readLine() ) != null )
System.out.println(line); {
LWJGLUtil.log( "Unexpected output from xrandr process: " + line );
} }
current = screens; current = screens;
} catch (IOException e) {
e.printStackTrace();
} }
catch( IOException e )
{
LWJGLUtil.log( "XRandR exception in setConfiguration(): " + e.getMessage() );
}
} }
/** /**
* @return the name of connected screens, or an empty array if * @return the name of connected screens, or an empty array if
* xrandr is not supported * xrandr is not supported
*/ */
public static String[] getScreenNames() { public static String[] getScreenNames()
{
populate(); populate();
return ( String[] ) screens.keySet().toArray( new String[ screens.size() ] ); return ( String[] ) screens.keySet().toArray( new String[ screens.size() ] );
} }
@ -175,17 +199,21 @@ public class XRandR {
* @return the possible resolutions of the named screen, or * @return the possible resolutions of the named screen, or
* <code>null</code> if there is no such screen * <code>null</code> if there is no such screen
*/ */
public static Screen[] getResolutions(String name) { public static Screen[] getResolutions( String name )
{
populate(); populate();
// clone the array to prevent held copies being altered // clone the array to prevent held copies being altered
return (Screen[]) ((Screen[]) screens.get(name)).clone(); return ( ( Screen[] ) screens.get( name ) ).clone();
} }
private static final Pattern SCREEN_PATTERN1 = Pattern.compile("^(\\d+)x(\\d+)\\+(\\d+)\\+(\\d+)$"); private static final Pattern SCREEN_PATTERN1 =
Pattern.compile( "^(\\d+)x(\\d+)\\+(\\d+)\\+(\\d+)$" );
private static final Pattern SCREEN_PATTERN2 = Pattern.compile( "^(\\d+)x(\\d+)$" ); private static final Pattern SCREEN_PATTERN2 = Pattern.compile( "^(\\d+)x(\\d+)$" );
/** /**
* Parses a screen configuration and adds it to the list if it's valid. * Parses a screen configuration and adds it to the list if it's
* valid.
* *
* @param list * @param list
* the list to add the Screen to if it's valid * the list to add the Screen to if it's valid
@ -195,11 +223,14 @@ public class XRandR {
* config string, format either widthxheight or * config string, format either widthxheight or
* widthxheight+xPos+yPos * widthxheight+xPos+yPos
*/ */
private static void parseScreen(List /* <Screen> */ list, String name, String what) { private static void parseScreen( List /* <Screen> */list, String name, String what )
{
Matcher m = SCREEN_PATTERN1.matcher( what ); Matcher m = SCREEN_PATTERN1.matcher( what );
if(!m.matches()) { if( !m.matches() )
{
m = SCREEN_PATTERN2.matcher( what ); m = SCREEN_PATTERN2.matcher( what );
if(!m.matches()) { if( !m.matches() )
{
System.out.println( "Did not match: " + what ); System.out.println( "Did not match: " + what );
return; return;
} }
@ -207,10 +238,13 @@ public class XRandR {
int width = Integer.parseInt( m.group( 1 ) ); int width = Integer.parseInt( m.group( 1 ) );
int height = Integer.parseInt( m.group( 2 ) ); int height = Integer.parseInt( m.group( 2 ) );
int xpos, ypos; int xpos, ypos;
if(m.groupCount() > 3) { if( m.groupCount() > 3 )
{
xpos = Integer.parseInt( m.group( 3 ) ); xpos = Integer.parseInt( m.group( 3 ) );
ypos = Integer.parseInt( m.group( 4 ) ); ypos = Integer.parseInt( m.group( 4 ) );
} else { }
else
{
xpos = 0; xpos = 0;
ypos = 0; ypos = 0;
} }
@ -223,8 +257,8 @@ public class XRandR {
* *
* @author ryanm * @author ryanm
*/ */
public static class Screen implements Cloneable { public static class Screen implements Cloneable
{
/** /**
* Name for this output * Name for this output
*/ */
@ -250,7 +284,8 @@ public class XRandR {
*/ */
public int yPos = 0; public int yPos = 0;
private Screen(String name, int width, int height, int xPos, int yPos) { private Screen( String name, int width, int height, int xPos, int yPos )
{
this.name = name; this.name = name;
this.width = width; this.width = width;
this.height = height; this.height = height;
@ -258,7 +293,8 @@ public class XRandR {
this.yPos = yPos; this.yPos = yPos;
} }
private void getArgs(List/* <String> */argList) { private void getArgs( List/* <String> */argList )
{
argList.add( "--output" ); argList.add( "--output" );
argList.add( name ); argList.add( name );
argList.add( "--mode" ); argList.add( "--mode" );
@ -267,8 +303,9 @@ public class XRandR {
argList.add( xPos + "x" + yPos ); argList.add( xPos + "x" + yPos );
} }
// @Override @Override
public String toString() { public String toString()
{
return name + " " + width + "x" + height + " @ " + xPos + "x" + yPos; return name + " " + width + "x" + height + " @ " + xPos + "x" + yPos;
} }
} }