summaryrefslogtreecommitdiff
path: root/src/interface.c
blob: a90354f719e85f29c5af5e5a8038dd40a06e96a0 (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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * pa-sink-ctl - NCurses based Pulseaudio control client
 * Copyright (C) 2011  Benjamin Franzke <benjaminfranzke@googlemail.com>
 * Copyright (C) 2010  Jan Klemkow <web2p10@wemelug.de>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>

#include <glib.h>
#include <pulse/pulseaudio.h>
#include <ncurses.h>

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

#ifdef HAVE_SIGNALFD
#include <sys/signalfd.h>
#include <signal.h>
#else
#include "unix_signal.h"
#endif

#define H_MSG_BOX 3

#define SELECTED_SINK -1

static int
sink_input_len(struct context *ctx, sink_info *sink)
{
	int len = 0;
	GList *l;

	for (l = ctx->input_list; l; l = l->next) {
		sink_input_info *input = l->data;

		if (input->sink == sink->index)
			len++;
	}

	return len;
}

static sink_input_info *
sink_get_nth_input(struct context *ctx, sink_info *sink, int n)
{
	GList *l;
	int i;

	for (l = ctx->input_list; l; l = l->next) {
		sink_input_info *input = l->data;
		if (input->sink != sink->index)
			continue;
		if (i++ == n)
			return input;
	}

	return NULL;
}

static gboolean
interface_resize(gpointer data)
{
	struct context *ctx = data;
	struct winsize wsize;
	gint height = 80;
	gint width  = 24;

	if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &wsize) >= 0) {
		height = wsize.ws_row;
		width  = wsize.ws_col;
	}

	resize_term(height, width);
	clear();
	refresh();

	wresize(ctx->menu_win, height - H_MSG_BOX, width);
	wresize(ctx->msg_win, H_MSG_BOX, width);
	mvwin(ctx->msg_win, height - H_MSG_BOX, 0);

	/* NULL := display old status */
	interface_set_status(ctx, NULL); 
	print_sink_list(ctx);
	return TRUE;
}

static void
print_volume(struct context *ctx, pa_volume_t volume, int mute, int y)
{
	gint x = 2 /* left */  + 2 /* index num width */ + 1 /* space */ +
		1 /* space */ + ctx->max_name_len + 1 /* space */;

	/* mute button + brackets + space */
	int volume_bar_len = getmaxx(ctx->menu_win) - x - 6;
	gint vol = (gint) (volume_bar_len * volume / PA_VOLUME_NORM);

	mvwprintw(ctx->menu_win, y, x - 1, "[%c]", mute ? 'M' : ' ');
	x += 3;

	mvwprintw(ctx->menu_win, y, x - 1 , "[");
	for (gint i = 0; i < vol; ++i)
		mvwprintw(ctx->menu_win, y, x + i, "=");
	for (gint i = vol; i < volume_bar_len; ++i)
		mvwprintw(ctx->menu_win, y, x + i, " ");
	mvwprintw(ctx->menu_win, y, x + volume_bar_len, "]");
}

static void
print_input_list(struct context *ctx, sink_info *sink,
		 gint sink_num, gint *poffset)
{
	GList *l;
	gint offset = *poffset;
	gint i;

	for (l = ctx->input_list, i = -1; l; l = l->next) {
		sink_input_info *input = l->data;
		if (input->sink != sink->index)
			continue;
		gboolean selected = (ctx->chooser_sink == sink_num &&
				     ctx->chooser_input == ++i);

		if (selected)
			wattron(ctx->menu_win, A_REVERSE);

		mvwprintw(ctx->menu_win, offset, 2, "%*s%-*s",
			  2+1+1, "", /* space for index number + indentation*/
			  ctx->max_name_len - 1, input->name);

		if (selected)
			wattroff(ctx->menu_win, A_REVERSE);

		print_volume(ctx, input->vol, input->mute, offset);
		offset++;
	}
	*poffset = offset;
}

/* looking for the longest name length of all SINK's and INPUT's */
static void
set_max_name_len(struct context *ctx)
{
	GList *l;
	guint len = 0;
	ctx->max_name_len = len;

	for (l = ctx->sink_list; l; l = l->next) {
		sink_info *sink = l->data;

		len = strlen(sink->device != NULL ? sink->device : sink->name);

		if (len > ctx->max_name_len)
			ctx->max_name_len = len;
	}

	for (l = ctx->input_list; l; l = l->next) {
		sink_input_info *input = l->data;

		len = strlen(input->name) + 1 /* indentation */;

		if (len > ctx->max_name_len)
			ctx->max_name_len = len;
	}
}

