View Javadoc
1   /*
2    * Copyright 2020-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.driver.unix;
6   
7   import java.util.ArrayList;
8   import java.util.Collections;
9   import java.util.List;
10  
11  import oshi.annotation.concurrent.ThreadSafe;
12  import oshi.util.ExecutingCommand;
13  import oshi.util.ParseUtil;
14  
15  /**
16   * Utility to query xrandr
17   */
18  @ThreadSafe
19  public final class Xrandr {
20  
21      private static final String[] XRANDR_VERBOSE = { "xrandr", "--verbose" };
22  
23      private Xrandr() {
24      }
25  
26      public static List<byte[]> getEdidArrays() {
27          // Special handling for X commands, don't use LC_ALL
28          List<String> xrandr = ExecutingCommand.runNative(XRANDR_VERBOSE, null);
29          // xrandr reports edid in multiple lines. After seeing a line containing
30          // EDID, read subsequent lines of hex until 256 characters are reached
31          if (xrandr.isEmpty()) {
32              return Collections.emptyList();
33          }
34          List<byte[]> displays = new ArrayList<>();
35          StringBuilder sb = null;
36          for (String s : xrandr) {
37              if (s.contains("EDID")) {
38                  sb = new StringBuilder();
39              } else if (sb != null) {
40                  sb.append(s.trim());
41                  if (sb.length() < 256) {
42                      continue;
43                  }
44                  String edidStr = sb.toString();
45                  byte[] edid = ParseUtil.hexStringToByteArray(edidStr);
46                  if (edid.length >= 128) {
47                      displays.add(edid);
48                  }
49                  sb = null;
50              }
51          }
52          return displays;
53      }
54  }