ed543d192c7e9f852265061bc1b93dfe687f282d
[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_and_play(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 client_cmd("play");
144 }
145
146 static int set_initial_volume(struct mixer *m, struct mixer_handle *h)
147 {
148 int i, ret;
149
150 for (i = 0; i < conf.ivol_given; i++) {
151 char *p, *ch, *arg = para_strdup(conf.ivol_arg[i]);
152 int32_t iv;
153 p = strchr(arg, ':');
154 if (p) {
155 *p = '\0';
156 p++;
157 ch = arg;
158 } else {
159 p = arg;
160 ch = NULL;
161 }
162 ret = para_atoi32(p, &iv);
163 if (ret < 0) {
164 free(arg);
165 return ret;
166 }
167 ret = set_channel(m, h, ch);
168 if (!ch)
169 ch = "default";
170 if (ret < 0) {
171 PARA_WARNING_LOG("ignoring channel %s\n", ch);
172 ret = 0;
173 } else {
174 PARA_INFO_LOG("initial volume %s: %d\n", ch, iv);
175 ret = m->set(h, iv);
176 }
177 free(arg);
178 if (ret < 0)
179 return ret;
180 }
181 return 1;
182 }
183
184 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
185 {
186 time_t t1, wake_time_epoch;
187 unsigned int delay;
188 struct tm *tm;
189 int ret, min = conf.wake_min_arg;
190 char *fo_mood = conf.fo_mood_arg;
191 char *fi_mood = conf.fi_mood_arg;
192 char *sleep_mood = conf.sleep_mood_arg;
193 int fit = conf.fi_time_arg;
194 int fot = conf.fo_time_arg;
195 int fiv = conf.fi_vol_arg;
196 int fov = conf.fo_vol_arg;
197
198 /* calculate wake time */
199 time(&t1);
200 if (conf.wake_hour_given) {
201 int hour = conf.wake_hour_arg;
202 tm = localtime(&t1);
203 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
204 t1 += 86400; /* wake time is tomorrow */
205 tm = localtime(&t1);
206 }
207 tm->tm_hour = hour;
208 tm->tm_min = min;
209 tm->tm_sec = 0;
210 } else {
211 t1 += 9 * 60 * 60; /* nine hours from now */
212 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
213 tm = localtime(&t1);
214 }
215 wake_time_epoch = mktime(tm);
216 PARA_INFO_LOG("waketime: %s", asctime(tm));
217 client_cmd("stop");
218 sleep(1);
219 if (fot) {
220 ret = set_initial_volume(m, h);
221 if (ret < 0)
222 return ret;
223 change_afs_mode_and_play(fo_mood);
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_and_play(sleep_mood);
237 else
238 client_cmd("stop");
239 if (!fit)
240 return 1;
241 for (;;) {
242 time(&t1);
243 if (wake_time_epoch <= t1 + fit)
244 break;
245 delay = wake_time_epoch - t1 - fit;
246 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
247 delay, delay / 3600,
248 (delay % 3600) / 60);
249 sleep(delay);
250 }
251 change_afs_mode_and_play(fi_mood);
252 ret = fade(m, h, fiv, fit);
253 PARA_INFO_LOG("fade complete, returning\n");
254 return ret;
255 }
256
257 static int snooze(struct mixer *m, struct mixer_handle *h)
258 {
259 int ret, val;
260
261 if (conf.so_time_arg <= 0)
262 return 1;
263 ret = m->get(h);
264 if (ret < 0)
265 return ret;
266 val = ret;
267 if (val < conf.so_vol_arg)
268 ret = m->set(h, conf.so_vol_arg);
269 else
270 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
271 if (ret < 0)
272 return ret;
273 client_cmd("pause");
274 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
275 sleep(conf.snooze_time_arg);
276 client_cmd("play");
277 return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
278 }
279
280 static int configfile_exists(void)
281 {
282 static char *config_file;
283
284 if (!conf.config_file_given) {
285 char *home = para_homedir();
286 free(config_file);
287 config_file = make_message("%s/.paraslash/fade.conf", home);
288 free(home);
289 conf.config_file_arg = config_file;
290 }
291 return file_exists(conf.config_file_arg);
292 }
293
294 static void init_mixers(void)
295 {
296 int i;
297
298 FOR_EACH_MIXER(i) {
299 struct mixer *m = &supported_mixer[i];
300 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
301 i, mixer_name[i]);
302 m->init(m);
303 }
304 }
305
306 static int set_val(struct mixer *m, struct mixer_handle *h)
307 {
308 return m->set(h, conf.val_arg);
309 }
310
311 static struct mixer *get_mixer_or_die(void)
312 {
313 int i;
314
315 if (!conf.mixer_api_given)
316 i = DEFAULT_MIXER;
317 else
318 FOR_EACH_MIXER(i)
319 if (!strcmp(mixer_name[i], conf.mixer_api_arg))
320 break;
321 if (i < NUM_SUPPORTED_MIXERS) {
322 PARA_NOTICE_LOG("using %s mixer API\n", mixer_name[i]);
323 return supported_mixer + i;
324 }
325 printf("available mixer APIs: ");
326 FOR_EACH_MIXER(i) {
327 int d = (i == DEFAULT_MIXER);
328 printf("%s%s%s ", d? "[" : "", mixer_name[i], d? "]" : "");
329 }
330 printf("\n");
331 exit(EXIT_FAILURE);
332 }
333
334 __noreturn static void print_help_and_die(void)
335 {
336 struct ggo_help h = DEFINE_GGO_HELP(fade);
337 bool d = conf.detailed_help_given;
338
339 ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
340 exit(0);
341 }
342
343 int main(int argc, char *argv[])
344 {
345 int ret;
346 struct mixer *m;
347 struct mixer_handle *h = NULL;
348
349 fade_cmdline_parser(argc, argv, &conf);
350 loglevel = get_loglevel_by_name(conf.loglevel_arg);
351 version_handle_flag("fade", conf.version_given);
352 if (conf.help_given || conf.detailed_help_given)
353 print_help_and_die();
354 ret = configfile_exists();
355 if (!ret && conf.config_file_given) {
356 PARA_EMERG_LOG("can not read config file %s\n",
357 conf.config_file_arg);
358 exit(EXIT_FAILURE);
359 }
360 if (ret) {
361 struct fade_cmdline_parser_params params = {
362 .override = 0,
363 .initialize = 0,
364 .check_required = 0,
365 .check_ambiguity = 0,
366 .print_errors = 1
367 };
368 fade_cmdline_parser_config_file(conf.config_file_arg,
369 &conf, &params);
370 loglevel = get_loglevel_by_name(conf.loglevel_arg);
371 }
372 init_mixers();
373 m = get_mixer_or_die();
374 ret = m->open(conf.mixer_device_arg, &h);
375 if (ret < 0)
376 goto out;
377 ret = set_channel(m, h, conf.mixer_channel_arg);
378 if (ret < 0)
379 goto out;
380 switch (conf.mode_arg) {
381 case mode_arg_fade:
382 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
383 break;
384 case mode_arg_snooze:
385 ret = snooze(m, h);
386 break;
387 case mode_arg_set:
388 ret = set_val(m, h);
389 break;
390 default: /* sleep mode */
391 ret = sweet_dreams(m, h);
392 break;
393 }
394 out:
395 m->close(&h);
396 if (ret < 0)
397 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
398 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
399 }