Code

screen: remove unnecessary paint calls from screen_resize()
[ncmpc.git] / src / screen.c
index 4059ac24fbb4168c438d24689529b1f99b707fd4..793eec93a0ddbe2e3ac9548bf97302e016ed8d73 100644 (file)
@@ -1,5 +1,6 @@
-/* 
- * (c) 2004 by Kalle Wallin (kaw@linux.se)
+/* ncmpc (Ncurses MPD Client)
+ * (c) 2004-2017 The Music Player Daemon Project
+ * Project homepage: http://musicpd.org
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
+#include "screen.h"
+#include "screen_interface.h"
+#include "screen_list.h"
+#include "screen_utils.h"
+#include "screen_status.h"
+#include "config.h"
+#include "i18n.h"
+#include "charset.h"
+#include "mpdclient.h"
+#include "utils.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 "screen_chat.h"
+
+#include <mpd/client.h>
+
 #include <stdlib.h>
 #include <unistd.h>
-#include <stdarg.h>
 #include <string.h>
 #include <time.h>
-//#include <signal.h>
 #include <locale.h>
-#include <glib.h>
-#include <ncurses.h>
 
-#include "config.h"
-#include "libmpdclient.h"
-#include "mpc.h"
-#include "command.h"
-#include "options.h"
-#include "colors.h"
-#include "screen.h"
-#include "screen_play.h"
-#include "screen_file.h"
-#include "screen_help.h"
-#include "screen_search.h"
-#include "screen_utils.h"
+#ifndef NCMPC_MINI
+/** welcome message time [s] */
+static const GTime SCREEN_WELCOME_TIME = 10;
+#endif
 
-#undef  ENABLE_STATUS_LINE_CLOCK
-#define ENABLE_SCROLLING
+/* minimum window size */
+static const int SCREEN_MIN_COLS = 14;
+static const int SCREEN_MIN_ROWS = 5;
 
-#define DEFAULT_CROSSFADE_TIME 10
+/* screens */
 
-#define STATUS_MESSAGE_TIMEOUT 3
-#define STATUS_LINE_MAX_SIZE   512
+struct screen screen;
+static const struct screen_functions *mode_fn = &screen_queue;
+static const struct screen_functions *mode_fn_prev = &screen_queue;
 
-#ifdef ENABLE_KEYDEF_SCREEN
-extern screen_functions_t *get_screen_keydef(void);
-#endif
+gboolean
+screen_is_visible(const struct screen_functions *sf)
+{
+       return sf == mode_fn;
+}
+
+void
+screen_switch(const struct screen_functions *sf, struct mpdclient *c)
+{
+       assert(sf != NULL);
 
-static screen_t *screen = NULL;
-static screen_functions_t *mode_fn = NULL;
+       if (sf == mode_fn)
+               return;
 
-static void
-switch_screen_mode(screen_mode_t new_mode, mpd_client_t *c)
+       mode_fn_prev = mode_fn;
+
+       /* close the old mode */
+       if (mode_fn->close != NULL)
+               mode_fn->close();
+
+       /* get functions for the new mode */
+       mode_fn = sf;
+
+       /* open the new mode */
+       if (mode_fn->open != NULL)
+               mode_fn->open(c);
+
+       screen_paint(c);
+}
+
+void
+screen_swap(struct mpdclient *c, const struct mpd_song *song)
 {
-  if( new_mode == screen->mode )
-    return;
-
-  /* close the old mode */
-  if( mode_fn && mode_fn->close )
-    mode_fn->close();
-
-  /* get functions for the new mode */
-  switch(new_mode)
-    {
-    case SCREEN_PLAY_WINDOW:
-      mode_fn = get_screen_playlist();
-      break;
-    case SCREEN_FILE_WINDOW:
-      mode_fn = get_screen_file();
-      break;
-    case SCREEN_HELP_WINDOW:
-      mode_fn = get_screen_help();
-      break;
-#ifdef ENABLE_KEYDEF_SCREEN
-    case SCREEN_KEYDEF_WINDOW:
-      mode_fn = get_screen_keydef();
-      break;
+       if (song != NULL)
+       {
+               if (false)
+                       { /* just a hack to make the ifdefs less ugly */ }
+#ifdef ENABLE_SONG_SCREEN
+               if (mode_fn_prev == &screen_song)
+                       screen_song_switch(c, song);
 #endif
-    default:
-      break;
-    }
+#ifdef ENABLE_LYRICS_SCREEN
+               else if (mode_fn_prev == &screen_lyrics)
+                       screen_lyrics_switch(c, song, true);
+#endif
+               else
+                       screen_switch(mode_fn_prev, c);
+       }
+       else
+               screen_switch(mode_fn_prev, c);
+}
 
- screen->mode = new_mode;
- screen->painted = 0;
+static int
+find_configured_screen(const char *name)
+{
+       unsigned i;
 
- /* open the new mode */
if( mode_fn && mode_fn->open )
-   mode_fn->open(screen, c);
+       for (i = 0; options.screen_list[i] != NULL; ++i)
              if (strcmp(options.screen_list[i], name) == 0)
+                       return i;
 
+       return -1;
 }
 
 static void
