Fix whitespace inconsistencies.
[herrie-working.git] / herrie / src / main.c
blob1adff3337fde5de6179f8c587161d9256be753f1
1 /*
2 * Copyright (c) 2006-2011 Ed Schouten <ed@80386.nl>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
26 /**
27 * @file main.c
28 * @brief Application startup routine.
31 #include "stdinc.h"
33 #include "audio_output.h"
34 #include "config.h"
35 #include "dbus.h"
36 #include "gui.h"
37 #include "playq.h"
38 #include "scrobbler.h"
39 #include "vfs.h"
41 /**
42 * @brief Display the application version and compile-time options.
44 static void
45 version(void)
47 g_printerr(APP_NAME " " APP_VERSION
48 " (Two-clause BSD license"
49 #if defined(BUILD_AO) || defined(BUILD_MP3) || defined(BUILD_SNDFILE)
50 ", using GNU GPL licensed libraries"
51 #endif /* BUILD_AO || BUILD_MP3 || BUILD_SNDFILE */
52 ")\n\n"
53 "%s: " CONFFILE "\n"
54 "%s: " AUDIO_OUTPUT "\n"
55 "%s: %s\n"
56 "%s: %s\n"
57 "%s: %s\n"
58 "%s: %s\n"
59 "%s:\n"
60 #ifdef BUILD_VORBIS
61 "- Ogg Vorbis\n"
62 #endif /* BUILD_VORBIS */
63 #ifdef BUILD_MP3
64 "- MP3\n"
65 #endif /* BUILD_MP3 */
66 #ifdef BUILD_MODPLUG
67 "- libmodplug\n"
68 #endif /* BUILD_MODPLUG */
69 #ifdef BUILD_SNDFILE
70 "- libsndfile\n"
71 #endif /* BUILD_SNDFILE */
73 _("Global configuration file"),
74 _("Audio output"),
75 _("Support for AudioScrobbler"),
76 #ifdef BUILD_SCROBBLER
77 _("yes"),
78 #else /* !BUILD_SCROBBLER */
79 _("no"),
80 #endif /* BUILD_SCROBBLER */
81 _("Support for DBus integration"),
82 #ifdef BUILD_DBUS
83 _("yes"),
84 #else /* !BUILD_DBUS */
85 _("no"),
86 #endif /* BUILD_DBUS */
87 _("Support for HTTP streams"),
88 #ifdef BUILD_HTTP
89 _("yes"),
90 #else /* !BUILD_HTTP */
91 _("no"),
92 #endif /* BUILD_HTTP */
93 _("Support for XSPF playlists (`spiff')"),
94 #ifdef BUILD_XSPF
95 _("yes"),
96 #else /* !BUILD_XSPF */
97 _("no"),
98 #endif /* BUILD_XSPF */
99 _("Supported audio file formats"));
101 exit(0);
105 * @brief Display the command line usage flags.
107 static void
108 usage(void)
110 g_printerr("%s: " APP_NAME " [-pvx] [-c configfile] "
111 "[file ...]\n", _("usage"));
112 exit(1);
116 * @brief Startup routine for the application.
119 main(int argc, char *argv[])
121 int ch, i, show_version = 0, autoplay = 0, xmms = 0;
122 char *cwd;
123 const char *errmsg;
124 struct vfsref *vr;
125 #ifdef CLOSE_STDERR
126 int devnull;
127 #endif /* CLOSE_STDERR */
129 #ifdef BUILD_NLS
130 setlocale(LC_ALL, "");
131 bindtextdomain(APP_NAME, TRANSDIR);
132 textdomain(APP_NAME);
133 #endif /* BUILD_NLS */
135 /* Global and local configuration files */
136 config_load(CONFFILE, 1);
137 config_load(CONFHOMEDIR "config", 1);
139 while ((ch = getopt(argc, argv, "c:pvx")) != -1) {
140 switch (ch) {
141 case 'c':
142 config_load(optarg, 0);
143 break;
144 case 'p':
145 autoplay = 1;
146 break;
147 case 'v':
148 show_version = 1;
149 break;
150 case 'x':
151 xmms = 1;
152 break;
153 default:
154 usage();
157 argc -= optind;
158 argv += optind;
160 if (show_version)
161 version();
163 g_thread_init(NULL);
165 if (audio_output_open() != 0)
166 return (-1);
168 /* Already initialize the GUI before chroot() */
169 gui_draw_init_pre();
171 #ifdef CLOSE_STDERR
172 devnull = open("/dev/null", O_WRONLY);
173 #endif /* CLOSE_STDERR */
174 if ((errmsg = vfs_lockup()) != NULL) {
175 gui_draw_init_abort();
176 g_printerr("%s", errmsg);
177 return (1);
180 vfs_cache_init();
182 /* Initialize the locks */
183 #ifdef BUILD_DBUS
184 dbus_init();
185 #endif /* BUILD_DBUS */
186 #ifdef BUILD_SCROBBLER
187 scrobbler_init();
188 #endif /* BUILD_SCROBBLER */
189 playq_init(autoplay, xmms, (argc == 0));
191 /* Draw all the windows */
192 gui_draw_init_post();
194 cwd = g_get_current_dir();
195 for (i = 0; i < argc; i++) {
196 /* Shell will expand ~ */
197 if ((vr = vfs_lookup(argv[i], NULL, cwd, 1)) != NULL) {
198 playq_song_add_tail(vr);
199 vfs_close(vr);
202 g_free(cwd);
204 #ifdef CLOSE_STDERR
205 /* Close stderr before starting playback */
206 dup2(devnull, STDERR_FILENO);
207 close(devnull);
208 #endif /* CLOSE_STDERR */
210 /* All set and done - spawn our threads */
211 playq_spawn();
212 #ifdef BUILD_DBUS
213 dbus_spawn();
214 #endif /* BUILD_DBUS */
215 #ifdef BUILD_SCROBBLER
216 scrobbler_spawn();
217 #endif /* BUILD_SCROBBLER */
219 /* And off we go! */
220 gui_input_loop();
221 g_assert_not_reached();
222 return (1);