|
1 | 1 | /*
|
2 | 2 | * alsawork.cpp
|
3 |
| - * Copyright (C) 2012-2015 Vitaly Tonkacheyev |
| 3 | + * Copyright (C) 2012-2019 Vitaly Tonkacheyev |
4 | 4 | *
|
5 | 5 | * This program is free software: you can redistribute it and/or modify
|
6 | 6 | * it under the terms of the GNU General Public License as published by
|
|
22 | 22 | #include <stdexcept>
|
23 | 23 |
|
24 | 24 | AlsaWork::AlsaWork()
|
25 |
| -: cardList_(std::vector<std::string>()) |
| 25 | + : cardList_(std::vector<std::string>()) |
26 | 26 | {
|
27 |
| - getCards(); |
28 |
| - int id = 0; |
29 |
| - std::for_each(cardList_.begin(), cardList_.end(), [&](const std::string &name){ |
30 |
| - devices_.push_back(AlsaDevice::Ptr(new AlsaDevice(id, name))); |
31 |
| - ++id; |
32 |
| - }); |
33 |
| - setCurrentCard(0); |
34 |
| - setCurrentMixer(0); |
| 27 | + getCards(); |
| 28 | + int id = 0; |
| 29 | + for(const std::string &name : cardList_){ |
| 30 | + devices_.push_back(AlsaDevice::Ptr(new AlsaDevice(id, name))); |
| 31 | + ++id; |
| 32 | + } |
| 33 | + setCurrentCard(0); |
| 34 | + setCurrentMixer(0); |
35 | 35 | }
|
36 | 36 |
|
37 | 37 | AlsaWork::~AlsaWork()
|
38 | 38 | {
|
39 |
| - if (!devices_.empty()) |
40 |
| - devices_.clear(); |
41 |
| - currentAlsaDevice_.reset(); |
42 |
| - snd_config_update_free_global(); |
| 39 | + if (!devices_.empty()) |
| 40 | + devices_.clear(); |
| 41 | + currentAlsaDevice_.reset(); |
| 42 | + snd_config_update_free_global(); |
43 | 43 | }
|
44 | 44 |
|
45 | 45 | //public
|
46 | 46 | void AlsaWork::setCurrentCard(int cardId)
|
47 | 47 | {
|
48 |
| - if(cardId < int(devices_.size())) { |
49 |
| - currentAlsaDevice_ = devices_.at(cardId); |
50 |
| - } |
| 48 | + if(cardId < int(devices_.size())) { |
| 49 | + currentAlsaDevice_ = devices_.at(cardId); |
| 50 | + } |
51 | 51 | }
|
52 | 52 |
|
53 | 53 | void AlsaWork::setCurrentMixer(const std::string &mixer)
|
54 | 54 | {
|
55 |
| - currentAlsaDevice_->setCurrentMixer(mixer); |
| 55 | + currentAlsaDevice_->setCurrentMixer(mixer); |
56 | 56 | }
|
57 | 57 |
|
58 | 58 | void AlsaWork::setCurrentMixer(int id)
|
59 | 59 | {
|
60 |
| - currentAlsaDevice_->setCurrentMixer(id); |
| 60 | + currentAlsaDevice_->setCurrentMixer(id); |
61 | 61 | }
|
62 | 62 |
|
63 | 63 | void AlsaWork::setAlsaVolume(double volume)
|
64 | 64 | {
|
65 |
| - currentAlsaDevice_->setDeviceVolume(volume); |
| 65 | + currentAlsaDevice_->setDeviceVolume(volume); |
66 | 66 | }
|
67 | 67 |
|
68 | 68 | double AlsaWork::getAlsaVolume() const
|
69 | 69 | {
|
70 |
| - return currentAlsaDevice_->getVolume(); |
| 70 | + return currentAlsaDevice_->getVolume(); |
71 | 71 | }
|
72 | 72 |
|
73 | 73 | const std::string AlsaWork::getCardName(int index)
|
74 | 74 | {
|
75 |
| - const std::string card(AlsaDevice::formatCardName(index)); |
76 |
| - snd_ctl_t *ctl; |
77 |
| - int err = snd_ctl_open(&ctl, card.c_str(), SND_CTL_NONBLOCK); |
78 |
| - if (err < 0) { |
79 |
| - checkError(err); |
80 |
| - return std::string(); |
81 |
| - } |
82 |
| - snd_ctl_card_info_t *cardInfo; |
83 |
| - snd_ctl_card_info_alloca(&cardInfo); |
84 |
| - err = snd_ctl_card_info(ctl, cardInfo); |
85 |
| - if (err < 0) { |
86 |
| - checkError(err); |
87 |
| - return std::string(); |
88 |
| - } |
89 |
| - const std::string cardName = snd_ctl_card_info_get_name(cardInfo); |
90 |
| - checkError(snd_ctl_close(ctl)); |
91 |
| - return cardName; |
| 75 | + const std::string card(AlsaDevice::formatCardName(index)); |
| 76 | + snd_ctl_t *ctl; |
| 77 | + int err = snd_ctl_open(&ctl, card.c_str(), SND_CTL_NONBLOCK); |
| 78 | + if (err < 0) { |
| 79 | + checkError(err); |
| 80 | + return std::string(); |
| 81 | + } |
| 82 | + snd_ctl_card_info_t *cardInfo; |
| 83 | + snd_ctl_card_info_alloca(&cardInfo); |
| 84 | + err = snd_ctl_card_info(ctl, cardInfo); |
| 85 | + if (err < 0) { |
| 86 | + checkError(err); |
| 87 | + return std::string(); |
| 88 | + } |
| 89 | + const std::string cardName = snd_ctl_card_info_get_name(cardInfo); |
| 90 | + checkError(snd_ctl_close(ctl)); |
| 91 | + return cardName; |
92 | 92 | }
|
93 | 93 |
|
94 | 94 | const std::string AlsaWork::getMixerName(int index)
|
95 | 95 | {
|
96 |
| - std::string mixerName; |
97 |
| - if (index >= 0 && index < currentAlsaDevice_->mixers().size()) { |
98 |
| - mixerName = currentAlsaDevice_->mixers().at(index); |
99 |
| - } |
100 |
| - return mixerName; |
| 96 | + std::string mixerName; |
| 97 | + if (index >= 0 && index < currentAlsaDevice_->mixers().size()) { |
| 98 | + mixerName = currentAlsaDevice_->mixers().at(index); |
| 99 | + } |
| 100 | + return mixerName; |
101 | 101 | }
|
102 | 102 |
|
103 | 103 | std::string AlsaWork::getCurrentMixerName() const
|
104 | 104 | {
|
105 |
| - return currentAlsaDevice_->currentMixer(); |
| 105 | + return currentAlsaDevice_->currentMixer(); |
106 | 106 | }
|
107 | 107 |
|
108 | 108 | const std::vector<std::string> &AlsaWork::getCardsList() const
|
109 | 109 | {
|
110 |
| - return cardList_; |
| 110 | + return cardList_; |
111 | 111 | }
|
112 | 112 |
|
113 | 113 | const std::vector<std::string> &AlsaWork::getVolumeMixers() const
|
114 | 114 | {
|
115 |
| - return currentAlsaDevice_->mixers(); |
| 115 | + return currentAlsaDevice_->mixers(); |
116 | 116 | }
|
117 | 117 |
|
118 | 118 | MixerSwitches::Ptr AlsaWork::getSwitchList() const
|
119 | 119 | {
|
120 |
| - return currentAlsaDevice_->switches(); |
| 120 | + return currentAlsaDevice_->switches(); |
121 | 121 | }
|
122 | 122 |
|
123 | 123 | void AlsaWork::setMute(bool enabled)
|
124 | 124 | {
|
125 |
| - currentAlsaDevice_->setMute(enabled); |
| 125 | + currentAlsaDevice_->setMute(enabled); |
126 | 126 | }
|
127 | 127 |
|
128 | 128 | bool AlsaWork::getMute()
|
129 | 129 | {
|
130 |
| - return currentAlsaDevice_->getMute(); |
| 130 | + return currentAlsaDevice_->getMute(); |
131 | 131 | }
|
132 | 132 |
|
133 | 133 | void AlsaWork::setSwitch(const std::string &mixer, int id, bool enabled)
|
134 | 134 | {
|
135 |
| - currentAlsaDevice_->setSwitch(mixer, id, enabled); |
| 135 | + currentAlsaDevice_->setSwitch(mixer, id, enabled); |
136 | 136 | }
|
137 | 137 |
|
138 | 138 | bool AlsaWork::checkCardId(int cardId)
|
139 | 139 | {
|
140 |
| - try { |
141 |
| - if (cardId < int(cardList_.size()) && !cardList_.at(cardId).empty()) { |
142 |
| - return true; |
143 |
| - } |
144 |
| - } |
145 |
| - catch (std::out_of_range &ex) { |
146 |
| - std::cerr << "alsawork.cpp::145:: Item out of Range " << ex.what() << std::endl; |
147 |
| - } |
148 |
| - return false; |
| 140 | + try { |
| 141 | + if (cardId < int(cardList_.size()) && !cardList_.at(cardId).empty()) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + catch (std::out_of_range &ex) { |
| 146 | + std::cerr << "alsawork.cpp::145:: Item out of Range " << ex.what() << std::endl; |
| 147 | + } |
| 148 | + return false; |
149 | 149 | }
|
150 | 150 |
|
151 | 151 | //private
|
152 | 152 | void AlsaWork::checkError (int errorIndex)
|
153 | 153 | {
|
154 |
| - if (errorIndex < 0) { |
155 |
| - std::cerr << snd_strerror(errorIndex) << std::endl; |
156 |
| - } |
| 154 | + if (errorIndex < 0) { |
| 155 | + std::cerr << snd_strerror(errorIndex) << std::endl; |
| 156 | + } |
157 | 157 | }
|
158 | 158 |
|
159 | 159 | int AlsaWork::getTotalCards()
|
160 | 160 | {
|
161 |
| - int cards = 0; |
162 |
| - int index = -1; |
163 |
| - while (true) { |
164 |
| - checkError(snd_card_next(&index)); |
165 |
| - if (index < 0) { |
166 |
| - break; |
167 |
| - } |
168 |
| - ++cards; |
169 |
| - } |
170 |
| - return cards; |
| 161 | + int cards = 0; |
| 162 | + int index = -1; |
| 163 | + while (true) { |
| 164 | + checkError(snd_card_next(&index)); |
| 165 | + if (index < 0) { |
| 166 | + break; |
| 167 | + } |
| 168 | + ++cards; |
| 169 | + } |
| 170 | + return cards; |
171 | 171 | }
|
172 | 172 |
|
173 | 173 | void AlsaWork::getCards()
|
174 | 174 | {
|
175 |
| - if (!cardList_.empty()) |
176 |
| - cardList_.clear(); |
177 |
| - totalCards_ = getTotalCards(); |
178 |
| - if (totalCards_ >= 1) { |
179 |
| - for (int card = 0; card < totalCards_; card++) { |
180 |
| - const std::string cname(getCardName(card)); |
181 |
| - cardList_.push_back(cname); |
182 |
| - } |
183 |
| - } |
| 175 | + if (!cardList_.empty()) |
| 176 | + cardList_.clear(); |
| 177 | + totalCards_ = getTotalCards(); |
| 178 | + if (totalCards_ >= 1) { |
| 179 | + for (int card = 0; card < totalCards_; card++) { |
| 180 | + const std::string cname(getCardName(card)); |
| 181 | + cardList_.push_back(cname); |
| 182 | + } |
| 183 | + } |
184 | 184 | }
|
185 | 185 |
|
186 | 186 | bool AlsaWork::haveVolumeMixers()
|
187 | 187 | {
|
188 |
| - return currentAlsaDevice_->haveMixers(); |
| 188 | + return currentAlsaDevice_->haveMixers(); |
189 | 189 | }
|
190 | 190 |
|
191 | 191 | bool AlsaWork::cardExists(int id)
|
192 | 192 | {
|
193 |
| - return bool(id >= 0 && id < totalCards_); |
| 193 | + return bool(id >= 0 && id < totalCards_); |
194 | 194 | }
|
195 | 195 |
|
196 | 196 | bool AlsaWork::mixerExists(const std::string &name)
|
197 | 197 | {
|
198 |
| - return Tools::itemExists(currentAlsaDevice_->mixers(), name); |
| 198 | + return Tools::itemExists(currentAlsaDevice_->mixers(), name); |
199 | 199 | }
|
200 | 200 |
|
201 | 201 | bool AlsaWork::mixerExists(int id)
|
202 | 202 | {
|
203 |
| - return bool(id >=0 && id < (int)currentAlsaDevice_->mixers().size()); |
| 203 | + return bool(id >=0 && id < (int)currentAlsaDevice_->mixers().size()); |
204 | 204 | }
|
205 | 205 |
|
206 | 206 | int AlsaWork::getFirstCardWithMixers()
|
207 | 207 | {
|
208 |
| - AlsaDevicePtrList::iterator it = std::find_if(devices_.begin(), |
209 |
| - devices_.end(), |
210 |
| - [](const AlsaDevice::Ptr &dev){return dev->haveMixers();}); |
211 |
| - return (it != devices_.end()) ? it - devices_.begin() : 0; |
| 208 | + auto it = std::find_if(devices_.begin(), |
| 209 | + devices_.end(), |
| 210 | + [](const AlsaDevice::Ptr &dev){return dev->haveMixers();}); |
| 211 | + return (it != devices_.end()) ? it - devices_.begin() : 0; |
212 | 212 | }
|
213 | 213 |
|
214 | 214 | int AlsaWork::getCurrentMixerId() const
|
215 | 215 | {
|
216 |
| - return currentAlsaDevice_->currentMixerId(); |
| 216 | + return currentAlsaDevice_->currentMixerId(); |
217 | 217 | }
|
0 commit comments