void
print_sink_list(struct context *ctx)
{
	gint i = 0;
	gint x = 2;
	gint offset = 2; /* top border + 1 empty line */
	GList *l;

	/* looking for the longest name for right indentation */
	set_max_name_len(ctx);

	werase(ctx->menu_win);
	box(ctx->menu_win, 0, 0);

	for (l = ctx->sink_list, i = 0; l; l = l->next,++i) {
		sink_info *sink = l->data;
		gboolean selected = (i == ctx->chooser_sink &&
				     ctx->chooser_input == SELECTED_SINK);

		if (selected)
			wattron(ctx->menu_win, A_REVERSE);

		mvwprintw(ctx->menu_win, offset, x, "%2u %-*s",
			  sink->index, ctx->max_name_len,
			  sink->device != NULL ? sink->device : sink->name);

		if (selected)
			wattroff(ctx->menu_win, A_REVERSE);
		print_volume(ctx, sink->vol, sink->mute, offset);

		offset++;
		print_input_list(ctx, sink, i, &offset);
	}
	wrefresh(ctx->menu_win);
}

static gboolean
interface_get_input(GIOChannel *source, GIOCondition condition, gpointer data)
{
	struct context *ctx = data;
	gint c;
	gboolean volume_increment = TRUE;
	sink_info *sink = NULL;
	guint32 index;
	pa_operation *o;

	if (!ctx->context_ready)
		return TRUE;

	c = wgetch(ctx->menu_win);
	switch (c) {
	case 'k':
	case 'w':
	case KEY_UP:
		if (ctx->chooser_input == SELECTED_SINK &&
		    ctx->chooser_sink > 0) {
			sink = g_list_nth_data(ctx->sink_list,
					       --ctx->chooser_sink);
			/* autoassigment to SELECTED_SINK (=-1) if length = 0 */
			ctx->chooser_input = sink_input_len(ctx, sink) - 1;
		}

		else if (ctx->chooser_input >= 0)
			--ctx->chooser_input;
		print_sink_list(ctx);
		break;

	case 'j':
	case 's':
	case KEY_DOWN:
		sink = g_list_nth_data(ctx->sink_list, ctx->chooser_sink);
		if (ctx->chooser_input == (sink_input_len(ctx, sink) - 1) &&
		    ctx->chooser_sink < (gint)g_list_length(ctx->sink_list)-1) {
			++ctx->chooser_sink;
			ctx->chooser_input = SELECTED_SINK;
		}
		else if (ctx->chooser_input < (sink_input_len(ctx, sink) - 1))
			++ctx->chooser_input;
		print_sink_list(ctx);
		break;

	case 'h':
	case 'a':
	case KEY_LEFT:
		volume_increment = FALSE;
		/* fall through */
	case 'l':
	case 'd':
	case KEY_RIGHT:
		sink = g_list_nth_data(ctx->sink_list, ctx->chooser_sink);
		pa_cvolume volume;
		pa_volume_t tmp_vol;
		pa_operation* (*volume_set) (pa_context*, guint32,
					     const pa_cvolume *,
					     pa_context_success_cb_t, gpointer);

		if (ctx->chooser_input >= 0) {
			sink_input_info *input =
				sink_get_nth_input(ctx, sink,
						   ctx->chooser_input);
			index      = input->index;
			volume     = (pa_cvolume) {.channels = input->channels};
			tmp_vol    = input->vol; 
			volume_set = pa_context_set_sink_input_volume;
		} else if (ctx->chooser_input == SELECTED_SINK) {
			index      = sink->index;
			volume     = (pa_cvolume) {.channels = sink->channels};
			tmp_vol    = sink->vol;
			volume_set = pa_context_set_sink_volume_by_index;
		} else
			break;

		pa_cvolume_set(&volume, volume.channels, tmp_vol);
		pa_volume_t inc = 2 * PA_VOLUME_NORM / 100;

		if (volume_increment)
			if (PA_VOLUME_NORM > tmp_vol &&
			    PA_VOLUME_NORM - tmp_vol > inc)
				pa_cvolume_inc(&volume, inc);
			else
				pa_cvolume_set(&volume, volume.channels,
					       PA_VOLUME_NORM);
		else
			pa_cvolume_dec(&volume, inc);


		pa_operation_unref(volume_set(ctx->context, index, &volume,
					      change_callback, ctx));
		break;
	case 'm':
	case 'x':
	case 'M':
		sink = g_list_nth_data(ctx->sink_list, ctx->chooser_sink);
		gint mute;
		pa_operation* (*mute_set) (pa_context*, guint32, int,
					   pa_context_success_cb_t, void*);

		if (ctx->chooser_input >= 0) {
			sink_input_info *input =
				sink_get_nth_input(ctx, sink,
						   ctx->chooser_input);
			index    = input->index;
			mute     = !input->mute;
			mute_set = pa_context_set_sink_input_mute;
		} else if (ctx->chooser_input == SELECTED_SINK) {
			index    = sink->index;
			mute     = !sink->mute;
			mute_set = pa_context_set_sink_mute_by_index;
		} else
			break;

		pa_operation_unref(mute_set(ctx->context, index, mute,
					    change_callback, ctx));
		break;

	case '\n':
	case '\t':
	case ' ':
		if (ctx->chooser_input == SELECTED_SINK)
			break;
		sink = g_list_nth_data(ctx->sink_list, ctx->chooser_sink);
		sink_input_info *input = sink_get_nth_input(ctx, sink,
							    ctx->chooser_input);
		if (g_list_length(ctx->sink_list) <= 1)
		    break;
		if (ctx->chooser_sink < (gint)g_list_length(ctx->sink_list) - 1)
			ctx->chooser_sink++;
		else
			ctx->chooser_sink = 0;

		sink = g_list_nth_data(ctx->sink_list, ctx->chooser_sink);
		/* chooser_input needs to be derived from $selected_index */
		o = pa_context_move_sink_input_by_index(ctx->context,
							input->index,
							sink->index,
							change_callback, NULL);
		pa_operation_unref(o);

		/* get new chooser_input, if non, select sink as fallback */
		ctx->chooser_input = SELECTED_SINK; 
		gint i = -1;
		for (GList *l = ctx->input_list; l; l = l->next) {
			sink_input_info *t = l->data;

			if (t->index == input->index) {
				ctx->chooser_input = ++i;
				break;
			}
			if (t->sink == sink->index)
				++i;
		}
		break;

	case 'q':
	default:
		quit(ctx);
		break;
	}

	return TRUE;
}