-paint_top_window(char *header, mpd_client_t *c, int clear)
+screen_next_mode(struct mpdclient *c, int offset)
 {
-  char flags[4];
-  static int prev_volume = -1;
-  static int prev_header_len = -1;
-  WINDOW *w = screen->top_window.w;
-
-  if(prev_header_len!=strlen(header))
-    {
-      prev_header_len = strlen(header);
-      clear = 1;
-    }
-
-  if(clear)
-    {
-      wmove(w, 0, 0);
-      wclrtoeol(w);
-    }
-
-  if(prev_volume!=c->status->volume || clear)
-    {
-      char buf[32];
-
-      if( header[0] )
-       {
-         colors_use(w, COLOR_TITLE_BOLD);
-         mvwaddstr(w, 0, 0, header);
-       }
-      else
-       {
-         colors_use(w, COLOR_TITLE_BOLD);
-         waddstr(w, get_key_names(CMD_SCREEN_HELP, FALSE));
-         colors_use(w, COLOR_TITLE);
-         waddstr(w, ":Help  ");
-         colors_use(w, COLOR_TITLE_BOLD);
-         waddstr(w, get_key_names(CMD_SCREEN_PLAY, FALSE));
-         colors_use(w, COLOR_TITLE);
-         waddstr(w, ":Playlist  ");
-         colors_use(w, COLOR_TITLE_BOLD);
-         waddstr(w, get_key_names(CMD_SCREEN_FILE, FALSE));
-         colors_use(w, COLOR_TITLE);
-         waddstr(w, ":Browse");
-       }
-      if( c->status->volume==MPD_STATUS_NO_VOLUME )
-       {
-         snprintf(buf, 32, "Volume n/a ");
-       }
-      else
-       {
-         snprintf(buf, 32, " Volume %d%%", c->status->volume); 
-       }
-      colors_use(w, COLOR_TITLE);
-      mvwaddstr(w, 0, screen->top_window.cols-strlen(buf), buf);
-
-      flags[0] = 0;
-      if( c->status->repeat )
-       strcat(flags, "r");
-      if( c->status->random )
-       strcat(flags, "z");
-      if( c->status->crossfade )
-       strcat(flags, "x");
-      if( c->status->updatingDb )
-       strcat(flags, "U");
-      colors_use(w, COLOR_LINE);
-      mvwhline(w, 1, 0, ACS_HLINE, screen->top_window.cols);
-      if( flags[0] )
-       {
-         wmove(w,1,screen->top_window.cols-strlen(flags)-3);
-         waddch(w, '[');
-         colors_use(w, COLOR_LINE_BOLD);
-         waddstr(w, flags);
-         colors_use(w, COLOR_LINE);
-         waddch(w, ']');
-       }
-      wnoutrefresh(w);
-    }
+       int max = g_strv_length(options.screen_list);
+
+       /* find current screen */
+       int current = find_configured_screen(screen_get_name(mode_fn));
+       int next = current + offset;
+       if (next<0)
+               next = max-1;
+       else if (next>=max)
+               next = 0;
+
+       const struct screen_functions *sf =
+               screen_lookup_name(options.screen_list[next]);
+       if (sf != NULL)
+               screen_switch(sf, c);
 }
 
 static void
-paint_progress_window(mpd_client_t *c)
+paint_top_window(const char *header, const struct mpdclient *c)
 {
-  double p;
-  int width;
-  int elapsedTime = c->status->elapsedTime;
-
-  if( c->status==NULL || IS_STOPPED(c->status->state) )
-    {
-      mvwhline(screen->progress_window.w, 0, 0, ACS_HLINE, 
-              screen->progress_window.cols);
-      wnoutrefresh(screen->progress_window.w);
-      return;
-    }
-
-  if( c->seek_song_id == c->song_id )
-    elapsedTime = c->seek_target_time;
-
-  p = ((double) elapsedTime) / ((double) c->status->totalTime);
-  
-  width = (int) (p * (double) screen->progress_window.cols);
-  mvwhline(screen->progress_window.w, 
-          0, 0,
-          ACS_HLINE, 
-          screen->progress_window.cols);
-  whline(screen->progress_window.w, '=', width-1);
-  mvwaddch(screen->progress_window.w, 0, width-1, 'O');
-  wnoutrefresh(screen->progress_window.w);
+       title_bar_paint(&screen.title_bar, header, c->status);
 }
 
-#ifdef ENABLE_SCROLLING
-static char *
-scroll_string(char *str, char *sep, int width)
+static void
+update_progress_window(struct mpdclient *c, bool repaint)
 {
-  static int offset = 0;
-  static time_t t = 0;
-  char *tmp, *buf;
-  size_t len;
-
-  if( offset==0 )
-    {
-      offset++;
-      return g_strdup(str);
-    }
-  /* create a buffer containing the string and the separator */
-  tmp = g_malloc(strlen(str)+strlen(sep)+1);
-  strcpy(tmp, str);
-  strcat(tmp, sep);
-  len = strlen(tmp);
-
-  if( offset >= len )
-   offset = 0;
-  
-  /* create the new scrolled string */
-  buf = g_malloc(width+1);
-  strncpy(buf, tmp+offset, width);
-  if( strlen(buf) < width )
-    strncat(buf, tmp, width-strlen(buf));
-
-  if( time(NULL)-t >= 1 )
-    {
-      t = time(NULL);
-      offset++;
-    }
-  g_free(tmp);
-  return buf;
+       unsigned elapsed;
+       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
+               elapsed = mpd_status_get_elapsed_time(c->status);
+
+       unsigned duration = mpdclient_is_playing(c)
+               ? mpd_status_get_total_time(c->status)
+               : 0;
+
+       if (progress_bar_set(&screen.progress_bar, elapsed, duration) ||
+           repaint)
+               progress_bar_paint(&screen.progress_bar);
 }
+
+void
+screen_exit(void)
+{
+       if (mode_fn->close != NULL)
+               mode_fn->close();
+
+       screen_list_exit();
+
+       string_list_free(screen.find_history);
+       g_free(screen.buf);
+       g_free(screen.findbuf);
+
+       title_bar_deinit(&screen.title_bar);
+       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
+}
 
-static void 
-paint_status_window(mpd_client_t *c)
+void
+screen_resize(struct mpdclient *c)
 {
-  WINDOW *w = screen->status_window.w;
-  mpd_Status *status = c->status;
-  mpd_Song *song   = c->song;
-  int elapsedTime = c->status->elapsedTime;
-  int x = 0;
-
-  if( time(NULL) - screen->status_timestamp <= STATUS_MESSAGE_TIMEOUT )
-    return;
-  
-  
-  wmove(w, 0, 0);
-  wclrtoeol(w);
-  colors_use(w, COLOR_STATUS_BOLD);
-  
-  switch(status->state)
-    {
-    case MPD_STATUS_STATE_STOP:
-      waddstr(w, "Stopped! ");
-      break;
-    case MPD_STATUS_STATE_PLAY:
-      waddstr(w, "Playing:");
-      break;
-    case MPD_STATUS_STATE_PAUSE:
-      waddstr(w, "[Paused]");
-      break;
-    default:
-      break;
-    }
-  x += 9;
-
-  /* create time string */
-  memset(screen->buf, 0, screen->buf_size);
-  if( IS_PLAYING(status->state) || IS_PAUSED(status->state) )
-    {
-      if( status->totalTime > 0 )
-       {
-         if( c->seek_song_id == c->song_id )
-           elapsedTime = c->seek_target_time;
-         snprintf(screen->buf, screen->buf_size, 
-                  " [%i:%02i/%i:%02i]",
-                  elapsedTime/60, elapsedTime%60,
-                  status->totalTime/60,   status->totalTime%60 );
-       }
-      else
-       {
-         snprintf(screen->buf, screen->buf_size,  " [%d kbps]", status->bitRate );
-       }
-    }
-  else
-    {
-      time_t timep;
-
-      time(&timep);
-      /* Note: setlocale(LC_TIME,"") should be used first */
-      //strftime(screen->buf, screen->buf_size, "%X ",  localtime(&timep));
-      strftime(screen->buf, screen->buf_size, " %k:%M",  localtime(&timep));
-    }
-
-  /* display song */
-  if( (IS_PLAYING(status->state) || IS_PAUSED(status->state)) &&  song )
-    {
-      char *songname = mpc_get_song_name(song);
-      int width = COLS-x-strlen(screen->buf);
-
-      colors_use(w, COLOR_STATUS);
-#ifdef ENABLE_SCROLLING
-      if( strlen(songname) > width )
-       {
-         char *tmp = scroll_string(songname, " *** ", width);
-         strcpy(songname, tmp);
-         g_free(tmp);
+       if (COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS) {
+               screen_exit();
+               fprintf(stderr, "%s\n", _("Error: Screen too small"));
+               exit(EXIT_FAILURE);
        }
-#endif
-      mvwaddnstr(w, 0, x, songname, width);
-    } 
-
-  /* distplay time string */
-  if( screen->buf[0] )
-    {
-      x = screen->status_window.cols - strlen(screen->buf);
-      colors_use(w, COLOR_STATUS_TIME);
-      mvwaddstr(w, 0, x, screen->buf);
-    }
-
-#ifdef ENABLE_STATUS_LINE_CLOCK
-  else if( c->status->state == MPD_STATUS_STATE_STOP )
-    {
-      time_t timep;
-
-      /* Note: setlocale(LC_TIME,"") should be used first */
-      time(&timep);
-      strftime(screen->buf, screen->buf_size, "%X ",  localtime(&timep));
-      x = screen->status_window.cols - strlen(screen->buf) ;
-      colors_use(w, COLOR_STATUS_TIME);
-      mvwaddstr(w, 0, x, screen->buf);
-    }
+#ifdef PDCURSES
+       resize_term(LINES, COLS);
+#else 
+       resizeterm(LINES, COLS);
 #endif
 
-  wnoutrefresh(w);
-}
+       screen.cols = COLS;
+       screen.rows = LINES;
 
+       title_bar_resize(&screen.title_bar, screen.cols);
 
+       /* main window */
+       screen.main_window.cols = screen.cols;
+       screen.main_window.rows = screen.rows-4;
+       wresize(screen.main_window.w, screen.main_window.rows, screen.cols);
 
-int
-screen_exit(void)
-{
-  endwin();
-  if( screen )
-    {
-      GList *list = g_list_first(screen->screen_list);
+       /* progress window */
+       progress_bar_resize(&screen.progress_bar, screen.cols,
+                           screen.rows - 2, 0);
 
-      /* close and exit all screens (playlist,browse,help...) */
-      while( list )
-       {
-         screen_functions_t *mode_fn = list->data;
-
-         if( mode_fn && mode_fn->close )
-           mode_fn->close();
-         if( mode_fn && mode_fn->exit )
-           mode_fn->exit();
-         list->data = NULL;
-         list=list->next;
-       }
-      g_list_free(screen->screen_list);
-
-      g_free(screen->buf);
-      g_free(screen->findbuf);
-      g_free(screen);
-      screen = NULL;
-    }
-  return 0;
+       /* status window */
+       status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0);
+
+       screen.buf_size = screen.cols;
+       g_free(screen.buf);
+       screen.buf = g_malloc(screen.cols);
+
+       /* resize all screens */
+       screen_list_resize(screen.main_window.cols, screen.main_window.rows);
+
+       /* ? - without this the cursor becomes visible with aterm & Eterm */
+       curs_set(1);
+       curs_set(0);
+
+       screen_paint(c);
 }
 
-void
-screen_resize(void)
+#ifndef NCMPC_MINI
+static gboolean
+welcome_timer_callback(gpointer data)
 {
-  GList *list;
+       struct mpdclient *c = data;
 
-#ifdef DEBUG
-  fprintf(stderr, "Resize rows %d->%d, cols %d->%d\n",
-         screen->rows, LINES,
-         screen->cols, COLS);
+#ifndef NCMPC_MINI
+       screen.welcome_source_id = 0;
 #endif
-      
-  if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
-    {
-      screen_exit();
-      fprintf(stderr, "Error: Screen to small!\n");
-      exit(EXIT_FAILURE);
-    }
-
-  resizeterm(LINES, COLS);
-
-  screen->cols = COLS;
-  screen->rows = LINES;
-
-  /* top window */
-  screen->top_window.cols = screen->cols;
-  wresize(screen->top_window.w, 2, screen->cols);
-
-  /* main window */
-  screen->main_window.cols = screen->cols;
-  screen->main_window.rows = screen->rows-4;
-  wresize(screen->main_window.w, screen->main_window.rows, screen->cols);
-  wclear(screen->main_window.w);
-
-  /* progress window */
-  screen->progress_window.cols = screen->cols;
-  wresize(screen->progress_window.w, 1, screen->cols);
-  mvwin(screen->progress_window.w, screen->rows-2, 0);
-
-  /* status window */
-  screen->status_window.cols = screen->cols;
-  wresize(screen->status_window.w, 1, screen->cols);
-  mvwin(screen->status_window.w, screen->rows-1, 0);
-
-  screen->buf_size = screen->cols;
-  g_free(screen->buf);
-  screen->buf = g_malloc(screen->cols);
-
-  list = g_list_first(screen->screen_list);
-  while( list )
-    {
-      screen_functions_t *mode_fn = list->data;
-
-      if( mode_fn && mode_fn->resize )
-       mode_fn->resize(screen->main_window.cols, screen->main_window.rows);
-
-      list=list->next;
-    }
-
-  /* ? - without this the cursor becomes visible with aterm & Eterm */
-  curs_set(1);
-  curs_set(0);     
-
-  screen->painted = 0;
-}
 
-void 
-screen_status_message(char *msg)
-{
-  WINDOW *w = screen->status_window.w;
-
-  wmove(w, 0, 0);
-  wclrtoeol(w);
-  colors_use(w, COLOR_STATUS_ALERT);
-  waddstr(w, msg);
-  wnoutrefresh(w);
-  screen->status_timestamp = time(NULL);
-}
+       paint_top_window(mode_fn->get_title != NULL
+                        ? mode_fn->get_title(screen.buf, screen.buf_size)
+                        : "",
+                        c);
+       doupdate();
 
-void 
-screen_status_printf(char *format, ...)
-{
-  char buffer[STATUS_LINE_MAX_SIZE];
-  va_list ap;
-  
-  va_start(ap,format);
-  vsnprintf(buffer,sizeof(buffer),format,ap);
-  va_end(ap);
-  screen_status_message(buffer);
+       return false;
 }
+#endif
 
-int
-screen_init(void)
+void
+screen_init(struct mpdclient *c)
 {
-  GList *list;
-
-  /* initialize the curses library */
-  initscr();
-  /* initialize color support */
-  colors_start();
-  /* tell curses not to do NL->CR/NL on output */
-  nonl();          
-  /* take input chars one at a time, no wait for \n */  
-  cbreak();       
-  /* don't echo input */
-  noecho();    
-  /* set cursor invisible */     
-  curs_set(0);     
-  /* return from getch() without blocking */
-  //  nodelay(stdscr, TRUE); 
-  keypad(stdscr, TRUE);  
-  timeout(SCREEN_TIMEOUT);
-
-  if( COLS<SCREEN_MIN_COLS || LINES<SCREEN_MIN_ROWS )
-    {
-      fprintf(stderr, "Error: Screen to small!\n");
-      exit(EXIT_FAILURE);
-    }
-
-  screen = g_malloc(sizeof(screen_t));
-  memset(screen, 0, sizeof(screen_t));
-  screen->mode = SCREEN_PLAY_WINDOW;
-  screen->cols = COLS;
-  screen->rows = LINES;
-  screen->buf  = g_malloc(screen->cols);
-  screen->buf_size = screen->cols;
-  screen->findbuf = NULL;
-  screen->painted = 0;
-  screen->start_timestamp = time(NULL);
-  screen->input_timestamp = time(NULL);
-  screen->last_cmd = CMD_NONE;
-
-  /* create top window */
-  screen->top_window.rows = 2;
-  screen->top_window.cols = screen->cols;
-  screen->top_window.w = newwin(screen->top_window.rows, 
-                                 screen->top_window.cols,
-                                 0, 0);
-  leaveok(screen->top_window.w, TRUE);
-  keypad(screen->top_window.w, TRUE);  
-
-  /* create main window */
-  screen->main_window.rows = screen->rows-4;
-  screen->main_window.cols = screen->cols;
-  screen->main_window.w = newwin(screen->main_window.rows, 
-                                screen->main_window.cols,
-                                2, 
-                                0);
-
-  //  leaveok(screen->main_window.w, TRUE); temporary disabled
-  keypad(screen->main_window.w, TRUE);  
-
-  /* create progress window */
-  screen->progress_window.rows = 1;
-  screen->progress_window.cols = screen->cols;
-  screen->progress_window.w = newwin(screen->progress_window.rows, 
-                                    screen->progress_window.cols,
-                                    screen->rows-2, 
-                                    0);
-  leaveok(screen->progress_window.w, TRUE);
-  
-  /* create status window */
-  screen->status_window.rows = 1;
-  screen->status_window.cols = screen->cols;
-  screen->status_window.w = newwin(screen->status_window.rows, 
-                                  screen->status_window.cols,
-                                  screen->rows-1, 
-                                  0);
-
-  leaveok(screen->status_window.w, FALSE);
-  keypad(screen->status_window.w, TRUE);  
-
-  if( options.enable_colors )
-    {
-      /* set background attributes */
-      wbkgd(stdscr, COLOR_PAIR(COLOR_LIST)); 
-      wbkgd(screen->main_window.w,     COLOR_PAIR(COLOR_LIST));
-      wbkgd(screen->top_window.w,      COLOR_PAIR(COLOR_TITLE));
-      wbkgd(screen->progress_window.w, COLOR_PAIR(COLOR_PROGRESSBAR));
-      wbkgd(screen->status_window.w,   COLOR_PAIR(COLOR_STATUS));
-      colors_use(screen->progress_window.w, COLOR_PROGRESSBAR);
-    }
-
-  /* initialize screens */
-  screen->screen_list = NULL;
-  screen->screen_list = g_list_append(screen->screen_list, 
-                                     (gpointer) get_screen_playlist());
-  screen->screen_list = g_list_append(screen->screen_list, 
-                                     (gpointer) get_screen_file());
-  screen->screen_list = g_list_append(screen->screen_list, 
-                                     (gpointer) get_screen_help());
-#ifdef ENABLE_KEYDEF_SCREEN
-  screen->screen_list = g_list_append(screen->screen_list, 
-                                     (gpointer) get_screen_keydef());
+       if (COLS < SCREEN_MIN_COLS || LINES < SCREEN_MIN_ROWS) {
+               fprintf(stderr, "%s\n", _("Error: Screen too small"));
+               exit(EXIT_FAILURE);
+       }
+
+       screen.cols = COLS;
+       screen.rows = LINES;
+
+       screen.buf  = g_malloc(screen.cols);
+       screen.buf_size = screen.cols;
+       screen.findbuf = 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);
+
+       /* create main window */
+       window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0);
+
+       if (!options.hardware_cursor)
+               leaveok(screen.main_window.w, TRUE);
+
+       keypad(screen.main_window.w, TRUE);
+
+       /* create progress window */
+       progress_bar_init(&screen.progress_bar, screen.cols,
+                         screen.rows - 2, 0);
+
+       /* create status window */
+       status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0);
+
+#ifdef ENABLE_COLORS
+       if (options.enable_colors) {
+               /* set background attributes */
+               wbkgd(stdscr, COLOR_PAIR(COLOR_LIST));
+               wbkgd(screen.main_window.w,     COLOR_PAIR(COLOR_LIST));
+               wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE));
+               wbkgd(screen.progress_bar.window.w,
+                     COLOR_PAIR(COLOR_PROGRESSBAR));
+               wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS));
+               colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR);
+       }
 #endif
 
-  list = screen->screen_list;
-  while( list )
-    {
-      screen_functions_t *fn = list->data;
-      
-      if( fn && fn->init )
-       fn->init(screen->main_window.w, 
-                screen->main_window.cols,
-                screen->main_window.rows);
-      
-      list = list->next;
-    }
-
-  mode_fn = get_screen_playlist();
-
-  return 0;
+       /* initialize screens */
+       screen_list_init(screen.main_window.w,
+                        screen.main_window.cols, screen.main_window.rows);
+
+       if (mode_fn->open != NULL)
+               mode_fn->open(c);
 }
 
-void 
-screen_paint(mpd_client_t *c)
+static void
+screen_refresh(struct mpdclient *c, bool main_dirty)
 {
-  /* paint the title/header window */
-  if( mode_fn && mode_fn->get_title )
-    paint_top_window(mode_fn->get_title(), c, 1);
-  else
-    paint_top_window("", c, 1);
-
-  /* paint the main window */
-  if( mode_fn && mode_fn->paint )
-    mode_fn->paint(screen, c);
-  
-  paint_progress_window(c);
-  paint_status_window(c);
-  screen->painted = 1;
-  wmove(screen->main_window.w, 0, 0);  
-  wnoutrefresh(screen->main_window.w);
-
-  /* tell curses to update */
-  doupdate();
+       /* update title/header window */
+       const char *title =
+#ifndef NCMPC_MINI
+               screen.welcome_source_id == 0 &&
+#endif
+               mode_fn->get_title != NULL
+               ? mode_fn->get_title(screen.buf, screen.buf_size)
+               : "";
+       assert(title != NULL);
+       paint_top_window(title, c);
+
+       /* paint the bottom window */
+
+       update_progress_window(c, true);
+       status_bar_paint(&screen.status_bar, c->status, c->song);
+
+       /* paint the main window */
+
+       if (main_dirty) {
+               wclear(screen.main_window.w);
+               if (mode_fn->paint != NULL)
+                       mode_fn->paint();
+       }
+
+       /* move the cursor to the origin */
+
+       if (!options.hardware_cursor)
+               wmove(screen.main_window.w, 0, 0);
+
+       wnoutrefresh(screen.main_window.w);
+
+       /* tell curses to update */
+       doupdate();
 }
 
-void 
-screen_update(mpd_client_t *c)
+void
+screen_paint(struct mpdclient *c)
 {
-  static int repeat = -1;
-  static int random = -1;
-  static int crossfade = -1;
-  static int dbupdate = -1;
-  static int welcome = 1;
-  list_window_t *lw = NULL;
-
-  if( !screen->painted )
-    return screen_paint(c);
-
-  /* print a message if mpd status has changed */
-  if( repeat<0 )
-    {
-      repeat = c->status->repeat;
-      random = c->status->random;
-      crossfade = c->status->crossfade;
-      dbupdate = c->status->updatingDb;
-    }
-  if( repeat != c->status->repeat )
-    screen_status_printf("Repeat is %s", 
-                        c->status->repeat  ? "On" : "Off");
-  if( random != c->status->random )
-    screen_status_printf("Random is %s", 
-                        c->status->random ? "On" : "Off");
-  if( crossfade != c->status->crossfade )
-    screen_status_printf("Crossfade %d seconds", c->status->crossfade);
-  if( dbupdate && dbupdate != c->status->updatingDb )
-    screen_status_printf("Database updated!");
-
-  repeat = c->status->repeat;
-  random = c->status->random;
-  crossfade = c->status->crossfade;
-  dbupdate = c->status->updatingDb;
-
-  /* update title/header window */
-  if( welcome && screen->last_cmd==CMD_NONE &&
-      time(NULL)-screen->start_timestamp <= SCREEN_WELCOME_TIME)
-    paint_top_window("", c, 0);
-  else if( mode_fn && mode_fn->get_title )
-    {
-      paint_top_window(mode_fn->get_title(), c, 0);
-      welcome = 0;
-    }
-  else
-    paint_top_window("", c, 0);
-
-  /* update the main window */
-  if( mode_fn && mode_fn->paint )
-    mode_fn->update(screen, c);
-
-  if( mode_fn && mode_fn->get_lw )
-    lw = mode_fn->get_lw();
-
-  /* update progress window */
-  paint_progress_window(c);
-
-  /* update status window */
-  paint_status_window(c);
-
-  /* move the cursor to the selected row in the main window */
-  if( lw )
-    wmove(screen->main_window.w, LW_ROW(lw), 0);   
-  else
-    wmove(screen->main_window.w, 0, 0);   
-  wnoutrefresh(screen->main_window.w);
-
-  /* tell curses to update */
-  doupdate();
+       screen_refresh(c, true);
 }
 
 void
-screen_idle(mpd_client_t *c)
+screen_update(struct mpdclient *c)
 {
-  if( c->seek_song_id ==  c->song_id &&
-      (screen->last_cmd == CMD_SEEK_FORWARD || 
-       screen->last_cmd == CMD_SEEK_BACKWARD) )
-    {
-      mpd_sendSeekCommand(c->connection, 
-                         c->seek_song_id, 
-                         c->seek_target_time);
-      mpd_finishCommand(c->connection);
-    }
-
-  screen->last_cmd = CMD_NONE;
-  c->seek_song_id = -1;
+#ifndef NCMPC_MINI
+       static bool was_connected;
+       static bool initialized = false;
+       static bool repeat;
+       static bool random_enabled;
+       static bool single;
+       static bool consume;
+       static unsigned crossfade;
+
+       /* print a message if mpd status has changed */
+       if ((c->events & MPD_IDLE_OPTIONS) && c->status != NULL) {
+               if (!initialized) {
+                       repeat = mpd_status_get_repeat(c->status);
+                       random_enabled = mpd_status_get_random(c->status);
+                       single = mpd_status_get_single(c->status);
+                       consume = mpd_status_get_consume(c->status);
+                       crossfade = mpd_status_get_crossfade(c->status);
+                       initialized = true;
+               }
+
+               if (repeat != mpd_status_get_repeat(c->status))
+                       screen_status_printf(mpd_status_get_repeat(c->status) ?
+                                            _("Repeat mode is on") :
+                                            _("Repeat mode is off"));
+
+               if (random_enabled != mpd_status_get_random(c->status))
+                       screen_status_printf(mpd_status_get_random(c->status) ?
+                                            _("Random mode is on") :
+                                            _("Random mode is off"));
+
+               if (single != mpd_status_get_single(c->status))
+                       screen_status_printf(mpd_status_get_single(c->status) ?
+                                            /* "single" mode means
+                                               that MPD will
+                                               automatically stop
+                                               after playing one
+                                               single song */
+                                            _("Single mode is on") :
+                                            _("Single mode is off"));
+
+               if (consume != mpd_status_get_consume(c->status))
+                       screen_status_printf(mpd_status_get_consume(c->status) ?
+                                            /* "consume" mode means
+                                               that MPD removes each
+                                               song which has
+                                               finished playing */
+                                            _("Consume mode is on") :
+                                            _("Consume mode is off"));
+
+               if (crossfade != mpd_status_get_crossfade(c->status))
+                       screen_status_printf(_("Crossfade %d seconds"),
+                                            mpd_status_get_crossfade(c->status));
+
+               repeat = mpd_status_get_repeat(c->status);
+               random_enabled = mpd_status_get_random(c->status);
+               single = mpd_status_get_single(c->status);
+               consume = mpd_status_get_consume(c->status);
+               crossfade = mpd_status_get_crossfade(c->status);
+       }
+
+       if ((c->events & MPD_IDLE_DATABASE) != 0 && was_connected &&
+           mpdclient_is_connected(c))
+               screen_status_printf(_("Database updated"));
+       was_connected = mpdclient_is_connected(c);
+#endif
+
+       /* update the main window */
+       if (mode_fn->update != NULL)
+               mode_fn->update(c);
+
+       screen_refresh(c, false);
 }
 
-void 
-screen_cmd(mpd_client_t *c, command_t cmd)
+#ifdef HAVE_GETMOUSE
+int
+screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row)
 {
-  int n = 0;
-  screen_mode_t new_mode = screen->mode;
-
-  screen->input_timestamp = time(NULL);
-  screen->last_cmd = cmd;
-
-  if( mode_fn && mode_fn->cmd && mode_fn->cmd(screen, c, cmd) )
-    return;
-
-  switch(cmd)
-    {
-    case CMD_PLAY:
-      mpd_sendPlayCommand(c->connection, play_get_selected());
-      mpd_finishCommand(c->connection);
-      break;
-    case CMD_PAUSE:
-      mpd_sendPauseCommand(c->connection);
-      mpd_finishCommand(c->connection);
-      break;
-    case CMD_STOP:
-      mpd_sendStopCommand(c->connection);
-      mpd_finishCommand(c->connection);
-      break;
-    case CMD_SEEK_FORWARD:
-      if( !IS_STOPPED(c->status->state) )
-       {
-         if( c->seek_song_id != c->song_id )
-           {
-             c->seek_song_id = c->song_id;
-             c->seek_target_time = c->status->elapsedTime;
-           }
-         c->seek_target_time++;
-         if( c->seek_target_time < c->status->totalTime )
-           break;
-         c->seek_target_time=0;
-       }
-      /* fall through... */
-    case CMD_TRACK_NEXT:
-      if( !IS_STOPPED(c->status->state) )
-       {
-         mpd_sendNextCommand(c->connection);
-         mpd_finishCommand(c->connection);
-       }
-      break;
-    case CMD_SEEK_BACKWARD:
-      if( !IS_STOPPED(c->status->state) )
-       {
-         if( c->seek_song_id != c->song_id )
-           {
-             c->seek_song_id = c->song_id;
-             c->seek_target_time = c->status->elapsedTime;
-           }
-         c->seek_target_time--;
-         if( c->seek_target_time < 0 )
-           c->seek_target_time=0;
-       }
-      break;
-    case CMD_TRACK_PREVIOUS:
-      if( !IS_STOPPED(c->status->state) )
-       {
-         mpd_sendPrevCommand(c->connection);
-         mpd_finishCommand(c->connection);
-       }
-      break;   
-    case CMD_SHUFFLE:
-      mpd_sendShuffleCommand(c->connection);
-      mpd_finishCommand(c->connection);
-      screen_status_message("Shuffled playlist!");
-      break;
-    case CMD_CLEAR:
-      mpd_sendClearCommand(c->connection);
-      mpd_finishCommand(c->connection);
-      file_clear_highlights(c);
-      screen_status_message("Cleared playlist!");
-      break;
-    case CMD_REPEAT:
-      n = !c->status->repeat;
-      mpd_sendRepeatCommand(c->connection, n);
-      mpd_finishCommand(c->connection);
-      break;
-    case CMD_RANDOM:
-      n = !c->status->random;
-      mpd_sendRandomCommand(c->connection, n);
-      mpd_finishCommand(c->connection);
-      break;
-    case CMD_CROSSFADE:
-      if( c->status->crossfade )
-       n = 0;
-      else
-       n = DEFAULT_CROSSFADE_TIME;
-      mpd_sendCrossfadeCommand(c->connection, n);
-      mpd_finishCommand(c->connection);
-      break;
-    case CMD_DB_UPDATE:
-      if( !c->status->updatingDb )
-       {
-         mpd_sendUpdateCommand(c->connection);
-         n = mpd_getUpdateId(c->connection);
-         mpd_finishCommand(c->connection);
-         if( !mpc_error(c) )
-           screen_status_printf("Database update started [%d]", n);
-       }
-      else
-       screen_status_printf("Database update running...");
-      break;
-    case CMD_VOLUME_UP:
-      if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume<100 )
-       {
-         c->status->volume=c->status->volume+1;
-         mpd_sendSetvolCommand(c->connection, c->status->volume  );
-         mpd_finishCommand(c->connection);
-       }
-      break;
-    case CMD_VOLUME_DOWN:
-      if( c->status->volume!=MPD_STATUS_NO_VOLUME && c->status->volume>0 )
-       {
-         c->status->volume=c->status->volume-1;
-         mpd_sendSetvolCommand(c->connection, c->status->volume  );
-         mpd_finishCommand(c->connection);
-       }
-      break;
-    case CMD_TOGGLE_FIND_WRAP:
-      options.find_wrap = !options.find_wrap;
-      screen_status_printf("Find mode: %s", 
-                          options.find_wrap ? "Wrapped" : "Normal");
-      break;
-    case CMD_TOGGLE_AUTOCENTER:
-      options.auto_center = !options.auto_center;
-      screen_status_printf("Auto center mode: %s", 
-                          options.auto_center ? "On" : "Off");
-      break;
-    case CMD_SCREEN_PREVIOUS:
-      if( screen->mode > SCREEN_PLAY_WINDOW )
-       new_mode = screen->mode - 1;
-      else
-       new_mode = SCREEN_HELP_WINDOW-1;
-      switch_screen_mode(new_mode, c);
-      break;
-    case CMD_SCREEN_NEXT:
-      new_mode = screen->mode + 1;
-      if( new_mode >= SCREEN_HELP_WINDOW )
-       new_mode = SCREEN_PLAY_WINDOW;
-      switch_screen_mode(new_mode, c);
-      break;
-    case CMD_SCREEN_PLAY:
-      switch_screen_mode(SCREEN_PLAY_WINDOW, c);
-      break;
-    case CMD_SCREEN_FILE:
-      switch_screen_mode(SCREEN_FILE_WINDOW, c);
-      break;
-    case CMD_SCREEN_SEARCH:
-      switch_screen_mode(SCREEN_SEARCH_WINDOW, c);
-      break;
-    case CMD_SCREEN_HELP:
-      switch_screen_mode(SCREEN_HELP_WINDOW, c);
-      break;
-#ifdef ENABLE_KEYDEF_SCREEN 
-    case CMD_SCREEN_KEYDEF:
-      switch_screen_mode(SCREEN_KEYDEF_WINDOW, c);
-      break;
+       MEVENT event;
+
+       /* retrieve the mouse event from curses */
+#ifdef PDCURSES
+       nc_getmouse(&event);
+#else
+       getmouse(&event);
 #endif
-    case CMD_QUIT:
-      exit(EXIT_SUCCESS);
-    default:
-      break;
-    }
+       /* calculate the selected row in the list window */
+       *row = event.y - screen.title_bar.window.rows;
+       /* copy button state bits */
+       *bstate = event.bstate;
+       /* if button 2 was pressed switch screen */
+       if (event.bstate & BUTTON2_CLICKED) {
+               screen_cmd(c, CMD_SCREEN_NEXT);
+               return 1;
+       }
 
+       return 0;
 }
+#endif
 
+void
+screen_cmd(struct mpdclient *c, command_t cmd)
+{
+#ifndef NCMPC_MINI
+       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))
+               return;
+
+       if (handle_player_command(c, cmd))
+               return;
+
+       switch(cmd) {
+       case CMD_TOGGLE_FIND_WRAP:
+               options.find_wrap = !options.find_wrap;
+               screen_status_printf(options.find_wrap ?
+                                    _("Find mode: Wrapped") :
+                                    _("Find mode: Normal"));
+               break;
+       case CMD_TOGGLE_AUTOCENTER:
+               options.auto_center = !options.auto_center;
+               screen_status_printf(options.auto_center ?
+                                    _("Auto center mode: On") :
+                                    _("Auto center mode: Off"));
+               break;
+       case CMD_SCREEN_UPDATE:
+               screen_paint(c);
+               break;
+       case CMD_SCREEN_PREVIOUS:
+               screen_next_mode(c, -1);
+               break;
+       case CMD_SCREEN_NEXT:
+               screen_next_mode(c, 1);
+               break;
+       case CMD_SCREEN_PLAY:
+               screen_switch(&screen_queue, c);
+               break;
+       case CMD_SCREEN_FILE:
+               screen_switch(&screen_browse, c);
+               break;
+#ifdef ENABLE_HELP_SCREEN
+       case CMD_SCREEN_HELP:
+               screen_switch(&screen_help, c);
+               break;
+#endif
+#ifdef ENABLE_SEARCH_SCREEN
+       case CMD_SCREEN_SEARCH:
+               screen_switch(&screen_search, c);
+               break;
+#endif
+#ifdef ENABLE_ARTIST_SCREEN
+       case CMD_SCREEN_ARTIST:
+               screen_switch(&screen_artist, c);
+               break;
+#endif
+#ifdef ENABLE_SONG_SCREEN
+       case CMD_SCREEN_SONG:
+               screen_switch(&screen_song, c);
+               break;
+#endif
+#ifdef ENABLE_KEYDEF_SCREEN
+       case CMD_SCREEN_KEYDEF:
+               screen_switch(&screen_keydef, c);
+               break;
+#endif
+#ifdef ENABLE_LYRICS_SCREEN
+       case CMD_SCREEN_LYRICS:
+               screen_switch(&screen_lyrics, c);
+               break;
+#endif
+#ifdef ENABLE_OUTPUTS_SCREEN
+       case CMD_SCREEN_OUTPUTS:
+               screen_switch(&screen_outputs, c);
+               break;
+#endif
+#ifdef ENABLE_CHAT_SCREEN
+       case CMD_SCREEN_CHAT:
+               screen_switch(&screen_chat, c);
+               break;
+#endif
+       case CMD_SCREEN_SWAP:
+               screen_swap(c, NULL);
+               break;
 
+       default:
+               break;
+       }
+}