new function: set_stream_fds()
[paraslash.git] / fade.c
1 /*
2 * Copyright (C) 1998-2005 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file fade.c a volume fader and alarm clock */
20
21 #include "fade.cmdline.h"
22 #include "para.h"
23 #include "fd.h"
24
25 #include <ctype.h>
26 #include <curses.h>
27 #include <stdlib.h> /* EXIT_SUCCESS */
28 #include <unistd.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <linux/soundcard.h>
33 #include "string.h"
34
35
36 struct gengetopt_args_info args_info;
37
38 void para_log(__unused int ll, const char *fmt,...)
39 {
40 va_list argp;
41 time_t t1;
42 struct tm *tm;
43
44 time(&t1);
45 tm = localtime(&t1);
46 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
47 va_start(argp, fmt);
48 vprintf(fmt, argp);
49 va_end(argp);
50 }
51
52 /*
53 * open mixer device
54 */
55 static int open_mixer(void)
56 {
57 int mixer_fd;
58
59 if ((mixer_fd = open(args_info.mixer_device_arg, O_RDWR, 0)) < 0)
60 return -1;
61 return mixer_fd;
62 }
63
64 /*
65 * get volume via mixer_fd
66 */
67 static int do_get_vol(int mixer_fd)
68 {
69 int volume;
70
71 if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
72 return -1;
73 /* take the mean value of left and right volume */
74 return (volume % 256 + (volume >> 8)) / 2;
75 }
76
77 /*
78 * open mixer, get volume and close mixer
79 */
80 static int get_vol(void)
81 {
82 int mixer_fd;
83 int volume;
84
85 mixer_fd = open_mixer();
86 if (mixer_fd < 0)
87 return -1;
88 volume = do_get_vol(mixer_fd);
89 close(mixer_fd);
90 return volume;
91 }
92
93 /*
94 * set volume via mixer_fd
95 */
96 static int do_set_vol(int mixer_fd, int volume)
97 {
98 int tmp;
99 tmp = (volume << 8) + volume;
100 if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
101 return 0;
102 return 1;
103 }
104
105 /*
106 * open mixer, set volume and close mixer
107 */
108 static int set_vol(int volume)
109 {
110 int mixer_fd;
111 int ret;
112
113 mixer_fd = open_mixer();
114 ret = 0;
115 if (mixer_fd < 0)
116 goto out;
117 if (!do_set_vol(mixer_fd, volume))
118 goto out;
119 ret = 1;
120 close(mixer_fd);
121 out:
122 return ret;
123 }
124
125 /*
126 * Open mixer, get volume, fade to new_vol in secs seconds and
127 * close mixer
128 */
129 static int fade(int new_vol, unsigned int secs)
130 {
131 int vol, mixer_fd = -1, diff, incr, ret;
132 struct timespec ts;
133 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
134
135 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
136 ret = 0;
137 if (!secs)
138 goto out;
139 mixer_fd = open_mixer();
140 if (mixer_fd < 0)
141 goto out;
142 vol = do_get_vol(mixer_fd);
143 if (vol < 0)
144 goto out;
145 diff = new_vol - vol;
146 if (!diff) {
147 sleep(secs);
148 ret = 1;
149 goto out;
150 }
151 incr = diff > 0? 1: -1;
152 diff = diff > 0? diff: -diff;
153 tmp = secs * 1000 / diff;
154 tmp2 = tmp % 1000;
155 while ((new_vol - vol) * incr > 0) {
156 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
157 ts.tv_sec = tmp / 1000; /* really nec ?*/
158 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
159 vol += incr;
160 if (!do_set_vol(mixer_fd, vol))
161 goto out;
162 //printf("vol = %i\n", vol);
163 nanosleep(&ts, NULL);
164 }
165 out:
166 if (mixer_fd >= 0)
167 close(mixer_fd);
168 return 1;
169 }
170
171 static int client_cmd(const char *cmd,...)
172 {
173 int ret, fds[3] = {0, 0, 0};
174 pid_t pid;
175 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
176 PARA_INFO_LOG("%s", cmdline);
177 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
178 free(cmdline);
179 return ret;
180 }
181
182 /*
183 * sleep
184 */
185 static void sweet_dreams(void)
186 {
187 time_t t1, wake_time_epoch;
188 unsigned int delay;
189 struct tm *tm;
190 int hour = args_info.wake_hour_arg;
191 int min = args_info.wake_min_arg;
192 char *fa_stream = args_info.fa_stream_arg;
193 char *wake_stream = args_info.wake_stream_arg;
194 //char *current_stream = stat_items[STREAM].content;
195 int wf = args_info.wake_fade_arg;
196 int sf = args_info.fa_fade_arg;
197 int wv = args_info.wake_vol_arg;
198 int sv = args_info.fa_vol_arg;
199 int iv = args_info.sleep_ivol_arg;
200 char *cmd, *sleep_stream = args_info.sleep_stream_given?
201 args_info.sleep_stream_arg : NULL;
202
203 if (sf) {
204 PARA_INFO_LOG("initial volume: %d\n", iv);
205 set_vol(iv);
206 cmd = make_message("csp %s\n", fa_stream);
207 client_cmd(cmd);
208 free(cmd);
209 fade(sv, sf);
210 }
211 if (sleep_stream) {
212 cmd = make_message("csp %s\n", sleep_stream);
213 client_cmd(cmd);
214 free(cmd);
215 } else
216 client_cmd("stop");
217 if (!wf)
218 return;
219 /* calculate wake time */
220 time(&t1);
221 tm = localtime(&t1);
222 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
223 /* wake time is tomorrow */
224 t1 += 86400;
225 tm = localtime(&t1);
226 t1 -= 86400;
227 }
228 tm->tm_hour = hour;
229 tm->tm_min = min;
230 tm->tm_sec = 0;
231 wake_time_epoch = mktime(tm);
232 PARA_INFO_LOG("waketime: %s", asctime(tm));
233 while (wake_time_epoch > t1 + wf) {
234 delay = wake_time_epoch - t1 - wf;
235 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
236 delay, delay / 3600,
237 (delay % 3600) / 60);
238 sleep(delay);
239 time(&t1);
240 }
241 cmd = make_message("csp %s\n", wake_stream);
242 client_cmd(cmd);
243 free(cmd);
244 fade(wv, wf);
245 PARA_INFO_LOG("%s", "fade complete, returning\n");
246 }
247
248 static void snooze(void)
249 {
250 if (get_vol() < args_info.snooze_out_vol_arg)
251 set_vol(args_info.snooze_out_vol_arg);
252 else
253 fade(args_info.snooze_out_vol_arg, args_info.snooze_out_fade_arg);
254 client_cmd("pause");
255 PARA_NOTICE_LOG("%d seconds snooze time...\n", args_info.snooze_time_arg);
256 sleep(args_info.snooze_time_arg);
257 client_cmd("play");
258 fade(args_info.snooze_in_vol_arg, args_info.snooze_in_fade_arg);
259 }
260
261 static int configfile_exists(void)
262 {
263 static char *config_file;
264
265 if (!args_info.config_file_given) {
266 char *home = para_homedir();
267 const char *conf = ".paraslash/fade.conf";
268 free(config_file);
269 config_file = make_message("%s/%s", home, conf);
270 free(home);
271 args_info.config_file_arg = config_file;
272 }
273 return file_exists(args_info.config_file_arg);
274 }
275
276
277 int main(int argc, char *argv[])
278 {
279 int ret;
280
281 if (cmdline_parser(argc, argv, &args_info))
282 exit(EXIT_FAILURE);
283 ret = configfile_exists();
284 if (!ret && args_info.config_file_given) {
285 PARA_EMERG_LOG("can not read config file %s\n",
286 args_info.config_file_arg);
287 exit(EXIT_FAILURE);
288 }
289 if (ret)
290 cmdline_parser_configfile(args_info.config_file_arg,
291 &args_info, 0, 0, 0);
292 if ((ret = open_mixer()) < 0) {
293 PARA_EMERG_LOG("can not open mixer device %s.",
294 args_info.mixer_device_arg);
295 exit(EXIT_FAILURE);
296 } else
297 close(ret);
298 ret = 0;
299 setlinebuf(stdout);
300 if (!strcmp(args_info.mode_arg, "sleep")) {
301 sweet_dreams();
302 goto out;
303 }
304 if (!strcmp(args_info.mode_arg, "fade")) {
305 fade(args_info.fade_vol_arg, args_info.fade_time_arg);
306 goto out;
307 }
308 if (!strcmp(args_info.mode_arg, "snooze")) {
309 snooze();
310 goto out;
311 }
312 ret = -1;
313 out:
314 return ret;
315 }