Skip to content

Commit

Permalink
Merge pull request #18 from rkrishn7/rkrishn7-ignore-source-outputs
Browse files Browse the repository at this point in the history
allow ignoring source outputs
  • Loading branch information
ErikReider authored Feb 10, 2024
2 parents c850bc4 + 7a0611f commit 8ac8608
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 14 deletions.
6 changes: 5 additions & 1 deletion include/data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

using namespace std;

#define MAX_IGNORED_SOURCE_OUTPUTS 100

enum SubscriptionType {
SUBSCRIPTION_TYPE_IDLE,
SUBSCRIPTION_TYPE_DRY_BOTH,
Expand Down Expand Up @@ -36,13 +38,15 @@ struct Data {
SubscriptionType subscriptionType;
pa_subscription_mask_t pa_subscriptionType;

char **ignoredSourceOutputs;

Idle *idle = NULL;

bool failed = false;

Data(pa_threaded_mainloop *mainloop, pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType);
pa_subscription_mask_t pa_subscriptionType, EventType eventType, char **ignoredSourceOutputs);

void quit(int returnValue = 0);

Expand Down
4 changes: 2 additions & 2 deletions include/pulse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class Pulse {
public:
int init(SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType);
pa_subscription_mask_t pa_subscriptionType, EventType eventType, char **ignoredSourceOutputs);

private:
static void sink_input_info_callback(pa_context *,
Expand All @@ -40,7 +40,7 @@ class Pulse {

void connect(pa_threaded_mainloop *mainloop, pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType);
pa_subscription_mask_t pa_subscriptionType, EventType eventType, char **ignoredSourceOutputs);

pa_threaded_mainloop *getMainLoop();

Expand Down
3 changes: 2 additions & 1 deletion src/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

Data::Data(pa_threaded_mainloop *mainloop, pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType, EventType eventType) {
pa_subscription_mask_t pa_subscriptionType, EventType eventType, char** ignoredSourceOutputs) {
this->mainloop = mainloop;
this->mainloop_api = mainloop_api;
this->subscriptionType = subscriptionType;
this->pa_subscriptionType = pa_subscriptionType;
this->eventCalled = eventType;
this->ignoredSourceOutputs = ignoredSourceOutputs;

if (subscriptionType == SUBSCRIPTION_TYPE_IDLE) idle = new Idle();
}
Expand Down
25 changes: 20 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ void showHelp(char **argv) {
"sink is running\n";
cout << "\t --dry-print-source \t\t Don't inhibit idle and print if any "
"source is running\n";
cout << "\t --ignore-source-outputs \t\t Don't inhibit idle for these "
"source outputs\n";

}

int main(int argc, char *argv[]) {
Expand All @@ -28,6 +31,9 @@ int main(int argc, char *argv[]) {
bool printSource = false;
bool printSink = false;

char* ignoredSourceOutputs[MAX_IGNORED_SOURCE_OUTPUTS];
int ignoredSourceOutputsCount = 0;

if (argc > 1) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "--dry-print-source") == 0) {
Expand All @@ -38,6 +44,15 @@ int main(int argc, char *argv[]) {
printBoth = true;
} else if (strcmp(argv[i], "--dry-print-both-waybar") == 0) {
printBothWayBar = true;
} else if (strcmp(argv[i], "--ignore-source-outputs") == 0 && i + 1 < argc) {
char *saveptr;
char *token = strtok_r(argv[++i], " ", &saveptr);
while (token != nullptr && ignoredSourceOutputsCount < MAX_IGNORED_SOURCE_OUTPUTS) {
ignoredSourceOutputs[ignoredSourceOutputsCount++] = token;
token = strtok_r(nullptr, " ", &saveptr);
}

ignoredSourceOutputs[ignoredSourceOutputsCount] = nullptr;
} else {
showHelp(argv);
return 0;
Expand All @@ -49,19 +64,19 @@ int main(int argc, char *argv[]) {
(pa_subscription_mask_t)(PA_SUBSCRIPTION_MASK_SINK_INPUT |
PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT);
if (!printSink && !printSource && !printBoth && !printBothWayBar) {
return Pulse().init(SUBSCRIPTION_TYPE_IDLE, all_mask, EVENT_TYPE_IDLE);
return Pulse().init(SUBSCRIPTION_TYPE_IDLE, all_mask, EVENT_TYPE_IDLE, ignoredSourceOutputs);
} else if (printBoth) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_BOTH, all_mask,
EVENT_TYPE_DRY_BOTH);
EVENT_TYPE_DRY_BOTH, ignoredSourceOutputs);
} else if (printBothWayBar) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_BOTH_WAYBAR, all_mask,
EVENT_TYPE_DRY_BOTH);
EVENT_TYPE_DRY_BOTH, ignoredSourceOutputs);
} else if (printSink) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_SINK, PA_SUBSCRIPTION_MASK_SINK_INPUT,
EVENT_TYPE_DRY_SINK);
EVENT_TYPE_DRY_SINK, ignoredSourceOutputs);
} else if (printSource) {
return Pulse().init(SUBSCRIPTION_TYPE_DRY_SOURCE,
PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, EVENT_TYPE_DRY_SOURCE);
PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, EVENT_TYPE_DRY_SOURCE, ignoredSourceOutputs);
}
return 0;
}
27 changes: 22 additions & 5 deletions src/pulse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,29 @@
#include <pulse/context.h>
#include <pulse/def.h>
#include <pulse/mainloop-api.h>
#include <pulse/proplist.h>
#include <pulse/pulseaudio.h>
#include <pulse/thread-mainloop.h>
#include <string.h>

