Code clean-up and minor optimizations. Also made sure that the current mode is always first.

This commit is contained in:
Ioannis Tsakpinis 2014-09-13 12:18:40 +03:00
parent 818adb7312
commit 46f602f0c6
1 changed files with 233 additions and 283 deletions

View File

@ -27,17 +27,14 @@
package org.lwjgl.opengl; package org.lwjgl.opengl;
import org.lwjgl.LWJGLUtil;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.*;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
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
@ -45,384 +42,337 @@ import org.lwjgl.LWJGLUtil;
* *
* @author ryanm * @author ryanm
*/ */
public class XRandR public class XRandR {
{
private static Screen[] current; private static Screen[] current;
/** /**
* Either the screen marked as "primary" (if it is turned on) * Either the screen marked as "primary" (if it is turned on)
* or the one with the largest (current) resolution. * or the one with the largest (current) resolution.
*/ */
private static String primaryScreenIdentifier; private static String primaryScreenIdentifier;
/** /**
* Used to save the configuration of all output devices to * Used to save the configuration of all output devices to
* restore it on exit or in case of crash. * restore it on exit or in case of crash.
*/ */
private static Screen[] savedConfiguration; private static Screen[] savedConfiguration;
private static Map<String, Screen[]> screens; private static Map<String, Screen[]> screens;
private static void populate() private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
{
if( screens == null )
{
screens = new HashMap<String, Screen[]>();
// ProcessBuilder pb = new ProcessBuilder( "xrandr", "-q" ); private static void populate() {
// pb.redirectErrorStream(); if ( screens != null )
try return;
{
// Process p= pb.start();
Process p = Runtime.getRuntime().exec( new String[] { "xrandr", "-q" } );
List<Screen> currentList = new ArrayList<Screen>(); screens = new HashMap<String, Screen[]>();
List<Screen> possibles = new ArrayList<Screen>();
String name = null;
// saves the position of the current screen. this is specified in the header of the screen block,
// but required later when parsing the screen modelines
int[] currentScreenPosition = new int[2];
BufferedReader br = new BufferedReader( new InputStreamReader( p.getInputStream() ) ); try {
String line; Process p = Runtime.getRuntime().exec(new String[] { "xrandr", "-q" });
while( ( line = br.readLine() ) != null )
{
line = line.trim();
String[] sa = line.split( "\\s+" );
if( "connected".equals(sa[1]) ) List<Screen> currentList = new ArrayList<Screen>();
{ List<Screen> possibles = new ArrayList<Screen>();
// found a new screen block String name = null;
if( name != null ) // saves the position of the current screen. this is specified in the header of the screen block,
{ // but required later when parsing the screen modelines
screens.put( name, possibles.toArray( new Screen[ possibles.size() ] ) ); int[] currentScreenPosition = new int[2];
possibles.clear();
}
name = sa[ 0 ];
// save position of this screen, will be used later when current modeline is parsed BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
if ("primary".equals(sa[ 2 ])) { String line;
parseScreenHeader(currentScreenPosition, sa[ 3 ]); while ( (line = br.readLine()) != null ) {
// save primary line = line.trim();
primaryScreenIdentifier = name; String[] sa = WHITESPACE_PATTERN.split(line);
} else {
parseScreenHeader(currentScreenPosition, sa[ 2 ]); if ( "connected".equals(sa[1]) ) {
} // found a new screen block
if ( name != null ) {
screens.put(name, possibles.toArray(new Screen[possibles.size()]));
possibles.clear();
} }
else if( Pattern.matches( "\\d*x\\d*", sa[ 0 ] ) ) name = sa[0];
{
// save position of this screen, will be used later when current modeline is parsed
if ( "primary".equals(sa[2]) ) {
parseScreenHeader(currentScreenPosition, sa[3]);
// save primary
primaryScreenIdentifier = name;
} else {
parseScreenHeader(currentScreenPosition, sa[2]);
}
} else {
Matcher m = SCREEN_MODELINE_PATTERN.matcher(sa[0]);
if ( m.matches() ) {
// found a new mode line // found a new mode line
parseScreenModeline( possibles, currentList, name, sa[ 0 ], Arrays.copyOfRange(sa, 1, sa.length), currentScreenPosition); parseScreenModeline(
possibles, currentList, name,
Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)),
sa, currentScreenPosition
);
} }
} }
screens.put( name, possibles.toArray( new Screen[ possibles.size() ] ) );
current = currentList.toArray(new Screen[currentList.size()]);
// set primary to largest screen if not set yet
if (primaryScreenIdentifier == null) {
long totalPixels = Long.MIN_VALUE;
for (Screen screen : current) {
if (1l * screen.width * screen.height > totalPixels) {
primaryScreenIdentifier = screen.name;
totalPixels = 1l * screen.width * screen.height;
}
}
}
} }
catch( Throwable e )
{ screens.put(name, possibles.toArray(new Screen[possibles.size()]));
LWJGLUtil.log( "Exception in XRandR.populate(): " + e.getMessage() );
screens.clear(); current = currentList.toArray(new Screen[currentList.size()]);
current = new Screen[ 0 ];
// set primary to largest screen if not set yet
if ( primaryScreenIdentifier == null ) {
long totalPixels = Long.MIN_VALUE;
for ( Screen screen : current ) {
if ( 1l * screen.width * screen.height > totalPixels ) {
primaryScreenIdentifier = screen.name;
totalPixels = 1l * screen.width * screen.height;
}
}
} }
} catch (Throwable e) {
LWJGLUtil.log("Exception in XRandR.populate(): " + e.getMessage());
screens.clear();
current = new Screen[0];
} }
} }
/** /**
* @return The current screen configuration of the primary device, * @return The current screen configuration of the primary device,
* or an empty array if xrandr is not supported * or an empty array if xrandr is not supported
*/ */
public static Screen[] getConfiguration() public static Screen[] getConfiguration() {
{
populate(); populate();
// find and return primary // find and return primary
for (Screen screen : current) { for ( Screen screen : current ) {
if (screen.name.equals(primaryScreenIdentifier)) { if ( screen.name.equals(primaryScreenIdentifier) ) {
return new Screen[]{screen}; return new Screen[] { screen };
} }
} }
// problem with primary device, fall back to old behaviour // problem with primary device, fall back to old behaviour
return current.clone(); return current.clone();
} }
/** /**
* @param disableOthers * @param disableOthers if screens not included in screens should be turned off (true) or left alone (false)
* if screens not included in screens should be turned off (true) or left alone (false) * @param screens The desired screen set, may not be <code>null</code>
* @param screens *
* The desired screen set, may not be <code>null</code> * @throws IllegalArgumentException if no screens are specified
* @throws IllegalArgumentException
* if no screens are specified
*/ */
public static void setConfiguration(boolean disableOthers, Screen... screens) public static void setConfiguration(boolean disableOthers, 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" );
List<String> cmd = new ArrayList<String>(); List<String> cmd = new ArrayList<String>();
cmd.add( "xrandr" ); cmd.add("xrandr");
if (disableOthers) { if ( disableOthers ) {
// 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 ( Screen screen : current ) { for ( Screen screen : current ) {
boolean found = false; boolean disable = true;
for ( Screen screen1 : screens ) { for ( Screen screen1 : screens ) {
if ( screen1.name.equals(screen.name) ) { if ( screen1.name.equals(screen.name) ) {
found = true; disable = false;
break; break;
} }
} }
if ( !found ) { if ( disable ) {
cmd.add("--output"); cmd.add("--output");
cmd.add(screen.name); cmd.add(screen.name);
cmd.add("--off"); cmd.add("--off");
} }
} }
} }
// set up new set // set up new set
for ( Screen screen : screens ) for ( Screen screen : screens )
screen.getArgs(cmd); screen.getArgs(cmd);
try try {
{ Process p = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
// ProcessBuilder pb = new ProcessBuilder( cmd );
// pb.redirectErrorStream();
// Process p = pb.start();
Process p =
Runtime.getRuntime().exec( 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 ) {
{ LWJGLUtil.log("Unexpected output from xrandr process: " + line);
LWJGLUtil.log( "Unexpected output from xrandr process: " + line );
} }
current = screens; current = screens;
} } catch (IOException e) {
catch( IOException e ) LWJGLUtil.log("XRandR exception in setConfiguration(): " + e.getMessage());
{
LWJGLUtil.log( "XRandR exception in setConfiguration(): " + e.getMessage() );
} }
} }
/** /**
* Saves the current configuration for all connected display devices. * Saves the current configuration for all connected display devices.
* This configuration can be restored on exit/crash by calling * This configuration can be restored on exit/crash by calling
* restoreConfiguration() * restoreConfiguration()
*/ */
public static void saveConfiguration() { public static void saveConfiguration() {
populate(); populate();
savedConfiguration = current.clone(); savedConfiguration = current.clone();
} }
/** /**
* Restores the configuration for all connected display devices. * Restores the configuration for all connected display devices.
* Used on exit or in case of a crash to reset all devices. * Used on exit or in case of a crash to reset all devices.
*/ */
public static void restoreConfiguration() { public static void restoreConfiguration() {
if (savedConfiguration != null) { if ( savedConfiguration != null ) {
setConfiguration(true, savedConfiguration); setConfiguration(true, savedConfiguration);
} }
} }
/** /**
* @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 screens.keySet().toArray( new String[ screens.size() ] ); return screens.keySet().toArray(new String[screens.size()]);
} }
/** /**
* @param name * @param name
*
* @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 screens.get(name).clone(); return screens.get(name).clone();
} }
private static final Pattern SCREEN_HEADER_PATTERN = private static final Pattern SCREEN_HEADER_PATTERN = Pattern.compile("^(\\d+)x(\\d+)[+](\\d+)[+](\\d+)$");
Pattern.compile( "^(\\d+)x(\\d+)\\+(\\d+)\\+(\\d+)$" ); private static final Pattern SCREEN_MODELINE_PATTERN = Pattern.compile("^(\\d+)x(\\d+)$");
private static final Pattern FREQ_PATTERN = Pattern.compile("^(\\d+)[.](\\d+)(?:\\s*[*])?(?:\\s*[+])?$");
private static final Pattern SCREEN_MODELINE_PATTERN = Pattern.compile( "^(\\d+)x(\\d+)$" ); /**
private static final Pattern FREQ_PATTERN = Pattern.compile("^(\\d+).(\\d+)[\\*,\\s]?\\+?$");
/**
* Parses a screen configuration and adds it to one of the lists if valid. * Parses a screen configuration and adds it to one of the lists if valid.
* *
* @param allModes * @param allModes the list to add the Screen to if it's valid
* the list to add the Screen to if it's valid * @param current the list to add the current screen config to
* @param current * @param name the name of this screen
* the list to add the current screen config to * @param modeLine config string
* @param name * @param screenPosition position of this screen
* the name of this screen
* @param res
* config string, format widthxheight
* @param freqs
* config strings, frequency as float, with optional * and +
* @param screenPosition
* position of this screen, null defaults to 0,0
*/ */
private static void parseScreenModeline( List<Screen> allModes, List<Screen> current, String name, String res, String[] freqs, int[] screenPosition) private static void parseScreenModeline(List<Screen> allModes, List<Screen> current, String name, int width, int height, String[] modeLine, int[] screenPosition) {
{ for ( int i = 1; i < modeLine.length; i++ ) {
Matcher m = SCREEN_MODELINE_PATTERN.matcher( res ); String freqS = modeLine[i];
if( !m.matches() ) if ( "+".equals(freqS) ) {
{ // previous rate was the "preferred" refresh rate
LWJGLUtil.log( "Did not match: " + res ); // no way to get this info to the application, so ignore it
return; continue;
} }
int width = Integer.parseInt( m.group( 1 ) );
int height = Integer.parseInt( m.group( 2 ) );
int xpos = screenPosition[0];
int ypos = screenPosition[1];
for (String freqS : freqs) { Matcher m = FREQ_PATTERN.matcher(freqS);
if ("+".equals(freqS)) { if ( !m.matches() ) {
// previous rate was the "preferred" refresh rate LWJGLUtil.log("Frequency match failed: " + Arrays.toString(modeLine));
// no way to get this info to the application, so ignore it return;
continue; }
}
m = FREQ_PATTERN.matcher(freqS); int freq = Integer.parseInt(m.group(1));
if( !m.matches() )
{ Screen s = new Screen(name, width, height, freq, 0, 0);
LWJGLUtil.log( "Did not match: " + res ); if ( freqS.contains("*") ) {
return; // current mode, save to current list with screen position
} current.add(new Screen(name, width, height, freq, screenPosition[0], screenPosition[1]));
int freq = Integer.parseInt(m.group(1)); // make sure the current mode is always first
if (freqS.contains("*")) { allModes.add(0, s);
// current mode, save to current list with screen position } else {
current.add( new Screen( name, width, height, freq, xpos, ypos ) ); // always add to List of all modes without screen position
} allModes.add(s);
// always add to List of all modes without screen position }
allModes.add( new Screen( name, width, height, freq, 0, 0 ) ); }
}
} }
/** /**
* Parses a screen configuration header and extracts information about the position of the screen. * Parses a screen configuration header and extracts information about the position of the screen.
* *
* @param screenPosition the int-array to write the position into * @param screenPosition the int-array to write the position into
* @param resPos String containing resolution and position, from xrandr * @param resPos String containing resolution and position, from xrandr
*/ */
private static void parseScreenHeader(int[] screenPosition, String resPos) { private static void parseScreenHeader(int[] screenPosition, String resPos) {
Matcher m = SCREEN_HEADER_PATTERN.matcher(resPos); Matcher m = SCREEN_HEADER_PATTERN.matcher(resPos);
if (!m.matches()) { if ( !m.matches() ) {
// screen not active! // screen not active!
screenPosition[0] = 0; screenPosition[0] = 0;
screenPosition[1] = 0; screenPosition[1] = 0;
return; return;
} }
screenPosition[0] = Integer.parseInt(m.group(3)); screenPosition[0] = Integer.parseInt(m.group(3));
screenPosition[1] = Integer.parseInt(m.group(4)); screenPosition[1] = Integer.parseInt(m.group(4));
} }
static Screen DisplayModetoScreen(DisplayMode mode) { static Screen DisplayModetoScreen(DisplayMode mode) {
populate(); populate();
Screen primary = findPrimary(current); Screen primary = findPrimary(current);
return new Screen(primary.name, mode.getWidth(), mode.getHeight(), mode.getFrequency(), primary.xPos, primary.yPos); return new Screen(primary.name, mode.getWidth(), mode.getHeight(), mode.getFrequency(), primary.xPos, primary.yPos);
} }
static DisplayMode ScreentoDisplayMode(Screen... screens) { static DisplayMode ScreentoDisplayMode(Screen... screens) {
populate(); populate();
Screen primary = findPrimary(screens); Screen primary = findPrimary(screens);
return new DisplayMode(primary.width, primary.height, 24, primary.freq); return new DisplayMode(primary.width, primary.height, 24, primary.freq);
} }
private static Screen findPrimary(Screen... screens) { private static Screen findPrimary(Screen... screens) {
for (Screen screen : screens) { for ( Screen screen : screens ) {
if (screen.name.equals(primaryScreenIdentifier)) { if ( screen.name.equals(primaryScreenIdentifier) ) {
return screen; return screen;
} }
} }
// fallback // fallback
return screens[0]; return screens[0];
} }
/** /**
* Encapsulates the configuration of a monitor. * Encapsulates the configuration of a monitor.
* Resolution and freq are fixed, position is mutable * Resolution and freq are fixed, position is mutable
* *
* @author ryanm * @author ryanm
*/ */
public static class Screen implements Cloneable public static class Screen implements Cloneable {
{ /** Name for this output */
/**
* Name for this output
*/
public final String name; public final String name;
/** /** Width in pixels */
* Width in pixels
*/
public final int width; public final int width;
/** /** Height in pixels */
* Height in pixels
*/
public final int height; public final int height;
/** /** Frequency in Hz */
* Frequency in Hz public final int freq;
*/
public final int freq;
/** /** Position on the x-axis, in pixels */
* Position on the x-axis, in pixels
*/
public int xPos; public int xPos;
/** /** Position on the y-axis, in pixels */
* Position on the y-axis, in pixels
*/
public int yPos; public int yPos;
Screen( String name, int width, int height, int freq, int xPos, int yPos ) Screen(String name, int width, int height, int freq, int xPos, int yPos) {
{
this.name = name; this.name = name;
this.width = width; this.width = width;
this.height = height; this.height = height;
this.freq = freq; this.freq = freq;
this.xPos = xPos; this.xPos = xPos;
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" ); argList.add(width + "x" + height);
argList.add( width + "x" + height ); argList.add("--rate");
argList.add( "--rate" ); argList.add(Integer.toString(freq));
argList.add( freq + "");//"" autoboxes freq as String argList.add("--pos");
argList.add( "--pos" ); 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 + " with " + freq + "Hz"; return name + " " + width + "x" + height + " @ " + xPos + "x" + yPos + " with " + freq + "Hz";
} }
} }