1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 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 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)
63 #define F8 KEY_F(8)
66 static command_definition_t cmds[] = {
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 /* movement */
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 { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
80 N_("Move cursor to the top of screen") },
81 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
82 N_("Move cursor to the middle of screen") },
83 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
84 N_("Move cursor to the bottom of screen") },
85 { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST, "home",
86 N_("Move cursor to the top of the list") },
87 { { END, 0x05, 0 }, 0, CMD_LIST_LAST, "end",
88 N_("Move cursor to the bottom of the list") },
89 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
90 N_("Page up") },
91 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
92 N_("Page down") },
93 { { 'v', 0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
94 N_("Range selection") },
95 { { 14, 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
96 N_("Scroll up one line") },
97 { { 2, 0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
98 N_("Scroll down one line") },
99 { { 'N', 0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
100 N_("Scroll up half a screen") },
101 { { 'B', 0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
102 N_("Scroll down half a screen") },
103 { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
104 N_("Select currently playing song") },
107 /* basic screens */
108 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
109 N_("Help screen") },
110 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
111 N_("Playlist screen") },
112 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
113 N_("Browse screen") },
116 /* player commands */
117 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
118 N_("Play/Enter directory") },
119 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
120 N_("Pause") },
121 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
122 N_("Stop") },
123 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
124 N_("Crop") },
125 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
126 N_("Next track") },
127 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
128 N_("Previous track") },
129 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
130 N_("Seek forward") },
131 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
132 N_("Seek backward") },
133 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
134 N_("Increase volume") },
135 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
136 N_("Decrease volume") },
137 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
138 N_("Select/deselect song in playlist") },
139 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
140 N_("Select all listed items") },
141 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
142 N_("Delete song from playlist") },
143 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
144 N_("Shuffle playlist") },
145 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
146 N_("Clear playlist") },
147 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
148 N_("Toggle repeat mode") },
149 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
150 N_("Toggle random mode") },
151 { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
152 N_("Toggle single mode") },
153 { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
154 N_("Toggle consume mode") },
155 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
156 N_("Toggle crossfade mode") },
157 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
158 N_("Start a music database update") },
159 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
160 N_("Save playlist") },
161 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
162 N_("Add url/file to playlist") },
164 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
165 N_("Go to root directory") },
166 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
167 N_("Go to parent directory") },
169 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
170 N_("Locate song in browser") },
172 /* lists */
173 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
174 N_("Move item up") },
175 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
176 N_("Move item down") },
177 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
178 N_("Refresh screen") },
181 /* ncmpc options */
182 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
183 /* translators: toggle between wrapping and non-wrapping
184 search */
185 N_("Toggle find mode") },
186 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
187 /* translators: the auto center mode always centers the song
188 currently being played */
189 N_("Toggle auto center mode") },
192 /* change screen */
193 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
194 N_("Next screen") },
195 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
196 N_("Previous screen") },
197 { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
198 N_("Swap to most recent screen") },
201 /* find */
202 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
203 N_("Forward find") },
204 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
205 N_("Forward find next") },
206 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
207 N_("Backward find") },
208 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
209 N_("Backward find previous") },
210 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
211 /* translators: this queries the user for a string
212 * and jumps directly (while the user is typing)
213 * to the entry which begins with this string */
214 N_("Jump to") },
217 /* extra screens */
218 #ifdef ENABLE_ARTIST_SCREEN
219 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
220 N_("Artist screen") },
221 #endif
222 #ifdef ENABLE_SEARCH_SCREEN
223 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
224 N_("Search screen") },
225 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
226 N_("Change search mode") },
227 #endif
228 #ifdef ENABLE_SONG_SCREEN
229 { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
230 N_("View the selected and the currently playing song") },
231 #endif
232 #ifdef ENABLE_LYRICS_SCREEN
233 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
234 N_("Lyrics screen") },
235 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
236 /* translators: interrupt the current background action,
237 e.g. stop loading lyrics from the internet */
238 N_("Interrupt action") },
239 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
240 N_("Update Lyrics") },
241 #endif
243 #ifdef ENABLE_OUTPUTS_SCREEN
244 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
245 N_("Outputs screen") },
246 #endif
249 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
250 };
252 #ifdef ENABLE_KEYDEF_SCREEN
253 command_definition_t *
254 get_command_definitions(void)
255 {
256 return cmds;
257 }
258 #endif
260 const char *
261 key2str(int key)
262 {
263 static char buf[32];
264 int i;
266 buf[0] = 0;
267 switch(key) {
268 case 0:
269 return _("Undefined");
270 case ' ':
271 return _("Space");
272 case 13:
273 return _("Enter");
274 case BS:
275 return _("Backspace");
276 case DEL:
277 return _("Delete");
278 case UP:
279 return _("Up");
280 case DWN:
281 return _("Down");
282 case LEFT:
283 return _("Left");
284 case RGHT:
285 return _("Right");
286 case HOME:
287 return _("Home");
288 case END:
289 return _("End");
290 case PGDN:
291 return _("PageDown");
292 case PGUP:
293 return _("PageUp");
294 case TAB:
295 return _("Tab");
296 case STAB:
297 return _("Shift+Tab");
298 case ESC:
299 return _("Esc");
300 case KEY_IC:
301 return _("Insert");
302 default:
303 for (i = 0; i <= 63; i++)
304 if (key == KEY_F(i)) {
305 g_snprintf(buf, 32, "F%d", i );
306 return buf;
307 }
308 if (!(key & ~037))
309 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
310 else if ((key & ~037) == 224)
311 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
312 else if (key > 32 && key < 256)
313 g_snprintf(buf, 32, "%c", key);
314 else
315 g_snprintf(buf, 32, "0x%03X", key);
316 }
318 return buf;
319 }
321 void
322 command_dump_keys(void)
323 {
324 int i;
326 i = 0;
327 while (cmds[i].description) {
328 if (cmds[i].command != CMD_NONE)
329 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
330 i++;
331 }
332 }
334 #ifndef NCMPC_MINI
336 static int
337 set_key_flags(command_definition_t *cp, command_t command, int flags)
338 {
339 int i;
341 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;
360 i=0;
361 while (cmds[i].description) {
362 if (cmds[i].command == command) {
363 int j;
364 static char keystr[80];
366 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
367 if (!all)
368 return keystr;
369 j=1;
370 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
371 g_strlcat(keystr, " ", sizeof(keystr));
372 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
373 j++;
374 }
375 return keystr;
376 }
377 i++;
378 }
379 return NULL;
380 }
382 const char *
383 get_key_description(command_t command)
384 {
385 int i;
387 i=0;
388 while (cmds[i].description) {
389 if (cmds[i].command == command)
390 return _(cmds[i].description);
391 i++;
392 }
394 return NULL;
395 }
397 const char *
398 get_key_command_name(command_t command)
399 {
400 int i;
402 i=0;
403 while (cmds[i].name) {
404 if (cmds[i].command == command)
405 return cmds[i].name;
406 i++;
407 }
408 return NULL;
409 }
411 command_t
412 get_key_command_from_name(char *name)
413 {
414 int i;
416 i=0;
417 while (cmds[i].name) {
418 if (strcmp(name, cmds[i].name) == 0)
419 return cmds[i].command;
420 i++;
421 }
423 return CMD_NONE;
424 }
426 command_t
427 find_key_command(int key, command_definition_t *c)
428 {
429 int i;
431 i=0;
432 while (key && c && c[i].name) {
433 if (c[i].keys[0] == key ||
434 c[i].keys[1] == key ||
435 c[i].keys[2] == key)
436 return c[i].command;
437 i++;
438 }
440 return CMD_NONE;
441 }
443 command_t
444 get_key_command(int key)
445 {
446 return find_key_command(key, cmds);
447 }
449 command_t
450 get_keyboard_command(void)
451 {
452 int key;
454 key = wgetch(stdscr);
455 if (key == ERR)
456 return CMD_NONE;
458 #ifdef HAVE_GETMOUSE
459 if (key == KEY_MOUSE)
460 return CMD_MOUSE_EVENT;
461 #endif
463 return get_key_command(key);
464 }
466 int
467 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
468 {
469 int i;
471 i=0;
472 while (cmds[i].name) {
473 if (cmds[i].command == command) {
474 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
475 #ifndef NCMPC_MINI
476 cmds[i].flags |= COMMAND_KEY_MODIFIED;
477 #endif
478 return 0;
479 }
480 i++;
481 }
482 return -1;
483 }
485 #ifndef NCMPC_MINI
487 int
488 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
489 {
490 int i;
491 int retval = 0;
493 if (cp == NULL)
494 cp = cmds;
496 i=0;
497 while (cp[i].name) {
498 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
499 i++;
500 }
502 i=0;
503 while (cp[i].name) {
504 int j;
505 command_t cmd;
507 for(j=0; j<MAX_COMMAND_KEYS; j++)
508 if (cp[i].keys[j] &&
509 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
510 if (buf) {
511 g_snprintf(buf, bufsize,
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 } else {
517 fprintf(stderr,
518 _("Key %s assigned to %s and %s"),
519 key2str(cp[i].keys[j]),
520 get_key_command_name(cp[i].command),
521 get_key_command_name(cmd));
522 fputc('\n', stderr);
523 }
524 cp[i].flags |= COMMAND_KEY_CONFLICT;
525 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
526 retval = -1;
527 }
528 i++;
529 }
530 return retval;
531 }
533 int
534 write_key_bindings(FILE *f, int flags)
535 {
536 int i,j;
538 if (flags & KEYDEF_WRITE_HEADER)
539 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
541 i = 0;
542 while (cmds[i].name && !ferror(f)) {
543 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
544 flags & KEYDEF_WRITE_ALL) {
545 fprintf(f, "## %s\n", cmds[i].description);
546 if (flags & KEYDEF_COMMENT_ALL)
547 fprintf(f, "#");
548 fprintf(f, "key %s = ", cmds[i].name);
549 for (j = 0; j < MAX_COMMAND_KEYS; j++) {
550 if (j && cmds[i].keys[j])
551 fprintf(f, ", ");
552 if (!j || cmds[i].keys[j]) {
553 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
554 isdigit(cmds[i].keys[j])))
555 fprintf(f, "\'%c\'", cmds[i].keys[j]);
556 else
557 fprintf(f, "%d", cmds[i].keys[j]);
558 }
559 }
560 fprintf(f,"\n\n");
561 }
562 i++;
563 }
565 return ferror(f);
566 }
568 #endif