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