Code

utils: move format_duration_*() to time_format.c
[ncmpc.git] / src / status_bar.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 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 "time_format.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 void
65 status_bar_clear_message(struct status_bar *p)
66 {
67         assert(p != NULL);
69         if (p->message_source_id != 0) {
70                 g_source_remove(p->message_source_id);
71                 p->message_source_id = 0;
72         }
74         WINDOW *w = p->window.w;
76         wmove(w, 0, 0);
77         wclrtoeol(w);
78         wrefresh(w);
79 }
81 #ifndef NCMPC_MINI
83 static void
84 format_bitrate(char *p, size_t max_length, const struct mpd_status *status)
85 {
86         if (options.visible_bitrate && mpd_status_get_kbit_rate(status) > 0)
87                 g_snprintf(p, max_length,
88                            " [%d kbps]",
89                            mpd_status_get_kbit_rate(status));
90         else
91                 p[0] = '\0';
92 }
94 #endif /* !NCMPC_MINI */
96 void
97 status_bar_paint(struct status_bar *p, const struct mpd_status *status,
98                  const struct mpd_song *song)
99 {
100         WINDOW *w = p->window.w;
101         char buffer[p->window.cols * 4 + 1];
103 #ifndef NCMPC_MINI
104         p->prev_status = status;
105         p->prev_song = song;
106 #endif
108         if (p->message_source_id != 0)
109                 return;
111         wmove(w, 0, 0);
112         wclrtoeol(w);
113         colors_use(w, COLOR_STATUS_BOLD);
115         enum mpd_state state = status == NULL ? MPD_STATE_UNKNOWN
116                 : mpd_status_get_state(status);
118         const char *str = NULL;
119         switch (state) {
120         case MPD_STATE_PLAY:
121                 str = _("Playing:");
122                 break;
123         case MPD_STATE_PAUSE:
124                 str = _("[Paused]");
125                 break;
126         case MPD_STATE_STOP:
127         default:
128                 break;
129         }
131         int x = 0;
132         if (str) {
133                 waddstr(w, str);
134                 x += utf8_width(str) + 1;
135         }
137         /* create time string */
138         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
139                 int elapsedTime = seek_id >= 0 &&
140                         seek_id == mpd_status_get_song_id(status)
141                         ? (unsigned)seek_target_time
142                         : mpd_status_get_elapsed_time(status);
143                 int total_time = mpd_status_get_total_time(status);
144                 if (elapsedTime > 0 || total_time > 0) {
145 #ifdef NCMPC_MINI
146                         static const char bitrate[1];
147 #else
148                         char bitrate[16];
149 #endif
150                         char elapsed_string[32], duration_string[32];
152                         /*checks the conf to see whether to display elapsed or remaining time */
153                         if (options.display_remaining_time)
154                                 elapsedTime = elapsedTime < total_time
155                                         ? total_time - elapsedTime
156                                         : 0;
158                         /* display bitrate if visible-bitrate is true */
159 #ifndef NCMPC_MINI
160                         format_bitrate(bitrate, sizeof(bitrate), status);
161 #endif
163                         /* write out the time */
164                         format_duration_short(elapsed_string,
165                                               sizeof(elapsed_string),
166                                               elapsedTime);
167                         format_duration_short(duration_string,
168                                               sizeof(duration_string),
169                                               total_time);
171                         g_snprintf(buffer, sizeof(buffer), "%s [%s/%s]",
172                                    bitrate, elapsed_string, duration_string);
173 #ifndef NCMPC_MINI
174                 } else {
175                         format_bitrate(buffer, sizeof(buffer), status);
176 #else
177                         buffer[0] = 0;
178 #endif
179                 }
180         } else {
181                 buffer[0] = 0;
182         }
184         /* display song */
185         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
186                 char songname[p->window.cols * 4 + 1];
187 #ifndef NCMPC_MINI
188                 int width = COLS - x - utf8_width(buffer);
189 #endif
191                 if (song)
192                         strfsong(songname, sizeof(songname),
193                                  options.status_format, song);
194                 else
195                         songname[0] = '\0';
197                 colors_use(w, COLOR_STATUS);
198                 /* scroll if the song name is to long */
199 #ifndef NCMPC_MINI
200                 if (options.scroll && utf8_width(songname) > (unsigned)width) {
201                         hscroll_set(&p->hscroll, x, 0, width, songname);
202                         hscroll_draw(&p->hscroll);
203                 } else {
204                         if (options.scroll)
205                                 hscroll_clear(&p->hscroll);
206                         mvwaddstr(w, 0, x, songname);
207                 }
208 #else
209                 mvwaddstr(w, 0, x, songname);
210 #endif
211 #ifndef NCMPC_MINI
212         } else if (options.scroll) {
213                 hscroll_clear(&p->hscroll);
214 #endif
215         }
217         /* display time string */
218         if (buffer[0] != 0) {
219                 x = p->window.cols - strlen(buffer);
220                 colors_use(w, COLOR_STATUS_TIME);
221                 mvwaddstr(w, 0, x, buffer);
222         }
224         wnoutrefresh(w);
227 void
228 status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
230         p->window.cols = width;
231         wresize(p->window.w, 1, width);
232         mvwin(p->window.w, y, x);
235 static gboolean
236 status_bar_clear_message_cb(gpointer data)
238         struct status_bar *p = data;
239         assert(p->message_source_id != 0);
240         p->message_source_id = 0;
242         status_bar_clear_message(p);
243         return false;
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_seconds(options.status_message_time,
265                                                      status_bar_clear_message_cb, p);