From cf98ad001d01ebce6cebf934027a636056302f7d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Sep 2009 16:32:47 +0200 Subject: [PATCH] screen: moved code to status_bar.c --- Makefile.am | 2 + src/screen.c | 174 +++------------------------------------- src/screen.h | 4 +- src/screen_utils.c | 6 +- src/status_bar.c | 196 +++++++++++++++++++++++++++++++++++++++++++++ src/status_bar.h | 65 +++++++++++++++ 6 files changed, 277 insertions(+), 170 deletions(-) create mode 100644 src/status_bar.c create mode 100644 src/status_bar.h diff --git a/Makefile.am b/Makefile.am index 0a12f3a..49ba9f4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -24,6 +24,7 @@ ncmpc_headers = \ src/window.h \ src/title_bar.h \ src/progress_bar.h \ + src/status_bar.h \ src/screen.h \ src/screen_list.h \ src/screen_play.h \ @@ -67,6 +68,7 @@ src_ncmpc_SOURCES = \ src/player_command.c \ src/title_bar.c \ src/progress_bar.c \ + src/status_bar.c \ src/screen.c \ src/screen_list.c \ src/screen_utils.c \ diff --git a/src/screen.c b/src/screen.c index 9001c71..9850f39 100644 --- a/src/screen.c +++ b/src/screen.c @@ -27,13 +27,8 @@ #include "utils.h" #include "options.h" #include "colors.h" -#include "strfsong.h" #include "player_command.h" -#ifndef NCMPC_MINI -#include "hscroll.h" -#endif - #include #include @@ -185,146 +180,6 @@ paint_progress_window(struct mpdclient *c) 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) { @@ -340,7 +195,7 @@ screen_exit(void) title_bar_deinit(&screen.title_bar); delwin(screen.main_window.w); progress_bar_deinit(&screen.progress_bar); - delwin(screen.status_window.w); + status_bar_deinit(&screen.status_bar); } void @@ -371,9 +226,8 @@ screen_resize(struct mpdclient *c) progress_bar_paint(&screen.progress_bar); /* 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); + status_bar_resize(&screen.status_bar, screen.cols, screen.rows - 1, 0); + status_bar_paint(&screen.status_bar, c->status, c->song); screen.buf_size = screen.cols; g_free(screen.buf); @@ -392,14 +246,7 @@ screen_resize(struct mpdclient *c) void screen_status_message(const 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); + status_bar_message(&screen.status_bar, msg); } void @@ -446,11 +293,8 @@ screen_init(struct mpdclient *c) progress_bar_paint(&screen.progress_bar); /* create status window */ - window_init(&screen.status_window, 1, screen.cols, - screen.rows - 1, 0); - - leaveok(screen.status_window.w, FALSE); - keypad(screen.status_window.w, TRUE); + status_bar_init(&screen.status_bar, screen.cols, screen.rows - 1, 0); + status_bar_paint(&screen.status_bar, c->status, c->song); #ifdef ENABLE_COLORS if (options.enable_colors) { @@ -460,7 +304,7 @@ screen_init(struct mpdclient *c) wbkgd(screen.title_bar.window.w, COLOR_PAIR(COLOR_TITLE)); wbkgd(screen.progress_bar.window.w, COLOR_PAIR(COLOR_PROGRESSBAR)); - wbkgd(screen.status_window.w, COLOR_PAIR(COLOR_STATUS)); + wbkgd(screen.status_bar.window.w, COLOR_PAIR(COLOR_STATUS)); colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR); } #endif @@ -492,7 +336,7 @@ screen_paint(struct mpdclient *c) /* paint the bottom window */ paint_progress_window(c); - paint_status_window(c); + status_bar_paint(&screen.status_bar, c->status, c->song); /* paint the main window */ @@ -599,7 +443,7 @@ screen_update(struct mpdclient *c) paint_progress_window(c); /* update status window */ - paint_status_window(c); + status_bar_paint(&screen.status_bar, c->status, c->song); /* update the main window */ if (mode_fn->update != NULL) diff --git a/src/screen.h b/src/screen.h index 57214c5..b0f53c7 100644 --- a/src/screen.h +++ b/src/screen.h @@ -25,6 +25,7 @@ #include "window.h" #include "title_bar.h" #include "progress_bar.h" +#include "status_bar.h" #include @@ -48,11 +49,10 @@ struct screen { struct title_bar title_bar; struct window main_window; struct progress_bar progress_bar; - struct window status_window; + struct status_bar status_bar; /* GTime is equivalent to time_t */ GTime start_timestamp; - GTime status_timestamp; unsigned cols, rows; diff --git a/src/screen_utils.c b/src/screen_utils.c index 7a3f04b..063893e 100644 --- a/src/screen_utils.c +++ b/src/screen_utils.c @@ -50,7 +50,7 @@ screen_bell(void) int screen_getch(const char *prompt) { - WINDOW *w = screen.status_window.w; + WINDOW *w = screen.status_bar.window.w; int key = -1; colors_use(w, COLOR_STATUS_ALERT); @@ -82,7 +82,7 @@ screen_readln(const char *prompt, GList **history, GCompletion *gcmp) { - struct window *window = &screen.status_window; + struct window *window = &screen.status_bar.window; WINDOW *w = window->w; char *line = NULL; @@ -97,7 +97,7 @@ screen_readln(const char *prompt, char * screen_read_password(const char *prompt) { - struct window *window = &screen.status_window; + struct window *window = &screen.status_bar.window; WINDOW *w = window->w; char *ret; diff --git a/src/status_bar.c b/src/status_bar.c new file mode 100644 index 0000000..3c16731 --- /dev/null +++ b/src/status_bar.c @@ -0,0 +1,196 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 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 "status_bar.h" +#include "options.h" +#include "colors.h" +#include "i18n.h" +#include "charset.h" +#include "strfsong.h" +#include "player_command.h" + +#ifndef NCMPC_MINI +#include "hscroll.h" +#endif + +#include + +#include + +void +status_bar_paint(const struct status_bar *p, const struct mpd_status *status, + const struct mpd_song *song) +{ + WINDOW *w = p->window.w; + enum mpd_state state; + int elapsedTime = 0; +#ifdef NCMPC_MINI + static char bitrate[1]; +#else + char bitrate[16]; +#endif + const char *str = NULL; + int x = 0; + char buffer[p->window.cols * 4 + 1]; + + if (time(NULL) - p->message_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 */ + if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) { + 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(status); + else if(!strcmp(options.timedisplay_type,"remaining")) + elapsedTime = total_time - + mpd_status_get_elapsed_time(status); + + if (song != NULL && + seek_id == (int)mpd_song_get_id(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(buffer, sizeof(buffer), + "%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(buffer, sizeof(buffer), + "%s [%i:%02i/%i:%02i]", + bitrate, elapsedTime/60, elapsedTime%60, + total_time / 60, total_time % 60); + } +#ifndef NCMPC_MINI + } else { + g_snprintf(buffer, sizeof(buffer), + " [%d kbps]", + mpd_status_get_kbit_rate(status)); +#endif + } +#ifndef NCMPC_MINI + } else { + if (options.display_time) { + time_t timep; + + time(&timep); + strftime(buffer, sizeof(buffer), "%X ",localtime(&timep)); + } else + buffer[0] = 0; +#endif + } + + /* display song */ + if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) { + char songname[p->window.cols * 4 + 1]; +#ifndef NCMPC_MINI + int width = COLS - x - utf8_width(buffer); +#endif + + if (song) + strfsong(songname, sizeof(songname), + 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, sizeof(songname)); + g_free(tmp); + } +#endif + //mvwaddnstr(w, 0, x, songname, width); + mvwaddstr(w, 0, x, songname); + } + + /* display time string */ + if (buffer[0] != 0) { + x = p->window.cols - strlen(buffer); + colors_use(w, COLOR_STATUS_TIME); + mvwaddstr(w, 0, x, buffer); + } + + wnoutrefresh(w); +} + +void +status_bar_resize(struct status_bar *p, unsigned width, int y, int x) +{ + p->window.cols = width; + wresize(p->window.w, 1, width); + mvwin(p->window.w, y, x); +} + +void +status_bar_message(struct status_bar *p, const char *msg) +{ + WINDOW *w = p->window.w; + + wmove(w, 0, 0); + wclrtoeol(w); + colors_use(w, COLOR_STATUS_ALERT); + waddstr(w, msg); + wnoutrefresh(w); + + p->message_timestamp = time(NULL); +} diff --git a/src/status_bar.h b/src/status_bar.h new file mode 100644 index 0000000..78b367c --- /dev/null +++ b/src/status_bar.h @@ -0,0 +1,65 @@ +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2009 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. + */ + +#ifndef NCMPC_STATUS_BAR_H +#define NCMPC_STATUS_BAR_H + +#include "window.h" + +#include + +#include + +struct mpd_status; +struct mpd_song; + +struct status_bar { + struct window window; + + GTime message_timestamp; +}; + +static inline void +status_bar_init(struct status_bar *p, unsigned width, int y, int x) +{ + window_init(&p->window, 1, width, y, x); + + leaveok(p->window.w, false); + keypad(p->window.w, true); + + p->message_timestamp = 0; +} + +static inline void +status_bar_deinit(struct status_bar *p) +{ + delwin(p->window.w); +} + +void +status_bar_paint(const struct status_bar *p, const struct mpd_status *status, + const struct mpd_song *song); + +void +status_bar_resize(struct status_bar *p, unsigned width, int y, int x); + +void +status_bar_message(struct status_bar *p, const char *msg); + +#endif -- 2.30.2