File: nmixer.cc

package info (click to toggle)
mp3blaster 2-0b16-1.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,012 kB
  • ctags: 1,241
  • sloc: cpp: 12,228; ansic: 1,557; sh: 362; makefile: 107
file content (332 lines) | stat: -rw-r--r-- 7,483 bytes parent folder | download
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
#include <nmixer.h>
#include <string.h>
#define MIXER_DEVICE "/dev/mixer"

#define BOTH_CHANNELS 0x11
#define RIGHT_CHANNEL 0x10
#define LEFT_CHANNEL  0x01

#ifndef mvwchgat
#define mvwchgat(a, b, c, d, e, f, g)
#endif

NMixer::NMixer(WINDOW *mixwin, const char *mixdev, int yoffset, int nrlines,
	int *pairs, int bgcolor)
{
	if (!mixdev)
	{
		this->mixdev = new char[strlen(MIXER_DEVICE)+1];
		strcpy(this->mixdev, MIXER_DEVICE);
	}
	else
	{
		this->mixdev = new char[strlen(mixdev)+1];
		strcpy(this->mixdev, mixdev);
	}
	this->mixwin = mixwin;
	this->yoffset = yoffset;
	this->nrlines = nrlines;
	this->bgcolor = bgcolor;
	getmaxyx(mixwin, maxy, maxx);
	if (!(this->nrlines))
		this->nrlines = maxy - this->yoffset;
	
	cpairs = new int[4];

	/* init colors */
	for (int i = 0; i < 4; i++)
	{
		if (pairs)
			cpairs[i] = pairs[i];
		else
			cpairs[i] = i;
	}
}

NMixer::~NMixer()
{
	if (supported) delete[] supported;
	if (cpairs) delete[] cpairs;
	if (mixers) delete mixers;
	if (mixdev) delete[] mixdev;
}

short
NMixer::NMixerInit()
{
	nrbars = 0;
	supported = NULL;
	//sbars = NULL;
	
	init_pair(cpairs[0], COLOR_WHITE, bgcolor);
	init_pair(cpairs[1], COLOR_GREEN, bgcolor);
	init_pair(cpairs[2], COLOR_YELLOW, bgcolor);
	init_pair(cpairs[3], COLOR_RED, bgcolor);

	if (maxx < 80 || nrlines < 8)
	{
		fprintf(stderr, "Window too small for mixer!\n");
		fflush(stderr);
		return 0;
	}

#ifdef HAVE_NASPLAYER
	if (mixdev && !strcmp(mixdev, "NAS")) //NAS-mixer
		mixers = new NASMixer(NULL);
	else
#endif
		mixers = new OSSMixer(NULL);
	//mixers = new OSSMixer(new NASMixer());
	supported = mixers->GetDevices(&nrbars);

	/* maxspos == max# bars ON-SCREEN */
	maxspos = ((nrlines - 1) / 3) - 1; /* 1 nonbar-line[s], 3 lines/bar */
	if ( (maxspos + 1) > nrbars)
		maxspos = nrbars - 1;
	currentspos = 0; /* current ON-SCREEN bar */
	minbar = 0;
	currentbar = 0; /* current bar (index) */

	for ( int i = 0; i < MYMIN(nrbars, (maxspos + 1)); i++)
	{
		mvwprintw(mixwin, yoffset + 2 + 3 * i, 65, "L");
		mvwprintw(mixwin, yoffset + 3 + 3 * i, 65, "R");
	}
	mvwprintw(mixwin, yoffset, 10,
		"0        1         2         3         4         5");
	wrefresh(mixwin);

	/* Draw all Scrollbars */
	RedrawBars();

	/* initialiation succeeded. */
	return 1;
}

void
NMixer::DrawScrollbar(short i, int spos)
{
	int j;
	int my_x, my_y;
	struct volume vol;
	const char *source = mixers->GetMixerLabel(supported[i]);
	const char empty_scrollbar[] = \
		"                                                          ";

	my_x = 2;
	my_y = yoffset + 2 + (3 * spos);
	
	//clear 2x58 positions on window
	mvwprintw(mixwin, my_y - 1, my_x, (char*)empty_scrollbar);
	mvwprintw(mixwin, my_y, my_x, empty_scrollbar);
	mvwprintw(mixwin, my_y + 1, my_x, empty_scrollbar);

	//draw new bar
	mvwprintw(mixwin, my_y - 1, my_x, source);
	if (i == currentbar)
	{
		mvwchgat(mixwin, my_y - 1, my_x, strlen(source), A_REVERSE, 
			cpairs[0], NULL);
	}

	/* get sound-settings */
	if (!(mixers->GetMixer(supported[i], &vol)))
		return;
	/* setting = setting / 2; */
	vol.left  /= 2;
	vol.right /= 2;
	if (vol.left  > 50) vol.left  = 50;
	if (vol.right > 50) vol.right = 50;

	mvwaddch(mixwin, my_y, my_x + 7, '[');
	mvwaddch(mixwin, my_y + 1, my_x + 7, '[');
	mvwaddch(mixwin, my_y, my_x + 58, ']');
	mvwaddch(mixwin, my_y + 1, my_x + 58, ']');
	wmove(mixwin, my_y, my_x + 8);
	for (j = 0; j < vol.left; j++)
		waddch(mixwin, '#');
	for (j = vol.left; j < 50; j++)
		waddch(mixwin, ' ');

	wmove(mixwin, my_y + 1, my_x + 8);
	for (j = 0; j < vol.right; j++)
		waddch(mixwin, '#');
	for (j = vol.right; j < 50; j++)
		waddch(mixwin, ' ');

	/* fix up some nice colors */
	mvwchgat(mixwin, my_y + 0, my_x + 8,  17, A_BOLD, cpairs[1], NULL);
	mvwchgat(mixwin, my_y + 0, my_x + 25, 16, A_BOLD, cpairs[2], NULL);
	mvwchgat(mixwin, my_y + 0, my_x + 41, 17, A_BOLD, cpairs[3], NULL);
	mvwchgat(mixwin, my_y + 1, my_x + 8,  17, A_BOLD, cpairs[1], NULL);
	mvwchgat(mixwin, my_y + 1, my_x + 25, 16, A_BOLD, cpairs[2], NULL);
	mvwchgat(mixwin, my_y + 1, my_x + 41, 17, A_BOLD, cpairs[3], NULL);

	wrefresh(mixwin);
	move(0, 0);
}

