aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--notify.h38
-rw-r--r--ponymix.cc48
-rw-r--r--pulse.cc26
-rw-r--r--pulse.h5
5 files changed, 80 insertions, 39 deletions
diff --git a/Makefile b/Makefile
index 6f81a0f..28bdaf3 100644
--- a/Makefile
+++ b/Makefile
@@ -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:
diff --git a/ponymix.cc b/ponymix.cc
index d8b62c6..d1e40e6 100644
--- a/ponymix.cc
+++ b/ponymix.cc
@@ -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);
}
diff --git a/pulse.cc b/pulse.cc
index 26f58eb..324c05d 100644
--- a/pulse.cc
+++ b/pulse.cc
@@ -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
//
diff --git a/pulse.h b/pulse.h
index 632fbb8..e0b2bd3 100644
--- a/pulse.h
+++ b/pulse.h
@@ -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: