File: main.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 (187 lines) | stat: -rw-r--r-- 3,723 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
/* A soundmixer for linux with a nice text-interface for those non-X-ers.
 * (C)1999 Bram Avontuur (brama@stack.nl)
 */
#include "nmixer.h"
#include <string.h>
#include <stdlib.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef HAVE_SYS_SOUNDCARD_H
#include <sys/soundcard.h>
#elif HAVE_MACHINE_SOUNDCARD_H
#include <machine/soundcard.h>
#elif HAVE_SOUNDCARD_H
#include <soundcard.h>
#endif

#ifdef __cplusplus
}
#endif

/* Prototypes */
short is_mixer_device(const char *mdev);
void usage(const char *);

extern char **environ; //needed for getenv!

int
main(int argc, char *argv[])
{
	NMixer *bla;
	int
		*initdevs = NULL,
		*initsets = NULL,
		initnr = 0,
		no_interface = 0;

	/* initialize curses screen management */
	initscr();
	start_color();
	cbreak();
	noecho();
	nonl();
	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);

	init_pair(0, COLOR_WHITE, COLOR_BLACK);
	init_pair(1, COLOR_GREEN, COLOR_BLACK);
	init_pair(2, COLOR_YELLOW, COLOR_BLACK);
	init_pair(3, COLOR_RED, COLOR_BLACK);

	border(0, 0, 0, 0, 0, 0, 0, 0);
	if (COLS < 80 || LINES < 8)
	{
		mvprintw(1,1, "You'll need at least an 80x8 terminal, sorry.");
		getch();
		endwin();
		exit(1);
	}

	/* parse arguments */
	for (int i = 1; i < (argc); i++)
	{
		char soundlabel[10];
		int soundvol, devindex;

		if (sscanf(argv[i], "%9[^=]=%d", soundlabel, &soundvol) != 2)
		{
			if (!strcmp(argv[i], "-q"))
			{
				no_interface=1;
				continue;
			}
			else
			{
				endwin();
				usage(argv[0]);
				exit(1);
			}
		}
		if ((devindex = is_mixer_device(soundlabel)) < 0)
		{
			endwin();
			fprintf(stderr, "%s is not a valid mixer-device.\n", soundlabel);
			usage(argv[0]);
			exit(1);
		}
	
		if (soundvol <0 || soundvol > 100)
		{
			endwin();
			fprintf(stderr, "Volume for %s out of range (range is 0..100)\n",
				soundlabel);
			exit(1);
		}

		/* device exists, whow whow whow yippee yo yippee yay */
		if (!(initdevs = (int*)realloc(initdevs, (++initnr * sizeof(int)))) ||
			!(initsets = (int*)realloc(initsets, (initnr * sizeof(int)))))
		{
			endwin();
			fprintf(stderr, "Allocation error!\n");
			exit(1);
		}
		initdevs[initnr-1] = devindex;
		initsets[initnr-1] = soundvol;
	}

	mvprintw(0, (COLS - strlen(MYVERSION)) / 2, MYVERSION);

	if (!strstr(argv[0], "nasmixer"))
		bla = new NMixer(stdscr, (const char*)NULL, 2, LINES - 4);
	else //use NAS-mixer!
	{
		const char *devnam = getenv("DISPLAY");
		if (!devnam)
		{
			endwin();
			fprintf(stderr, "Set DISPLAY environment variable if you want "\
				"to use the NAS mixer.\n");
			exit(1);
		}
#if defined(HAVE_NASPLAYER)
		bla = new NMixer(stdscr, "NAS", 2, LINES - 4);
#else
		endwin();
		fprintf(stderr, "Nmixer was not compiled with NAS support.\n");
		exit(1);
#endif
	}

	if (!(bla->NMixerInit()))
	{
		endwin();
		fprintf(stderr, "Couldn't initialize mixer.\n");
		exit(1);
	}
	
	for (int i = 0; i < initnr; i++)
	{
		struct volume tmpvol;

		tmpvol.left = tmpvol.right = initsets[i];
		bla->SetMixer(initdevs[i], tmpvol, 0);
	}
	if (initnr > -1)
		bla->RedrawBars();

	if (!no_interface)
		/* get input */
		while (bla->ProcessKey(getch()));

	endwin();
	return 0;
}

short
is_mixer_device(const char *mdev)
{
	const char *devlabels[] = SOUND_DEVICE_NAMES;
	short isdev = -1;

	for (int i = 0; i < SOUND_MIXER_NRDEVICES; i++)
	{
		if (!strcmp(mdev, devlabels[i]))
		{
			isdev = i;
		}
	}

	return isdev;
}

void
usage(const char *progname)
{
	fprintf(stderr, "Usage: %s [<mixerdevice=volume> ...]\n\n" \
		"Valid mixer-devices for your system (not necessarily supported " \
		"by your sound hardware!) are:", progname);

	const char *devs[] = SOUND_DEVICE_NAMES;
	for (int i = 0; i < SOUND_MIXER_NRDEVICES; i++)
		fprintf(stderr, " %s", devs[i]);
	fprintf(stderr, ".\n");
}