Code

configure.ac: disable -Wdeclaration-after-statement
[ncmpc.git] / src / screen.c
index d9e7fdbe86ed0510a61ffd7d856e652b2e678a0b..9bbfda742ff2a32afd7c82d78ac26cd03e02af86 100644 (file)
@@ -1,5 +1,5 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (c) 2004-2010 The Music Player Daemon Project
  * Project homepage: http://musicpd.org
 
  * This program is free software; you can redistribute it and/or modify
@@ -21,6 +21,7 @@
 #include "screen_interface.h"
 #include "screen_list.h"
 #include "screen_utils.h"
+#include "screen_message.h"
 #include "config.h"
 #include "i18n.h"
 #include "charset.h"
 #include "options.h"
 #include "colors.h"
 #include "player_command.h"
+#include "screen_help.h"
+#include "screen_queue.h"
+#include "screen_file.h"
+#include "screen_artist.h"
+#include "screen_search.h"
+#include "screen_song.h"
+#include "screen_keydef.h"
+#include "screen_lyrics.h"
+#include "screen_outputs.h"
 
 #include <mpd/client.h>
 
 #include <stdlib.h>
 #include <unistd.h>
-#include <stdarg.h>
 #include <string.h>
 #include <time.h>
 #include <locale.h>
@@ -50,13 +59,9 @@ static const int SCREEN_MIN_ROWS = 5;
 
 /* screens */
 
-#ifndef NCMPC_MINI
-static gboolean welcome = TRUE;
-#endif
-
 struct screen screen;
-static const struct screen_functions *mode_fn = &screen_playlist;
-static const struct screen_functions *mode_fn_prev = &screen_playlist;
+static const struct screen_functions *mode_fn = &screen_queue;
+static const struct screen_functions *mode_fn_prev = &screen_queue;
 
 gboolean
 screen_is_visible(const struct screen_functions *sf)
@@ -142,18 +147,6 @@ screen_next_mode(struct mpdclient *c, int offset)
                screen_switch(sf, c);
 }
 
-static inline int
-volume_length(int volume)
-{
-       if (volume == 100)
-               return 3;
-       if (volume >= 10 && volume < 100)
-               return 2;
-       if (volume >= 0 && volume < 10)
-               return 1;
-       return -1;
-}
-
 static void
 paint_top_window(const char *header, const struct mpdclient *c)
 {
@@ -161,23 +154,23 @@ paint_top_window(const char *header, const struct mpdclient *c)
 }
 
 static void
-paint_progress_window(struct mpdclient *c)
+update_progress_window(struct mpdclient *c, bool repaint)
 {
        unsigned elapsed, duration;
 
-       if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song))
+       if (c->status == NULL)
+               elapsed = 0;
+       else if (seek_id >= 0 && seek_id == mpd_status_get_song_id(c->status))
                elapsed = seek_target_time;
-       else if (c->status != NULL)
-               elapsed = mpd_status_get_elapsed_time(c->status);
        else
-               elapsed = 0;
+               elapsed = mpd_status_get_elapsed_time(c->status);
 
-       duration = c->status != NULL &&
-               !IS_STOPPED(mpd_status_get_state(c->status))
+       duration = mpdclient_is_playing(c)
                ? mpd_status_get_total_time(c->status)
                : 0;
 
-       if (progress_bar_set(&screen.progress_bar, elapsed, duration))
+       if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
+           repaint)
                progress_bar_paint(&screen.progress_bar);
 }
 
@@ -197,6 +190,11 @@ screen_exit(void)
        delwin(screen.main_window.w);
        progress_bar_deinit(&screen.progress_bar);
        status_bar_deinit(&screen.status_bar);
+
+#ifndef NCMPC_MINI
+       if (screen.welcome_source_id != 0)
+               g_source_remove(screen.welcome_source_id);
+#endif
 }
 
 void
@@ -244,24 +242,25 @@ screen_resize(struct mpdclient *c)
        screen_paint(c);
 }
 