#include <cstring>
#include <cstdlib>
#include <iostream>

#include "data.hpp"

int Pulse::init(SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType,
EventType eventType) {
EventType eventType,
char **ignoredSourceOutputs) {
pa_threaded_mainloop *mainloop = getMainLoop();
pa_mainloop_api *mainloop_api = getMainLoopApi(mainloop);
if (pa_threaded_mainloop_start(mainloop)) {
fprintf(stderr, "pa_threaded_mainloop_start() failed.\n");
return 1;
}
connect(mainloop, mainloop_api, subscriptionType, pa_subscriptionType,
eventType);
eventType, ignoredSourceOutputs);
return 0;
}

Expand All @@ -37,7 +40,20 @@ void Pulse::source_output_info_callback(pa_context *,
const pa_source_output_info *i, int,
void *userdata) {
Data *data = (Data *)userdata;
if (i && !i->corked && i->client != PA_INVALID_INDEX) data->activeSource = true;
bool ignoreSourceOutput = false;
if (i && i->proplist) {
const char *appName = pa_proplist_gets(i->proplist, "application.name");
if (appName) {
int i = 0;
while (data->ignoredSourceOutputs[i] != nullptr) {
if (strcmp(appName, data->ignoredSourceOutputs[i]) == 0) {
ignoreSourceOutput = true;
}
i++;
}
}
}
if (i && !i->corked && i->client != PA_INVALID_INDEX && !ignoreSourceOutput) data->activeSource = true;
pa_threaded_mainloop_signal(data->mainloop, 0);
}

Expand Down Expand Up @@ -155,9 +171,10 @@ void Pulse::connect(pa_threaded_mainloop *mainloop,
pa_mainloop_api *mainloop_api,
SubscriptionType subscriptionType,
pa_subscription_mask_t pa_subscriptionType,
EventType eventType) {
EventType eventType,
char **ignoredSourceOutputs) {
Data *data = new Data(mainloop, mainloop_api, subscriptionType,
pa_subscriptionType, eventType);
pa_subscriptionType, eventType, ignoredSourceOutputs);

pa_context *context = getContext(mainloop, mainloop_api, data);

Expand Down

0 comments on commit 8ac8608

Please sign in to comment.