Add some more derived files to .gitignore.
[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 <sys/types.h>
10 #include <dirent.h>
11
12 #include "fade.cmdline.h"
13 #include "para.h"
14 #include "fd.h"
15
16 #include <stropts.h>
17 #include <ctype.h>
18 #include <stdlib.h> /* EXIT_SUCCESS */
19 #include <unistd.h>
20 #include <signal.h>
21 #include <string.h>
22 #include <limits.h>
23 #include <linux/soundcard.h>
24 #include "string.h"
25 #include "error.h"
26
27
28 INIT_FADE_ERRLISTS;
29 struct fade_args_info conf;
30
31 __printf_2_3 void para_log(__a_unused int ll, const char *fmt, ...)
32 {
33         va_list argp;
34         time_t t1;
35         struct tm *tm;
36
37         time(&t1);
38         tm = localtime(&t1);
39         printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
40         va_start(argp, fmt);
41         vprintf(fmt, argp);
42         va_end(argp);
43 }
44
45 /*
46  * open mixer device
47  */
48 static int open_mixer(void)
49 {
50         return para_open(conf.mixer_device_arg, O_RDWR, 42);
51 }
52
53 /*
54  * get volume via mixer_fd
55  */
56 static int do_get_vol(int mixer_fd)
57 {
58         int volume;
59
60         if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
61                 return -ERRNO_TO_PARA_ERROR(errno);
62         /* take the mean value of left and right volume */
63         return (volume % 256 + (volume >> 8)) / 2;
64 }
65
66 /*
67  * open mixer, get volume and close mixer
68  */
69 static int get_vol(void)
70 {
71         int mixer_fd;
72         int volume;
73
74         mixer_fd = open_mixer();
75         if (mixer_fd < 0)
76                 return mixer_fd;
77         volume = do_get_vol(mixer_fd);
78         close(mixer_fd);
79         return volume;
80 }
81
82 /*
83  * set volume via mixer_fd
84  */
85 static int do_set_vol(int mixer_fd, int volume)
86 {
87         int tmp = (volume << 8) + volume;
88
89         if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
90                 return -ERRNO_TO_PARA_ERROR(errno);
91         return 1;
92 }
93
94 /*
95  * open mixer, set volume and close mixer
96  */
97 static int set_vol(int volume)
98 {
99         int mixer_fd, ret = open_mixer();
100
101         if (ret < 0)
102                 return ret;
103         mixer_fd = ret;
104         ret = do_set_vol(mixer_fd, volume);
105         close(mixer_fd);
106         return ret;
107 }
108
109 /*
110  * Open mixer, get volume, fade to new_vol in secs seconds and
111  * close mixer
112  */
113 static int fade(int new_vol, int fade_time)
114 {
115         int vol, mixer_fd, diff, incr, ret;
116         unsigned secs;
117         struct timespec ts;
118         unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
119
120         if (fade_time <= 0)
121                 return 1;
122         secs = fade_time;
123         PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
124         ret = open_mixer();
125         if (ret < 0)
126                 return ret;
127         mixer_fd = ret;
128         ret = do_get_vol(mixer_fd);
129         if (ret < 0)
130                 goto out;
131         vol = ret;
132         diff = new_vol - vol;
133         if (!diff) {
134                 sleep(secs);
135                 goto out;
136         }
137         incr = diff > 0? 1: -1;
138         diff = diff > 0? diff: -diff;
139         tmp = secs * 1000 / diff;
140         tmp2 = tmp % 1000;
141         while ((new_vol - vol) * incr > 0) {
142                 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
143                 ts.tv_sec = tmp / 1000; /* really nec ?*/
144                 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
145                 vol += incr;
146                 ret = do_set_vol(mixer_fd, vol);
147                 if (ret < 0)
148                         goto out;
149                 //printf("vol = %i\n", vol);
150                 nanosleep(&ts, NULL);
151         }
152 out:
153         close(mixer_fd);
154         return ret;
155 }
156
157 static void client_cmd(const char *cmd)
158 {
159         int ret, fds[3] = {0, 0, 0};
160         pid_t pid;
161         char *cmdline = make_message(BINDIR "/para_client %s", cmd);
162
163         PARA_INFO_LOG("%s\n", cmdline);
164         ret = para_exec_cmdline_pid(&pid, cmdline, fds);
165         free(cmdline);
166         if (ret < 0) {
167                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
168                 exit(EXIT_FAILURE);
169         }
170         do
171                 ret = wait(NULL);
172         while (ret != -1 && errno != ECHILD);
173 }
174
175 static void change_afs_mode_and_play(char *afs_mode)
176 {
177         char *cmd;
178
179         client_cmd("stop");
180         if (!afs_mode)
181                 return;
182         cmd = make_message("select %s\n", afs_mode);
183         client_cmd(cmd);
184         free(cmd);
185         client_cmd("play");
186 }
187
188 /*
189  * sleep
190  */
191 static int sweet_dreams(void)
192 {
193         time_t t1, wake_time_epoch;
194         unsigned int delay;
195         struct tm *tm;
196         int ret, min = conf.wake_min_arg;
197         char *fa_mode = conf.fa_mode_arg;
198         char *wake_mode = conf.wake_mode_arg;
199         char *sleep_mode = conf.sleep_mode_arg;
200         int wf = conf.wake_fade_arg;
201         int sf = conf.fa_fade_arg;
202         int wv = conf.wake_vol_arg;
203         int sv = conf.fa_vol_arg;
204         int iv = conf.sleep_ivol_arg;
205
206         /* calculate wake time */
207         time(&t1);
208         if (conf.wake_hour_given) {
209                 int hour = conf.wake_hour_arg;
210                 tm = localtime(&t1);
211                 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
212                         t1 += 86400; /* wake time is tomorrow */
213                         tm = localtime(&t1);
214                 }
215                 tm->tm_hour = hour;
216                 tm->tm_min = min;
217                 tm->tm_sec = 0;
218         } else {
219                 t1 += 9 * 60 * 60; /* nine hours from now */
220                 PARA_INFO_LOG("default wake time: %lu\n", t1);
221                 tm = localtime(&t1);
222         }
223         wake_time_epoch = mktime(tm);
224         PARA_INFO_LOG("waketime: %s", asctime(tm));
225         client_cmd("stop");
226         sleep(1);
227         if (sf) {
228                 PARA_INFO_LOG("initial volume: %d\n", iv);
229                 ret = set_vol(iv);
230                 if (ret < 0)
231                         return ret;
232                 change_afs_mode_and_play(fa_mode);
233                 ret = fade(sv, sf);
234                 if (ret < 0)
235                         return ret;
236         } else {
237                 ret = set_vol(sf);
238                 if (ret < 0)
239                         return ret;
240         }
241         if (conf.sleep_mode_given)
242                 change_afs_mode_and_play(sleep_mode);
243         else
244                 client_cmd("stop");
245         if (!wf)
246                 return 1;
247         for (;;) {
248                 time(&t1);
249                 if (wake_time_epoch <= t1 + wf)
250                         break;
251                 delay = wake_time_epoch - t1 - wf;
252                 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
253                         delay, delay / 3600,
254                         (delay % 3600) / 60);
255                 sleep(delay);
256         }
257         change_afs_mode_and_play(wake_mode);
258         ret = fade(wv, wf);
259         PARA_INFO_LOG("fade complete, returning\n");
260         return ret;
261 }
262
263 static int snooze(void)
264 {
265         int ret;
266         unsigned sleep_time;
267
268         if (conf.snooze_time_arg <= 0)
269                 return 1;
270         sleep_time = conf.snooze_time_arg;
271         if (get_vol() < conf.snooze_out_vol_arg)
272                 ret = set_vol(conf.snooze_out_vol_arg);
273         else
274                 ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
275         if (ret < 0)
276                 return ret;
277         client_cmd("pause");
278         PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
279         sleep(sleep_time);
280         client_cmd("play");
281         return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
282 }
283
284 static int configfile_exists(void)
285 {
286         static char *config_file;
287
288         if (!conf.config_file_given) {
289                 char *home = para_homedir();
290                 free(config_file);
291                 config_file = make_message("%s/.paraslash/fade.conf", home);
292                 free(home);
293                 conf.config_file_arg = config_file;
294         }
295         return file_exists(conf.config_file_arg);
296 }
297
298 int main(int argc, char *argv[])
299 {
300         int ret;
301
302         if (fade_cmdline_parser(argc, argv, &conf))
303                 exit(EXIT_FAILURE);
304         HANDLE_VERSION_FLAG("fade", conf);
305         ret = configfile_exists();
306         if (!ret && conf.config_file_given) {
307                 PARA_EMERG_LOG("can not read config file %s\n",
308                         conf.config_file_arg);
309                 exit(EXIT_FAILURE);
310         }
311         if (ret) {
312                 struct fade_cmdline_parser_params params = {
313                         .override = 0,
314                         .initialize = 0,
315                         .check_required = 0,
316                         .check_ambiguity = 0
317                 };
318                 fade_cmdline_parser_config_file(conf.config_file_arg,
319                         &conf, &params);
320         }
321         if (!strcmp(conf.mode_arg, "sleep")) {
322                 ret = sweet_dreams();
323                 goto out;
324         }
325         if (!strcmp(conf.mode_arg, "fade")) {
326                 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
327                 goto out;
328         }
329         if (!strcmp(conf.mode_arg, "snooze")) {
330                 ret = snooze();
331                 goto out;
332         }
333         ret = -E_FADE_SYNTAX;
334 out:
335         if (ret < 0)
336                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
337         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
338 }