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