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 {
36 signed char highlight;
37 command_t command;
38 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 char *
151 list_callback(int index, int *highlight, void *data)
152 {
153 static char buf[512];
155 if( help_text_rows<0 )
156 {
157 help_text_rows = 0;
158 while( help_text[help_text_rows].highlight != -1 )
159 help_text_rows++;
160 }
162 *highlight = 0;
163 if( index<help_text_rows )
164 {
165 *highlight = help_text[index].highlight > 0;
166 if( help_text[index].command == CMD_NONE )
167 {
168 if( help_text[index].text )
169 g_snprintf(buf, sizeof(buf), " %s", _(help_text[index].text));
170 else
171 if( help_text[index].highlight == 2 )
172 {
173 int i;
175 for(i=3; i<COLS-3 && i<sizeof(buf); i++)
176 buf[i]='-';
177 buf[i] = '\0';
178 }
179 else
180 g_strlcpy(buf, " ", sizeof(buf));
181 return buf;
182 }
183 if( help_text[index].text )
184 g_snprintf(buf, sizeof(buf),
185 "%20s : %s ",
186 get_key_names(help_text[index].command, TRUE),
187 _(help_text[index].text));
188 else
189 g_snprintf(buf, sizeof(buf),
190 "%20s : %s ",
191 get_key_names(help_text[index].command, TRUE),
192 get_key_description(help_text[index].command));
193 return buf;
194 }
196 return NULL;
197 }
199 static void
200 help_init(WINDOW *w, int cols, int rows)
201 {
202 lw = list_window_init(w, cols, rows);
203 lw->flags = LW_HIDE_CURSOR;
204 }
206 static void
207 help_resize(int cols, int rows)
208 {
209 lw->cols = cols;
210 lw->rows = rows;
211 }
213 static void
214 help_exit(void)
215 {
216 list_window_free(lw);
217 }
220 static char *
221 help_title(char *str, size_t size)
222 {
223 return _("Help");
224 }
226 static void
227 help_paint(screen_t *screen, mpdclient_t *c)
228 {
229 lw->clear = 1;
230 list_window_paint(lw, list_callback, NULL);
231 wrefresh(lw->w);
232 }
234 static void
235 help_update(screen_t *screen, mpdclient_t *c)
236 {
237 if( lw->repaint )
238 {
239 list_window_paint(lw, list_callback, NULL);
240 wrefresh(lw->w);
241 lw->repaint = 0;
242 }
243 }
246 static int
247 help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
248 {
249 lw->repaint=1;
250 lw->clear=1;
251 switch(cmd)
252 {
253 case CMD_LIST_NEXT:
254 if( lw->start+lw->rows < help_text_rows )
255 lw->start++;
256 return 1;
257 case CMD_LIST_PREVIOUS:
258 if( lw->start >0 )
259 lw->start--;
260 return 1;
261 case CMD_LIST_FIRST:
262 lw->start = 0;
263 return 1;
264 case CMD_LIST_LAST:
265 lw->start = help_text_rows-lw->rows;
266 if( lw->start<0 )
267 lw->start = 0;
268 return 1;
269 case CMD_LIST_NEXT_PAGE:
270 lw->start = lw->start + lw->rows;
271 if( lw->start+lw->rows >= help_text_rows )
272 lw->start = help_text_rows-lw->rows;
273 if( lw->start<0 )
274 lw->start = 0;
275 return 1;
276 case CMD_LIST_PREVIOUS_PAGE:
277 lw->start = lw->start - lw->rows;
278 if( lw->start<0 )
279 lw->start = 0;
280 return 1;
281 default:
282 break;
283 }
285 lw->selected = lw->start+lw->rows;
286 if( screen_find(screen, c,
287 lw, help_text_rows,
288 cmd, list_callback, NULL) )
289 {
290 /* center the row */
291 lw->start = lw->selected-(lw->rows/2);
292 if( lw->start+lw->rows > help_text_rows )
293 lw->start = help_text_rows-lw->rows;
294 if( lw->start<0 )
295 lw->start=0;
296 return 1;
297 }
299 return 0;
300 }
302 static list_window_t *
303 help_lw(void)
304 {
305 return lw;
306 }
308 screen_functions_t *
309 get_screen_help(void)
310 {
311 static screen_functions_t functions;
313 memset(&functions, 0, sizeof(screen_functions_t));
314 functions.init = help_init;
315 functions.exit = help_exit;
316 functions.open = NULL;
317 functions.close = NULL;
318 functions.resize = help_resize;
319 functions.paint = help_paint;
320 functions.update = help_update;
321 functions.cmd = help_cmd;
322 functions.get_lw = help_lw;
323 functions.get_title = help_title;
325 return &functions;
326 }