~vilhalmer/pavc

ref: 66a6f1a5e09e919129523c639f738450557b48f2 pavc/pavc.c -rw-r--r-- 5.5 KiB
66a6f1a5Bill Doyle Implement everything else 9 months ago
                                                                                
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
242
243
244
245
246
247
// PulseAudio Volume Control
// It does one thing

#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <pulse/pulseaudio.h>

struct pavc {
	pa_mainloop * loop;
	enum {
		PRINT = 0,
		MUTE,
		UNMUTE,
		TOGGLE,
		UP,
		DOWN,
		SET,
	} mode;
	char * sink;
	uint32_t amount;
};

static void query_volume_sink(
		pa_context * ctx __attribute__((unused)),
		const pa_sink_info * info,
		int eol,
		void * userdata) {
	struct pavc * pavc = userdata;
	if (eol) {
		pa_mainloop_quit(pavc->loop, 0);
		return;
	}

	pa_volume_t vol = pa_cvolume_avg(&info->volume);
	char vol_str[1024] = {0};
	pa_volume_snprint(vol_str, 1023, vol);
	if (info->mute) {
		printf("%s mute\n", vol_str);
	}
	else {
		printf("%s\n", vol_str);
	}
}

static void adjust_volume_sink(
		pa_context * ctx,
		const pa_sink_info * info,
		int eol,
		void * userdata) {
	struct pavc * pavc = userdata;
	if (eol) {
		pa_mainloop_quit(pavc->loop, 0);
		return;
	}

	pa_cvolume vol = info->volume;
	switch (pavc->mode) {
	case UP:
		pa_cvolume_inc_clamp(&vol, pavc->amount, PA_VOLUME_NORM);
		break;
	case DOWN:
		pa_cvolume_dec(&vol, pavc->amount);
		break;
	case SET:
		pa_cvolume_set(&vol, info->channel_map.channels, pavc->amount);
		break;
	default:
		assert(!"unreachable");
	}

	pa_context_set_sink_volume_by_name(ctx, pavc->sink, &vol, NULL, NULL);
}

static void toggle_mute_sink(
		pa_context * ctx,
		const pa_sink_info * info,
		int eol,
		void * userdata) {
	struct pavc * pavc = userdata;
	if (eol) {
		pa_mainloop_quit(pavc->loop, 0);
		return;
	}

	pa_context_set_sink_mute_by_name(ctx, pavc->sink, !info->mute, NULL, NULL);
}

static void state_changed(pa_context * ctx, void * userdata) {
	struct pavc * pavc = userdata;

	switch (pa_context_get_state(ctx)) {
	case PA_CONTEXT_FAILED:
		fprintf(stderr, "Unable to connect to pulseaudio: %s\n", pa_strerror(pa_context_errno(ctx)));
		pa_mainloop_quit(pavc->loop, 2);
		break;
	case PA_CONTEXT_READY:
		// woohoo
		switch (pavc->mode) {
		case PRINT:
			pa_context_get_sink_info_by_name(ctx, pavc->sink, query_volume_sink, pavc);
			return;  // Don't exit yet, need to get a response.
			break;
		case MUTE:
			pa_context_set_sink_mute_by_name(ctx, pavc->sink, true, NULL, NULL);
			break;
		case UNMUTE:
			pa_context_set_sink_mute_by_name(ctx, pavc->sink, false, NULL, NULL);
			break;
		case TOGGLE:
			pa_context_get_sink_info_by_name(ctx, pavc->sink, toggle_mute_sink, pavc);
			return;
		case UP:
		case DOWN:
		case SET:
			pa_context_get_sink_info_by_name(ctx, pavc->sink, adjust_volume_sink, pavc);
			return;
		default:
			break;
		}
		pa_mainloop_quit(pavc->loop, 0);
		break;
	case PA_CONTEXT_UNCONNECTED:
	case PA_CONTEXT_AUTHORIZING:
	case PA_CONTEXT_CONNECTING:
	case PA_CONTEXT_SETTING_NAME:
	case PA_CONTEXT_TERMINATED:
		// who cares
		break;
	}
}


const char * usage =
	"Usage: pavc [-h] [-s sink_id] [<command> [<args>]]\n"
	"  -h        Show this help\n"
	"  -s        Operate on a specific sink instead of the default; may be\n"
	"            either a name or index\n"
	"\n"
	"Commands:\n"
	"  mute      Mute the sink\n"
	"  unmute    Unmute the sink\n"
	"  toggle    Toggle between muted and unmuted\n"
	"  up [%]    Increase the volume by the given percent, or 5% by default\n"
	"  down [%]  Decrease the volume by the given percent, or 5% by default\n"
	"  set <%>   Set the volume to an absolute percent\n"
	"\n"
	"If no command is given, the current volume of the selected sink is\n"
	"printed. Commands may be abbreviated.\n"
	"";


int main(int argc, char * argv[]) {
	pa_mainloop * loop = pa_mainloop_new();
	assert(loop);

	pa_mainloop_api * api = pa_mainloop_get_api(loop);
	assert(api);

	pa_proplist * properties = pa_proplist_new();
	pa_context * ctx = pa_context_new_with_proplist(api, "pavc", properties);
	assert(ctx);

	assert(pa_context_connect(ctx, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL) >= 0);

	struct pavc pavc = {
		.loop = loop,
		.mode = PRINT,
		.sink = "@DEFAULT_SINK@",
		.amount = PA_VOLUME_NORM / 20,
	};

	int opt;
	while ((opt = getopt(argc, argv, "hs:")) != -1) {
		switch (opt) {
		case 's':
			pavc.sink = optarg;
			break;
		case 'h':
			printf("%s", usage);
			return 0;
		default:
			printf("%s", usage);
			return 1;
		}
	}

	int i = optind;
	if (i < argc) {
		if (strncmp("mute", argv[i], strlen(argv[i])) == 0) {
			pavc.mode = MUTE;
		}
		else if (strncmp("unmute", argv[i], strlen(argv[i])) == 0) {
			pavc.mode = UNMUTE;
		}
		else if (strncmp("toggle", argv[i], strlen(argv[i])) == 0) {
			pavc.mode = TOGGLE;
		}
		else if (strncmp("up", argv[i], strlen(argv[i])) == 0) {
			pavc.mode = UP;
		}
		else if (strncmp("down", argv[i], strlen(argv[i])) == 0) {
			pavc.mode = DOWN;
		}
		else if (strncmp("set", argv[i], strlen(argv[i])) == 0) {
			pavc.mode = SET;
		}
		else {
			printf("%s", usage);
			return 1;
		}
		++i;
	}

	if (i < argc) {
		if (pavc.mode == UP || pavc.mode == DOWN || pavc.mode == SET) {
			char * extra;
			long int amount = strtol(argv[i], &extra, 10);
			if ((*extra != '%' && *extra != '\0')) {
				fprintf(stderr, "Invalid percent '%s'\n", argv[i]);
				return 1;
			}
			pavc.amount = (amount / 100.0) * PA_VOLUME_NORM;
		}
		else {
			fprintf(stderr, "Unexpected argument\n");
			return 1;
		}
	}
	pa_context_set_state_callback(ctx, state_changed, &pavc);

	int exit = 0;
	if (pa_mainloop_run(loop, &exit) < 0) {
		fprintf(stderr, "Unable to communicate with pulseaudio\n");
		exit = 1;
	}

	pa_context_disconnect(ctx);
	pa_context_unref(ctx);
	pa_proplist_free(properties);
	pa_mainloop_free(loop);

	return exit;
}