1 /*
2 * $Id$
3 *
4 * (c) 2004 by Kalle Wallin <kaw@linux.se>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 */
21 #include "config.h"
22 #include "ncmpc.h"
23 #include "mpdclient.h"
24 #include "command.h"
25 #include "screen.h"
26 #include "screen_utils.h"
27 #include "gcc.h"
29 #include <stdlib.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <ncurses.h>
35 typedef struct {
36 signed char highlight;
37 command_t command;
38 const char *text;
39 } help_text_row_t;
41 static help_text_row_t help_text[] =
42 {
43 { 1, CMD_NONE, N_("Keys - Movement") },
44 { 2, CMD_NONE, NULL },
45 { 0, CMD_LIST_PREVIOUS, NULL },
46 { 0, CMD_LIST_NEXT, NULL },
47 { 0, CMD_LIST_PREVIOUS_PAGE, NULL },
48 { 0, CMD_LIST_NEXT_PAGE, NULL },
49 { 0, CMD_LIST_FIRST, NULL },
50 { 0, CMD_LIST_LAST, NULL },
51 { 0, CMD_NONE, NULL },
52 { 0, CMD_SCREEN_PREVIOUS,NULL },
53 { 0, CMD_SCREEN_NEXT, NULL },
54 { 0, CMD_SCREEN_HELP, NULL },
55 { 0, CMD_SCREEN_PLAY, NULL },
56 { 0, CMD_SCREEN_FILE, NULL },
57 #ifdef ENABLE_SEARCH_SCREEN
58 { 0, CMD_SCREEN_SEARCH, NULL },
59 #endif
60 #ifdef ENABLE_KEYDEF_SCREEN
61 { 0, CMD_SCREEN_KEYDEF, NULL },
62 #endif
64 { 0, CMD_NONE, NULL },
65 { 0, CMD_NONE, NULL },
66 { 1, CMD_NONE, N_("Keys - Global") },
67 { 2, CMD_NONE, NULL },
68 { 0, CMD_STOP, NULL },
69 { 0, CMD_PAUSE, NULL },
70 { 0, CMD_CROP, NULL },
71 { 0, CMD_TRACK_NEXT, NULL },
72 { 0, CMD_TRACK_PREVIOUS, NULL },
73 { 0, CMD_SEEK_FORWARD, NULL },
74 { 0, CMD_SEEK_BACKWARD, NULL },
75 { 0, CMD_VOLUME_DOWN, NULL },
76 { 0, CMD_VOLUME_UP, NULL },
77 { 0, CMD_NONE, NULL },
78 { 0, CMD_REPEAT, NULL },
79 { 0, CMD_RANDOM, NULL },
80 { 0, CMD_CROSSFADE, NULL },
81 { 0, CMD_SHUFFLE, NULL },
82 { 0, CMD_DB_UPDATE, NULL },
83 { 0, CMD_NONE, NULL },
84 { 0, CMD_LIST_FIND, NULL },
85 { 0, CMD_LIST_RFIND, NULL },
86 { 0, CMD_LIST_FIND_NEXT, NULL },
87 { 0, CMD_LIST_RFIND_NEXT, NULL },
88 { 0, CMD_TOGGLE_FIND_WRAP, NULL },
89 { 0, CMD_NONE, NULL },
90 { 0, CMD_QUIT, NULL },
92 { 0, CMD_NONE, NULL },
93 { 0, CMD_NONE, NULL },
94 { 1, CMD_NONE, N_("Keys - Playlist screen") },
95 { 2, CMD_NONE, NULL },
96 { 0, CMD_PLAY, N_("Play") },
97 { 0, CMD_DELETE, NULL },
98 { 0, CMD_CLEAR, NULL },
99 { 1, CMD_LIST_MOVE_UP, N_("Move song up") },
100 { 0, CMD_LIST_MOVE_DOWN, N_("Move song down") },
101 { 0, CMD_ADD, NULL },
102 { 0, CMD_SAVE_PLAYLIST, NULL },
103 { 0, CMD_SCREEN_UPDATE, N_("Center") },
104 { 0, CMD_TOGGLE_AUTOCENTER, NULL },
106 { 0, CMD_NONE, NULL },
107 { 0, CMD_NONE, NULL },
108 { 1, CMD_NONE, N_("Keys - Browse screen") },
109 { 2, CMD_NONE, NULL },
110 { 0, CMD_PLAY, N_("Enter directory/Select and play song") },
111 { 0, CMD_SELECT, NULL },
112 { 0, CMD_SAVE_PLAYLIST, NULL },
113 { 0, CMD_DELETE, N_("Delete playlist") },
114 { 0, CMD_GO_PARENT_DIRECTORY, NULL },
115 { 0, CMD_GO_ROOT_DIRECTORY, NULL },
116 { 0, CMD_SCREEN_UPDATE, NULL },
118 #ifdef ENABLE_SEARCH_SCREEN
119 { 0, CMD_NONE, NULL },
120 { 0, CMD_NONE, NULL },
121 { 1, CMD_NONE, N_("Keys - Search screen") },
122 { 2, CMD_NONE, NULL },
123 { 0, CMD_SCREEN_SEARCH, N_("Search") },
124 { 0, CMD_PLAY, N_("Select and play") },
125 { 0, CMD_SELECT, NULL },
126 { 0, CMD_SELECT_ALL, NULL },
127 { 0, CMD_SEARCH_MODE, NULL },
128 #endif
129 #ifdef ENABLE_LYRICS_SCREEN
130 { 0, CMD_NONE, NULL },
131 { 0, CMD_NONE, NULL },
132 { 1, CMD_NONE, N_("Keys - Lyrics screen") },
133 { 2, CMD_NONE, NULL },
134 { 0, CMD_SCREEN_LYRICS, N_("View Lyrics") },
135 { 0, CMD_SELECT, N_("(Re)load lyrics") },
136 { 0, CMD_INTERRUPT, N_("Interrupt retrieval") },
137 { 0, CMD_LYRICS_UPDATE, N_("Explicitly download lyrics") },
138 { 0, CMD_ADD, N_("Save lyrics") },
139 #endif
140 };
142 #define help_text_rows (sizeof(help_text) / sizeof(help_text[0]))
144 static list_window_t *lw = NULL;
147 static const char *
148 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
149 {
150 static char buf[512];
152 if (idx >= help_text_rows)
153 return NULL;
155 if (help_text[idx].highlight)
156 *highlight = 1;
158 if (help_text[idx].command == CMD_NONE) {
159 if (help_text[idx].text)
160 g_snprintf(buf, sizeof(buf), " %s", _(help_text[idx].text));
161 else if (help_text[idx].highlight == 2) {
162 int i;
164 for (i = 3; i < COLS - 3 && i < (int)sizeof(buf); i++)
165 buf[i] = '-';
166 buf[i] = '\0';
167 } else
168 g_strlcpy(buf, " ", sizeof(buf));
169 return buf;
170 }
172 if (help_text[idx].text)
173 g_snprintf(buf, sizeof(buf),
174 "%20s : %s ",
175 get_key_names(help_text[idx].command, TRUE),
176 _(help_text[idx].text));
177 else
178 g_snprintf(buf, sizeof(buf),
179 "%20s : %s ",
180 get_key_names(help_text[idx].command, TRUE),
181 get_key_description(help_text[idx].command));
182 return buf;
183 }
185 static void
186 help_init(WINDOW *w, int cols, int rows)
187 {
188 lw = list_window_init(w, cols, rows);
189 lw->flags = LW_HIDE_CURSOR;
190 }
192 static void
193 help_resize(int cols, int rows)
194 {
195 lw->cols = cols;
196 lw->rows = rows;
197 }
199 static void
200 help_exit(void)
201 {
202 list_window_free(lw);
203 }
206 static const char *
207 help_title(mpd_unused char *str, mpd_unused size_t size)
208 {
209 return _("Help");
210 }
212 static void
213 help_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
214 {
215 lw->clear = 1;
216 list_window_paint(lw, list_callback, NULL);
217 wrefresh(lw->w);
218 }
220 static int
221 help_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
222 {
223 if (list_window_scroll_cmd(lw, help_text_rows, cmd)) {
224 list_window_paint(lw, list_callback, NULL);
225 wrefresh(lw->w);
226 return 1;
227 }
229 lw->selected = lw->start+lw->rows;
230 if (screen_find(screen,
231 lw, help_text_rows,
232 cmd, list_callback, NULL)) {
233 /* center the row */
234 list_window_center(lw, help_text_rows, lw->selected);
235 list_window_paint(lw, list_callback, NULL);
236 wrefresh(lw->w);
237 return 1;
238 }
240 return 0;
241 }
243 const struct screen_functions screen_help = {
244 .init = help_init,
245 .exit = help_exit,
246 .resize = help_resize,
247 .paint = help_paint,
248 .cmd = help_cmd,
249 .get_title = help_title,
250 };