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