1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
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.
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.
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 F1 KEY_F(1)
59 #define F2 KEY_F(2)
60 #define F3 KEY_F(3)
61 #define F4 KEY_F(4)
62 #define F5 KEY_F(5)
63 #define F6 KEY_F(6)
64 #define F7 KEY_F(7)
65 #define F8 KEY_F(8)
68 static command_definition_t cmds[] = {
69 #ifdef ENABLE_KEYDEF_SCREEN
70 { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
71 N_("Key configuration screen") },
72 #endif
73 { { 'q', 'Q', 3 }, 0, CMD_QUIT, "quit",
74 N_("Quit") },
76 /* movement */
77 { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
78 N_("Move cursor up") },
79 { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
80 N_("Move cursor down") },
81 { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
82 N_("Move cursor to the top of screen") },
83 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
84 N_("Move cursor to the middle of screen") },
85 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
86 N_("Move cursor to the bottom of screen") },
87 { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST, "home",
88 N_("Move cursor to the top of the list") },
89 { { END, 0x05, 0 }, 0, CMD_LIST_LAST, "end",
90 N_("Move cursor to the bottom of the list") },
91 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
92 N_("Page up") },
93 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
94 N_("Page down") },
95 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
96 N_("Range selection") },
97 { { 14, 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
98 N_("Scroll up one line") },
99 { { 2, 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
100 N_("Scroll down one line") },
101 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
102 N_("Scroll up half a screen") },
103 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
104 N_("Scroll down half a screen") },
105 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
106 N_("Select currently playing song") },
109 /* basic screens */
110 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
111 N_("Help screen") },
112 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
113 N_("Playlist screen") },
114 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
115 N_("Browse screen") },
118 /* player commands */
119 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
120 N_("Play/Enter directory") },
121 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
122 N_("Pause") },
123 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
124 N_("Stop") },
125 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
126 N_("Crop") },
127 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
128 N_("Next track") },
129 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
130 N_("Previous track") },
131 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
132 N_("Seek forward") },
133 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
134 N_("Seek backward") },
135 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
136 N_("Increase volume") },
137 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
138 N_("Decrease volume") },
139 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
140 N_("Select/deselect song in playlist") },
141 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
142 N_("Select all listed items") },
143 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
144 N_("Delete song from playlist") },
145 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
146 N_("Shuffle playlist") },
147 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
148 N_("Clear playlist") },
149 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
150 N_("Toggle repeat mode") },
151 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
152 N_("Toggle random mode") },
153 { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
154 N_("Toggle single mode") },
155 { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
156 N_("Toggle consume mode") },
157 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
158 N_("Toggle crossfade mode") },
159 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
160 N_("Start a music database update") },
161 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
162 N_("Save playlist") },
163 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
164 N_("Add url/file to playlist") },
166 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
167 N_("Go to root directory") },
168 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
169 N_("Go to parent directory") },
171 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
172 N_("Locate song in browser") },
174 /* lists */
175 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
176 N_("Move item up") },
177 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
178 N_("Move item down") },
179 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
180 N_("Refresh screen") },
183 /* ncmpc options */
184 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
185 /* translators: toggle between wrapping and non-wrapping
186 search */
187 N_("Toggle find mode") },
188 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
189 /* translators: the auto center mode always centers the song
190 currently being played */
191 N_("Toggle auto center mode") },
194 /* change screen */
195 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
196 N_("Next screen") },
197 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
198 N_("Previous screen") },
199 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
200 N_("Swap to most recent screen") },
203 /* find */
204 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
205 N_("Forward find") },
206 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
207 N_("Forward find next") },
208 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
209 N_("Backward find") },
210 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
211 N_("Backward find previous") },
212 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
213 /* translators: this queries the user for a string
214 * and jumps directly (while the user is typing)
215 * to the entry which begins with this string */
216 N_("Jump to") },
219 /* extra screens */
220 #ifdef ENABLE_ARTIST_SCREEN
221 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
222 N_("Artist screen") },
223 #endif
224 #ifdef ENABLE_SEARCH_SCREEN
225 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
226 N_("Search screen") },
227 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
228 N_("Change search mode") },
229 #endif
230 #ifdef ENABLE_SONG_SCREEN
231 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
232 N_("View the selected and the currently playing song") },
233 #endif
234 #ifdef ENABLE_LYRICS_SCREEN
235 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
236 N_("Lyrics screen") },
237 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
238 /* translators: interrupt the current background action,
239 e.g. stop loading lyrics from the internet */
240 N_("Interrupt action") },
241 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
242 N_("Update Lyrics") },
243 #endif
245 #ifdef ENABLE_OUTPUTS_SCREEN
246 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
247 N_("Outputs screen") },
248 #endif
251 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
252 };
254 #ifdef ENABLE_KEYDEF_SCREEN
255 command_definition_t *
256 get_command_definitions(void)
257 {
258 return cmds;
259 }
260 #endif
262 const char *
263 key2str(int key)
264 {
265 static char buf[32];
266 int i;
268 buf[0] = 0;
269 switch(key) {
270 case 0:
271 return _("Undefined");
272 case ' ':
273 return _("Space");
274 case 13:
275 return _("Enter");
276 case BS:
277 return _("Backspace");
278 case DEL:
279 return _("Delete");
280 case UP:
281 return _("Up");
282 case DWN:
283 return _("Down");
284 case LEFT:
285 return _("Left");
286 case RGHT:
287 return _("Right");
288 case HOME:
289 return _("Home");
290 case END:
291 return _("End");
292 case PGDN:
293 return _("PageDown");
294 case PGUP:
295 return _("PageUp");
296 case TAB:
297 return _("Tab");
298 case STAB:
299 return _("Shift+Tab");
300 case ESC:
301 return _("Esc");
302 case KEY_IC:
303 return _("Insert");
304 default:
305 for (i = 0; i <= 63; i++)
306 if (key == KEY_F(i)) {
307 g_snprintf(buf, 32, "F%d", i );
308 return buf;
309 }
310 if (!(key & ~037))
311 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
312 else if ((key & ~037) == 224)
313 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
314 else if (key > 32 && key < 256)
315 g_snprintf(buf, 32, "%c", key);
316 else
317 g_snprintf(buf, 32, "0x%03X", key);
318 }
320 return buf;
321 }
323 void
324 command_dump_keys(void)
325 {
326 int i = 0;
328 while (cmds[i].description) {
329 if (cmds[i].command != CMD_NONE)
330 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
331 i++;
332 }
333 }
335 #ifndef NCMPC_MINI
337 static int
338 set_key_flags(command_definition_t *cp, command_t command, int flags)
339 {
340 int i = 0;
342 while (cp[i].name) {
343 if (cp[i].command == command) {
344 cp[i].flags |= flags;
345 return 0;
346 }
347 i++;
348 }
350 return 1;
351 }
353 #endif
355 const char *
356 get_key_names(command_t command, int all)
357 {
358 int i = 0;
360 while (cmds[i].description) {
361 if (cmds[i].command == command) {
362 int j;
363 static char keystr[80];
365 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
366 if (!all)
367 return keystr;
368 j=1;
369 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
370 g_strlcat(keystr, " ", sizeof(keystr));
371 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
372 j++;
373 }
374 return keystr;
375 }
376 i++;
377 }
378 return NULL;
379 }
381 const char *
382 get_key_description(command_t command)
383 {
384 int i = 0;
386 while (cmds[i].description) {
387 if (cmds[i].command == command)
388 return _(cmds[i].description);
389 i++;
390 }
392 return NULL;
393 }
395 const char *
396 get_key_command_name(command_t command)
397 {
398 int i = 0;
400 while (cmds[i].name) {
401 if (cmds[i].command == command)
402 return cmds[i].name;
403 i++;
404 }
405 return NULL;
406 }
408 command_t
409 get_key_command_from_name(char *name)
410 {
411 int i = 0;
413 while (cmds[i].name) {
414 if (strcmp(name, cmds[i].name) == 0)
415 return cmds[i].command;
416 i++;
417 }
419 return CMD_NONE;
420 }
422 command_t
423 find_key_command(int key, command_definition_t *c)
424 {
425 int i = 0;
427 while (key && c && c[i].name) {
428 if (c[i].keys[0] == key ||
429 c[i].keys[1] == key ||
430 c[i].keys[2] == key)
431 return c[i].command;
432 i++;
433 }
435 return CMD_NONE;
436 }
438 command_t
439 get_key_command(int key)
440 {
441 return find_key_command(key, cmds);
442 }
444 command_t
445 get_keyboard_command(void)
446 {
447 int key;
449 key = wgetch(stdscr);
450 if (key == ERR)
451 return CMD_NONE;
453 #ifdef HAVE_GETMOUSE
454 if (key == KEY_MOUSE)
455 return CMD_MOUSE_EVENT;
456 #endif
458 return get_key_command(key);
459 }
461 int
462 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
463 {
464 int i = 0;
466 while (cmds[i].name) {
467 if (cmds[i].command == command) {
468 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
469 #ifndef NCMPC_MINI
470 cmds[i].flags |= COMMAND_KEY_MODIFIED;
471 #endif
472 return 0;
473 }
474 i++;
475 }
476 return -1;
477 }
479 #ifndef NCMPC_MINI
481 int
482 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
483 {
484 int i = 0;
485 int retval = 0;
487 if (cp == NULL)
488 cp = cmds;
490 while (cp[i].name) {
491 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
492 i++;
493 }
495 i = 0;
496 while (cp[i].name) {
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 if (buf) {
504 g_snprintf(buf, bufsize,
505 _("Key %s assigned to %s and %s"),
506 key2str(cp[i].keys[j]),
507 get_key_command_name(cp[i].command),
508 get_key_command_name(cmd));
509 } else {
510 fprintf(stderr,
511 _("Key %s assigned to %s and %s"),
512 key2str(cp[i].keys[j]),
513 get_key_command_name(cp[i].command),
514 get_key_command_name(cmd));
515 fputc('\n', stderr);
516 }
517 cp[i].flags |= COMMAND_KEY_CONFLICT;
518 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
519 retval = -1;
520 }
521 i++;
522 }
523 return retval;
524 }
526 int
527 write_key_bindings(FILE *f, int flags)
528 {
529 int i = 0, j;
531 if (flags & KEYDEF_WRITE_HEADER)
532 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
534 while (cmds[i].name && !ferror(f)) {
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 (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 i++;
555 }
557 return ferror(f);
558 }
560 #endif