~vilhalmer/pavc

ref: b217883f333381da990d89335000eac76e60cdcd pavc/pavc.c -rw-r--r-- 4.2 KiB
b217883fBill Doyle Implement mute/unmute 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
// 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;
};

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);
	printf("volume: %s\n", vol_str);
}

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;
		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@",
	};

	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;
	}

	int amount = 0;
	if (i < argc) {
		if (pavc.mode == UP || pavc.mode == DOWN || pavc.mode == SET) {
			char * extra;
			amount = strtol(argv[i], &extra, 10);
			if ((*extra != '%' && *extra != '\0') || amount < 0) {
				fprintf(stderr, "Invalid percent '%s'\n", argv[i]);
				return 1;
			}
		}
		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;
}