Simplify vss_preselect().
[paraslash.git] / fade.c
1 /*
2 * Copyright (C) 1998-2008 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 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 change_afs_mode_and_play(sleep_mode);
242 if (!wf)
243 return 1;
244 for (;;) {
245 time(&t1);
246 if (wake_time_epoch <= t1 + wf)
247 break;
248 delay = wake_time_epoch - t1 - wf;
249 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
250 delay, delay / 3600,
251 (delay % 3600) / 60);
252 sleep(delay);
253 }
254 change_afs_mode_and_play(wake_mode);
255 ret = fade(wv, wf);
256 PARA_INFO_LOG("fade complete, returning\n");
257 return ret;
258 }
259
260 static int snooze(void)
261 {
262 int ret;
263 unsigned sleep_time;
264
265 if (conf.snooze_time_arg <= 0)
266 return 1;
267 sleep_time = conf.snooze_time_arg;
268 if (get_vol() < conf.snooze_out_vol_arg)
269 ret = set_vol(conf.snooze_out_vol_arg);
270 else
271 ret = fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
272 if (ret < 0)
273 return ret;
274 client_cmd("pause");
275 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
276 sleep(sleep_time);
277 client_cmd("play");
278 return fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
279 }
280
281 static int configfile_exists(void)
282 {
283 static char *config_file;
284
285 if (!conf.config_file_given) {
286 char *home = para_homedir();
287 free(config_file);
288 config_file = make_message("%s/.paraslash/fade.conf", home);
289 free(home);
290 conf.config_file_arg = config_file;
291 }
292 return file_exists(conf.config_file_arg);
293 }
294
295 int main(int argc, char *argv[])
296 {
297 int ret;
298
299 if (fade_cmdline_parser(argc, argv, &conf))
300 exit(EXIT_FAILURE);
301 HANDLE_VERSION_FLAG("fade", conf);
302 ret = configfile_exists();
303 if (!ret && conf.config_file_given) {
304 PARA_EMERG_LOG("can not read config file %s\n",
305 conf.config_file_arg);
306 exit(EXIT_FAILURE);
307 }
308 if (ret) {
309 struct fade_cmdline_parser_params params = {
310 .override = 0,
311 .initialize = 0,
312 .check_required = 0,
313 .check_ambiguity = 0
314 };
315 fade_cmdline_parser_config_file(conf.config_file_arg,
316 &conf, &params);
317 }
318 if (!strcmp(conf.mode_arg, "sleep")) {
319 ret = sweet_dreams();
320 goto out;
321 }
322 if (!strcmp(conf.mode_arg, "fade")) {
323 ret = fade(conf.fade_vol_arg, conf.fade_time_arg);
324 goto out;
325 }
326 if (!strcmp(conf.mode_arg, "snooze")) {
327 ret = snooze();
328 goto out;
329 }
330 ret = -E_FADE_SYNTAX;
331 out:
332 if (ret < 0)
333 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
334 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
335 }