fade: Switch to modular mixer API.
[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
11 #include "fade.cmdline.h"
12 #include "para.h"
13 #include "fd.h"
14 #include "string.h"
15 #include "mix.h"
16 #include "error.h"
17 #include "version.h"
18
19 INIT_FADE_ERRLISTS;
20 static struct fade_args_info conf;
21
22 enum mixer_id {MIXER_ENUM};
23 static char *mixer_name[] = {MIXER_NAMES};
24 DECLARE_MIXER_INITS;
25 static struct mixer supported_mixer[] = {MIXER_ARRAY};
26
27 __printf_2_3 void date_log(__a_unused int ll, const char *fmt, ...)
28 {
29 va_list argp;
30 time_t t1;
31 struct tm *tm;
32
33 time(&t1);
34 tm = localtime(&t1);
35 printf("%d:%02d:%02d ", tm->tm_hour, tm->tm_min, tm->tm_sec);
36 va_start(argp, fmt);
37 vprintf(fmt, argp);
38 va_end(argp);
39 }
40 __printf_2_3 void (*para_log)(int, const char*, ...) = date_log;
41
42 /* Fade to new volume in fade_time seconds. */
43 static int fade(struct mixer *m, struct mixer_handle *h, int new_vol, int fade_time)
44 {
45 int vol, diff, incr, ret;
46 unsigned secs;
47 struct timespec ts;
48 unsigned long long tmp, tmp2; /* Careful with that axe, Eugene! */
49
50 if (fade_time <= 0)
51 return 1;
52 secs = fade_time;
53 PARA_NOTICE_LOG("fading to %d in %d seconds\n", new_vol, secs);
54 ret = m->get(h);
55 if (ret < 0)
56 goto out;
57 vol = ret;
58 diff = new_vol - vol;
59 if (!diff) {
60 sleep(secs);
61 goto out;
62 }
63 incr = diff > 0? 1: -1;
64 diff = diff > 0? diff: -diff;
65 tmp = secs * 1000 / diff;
66 tmp2 = tmp % 1000;
67 while ((new_vol - vol) * incr > 0) {
68 ts.tv_nsec = tmp2 * 1000000; /* really nec ?*/
69 ts.tv_sec = tmp / 1000; /* really nec ?*/
70 //printf("ts.tv_sec: %i\n", ts.tv_nsec);
71 vol += incr;
72 ret = m->set(h, vol);
73 if (ret < 0)
74 goto out;
75 //printf("vol = %i\n", vol);
76 nanosleep(&ts, NULL);
77 }
78 out:
79 return ret;
80 }
81
82 static void client_cmd(const char *cmd)
83 {
84 int ret, status, fds[3] = {0, 0, 0};
85 pid_t pid;
86 char *cmdline = make_message(BINDIR "/para_client %s", cmd);
87
88 PARA_INFO_LOG("%s\n", cmdline);
89 ret = para_exec_cmdline_pid(&pid, cmdline, fds);
90 free(cmdline);
91 if (ret < 0) {
92 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
93 goto fail;
94 }
95 do
96 pid = waitpid(pid, &status, 0);
97 while (pid == -1 && errno == EINTR);
98 if (pid < 0) {
99 PARA_ERROR_LOG("%s\n", strerror(errno));
100 goto fail;
101 }
102 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
103 goto fail;
104 return;
105 fail:
106 PARA_EMERG_LOG("command \"%s\" failed\n", cmd);
107 exit(EXIT_FAILURE);
108 }
109
110 static void change_afs_mode_and_play(char *afs_mode)
111 {
112 char *cmd;
113
114 client_cmd("stop");
115 if (!afs_mode)
116 return;
117 cmd = make_message("select %s", afs_mode);
118 client_cmd(cmd);
119 free(cmd);
120 client_cmd("play");
121 }
122
123 static int sweet_dreams(struct mixer *m, struct mixer_handle *h)
124 {
125 time_t t1, wake_time_epoch;
126 unsigned int delay;
127 struct tm *tm;
128 int ret, min = conf.wake_min_arg;
129 char *fo_mood = conf.fo_mood_arg;
130 char *fi_mood = conf.fi_mood_arg;
131 char *sleep_mood = conf.sleep_mood_arg;
132 int fit = conf.fi_time_arg;
133 int fot = conf.fo_time_arg;
134 int fiv = conf.fi_vol_arg;
135 int fov = conf.fo_vol_arg;
136 int iv = conf.ivol_arg;
137
138 /* calculate wake time */
139 time(&t1);
140 if (conf.wake_hour_given) {
141 int hour = conf.wake_hour_arg;
142 tm = localtime(&t1);
143 if (tm->tm_hour > hour || (tm->tm_hour == hour && tm->tm_min> min)) {
144 t1 += 86400; /* wake time is tomorrow */
145 tm = localtime(&t1);
146 }
147 tm->tm_hour = hour;
148 tm->tm_min = min;
149 tm->tm_sec = 0;
150 } else {
151 t1 += 9 * 60 * 60; /* nine hours from now */
152 PARA_INFO_LOG("default wake time: %lu\n", (long unsigned)t1);
153 tm = localtime(&t1);
154 }
155 wake_time_epoch = mktime(tm);
156 PARA_INFO_LOG("waketime: %s", asctime(tm));
157 client_cmd("stop");
158 sleep(1);
159 if (fot) {
160 PARA_INFO_LOG("initial volume: %d\n", iv);
161 ret = m->set(h, iv);
162 if (ret < 0)
163 return ret;
164 change_afs_mode_and_play(fo_mood);
165 ret = fade(m, h, fov, fot);
166 if (ret < 0)
167 return ret;
168 } else {
169 ret = m->set(h, fov);
170 if (ret < 0)
171 return ret;
172 }
173 if (conf.sleep_mood_given)
174 change_afs_mode_and_play(sleep_mood);
175 else
176 client_cmd("stop");
177 if (!fit)
178 return 1;
179 for (;;) {
180 time(&t1);
181 if (wake_time_epoch <= t1 + fit)
182 break;
183 delay = wake_time_epoch - t1 - fit;
184 PARA_INFO_LOG("sleeping %u seconds (%u:%02u)\n",
185 delay, delay / 3600,
186 (delay % 3600) / 60);
187 sleep(delay);
188 }
189 change_afs_mode_and_play(fi_mood);
190 ret = fade(m, h, fiv, fit);
191 PARA_INFO_LOG("fade complete, returning\n");
192 return ret;
193 }
194
195 static int snooze(struct mixer *m, struct mixer_handle *h)
196 {
197 int ret, val;
198
199 if (conf.so_time_arg <= 0)
200 return 1;
201 ret = m->get(h);
202 if (ret < 0)
203 return ret;
204 val = ret;
205 if (val < conf.so_vol_arg)
206 ret = m->set(h, conf.so_vol_arg);
207 else
208 ret = fade(m, h, conf.so_vol_arg, conf.so_time_arg);
209 if (ret < 0)
210 return ret;
211 client_cmd("pause");
212 PARA_NOTICE_LOG("%d seconds snooze time...\n", conf.snooze_time_arg);
213 sleep(conf.snooze_time_arg);
214 client_cmd("play");
215 return fade(m, h, conf.si_vol_arg, conf.si_time_arg);
216 }
217
218 static int configfile_exists(void)
219 {
220 static char *config_file;
221
222 if (!conf.config_file_given) {
223 char *home = para_homedir();
224 free(config_file);
225 config_file = make_message("%s/.paraslash/fade.conf", home);
226 free(home);
227 conf.config_file_arg = config_file;
228 }
229 return file_exists(conf.config_file_arg);
230 }
231
232 static void init_mixers(void)
233 {
234 int i;
235
236 for (i = 0; i < NUM_SUPPORTED_MIXERS; i++) {
237 struct mixer *m = &supported_mixer[i];
238 PARA_DEBUG_LOG("initializing mixer API #%d (%s)\n",
239 i, mixer_name[i]);
240 m->init(m);
241 }
242 }
243
244 static int set_channel(struct mixer *m, struct mixer_handle *h)
245 {
246 char *channels;
247 int ret;
248
249 ret = m->set_channel(h, conf.mixer_channel_arg);
250 if (ret >= 0) {
251 PARA_NOTICE_LOG("using %s mixer channel\n",
252 conf.mixer_channel_arg? conf.mixer_channel_arg
253 : "default");
254 return ret;
255 }
256 channels = m->get_channels(h);
257 printf("Available channels: %s\n", channels);
258 free(channels);
259 return ret;
260 }
261
262 int main(int argc, char *argv[])
263 {
264 int ret;
265 struct mixer *m;
266 struct mixer_handle *h = NULL;
267
268 if (fade_cmdline_parser(argc, argv, &conf))
269 exit(EXIT_FAILURE);
270 HANDLE_VERSION_FLAG("fade", conf);
271 ret = configfile_exists();
272 if (!ret && conf.config_file_given) {
273 PARA_EMERG_LOG("can not read config file %s\n",
274 conf.config_file_arg);
275 exit(EXIT_FAILURE);
276 }
277 if (ret) {
278 struct fade_cmdline_parser_params params = {
279 .override = 0,
280 .initialize = 0,
281 .check_required = 0,
282 .check_ambiguity = 0,
283 .print_errors = 1
284 };
285 fade_cmdline_parser_config_file(conf.config_file_arg,
286 &conf, &params);
287 }
288 init_mixers();
289 m = &supported_mixer[DEFAULT_MIXER];
290 PARA_INFO_LOG("using %s mixer\n", mixer_name[DEFAULT_MIXER]);
291 ret = m->open(conf.mixer_device_arg, &h);
292 if (ret < 0)
293 goto out;
294 ret = set_channel(m, h);
295 if (ret < 0)
296 goto out;
297 switch (conf.mode_arg) {
298 case mode_arg_fade:
299 ret = fade(m, h, conf.fade_vol_arg, conf.fade_time_arg);
300 break;
301 case mode_arg_snooze:
302 ret = snooze(m, h);
303 break;
304 default: /* sleep mode */
305 ret = sweet_dreams(m, h);
306 break;
307 }
308 out:
309 m->close(&h);
310 if (ret < 0)
311 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
312 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
313 }