First draft of the dccp sender/receiver pair.
[paraslash.git] / fade.c
1 /*
2 * Copyright (C) 1998-2005 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file fade.c a volume fader and alarm clock */
20
21 #include "fade.cmdline.h"
22 #include "para.h"
23
24 #include <ctype.h>
25 #include <curses.h>
26 #include <stdlib.h> /* EXIT_SUCCESS */
27 #include <unistd.h>
28 #include <signal.h>
29 #include <string.h>
30 #include <limits.h>
31 #include <linux/soundcard.h>
32 #include "string.h"
33
34
35 struct gengetopt_args_info args_info;
36
37 void para_log(__unused int ll, char *fmt,...)
38 {
39 va_list argp;
40 time_t t1;
41 struct tm *tm;
42
43 time(&t1);
44 tm = localtime(&t1);
45 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
46 va_start(argp, fmt);
47 vprintf(fmt, argp);
48 va_end(argp);
49 }
50
51 /*
52 * open mixer device
53 */
54 static int open_mixer(void)
55 {
56 int mixer_fd;
57
58 if ((mixer_fd = open(args_info.mixer_device_arg, O_RDWR, 0)) < 0)
59 return -1;
60 return mixer_fd;
61 }
62
63 /*
64 * get volume via mixer_fd
65 */
66 static int do_get_vol(int mixer_fd)
67 {
68 int volume;
69
70 if (ioctl(mixer_fd, MIXER_READ(SOUND_MIXER_VOLUME), &volume) < 0)
71 return -1;
72 /* take the mean value of left and right volume */
73 return (volume % 256 + (volume >> 8)) / 2;
74 }
75
76 /*
77 * open mixer, get volume and close mixer
78 */
79 static int get_vol(void)
80 {
81 int mixer_fd;
82 int volume;
83
84 mixer_fd = open_mixer();
85 if (mixer_fd < 0)
86 return -1;
87 volume = do_get_vol(mixer_fd);
88 close(mixer_fd);
89 return volume;
90 }
91
92 /*
93 * set volume via mixer_fd
94 */
95 static int do_set_vol(int mixer_fd, int volume)
96 {
97 int tmp;
98 tmp = (volume << 8) + volume;
99 if (ioctl(mixer_fd, MIXER_WRITE(SOUND_MIXER_VOLUME), &tmp) < 0)
100 return 0;
101 return 1;
102 }
103
104 /*
105 * open mixer, set volume and close mixer
106 */
107 static int set_vol(int volume)
108 {
109 int mixer_fd;
110 int ret;
111
112 mixer_fd = open_mixer();
113 ret = 0;
114 if (mixer_fd < 0)
115 goto out;
116 if (!do_set_vol(mixer_fd, volume))
117 goto out;
118 ret = 1;
119 close(mixer_fd);
120 out:
121 return ret;
122 }
123
124 /*
125 * Open mixer, get volume, fade to new_vol in secs seconds and
126 * close mixer
127 */
128 static int fade(int new_vol, unsigned int secs)
129 {
130 int vol, mixer_fd = -1, diff, incr, ret;
131 struct timespec ts;
132 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
133
134 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
135 ret = 0;
136 if (!secs)
137 goto out;
138 mixer_fd = open_mixer();
139 if (mixer_fd < 0)
140 goto out;
141 vol = do_get_vol(mixer_fd);
142 if (vol < 0)
143 goto out;
144 diff = new_vol - vol;
145 if (!diff) {
146 sleep(secs);
147 ret = 1;
148 goto out;
149 }
150 incr = diff > 0? 1: -1;
151 diff = diff > 0? diff: -diff;
152 tmp = secs * 1000 / diff;
153 tmp2 = tmp % 1000;
154 while ((new_vol - vol) * incr > 0) {
155 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
156 ts.tv_sec = tmp / 1000; /* really nec ?*/
157 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
158 vol += incr;
159 if (!do_set_vol(mixer_fd, vol))
160 goto out;
161 //printf("vol = %i\n", vol);
162 nanosleep(&ts, NULL);
163 }
164 out:
165 if (mixer_fd >= 0)
166 close(mixer_fd);
167 return 1;
168 }
169
170 static int client_cmd(char *cmd,...)
171 {
172 int ret, fds[3] = {0, 0, 0};
173 pid_t pid;
174 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
175 PARA_INFO_LOG("%s", cmdline);
176 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
177 free(cmdline);
178 return ret;
179 }
180
181 /*
182 * sleep
183 */
184 static void sweet_dreams(void)
185 {
186 time_t t1, wake_time_epoch;
187 unsigned int delay;
188 struct tm *tm;
189 int hour = args_info.wake_hour_arg;
190 int min = args_info.wake_min_arg;
191 char *fa_stream = args_info.fa_stream_arg;
192 char *wake_stream = args_info.wake_stream_arg;
193 //char *current_stream = stat_items[STREAM].content;
194 int wf = args_info.wake_fade_arg;
195 int sf = args_info.fa_fade_arg;
196 int wv = args_info.wake_vol_arg;
197 int sv = args_info.fa_vol_arg;
198 int iv = args_info.sleep_ivol_arg;
199 char *cmd, *sleep_stream = args_info.sleep_stream_given?
200 args_info.sleep_stream_arg : NULL;
201
202 if (sf) {
203 PARA_INFO_LOG("initial volume: %d\n", iv);
204 set_vol(iv);
205 cmd = make_message("csp %s\n", fa_stream);
206 client_cmd(cmd);
207 free(cmd);
208 fade(sv, sf);
209 }
210 if (sleep_stream) {
211 cmd = make_message("csp %s\n", sleep_stream);
212 client_cmd(cmd);
213 free(cmd);
214 } else
215 client_cmd("stop");
216 if (!wf)
217 return;
218 /* calculate wake time */
219 time(&t1);
220 tm = localtime(&t1);
221 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
222 /* wake time is tomorrow */
223 t1 += 86400;
224 tm = localtime(&t1);
225 t1 -= 86400;
226 }
227 tm->tm_hour = hour;
228 tm->tm_min = min;
229 tm->tm_sec = 0;
230 wake_time_epoch = mktime(tm);
231 PARA_INFO_LOG("waketime: %s", asctime(tm));
232 while (wake_time_epoch > t1 + wf) {
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 time(&t1);
239 }
240 cmd = make_message("csp %s\n", wake_stream);
241 client_cmd(cmd);
242 free(cmd);
243 fade(wv, wf);
244 PARA_INFO_LOG("%s", "fade complete, returning\n");
245 }
246
247 static void snooze(void)
248 {
249 if (get_vol() < args_info.snooze_out_vol_arg)
250 set_vol(args_info.snooze_out_vol_arg);
251 else
252 fade(args_info.snooze_out_vol_arg, args_info.snooze_out_fade_arg);
253 client_cmd("pause");
254 PARA_NOTICE_LOG("%d seconds snooze time...\n", args_info.snooze_time_arg);
255 sleep(args_info.snooze_time_arg);
256 client_cmd("play");
257 fade(args_info.snooze_in_vol_arg, args_info.snooze_in_fade_arg);
258 }
259
260 static int configfile_exists(void)
261 {
262 static char *config_file;
263
264 if (!args_info.config_file_given) {
265 char *home = para_homedir();
266 const char *conf = ".paraslash/fade.conf";
267 free(config_file);
268 config_file = make_message("%s/%s", home, conf);
269 free(home);
270 args_info.config_file_arg = config_file;
271 }
272 return file_exists(args_info.config_file_arg);
273 }
274
275
276 int main(int argc, char *argv[])
277 {
278 int ret;
279
280 if (cmdline_parser(argc, argv, &args_info))
281 exit(EXIT_FAILURE);
282 ret = configfile_exists();
283 if (!ret && args_info.config_file_given) {
284 PARA_EMERG_LOG("can not read config file %s\n",
285 args_info.config_file_arg);
286 exit(EXIT_FAILURE);
287 }
288 if (ret)
289 cmdline_parser_configfile(args_info.config_file_arg,
290 &args_info, 0, 0, 0);
291 if ((ret = open_mixer()) < 0) {
292 PARA_EMERG_LOG("can not open mixer device %s.",
293 args_info.mixer_device_arg);
294 exit(EXIT_FAILURE);
295 } else
296 close(ret);
297 ret = 0;
298 setlinebuf(stdout);
299 if (!strcmp(args_info.mode_arg, "sleep")) {
300 sweet_dreams();
301 goto out;
302 }
303 if (!strcmp(args_info.mode_arg, "fade")) {
304 fade(args_info.fade_vol_arg, args_info.fade_time_arg);
305 goto out;
306 }
307 if (!strcmp(args_info.mode_arg, "snooze")) {
308 snooze();
309 goto out;
310 }
311 ret = -1;
312 out:
313 return ret;
314 }