Skip to content

Commit

Permalink
added fallback getting displayInfo when displayInfo is null
Browse files Browse the repository at this point in the history
  • Loading branch information
issess committed Sep 10, 2022
1 parent 0a0a446 commit 7e74ed1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
10 changes: 10 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ public static String execReadLine(String... cmd) throws IOException, Interrupted
}
return result;
}

public static String execReadOutput(String... cmd) throws IOException, InterruptedException {
Process process = Runtime.getRuntime().exec(cmd);
String output = IO.toString(process.getInputStream());
int exitCode = process.waitFor();
if (exitCode != 0) {
throw new IOException("Command " + Arrays.toString(cmd) + " returned with value " + exitCode);
}
return output;
}
}
11 changes: 11 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

import java.io.FileDescriptor;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.Scanner;

public final class IO {
private IO() {
Expand Down Expand Up @@ -37,4 +39,13 @@ public static void writeFully(FileDescriptor fd, ByteBuffer from) throws IOExcep
public static void writeFully(FileDescriptor fd, byte[] buffer, int offset, int len) throws IOException {
writeFully(fd, ByteBuffer.wrap(buffer, offset, len));
}

public static String toString(InputStream inputStream) {
StringBuilder builder = new StringBuilder();
Scanner scanner = new Scanner(inputStream);
while (scanner.hasNextLine()) {
builder.append(scanner.nextLine()).append('\n');
}
return builder.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,70 @@
package com.genymobile.scrcpy.wrappers;

import android.text.TextUtils;
import android.view.Display;

import com.genymobile.scrcpy.Command;
import com.genymobile.scrcpy.DisplayInfo;
import com.genymobile.scrcpy.Ln;
import com.genymobile.scrcpy.Size;

import java.lang.reflect.Field;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class DisplayManager {
private final Object manager; // instance of hidden class android.hardware.display.DisplayManagerGlobal

public DisplayManager(Object manager) {
this.manager = manager;
}

private DisplayInfo parseDumpsysDisplay(int displayId) {
try {
String dumpDisplay = Command.execReadOutput("dumpsys", "display");
Pattern regex = Pattern.compile(
"^ mBaseDisplayInfo=DisplayInfo\\{\".*\", displayId " + displayId +
".+?(, FLAG_.*), real ([0-9]+) x ([0-9]+).+, rotation ([0-9]+).*, " +
"layerStack ([0-9]+).*", Pattern.MULTILINE);
Matcher m = regex.matcher(dumpDisplay);
if (!m.find()) {
return null;
}
int flags = stringToFlags(m.group(1));
int width = Integer.parseInt(m.group(2));
int height = Integer.parseInt(m.group(3));
int rotation = Integer.parseInt(m.group(4));
int layerStack = Integer.parseInt(m.group(5));

return new DisplayInfo(displayId, new Size(width, height), rotation, layerStack, flags);
} catch (Exception e) {
Ln.e("Could not get display info from \"dumpsys display\" output", e);
return null;
}
}

private int stringToFlags(String text) {
int flags = 0;
String[] flagStrings = text.split(", ");
for (String flagString : flagStrings) {
if (!TextUtils.isEmpty(flagString)) {
try {
Field filed = Display.class.getDeclaredField(flagString);
flags |= filed.getInt(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
Ln.e("Could not get display flag " + flagString, e);
}
}
}
return flags;
}

public DisplayInfo getDisplayInfo(int displayId) {
try {
Object displayInfo = manager.getClass().getMethod("getDisplayInfo", int.class).invoke(manager, displayId);
if (displayInfo == null) {
return null;
// fallback when displayInfo is null
return parseDumpsysDisplay(displayId);
}
Class<?> cls = displayInfo.getClass();
// width and height already take the rotation into account
Expand Down

0 comments on commit 7e74ed1

Please sign in to comment.