Code

status_bar: moved code to format_bitrate()
[ncmpc.git] / src / status_bar.c
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 void
35 status_bar_init(struct status_bar *p, unsigned width, int y, int x)
36 {
37         window_init(&p->window, 1, width, y, x);
39         leaveok(p->window.w, false);
40         keypad(p->window.w, true);
42         p->message_source_id = 0;
44 #ifndef NCMPC_MINI
45         if (options.scroll)
46                 hscroll_init(&p->hscroll, p->window.w, options.scroll_sep);
48         p->prev_status = NULL;
49         p->prev_song = NULL;
50 #endif
51 }
53 void
54 status_bar_deinit(struct status_bar *p)
55 {
56         delwin(p->window.w);
58 #ifndef NCMPC_MINI
59         if (options.scroll)
60                 hscroll_clear(&p->hscroll);
61 #endif
62 }
64 static gboolean
65 status_bar_clear_message(gpointer data)
66 {
67         struct status_bar *p = data;
68         WINDOW *w = p->window.w;
70         assert(p != NULL);
71         assert(p->message_source_id != 0);
73         p->message_source_id = 0;
75         wmove(w, 0, 0);
76         wclrtoeol(w);
77         wrefresh(w);
79         return false;
80 }
82 #ifndef NCMPC_MINI
84 static void
85 format_bitrate(char *p, size_t max_length, const struct mpd_status *status)
86 {
87         if (options.visible_bitrate && mpd_status_get_kbit_rate(status) > 0)
88                 g_snprintf(p, max_length,
89                            " [%d kbps]",
90                            mpd_status_get_kbit_rate(status));
91         else
92                 p[0] = '\0';
93 }
95 #endif /* !NCMPC_MINI */
97 void
98 status_bar_paint(struct status_bar *p, const struct mpd_status *status,
99                  const struct mpd_song *song)
101         WINDOW *w = p->window.w;
102         enum mpd_state state;
103         int elapsedTime = 0;
104 #ifdef NCMPC_MINI
105         static char bitrate[1];
106 #else
107         char bitrate[16];
108 #endif
109         const char *str = NULL;
110         int x = 0;
111         char buffer[p->window.cols * 4 + 1];
113 #ifndef NCMPC_MINI
114         p->prev_status = status;
115         p->prev_song = song;
116 #endif
118         if (p->message_source_id != 0)
119                 return;
121         wmove(w, 0, 0);
122         wclrtoeol(w);
123         colors_use(w, COLOR_STATUS_BOLD);
125         state = status == NULL ? MPD_STATE_UNKNOWN
126                 : mpd_status_get_state(status);
128         switch (state) {
129         case MPD_STATE_PLAY:
130                 str = _("Playing:");
131                 break;
132         case MPD_STATE_PAUSE:
133                 str = _("[Paused]");
134                 break;
135         case MPD_STATE_STOP:
136         default:
137                 break;
138         }
140         if (str) {
141                 waddstr(w, str);
142                 x += utf8_width(str) + 1;
143         }
145         /* create time string */
146         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
147                 int total_time = mpd_status_get_total_time(status);
148                 if (total_time > 0) {
149                         char elapsed_string[32], duration_string[32];
151                         /*checks the conf to see whether to display elapsed or remaining time */
152                         if (seek_id >= 0 &&
153                             seek_id == mpd_status_get_song_id(status))
154                                 elapsedTime = seek_target_time;
155                         else if (options.display_remaining_time)
156                                 elapsedTime = total_time -
157                                         mpd_status_get_elapsed_time(status);
158                         else
159                                 elapsedTime = mpd_status_get_elapsed_time(status);
161                         /* display bitrate if visible-bitrate is true */
162 #ifndef NCMPC_MINI
163                         format_bitrate(bitrate, sizeof(bitrate), status);
164 #endif
166                         /* write out the time */
167                         format_duration_short(elapsed_string,
168                                               sizeof(elapsed_string),
169                                               elapsedTime);
170                         format_duration_short(duration_string,
171                                               sizeof(duration_string),
172                                               total_time);
174                         g_snprintf(buffer, sizeof(buffer), "%s [%s/%s]",
175                                    bitrate, elapsed_string, duration_string);
176 #ifndef NCMPC_MINI
177                 } else {
178                         format_bitrate(buffer, sizeof(buffer), status);
179 #else
180                         buffer[0] = 0;
181 #endif
182                 }
183         } else {
184 #ifndef NCMPC_MINI
185                 if (options.display_time) {
186                         time_t timep;
188                         time(&timep);
189                         strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
190                 } else
191 #endif
192                         buffer[0] = 0;
193         }
195         /* display song */
196         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
197                 char songname[p->window.cols * 4 + 1];
198 #ifndef NCMPC_MINI
199                 int width = COLS - x - utf8_width(buffer);
200 #endif
202                 if (song)
203                         strfsong(songname, sizeof(songname),
204                                  options.status_format, song);
205                 else
206                         songname[0] = '\0';
208                 colors_use(w, COLOR_STATUS);
209                 /* scroll if the song name is to long */
210 #ifndef NCMPC_MINI
211                 if (options.scroll && utf8_width(songname) > (unsigned)width) {
212                         hscroll_set(&p->hscroll, x, 0, width, songname);
213                         hscroll_draw(&p->hscroll);
214                 } else {
215                         if (options.scroll)
216                                 hscroll_clear(&p->hscroll);
217                         mvwaddstr(w, 0, x, songname);
218                 }
219 #else
220                 mvwaddstr(w, 0, x, songname);
221 #endif
222 #ifndef NCMPC_MINI
223         } else if (options.scroll) {
224                 hscroll_clear(&p->hscroll);
225 #endif
226         }
228         /* display time string */
229         if (buffer[0] != 0) {
230                 x = p->window.cols - strlen(buffer);
231                 colors_use(w, COLOR_STATUS_TIME);
232                 mvwaddstr(w, 0, x, buffer);
233         }
235         wnoutrefresh(w);
238 void
239 status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
241         p->window.cols = width;
242         wresize(p->window.w, 1, width);
243         mvwin(p->window.w, y, x);
246 void
247 status_bar_message(struct status_bar *p, const char *msg)
249         WINDOW *w = p->window.w;
251 #ifndef NCMPC_MINI
252         if (options.scroll)
253                 hscroll_clear(&p->hscroll);
254 #endif
256         wmove(w, 0, 0);
257         wclrtoeol(w);
258         colors_use(w, COLOR_STATUS_ALERT);
259         waddstr(w, msg);
260         wnoutrefresh(w);
262         if (p->message_source_id != 0)
263                 g_source_remove(p->message_source_id);
264         p->message_source_id = g_timeout_add(options.status_message_time * 1000,
265                                              status_bar_clear_message, p);