Code

Update copyright notices
[ncmpc.git] / src / status_bar.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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         const char *str = NULL;
104         int x = 0;
105         char buffer[p->window.cols * 4 + 1];
107 #ifndef NCMPC_MINI
108         p->prev_status = status;
109         p->prev_song = song;
110 #endif
112         if (p->message_source_id != 0)
113                 return;
115         wmove(w, 0, 0);
116         wclrtoeol(w);
117         colors_use(w, COLOR_STATUS_BOLD);
119         state = status == NULL ? MPD_STATE_UNKNOWN
120                 : mpd_status_get_state(status);
122         switch (state) {
123         case MPD_STATE_PLAY:
124                 str = _("Playing:");
125                 break;
126         case MPD_STATE_PAUSE:
127                 str = _("[Paused]");
128                 break;
129         case MPD_STATE_STOP:
130         default:
131                 break;
132         }
134         if (str) {
135                 waddstr(w, str);
136                 x += utf8_width(str) + 1;
137         }
139         /* create time string */
140         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
141                 int elapsedTime = seek_id >= 0 &&
142                         seek_id == mpd_status_get_song_id(status)
143                         ? (unsigned)seek_target_time
144                         : mpd_status_get_elapsed_time(status);
145                 int total_time = mpd_status_get_total_time(status);
146                 if (elapsedTime > 0 || total_time > 0) {
147 #ifdef NCMPC_MINI
148                         static const char bitrate[1];
149 #else
150                         char bitrate[16];
151 #endif
152                         char elapsed_string[32], duration_string[32];
154                         /*checks the conf to see whether to display elapsed or remaining time */
155                         if (options.display_remaining_time)
156                                 elapsedTime = elapsedTime < total_time
157                                         ? total_time - elapsedTime
158                                         : 0;
160                         /* display bitrate if visible-bitrate is true */
161 #ifndef NCMPC_MINI
162                         format_bitrate(bitrate, sizeof(bitrate), status);
163 #endif
165                         /* write out the time */
166                         format_duration_short(elapsed_string,
167                                               sizeof(elapsed_string),
168                                               elapsedTime);
169                         format_duration_short(duration_string,
170                                               sizeof(duration_string),
171                                               total_time);
173                         g_snprintf(buffer, sizeof(buffer), "%s [%s/%s]",
174                                    bitrate, elapsed_string, duration_string);
175 #ifndef NCMPC_MINI
176                 } else {
177                         format_bitrate(buffer, sizeof(buffer), status);
178 #else
179                         buffer[0] = 0;
180 #endif
181                 }
182         } else {
183 #ifndef NCMPC_MINI
184                 if (options.display_time) {
185                         time_t timep;
187                         time(&timep);
188                         strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
189                 } else
190 #endif
191                         buffer[0] = 0;
192         }
194         /* display song */
195         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
196                 char songname[p->window.cols * 4 + 1];
197 #ifndef NCMPC_MINI
198                 int width = COLS - x - utf8_width(buffer);
199 #endif
201                 if (song)
202                         strfsong(songname, sizeof(songname),
203                                  options.status_format, song);
204                 else
205                         songname[0] = '\0';
207                 colors_use(w, COLOR_STATUS);
208                 /* scroll if the song name is to long */
209 #ifndef NCMPC_MINI
210                 if (options.scroll && utf8_width(songname) > (unsigned)width) {
211                         hscroll_set(&p->hscroll, x, 0, width, songname);
212                         hscroll_draw(&p->hscroll);
213                 } else {
214                         if (options.scroll)
215                                 hscroll_clear(&p->hscroll);
216                         mvwaddstr(w, 0, x, songname);
217                 }
218 #else
219                 mvwaddstr(w, 0, x, songname);
220 #endif
221 #ifndef NCMPC_MINI
222         } else if (options.scroll) {
223                 hscroll_clear(&p->hscroll);
224 #endif
225         }
227         /* display time string */
228         if (buffer[0] != 0) {
229                 x = p->window.cols - strlen(buffer);
230                 colors_use(w, COLOR_STATUS_TIME);
231                 mvwaddstr(w, 0, x, buffer);
232         }
234         wnoutrefresh(w);
237 void
238 status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
240         p->window.cols = width;
241         wresize(p->window.w, 1, width);
242         mvwin(p->window.w, y, x);
245 void
246 status_bar_message(struct status_bar *p, const char *msg)
248         WINDOW *w = p->window.w;
250 #ifndef NCMPC_MINI
251         if (options.scroll)
252                 hscroll_clear(&p->hscroll);
253 #endif
255         wmove(w, 0, 0);
256         wclrtoeol(w);
257         colors_use(w, COLOR_STATUS_ALERT);
258         waddstr(w, msg);
259         wnoutrefresh(w);
261         if (p->message_source_id != 0)
262                 g_source_remove(p->message_source_id);
263         p->message_source_id = g_timeout_add(options.status_message_time * 1000,
264                                              status_bar_clear_message, p);