Code

screen: moved code to status_bar.c
[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"
28 #ifndef NCMPC_MINI
29 #include "hscroll.h"
30 #endif
32 #include <mpd/client.h>
34 #include <string.h>
36 void
37 status_bar_paint(const struct status_bar *p, const struct mpd_status *status,
38                  const struct mpd_song *song)
39 {
40         WINDOW *w = p->window.w;
41         enum mpd_state state;
42         int elapsedTime = 0;
43 #ifdef NCMPC_MINI
44         static char bitrate[1];
45 #else
46         char bitrate[16];
47 #endif
48         const char *str = NULL;
49         int x = 0;
50         char buffer[p->window.cols * 4 + 1];
52         if (time(NULL) - p->message_timestamp <= options.status_message_time)
53                 return;
55         wmove(w, 0, 0);
56         wclrtoeol(w);
57         colors_use(w, COLOR_STATUS_BOLD);
59         state = status == NULL ? MPD_STATE_UNKNOWN
60                 : mpd_status_get_state(status);
62         switch (state) {
63         case MPD_STATE_PLAY:
64                 str = _("Playing:");
65                 break;
66         case MPD_STATE_PAUSE:
67                 str = _("[Paused]");
68                 break;
69         case MPD_STATE_STOP:
70         default:
71                 break;
72         }
74         if (str) {
75                 waddstr(w, str);
76                 x += utf8_width(str) + 1;
77         }
79         /* create time string */
80         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
81                 int total_time = mpd_status_get_total_time(status);
82                 if (total_time > 0) {
83                         /*checks the conf to see whether to display elapsed or remaining time */
84                         if(!strcmp(options.timedisplay_type,"elapsed"))
85                                 elapsedTime = mpd_status_get_elapsed_time(status);
86                         else if(!strcmp(options.timedisplay_type,"remaining"))
87                                 elapsedTime = total_time -
88                                         mpd_status_get_elapsed_time(status);
90                         if (song != NULL &&
91                             seek_id == (int)mpd_song_get_id(song))
92                                 elapsedTime = seek_target_time;
94                         /* display bitrate if visible-bitrate is true */
95 #ifndef NCMPC_MINI
96                         if (options.visible_bitrate) {
97                                 g_snprintf(bitrate, 16,
98                                            " [%d kbps]",
99                                            mpd_status_get_kbit_rate(status));
100                         } else {
101                                 bitrate[0] = '\0';
102                         }
103 #endif
105                         /*write out the time, using hours if time over 60 minutes*/
106                         if (total_time > 3600) {
107                                 g_snprintf(buffer, sizeof(buffer),
108                                            "%s [%i:%02i:%02i/%i:%02i:%02i]",
109                                            bitrate, elapsedTime/3600, (elapsedTime%3600)/60, elapsedTime%60,
110                                            total_time / 3600,
111                                            (total_time % 3600)/60,
112                                            total_time % 60);
113                         } else {
114                                 g_snprintf(buffer, sizeof(buffer),
115                                            "%s [%i:%02i/%i:%02i]",
116                                            bitrate, elapsedTime/60, elapsedTime%60,
117                                            total_time / 60, total_time % 60);
118                         }
119 #ifndef NCMPC_MINI
120                 } else {
121                         g_snprintf(buffer, sizeof(buffer),
122                                    " [%d kbps]",
123                                    mpd_status_get_kbit_rate(status));
124 #endif
125                 }
126 #ifndef NCMPC_MINI
127         } else {
128                 if (options.display_time) {
129                         time_t timep;
131                         time(&timep);
132                         strftime(buffer, sizeof(buffer), "%X ",localtime(&timep));
133                 } else
134                         buffer[0] = 0;
135 #endif
136         }
138         /* display song */
139         if (state == MPD_STATE_PLAY || state == MPD_STATE_PAUSE) {
140                 char songname[p->window.cols * 4 + 1];
141 #ifndef NCMPC_MINI
142                 int width = COLS - x - utf8_width(buffer);
143 #endif
145                 if (song)
146                         strfsong(songname, sizeof(songname),
147                                  options.status_format, song);
148                 else
149                         songname[0] = '\0';
151                 colors_use(w, COLOR_STATUS);
152                 /* scroll if the song name is to long */
153 #ifndef NCMPC_MINI
154                 if (options.scroll && utf8_width(songname) > (unsigned)width) {
155                         static  scroll_state_t st = { 0, 0 };
156                         char *tmp = strscroll(songname, options.scroll_sep, width, &st);
158                         g_strlcpy(songname, tmp, sizeof(songname));
159                         g_free(tmp);
160                 }
161 #endif
162                 //mvwaddnstr(w, 0, x, songname, width);
163                 mvwaddstr(w, 0, x, songname);
164         }
166         /* display time string */
167         if (buffer[0] != 0) {
168                 x = p->window.cols - strlen(buffer);
169                 colors_use(w, COLOR_STATUS_TIME);
170                 mvwaddstr(w, 0, x, buffer);
171         }
173         wnoutrefresh(w);
176 void
177 status_bar_resize(struct status_bar *p, unsigned width, int y, int x)
179         p->window.cols = width;
180         wresize(p->window.w, 1, width);
181         mvwin(p->window.w, y, x);
184 void
185 status_bar_message(struct status_bar *p, const char *msg)
187         WINDOW *w = p->window.w;
189         wmove(w, 0, 0);
190         wclrtoeol(w);
191         colors_use(w, COLOR_STATUS_ALERT);
192         waddstr(w, msg);
193         wnoutrefresh(w);
195         p->message_timestamp = time(NULL);