1
2
3
4
5 package oshi.hardware.platform.linux;
6
7 import java.io.File;
8 import java.util.ArrayList;
9 import java.util.List;
10 import java.util.Map;
11
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
14
15 import oshi.annotation.concurrent.Immutable;
16 import oshi.hardware.SoundCard;
17 import oshi.hardware.common.AbstractSoundCard;
18 import oshi.util.FileUtil;
19 import oshi.util.platform.linux.ProcPath;
20
21
22
23
24 @Immutable
25 final class LinuxSoundCard extends AbstractSoundCard {
26
27 private static final Logger LOG = LoggerFactory.getLogger(LinuxSoundCard.class);
28
29 private static final String CARD_FOLDER = "card";
30 private static final String CARDS_FILE = "cards";
31 private static final String ID_FILE = "id";
32
33
34
35
36
37
38
39
40 LinuxSoundCard(String kernelVersion, String name, String codec) {
41 super(kernelVersion, name, codec);
42 }
43
44
45
46
47
48
49
50 private static List<File> getCardFolders() {
51 File cardsDirectory = new File(ProcPath.ASOUND);
52 List<File> cardFolders = new ArrayList<>();
53 File[] allContents = cardsDirectory.listFiles();
54 if (allContents != null) {
55 for (File card : allContents) {
56 if (card.getName().startsWith(CARD_FOLDER) && card.isDirectory()) {
57 cardFolders.add(card);
58 }
59 }
60 } else {
61 LOG.warn("No Audio Cards Found");
62 }
63 return cardFolders;
64 }
65
66
67
68
69
70
71
72 private static String getSoundCardVersion() {
73 String driverVersion = FileUtil.getStringFromFile(ProcPath.ASOUND + "version");
74 return driverVersion.isEmpty() ? "not available" : driverVersion;
75 }
76
77
78
79
80
81
82
83
84
85
86
87 private static String getCardCodec(File cardDir) {
88 String cardCodec = "";
89 File[] cardFiles = cardDir.listFiles();
90 if (cardFiles != null) {
91 for (File file : cardFiles) {
92 if (file.getName().startsWith("codec")) {
93 if (!file.isDirectory()) {
94 cardCodec = FileUtil.getKeyValueMapFromFile(file.getPath(), ":").get("Codec");
95 } else {
96
97
98
99
100 File[] codecs = file.listFiles();
101 if (codecs != null) {
102 for (File codec : codecs) {
103 if (!codec.isDirectory() && codec.getName().contains("#")) {
104 cardCodec = codec.getName().substring(0, codec.getName().indexOf('#'));
105 break;
106 }
107 }
108 }
109 }
110 }
111 }
112 }
113 return cardCodec;
114 }
115
116
117
118
119
120
121
122
123
124
125
126 private static String getCardName(File file) {
127 String cardName = "Not Found..";
128 Map<String, String> cardNamePairs = FileUtil.getKeyValueMapFromFile(ProcPath.ASOUND + "/" + CARDS_FILE, ":");
129 String cardId = FileUtil.getStringFromFile(file.getPath() + "/" + ID_FILE);
130 for (Map.Entry<String, String> entry : cardNamePairs.entrySet()) {
131 if (entry.getKey().contains(cardId)) {
132 cardName = entry.getValue();
133 return cardName;
134 }
135 }
136 return cardName;
137 }
138
139
140
141
142
143
144 public static List<SoundCard> getSoundCards() {
145 List<SoundCard> soundCards = new ArrayList<>();
146 for (File cardFile : getCardFolders()) {
147 soundCards.add(new LinuxSoundCard(getSoundCardVersion(), getCardName(cardFile), getCardCodec(cardFile)));
148 }
149 return soundCards;
150 }
151 }