X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fscreen.c;h=6d2610ad1770e461bd72a503ada7a016407bec3e;hb=bf5fc85f37af5698d477edc5dae250bf8984ed00;hp=0545bd079c4b8cb2f11595ae397ce5e58e2e669c;hpb=e9e1ac46382355c1eb77dde720d026f25e62a329;p=ncmpc.git diff --git a/src/screen.c b/src/screen.c index 0545bd0..6d2610a 100644 --- a/src/screen.c +++ b/src/screen.c @@ -1,95 +1,80 @@ /* 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" #include "mpdclient.h" -#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 - -#ifndef NCMPC_MINI -/** welcome message time [s] */ -static const GTime SCREEN_WELCOME_TIME = 10; -#endif - -/* minimum window size */ -static const int SCREEN_MIN_COLS = 14; -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; +struct screen screen = { + .current_page = &screen_queue, +}; -gboolean -screen_is_visible(const struct screen_functions *sf) -{ - return sf == mode_fn; -} +static const struct screen_functions *mode_fn_prev = &screen_queue; void screen_switch(const struct screen_functions *sf, struct mpdclient *c) { assert(sf != NULL); - if (sf == mode_fn) + if (sf == screen.current_page) return; - mode_fn_prev = mode_fn; + mode_fn_prev = screen.current_page; /* close the old mode */ - if (mode_fn->close != NULL) - mode_fn->close(); + if (screen.current_page->close != NULL) + screen.current_page->close(); /* get functions for the new mode */ - mode_fn = sf; + screen.current_page = sf; /* open the new mode */ - if (mode_fn->open != NULL) - mode_fn->open(c); + if (sf->open != NULL) + sf->open(c); - screen_paint(c); + screen_paint(c, true); } void @@ -130,554 +115,41 @@ 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(screen.current_page)); + 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) -{ - 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); -#ifndef NCMPC_MINI - } else { -#ifdef ENABLE_HELP_SCREEN - print_hotkey(w, CMD_SCREEN_HELP, _("Help")); -#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 - } - - 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; -} - -static void -paint_top_window(const char *header, struct mpdclient *c, int full_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) -{ - double p; - int width; - int elapsedTime; - - if (c->status==NULL || IS_STOPPED(mpd_status_get_state(c->status))) { - mvwhline(screen.progress_window.w, 0, 0, ACS_HLINE, - screen.progress_window.cols); - wnoutrefresh(screen.progress_window.w); - return; - } - - if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song)) - elapsedTime = seek_target_time; - else - elapsedTime = mpd_status_get_elapsed_time(c->status); - - p = ((double) elapsedTime) / ((double) mpd_status_get_total_time(c->status)); - - 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); -} - -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) -{ - 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); - - delwin(screen.top_window.w); - delwin(screen.main_window.w); - delwin(screen.progress_window.w); - delwin(screen.status_window.w); -} - -void -screen_resize(struct mpdclient *c) -{ - if (COLSopen != NULL) - mode_fn->open(c); -} - -void -screen_paint(struct mpdclient *c) -{ - const char *title = NULL; - - if (mode_fn->get_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); - - /* paint the bottom window */ - - paint_progress_window(c); - paint_status_window(c); - - /* paint the main window */ - - 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(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; } @@ -714,65 +186,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 && - screen.last_cmd==CMD_NONE && - 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 (screen.current_page->update != NULL) + screen.current_page->update(c); - if (!options.hardware_cursor) - wmove(screen.main_window.w, 0, 0); - - wnoutrefresh(screen.main_window.w); - - /* tell curses to update */ - doupdate(); -} - -void -screen_idle(struct mpdclient *c) -{ - if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song) && - (screen.last_cmd == CMD_SEEK_FORWARD || - screen.last_cmd == CMD_SEEK_BACKWARD)) - mpdclient_cmd_seek(c, seek_id, seek_target_time); - - screen.last_cmd = CMD_NONE; - seek_id = -1; + screen_paint(c, false); } #ifdef HAVE_GETMOUSE @@ -781,10 +212,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 */ @@ -800,12 +235,15 @@ screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row) void screen_cmd(struct mpdclient *c, command_t cmd) { - screen.last_cmd = 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)) + if (screen.current_page->cmd != NULL && + screen.current_page->cmd(c, cmd)) return; if (handle_player_command(c, cmd)) @@ -825,7 +263,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); @@ -834,7 +272,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); @@ -873,10 +311,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;