View Javadoc
1   /*
2    * Copyright 2020-2023 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.mac;
6   
7   import java.util.ArrayList;
8   import java.util.List;
9   import java.util.Locale;
10  
11  import oshi.annotation.concurrent.Immutable;
12  import oshi.hardware.GraphicsCard;
13  import oshi.hardware.common.AbstractGraphicsCard;
14  import oshi.util.Constants;
15  import oshi.util.ExecutingCommand;
16  import oshi.util.ParseUtil;
17  
18  /**
19   * Graphics card info obtained by system_profiler SPDisplaysDataType.
20   */
21  @Immutable
22  final class MacGraphicsCard extends AbstractGraphicsCard {
23  
24      /**
25       * Constructor for MacGraphicsCard
26       *
27       * @param name        The name
28       * @param deviceId    The device ID
29       * @param vendor      The vendor
30       * @param versionInfo The version info
31       * @param vram        The VRAM
32       */
33      MacGraphicsCard(String name, String deviceId, String vendor, String versionInfo, long vram) {
34          super(name, deviceId, vendor, versionInfo, vram);
35      }
36  
37      /**
38       * public method used by {@link oshi.hardware.common.AbstractHardwareAbstractionLayer} to access the graphics cards.
39       *
40       * @return List of {@link oshi.hardware.platform.mac.MacGraphicsCard} objects.
41       */
42      public static List<GraphicsCard> getGraphicsCards() {
43          List<GraphicsCard> cardList = new ArrayList<>();
44          List<String> sp = ExecutingCommand.runNative("system_profiler SPDisplaysDataType");
45          String name = Constants.UNKNOWN;
46          String deviceId = Constants.UNKNOWN;
47          String vendor = Constants.UNKNOWN;
48          List<String> versionInfoList = new ArrayList<>();
49          long vram = 0;
50          int cardNum = 0;
51          for (String line : sp) {
52              String[] split = line.trim().split(":", 2);
53              if (split.length == 2) {
54                  String prefix = split[0].toLowerCase(Locale.ROOT);
55                  if (prefix.equals("chipset model")) {
56                      // Save previous card
57                      if (cardNum++ > 0) {
58                          cardList.add(new MacGraphicsCard(name, deviceId, vendor,
59                                  versionInfoList.isEmpty() ? Constants.UNKNOWN : String.join(", ", versionInfoList),
60                                  vram));
61                          versionInfoList.clear();
62                      }
63                      name = split[1].trim();
64                  } else if (prefix.equals("device id")) {
65                      deviceId = split[1].trim();
66                  } else if (prefix.equals("vendor")) {
67                      vendor = split[1].trim();
68                  } else if (prefix.contains("version") || prefix.contains("revision")) {
69                      versionInfoList.add(line.trim());
70                  } else if (prefix.startsWith("vram")) {
71                      vram = ParseUtil.parseDecimalMemorySizeToBinary(split[1].trim());
72                  }
73              }
74          }
75          cardList.add(new MacGraphicsCard(name, deviceId, vendor,
76                  versionInfoList.isEmpty() ? Constants.UNKNOWN : String.join(", ", versionInfoList), vram));
77          return cardList;
78      }
79  }