void
interface_clear(struct context *ctx)
{
	g_source_remove(ctx->resize_source_id);
	g_source_remove(ctx->input_source_id);
	close(ctx->signal_fd);
	clear();
	refresh();
	endwin();
}

void
interface_set_status(struct context *ctx, const gchar *msg)
{
	if (msg != NULL) {
		g_free(ctx->status);
		ctx->status = g_strdup(msg);
	}
	werase(ctx->msg_win);
	box(ctx->msg_win, 0, 0);
	if (ctx->status != NULL)
		mvwprintw(ctx->msg_win, 1, 1, ctx->status);
	wrefresh(ctx->msg_win);
	refresh();
}

#ifdef HAVE_SIGNALFD
static gboolean
resize_gio(GIOChannel *source, GIOCondition condition, gpointer data)
{
	struct context *ctx = data;
	struct signalfd_siginfo fdsi;
	ssize_t s;

	g_assert(condition & G_IO_IN);

	s = read(ctx->signal_fd, &fdsi, sizeof fdsi);
	if (s != sizeof fdsi || fdsi.ssi_signo != SIGWINCH)
		return FALSE;

	return interface_resize(ctx);
}
#endif

void
interface_init(struct context *ctx)
{
	GIOChannel *input_channel;

	/* Selected sink-device. 0 is the first device */
	ctx->chooser_sink  = 0;	
	/* Selected input of the current sink-device.  */
	/* SELECTED_SINK refers to sink-device itself  */
	ctx->chooser_input = SELECTED_SINK;
	initscr();
	clear();

	noecho();
	/* Line buffering disabled. pass on everything */
	cbreak();
	/* hide cursor */
	curs_set(0);

	/* 0,0,0,0 := fullscreen */
	ctx->menu_win = newwin(0, 0, 0, 0);
	ctx->msg_win  = newwin(0, 0, 0, 0);

	/* multichar keys are mapped to one char */
	keypad(ctx->menu_win, TRUE);

	/* "resizing" here is for initial box positioning and layout */ 
	interface_resize(ctx);

#ifdef HAVE_SIGNALFD
	{
		GIOChannel *channel;
		sigset_t mask;

		sigemptyset(&mask);
		sigaddset(&mask, SIGWINCH);

		if (sigprocmask(SIG_BLOCK, &mask, NULL) == -1)
			exit(EXIT_FAILURE);
		ctx->signal_fd = signalfd(-1, &mask, 0);
		channel = g_io_channel_unix_new(ctx->signal_fd);
		g_io_add_watch(channel, G_IO_IN, resize_gio, ctx);
		g_io_channel_unref(channel);
	}
#else
	/* register event handler for resize and input */
	ctx->resize_source_id =	unix_signal_add(SIGWINCH,
						interface_resize, ctx);
#endif
	input_channel = g_io_channel_unix_new(STDIN_FILENO);
	if (!input_channel)
		exit(EXIT_FAILURE);
	ctx->input_source_id = g_io_add_watch(input_channel, G_IO_IN,
					      interface_get_input, ctx);
	g_io_channel_unref(input_channel);

	refresh();
}