fade: Fix sleep time in snooze mode.
[paraslash.git] / fade.c
1 /*
2 * Copyright (C) 1998-2012 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 #include <sys/types.h>
11 #include <sys/ioctl.h>
12 #include <ctype.h>
13 #include <stdlib.h> /* EXIT_SUCCESS */
14 #include <unistd.h>
15 #include <signal.h>
16 #include <string.h>
17 #include <limits.h>
18 #include <sys/soundcard.h>
19
20 #include "fade.cmdline.h"
21 #include "para.h"
22 #include "fd.h"
23 #include "string.h"
24 #include "error.h"
25 #include "version.h"
26
27 INIT_FADE_ERRLISTS;
28 static struct fade_args_info conf;
29
30 __printf_2_3 void date_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 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
44
45 /*
46 * Open the mixer device.
47 */
48 static int open_mixer(void)
49 {
50 PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg);
51 return para_open(conf.mixer_device_arg, O_RDWR, 42);
52 }
53
54 /*
55 * Get channel volume via mixer_fd.
56 */
57 static int get_mixer_channel(int mixer_fd)
58 {
59 int volume;
60
61 if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &volume) < 0)
62 return -ERRNO_TO_PARA_ERROR(errno);
63 /* take the mean value of left and right volume */
64 return (volume % 256 + (volume >> 8)) / 2;
65 }
66
67 /*
68 * Open mixer, get volume and close mixer.
69 */
70 static int open_and_get_mixer_channel(void)
71 {
72 int mixer_fd;
73 int volume;
74
75 mixer_fd = open_mixer();
76 if (mixer_fd < 0)
77 return mixer_fd;
78 volume = get_mixer_channel(mixer_fd);
79 close(mixer_fd);
80 return volume;
81 }
82
83 /*
84 * Set channel volume via mixer_fd.
85 */
86 static int set_mixer_channel(int mixer_fd, int volume)
87 {
88 int tmp = (volume << 8) + volume;
89
90 if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &tmp) < 0)
91 return -ERRNO_TO_PARA_ERROR(errno);
92 return 1;
93 }
94
95 /*
96 * Open mixer, set volume and close mixer.
97 */
98 static int open_and_set_mixer_channel(int volume)
99 {
100 int mixer_fd, ret = open_mixer();
101
102 if (ret < 0)
103 return ret;
104 mixer_fd = ret;
105 ret = set_mixer_channel(mixer_fd, volume);
106 close(mixer_fd);
107 return ret;
108 }
109
110 static void fixup_mixer_channel_arg(void)
111 {
112 int val = SOUND_MIXER_VOLUME; /* STFU, gcc */
113
114 switch (conf.mixer_channel_arg) {
115 case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
116 case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
117 case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
118 case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
119 case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
120 case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
121 case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
122 case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
123 case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
124 case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
125 case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
126 case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
127 case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
128 case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
129 default: break;
130 }
131 conf.mixer_channel_arg = val;
132 }
133
134 /*
135 * Open mixer, get volume, fade to new_vol in secs seconds and
136 * close mixer.
137 */
138 static int fade(int new_vol, int fade_time)
139 {
140 int vol, mixer_fd, diff, incr, ret;
141 unsigned secs;
142 struct timespec ts;
143 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
144
145 if (fade_time <= 0)
146 return 1;
147 secs = fade_time;
148 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
149 ret = open_mixer();
150 if (ret < 0)
151 return ret;
152 mixer_fd = ret;
153 ret = get_mixer_channel(mixer_fd);
154 if (ret < 0)
155 goto out;
156 vol = ret;
157 diff = new_vol - vol;
158 if (!diff) {
159 sleep(secs);
160 goto out;
161 }
162 incr = diff > 0? 1: -1;
163 diff = diff > 0? diff: -diff;
164 tmp = secs * 1000 / diff;
165 tmp2 = tmp % 1000;
166 while ((new_vol - vol) * incr > 0) {
167 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
168 ts.tv_sec = tmp / 1000; /* really nec ?*/
169 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
170 vol += incr;
171 ret = set_mixer_channel(mixer_fd, vol);
172 if (ret < 0)
173 goto out;
174 //printf("vol = %i\n", vol);
175 nanosleep(&ts, NULL);
176 }
177 out:
178 close(mixer_fd);
179 return ret;
180 }
181
182 static void client_cmd(const char *cmd)
183 {
184 int ret, fds[3] = {0, 0, 0};
185 pid_t pid;
186 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
187
188 PARA_INFO_LOG("%s\n", cmdline);
189 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
190 free(cmdline);
191 if (ret < 0) {
192 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
193 exit(EXIT_FAILURE);
194 }
195 do
196 ret = wait(NULL);
197 while (ret != -1 && errno != ECHILD);
198 }
199
200 static void change_afs_mode_and_play(char *afs_mode)
201 {
202 char *cmd;
203
204 client_cmd("stop");
205 if (!afs_mode)
206 return;
207 cmd = make_message("select %s\n", afs_mode);
208 client_cmd(cmd);
209 free(cmd);
210 client_cmd("play");
211 }
212
213 /*
214 * sleep
215 */
216 static int sweet_dreams(void)
217 {
218 time_t t1, wake_time_epoch;
219 unsigned int delay;
220 struct tm *tm;
221 int ret, min = conf.wake_min_arg;
222 char *fo_mood = conf.fo_mood_arg;
223 char *fi_mood = conf.fi_mood_arg;
224 char *sleep_mood = conf.sleep_mood_arg;
225 int fit = conf.fi_time_arg;
226 int fot = conf.fo_time_arg;
227 int fiv = conf.fi_vol_arg;
228 int fov = conf.fo_vol_arg;
229 int iv = conf.ivol_arg;
230
231 /* calculate wake time */
232 time(&t1);
233 if (conf.wake_hour_given) {
234 int hour = conf.wake_hour_arg;
235 tm = localtime(&t1);
236 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
237 t1 += 86400; /* wake time is tomorrow */
238 tm = localtime(&t1);
239 }
240 tm->tm_hour = hour;
241 tm->tm_min = min;
242 tm->tm_sec = 0;
243 } else {
244 t1 += 9 * 60 * 60; /* nine hours from now */
245 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
246 tm = localtime(&t1);
247 }
248 wake_time_epoch = mktime(tm);
249 PARA_INFO_LOG("waketime: %s", asctime(tm));
250 client_cmd("stop");
251 sleep(1);
252 if (fot) {
253 PARA_INFO_LOG("initial volume: %d\n", iv);
254 ret = open_and_set_mixer_channel(iv);
255 if (ret < 0)
256 return ret;
257 change_afs_mode_and_play(fo_mood);
258 ret = fade(fov, fot);
259 if (ret < 0)
260 return ret;
261 } else {
262 ret = open_and_set_mixer_channel(fov);
263 if (ret < 0)
264 return ret;
265 }
266 if (conf.sleep_mood_given)
267 change_afs_mode_and_play(sleep_mood);
268 else
269 client_cmd("stop");
270 if (!fit)
271 return 1;
272 for (;;) {
273 time(&t1);
274 if (wake_time_epoch <= t1 + fit)
275 break;
276 delay = wake_time_epoch - t1 - fit;
277 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
278 delay, delay / 3600,
279 (delay % 3600) / 60);
280 sleep(delay);
281 }
282 change_afs_mode_and_play(fi_mood);
283 ret = fade(fiv, fit);
284 PARA_INFO_LOG("fade complete, returning\n");
285 return ret;
286 }
287
288 static int snooze(void)
289 {
290 int ret;
291
292 if (conf.so_time_arg <= 0)
293 return 1;
294 if (open_and_get_mixer_channel() < conf.so_vol_arg)
295 ret = open_and_set_mixer_channel(conf.so_vol_arg);
296 else
297 ret = fade(conf.so_vol_arg, conf.so_time_arg);
298 if (ret < 0)
299 return ret;
300 client_cmd("pause");
301 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
302 sleep(conf.snooze_time_arg);
303 client_cmd("play");
304 return fade(conf.si_vol_arg, conf.si_time_arg);
305 }
306
307 static int configfile_exists(void)
308 {
309 static char *config_file;
310
311 if (!conf.config_file_given) {
312 char *home = para_homedir();
313 free(config_file);
314 config_file = make_message("%s/.paraslash/fade.conf", home);
315 free(home);
316 conf.config_file_arg = config_file;
317 }
318 return file_exists(conf.config_file_arg);
319 }
320
321 int main(int argc, char *argv[])
322 {
323 int ret;
324
325 if (fade_cmdline_parser(argc, argv, &conf))
326 exit(EXIT_FAILURE);
327 HANDLE_VERSION_FLAG("fade", conf);
328 ret = configfile_exists();
329 if (!ret && conf.config_file_given) {
330 PARA_EMERG_LOG("can not read config file %s\n",
331 conf.config_file_arg);
332 exit(EXIT_FAILURE);
333 }
334 if (ret) {
335 struct fade_cmdline_parser_params params = {
336 .override = 0,
337 .initialize = 0,
338 .check_required = 0,
339 .check_ambiguity = 0,
340 .print_errors = 1
341 };
342 fade_cmdline_parser_config_file(conf.config_file_arg,
343 &conf, &params);
344 }
345 fixup_mixer_channel_arg();
346 switch (conf.mode_arg) {
347 case mode_arg_fade:
348 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
349 break;
350 case mode_arg_snooze:
351 ret = snooze();
352 break;
353 default: /* sleep mode */
354 ret = sweet_dreams();
355 break;
356 }
357 if (ret < 0)
358 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
359 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
360 }