16012f4deb4d5807a9895a595b3910f1f69fb48e
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "status_bar.h"
21 #include "options.h"
22 #include "colors.h"
23 #include "i18n.h"
24 #include "charset.h"
25 #include "strfsong.h"
26 #include "player_command.h"
27 #include "utils.h"
29 #include <mpd/client.h>
31 #include <assert.h>
32 #include <string.h>
34 #ifndef NCMPC_MINI
35 static gboolean
36 scroll_timer_callback(gpointer data)
37 {
38 struct status_bar *p = data;
40 p->scroll_source_id = 0;
42 hscroll_step(&p->hscroll);
43 status_bar_paint(p, p->prev_status, p->prev_song);
44 doupdate();
45 return false;
46 }
47 #endif
49 static gboolean
50 status_bar_clear_message(gpointer data)
51 {
52 struct status_bar *p = data;
53 WINDOW *w = p->window.w;
55 assert(p != NULL);
56 assert(p->message_source_id != 0);
58 p->message_source_id = 0;
60 wmove(w, 0, 0);
61 wclrtoeol(w);
62 wrefresh(w);
64 return false;
65 }
67 void
68 status_bar_paint(struct status_bar *p, const struct mpd_status *status,
69 const struct mpd_song *song)
70 {
71 WINDOW *w = p->window.w;
72 enum mpd_state state;
73 int elapsedTime = 0;
74 #ifdef NCMPC_MINI
75 static char bitrate[1];
76 #else
77 char bitrate[16];
78 #endif
79 const char *str = NULL;
80 int x = 0;
81 char buffer[p->window.cols * 4 + 1];
83 #ifndef NCMPC_MINI
84 p->prev_status = status;
85 p->prev_song = song;
86 #endif
88 if (p->message_source_id != 0)
89 return;
91 wmove(w, 0, 0);
92 wclrtoeol(w);
93 colors_use(w, COLOR_STATUS_BOLD);
95 state = status == NULL ? MPD_STATE_UNKNOWN
96 : mpd_status_get_state(status);
98 switch (state) {
99 case MPD_STATE_PLAY:
100 str = _("Playing:");
101 break;
102 case MPD_STATE_PAUSE:
103 str = _("[Paused]");
104 break;
105 case MPD_STATE_STOP:
106 default:
107 break;
108 }
110 if (str) {
111 waddstr(w, str);
112 x += utf8_width(str) + 1;
113 }
115 /* create time string */
116 if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
117 int total_time = mpd_status_get_total_time(status);
118 if (total_time > 0) {
119 char elapsed_string[32], duration_string[32];
121 /*checks the conf to see whether to display elapsed or remaining time */
122 if (song != NULL &&
123 seek_id == (int)mpd_song_get_id(song))
124 elapsedTime = seek_target_time;
125 else if (options.display_remaining_time)
126 elapsedTime = total_time -
127 mpd_status_get_elapsed_time(status);
128 else
129 elapsedTime = mpd_status_get_elapsed_time(status);
131 /* display bitrate if visible-bitrate is true */
132 #ifndef NCMPC_MINI
133 if (options.visible_bitrate) {
134 g_snprintf(bitrate, 16,
135 " [%d kbps]",
136 mpd_status_get_kbit_rate(status));
137 } else {
138 bitrate[0] = '\0';
139 }
140 #endif
142 /* write out the time */
143 format_duration_short(elapsed_string,
144 sizeof(elapsed_string),
145 elapsedTime);
146 format_duration_short(duration_string,
147 sizeof(duration_string),
148 total_time);
150 g_snprintf(buffer, sizeof(buffer), "%s [%s/%s]",
151 bitrate, elapsed_string, duration_string);
152 #ifndef NCMPC_MINI
153 } else if (options.visible_bitrate) {
154 g_snprintf(buffer, sizeof(buffer),
155 " [%d kbps]",
156 mpd_status_get_kbit_rate(status));
157 #endif
158 } else {
159 buffer[0] = 0;
160 }
161 } else {
162 #ifndef NCMPC_MINI
163 if (options.display_time) {
164 time_t timep;
166 time(&timep);
167 strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
168 } else
169 #endif
170 buffer[0] = 0;
171 }
173 /* display song */
174 if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
175 char songname[p->window.cols * 4 + 1];
176 #ifndef NCMPC_MINI
177 int width = COLS - x - utf8_width(buffer);
178 #endif
180 if (song)
181 strfsong(songname, sizeof(songname),
182 options.status_format, song);
183 else
184 songname[0] = '\0';
186 colors_use(w, COLOR_STATUS);
187 /* scroll if the song name is to long */
188 #ifndef NCMPC_MINI
189 if (options.scroll && utf8_width(songname) > (unsigned)width) {
190 char *tmp = strscroll(&p->hscroll, songname,
191 options.scroll_sep, width);
193 g_strlcpy(songname, tmp, sizeof(songname));
194 g_free(tmp);
196 if (p->scroll_source_id == 0)
197 p->scroll_source_id =
198 g_timeout_add(1000,
199 scroll_timer_callback,
200 p);
201 } else if (p->scroll_source_id != 0) {
202 g_source_remove(p->scroll_source_id);
203 p->scroll_source_id = 0;
204 }
205 #endif
206 //mvwaddnstr(w, 0, x, songname, width);
207 mvwaddstr(w, 0, x, songname);
208 #ifndef NCMPC_MINI
209 } else if (p->scroll_source_id != 0) {
210 g_source_remove(p->scroll_source_id);
211 p->scroll_source_id = 0;
212 #endif
213 }
215 /* display time string */
216 if (buffer[0] != 0) {
217 x = p->window.cols - strlen(buffer);
218 colors_use(w, COLOR_STATUS_TIME);
219 mvwaddstr(w, 0, x, buffer);
220 }
222 wnoutrefresh(w);
223 }
225 void
226 status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
227 {
228 p->window.cols = width;
229 wresize(p->window.w, 1, width);
230 mvwin(p->window.w, y, x);
231 }
233 void
234 status_bar_message(struct status_bar *p, const char *msg)
235 {
236 WINDOW *w = p->window.w;
238 wmove(w, 0, 0);
239 wclrtoeol(w);
240 colors_use(w, COLOR_STATUS_ALERT);
241 waddstr(w, msg);
242 wnoutrefresh(w);
244 if (p->message_source_id != 0)
245 g_source_remove(p->message_source_id);
246 p->message_source_id = g_timeout_add(options.status_message_time * 1000,
247 status_bar_clear_message, p);
248 }