1 /*
2  * xmixer:main.c, tabstop=4
3  *
4  * Copyright (C) 1997 Rasca, Berlin
5  * EMail: thron@gmx.de
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21 
22 
23 #include <stdio.h>
24 #include <stdlib.h>		/* free() */
25 #include <unistd.h>
26 #include <limits.h>
27 #include "gui.h"
28 #include "mixer.h"
29 #include "scf.h"
30 
31 #define TRUE	1
32 #define FALSE	0
33 
34 /*
35  * give a short help how to use it and terminate
36  */
37 void
usage(const char * prg)38 usage (const char *prg)
39 {
40 	fprintf (stderr,
41 		"Usage: %s "
42 		"[-m mixer_device] [-q] [-n] [-d <display>] [-V] -[h?]\n",
43 		prg);
44 	exit(1);
45 }
46 
47 /*
48  * read the configuration file
49  */
50 int
parse_and_eval_scf(const char * prog_name,const string mixer)51 parse_and_eval_scf (const char *prog_name, const string mixer)
52 {
53 	scf_id scf;
54 	int nod, i, left =0 , right =0;
55 	string name;
56 
57 	scf = scf_init (prog_name, NULL);
58 	if (scf == SCF_FALSE) {
59 		fprintf (stderr, "Can't init scf handle!\n");
60 		return (FALSE);
61 	}
62 	scf_read (scf);
63 	nod = mixer_num_of_devs (1);
64 	for (i = 0; i < nod; i++) {
65 		name = (const string) mixer_get_name (1, i);
66 		if (scf_get_array_int_val (scf, mixer, name, 0, &left) == SCF_TRUE)
67 			mixer_set_vol_left (1, i, left);
68 		if (mixer_is_stereo (1, i)) {
69 			if (scf_get_array_int_val (scf, mixer, name, 1, &right) == SCF_TRUE)
70 				mixer_set_vol_right (1, i, right);
71 		}
72 	}
73 	scf_fini (scf);
74 	return (TRUE);
75 }
76 
77 /*
78  * just write the current settings of the mixer device to stdout
79  */
80 void
dump_settings(FILE * fp,const char * mixer_dev,int mixer_id)81 dump_settings (FILE *fp, const char *mixer_dev, int mixer_id) {
82 	int nod, i;
83 
84 	nod = mixer_num_of_devs (mixer_id);
85 	fprintf (fp, "[%s]\n", mixer_dev);
86 	for (i = 0; i < nod; i++) {
87 		if (mixer_is_stereo (1,i)) {
88 			fprintf (fp, "%s={%d %d}\n",
89 					mixer_get_name(1,i), mixer_get_vol_left(1,i),
90 					mixer_get_vol_right(1,i));
91 		} else {
92 			fprintf (fp, "%s={%d}\n",
93 				mixer_get_name(1,i), mixer_get_vol_left(1,i));
94 		}
95 	}
96 }
97 
98 /*
99  */
100 int
main(int argc,char ** argv)101 main ( int argc, char **argv) {
102 	int rc = 0, c;
103 	int quiet = FALSE, dump = FALSE, ignore_scf = FALSE;
104 	char *mixer_dev;
105 	char *dev_name;
106 
107 	mixer_dev = NULL;
108 
109 	while ((c = getopt(argc, argv, "?hm:qnVDd:g:xr:-")) != -1) {
110 		switch (c) {
111 			case '?':
112 			case 'h':
113 				usage (argv[0]);
114 				break;
115 			case 'm':
116 				mixer_dev = optarg;
117 				break;
118 			case 'q':
119 				quiet = TRUE;
120 				break;
121 			case 'd':
122 			case 'g':
123 			case 'x':
124 			case 'r':
125 				/* for the X11 stuff .. */
126 				break;
127 			case 'D':
128 				dump = TRUE;
129 				break;
130 			case 'n':
131 				ignore_scf = TRUE;
132 				break;
133 			case 'V':
134 				printf ("Version %s, "
135 						"(c) rasca, published unter the GNU GPL\n", VERSION);
136 				break;
137 			default:
138 				break;
139 		}
140 	}
141 	if (quiet || dump) {
142 		if (argc > optind) {
143 			usage (argv[0]);
144 		}
145 		if (mixer_dev == NULL)
146 			mixer_dev = DEFAULT_MIXER;
147 		if (!mixer_init (mixer_dev))
148 			return (1);
149 		if (dump) {
150 			dump_settings (stdout, mixer_dev, 1);
151 			return (0);
152 		}
153 		if (!ignore_scf) {
154 			parse_and_eval_scf (argv[0], mixer_dev);
155 		}
156 	} else {
157 		/* use gui */
158 		dev_name = gui_init (&argc, &argv);
159 		if (argc > optind) {
160 			usage (argv[0]);
161 		}
162 		if (mixer_dev == NULL) {
163 			if (dev_name == NULL)
164 				mixer_dev = DEFAULT_MIXER;
165 			else
166 				mixer_dev = dev_name;
167 		}
168 		if (!mixer_init (mixer_dev))
169 			return (1);
170 		if (!ignore_scf)
171 			parse_and_eval_scf (argv[0], mixer_dev);
172 		rc = gui_main (mixer_dev, argv[0]);
173 	}
174 	return (rc);
175 }
176 
177