73ccd674386478bf58750965fa4fb796d106d806
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_NEXT, NULL },
53 { 0, CMD_SCREEN_HELP, NULL },
54 { 0, CMD_SCREEN_PLAY, NULL },
55 { 0, CMD_SCREEN_FILE, NULL },
56 #ifdef ENABLE_CLOCK_SCREEN
57 { 0, CMD_SCREEN_CLOCK, NULL },
58 #endif
59 #ifdef ENABLE_KEYDEF_SCREEN
60 { 0, CMD_SCREEN_KEYDEF, NULL },
61 #endif
63 { 0, CMD_NONE, NULL },
64 { 0, CMD_NONE, NULL },
65 { 1, CMD_NONE, N_("Keys - Global") },
66 { 2, CMD_NONE, NULL },
67 { 0, CMD_STOP, NULL },
68 { 0, CMD_PAUSE, NULL },
69 { 0, CMD_TRACK_NEXT, NULL },
70 { 0, CMD_TRACK_PREVIOUS, NULL },
71 { 0, CMD_SEEK_FORWARD, NULL },
72 { 0, CMD_SEEK_BACKWARD, NULL },
73 { 0, CMD_VOLUME_DOWN, NULL },
74 { 0, CMD_VOLUME_UP, NULL },
75 { 0, CMD_NONE, NULL },
76 { 0, CMD_REPEAT, NULL },
77 { 0, CMD_RANDOM, NULL },
78 { 0, CMD_CROSSFADE, NULL },
79 { 0, CMD_SHUFFLE, NULL },
80 { 0, CMD_DB_UPDATE, NULL },
81 { 0, CMD_NONE, NULL },
82 { 0, CMD_LIST_FIND, NULL },
83 { 0, CMD_LIST_RFIND, NULL },
84 { 0, CMD_LIST_FIND_NEXT, NULL },
85 { 0, CMD_LIST_RFIND_NEXT, NULL },
86 { 0, CMD_TOGGLE_FIND_WRAP, NULL },
87 { 0, CMD_NONE, NULL },
88 { 0, CMD_QUIT, NULL },
90 { 0, CMD_NONE, NULL },
91 { 0, CMD_NONE, NULL },
92 { 1, CMD_NONE, N_("Keys - Playlist screen") },
93 { 2, CMD_NONE, NULL },
94 { 0, CMD_PLAY, N_("Play") },
95 { 0, CMD_DELETE, NULL },
96 { 0, CMD_CLEAR, NULL },
97 { 0, CMD_LIST_MOVE_UP, N_("Move song up") },
98 { 0, CMD_LIST_MOVE_DOWN, N_("Move song down") },
99 { 0, CMD_ADD, NULL },
100 { 0, CMD_SAVE_PLAYLIST, NULL },
101 { 0, CMD_SCREEN_UPDATE, N_("Center") },
102 { 0, CMD_TOGGLE_AUTOCENTER, NULL },
104 { 0, CMD_NONE, NULL },
105 { 0, CMD_NONE, NULL },
106 { 1, CMD_NONE, N_("Keys - Browse screen") },
107 { 2, CMD_NONE, NULL },
108 { 0, CMD_PLAY, N_("Enter directory") },
109 { 0, CMD_SELECT, NULL },
110 { 0, CMD_DELETE, NULL },
111 { 0, CMD_SCREEN_UPDATE, NULL },
113 { 0, CMD_NONE, NULL },
114 {-1, CMD_NONE, NULL }
115 };
117 static int help_text_rows = -1;
118 static list_window_t *lw = NULL;
121 static char *
122 list_callback(int index, int *highlight, void *data)
123 {
124 static char buf[512];
126 if( help_text_rows<0 )
127 {
128 help_text_rows = 0;
129 while( help_text[help_text_rows].highlight != -1 )
130 help_text_rows++;
131 }
133 *highlight = 0;
134 if( index<help_text_rows )
135 {
136 *highlight = help_text[index].highlight > 0;
137 if( help_text[index].command == CMD_NONE )
138 {
139 if( help_text[index].text )
140 g_snprintf(buf, sizeof(buf), " %s", _(help_text[index].text));
141 else
142 if( help_text[index].highlight == 2 )
143 {
144 int i;
146 for(i=3; i<COLS-3 && i<sizeof(buf); i++)
147 buf[i]='-';
148 buf[i] = '\0';
149 }
150 else
151 g_strlcpy(buf, " ", sizeof(buf));
152 return buf;
153 }
154 if( help_text[index].text )
155 g_snprintf(buf, sizeof(buf),
156 "%20s : %s ",
157 get_key_names(help_text[index].command, TRUE),
158 _(help_text[index].text));
159 else
160 g_snprintf(buf, sizeof(buf),
161 "%20s : %s ",
162 get_key_names(help_text[index].command, TRUE),
163 get_key_description(help_text[index].command));
164 return buf;
165 }
167 return NULL;
168 }
170 static void
171 help_init(WINDOW *w, int cols, int rows)
172 {
173 lw = list_window_init(w, cols, rows);
174 lw->flags = LW_HIDE_CURSOR;
175 }
177 static void
178 help_resize(int cols, int rows)
179 {
180 lw->cols = cols;
181 lw->rows = rows;
182 }
184 static void
185 help_exit(void)
186 {
187 list_window_free(lw);
188 }
191 static char *
192 help_title(char *str, size_t size)
193 {
194 return _("Help");
195 }
197 static void
198 help_paint(screen_t *screen, mpdclient_t *c)
199 {
200 lw->clear = 1;
201 list_window_paint(lw, list_callback, NULL);
202 wrefresh(lw->w);
203 }
205 static void
206 help_update(screen_t *screen, mpdclient_t *c)
207 {
208 if( lw->repaint )
209 {
210 list_window_paint(lw, list_callback, NULL);
211 wrefresh(lw->w);
212 lw->repaint = 0;
213 }
214 }
217 static int
218 help_cmd(screen_t *screen, mpdclient_t *c, command_t cmd)
219 {
220 lw->repaint=1;
221 switch(cmd)
222 {
223 case CMD_LIST_NEXT:
224 if( lw->start+lw->rows < help_text_rows )
225 lw->start++;
226 return 1;
227 case CMD_LIST_PREVIOUS:
228 if( lw->start >0 )
229 lw->start--;
230 return 1;
231 case CMD_LIST_FIRST:
232 lw->start = 0;
233 return 1;
234 case CMD_LIST_LAST:
235 lw->start = help_text_rows-lw->rows;
236 if( lw->start<0 )
237 lw->start = 0;
238 return 1;
239 case CMD_LIST_NEXT_PAGE:
240 lw->start = lw->start + lw->rows;
241 if( lw->start+lw->rows >= help_text_rows )
242 lw->start = help_text_rows-lw->rows;
243 if( lw->start<0 )
244 lw->start = 0;
245 return 1;
246 case CMD_LIST_PREVIOUS_PAGE:
247 lw->start = lw->start - lw->rows;
248 if( lw->start<0 )
249 lw->start = 0;
250 return 1;
251 default:
252 break;
253 }
255 lw->selected = lw->start+lw->rows;
256 if( screen_find(screen, c,
257 lw, help_text_rows,
258 cmd, list_callback) )
259 {
260 /* center the row */
261 lw->start = lw->selected-(lw->rows/2);
262 if( lw->start+lw->rows > help_text_rows )
263 lw->start = help_text_rows-lw->rows;
264 if( lw->start<0 )
265 lw->start=0;
266 return 1;
267 }
269 return 0;
270 }
272 static list_window_t *
273 help_lw(void)
274 {
275 return lw;
276 }
278 screen_functions_t *
279 get_screen_help(void)
280 {
281 static screen_functions_t functions;
283 memset(&functions, 0, sizeof(screen_functions_t));
284 functions.init = help_init;
285 functions.exit = help_exit;
286 functions.open = NULL;
287 functions.close = NULL;
288 functions.resize = help_resize;
289 functions.paint = help_paint;
290 functions.update = help_update;
291 functions.cmd = help_cmd;
292 functions.get_lw = help_lw;
293 functions.get_title = help_title;
295 return &functions;
296 }