wmshutdown: Destroy dialog window before shutting down. This is especially useful...
[dockapps.git] / AlsaMixer.app / AMixer / AItem.cc
blobd8e4c7baef526cfc894dfec146ccc741de4c1d73
1 // AItem.cc
2 //
3 // Copyright (C) 2004, Petr Hlavka
4 //
5 // SPDX-License-Identifier: GPL-2.0+
7 #include "AItem.h"
8 #include "AMixer.h"
10 #include <alsa/asoundlib.h>
11 #include <vector>
13 #define ROUND_POS(x) (long ((long (x) + 0.5 > (x)) ? (x) : (x) + 1))
16 // load only playback channels, don't care about common items
17 AItem::AItem(AMixer *m, snd_mixer_elem_t *e) {
18 mixer = m;
19 aElem = e;
21 name = snd_mixer_selem_get_name(aElem);
23 hPVolume = snd_mixer_selem_has_playback_volume(aElem);
24 hPSwitch = snd_mixer_selem_has_playback_switch(aElem);
26 if (hPVolume)
27 snd_mixer_selem_get_playback_volume_range(aElem, &minPVolume, &maxPVolume);
28 else
29 minPVolume = maxPVolume = 0;
31 for (int channel = 0; channel <= (int) SND_MIXER_SCHN_LAST; channel++) {
32 if (snd_mixer_selem_has_playback_channel(aElem, (SNDCHID_T) channel)) {
33 AChannel *ch = new AChannel(this, (SNDCHID_T) channel);
34 pbChannels.push_back(ch);
40 AItem::~AItem() {
41 for (unsigned int i = 0; i < pbChannels.size(); i++)
42 delete pbChannels[i];
46 // set same volume for all channels in this item in percent
47 void AItem::setVolumePerc(unsigned int percent) {
48 if (percent > 100)
49 percent = 100;
51 snd_mixer_selem_set_playback_volume_all(aElem, (long) ROUND_POS((minPVolume + (maxPVolume - minPVolume) * percent / 100.0)));
55 // get max channel volume of all channels in this item in percent
56 unsigned int AItem::getVolumePerc() {
57 long max_vol = 0, act_vol;
59 // find the max volume
60 for (unsigned int i = 0; i < pbChannels.size(); i++)
61 if ((act_vol = pbChannels[i]->getVolume()) > max_vol)
62 max_vol = act_vol;
64 // convert it into percent
65 if (minPVolume != maxPVolume)
66 return ((unsigned int) ROUND_POS((max_vol - minPVolume) * 100.0 / (maxPVolume - minPVolume)));
67 else
68 return (0);
72 // in this app side of view, item is muted if all channels is muted
73 bool AItem::isMuted() {
74 for (unsigned int i = 0; i < pbChannels.size(); i++)
75 if (!pbChannels[i]->isMuted())
76 return (false);
78 return (true);
82 // mute all channels
83 void AItem::mute() {
84 snd_mixer_selem_set_playback_switch_all(aElem, false);
88 // unmute all channels
89 void AItem::unmute() {
90 snd_mixer_selem_set_playback_switch_all(aElem, true);