Code

main: move default_settings_name() to mpdclient.c
[ncmpc.git] / src / main.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
20 #include "config.h"
21 #include "ncmpc.h"
22 #include "mpdclient.h"
23 #include "callbacks.h"
24 #include "charset.h"
25 #include "options.h"
26 #include "command.h"
27 #include "ncu.h"
28 #include "screen.h"
29 #include "screen_status.h"
30 #include "xterm_title.h"
31 #include "strfsong.h"
32 #include "i18n.h"
33 #include "player_command.h"
34 #include "keyboard.h"
35 #include "lirc.h"
36 #include "signals.h"
38 #ifndef NCMPC_MINI
39 #include "conf.h"
40 #endif
42 #ifdef ENABLE_LYRICS_SCREEN
43 #include "lyrics.h"
44 #endif
46 #include <mpd/client.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <fcntl.h>
51 #include <signal.h>
52 #include <string.h>
54 #ifdef ENABLE_LOCALE
55 #include <locale.h>
56 #endif
58 /* time between mpd updates [ms] */
59 static const guint update_interval = 500;
61 #define BUFSIZE 1024
63 static struct mpdclient *mpd = NULL;
64 static GMainLoop *main_loop;
65 static guint reconnect_source_id, update_source_id;
67 #ifndef NCMPC_MINI
68 static guint check_key_bindings_source_id;
69 #endif
71 #ifndef NCMPC_MINI
72 static void
73 update_xterm_title(void)
74 {
75         const struct mpd_song *song = mpd->song;
77         char tmp[BUFSIZE];
78         const char *new_title = NULL;
79         if (options.xterm_title_format && mpd->playing && song)
80                 new_title = strfsong(tmp, BUFSIZE, options.xterm_title_format, song) > 0
81                         ? tmp
82                         : NULL;
84         if (new_title == NULL)
85                 new_title = PACKAGE " version " VERSION;
87         static char title[BUFSIZE];
88         if (strncmp(title, new_title, BUFSIZE)) {
89                 g_strlcpy(title, new_title, BUFSIZE);
90                 set_xterm_title(title);
91         }
92 }
93 #endif
95 static gboolean
96 timer_mpd_update(gpointer data);
98 static void
99 enable_update_timer(void)
101         if (update_source_id != 0)
102                 return;
104         update_source_id = g_timeout_add(update_interval,
105                                          timer_mpd_update, NULL);
108 static void
109 disable_update_timer(void)
111         if (update_source_id == 0)
112                 return;
114         g_source_remove(update_source_id);
115         update_source_id = 0;
118 static bool
119 should_enable_update_timer(void)
121         return mpd->playing;
124 static void
125 auto_update_timer(void)
127         if (should_enable_update_timer())
128                 enable_update_timer();
129         else
130                 disable_update_timer();
133 static void
134 do_mpd_update(void)
136         if (mpdclient_is_connected(mpd) &&
137             (mpd->events != 0 || mpd->playing))
138                 mpdclient_update(mpd);
140 #ifndef NCMPC_MINI
141         if (options.enable_xterm_title)
142                 update_xterm_title();
143 #endif
145         screen_update(mpd);
146         mpd->events = 0;
149 /**
150  * This timer is installed when the connection to the MPD server is
151  * broken.  It tries to recover by reconnecting periodically.
152  */
153 static gboolean
154 timer_reconnect(gcc_unused gpointer data)
156         assert(mpdclient_is_dead(mpd));
158         reconnect_source_id = 0;
160         char *name = mpdclient_settings_name(mpd);
161         screen_status_printf(_("Connecting to %s...  [Press %s to abort]"),
162                              name, get_key_names(CMD_QUIT, false));
163         g_free(name);
164         doupdate();
166         mpdclient_connect(mpd);
168         return FALSE;
171 void
172 mpdclient_connected_callback(void)
174         assert(reconnect_source_id == 0);
176 #ifndef NCMPC_MINI
177         /* quit if mpd is pre 0.14 - song id not supported by mpd */
178         struct mpd_connection *connection = mpdclient_get_connection(mpd);
179         if (mpd_connection_cmp_server_version(connection, 0, 16, 0) < 0) {
180                 const unsigned *version =
181                         mpd_connection_get_server_version(connection);
182                 screen_status_printf(_("Error: MPD version %d.%d.%d is too old (%s needed)"),
183                                      version[0], version[1], version[2],
184                                      "0.16.0");
185                 mpdclient_disconnect(mpd);
186                 doupdate();
188                 /* try again after 30 seconds */
189                 reconnect_source_id =
190                         g_timeout_add_seconds(30, timer_reconnect, NULL);
191                 return;
192         }
193 #endif
195         screen_status_clear_message();
196         doupdate();
198         /* update immediately */
199         mpd->events = MPD_IDLE_ALL;
201         do_mpd_update();
203         auto_update_timer();
206 void
207 mpdclient_failed_callback(void)
209         assert(reconnect_source_id == 0);
211         /* try again in 5 seconds */
212         reconnect_source_id = g_timeout_add_seconds(5, timer_reconnect, NULL);
215 void
216 mpdclient_lost_callback(void)
218         assert(reconnect_source_id == 0);
220         screen_update(mpd);
222         reconnect_source_id = g_timeout_add_seconds(1, timer_reconnect, NULL);
225 /**
226  * This function is called by the gidle.c library when MPD sends us an
227  * idle event (or when the connection dies).
228  */
229 void
230 mpdclient_idle_callback(gcc_unused enum mpd_idle events)
232 #ifndef NCMPC_MINI
233         if (options.enable_xterm_title)
234                 update_xterm_title();
235 #endif
237         screen_update(mpd);
238         auto_update_timer();
241 static gboolean
242 timer_mpd_update(gcc_unused gpointer data)
244         do_mpd_update();
246         if (should_enable_update_timer())
247                 return true;
248         else {
249                 update_source_id = 0;
250                 return false;
251         }
254 void begin_input_event(void)
258 void end_input_event(void)
260         screen_update(mpd);
261         mpd->events = 0;
263         auto_update_timer();
266 bool
267 do_input_event(command_t cmd)
269         if (cmd == CMD_QUIT) {
270                 g_main_loop_quit(main_loop);
271                 return false;
272         }
274         screen_cmd(mpd, cmd);
276         if (cmd == CMD_VOLUME_UP || cmd == CMD_VOLUME_DOWN)
277                 /* make sure we don't update the volume yet */
278                 disable_update_timer();
280         return true;
283 #ifndef NCMPC_MINI
284 /**
285  * Check the configured key bindings for errors, and display a status
286  * message every 10 seconds.
287  */
288 static gboolean
289 timer_check_key_bindings(gcc_unused gpointer data)
291         char buf[256];
293         if (check_key_bindings(NULL, buf, sizeof(buf))) {
294                 /* no error: disable this timer for the rest of this
295                    process */
296                 check_key_bindings_source_id = 0;
297                 return FALSE;
298         }
300 #ifdef ENABLE_KEYDEF_SCREEN
301         g_strchomp(buf);
302         g_strlcat(buf, " (", sizeof(buf));
303         /* to translators: a key was bound twice in the key editor,
304            and this is a hint for the user what to press to correct
305            that */
306         char comment[64];
307         g_snprintf(comment, sizeof(comment), _("press %s for the key editor"),
308                    get_key_names(CMD_SCREEN_KEYDEF, false));
309         g_strlcat(buf, comment, sizeof(buf));
310         g_strlcat(buf, ")", sizeof(buf));
311 #endif
313         screen_status_printf("%s", buf);
315         doupdate();
316         return TRUE;
318 #endif
320 int
321 main(int argc, const char *argv[])
323 #ifdef ENABLE_LOCALE
324 #ifndef ENABLE_NLS
325         gcc_unused
326 #endif
327         const char *charset = NULL;
328         /* time and date formatting */
329         setlocale(LC_TIME,"");
330         /* care about sorting order etc */
331         setlocale(LC_COLLATE,"");
332         /* charset */
333         setlocale(LC_CTYPE,"");
334         /* initialize charset conversions */
335         charset = charset_init();
337         /* initialize i18n support */
338 #endif
340 #ifdef ENABLE_NLS
341         setlocale(LC_MESSAGES, "");
342         bindtextdomain(GETTEXT_PACKAGE, LOCALE_DIR);
343 #ifdef ENABLE_LOCALE
344         bind_textdomain_codeset(GETTEXT_PACKAGE, charset);
345 #endif
346         textdomain(GETTEXT_PACKAGE);
347 #endif
349         /* initialize options */
350         options_init();
352         /* parse command line options - 1 pass get configuration files */
353         options_parse(argc, argv);
355 #ifndef NCMPC_MINI
356         /* read configuration */
357         read_configuration();
359         /* check key bindings */
360         check_key_bindings(NULL, NULL, 0);
361 #endif
363         /* parse command line options - 2 pass */
364         options_parse(argc, argv);
366         ncu_init();
368 #ifdef ENABLE_LYRICS_SCREEN
369         lyrics_init();
370 #endif
372         /* create mpdclient instance */
373         mpd = mpdclient_new(options.host, options.port,
374                             options.timeout_ms,
375                             options.password);
377         /* initialize curses */
378         screen_init(mpd);
380         /* the main loop */
381         main_loop = g_main_loop_new(NULL, FALSE);
383         /* watch out for keyboard input */
384         keyboard_init();
386         /* watch out for lirc input */
387         ncmpc_lirc_init();
389         signals_init(main_loop, mpd);
391         /* attempt to connect */
392         reconnect_source_id = g_idle_add(timer_reconnect, NULL);
394         auto_update_timer();
396 #ifndef NCMPC_MINI
397         check_key_bindings_source_id =
398                 g_timeout_add_seconds(10, timer_check_key_bindings, NULL);
399 #endif
401         screen_paint(mpd, true);
403         g_main_loop_run(main_loop);
404         g_main_loop_unref(main_loop);
406         /* cleanup */
408         cancel_seek_timer();
410         disable_update_timer();
412         if (reconnect_source_id != 0)
413                 g_source_remove(reconnect_source_id);
415 #ifndef NCMPC_MINI
416         if (check_key_bindings_source_id != 0)
417                 g_source_remove(check_key_bindings_source_id);
418 #endif
420         signals_deinit();
421         ncmpc_lirc_deinit();
423         screen_exit();
424 #ifndef NCMPC_MINI
425         set_xterm_title("");
426 #endif
427         printf("\n");
429         mpdclient_free(mpd);
431 #ifdef ENABLE_LYRICS_SCREEN
432         lyrics_deinit();
433 #endif
435         ncu_deinit();
436         options_deinit();
438         return 0;