From: Max Kellermann Date: Tue, 29 Sep 2009 22:35:22 +0000 (+0200) Subject: screen: moved code to progress_bar.c X-Git-Tag: release-0.16~288 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=6a76472ab9ebcb1ef4a688e863c46cc0d66ebb9c;p=ncmpc.git screen: moved code to progress_bar.c This class repaints only if the bar width has really changed. It uses integer division instead of floating point. --- diff --git a/Makefile.am b/Makefile.am index e90fb2e..5111564 100644 --- a/Makefile.am +++ b/Makefile.am @@ -22,6 +22,7 @@ ncmpc_headers = \ src/ncu.h \ src/player_command.h \ src/window.h \ + src/progress_bar.h \ src/screen.h \ src/screen_list.h \ src/screen_play.h \ @@ -63,6 +64,7 @@ src_ncmpc_SOURCES = \ src/command.c \ src/ncu.c \ src/player_command.c \ + src/progress_bar.c \ src/screen.c \ src/screen_list.c \ src/screen_utils.c \ diff --git a/src/progress_bar.c b/src/progress_bar.c new file mode 100644 index 0000000..8fe2784 --- /dev/null +++ b/src/progress_bar.c @@ -0,0 +1,76 @@ +/* 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 "progress_bar.h" + +#include + +void +progress_bar_paint(const struct progress_bar *p) +{ + assert(p != NULL); + + mvwhline(p->window.w, 0, 0, ACS_HLINE, p->window.cols); + + if (p->max > 0) { + assert(p->width < p->window.cols); + + if (p->width > 0) + whline(p->window.w, '=', p->width); + + mvwaddch(p->window.w, 0, p->width, 'O'); + } + + wnoutrefresh(p->window.w); +} + +bool +progress_bar_resize(struct progress_bar *p) +{ + unsigned old_width; + + assert(p != NULL); + + if (p->max == 0) + return false; + + old_width = p->width; + p->width = (p->window.cols * p->current) / (p->max + 1); + assert(p->width < p->window.cols); + + return p->width != old_width; +} + +bool +progress_bar_set(struct progress_bar *p, unsigned current, unsigned max) +{ + bool modified; + + assert(p != NULL); + + if (current > max) + current = max; + + modified = (max == 0) != (p->max == 0); + + p->max = max; + p->current = current; + + return progress_bar_resize(p) || modified; +} diff --git a/src/progress_bar.h b/src/progress_bar.h new file mode 100644 index 0000000..d6fdba5 --- /dev/null +++ b/src/progress_bar.h @@ -0,0 +1,55 @@ +/* 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_PROGRESS_BAR_H +#define NCMPC_PROGRESS_BAR_H + +#include "window.h" + +#include + +struct progress_bar { + struct window window; + + unsigned current, max; + + unsigned width; +}; + +static inline void +progress_bar_init(struct progress_bar *p, unsigned height, unsigned width, + int y, int x) +{ + window_init(&p->window, height, width, y, x); + + p->current = 0; + p->max = 0; + p->width = 0; +} + +void +progress_bar_paint(const struct progress_bar *p); + +bool +progress_bar_resize(struct progress_bar *p); + +bool +progress_bar_set(struct progress_bar *p, unsigned current, unsigned max); + +#endif diff --git a/src/screen.c b/src/screen.c index a8b2ce5..6ab4a60 100644 --- a/src/screen.c +++ b/src/screen.c @@ -282,32 +282,22 @@ paint_top_window(const char *header, struct mpdclient *c, int full_repaint) 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; - } + unsigned elapsed, duration; if (c->song != NULL && seek_id == (int)mpd_song_get_id(c->song)) - elapsedTime = seek_target_time; + elapsed = seek_target_time; + else if (c->status != NULL) + elapsed = mpd_status_get_elapsed_time(c->status); 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); + elapsed = 0; + + duration = c->status != NULL && + !IS_STOPPED(mpd_status_get_state(c->status)) + ? mpd_status_get_total_time(c->status) + : 0; + + if (progress_bar_set(&screen.progress_bar, elapsed, duration)) + progress_bar_paint(&screen.progress_bar); } static void @@ -464,7 +454,7 @@ screen_exit(void) delwin(screen.top_window.w); delwin(screen.main_window.w); - delwin(screen.progress_window.w); + delwin(screen.progress_bar.window.w); delwin(screen.status_window.w); } @@ -493,9 +483,11 @@ screen_resize(struct mpdclient *c) 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); + screen.progress_bar.window.cols = screen.cols; + wresize(screen.progress_bar.window.w, 1, screen.cols); + mvwin(screen.progress_bar.window.w, screen.rows-2, 0); + progress_bar_resize(&screen.progress_bar); + progress_bar_paint(&screen.progress_bar); /* status window */ screen.status_window.cols = screen.cols; @@ -570,9 +562,10 @@ screen_init(struct mpdclient *c) keypad(screen.main_window.w, TRUE); /* create progress window */ - window_init(&screen.progress_window, 1, screen.cols, - screen.rows - 2, 0); - leaveok(screen.progress_window.w, TRUE); + progress_bar_init(&screen.progress_bar, 1, screen.cols, + screen.rows - 2, 0); + + leaveok(screen.progress_bar.window.w, TRUE); /* create status window */ window_init(&screen.status_window, 1, screen.cols, @@ -587,9 +580,10 @@ screen_init(struct mpdclient *c) 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.progress_bar.window.w, + COLOR_PAIR(COLOR_PROGRESSBAR)); wbkgd(screen.status_window.w, COLOR_PAIR(COLOR_STATUS)); - colors_use(screen.progress_window.w, COLOR_PROGRESSBAR); + colors_use(screen.progress_bar.window.w, COLOR_PROGRESSBAR); } #endif diff --git a/src/screen.h b/src/screen.h index ebf3674..6b7d1a9 100644 --- a/src/screen.h +++ b/src/screen.h @@ -23,6 +23,7 @@ #include "config.h" #include "command.h" #include "window.h" +#include "progress_bar.h" #include @@ -45,7 +46,7 @@ struct mpdclient; struct screen { struct window top_window; struct window main_window; - struct window progress_window; + struct progress_bar progress_bar; struct window status_window; /* GTime is equivalent to time_t */