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