Skip to content

Commit

Permalink
Added dry-print-both with waybar friendly output
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikReider committed Jan 11, 2022
1 parent 4a68078 commit 8998439
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
2 changes: 2 additions & 0 deletions include/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using namespace std;
enum SubscriptionType {
SUBSCRIPTION_TYPE_IDLE,
SUBSCRIPTION_TYPE_DRY_BOTH,
SUBSCRIPTION_TYPE_DRY_BOTH_WAYBAR,
SUBSCRIPTION_TYPE_DRY_SINK,
SUBSCRIPTION_TYPE_DRY_SOURCE,
};
Expand Down Expand Up @@ -49,4 +50,5 @@ struct Data {

private:
void print(bool isRunning);
void printWayBar(bool activeSink, bool activeSource);
};
17 changes: 17 additions & 0 deletions src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ void Data::handleAction() {
case SUBSCRIPTION_TYPE_DRY_BOTH:
this->print(activeSink || activeSource);
break;
case SUBSCRIPTION_TYPE_DRY_BOTH_WAYBAR:
this->printWayBar(activeSink, activeSource);
break;
case SUBSCRIPTION_TYPE_DRY_SINK:
this->print(activeSink);
break;
Expand All @@ -38,3 +41,17 @@ void Data::handleAction() {
void Data::print(bool isRunning) {
cout << (isRunning ? "RUNNING" : "NOT RUNNING") << endl;
}

void Data::printWayBar(bool activeSink, bool activeSource) {
string result[2] = {activeSink ? "output" : "", activeSource ? "input" : ""};
string text = "";
for (const auto &str : result) {
if (!text.empty() && !str.empty()) text += "-";
text += str;
}
if (text.empty()) text = "none";
printf(
"{\"text\": \"\", \"alt\": \"%s\", \"tooltip\": \"\", \"class\": "
"\"%s\"}\n",
text.c_str(), text.c_str());
}
45 changes: 23 additions & 22 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
#include <cstring>

#include "data.hpp"
#include "pulse.hpp"

void showHelp(char **argv) {
cout << "Usage:" << endl;
cout << "\t" << argv[0] << " <OPTION>" << endl;
cout << "Options:" << endl;
cout << "\t" << argv[0]
<< "\t\t\t\t Inhibits idle if either any sink or any source is running"
<< endl;
cout << "\t" << argv[0] << " -h, --help \t\t Show help options" << endl;
cout << "\t" << argv[0]
<< " --dry-print-both \t Don't inhibit idle and print if either any "
"sink or any source is running"
<< endl;
cout << "\t" << argv[0]
<< " --dry-print-sink \t Don't inhibit idle and print if any sink is "
"running"
<< endl;
cout << "\t" << argv[0]
<< " --dry-print-source \t Don't inhibit idle and print if any source "
"is running"
<< endl;
string name = basename(argv[0]);
cout << "Usage:\n";
cout << "\t" << name << " <OPTION>\n";
cout << "Options:\n";
cout << "\t " << name
<< "\t Inhibits idle if either any sink or any source is running\n";
cout << "\t -h, --help \t\t\t Show help options\n";
cout << "\t --dry-print-both \t\t Don't inhibit idle and print if either any "
"sink or any source is running\n";
cout << "\t --dry-print-both-waybar \t Same as --dry-print-both but outputs "
"in a waybar friendly manner\n";
cout << "\t --dry-print-sink \t\t Don't inhibit idle and print if any sink is "
"running\n";
cout << "\t --dry-print-source \t\t Don't inhibit idle and print if any source "
"is running\n";
}

int main(int argc, char *argv[]) {
bool inhibitIdle = true;

bool printBoth = false;
bool printBothWayBar = false;
bool printSource = false;
bool printSink = false;

Expand All @@ -39,19 +35,24 @@ int main(int argc, char *argv[]) {
printSink = true;
} else if (strcmp(argv[i], "--dry-print-both") == 0) {
printBoth = true;
} else if (strcmp(argv[i], "--dry-print-both-waybar") == 0) {
printBothWayBar = true;
} else {
showHelp(argv);
return 0;
}
}
}

if (inhibitIdle && (!printSink && !printSource && !printBoth)) {
if (!printSink && !printSource && !printBoth && !printBothWayBar) {
return Pulse().init(SUBSCRIPTION_TYPE_IDLE, PA_SUBSCRIPTION_MASK_ALL,
EVENT_TYPE_IDLE);
} else if (printBoth) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_BOTH, PA_SUBSCRIPTION_MASK_ALL,
EVENT_TYPE_DRY_BOTH);
} else if (printBothWayBar) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_BOTH_WAYBAR,
PA_SUBSCRIPTION_MASK_ALL, EVENT_TYPE_DRY_BOTH);
} else if (printSink) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_SINK, PA_SUBSCRIPTION_MASK_SINK,
EVENT_TYPE_DRY_SINK);
Expand Down
5 changes: 4 additions & 1 deletion src/pulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <cstdlib>
#include <iostream>

#include "data.hpp"

int Pulse::init(SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType,
EventType eventType) {
Expand Down Expand Up @@ -86,7 +88,8 @@ void Pulse::subscribe_callback(pa_context *, pa_subscription_event_type_t type,
}
if (data->subscriptionType == SUBSCRIPTION_TYPE_IDLE) {
eventType = EVENT_TYPE_IDLE;
} else if (data->subscriptionType == SUBSCRIPTION_TYPE_DRY_BOTH) {
} else if (data->subscriptionType == SUBSCRIPTION_TYPE_DRY_BOTH ||
data->subscriptionType == SUBSCRIPTION_TYPE_DRY_BOTH_WAYBAR) {
eventType = EVENT_TYPE_DRY_BOTH;
}
data->eventCalled = eventType;
Expand Down

0 comments on commit 8998439

Please sign in to comment.