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