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 <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <ncurses.h>
26 #include "config.h"
27 #include "ncmpc.h"
28 #include "mpdclient.h"
29 #include "command.h"
30 #include "screen.h"
31 #include "screen_utils.h"
34 typedef struct {
35 signed char highlight;
36 command_t command;
37 char *text;
38 } help_text_row_t;
40 static help_text_row_t help_text[] =
41 {
42 { 1, CMD_NONE, N_("Keys - Movement") },
43 { 2, CMD_NONE, NULL },
44 { 0, CMD_LIST_PREVIOUS, NULL },
45 { 0, CMD_LIST_NEXT, NULL },
46 { 0, CMD_LIST_PREVIOUS_PAGE, NULL },
47 { 0, CMD_LIST_NEXT_PAGE, NULL },
48 { 0, CMD_LIST_FIRST, NULL },
49 { 0, CMD_LIST_LAST, NULL },
50 { 0, CMD_NONE, NULL },
51 { 0, CMD_SCREEN_PREVIOUS,NULL },
52 { 0, CMD_SCREEN_NEXT, NULL },
53 { 0, CMD_SCREEN_HELP, NULL },
54 { 0, CMD_SCREEN_PLAY, NULL },
55 { 0, CMD_SCREEN_FILE, NULL },
56 #ifdef ENABLE_SEARCH_SCREEN
57 { 0, CMD_SCREEN_SEARCH, NULL },
58 #endif
59 #ifdef ENABLE_CLOCK_SCREEN
60 { 0, CMD_SCREEN_CLOCK, NULL },
61 #endif
62 #ifdef ENABLE_KEYDEF_SCREEN
63 { 0, CMD_SCREEN_KEYDEF, NULL },
64 #endif
66 { 0, CMD_NONE, NULL },
67 { 0, CMD_NONE, NULL },
68 { 1, CMD_NONE, N_("Keys - Global") },
69 { 2, CMD_NONE, NULL },
70 { 0, CMD_STOP, NULL },
71 { 0, CMD_PAUSE, NULL },
72 { 0, CMD_TRACK_NEXT, NULL },
73 { 0, CMD_TRACK_PREVIOUS, NULL },
74 { 0, CMD_SEEK_FORWARD, NULL },
75 { 0, CMD_SEEK_BACKWARD, NULL },
76 { 0, CMD_VOLUME_DOWN, NULL },
77 { 0, CMD_VOLUME_UP, NULL },
78 { 0, CMD_NONE, NULL },
79 { 0, CMD_REPEAT, NULL },
80 { 0, CMD_RANDOM, NULL },
81 { 0, CMD_CROSSFADE, NULL },
82 { 0, CMD_SHUFFLE, NULL },
83 { 0, CMD_DB_UPDATE, NULL },
84 { 0, CMD_NONE, NULL },
85 { 0, CMD_LIST_FIND, NULL },
86 { 0, CMD_LIST_RFIND, NULL },
87 { 0, CMD_LIST_FIND_NEXT, NULL },
88 { 0, CMD_LIST_RFIND_NEXT, NULL },
89 { 0, CMD_TOGGLE_FIND_WRAP, NULL },
90 { 0, CMD_NONE, NULL },
91 { 0, CMD_QUIT, NULL },
93 { 0, CMD_NONE, NULL },
94 { 0, CMD_NONE, NULL },
95 { 1, CMD_NONE, N_("Keys - Playlist screen") },
96 { 2, CMD_NONE, NULL },
97 { 0, CMD_PLAY, N_("Play") },
98 { 0, CMD_DELETE, NULL },
99 { 0, CMD_CLEAR, NULL },
100 { 1, CMD_LIST_MOVE_UP, N_("Move song up") },
101 { 0, CMD_LIST_MOVE_DOWN, N_("Move song down") },
102 { 0, CMD_ADD, NULL },
103 { 0, CMD_SAVE_PLAYLIST, NULL },
104 { 0, CMD_SCREEN_UPDATE, N_("Center") },
105 { 0, CMD_TOGGLE_AUTOCENTER, NULL },
107 { 0, CMD_NONE, NULL },
108 { 0, CMD_NONE, NULL },
109 { 1, CMD_NONE, N_("Keys - Browse screen") },
110 { 2, CMD_NONE, NULL },
111 { 0, CMD_PLAY, N_("Enter directory/Select and play song") },
112 { 0, CMD_SELECT, NULL },
113 { 0, CMD_SAVE_PLAYLIST, NULL },
114 { 0, CMD_DELETE, N_("Delete playlist") },
115 { 0, CMD_GO_PARENT_DIRECTORY, NULL },
116 { 0, CMD_GO_ROOT_DIRECTORY, NULL },
117 { 0, CMD_SCREEN_UPDATE, NULL },
119 #ifdef ENABLE_SEARCH_SCREEN
120 { 0, CMD_NONE, NULL },
121 { 0, CMD_NONE, NULL },
122 { 1, CMD_NONE, N_("Keys - Search screen") },
123 { 2, CMD_NONE, NULL },
124 { 0, CMD_SCREEN_SEARCH, N_("Search") },
125 { 0, CMD_PLAY, N_("Select and play") },
126 { 0, CMD_SELECT, NULL },
127 { 0, CMD_SELECT_ALL, NULL },
128 { 0, CMD_SEARCH_MODE, NULL },
129 #endif
130 #ifdef ENABLE_LYRICS_SCREEN
131 { 0, CMD_NONE, NULL },
132 { 0, CMD_NONE, NULL },
133 { 1, CMD_NONE, N_("Keys - Lyrics screen") },
134 { 2, CMD_NONE, NULL },
135 { 0, CMD_SCREEN_LYRICS, N_("View Lyrics") },
136 { 0, CMD_SELECT, N_("(Re)load lyrics") },
137 { 0, CMD_INTERRUPT, N_("Interrupt retrieval") },
138 { 0, CMD_LYRICS_UPDATE, N_("Explicitly download lyrics") },
139 { 0, CMD_ADD, N_("Save lyrics") },
140 #endif
141 { 0, CMD_NONE, NULL },
142 {-1, CMD_NONE, NULL }
143 };
145 static int help_text_rows = -1;
146 static list_window_t *lw = NULL;
149 static char *
150 list_callback(int index, int *highlight, void *data)
151 {
152 static char buf[512];
154 if (help_text_rows < 0) {
155 help_text_rows = 0;
156 while (help_text[help_text_rows].highlight != -1)
157 help_text_rows++;
158 }
160 *highlight = 0;
161 if (index < help_text_rows) {
162 *highlight = help_text[index].highlight > 0;
163 if (help_text[index].command == CMD_NONE) {
164 if (help_text[index].text)
165 g_snprintf(buf, sizeof(buf), " %s", _(help_text[index].text));
166 else if (help_text[index].highlight == 2) {
167 int i;
169 for (i = 3; i < COLS - 3 && i < sizeof(buf); i++)
170 buf[i] = '-';
171 buf[i] = '\0';
172 } else
173 g_strlcpy(buf, " ", sizeof(buf));
174 return buf;
175 }
177 if (help_text[index].text)
178 g_snprintf(buf, sizeof(buf),
179 "%20s : %s ",
180 get_key_names(help_text[index].command, TRUE),
181 _(help_text[index].text));
182 else
183 g_snprintf(buf, sizeof(buf),
184 "%20s : %s ",
185 get_key_names(help_text[index].command, TRUE),
186 get_key_description(help_text[index].command));
187 return buf;
188 }
190 return NULL;
191 }
193 static void
194 help_init(WINDOW *w, int cols, int rows)
195 {
196 lw = list_window_init(w, cols, rows);
197 lw->flags = LW_HIDE_CURSOR;
198 }
200 static void
201 help_resize(int cols, int rows)
202 {
203 lw->cols = cols;
204 lw->rows = rows;
205 }
207 static void
208 help_exit(void)
209 {
210 list_window_free(lw);
211 }
214 static char *
215 help_title(char *str, size_t size)
216 {
217 return _("Help");
218 }
220 static void
221 help_paint(screen_t *screen, mpdclient_t *c)
222 {
223 lw->clear = 1;
224 list_window_paint(lw, list_callback, NULL);
225 wrefresh(lw->w);
226 }
228 static void
229 help_update(screen_t *screen, mpdclient_t *c)
230 {
231 if (lw->repaint) {
232 list_window_paint(lw, list_callback, NULL);
233 wrefresh(lw->w);
234 lw->repaint = 0;
235 }
236 }
239 static int
240 help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
241 {
242 lw->repaint=1;
243 lw->clear=1;
244 switch(cmd) {
245 case CMD_LIST_NEXT:
246 if (lw->start + lw->rows < help_text_rows)
247 lw->start++;
248 return 1;
249 case CMD_LIST_PREVIOUS:
250 if (lw->start > 0)
251 lw->start--;
252 return 1;
253 case CMD_LIST_FIRST:
254 lw->start = 0;
255 return 1;
256 case CMD_LIST_LAST:
257 lw->start = help_text_rows - lw->rows;
258 if (lw->start < 0)
259 lw->start = 0;
260 return 1;
261 case CMD_LIST_NEXT_PAGE:
262 lw->start = lw->start + lw->rows;
263 if (lw->start + lw->rows >= help_text_rows)
264 lw->start = help_text_rows - lw->rows;
265 if (lw->start < 0)
266 lw->start = 0;
267 return 1;
268 case CMD_LIST_PREVIOUS_PAGE:
269 lw->start = lw->start - lw->rows;
270 if (lw->start < 0)
271 lw->start = 0;
272 return 1;
273 default:
274 break;
275 }
277 lw->selected = lw->start+lw->rows;
278 if (screen_find(screen,
279 lw, help_text_rows,
280 cmd, list_callback, NULL)) {
281 /* center the row */
282 lw->start = lw->selected - (lw->rows / 2);
283 if (lw->start + lw->rows > help_text_rows)
284 lw->start = help_text_rows - lw->rows;
285 if (lw->start < 0)
286 lw->start = 0;
287 return 1;
288 }
290 return 0;
291 }
293 static list_window_t *
294 help_lw(void)
295 {
296 return lw;
297 }
299 screen_functions_t *
300 get_screen_help(void)
301 {
302 static screen_functions_t functions;
304 memset(&functions, 0, sizeof(screen_functions_t));
305 functions.init = help_init;
306 functions.exit = help_exit;
307 functions.open = NULL;
308 functions.close = NULL;
309 functions.resize = help_resize;
310 functions.paint = help_paint;
311 functions.update = help_update;
312 functions.cmd = help_cmd;
313 functions.get_lw = help_lw;
314 functions.get_title = help_title;
316 return &functions;
317 }