Merge branch 'master' into next
[paraslash.git] / fade.c
1 /*
2  * Copyright (C) 1998-2009 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 linux. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11 #include <dirent.h>
12 #include <sys/ioctl.h>
13 #include <ctype.h>
14 #include <stdlib.h> /* EXIT_SUCCESS */
15 #include <unistd.h>
16 #include <signal.h>
17 #include <string.h>
18 #include <limits.h>
19 #include <sys/soundcard.h>
20
21 #include "fade.cmdline.h"
22 #include "para.h"
23 #include "fd.h"
24 #include "string.h"
25 #include "error.h"
26
27 INIT_FADE_ERRLISTS;
28 struct fade_args_info conf;
29
30 __printf_2_3 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 para_open(conf.mixer_device_arg, O_RDWR, 42);
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 -ERRNO_TO_PARA_ERROR(errno);
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 mixer_fd;
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 -ERRNO_TO_PARA_ERROR(errno);
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 int fade(int new_vol, int fade_time)
113 {
114         int vol, mixer_fd, diff, incr, ret;
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 1;
121         secs = fade_time;
122         PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
123         ret = open_mixer();
124         if (ret < 0)
125                 return ret;
126         mixer_fd = ret;
127         ret = do_get_vol(mixer_fd);
128         if (ret < 0)
129                 goto out;
130         vol = ret;
131         diff = new_vol - vol;
132         if (!diff) {
133                 sleep(secs);
134                 goto out;
135         }
136         incr = diff > 0? 1: -1;
137         diff = diff > 0? diff: -diff;
138         tmp = secs * 1000 / diff;
139         tmp2 = tmp % 1000;
140         while ((new_vol - vol) * incr > 0) {
141                 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
142                 ts.tv_sec = tmp / 1000; /* really nec ?*/
143                 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
144                 vol += incr;
145                 ret = do_set_vol(mixer_fd, vol);
146                 if (ret < 0)
147                         goto out;
148                 //printf("vol = %i\n", vol);
149                 nanosleep(&ts, NULL);
150         }
151 out:
152         close(mixer_fd);
153         return ret;
154 }
155
156 static void client_cmd(const char *cmd)
157 {
158         int ret, fds[3] = {0, 0, 0};
159         pid_t pid;
160         char *cmdline = make_message(BINDIR "/para_client %s", cmd);
161
162         PARA_INFO_LOG("%s\n", cmdline);
163         ret = para_exec_cmdline_pid(&pid, cmdline, fds);
164         free(cmdline);
165         if (ret < 0) {
166                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
167                 exit(EXIT_FAILURE);
168         }
169         do
170                 ret = wait(NULL);
171         while (ret != -1 && errno != ECHILD);
172 }
173
174 static void change_afs_mode_and_play(char *afs_mode)
175 {
176         char *cmd;
177
178         client_cmd("stop");
179         if (!afs_mode)
180                 return;
181         cmd = make_message("select %s\n", afs_mode);
182         client_cmd(cmd);
183         free(cmd);
184         client_cmd("play");
185 }
186
187 /*
188  * sleep
189  */
190 static int sweet_dreams(void)
191 {
192         time_t t1, wake_time_epoch;
193         unsigned int delay;
194         struct tm *tm;
195         int ret, min = conf.wake_min_arg;
196         char *fa_mode = conf.fa_mode_arg;
197         char *wake_mode = conf.wake_mode_arg;
198         char *sleep_mode = conf.sleep_mode_arg;
199         int wf = conf.wake_fade_arg;
200         int sf = conf.fa_fade_arg;
201         int wv = conf.wake_vol_arg;
202         int sv = conf.fa_vol_arg;
203         int iv = conf.sleep_ivol_arg;
204
205         /* calculate wake time */
206         time(&t1);
207         if (conf.wake_hour_given) {
208                 int hour = conf.wake_hour_arg;
209                 tm = localtime(&t1);
210                 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
211                         t1 += 86400; /* wake time is tomorrow */
212                         tm = localtime(&t1);
213                 }
214                 tm->tm_hour = hour;
215                 tm->tm_min = min;
216                 tm->tm_sec = 0;
217         } else {
218                 t1 += 9 * 60 * 60; /* nine hours from now */
219                 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
220                 tm = localtime(&t1);
221         }
222         wake_time_epoch = mktime(tm);
223         PARA_INFO_LOG("waketime: %s", asctime(tm));
224         client_cmd("stop");
225         sleep(1);
226         if (sf) {
227                 PARA_INFO_LOG("initial volume: %d\n", iv);
228                 ret = set_vol(iv);
229                 if (ret < 0)
230                         return ret;
231                 change_afs_mode_and_play(fa_mode);
232                 ret = fade(sv, sf);
233                 if (ret < 0)
234                         return ret;
235         } else {
236                 ret = set_vol(sf);
237                 if (ret < 0)
238                         return ret;
239         }
240         if (conf.sleep_mode_given)
241                 change_afs_mode_and_play(sleep_mode);
242         else
243                 client_cmd("stop");
244         if (!wf)
245                 return 1;
246         for (;;) {
247                 time(&t1);
248                 if (wake_time_epoch <= t1 + wf)
249                         break;
250                 delay = wake_time_epoch - t1 - wf;
251                 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
252                         delay, delay / 3600,
253                         (delay % 3600) / 60);
254                 sleep(delay);
255         }
256         change_afs_mode_and_play(wake_mode);
257         ret = fade(wv, wf);
258         PARA_INFO_LOG("fade complete, returning\n");
259         return ret;
260 }
261
262 static int snooze(void)
263 {
264         int ret;
265         unsigned sleep_time;
266
267         if (conf.snooze_time_arg <= 0)
268                 return 1;
269         sleep_time = conf.snooze_time_arg;
270         if (get_vol() < conf.snooze_out_vol_arg)
271                 ret = set_vol(conf.snooze_out_vol_arg);
272         else
273                 ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
274         if (ret < 0)
275                 return ret;
276         client_cmd("pause");
277         PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
278         sleep(sleep_time);
279         client_cmd("play");
280         return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
281 }
282
283 static int configfile_exists(void)
284 {
285         static char *config_file;
286
287         if (!conf.config_file_given) {
288                 char *home = para_homedir();
289                 free(config_file);
290                 config_file = make_message("%s/.paraslash/fade.conf", home);
291                 free(home);
292                 conf.config_file_arg = config_file;
293         }
294         return file_exists(conf.config_file_arg);
295 }
296
297 int main(int argc, char *argv[])
298 {
299         int ret;
300
301         if (fade_cmdline_parser(argc, argv, &conf))
302                 exit(EXIT_FAILURE);
303         HANDLE_VERSION_FLAG("fade", conf);
304         ret = configfile_exists();
305         if (!ret && conf.config_file_given) {
306                 PARA_EMERG_LOG("can not read config file %s\n",
307                         conf.config_file_arg);
308                 exit(EXIT_FAILURE);
309         }
310         if (ret) {
311                 struct fade_cmdline_parser_params params = {
312                         .override = 0,
313                         .initialize = 0,
314                         .check_required = 0,
315                         .check_ambiguity = 0
316                 };
317                 fade_cmdline_parser_config_file(conf.config_file_arg,
318                         &conf, &params);
319         }
320         if (!strcmp(conf.mode_arg, "sleep")) {
321                 ret = sweet_dreams();
322                 goto out;
323         }
324         if (!strcmp(conf.mode_arg, "fade")) {
325                 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
326                 goto out;
327         }
328         if (!strcmp(conf.mode_arg, "snooze")) {
329                 ret = snooze();
330                 goto out;
331         }
332         ret = -E_FADE_SYNTAX;
333 out:
334         if (ret < 0)
335                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
336         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
337 }