summaryrefslogtreecommitdiff
path: root/pavmon.cc
blob: df639e42d08aef63bacb6722a29e30096487cbe6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2022 Benjamin Barenblat
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.

#include <getopt.h>
#include <pulse/def.h>
#include <pulse/error.h>
#include <pulse/introspect.h>
#include <pulse/volume.h>
#include <signal.h>
#include <unistd.h>

#include <iomanip>
#include <ios>
#include <iostream>
#include <locale>
#include <string>
#include <string_view>

#include "pulse.h"

namespace {

constexpr std::string_view kShortUsage = "Usage: pavmon SINK_NAME\n";

constexpr std::string_view kHelp = R"(
Monitor the volume level of a PulseAudio sink, printing a new line each time it
changes.

Options:
      --help                  display this help and exit
      --version               display version information and exit
)";

constexpr std::string_view kAskForHelp =
    "Try \"pavmon --help\" for more information.\n";

constexpr std::string_view kVersionInfo = R"(pavmon 1.0.0
Copyright 2022 Benjamin Barenblat
Licensed under the Apache License, Version 2.0
)";

enum {
  kHelpLongOption = 128,
  kVersionLongOption,
};

void GetAndPrintVolume(const std::string& sink_name, pulse::PollMainLoop& loop,
                       pulse::Context& ctx) noexcept {
  ctx.GetSinkInfo(
      sink_name.c_str(),
      [&]() noexcept {
        std::clog << "pavmon: failed to get volume" << std::endl;
        loop.Quit(1);
      },
      [&](const pa_sink_info& info) noexcept {
        if (info.mute) {
          std::cout << "muted (";
        }
        std::cout << std::fixed << std::setprecision(1)
                  << pa_sw_volume_to_dB(pa_cvolume_avg(&info.volume)) << " dB";
        if (info.mute) {
          std::cout << ')';
        }
        std::cout << std::endl;
      });
}

void SubscribeToEvents(const std::string& sink_name, pulse::PollMainLoop& loop,
                       pulse::Context& ctx) noexcept {
  ctx.Subscribe(
      PA_SUBSCRIPTION_MASK_SINK,
      [&]() noexcept {
        std::clog << "pavmon: failed to subscribe to events" << std::endl;
        loop.Quit(1);
      },
      [&](pa_subscription_event_type_t) noexcept {
        GetAndPrintVolume(sink_name, loop, ctx);
      });
}

}  // namespace

int main(int argc, char* argv[]) {
  std::locale loc("");
  std::locale::global(loc);
  std::cin.imbue(loc);
  std::cout.imbue(loc);
  std::cerr.imbue(loc);
  std::clog.imbue(loc);
  std::wcin.imbue(loc);
  std::wcout.imbue(loc);
  std::wcerr.imbue(loc);
  std::wclog.imbue(loc);

  std::ios_base::sync_with_stdio(false);

  static option long_options[] = {
      {"help", no_argument, nullptr, kHelpLongOption},
      {"version", no_argument, nullptr, kVersionLongOption},
      {nullptr, 0, nullptr, 0},
  };
  while (true) {
    int c = getopt_long(argc, argv, "", long_options, /*longindex=*/nullptr);
    if (c == -1) {
      break;
    }
    switch (c) {
      case kHelpLongOption:
        std::cout << kShortUsage << kHelp;
        return 0;
      case kVersionLongOption:
        std::cout << kVersionInfo;
        return 0;
      case '?':
        std::clog << kAskForHelp;
        return 1;
      default:
        std::clog << "Internal error; please report.\n";
        return 1;
    }
  }
  if (optind != argc - 1) {
    std::clog << kShortUsage << kAskForHelp;
    return 1;
  }

  std::string sink_name = argv[1];

  pulse::PollMainLoop loop;

  loop.HandleSignals();
  auto quit_on_signal = [&]() noexcept { loop.Quit(0); };
  pulse::SignalEventSource quit_on_sigterm(SIGTERM, quit_on_signal);
  pulse::SignalEventSource quit_on_sigint(SIGINT, quit_on_signal);

  pulse::PropertyList proplist;
  proplist.Set(PA_PROP_APPLICATION_NAME, "PulseAudio volume monitor");
  proplist.Set(PA_PROP_APPLICATION_ID, "pavmon");
  proplist.Set(PA_PROP_APPLICATION_VERSION, "1.0.0");
  proplist.Set(PA_PROP_APPLICATION_PROCESS_ID, std::to_string(getpid()));
  if (const char* user = getlogin(); user != nullptr) {
    proplist.Set(PA_PROP_APPLICATION_PROCESS_USER, user);
  }
  // TODO(bbarenblat@gmail.com): More of these?

  pulse::Context ctx("PulseAudio volume meter", proplist, loop);
  ctx.Connect(PA_CONTEXT_NOFAIL, [&](pa_context_state_t state) noexcept {
    switch (state) {
      case PA_CONTEXT_UNCONNECTED:
      case PA_CONTEXT_CONNECTING:
      case PA_CONTEXT_AUTHORIZING:
      case PA_CONTEXT_SETTING_NAME:
        break;

      case PA_CONTEXT_READY:
        GetAndPrintVolume(sink_name, loop, ctx);
        SubscribeToEvents(sink_name, loop, ctx);
        break;

      case PA_CONTEXT_TERMINATED:
        loop.Quit(0);
        break;

      case PA_CONTEXT_FAILED:
      default:
        std::clog << "pavmon: failed to connect to PulseAudio" << std::endl;
        loop.Quit(1);
        break;
    }
  });

  return loop.Run();
}