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 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
37 #define BS KEY_BACKSPACE
38 #define DEL KEY_DC
39 #define UP KEY_UP
40 #define DWN KEY_DOWN
41 #define LEFT KEY_LEFT
42 #define RGHT KEY_RIGHT
43 #define HOME KEY_HOME
44 #define END KEY_END
45 #define PGDN KEY_NPAGE
46 #define PGUP KEY_PPAGE
47 #define TAB 0x09
48 #define STAB 0x161
49 #define ESC 0x1B
50 #define RET '\r'
51 #define F1 KEY_F(1)
52 #define F2 KEY_F(2)
53 #define F3 KEY_F(3)
54 #define F4 KEY_F(4)
55 #define F5 KEY_F(5)
56 #define F6 KEY_F(6)
57 #define F7 KEY_F(7)
58 #define F8 KEY_F(8)
59 #define C(x) KEY_CTL(x)
61 static command_definition_t cmds[] = {
62 #ifdef ENABLE_KEYDEF_SCREEN
63 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
64 N_("Key configuration screen") },
65 #endif
66 { { 'q', 'Q', C('C') }, 0, CMD_QUIT, "quit",
67 N_("Quit") },
69 /* movement */
70 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
71 N_("Move cursor up") },
72 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
73 N_("Move cursor down") },
74 { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
75 N_("Move cursor to the top of screen") },
76 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
77 N_("Move cursor to the middle of screen") },
78 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
79 N_("Move cursor to the bottom of screen") },
80 { { HOME, C('A'), 0 }, 0, CMD_LIST_FIRST, "home",
81 N_("Move cursor to the top of the list") },
82 { { END, C('E'), 0 }, 0, CMD_LIST_LAST, "end",
83 N_("Move cursor to the bottom of the list") },
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") },
88 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
89 N_("Range selection") },
90 { { C('N'), 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
91 N_("Scroll up one line") },
92 { { C('B'), 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
93 N_("Scroll down one line") },
94 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
95 N_("Scroll up half a screen") },
96 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
97 N_("Scroll down half a screen") },
98 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
99 N_("Select currently playing song") },
102 /* basic screens */
103 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
104 N_("Help screen") },
105 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
106 N_("Playlist screen") },
107 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
108 N_("Browse screen") },
111 /* player commands */
112 { { RET, 0, 0 }, 0, CMD_PLAY, "play",
113 N_("Play/Enter directory") },
114 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
115 N_("Pause") },
116 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
117 N_("Stop") },
118 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
119 N_("Crop") },
120 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
121 N_("Next track") },
122 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
123 N_("Previous track") },
124 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
125 N_("Seek forward") },
126 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
127 N_("Seek backward") },
128 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
129 N_("Increase volume") },
130 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
131 N_("Decrease volume") },
132 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
133 N_("Select/deselect song in playlist") },
134 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
135 N_("Select all listed items") },
136 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
137 N_("Delete song from playlist") },
138 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
139 N_("Shuffle playlist") },
140 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
141 N_("Clear playlist") },
142 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
143 N_("Toggle repeat mode") },
144 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
145 N_("Toggle random mode") },
146 { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
147 N_("Toggle single mode") },
148 { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
149 N_("Toggle consume mode") },
150 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
151 N_("Toggle crossfade mode") },
152 { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
153 N_("Start a music database update") },
154 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
155 N_("Save playlist") },
156 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
157 N_("Add url/file to playlist") },
159 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
160 N_("Go to root directory") },
161 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
162 N_("Go to parent directory") },
164 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
165 N_("Locate song in browser") },
167 /* lists */
168 { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
169 N_("Move item up") },
170 { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
171 N_("Move item down") },
172 { { C('L'), 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
173 N_("Refresh screen") },
176 /* ncmpc options */
177 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
178 /* translators: toggle between wrapping and non-wrapping
179 search */
180 N_("Toggle find mode") },
181 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
182 /* translators: the auto center mode always centers the song
183 currently being played */
184 N_("Toggle auto center mode") },
187 /* change screen */
188 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
189 N_("Next screen") },
190 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
191 N_("Previous screen") },
192 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
193 N_("Swap to most recent screen") },
196 /* find */
197 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
198 N_("Forward find") },
199 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
200 N_("Forward find next") },
201 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
202 N_("Backward find") },
203 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
204 N_("Backward find previous") },
205 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
206 /* translators: this queries the user for a string
207 * and jumps directly (while the user is typing)
208 * to the entry which begins with this string */
209 N_("Jump to") },
212 /* extra screens */
213 #ifdef ENABLE_ARTIST_SCREEN
214 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
215 N_("Artist screen") },
216 #endif
217 #ifdef ENABLE_SEARCH_SCREEN
218 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
219 N_("Search screen") },
220 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
221 N_("Change search mode") },
222 #endif
223 #ifdef ENABLE_SONG_SCREEN
224 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
225 N_("View the selected and the currently playing song") },
226 #endif
227 #ifdef ENABLE_LYRICS_SCREEN
228 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
229 N_("Lyrics screen") },
230 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
231 /* translators: interrupt the current background action,
232 e.g. stop loading lyrics from the internet */
233 N_("Interrupt action") },
234 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
235 N_("Update Lyrics") },
236 /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
237 at some point */
238 { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
239 N_("Edit the current item") },
240 #endif
242 #ifdef ENABLE_OUTPUTS_SCREEN
243 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
244 N_("Outputs screen") },
245 #endif
248 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
249 };
251 #ifdef ENABLE_KEYDEF_SCREEN
252 command_definition_t *
253 get_command_definitions(void)
254 {
255 return cmds;
256 }
258 size_t
259 get_cmds_max_name_width(command_definition_t *c)
260 {
261 static size_t max = 0;
263 if (max != 0)
264 return max;
266 size_t len;
267 command_definition_t *p;
269 for (p = c; p->name != NULL; p++) {
270 /*
271 * width and length are considered the same here, as command
272 * names are not translated.
273 */
274 len = (size_t) strlen(p->name);
275 if (len > max)
276 max = len;
277 }
279 return max;
280 }
281 #endif
283 const char *
284 key2str(int key)
285 {
286 static char buf[32];
287 int i;
289 buf[0] = 0;
290 switch(key) {
291 case 0:
292 return _("Undefined");
293 case ' ':
294 return _("Space");
295 case RET:
296 return _("Enter");
297 case BS:
298 return _("Backspace");
299 case DEL:
300 return _("Delete");
301 case UP:
302 return _("Up");
303 case DWN:
304 return _("Down");
305 case LEFT:
306 return _("Left");
307 case RGHT:
308 return _("Right");
309 case HOME:
310 return _("Home");
311 case END:
312 return _("End");
313 case PGDN:
314 return _("PageDown");
315 case PGUP:
316 return _("PageUp");
317 case TAB:
318 return _("Tab");
319 case STAB:
320 return _("Shift+Tab");
321 case ESC:
322 return _("Esc");
323 case KEY_IC:
324 return _("Insert");
325 default:
326 for (i = 0; i <= 63; i++)
327 if (key == KEY_F(i)) {
328 g_snprintf(buf, 32, _("F%d"), i );
329 return buf;
330 }
331 if (!(key & ~037))
332 g_snprintf(buf, 32, _("Ctrl-%c"), 'A'+(key & 037)-1 );
333 else if ((key & ~037) == 224)
334 g_snprintf(buf, 32, _("Alt-%c"), 'A'+(key & 037)-1 );
335 else if (key > 32 && key < 256)
336 g_snprintf(buf, 32, "%c", key);
337 else
338 g_snprintf(buf, 32, "0x%03X", key);
339 }
341 return buf;
342 }
344 void
345 command_dump_keys(void)
346 {
347 for (int i = 0; cmds[i].description; i++)
348 if (cmds[i].command != CMD_NONE)
349 printf(" %20s : %s\n",
350 get_key_names(cmds[i].command, true),
351 cmds[i].name);
352 }
354 #ifndef NCMPC_MINI
356 static void
357 set_key_flags(command_definition_t *cp, command_t command, int flags)
358 {
359 for (int i = 0; cp[i].name; i++) {
360 if (cp[i].command == command) {
361 cp[i].flags |= flags;
362 break;
363 }
364 }
365 }
367 #endif
369 const char *
370 get_key_names(command_t command, bool all)
371 {
372 for (int i = 0; cmds[i].description; i++) {
373 if (cmds[i].command == command) {
374 static char keystr[80];
376 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
377 if (!all)
378 return keystr;
380 for (int j = 1; j < MAX_COMMAND_KEYS &&
381 cmds[i].keys[j] > 0; j++) {
382 g_strlcat(keystr, " ", sizeof(keystr));
383 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
384 }
385 return keystr;
386 }
387 }
388 return NULL;
389 }
391 const char *
392 get_key_description(command_t command)
393 {
394 for (int i = 0; cmds[i].description; i++)
395 if (cmds[i].command == command)
396 return _(cmds[i].description);
398 return NULL;
399 }
401 const char *
402 get_key_command_name(command_t command)
403 {
404 for (int i = 0; cmds[i].name; i++)
405 if (cmds[i].command == command)
406 return cmds[i].name;
408 return NULL;
409 }
411 command_t
412 get_key_command_from_name(char *name)
413 {
414 for (int i = 0; cmds[i].name; i++)
415 if (strcmp(name, cmds[i].name) == 0)
416 return cmds[i].command;
418 return CMD_NONE;
419 }
421 command_t
422 find_key_command(int key, command_definition_t *c)
423 {
424 assert(key != 0);
425 assert(c != NULL);
427 for (int i = 0; c[i].name; i++) {
428 for (int j = 0; j < MAX_COMMAND_KEYS; j++)
429 if (c[i].keys[j] == key)
430 return c[i].command;
431 }
433 return CMD_NONE;
434 }
436 command_t
437 get_key_command(int key)
438 {
439 return find_key_command(key, cmds);
440 }
442 command_t
443 get_keyboard_command(void)
444 {
445 int key;
447 key = wgetch(stdscr);
448 if (key == ERR)
449 return CMD_NONE;
451 #ifdef HAVE_GETMOUSE
452 if (key == KEY_MOUSE)
453 return CMD_MOUSE_EVENT;
454 #endif
456 return get_key_command(key);
457 }
459 int
460 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
461 {
462 for (int i = 0; cmds[i].name; i++) {
463 if (cmds[i].command == command) {
464 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
465 #ifndef NCMPC_MINI
466 cmds[i].flags |= COMMAND_KEY_MODIFIED;
467 #endif
468 return 0;
469 }
470 }
472 return -1;
473 }
475 #ifndef NCMPC_MINI
477 int
478 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
479 {
480 int i;
481 int retval = 0;
483 if (cp == NULL)
484 cp = cmds;
486 for (i = 0; cp[i].name; i++)
487 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
489 for (i = 0; cp[i].name; i++) {
490 int j;
491 command_t cmd;
493 for(j=0; j<MAX_COMMAND_KEYS; j++) {
494 if (cp[i].keys[j] &&
495 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
496 if (buf) {
497 g_snprintf(buf, bufsize,
498 _("Key %s assigned to %s and %s"),
499 key2str(cp[i].keys[j]),
500 get_key_command_name(cp[i].command),
501 get_key_command_name(cmd));
502 } else {
503 fprintf(stderr,
504 _("Key %s assigned to %s and %s"),
505 key2str(cp[i].keys[j]),
506 get_key_command_name(cp[i].command),
507 get_key_command_name(cmd));
508 fputc('\n', stderr);
509 }
510 cp[i].flags |= COMMAND_KEY_CONFLICT;
511 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
512 retval = -1;
513 }
514 }
515 }
517 return retval;
518 }
520 int
521 write_key_bindings(FILE *f, int flags)
522 {
523 if (flags & KEYDEF_WRITE_HEADER)
524 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
526 for (int i = 0; cmds[i].name && !ferror(f); i++) {
527 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
528 flags & KEYDEF_WRITE_ALL) {
529 fprintf(f, "## %s\n", cmds[i].description);
530 if (flags & KEYDEF_COMMENT_ALL)
531 fprintf(f, "#");
532 fprintf(f, "key %s = ", cmds[i].name);
533 for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
534 if (j && cmds[i].keys[j])
535 fprintf(f, ", ");
536 if (!j || cmds[i].keys[j]) {
537 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
538 isdigit(cmds[i].keys[j])))
539 fprintf(f, "\'%c\'", cmds[i].keys[j]);
540 else
541 fprintf(f, "%d", cmds[i].keys[j]);
542 }
543 }
544 fprintf(f,"\n\n");
545 }
546 }
548 return ferror(f);
549 }
551 #endif /* NCMPC_MINI */