mirror of
https://github.com/moonlight-stream/moonlight-embedded.git
synced 2026-06-15 21:31:12 +00:00
Fix resolution changing on Windows. Improve the resolution selection algorithm.
This commit is contained in:
@@ -11,7 +11,9 @@ import java.awt.GraphicsEnvironment;
|
|||||||
import java.awt.Point;
|
import java.awt.Point;
|
||||||
import java.awt.Toolkit;
|
import java.awt.Toolkit;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
|
||||||
import javax.swing.Box;
|
import javax.swing.Box;
|
||||||
@@ -31,6 +33,9 @@ import com.limelight.nvstream.StreamConfiguration;
|
|||||||
public class StreamFrame extends JFrame {
|
public class StreamFrame extends JFrame {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
private static final double DESIRED_ASPECT_RATIO = 16.0/9.0;
|
||||||
|
private static final double ALTERNATE_ASPECT_RATIO = 16.0/10.0;
|
||||||
|
|
||||||
private KeyboardHandler keyboard;
|
private KeyboardHandler keyboard;
|
||||||
private MouseHandler mouse;
|
private MouseHandler mouse;
|
||||||
private JProgressBar spinner;
|
private JProgressBar spinner;
|
||||||
@@ -67,46 +72,77 @@ public class StreamFrame extends JFrame {
|
|||||||
this.getRootPane().setBackground(Color.BLACK);
|
this.getRootPane().setBackground(Color.BLACK);
|
||||||
|
|
||||||
if (fullscreen) {
|
if (fullscreen) {
|
||||||
makeFullScreen();
|
makeFullScreen(streamConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
hideCursor();
|
hideCursor();
|
||||||
this.setVisible(true);
|
this.setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
private DisplayMode getBestDisplay(DisplayMode[] configs) {
|
private ArrayList<DisplayMode> getDisplayModesByAspectRatio(DisplayMode[] configs, double aspectRatio) {
|
||||||
Arrays.sort(configs, new Comparator<DisplayMode>() {
|
ArrayList<DisplayMode> matchingConfigs = new ArrayList<DisplayMode>();
|
||||||
|
|
||||||
|
for (DisplayMode config : configs) {
|
||||||
|
if ((double)config.getWidth()/(double)config.getHeight() == aspectRatio) {
|
||||||
|
matchingConfigs.add(config);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return matchingConfigs;
|
||||||
|
}
|
||||||
|
|
||||||
|
private DisplayMode getBestDisplay(StreamConfiguration targetConfig, DisplayMode[] configs) {
|
||||||
|
int targetDisplaySize = targetConfig.getWidth()*targetConfig.getHeight();
|
||||||
|
|
||||||
|
// Try to match the target aspect ratio
|
||||||
|
ArrayList<DisplayMode> aspectMatchingConfigs = getDisplayModesByAspectRatio(configs, DESIRED_ASPECT_RATIO);
|
||||||
|
if (aspectMatchingConfigs.size() == 0) {
|
||||||
|
// No matches for the target, so try the alternate
|
||||||
|
aspectMatchingConfigs = getDisplayModesByAspectRatio(configs, ALTERNATE_ASPECT_RATIO);
|
||||||
|
if (aspectMatchingConfigs.size() == 0) {
|
||||||
|
// No matches for either, so just use all of them
|
||||||
|
aspectMatchingConfigs = new ArrayList<DisplayMode>(Arrays.asList(configs));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort by display size
|
||||||
|
Collections.sort(aspectMatchingConfigs, new Comparator<DisplayMode>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(DisplayMode o1, DisplayMode o2) {
|
public int compare(DisplayMode o1, DisplayMode o2) {
|
||||||
if (o1.getWidth() > o2.getWidth()) {
|
if (o1.getWidth()*o1.getHeight() > o2.getWidth()*o2.getHeight()) {
|
||||||
return -1;
|
return -1;
|
||||||
} else if (o2.getWidth() > o1.getWidth()) {
|
} else if (o2.getWidth()*o2.getHeight() > o1.getWidth()*o1.getHeight()) {
|
||||||
return 1;
|
return 1;
|
||||||
} else {
|
} else {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Find the aspect-matching config with the closest matching display size
|
||||||
DisplayMode bestConfig = null;
|
DisplayMode bestConfig = null;
|
||||||
for (DisplayMode config : configs) {
|
for (DisplayMode config : aspectMatchingConfigs) {
|
||||||
if (config.getWidth() >= getSize().width && config.getHeight() >= getSize().height) {
|
if (config.getWidth()*config.getHeight() >= targetDisplaySize) {
|
||||||
bestConfig = config;
|
bestConfig = config;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (bestConfig == null) {
|
|
||||||
return configs[0];
|
if (bestConfig != null) {
|
||||||
|
System.out.println("Using full-screen display mode "+bestConfig.getWidth()+"x"+bestConfig.getHeight()+
|
||||||
|
" for "+targetConfig.getWidth()+"x"+targetConfig.getHeight()+" stream");
|
||||||
}
|
}
|
||||||
|
|
||||||
return bestConfig;
|
return bestConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void makeFullScreen() {
|
private void makeFullScreen(StreamConfiguration streamConfig) {
|
||||||
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
|
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
|
||||||
if (gd.isFullScreenSupported()) {
|
if (gd.isFullScreenSupported()) {
|
||||||
this.setUndecorated(true);
|
this.setUndecorated(true);
|
||||||
gd.setFullScreenWindow(this);
|
gd.setFullScreenWindow(this);
|
||||||
|
|
||||||
if (gd.isDisplayChangeSupported()) {
|
if (gd.isDisplayChangeSupported()) {
|
||||||
DisplayMode config = getBestDisplay(gd.getDisplayModes());
|
DisplayMode config = getBestDisplay(streamConfig, gd.getDisplayModes());
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
gd.setDisplayMode(config);
|
gd.setDisplayMode(config);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user