From 1c5f0f00f805181a8281dbf68229f83b7d65f45c Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 30 Sep 2009 08:22:08 +0200 Subject: [PATCH] screen: moved code to title_bar.c --- Makefile.am | 2 + src/screen.c | 143 ++++-------------------------------------------- src/screen.h | 3 +- src/title_bar.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++ src/title_bar.h | 55 +++++++++++++++++++ 5 files changed, 211 insertions(+), 132 deletions(-) create mode 100644 src/title_bar.c create mode 100644 src/title_bar.h diff --git a/Makefile.am b/Makefile.am index 5111564..0a12f3a 100644 --- a/Makefile.am +++ b/Makefile.am @@ -22,6 +22,7 @@ ncmpc_headers = \ src/ncu.h \ src/player_command.h \ src/window.h \ + src/title_bar.h \ src/progress_bar.h \ src/screen.h \ src/screen_list.h \ @@ -64,6 +65,7 @@ src_ncmpc_SOURCES = \ src/command.c \ src/ncu.c \ src/player_command.c \ + src/title_bar.c \ src/progress_bar.c \ src/screen.c \ src/screen_list.c \ diff --git a/src/screen.c b/src/screen.c index a4f0a89..9001c71 100644 --- a/src/screen.c +++ b/src/screen.c @@ -146,99 +146,6 @@ screen_next_mode(struct mpdclient *c, int offset) 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) - : -1; -} - -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 < 0) - 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) { @@ -252,31 +159,9 @@ volume_length(int volume) } static void -paint_top_window(const char *header, struct mpdclient *c, int full_repaint) +paint_top_window(const char *header, const struct mpdclient *c) { - 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); + title_bar_paint(&screen.title_bar, header, c->status); } static void @@ -452,7 +337,7 @@ 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); progress_bar_deinit(&screen.progress_bar); delwin(screen.status_window.w); @@ -472,9 +357,7 @@ screen_resize(struct mpdclient *c) screen.cols = COLS; screen.rows = LINES; - /* top window */ - screen.top_window.cols = screen.cols; - wresize(screen.top_window.w, 2, screen.cols); + title_bar_resize(&screen.title_bar, screen.cols); /* main window */ screen.main_window.cols = screen.cols; @@ -549,9 +432,7 @@ screen_init(struct mpdclient *c) screen.start_timestamp = time(NULL); /* create top window */ - window_init(&screen.top_window, 2, screen.cols, 0, 0); - leaveok(screen.top_window.w, TRUE); - keypad(screen.top_window.w, TRUE); + title_bar_init(&screen.title_bar, screen.cols, 0, 0); /* create main window */ window_init(&screen.main_window, screen.rows - 4, screen.cols, 2, 0); @@ -576,7 +457,7 @@ screen_init(struct mpdclient *c) /* set background attributes */ 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.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)); @@ -604,9 +485,9 @@ screen_paint(struct mpdclient *c) /* paint the title/header window */ if( title ) - paint_top_window(title, c, 1); + paint_top_window(title, c); else - paint_top_window("", c, 1); + paint_top_window("", c); /* paint the bottom window */ @@ -703,16 +584,16 @@ screen_update(struct mpdclient *c) /* update title/header window */ if (welcome && options.welcome_screen_list && time(NULL)-screen.start_timestamp <= SCREEN_WELCOME_TIME) - paint_top_window("", c, 0); + paint_top_window("", c); else #endif if (mode_fn->get_title != NULL) { - paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c, 0); + paint_top_window(mode_fn->get_title(screen.buf,screen.buf_size), c); #ifndef NCMPC_MINI welcome = FALSE; #endif } else - paint_top_window("", c, 0); + paint_top_window("", c); /* update progress window */ paint_progress_window(c); @@ -744,7 +625,7 @@ screen_get_mouse_event(struct mpdclient *c, unsigned long *bstate, int *row) /* retrieve the mouse event from ncurses */ getmouse(&event); /* 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 */ diff --git a/src/screen.h b/src/screen.h index 6b7d1a9..57214c5 100644 --- a/src/screen.h +++ b/src/screen.h @@ -23,6 +23,7 @@ #include "config.h" #include "command.h" #include "window.h" +#include "title_bar.h" #include "progress_bar.h" #include @@ -44,7 +45,7 @@ struct mpdclient; struct screen { - struct window top_window; + struct title_bar title_bar; struct window main_window; struct progress_bar progress_bar; struct window status_window; diff --git a/src/title_bar.c b/src/title_bar.c new file mode 100644 index 0000000..5fa0ca5 --- /dev/null +++ b/src/title_bar.c @@ -0,0 +1,140 @@ +/* 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 "title_bar.h" +#include "colors.h" +#include "command.h" +#include "i18n.h" +#include "charset.h" + +#include + +#include + +#include +#include + +#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) + : -1; +} + +void +title_bar_paint(const struct title_bar *p, const char *title, + const struct mpd_status *status) +{ + WINDOW *w = p->window.w; + int volume; + char flags[5]; + char buf[32]; + + assert(p != NULL); + + wmove(w, 0, 0); + wclrtoeol(w); + + if (title[0]) { + colors_use(w, COLOR_TITLE_BOLD); + mvwaddstr(w, 0, 0, title); +#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(status); + if (volume < 0) + g_snprintf(buf, 32, _("Volume n/a")); + else + g_snprintf(buf, 32, _("Volume %d%%"), volume); + + colors_use(w, COLOR_TITLE); + mvwaddstr(w, 0, p->window.cols - utf8_width(buf), buf); + + flags[0] = 0; + if (status != NULL) { + if (mpd_status_get_repeat(status)) + g_strlcat(flags, "r", sizeof(flags)); + if (mpd_status_get_random(status)) + g_strlcat(flags, "z", sizeof(flags)); + if (mpd_status_get_single(status)) + g_strlcat(flags, "s", sizeof(flags)); + if (mpd_status_get_consume(status)) + g_strlcat(flags, "c", sizeof(flags)); + if (mpd_status_get_crossfade(status)) + g_strlcat(flags, "x", sizeof(flags)); + if (mpd_status_get_update_id(status) != 0) + g_strlcat(flags, "U", sizeof(flags)); + } + + colors_use(w, COLOR_LINE); + mvwhline(w, 1, 0, ACS_HLINE, p->window.cols); + if (flags[0]) { + wmove(w, 1, p->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); +} + +void +title_bar_resize(struct title_bar *p, unsigned width) +{ + assert(p != NULL); + + p->window.cols = width; + wresize(p->window.w, 2, width); +} diff --git a/src/title_bar.h b/src/title_bar.h new file mode 100644 index 0000000..fd6d816 --- /dev/null +++ b/src/title_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_TITLE_BAR_H +#define NCMPC_TITLE_BAR_H + +#include "window.h" + +#include + +struct mpd_status; + +struct title_bar { + struct window window; +}; + +static inline void +title_bar_init(struct title_bar *p, unsigned width, int y, int x) +{ + window_init(&p->window, 2, width, y, x); + + leaveok(p->window.w, true); + keypad(p->window.w, true); +} + +static inline void +title_bar_deinit(struct title_bar *p) +{ + delwin(p->window.w); +} + +void +title_bar_paint(const struct title_bar *p, const char *title, + const struct mpd_status *status); + +void +title_bar_resize(struct title_bar *p, unsigned width); + +#endif -- 2.30.2