1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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 F9 KEY_F(9)
60 #define C(x) KEY_CTL(x)
62 static command_definition_t cmds[] = {
63 #ifdef ENABLE_KEYDEF_SCREEN
64 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
65 N_("Key configuration screen") },
66 #endif
67 { { 'q', 'Q', C('C') }, 0, CMD_QUIT, "quit",
68 N_("Quit") },
70 /* movement */
71 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
72 N_("Move cursor up") },
73 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
74 N_("Move cursor down") },
75 { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
76 N_("Move cursor to the top of screen") },
77 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
78 N_("Move cursor to the middle of screen") },
79 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
80 N_("Move cursor to the bottom of screen") },
81 { { HOME, C('A'), 0 }, 0, CMD_LIST_FIRST, "home",
82 N_("Move cursor to the top of the list") },
83 { { END, C('E'), 0 }, 0, CMD_LIST_LAST, "end",
84 N_("Move cursor to the bottom of the list") },
85 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
86 N_("Page up") },
87 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
88 N_("Page down") },
89 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
90 N_("Range selection") },
91 { { C('N'), 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
92 N_("Scroll down one line") },
93 { { C('B'), 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
94 N_("Scroll up one line") },
95 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
96 N_("Scroll up half a screen") },
97 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
98 N_("Scroll down half a screen") },
99 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
100 N_("Select currently playing song") },
103 /* basic screens */
104 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
105 N_("Help screen") },
106 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
107 N_("Queue screen") },
108 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
109 N_("Browse screen") },
112 /* player commands */
113 { { RET, 0, 0 }, 0, CMD_PLAY, "play",
114 N_("Play/Enter directory") },
115 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
116 N_("Pause") },
117 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
118 N_("Stop") },
119 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
120 N_("Crop") },
121 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
122 N_("Next track") },
123 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
124 N_("Previous track") },
125 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
126 N_("Seek forward") },
127 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
128 N_("Seek backward") },
129 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
130 N_("Increase volume") },
131 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
132 N_("Decrease volume") },
133 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
134 N_("Select/deselect song in queue") },
135 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
136 N_("Select all listed items") },
137 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
138 N_("Delete song from queue") },
139 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
140 N_("Shuffle queue") },
141 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
142 N_("Clear queue") },
143 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
144 N_("Toggle repeat mode") },
145 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
146 N_("Toggle random mode") },
147 { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
148 N_("Toggle single mode") },
149 { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
150 N_("Toggle consume mode") },
151 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
152 N_("Toggle crossfade mode") },
153 { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
154 N_("Start a music database update") },
155 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
156 N_("Save queue") },
157 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
158 N_("Add url/file to queue") },
160 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
161 N_("Go to root directory") },
162 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
163 N_("Go to parent directory") },
165 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
166 N_("Locate song in browser") },
168 /* lists */
169 { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
170 N_("Move item up") },
171 { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
172 N_("Move item down") },
173 { { C('L'), 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
174 N_("Refresh screen") },
177 /* ncmpc options */
178 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
179 /* translators: toggle between wrapping and non-wrapping
180 search */
181 N_("Toggle find mode") },
182 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
183 /* translators: the auto center mode always centers the song
184 currently being played */
185 N_("Toggle auto center mode") },
188 /* change screen */
189 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
190 N_("Next screen") },
191 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
192 N_("Previous screen") },
193 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
194 N_("Swap to most recent screen") },
197 /* find */
198 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
199 N_("Forward find") },
200 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
201 N_("Forward find next") },
202 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
203 N_("Backward find") },
204 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
205 N_("Backward find previous") },
206 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
207 /* translators: this queries the user for a string
208 * and jumps directly (while the user is typing)
209 * to the entry which begins with this string */
210 N_("Jump to") },
213 /* extra screens */
214 #ifdef ENABLE_ARTIST_SCREEN
215 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
216 N_("Artist screen") },
217 #endif
218 #ifdef ENABLE_SEARCH_SCREEN
219 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
220 N_("Search screen") },
221 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
222 N_("Change search mode") },
223 #endif
224 #ifdef ENABLE_SONG_SCREEN
225 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
226 N_("View the selected and the currently playing song") },
227 #endif
228 #ifdef ENABLE_LYRICS_SCREEN
229 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
230 N_("Lyrics screen") },
231 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
232 /* translators: interrupt the current background action,
233 e.g. stop loading lyrics from the internet */
234 N_("Interrupt action") },
235 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
236 N_("Update Lyrics") },
237 /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
238 at some point */
239 { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
240 N_("Edit the current item") },
241 #endif
243 #ifdef ENABLE_OUTPUTS_SCREEN
244 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
245 N_("Outputs screen") },
246 #endif
248 #ifdef ENABLE_CHAT_SCREEN
249 { {'9', F9, 0}, 0, CMD_SCREEN_CHAT, "screen-chat",
250 N_("Chat screen") },
251 #endif
253 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
254 };
256 #ifdef ENABLE_KEYDEF_SCREEN
257 command_definition_t *
258 get_command_definitions(void)
259 {
260 return cmds;
261 }
263 size_t
264 get_cmds_max_name_width(command_definition_t *c)
265 {
266 static size_t max = 0;
268 if (max != 0)
269 return max;
271 for (command_definition_t *p = c; p->name != NULL; p++) {
272 /*
273 * width and length are considered the same here, as command
274 * names are not translated.
275 */
276 size_t len = strlen(p->name);
277 if (len > max)
278 max = len;
279 }
281 return max;
282 }
283 #endif
285 const char *
286 key2str(int key)
287 {
288 switch(key) {
289 static char buf[32];
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 (int 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 return buf;
340 }
341 }
343 void
344 command_dump_keys(void)
345 {
346 for (size_t i = 0; cmds[i].description; i++)
347 if (cmds[i].command != CMD_NONE)
348 printf(" %20s : %s\n",
349 get_key_names(cmds[i].command, true),
350 cmds[i].name);
351 }
353 #ifndef NCMPC_MINI
355 static void
356 set_key_flags(command_definition_t *cp, command_t command, int flags)
357 {
358 for (size_t i = 0; cp[i].name; i++) {
359 if (cp[i].command == command) {
360 cp[i].flags |= flags;
361 break;
362 }
363 }
364 }
366 #endif
368 const char *
369 get_key_names(command_t command, bool all)
370 {
371 for (int i = 0; cmds[i].description; i++) {
372 if (cmds[i].command == command) {
373 static char keystr[80];
375 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
376 if (!all)
377 return keystr;
379 for (unsigned j = 1; j < MAX_COMMAND_KEYS &&
380 cmds[i].keys[j] > 0; j++) {
381 g_strlcat(keystr, " ", sizeof(keystr));
382 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
383 }
384 return keystr;
385 }
386 }
387 return NULL;
388 }
390 const char *
391 get_key_description(command_t command)
392 {
393 for (size_t i = 0; cmds[i].description; i++)
394 if (cmds[i].command == command)
395 return _(cmds[i].description);
397 return NULL;
398 }
400 const char *
401 get_key_command_name(command_t command)
402 {
403 for (size_t i = 0; cmds[i].name; i++)
404 if (cmds[i].command == command)
405 return cmds[i].name;
407 return NULL;
408 }
410 command_t
411 get_key_command_from_name(const char *name)
412 {
413 for (size_t i = 0; cmds[i].name; i++)
414 if (strcmp(name, cmds[i].name) == 0)
415 return cmds[i].command;
417 return CMD_NONE;
418 }
420 command_t
421 find_key_command(int key, const command_definition_t *c)
422 {
423 assert(key != 0);
424 assert(c != NULL);
426 for (size_t i = 0; c[i].name; i++) {
427 for (int j = 0; j < MAX_COMMAND_KEYS; j++)
428 if (c[i].keys[j] == key)
429 return c[i].command;
430 }
432 return CMD_NONE;
433 }
435 command_t
436 get_key_command(int key)
437 {
438 return find_key_command(key, cmds);
439 }
441 command_t
442 get_keyboard_command(void)
443 {
444 int key = wgetch(stdscr);
445 if (key == ERR || key == '\0')
446 return CMD_NONE;
448 #ifdef HAVE_GETMOUSE
449 if (key == KEY_MOUSE)
450 return CMD_MOUSE_EVENT;
451 #endif
453 return get_key_command(key);
454 }
456 bool
457 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
458 {
459 for (size_t i = 0; cmds[i].name; i++) {
460 if (cmds[i].command == command) {
461 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
462 #ifndef NCMPC_MINI
463 cmds[i].flags |= COMMAND_KEY_MODIFIED;
464 #endif
465 return true;
466 }
467 }
469 return false;
470 }
472 #ifndef NCMPC_MINI
474 bool
475 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
476 {
477 bool success = true;
479 if (cp == NULL)
480 cp = cmds;
482 for (size_t i = 0; cp[i].name; i++)
483 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
485 for (size_t i = 0; cp[i].name; i++) {
486 int j;
487 command_t cmd;
489 for(j=0; j<MAX_COMMAND_KEYS; j++) {
490 if (cp[i].keys[j] &&
491 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
492 if (buf) {
493 g_snprintf(buf, bufsize,
494 _("Key %s assigned to %s and %s"),
495 key2str(cp[i].keys[j]),
496 get_key_command_name(cp[i].command),
497 get_key_command_name(cmd));
498 } else {
499 fprintf(stderr,
500 _("Key %s assigned to %s and %s"),
501 key2str(cp[i].keys[j]),
502 get_key_command_name(cp[i].command),
503 get_key_command_name(cmd));
504 fputc('\n', stderr);
505 }
506 cp[i].flags |= COMMAND_KEY_CONFLICT;
507 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
508 success = false;
509 }
510 }
511 }
513 return success;
514 }
516 bool
517 write_key_bindings(FILE *f, int flags)
518 {
519 if (flags & KEYDEF_WRITE_HEADER)
520 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
522 for (size_t i = 0; cmds[i].name && !ferror(f); i++) {
523 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
524 flags & KEYDEF_WRITE_ALL) {
525 fprintf(f, "## %s\n", cmds[i].description);
526 if (flags & KEYDEF_COMMENT_ALL)
527 fprintf(f, "#");
528 fprintf(f, "key %s = ", cmds[i].name);
529 for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
530 if (j && cmds[i].keys[j])
531 fprintf(f, ", ");
532 if (!j || cmds[i].keys[j]) {
533 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
534 isdigit(cmds[i].keys[j])))
535 fprintf(f, "\'%c\'", cmds[i].keys[j]);
536 else
537 fprintf(f, "%d", cmds[i].keys[j]);
538 }
539 }
540 fprintf(f,"\n\n");
541 }
542 }
544 return ferror(f) == 0;
545 }
547 #endif /* NCMPC_MINI */