1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
20 #include "command.h"
21 #include "config.h"
22 #include "ncmpc.h"
23 #include "i18n.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 KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
45 #define BS KEY_BACKSPACE
46 #define DEL KEY_DC
47 #define UP KEY_UP
48 #define DWN KEY_DOWN
49 #define LEFT KEY_LEFT
50 #define RGHT KEY_RIGHT
51 #define HOME KEY_HOME
52 #define END KEY_END
53 #define PGDN KEY_NPAGE
54 #define PGUP KEY_PPAGE
55 #define TAB 0x09
56 #define STAB 0x161
57 #define ESC 0x1B
58 #define RET '\r'
59 #define F1 KEY_F(1)
60 #define F2 KEY_F(2)
61 #define F3 KEY_F(3)
62 #define F4 KEY_F(4)
63 #define F5 KEY_F(5)
64 #define F6 KEY_F(6)
65 #define F7 KEY_F(7)
66 #define F8 KEY_F(8)
67 #define C(x) KEY_CTL(x)
69 static command_definition_t cmds[] = {
70 #ifdef ENABLE_KEYDEF_SCREEN
71 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
72 N_("Key configuration screen") },
73 #endif
74 { { 'q', 'Q', C('C') }, 0, CMD_QUIT, "quit",
75 N_("Quit") },
77 /* movement */
78 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
79 N_("Move cursor up") },
80 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
81 N_("Move cursor down") },
82 { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
83 N_("Move cursor to the top of screen") },
84 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
85 N_("Move cursor to the middle of screen") },
86 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
87 N_("Move cursor to the bottom of screen") },
88 { { HOME, C('A'), 0 }, 0, CMD_LIST_FIRST, "home",
89 N_("Move cursor to the top of the list") },
90 { { END, C('E'), 0 }, 0, CMD_LIST_LAST, "end",
91 N_("Move cursor to the bottom of the list") },
92 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
93 N_("Page up") },
94 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
95 N_("Page down") },
96 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
97 N_("Range selection") },
98 { { C('N'), 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
99 N_("Scroll up one line") },
100 { { C('B'), 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
101 N_("Scroll down one line") },
102 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
103 N_("Scroll up half a screen") },
104 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
105 N_("Scroll down half a screen") },
106 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
107 N_("Select currently playing song") },
110 /* basic screens */
111 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
112 N_("Help screen") },
113 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
114 N_("Playlist screen") },
115 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
116 N_("Browse screen") },
119 /* player commands */
120 { { RET, 0, 0 }, 0, CMD_PLAY, "play",
121 N_("Play/Enter directory") },
122 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
123 N_("Pause") },
124 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
125 N_("Stop") },
126 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
127 N_("Crop") },
128 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
129 N_("Next track") },
130 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
131 N_("Previous track") },
132 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
133 N_("Seek forward") },
134 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
135 N_("Seek backward") },
136 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
137 N_("Increase volume") },
138 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
139 N_("Decrease volume") },
140 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
141 N_("Select/deselect song in playlist") },
142 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
143 N_("Select all listed items") },
144 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
145 N_("Delete song from playlist") },
146 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
147 N_("Shuffle playlist") },
148 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
149 N_("Clear playlist") },
150 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
151 N_("Toggle repeat mode") },
152 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
153 N_("Toggle random mode") },
154 { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
155 N_("Toggle single mode") },
156 { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
157 N_("Toggle consume mode") },
158 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
159 N_("Toggle crossfade mode") },
160 { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
161 N_("Start a music database update") },
162 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
163 N_("Save playlist") },
164 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
165 N_("Add url/file to playlist") },
167 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
168 N_("Go to root directory") },
169 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
170 N_("Go to parent directory") },
172 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
173 N_("Locate song in browser") },
175 /* lists */
176 { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
177 N_("Move item up") },
178 { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
179 N_("Move item down") },
180 { { C('L'), 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
181 N_("Refresh screen") },
184 /* ncmpc options */
185 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
186 /* translators: toggle between wrapping and non-wrapping
187 search */
188 N_("Toggle find mode") },
189 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
190 /* translators: the auto center mode always centers the song
191 currently being played */
192 N_("Toggle auto center mode") },
195 /* change screen */
196 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
197 N_("Next screen") },
198 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
199 N_("Previous screen") },
200 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
201 N_("Swap to most recent screen") },
204 /* find */
205 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
206 N_("Forward find") },
207 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
208 N_("Forward find next") },
209 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
210 N_("Backward find") },
211 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
212 N_("Backward find previous") },
213 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
214 /* translators: this queries the user for a string
215 * and jumps directly (while the user is typing)
216 * to the entry which begins with this string */
217 N_("Jump to") },
220 /* extra screens */
221 #ifdef ENABLE_ARTIST_SCREEN
222 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
223 N_("Artist screen") },
224 #endif
225 #ifdef ENABLE_SEARCH_SCREEN
226 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
227 N_("Search screen") },
228 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
229 N_("Change search mode") },
230 #endif
231 #ifdef ENABLE_SONG_SCREEN
232 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
233 N_("View the selected and the currently playing song") },
234 #endif
235 #ifdef ENABLE_LYRICS_SCREEN
236 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
237 N_("Lyrics screen") },
238 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
239 /* translators: interrupt the current background action,
240 e.g. stop loading lyrics from the internet */
241 N_("Interrupt action") },
242 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
243 N_("Update Lyrics") },
244 /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
245 at some point */
246 { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
247 N_("Edit the current item") },
248 #endif
250 #ifdef ENABLE_OUTPUTS_SCREEN
251 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
252 N_("Outputs screen") },
253 #endif
256 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
257 };
259 #ifdef ENABLE_KEYDEF_SCREEN
260 command_definition_t *
261 get_command_definitions(void)
262 {
263 return cmds;
264 }
266 size_t
267 get_cmds_max_name_width(command_definition_t *c)
268 {
269 static size_t max = 0;
271 if (max != 0)
272 return max;
274 size_t len;
275 command_definition_t *p;
277 for (p = c; p->name != NULL; p++) {
278 /*
279 * width and length are considered the same here, as command
280 * names are not translated.
281 */
282 len = (size_t) strlen(p->name);
283 if (len > max)
284 max = len;
285 }
287 return max;
288 }
289 #endif
291 const char *
292 key2str(int key)
293 {
294 static char buf[32];
295 int i;
297 buf[0] = 0;
298 switch(key) {
299 case 0:
300 return _("Undefined");
301 case ' ':
302 return _("Space");
303 case RET:
304 return _("Enter");
305 case BS:
306 return _("Backspace");
307 case DEL:
308 return _("Delete");
309 case UP:
310 return _("Up");
311 case DWN:
312 return _("Down");
313 case LEFT:
314 return _("Left");
315 case RGHT:
316 return _("Right");
317 case HOME:
318 return _("Home");
319 case END:
320 return _("End");
321 case PGDN:
322 return _("PageDown");
323 case PGUP:
324 return _("PageUp");
325 case TAB:
326 return _("Tab");
327 case STAB:
328 return _("Shift+Tab");
329 case ESC:
330 return _("Esc");
331 case KEY_IC:
332 return _("Insert");
333 default:
334 for (i = 0; i <= 63; i++)
335 if (key == KEY_F(i)) {
336 g_snprintf(buf, 32, _("F%d"), i );
337 return buf;
338 }
339 if (!(key & ~037))
340 g_snprintf(buf, 32, _("Ctrl-%c"), 'A'+(key & 037)-1 );
341 else if ((key & ~037) == 224)
342 g_snprintf(buf, 32, _("Alt-%c"), 'A'+(key & 037)-1 );
343 else if (key > 32 && key < 256)
344 g_snprintf(buf, 32, "%c", key);
345 else
346 g_snprintf(buf, 32, "0x%03X", key);
347 }
349 return buf;
350 }
352 void
353 command_dump_keys(void)
354 {
355 for (int i = 0; cmds[i].description; i++)
356 if (cmds[i].command != CMD_NONE)
357 printf(" %20s : %s\n",
358 get_key_names(cmds[i].command, true),
359 cmds[i].name);
360 }
362 #ifndef NCMPC_MINI
364 static void
365 set_key_flags(command_definition_t *cp, command_t command, int flags)
366 {
367 for (int i = 0; cp[i].name; i++) {
368 if (cp[i].command == command) {
369 cp[i].flags |= flags;
370 break;
371 }
372 }
373 }
375 #endif
377 const char *
378 get_key_names(command_t command, bool all)
379 {
380 for (int i = 0; cmds[i].description; i++) {
381 if (cmds[i].command == command) {
382 static char keystr[80];
384 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
385 if (!all)
386 return keystr;
388 for (int j = 1; j < MAX_COMMAND_KEYS &&
389 cmds[i].keys[j] > 0; j++) {
390 g_strlcat(keystr, " ", sizeof(keystr));
391 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
392 }
393 return keystr;
394 }
395 }
396 return NULL;
397 }
399 const char *
400 get_key_description(command_t command)
401 {
402 for (int i = 0; cmds[i].description; i++)
403 if (cmds[i].command == command)
404 return _(cmds[i].description);
406 return NULL;
407 }
409 const char *
410 get_key_command_name(command_t command)
411 {
412 for (int i = 0; cmds[i].name; i++)
413 if (cmds[i].command == command)
414 return cmds[i].name;
416 return NULL;
417 }
419 command_t
420 get_key_command_from_name(char *name)
421 {
422 for (int i = 0; cmds[i].name; i++)
423 if (strcmp(name, cmds[i].name) == 0)
424 return cmds[i].command;
426 return CMD_NONE;
427 }
429 command_t
430 find_key_command(int key, command_definition_t *c)
431 {
432 assert(key != 0);
433 assert(c != NULL);
435 for (int i = 0; c[i].name; i++) {
436 for (int j = 0; j < MAX_COMMAND_KEYS; j++)
437 if (c[i].keys[j] == key)
438 return c[i].command;
439 }
441 return CMD_NONE;
442 }
444 command_t
445 get_key_command(int key)
446 {
447 return find_key_command(key, cmds);
448 }
450 command_t
451 get_keyboard_command(void)
452 {
453 int key;
455 key = wgetch(stdscr);
456 if (key == ERR)
457 return CMD_NONE;
459 #ifdef HAVE_GETMOUSE
460 if (key == KEY_MOUSE)
461 return CMD_MOUSE_EVENT;
462 #endif
464 return get_key_command(key);
465 }
467 int
468 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
469 {
470 for (int i = 0; cmds[i].name; i++) {
471 if (cmds[i].command == command) {
472 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
473 #ifndef NCMPC_MINI
474 cmds[i].flags |= COMMAND_KEY_MODIFIED;
475 #endif
476 return 0;
477 }
478 }
480 return -1;
481 }
483 #ifndef NCMPC_MINI
485 int
486 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
487 {
488 int i;
489 int retval = 0;
491 if (cp == NULL)
492 cp = cmds;
494 for (i = 0; cp[i].name; i++)
495 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
497 for (i = 0; cp[i].name; i++) {
498 int j;
499 command_t cmd;
501 for(j=0; j<MAX_COMMAND_KEYS; j++) {
502 if (cp[i].keys[j] &&
503 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
504 if (buf) {
505 g_snprintf(buf, bufsize,
506 _("Key %s assigned to %s and %s"),
507 key2str(cp[i].keys[j]),
508 get_key_command_name(cp[i].command),
509 get_key_command_name(cmd));
510 } else {
511 fprintf(stderr,
512 _("Key %s assigned to %s and %s"),
513 key2str(cp[i].keys[j]),
514 get_key_command_name(cp[i].command),
515 get_key_command_name(cmd));
516 fputc('\n', stderr);
517 }
518 cp[i].flags |= COMMAND_KEY_CONFLICT;
519 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
520 retval = -1;
521 }
522 }
523 }
525 return retval;
526 }
528 int
529 write_key_bindings(FILE *f, int flags)
530 {
531 if (flags & KEYDEF_WRITE_HEADER)
532 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
534 for (int i = 0; cmds[i].name && !ferror(f); i++) {
535 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
536 flags & KEYDEF_WRITE_ALL) {
537 fprintf(f, "## %s\n", cmds[i].description);
538 if (flags & KEYDEF_COMMENT_ALL)
539 fprintf(f, "#");
540 fprintf(f, "key %s = ", cmds[i].name);
541 for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
542 if (j && cmds[i].keys[j])
543 fprintf(f, ", ");
544 if (!j || cmds[i].keys[j]) {
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 }
556 return ferror(f);
557 }
559 #endif /* NCMPC_MINI */