/* bar     : Which bar to modify (prolly currentsbar)
 * amount  : Amount by which to change OR (if absolute != 0) the absolute
 *         : value (0<=value<=100) to change to.
 * absolute: !0 if amount is an absolute value (and not relative to current
 *         : setting)
 * update  : Wether to update the screen or not..
 * channels: One of LEFT_CHANNEL/RIGHT_CHANNEL/BOTH_CHANNELS
 */
void
NMixer::ChangeBar(short bar, short amount, short absolute, short channels,
	short update)
{
	struct volume vol;

	if (!(mixers->GetMixer(supported[bar], &vol)))
		return;

	if (channels == BOTH_CHANNELS || channels == LEFT_CHANNEL)
	{
		if (absolute) vol.left = 0;
		vol.left += amount;

		if (vol.left < 0) vol.left = 0;
		if (vol.left > 100) vol.left = 100;
	}

	if (channels == BOTH_CHANNELS || channels == RIGHT_CHANNEL)
	{	
		if (absolute) vol.right = 0;
		vol.right += amount;

		if (vol.right < 0) vol.right = 0;
		if (vol.right > 100) vol.right = 100;
	}
	if (mixers->SetMixer(supported[bar], &vol) && update)
		DrawScrollbar(bar, currentspos);
}

void
NMixer::RedrawBars()
{
	int i;

	for (i = 0; i < (maxspos + 1); i++)
		DrawScrollbar(minbar + i, i);
}

short
NMixer::ProcessKey(int key)
{
	short oldbar = currentbar;

	if (key == KEY_F(12) || key == 'q')
		return 0;

	switch(key)
	{
		case KEY_DOWN:
		case 'j':
			if (++currentbar >= nrbars) /* flip da dip, B! */
			{ 
				currentbar = 0;
				minbar = 0;
				currentspos = 0;
				RedrawBars();
			}
			else
			{
				if (currentspos == maxspos) /* scroll bars up by one */
				{
					minbar++;
					RedrawBars();
				}
				else
				{
					currentspos++;	
					DrawScrollbar(oldbar, currentspos - 1);
					DrawScrollbar(currentbar, currentspos);
				}
			}
			break;
		case KEY_UP:
		case 'k':
			if (--currentbar < 0)
			{
				currentbar = nrbars - 1;
				currentspos = maxspos;
				minbar = nrbars - maxspos - 1;
				RedrawBars();
			}
			else
			{
				if (!currentspos)
				{
					minbar--;
					RedrawBars();
				}
				else
				{
					currentspos--;
					DrawScrollbar(oldbar, currentspos + 1);
					DrawScrollbar(currentbar, currentspos);
				}
			}
			break;
		case KEY_RIGHT:
		case 'l':
			ChangeBar(currentbar,  2, 0, BOTH_CHANNELS);
			break;
		case KEY_LEFT:
		case 'h':
			ChangeBar(currentbar, -2, 0, BOTH_CHANNELS);
			break;
		case KEY_PPAGE:
			ChangeBar(currentbar, 2, 0, LEFT_CHANNEL);
			break;
		case KEY_HOME:
			ChangeBar(currentbar, -2, 0, LEFT_CHANNEL);
			break;
		case KEY_NPAGE:
			ChangeBar(currentbar, 2, 0, RIGHT_CHANNEL);
			break;
		case '0':
			ChangeBar(currentbar, 0, 1, BOTH_CHANNELS);
			break;
		case '1':
			ChangeBar(currentbar, 20, 1, BOTH_CHANNELS);
			break;
		case '2':
			ChangeBar(currentbar, 40, 1, BOTH_CHANNELS);
			break;
		case '3':
			ChangeBar(currentbar, 60, 1, BOTH_CHANNELS);
			break;
		case '4':
			ChangeBar(currentbar, 80, 1, BOTH_CHANNELS);
			break;
		case '5':
			ChangeBar(currentbar, 100, 1, BOTH_CHANNELS);
			break;
		case KEY_END:
			ChangeBar(currentbar, -2, 0, RIGHT_CHANNEL);
			break;
	}
	return 1;
}

void
NMixer::SetMixer(int device, struct volume value, short update)
{
	for (int i = 0; i < nrbars; i++)
		if (supported[i] == device)
		{
			ChangeBar(i, value.left, 1, LEFT_CHANNEL, update);
			ChangeBar(i, value.right, 1, RIGHT_CHANNEL, update);
			break;
		}
}

bool
NMixer::GetMixer(int device, struct volume *vol)
{
	return mixers->GetMixer(device, vol);
}