Merge branch 'refs/heads/t/gui-improvements'
[paraslash.git] / write.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file write.c Paraslash's standalone wav/raw player. */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "string.h"
14 #include "write.cmdline.h"
15 #include "list.h"
16 #include "sched.h"
17 #include "ggo.h"
18 #include "stdin.h"
19 #include "buffer_tree.h"
20 #include "write.h"
21 #include "write_common.h"
22 #include "fd.h"
23 #include "error.h"
24 #include "version.h"
25 #include "check_wav.h"
26
27 INIT_WRITE_ERRLISTS;
28
29 static struct write_args_info conf;
30
31 static struct stdin_task sit;
32
33 static int loglevel;
34 INIT_STDERR_LOGGING(loglevel)
35
36 __noreturn static void print_help_and_die(void)
37 {
38         struct ggo_help h = DEFINE_GGO_HELP(write);
39         bool d = conf.detailed_help_given;
40
41         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
42         print_writer_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
43         exit(0);
44 }
45
46 /*
47  * Parse config and register a task for a writer node.
48  *
49  * \param arg Command line arguments.
50  * \param parent The new node will be a child of \a parent.
51  * \param wn The writer node.
52  *
53  * If arg is \p NULL, the OS-dependent default writer is used with no
54  * arguments.  The default writers are alsa for Linux, osx for OS X, oss for
55  * *BSD, and the file writer if the default writer is not supported.
56  *
57  * Once the writer configuration has been retrieved from the ->parse_config
58  * callback a writer node is created, its buffer tree node is added to the
59  * buffer tree as a child of the given parent.
60  *
61  * Finally, the new writer node's task structure is initialized and registered
62  * to the paraslash scheduler.
63  *
64  * \return Standard.
65  */
66 static void setup_writer_node(const char *arg, struct btr_node *parent,
67                 struct writer_node *wn, struct sched *s)
68 {
69         wn->conf = check_writer_arg_or_die(arg, &wn->writer_num);
70         register_writer_node(wn, parent, s);
71 }
72
73 struct write_task {
74         struct task *task;
75         struct check_wav_context *cwc;
76 };
77
78 static void write_pre_select(struct sched *s, void *context)
79 {
80         struct write_task *wt = context;
81         check_wav_pre_select(s, wt->cwc);
82 }
83
84 static int write_post_select(__a_unused struct sched *s, void *context)
85 {
86         struct write_task *wt = context;
87         return check_wav_post_select(wt->cwc);
88 }
89
90 static int setup_and_schedule(void)
91 {
92         int i, ret;
93         struct btr_node *cw_btrn;
94         struct writer_node *wns;
95         static struct sched s;
96         struct wav_params wp;
97         struct write_task wt;
98
99         sit.btrn = btr_new_node(&(struct btr_node_description)
100                 EMBRACE(.name = "stdin"));
101         stdin_task_register(&sit, &s);
102
103         COPY_WAV_PARMS(&wp, &conf);
104         wt.cwc = check_wav_init(sit.btrn, NULL, &wp, &cw_btrn);
105         wt.task = task_register(&(struct task_info) {
106                 .name = "write",
107                 .pre_select = write_pre_select,
108                 .post_select = write_post_select,
109                 .context = &wt,
110         }, &s);
111         if (!conf.writer_given) {
112                 wns = para_calloc(sizeof(*wns));
113                 setup_writer_node(NULL, cw_btrn, wns, &s);
114                 i = 1;
115         } else {
116                 wns = para_calloc(conf.writer_given * sizeof(*wns));
117                 for (i = 0; i < conf.writer_given; i++)
118                         setup_writer_node(conf.writer_arg[i], cw_btrn,
119                                 wns + i, &s);
120         }
121
122         s.default_timeout.tv_sec = 10;
123         s.default_timeout.tv_usec = 50000;
124         ret = schedule(&s);
125         if (ret >= 0) {
126                 int j, ts;
127                 for (j = 0; j < i; j++) {
128                         struct writer_node *wn = wns + j;
129                         ts = task_status(wn->task);
130                         assert(ts < 0);
131                         if (ts != -E_WRITE_COMMON_EOF && ts != -E_BTR_EOF) {
132                                 const char *name = writer_names[wn->writer_num];
133                                 PARA_ERROR_LOG("%s: %s\n", name,
134                                         para_strerror(-ts));
135                                 if (ret >= 0)
136                                         ret = ts;
137                         }
138                 }
139         }
140         for (i--; i >= 0; i--) {
141                 struct writer_node *wn = wns + i;
142                 struct writer *w = writers + wn->writer_num;
143
144                 w->close(wn);
145                 btr_remove_node(&wn->btrn);
146                 w->free_config(wn->conf);
147                 free(wn->conf);
148         }
149         free(wns);
150         check_wav_shutdown(wt.cwc);
151         sched_shutdown(&s);
152         return ret;
153 }
154
155 /**
156  * Para_write's main function.
157  *
158  * \param argc The usual argument counter.
159  * \param argv The usual argument vector.
160  *
161  * It sets up and starts the tasks and the buffer tree nodes determined by
162  * command line options.
163  *
164  * \return \p EXIT_SUCCESS or EXIT_FAILURE
165  */
166 int main(int argc, char *argv[])
167 {
168         int ret;
169
170         write_cmdline_parser(argc, argv, &conf);
171         loglevel = get_loglevel_by_name(conf.loglevel_arg);
172         writer_init();
173         version_handle_flag("write", conf.version_given);
174         if (conf.help_given || conf.detailed_help_given)
175                 print_help_and_die();
176
177         ret = setup_and_schedule();
178         if (ret < 0) {
179                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
180                 exit(EXIT_FAILURE);
181         }
182         exit(EXIT_SUCCESS);
183 }