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