-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeadsec.c
More file actions
241 lines (213 loc) · 5.65 KB
/
Copy pathdeadsec.c
File metadata and controls
241 lines (213 loc) · 5.65 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/* Deadsec - Neo Sahadeo 2025
* MIT LICENSE
* */
#include <argp.h>
#include <argz.h>
#include <error.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
const char *argp_program_version = "v1.0.0";
const char *argp_program_bug_address = "neosahadeo+deadsec@protonmail.com";
int parse_opt(int key, char *arg, struct argp_state *state);
struct argp_option options[] = {
{"link", 'l', "SOURCE SINK [-d]", 0,
"Link sources to a sink. Specify -d to disonnect instead of connect"},
{0, 'd', 0, OPTION_HIDDEN},
{0, 0, 0, 0, " "}, // add a space
{"search", 'S', "STRING [-i][-o]", 0,
"Search sources/sinks. Specify -i for inputs or -o for outputs"},
{0, 'i', 0, OPTION_HIDDEN},
{0, 'o', 0, OPTION_HIDDEN},
{0, 0, 0, 0, " "}, // add a space
{"sample", 's', "NUM", 0, "Set the sample buffer size of pipewire"},
{0}};
struct argp argp = {
options, parse_opt, NULL,
"Connect and search pipewire connections and the ability to "
"adjust the sample rate"};
typedef struct arguments {
char *argz;
size_t argz_len;
bool link;
bool search;
bool sample;
bool disonnect;
bool input;
bool output;
} arguments;
int parse_opt(int key, char *arg, struct argp_state *state) {
arguments *arguments = state->input;
switch (key) {
case 'l': {
arguments->link = true;
argz_add(&arguments->argz, &arguments->argz_len, arg);
} break;
case 'd': {
arguments->disonnect = true;
} break;
case 's': {
arguments->sample = true;
argz_add(&arguments->argz, &arguments->argz_len, arg);
} break;
case 'o': {
arguments->output = true;
} break;
case 'i': {
arguments->input = true;
} break;
case 'S': {
arguments->search = true;
argz_add(&arguments->argz, &arguments->argz_len, arg);
} break;
case ARGP_KEY_ARG: {
argz_add(&arguments->argz, &arguments->argz_len, arg);
} break;
case ARGP_KEY_INIT: {
arguments->argz = 0;
arguments->argz_len = 0;
} break;
case ARGP_KEY_END: {
if ((arguments->link + arguments->search + arguments->sample) != 1) {
argp_error(state, "Cannot use more than one flag at a time");
}
size_t c = argz_count(arguments->argz, arguments->argz_len);
}
}
return 0;
}
void pwsample(const char *sample_rate) {
FILE *pipe;
char cmd[1024];
char line[1024];
sprintf(cmd, "pw-metadata -n settings 0 clock.force-quantum %s", sample_rate);
pipe = popen(cmd, "w");
if (pipe == NULL) {
perror("popen failed");
return;
}
pclose(pipe);
}
void pwsearch(const char *query, arguments args) {
FILE *pipe;
char cmd[1024];
char line[1024];
if (args.input) {
sprintf(cmd, "pw-link -I -i | grep -Poe .*%s.*", query);
} else {
sprintf(cmd, "pw-link -I -o | grep -Poe .*%s.*", query);
}
pipe = popen(cmd, "r");
if (pipe == NULL) {
perror("popen failed");
return;
}
while (fgets(line, sizeof(line), pipe) != NULL) {
printf("%s\n", line);
}
pclose(pipe);
}
void pwconnect(const char *sink, const char *source, bool should_connect) {
FILE *pipe;
char cmd[1024];
char line[1024];
char loop_buffer[1024];
// Get sink first
sprintf(cmd, "pw-link -I -i | grep -Poe .*%s.*", sink);
pipe = popen(cmd, "r");
if (pipe == NULL) {
perror("popen failed");
return;
}
int max_output_channels = 2;
int right_output = 0;
int left_output = 0;
while (fgets(line, sizeof(line), pipe) != NULL) {
sprintf(cmd, "echo '%s' | grep -Poe '\\d+'", line);
FILE *fp = popen(cmd, "r");
if (pipe == NULL) {
perror("popen failed");
break;
}
fgets(loop_buffer, sizeof(loop_buffer), fp);
// assumes left channel is listed first
if (max_output_channels % 2) {
right_output = atoi(loop_buffer);
} else {
left_output = atoi(loop_buffer);
}
max_output_channels--;
pclose(fp);
if (max_output_channels == 0)
break;
}
pclose(pipe);
// Get sources after sink
sprintf(cmd, "pw-link -I -o | grep -Poe '\\d+(?=.*%s)'", source);
pipe = popen(cmd, "r");
if (pipe == NULL) {
perror("popen failed");
return;
}
int c = 0;
char disconnect_string[2] = "";
if (should_connect) {
disconnect_string[0] = '-';
disconnect_string[1] = 'd';
}
while (fgets(line, sizeof(line), pipe) != NULL) {
if (c % 2) {
// source -> sink
sprintf(loop_buffer, "pw-link %d %d %s > /dev/null 2>&1 &", atoi(line),
right_output, disconnect_string);
} else {
sprintf(loop_buffer, "pw-link %d %d %s > /dev/null 2>&1 &", atoi(line),
left_output, disconnect_string);
}
FILE *p = popen(loop_buffer, "r");
pclose(p);
c++;
}
pclose(pipe);
}
int main(int argc, char **argv) {
arguments arguments;
argp_parse(&argp, argc, argv, 0, 0, &arguments);
const char *buffer = NULL;
const char *word = NULL;
if (arguments.search) {
word = argz_next(arguments.argz, arguments.argz_len, buffer);
pwsearch(word, arguments);
}
if (arguments.sample) {
word = argz_next(arguments.argz, arguments.argz_len, buffer);
pwsample(word);
}
if (arguments.link) {
const char *sink, *source = NULL;
int c = 1;
while ((word = argz_next(arguments.argz, arguments.argz_len, buffer))) {
if (c % 2) {
source = word;
} else {
sink = word;
}
buffer = word;
c++;
}
if (c > 3) {
printf("Too many arguments supplied\n");
exit(EXIT_FAILURE);
} else if (c < 3) {
printf("Not enough arguments supplied\n");
exit(EXIT_FAILURE);
}
pwconnect(sink, source, arguments.disonnect);
}
free(arguments.argz);
exit(0);
}