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