alsa: New writer option: --buffer-time.
[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         printf("%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 /* Fade to new volume in fade_time seconds. */
48 static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
49 {
50         int vol, diff, incr, ret;
51         unsigned secs;
52         struct timespec ts;
53         unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
54
55         if (fade_time <= 0)
56                 return m->set(h, new_vol);
57         secs = fade_time;
58         PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
59         ret = m->get(h);
60         if (ret < 0)
61                 goto out;
62         vol = ret;
63         diff = new_vol - vol;
64         if (!diff) {
65                 sleep(secs);
66                 goto out;
67         }
68         incr = diff > 0? 1: -1;
69         diff = diff > 0? diff: -diff;
70         tmp = secs * 1000 / diff;
71         tmp2 = tmp % 1000;
72         while ((new_vol - vol) * incr > 0) {
73                 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
74                 ts.tv_sec = tmp / 1000; /* really nec ?*/
75                 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
76                 vol += incr;
77                 ret = m->set(h, vol);
78                 if (ret < 0)
79                         goto out;
80                 //printf("vol = %i\n", vol);
81                 nanosleep(&ts, NULL);
82         }
83 out:
84         return ret;
85 }
86
87 static void client_cmd(const char *cmd)
88 {
89         int ret, status, fds[3] = {0, 0, 0};
90         pid_t pid;
91         char *cmdline = make_message(BINDIR "/para_client %s", cmd);
92
93         PARA_NOTICE_LOG("%s\n", cmdline);
94         ret = para_exec_cmdline_pid(&pid, cmdline, fds);
95         free(cmdline);
96         if (ret < 0) {
97                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
98                 goto fail;
99         }
100         do
101                 pid = waitpid(pid, &status, 0);
102         while (pid == -1 && errno == EINTR);
103         if (pid < 0) {
104                 PARA_ERROR_LOG("%s\n", strerror(errno));
105                 goto fail;
106         }
107         if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
108                 goto fail;
109         return;
110 fail:
111         PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
112         exit(EXIT_FAILURE);
113 }
114
115 static void change_afs_mode_and_play(char *afs_mode)
116 {
117         char *cmd;
118
119         client_cmd("stop");
120         if (!afs_mode)
121                 return;
122         cmd = make_message("select %s", afs_mode);
123         client_cmd(cmd);
124         free(cmd);
125         client_cmd("play");
126 }
127
128 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
129 {
130         time_t t1, wake_time_epoch;
131         unsigned int delay;
132         struct tm *tm;
133         int ret, min = conf.wake_min_arg;
134         char *fo_mood = conf.fo_mood_arg;
135         char *fi_mood = conf.fi_mood_arg;
136         char *sleep_mood = conf.sleep_mood_arg;
137         int fit = conf.fi_time_arg;
138         int fot = conf.fo_time_arg;
139         int fiv = conf.fi_vol_arg;
140         int fov = conf.fo_vol_arg;
141         int iv = conf.ivol_arg;
142
143         /* calculate wake time */
144         time(&t1);
145         if (conf.wake_hour_given) {
146                 int hour = conf.wake_hour_arg;
147                 tm = localtime(&t1);
148                 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
149                         t1 += 86400; /* wake time is tomorrow */
150                         tm = localtime(&t1);
151                 }
152                 tm->tm_hour = hour;
153                 tm->tm_min = min;
154                 tm->tm_sec = 0;
155         } else {
156                 t1 += 9 * 60 * 60; /* nine hours from now */
157                 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
158                 tm = localtime(&t1);
159         }
160         wake_time_epoch = mktime(tm);
161         PARA_INFO_LOG("waketime: %s", asctime(tm));
162         client_cmd("stop");
163         sleep(1);
164         if (fot) {
165                 PARA_INFO_LOG("initial volume: %d\n", iv);
166                 ret = m->set(h, iv);
167                 if (ret < 0)
168                         return ret;
169                 change_afs_mode_and_play(fo_mood);
170                 ret = fade(m, h, fov, fot);
171                 if (ret < 0)
172                         return ret;
173         } else {
174                 ret = m->set(h, fov);
175                 if (ret < 0)
176                         return ret;
177         }
178         if (conf.sleep_mood_given)
179                 change_afs_mode_and_play(sleep_mood);
180         else
181                 client_cmd("stop");
182         if (!fit)
183                 return 1;
184         for (;;) {
185                 time(&t1);
186                 if (wake_time_epoch <= t1 + fit)
187                         break;
188                 delay = wake_time_epoch - t1 - fit;
189                 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
190                         delay, delay / 3600,
191                         (delay % 3600) / 60);
192                 sleep(delay);
193         }
194         change_afs_mode_and_play(fi_mood);
195         ret = fade(m, h, fiv, fit);
196         PARA_INFO_LOG("fade complete, returning\n");
197         return ret;
198 }
199
200 static int snooze(struct mixer *m, struct mixer_handle *h)
201 {
202         int ret, val;
203
204         if (conf.so_time_arg <= 0)
205                 return 1;
206         ret = m->get(h);
207         if (ret < 0)
208                 return ret;
209         val = ret;
210         if (val < conf.so_vol_arg)
211                 ret = m->set(h, conf.so_vol_arg);
212         else
213                 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
214         if (ret < 0)
215                 return ret;
216         client_cmd("pause");
217         PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
218         sleep(conf.snooze_time_arg);
219         client_cmd("play");
220         return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
221 }
222
223 static int configfile_exists(void)
224 {
225         static char *config_file;
226
227         if (!conf.config_file_given) {
228                 char *home = para_homedir();
229                 free(config_file);
230                 config_file = make_message("%s/.paraslash/fade.conf", home);
231                 free(home);
232                 conf.config_file_arg = config_file;
233         }
234         return file_exists(conf.config_file_arg);
235 }
236
237 static void init_mixers(void)
238 {
239         int i;
240
241         FOR_EACH_MIXER(i) {
242                 struct mixer *m = &supported_mixer[i];
243                 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
244                         i, mixer_name[i]);
245                 m->init(m);
246         }
247 }
248
249 static int set_channel(struct mixer *m, struct mixer_handle *h)
250 {
251         char *channels;
252         int ret;
253
254         ret = m->set_channel(h, conf.mixer_channel_arg);
255         if (ret >= 0) {
256                 PARA_NOTICE_LOG("using %s mixer channel\n",
257                         conf.mixer_channel_arg?  conf.mixer_channel_arg
258                                 : "default");
259                 return ret;
260         }
261         channels = m->get_channels(h);
262         printf("Available channels: %s\n", channels);
263         free(channels);
264         return ret;
265 }
266
267 static struct mixer *get_mixer_or_die(void)
268 {
269         int i;
270
271         if (!conf.mixer_api_given)
272                 i = DEFAULT_MIXER;
273         else
274                 FOR_EACH_MIXER(i)
275                         if (!strcmp(mixer_name[i], conf.mixer_api_arg))
276                                 break;
277         if (i < NUM_SUPPORTED_MIXERS) {
278                 PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
279                 return supported_mixer + i;
280         }
281         printf("available mixer APIs: ");
282         FOR_EACH_MIXER(i) {
283                 int d = (i == DEFAULT_MIXER);
284                 printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
285         }
286         printf("\n");
287         exit(EXIT_FAILURE);
288 }
289
290 __noreturn static void print_help_and_die(void)
291 {
292         struct ggo_help h = DEFINE_GGO_HELP(fade);
293         bool d = conf.detailed_help_given;
294
295         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
296         exit(0);
297 }
298
299 int main(int argc, char *argv[])
300 {
301         int ret;
302         struct mixer *m;
303         struct mixer_handle *h = NULL;
304
305         fade_cmdline_parser(argc, argv, &conf);
306         loglevel = get_loglevel_by_name(conf.loglevel_arg);
307         version_handle_flag("fade", conf.version_given);
308         if (conf.help_given || conf.detailed_help_given)
309                 print_help_and_die();
310         ret = configfile_exists();
311         if (!ret && conf.config_file_given) {
312                 PARA_EMERG_LOG("can not read config file %s\n",
313                         conf.config_file_arg);
314                 exit(EXIT_FAILURE);
315         }
316         if (ret) {
317                 struct fade_cmdline_parser_params params = {
318                         .override = 0,
319                         .initialize = 0,
320                         .check_required = 0,
321                         .check_ambiguity = 0,
322                         .print_errors = 1
323                 };
324                 fade_cmdline_parser_config_file(conf.config_file_arg,
325                         &conf, &params);
326                 loglevel = get_loglevel_by_name(conf.loglevel_arg);
327         }
328         init_mixers();
329         m = get_mixer_or_die();
330         ret = m->open(conf.mixer_device_arg, &h);
331         if (ret < 0)
332                 goto out;
333         ret = set_channel(m, h);
334         if (ret < 0)
335                 goto out;
336         switch (conf.mode_arg) {
337         case mode_arg_fade:
338                 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
339                 break;
340         case mode_arg_snooze:
341                 ret = snooze(m, h);
342                 break;
343         default: /* sleep mode */
344                 ret = sweet_dreams(m, h);
345                 break;
346         }
347 out:
348         m->close(&h);
349         if (ret < 0)
350                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
351         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
352 }