Code

screen_song: moved code to format_duration_short()
[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 #ifndef NCMPC_MINI
30 #include "hscroll.h"
31 #endif
33 #include <mpd/client.h>
35 #include <assert.h>
36 #include <string.h>
38 static gboolean
39 status_bar_clear_message(gpointer data)
40 {
41         struct status_bar *p = data;
42         WINDOW *w = p->window.w;
44         assert(p != NULL);
45         assert(p->message_source_id != 0);
47         p->message_source_id = 0;
49         wmove(w, 0, 0);
50         wclrtoeol(w);
51         wrefresh(w);
53         return false;
54 }
56 void
57 status_bar_paint(const struct status_bar *p, const struct mpd_status *status,
58                  const struct mpd_song *song)
59 {
60         WINDOW *w = p->window.w;
61         enum mpd_state state;
62         int elapsedTime = 0;
63 #ifdef NCMPC_MINI
64         static char bitrate[1];
65 #else
66         char bitrate[16];
67 #endif
68         const char *str = NULL;
69         int x = 0;
70         char buffer[p->window.cols * 4 + 1];
72         if (p->message_source_id != 0)
73                 return;
75         wmove(w, 0, 0);
76         wclrtoeol(w);
77         colors_use(w, COLOR_STATUS_BOLD);
79         state = status == NULL ? MPD_STATE_UNKNOWN
80                 : mpd_status_get_state(status);
82         switch (state) {
83         case MPD_STATE_PLAY:
84                 str = _("Playing:");
85                 break;
86         case MPD_STATE_PAUSE:
87                 str = _("[Paused]");
88                 break;
89         case MPD_STATE_STOP:
90         default:
91                 break;
92         }
94         if (str) {
95                 waddstr(w, str);
96                 x += utf8_width(str) + 1;
97         }
99         /* create time string */
100         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
101                 int total_time = mpd_status_get_total_time(status);
102                 if (total_time > 0) {
103                         char elapsed_string[32], duration_string[32];
105                         /*checks the conf to see whether to display elapsed or remaining time */
106                         if(!strcmp(options.timedisplay_type,"elapsed"))
107                                 elapsedTime = mpd_status_get_elapsed_time(status);
108                         else if(!strcmp(options.timedisplay_type,"remaining"))
109                                 elapsedTime = total_time -
110                                         mpd_status_get_elapsed_time(status);
112                         if (song != NULL &&
113                             seek_id == (int)mpd_song_get_id(song))
114                                 elapsedTime = seek_target_time;
116                         /* display bitrate if visible-bitrate is true */
117 #ifndef NCMPC_MINI
118                         if (options.visible_bitrate) {
119                                 g_snprintf(bitrate, 16,
120                                            " [%d kbps]",
121                                            mpd_status_get_kbit_rate(status));
122                         } else {
123                                 bitrate[0] = '\0';
124                         }
125 #endif
127                         /* write out the time */
128                         format_duration_short(elapsed_string,
129                                               sizeof(elapsed_string),
130                                               elapsedTime);
131                         format_duration_short(duration_string,
132                                               sizeof(duration_string),
133                                               total_time);
135                         g_snprintf(buffer, sizeof(buffer), "%s [%s/%s]",
136                                    bitrate, elapsed_string, duration_string);
137 #ifndef NCMPC_MINI
138                 } else {
139                         g_snprintf(buffer, sizeof(buffer),
140                                    " [%d kbps]",
141                                    mpd_status_get_kbit_rate(status));
142 #endif
143                 }
144 #ifndef NCMPC_MINI
145         } else {
146                 if (options.display_time) {
147                         time_t timep;
149                         time(&timep);
150                         strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
151                 } else
152 #endif
153                         buffer[0] = 0;
154         }
156         /* display song */
157         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
158                 char songname[p->window.cols * 4 + 1];
159 #ifndef NCMPC_MINI
160                 int width = COLS - x - utf8_width(buffer);
161 #endif
163                 if (song)
164                         strfsong(songname, sizeof(songname),
165                                  options.status_format, song);
166                 else
167                         songname[0] = '\0';
169                 colors_use(w, COLOR_STATUS);
170                 /* scroll if the song name is to long */
171 #ifndef NCMPC_MINI
172                 if (options.scroll && utf8_width(songname) > (unsigned)width) {
173                         static  scroll_state_t st = { 0, 0 };
174                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
176                         g_strlcpy(songname, tmp, sizeof(songname));
177                         g_free(tmp);
178                 }
179 #endif
180                 //mvwaddnstr(w, 0, x, songname, width);
181                 mvwaddstr(w, 0, x, songname);
182         }
184         /* display time string */
185         if (buffer[0] != 0) {
186                 x = p->window.cols - strlen(buffer);
187                 colors_use(w, COLOR_STATUS_TIME);
188                 mvwaddstr(w, 0, x, buffer);
189         }
191         wnoutrefresh(w);
194 void
195 status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
197         p->window.cols = width;
198         wresize(p->window.w, 1, width);
199         mvwin(p->window.w, y, x);
202 void
203 status_bar_message(struct status_bar *p, const char *msg)
205         WINDOW *w = p->window.w;
207         wmove(w, 0, 0);
208         wclrtoeol(w);
209         colors_use(w, COLOR_STATUS_ALERT);
210         waddstr(w, msg);
211         wnoutrefresh(w);
213         if (p->message_source_id != 0)
214                 g_source_remove(p->message_source_id);
215         p->message_source_id = g_timeout_add(options.status_message_time * 1000,
216                                              status_bar_clear_message, p);