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 <ctype.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <glib.h>
27 #include <signal.h>
28 #include <ncurses.h>
30 #include "config.h"
31 #include "ncmpc.h"
32 #include "command.h"
33 #include "mpdclient.h"
34 #include "screen.h"
36 #undef DEBUG_KEYS
38 #ifdef DEBUG_KEYS
39 #define DK(x) x
40 #else
41 #define DK(x)
42 #endif
44 #define BS KEY_BACKSPACE
45 #define DEL KEY_DC
46 #define UP KEY_UP
47 #define DWN KEY_DOWN
48 #define LEFT KEY_LEFT
49 #define RGHT KEY_RIGHT
50 #define HOME KEY_HOME
51 #define END KEY_END
52 #define PGDN KEY_NPAGE
53 #define PGUP KEY_PPAGE
54 #define TAB 0x09
55 #define STAB 0x161
56 #define ESC 0x1B
57 #define F1 KEY_F(1)
58 #define F2 KEY_F(2)
59 #define F3 KEY_F(3)
60 #define F4 KEY_F(4)
61 #define F5 KEY_F(5)
62 #define F6 KEY_F(6)
63 #define F7 KEY_F(7)
66 static command_definition_t cmds[] =
67 {
68 #ifdef ENABLE_KEYDEF_SCREEN
69 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
70 N_("Key configuration screen") },
71 #endif
72 { { 'q', 'Q', 3 }, 0, CMD_QUIT, "quit",
73 N_("Quit") },
75 /* movment */
76 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
77 N_("Move cursor up") },
78 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
79 N_("Move cursor down") },
80 { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST, "home",
81 N_("Home ") },
82 { { END, 0x05, 0 }, 0, CMD_LIST_LAST, "end",
83 N_("End ") },
84 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
85 N_("Page up") },
86 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
87 N_("Page down") },
90 /* basic screens */
91 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
92 N_("Help screen") },
93 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
94 N_("Playlist screen") },
95 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
96 N_("Browse screen") },
99 /* player commands */
100 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
101 N_("Play/Enter directory") },
102 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
103 N_("Pause") },
104 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
105 N_("Stop") },
106 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
107 N_("Next track") },
108 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
109 N_("Previous track") },
110 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
111 N_("Seek forward") },
112 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
113 N_("Seek backward") },
114 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
115 N_("Increase volume") },
116 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
117 N_("Decrease volume") },
118 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
119 N_("Select/deselect song in playlist") },
120 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
121 N_("Select all listed items") },
122 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
123 N_("Delete song from playlist") },
124 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
125 N_("Shuffle playlist") },
126 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
127 N_("Clear playlist") },
128 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
129 N_("Toggle repeat mode") },
130 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
131 N_("Toggle random mode") },
132 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
133 N_("Toggle crossfade mode") },
134 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
135 N_("Start a music database update") },
136 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
137 N_("Save playlist") },
138 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
139 N_("Add url/file to playlist") },
141 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
142 N_("Go to root directory") },
143 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
144 N_("Go to parent directory") },
146 /* lists */
147 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
148 N_("Move item up") },
149 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
150 N_("Move item down") },
151 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
152 N_("Update screen") },
155 /* ncmpc options */
156 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
157 N_("Toggle find mode") },
158 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
159 N_("Toggle auto center mode") },
162 /* change screen */
163 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
164 N_("Next screen") },
165 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
166 N_("Previous screen") },
169 /* find */
170 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
171 N_("Forward find") },
172 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
173 N_("Forward find next") },
174 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
175 N_("Backward find") },
176 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
177 N_("Backward find previous") },
180 /* extra screens */
181 #ifdef ENABLE_ARTIST_SCREEN
182 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
183 N_("Artist screen") },
184 #endif
185 #ifdef ENABLE_SEARCH_SCREEN
186 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
187 N_("Search screen") },
188 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
189 N_("Change search mode") },
190 #endif
192 #ifdef ENABLE_CLOCK_SCREEN
193 { {'6', F6, 0 }, 0, CMD_SCREEN_CLOCK, "screen-clock",
194 N_("Clock screen") },
195 #endif
196 #ifdef ENABLE_LYRICS_SCREEN
197 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
198 N_("Lyrics screen") },
199 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
200 N_("Interrupt action") },
201 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
202 N_("Update Lyrics") },
203 #endif
206 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
207 };
209 command_definition_t *
210 get_command_definitions(void)
211 {
212 return cmds;
213 }
215 const char *
216 key2str(int key)
217 {
218 static char buf[32];
219 int i;
221 buf[0] = 0;
222 switch(key)
223 {
224 case 0:
225 return _("Undefined");
226 case ' ':
227 return _("Space");
228 case 13:
229 return _("Enter");
230 case BS:
231 return _("Backspace");
232 case DEL:
233 return _("Delete");
234 case UP:
235 return _("Up");
236 case DWN:
237 return _("Down");
238 case LEFT:
239 return _("Left");
240 case RGHT:
241 return _("Right");
242 case HOME:
243 return _("Home");
244 case END:
245 return _("End");
246 case PGDN:
247 return _("PageDown");
248 case PGUP:
249 return _("PageUp");
250 case TAB:
251 return _("Tab");
252 case STAB:
253 return _("Shift+Tab");
254 case ESC:
255 return _("Esc");
256 case KEY_IC:
257 return _("Insert");
258 default:
259 for(i=0; i<=63; i++)
260 if( key==KEY_F(i) )
261 {
262 g_snprintf(buf, 32, "F%d", i );
263 return buf;
264 }
265 if( !(key & ~037) )
266 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
267 else if( (key & ~037) == 224 )
268 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
269 else if( key>32 && key<256 )
270 g_snprintf(buf, 32, "%c", key);
271 else
272 g_snprintf(buf, 32, "0x%03X", key);
273 }
274 return buf;
275 }
277 void
278 command_dump_keys(void)
279 {
280 int i;
282 i=0;
283 while( cmds[i].description )
284 {
285 if( cmds[i].command != CMD_NONE )
286 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
287 i++;
288 }
289 }
291 static int
292 set_key_flags(command_definition_t *cp, command_t command, int flags)
293 {
294 int i;
296 i=0;
297 while( cp[i].name )
298 {
299 if( cp[i].command == command )
300 {
301 cp[i].flags |= flags;
302 return 0;
303 }
304 i++;
305 }
306 return 1;
307 }
309 const char *
310 get_key_names(command_t command, int all)
311 {
312 int i;
314 i=0;
315 while( cmds[i].description )
316 {
317 if( cmds[i].command == command )
318 {
319 int j;
320 static char keystr[80];
322 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
323 if( !all )
324 return keystr;
325 j=1;
326 while( j<MAX_COMMAND_KEYS && cmds[i].keys[j]>0 )
327 {
328 g_strlcat(keystr, " ", sizeof(keystr));
329 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
330 j++;
331 }
332 return keystr;
333 }
334 i++;
335 }
336 return NULL;
337 }
339 const char *
340 get_key_description(command_t command)
341 {
342 int i;
344 i=0;
345 while( cmds[i].description )
346 {
347 if( cmds[i].command == command )
348 return _(cmds[i].description);
349 i++;
350 }
351 return NULL;
352 }
354 const char *
355 get_key_command_name(command_t command)
356 {
357 int i;
359 i=0;
360 while( cmds[i].name )
361 {
362 if( cmds[i].command == command )
363 return cmds[i].name;
364 i++;
365 }
366 return NULL;
367 }
369 command_t
370 get_key_command_from_name(char *name)
371 {
372 int i;
374 i=0;
375 while( cmds[i].name )
376 {
377 if( strcmp(name, cmds[i].name) == 0 )
378 return cmds[i].command;
379 i++;
380 }
381 return CMD_NONE;
382 }
385 command_t
386 find_key_command(int key, command_definition_t *cmds)
387 {
388 int i;
390 i=0;
391 while (key && cmds && cmds[i].name) {
392 if (cmds[i].keys[0] == key ||
393 cmds[i].keys[1] == key ||
394 cmds[i].keys[2] == key)
395 return cmds[i].command;
396 i++;
397 }
399 return CMD_NONE;
400 }
402 command_t
403 get_key_command(int key)
404 {
405 return find_key_command(key, cmds);
406 }
408 int
409 my_wgetch(WINDOW *w)
410 {
411 int c;
413 c = wgetch(w);
415 /* handle resize event */
416 if( c==KEY_RESIZE )
417 screen_resize();
419 #ifdef ENABLE_RAW_MODE
420 /* handle SIGSTOP (Ctrl-Z) */
421 if( c==26 || c==407 )
422 sigstop();
423 /* handle SIGINT (Ctrl-C) */
424 if( c==3 )
425 exit(EXIT_SUCCESS);
426 #endif
428 return c;
429 }
431 command_t
432 get_keyboard_command_with_timeout(int ms)
433 {
434 int key;
436 if( ms != SCREEN_TIMEOUT)
437 timeout(ms);
438 key = my_wgetch(stdscr);
439 if( ms != SCREEN_TIMEOUT)
440 timeout(SCREEN_TIMEOUT);
442 if( key==ERR )
443 return CMD_NONE;
445 #ifdef HAVE_GETMOUSE
446 if( key==KEY_MOUSE )
447 return CMD_MOUSE_EVENT;
448 #endif
450 return get_key_command(key);
451 }
453 command_t
454 get_keyboard_command(void)
455 {
456 return get_keyboard_command_with_timeout(SCREEN_TIMEOUT);
457 }
459 int
460 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
461 {
462 int i;
464 i=0;
465 while( cmds[i].name )
466 {
467 if( cmds[i].command == command )
468 {
469 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
470 cmds[i].flags |= COMMAND_KEY_MODIFIED;
471 return 0;
472 }
473 i++;
474 }
475 return -1;
476 }
478 int
479 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
480 {
481 int i;
482 int retval = 0;
484 if( cp==NULL )
485 cp = cmds;
487 i=0;
488 while( cp[i].name )
489 {
490 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
491 i++;
492 }
494 i=0;
495 while( cp[i].name )
496 {
497 int j;
498 command_t cmd;
500 for(j=0; j<MAX_COMMAND_KEYS; j++)
501 if( cp[i].keys[j] &&
502 (cmd=find_key_command(cp[i].keys[j],cp)) != cp[i].command )
503 {
504 if( buf )
505 #ifdef ENABLE_KEYDEF_SCREEN
506 g_snprintf(buf, bufsize,
507 _("Key %s assigned to %s and %s (press %s for the key editor)"),
508 key2str(cp[i].keys[j]),
509 get_key_command_name(cp[i].command),
510 get_key_command_name(cmd),
511 get_key_names(CMD_SCREEN_KEYDEF,0));
512 #else
513 g_snprintf(buf, bufsize,
514 _("Error: Key %s assigned to %s and %s !!!\n"),
515 key2str(cp[i].keys[j]),
516 get_key_command_name(cp[i].command),
517 get_key_command_name(cmd));
518 #endif
519 else
520 fprintf(stderr,
521 _("Error: Key %s assigned to %s and %s !!!\n"),
522 key2str(cp[i].keys[j]),
523 get_key_command_name(cp[i].command),
524 get_key_command_name(cmd));
525 cp[i].flags |= COMMAND_KEY_CONFLICT;
526 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
527 retval = -1;
528 }
529 i++;
530 }
531 return retval;
532 }
534 int
535 write_key_bindings(FILE *f, int flags)
536 {
537 int i,j;
539 if( flags & KEYDEF_WRITE_HEADER )
540 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
542 i=0;
543 while( cmds[i].name && !ferror(f) )
544 {
545 if( cmds[i].flags & COMMAND_KEY_MODIFIED || flags & KEYDEF_WRITE_ALL)
546 {
547 fprintf(f, "## %s\n", cmds[i].description);
548 if( flags & KEYDEF_COMMENT_ALL )
549 fprintf(f, "#");
550 fprintf(f, "key %s = ", cmds[i].name);
551 for(j=0; j<MAX_COMMAND_KEYS; j++)
552 {
553 if( j && cmds[i].keys[j] )
554 fprintf(f, ", ");
555 if( !j || cmds[i].keys[j] )
556 {
557 if( cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
558 isdigit(cmds[i].keys[j])) )
559 fprintf(f, "\'%c\'", cmds[i].keys[j]);
560 else
561 fprintf(f, "%d", cmds[i].keys[j]);
562 }
563 }
564 fprintf(f,"\n\n");
565 }
566 i++;
567 }
568 return ferror(f);
569 }