From 0e8407f567c9beab018033332f79a0bc45d18694 Mon Sep 17 00:00:00 2001 From: Vitozz Date: Thu, 24 Jan 2019 00:46:54 +0200 Subject: [PATCH] Codestyle fixed --- alsawork/alsadevice.cpp | 660 ++++++++++++++++++------------------- alsawork/alsadevice.h | 80 ++--- alsawork/alsawork.cpp | 180 +++++----- alsawork/alsawork.h | 78 ++--- alsawork/mixerswitches.cpp | 90 ++--- alsawork/mixerswitches.h | 34 +- gui/settingsframe.cpp | 646 ++++++++++++++++++------------------ gui/settingsframe.h | 174 +++++----- gui/sliderwindow.cpp | 116 +++---- gui/sliderwindow.h | 26 +- gui/trayicon.cpp | 611 +++++++++++++++++----------------- gui/trayicon.h | 118 +++---- main.cpp | 122 +++---- pulsework/pulsecore.cpp | 410 +++++++++++------------ pulsework/pulsecore.h | 116 +++---- pulsework/pulsedevice.cpp | 80 ++--- pulsework/pulsedevice.h | 46 +-- tools/core.cpp | 542 +++++++++++++++--------------- tools/core.h | 112 +++---- tools/settings.cpp | 246 +++++++------- tools/settings.h | 62 ++-- tools/settingsstr.cpp | 168 +++++----- tools/settingsstr.h | 92 +++--- tools/tools.cpp | 112 +++---- tools/tools.h | 80 ++--- 25 files changed, 2496 insertions(+), 2505 deletions(-) diff --git a/alsawork/alsadevice.cpp b/alsawork/alsadevice.cpp index 182eb8e..4b85f8c 100644 --- a/alsawork/alsadevice.cpp +++ b/alsawork/alsadevice.cpp @@ -1,6 +1,6 @@ /* * alsadevice.cpp - * Copyright (C) 2014-2015 Vitaly Tonkacheyev + * Copyright (C) 2014-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,16 +23,16 @@ #define ZERO 0.0; AlsaDevice::AlsaDevice(int id, const std::string &card) -: id_(id), - name_(card), - volumeMixers_(std::vector()), - captureMixers_(std::vector()), - mixers_(std::vector()), - switches_(MixerSwitches::Ptr(new MixerSwitches())), - currentMixerId_(0), - currentMixerName_(std::string()) + : id_(id), + name_(card), + volumeMixers_(std::vector()), + captureMixers_(std::vector()), + mixers_(std::vector()), + switches_(MixerSwitches::Ptr(new MixerSwitches())), + currentMixerId_(0), + currentMixerName_(std::string()) { - updateElements(); + updateElements(); } AlsaDevice::~AlsaDevice() @@ -41,434 +41,434 @@ AlsaDevice::~AlsaDevice() void AlsaDevice::updateElements() { - if (!volumeMixers_.empty()) { - volumeMixers_.clear(); - } - if (!captureMixers_.empty()) { - captureMixers_.clear(); - } - if (switches_ && !switches_->isEmpty()) { - switches_->clearAll(); - } - snd_mixer_t *handle = getMixerHanlde(id_); - snd_mixer_selem_id_t *smid; - snd_mixer_selem_id_alloca(&smid); - std::string deviceName; - snd_mixer_elem_t *element; - for (element = snd_mixer_first_elem(handle); - element; - element = snd_mixer_elem_next(element)) { - if (!snd_mixer_elem_empty(element)) { - switchcap sCap; - snd_mixer_selem_get_id(element, smid); - deviceName = snd_mixer_selem_id_get_name(smid); - snd_mixer_selem_channel_id_t channel = checkMixerChannels(element); - - if (snd_mixer_selem_has_playback_volume(element) - || snd_mixer_selem_has_playback_volume_joined(element) - || snd_mixer_selem_has_common_volume(element)) { - volumeMixers_.push_back(deviceName); - } - if (snd_mixer_selem_has_capture_volume(element) - || snd_mixer_selem_has_capture_volume_joined(element)) { - captureMixers_.push_back(deviceName); - } - if (snd_mixer_selem_has_capture_switch(element) - || snd_mixer_selem_has_common_switch(element) - || snd_mixer_selem_has_capture_switch_joined(element) - || snd_mixer_selem_has_capture_switch_exclusive(element)){ - int value = 0; - checkError(snd_mixer_selem_get_capture_switch(element, channel, &value)); - sCap = std::make_pair(deviceName, bool(value)); - switches_->pushBack(CAPTURE, sCap); - } - if (snd_mixer_selem_has_playback_switch(element) - || snd_mixer_selem_has_playback_switch_joined(element)){ - int value = 0; - checkError(snd_mixer_selem_get_playback_switch(element, channel, &value)); - sCap = std::make_pair(deviceName, bool(value)); - switches_->pushBack(PLAYBACK, sCap); - } - if (snd_mixer_selem_is_enumerated(element)) { - uint value = 0; - checkError(snd_mixer_selem_get_enum_item(element, channel, &value)); - sCap = std::make_pair(deviceName, bool(value)); - switches_->pushBack(ENUM, sCap); - } - } - } - checkError(snd_mixer_close(handle)); - initMixerList(); + if (!volumeMixers_.empty()) { + volumeMixers_.clear(); + } + if (!captureMixers_.empty()) { + captureMixers_.clear(); + } + if (switches_ && !switches_->isEmpty()) { + switches_->clearAll(); + } + snd_mixer_t *handle = getMixerHanlde(id_); + snd_mixer_selem_id_t *smid; + snd_mixer_selem_id_alloca(&smid); + std::string deviceName; + snd_mixer_elem_t *element; + for (element = snd_mixer_first_elem(handle); + element; + element = snd_mixer_elem_next(element)) { + if (!snd_mixer_elem_empty(element)) { + switchcap sCap; + snd_mixer_selem_get_id(element, smid); + deviceName = snd_mixer_selem_id_get_name(smid); + snd_mixer_selem_channel_id_t channel = checkMixerChannels(element); + + if (snd_mixer_selem_has_playback_volume(element) + || snd_mixer_selem_has_playback_volume_joined(element) + || snd_mixer_selem_has_common_volume(element)) { + volumeMixers_.push_back(deviceName); + } + if (snd_mixer_selem_has_capture_volume(element) + || snd_mixer_selem_has_capture_volume_joined(element)) { + captureMixers_.push_back(deviceName); + } + if (snd_mixer_selem_has_capture_switch(element) + || snd_mixer_selem_has_common_switch(element) + || snd_mixer_selem_has_capture_switch_joined(element) + || snd_mixer_selem_has_capture_switch_exclusive(element)){ + int value = 0; + checkError(snd_mixer_selem_get_capture_switch(element, channel, &value)); + sCap = std::make_pair(deviceName, bool(value)); + switches_->pushBack(CAPTURE, sCap); + } + if (snd_mixer_selem_has_playback_switch(element) + || snd_mixer_selem_has_playback_switch_joined(element)){ + int value = 0; + checkError(snd_mixer_selem_get_playback_switch(element, channel, &value)); + sCap = std::make_pair(deviceName, bool(value)); + switches_->pushBack(PLAYBACK, sCap); + } + if (snd_mixer_selem_is_enumerated(element)) { + uint value = 0; + checkError(snd_mixer_selem_get_enum_item(element, channel, &value)); + sCap = std::make_pair(deviceName, bool(value)); + switches_->pushBack(ENUM, sCap); + } + } + } + checkError(snd_mixer_close(handle)); + initMixerList(); } void AlsaDevice::initMixerList() { - bool isplay = havePlaybackMixers(); - bool isrec = haveCaptureMixers(); - if (!mixers_.empty()) { - mixers_.clear(); - } - if (isplay) { - mixers_.assign(volumeMixers_.begin(), volumeMixers_.end()); - } - if (isrec && !mixers_.empty()) { - mixers_.insert(mixers_.end(), captureMixers_.begin(), captureMixers_.end()); - } - if (!mixers_.empty()) { - setCurrentMixer(0); - } + bool isplay = havePlaybackMixers(); + bool isrec = haveCaptureMixers(); + if (!mixers_.empty()) { + mixers_.clear(); + } + if (isplay) { + mixers_.assign(volumeMixers_.begin(), volumeMixers_.end()); + } + if (isrec && !mixers_.empty()) { + mixers_.insert(mixers_.end(), captureMixers_.begin(), captureMixers_.end()); + } + if (!mixers_.empty()) { + setCurrentMixer(0); + } } snd_mixer_elem_t *AlsaDevice::initMixerElement(snd_mixer_t *handle, const char *mixer) { - snd_mixer_selem_id_t *smid; - snd_mixer_selem_id_alloca(&smid); - snd_mixer_selem_id_set_index(smid, 0); - snd_mixer_selem_id_set_name(smid, mixer); - snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, smid); - return elem; + snd_mixer_selem_id_t *smid; + snd_mixer_selem_id_alloca(&smid); + snd_mixer_selem_id_set_index(smid, 0); + snd_mixer_selem_id_set_name(smid, mixer); + snd_mixer_elem_t* elem = snd_mixer_find_selem(handle, smid); + return elem; } snd_mixer_t *AlsaDevice::getMixerHanlde(int id) { - const std::string card(formatCardName(id)); - snd_mixer_t *handle; - checkError(snd_mixer_open(&handle, 0)); - checkError(snd_mixer_attach(handle, card.c_str())); - checkError(snd_mixer_selem_register(handle, NULL, NULL)); - checkError(snd_mixer_load(handle)); - return handle; + const std::string card(formatCardName(id)); + snd_mixer_t *handle; + checkError(snd_mixer_open(&handle, 0)); + checkError(snd_mixer_attach(handle, card.c_str())); + checkError(snd_mixer_selem_register(handle, nullptr, nullptr)); + checkError(snd_mixer_load(handle)); + return handle; } snd_mixer_selem_channel_id_t AlsaDevice::checkMixerChannels(snd_mixer_elem_t *element) { - if (snd_mixer_selem_is_playback_mono(element)) { - return SND_MIXER_SCHN_MONO; - } - else { - for (int channel = 0; channel <= SND_MIXER_SCHN_LAST; channel++) { - if (snd_mixer_selem_has_playback_channel(element, (snd_mixer_selem_channel_id_t)channel)) { - return (snd_mixer_selem_channel_id_t)channel; - } - } - } - if (snd_mixer_selem_is_capture_mono(element)) { - return SND_MIXER_SCHN_MONO; - } - else { - for (int channel = 0; channel <= SND_MIXER_SCHN_LAST; channel++) { - if (snd_mixer_selem_has_capture_channel(element, (snd_mixer_selem_channel_id_t)channel)) { - return (snd_mixer_selem_channel_id_t)channel; - } - } - } - return SND_MIXER_SCHN_UNKNOWN; + if (snd_mixer_selem_is_playback_mono(element)) { + return SND_MIXER_SCHN_MONO; + } + else { + for (int channel = 0; channel <= SND_MIXER_SCHN_LAST; channel++) { + if (snd_mixer_selem_has_playback_channel(element, (snd_mixer_selem_channel_id_t)channel)) { + return (snd_mixer_selem_channel_id_t)channel; + } + } + } + if (snd_mixer_selem_is_capture_mono(element)) { + return SND_MIXER_SCHN_MONO; + } + else { + for (int channel = 0; channel <= SND_MIXER_SCHN_LAST; channel++) { + if (snd_mixer_selem_has_capture_channel(element, (snd_mixer_selem_channel_id_t)channel)) { + return (snd_mixer_selem_channel_id_t)channel; + } + } + } + return SND_MIXER_SCHN_UNKNOWN; } //This part of code from alsa-utils.git/alsamixer/volume_mapping.c //Copyright (c) 2010 Clemens Ladisch double AlsaDevice::getExp10(double value) { - return exp(value * log(10)); + return exp(value * log(10)); } double AlsaDevice::getNormVolume(snd_mixer_elem_t *element) { - long min, max, value; - double norm, minNorm; - int err; - snd_mixer_selem_channel_id_t chanelid = checkMixerChannels(element); - if (snd_mixer_selem_has_playback_volume(element)) { - err = snd_mixer_selem_get_playback_dB_range(element, &min, &max); - if (err < 0 || min >= max) { - err = snd_mixer_selem_get_playback_volume_range(element, &min, &max); - if (err < 0 || min == max) { - return 0; - } - err = snd_mixer_selem_get_playback_volume(element, chanelid, &value); - if (err < 0) { - return 0; - } - return (value - min) / (double)(max-min); - } - err = snd_mixer_selem_get_playback_dB(element, chanelid, &value); - if (err < 0) { - return 0; - } - if (useLinearDb(min, max)) { - return (value - min) / (double)(max-min); - } - norm = getExp10((value - max) / 6000.0); - if (min != SND_CTL_TLV_DB_GAIN_MUTE) { - minNorm = getExp10((min - max) / 6000.0); - norm = (norm - minNorm)/(1 - minNorm); - } - return norm; - } - else if (snd_mixer_selem_has_capture_volume(element)) { - err = snd_mixer_selem_get_capture_dB_range(element, &min, &max); - if (err < 0 || min >= max) { - err = snd_mixer_selem_get_capture_volume_range(element, &min, &max); - if (err < 0 || min == max) { - return 0; - } - err = snd_mixer_selem_get_capture_volume(element, chanelid, &value); - if (err < 0) { - return 0; - } - return (value - min) / (double)(max-min); - } - - err = snd_mixer_selem_get_capture_dB(element, chanelid, &value); - if (err < 0) { - return 0; - } - if (useLinearDb(min, max)) { - return (value - min) / (double)(max-min); - } - norm = getExp10((value - max) / 6000.0); - if (min != SND_CTL_TLV_DB_GAIN_MUTE) { - minNorm = getExp10((min - max) / 6000.0); - norm = (norm - minNorm)/(1 - minNorm); - } - return norm; - } - return ZERO; + long min, max, value; + double norm, minNorm; + int err; + snd_mixer_selem_channel_id_t chanelid = checkMixerChannels(element); + if (snd_mixer_selem_has_playback_volume(element)) { + err = snd_mixer_selem_get_playback_dB_range(element, &min, &max); + if (err < 0 || min >= max) { + err = snd_mixer_selem_get_playback_volume_range(element, &min, &max); + if (err < 0 || min == max) { + return 0; + } + err = snd_mixer_selem_get_playback_volume(element, chanelid, &value); + if (err < 0) { + return 0; + } + return (value - min) / (double)(max-min); + } + err = snd_mixer_selem_get_playback_dB(element, chanelid, &value); + if (err < 0) { + return 0; + } + if (useLinearDb(min, max)) { + return (value - min) / (double)(max-min); + } + norm = getExp10((value - max) / 6000.0); + if (min != SND_CTL_TLV_DB_GAIN_MUTE) { + minNorm = getExp10((min - max) / 6000.0); + norm = (norm - minNorm)/(1 - minNorm); + } + return norm; + } + else if (snd_mixer_selem_has_capture_volume(element)) { + err = snd_mixer_selem_get_capture_dB_range(element, &min, &max); + if (err < 0 || min >= max) { + err = snd_mixer_selem_get_capture_volume_range(element, &min, &max); + if (err < 0 || min == max) { + return 0; + } + err = snd_mixer_selem_get_capture_volume(element, chanelid, &value); + if (err < 0) { + return 0; + } + return (value - min) / (double)(max-min); + } + + err = snd_mixer_selem_get_capture_dB(element, chanelid, &value); + if (err < 0) { + return 0; + } + if (useLinearDb(min, max)) { + return (value - min) / (double)(max-min); + } + norm = getExp10((value - max) / 6000.0); + if (min != SND_CTL_TLV_DB_GAIN_MUTE) { + minNorm = getExp10((min - max) / 6000.0); + norm = (norm - minNorm)/(1 - minNorm); + } + return norm; + } + return ZERO; } bool AlsaDevice::useLinearDb(long min, long max) { - const long maxDB = 24; - return (max - min) <= maxDB*100; + const long maxDB = 24; + return (max - min) <= maxDB*100; } void AlsaDevice::setNormVolume(snd_mixer_elem_t *element, double volume) { - long min, max, value; - double min_norm; - int err; - if (snd_mixer_selem_has_playback_volume(element)) { - err = snd_mixer_selem_get_playback_dB_range(element, &min, &max); - if (err < 0 || min >= max) { - err = snd_mixer_selem_get_playback_volume_range(element, &min, &max); - if (err < 0) { - return; - } - value = lrint(volume*(max-min)) + min; - checkError(snd_mixer_selem_set_playback_volume_all(element, value)); - return; - } - if (useLinearDb(min, max)) { - value = lrint(volume*(max-min)) + min; - checkError(snd_mixer_selem_set_playback_dB_all(element, value, 1)); - return; - } - if (min != SND_CTL_TLV_DB_GAIN_MUTE) { - min_norm = getExp10((min-max)/6000.0); - volume = volume * (1-min_norm) + min_norm; - } - value = lrint(6000.0 * log10(volume))+max; - checkError(snd_mixer_selem_set_playback_dB_all(element, value, 1)); - return; - } - else if (snd_mixer_selem_has_capture_volume(element)) { - err = snd_mixer_selem_get_capture_dB_range(element, &min, &max); - if (err < 0 || min >= max) { - err = snd_mixer_selem_get_capture_volume_range(element, &min, &max); - if (err < 0) { - return; - } - value = lrint(volume*(max-min)) + min; - checkError(snd_mixer_selem_set_capture_volume_all(element, value)); - return; - } - if (useLinearDb(min, max)) { - value = lrint(volume*(max-min)) + min; - checkError(snd_mixer_selem_set_capture_dB_all(element, value, 1)); - return; - } - if (min != SND_CTL_TLV_DB_GAIN_MUTE) { - min_norm = getExp10((min-max)/6000.0); - volume = volume * (1-min_norm) + min_norm; - } - value = lrint(6000.0 * log10(volume))+max; - checkError(snd_mixer_selem_set_capture_dB_all(element, value, 1)); - } + long min, max, value; + double min_norm; + int err; + if (snd_mixer_selem_has_playback_volume(element)) { + err = snd_mixer_selem_get_playback_dB_range(element, &min, &max); + if (err < 0 || min >= max) { + err = snd_mixer_selem_get_playback_volume_range(element, &min, &max); + if (err < 0) { + return; + } + value = lrint(volume*(max-min)) + min; + checkError(snd_mixer_selem_set_playback_volume_all(element, value)); + return; + } + if (useLinearDb(min, max)) { + value = lrint(volume*(max-min)) + min; + checkError(snd_mixer_selem_set_playback_dB_all(element, value, 1)); + return; + } + if (min != SND_CTL_TLV_DB_GAIN_MUTE) { + min_norm = getExp10((min-max)/6000.0); + volume = volume * (1-min_norm) + min_norm; + } + value = lrint(6000.0 * log10(volume))+max; + checkError(snd_mixer_selem_set_playback_dB_all(element, value, 1)); + return; + } + else if (snd_mixer_selem_has_capture_volume(element)) { + err = snd_mixer_selem_get_capture_dB_range(element, &min, &max); + if (err < 0 || min >= max) { + err = snd_mixer_selem_get_capture_volume_range(element, &min, &max); + if (err < 0) { + return; + } + value = lrint(volume*(max-min)) + min; + checkError(snd_mixer_selem_set_capture_volume_all(element, value)); + return; + } + if (useLinearDb(min, max)) { + value = lrint(volume*(max-min)) + min; + checkError(snd_mixer_selem_set_capture_dB_all(element, value, 1)); + return; + } + if (min != SND_CTL_TLV_DB_GAIN_MUTE) { + min_norm = getExp10((min-max)/6000.0); + volume = volume * (1-min_norm) + min_norm; + } + value = lrint(6000.0 * log10(volume))+max; + checkError(snd_mixer_selem_set_capture_dB_all(element, value, 1)); + } } //This part of code from alsa-utils.git/alsamixer/volume_mapping.c void AlsaDevice::setDeviceVolume(double volume) { - if (!currentMixerName_.empty()) { - snd_mixer_t *handle = getMixerHanlde(id_); - snd_mixer_elem_t *element = initMixerElement(handle, currentMixerName_.c_str()); - if (!snd_mixer_elem_empty(element)) { - setNormVolume(element, volume/100); - } - checkError(snd_mixer_close(handle)); - } + if (!currentMixerName_.empty()) { + snd_mixer_t *handle = getMixerHanlde(id_); + snd_mixer_elem_t *element = initMixerElement(handle, currentMixerName_.c_str()); + if (!snd_mixer_elem_empty(element)) { + setNormVolume(element, volume/100); + } + checkError(snd_mixer_close(handle)); + } } double AlsaDevice::getVolume() { - double result = ZERO; - if (!currentMixerName_.empty()) { - snd_mixer_t *handle = getMixerHanlde(id_); - snd_mixer_elem_t *elem = initMixerElement(handle, currentMixerName_.c_str()); - if (!snd_mixer_elem_empty(elem)) { - result = getNormVolume(elem)*100; - } - checkError(snd_mixer_close(handle)); - } - return round(result); + double result = ZERO; + if (!currentMixerName_.empty()) { + snd_mixer_t *handle = getMixerHanlde(id_); + snd_mixer_elem_t *elem = initMixerElement(handle, currentMixerName_.c_str()); + if (!snd_mixer_elem_empty(elem)) { + result = getNormVolume(elem)*100; + } + checkError(snd_mixer_close(handle)); + } + return round(result); } void AlsaDevice::setSwitch(const std::string &mixer, int id, bool enabled) { - snd_mixer_t *handle = getMixerHanlde(id_); - snd_mixer_elem_t* elem = initMixerElement(handle, mixer.c_str()); - if (!snd_mixer_elem_empty(elem)) { - switch (id) { - case PLAYBACK: - checkError(snd_mixer_selem_set_playback_switch_all(elem, int(enabled))); - break; - case CAPTURE: - checkError(snd_mixer_selem_set_capture_switch_all(elem, int(enabled))); - break; - case ENUM: - snd_mixer_selem_channel_id_t channel = checkMixerChannels(elem); - checkError(snd_mixer_selem_set_enum_item(elem, channel, uint(enabled))); - break; - } - } - checkError(snd_mixer_close(handle)); + snd_mixer_t *handle = getMixerHanlde(id_); + snd_mixer_elem_t* elem = initMixerElement(handle, mixer.c_str()); + if (!snd_mixer_elem_empty(elem)) { + switch (id) { + case PLAYBACK: + checkError(snd_mixer_selem_set_playback_switch_all(elem, int(enabled))); + break; + case CAPTURE: + checkError(snd_mixer_selem_set_capture_switch_all(elem, int(enabled))); + break; + case ENUM: + snd_mixer_selem_channel_id_t channel = checkMixerChannels(elem); + checkError(snd_mixer_selem_set_enum_item(elem, channel, uint(enabled))); + break; + } + } + checkError(snd_mixer_close(handle)); } void AlsaDevice::setMute(bool enabled) { - if (!currentMixerName_.empty()) { - snd_mixer_t *handle = getMixerHanlde(id_); - snd_mixer_elem_t* elem = initMixerElement(handle, currentMixerName_.c_str()); - if (!snd_mixer_elem_empty(elem)) { - if (snd_mixer_selem_has_playback_switch(elem) - || snd_mixer_selem_has_playback_switch_joined(elem)) { - checkError(snd_mixer_selem_set_playback_switch_all(elem, int(enabled))); - } - if (snd_mixer_selem_has_capture_switch(elem) - || snd_mixer_selem_has_common_switch(elem) - || snd_mixer_selem_has_capture_switch_joined(elem) - || snd_mixer_selem_has_capture_switch_exclusive(elem)) { - checkError(snd_mixer_selem_set_capture_switch_all(elem, int(enabled))); - } - } - checkError(snd_mixer_close(handle)); - } + if (!currentMixerName_.empty()) { + snd_mixer_t *handle = getMixerHanlde(id_); + snd_mixer_elem_t* elem = initMixerElement(handle, currentMixerName_.c_str()); + if (!snd_mixer_elem_empty(elem)) { + if (snd_mixer_selem_has_playback_switch(elem) + || snd_mixer_selem_has_playback_switch_joined(elem)) { + checkError(snd_mixer_selem_set_playback_switch_all(elem, int(enabled))); + } + if (snd_mixer_selem_has_capture_switch(elem) + || snd_mixer_selem_has_common_switch(elem) + || snd_mixer_selem_has_capture_switch_joined(elem) + || snd_mixer_selem_has_capture_switch_exclusive(elem)) { + checkError(snd_mixer_selem_set_capture_switch_all(elem, int(enabled))); + } + } + checkError(snd_mixer_close(handle)); + } } bool AlsaDevice::getMute() { - if (!currentMixerName_.empty()) { - snd_mixer_t *handle = getMixerHanlde(id_); - snd_mixer_elem_t* elem = initMixerElement(handle, currentMixerName_.c_str()); - if (!snd_mixer_elem_empty(elem)) { - snd_mixer_selem_channel_id_t channel = checkMixerChannels(elem); - if (snd_mixer_selem_has_playback_switch(elem) - || snd_mixer_selem_has_playback_switch_joined(elem)) { - int value = 0; - checkError(snd_mixer_selem_get_playback_switch(elem, channel, &value)); - checkError(snd_mixer_close(handle)); - return bool(value); - } - if (snd_mixer_selem_has_capture_switch(elem) - || snd_mixer_selem_has_common_switch(elem) - || snd_mixer_selem_has_capture_switch_joined(elem) - || snd_mixer_selem_has_capture_switch_exclusive(elem)) { - int value = 0; - checkError(snd_mixer_selem_get_capture_switch(elem, channel, &value)); - checkError(snd_mixer_close(handle)); - return bool(value); - } - } - checkError(snd_mixer_close(handle)); - } - return true; + if (!currentMixerName_.empty()) { + snd_mixer_t *handle = getMixerHanlde(id_); + snd_mixer_elem_t* elem = initMixerElement(handle, currentMixerName_.c_str()); + if (!snd_mixer_elem_empty(elem)) { + snd_mixer_selem_channel_id_t channel = checkMixerChannels(elem); + if (snd_mixer_selem_has_playback_switch(elem) + || snd_mixer_selem_has_playback_switch_joined(elem)) { + int value = 0; + checkError(snd_mixer_selem_get_playback_switch(elem, channel, &value)); + checkError(snd_mixer_close(handle)); + return bool(value); + } + if (snd_mixer_selem_has_capture_switch(elem) + || snd_mixer_selem_has_common_switch(elem) + || snd_mixer_selem_has_capture_switch_joined(elem) + || snd_mixer_selem_has_capture_switch_exclusive(elem)) { + int value = 0; + checkError(snd_mixer_selem_get_capture_switch(elem, channel, &value)); + checkError(snd_mixer_close(handle)); + return bool(value); + } + } + checkError(snd_mixer_close(handle)); + } + return true; } std::string AlsaDevice::formatCardName(long long int id) { - return std::string("hw:") + std::to_string(id); + return std::string("hw:") + std::to_string(id); } void AlsaDevice::setCurrentMixer(int id) { - if(id >= 0 && id < int(mixers_.size())) { - currentMixerId_ = id; - currentMixerName_ = mixers_.at(id); - } + if(id >= 0 && id < int(mixers_.size())) { + currentMixerId_ = id; + currentMixerName_ = mixers_.at(id); + } } void AlsaDevice::setCurrentMixer(const std::string &mixer) { - if(Tools::itemExists(mixers_, mixer)){ - currentMixerId_ = Tools::itemIndex(mixers_, mixer); - currentMixerName_ = mixer; + if(Tools::itemExists(mixers_, mixer)){ + currentMixerId_ = Tools::itemIndex(mixers_, mixer); + currentMixerName_ = mixer; #ifdef IS_DEBUG - std::cout << "MID " << currentMixerId_ << std::endl; - std::cout << "MN " << currentMixerName_ << std::endl; + std::cout << "MID " << currentMixerId_ << std::endl; + std::cout << "MN " << currentMixerName_ << std::endl; #endif - } + } } const std::string &AlsaDevice::name() const { - return name_; + return name_; } int AlsaDevice::id() const { - return id_; + return id_; } const std::vector &AlsaDevice::mixers() const { - return mixers_; + return mixers_; } void AlsaDevice::checkError (int errorIndex) { - if (errorIndex < 0) { - std::cerr << snd_strerror(errorIndex) << std::endl; - } + if (errorIndex < 0) { + std::cerr << snd_strerror(errorIndex) << std::endl; + } } bool AlsaDevice::havePlaybackMixers() { - return !volumeMixers_.empty(); + return !volumeMixers_.empty(); } bool AlsaDevice::haveCaptureMixers() { - return !captureMixers_.empty(); + return !captureMixers_.empty(); } bool AlsaDevice::haveMixers() { - return !mixers_.empty(); + return !mixers_.empty(); } MixerSwitches::Ptr AlsaDevice::switches() { - updateElements(); - return switches_; + updateElements(); + return switches_; } int AlsaDevice::currentMixerId() const { - return currentMixerId_; + return currentMixerId_; } const std::string &AlsaDevice::currentMixer() const { - return currentMixerName_; + return currentMixerName_; } diff --git a/alsawork/alsadevice.h b/alsawork/alsadevice.h index e48993a..1bd2673 100644 --- a/alsawork/alsadevice.h +++ b/alsawork/alsadevice.h @@ -1,6 +1,6 @@ /* * alsadevice.h - * Copyright (C) 2014-2015 Vitaly Tonkacheyev + * Copyright (C) 2014-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,49 +30,49 @@ class AlsaDevice { public: - AlsaDevice(int id, const std::string &card); - ~AlsaDevice(); - AlsaDevice(AlsaDevice const &); - typedef std::shared_ptr Ptr; - const std::string &name() const; - int id() const; - const std::vector &mixers() const; - MixerSwitches::Ptr switches(); - const std::string ¤tMixer() const; - int currentMixerId() const; - bool havePlaybackMixers(); - bool haveCaptureMixers(); - bool haveMixers(); - double getVolume(); - bool getMute(); - void setDeviceVolume(double volume); - void setCurrentMixer(int id); - void setCurrentMixer(const std::string &mixer); - void setSwitch(const std::string &mixer, int id, bool enabled); - void setMute(bool enabled); - static std::string formatCardName(long long int id); + AlsaDevice(int id, const std::string &card); + ~AlsaDevice(); + AlsaDevice(AlsaDevice const &); + typedef std::shared_ptr Ptr; + const std::string &name() const; + int id() const; + const std::vector &mixers() const; + MixerSwitches::Ptr switches(); + const std::string ¤tMixer() const; + int currentMixerId() const; + bool havePlaybackMixers(); + bool haveCaptureMixers(); + bool haveMixers(); + double getVolume(); + bool getMute(); + void setDeviceVolume(double volume); + void setCurrentMixer(int id); + void setCurrentMixer(const std::string &mixer); + void setSwitch(const std::string &mixer, int id, bool enabled); + void setMute(bool enabled); + static std::string formatCardName(long long int id); private: - snd_mixer_t *getMixerHanlde(int id); - snd_mixer_selem_channel_id_t checkMixerChannels(snd_mixer_elem_t *element); - snd_mixer_elem_t *initMixerElement(snd_mixer_t *handle, const char *mixer); - void checkError (int errorIndex); - void initMixerList(); - double getNormVolume(snd_mixer_elem_t *element); - double getExp10(double value); - bool useLinearDb(long min, long max); - void setNormVolume(snd_mixer_elem_t *element, double volume); - void updateElements(); + snd_mixer_t *getMixerHanlde(int id); + snd_mixer_selem_channel_id_t checkMixerChannels(snd_mixer_elem_t *element); + snd_mixer_elem_t *initMixerElement(snd_mixer_t *handle, const char *mixer); + void checkError (int errorIndex); + void initMixerList(); + double getNormVolume(snd_mixer_elem_t *element); + double getExp10(double value); + bool useLinearDb(long min, long max); + void setNormVolume(snd_mixer_elem_t *element, double volume); + void updateElements(); private: - int id_; - std::string name_; - std::vector volumeMixers_; - std::vector captureMixers_; - std::vector mixers_; - MixerSwitches::Ptr switches_; - int currentMixerId_; - std::string currentMixerName_; + int id_; + std::string name_; + std::vector volumeMixers_; + std::vector captureMixers_; + std::vector mixers_; + MixerSwitches::Ptr switches_; + int currentMixerId_; + std::string currentMixerName_; }; diff --git a/alsawork/alsawork.cpp b/alsawork/alsawork.cpp index 0950d09..037f2c3 100644 --- a/alsawork/alsawork.cpp +++ b/alsawork/alsawork.cpp @@ -1,6 +1,6 @@ /* * alsawork.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,196 +22,196 @@ #include AlsaWork::AlsaWork() -: cardList_(std::vector()) + : cardList_(std::vector()) { - getCards(); - int id = 0; - std::for_each(cardList_.begin(), cardList_.end(), [&](const std::string &name){ - devices_.push_back(AlsaDevice::Ptr(new AlsaDevice(id, name))); - ++id; - }); - setCurrentCard(0); - setCurrentMixer(0); + getCards(); + int id = 0; + for(const std::string &name : cardList_){ + devices_.push_back(AlsaDevice::Ptr(new AlsaDevice(id, name))); + ++id; + } + setCurrentCard(0); + setCurrentMixer(0); } AlsaWork::~AlsaWork() { - if (!devices_.empty()) - devices_.clear(); - currentAlsaDevice_.reset(); - snd_config_update_free_global(); + if (!devices_.empty()) + devices_.clear(); + currentAlsaDevice_.reset(); + snd_config_update_free_global(); } //public void AlsaWork::setCurrentCard(int cardId) { - if(cardId < int(devices_.size())) { - currentAlsaDevice_ = devices_.at(cardId); - } + if(cardId < int(devices_.size())) { + currentAlsaDevice_ = devices_.at(cardId); + } } void AlsaWork::setCurrentMixer(const std::string &mixer) { - currentAlsaDevice_->setCurrentMixer(mixer); + currentAlsaDevice_->setCurrentMixer(mixer); } void AlsaWork::setCurrentMixer(int id) { - currentAlsaDevice_->setCurrentMixer(id); + currentAlsaDevice_->setCurrentMixer(id); } void AlsaWork::setAlsaVolume(double volume) { - currentAlsaDevice_->setDeviceVolume(volume); + currentAlsaDevice_->setDeviceVolume(volume); } double AlsaWork::getAlsaVolume() const { - return currentAlsaDevice_->getVolume(); + return currentAlsaDevice_->getVolume(); } const std::string AlsaWork::getCardName(int index) { - const std::string card(AlsaDevice::formatCardName(index)); - snd_ctl_t *ctl; - int err = snd_ctl_open(&ctl, card.c_str(), SND_CTL_NONBLOCK); - if (err < 0) { - checkError(err); - return std::string(); - } - snd_ctl_card_info_t *cardInfo; - snd_ctl_card_info_alloca(&cardInfo); - err = snd_ctl_card_info(ctl, cardInfo); - if (err < 0) { - checkError(err); - return std::string(); - } - const std::string cardName = snd_ctl_card_info_get_name(cardInfo); - checkError(snd_ctl_close(ctl)); - return cardName; + const std::string card(AlsaDevice::formatCardName(index)); + snd_ctl_t *ctl; + int err = snd_ctl_open(&ctl, card.c_str(), SND_CTL_NONBLOCK); + if (err < 0) { + checkError(err); + return std::string(); + } + snd_ctl_card_info_t *cardInfo; + snd_ctl_card_info_alloca(&cardInfo); + err = snd_ctl_card_info(ctl, cardInfo); + if (err < 0) { + checkError(err); + return std::string(); + } + const std::string cardName = snd_ctl_card_info_get_name(cardInfo); + checkError(snd_ctl_close(ctl)); + return cardName; } const std::string AlsaWork::getMixerName(int index) { - std::string mixerName; - if (index >= 0 && index < currentAlsaDevice_->mixers().size()) { - mixerName = currentAlsaDevice_->mixers().at(index); - } - return mixerName; + std::string mixerName; + if (index >= 0 && index < currentAlsaDevice_->mixers().size()) { + mixerName = currentAlsaDevice_->mixers().at(index); + } + return mixerName; } std::string AlsaWork::getCurrentMixerName() const { - return currentAlsaDevice_->currentMixer(); + return currentAlsaDevice_->currentMixer(); } const std::vector &AlsaWork::getCardsList() const { - return cardList_; + return cardList_; } const std::vector &AlsaWork::getVolumeMixers() const { - return currentAlsaDevice_->mixers(); + return currentAlsaDevice_->mixers(); } MixerSwitches::Ptr AlsaWork::getSwitchList() const { - return currentAlsaDevice_->switches(); + return currentAlsaDevice_->switches(); } void AlsaWork::setMute(bool enabled) { - currentAlsaDevice_->setMute(enabled); + currentAlsaDevice_->setMute(enabled); } bool AlsaWork::getMute() { - return currentAlsaDevice_->getMute(); + return currentAlsaDevice_->getMute(); } void AlsaWork::setSwitch(const std::string &mixer, int id, bool enabled) { - currentAlsaDevice_->setSwitch(mixer, id, enabled); + currentAlsaDevice_->setSwitch(mixer, id, enabled); } bool AlsaWork::checkCardId(int cardId) { - try { - if (cardId < int(cardList_.size()) && !cardList_.at(cardId).empty()) { - return true; - } - } - catch (std::out_of_range &ex) { - std::cerr << "alsawork.cpp::145:: Item out of Range " << ex.what() << std::endl; - } - return false; + try { + if (cardId < int(cardList_.size()) && !cardList_.at(cardId).empty()) { + return true; + } + } + catch (std::out_of_range &ex) { + std::cerr << "alsawork.cpp::145:: Item out of Range " << ex.what() << std::endl; + } + return false; } //private void AlsaWork::checkError (int errorIndex) { - if (errorIndex < 0) { - std::cerr << snd_strerror(errorIndex) << std::endl; - } + if (errorIndex < 0) { + std::cerr << snd_strerror(errorIndex) << std::endl; + } } int AlsaWork::getTotalCards() { - int cards = 0; - int index = -1; - while (true) { - checkError(snd_card_next(&index)); - if (index < 0) { - break; - } - ++cards; - } - return cards; + int cards = 0; + int index = -1; + while (true) { + checkError(snd_card_next(&index)); + if (index < 0) { + break; + } + ++cards; + } + return cards; } void AlsaWork::getCards() { - if (!cardList_.empty()) - cardList_.clear(); - totalCards_ = getTotalCards(); - if (totalCards_ >= 1) { - for (int card = 0; card < totalCards_; card++) { - const std::string cname(getCardName(card)); - cardList_.push_back(cname); - } - } + if (!cardList_.empty()) + cardList_.clear(); + totalCards_ = getTotalCards(); + if (totalCards_ >= 1) { + for (int card = 0; card < totalCards_; card++) { + const std::string cname(getCardName(card)); + cardList_.push_back(cname); + } + } } bool AlsaWork::haveVolumeMixers() { - return currentAlsaDevice_->haveMixers(); + return currentAlsaDevice_->haveMixers(); } bool AlsaWork::cardExists(int id) { - return bool(id >= 0 && id < totalCards_); + return bool(id >= 0 && id < totalCards_); } bool AlsaWork::mixerExists(const std::string &name) { - return Tools::itemExists(currentAlsaDevice_->mixers(), name); + return Tools::itemExists(currentAlsaDevice_->mixers(), name); } bool AlsaWork::mixerExists(int id) { - return bool(id >=0 && id < (int)currentAlsaDevice_->mixers().size()); + return bool(id >=0 && id < (int)currentAlsaDevice_->mixers().size()); } int AlsaWork::getFirstCardWithMixers() { - AlsaDevicePtrList::iterator it = std::find_if(devices_.begin(), - devices_.end(), - [](const AlsaDevice::Ptr &dev){return dev->haveMixers();}); - return (it != devices_.end()) ? it - devices_.begin() : 0; + auto it = std::find_if(devices_.begin(), + devices_.end(), + [](const AlsaDevice::Ptr &dev){return dev->haveMixers();}); + return (it != devices_.end()) ? it - devices_.begin() : 0; } int AlsaWork::getCurrentMixerId() const { - return currentAlsaDevice_->currentMixerId(); + return currentAlsaDevice_->currentMixerId(); } diff --git a/alsawork/alsawork.h b/alsawork/alsawork.h index 0137183..4b6c5c4 100644 --- a/alsawork/alsawork.h +++ b/alsawork/alsawork.h @@ -1,6 +1,6 @@ /* * alsawork.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,47 +33,47 @@ typedef std::vector AlsaDevicePtrList; class AlsaWork { public: - AlsaWork(); - AlsaWork(AlsaWork const &); - ~AlsaWork(); - typedef std::shared_ptr Ptr; - void setCurrentCard(int cardId); - void setCurrentMixer(const std::string &mixer); - void setCurrentMixer(int id); - void setAlsaVolume(double volume); - double getAlsaVolume() const; - const std::string getCardName(int index); - const std::string getMixerName(int index); - std::string getCurrentMixerName() const; - const std::vector &getCardsList() const; - const std::vector &getVolumeMixers() const; - MixerSwitches::Ptr getSwitchList() const; - void setSwitch(const std::string& mixer, int id, bool enabled); - void setMute(bool enabled); - bool getMute(); - bool haveVolumeMixers(); - bool cardExists(int id); - bool mixerExists(const std::string &name); - bool mixerExists(int id); - int getFirstCardWithMixers(); - int getCurrentMixerId() const; + AlsaWork(); + AlsaWork(AlsaWork const &); + ~AlsaWork(); + typedef std::shared_ptr Ptr; + void setCurrentCard(int cardId); + void setCurrentMixer(const std::string &mixer); + void setCurrentMixer(int id); + void setAlsaVolume(double volume); + double getAlsaVolume() const; + const std::string getCardName(int index); + const std::string getMixerName(int index); + std::string getCurrentMixerName() const; + const std::vector &getCardsList() const; + const std::vector &getVolumeMixers() const; + MixerSwitches::Ptr getSwitchList() const; + void setSwitch(const std::string& mixer, int id, bool enabled); + void setMute(bool enabled); + bool getMute(); + bool haveVolumeMixers(); + bool cardExists(int id); + bool mixerExists(const std::string &name); + bool mixerExists(int id); + int getFirstCardWithMixers(); + int getCurrentMixerId() const; private: - bool checkCardId(int cardId); - int getTotalCards(); - snd_mixer_t *getMixerHanlde(int id); - snd_mixer_elem_t *initMixerElement(snd_mixer_t *handle, const char *mixer); - void setVolume(snd_mixer_elem_t *element, snd_mixer_t *handle, double volume); - void checkError (int errorIndex); - void getCards(); - void updateMixers(int cardIndex); - void updateMixerList(int cardIndex); - snd_mixer_selem_channel_id_t checkMixerChannels(snd_mixer_elem_t *element); + bool checkCardId(int cardId); + int getTotalCards(); + snd_mixer_t *getMixerHanlde(int id); + snd_mixer_elem_t *initMixerElement(snd_mixer_t *handle, const char *mixer); + void setVolume(snd_mixer_elem_t *element, snd_mixer_t *handle, double volume); + void checkError (int errorIndex); + void getCards(); + void updateMixers(int cardIndex); + void updateMixerList(int cardIndex); + snd_mixer_selem_channel_id_t checkMixerChannels(snd_mixer_elem_t *element); private: - std::vector cardList_; - int totalCards_; - AlsaDevice::Ptr currentAlsaDevice_; - AlsaDevicePtrList devices_; + std::vector cardList_; + int totalCards_; + AlsaDevice::Ptr currentAlsaDevice_; + AlsaDevicePtrList devices_; }; #endif // ALSAWORK_H diff --git a/alsawork/mixerswitches.cpp b/alsawork/mixerswitches.cpp index ca6565a..aac7fc4 100644 --- a/alsawork/mixerswitches.cpp +++ b/alsawork/mixerswitches.cpp @@ -1,6 +1,6 @@ /* * mixerswitches.cpp - * Copyright (C) 2012 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,89 +24,89 @@ MixerSwitches::MixerSwitches() } MixerSwitches::MixerSwitches(const MixerSwitches &ms) -: captureSwitchList_(ms.captureSwitchList()), - playbackSwitchList_(ms.playbackSwitchList()), - enumSwitchList_(ms.enumSwitchList()) + : captureSwitchList_(ms.captureSwitchList()), + playbackSwitchList_(ms.playbackSwitchList()), + enumSwitchList_(ms.enumSwitchList()) { } void MixerSwitches::setCaptureSwitchList(const std::vector &list) { - captureSwitchList_.assign(list.begin(), list.end()); + captureSwitchList_.assign(list.begin(), list.end()); } void MixerSwitches::setPlaybackSwitchList(const std::vector &list) { - playbackSwitchList_.assign(list.begin(), list.end()); + playbackSwitchList_.assign(list.begin(), list.end()); } void MixerSwitches::setEnumSwitchList(const std::vector &list) { - enumSwitchList_.assign(list.begin(), list.end()); + enumSwitchList_.assign(list.begin(), list.end()); } void MixerSwitches::pushBack(SwitchType sType, switchcap &item) { - switch (sType) { - case PLAYBACK: - playbackSwitchList_.push_back(item); - break; - case CAPTURE: - captureSwitchList_.push_back(item); - break; - case ENUM: - enumSwitchList_.push_back(item); - break; - } + switch (sType) { + case PLAYBACK: + playbackSwitchList_.push_back(item); + break; + case CAPTURE: + captureSwitchList_.push_back(item); + break; + case ENUM: + enumSwitchList_.push_back(item); + break; + } } void MixerSwitches::clear(SwitchType sType) { - switch (sType) { - case PLAYBACK: - if (!playbackSwitchList_.empty()) - playbackSwitchList_.clear(); - break; - case CAPTURE: - if (!captureSwitchList_.empty()) - captureSwitchList_.clear(); - break; - case ENUM: - if (!enumSwitchList_.empty()) - enumSwitchList_.clear(); - break; - } + switch (sType) { + case PLAYBACK: + if (!playbackSwitchList_.empty()) + playbackSwitchList_.clear(); + break; + case CAPTURE: + if (!captureSwitchList_.empty()) + captureSwitchList_.clear(); + break; + case ENUM: + if (!enumSwitchList_.empty()) + enumSwitchList_.clear(); + break; + } } void MixerSwitches::clearAll() { - if (!playbackSwitchList_.empty()) { - playbackSwitchList_.clear(); - } - if (!captureSwitchList_.empty()) { - captureSwitchList_.clear(); - } - if (!enumSwitchList_.empty()) { - enumSwitchList_.clear(); - } + if (!playbackSwitchList_.empty()) { + playbackSwitchList_.clear(); + } + if (!captureSwitchList_.empty()) { + captureSwitchList_.clear(); + } + if (!enumSwitchList_.empty()) { + enumSwitchList_.clear(); + } } bool MixerSwitches::isEmpty() { - return (playbackSwitchList_.empty() && captureSwitchList_.empty() && enumSwitchList_.empty()); + return (playbackSwitchList_.empty() && captureSwitchList_.empty() && enumSwitchList_.empty()); } const std::vector &MixerSwitches::captureSwitchList() const { - return captureSwitchList_; + return captureSwitchList_; } const std::vector &MixerSwitches::playbackSwitchList() const { - return playbackSwitchList_; + return playbackSwitchList_; } const std::vector &MixerSwitches::enumSwitchList() const { - return enumSwitchList_; + return enumSwitchList_; } diff --git a/alsawork/mixerswitches.h b/alsawork/mixerswitches.h index 6347d14..226e7d5 100644 --- a/alsawork/mixerswitches.h +++ b/alsawork/mixerswitches.h @@ -1,6 +1,6 @@ /* * mixerswitches.h - * Copyright (C) 2012 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,24 +26,24 @@ class MixerSwitches { public: - MixerSwitches(); - MixerSwitches(const MixerSwitches &ms); - typedef std::shared_ptr Ptr; - void pushBack(SwitchType sType, switchcap &item); - void setCaptureSwitchList(const std::vector &list); - void setPlaybackSwitchList(const std::vector &list); - void setEnumSwitchList(const std::vector &list); - void clear(SwitchType sType); - void clearAll(); - const std::vector &captureSwitchList() const; - const std::vector &playbackSwitchList() const; - const std::vector &enumSwitchList() const; - bool isEmpty(); + MixerSwitches(); + MixerSwitches(const MixerSwitches &ms); + typedef std::shared_ptr Ptr; + void pushBack(SwitchType sType, switchcap &item); + void setCaptureSwitchList(const std::vector &list); + void setPlaybackSwitchList(const std::vector &list); + void setEnumSwitchList(const std::vector &list); + void clear(SwitchType sType); + void clearAll(); + const std::vector &captureSwitchList() const; + const std::vector &playbackSwitchList() const; + const std::vector &enumSwitchList() const; + bool isEmpty(); private: - std::vector captureSwitchList_; - std::vector playbackSwitchList_; - std::vector enumSwitchList_; + std::vector captureSwitchList_; + std::vector playbackSwitchList_; + std::vector enumSwitchList_; }; #endif // MIXERSWITCHES_H diff --git a/gui/settingsframe.cpp b/gui/settingsframe.cpp index 49b11ac..d9ffad0 100644 --- a/gui/settingsframe.cpp +++ b/gui/settingsframe.cpp @@ -1,6 +1,6 @@ /* * settingsframe.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,465 +29,459 @@ #define EN_SWITCH_NAME _("Enumerated Control") SettingsFrame::SettingsFrame(BaseObjectType* cobject, - const Glib::RefPtr& refGlade) -: Gtk::Dialog(cobject), - okButton_(0), - cancelButton_(0), - sndCardBox_(0), - mixerBox_(0), - playbackSwitchTree_(0), - captureSwitchTree_(0), - otherSwitchTree_(0), - isAutoRun_(0), - tabPos_(0), - tabWidget_(0), - pulseHBox_(0), - alsaHBox_(0), - usePulse_(0), - usePolling_(0), -#ifdef HAVE_PULSE - pulseBox_(0), - pulseDev_(0), - pulseCards_(Glib::RefPtr()), -#endif - cards_(Glib::RefPtr()), - mixers_(Glib::RefPtr()), - pbSwitches_(Glib::RefPtr()), - capSwitches_(Glib::RefPtr()), - enumSwitches_(Glib::RefPtr()), - settings_(settingsStr::Ptr()), - mixerId_(0), - cardId_(0), - isPulse_(false) + const Glib::RefPtr& refGlade) + : Gtk::Dialog(cobject), + okButton_(nullptr), + cancelButton_(nullptr), + sndCardBox_(nullptr), + mixerBox_(nullptr), + playbackSwitchTree_(nullptr), + captureSwitchTree_(nullptr), + otherSwitchTree_(nullptr), + isAutoRun_(nullptr), + tabPos_(nullptr), + tabWidget_(nullptr), + pulseHBox_(nullptr), + alsaHBox_(nullptr), + usePulse_(nullptr), + usePolling_(nullptr), + #ifdef HAVE_PULSE + pulseBox_(nullptr), + pulseDev_(0), + pulseCards_(Glib::RefPtr()), + #endif + cards_(Glib::RefPtr()), + mixers_(Glib::RefPtr()), + pbSwitches_(Glib::RefPtr()), + capSwitches_(Glib::RefPtr()), + enumSwitches_(Glib::RefPtr()), + settings_(settingsStr::Ptr()), + mixerId_(0), + cardId_(0), + isPulse_(false) { - //init all lists - Glib::RefPtr builder = refGlade; - builder->get_widget("ok_button", okButton_); - builder->get_widget("cancel_button", cancelButton_); - builder->get_widget("sndcardbox", sndCardBox_); - builder->get_widget("mixerBox", mixerBox_); - builder->get_widget("playbacktree", playbackSwitchTree_); - builder->get_widget("capturetree", captureSwitchTree_); - builder->get_widget("othertree", otherSwitchTree_); - builder->get_widget("is_autorun", isAutoRun_); - builder->get_widget("tabspos", tabPos_); - builder->get_widget("tabwidget", tabWidget_); - builder->get_widget("pulseBox", pulseHBox_); - builder->get_widget("alsaBox", alsaHBox_); - builder->get_widget("usePulse", usePulse_); - builder->get_widget("usePolling", usePolling_); + //init all lists + Glib::RefPtr builder = refGlade; + builder->get_widget("ok_button", okButton_); + builder->get_widget("cancel_button", cancelButton_); + builder->get_widget("sndcardbox", sndCardBox_); + builder->get_widget("mixerBox", mixerBox_); + builder->get_widget("playbacktree", playbackSwitchTree_); + builder->get_widget("capturetree", captureSwitchTree_); + builder->get_widget("othertree", otherSwitchTree_); + builder->get_widget("is_autorun", isAutoRun_); + builder->get_widget("tabspos", tabPos_); + builder->get_widget("tabwidget", tabWidget_); + builder->get_widget("pulseBox", pulseHBox_); + builder->get_widget("alsaBox", alsaHBox_); + builder->get_widget("usePulse", usePulse_); + builder->get_widget("usePolling", usePolling_); #ifdef HAVE_PULSE - builder->get_widget("pulseDevices", pulseBox_); + builder->get_widget("pulseDevices", pulseBox_); #endif - //signals - if (tabPos_) { - tabPos_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onTabPos)); - } - if (okButton_) { - okButton_->signal_pressed().connect(sigc::mem_fun(*this, &SettingsFrame::onOkButton)); - } - if (cancelButton_) { - cancelButton_->signal_pressed().connect(sigc::mem_fun(*this, &SettingsFrame::onCancelButton)); - } - if (isAutoRun_) { - isAutoRun_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onAutorunToggled)); - } - if (sndCardBox_) { - sndCardBox_->signal_changed().connect(sigc::mem_fun(*this, &SettingsFrame::sndBoxChanged)); - } - if (mixerBox_) { - mixerBox_->signal_changed().connect(sigc::mem_fun(*this, &SettingsFrame::mixerBoxChanged)); - } - if (usePolling_) { - usePolling_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onUsePollingToggled)); - } + //signals + if (tabPos_) { + tabPos_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onTabPos)); + } + if (okButton_) { + okButton_->signal_pressed().connect(sigc::mem_fun(*this, &SettingsFrame::onOkButton)); + } + if (cancelButton_) { + cancelButton_->signal_pressed().connect(sigc::mem_fun(*this, &SettingsFrame::onCancelButton)); + } + if (isAutoRun_) { + isAutoRun_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onAutorunToggled)); + } + if (sndCardBox_) { + sndCardBox_->signal_changed().connect(sigc::mem_fun(*this, &SettingsFrame::sndBoxChanged)); + } + if (mixerBox_) { + mixerBox_->signal_changed().connect(sigc::mem_fun(*this, &SettingsFrame::mixerBoxChanged)); + } + if (usePolling_) { + usePolling_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onUsePollingToggled)); + } #ifdef HAVE_PULSE - if (usePulse_) { - usePulse_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onPulseToggled)); - } - if (pulseBox_) { - pulseBox_->signal_changed().connect(sigc::mem_fun(*this, &SettingsFrame::onPulseDeviceChanged)); - } + if (usePulse_) { + usePulse_->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onPulseToggled)); + } + if (pulseBox_) { + pulseBox_->signal_changed().connect(sigc::mem_fun(*this, &SettingsFrame::onPulseDeviceChanged)); + } #else - pulseHBox_->set_visible(false); - usePulse_->set_visible(false); + pulseHBox_->set_visible(false); + usePulse_->set_visible(false); #endif - this->signal_delete_event().connect(sigc::mem_fun(*this, &SettingsFrame::onDeleteEvent)); - pulseHBox_->set_visible(false); + this->signal_delete_event().connect(sigc::mem_fun(*this, &SettingsFrame::onDeleteEvent)); + pulseHBox_->set_visible(false); } SettingsFrame::~SettingsFrame() { #ifdef HAVE_PULSE - delete pulseBox_; + delete pulseBox_; #endif - delete usePulse_; - delete pulseHBox_; - delete okButton_; - delete cancelButton_; - delete sndCardBox_; - delete mixerBox_; - delete playbackSwitchTree_; - delete captureSwitchTree_; - delete otherSwitchTree_; - delete isAutoRun_; - delete tabPos_; - delete tabWidget_; + delete usePulse_; + delete pulseHBox_; + delete okButton_; + delete cancelButton_; + delete sndCardBox_; + delete mixerBox_; + delete playbackSwitchTree_; + delete captureSwitchTree_; + delete otherSwitchTree_; + delete isAutoRun_; + delete tabPos_; + delete tabWidget_; } void SettingsFrame::initParms(const settingsStr::Ptr &str) { - settings_ = str; - if (tabPos_) { - tabPos_->set_active(settings_->notebookOrientation()); - } - if (isAutoRun_) { - isAutoRun_->set_active(settings_->isAutorun()); - } - if (usePolling_) { - usePolling_->set_active(settings_->usePolling()); - } + settings_ = str; + if (tabPos_) { + tabPos_->set_active(settings_->notebookOrientation()); + } + if (isAutoRun_) { + isAutoRun_->set_active(settings_->isAutorun()); + } + if (usePolling_) { + usePolling_->set_active(settings_->usePolling()); + } #ifdef HAVE_PULSE - if (usePulse_) { - usePulse_->set_active(settings_->usePulse()); - } - pulseDev_ = settings_->pulseDeviceId(); + if (usePulse_) { + usePulse_->set_active(settings_->usePulse()); + } + pulseDev_ = settings_->pulseDeviceId(); #endif - mixerId_ = settings_->mixerId(); - cardId_ = settings_->cardId(); - setupTreeModels(); + mixerId_ = settings_->mixerId(); + cardId_ = settings_->cardId(); + setupTreeModels(); } void SettingsFrame::setTabPos(bool orient) { - if (tabWidget_) { - if(orient) { - tabWidget_->set_tab_pos(Gtk::POS_TOP); - } - else { - tabWidget_->set_tab_pos(Gtk::POS_LEFT); - } - settings_->setNotebookOrientation(orient); - } + if (tabWidget_) { + if(orient) { + tabWidget_->set_tab_pos(Gtk::POS_TOP); + } + else { + tabWidget_->set_tab_pos(Gtk::POS_LEFT); + } + settings_->setNotebookOrientation(orient); + } } void SettingsFrame::onTabPos() { - setTabPos(tabPos_->get_active()); + setTabPos(tabPos_->get_active()); } void SettingsFrame::onOkButton() { - mixerId_ = (mixerId_ < 0) ? 0 : mixerId_; - settings_->setMixerId(mixerId_); - cardId_ = (cardId_ < 0) ? 0 : cardId_; - settings_->setCardId(cardId_); - response(OK_RESPONSE); - hide(); + mixerId_ = (mixerId_ < 0) ? 0 : mixerId_; + settings_->setMixerId(mixerId_); + cardId_ = (cardId_ < 0) ? 0 : cardId_; + settings_->setCardId(cardId_); + response(OK_RESPONSE); + hide(); } void SettingsFrame::onCancelButton() { - response(CANCEL_RESPONSE); - hide(); + response(CANCEL_RESPONSE); + hide(); } bool SettingsFrame::onDeleteEvent(GdkEventAny *event) { - if (event->type == GDK_DESTROY || event->type == GDK_DELETE) - onCancelButton(); - return true; + if (event->type == GDK_DESTROY || event->type == GDK_DELETE) + onCancelButton(); + return true; } settingsStr::Ptr SettingsFrame::getSettings() const { - return settings_; + return settings_; } void SettingsFrame::setupTreeModels() { - //treeview setup - setupSoundCards(); + //treeview setup + setupSoundCards(); #ifdef HAVE_PULSE - setupPulseDevices(); + setupPulseDevices(); #endif } void SettingsFrame::setupSoundCards() { - if (sndCardBox_ && settings_){ - const std::vector cards = settings_->cardList(); - sndCardBox_->clear(); - cards_ = Gtk::ListStore::create(m_Columns); - sndCardBox_->set_model(cards_); - Gtk::TreeModel::Row row; - uint i = 0; - std::for_each(cards.begin(), cards.end(), [&](const Glib::ustring &name){ - row = *(cards_->append()); - row[m_Columns.m_col_name] = name; - if (i == (uint)cardId_) { - sndCardBox_->set_active(row); - } - ++i; - }); - sndCardBox_->pack_start(m_Columns.m_col_name); - } + if (sndCardBox_ && settings_){ + sndCardBox_->clear(); + cards_ = Gtk::ListStore::create(m_Columns); + sndCardBox_->set_model(cards_); + Gtk::TreeModel::Row row; + uint i = 0; + for(const std::string &name : settings_->cardList()){ + row = *(cards_->append()); + row[m_Columns.m_col_name] = Glib::ustring(name); + if (i == (uint)cardId_) { + sndCardBox_->set_active(row); + } + ++i; + } + sndCardBox_->pack_start(m_Columns.m_col_name); + } } #ifdef HAVE_PULSE void SettingsFrame::setupPulseDevices() { - if(pulseBox_ && settings_) { - pulseBox_->clear(); - pulseCards_ = Gtk::ListStore::create(m_Columns); - pulseBox_->set_model(pulseCards_); - Gtk::TreeModel::Row row; - const std::vector cards(settings_->pulseDevices()); - uint i = 0; - std::for_each(cards.begin(), cards.end(), [&](const Glib::ustring &name){ - row = *(pulseCards_->append()); - row[m_Columns.m_col_name] = name; - if (i == (uint)pulseDev_) { - pulseBox_->set_active(row); - } - ++i; - }); - pulseBox_->pack_start(m_Columns.m_col_name); - } + if(pulseBox_ && settings_) { + pulseBox_->clear(); + pulseCards_ = Gtk::ListStore::create(m_Columns); + pulseBox_->set_model(pulseCards_); + Gtk::TreeModel::Row row; + uint i = 0; + for(const std::string &name : settings_->pulseDevices()){ + row = *(pulseCards_->append()); + row[m_Columns.m_col_name] = Glib::ustring(name); + if (i == (uint)pulseDev_) { + pulseBox_->set_active(row); + } + ++i; + } + pulseBox_->pack_start(m_Columns.m_col_name); + } } #endif void SettingsFrame::setupMixers() { - if (mixerBox_ && settings_) { - mixerBox_->clear(); - mixers_ = Gtk::ListStore::create(m_Columns); - mixerBox_->set_model(mixers_); - if (settings_->mixerList().size() > 0) { - Gtk::TreeModel::Row row; - const std::vector mixers = settings_->mixerList(); - uint i = 0; - std::for_each(mixers.begin(), mixers.end(), [&](const Glib::ustring &name){ - row = *(mixers_->append()); - row[m_Columns.m_col_name] = name; - if (i == (uint)mixerId_) { - mixerBox_->set_active(row); - } - ++i; - }); - mixerBox_->pack_start(m_Columns.m_col_name); - } - } + if (mixerBox_ && settings_) { + mixerBox_->clear(); + mixers_ = Gtk::ListStore::create(m_Columns); + mixerBox_->set_model(mixers_); + if (settings_->mixerList().size() > 0) { + Gtk::TreeModel::Row row; + uint i = 0; + for(const std::string &name : settings_->mixerList()) { + row = *(mixers_->append()); + row[m_Columns.m_col_name] = Glib::ustring(name); + if (i == (uint)mixerId_) { + mixerBox_->set_active(row); + } + ++i; + } + mixerBox_->pack_start(m_Columns.m_col_name); + } + } } void SettingsFrame::updateSwitchTree() { - if (!settings_) - return; - - if (playbackSwitchTree_) { - playbackSwitchTree_->remove_all_columns(); - Gtk::CellRendererToggle *pcell = Gtk::manage(new Gtk::CellRendererToggle); - pcell->set_activatable(true); - pcell->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onPlaybackCellToggled)); - if (pbSwitches_) - pbSwitches_->clear(); - pbSwitches_ = Glib::RefPtr(Gtk::ListStore::create(m_TColumns)); - playbackSwitchTree_->set_model(pbSwitches_); - Gtk::TreeModel::Row row; - const int colsCount = playbackSwitchTree_->append_column(STATUS, *pcell); - Gtk::TreeViewColumn* pColumn = playbackSwitchTree_->get_column(colsCount -1); - if (colsCount) { - pColumn->add_attribute(pcell->property_active(), m_TColumns.m_col_toggle); - } - const std::vector pbsl(settings_->switchList()->playbackSwitchList()); - std::for_each(pbsl.begin(), pbsl.end(), [&](const switchcap &scap){ - row = *(pbSwitches_->append()); - row[m_TColumns.m_col_toggle] = scap.second; - row[m_TColumns.m_col_name] = scap.first; - }); - playbackSwitchTree_->append_column(PB_SWITCH_NAME, m_TColumns.m_col_name); - playbackSwitchTree_->show_all_children(); - } - if (captureSwitchTree_) { - captureSwitchTree_->remove_all_columns(); - Gtk::CellRendererToggle *rcell = Gtk::manage(new Gtk::CellRendererToggle); - rcell->set_activatable(true); - rcell->set_radio(true); - rcell->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onCaptureCellToggled)); - if (capSwitches_) - capSwitches_->clear(); - capSwitches_ = Glib::RefPtr(Gtk::ListStore::create(m_TColumns)); - captureSwitchTree_->set_model(capSwitches_); - Gtk::TreeModel::Row row; - const int colsCount = captureSwitchTree_->append_column(STATUS, *rcell); - Gtk::TreeViewColumn* pColumn = captureSwitchTree_->get_column(colsCount -1); - if (colsCount) { - pColumn->add_attribute(rcell->property_active(), m_TColumns.m_col_toggle); - } - const std::vector ctsl(settings_->switchList()->captureSwitchList()); - std::for_each(ctsl.begin(), ctsl.end(), [&](const switchcap &scap){ - row = *(capSwitches_->append()); - row[m_TColumns.m_col_toggle] = scap.second; - row[m_TColumns.m_col_name] = scap.first; - }); - captureSwitchTree_->append_column(CP_SWITCH_NAME, m_TColumns.m_col_name); - captureSwitchTree_->show_all_children(); - } - if (otherSwitchTree_) { - otherSwitchTree_->remove_all_columns(); - Gtk::CellRendererToggle *ecell = Gtk::manage(new Gtk::CellRendererToggle); - ecell->set_activatable(true); - ecell->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onEnumCellToggled)); - if (enumSwitches_) - enumSwitches_->clear(); - enumSwitches_ = Glib::RefPtr(Gtk::ListStore::create(m_TColumns)); - otherSwitchTree_->set_model(enumSwitches_); - Gtk::TreeModel::Row row; - const int colsCount = otherSwitchTree_->append_column(STATUS, *ecell); - Gtk::TreeViewColumn* pColumn = otherSwitchTree_->get_column(colsCount -1); - if (colsCount) { - pColumn->add_attribute(ecell->property_active(), m_TColumns.m_col_toggle); - } - const std::vector ensl(settings_->switchList()->enumSwitchList()); - std::for_each(ensl.begin(), ensl.end(), [&](const switchcap &scap){ - row = *(enumSwitches_->append()); - row[m_TColumns.m_col_toggle] = scap.second; - row[m_TColumns.m_col_name] = scap.first; - }); - otherSwitchTree_->append_column(EN_SWITCH_NAME, m_TColumns.m_col_name); - otherSwitchTree_->show_all_children(); - } + if (!settings_) + return; + + if (playbackSwitchTree_) { + playbackSwitchTree_->remove_all_columns(); + Gtk::CellRendererToggle *pcell = Gtk::manage(new Gtk::CellRendererToggle); + pcell->set_activatable(true); + pcell->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onPlaybackCellToggled)); + if (pbSwitches_) + pbSwitches_->clear(); + pbSwitches_ = Glib::RefPtr(Gtk::ListStore::create(m_TColumns)); + playbackSwitchTree_->set_model(pbSwitches_); + Gtk::TreeModel::Row row; + const int colsCount = playbackSwitchTree_->append_column(STATUS, *pcell); + Gtk::TreeViewColumn* pColumn = playbackSwitchTree_->get_column(colsCount -1); + if (colsCount) { + pColumn->add_attribute(pcell->property_active(), m_TColumns.m_col_toggle); + } + for(const switchcap &scap : settings_->switchList()->playbackSwitchList()) { + row = *(pbSwitches_->append()); + row[m_TColumns.m_col_toggle] = scap.second; + row[m_TColumns.m_col_name] = scap.first; + } + playbackSwitchTree_->append_column(PB_SWITCH_NAME, m_TColumns.m_col_name); + playbackSwitchTree_->show_all_children(); + } + if (captureSwitchTree_) { + captureSwitchTree_->remove_all_columns(); + Gtk::CellRendererToggle *rcell = Gtk::manage(new Gtk::CellRendererToggle); + rcell->set_activatable(true); + rcell->set_radio(true); + rcell->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onCaptureCellToggled)); + if (capSwitches_) + capSwitches_->clear(); + capSwitches_ = Glib::RefPtr(Gtk::ListStore::create(m_TColumns)); + captureSwitchTree_->set_model(capSwitches_); + Gtk::TreeModel::Row row; + const int colsCount = captureSwitchTree_->append_column(STATUS, *rcell); + Gtk::TreeViewColumn* pColumn = captureSwitchTree_->get_column(colsCount -1); + if (colsCount) { + pColumn->add_attribute(rcell->property_active(), m_TColumns.m_col_toggle); + } + for (const switchcap &scap : settings_->switchList()->captureSwitchList()) { + row = *(capSwitches_->append()); + row[m_TColumns.m_col_toggle] = scap.second; + row[m_TColumns.m_col_name] = scap.first; + } + captureSwitchTree_->append_column(CP_SWITCH_NAME, m_TColumns.m_col_name); + captureSwitchTree_->show_all_children(); + } + if (otherSwitchTree_) { + otherSwitchTree_->remove_all_columns(); + Gtk::CellRendererToggle *ecell = Gtk::manage(new Gtk::CellRendererToggle); + ecell->set_activatable(true); + ecell->signal_toggled().connect(sigc::mem_fun(*this, &SettingsFrame::onEnumCellToggled)); + if (enumSwitches_) + enumSwitches_->clear(); + enumSwitches_ = Glib::RefPtr(Gtk::ListStore::create(m_TColumns)); + otherSwitchTree_->set_model(enumSwitches_); + Gtk::TreeModel::Row row; + const int colsCount = otherSwitchTree_->append_column(STATUS, *ecell); + Gtk::TreeViewColumn* pColumn = otherSwitchTree_->get_column(colsCount -1); + if (colsCount) { + pColumn->add_attribute(ecell->property_active(), m_TColumns.m_col_toggle); + } + for(const switchcap &scap : settings_->switchList()->enumSwitchList()) { + row = *(enumSwitches_->append()); + row[m_TColumns.m_col_toggle] = scap.second; + row[m_TColumns.m_col_name] = scap.first; + } + otherSwitchTree_->append_column(EN_SWITCH_NAME, m_TColumns.m_col_name); + otherSwitchTree_->show_all_children(); + } } void SettingsFrame::sndBoxChanged() { - cardId_ = sndCardBox_->get_active_row_number(); - settings_->setCardId(cardId_); - m_signal_sndcard_changed(cardId_); + cardId_ = sndCardBox_->get_active_row_number(); + settings_->setCardId(cardId_); + m_signal_sndcard_changed(cardId_); } #ifdef HAVE_PULSE void SettingsFrame::onPulseDeviceChanged() { - pulseDev_ = pulseBox_->get_active_row_number(); - settings_->setPulseDeviceId(pulseDev_); - m_signal_pulsedev_changed(pulseDev_); + pulseDev_ = pulseBox_->get_active_row_number(); + settings_->setPulseDeviceId(pulseDev_); + m_signal_pulsedev_changed(pulseDev_); } #endif void SettingsFrame::updateMixers(const std::vector &mixers) { - settings_->setList(MIXERS, mixers); - setupMixers(); + settings_->setList(MIXERS, mixers); + setupMixers(); } void SettingsFrame::updateSwitches(const MixerSwitches::Ptr &slist) { - //settings_->clearSwitches(); - settings_->addMixerSwitch(slist); - updateSwitchTree(); + //settings_->clearSwitches(); + settings_->addMixerSwitch(slist); + updateSwitchTree(); } void SettingsFrame::mixerBoxChanged() { - const int row = mixerBox_->get_active_row_number(); - if (mixerId_ != row && row >=0) { - mixerId_ = row; - } + const int row = mixerBox_->get_active_row_number(); + if (mixerId_ != row && row >=0) { + mixerId_ = row; + } } void SettingsFrame::onPlaybackCellToggled(const Glib::ustring& path) { - Gtk::TreeModel::iterator it = pbSwitches_->get_iter(path); - Gtk::TreeModel::Row row = *it; - if (bool(row.get_value(m_TColumns.m_col_toggle))) { - row[m_TColumns.m_col_toggle] = false; - } - else { - row[m_TColumns.m_col_toggle] = true; - } - m_type_toggled_signal(row.get_value(m_TColumns.m_col_name), - PLAYBACK, - bool(row.get_value(m_TColumns.m_col_toggle))); + Gtk::TreeModel::iterator it = pbSwitches_->get_iter(path); + Gtk::TreeModel::Row row = *it; + if (bool(row.get_value(m_TColumns.m_col_toggle))) { + row[m_TColumns.m_col_toggle] = false; + } + else { + row[m_TColumns.m_col_toggle] = true; + } + m_type_toggled_signal(row.get_value(m_TColumns.m_col_name), + PLAYBACK, + bool(row.get_value(m_TColumns.m_col_toggle))); } void SettingsFrame::onCaptureCellToggled(const Glib::ustring& path) { - Gtk::TreeModel::Row row; - std::for_each(capSwitches_->children().begin(), capSwitches_->children().end(), [&](Gtk::TreeModel::Row r){ - row = r; - row[m_TColumns.m_col_toggle] = false; - }); - Gtk::TreeModel::iterator iter = capSwitches_->get_iter(path); - row = *iter; - if (!bool(row.get_value(m_TColumns.m_col_toggle))) { - row[m_TColumns.m_col_toggle] = true; - } - m_type_toggled_signal(row.get_value(m_TColumns.m_col_name), - CAPTURE, - bool(row.get_value(m_TColumns.m_col_toggle))); + Gtk::TreeModel::Row row; + std::for_each(capSwitches_->children().begin(), capSwitches_->children().end(), [&](Gtk::TreeModel::Row r){ + row = r; + row[m_TColumns.m_col_toggle] = false; + }); + Gtk::TreeModel::iterator iter = capSwitches_->get_iter(path); + row = *iter; + if (!bool(row.get_value(m_TColumns.m_col_toggle))) { + row[m_TColumns.m_col_toggle] = true; + } + m_type_toggled_signal(row.get_value(m_TColumns.m_col_name), + CAPTURE, + bool(row.get_value(m_TColumns.m_col_toggle))); } void SettingsFrame::onEnumCellToggled(const Glib::ustring& path) { - Gtk::TreeModel::iterator it = enumSwitches_->get_iter(path); - Gtk::TreeModel::Row row = *it; - if (bool(row.get_value(m_TColumns.m_col_toggle))) { - row[m_TColumns.m_col_toggle] = false; - } - else { - row[m_TColumns.m_col_toggle] = true; - } - m_type_toggled_signal(row.get_value(m_TColumns.m_col_name), - ENUM, - bool(row.get_value(m_TColumns.m_col_toggle))); + Gtk::TreeModel::iterator it = enumSwitches_->get_iter(path); + Gtk::TreeModel::Row row = *it; + if (bool(row.get_value(m_TColumns.m_col_toggle))) { + row[m_TColumns.m_col_toggle] = false; + } + else { + row[m_TColumns.m_col_toggle] = true; + } + m_type_toggled_signal(row.get_value(m_TColumns.m_col_name), + ENUM, + bool(row.get_value(m_TColumns.m_col_toggle))); } void SettingsFrame::onAutorunToggled() { - settings_->setIsAutorun(isAutoRun_->get_active()); + settings_->setIsAutorun(isAutoRun_->get_active()); } #ifdef HAVE_PULSE void SettingsFrame::onPulseToggled() { - isPulse_ = usePulse_->get_active(); - alsaHBox_->set_visible(!isPulse_); - pulseHBox_->set_visible(isPulse_); - m_signal_pulse_toggled(isPulse_); + isPulse_ = usePulse_->get_active(); + alsaHBox_->set_visible(!isPulse_); + pulseHBox_->set_visible(isPulse_); + m_signal_pulse_toggled(isPulse_); } #endif void SettingsFrame::onUsePollingToggled() { - bool usePoll = usePolling_->get_active(); - settings_->setUsePolling(usePoll); + bool usePoll = usePolling_->get_active(); + settings_->setUsePolling(usePoll); } void SettingsFrame::disablePulseCheckButton() { - usePulse_->set_visible(false); + usePulse_->set_visible(false); } SettingsFrame::type_toggled_signal SettingsFrame::signal_switches_toggled() { - return m_type_toggled_signal; + return m_type_toggled_signal; } SettingsFrame::type_int_signal SettingsFrame::signal_sndcard_changed() { - return m_signal_sndcard_changed; + return m_signal_sndcard_changed; } #ifdef HAVE_PULSE SettingsFrame::type_int_signal SettingsFrame::signal_pulsedevices_changed() { - return m_signal_pulsedev_changed; + return m_signal_pulsedev_changed; } SettingsFrame::type_bool_signal SettingsFrame::signal_pulsdev_toggled() { - return m_signal_pulse_toggled; + return m_signal_pulse_toggled; } #endif diff --git a/gui/settingsframe.h b/gui/settingsframe.h index 729b180..1fcc718 100644 --- a/gui/settingsframe.h +++ b/gui/settingsframe.h @@ -1,6 +1,6 @@ /* * settingsframe.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,105 +34,105 @@ class SettingsFrame : public Gtk::Dialog { public: - SettingsFrame(BaseObjectType* cobject, - const Glib::RefPtr& refGlade); - ~SettingsFrame(); - enum RESPONCES { - OK_RESPONSE = 1, - CANCEL_RESPONSE = 0 - }; - void initParms(const settingsStr::Ptr &str); - void updateControls(const settingsStr::Ptr &str); - void updateMixers(const std::vector &mixers); - void updateSwitches(const MixerSwitches::Ptr &slist); - void disablePulseCheckButton(); - settingsStr::Ptr getSettings() const; - // - typedef sigc::signal type_toggled_signal; - type_toggled_signal signal_switches_toggled(); - typedef sigc::signal type_bool_signal; - typedef sigc::signal type_int_signal; - type_int_signal signal_sndcard_changed(); - type_int_signal signal_pulsedevices_changed(); - type_bool_signal signal_pulsdev_toggled(); + SettingsFrame(BaseObjectType* cobject, + const Glib::RefPtr& refGlade); + ~SettingsFrame(); + enum RESPONCES { + OK_RESPONSE = 1, + CANCEL_RESPONSE = 0 + }; + void initParms(const settingsStr::Ptr &str); + void updateControls(const settingsStr::Ptr &str); + void updateMixers(const std::vector &mixers); + void updateSwitches(const MixerSwitches::Ptr &slist); + void disablePulseCheckButton(); + settingsStr::Ptr getSettings() const; + // + typedef sigc::signal type_toggled_signal; + type_toggled_signal signal_switches_toggled(); + typedef sigc::signal type_bool_signal; + typedef sigc::signal type_int_signal; + type_int_signal signal_sndcard_changed(); + type_int_signal signal_pulsedevices_changed(); + type_bool_signal signal_pulsdev_toggled(); protected: - class ModelCheckBox : public Gtk::TreeModel::ColumnRecord - { - public: - inline ModelCheckBox() { add(m_col_name); } - Gtk::TreeModelColumn m_col_name; - }; - ModelCheckBox m_Columns; - class ModelTreeView : public Gtk::TreeModel::ColumnRecord - { - public: - inline ModelTreeView() { add(m_col_toggle); add(m_col_name); } - Gtk::TreeModelColumn m_col_toggle; - Gtk::TreeModelColumn m_col_name; - }; - ModelTreeView m_TColumns; - void onTabPos(); - void onOkButton(); - void onCancelButton(); - bool onDeleteEvent(GdkEventAny* event); - void sndBoxChanged(); - void mixerBoxChanged(); - void onPlaybackCellToggled(const Glib::ustring &path); - void onCaptureCellToggled(const Glib::ustring &path); - void onEnumCellToggled(const Glib::ustring &path); - void onAutorunToggled(); - void onUsePollingToggled(); + class ModelCheckBox : public Gtk::TreeModel::ColumnRecord + { + public: + inline ModelCheckBox() { add(m_col_name); } + Gtk::TreeModelColumn m_col_name; + }; + ModelCheckBox m_Columns; + class ModelTreeView : public Gtk::TreeModel::ColumnRecord + { + public: + inline ModelTreeView() { add(m_col_toggle); add(m_col_name); } + Gtk::TreeModelColumn m_col_toggle; + Gtk::TreeModelColumn m_col_name; + }; + ModelTreeView m_TColumns; + void onTabPos(); + void onOkButton(); + void onCancelButton(); + bool onDeleteEvent(GdkEventAny* event); + void sndBoxChanged(); + void mixerBoxChanged(); + void onPlaybackCellToggled(const Glib::ustring &path); + void onCaptureCellToggled(const Glib::ustring &path); + void onEnumCellToggled(const Glib::ustring &path); + void onAutorunToggled(); + void onUsePollingToggled(); #ifdef HAVE_PULSE - void onPulseToggled(); - void onPulseDeviceChanged(); + void onPulseToggled(); + void onPulseDeviceChanged(); #endif - //signals - type_toggled_signal m_type_toggled_signal; + //signals + type_toggled_signal m_type_toggled_signal; #ifdef HAVE_PULSE - type_bool_signal m_signal_pulse_toggled; - type_int_signal m_signal_pulsedev_changed; + type_bool_signal m_signal_pulse_toggled; + type_int_signal m_signal_pulsedev_changed; #endif - type_int_signal m_signal_sndcard_changed; + type_int_signal m_signal_sndcard_changed; private: - void setupTreeModels(); - void setupSoundCards(); + void setupTreeModels(); + void setupSoundCards(); #ifdef HAVE_PULSE - void setupPulseDevices(); + void setupPulseDevices(); #endif - void setupMixers(); - void updateSwitchTree(); - void setTabPos(bool orient); + void setupMixers(); + void updateSwitchTree(); + void setTabPos(bool orient); private: - Gtk::Button *okButton_; - Gtk::Button *cancelButton_; - Gtk::ComboBox *sndCardBox_; - Gtk::ComboBox *mixerBox_; - Gtk::TreeView *playbackSwitchTree_; - Gtk::TreeView *captureSwitchTree_; - Gtk::TreeView *otherSwitchTree_; - Gtk::CheckButton *isAutoRun_; - Gtk::CheckButton *tabPos_; - Gtk::Notebook *tabWidget_; - Gtk::Box *pulseHBox_; - Gtk::Box *alsaHBox_; - Gtk::CheckButton *usePulse_; - Gtk::CheckButton *usePolling_; + Gtk::Button *okButton_; + Gtk::Button *cancelButton_; + Gtk::ComboBox *sndCardBox_; + Gtk::ComboBox *mixerBox_; + Gtk::TreeView *playbackSwitchTree_; + Gtk::TreeView *captureSwitchTree_; + Gtk::TreeView *otherSwitchTree_; + Gtk::CheckButton *isAutoRun_; + Gtk::CheckButton *tabPos_; + Gtk::Notebook *tabWidget_; + Gtk::Box *pulseHBox_; + Gtk::Box *alsaHBox_; + Gtk::CheckButton *usePulse_; + Gtk::CheckButton *usePolling_; #ifdef HAVE_PULSE - Gtk::ComboBox *pulseBox_; - int pulseDev_; - Glib::RefPtr pulseCards_; + Gtk::ComboBox *pulseBox_; + int pulseDev_; + Glib::RefPtr pulseCards_; #endif - Glib::RefPtr cards_; - Glib::RefPtr mixers_; - Glib::RefPtr pbSwitches_; - Glib::RefPtr capSwitches_; - Glib::RefPtr enumSwitches_; - settingsStr::Ptr settings_; - int mixerId_; - int cardId_; - bool isPulse_; + Glib::RefPtr cards_; + Glib::RefPtr mixers_; + Glib::RefPtr pbSwitches_; + Glib::RefPtr capSwitches_; + Glib::RefPtr enumSwitches_; + settingsStr::Ptr settings_; + int mixerId_; + int cardId_; + bool isPulse_; }; #endif // SETTINGSFRAME_H diff --git a/gui/sliderwindow.cpp b/gui/sliderwindow.cpp index 62a9e47..da10741 100644 --- a/gui/sliderwindow.cpp +++ b/gui/sliderwindow.cpp @@ -1,6 +1,6 @@ /* * sliderwindow.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,98 +24,98 @@ #define SLIDER_MIN_WIDTH 20 SliderWindow::SliderWindow(BaseObjectType* cobject, const Glib::RefPtr& refGlade) -: Gtk::Window(cobject), - volumeSlider_(0) + : Gtk::Window(cobject), + volumeSlider_(nullptr) { - volumeValue_ = 0; - refGlade->get_widget("volume_slider", volumeSlider_); - if (volumeSlider_) { - volumeSlider_->signal_value_changed().connect(sigc::mem_fun(*this, &SliderWindow::on_volume_slider)); - set_default_size(volumeSlider_->get_width(), volumeSlider_->get_width()); - } - add_events(Gdk::LEAVE_NOTIFY_MASK); - signal_leave_notify_event().connect(sigc::mem_fun(*this, &SliderWindow::on_focus_out)); - set_border_width(0); + volumeValue_ = 0; + refGlade->get_widget("volume_slider", volumeSlider_); + if (volumeSlider_) { + volumeSlider_->signal_value_changed().connect(sigc::mem_fun(*this, &SliderWindow::on_volume_slider)); + set_default_size(volumeSlider_->get_width(), volumeSlider_->get_width()); + } + add_events(Gdk::LEAVE_NOTIFY_MASK); + signal_leave_notify_event().connect(sigc::mem_fun(*this, &SliderWindow::on_focus_out)); + set_border_width(0); - int sliderWidth = 0; + int sliderWidth = 0; #ifndef IS_GTK_2 - sliderWidth = volumeSlider_->get_allocated_width(); + sliderWidth = volumeSlider_->get_allocated_width(); #else - sliderWidth = volumeSlider_->get_width(); + sliderWidth = volumeSlider_->get_width(); #endif - if (sliderWidth < SLIDER_MIN_WIDTH) { - set_size_request(SLIDER_MIN_WIDTH, SLIDER_HEIGHT); - } - set_keep_above(true); + if (sliderWidth < SLIDER_MIN_WIDTH) { + set_size_request(SLIDER_MIN_WIDTH, SLIDER_HEIGHT); + } + set_keep_above(true); } SliderWindow::~SliderWindow() { - delete volumeSlider_; + delete volumeSlider_; } void SliderWindow::setWindowPosition(const iconPosition &pos) { - if (!get_visible()) { + if (!get_visible()) { #ifndef IS_GTK_2 - const int wWidth = volumeSlider_->get_allocated_width(); - const int wHeight = volumeSlider_->get_allocated_height(); + const int wWidth = volumeSlider_->get_allocated_width(); + const int wHeight = volumeSlider_->get_allocated_height(); #else - const int wWidth = volumeSlider_->get_width(); - const int wHeight = SLIDER_HEIGHT; + const int wWidth = volumeSlider_->get_width(); + const int wHeight = SLIDER_HEIGHT; #endif #ifdef IS_DEBUG - std::cout << "Screen height = " << pos.screenHeight_ << std::endl; - std::cout << "At top = " << pos.trayAtTop_ << std::endl; - std::cout << "wHeight = " << wHeight << std::endl; - std::cout << "iconHeight = " << pos.iconHeight_ << std::endl; + std::cout << "Screen height = " << pos.screenHeight_ << std::endl; + std::cout << "At top = " << pos.trayAtTop_ << std::endl; + std::cout << "wHeight = " << wHeight << std::endl; + std::cout << "iconHeight = " << pos.iconHeight_ << std::endl; #endif - const int wY = pos.trayAtTop_ ? pos.iconHeight_ + 4 : pos.screenHeight_ - wHeight - pos.iconHeight_ - 4; - int wX; - if (pos.geometryAvailable_) { + const int wY = pos.trayAtTop_ ? pos.iconHeight_ + 4 : pos.screenHeight_ - wHeight - pos.iconHeight_ - 4; + int wX; + if (pos.geometryAvailable_) { #ifdef IS_DEBUG - std::cout << "Geometry available" << std::endl; - std::cout << "wY = " << wY << std::endl; + std::cout << "Geometry available" << std::endl; + std::cout << "wY = " << wY << std::endl; #endif - wX = (wWidth > 1) ? pos.iconX_ - (wWidth/2 - pos.iconWidth_/2) : pos.iconX_ - pos.iconWidth_/2; - } - else{ - wX = (wWidth > 1) ? pos.iconX_ - wWidth/2 : pos.iconX_ - pos.iconWidth_; - } - show_all(); - this->move(wX, wY); - } - else { - hide(); - } + wX = (wWidth > 1) ? pos.iconX_ - (wWidth/2 - pos.iconWidth_/2) : pos.iconX_ - pos.iconWidth_/2; + } + else{ + wX = (wWidth > 1) ? pos.iconX_ - wWidth/2 : pos.iconX_ - pos.iconWidth_; + } + show_all(); + this->move(wX, wY); + } + else { + hide(); + } } void SliderWindow::on_volume_slider() { - volumeValue_ = volumeSlider_->get_value(); - m_signal_volume_changed(volumeValue_); + volumeValue_ = volumeSlider_->get_value(); + m_signal_volume_changed(volumeValue_); } bool SliderWindow::on_focus_out(GdkEventCrossing* event) { - if ((event->type == GDK_LEAVE_NOTIFY) - && (event->x < 0 - ||event->x >= get_width() - ||event->y < 0 - ||event->y >= get_height())){ - hide(); - } - return false; + if ((event->type == GDK_LEAVE_NOTIFY) + && (event->x < 0 + ||event->x >= get_width() + ||event->y < 0 + ||event->y >= get_height())){ + hide(); + } + return false; } void SliderWindow::setVolumeValue(double value) { - volumeValue_ = value; - volumeSlider_->set_value(value); + volumeValue_ = value; + volumeSlider_->set_value(value); } SliderWindow::type_sliderwindow_signal SliderWindow::signal_volume_changed() { - return m_signal_volume_changed; + return m_signal_volume_changed; } diff --git a/gui/sliderwindow.h b/gui/sliderwindow.h index ce463e1..b4e5a2e 100644 --- a/gui/sliderwindow.h +++ b/gui/sliderwindow.h @@ -1,6 +1,6 @@ /* * sliderwindow.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -29,22 +29,22 @@ class SliderWindow : public Gtk::Window { public: - SliderWindow(BaseObjectType *cobject, const Glib::RefPtr&refGlade); - ~SliderWindow(); - void setWindowPosition(const iconPosition& pos); - void setVolumeValue(double value); - //signal - typedef sigc::signal type_sliderwindow_signal; - type_sliderwindow_signal signal_volume_changed(); + SliderWindow(BaseObjectType *cobject, const Glib::RefPtr&refGlade); + ~SliderWindow(); + void setWindowPosition(const iconPosition& pos); + void setVolumeValue(double value); + //signal + typedef sigc::signal type_sliderwindow_signal; + type_sliderwindow_signal signal_volume_changed(); private: - void on_volume_slider(); - bool on_focus_out(GdkEventCrossing* event); + void on_volume_slider(); + bool on_focus_out(GdkEventCrossing* event); protected: - type_sliderwindow_signal m_signal_volume_changed; + type_sliderwindow_signal m_signal_volume_changed; private: - Gtk::Scale *volumeSlider_; - double volumeValue_; + Gtk::Scale *volumeSlider_; + double volumeValue_; }; #endif // SLIDERWINDOW_H diff --git a/gui/trayicon.cpp b/gui/trayicon.cpp index 7b7bb1d..aaccd74 100644 --- a/gui/trayicon.cpp +++ b/gui/trayicon.cpp @@ -1,6 +1,6 @@ /* * trayicon.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -47,452 +47,449 @@ static const int OFFSET = 2; TrayIcon::TrayIcon(double volume, const std::string &cardName, const std::string &mixerName, bool muted) -: volumeValue_(volume), - cardName_(cardName), - mixerName_(mixerName), - muted_(muted), - menu_(Gtk::manage(new Gtk::Menu())), - restoreItem_(Gtk::manage(new Gtk::MenuItem(RESTOREITEM))), - settingsItem_(Gtk::manage(new Gtk::MenuItem(SETTSITEM))), - aboutItem_(Gtk::manage(new Gtk::MenuItem(ABOUTITEM))), - quitItem_(Gtk::manage(new Gtk::MenuItem(QUITITEM))), - muteItem_(Gtk::manage(new Gtk::CheckMenuItem(MUTEITEM))), - mouseX_(0), - mouseY_(0), - pixbufWidth_(0), - pixbufHeight_(0), - isLegacyIcon_(true), -#if defined(USE_APPINDICATOR) || defined(USE_KDE) - newIcon_(0), -#endif - legacyIcon_(0) + : volumeValue_(volume), + cardName_(cardName), + mixerName_(mixerName), + muted_(muted), + menu_(Gtk::manage(new Gtk::Menu())), + restoreItem_(Gtk::manage(new Gtk::MenuItem(RESTOREITEM))), + settingsItem_(Gtk::manage(new Gtk::MenuItem(SETTSITEM))), + aboutItem_(Gtk::manage(new Gtk::MenuItem(ABOUTITEM))), + quitItem_(Gtk::manage(new Gtk::MenuItem(QUITITEM))), + muteItem_(Gtk::manage(new Gtk::CheckMenuItem(MUTEITEM))), + mouseX_(0), + mouseY_(0), + pixbufWidth_(0), + pixbufHeight_(0), + isLegacyIcon_(true), + #if defined(USE_APPINDICATOR) || defined(USE_KDE) + newIcon_(nullptr), + #endif + legacyIcon_(nullptr) { - const Glib::ustring searchPath = Glib::ustring("icons/") + getIconName(100); - const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); + const Glib::ustring searchPath = Glib::ustring("icons/") + getIconName(100); + const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); #if defined(USE_APPINDICATOR) - newIcon_ = StatusNotifierPtr(app_indicator_new("AlsaVolume", - iconPath.c_str(), - APP_INDICATOR_CATEGORY_APPLICATION_STATUS)); - if (newIcon_) { - isLegacyIcon_ = false; - } - app_indicator_set_status(newIcon_.get(), APP_INDICATOR_STATUS_ACTIVE); - app_indicator_set_menu(newIcon_.get(), menu_->gobj()); - app_indicator_set_secondary_activate_target(newIcon_.get(), GTK_WIDGET(muteItem_->gobj())); - g_signal_connect(newIcon_.get(), "scroll-event", (GCallback)TrayIcon::onScrollEventAI, this); + newIcon_ = StatusNotifierPtr(app_indicator_new("AlsaVolume", + iconPath.c_str(), + APP_INDICATOR_CATEGORY_APPLICATION_STATUS)); + if (newIcon_) { + isLegacyIcon_ = false; + } + app_indicator_set_status(newIcon_.get(), APP_INDICATOR_STATUS_ACTIVE); + app_indicator_set_menu(newIcon_.get(), menu_->gobj()); + app_indicator_set_secondary_activate_target(newIcon_.get(), GTK_WIDGET(muteItem_->gobj())); + g_signal_connect(newIcon_.get(), "scroll-event", (GCallback)TrayIcon::onScrollEventAI, this); #elif defined(USE_KDE) - if (checkDBusInterfaceExists("org.kde.StatusNotifierWatcher")) { - newIcon_ = StatusNotifierPtr(status_notifier_item_new_from_icon_name("AlsaVolume", - STATUS_NOTIFIER_CATEGORY_APPLICATION_STATUS, - iconPath.c_str())); - if ( newIcon_ ) { - status_notifier_item_set_status(newIcon_.get(), STATUS_NOTIFIER_STATUS_ACTIVE); - status_notifier_item_set_title(newIcon_.get(), "AlsaVolume"); - status_notifier_item_register(newIcon_.get()); - status_notifier_item_set_window_id(newIcon_.get(),0); - StatusNotifierState state = status_notifier_item_get_state(newIcon_.get()); + if (checkDBusInterfaceExists("org.kde.StatusNotifierWatcher")) { + newIcon_ = StatusNotifierPtr(status_notifier_item_new_from_icon_name("AlsaVolume", + STATUS_NOTIFIER_CATEGORY_APPLICATION_STATUS, + iconPath.c_str())); + if ( newIcon_ ) { + status_notifier_item_set_status(newIcon_.get(), STATUS_NOTIFIER_STATUS_ACTIVE); + status_notifier_item_set_title(newIcon_.get(), "AlsaVolume"); + status_notifier_item_register(newIcon_.get()); + status_notifier_item_set_window_id(newIcon_.get(),0); + StatusNotifierState state = status_notifier_item_get_state(newIcon_.get()); #ifdef IS_DEBUG - std::cout << "StatusNotifier state " << state << std::endl; + std::cout << "StatusNotifier state " << state << std::endl; #endif - g_signal_connect(newIcon_.get(), "registration-failed", (GCallback)TrayIcon::onRegisterError, this); - if ( state != STATUS_NOTIFIER_STATE_NOT_REGISTERED && state != STATUS_NOTIFIER_STATE_FAILED ) { - isLegacyIcon_ = false; + g_signal_connect(newIcon_.get(), "registration-failed", (GCallback)TrayIcon::onRegisterError, this); + if ( state != STATUS_NOTIFIER_STATE_NOT_REGISTERED && state != STATUS_NOTIFIER_STATE_FAILED ) { + isLegacyIcon_ = false; #ifdef IS_DEBUG - std::cout << "New Icon created" << std::endl; + std::cout << "New Icon created" << std::endl; #endif - g_signal_connect(newIcon_.get(), "activate", (GCallback)TrayIcon::onActivate, this); - g_signal_connect(newIcon_.get(), "context-menu", (GCallback)TrayIcon::onContextMenu, this); - g_signal_connect(newIcon_.get(), "secondary-activate", (GCallback)TrayIcon::onSecondaryActivate, this); - g_signal_connect(newIcon_.get(), "scroll", (GCallback)TrayIcon::onScroll, this); - } - } - } + g_signal_connect(newIcon_.get(), "activate", (GCallback)TrayIcon::onActivate, this); + g_signal_connect(newIcon_.get(), "context-menu", (GCallback)TrayIcon::onContextMenu, this); + g_signal_connect(newIcon_.get(), "secondary-activate", (GCallback)TrayIcon::onSecondaryActivate, this); + g_signal_connect(newIcon_.get(), "scroll", (GCallback)TrayIcon::onScroll, this); + } + } + } #endif - if(isLegacyIcon_) { - legacyIcon_ = Gtk::StatusIcon::create(iconPath); - //Staus icon signals - legacyIcon_->signal_popup_menu().connect(sigc::mem_fun(*this, &TrayIcon::onPopup)); - legacyIcon_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onHideRestore)); - legacyIcon_->signal_scroll_event().connect(sigc::mem_fun(*this, &TrayIcon::onScrollEvent)); - legacyIcon_->signal_button_press_event().connect(sigc::mem_fun(*this, &TrayIcon::onButtonClick)); - // - } - Gtk::SeparatorMenuItem *separator2 = Gtk::manage(new Gtk::SeparatorMenuItem()); - settingsItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::runSettings)); + if(isLegacyIcon_) { + legacyIcon_ = Gtk::StatusIcon::create(iconPath); + //Staus icon signals + legacyIcon_->signal_popup_menu().connect(sigc::mem_fun(*this, &TrayIcon::onPopup)); + legacyIcon_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onHideRestore)); + legacyIcon_->signal_scroll_event().connect(sigc::mem_fun(*this, &TrayIcon::onScrollEvent)); + legacyIcon_->signal_button_press_event().connect(sigc::mem_fun(*this, &TrayIcon::onButtonClick)); + // + } + Gtk::SeparatorMenuItem *separator2 = Gtk::manage(new Gtk::SeparatorMenuItem()); + settingsItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::runSettings)); #ifdef USE_APPINDICATOR - if (!isLegacyIcon_) { - restoreItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onHideRestore)); - menu_->append(*Gtk::manage(restoreItem_)); - } + if (!isLegacyIcon_) { + restoreItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onHideRestore)); + menu_->append(*Gtk::manage(restoreItem_)); + } #endif - menu_->append(*Gtk::manage(settingsItem_)); - muteItem_->signal_toggled().connect(sigc::mem_fun(*this, &TrayIcon::onMute)); - menu_->append(*Gtk::manage(muteItem_)); - aboutItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onAbout)); - menu_->append(*Gtk::manage(aboutItem_)); - menu_->append(*Gtk::manage(separator2)); - quitItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onQuit)); - menu_->append(*Gtk::manage(quitItem_)); - menu_->show_all_children(); - on_signal_volume_changed(volumeValue_, cardName, mixerName); - muteItem_->set_active(muted_); + menu_->append(*Gtk::manage(settingsItem_)); + muteItem_->signal_toggled().connect(sigc::mem_fun(*this, &TrayIcon::onMute)); + menu_->append(*Gtk::manage(muteItem_)); + aboutItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onAbout)); + menu_->append(*Gtk::manage(aboutItem_)); + menu_->append(*Gtk::manage(separator2)); + quitItem_->signal_activate().connect(sigc::mem_fun(*this, &TrayIcon::onQuit)); + menu_->append(*Gtk::manage(quitItem_)); + menu_->show_all_children(); + on_signal_volume_changed(volumeValue_, cardName, mixerName); + muteItem_->set_active(muted_); } #ifdef USE_APPINDICATOR void TrayIcon::onScrollEventAI(AppIndicator *ai, gint steps, gint direction, TrayIcon *userdata) { - (void) ai; - (void) steps; - double value = 0.0; - if (direction == GDK_SCROLL_UP) { - value+=OFFSET; - } - else if(direction == GDK_SCROLL_DOWN) { - value-=OFFSET; - } - userdata->m_signal_value_changed(value); + (void) ai; + (void) steps; + double value = 0.0; + if (direction == GDK_SCROLL_UP) { + value+=OFFSET; + } + else if(direction == GDK_SCROLL_DOWN) { + value-=OFFSET; + } + userdata->m_signal_value_changed(value); } #endif void TrayIcon::onHideRestore() { - Glib::RefPtr screen; - Gdk::Rectangle area; - Gtk::Orientation orientation; - iconPosition pos; - if (isLegacyIcon_) { - if (legacyIcon_->get_geometry(screen, area, orientation)) { - pos.iconX_ = area.get_x(); - pos.iconY_ = area.get_y(); - const int areaHeight = area.get_height(); - const int areaWidth = area.get_width(); - pos.iconHeight_ = (areaHeight > 0) ? areaHeight : pixbufHeight_; - pos.iconWidth_ = (areaWidth > 0) ? areaWidth : pixbufWidth_; - pos.screenHeight_ = screen->get_height(); - pos.screenWidth_ = screen->get_width(); - pos.geometryAvailable_ = bool(pos.iconX_ > 0 || pos.iconY_ > 0); - if (!pos.geometryAvailable_) { - pos.iconX_ = mouseX_; - pos.iconY_ = mouseY_; - } - pos.trayAtTop_ = bool(pos.iconY_ < pos.screenHeight_/2); - m_signal_on_restore(pos); - } - } + Glib::RefPtr screen; + Gdk::Rectangle area; + Gtk::Orientation orientation; + iconPosition pos; + if (isLegacyIcon_) { + if (legacyIcon_->get_geometry(screen, area, orientation)) { + pos.iconX_ = area.get_x(); + pos.iconY_ = area.get_y(); + const int areaHeight = area.get_height(); + const int areaWidth = area.get_width(); + pos.iconHeight_ = (areaHeight > 0) ? areaHeight : pixbufHeight_; + pos.iconWidth_ = (areaWidth > 0) ? areaWidth : pixbufWidth_; + pos.screenHeight_ = screen->get_height(); + pos.screenWidth_ = screen->get_width(); + pos.geometryAvailable_ = bool(pos.iconX_ > 0 || pos.iconY_ > 0); + if (!pos.geometryAvailable_) { + pos.iconX_ = mouseX_; + pos.iconY_ = mouseY_; + } + pos.trayAtTop_ = bool(pos.iconY_ < pos.screenHeight_/2); + m_signal_on_restore(pos); + } + } #ifdef USE_APPINDICATOR - else { - screen = restoreItem_->get_screen(); -//Dirty hack to obtain mouse position + else { + screen = restoreItem_->get_screen(); + //Dirty hack to obtain mouse position #ifdef IS_GTK_2 - Glib::RefPtr display = restoreItem_->get_display(); - Gdk::ModifierType type; - display->get_pointer(pos.iconX_, pos.iconY_, type); + Glib::RefPtr display = restoreItem_->get_display(); + Gdk::ModifierType type; + display->get_pointer(pos.iconX_, pos.iconY_, type); #else - Glib::RefPtr display = restoreItem_->get_display(); - Glib::RefPtr manager = display->get_device_manager(); - std::vector >list = manager->list_devices(Gdk::DEVICE_TYPE_MASTER); - std::for_each(list.begin(), - list.end(), - [&](Glib::RefPtr item){ - item->get_position(pos.iconX_, pos.iconY_); - }); + Glib::RefPtr display = restoreItem_->get_display(); + Glib::RefPtr manager = display->get_device_manager(); + for(Glib::RefPtr item : manager->list_devices(Gdk::DEVICE_TYPE_MASTER)) { + item->get_position(pos.iconX_, pos.iconY_); + } #ifdef IS_DEBUG - std::cout << "X=" << pos.iconX_ << " Y=" << pos.iconY_ << std::endl; + std::cout << "X=" << pos.iconX_ << " Y=" << pos.iconY_ << std::endl; #endif #endif -// - pos.screenHeight_ = screen->get_height(); - pos.screenWidth_ = screen->get_width(); - pos.trayAtTop_ = bool(pos.iconY_ < pos.screenHeight_/2); - pos.iconHeight_ = pixbufHeight_; - pos.iconWidth_ = pixbufWidth_; - pos.geometryAvailable_ = false; - m_signal_on_restore(pos); - } + // + pos.screenHeight_ = screen->get_height(); + pos.screenWidth_ = screen->get_width(); + pos.trayAtTop_ = bool(pos.iconY_ < pos.screenHeight_/2); + pos.iconHeight_ = pixbufHeight_; + pos.iconWidth_ = pixbufWidth_; + pos.geometryAvailable_ = false; + m_signal_on_restore(pos); + } #endif } #ifdef USE_KDE void TrayIcon::onActivate(StatusNotifierItem *sn, gint x, gint y, TrayIcon *userdata) { - (void)sn; - Glib::RefPtr screen = userdata->aboutItem_->get_screen(); - iconPosition pos; - pos.iconX_ = x + userdata->pixbufWidth_/2; - pos.iconY_ = y; - pos.screenHeight_ = screen->get_height(); - pos.screenWidth_ = screen->get_width(); - pos.trayAtTop_ = bool(pos.iconY_ < pos.screenHeight_/2); - pos.iconHeight_ = userdata->pixbufHeight_; - pos.iconWidth_ = userdata->pixbufWidth_; - pos.geometryAvailable_ = false; - userdata->m_signal_on_restore(pos); + (void)sn; + Glib::RefPtr screen = userdata->aboutItem_->get_screen(); + iconPosition pos; + pos.iconX_ = x + userdata->pixbufWidth_/2; + pos.iconY_ = y; + pos.screenHeight_ = screen->get_height(); + pos.screenWidth_ = screen->get_width(); + pos.trayAtTop_ = bool(pos.iconY_ < pos.screenHeight_/2); + pos.iconHeight_ = userdata->pixbufHeight_; + pos.iconWidth_ = userdata->pixbufWidth_; + pos.geometryAvailable_ = false; + userdata->m_signal_on_restore(pos); } void TrayIcon::onContextMenu(StatusNotifierItem *sn, gint x, gint y, TrayIcon *userdata) { - (void)sn; - (void)x; - (void)y; - userdata->menu_->popup(0, gtk_get_current_event_time()); + (void)sn; + (void)x; + (void)y; + userdata->menu_->popup(0, gtk_get_current_event_time()); } void TrayIcon::onSecondaryActivate(StatusNotifierItem *sn, gint x, gint y, TrayIcon *userdata) { - (void)sn; - (void)x; - (void)y; - userdata->muteItem_->set_active(!userdata->muteItem_->get_active()); + (void)sn; + (void)x; + (void)y; + userdata->muteItem_->set_active(!userdata->muteItem_->get_active()); } void TrayIcon::onScroll(StatusNotifierItem *sn, gint delta, StatusNotifierScrollOrientation orient, TrayIcon *userdata) { - (void)sn; - (void)orient; - double value = 0.0; - if (delta >0) { - value+=OFFSET; - } - else { - value-=OFFSET; - } + (void)sn; + (void)orient; + double value = 0.0; + if (delta >0) { + value+=OFFSET; + } + else { + value-=OFFSET; + } #ifdef IS_DEBUG - std::cout << "Value " << value << std::endl; + std::cout << "Value " << value << std::endl; #endif - userdata->m_signal_value_changed(value); + userdata->m_signal_value_changed(value); } bool TrayIcon::checkDBusInterfaceExists(const Glib::ustring &serviceName) { - bool isExists_; - const std::string DBUS_SERVICE = "org.freedesktop.DBus"; - Glib::RefPtr proxy = Gio::DBus::Proxy::create_for_bus_sync(Gio::DBus::BUS_TYPE_SESSION, - DBUS_SERVICE, - "/", - DBUS_SERVICE); - Glib::VariantContainerBase result = proxy->call_sync("ListNames"); - Glib::Variant > gvar = Glib::VariantBase::cast_dynamic< - Glib::Variant > - >(result.get_child(0)); - std::vector interfaces = gvar.get(); - isExists_ = Tools::itemExists(interfaces, serviceName); + bool isExists_; + const std::string DBUS_SERVICE = "org.freedesktop.DBus"; + Glib::RefPtr proxy = Gio::DBus::Proxy::create_for_bus_sync(Gio::DBus::BUS_TYPE_SESSION, + DBUS_SERVICE, + "/", + DBUS_SERVICE); + Glib::VariantContainerBase result = proxy->call_sync("ListNames"); + Glib::Variant > gvar = Glib::VariantBase::cast_dynamic< + Glib::Variant > + >(result.get_child(0)); + std::vector interfaces = gvar.get(); + isExists_ = Tools::itemExists(interfaces, serviceName); #ifdef IS_DEBUG - std::cout << "Interface exists " << isExists_ << std::endl; + std::cout << "Interface exists " << isExists_ << std::endl; #endif - return isExists_; + return isExists_; } void TrayIcon::onRegisterError(StatusNotifierItem *sn, GError *error, TrayIcon *userdata) { - (void)sn; - std::cerr << error->code << " "<< error->message << std::endl; - if(userdata->isLegacyIcon_) { - const Glib::ustring searchPath = Glib::ustring("icons/") + userdata->getIconName(100); - const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); - userdata->legacyIcon_ = Gtk::StatusIcon::create(iconPath); - //Staus icon signals - userdata->legacyIcon_->signal_popup_menu().connect(sigc::mem_fun(*userdata, &TrayIcon::onPopup)); - userdata->legacyIcon_->signal_activate().connect(sigc::mem_fun(*userdata, &TrayIcon::onHideRestore)); - userdata->legacyIcon_->signal_scroll_event().connect(sigc::mem_fun(*userdata, &TrayIcon::onScrollEvent)); - userdata->legacyIcon_->signal_button_press_event().connect(sigc::mem_fun(*userdata, &TrayIcon::onButtonClick)); - // - } + (void)sn; + std::cerr << error->code << " "<< error->message << std::endl; + if(userdata->isLegacyIcon_) { + const Glib::ustring searchPath = Glib::ustring("icons/") + userdata->getIconName(100); + const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); + userdata->legacyIcon_ = Gtk::StatusIcon::create(iconPath); + //Staus icon signals + userdata->legacyIcon_->signal_popup_menu().connect(sigc::mem_fun(*userdata, &TrayIcon::onPopup)); + userdata->legacyIcon_->signal_activate().connect(sigc::mem_fun(*userdata, &TrayIcon::onHideRestore)); + userdata->legacyIcon_->signal_scroll_event().connect(sigc::mem_fun(*userdata, &TrayIcon::onScrollEvent)); + userdata->legacyIcon_->signal_button_press_event().connect(sigc::mem_fun(*userdata, &TrayIcon::onButtonClick)); + // + } } #endif void TrayIcon::onQuit() { - m_signal_save_settings(); - exit(0); + m_signal_save_settings(); + exit(0); } void TrayIcon::runSettings() { - m_signal_ask_settings(); + m_signal_ask_settings(); } void TrayIcon::onAbout() { - m_signal_ask_dialog(); + m_signal_ask_dialog(); } void TrayIcon::onMute() { - muted_ = muteItem_->get_active(); - if (muted_) { - setIcon(0); - } - else { - setIcon(volumeValue_); - } - m_signal_on_mute(!muted_); + muted_ = muteItem_->get_active(); + if (muted_) { + setIcon(0); + } + else { + setIcon(volumeValue_); + } + m_signal_on_mute(!muted_); } Glib::ustring TrayIcon::getIconName(double value) const { - const std::string ICON_PREFIX = "tb_icon"; - int number = 100; - Glib::ustring iconPath = Glib::ustring::compose("%1%2.png",ICON_PREFIX, Glib::ustring::format(number)); - value = (value <= 0) ? 0 : (value > 100) ? 100 : value; - number = (value < 20) ? 20 : floor(value/20+0.5)*20; - if (value <= 0 || muted_) { - number = 0; - } - iconPath = Glib::ustring::compose("%1%2.png",ICON_PREFIX, Glib::ustring::format(number)); + const std::string ICON_PREFIX = "tb_icon"; + int number = 100; + Glib::ustring iconPath = Glib::ustring::compose("%1%2.png",ICON_PREFIX, Glib::ustring::format(number)); + value = (value <= 0) ? 0 : (value > 100) ? 100 : value; + number = (value < 20) ? 20 : floor(value/20+0.5)*20; + if (value <= 0 || muted_) { + number = 0; + } + iconPath = Glib::ustring::compose("%1%2.png",ICON_PREFIX, Glib::ustring::format(number)); #ifdef IS_DEBUG - std::cout << iconPath << std::endl; + std::cout << iconPath << std::endl; #endif - return iconPath; + return iconPath; } void TrayIcon::setIcon(double value) { - const Glib::ustring searchPath = Glib::ustring("icons/") + getIconName(value); - const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); - if (!iconPath.empty()) { - const Glib::RefPtr pixbuf = Glib::RefPtr(Gdk::Pixbuf::create_from_file(iconPath)); - pixbufWidth_ = pixbuf->get_width(); - pixbufHeight_ = pixbuf->get_height() + 4; - try { - if(isLegacyIcon_) { - legacyIcon_->set(pixbuf); - } + const Glib::ustring searchPath = Glib::ustring("icons/") + getIconName(value); + const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); + if (!iconPath.empty()) { + const Glib::RefPtr pixbuf = Glib::RefPtr(Gdk::Pixbuf::create_from_file(iconPath)); + pixbufWidth_ = pixbuf->get_width(); + pixbufHeight_ = pixbuf->get_height() + 4; + try { + if(isLegacyIcon_) { + legacyIcon_->set(pixbuf); + } #if defined (USE_APPINDICATOR) - else { - app_indicator_set_icon_full(newIcon_.get(), iconPath.c_str(), "VolumeIcon"); - } + else { + app_indicator_set_icon_full(newIcon_.get(), iconPath.c_str(), "VolumeIcon"); + } #elif defined(USE_KDE) - else { - status_notifier_item_set_from_icon_name(newIcon_.get(), STATUS_NOTIFIER_ICON, iconPath.c_str()); - } + else { + status_notifier_item_set_from_icon_name(newIcon_.get(), STATUS_NOTIFIER_ICON, iconPath.c_str()); + } #endif - } - catch (Glib::FileError &err) { - std::cerr << "FileError::trayicon.cpp::157:: " << err.what() << std::endl; - exit(1); - } - } + } + catch (Glib::FileError &err) { + std::cerr << "FileError::trayicon.cpp::157:: " << err.what() << std::endl; + exit(1); + } + } } void TrayIcon::setTooltip(const Glib::ustring &message) { - if (!message.empty()) - if (isLegacyIcon_) { - legacyIcon_->set_tooltip_text(message); - } + if (!message.empty()) + if (isLegacyIcon_) { + legacyIcon_->set_tooltip_text(message); + } #if defined(USE_APPINDICATOR) - else { - app_indicator_set_title(newIcon_.get(), message.c_str()); - } + else { + app_indicator_set_title(newIcon_.get(), message.c_str()); + } #elif defined(USE_KDE) - else { - const Glib::ustring searchPath = Glib::ustring("icons/") + getIconName(volumeValue_); - const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); - status_notifier_item_set_tooltip(newIcon_.get(), iconPath.c_str(), "AlsaVolume" ,message.c_str()); - } + else { + const Glib::ustring searchPath = Glib::ustring("icons/") + getIconName(volumeValue_); + const Glib::ustring iconPath = Tools::getResPath(searchPath.c_str()); + status_notifier_item_set_tooltip(newIcon_.get(), iconPath.c_str(), "AlsaVolume" ,message.c_str()); + } #endif } void TrayIcon::setMuted(bool isit) { - if (isit) { - setIcon(0); - } - muteItem_->set_active(isit); - muted_ = isit; + if (isit) { + setIcon(0); + } + muteItem_->set_active(isit); + muted_ = isit; } void TrayIcon::onPopup(guint button, guint32 activate_time) { - legacyIcon_->popup_menu_at_position(*menu_, button, activate_time); + legacyIcon_->popup_menu_at_position(*menu_, button, activate_time); } bool TrayIcon::onScrollEvent(GdkEventScroll* event) { - double value = 0.0; - if (event->direction == GDK_SCROLL_UP) { - value+=OFFSET; - } - else if(event->direction == GDK_SCROLL_DOWN) { - value-=OFFSET; - } - m_signal_value_changed(value); - return false; + double value = 0.0; + if (event->direction == GDK_SCROLL_UP) { + value+=OFFSET; + } + else if(event->direction == GDK_SCROLL_DOWN) { + value-=OFFSET; + } + m_signal_value_changed(value); + return false; } bool TrayIcon::onButtonClick(GdkEventButton* event) { - if (event->button == GDK_BUTTON_MIDDLE) { - muteItem_->set_active(!muteItem_->get_active()); - onMute(); - } - if (event->button == GDK_BUTTON_PRIMARY) { + if (event->button == GDK_BUTTON_MIDDLE) { + muteItem_->set_active(!muteItem_->get_active()); + onMute(); + } + if (event->button == GDK_BUTTON_PRIMARY) { #ifdef IS_DEBUG - std::cout << "Pressed" << std::endl; + std::cout << "Pressed" << std::endl; #endif - setMousePos(event->x_root, event->y_root); - } - return false; + setMousePos(event->x_root, event->y_root); + } + return false; } void TrayIcon::setMousePos(const int X, const int Y) { - mouseX_ = X; - mouseY_ = Y; + mouseX_ = X; + mouseY_ = Y; } void TrayIcon::on_signal_volume_changed(double volume, const std::string &cardName, const std::string &mixerName) { - volumeValue_ = volume; - cardName_ = cardName; - mixerName_ = mixerName; - if (!muted_) { - setIcon(volumeValue_); - } - else { - setIcon(0); - } - const Glib::ustring tip = (!mixerName.empty()) ? Glib::ustring(CARDL) - + Glib::ustring(cardName_) - + Glib::ustring("\n") - + Glib::ustring(MIXERL) - + Glib::ustring(mixerName_) - + Glib::ustring("\n") - + Glib::ustring(VOLUMEL) - + Glib::ustring::format(volumeValue_,"%") - : Glib::ustring(CARDL) - + Glib::ustring(cardName_) - + Glib::ustring("\n") - + Glib::ustring(VOLUMEL) - + Glib::ustring::format(volumeValue_,"%"); - setTooltip(tip); + volumeValue_ = volume; + cardName_ = cardName; + mixerName_ = mixerName; + if (!muted_) { + setIcon(volumeValue_); + } + else { + setIcon(0); + } + const Glib::ustring tip = (!mixerName.empty()) ? Glib::ustring(CARDL) + + Glib::ustring(cardName_) + + Glib::ustring("\n") + + Glib::ustring(MIXERL) + + Glib::ustring(mixerName_) + + Glib::ustring("\n") + + Glib::ustring(VOLUMEL) + + Glib::ustring::format(volumeValue_,"%") + : Glib::ustring(CARDL) + + Glib::ustring(cardName_) + + Glib::ustring("\n") + + Glib::ustring(VOLUMEL) + + Glib::ustring::format(volumeValue_,"%"); + setTooltip(tip); } TrayIcon::type_trayicon_simple_signal TrayIcon::signal_ask_dialog() { - return m_signal_ask_dialog; + return m_signal_ask_dialog; } TrayIcon::type_trayicon_simple_signal TrayIcon::signal_ask_settings() { - return m_signal_ask_settings; + return m_signal_ask_settings; } TrayIcon::type_trayicon_simple_signal TrayIcon::signal_save_settings() { - return m_signal_save_settings; + return m_signal_save_settings; } TrayIcon::type_trayicon_4int_signal TrayIcon::signal_on_restore() { - return m_signal_on_restore; + return m_signal_on_restore; } TrayIcon::type_trayicon_double_signal TrayIcon::signal_value_changed() { - return m_signal_value_changed; + return m_signal_value_changed; } TrayIcon::type_trayicon_bool_signal TrayIcon::signal_on_mute() { - return m_signal_on_mute; + return m_signal_on_mute; } diff --git a/gui/trayicon.h b/gui/trayicon.h index 17e6e8e..4d3a821 100644 --- a/gui/trayicon.h +++ b/gui/trayicon.h @@ -1,6 +1,6 @@ /* * trayicon.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -40,75 +40,75 @@ typedef std::shared_ptr StatusNotifierPtr; class TrayIcon { public: - TrayIcon(double volume, const std::string &cardName, const std::string &mixerName, bool muted); - typedef std::shared_ptr Ptr; - void on_signal_volume_changed(double volume, const std::string &cardName, const std::string &mixerName); - void setMuted(bool isit); - //signals - typedef sigc::signal type_trayicon_simple_signal; - type_trayicon_simple_signal signal_ask_dialog(); - type_trayicon_simple_signal signal_ask_settings(); - type_trayicon_simple_signal signal_save_settings(); - type_trayicon_simple_signal signal_ask_extmixer(); - typedef sigc::signal type_trayicon_4int_signal; - type_trayicon_4int_signal signal_on_restore(); - typedef sigc::signal type_trayicon_double_signal; - type_trayicon_double_signal signal_value_changed(); - typedef sigc::signal type_trayicon_bool_signal; - type_trayicon_bool_signal signal_on_mute(); + TrayIcon(double volume, const std::string &cardName, const std::string &mixerName, bool muted); + typedef std::shared_ptr Ptr; + void on_signal_volume_changed(double volume, const std::string &cardName, const std::string &mixerName); + void setMuted(bool isit); + //signals + typedef sigc::signal type_trayicon_simple_signal; + type_trayicon_simple_signal signal_ask_dialog(); + type_trayicon_simple_signal signal_ask_settings(); + type_trayicon_simple_signal signal_save_settings(); + type_trayicon_simple_signal signal_ask_extmixer(); + typedef sigc::signal type_trayicon_4int_signal; + type_trayicon_4int_signal signal_on_restore(); + typedef sigc::signal type_trayicon_double_signal; + type_trayicon_double_signal signal_value_changed(); + typedef sigc::signal type_trayicon_bool_signal; + type_trayicon_bool_signal signal_on_mute(); protected: - //menu actions - void onHideRestore(); - void runSettings(); - void onMute(); - void onAbout(); - void onQuit(); - //icon actions - void onPopup(guint button, guint32 activate_time); - bool onScrollEvent(GdkEventScroll *event); - bool onButtonClick(GdkEventButton *event); - //signals - type_trayicon_simple_signal m_signal_ask_dialog; - type_trayicon_simple_signal m_signal_ask_settings; - type_trayicon_simple_signal m_signal_save_settings; - type_trayicon_4int_signal m_signal_on_restore; - type_trayicon_double_signal m_signal_value_changed; - type_trayicon_bool_signal m_signal_on_mute; + //menu actions + void onHideRestore(); + void runSettings(); + void onMute(); + void onAbout(); + void onQuit(); + //icon actions + void onPopup(guint button, guint32 activate_time); + bool onScrollEvent(GdkEventScroll *event); + bool onButtonClick(GdkEventButton *event); + //signals + type_trayicon_simple_signal m_signal_ask_dialog; + type_trayicon_simple_signal m_signal_ask_settings; + type_trayicon_simple_signal m_signal_save_settings; + type_trayicon_4int_signal m_signal_on_restore; + type_trayicon_double_signal m_signal_value_changed; + type_trayicon_bool_signal m_signal_on_mute; private: - void setIcon(double value); - void setTooltip(const Glib::ustring &message); - Glib::ustring getIconName(double value) const; - void setMousePos(const int X, const int Y); + void setIcon(double value); + void setTooltip(const Glib::ustring &message); + Glib::ustring getIconName(double value) const; + void setMousePos(const int X, const int Y); #if defined(USE_APPINDICATOR) - static void onScrollEventAI(AppIndicator *ai, gint steps, gint direction, TrayIcon *userdata); + static void onScrollEventAI(AppIndicator *ai, gint steps, gint direction, TrayIcon *userdata); #elif defined(USE_KDE) - static void onActivate(StatusNotifierItem * sn, gint x, gint y, TrayIcon *userdata); - static void onSecondaryActivate(StatusNotifierItem * sn, gint x, gint y, TrayIcon *userdata); - static void onScroll(StatusNotifierItem * sn, gint delta, StatusNotifierScrollOrientation orient, TrayIcon *userdata); - static void onContextMenu(StatusNotifierItem * sn, gint x, gint y, TrayIcon *userdata); - bool checkDBusInterfaceExists(const Glib::ustring &serviceName); - static void onRegisterError(StatusNotifierItem * sn, GError *error, TrayIcon *userdata); + static void onActivate(StatusNotifierItem * sn, gint x, gint y, TrayIcon *userdata); + static void onSecondaryActivate(StatusNotifierItem * sn, gint x, gint y, TrayIcon *userdata); + static void onScroll(StatusNotifierItem * sn, gint delta, StatusNotifierScrollOrientation orient, TrayIcon *userdata); + static void onContextMenu(StatusNotifierItem * sn, gint x, gint y, TrayIcon *userdata); + bool checkDBusInterfaceExists(const Glib::ustring &serviceName); + static void onRegisterError(StatusNotifierItem * sn, GError *error, TrayIcon *userdata); #endif private: - double volumeValue_; - std::string cardName_; - std::string mixerName_; - bool muted_; - Gtk::Menu *menu_; - Gtk::MenuItem *restoreItem_, *settingsItem_, *aboutItem_, *quitItem_; - Gtk::CheckMenuItem* muteItem_; - int mouseX_; - int mouseY_; - int pixbufWidth_; - int pixbufHeight_; - bool isLegacyIcon_; + double volumeValue_; + std::string cardName_; + std::string mixerName_; + bool muted_; + Gtk::Menu *menu_; + Gtk::MenuItem *restoreItem_, *settingsItem_, *aboutItem_, *quitItem_; + Gtk::CheckMenuItem* muteItem_; + int mouseX_; + int mouseY_; + int pixbufWidth_; + int pixbufHeight_; + bool isLegacyIcon_; #if defined(USE_APPINDICATOR) || defined(USE_KDE) - StatusNotifierPtr newIcon_; + StatusNotifierPtr newIcon_; #endif - Glib::RefPtr legacyIcon_; + Glib::RefPtr legacyIcon_; }; #endif // TRAYICON_H diff --git a/main.cpp b/main.cpp index 3344a12..4036367 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,6 @@ /* * main.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -37,72 +37,72 @@ int main (int argc, char *argv[]) { - bindtextdomain(PACKAGE, Tools::getDirPath("locale").c_str()); - bind_textdomain_codeset(PACKAGE, CODEC); - textdomain(PACKAGE); + bindtextdomain(PACKAGE, Tools::getDirPath("locale").c_str()); + bind_textdomain_codeset(PACKAGE, CODEC); + textdomain(PACKAGE); #ifndef IS_GTK_2 - Glib::RefPtr app = Gtk::Application::create(argc, argv, "org.gtkmm.alsavolume"); - Glib::ustring slider_ui_ = Tools::getResPath("gladefiles/SliderFrame.glade"); - Glib::ustring settings_ui_ = Tools::getResPath("gladefiles/SettingsFrame.glade"); + Glib::RefPtr app = Gtk::Application::create(argc, argv, "org.gtkmm.alsavolume"); + Glib::ustring slider_ui_ = Tools::getResPath("gladefiles/SliderFrame.glade"); + Glib::ustring settings_ui_ = Tools::getResPath("gladefiles/SettingsFrame.glade"); #else - Gtk::Main app(argc, argv); - Glib::ustring slider_ui_ = Tools::getResPath("gladefiles/SliderFrame_2.glade"); - Glib::ustring settings_ui_ = Tools::getResPath("gladefiles/SettingsFrame_2.glade"); + Gtk::Main app(argc, argv); + Glib::ustring slider_ui_ = Tools::getResPath("gladefiles/SliderFrame_2.glade"); + Glib::ustring settings_ui_ = Tools::getResPath("gladefiles/SettingsFrame_2.glade"); #endif - if (slider_ui_.empty()) { - std::cerr << "No SliderFrame.glade file found" << std::endl; - return 1; - } - if (settings_ui_.empty()) { - std::cerr << "No SettingsFrame.glade file found" << std::endl; - return 1; - } - Glib::RefPtr refBuilder = Gtk::Builder::create(); - try { - refBuilder->add_from_file(slider_ui_); - refBuilder->add_from_file(settings_ui_); - } - catch(const Gtk::BuilderError& ex) { - std::cerr << "BuilderError::main.cpp::62 " << ex.what() << std::endl; - return 1; - } - catch(const Glib::MarkupError& ex) { - std::cerr << "MarkupError::main.cpp::62 " << ex.what() << std::endl; - return 1; - } - catch(const Glib::FileError& ex) { - std::cerr << "FileError::main.cpp::62 " << ex.what() << std::endl; - return 1; - } - Core::Ptr core(new Core(refBuilder)); + if (slider_ui_.empty()) { + std::cerr << "No SliderFrame.glade file found" << std::endl; + return 1; + } + if (settings_ui_.empty()) { + std::cerr << "No SettingsFrame.glade file found" << std::endl; + return 1; + } + Glib::RefPtr refBuilder = Gtk::Builder::create(); + try { + refBuilder->add_from_file(slider_ui_); + refBuilder->add_from_file(settings_ui_); + } + catch(const Gtk::BuilderError& ex) { + std::cerr << "BuilderError::main.cpp::62 " << ex.what() << std::endl; + return 1; + } + catch(const Glib::MarkupError& ex) { + std::cerr << "MarkupError::main.cpp::62 " << ex.what() << std::endl; + return 1; + } + catch(const Glib::FileError& ex) { + std::cerr << "FileError::main.cpp::62 " << ex.what() << std::endl; + return 1; + } + Core::Ptr core(new Core(refBuilder)); #ifndef IS_GTK_2 - app->hold(); + app->hold(); #endif - SliderWindow *sliderWindow = 0; - refBuilder->get_widget_derived("volumeFrame", sliderWindow); - TrayIcon::Ptr trayIcon(new TrayIcon(core->getVolumeValue(), - core->getSoundCardName(), - core->getActiveMixer(), - core->getMuted())); - if (trayIcon && sliderWindow) { - sliderWindow->setVolumeValue(core->getVolumeValue()); - core->signal_value_changed().connect(sigc::mem_fun(*trayIcon, &TrayIcon::on_signal_volume_changed)); - core->signal_mixer_muted().connect(sigc::mem_fun(*trayIcon, &TrayIcon::setMuted)); - core->signal_volume_changed().connect(sigc::mem_fun(*sliderWindow, &SliderWindow::setVolumeValue)); - sliderWindow->signal_volume_changed().connect(sigc::mem_fun(*core, &Core::onVolumeSlider)); - trayIcon->signal_ask_dialog().connect(sigc::mem_fun(*core, &Core::runAboutDialog)); - trayIcon->signal_ask_settings().connect(sigc::mem_fun(*core, &Core::runSettings)); - trayIcon->signal_on_restore().connect(sigc::mem_fun(*sliderWindow, &SliderWindow::setWindowPosition)); - trayIcon->signal_save_settings().connect(sigc::mem_fun(*core, &Core::saveSettings)); - trayIcon->signal_on_mute().connect(sigc::mem_fun(*core, &Core::soundMuted)); - trayIcon->signal_value_changed().connect(sigc::mem_fun(*core, &Core::onTrayIconScroll)); - sliderWindow->set_visible(false); + SliderWindow *sliderWindow = nullptr; + refBuilder->get_widget_derived("volumeFrame", sliderWindow); + TrayIcon::Ptr trayIcon(new TrayIcon(core->getVolumeValue(), + core->getSoundCardName(), + core->getActiveMixer(), + core->getMuted())); + if (trayIcon && sliderWindow) { + sliderWindow->setVolumeValue(core->getVolumeValue()); + core->signal_value_changed().connect(sigc::mem_fun(*trayIcon, &TrayIcon::on_signal_volume_changed)); + core->signal_mixer_muted().connect(sigc::mem_fun(*trayIcon, &TrayIcon::setMuted)); + core->signal_volume_changed().connect(sigc::mem_fun(*sliderWindow, &SliderWindow::setVolumeValue)); + sliderWindow->signal_volume_changed().connect(sigc::mem_fun(*core, &Core::onVolumeSlider)); + trayIcon->signal_ask_dialog().connect(sigc::mem_fun(*core, &Core::runAboutDialog)); + trayIcon->signal_ask_settings().connect(sigc::mem_fun(*core, &Core::runSettings)); + trayIcon->signal_on_restore().connect(sigc::mem_fun(*sliderWindow, &SliderWindow::setWindowPosition)); + trayIcon->signal_save_settings().connect(sigc::mem_fun(*core, &Core::saveSettings)); + trayIcon->signal_on_mute().connect(sigc::mem_fun(*core, &Core::soundMuted)); + trayIcon->signal_value_changed().connect(sigc::mem_fun(*core, &Core::onTrayIconScroll)); + sliderWindow->set_visible(false); #ifndef IS_GTK_2 - return app->run(); + return app->run(); #else - Gtk::Main::run(); + Gtk::Main::run(); #endif - } - delete sliderWindow; - return 0; + } + delete sliderWindow; + return 0; } diff --git a/pulsework/pulsecore.cpp b/pulsework/pulsecore.cpp index 6f1e908..573c048 100755 --- a/pulsework/pulsecore.cpp +++ b/pulsework/pulsecore.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 2011 Clément Démoulins - * Copyright (C) 2014-2015 Vitaly Tonkacheyev + * Copyright (C) 2014-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,361 +25,361 @@ //Callbacks void state_cb(pa_context* context, void* raw) { - PulseCore *state = static_cast (raw); - switch(pa_context_get_state(context)) { - case PA_CONTEXT_READY: - state->pState = CONNECTED; - break; - case PA_CONTEXT_FAILED: - state->pState = ERROR; - break; - case PA_CONTEXT_UNCONNECTED: - case PA_CONTEXT_AUTHORIZING: - case PA_CONTEXT_SETTING_NAME: - case PA_CONTEXT_CONNECTING: - case PA_CONTEXT_TERMINATED: - break; - } + PulseCore *state = static_cast (raw); + switch(pa_context_get_state(context)) { + case PA_CONTEXT_READY: + state->pState = CONNECTED; + break; + case PA_CONTEXT_FAILED: + state->pState = ERROR; + break; + case PA_CONTEXT_UNCONNECTED: + case PA_CONTEXT_AUTHORIZING: + case PA_CONTEXT_SETTING_NAME: + case PA_CONTEXT_CONNECTING: + case PA_CONTEXT_TERMINATED: + break; + } } void sink_list_cb(pa_context *c, const pa_sink_info *i, int eol, void *raw) { - (void)c; - if (eol != 0) return; - PulseDevicePtrList *sinks = static_cast(raw); - const PulseDevice::Ptr s(new PulseDevice(i)); - sinks->push_back(s); + (void)c; + if (eol != 0) return; + PulseDevicePtrList *sinks = static_cast(raw); + const PulseDevice::Ptr s(new PulseDevice(i)); + sinks->push_back(s); } void source_list_cb(pa_context *c, const pa_source_info *i, int eol, void *raw) { - (void)c; - if (eol != 0) return; - PulseDevicePtrList *sources = static_cast(raw); - const PulseDevice::Ptr s(new PulseDevice(i)); - sources->push_back(s); + (void)c; + if (eol != 0) return; + PulseDevicePtrList *sources = static_cast(raw); + const PulseDevice::Ptr s(new PulseDevice(i)); + sources->push_back(s); } void server_info_cb(pa_context* c, const pa_server_info* i, void* raw) { - (void)c; - ServerInfo* info = static_cast(raw); - info->defaultSinkName = std::string(i->default_sink_name); - info->defaultSourceName = std::string(i->default_source_name); + (void)c; + ServerInfo* info = static_cast(raw); + info->defaultSinkName = std::string(i->default_sink_name); + info->defaultSourceName = std::string(i->default_source_name); } void success_cb(pa_context* c, int success, void* raw) { - (void)c; - (void)success; - (void)raw; + (void)c; + (void)success; + (void)raw; } // PulseCore::PulseCore(const char *clientName) -: mainLoop_(pa_mainloop_new()), - mainLoopApi_(pa_mainloop_get_api(mainLoop_)), - context_(pa_context_new(mainLoopApi_,clientName)) -{ - isAvailable_ = true; - pState = CONNECTING; - pa_context_set_state_callback(context_, &state_cb, this); - pa_context_connect(context_, NULL, PA_CONTEXT_NOFLAGS, NULL); - while (pState == CONNECTING) { - pa_mainloop_iterate(mainLoop_, 1, &retval_); - } - if (pState == ERROR) { - onError("Pulseaudio Connection Error"); - isAvailable_ = false; - } - if (isAvailable_) { - updateDevices(); - currentDeviceName_ = defaultSink(); - currentDeviceIndex_ = getCurrentDeviceIndex(); - } + : mainLoop_(pa_mainloop_new()), + mainLoopApi_(pa_mainloop_get_api(mainLoop_)), + context_(pa_context_new(mainLoopApi_,clientName)) +{ + isAvailable_ = true; + pState = CONNECTING; + pa_context_set_state_callback(context_, &state_cb, this); + pa_context_connect(context_, nullptr, PA_CONTEXT_NOFLAGS, nullptr); + while (pState == CONNECTING) { + pa_mainloop_iterate(mainLoop_, 1, &retval_); + } + if (pState == ERROR) { + onError("Pulseaudio Connection Error"); + isAvailable_ = false; + } + if (isAvailable_) { + updateDevices(); + currentDeviceName_ = defaultSink(); + currentDeviceIndex_ = getCurrentDeviceIndex(); + } } PulseCore::~PulseCore() { - if (!sinks_.empty()) - sinks_.clear(); - if (!sources_.empty()) - sources_.clear(); - if (pState == CONNECTED) - pa_context_disconnect(context_); - pa_mainloop_free(mainLoop_); + if (!sinks_.empty()) + sinks_.clear(); + if (!sources_.empty()) + sources_.clear(); + if (pState == CONNECTED) + pa_context_disconnect(context_); + pa_mainloop_free(mainLoop_); } void PulseCore::iterate(pa_operation *op) { - while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) { - pa_mainloop_iterate(mainLoop_, 1, &retval_); - } + while (pa_operation_get_state(op) == PA_OPERATION_RUNNING) { + pa_mainloop_iterate(mainLoop_, 1, &retval_); + } } void PulseCore::getSinks() { - pa_operation* op = pa_context_get_sink_info_list(context_, &sink_list_cb, &sinks_); - iterate(op); - pa_operation_unref(op); + pa_operation* op = pa_context_get_sink_info_list(context_, &sink_list_cb, &sinks_); + iterate(op); + pa_operation_unref(op); } void PulseCore::getSources() { - pa_operation* op = pa_context_get_source_info_list(context_, &source_list_cb, &sources_); - iterate(op); - pa_operation_unref(op); + pa_operation* op = pa_context_get_source_info_list(context_, &source_list_cb, &sources_); + iterate(op); + pa_operation_unref(op); } PulseDevice::Ptr PulseCore::getSink(u_int32_t index) { - if (index < sinks_.size()) { - return sinks_.at(index); - } - else { - onError("ERROR in pulsecore.cpp:132. This pulseaudio sink does not exits. Default sink will be used"); - } - return getDefaultSink(); + if (index < sinks_.size()) { + return sinks_.at(index); + } + else { + onError("ERROR in pulsecore.cpp:132. This pulseaudio sink does not exits. Default sink will be used"); + } + return getDefaultSink(); } PulseDevice::Ptr PulseCore::getSink(const std::string &name) { - PulseDevicePtrList::iterator it = std::find_if(sinks_.begin(), - sinks_.end(), - [=](const PulseDevice::Ptr &device){return (device->name() == name);}); - return (it != sinks_.end()) ? *it : getDefaultSink(); + auto it = std::find_if(sinks_.begin(), + sinks_.end(), + [=](const PulseDevice::Ptr &device){return (device->name() == name);}); + return (it != sinks_.end()) ? *it : getDefaultSink(); } PulseDevice::Ptr PulseCore::getSource(u_int32_t index) { - if (index < sources_.size()) { - return sources_.at(index); - } - else { - onError("ERROR in pulsecore.cpp:161. This pulseaudio source does not exits. Default source will be used"); - } - return getDefaultSource(); + if (index < sources_.size()) { + return sources_.at(index); + } + else { + onError("ERROR in pulsecore.cpp:161. This pulseaudio source does not exits. Default source will be used"); + } + return getDefaultSource(); } PulseDevice::Ptr PulseCore::getSource(const std::string &name) { - PulseDevicePtrList::iterator it = std::find_if(sources_.begin(), - sources_.end(), - [=](const PulseDevice::Ptr &device){return (device->name() == name);}); - return (it != sinks_.end()) ? *it : getDefaultSource(); + auto it = std::find_if(sources_.begin(), + sources_.end(), + [=](const PulseDevice::Ptr &device){return (device->name() == name);}); + return (it != sinks_.end()) ? *it : getDefaultSource(); } PulseDevice::Ptr PulseCore::getDefaultSink() { - ServerInfo info; - pa_operation* op = pa_context_get_server_info(context_, &server_info_cb, &info); - iterate(op); - pa_operation_unref(op); - if (!info.defaultSinkName.empty()) { - return getSink(info.defaultSinkName); - } - isAvailable_ = false; - return PulseDevice::Ptr(); + ServerInfo info; + pa_operation* op = pa_context_get_server_info(context_, &server_info_cb, &info); + iterate(op); + pa_operation_unref(op); + if (!info.defaultSinkName.empty()) { + return getSink(info.defaultSinkName); + } + isAvailable_ = false; + return PulseDevice::Ptr(); } PulseDevice::Ptr PulseCore::getDefaultSource() { - ServerInfo info; - pa_operation* op = pa_context_get_server_info(context_, &server_info_cb, &info); - iterate(op); - pa_operation_unref(op); - if (!info.defaultSourceName.empty()) { - return getSource(info.defaultSourceName); - } - isAvailable_ = false; - return PulseDevice::Ptr(); + ServerInfo info; + pa_operation* op = pa_context_get_server_info(context_, &server_info_cb, &info); + iterate(op); + pa_operation_unref(op); + if (!info.defaultSourceName.empty()) { + return getSource(info.defaultSourceName); + } + isAvailable_ = false; + return PulseDevice::Ptr(); } const std::vector &PulseCore::getSinksDescriptions() const { - return sinksDescriptions_; + return sinksDescriptions_; } const std::vector &PulseCore::getSourcesDescriptions() const { - return sourcesDescriptions_; + return sourcesDescriptions_; } const std::string PulseCore::getDeviceDescription(const std::string &name) { - std::string desc = getDeviceByName(name)->description(); - return desc.empty() ? getDefaultSink()->description() : desc; + std::string desc = getDeviceByName(name)->description(); + return desc.empty() ? getDefaultSink()->description() : desc; } PulseDevice::Ptr PulseCore::getDeviceByName(const std::string &name) { - PulseDevice::Ptr result; - std::for_each(sinks_.begin(), sinks_.end(), [&](const PulseDevice::Ptr &device){ - if(device->name() == name) { - result = device; - } - }); - std::for_each(sources_.begin(), sources_.end(), [&](const PulseDevice::Ptr &device){ - if(device->name() == name) { - result = device; - } - }); - return result; + PulseDevice::Ptr result; + for(const PulseDevice::Ptr &device : sinks_){ + if(device->name() == name) { + result = device; + } + } + for(const PulseDevice::Ptr &device : sources_){ + if(device->name() == name) { + result = device; + } + } + return result; } const std::string PulseCore::getDeviceNameByIndex(int index) { - return getDeviceByIndex(index)->name(); + return getDeviceByIndex(index)->name(); } const std::string PulseCore::defaultSink() { - return getDefaultSink()->name(); + return getDefaultSink()->name(); } void PulseCore::setVolume_(const PulseDevice::Ptr &device, int value) { - pa_cvolume* new_cvolume = pa_cvolume_set(&device->volume, - device->volume.channels, - (pa_volume_t) device->round(MAX(((double)value * PA_VOLUME_NORM) / 100, 0.0)) - ); - pa_operation* op; - if (device->type() == SINK) { - op = pa_context_set_sink_volume_by_index(context_, device->index(), new_cvolume, success_cb, NULL); - } - else { - op = pa_context_set_source_volume_by_index(context_, device->index(), new_cvolume, success_cb, NULL); - } - iterate(op); - pa_operation_unref(op); + pa_cvolume* new_cvolume = pa_cvolume_set(&device->volume, + device->volume.channels, + (pa_volume_t) device->round(MAX(((double)value * PA_VOLUME_NORM) / 100, 0.0)) + ); + pa_operation* op; + if (device->type() == SINK) { + op = pa_context_set_sink_volume_by_index(context_, device->index(), new_cvolume, success_cb, nullptr); + } + else { + op = pa_context_set_source_volume_by_index(context_, device->index(), new_cvolume, success_cb, nullptr); + } + iterate(op); + pa_operation_unref(op); } void PulseCore::setMute_(const PulseDevice::Ptr &device, bool mute) { - pa_operation* op; - if (device->type() == SINK) { - op = pa_context_set_sink_mute_by_index(context_, device->index(), (int) mute, success_cb, NULL); - } - else { - op = pa_context_set_source_mute_by_index(context_, device->index(), (int) mute, success_cb, NULL); - } - iterate(op); - pa_operation_unref(op); + pa_operation* op; + if (device->type() == SINK) { + op = pa_context_set_sink_mute_by_index(context_, device->index(), (int) mute, success_cb, nullptr); + } + else { + op = pa_context_set_source_mute_by_index(context_, device->index(), (int) mute, success_cb, nullptr); + } + iterate(op); + pa_operation_unref(op); } void PulseCore::setCurrentDevice(const std::string &name) { - updateDevices(); - currentDeviceName_ = getDeviceByName(name)->name(); - currentDeviceIndex_ = getCurrentDeviceIndex(); + updateDevices(); + currentDeviceName_ = getDeviceByName(name)->name(); + currentDeviceIndex_ = getCurrentDeviceIndex(); } void PulseCore::setVolume(int value) { - setVolume_(getDeviceByIndex(currentDeviceIndex_), value); + setVolume_(getDeviceByIndex(currentDeviceIndex_), value); } void PulseCore::setMute(bool mute) { - setMute_(getDeviceByIndex(currentDeviceIndex_), !mute); + setMute_(getDeviceByIndex(currentDeviceIndex_), !mute); } void PulseCore::updateDevices() { - clearLists(); - getSinks(); - getSources(); - std::for_each(sinks_.begin(), sinks_.end(), [&](const PulseDevice::Ptr &device){ - devicesNames_.push_back(device->name()); - sinksDescriptions_.push_back(device->description()); - }); - std::for_each(sources_.begin(), sources_.end(), [&](const PulseDevice::Ptr &device){ - devicesNames_.push_back(device->name()); - sourcesDescriptions_.push_back(device->description()); - }); - devicesDescs_.insert(devicesDescs_.end(), sinksDescriptions_.begin(), sinksDescriptions_.end()); - devicesDescs_.insert(devicesDescs_.end(), sourcesDescriptions_.begin(), sourcesDescriptions_.end()); + clearLists(); + getSinks(); + getSources(); + for(const PulseDevice::Ptr &device : sinks_){ + devicesNames_.push_back(device->name()); + sinksDescriptions_.push_back(device->description()); + } + for(const PulseDevice::Ptr &device : sources_){ + devicesNames_.push_back(device->name()); + sourcesDescriptions_.push_back(device->description()); + } + devicesDescs_.insert(devicesDescs_.end(), sinksDescriptions_.begin(), sinksDescriptions_.end()); + devicesDescs_.insert(devicesDescs_.end(), sourcesDescriptions_.begin(), sourcesDescriptions_.end()); } void PulseCore::clearLists() { - if (!sinks_.empty()) { - sinks_.clear(); - } - if (!sources_.empty()) { - sources_.clear(); - } - if (!sinksDescriptions_.empty()) { - sinksDescriptions_.clear(); - } - if(!sourcesDescriptions_.empty()) { - sourcesDescriptions_.clear(); - } - if(!devicesNames_.empty()) { - devicesNames_.clear(); - } - if(!devicesDescs_.empty()) { - devicesDescs_.clear(); - } + if (!sinks_.empty()) { + sinks_.clear(); + } + if (!sources_.empty()) { + sources_.clear(); + } + if (!sinksDescriptions_.empty()) { + sinksDescriptions_.clear(); + } + if(!sourcesDescriptions_.empty()) { + sourcesDescriptions_.clear(); + } + if(!devicesNames_.empty()) { + devicesNames_.clear(); + } + if(!devicesDescs_.empty()) { + devicesDescs_.clear(); + } } int PulseCore::getVolume() { - return getDeviceByIndex(currentDeviceIndex_)->volume_percent(); + return getDeviceByIndex(currentDeviceIndex_)->volume_percent(); } bool PulseCore::getMute() { - return getDeviceByIndex(currentDeviceIndex_)->mute(); + return getDeviceByIndex(currentDeviceIndex_)->mute(); } const std::vector &PulseCore::getCardList() const { - return devicesDescs_; + return devicesDescs_; } const std::vector &PulseCore::getCardNames() const { - return devicesNames_; + return devicesNames_; } PulseDevice::Ptr PulseCore::getDeviceByIndex(int index) { - updateDevices(); - PulseDevice::Ptr device = getDefaultSink(); - const int sinksSize = sinksDescriptions_.size(); - const int sourcesSize = sourcesDescriptions_.size(); - if (index >=0 && index < (sinksSize + sourcesSize)) { - const int deltaIndex = sinksSize - index; - device = (deltaIndex > 0) ? getSink(index) : getSource(abs(deltaIndex)); - } - return device; + updateDevices(); + PulseDevice::Ptr device = getDefaultSink(); + const int sinksSize = sinksDescriptions_.size(); + const int sourcesSize = sourcesDescriptions_.size(); + if (index >=0 && index < (sinksSize + sourcesSize)) { + const int deltaIndex = sinksSize - index; + device = (deltaIndex > 0) ? getSink(index) : getSource(abs(deltaIndex)); + } + return device; } int PulseCore::getCurrentDeviceIndex() { - const int index = Tools::itemIndex(devicesNames_, currentDeviceName_); - return ((index > 0) ? index : 0); + const int index = Tools::itemIndex(devicesNames_, currentDeviceName_); + return ((index > 0) ? index : 0); } int PulseCore::getCardIndex() { - return getDeviceByIndex(currentDeviceIndex_)->card(); + return getDeviceByIndex(currentDeviceIndex_)->card(); } bool PulseCore::deviceNameExists(const std::string &name) { - return Tools::itemExists(devicesNames_, name); + return Tools::itemExists(devicesNames_, name); } bool PulseCore::deviceDescriptionExists(const std::string &description) { - return Tools::itemExists(devicesDescs_, description); + return Tools::itemExists(devicesDescs_, description); } bool PulseCore::available() { - return isAvailable_; + return isAvailable_; } void PulseCore::refreshDevices() { - updateDevices(); + updateDevices(); } diff --git a/pulsework/pulsecore.h b/pulsework/pulsecore.h index 3c2f12f..797a361 100755 --- a/pulsework/pulsecore.h +++ b/pulsework/pulsecore.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2011 Clément Démoulins - * Copyright (C) 2014-2015 Vitaly Tonkacheyev + * Copyright (C) 2014-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -26,13 +26,13 @@ #include struct ServerInfo { - std::string defaultSourceName; - std::string defaultSinkName; + std::string defaultSourceName; + std::string defaultSinkName; }; enum state { - CONNECTING, - CONNECTED, - ERROR + CONNECTING, + CONNECTED, + ERROR }; typedef std::vector PulseDevicePtrList; @@ -40,60 +40,60 @@ typedef std::vector PulseDevicePtrList; class PulseCore { public: - explicit PulseCore(const char *clientName); - ~PulseCore(); - PulseCore(PulseCore const &); - typedef std::shared_ptr Ptr; - state pState; - const std::string defaultSink(); - const std::vector &getCardList() const; - const std::vector &getCardNames() const; - const std::string getDeviceDescription(const std::string &name); - const std::string getDeviceNameByIndex(int index); - int getCurrentDeviceIndex(); - int getVolume(); - int getCardIndex(); - bool getMute(); - bool deviceNameExists(const std::string &name); - bool deviceDescriptionExists(const std::string &description); - void setVolume(int value); - void setMute(bool mute); - void setCurrentDevice(const std::string &name); - bool available(); - void refreshDevices(); + explicit PulseCore(const char *clientName); + ~PulseCore(); + PulseCore(PulseCore const &); + typedef std::shared_ptr Ptr; + state pState; + const std::string defaultSink(); + const std::vector &getCardList() const; + const std::vector &getCardNames() const; + const std::string getDeviceDescription(const std::string &name); + const std::string getDeviceNameByIndex(int index); + int getCurrentDeviceIndex(); + int getVolume(); + int getCardIndex(); + bool getMute(); + bool deviceNameExists(const std::string &name); + bool deviceDescriptionExists(const std::string &description); + void setVolume(int value); + void setMute(bool mute); + void setCurrentDevice(const std::string &name); + bool available(); + void refreshDevices(); private: - void getSinks(); - void getSources(); - PulseDevice::Ptr getSink(u_int32_t); - PulseDevice::Ptr getSink(const std::string &name); - PulseDevice::Ptr getSource(u_int32_t); - PulseDevice::Ptr getSource(const std::string &name); - PulseDevice::Ptr getDefaultSink(); - PulseDevice::Ptr getDefaultSource(); - PulseDevice::Ptr getDeviceByName(const std::string &name); - PulseDevice::Ptr getDeviceByIndex(int index); - const std::vector &getSinksDescriptions() const; - const std::vector &getSourcesDescriptions() const; - void setVolume_(const PulseDevice::Ptr &device, int value); - void setMute_(const PulseDevice::Ptr &device, bool mute); - void iterate(pa_operation* op); - inline void onError(const std::string &message){ std::cerr << "Error: " << message << std::endl; } - void updateDevices(); - void clearLists(); + void getSinks(); + void getSources(); + PulseDevice::Ptr getSink(u_int32_t); + PulseDevice::Ptr getSink(const std::string &name); + PulseDevice::Ptr getSource(u_int32_t); + PulseDevice::Ptr getSource(const std::string &name); + PulseDevice::Ptr getDefaultSink(); + PulseDevice::Ptr getDefaultSource(); + PulseDevice::Ptr getDeviceByName(const std::string &name); + PulseDevice::Ptr getDeviceByIndex(int index); + const std::vector &getSinksDescriptions() const; + const std::vector &getSourcesDescriptions() const; + void setVolume_(const PulseDevice::Ptr &device, int value); + void setMute_(const PulseDevice::Ptr &device, bool mute); + void iterate(pa_operation* op); + inline void onError(const std::string &message){ std::cerr << "Error: " << message << std::endl; } + void updateDevices(); + void clearLists(); private: - pa_mainloop* mainLoop_; - pa_mainloop_api* mainLoopApi_; - pa_context* context_; - int retval_; - PulseDevicePtrList sources_; - PulseDevicePtrList sinks_; - std::string currentDeviceName_; - int currentDeviceIndex_; - std::vector sinksDescriptions_; - std::vector sourcesDescriptions_; - std::vector devicesNames_; - std::vector devicesDescs_; - bool isAvailable_; + pa_mainloop* mainLoop_; + pa_mainloop_api* mainLoopApi_; + pa_context* context_; + int retval_; + PulseDevicePtrList sources_; + PulseDevicePtrList sinks_; + std::string currentDeviceName_; + int currentDeviceIndex_; + std::vector sinksDescriptions_; + std::vector sourcesDescriptions_; + std::vector devicesNames_; + std::vector devicesDescs_; + bool isAvailable_; }; #endif // PULSECORE_H diff --git a/pulsework/pulsedevice.cpp b/pulsework/pulsedevice.cpp index 27f2e7e..e553dcb 100755 --- a/pulsework/pulsedevice.cpp +++ b/pulsework/pulsedevice.cpp @@ -1,6 +1,6 @@ /* * Copyright (C) 2011 Clément Démoulins - * Copyright (C) 2014-2015 Vitaly Tonkacheyev + * Copyright (C) 2014-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,92 +24,92 @@ static uint32_t getCardId(pa_proplist *pl) { - const char* cardId = pa_proplist_gets(pl, ALSA_CARDID_PROPERTY); - return (cardId != NULL) ? std::stoi(std::string(cardId)) : 0; + const char* cardId = pa_proplist_gets(pl, ALSA_CARDID_PROPERTY); + return (cardId != nullptr) ? std::stoi(std::string(cardId)) : 0; } PulseDevice::PulseDevice() -: index_(0), - card_(0), - type_(SINK), - name_(std::string()), - description_(std::string()), - mute_(false) + : index_(0), + card_(0), + type_(SINK), + name_(std::string()), + description_(std::string()), + mute_(false) { } PulseDevice::PulseDevice(const pa_source_info* i) -: index_(i->index), - card_(getCardId(i->proplist)), - type_(SOURCE), - name_(std::string(i->name)), - description_(std::string(i->description)) + : index_(i->index), + card_(getCardId(i->proplist)), + type_(SOURCE), + name_(std::string(i->name)), + description_(std::string(i->description)) { - volume.channels = i->volume.channels; - int n; - for (n = 0; n < volume.channels; ++n) { - volume.values[n] = i->volume.values[n]; - } - mute_ = i->mute == 1; + volume.channels = i->volume.channels; + int n; + for (n = 0; n < volume.channels; ++n) { + volume.values[n] = i->volume.values[n]; + } + mute_ = i->mute == 1; } PulseDevice::PulseDevice(const pa_sink_info* i) -: index_(i->index), - card_(getCardId(i->proplist)), - type_(SINK), - name_(std::string(i->name)), - description_(std::string(i->description)) + : index_(i->index), + card_(getCardId(i->proplist)), + type_(SINK), + name_(std::string(i->name)), + description_(std::string(i->description)) { - volume.channels = i->volume.channels; - int n; - for (n = 0; n < volume.channels; ++n) { - volume.values[n] = i->volume.values[n]; - } - mute_ = i->mute == 1; + volume.channels = i->volume.channels; + int n; + for (n = 0; n < volume.channels; ++n) { + volume.values[n] = i->volume.values[n]; + } + mute_ = i->mute == 1; } int PulseDevice::percent(pa_cvolume& volume_) const { - return (int) round(((double) pa_cvolume_avg(&volume_) * 100.) / PA_VOLUME_NORM); + return (int) round(((double) pa_cvolume_avg(&volume_) * 100.) / PA_VOLUME_NORM); } double PulseDevice::round(double value) const { - return (value > 0.0) ? floor(value + 0.5) : ceil(value - 0.5); + return (value > 0.0) ? floor(value + 0.5) : ceil(value - 0.5); } uint32_t PulseDevice::index() const { - return index_; + return index_; } uint32_t PulseDevice::card() const { - return card_; + return card_; } device_type PulseDevice::type() const { - return type_; + return type_; } const std::string &PulseDevice::name() const { - return name_; + return name_; } const std::string &PulseDevice::description() const { - return description_; + return description_; } int PulseDevice::volume_percent() { - return percent(volume); + return percent(volume); } bool PulseDevice::mute() const { - return mute_; + return mute_; } diff --git a/pulsework/pulsedevice.h b/pulsework/pulsedevice.h index c4dce4d..f202ebb 100755 --- a/pulsework/pulsedevice.h +++ b/pulsework/pulsedevice.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2011 Clément Démoulins - * Copyright (C) 2014-2015 Vitaly Tonkacheyev + * Copyright (C) 2014-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,35 +25,35 @@ #include enum device_type { - SOURCE, - SINK + SOURCE, + SINK }; class PulseDevice { public: - PulseDevice(); - explicit PulseDevice(const pa_source_info* i_); - explicit PulseDevice(const pa_sink_info* i); - typedef std::shared_ptr Ptr; - uint32_t index() const; - uint32_t card() const; - device_type type() const; - const std::string &name() const; - const std::string &description() const; - pa_cvolume volume; - int volume_percent(); - bool mute() const; - double round(double value) const; + PulseDevice(); + explicit PulseDevice(const pa_source_info* i_); + explicit PulseDevice(const pa_sink_info* i); + typedef std::shared_ptr Ptr; + uint32_t index() const; + uint32_t card() const; + device_type type() const; + const std::string &name() const; + const std::string &description() const; + pa_cvolume volume; + int volume_percent(); + bool mute() const; + double round(double value) const; private: - int percent(pa_cvolume& volume) const; + int percent(pa_cvolume& volume) const; private: - uint32_t index_; - uint32_t card_; - device_type type_; - std::string name_; - std::string description_; - bool mute_; + uint32_t index_; + uint32_t card_; + device_type type_; + std::string name_; + std::string description_; + bool mute_; }; #endif // PULSEDEVICE_H diff --git a/tools/core.cpp b/tools/core.cpp index f11b8c9..706271b 100644 --- a/tools/core.cpp +++ b/tools/core.cpp @@ -1,6 +1,6 @@ /* * core.cpp - * Copyright (C) 2013-2018 Vitaly Tonkacheyev + * Copyright (C) 2013-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,409 +41,409 @@ #define POLLING_INTERVAL 2000 Core::Core(const Glib::RefPtr &refGlade) -: settings_(Settings::Ptr(new Settings())), - alsaWork_(AlsaWork::Ptr(new AlsaWork())), - settingsStr_(settingsStr::Ptr(new settingsStr())), - mixerName_(settings_->getMixer()), - volumeValue_(0.0), - pollVolume_(0.0), - settingsDialog_(0), - isPulse_(false), - isMuted_(false) + : settings_(Settings::Ptr(new Settings())), + alsaWork_(AlsaWork::Ptr(new AlsaWork())), + settingsStr_(settingsStr::Ptr(new settingsStr())), + mixerName_(settings_->getMixer()), + volumeValue_(0.0), + pollVolume_(0.0), + settingsDialog_(nullptr), + isPulse_(false), + isMuted_(false) { #ifdef HAVE_PULSE - isPulse_ = settings_->usePulse(); - initPulseAudio(); + isPulse_ = settings_->usePulse(); + initPulseAudio(); #else - isPulse_ = false; + isPulse_ = false; #endif - settingsStr_->setUsePulse(isPulse_); - alsaCards_ = alsaWork_->getCardsList(); - settingsStr_->setList(CARDS, alsaCards_); - settingsStr_->setMixerId(settings_->getMixerId()); - int cardId = settings_->getSoundCard(); - cardId = (alsaWork_->cardExists(settings_->getSoundCard())) ? cardId : alsaWork_->getFirstCardWithMixers(); - if (mixerName_.empty()) { - mixerName_ = alsaWork_->getMixerName(settingsStr_->mixerId()); - } - updateControls(cardId); + settingsStr_->setUsePulse(isPulse_); + alsaCards_ = alsaWork_->getCardsList(); + settingsStr_->setList(CARDS, alsaCards_); + settingsStr_->setMixerId(settings_->getMixerId()); + int cardId = settings_->getSoundCard(); + cardId = (alsaWork_->cardExists(settings_->getSoundCard())) ? cardId : alsaWork_->getFirstCardWithMixers(); + if (mixerName_.empty()) { + mixerName_ = alsaWork_->getMixerName(settingsStr_->mixerId()); + } + updateControls(cardId); #ifdef IS_DEBUG - std::cout << "Id in settings - " << settingsStr_->mixerId() << std::endl; + std::cout << "Id in settings - " << settingsStr_->mixerId() << std::endl; #endif - pollVolume_ = volumeValue_; - settingsStr_->setNotebookOrientation(settings_->getNotebookOrientation()); - settings_->setVersion(VERSION); - settingsStr_->setUsePolling(settings_->usePolling()); - refGlade->get_widget_derived("settingsDialog", settingsDialog_); - //connect signals - if (settingsDialog_) { - signal_switches_ = settingsDialog_->signal_switches_toggled().connect(sigc::mem_fun(*this, &Core::switchChanged)); - signal_sndcard_ = settingsDialog_->signal_sndcard_changed().connect(sigc::mem_fun(*this, &Core::updateControls)); + pollVolume_ = volumeValue_; + settingsStr_->setNotebookOrientation(settings_->getNotebookOrientation()); + settings_->setVersion(VERSION); + settingsStr_->setUsePolling(settings_->usePolling()); + refGlade->get_widget_derived("settingsDialog", settingsDialog_); + //connect signals + if (settingsDialog_) { + signal_switches_ = settingsDialog_->signal_switches_toggled().connect(sigc::mem_fun(*this, &Core::switchChanged)); + signal_sndcard_ = settingsDialog_->signal_sndcard_changed().connect(sigc::mem_fun(*this, &Core::updateControls)); #ifdef HAVE_PULSE - signal_pulsdev_ = settingsDialog_->signal_pulsdev_toggled().connect(sigc::mem_fun(*this, &Core::onSettingsDialogUsePulse)); - signal_pulsedevices_ = settingsDialog_->signal_pulsedevices_changed().connect(sigc::mem_fun(*this, &Core::updatePulseDevices)); + signal_pulsdev_ = settingsDialog_->signal_pulsdev_toggled().connect(sigc::mem_fun(*this, &Core::onSettingsDialogUsePulse)); + signal_pulsedevices_ = settingsDialog_->signal_pulsedevices_changed().connect(sigc::mem_fun(*this, &Core::updatePulseDevices)); #endif - } - signal_timer_ = Glib::signal_timeout().connect(sigc::mem_fun(*this,&Core::onTimeout), POLLING_INTERVAL); + } + signal_timer_ = Glib::signal_timeout().connect(sigc::mem_fun(*this,&Core::onTimeout), POLLING_INTERVAL); } Core::~Core() { - if (settingsDialog_) - delete settingsDialog_; + if (settingsDialog_) + delete settingsDialog_; } void Core::runAboutDialog() { - std::shared_ptr dialog(new Gtk::AboutDialog()); - dialog->set_title(TITLE); - dialog->set_program_name(PROGNAME); - dialog->set_comments(COMMENTS); - dialog->set_version(VERSION); - dialog->set_copyright(COPYRIGHT); - dialog->set_website(WEBSITE); - dialog->set_website_label(WEBSITELABEL); - const std::string logoName = Tools::getResPath("icons/volume.png"); - const std::string iconName = Tools::getResPath("icons/tb_icon100.png"); - Glib::RefPtr logo = Gdk::Pixbuf::create_from_file(logoName); - Glib::RefPtr icon = Gdk::Pixbuf::create_from_file(iconName); - dialog->set_icon(icon); - dialog->set_logo(logo); - dialog->run(); + std::shared_ptr dialog(new Gtk::AboutDialog()); + dialog->set_title(TITLE); + dialog->set_program_name(PROGNAME); + dialog->set_comments(COMMENTS); + dialog->set_version(VERSION); + dialog->set_copyright(COPYRIGHT); + dialog->set_website(WEBSITE); + dialog->set_website_label(WEBSITELABEL); + const std::string logoName = Tools::getResPath("icons/volume.png"); + const std::string iconName = Tools::getResPath("icons/tb_icon100.png"); + Glib::RefPtr logo = Gdk::Pixbuf::create_from_file(logoName); + Glib::RefPtr icon = Gdk::Pixbuf::create_from_file(iconName); + dialog->set_icon(icon); + dialog->set_logo(logo); + dialog->run(); } #ifdef HAVE_PULSE void Core::initPulseAudio() { - if (!pulse_) { - pulse_ = PulseCore::Ptr(new PulseCore("alsavolume")); - if (pulse_->available()) { - pulseDevice_ = settings_->pulseDeviceName(); - if (pulseDevice_.empty() || !pulse_->deviceNameExists(pulseDevice_)) { - pulseDevice_ = pulse_->defaultSink(); - } - pulse_->setCurrentDevice(pulseDevice_); - updatePulseDevices(pulse_->getCurrentDeviceIndex()); - } - else { - errorDialog(_("Can't start PulseAudio! Using Alsa by default")); - pulse_.reset(); - isPulse_ = false; - settingsStr_->setUsePulse(isPulse_); - settings_->setUsePulse(isPulse_); - } - } + if (!pulse_) { + pulse_ = PulseCore::Ptr(new PulseCore("alsavolume")); + if (pulse_->available()) { + pulseDevice_ = settings_->pulseDeviceName(); + if (pulseDevice_.empty() || !pulse_->deviceNameExists(pulseDevice_)) { + pulseDevice_ = pulse_->defaultSink(); + } + pulse_->setCurrentDevice(pulseDevice_); + updatePulseDevices(pulse_->getCurrentDeviceIndex()); + } + else { + errorDialog(_("Can't start PulseAudio! Using Alsa by default")); + pulse_.reset(); + isPulse_ = false; + settingsStr_->setUsePulse(isPulse_); + settings_->setUsePulse(isPulse_); + } + } } #endif void Core::errorDialog(const std::string &errorMessage) { - std::shared_ptr warn_(new Gtk::MessageDialog(Glib::ustring(errorMessage))); - warn_->run(); + std::shared_ptr warn_(new Gtk::MessageDialog(Glib::ustring(errorMessage))); + warn_->run(); } void Core::blockAllSignals(bool isblock) { - signal_switches_.block(isblock); - signal_sndcard_.block(isblock); + signal_switches_.block(isblock); + signal_sndcard_.block(isblock); #ifdef HAVE_PULSE - signal_pulsdev_.block(isblock); - signal_pulsedevices_.block(isblock); + signal_pulsdev_.block(isblock); + signal_pulsedevices_.block(isblock); #endif } void Core::runSettings() { - if (settingsDialog_) { + if (settingsDialog_) { #ifdef HAVE_PULSE - if (pulse_) { - updatePulseDevices(pulse_->getCurrentDeviceIndex()); - } - else { - settingsDialog_->disablePulseCheckButton(); - } + if (pulse_) { + updatePulseDevices(pulse_->getCurrentDeviceIndex()); + } + else { + settingsDialog_->disablePulseCheckButton(); + } #endif - blockAllSignals(true); - settingsDialog_->initParms(settingsStr_); - settingsDialog_->updateMixers(settingsStr_->mixerList()); - settingsDialog_->updateSwitches(settingsStr_->switchList()); - blockAllSignals(false); - int response = settingsDialog_->run(); - if ( response == settingsDialog_->OK_RESPONSE ) { - onSettingsDialogOk(settingsDialog_->getSettings()); - } - } + blockAllSignals(true); + settingsDialog_->initParms(settingsStr_); + settingsDialog_->updateMixers(settingsStr_->mixerList()); + settingsDialog_->updateSwitches(settingsStr_->switchList()); + blockAllSignals(false); + int response = settingsDialog_->run(); + if ( response == settingsDialog_->OK_RESPONSE ) { + onSettingsDialogOk(settingsDialog_->getSettings()); + } + } } void Core::saveSettings() { - settings_->saveSoundCard(settingsStr_->cardId()); - settings_->saveMixer(std::string(mixerName_.c_str())); - settings_->saveNotebookOrientation(settingsStr_->notebookOrientation()); - settings_->setUsePulse(isPulse_); - settings_->setAutorun(settingsStr_->isAutorun()); - settings_->setUsePolling(settingsStr_->usePolling()); - settings_->saveMixerId(settingsStr_->mixerId()); + settings_->saveSoundCard(settingsStr_->cardId()); + settings_->saveMixer(std::string(mixerName_.c_str())); + settings_->saveNotebookOrientation(settingsStr_->notebookOrientation()); + settings_->setUsePulse(isPulse_); + settings_->setAutorun(settingsStr_->isAutorun()); + settings_->setUsePolling(settingsStr_->usePolling()); + settings_->saveMixerId(settingsStr_->mixerId()); #ifdef HAVE_PULSE - if (pulse_) { - settings_->savePulseDeviceName(pulseDevice_); - } + if (pulse_) { + settings_->savePulseDeviceName(pulseDevice_); + } #endif } void Core::onSettingsDialogOk(const settingsStr::Ptr &str) { - settingsStr_->setCardId(str->cardId()); - settingsStr_->setMixerId(str->mixerId()); - settingsStr_->setNotebookOrientation(str->notebookOrientation()); - settingsStr_->setIsAutorun(str->isAutorun()); - settingsStr_->setUsePolling(str->usePolling()); - updateControls(settingsStr_->cardId()); + settingsStr_->setCardId(str->cardId()); + settingsStr_->setMixerId(str->mixerId()); + settingsStr_->setNotebookOrientation(str->notebookOrientation()); + settingsStr_->setIsAutorun(str->isAutorun()); + settingsStr_->setUsePolling(str->usePolling()); + updateControls(settingsStr_->cardId()); #ifdef HAVE_PULSE - if (isPulse_ && pulse_) { - volumeValue_ = pulse_->getVolume(); - } + if (isPulse_ && pulse_) { + volumeValue_ = pulse_->getVolume(); + } #endif - saveSettings(); + saveSettings(); } #ifdef HAVE_PULSE void Core::onSettingsDialogUsePulse(bool isPulse) { - if (pulse_) { - isPulse_ = isPulse; - settingsStr_->setUsePulse(isPulse); - settings_->setUsePulse(isPulse); - updateTrayIcon(volumeValue_); - } + if (pulse_) { + isPulse_ = isPulse; + settingsStr_->setUsePulse(isPulse); + settings_->setUsePulse(isPulse); + updateTrayIcon(volumeValue_); + } } #endif void Core::switchChanged(const std::string &name, int id, bool enabled) { - alsaWork_->setSwitch(name, id, enabled); + alsaWork_->setSwitch(name, id, enabled); } void Core::soundMuted(bool mute) { - signal_timer_.block(true); - if (!isPulse_) { - isMuted_ = mute; - alsaWork_->setMute(mute); - } + signal_timer_.block(true); + if (!isPulse_) { + isMuted_ = mute; + alsaWork_->setMute(mute); + } #ifdef HAVE_PULSE - else if (pulse_) { - isMuted_ = mute; - pulse_->setMute(mute); - } + else if (pulse_) { + isMuted_ = mute; + pulse_->setMute(mute); + } #endif - signal_timer_.block(false); + signal_timer_.block(false); } bool Core::getMuted() { - if (!isPulse_) { - isMuted_ = !alsaWork_->getMute(); - return isMuted_; - } + if (!isPulse_) { + isMuted_ = !alsaWork_->getMute(); + return isMuted_; + } #ifdef HAVE_PULSE - else if (pulse_) { - isMuted_ = pulse_->getMute(); - return isMuted_; - } + else if (pulse_) { + isMuted_ = pulse_->getMute(); + return isMuted_; + } #endif - return false; + return false; } void Core::updateControls(int cardId) { - settingsStr_->clear(MIXERS); - settingsStr_->clearSwitches(); - const int soundCardId = (alsaWork_->cardExists(cardId)) ? cardId : alsaWork_->getFirstCardWithMixers(); - alsaWork_->setCurrentCard(soundCardId); - settingsStr_->setCardId(soundCardId); - settingsStr_->setList(MIXERS, alsaWork_->getVolumeMixers()); - settingsStr_->addMixerSwitch(alsaWork_->getSwitchList()); - const std::string newName = alsaWork_->getMixerName(settingsStr_->mixerId()); - if (mixerName_ != newName) { - mixerName_ = newName; - alsaWork_->setCurrentMixer(mixerName_); - } - if (!isPulse_) { - volumeValue_ = alsaWork_->getAlsaVolume(); - } + settingsStr_->clear(MIXERS); + settingsStr_->clearSwitches(); + const int soundCardId = (alsaWork_->cardExists(cardId)) ? cardId : alsaWork_->getFirstCardWithMixers(); + alsaWork_->setCurrentCard(soundCardId); + settingsStr_->setCardId(soundCardId); + settingsStr_->setList(MIXERS, alsaWork_->getVolumeMixers()); + settingsStr_->addMixerSwitch(alsaWork_->getSwitchList()); + const std::string newName = alsaWork_->getMixerName(settingsStr_->mixerId()); + if (mixerName_ != newName) { + mixerName_ = newName; + alsaWork_->setCurrentMixer(mixerName_); + } + if (!isPulse_) { + volumeValue_ = alsaWork_->getAlsaVolume(); + } #ifdef HAVE_PULSE - else if (pulse_) { - volumeValue_ = pulse_->getVolume(); - } + else if (pulse_) { + volumeValue_ = pulse_->getVolume(); + } #endif - m_signal_volume_changed(volumeValue_); - updateTrayIcon(volumeValue_); - m_signal_mixer_muted(getMuted()); - if(settingsDialog_) { - blockAllSignals(true); - settingsDialog_->updateMixers(settingsStr_->mixerList()); - settingsDialog_->updateSwitches(settingsStr_->switchList()); - blockAllSignals(false); - } + m_signal_volume_changed(volumeValue_); + updateTrayIcon(volumeValue_); + m_signal_mixer_muted(getMuted()); + if(settingsDialog_) { + blockAllSignals(true); + settingsDialog_->updateMixers(settingsStr_->mixerList()); + settingsDialog_->updateSwitches(settingsStr_->switchList()); + blockAllSignals(false); + } } #ifdef HAVE_PULSE void Core::updatePulseDevices(int deviceId) { - if (pulse_) { - const std::string olddev = settingsStr_->pulseDeviceName(); - pulse_->refreshDevices(); - settingsStr_->setPulseDevices(pulse_->getCardList()); - const std::string currDev = pulse_->getDeviceNameByIndex(deviceId); - pulse_->setCurrentDevice(currDev); - pulseDevice_ = currDev; - pulseDeviceDesc_ = pulse_->getDeviceDescription(pulseDevice_); - settingsStr_->setPulseDeviceName(pulseDevice_); - settingsStr_->setPulseDeviceDesc(pulseDeviceDesc_); - settingsStr_->setPulseDeviceId(pulse_->getCurrentDeviceIndex()); - if ( olddev != currDev ) { - updateControls(pulse_->getCardIndex()); - } - } + if (pulse_) { + const std::string olddev = settingsStr_->pulseDeviceName(); + pulse_->refreshDevices(); + settingsStr_->setPulseDevices(pulse_->getCardList()); + const std::string currDev = pulse_->getDeviceNameByIndex(deviceId); + pulse_->setCurrentDevice(currDev); + pulseDevice_ = currDev; + pulseDeviceDesc_ = pulse_->getDeviceDescription(pulseDevice_); + settingsStr_->setPulseDeviceName(pulseDevice_); + settingsStr_->setPulseDeviceDesc(pulseDeviceDesc_); + settingsStr_->setPulseDeviceId(pulse_->getCurrentDeviceIndex()); + if ( olddev != currDev ) { + updateControls(pulse_->getCardIndex()); + } + } } #endif std::string Core::getSoundCardName() const { - std::string result; - if (!isPulse_) { - result = alsaWork_->getCardName(settingsStr_->cardId()); - } + std::string result; + if (!isPulse_) { + result = alsaWork_->getCardName(settingsStr_->cardId()); + } #ifdef HAVE_PULSE - else if (pulse_) { - result = pulseDeviceDesc_; - } + else if (pulse_) { + result = pulseDeviceDesc_; + } #endif - return result; + return result; } std::string Core::getActiveMixer() const { - return ((!isPulse_) ? alsaWork_->getCurrentMixerName() : std::string()); + return ((!isPulse_) ? alsaWork_->getCurrentMixerName() : std::string()); } double Core::getVolumeValue() const { - if (!isPulse_) { - return alsaWork_->getAlsaVolume(); - } + if (!isPulse_) { + return alsaWork_->getAlsaVolume(); + } #ifdef HAVE_PULSE - else if (pulse_) { - return pulse_->getVolume(); - } + else if (pulse_) { + return pulse_->getVolume(); + } #endif - return 0.0; + return 0.0; } void Core::onTrayIconScroll(double value) { - volumeValue_ += value; - if (volumeValue_ >= 100) { - volumeValue_ = 100; - } - else if (volumeValue_ <= 0){ - volumeValue_ = 0; - } - m_signal_volume_changed(volumeValue_); //send signal to sliderwindow + volumeValue_ += value; + if (volumeValue_ >= 100) { + volumeValue_ = 100; + } + else if (volumeValue_ <= 0){ + volumeValue_ = 0; + } + m_signal_volume_changed(volumeValue_); //send signal to sliderwindow } void Core::onVolumeSlider(double value) { - signal_timer_.block(true); - volumeValue_ = value; - pollVolume_ = value; - if (!isPulse_) { - alsaWork_->setAlsaVolume(value); - pollVolume_ = alsaWork_->getAlsaVolume(); - } + signal_timer_.block(true); + volumeValue_ = value; + pollVolume_ = value; + if (!isPulse_) { + alsaWork_->setAlsaVolume(value); + pollVolume_ = alsaWork_->getAlsaVolume(); + } #ifdef HAVE_PULSE - else if (pulse_) { - pulse_->setVolume(int(value)); - } + else if (pulse_) { + pulse_->setVolume(int(value)); + } #endif - updateTrayIcon(value); - signal_timer_.block(false); + updateTrayIcon(value); + signal_timer_.block(false); } void Core::updateTrayIcon(double value) { - if (!isPulse_) { - const std::string mixer = getActiveMixer(); - if (!mixer.empty()) { - m_signal_value_changed(value, getSoundCardName(), mixer); - } - else { - errorDialog(_("Sound card not contains any volume control mixers")); - m_signal_value_changed(value, getSoundCardName(), std::string("N/A")); - } - } - else { - m_signal_value_changed(value, getSoundCardName(), std::string()); - } + if (!isPulse_) { + const std::string mixer = getActiveMixer(); + if (!mixer.empty()) { + m_signal_value_changed(value, getSoundCardName(), mixer); + } + else { + errorDialog(_("Sound card not contains any volume control mixers")); + m_signal_value_changed(value, getSoundCardName(), std::string("N/A")); + } + } + else { + m_signal_value_changed(value, getSoundCardName(), std::string()); + } } bool Core::onTimeout() { - if (settingsStr_->usePolling()) { - if (!isPulse_) { + if (settingsStr_->usePolling()) { + if (!isPulse_) { #ifdef IS_DEBUG - std::cout << "mixerName= " << mixerName_ << std::endl; - std::cout << "CurrentMixerName= " << alsaWork_->getCurrentMixerName() << std::endl; + std::cout << "mixerName= " << mixerName_ << std::endl; + std::cout << "CurrentMixerName= " << alsaWork_->getCurrentMixerName() << std::endl; #endif - if (mixerName_ != alsaWork_->getCurrentMixerName()) { - return true; - } - const double volume = alsaWork_->getAlsaVolume(); - bool ismute = !alsaWork_->getMute(); - if (pollVolume_ != volume) { - pollVolume_ = volume; - m_signal_volume_changed(pollVolume_); - } - if (ismute != isMuted_) { - isMuted_ = ismute; - m_signal_mixer_muted(isMuted_); - } - } + if (mixerName_ != alsaWork_->getCurrentMixerName()) { + return true; + } + const double volume = alsaWork_->getAlsaVolume(); + bool ismute = !alsaWork_->getMute(); + if (pollVolume_ != volume) { + pollVolume_ = volume; + m_signal_volume_changed(pollVolume_); + } + if (ismute != isMuted_) { + isMuted_ = ismute; + m_signal_mixer_muted(isMuted_); + } + } #ifdef HAVE_PULSE - if (isPulse_ && pulse_) { - const int index = pulse_->getCurrentDeviceIndex(); - if (pulseDevice_ != pulse_->getDeviceNameByIndex(index)) { - return true; - } - const int volume = pulse_->getVolume(); - bool ismute = pulse_->getMute(); - if (pollVolume_ != volume) { - pollVolume_ = volume; - m_signal_volume_changed(pollVolume_); - } - if (ismute != isMuted_) { - isMuted_ = ismute; - m_signal_mixer_muted(isMuted_); - } - } + if (isPulse_ && pulse_) { + const int index = pulse_->getCurrentDeviceIndex(); + if (pulseDevice_ != pulse_->getDeviceNameByIndex(index)) { + return true; + } + const int volume = pulse_->getVolume(); + bool ismute = pulse_->getMute(); + if (pollVolume_ != volume) { + pollVolume_ = volume; + m_signal_volume_changed(pollVolume_); + } + if (ismute != isMuted_) { + isMuted_ = ismute; + m_signal_mixer_muted(isMuted_); + } + } #endif - } - return true; + } + return true; } Core::type_double_signal Core::signal_volume_changed() { - return m_signal_volume_changed; + return m_signal_volume_changed; } Core::type_volumevalue_signal Core::signal_value_changed() { - return m_signal_value_changed; + return m_signal_value_changed; } Core::type_bool_signal Core::signal_mixer_muted() { - return m_signal_mixer_muted; + return m_signal_mixer_muted; } diff --git a/tools/core.h b/tools/core.h index 090687e..657ddcf 100644 --- a/tools/core.h +++ b/tools/core.h @@ -1,6 +1,6 @@ /* * core.h - * Copyright (C) 2013-2015 Vitaly Tonkacheyev + * Copyright (C) 2013-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -33,69 +33,69 @@ class Core { public: - explicit Core(const Glib::RefPtr& refGlade); - Core(Core const &); - ~Core(); - typedef std::shared_ptr Ptr; - void runAboutDialog(); - std::string getSoundCardName() const; - std::string getActiveMixer() const; - void setActiveMixer(int index); - void saveSettings(); - void runSettings(); - void switchChanged (const std::string& name, int id, bool enabled); - void soundMuted(bool mute); - bool getMuted(); - void onExtMixerSignal(); - double getVolumeValue() const; - void onTrayIconScroll(double value); - void onVolumeSlider(double value); - void onSettingsDialogOk(const settingsStr::Ptr &str); - void onSettingsDialogUsePulse(bool isPulse); + explicit Core(const Glib::RefPtr& refGlade); + Core(Core const &); + ~Core(); + typedef std::shared_ptr Ptr; + void runAboutDialog(); + std::string getSoundCardName() const; + std::string getActiveMixer() const; + void setActiveMixer(int index); + void saveSettings(); + void runSettings(); + void switchChanged (const std::string& name, int id, bool enabled); + void soundMuted(bool mute); + bool getMuted(); + void onExtMixerSignal(); + double getVolumeValue() const; + void onTrayIconScroll(double value); + void onVolumeSlider(double value); + void onSettingsDialogOk(const settingsStr::Ptr &str); + void onSettingsDialogUsePulse(bool isPulse); - typedef sigc::signal type_double_signal; - type_double_signal signal_volume_changed(); - typedef sigc::signal type_volumevalue_signal; - type_volumevalue_signal signal_value_changed(); - typedef sigc::signal type_bool_signal; - type_bool_signal signal_mixer_muted(); + typedef sigc::signal type_double_signal; + type_double_signal signal_volume_changed(); + typedef sigc::signal type_volumevalue_signal; + type_volumevalue_signal signal_value_changed(); + typedef sigc::signal type_bool_signal; + type_bool_signal signal_mixer_muted(); private: - type_double_signal m_signal_volume_changed; - type_volumevalue_signal m_signal_value_changed; - type_bool_signal m_signal_mixer_muted; - void updateControls(int cardId); + type_double_signal m_signal_volume_changed; + type_volumevalue_signal m_signal_value_changed; + type_bool_signal m_signal_mixer_muted; + void updateControls(int cardId); #ifdef HAVE_PULSE - void updatePulseDevices(int deviceId); - void initPulseAudio(); + void updatePulseDevices(int deviceId); + void initPulseAudio(); #endif - void updateSettings(int cardId); - void updateTrayIcon(double value); - void blockAllSignals(bool isblock); - void errorDialog(const std::string &errorMessage); - bool onTimeout(); + void updateSettings(int cardId); + void updateTrayIcon(double value); + void blockAllSignals(bool isblock); + void errorDialog(const std::string &errorMessage); + bool onTimeout(); private: - Settings::Ptr settings_; - AlsaWork::Ptr alsaWork_; - settingsStr::Ptr settingsStr_; - std::string mixerName_; - double volumeValue_; - double pollVolume_; - SettingsFrame *settingsDialog_; - bool isPulse_; - bool isMuted_; - std::vector alsaCards_; - sigc::connection signal_switches_; - sigc::connection signal_sndcard_; - sigc::connection signal_timer_; + Settings::Ptr settings_; + AlsaWork::Ptr alsaWork_; + settingsStr::Ptr settingsStr_; + std::string mixerName_; + double volumeValue_; + double pollVolume_; + SettingsFrame *settingsDialog_; + bool isPulse_; + bool isMuted_; + std::vector alsaCards_; + sigc::connection signal_switches_; + sigc::connection signal_sndcard_; + sigc::connection signal_timer_; #ifdef HAVE_PULSE - PulseCore::Ptr pulse_; - std::string pulseDevice_; - std::string pulseDeviceDesc_; - std::vector pulseDevices_; - sigc::connection signal_pulsdev_; - sigc::connection signal_pulsedevices_; + PulseCore::Ptr pulse_; + std::string pulseDevice_; + std::string pulseDeviceDesc_; + std::vector pulseDevices_; + sigc::connection signal_pulsdev_; + sigc::connection signal_pulsedevices_; #endif }; diff --git a/tools/settings.cpp b/tools/settings.cpp index b230572..b8aca42 100644 --- a/tools/settings.cpp +++ b/tools/settings.cpp @@ -1,6 +1,6 @@ /* * settings.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -34,216 +34,216 @@ #define USEPOLL "usepolling" Settings::Settings() -: configFile_(new Glib::KeyFile()), - desktopFile_(new Glib::KeyFile()) + : configFile_(new Glib::KeyFile()), + desktopFile_(new Glib::KeyFile()) { - const std::string configDir = std::string(Tools::getHomePath() + "/.config/cppAlsaVolume"); - iniFileName_ = configDir + std::string("/config.ini"); - desktopFilePath_ = std::string(Tools::getHomePath() + "/.config/autostart/alsavolume.desktop"); - Tools::createDirectory(configDir); - loadConfig(iniFileName_); - loadDesktopFile(desktopFilePath_); + const std::string configDir = std::string(Tools::getHomePath() + "/.config/cppAlsaVolume"); + iniFileName_ = configDir + std::string("/config.ini"); + desktopFilePath_ = std::string(Tools::getHomePath() + "/.config/autostart/alsavolume.desktop"); + Tools::createDirectory(configDir); + loadConfig(iniFileName_); + loadDesktopFile(desktopFilePath_); } Settings::~Settings() { - delete configFile_; - delete desktopFile_; + delete configFile_; + delete desktopFile_; } void Settings::loadConfig(const std::string& fileName) { - if (!Tools::checkFileExists(fileName)) { - parseConfig(iniFileName_, std::string("")); - } - try { - configFile_->load_from_file(fileName); - } - catch (Glib::FileError &err) { - std::cerr << "settings.cpp::51:: " << fileName << " - " << err.what() << std::endl; - } + if (!Tools::checkFileExists(fileName)) { + parseConfig(iniFileName_, std::string("")); + } + try { + configFile_->load_from_file(fileName); + } + catch (Glib::FileError &err) { + std::cerr << "settings.cpp::51:: " << fileName << " - " << err.what() << std::endl; + } } void Settings::parseConfig(const Glib::ustring& keyFileName, const Glib::ustring& keyFileData) { - Tools::saveFile(keyFileName, keyFileData); + Tools::saveFile(keyFileName, keyFileData); } void Settings::saveSoundCard(int soundCard) { - configFile_->set_integer(Glib::ustring(MAIN),Glib::ustring(CARD),soundCard); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_integer(Glib::ustring(MAIN),Glib::ustring(CARD),soundCard); + parseConfig(iniFileName_, configFile_->to_data()); } void Settings::saveMixer(const std::string &mixerName) { - configFile_->set_string(Glib::ustring(MAIN),Glib::ustring(MIXER),mixerName); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_string(Glib::ustring(MAIN),Glib::ustring(MIXER),mixerName); + parseConfig(iniFileName_, configFile_->to_data()); } void Settings::saveMixerId(int mixerId) { - configFile_->set_integer(Glib::ustring(MAIN),Glib::ustring(MIXERID),mixerId); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_integer(Glib::ustring(MAIN),Glib::ustring(MIXERID),mixerId); + parseConfig(iniFileName_, configFile_->to_data()); } void Settings::saveNotebookOrientation(bool orient) { - configFile_->set_boolean(Glib::ustring(MAIN),Glib::ustring(ORIENT),orient); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_boolean(Glib::ustring(MAIN),Glib::ustring(ORIENT),orient); + parseConfig(iniFileName_, configFile_->to_data()); } void Settings::loadDesktopFile(const std::string &fileName) { - if (!Tools::checkFileExists(fileName)) { - initDesktopFileData(); - parseConfig(desktopFilePath_, desktopFile_->to_data()); - } - try { - desktopFile_->load_from_file(fileName); - } - catch (Glib::FileError &err) { - std::cerr << "settings.cpp::143:: " << fileName << " - " << err.what() << std::endl; - } + if (!Tools::checkFileExists(fileName)) { + initDesktopFileData(); + parseConfig(desktopFilePath_, desktopFile_->to_data()); + } + try { + desktopFile_->load_from_file(fileName); + } + catch (Glib::FileError &err) { + std::cerr << "settings.cpp::143:: " << fileName << " - " << err.what() << std::endl; + } } void Settings::initDesktopFileData() { - const Glib::ustring entry = Glib::ustring("Desktop Entry"); - desktopFile_->set_string(entry, Glib::ustring("Encoding"),Glib::ustring("UTF-8")); - desktopFile_->set_string(entry,Glib::ustring("Name"),Glib::ustring("AlsaVolume")); - desktopFile_->set_string(entry,Glib::ustring("Comment"),Glib::ustring("Changes the volume of ALSA from the system tray")); - desktopFile_->set_string(entry,Glib::ustring("Exec"),Glib::ustring("alsavolume")); - desktopFile_->set_string(entry,Glib::ustring("Type"),Glib::ustring("Application")); - desktopFile_->set_string(entry,Glib::ustring("Version"),Glib::ustring(version_)); - desktopFile_->set_boolean(entry,Glib::ustring("Hidden"),true); - desktopFile_->set_string(entry,Glib::ustring("Comment[ru]"),Glib::ustring("Регулятор громкости ALSA")); + const Glib::ustring entry = Glib::ustring("Desktop Entry"); + desktopFile_->set_string(entry, Glib::ustring("Encoding"),Glib::ustring("UTF-8")); + desktopFile_->set_string(entry,Glib::ustring("Name"),Glib::ustring("AlsaVolume")); + desktopFile_->set_string(entry,Glib::ustring("Comment"),Glib::ustring("Changes the volume of ALSA from the system tray")); + desktopFile_->set_string(entry,Glib::ustring("Exec"),Glib::ustring("alsavolume")); + desktopFile_->set_string(entry,Glib::ustring("Type"),Glib::ustring("Application")); + desktopFile_->set_string(entry,Glib::ustring("Version"),Glib::ustring(version_)); + desktopFile_->set_boolean(entry,Glib::ustring("Hidden"),true); + desktopFile_->set_string(entry,Glib::ustring("Comment[ru]"),Glib::ustring("Регулятор громкости ALSA")); } void Settings::setAutorun(bool isAutorun) { - desktopFile_->set_boolean(Glib::ustring("Desktop Entry"),Glib::ustring("Hidden"),!isAutorun); - parseConfig(desktopFilePath_, desktopFile_->to_data()); + desktopFile_->set_boolean(Glib::ustring("Desktop Entry"),Glib::ustring("Hidden"),!isAutorun); + parseConfig(desktopFilePath_, desktopFile_->to_data()); } void Settings::setVersion(const Glib::ustring &version) { - version_ = version; - desktopFile_->set_string(Glib::ustring("Desktop Entry"),Glib::ustring("Version"), version); - parseConfig(desktopFilePath_, desktopFile_->to_data()); + version_ = version; + desktopFile_->set_string(Glib::ustring("Desktop Entry"),Glib::ustring("Version"), version); + parseConfig(desktopFilePath_, desktopFile_->to_data()); } void Settings::setUsePulse(bool use) { - configFile_->set_boolean(Glib::ustring(MAIN), Glib::ustring(ISPULSE), use); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_boolean(Glib::ustring(MAIN), Glib::ustring(ISPULSE), use); + parseConfig(iniFileName_, configFile_->to_data()); } void Settings::setUsePolling(bool use) { - configFile_->set_boolean(Glib::ustring(MAIN), Glib::ustring(USEPOLL), use); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_boolean(Glib::ustring(MAIN), Glib::ustring(USEPOLL), use); + parseConfig(iniFileName_, configFile_->to_data()); } void Settings::savePulseDeviceName(const std::string &name) { - configFile_->set_string(Glib::ustring(MAIN), Glib::ustring(PULSEDEV), Glib::ustring(name)); - parseConfig(iniFileName_, configFile_->to_data()); + configFile_->set_string(Glib::ustring(MAIN), Glib::ustring(PULSEDEV), Glib::ustring(name)); + parseConfig(iniFileName_, configFile_->to_data()); } int Settings::getSoundCard() const { - int card = 0; - try { - card = (int)configFile_->get_integer(Glib::ustring(MAIN),Glib::ustring(CARD)); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::160::KeyFileError " << ex.what() << std::endl; - } - return card; + int card = 0; + try { + card = (int)configFile_->get_integer(Glib::ustring(MAIN),Glib::ustring(CARD)); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::160::KeyFileError " << ex.what() << std::endl; + } + return card; } int Settings::getMixerId() const { - int id = 0; - try { - id = (int)configFile_->get_integer(Glib::ustring(MAIN),Glib::ustring(MIXERID)); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::172::KeyFileError " << ex.what() << std::endl; - } - return id; + int id = 0; + try { + id = (int)configFile_->get_integer(Glib::ustring(MAIN),Glib::ustring(MIXERID)); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::172::KeyFileError " << ex.what() << std::endl; + } + return id; } Glib::ustring Settings::getMixer() const { - Glib::ustring mixer(""); - try { - mixer = Glib::ustring(configFile_->get_string(Glib::ustring(MAIN),Glib::ustring(MIXER))); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::184::KeyFileError " << ex.what() << std::endl; - } - return mixer; + Glib::ustring mixer(""); + try { + mixer = Glib::ustring(configFile_->get_string(Glib::ustring(MAIN),Glib::ustring(MIXER))); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::184::KeyFileError " << ex.what() << std::endl; + } + return mixer; } bool Settings::getNotebookOrientation() { - bool orient = false; - try { - orient = bool(configFile_->get_boolean(Glib::ustring(MAIN),Glib::ustring(ORIENT))); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::196::KeyFileError " << ex.what() << std::endl; - } - return orient; + bool orient = false; + try { + orient = bool(configFile_->get_boolean(Glib::ustring(MAIN),Glib::ustring(ORIENT))); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::196::KeyFileError " << ex.what() << std::endl; + } + return orient; } bool Settings::getAutorun() { - bool isAutorun = false; - try { - isAutorun = !bool(desktopFile_->get_boolean(Glib::ustring("Desktop Entry"),Glib::ustring("Hidden"))); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::208::KeyFileError " << ex.what() << std::endl; - } - return isAutorun; + bool isAutorun = false; + try { + isAutorun = !bool(desktopFile_->get_boolean(Glib::ustring("Desktop Entry"),Glib::ustring("Hidden"))); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::208::KeyFileError " << ex.what() << std::endl; + } + return isAutorun; } std::string Settings::pulseDeviceName() const { - std::string device(""); - try { - device = std::string(configFile_->get_string(Glib::ustring(MAIN),Glib::ustring(PULSEDEV))); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::220::KeyFileError " << ex.what() << std::endl; - } - return device; + std::string device(""); + try { + device = std::string(configFile_->get_string(Glib::ustring(MAIN),Glib::ustring(PULSEDEV))); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::220::KeyFileError " << ex.what() << std::endl; + } + return device; } bool Settings::usePulse() { - bool isPulse = false; - try { - isPulse = bool(configFile_->get_boolean(Glib::ustring(MAIN),Glib::ustring(ISPULSE))); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::232::KeyFileError " << ex.what() << std::endl; - } - return isPulse; + bool isPulse = false; + try { + isPulse = bool(configFile_->get_boolean(Glib::ustring(MAIN),Glib::ustring(ISPULSE))); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::232::KeyFileError " << ex.what() << std::endl; + } + return isPulse; } bool Settings::usePolling() { - bool isPolling = true; - try { - isPolling = bool(configFile_->get_boolean(Glib::ustring(MAIN),Glib::ustring(USEPOLL))); - } - catch (const Glib::KeyFileError& ex) { - std::cerr << "settings.cpp::244::KeyFileError " << ex.what() << std::endl; - } - return isPolling; + bool isPolling = true; + try { + isPolling = bool(configFile_->get_boolean(Glib::ustring(MAIN),Glib::ustring(USEPOLL))); + } + catch (const Glib::KeyFileError& ex) { + std::cerr << "settings.cpp::244::KeyFileError " << ex.what() << std::endl; + } + return isPolling; } diff --git a/tools/settings.h b/tools/settings.h index 9fae67b..96d8ea1 100644 --- a/tools/settings.h +++ b/tools/settings.h @@ -1,6 +1,6 @@ /* * settings.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,40 +25,40 @@ class Settings { public: - Settings(); - Settings(Settings const &); - ~Settings(); - typedef std::shared_ptr Ptr; - int getSoundCard() const; - int getMixerId() const; - Glib::ustring getMixer() const; - bool getNotebookOrientation(); - bool getAutorun(); - bool usePulse(); - bool usePolling(); - std::string pulseDeviceName() const; - void saveSoundCard(int soundCard); - void saveMixer(const std::string& mixerName); - void saveMixerId(int mixerId); - void saveNotebookOrientation(bool orient); - void setAutorun(bool isAutorun); - void setVersion(const Glib::ustring& version); - void setUsePulse(bool use); - void savePulseDeviceName(const std::string &name); - void setUsePolling(bool use); + Settings(); + Settings(Settings const &); + ~Settings(); + typedef std::shared_ptr Ptr; + int getSoundCard() const; + int getMixerId() const; + Glib::ustring getMixer() const; + bool getNotebookOrientation(); + bool getAutorun(); + bool usePulse(); + bool usePolling(); + std::string pulseDeviceName() const; + void saveSoundCard(int soundCard); + void saveMixer(const std::string& mixerName); + void saveMixerId(int mixerId); + void saveNotebookOrientation(bool orient); + void setAutorun(bool isAutorun); + void setVersion(const Glib::ustring& version); + void setUsePulse(bool use); + void savePulseDeviceName(const std::string &name); + void setUsePolling(bool use); private: - void parseConfig(const Glib::ustring& keyFileName, const Glib::ustring& keyFileData); - void loadConfig(const std::string& fileName); - void loadDesktopFile(const std::string& fileName); - void initDesktopFileData(); + void parseConfig(const Glib::ustring& keyFileName, const Glib::ustring& keyFileData); + void loadConfig(const std::string& fileName); + void loadDesktopFile(const std::string& fileName); + void initDesktopFileData(); private: - Glib::KeyFile *configFile_; - Glib::KeyFile *desktopFile_; - std::string iniFileName_; - std::string desktopFilePath_; - std::string version_; + Glib::KeyFile *configFile_; + Glib::KeyFile *desktopFile_; + std::string iniFileName_; + std::string desktopFilePath_; + std::string version_; }; #endif // SETTINGS_H diff --git a/tools/settingsstr.cpp b/tools/settingsstr.cpp index 96071e5..e798d37 100644 --- a/tools/settingsstr.cpp +++ b/tools/settingsstr.cpp @@ -1,6 +1,6 @@ /* * settingsstr.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,36 +20,36 @@ #include "settingsstr.h" settingsStr::settingsStr() -: cardId_(0), - mixerId_(0), - notebookOrientation_(false), - isAutorun_(false), - usePulse_(false), - usePolling_(true), - cardList_(std::vector()), - mixerList_(std::vector()), - switchList_(MixerSwitches::Ptr()), - pulseDevices_(std::vector()), - pulseDeviceId_(0), - pulseDeviceName_(std::string()), - pulseDeviceDesc_(std::string()) + : cardId_(0), + mixerId_(0), + notebookOrientation_(false), + isAutorun_(false), + usePulse_(false), + usePolling_(true), + cardList_(std::vector()), + mixerList_(std::vector()), + switchList_(MixerSwitches::Ptr()), + pulseDevices_(std::vector()), + pulseDeviceId_(0), + pulseDeviceName_(std::string()), + pulseDeviceDesc_(std::string()) { } settingsStr::settingsStr(settingsStr &str) -: cardId_(str.cardId()), - mixerId_(str.mixerId()), - notebookOrientation_(str.notebookOrientation()), - isAutorun_(str.isAutorun()), - usePulse_(str.usePulse()), - usePolling_(str.usePolling()), - cardList_(str.cardList()), - mixerList_(str.mixerList()), - switchList_(str.switchList()), - pulseDevices_(str.pulseDevices()), - pulseDeviceId_(str.pulseDeviceId()), - pulseDeviceName_(str.pulseDeviceName()), - pulseDeviceDesc_(str.pulseDeviceDesc()) + : cardId_(str.cardId()), + mixerId_(str.mixerId()), + notebookOrientation_(str.notebookOrientation()), + isAutorun_(str.isAutorun()), + usePulse_(str.usePulse()), + usePolling_(str.usePolling()), + cardList_(str.cardList()), + mixerList_(str.mixerList()), + switchList_(str.switchList()), + pulseDevices_(str.pulseDevices()), + pulseDeviceId_(str.pulseDeviceId()), + pulseDeviceName_(str.pulseDeviceName()), + pulseDeviceDesc_(str.pulseDeviceDesc()) { } @@ -59,171 +59,171 @@ settingsStr::~settingsStr() unsigned int settingsStr::cardId() const { - return cardId_; + return cardId_; } unsigned int settingsStr::mixerId() const { - return mixerId_; + return mixerId_; } bool settingsStr::notebookOrientation() { - return notebookOrientation_; + return notebookOrientation_; } bool settingsStr::isAutorun() { - return isAutorun_; + return isAutorun_; } bool settingsStr::usePolling() { - return usePolling_; + return usePolling_; } const std::vector &settingsStr::cardList() const { - return cardList_; + return cardList_; } const std::vector &settingsStr::mixerList() const { - return mixerList_; + return mixerList_; } int settingsStr::pulseDeviceId() const { - return pulseDeviceId_; + return pulseDeviceId_; } const std::string &settingsStr::pulseDeviceName() const { - return pulseDeviceName_; + return pulseDeviceName_; } const std::string &settingsStr::pulseDeviceDesc() const { - return pulseDeviceDesc_; + return pulseDeviceDesc_; } const std::vector &settingsStr::pulseDevices() const { - return pulseDevices_; + return pulseDevices_; } bool settingsStr::usePulse() { - return usePulse_; + return usePulse_; } void settingsStr::setCardId(unsigned int id) { - cardId_ = id; + cardId_ = id; } void settingsStr::setMixerId(unsigned int id) { - mixerId_ = id; + mixerId_ = id; } void settingsStr::setNotebookOrientation(bool orient) { - notebookOrientation_ = orient; + notebookOrientation_ = orient; } void settingsStr::setIsAutorun(bool autorun) { - isAutorun_ = autorun; + isAutorun_ = autorun; } void settingsStr::pushBack(ListType listType, const std::string &item) { - switch (listType) { - case CARDS: - cardList_.push_back(item); - break; - case MIXERS: - mixerList_.push_back(item); - break; - } + switch (listType) { + case CARDS: + cardList_.push_back(item); + break; + case MIXERS: + mixerList_.push_back(item); + break; + } } void settingsStr::addMixerSwitch(const MixerSwitches::Ptr &switchItem) { - switchList_ = switchItem; + switchList_ = switchItem; } void settingsStr::setList(ListType listType,const std::vector &list) { - switch (listType) { - case CARDS: - cardList_.assign(list.begin(),list.end()); - break; - case MIXERS: - mixerList_.assign(list.begin(),list.end()); - break; - } + switch (listType) { + case CARDS: + cardList_.assign(list.begin(),list.end()); + break; + case MIXERS: + mixerList_.assign(list.begin(),list.end()); + break; + } } MixerSwitches::Ptr settingsStr::switchList() const { - return switchList_; + return switchList_; } void settingsStr::clear(ListType listType) { - switch (listType) { - case CARDS: - if (!cardList_.empty()) - cardList_.clear(); - break; - case MIXERS: - if (!mixerList_.empty()) - mixerList_.clear(); - break; - } + switch (listType) { + case CARDS: + if (!cardList_.empty()) + cardList_.clear(); + break; + case MIXERS: + if (!mixerList_.empty()) + mixerList_.clear(); + break; + } } void settingsStr::clearSwitches() { - if (switchList_) { - switchList_->clear(PLAYBACK); - switchList_->clear(CAPTURE); - switchList_->clear(ENUM); - } + if (switchList_) { + switchList_->clear(PLAYBACK); + switchList_->clear(CAPTURE); + switchList_->clear(ENUM); + } } void settingsStr::setPulseDeviceId(int id) { - pulseDeviceId_ = id; + pulseDeviceId_ = id; } void settingsStr::setPulseDeviceName(const std::string &name) { - pulseDeviceName_ = name; + pulseDeviceName_ = name; } void settingsStr::setPulseDeviceDesc(const std::string &description) { - pulseDeviceDesc_ = description; + pulseDeviceDesc_ = description; } void settingsStr::setPulseDevices(const std::vector &devices) { - if(!pulseDevices_.empty() && !devices.empty()) - pulseDevices_.clear(); - pulseDevices_.assign(devices.begin(), devices.end()); + if(!pulseDevices_.empty() && !devices.empty()) + pulseDevices_.clear(); + pulseDevices_.assign(devices.begin(), devices.end()); } void settingsStr::setUsePulse(bool use) { - usePulse_ = use; + usePulse_ = use; } void settingsStr::setUsePolling(bool use) { - usePolling_ = use; + usePolling_ = use; } diff --git a/tools/settingsstr.h b/tools/settingsstr.h index 20b3131..04cb83e 100644 --- a/tools/settingsstr.h +++ b/tools/settingsstr.h @@ -1,6 +1,6 @@ /* * settingsstr.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,53 +28,53 @@ class settingsStr { public: - settingsStr(); - settingsStr(settingsStr &str); - ~settingsStr(); - typedef std::shared_ptr Ptr; - unsigned int cardId() const; - unsigned int mixerId() const; - int pulseDeviceId() const; - bool notebookOrientation(); - bool isAutorun(); - bool usePulse(); - bool usePolling(); - const std::string &pulseDeviceName() const; - const std::string &pulseDeviceDesc() const; - const std::vector &cardList() const; - const std::vector &mixerList() const; - const std::vector &pulseDevices() const; - MixerSwitches::Ptr switchList() const; - void setCardId(unsigned int id); - void setMixerId(unsigned int id); - void setNotebookOrientation(bool orient); - void setIsAutorun(bool autorun); - void pushBack(ListType listType, const std::string &item); - void addMixerSwitch(const MixerSwitches::Ptr &switchItem); - void setList(ListType listType, const std::vector &list); - void clear(ListType listType); - void clearSwitches(); - void setPulseDeviceId(int id); - void setPulseDeviceName(const std::string &name); - void setPulseDeviceDesc(const std::string &description); - void setPulseDevices(const std::vector &devices); - void setUsePulse(bool use); - void setUsePolling(bool use); + settingsStr(); + settingsStr(settingsStr &str); + ~settingsStr(); + typedef std::shared_ptr Ptr; + unsigned int cardId() const; + unsigned int mixerId() const; + int pulseDeviceId() const; + bool notebookOrientation(); + bool isAutorun(); + bool usePulse(); + bool usePolling(); + const std::string &pulseDeviceName() const; + const std::string &pulseDeviceDesc() const; + const std::vector &cardList() const; + const std::vector &mixerList() const; + const std::vector &pulseDevices() const; + MixerSwitches::Ptr switchList() const; + void setCardId(unsigned int id); + void setMixerId(unsigned int id); + void setNotebookOrientation(bool orient); + void setIsAutorun(bool autorun); + void pushBack(ListType listType, const std::string &item); + void addMixerSwitch(const MixerSwitches::Ptr &switchItem); + void setList(ListType listType, const std::vector &list); + void clear(ListType listType); + void clearSwitches(); + void setPulseDeviceId(int id); + void setPulseDeviceName(const std::string &name); + void setPulseDeviceDesc(const std::string &description); + void setPulseDevices(const std::vector &devices); + void setUsePulse(bool use); + void setUsePolling(bool use); private: - unsigned int cardId_; - unsigned int mixerId_; - bool notebookOrientation_; - bool isAutorun_; - bool usePulse_; - bool usePolling_; - std::vector cardList_; - std::vector mixerList_; - MixerSwitches::Ptr switchList_; - std::vector pulseDevices_; - int pulseDeviceId_; - std::string pulseDeviceName_; - std::string pulseDeviceDesc_; + unsigned int cardId_; + unsigned int mixerId_; + bool notebookOrientation_; + bool isAutorun_; + bool usePulse_; + bool usePolling_; + std::vector cardList_; + std::vector mixerList_; + MixerSwitches::Ptr switchList_; + std::vector pulseDevices_; + int pulseDeviceId_; + std::string pulseDeviceName_; + std::string pulseDeviceDesc_; }; #endif // SETTINGSSTR_H diff --git a/tools/tools.cpp b/tools/tools.cpp index 12920d7..7cd0269 100644 --- a/tools/tools.cpp +++ b/tools/tools.cpp @@ -1,6 +1,6 @@ /* * tools.cpp - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -30,101 +30,101 @@ bool Tools::checkFileExists(const std::string &fileName) { - return g_file_test(fileName.c_str(), G_FILE_TEST_EXISTS); + return g_file_test(fileName.c_str(), G_FILE_TEST_EXISTS); } bool Tools::checkDirExists(const std::string &fileName) { - return g_file_test(fileName.c_str(), G_FILE_TEST_IS_DIR); + return g_file_test(fileName.c_str(), G_FILE_TEST_IS_DIR); } std::string Tools::getCWD() { - const size_t cwdSize = 255; - char cwdBuffer[255]; - return getcwd(cwdBuffer, cwdSize); + const size_t cwdSize = 255; + char cwdBuffer[255]; + return getcwd(cwdBuffer, cwdSize); } std::string Tools::getHomePath() { - return std::string(getenv("HOME")); + return std::string(getenv("HOME")); } std::vector Tools::getProjectPathes() { - const std::string cwd = getCWD(); - std::vector list; - list.push_back(getHomePath() + std::string("/.local") + PATH_SUFFIX); - list.push_back(cwd + "/"); - list.push_back(cwd + "/" + PATH_SUFFIX); - list.push_back(cwd.substr(0, cwd.find_last_of("/")) + PATH_SUFFIX); - list.push_back(std::string("/usr") + PATH_SUFFIX); - list.push_back(std::string("/usr/local") + PATH_SUFFIX); - return list; + const std::string cwd = getCWD(); + std::vector list({getHomePath() + std::string("/.local") + PATH_SUFFIX, + cwd + "/", + cwd + "/" + PATH_SUFFIX, + cwd.substr(0, cwd.find_last_of("/")) + PATH_SUFFIX, + std::string("/usr") + PATH_SUFFIX, + std::string("/usr/local") + PATH_SUFFIX} + ); + return list; } std::string Tools::getResPath(const char *resName) { - const std::string resName_(resName); - const std::vector list = getProjectPathes(); - std::string fileName; - std::vector::const_iterator it = std::find_if(list.begin(), - list.end(), - [&](const std::string &path){ - fileName = path + resName_; - return checkFileExists(fileName); - }); - return (it != list.end()) ? fileName : std::string(); + const std::string resName_(resName); + const std::vector list = getProjectPathes(); + std::string fileName; + auto it = std::find_if(list.begin(), + list.end(), + [&](const std::string &path){ + fileName = path + resName_; + return checkFileExists(fileName); + }); + return (it != list.end()) ? fileName : std::string(); } std::string Tools::getDirPath(const char *dirName) { - const std::string dirName_(dirName); - const std::vector list = getProjectPathes(); - std::string directoryName; - std::vector::const_iterator it = std::find_if(list.begin(), - list.end(), - [&](const std::string &path){ - directoryName = path + dirName_; - return checkDirExists(directoryName); - }); - return (it != list.end()) ? directoryName : std::string(); + const std::string dirName_(dirName); + const std::vector list = getProjectPathes(); + std::string directoryName; + auto it = std::find_if(list.begin(), + list.end(), + [&](const std::string &path){ + directoryName = path + dirName_; + return checkDirExists(directoryName); + }); + return (it != list.end()) ? directoryName : std::string(); } void Tools::createDirectory(const std::string &dirName) { - if (!checkDirExists(dirName)) { - std::cerr << "Directory " << dirName << " not found. Attempting to create it.." << std::endl; - gint err = g_mkdir_with_parents(dirName.c_str(), MK_RIGHTS); - if (err < 0) { - std::cerr << "tools.cpp::102::createDirectory:: " << g_file_error_from_errno(err) << std::endl; - } - } + if (!checkDirExists(dirName)) { + std::cerr << "Directory " << dirName << " not found. Attempting to create it.." << std::endl; + gint err = g_mkdir_with_parents(dirName.c_str(), MK_RIGHTS); + if (err < 0) { + std::cerr << "tools.cpp::102::createDirectory:: " << g_file_error_from_errno(err) << std::endl; + } + } } void Tools::saveFile(const std::string &fileName, const std::string &fileData) { - try { - std::ofstream ofile(fileName.c_str()); - ofile << fileData << std::endl; - ofile.close(); - } - catch ( const std::exception & ex ) { - std::cerr << "tools.cpp::112::saveFile:: " << ex.what() << std::endl; - } + try { + std::ofstream ofile(fileName.c_str()); + ofile << fileData << std::endl; + ofile.close(); + } + catch ( const std::exception & ex ) { + std::cerr << "tools.cpp::112::saveFile:: " << ex.what() << std::endl; + } } std::string Tools::pathToFileName(const std::string &path) { - return g_path_get_basename(path.c_str()); + return g_path_get_basename(path.c_str()); } #ifdef IS_DEBUG void Tools::printList(const std::vector &list) { - std::cout << "Printing vector contents" << std::endl; - std::for_each(list.begin(), - list.end(), - []( const std::string &item){ std::cout << item << std::endl; }); + std::cout << "Printing vector contents" << std::endl; + for(const std::string &item : list) { + std::cout << item << std::endl; + } } #endif diff --git a/tools/tools.h b/tools/tools.h index d798612..b813e89 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -1,6 +1,6 @@ /* * tools.h - * Copyright (C) 2012-2015 Vitaly Tonkacheyev + * Copyright (C) 2012-2019 Vitaly Tonkacheyev * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -25,57 +25,57 @@ #include enum SwitchType { - PLAYBACK = 0, - CAPTURE = 1, - ENUM = 2 + PLAYBACK = 0, + CAPTURE = 1, + ENUM = 2 }; enum ListType { - CARDS = 0, - MIXERS = 1 + CARDS = 0, + MIXERS = 1 }; struct iconPosition { - int iconX_; - int iconY_; - int iconWidth_; - int iconHeight_; - int screenHeight_; - int screenWidth_; - bool geometryAvailable_; - bool trayAtTop_; + int iconX_; + int iconY_; + int iconWidth_; + int iconHeight_; + int screenHeight_; + int screenWidth_; + bool geometryAvailable_; + bool trayAtTop_; }; typedef std::pair switchcap; namespace Tools { - std::string getCWD(); - std::string getHomePath(); - std::vector getProjectPathes(); - std::string getResPath(const char *resName); - std::string getDirPath(const char *dirName); - bool checkFileExists(const std::string &fileName); - bool checkDirExists(const std::string &fileName); - void createDirectory(const std::string &dirName); - void saveFile(const std::string &fileName, const std::string &fileData); - std::string pathToFileName(const std::string &path); +std::string getCWD(); +std::string getHomePath(); +std::vector getProjectPathes(); +std::string getResPath(const char *resName); +std::string getDirPath(const char *dirName); +bool checkFileExists(const std::string &fileName); +bool checkDirExists(const std::string &fileName); +void createDirectory(const std::string &dirName); +void saveFile(const std::string &fileName, const std::string &fileData); +std::string pathToFileName(const std::string &path); #ifdef IS_DEBUG - void printList(const std::vector &list); +void printList(const std::vector &list); #endif - //Template functions - template - int itemIndex(const std::vector &vect, const T &item) { - int index = 0; - typename std::vector::const_iterator it = std::find(vect.begin(), vect.end(), item); - if (it != vect.end()) { - index = it - vect.begin(); - } - return index; - } - template - bool itemExists(const std::vector &vect, const T &item) { - typename std::vector::const_iterator it = std::find(vect.begin(), vect.end(), item); - return (it != vect.end()); - } +//Template functions +template +int itemIndex(const std::vector &vect, const T &item) { + int index = 0; + typename std::vector::const_iterator it = std::find(vect.begin(), vect.end(), item); + if (it != vect.end()) { + index = it - vect.begin(); + } + return index; +} +template +bool itemExists(const std::vector &vect, const T &item) { + typename std::vector::const_iterator it = std::find(vect.begin(), vect.end(), item); + return (it != vect.end()); +} } #endif // FILEWORK_H