Code

screen_song: support CMD_SCREEN_LYRICS
[ncmpc.git] / src / screen_song.c
1 /*
2  * Copyright (C) 2008 Max Kellermann <max@duempel.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "i18n.h"
20 #include "screen.h"
21 #include "screen_utils.h"
22 #include "charset.h"
24 static list_window_t *lw;
26 struct {
27         struct mpd_song *song;
28         GPtrArray *lines;
29 } current;
31 static void
32 screen_song_clear(void)
33 {
34         for (guint i = 0; i < current.lines->len; ++i)
35                 g_free(g_ptr_array_index(current.lines, i));
37         g_ptr_array_set_size(current.lines, 0);
39         if (current.song != NULL) {
40                 mpd_freeSong(current.song);
41                 current.song = NULL;
42         }
43 }
45 static void
46 screen_song_paint(void);
48 /**
49  * Repaint and update the screen.
50  */
51 static void
52 screen_song_repaint(void)
53 {
54         screen_song_paint();
55         wrefresh(lw->w);
56 }
58 static const char *
59 screen_song_list_callback(unsigned idx, G_GNUC_UNUSED int *highlight,
60                           G_GNUC_UNUSED void *data)
61 {
62         static char buffer[256];
63         char *value;
65         if (idx >= current.lines->len)
66                 return NULL;
68         value = utf8_to_locale(g_ptr_array_index(current.lines, idx));
69         g_strlcpy(buffer, value, sizeof(buffer));
70         g_free(value);
72         return buffer;
73 }
76 static void
77 screen_song_init(WINDOW *w, int cols, int rows)
78 {
79         current.lines = g_ptr_array_new();
80         lw = list_window_init(w, cols, rows);
81         lw->flags = LW_HIDE_CURSOR;
82 }
84 static void
85 screen_song_exit(void)
86 {
87         list_window_free(lw);
89         screen_song_clear();
91         g_ptr_array_free(current.lines, TRUE);
92         current.lines = NULL;
93 }
95 static void
96 screen_song_resize(int cols, int rows)
97 {
98         lw->cols = cols;
99         lw->rows = rows;
102 static const char *
103 screen_song_title(G_GNUC_UNUSED char *str, G_GNUC_UNUSED size_t size)
105         return _("Song viewer");
108 static void
109 screen_song_paint(void)
111         list_window_paint(lw, screen_song_list_callback, NULL);
114 static bool
115 screen_song_cmd(mpdclient_t *c, command_t cmd)
117         if (list_window_scroll_cmd(lw, current.lines->len, cmd)) {
118                 screen_song_repaint();
119                 return true;
120         }
122         switch(cmd) {
123         case CMD_LOCATE:
124                 if (current.song != NULL) {
125                         screen_file_goto_song(c, current.song);
126                         return true;
127                 }
129                 return false;
131 #ifdef ENABLE_LYRICS_SCREEN
132         case CMD_SCREEN_LYRICS:
133                 if (current.song != NULL) {
134                         screen_lyrics_switch(c, current.song);
135                         return true;
136                 }
138                 return false;
139 #endif
141         default:
142                 break;
143         }
145         if (screen_find(lw, current.lines->len,
146                         cmd, screen_song_list_callback, NULL)) {
147                 /* center the row */
148                 list_window_center(lw, current.lines->len, lw->selected);
149                 screen_song_repaint();
150                 return true;
151         }
153         return false;
156 const struct screen_functions screen_song = {
157         .init = screen_song_init,
158         .exit = screen_song_exit,
159         .resize = screen_song_resize,
160         .paint = screen_song_paint,
161         .cmd = screen_song_cmd,
162         .get_title = screen_song_title,
163 };
165 static void
166 screen_song_append(const char *label, const char *value)
168         assert(label != NULL);
170         if (value != NULL)
171                 g_ptr_array_add(current.lines,
172                                 g_strdup_printf("%s: %s", label, value));
175 void
176 screen_song_switch(struct mpdclient *c, const struct mpd_song *song)
178         assert(song != NULL);
179         assert(song->file != NULL);
181         screen_song_clear();
183         current.song = mpd_songDup(song);
185         g_ptr_array_add(current.lines, g_strdup(song->file));
187         screen_song_append(_("Artist"), song->artist);
188         screen_song_append(_("Title"), song->title);
189         screen_song_append(_("Album"), song->album);
190         screen_song_append(_("Composer"), song->composer);
191         screen_song_append(_("Name"), song->name);
192         screen_song_append(_("Disc"), song->disc);
193         screen_song_append(_("Track"), song->track);
194         screen_song_append(_("Date"), song->date);
195         screen_song_append(_("Genre"), song->genre);
196         screen_song_append(_("Comment"), song->comment);
198         screen_switch(&screen_song, c);