summaryrefslogtreecommitdiff
path: root/src/pa-sink-ctl.c
blob: a2d8afe3f311a582cca484d3fcca1555b1cf6ac2 (plain)
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
#include <glib.h>
#include <pulse/pulseaudio.h>
#include <pulse/glib-mainloop.h>

#include "sink.h"
#include "interface.h"
#include "pa-sink-ctl.h"

pa_context *context = NULL;
gboolean context_ready = FALSE;

static gboolean info_callbacks_finished = TRUE;
static gboolean info_callbacks_blocked = FALSE;

struct fetch_ctx {
	GList *sinks;
};

GList *sink_list;

#define list_append_struct(list, data) \
	do { \
		(list) = g_list_append((list), \
				       g_memdup(&(data), sizeof(data))); \
	} while (0)

int
main(int argc, char** argv)
{
	pa_mainloop_api  *mainloop_api = NULL;
	pa_glib_mainloop *m            = NULL;

	sink_list = NULL;

	GMainLoop *g_loop = g_main_loop_new(NULL, FALSE);

	interface_init();

	if (!(m = pa_glib_mainloop_new(NULL))) {
		interface_clear();
		g_printerr("error: pa_glib_mainloop_new() failed.\n");
		return -1;
	}

	mainloop_api = pa_glib_mainloop_get_api(m);

	if (!(context = pa_context_new(mainloop_api, "pa-sink-ctl"))) {
		interface_clear();
		g_printerr("error: pa_context_new() failed.\n");
		return -1;
	}
	
	// define callback for connection init
	pa_context_set_state_callback(context, context_state_callback, g_loop);
	if (pa_context_connect(context, NULL, PA_CONTEXT_NOAUTOSPAWN, NULL)) {
		interface_clear();
		g_printerr("error: pa_context_connect() failed.\n");
	}

	g_main_loop_run(g_loop);

	interface_clear();
	g_list_free(sink_list);

	pa_glib_mainloop_free(m);
	g_main_loop_unref(g_loop);

	return 0;
}

static void
subscribe_cb(pa_context *c, pa_subscription_event_type_t t, guint32 idx, gpointer userdata)
{
	if (!info_callbacks_finished)
		info_callbacks_blocked = TRUE;
	else
		collect_all_info();
}

/*
 * is called after connection
 */
void
context_state_callback(pa_context *c, gpointer userdata)
{
	static pa_operation *o = NULL;
	context_ready = FALSE;
	switch (pa_context_get_state(c)) {
		case PA_CONTEXT_CONNECTING:
			interface_set_status("connecting...");
			break;
		case PA_CONTEXT_AUTHORIZING:
			interface_set_status("authorizing...");
			break;
		case PA_CONTEXT_SETTING_NAME:
			interface_set_status("setting name...");
			break;

		case PA_CONTEXT_READY:
			collect_all_info();
			pa_context_set_subscribe_callback(context, subscribe_cb, NULL);
			g_assert((o = pa_context_subscribe(c, (pa_subscription_mask_t) (
					PA_SUBSCRIPTION_MASK_SINK | PA_SUBSCRIPTION_MASK_SINK_INPUT
					), NULL, NULL)));
			context_ready = TRUE;
			interface_set_status("ready to process events.");
			break;
		case PA_CONTEXT_FAILED:
			interface_set_status("cannot connect!");
			break;

		case PA_CONTEXT_TERMINATED:
			g_assert(o != NULL);
			pa_operation_cancel(o);
			pa_operation_unref(o);
			o = NULL;
			interface_set_status("connection terminated.");
			g_main_loop_quit((GMainLoop *)userdata);
			break;
		default:
			interface_set_status("unknown state");
			break;
	}
}


/*
 * the begin of the callback loops
 */
void
get_sink_info_callback(pa_context *c, const pa_sink_info *i, gint is_last, gpointer userdata)
{
	g_assert(userdata != NULL);
	struct fetch_ctx *fetch_ctx = userdata;

	if (is_last < 0) {
		g_printerr("Failed to get sink information: %s\n", pa_strerror(pa_context_errno(c)));
		quit();
	}

	if (is_last) {
		pa_operation_unref(pa_context_get_sink_input_info_list(c, get_sink_input_info_callback, fetch_ctx));
		return;
	}

	sink_info sink = {
		.index = i->index,
		.mute  = i->mute,
		.vol   = pa_cvolume_avg(&i->volume),
		.channels = i->volume.channels,
		.name = g_strdup(i->name),
		.device = pa_proplist_contains(i->proplist, "device.product.name") ? 
			g_strdup(pa_proplist_gets(i->proplist, "device.product.name")) : NULL,
		.input_list = NULL
	};

	list_append_struct(fetch_ctx->sinks, sink);
}

/*
 * is called after sink-input
 */
void
get_sink_input_info_callback(pa_context *c, const pa_sink_input_info *i, gint is_last, gpointer userdata)
{
	g_assert(userdata != NULL);
	struct fetch_ctx *fetch_ctx = userdata;

	if (is_last < 0) {
		g_printerr("Failed to get sink input information: %s\n", pa_strerror(pa_context_errno(c)));
		return;
	}

	if (is_last) {
		info_callbacks_finished = TRUE;
		g_list_free_full(sink_list, g_free);
		sink_list = fetch_ctx->sinks;
		g_free(fetch_ctx);

		print_sink_list();

		if (info_callbacks_blocked) {
			info_callbacks_blocked = FALSE;
			collect_all_info();
		}
		return;
	}

	if (!(i->client != PA_INVALID_INDEX)) return;

	sink_input_info sink_input = {
		.index = i->index,
		.sink = i->sink,
		.name = pa_proplist_contains(i->proplist, "application.name") ?
			g_strdup(pa_proplist_gets(i->proplist, "application.name")):
			g_strdup(i->name),
		.mute = i->mute,
		.channels = i->volume.channels,
		.vol = pa_cvolume_avg(&i->volume),
		.pid = NULL /* maybe obsolete */
	};

	sink_info *sink = g_list_nth_data(fetch_ctx->sinks, i->sink);
	list_append_struct(sink->input_list, sink_input);
}

void
quit(void)
{
	pa_context_disconnect(context);
}

/*
 * is called, after user input
 */
void
change_callback(pa_context* c, gint success, gpointer userdata)
{
	return;
}

void
collect_all_info(void)
{
	if (!info_callbacks_finished)
		return;
	info_callbacks_finished = FALSE;
	pa_operation_unref(pa_context_get_sink_info_list(context, get_sink_info_callback, g_new0(struct fetch_ctx, 1)));
}