]> git.tuebingen.mpg.de Git - paraslash.git/blob - fade.c
fade: Switch to the fade-in mood *before* sleeping.
[paraslash.git] / fade.c
1 /*
2  * Copyright (C) 1998-2013 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 for OSS. */
8
9 #include <regex.h>
10
11 #include "fade.cmdline.h"
12 #include "para.h"
13 #include "fd.h"
14 #include "string.h"
15 #include "mix.h"
16 #include "error.h"
17 #include "ggo.h"
18 #include "version.h"
19
20 INIT_FADE_ERRLISTS;
21 static struct fade_args_info conf;
22
23 enum mixer_id {MIXER_ENUM};
24 static char *mixer_name[] = {MIXER_NAMES};
25 DECLARE_MIXER_INITS;
26 static struct mixer supported_mixer[] = {MIXER_ARRAY};
27 #define FOR_EACH_MIXER(i) for ((i) = 0; (i) < NUM_SUPPORTED_MIXERS; (i)++)
28
29 static int loglevel;
30 static __printf_2_3 void date_log(int ll, const char *fmt, ...)
31 {
32         va_list argp;
33         time_t t1;
34         struct tm *tm;
35
36         if (ll < loglevel)
37                 return;
38         time(&t1);
39         tm = localtime(&t1);
40         fprintf(stderr, "%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
41         va_start(argp, fmt);
42         vprintf(fmt, argp);
43         va_end(argp);
44 }
45 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
46
47 static int set_channel(struct mixer *m, struct mixer_handle *h, const char *channel)
48 {
49         char *channels;
50         int ret;
51
52         ret = m->set_channel(h, channel);
53         if (ret >= 0) {
54                 PARA_NOTICE_LOG("using %s mixer channel\n",
55                         channel? channel : "default");
56                 return ret;
57         }
58         channels = m->get_channels(h);
59         printf("Available channels: %s\n", channels);
60         free(channels);
61         return ret;
62 }
63
64 /* Fade to new volume in fade_time seconds. */
65 static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
66 {
67         int vol, diff, incr, ret;
68         unsigned secs;
69         struct timespec ts;
70         unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
71
72         if (fade_time <= 0)
73                 return m->set(h, new_vol);
74         secs = fade_time;
75         ret = m->get(h);
76         if (ret < 0)
77                 goto out;
78         vol = ret;
79         PARA_NOTICE_LOG("fading %s from %d to %d in %d seconds\n",
80                 conf.mixer_channel_arg, vol, new_vol, secs);
81         diff = new_vol - vol;
82         if (!diff) {
83                 sleep(secs);
84                 goto out;
85         }
86         incr = diff > 0? 1: -1;
87         diff = diff > 0? diff: -diff;
88         tmp = secs * 1000 / diff;
89         tmp2 = tmp % 1000;
90         while ((new_vol - vol) * incr > 0) {
91                 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
92                 ts.tv_sec = tmp / 1000; /* really nec ?*/
93                 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
94                 vol += incr;
95                 ret = m->set(h, vol);
96                 if (ret < 0)
97                         goto out;
98                 //printf("vol = %i\n", vol);
99                 nanosleep(&ts, NULL);
100         }
101 out:
102         return ret;
103 }
104
105 static void client_cmd(const char *cmd)
106 {
107         int ret, status, fds[3] = {0, 0, 0};
108         pid_t pid;
109         char *cmdline = make_message(BINDIR "/para_client %s", cmd);
110
111         PARA_NOTICE_LOG("%s\n", cmdline);
112         ret = para_exec_cmdline_pid(&pid, cmdline, fds);
113         free(cmdline);
114         if (ret < 0) {
115                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
116                 goto fail;
117         }
118         do
119                 pid = waitpid(pid, &status, 0);
120         while (pid == -1 && errno == EINTR);
121         if (pid < 0) {
122                 PARA_ERROR_LOG("%s\n", strerror(errno));
123                 goto fail;
124         }
125         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
126                 goto fail;
127         return;
128 fail:
129         PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
130         exit(EXIT_FAILURE);
131 }
132
133 static void change_afs_mode(char *afs_mode)
134 {
135         char *cmd;
136
137         client_cmd("stop");
138         if (!afs_mode)
139                 return;
140         cmd = make_message("select %s", afs_mode);
141         client_cmd(cmd);
142         free(cmd);
143 }
144
145 static int set_initial_volume(struct mixer *m, struct mixer_handle *h)
146 {
147         int i, ret;
148
149         for (i = 0; i < conf.ivol_given; i++) {
150                 char *p, *ch, *arg = para_strdup(conf.ivol_arg[i]);
151                 int32_t iv;
152                 p = strchr(arg, ':');
153                 if (p) {
154                         *p = '\0';
155                         p++;
156                         ch = arg;
157                 } else {
158                         p = arg;
159                         ch = NULL;
160                 }
161                 ret = para_atoi32(p, &iv);
162                 if (ret < 0) {
163                         free(arg);
164                         return ret;
165                 }
166                 ret = set_channel(m, h, ch);
167                 if (!ch)
168                         ch = "default";
169                 if (ret < 0) {
170                         PARA_WARNING_LOG("ignoring channel %s\n", ch);
171                         ret = 0;
172                 } else {
173                         PARA_INFO_LOG("initial volume %s: %d\n", ch, iv);
174                         ret = m->set(h, iv);
175                 }
176                 free(arg);
177                 if (ret < 0)
178                         return ret;
179         }
180         return 1;
181 }
182
183 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
184 {
185         time_t t1, wake_time_epoch;
186         unsigned int delay;
187         struct tm *tm;
188         int ret, min = conf.wake_min_arg;
189         char *fo_mood = conf.fo_mood_arg;
190         char *fi_mood = conf.fi_mood_arg;
191         char *sleep_mood = conf.sleep_mood_arg;
192         int fit = conf.fi_time_arg;
193         int fot = conf.fo_time_arg;
194         int fiv = conf.fi_vol_arg;
195         int fov = conf.fo_vol_arg;
196
197         /* calculate wake time */
198         time(&t1);
199         if (conf.wake_hour_given) {
200                 int hour = conf.wake_hour_arg;
201                 tm = localtime(&t1);
202                 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
203                         t1 += 86400; /* wake time is tomorrow */
204                         tm = localtime(&t1);
205                 }
206                 tm->tm_hour = hour;
207                 tm->tm_min = min;
208                 tm->tm_sec = 0;
209         } else {
210                 t1 += 9 * 60 * 60; /* nine hours from now */
211                 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
212                 tm = localtime(&t1);
213         }
214         wake_time_epoch = mktime(tm);
215         PARA_INFO_LOG("waketime: %s", asctime(tm));
216         client_cmd("stop");
217         sleep(1);
218         if (fot) {
219                 ret = set_initial_volume(m, h);
220                 if (ret < 0)
221                         return ret;
222                 change_afs_mode(fo_mood);
223                 client_cmd("play");
224                 ret = set_channel(m, h, conf.mixer_channel_arg);
225                 if (ret < 0)
226                         return ret;
227                 ret = fade(m, h, fov, fot);
228                 if (ret < 0)
229                         return ret;
230         } else {
231                 ret = m->set(h, fov);
232                 if (ret < 0)
233                         return ret;
234         }
235         if (conf.sleep_mood_given) {
236                 change_afs_mode(sleep_mood);
237                 client_cmd("play");
238         } else
239                 client_cmd("stop");
240         if (!fit)
241                 return 1;
242         change_afs_mode(fi_mood);
243         for (;;) {
244                 time(&t1);
245                 if (wake_time_epoch <= t1 + fit)
246                         break;
247                 delay = wake_time_epoch - t1 - fit;
248                 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
249                         delay, delay / 3600,
250                         (delay % 3600) / 60);
251                 sleep(delay);
252         }
253         client_cmd("play");
254         ret = fade(m, h, fiv, fit);
255         PARA_INFO_LOG("fade complete, returning\n");
256         return ret;
257 }
258
259 static int snooze(struct mixer *m, struct mixer_handle *h)
260 {
261         int ret, val;
262
263         if (conf.so_time_arg <= 0)
264                 return 1;
265         ret = m->get(h);
266         if (ret < 0)
267                 return ret;
268         val = ret;
269         if (val < conf.so_vol_arg)
270                 ret = m->set(h, conf.so_vol_arg);
271         else
272                 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
273         if (ret < 0)
274                 return ret;
275         client_cmd("pause");
276         PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
277         sleep(conf.snooze_time_arg);
278         client_cmd("play");
279         return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
280 }
281
282 static int configfile_exists(void)
283 {
284         static char *config_file;
285
286         if (!conf.config_file_given) {
287                 char *home = para_homedir();
288                 free(config_file);
289                 config_file = make_message("%s/.paraslash/fade.conf", home);
290                 free(home);
291                 conf.config_file_arg = config_file;
292         }
293         return file_exists(conf.config_file_arg);
294 }
295
296 static void init_mixers(void)
297 {
298         int i;
299
300         FOR_EACH_MIXER(i) {
301                 struct mixer *m = &supported_mixer[i];
302                 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
303                         i, mixer_name[i]);
304                 m->init(m);
305         }
306 }
307
308 static int set_val(struct mixer *m, struct mixer_handle *h)
309 {
310         return m->set(h, conf.val_arg);
311 }
312
313 static struct mixer *get_mixer_or_die(void)
314 {
315         int i;
316
317         if (!conf.mixer_api_given)
318                 i = DEFAULT_MIXER;
319         else
320                 FOR_EACH_MIXER(i)
321                         if (!strcmp(mixer_name[i], conf.mixer_api_arg))
322                                 break;
323         if (i < NUM_SUPPORTED_MIXERS) {
324                 PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
325                 return supported_mixer + i;
326         }
327         printf("available mixer APIs: ");
328         FOR_EACH_MIXER(i) {
329                 int d = (i == DEFAULT_MIXER);
330                 printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
331         }
332         printf("\n");
333         exit(EXIT_FAILURE);
334 }
335
336 __noreturn static void print_help_and_die(void)
337 {
338         struct ggo_help h = DEFINE_GGO_HELP(fade);
339         bool d = conf.detailed_help_given;
340
341         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
342         exit(0);
343 }
344
345 int main(int argc, char *argv[])
346 {
347         int ret;
348         struct mixer *m;
349         struct mixer_handle *h = NULL;
350
351         fade_cmdline_parser(argc, argv, &conf);
352         loglevel = get_loglevel_by_name(conf.loglevel_arg);
353         version_handle_flag("fade", conf.version_given);
354         if (conf.help_given || conf.detailed_help_given)
355                 print_help_and_die();
356         ret = configfile_exists();
357         if (!ret && conf.config_file_given) {
358                 PARA_EMERG_LOG("can not read config file %s\n",
359                         conf.config_file_arg);
360                 exit(EXIT_FAILURE);
361         }
362         if (ret) {
363                 struct fade_cmdline_parser_params params = {
364                         .override = 0,
365                         .initialize = 0,
366                         .check_required = 0,
367                         .check_ambiguity = 0,
368                         .print_errors = 1
369                 };
370                 fade_cmdline_parser_config_file(conf.config_file_arg,
371                         &conf, &params);
372                 loglevel = get_loglevel_by_name(conf.loglevel_arg);
373         }
374         init_mixers();
375         m = get_mixer_or_die();
376         ret = m->open(conf.mixer_device_arg, &h);
377         if (ret < 0)
378                 goto out;
379         ret = set_channel(m, h, conf.mixer_channel_arg);
380         if (ret < 0)
381                 goto out;
382         switch (conf.mode_arg) {
383         case mode_arg_fade:
384                 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
385                 break;
386         case mode_arg_snooze:
387                 ret = snooze(m, h);
388                 break;
389         case mode_arg_set:
390                 ret = set_val(m, h);
391                 break;
392         default: /* sleep mode */
393                 ret = sweet_dreams(m, h);
394                 break;
395         }
396 out:
397         m->close(&h);
398         if (ret < 0)
399                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
400         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
401 }