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_CLOCK_SCREEN
61 { 0, CMD_SCREEN_CLOCK, NULL },
62 #endif
63 #ifdef ENABLE_KEYDEF_SCREEN
64 { 0, CMD_SCREEN_KEYDEF, NULL },
65 #endif
67 { 0, CMD_NONE, NULL },
68 { 0, CMD_NONE, NULL },
69 { 1, CMD_NONE, N_("Keys - Global") },
70 { 2, CMD_NONE, NULL },
71 { 0, CMD_STOP, NULL },
72 { 0, CMD_PAUSE, NULL },
73 { 0, CMD_TRACK_NEXT, NULL },
74 { 0, CMD_TRACK_PREVIOUS, NULL },
75 { 0, CMD_SEEK_FORWARD, NULL },
76 { 0, CMD_SEEK_BACKWARD, NULL },
77 { 0, CMD_VOLUME_DOWN, NULL },
78 { 0, CMD_VOLUME_UP, NULL },
79 { 0, CMD_NONE, NULL },
80 { 0, CMD_REPEAT, NULL },
81 { 0, CMD_RANDOM, NULL },
82 { 0, CMD_CROSSFADE, NULL },
83 { 0, CMD_SHUFFLE, NULL },
84 { 0, CMD_DB_UPDATE, NULL },
85 { 0, CMD_NONE, NULL },
86 { 0, CMD_LIST_FIND, NULL },
87 { 0, CMD_LIST_RFIND, NULL },
88 { 0, CMD_LIST_FIND_NEXT, NULL },
89 { 0, CMD_LIST_RFIND_NEXT, NULL },
90 { 0, CMD_TOGGLE_FIND_WRAP, NULL },
91 { 0, CMD_NONE, NULL },
92 { 0, CMD_QUIT, NULL },
94 { 0, CMD_NONE, NULL },
95 { 0, CMD_NONE, NULL },
96 { 1, CMD_NONE, N_("Keys - Playlist screen") },
97 { 2, CMD_NONE, NULL },
98 { 0, CMD_PLAY, N_("Play") },
99 { 0, CMD_DELETE, NULL },
100 { 0, CMD_CLEAR, NULL },
101 { 1, CMD_LIST_MOVE_UP, N_("Move song up") },
102 { 0, CMD_LIST_MOVE_DOWN, N_("Move song down") },
103 { 0, CMD_ADD, NULL },
104 { 0, CMD_SAVE_PLAYLIST, NULL },
105 { 0, CMD_SCREEN_UPDATE, N_("Center") },
106 { 0, CMD_TOGGLE_AUTOCENTER, NULL },
108 { 0, CMD_NONE, NULL },
109 { 0, CMD_NONE, NULL },
110 { 1, CMD_NONE, N_("Keys - Browse screen") },
111 { 2, CMD_NONE, NULL },
112 { 0, CMD_PLAY, N_("Enter directory/Select and play song") },
113 { 0, CMD_SELECT, NULL },
114 { 0, CMD_SAVE_PLAYLIST, NULL },
115 { 0, CMD_DELETE, N_("Delete playlist") },
116 { 0, CMD_GO_PARENT_DIRECTORY, NULL },
117 { 0, CMD_GO_ROOT_DIRECTORY, NULL },
118 { 0, CMD_SCREEN_UPDATE, NULL },
120 #ifdef ENABLE_SEARCH_SCREEN
121 { 0, CMD_NONE, NULL },
122 { 0, CMD_NONE, NULL },
123 { 1, CMD_NONE, N_("Keys - Search screen") },
124 { 2, CMD_NONE, NULL },
125 { 0, CMD_SCREEN_SEARCH, N_("Search") },
126 { 0, CMD_PLAY, N_("Select and play") },
127 { 0, CMD_SELECT, NULL },
128 { 0, CMD_SELECT_ALL, NULL },
129 { 0, CMD_SEARCH_MODE, NULL },
130 #endif
131 #ifdef ENABLE_LYRICS_SCREEN
132 { 0, CMD_NONE, NULL },
133 { 0, CMD_NONE, NULL },
134 { 1, CMD_NONE, N_("Keys - Lyrics screen") },
135 { 2, CMD_NONE, NULL },
136 { 0, CMD_SCREEN_LYRICS, N_("View Lyrics") },
137 { 0, CMD_SELECT, N_("(Re)load lyrics") },
138 { 0, CMD_INTERRUPT, N_("Interrupt retrieval") },
139 { 0, CMD_LYRICS_UPDATE, N_("Explicitly download lyrics") },
140 { 0, CMD_ADD, N_("Save lyrics") },
141 #endif
142 { 0, CMD_NONE, NULL },
143 {-1, CMD_NONE, NULL }
144 };
146 static int help_text_rows = -1;
147 static list_window_t *lw = NULL;
150 static const char *
151 list_callback(unsigned idx, int *highlight, mpd_unused void *data)
152 {
153 static char buf[512];
155 if (help_text_rows < 0) {
156 help_text_rows = 0;
157 while (help_text[help_text_rows].highlight != -1)
158 help_text_rows++;
159 }
161 *highlight = 0;
162 if (idx < (unsigned)help_text_rows) {
163 *highlight = help_text[idx].highlight > 0;
164 if (help_text[idx].command == CMD_NONE) {
165 if (help_text[idx].text)
166 g_snprintf(buf, sizeof(buf), " %s", _(help_text[idx].text));
167 else if (help_text[idx].highlight == 2) {
168 int i;
170 for (i = 3; i < COLS - 3 && i < (int)sizeof(buf); i++)
171 buf[i] = '-';
172 buf[i] = '\0';
173 } else
174 g_strlcpy(buf, " ", sizeof(buf));
175 return buf;
176 }
178 if (help_text[idx].text)
179 g_snprintf(buf, sizeof(buf),
180 "%20s : %s ",
181 get_key_names(help_text[idx].command, TRUE),
182 _(help_text[idx].text));
183 else
184 g_snprintf(buf, sizeof(buf),
185 "%20s : %s ",
186 get_key_names(help_text[idx].command, TRUE),
187 get_key_description(help_text[idx].command));
188 return buf;
189 }
191 return NULL;
192 }
194 static void
195 help_init(WINDOW *w, int cols, int rows)
196 {
197 lw = list_window_init(w, cols, rows);
198 lw->flags = LW_HIDE_CURSOR;
199 }
201 static void
202 help_resize(int cols, int rows)
203 {
204 lw->cols = cols;
205 lw->rows = rows;
206 }
208 static void
209 help_exit(void)
210 {
211 list_window_free(lw);
212 }
215 static const char *
216 help_title(mpd_unused char *str, mpd_unused size_t size)
217 {
218 return _("Help");
219 }
221 static void
222 help_paint(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
223 {
224 lw->clear = 1;
225 list_window_paint(lw, list_callback, NULL);
226 wrefresh(lw->w);
227 }
229 static void
230 help_update(mpd_unused screen_t *screen, mpd_unused mpdclient_t *c)
231 {
232 if (lw->repaint) {
233 list_window_paint(lw, list_callback, NULL);
234 wrefresh(lw->w);
235 lw->repaint = 0;
236 }
237 }
240 static int
241 help_cmd(screen_t *screen, mpd_unused mpdclient_t *c, command_t cmd)
242 {
243 lw->repaint=1;
244 lw->clear=1;
245 switch(cmd) {
246 case CMD_LIST_NEXT:
247 if (lw->start + lw->rows < (unsigned)help_text_rows)
248 lw->start++;
249 return 1;
250 case CMD_LIST_PREVIOUS:
251 if (lw->start > 0)
252 lw->start--;
253 return 1;
254 case CMD_LIST_FIRST:
255 lw->start = 0;
256 return 1;
257 case CMD_LIST_LAST:
258 if ((unsigned)help_text_rows > lw->rows)
259 lw->start = help_text_rows - lw->rows;
260 else
261 lw->start = 0;
262 return 1;
263 case CMD_LIST_NEXT_PAGE:
264 lw->start = lw->start + lw->rows;
265 if (lw->start + lw->rows >= (unsigned)help_text_rows) {
266 if ((unsigned)help_text_rows > lw->rows)
267 lw->start = help_text_rows - lw->rows;
268 else
269 lw->start = 0;
270 }
271 return 1;
272 case CMD_LIST_PREVIOUS_PAGE:
273 if (lw->start > lw->rows)
274 lw->start -= lw->rows;
275 else
276 lw->start = 0;
277 return 1;
278 default:
279 break;
280 }
282 lw->selected = lw->start+lw->rows;
283 if (screen_find(screen,
284 lw, help_text_rows,
285 cmd, list_callback, NULL)) {
286 /* center the row */
287 if (lw->selected > lw->rows / 2)
288 lw->start = lw->selected - lw->rows / 2;
289 else
290 lw->start = 0;
291 if (lw->start + lw->rows > (unsigned)help_text_rows) {
292 if (lw->rows < (unsigned)help_text_rows)
293 lw->start = help_text_rows - lw->rows;
294 else
295 lw->start = 0;
296 }
297 return 1;
298 }
300 return 0;
301 }
303 static list_window_t *
304 help_lw(void)
305 {
306 return lw;
307 }
309 screen_functions_t *
310 get_screen_help(void)
311 {
312 static screen_functions_t functions;
314 memset(&functions, 0, sizeof(screen_functions_t));
315 functions.init = help_init;
316 functions.exit = help_exit;
317 functions.open = NULL;
318 functions.close = NULL;
319 functions.resize = help_resize;
320 functions.paint = help_paint;
321 functions.update = help_update;
322 functions.cmd = help_cmd;
323 functions.get_lw = help_lw;
324 functions.get_title = help_title;
326 return &functions;
327 }