View Javadoc
1   /*
2    * Copyright 2018-2022 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   
10  import oshi.annotation.concurrent.Immutable;
11  import oshi.hardware.SoundCard;
12  import oshi.hardware.common.AbstractSoundCard;
13  import oshi.util.FileUtil;
14  import oshi.util.ParseUtil;
15  
16  /**
17   * Sound card data obtained via AppleHDA kext
18   */
19  @Immutable
20  final class MacSoundCard extends AbstractSoundCard {
21  
22      private static final String APPLE = "Apple Inc.";
23  
24      /**
25       * Constructor for MacSoundCard.
26       *
27       * @param kernelVersion The version
28       * @param name          The name
29       * @param codec         The codec
30       */
31      MacSoundCard(String kernelVersion, String name, String codec) {
32          super(kernelVersion, name, codec);
33      }
34  
35      /**
36       * public method used by {@link oshi.hardware.common.AbstractHardwareAbstractionLayer} to access the sound cards.
37       *
38       * @return List of {@link oshi.hardware.platform.mac.MacSoundCard} objects.
39       */
40      public static List<SoundCard> getSoundCards() {
41          List<SoundCard> soundCards = new ArrayList<>();
42  
43          // /System/Library/Extensions/AppleHDA.kext/Contents/Info.plist
44  
45          // ..... snip ....
46          // <dict>
47          // <key>com.apple.driver.AppleHDAController</key>
48          // <string>1.7.2a1</string>
49  
50          String manufacturer = APPLE;
51          String kernelVersion = "AppleHDAController";
52          String codec = "AppleHDACodec";
53  
54          boolean version = false;
55          String versionMarker = "<key>com.apple.driver.AppleHDAController</key>";
56  
57          for (final String checkLine : FileUtil
58                  .readFile("/System/Library/Extensions/AppleHDA.kext/Contents/Info.plist")) {
59              if (checkLine.contains(versionMarker)) {
60                  version = true;
61                  continue;
62              }
63              if (version) {
64                  kernelVersion = "AppleHDAController "
65                          + ParseUtil.getTextBetweenStrings(checkLine, "<string>", "</string>");
66                  version = false;
67              }
68          }
69          soundCards.add(new MacSoundCard(kernelVersion, manufacturer, codec));
70  
71          return soundCards;
72      }
73  }