summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 8f159b4)
raw | patch | inline | side by side (parent: 8f159b4)
author | Max Kellermann <max@duempel.org> | |
Tue, 29 Sep 2009 22:35:22 +0000 (00:35 +0200) | ||
committer | Max Kellermann <max@duempel.org> | |
Tue, 29 Sep 2009 22:35:22 +0000 (00:35 +0200) |
This class repaints only if the bar width has really changed. It uses
integer division instead of floating point.
integer division instead of floating point.
Makefile.am | patch | blob | history | |
src/progress_bar.c | [new file with mode: 0644] | patch | blob |
src/progress_bar.h | [new file with mode: 0644] | patch | blob |
src/screen.c | patch | blob | history | |
src/screen.h | patch | blob | history |
diff --git a/Makefile.am b/Makefile.am
index e90fb2ef7b3e16ecec774defeefb25e2b4dc80f8..5111564769fbac6d99fc52f8a1393614e8a60c37 100644 (file)
--- a/Makefile.am
+++ b/Makefile.am
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 \
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
--- /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 <assert.h>
+
+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
--- /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 <stdbool.h>
+
+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 a8b2ce56f600585ad587cba5cfd5ae6433c9c303..6ab4a60285f6aa23c695b02816a5a2d91cfbda23 100644 (file)
--- a/src/screen.c
+++ b/src/screen.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;
- }
+ 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
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);
}
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;
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,
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 ebf36748f4a646dafcabd99da58d75e6709a740c..6b7d1a99b01f1a1bddd8383fb0097b93537dffff 100644 (file)
--- a/src/screen.h
+++ b/src/screen.h
#include "config.h"
#include "command.h"
#include "window.h"
+#include "progress_bar.h"
#include <mpd/client.h>
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 */