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