diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | notify.h | 38 | ||||
-rw-r--r-- | ponymix.cc | 48 | ||||
-rw-r--r-- | pulse.cc | 26 | ||||
-rw-r--r-- | pulse.h | 5 |
5 files changed, 80 insertions, 39 deletions
@@ -18,7 +18,7 @@ LDLIBS := \ all: ponymix ponymix: ponymix.cc pulse.o -pulse.o: pulse.cc pulse.h +pulse.o: pulse.cc pulse.h notify.h install: ponymix install -Dm755 ponymix $(DESTDIR)/usr/bin/ponymix diff --git a/notify.h b/notify.h new file mode 100644 index 0000000..03f6775 --- /dev/null +++ b/notify.h @@ -0,0 +1,38 @@ +#pragma once + +#include <stdio.h> + +enum NotificationType { + NOTIFY_VOLUME, + NOTIFY_BALANCE, + NOTIFY_UNMUTE, + NOTIFY_MUTE, +}; + +class Notifier { + public: + virtual ~Notifier() {} + + virtual void Notify(enum NotificationType type, long value, bool mute) = 0; + + protected: + bool initialized_; +}; + +class CommandLineNotifier : public Notifier { + public: + virtual ~CommandLineNotifier() {} + + virtual void Notify(enum NotificationType type, long value, bool) { + switch (type) { + case NOTIFY_VOLUME: + case NOTIFY_BALANCE: + case NOTIFY_UNMUTE: + case NOTIFY_MUTE: + printf("%ld\n", value); + break; + } + } +}; + +// vim: set et ts=2 sw=2: @@ -278,11 +278,7 @@ static int SetVolume(PulseClient& ponymix, int, char* argv[]) { errx(1, "error: failed to convert string to integer: %s", argv[0]); } - if (!ponymix.SetVolume(*device, volume)) return 1; - - printf("%d\n", device->Volume()); - - return 0; + return !ponymix.SetVolume(*device, volume); } static int GetBalance(PulseClient& ponymix, int, char*[]) { @@ -301,11 +297,7 @@ static int SetBalance(PulseClient& ponymix, int, char* argv[]) { errx(1, "error: failed to convert string to integer: %s", argv[0]); } - if (!ponymix.SetBalance(*device, balance)) return 1; - - printf("%d\n", device->Balance()); - - return 0; + return !ponymix.SetBalance(*device, balance); } static int AdjBalance(PulseClient& ponymix, int, char* argv[]) { @@ -318,11 +310,7 @@ static int AdjBalance(PulseClient& ponymix, int, char* argv[]) { errx(1, "error: failed to convert string to integer: %s", argv[0]); } - if (!ponymix.SetBalance(*device, device->Balance() + balance)) return 1; - - printf("%d\n", device->Balance()); - - return 0; + return !ponymix.SetBalance(*device, device->Balance() + balance); } static int adj_volume(PulseClient& ponymix, @@ -338,11 +326,7 @@ static int adj_volume(PulseClient& ponymix, } ponymix.SetVolumeRange(0, 100); - if (!(ponymix.*adjust)(*device, delta)) return 1; - - printf("%d\n", device->Volume()); - - return 0; + return !(ponymix.*adjust)(*device, delta); } static int IncreaseVolume(PulseClient& ponymix, int, char* argv[]) { @@ -356,31 +340,19 @@ static int DecreaseVolume(PulseClient& ponymix, int, char* argv[]) { static int Mute(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); - if (!ponymix.SetMute(*device, true)) return 1; - - printf("%d\n", device->Volume()); - - return 0; + return !ponymix.SetMute(*device, true); } static int Unmute(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); - if (!ponymix.SetMute(*device, false)) return 1; - - printf("%d\n", device->Volume()); - - return 0; + return !ponymix.SetMute(*device, false); } static int ToggleMute(PulseClient& ponymix, int, char*[]) { auto device = string_to_device_or_die(ponymix, opt_device, opt_devtype); - if (!ponymix.SetMute(*device, !ponymix.IsMuted(*device))) return 1; - - printf("%d\n", device->Volume()); - - return 0; + return !ponymix.SetMute(*device, !ponymix.IsMuted(*device)); } static int IsMuted(PulseClient& ponymix, int, char*[]) { @@ -632,6 +604,12 @@ int main(int argc, char* argv[]) { argc -= optind; argv += optind; + try { + ponymix.EnableNotifications(new CommandLineNotifier); + } catch (std::exception e) { + fprintf(stderr, "failed to enable notifier\n"); + } + return CommandDispatch(ponymix, argc, argv); } @@ -349,7 +349,13 @@ bool PulseClient::SetMute(Device& device, bool mute) { mainloop_iterate(op); pa_operation_unref(op); - if (success) device.mute_ = mute; + if (success) { + device.mute_ = mute; + if (notifier_.get()) { + notifier_->Notify(mute ? NOTIFY_MUTE : NOTIFY_UNMUTE, + device.volume_percent_, mute); + } + } return success; } @@ -372,7 +378,12 @@ bool PulseClient::SetVolume(Device& device, long volume) { mainloop_iterate(op); pa_operation_unref(op); - if (success) device.update_volume(*cvol); + if (success) { + device.update_volume(*cvol); + if (notifier_.get()) notifier_->Notify(NOTIFY_VOLUME, + device.volume_percent_, + device.mute_); + } return success; } @@ -405,7 +416,12 @@ bool PulseClient::SetBalance(Device& device, long balance) { mainloop_iterate(op); pa_operation_unref(op); - if (success) device.update_volume(*cvol); + if (success) { + device.update_volume(*cvol); + if (notifier_.get()) notifier_->Notify(NOTIFY_BALANCE, + device.balance_, + false); + } return success; } @@ -544,6 +560,10 @@ void PulseClient::remove_device(Device& device) { devlist->end()); } +void PulseClient::EnableNotifications(Notifier* notifier) { + notifier_.reset(notifier); +} + // // Cards // @@ -1,5 +1,7 @@ #pragma once +#include "notify.h" + // C #include <string.h> @@ -219,6 +221,8 @@ class PulseClient { balance_range_ = { min, max }; } + void EnableNotifications(Notifier* notifier); + private: void mainloop_iterate(pa_operation* op); template<class T> T* find_fuzzy(vector<T>& haystack, const string& needle); @@ -244,6 +248,7 @@ class PulseClient { ServerInfo defaults_; Range<int> volume_range_; Range<int> balance_range_; + unique_ptr<Notifier> notifier_; }; // vim: set et ts=2 sw=2: |