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