Fix com_stat() for arguments greater than one.
[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 */
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
26
27 struct fade_args_info conf;
28
29 void para_log(__a_unused int ll, const char *fmt,...)
30 {
31 va_list argp;
32 time_t t1;
33 struct tm *tm;
34
35 time(&t1);
36 tm = localtime(&t1);
37 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
38 va_start(argp, fmt);
39 vprintf(fmt, argp);
40 va_end(argp);
41 }
42
43 /*
44 * open mixer device
45 */
46 static int open_mixer(void)
47 {
48 return open(conf.mixer_device_arg, O_RDWR, 0);
49 }
50
51 /*
52 * get volume via mixer_fd
53 */
54 static int do_get_vol(int mixer_fd)
55 {
56 int volume;
57
58 if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
59 return -1;
60 /* take the mean value of left and right volume */
61 return (volume % 256 + (volume >> 8)) / 2;
62 }
63
64 /*
65 * open mixer, get volume and close mixer
66 */
67 static int get_vol(void)
68 {
69 int mixer_fd;
70 int volume;
71
72 mixer_fd = open_mixer();
73 if (mixer_fd < 0)
74 return -1;
75 volume = do_get_vol(mixer_fd);
76 close(mixer_fd);
77 return volume;
78 }
79
80 /*
81 * set volume via mixer_fd
82 */
83 static int do_set_vol(int mixer_fd, int volume)
84 {
85 int tmp = (volume << 8) + volume;
86
87 if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
88 return -1;
89 return 1;
90 }
91
92 /*
93 * open mixer, set volume and close mixer
94 */
95 static int set_vol(int volume)
96 {
97 int mixer_fd, ret = open_mixer();
98
99 if (ret < 0)
100 return ret;
101 mixer_fd = ret;
102 ret = do_set_vol(mixer_fd, volume);
103 close(mixer_fd);
104 return ret;
105 }
106
107 /*
108 * Open mixer, get volume, fade to new_vol in secs seconds and
109 * close mixer
110 */
111 static void fade(int new_vol, int fade_time)
112 {
113 int vol, mixer_fd, diff, incr;
114 unsigned secs;
115 struct timespec ts;
116 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
117
118 if (fade_time <= 0)
119 return;
120 secs = fade_time;
121 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
122 mixer_fd = open_mixer();
123 if (mixer_fd < 0)
124 return;
125 vol = do_get_vol(mixer_fd);
126 if (vol < 0)
127 goto out;
128 diff = new_vol - vol;
129 if (!diff) {
130 sleep(secs);
131 goto out;
132 }
133 incr = diff > 0? 1: -1;
134 diff = diff > 0? diff: -diff;
135 tmp = secs * 1000 / diff;
136 tmp2 = tmp % 1000;
137 while ((new_vol - vol) * incr > 0) {
138 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
139 ts.tv_sec = tmp / 1000; /* really nec ?*/
140 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
141 vol += incr;
142 if (do_set_vol(mixer_fd, vol) < 0)
143 goto out;
144 //printf("vol = %i\n", vol);
145 nanosleep(&ts, NULL);
146 }
147 out:
148 close(mixer_fd);
149 }
150
151 static void client_cmd(const char *cmd)
152 {
153 int ret, fds[3] = {0, 0, 0};
154 pid_t pid;
155 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
156
157 PARA_INFO_LOG("%s\n", cmdline);
158 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
159 free(cmdline);
160 if (ret < 0)
161 exit(EXIT_FAILURE);
162 do
163 ret = wait(NULL);
164 while (ret != -1 && errno != ECHILD);
165 }
166
167 static void change_afs_mode_and_play(char *afs_mode)
168 {
169 char *cmd;
170
171 client_cmd("stop");
172 if (!afs_mode)
173 return;
174 cmd = make_message("select %s\n", afs_mode);
175 client_cmd(cmd);
176 free(cmd);
177 client_cmd("play");
178 }
179
180 /*
181 * sleep
182 */
183 static void sweet_dreams(void)
184 {
185 time_t t1, wake_time_epoch;
186 unsigned int delay;
187 struct tm *tm;
188 int min = conf.wake_min_arg;
189 char *fa_mode = conf.fa_mode_arg;
190 char *wake_mode = conf.wake_mode_arg;
191 char *sleep_mode = conf.sleep_mode_arg;
192 int wf = conf.wake_fade_arg;
193 int sf = conf.fa_fade_arg;
194 int wv = conf.wake_vol_arg;
195 int sv = conf.fa_vol_arg;
196 int iv = conf.sleep_ivol_arg;
197
198 /* calculate wake time */
199 time(&t1);
200 if (conf.wake_hour_given) {
201 int hour = conf.wake_hour_arg;
202 tm = localtime(&t1);
203 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
204 t1 += 86400; /* wake time is tomorrow */
205 tm = localtime(&t1);
206 }
207 tm->tm_hour = hour;
208 tm->tm_min = min;
209 tm->tm_sec = 0;
210 } else {
211 t1 += 9 * 60 * 60; /* nine hours from now */
212 PARA_INFO_LOG("default wake time: %lu\n", t1);
213 tm = localtime(&t1);
214 }
215 wake_time_epoch = mktime(tm);
216 PARA_INFO_LOG("waketime: %s", asctime(tm));
217 client_cmd("stop");
218 sleep(1);
219 if (sf) {
220 PARA_INFO_LOG("initial volume: %d\n", iv);
221 set_vol(iv);
222 change_afs_mode_and_play(fa_mode);
223 fade(sv, sf);
224 } else
225 set_vol(sf);
226 change_afs_mode_and_play(sleep_mode);
227 if (!wf)
228 return;
229 for (;;) {
230 time(&t1);
231 if (wake_time_epoch <= t1 + wf)
232 break;
233 delay = wake_time_epoch - t1 - wf;
234 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
235 delay, delay / 3600,
236 (delay % 3600) / 60);
237 sleep(delay);
238 }
239 change_afs_mode_and_play(wake_mode);
240 fade(wv, wf);
241 PARA_INFO_LOG("fade complete, returning\n");
242 }
243
244 static void snooze(void)
245 {
246 unsigned sleep_time;
247
248 if (conf.snooze_time_arg <= 0)
249 return;
250 sleep_time = conf.snooze_time_arg;
251 if (get_vol() < conf.snooze_out_vol_arg)
252 set_vol(conf.snooze_out_vol_arg);
253 else
254 fade(conf.snooze_out_vol_arg, conf.snooze_out_fade_arg);
255 client_cmd("pause");
256 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
257 sleep(sleep_time);
258 client_cmd("play");
259 fade(conf.snooze_in_vol_arg, conf.snooze_in_fade_arg);
260 }
261
262 static int configfile_exists(void)
263 {
264 static char *config_file;
265
266 if (!conf.config_file_given) {
267 char *home = para_homedir();
268 free(config_file);
269 config_file = make_message("%s/.paraslash/fade.conf", home);
270 free(home);
271 conf.config_file_arg = config_file;
272 }
273 return file_exists(conf.config_file_arg);
274 }
275
276
277 int main(int argc, char *argv[])
278 {
279 int ret;
280
281 if (fade_cmdline_parser(argc, argv, &conf))
282 exit(EXIT_FAILURE);
283 HANDLE_VERSION_FLAG("fade", conf);
284 ret = configfile_exists();
285 if (!ret && conf.config_file_given) {
286 PARA_EMERG_LOG("can not read config file %s\n",
287 conf.config_file_arg);
288 exit(EXIT_FAILURE);
289 }
290 if (ret) {
291 struct fade_cmdline_parser_params params = {
292 .override = 0,
293 .initialize = 0,
294 .check_required = 0,
295 .check_ambiguity = 0
296 };
297 fade_cmdline_parser_config_file(conf.config_file_arg,
298 &conf, &params);
299 }
300 ret = open_mixer();
301 if (ret < 0) {
302 PARA_EMERG_LOG("can not open mixer device %s.",
303 conf.mixer_device_arg);
304 exit(EXIT_FAILURE);
305 }
306 close(ret);
307 ret = 0;
308 // setlinebuf(stdout);
309 if (!strcmp(conf.mode_arg, "sleep")) {
310 sweet_dreams();
311 goto out;
312 }
313 if (!strcmp(conf.mode_arg, "fade")) {
314 fade(conf.fade_vol_arg, conf.fade_time_arg);
315 goto out;
316 }
317 if (!strcmp(conf.mode_arg, "snooze")) {
318 snooze();
319 goto out;
320 }
321 ret = -1;
322 out:
323 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
324 }