-void
-screen_status_message(const char *msg)
+#ifndef NCMPC_MINI
+static gboolean
+welcome_timer_callback(gpointer data)
 {
-       status_bar_message(&screen.status_bar, msg);
-}
+       struct mpdclient *c = data;
 
-void
-screen_status_printf(const char *format, ...)
-{
-       char *msg;
-       va_list ap;
-
-       va_start(ap,format);
-       msg = g_strdup_vprintf(format,ap);
-       va_end(ap);
-       screen_status_message(msg);
-       g_free(msg);
+#ifndef NCMPC_MINI
+       screen.welcome_source_id = 0;
+#endif
+
+       paint_top_window(mode_fn->get_title != NULL
+                        ? mode_fn->get_title(screen.buf, screen.buf_size)
+                        : "",
+                        c);
+       doupdate();
+
+       return false;
 }
+#endif
 
 void
 screen_init(struct mpdclient *c)
@@ -277,7 +276,13 @@ screen_init(struct mpdclient *c)
        screen.buf  = g_malloc(screen.cols);
        screen.buf_size = screen.cols;
        screen.findbuf = NULL;
-       screen.start_timestamp = time(NULL);
+
+#ifndef NCMPC_MINI
+       if (options.welcome_screen_list)
+               screen.welcome_source_id =
+                       g_timeout_add(SCREEN_WELCOME_TIME * 1000,
+                                     welcome_timer_callback, c);
+#endif
 
        /* create top window */
        title_bar_init(&screen.title_bar, screen.cols, 0, 0);
@@ -336,7 +341,7 @@ screen_paint(struct mpdclient *c)
 
        /* paint the bottom window */
 
-       paint_progress_window(c);
+       update_progress_window(c, true);
        status_bar_paint(&screen.status_bar, c->status, c->song);
 
        /* paint the main window */
@@ -360,6 +365,7 @@ void
 screen_update(struct mpdclient *c)
 {
 #ifndef NCMPC_MINI
+       static bool was_connected;
        static bool initialized = false;
        static bool repeat;
        static bool random_enabled;
@@ -418,25 +424,23 @@ screen_update(struct mpdclient *c)
                crossfade = mpd_status_get_crossfade(c->status);
        }
 
-       if (c->events & MPD_IDLE_DATABASE)
+       if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
+           mpdclient_is_connected(c))
                screen_status_printf(_("Database updated"));
+       was_connected = mpdclient_is_connected(c);
 
        /* update title/header window */
-       if (welcome && options.welcome_screen_list &&
-           time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME)
+       if (screen.welcome_source_id != 0)
                paint_top_window("", c);
        else
 #endif
        if (mode_fn->get_title != NULL) {
                paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c);
-#ifndef NCMPC_MINI
-               welcome = FALSE;
-#endif
        } else
                paint_top_window("", c);
 
        /* update progress window */
-       paint_progress_window(c);
+       update_progress_window(c, false);
 
        /* update status window */
        status_bar_paint(&screen.status_bar, c->status, c->song);
@@ -482,7 +486,10 @@ void
 screen_cmd(struct mpdclient *c, command_t cmd)
 {
 #ifndef NCMPC_MINI
-       welcome = FALSE;
+       if (screen.welcome_source_id != 0) {
+               g_source_remove(screen.welcome_source_id);
+               screen.welcome_source_id = 0;
+       }
 #endif
 
        if (mode_fn->cmd != NULL && mode_fn->cmd(c, cmd))
@@ -514,7 +521,7 @@ screen_cmd(struct mpdclient *c, command_t cmd)
                screen_next_mode(c, 1);
                break;
        case CMD_SCREEN_PLAY:
-               screen_switch(&screen_playlist, c);
+               screen_switch(&screen_queue, c);
                break;
        case CMD_SCREEN_FILE:
                screen_switch(&screen_browse, c);