View Javadoc
1   /*
2    * Copyright 2018-2022 The OSHI Project Contributors
3    * SPDX-License-Identifier: MIT
4    */
5   package oshi.hardware.platform.unix.freebsd;
6   
7   import java.util.ArrayList;
8   import java.util.HashMap;
9   import java.util.List;
10  import java.util.Map;
11  
12  import oshi.annotation.concurrent.Immutable;
13  import oshi.hardware.SoundCard;
14  import oshi.hardware.common.AbstractSoundCard;
15  import oshi.util.ExecutingCommand;
16  import oshi.util.ParseUtil;
17  
18  /**
19   * FreeBSD soundcard.
20   */
21  @Immutable
22  final class FreeBsdSoundCard extends AbstractSoundCard {
23  
24      private static final String LSHAL = "lshal";
25  
26      /**
27       * Constructor for FreeBsdSoundCard.
28       *
29       * @param kernelVersion The version
30       * @param name          The name
31       * @param codec         The codec
32       */
33      FreeBsdSoundCard(String kernelVersion, String name, String codec) {
34          super(kernelVersion, name, codec);
35      }
36  
37      /**
38       * <p>
39       * getSoundCards.
40       * </p>
41       *
42       * @return a {@link java.util.List} object.
43       */
44      public static List<SoundCard> getSoundCards() {
45          Map<String, String> vendorMap = new HashMap<>();
46          Map<String, String> productMap = new HashMap<>();
47          vendorMap.clear();
48          productMap.clear();
49          List<String> sounds = new ArrayList<>();
50          String key = "";
51          for (String line : ExecutingCommand.runNative(LSHAL)) {
52              line = line.trim();
53              if (line.startsWith("udi =")) {
54                  // we have the key.
55                  key = ParseUtil.getSingleQuoteStringValue(line);
56              } else if (!key.isEmpty() && !line.isEmpty()) {
57                  if (line.contains("freebsd.driver =") && "pcm".equals(ParseUtil.getSingleQuoteStringValue(line))) {
58                      sounds.add(key);
59                  } else if (line.contains("info.product")) {
60                      productMap.put(key, ParseUtil.getStringBetween(line, '\''));
61                  } else if (line.contains("info.vendor")) {
62                      vendorMap.put(key, ParseUtil.getStringBetween(line, '\''));
63                  }
64              }
65          }
66          List<SoundCard> soundCards = new ArrayList<>();
67          for (String s : sounds) {
68              soundCards.add(new FreeBsdSoundCard(productMap.get(s), vendorMap.get(s) + " " + productMap.get(s),
69                      productMap.get(s)));
70          }
71          return soundCards;
72      }
73  }