summaryrefslogtreecommitdiff
path: root/src/interface.c
blob: 462b82ce650f591e65ce135b45d1a37e7a3b2f89 (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
#include <stdio.h>
#include <pulse/pulseaudio.h>
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>

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

#define VOLUME_MAX UINT16_MAX
#define VOLUME_BAR_LEN 50
#define WIDTH 80
#define HEIGHT 10

// ncurses
WINDOW *menu_win;
int chooser_sink;
int chooser_input;
int startx;
int starty;

extern int sink_counter;
extern int sink_max;
extern sink_info** sink_list;

extern pa_context* context;

void interface_init(void)
{
	// ncurses
	chooser_sink = 0;
	chooser_input = 0;

	initscr();
	clear();
	noecho();
	cbreak();	/* Line buffering disabled. pass on everything */
	startx = (80 - WIDTH) / 2;
	starty = (24 - HEIGHT) / 2;
	menu_win = newwin(HEIGHT, WIDTH, starty, startx);
	keypad(menu_win, TRUE);
	mvprintw(0, 0, "Use arrow keys to go up and down, Press enter to select a choice");
	refresh();
}

void print_sink_list(void) {
	int x, y, i;
	x = 2;
	y = 2;
	
	box(menu_win, 0, 0);

//	printf("print sinks: %d\n", sink_input_counter);

//	qsort(sink_input_list, sink_input_counter, sizeof(sink_input_info*), cmp_sink_input_list);
	
	for (i = 0; i < sink_counter; ++i) {
		
		if (i == chooser_sink)
			wattron(menu_win, A_REVERSE);

		mvwprintw(menu_win, y+i, x, "%d\t%s\t",
			sink_list[i]->index,
			sink_list[i]->name);

		print_input_list(i);

		if (i == chooser_sink)
			wattroff(menu_win, A_REVERSE);
	}
	y += i;
/*	for (i = 0; i < sink_input_counter; ++i) {
		if (i == chooser)
			wattron(menu_win, A_REVERSE);

		mvwprintw(menu_win, y+i, x, "%d\t%s\t",
			sink_input_list[i]->sink,
			sink_input_list[i]->name);

		if (i == chooser)
			wattroff(menu_win, A_REVERSE);

		print_volume(sink_input_list[i]->vol, y+i);
	}*/
}

void print_input_list(int sink_num) {
	int offset = sink_num + 1 + 2;
	for (int i = 0; i < sink_num; ++i)
		offset += sink_list[i]->input_counter;

	for (int i = 0; i < sink_list[sink_num]->input_counter; ++i) {
		mvwprintw(menu_win, offset + i, 2 + 5, "\t%s",
			sink_list[sink_num]->input_list[i]->name);
	}
		
}

void print_volume(pa_volume_t volume, int y) {

	int x = 20;

	unsigned int vol = (unsigned int) ( (((double)volume) / ((double)VOLUME_MAX)) * VOLUME_BAR_LEN );
	mvwprintw(menu_win, y, x - 1 , "[");
	for (int i = 0; i < vol; ++i)
		mvwprintw(menu_win, y, x + i, "=");
	for (int i = vol; i < VOLUME_BAR_LEN; ++i)
		mvwprintw(menu_win, y, x + i, " ");
	
	mvwprintw(menu_win, y, x + VOLUME_BAR_LEN, "]");
}

void get_input(void)
{
	int c;
//	uint32_t sink;
	c = wgetch(menu_win);
	switch (c) {
		case KEY_UP:
			if (chooser_input == -1 && chooser_sink > 0) {
				--chooser_sink;
				chooser_input = sink_list[chooser_sink]->input_counter - 1;
			}

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

		case KEY_DOWN:
			if (chooser_input == sink_list[chooser_sink]->input_counter - 1 && chooser_sink < sink_counter - 1) {
					++chooser_sink;
					chooser_input = 0;
			}
			else if (chooser_input < sink_list[chooser_sink]->input_counter)
				++chooser_input;
			break;

		case KEY_LEFT:
			break;

		case KEY_RIGHT:
			break;

		case 32:
			
/*			if (sink_input_list[chooser]->sink < sink_max)
				sink = sink_input_list[chooser]->sink + 1;
			else
				sink = 0;

			pa_operation_unref(
				pa_context_move_sink_input_by_index(
					context, 
					sink_input_list[chooser]->index,
					sink, 
					change_callback, 
					NULL));
			return;*/
			break;

		default:
			printf("key: %d\n", c);
			quit();
			break;
	}
	
	collect_all_info();
//	pa_operation_unref(pa_context_get_sink_info_list(context, get_sink_info_callback, NULL));
//	sink_input_counter = 0;
//	pa_operation_unref(pa_context_get_sink_input_info_list(context, get_sink_input_info_callback, NULL));
}

void interface_clear(void)
{
	clrtoeol();
	refresh();
	endwin();
	exit(0);
}