8eeb79e7d3f335b08ed1d2b8f3606e2fdec4cd6d
[paraslash.git] / fade.c
1 /*
2 * Copyright (C) 1998-2011 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 <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 static 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 the mixer device.
46 */
47 static int open_mixer(void)
48 {
49 PARA_INFO_LOG("opening %s\n", conf.mixer_device_arg);
50 return para_open(conf.mixer_device_arg, O_RDWR, 42);
51 }
52
53 /*
54 * Get channel volume via mixer_fd.
55 */
56 static int get_mixer_channel(int mixer_fd)
57 {
58 int volume;
59
60 if (ioctl(mixer_fd, MIXER_READ(conf.mixer_channel_arg), &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 open_and_get_mixer_channel(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 = get_mixer_channel(mixer_fd);
78 close(mixer_fd);
79 return volume;
80 }
81
82 /*
83 * Set channel volume via mixer_fd.
84 */
85 static int set_mixer_channel(int mixer_fd, int volume)
86 {
87 int tmp = (volume << 8) + volume;
88
89 if (ioctl(mixer_fd, MIXER_WRITE(conf.mixer_channel_arg), &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 open_and_set_mixer_channel(int volume)
98 {
99 int mixer_fd, ret = open_mixer();
100
101 if (ret < 0)
102 return ret;
103 mixer_fd = ret;
104 ret = set_mixer_channel(mixer_fd, volume);
105 close(mixer_fd);
106 return ret;
107 }
108
109 static void fixup_mixer_channel_arg(void)
110 {
111 int val = SOUND_MIXER_VOLUME; /* STFU, gcc */
112
113 switch (conf.mixer_channel_arg) {
114 case mixer_channel_arg_volume: val = SOUND_MIXER_VOLUME; break;
115 case mixer_channel_arg_bass: val = SOUND_MIXER_BASS; break;
116 case mixer_channel_arg_treble: val = SOUND_MIXER_TREBLE; break;
117 case mixer_channel_arg_synth: val = SOUND_MIXER_SYNTH; break;
118 case mixer_channel_arg_pcm: val = SOUND_MIXER_PCM; break;
119 case mixer_channel_arg_speaker: val = SOUND_MIXER_SPEAKER; break;
120 case mixer_channel_arg_line: val = SOUND_MIXER_LINE; break;
121 case mixer_channel_arg_mic: val = SOUND_MIXER_MIC; break;
122 case mixer_channel_arg_cd: val = SOUND_MIXER_CD; break;
123 case mixer_channel_arg_imix: val = SOUND_MIXER_IMIX; break;
124 case mixer_channel_arg_altpcm: val = SOUND_MIXER_ALTPCM; break;
125 case mixer_channel_arg_reclev: val = SOUND_MIXER_RECLEV; break;
126 case mixer_channel_arg_igain: val = SOUND_MIXER_IGAIN; break;
127 case mixer_channel_arg_ogain: val = SOUND_MIXER_OGAIN; break;
128 }
129 conf.mixer_channel_arg = val;
130 }
131
132 /*
133 * Open mixer, get volume, fade to new_vol in secs seconds and
134 * close mixer.
135 */
136 static int fade(int new_vol, int fade_time)
137 {
138 int vol, mixer_fd, diff, incr, ret;
139 unsigned secs;
140 struct timespec ts;
141 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
142
143 if (fade_time <= 0)
144 return 1;
145 secs = fade_time;
146 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
147 ret = open_mixer();
148 if (ret < 0)
149 return ret;
150 mixer_fd = ret;
151 ret = get_mixer_channel(mixer_fd);
152 if (ret < 0)
153 goto out;
154 vol = ret;
155 diff = new_vol - vol;
156 if (!diff) {
157 sleep(secs);
158 goto out;
159 }
160 incr = diff > 0? 1: -1;
161 diff = diff > 0? diff: -diff;
162 tmp = secs * 1000 / diff;
163 tmp2 = tmp % 1000;
164 while ((new_vol - vol) * incr > 0) {
165 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
166 ts.tv_sec = tmp / 1000; /* really nec ?*/
167 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
168 vol += incr;
169 ret = set_mixer_channel(mixer_fd, vol);
170 if (ret < 0)
171 goto out;
172 //printf("vol = %i\n", vol);
173 nanosleep(&ts, NULL);
174 }
175 out:
176 close(mixer_fd);
177 return ret;
178 }
179
180 static void client_cmd(const char *cmd)
181 {
182 int ret, fds[3] = {0, 0, 0};
183 pid_t pid;
184 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
185
186 PARA_INFO_LOG("%s\n", cmdline);
187 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
188 free(cmdline);
189 if (ret < 0) {
190 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
191 exit(EXIT_FAILURE);
192 }
193 do
194 ret = wait(NULL);
195 while (ret != -1 && errno != ECHILD);
196 }
197
198 static void change_afs_mode_and_play(char *afs_mode)
199 {
200 char *cmd;
201
202 client_cmd("stop");
203 if (!afs_mode)
204 return;
205 cmd = make_message("select %s\n", afs_mode);
206 client_cmd(cmd);
207 free(cmd);
208 client_cmd("play");
209 }
210
211 /*
212 * sleep
213 */
214 static int sweet_dreams(void)
215 {
216 time_t t1, wake_time_epoch;
217 unsigned int delay;
218 struct tm *tm;
219 int ret, min = conf.wake_min_arg;
220 char *fo_mood = conf.fo_mood_arg;
221 char *fi_mood = conf.fi_mood_arg;
222 char *sleep_mood = conf.sleep_mood_arg;
223 int fit = conf.fi_time_arg;
224 int fot = conf.fo_time_arg;
225 int fiv = conf.fi_vol_arg;
226 int fov = conf.fo_vol_arg;
227 int iv = conf.ivol_arg;
228
229 /* calculate wake time */
230 time(&t1);
231 if (conf.wake_hour_given) {
232 int hour = conf.wake_hour_arg;
233 tm = localtime(&t1);
234 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
235 t1 += 86400; /* wake time is tomorrow */
236 tm = localtime(&t1);
237 }
238 tm->tm_hour = hour;
239 tm->tm_min = min;
240 tm->tm_sec = 0;
241 } else {
242 t1 += 9 * 60 * 60; /* nine hours from now */
243 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
244 tm = localtime(&t1);
245 }
246 wake_time_epoch = mktime(tm);
247 PARA_INFO_LOG("waketime: %s", asctime(tm));
248 client_cmd("stop");
249 sleep(1);
250 if (fot) {
251 PARA_INFO_LOG("initial volume: %d\n", iv);
252 ret = open_and_set_mixer_channel(iv);
253 if (ret < 0)
254 return ret;
255 change_afs_mode_and_play(fo_mood);
256 ret = fade(fov, fot);
257 if (ret < 0)
258 return ret;
259 } else {
260 ret = open_and_set_mixer_channel(fov);
261 if (ret < 0)
262 return ret;
263 }
264 if (conf.sleep_mood_given)
265 change_afs_mode_and_play(sleep_mood);
266 else
267 client_cmd("stop");
268 if (!fit)
269 return 1;
270 for (;;) {
271 time(&t1);
272 if (wake_time_epoch <= t1 + fit)
273 break;
274 delay = wake_time_epoch - t1 - fit;
275 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
276 delay, delay / 3600,
277 (delay % 3600) / 60);
278 sleep(delay);
279 }
280 change_afs_mode_and_play(fi_mood);
281 ret = fade(fiv, fit);
282 PARA_INFO_LOG("fade complete, returning\n");
283 return ret;
284 }
285
286 static int snooze(void)
287 {
288 int ret;
289 unsigned sleep_time;
290
291 if (conf.so_time_arg <= 0)
292 return 1;
293 sleep_time = conf.so_time_arg;
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(sleep_time);
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_sleep:
348 ret = sweet_dreams();
349 break;
350 case mode_arg_fade:
351 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
352 break;
353 case mode_arg_snooze:
354 ret = snooze();
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 }