Code clean-up and minor optimizations. Also made sure that the current mode is always first.
This commit is contained in:
parent
818adb7312
commit
46f602f0c6
|
@ -27,17 +27,14 @@
|
|||
|
||||
package org.lwjgl.opengl;
|
||||
|
||||
import org.lwjgl.LWJGLUtil;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import org.lwjgl.LWJGLUtil;
|
||||
|
||||
/**
|
||||
* Utility for working with the xrandr commmand-line utility. Assumes
|
||||
|
@ -45,8 +42,8 @@ import org.lwjgl.LWJGLUtil;
|
|||
*
|
||||
* @author ryanm
|
||||
*/
|
||||
public class XRandR
|
||||
{
|
||||
public class XRandR {
|
||||
|
||||
private static Screen[] current;
|
||||
|
||||
/**
|
||||
|
@ -63,17 +60,15 @@ public class XRandR
|
|||
|
||||
private static Map<String, Screen[]> screens;
|
||||
|
||||
private static void populate()
|
||||
{
|
||||
if( screens == null )
|
||||
{
|
||||
private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
|
||||
|
||||
private static void populate() {
|
||||
if ( screens != null )
|
||||
return;
|
||||
|
||||
screens = new HashMap<String, Screen[]>();
|
||||
|
||||
// ProcessBuilder pb = new ProcessBuilder( "xrandr", "-q" );
|
||||
// pb.redirectErrorStream();
|
||||
try
|
||||
{
|
||||
// Process p= pb.start();
|
||||
try {
|
||||
Process p = Runtime.getRuntime().exec(new String[] { "xrandr", "-q" });
|
||||
|
||||
List<Screen> currentList = new ArrayList<Screen>();
|
||||
|
@ -85,16 +80,13 @@ public class XRandR
|
|||
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
String line;
|
||||
while( ( line = br.readLine() ) != null )
|
||||
{
|
||||
while ( (line = br.readLine()) != null ) {
|
||||
line = line.trim();
|
||||
String[] sa = line.split( "\\s+" );
|
||||
String[] sa = WHITESPACE_PATTERN.split(line);
|
||||
|
||||
if( "connected".equals(sa[1]) )
|
||||
{
|
||||
if ( "connected".equals(sa[1]) ) {
|
||||
// found a new screen block
|
||||
if( name != null )
|
||||
{
|
||||
if ( name != null ) {
|
||||
screens.put(name, possibles.toArray(new Screen[possibles.size()]));
|
||||
possibles.clear();
|
||||
}
|
||||
|
@ -108,11 +100,16 @@ public class XRandR
|
|||
} else {
|
||||
parseScreenHeader(currentScreenPosition, sa[2]);
|
||||
}
|
||||
}
|
||||
else if( Pattern.matches( "\\d*x\\d*", sa[ 0 ] ) )
|
||||
{
|
||||
} else {
|
||||
Matcher m = SCREEN_MODELINE_PATTERN.matcher(sa[0]);
|
||||
if ( m.matches() ) {
|
||||
// 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
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -130,22 +127,18 @@ public class XRandR
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch( Throwable e )
|
||||
{
|
||||
} 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,
|
||||
* or an empty array if xrandr is not supported
|
||||
*/
|
||||
public static Screen[] getConfiguration()
|
||||
{
|
||||
public static Screen[] getConfiguration() {
|
||||
populate();
|
||||
|
||||
// find and return primary
|
||||
|
@ -160,15 +153,12 @@ public class XRandR
|
|||
}
|
||||
|
||||
/**
|
||||
* @param disableOthers
|
||||
* 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>
|
||||
* @throws IllegalArgumentException
|
||||
* if no screens are specified
|
||||
* @param disableOthers 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>
|
||||
*
|
||||
* @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 )
|
||||
throw new IllegalArgumentException("Must specify at least one screen");
|
||||
|
||||
|
@ -178,15 +168,15 @@ public class XRandR
|
|||
if ( disableOthers ) {
|
||||
// switch off those in the current set not in the new set
|
||||
for ( Screen screen : current ) {
|
||||
boolean found = false;
|
||||
boolean disable = true;
|
||||
for ( Screen screen1 : screens ) {
|
||||
if ( screen1.name.equals(screen.name) ) {
|
||||
found = true;
|
||||
disable = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ( !found ) {
|
||||
if ( disable ) {
|
||||
cmd.add("--output");
|
||||
cmd.add(screen.name);
|
||||
cmd.add("--off");
|
||||
|
@ -198,24 +188,16 @@ public class XRandR
|
|||
for ( Screen screen : screens )
|
||||
screen.getArgs(cmd);
|
||||
|
||||
try
|
||||
{
|
||||
// ProcessBuilder pb = new ProcessBuilder( cmd );
|
||||
// pb.redirectErrorStream();
|
||||
// Process p = pb.start();
|
||||
Process p =
|
||||
Runtime.getRuntime().exec( cmd.toArray( new String[ cmd.size() ] ) );
|
||||
try {
|
||||
Process p = Runtime.getRuntime().exec(cmd.toArray(new String[cmd.size()]));
|
||||
// no output is expected, but check anyway
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
|
||||
String line;
|
||||
while( ( line = br.readLine() ) != null )
|
||||
{
|
||||
while ( (line = br.readLine()) != null ) {
|
||||
LWJGLUtil.log("Unexpected output from xrandr process: " + line);
|
||||
}
|
||||
current = screens;
|
||||
}
|
||||
catch( IOException e )
|
||||
{
|
||||
} catch (IOException e) {
|
||||
LWJGLUtil.log("XRandR exception in setConfiguration(): " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
@ -244,79 +226,63 @@ public class XRandR
|
|||
* @return the name of connected screens, or an empty array if
|
||||
* xrandr is not supported
|
||||
*/
|
||||
public static String[] getScreenNames()
|
||||
{
|
||||
public static String[] getScreenNames() {
|
||||
populate();
|
||||
return screens.keySet().toArray(new String[screens.size()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param name
|
||||
*
|
||||
* @return the possible resolutions of the named screen, or
|
||||
* <code>null</code> if there is no such screen
|
||||
*/
|
||||
public static Screen[] getResolutions( String name )
|
||||
{
|
||||
public static Screen[] getResolutions(String name) {
|
||||
populate();
|
||||
// clone the array to prevent held copies being altered
|
||||
return screens.get(name).clone();
|
||||
}
|
||||
|
||||
private static final Pattern SCREEN_HEADER_PATTERN =
|
||||
Pattern.compile( "^(\\d+)x(\\d+)\\+(\\d+)\\+(\\d+)$" );
|
||||
|
||||
private static final Pattern SCREEN_HEADER_PATTERN = 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]?\\+?$");
|
||||
private static final Pattern FREQ_PATTERN = Pattern.compile("^(\\d+)[.](\\d+)(?:\\s*[*])?(?:\\s*[+])?$");
|
||||
|
||||
/**
|
||||
* Parses a screen configuration and adds it to one of the lists if valid.
|
||||
*
|
||||
* @param allModes
|
||||
* the list to add the Screen to if it's valid
|
||||
* @param current
|
||||
* the list to add the current screen config to
|
||||
* @param name
|
||||
* 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
|
||||
* @param allModes the list to add the Screen to if it's valid
|
||||
* @param current the list to add the current screen config to
|
||||
* @param name the name of this screen
|
||||
* @param modeLine config string
|
||||
* @param screenPosition position of this screen
|
||||
*/
|
||||
private static void parseScreenModeline( List<Screen> allModes, List<Screen> current, String name, String res, String[] freqs, int[] screenPosition)
|
||||
{
|
||||
Matcher m = SCREEN_MODELINE_PATTERN.matcher( res );
|
||||
if( !m.matches() )
|
||||
{
|
||||
LWJGLUtil.log( "Did not match: " + res );
|
||||
return;
|
||||
}
|
||||
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) {
|
||||
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++ ) {
|
||||
String freqS = modeLine[i];
|
||||
if ( "+".equals(freqS) ) {
|
||||
// previous rate was the "preferred" refresh rate
|
||||
// no way to get this info to the application, so ignore it
|
||||
continue;
|
||||
}
|
||||
m = FREQ_PATTERN.matcher(freqS);
|
||||
if( !m.matches() )
|
||||
{
|
||||
LWJGLUtil.log( "Did not match: " + res );
|
||||
|
||||
Matcher m = FREQ_PATTERN.matcher(freqS);
|
||||
if ( !m.matches() ) {
|
||||
LWJGLUtil.log("Frequency match failed: " + Arrays.toString(modeLine));
|
||||
return;
|
||||
}
|
||||
|
||||
int freq = Integer.parseInt(m.group(1));
|
||||
|
||||
Screen s = new Screen(name, width, height, freq, 0, 0);
|
||||
if ( freqS.contains("*") ) {
|
||||
// current mode, save to current list with screen position
|
||||
current.add( new Screen( name, width, height, freq, xpos, ypos ) );
|
||||
}
|
||||
current.add(new Screen(name, width, height, freq, screenPosition[0], screenPosition[1]));
|
||||
// make sure the current mode is always first
|
||||
allModes.add(0, s);
|
||||
} else {
|
||||
// always add to List of all modes without screen position
|
||||
allModes.add( new Screen( name, width, height, freq, 0, 0 ) );
|
||||
allModes.add(s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -366,40 +332,26 @@ public class XRandR
|
|||
*
|
||||
* @author ryanm
|
||||
*/
|
||||
public static class Screen implements Cloneable
|
||||
{
|
||||
/**
|
||||
* Name for this output
|
||||
*/
|
||||
public static class Screen implements Cloneable {
|
||||
/** Name for this output */
|
||||
public final String name;
|
||||
|
||||
/**
|
||||
* Width in pixels
|
||||
*/
|
||||
/** Width in pixels */
|
||||
public final int width;
|
||||
|
||||
/**
|
||||
* Height in pixels
|
||||
*/
|
||||
/** Height in pixels */
|
||||
public final int height;
|
||||
|
||||
/**
|
||||
* Frequency in Hz
|
||||
*/
|
||||
/** Frequency in Hz */
|
||||
public final int freq;
|
||||
|
||||
/**
|
||||
* Position on the x-axis, in pixels
|
||||
*/
|
||||
/** Position on the x-axis, in pixels */
|
||||
public int xPos;
|
||||
|
||||
/**
|
||||
* Position on the y-axis, in pixels
|
||||
*/
|
||||
/** Position on the y-axis, in pixels */
|
||||
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.width = width;
|
||||
this.height = height;
|
||||
|
@ -408,21 +360,19 @@ public class XRandR
|
|||
this.yPos = yPos;
|
||||
}
|
||||
|
||||
private void getArgs( List<String> argList )
|
||||
{
|
||||
private void getArgs(List<String> argList) {
|
||||
argList.add("--output");
|
||||
argList.add(name);
|
||||
argList.add("--mode");
|
||||
argList.add(width + "x" + height);
|
||||
argList.add("--rate");
|
||||
argList.add( freq + "");//"" autoboxes freq as String
|
||||
argList.add(Integer.toString(freq));
|
||||
argList.add("--pos");
|
||||
argList.add(xPos + "x" + yPos);
|
||||
}
|
||||
|
||||
//@Override
|
||||
public String toString()
|
||||
{
|
||||
public String toString() {
|
||||
return name + " " + width + "x" + height + " @ " + xPos + "x" + yPos + " with " + freq + "Hz";
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue