X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fscreen.c;h=a8c80bcee3897f0603fe6f934444af06798cdc50;hb=d50894768e12b434092ba2fc8bb88b4d0b816ef8;hp=bfba41befec02897ae01897c14fcdd44ae5ad613;hpb=cb11ae2f56f4caff86234c6437a9e6c699840250;p=ncmpc.git diff --git a/src/screen.c b/src/screen.c index bfba41b..a8c80bc 100644 --- a/src/screen.c +++ b/src/screen.c @@ -1,25 +1,27 @@ /* ncmpc (Ncurses MPD Client) - * (c) 2004-2009 The Music Player Daemon Project + * (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 * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - + * * This program is distributed in the hope that it will be useful, * 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., * 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" @@ -27,18 +29,22 @@ #include "utils.h" #include "options.h" #include "colors.h" -#include "strfsong.h" #include "player_command.h" - -#ifndef NCMPC_MINI -#include "hscroll.h" -#endif +#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 #include #include -#include #include #include #include @@ -54,13 +60,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) @@ -89,7 +91,7 @@ screen_switch(const struct screen_functions *sf, struct mpdclient *c) if (mode_fn->open != NULL) mode_fn->open(c); - screen_paint(c); + screen_paint(c, true); } void @@ -130,316 +132,56 @@ static void screen_next_mode(struct mpdclient *c, int offset) { int max = g_strv_length(options.screen_list); - int current, next; - const struct screen_functions *sf; /* find current screen */ - current = find_configured_screen(screen_get_name(mode_fn)); - next = current + offset; + 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; - sf = screen_lookup_name(options.screen_list[next]); + const struct screen_functions *sf = + screen_lookup_name(options.screen_list[next]); if (sf != NULL) screen_switch(sf, c); } -#ifndef NCMPC_MINI static void -print_hotkey(WINDOW *w, command_t cmd, const char *label) +paint_top_window(const struct mpdclient *c) { - colors_use(w, COLOR_TITLE_BOLD); - waddstr(w, get_key_names(cmd, FALSE)); - colors_use(w, COLOR_TITLE); - waddch(w, ':'); - waddstr(w, label); - waddch(w, ' '); - waddch(w, ' '); -} -#endif - -static inline int -get_volume(const struct mpd_status *status) -{ - return status != NULL - ? mpd_status_get_volume(status) - : MPD_STATUS_NO_VOLUME; -} - -static void -paint_top_window2(const char *header, struct mpdclient *c) -{ - int volume; - char flags[5]; - WINDOW *w = screen.top_window.w; - char buf[32]; - - if (header[0]) { - colors_use(w, COLOR_TITLE_BOLD); - mvwaddstr(w, 0, 0, header); + const char *title = #ifndef NCMPC_MINI - } else { -#ifdef ENABLE_HELP_SCREEN - print_hotkey(w, CMD_SCREEN_HELP, _("Help")); + screen.welcome_source_id == 0 && #endif - print_hotkey(w, CMD_SCREEN_PLAY, _("Playlist")); - print_hotkey(w, CMD_SCREEN_FILE, _("Browse")); -#ifdef ENABLE_ARTIST_SCREEN - print_hotkey(w, CMD_SCREEN_ARTIST, _("Artist")); -#endif -#ifdef ENABLE_SEARCH_SCREEN - print_hotkey(w, CMD_SCREEN_SEARCH, _("Search")); -#endif -#ifdef ENABLE_LYRICS_SCREEN - print_hotkey(w, CMD_SCREEN_LYRICS, _("Lyrics")); -#endif -#ifdef ENABLE_OUTPUTS_SCREEN - print_hotkey(w, CMD_SCREEN_OUTPUTS, _("Outputs")); -#endif -#endif - } + mode_fn->get_title != NULL + ? mode_fn->get_title(screen.buf, screen.buf_size) + : ""; + assert(title != NULL); - volume = get_volume(c->status); - if (volume == MPD_STATUS_NO_VOLUME) - g_snprintf(buf, 32, _("Volume n/a")); - else - g_snprintf(buf, 32, _("Volume %d%%"), volume); - - colors_use(w, COLOR_TITLE); - mvwaddstr(w, 0, screen.top_window.cols - utf8_width(buf), buf); - - flags[0] = 0; - if (c->status != NULL) { - if (mpd_status_get_repeat(c->status)) - g_strlcat(flags, "r", sizeof(flags)); - if (mpd_status_get_random(c->status)) - g_strlcat(flags, "z", sizeof(flags)); - if (mpd_status_get_single(c->status)) - g_strlcat(flags, "s", sizeof(flags)); - if (mpd_status_get_consume(c->status)) - g_strlcat(flags, "c", sizeof(flags)); - if (mpd_status_get_crossfade(c->status)) - g_strlcat(flags, "x", sizeof(flags)); - if (mpd_status_get_update_id(c->status) != 0) - g_strlcat(flags, "U", sizeof(flags)); - } - - 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); -} - -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; + title_bar_paint(&screen.title_bar, title, c->status); } static void -paint_top_window(const char *header, struct mpdclient *c, int full_repaint) +update_progress_window(struct mpdclient *c, bool repaint) { - static int prev_volume = -1; - static unsigned prev_header_len = -1; - WINDOW *w = screen.top_window.w; - - if (prev_header_len != utf8_width(header)) { - prev_header_len = utf8_width(header); - full_repaint = 1; - } - - if (c->status && - volume_length(prev_volume) != - volume_length(mpd_status_get_volume(c->status))) - full_repaint = 1; - - if (full_repaint) { - wmove(w, 0, 0); - wclrtoeol(w); - } - - if ((c->status != NULL && - prev_volume != mpd_status_get_volume(c->status)) || - full_repaint) - paint_top_window2(header, c); -} - -static void -paint_progress_window(struct mpdclient *c) -{ - unsigned elapsed, duration; - - if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song)) + 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 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)) + unsigned 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); } -static void -paint_status_window(struct mpdclient *c) -{ - WINDOW *w = screen.status_window.w; - const struct mpd_status *status = c->status; - enum mpd_state state; - const struct mpd_song *song = c->song; - int elapsedTime = 0; -#ifdef NCMPC_MINI - static char bitrate[1]; -#else - char bitrate[16]; -#endif - const char *str = NULL; - int x = 0; - - if( time(NULL) - screen.status_timestamp <= options.status_message_time ) - return; - - wmove(w, 0, 0); - wclrtoeol(w); - colors_use(w, COLOR_STATUS_BOLD); - - state = status == NULL ? MPD_STATE_UNKNOWN - : mpd_status_get_state(status); - - switch (state) { - case MPD_STATE_PLAY: - str = _("Playing:"); - break; - case MPD_STATE_PAUSE: - str = _("[Paused]"); - break; - case MPD_STATE_STOP: - default: - break; - } - - if (str) { - waddstr(w, str); - x += utf8_width(str) + 1; - } - - /* create time string */ - memset(screen.buf, 0, screen.buf_size); - if (IS_PLAYING(state) || IS_PAUSED(state)) { - int total_time = mpd_status_get_total_time(status); - if (total_time > 0) { - /*checks the conf to see whether to display elapsed or remaining time */ - if(!strcmp(options.timedisplay_type,"elapsed")) - elapsedTime = mpd_status_get_elapsed_time(c->status); - else if(!strcmp(options.timedisplay_type,"remaining")) - elapsedTime = total_time - - mpd_status_get_elapsed_time(c->status); - - if (c->song != NULL && - seek_id == (int)mpd_song_get_id(c->song)) - elapsedTime = seek_target_time; - - /* display bitrate if visible-bitrate is true */ -#ifndef NCMPC_MINI - if (options.visible_bitrate) { - g_snprintf(bitrate, 16, - " [%d kbps]", - mpd_status_get_kbit_rate(status)); - } else { - bitrate[0] = '\0'; - } -#endif - - /*write out the time, using hours if time over 60 minutes*/ - if (total_time > 3600) { - g_snprintf(screen.buf, screen.buf_size, - "%s [%i:%02i:%02i/%i:%02i:%02i]", - bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60, - total_time / 3600, - (total_time % 3600)/60, - total_time % 60); - } else { - g_snprintf(screen.buf, screen.buf_size, - "%s [%i:%02i/%i:%02i]", - bitrate, elapsedTime/60, elapsedTime%60, - total_time / 60, total_time % 60); - } -#ifndef NCMPC_MINI - } else { - g_snprintf(screen.buf, screen.buf_size, - " [%d kbps]", - mpd_status_get_kbit_rate(status)); -#endif - } -#ifndef NCMPC_MINI - } else { - if (options.display_time) { - time_t timep; - - time(&timep); - strftime(screen.buf, screen.buf_size, "%X ",localtime(&timep)); - } -#endif - } - - /* display song */ - if (IS_PLAYING(state) || IS_PAUSED(state)) { - char songname[MAX_SONGNAME_LENGTH]; -#ifndef NCMPC_MINI - int width = COLS - x - utf8_width(screen.buf); -#endif - - if (song) - strfsong(songname, MAX_SONGNAME_LENGTH, - options.status_format, song); - else - songname[0] = '\0'; - - colors_use(w, COLOR_STATUS); - /* scroll if the song name is to long */ -#ifndef NCMPC_MINI - if (options.scroll && utf8_width(songname) > (unsigned)width) { - static scroll_state_t st = { 0, 0 }; - char *tmp = strscroll(songname, options.scroll_sep, width, &st); - - g_strlcpy(songname, tmp, MAX_SONGNAME_LENGTH); - g_free(tmp); - } -#endif - //mvwaddnstr(w, 0, x, songname, width); - mvwaddstr(w, 0, x, songname); - } - - /* display 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); - } - - wnoutrefresh(w); -} - void screen_exit(void) { @@ -452,10 +194,15 @@ screen_exit(void) g_free(screen.buf); g_free(screen.findbuf); - delwin(screen.top_window.w); + title_bar_deinit(&screen.title_bar); delwin(screen.main_window.w); - delwin(screen.progress_bar.window.w); - delwin(screen.status_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 @@ -463,36 +210,31 @@ screen_resize(struct mpdclient *c) { if (COLSget_title != NULL) - title = mode_fn->get_title(screen.buf, screen.buf_size); - - /* paint the title/header window */ - if( title ) - paint_top_window(title, c, 1); - else - paint_top_window("", c, 1); + /* update title/header window */ + paint_top_window(c); /* paint the bottom window */ - paint_progress_window(c); - paint_status_window(c); + update_progress_window(c, true); + status_bar_paint(&screen.status_bar, c->status, c->song); /* paint the main window */ - wclear(screen.main_window.w); - if (mode_fn->paint != NULL) - mode_fn->paint(); + if (main_dirty) { + wclear(screen.main_window.w); + if (mode_fn->paint != NULL) + mode_fn->paint(); + } /* move the cursor to the origin */ @@ -637,23 +360,22 @@ void screen_update(struct mpdclient *c) { #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; - static unsigned dbupdate; /* print a message if mpd status has changed */ - if (c->status != NULL) { + 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); - dbupdate = mpd_status_get_update_id(c->status); initialized = true; } @@ -690,52 +412,24 @@ screen_update(struct mpdclient *c) screen_status_printf(_("Crossfade %d seconds"), mpd_status_get_crossfade(c->status)); - if (dbupdate != 0 && - dbupdate != mpd_status_get_update_id(c->status)) { - screen_status_printf(_("Database updated")); - } - 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); - dbupdate = mpd_status_get_update_id(c->status); } - /* update title/header window */ - if (welcome && options.welcome_screen_list && - time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME) - paint_top_window("", c, 0); - else -#endif - if (mode_fn->get_title != NULL) { - paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0); -#ifndef NCMPC_MINI - welcome = FALSE; + 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 - } else - paint_top_window("", c, 0); - - /* update progress window */ - paint_progress_window(c); - - /* update status window */ - paint_status_window(c); /* update the main window */ if (mode_fn->update != NULL) mode_fn->update(c); - /* 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(); + screen_paint(c, false); } #ifdef HAVE_GETMOUSE @@ -744,10 +438,14 @@ screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row) { MEVENT event; - /* retrieve the mouse event from ncurses */ + /* retrieve the mouse event from curses */ +#ifdef PDCURSES + nc_getmouse(&event); +#else getmouse(&event); +#endif /* calculate the selected row in the list window */ - *row = event.y - screen.top_window.rows; + *row = event.y - screen.title_bar.window.rows; /* copy button state bits */ *bstate = event.bstate; /* if button 2 was pressed switch screen */ @@ -764,7 +462,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)) @@ -787,7 +488,7 @@ screen_cmd(struct mpdclient *c, command_t cmd) _("Auto center mode: Off")); break; case CMD_SCREEN_UPDATE: - screen_paint(c); + screen_paint(c, true); break; case CMD_SCREEN_PREVIOUS: screen_next_mode(c, -1); @@ -796,7 +497,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); @@ -835,10 +536,15 @@ screen_cmd(struct mpdclient *c, command_t cmd) 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; -#endif default: break;