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 top of screen") },
81 { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
82 N_("Move cursor to middle of screen") },
83 { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
84 N_("Move cursor to bottom of screen") },
85 { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST, "home",
86 N_("Home") },
87 { { END, 0x05, 0 }, 0, CMD_LIST_LAST, "end",
88 N_("End") },
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_VISUAL_SELECT, "visual-select",
94 N_("Visual 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") },
105 /* basic screens */
106 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
107 N_("Help screen") },
108 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
109 N_("Playlist screen") },
110 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
111 N_("Browse screen") },
114 /* player commands */
115 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
116 N_("Play/Enter directory") },
117 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
118 N_("Pause") },
119 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
120 N_("Stop") },
121 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
122 N_("Crop") },
123 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
124 N_("Next track") },
125 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
126 N_("Previous track") },
127 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
128 N_("Seek forward") },
129 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
130 N_("Seek backward") },
131 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
132 N_("Increase volume") },
133 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
134 N_("Decrease volume") },
135 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
136 N_("Select/deselect song in playlist") },
137 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
138 N_("Select all listed items") },
139 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
140 N_("Delete song from playlist") },
141 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
142 N_("Shuffle playlist") },
143 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
144 N_("Clear playlist") },
145 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
146 N_("Toggle repeat mode") },
147 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
148 N_("Toggle random mode") },
149 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
150 N_("Toggle crossfade mode") },
151 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
152 N_("Start a music database update") },
153 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
154 N_("Save playlist") },
155 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
156 N_("Add url/file to playlist") },
158 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
159 N_("Go to root directory") },
160 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
161 N_("Go to parent directory") },
163 #ifdef ENABLE_SONG_SCREEN
164 { { 'i', 0, 0 }, 0, CMD_VIEW, "view",
165 N_("View the song") },
166 #endif
168 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
169 N_("Locate song in browser") },
171 /* lists */
172 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
173 N_("Move item up") },
174 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
175 N_("Move item down") },
176 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
177 N_("Refresh screen") },
180 /* ncmpc options */
181 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
182 /* translators: toggle between wrapping and non-wrapping
183 search */
184 N_("Toggle find mode") },
185 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
186 /* translators: the auto center mode always centers the song
187 currently being played */
188 N_("Toggle auto center mode") },
191 /* change screen */
192 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
193 N_("Next screen") },
194 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
195 N_("Previous screen") },
196 { { '~', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
197 N_("Swap to most recent screen") },
200 /* find */
201 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
202 N_("Forward find") },
203 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
204 N_("Forward find next") },
205 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
206 N_("Backward find") },
207 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
208 N_("Backward find previous") },
209 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
210 /* translators: this queries the user for a string
211 * and jumps directly (while the user is typing)
212 * to the entry which begins with this string */
213 N_("Jump to") },
216 /* extra screens */
217 #ifdef ENABLE_ARTIST_SCREEN
218 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
219 N_("Artist screen") },
220 #endif
221 #ifdef ENABLE_SEARCH_SCREEN
222 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
223 N_("Search screen") },
224 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
225 N_("Change search mode") },
226 #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 #endif
239 #ifdef ENABLE_OUTPUTS_SCREEN
240 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
241 N_("Outputs screen") },
242 #endif
245 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
246 };
248 #ifdef ENABLE_KEYDEF_SCREEN
249 command_definition_t *
250 get_command_definitions(void)
251 {
252 return cmds;
253 }
254 #endif
256 const char *
257 key2str(int key)
258 {
259 static char buf[32];
260 int i;
262 buf[0] = 0;
263 switch(key) {
264 case 0:
265 return _("Undefined");
266 case ' ':
267 return _("Space");
268 case 13:
269 return _("Enter");
270 case BS:
271 return _("Backspace");
272 case DEL:
273 return _("Delete");
274 case UP:
275 return _("Up");
276 case DWN:
277 return _("Down");
278 case LEFT:
279 return _("Left");
280 case RGHT:
281 return _("Right");
282 case HOME:
283 return _("Home");
284 case END:
285 return _("End");
286 case PGDN:
287 return _("PageDown");
288 case PGUP:
289 return _("PageUp");
290 case TAB:
291 return _("Tab");
292 case STAB:
293 return _("Shift+Tab");
294 case ESC:
295 return _("Esc");
296 case KEY_IC:
297 return _("Insert");
298 default:
299 for (i = 0; i <= 63; i++)
300 if (key == KEY_F(i)) {
301 g_snprintf(buf, 32, "F%d", i );
302 return buf;
303 }
304 if (!(key & ~037))
305 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
306 else if ((key & ~037) == 224)
307 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
308 else if (key > 32 && key < 256)
309 g_snprintf(buf, 32, "%c", key);
310 else
311 g_snprintf(buf, 32, "0x%03X", key);
312 }
314 return buf;
315 }
317 void
318 command_dump_keys(void)
319 {
320 int i;
322 i = 0;
323 while (cmds[i].description) {
324 if (cmds[i].command != CMD_NONE)
325 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
326 i++;
327 }
328 }
330 #ifndef NCMPC_MINI
332 static int
333 set_key_flags(command_definition_t *cp, command_t command, int flags)
334 {
335 int i;
337 i = 0;
338 while (cp[i].name) {
339 if (cp[i].command == command) {
340 cp[i].flags |= flags;
341 return 0;
342 }
343 i++;
344 }
346 return 1;
347 }
349 #endif
351 const char *
352 get_key_names(command_t command, int all)
353 {
354 int i;
356 i=0;
357 while (cmds[i].description) {
358 if (cmds[i].command == command) {
359 int j;
360 static char keystr[80];
362 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
363 if (!all)
364 return keystr;
365 j=1;
366 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
367 g_strlcat(keystr, " ", sizeof(keystr));
368 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
369 j++;
370 }
371 return keystr;
372 }
373 i++;
374 }
375 return NULL;
376 }
378 const char *
379 get_key_description(command_t command)
380 {
381 int i;
383 i=0;
384 while (cmds[i].description) {
385 if (cmds[i].command == command)
386 return _(cmds[i].description);
387 i++;
388 }
390 return NULL;
391 }
393 const char *
394 get_key_command_name(command_t command)
395 {
396 int i;
398 i=0;
399 while (cmds[i].name) {
400 if (cmds[i].command == command)
401 return cmds[i].name;
402 i++;
403 }
404 return NULL;
405 }
407 command_t
408 get_key_command_from_name(char *name)
409 {
410 int i;
412 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;
427 i=0;
428 while (key && c && c[i].name) {
429 if (c[i].keys[0] == key ||
430 c[i].keys[1] == key ||
431 c[i].keys[2] == key)
432 return c[i].command;
433 i++;
434 }
436 return CMD_NONE;
437 }
439 command_t
440 get_key_command(int key)
441 {
442 return find_key_command(key, cmds);
443 }
445 command_t
446 get_keyboard_command(void)
447 {
448 int key;
450 key = wgetch(stdscr);
451 if (key == ERR)
452 return CMD_NONE;
454 #ifdef HAVE_GETMOUSE
455 if (key == KEY_MOUSE)
456 return CMD_MOUSE_EVENT;
457 #endif
459 return get_key_command(key);
460 }
462 int
463 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
464 {
465 int i;
467 i=0;
468 while (cmds[i].name) {
469 if (cmds[i].command == command) {
470 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
471 #ifndef NCMPC_MINI
472 cmds[i].flags |= COMMAND_KEY_MODIFIED;
473 #endif
474 return 0;
475 }
476 i++;
477 }
478 return -1;
479 }
481 #ifndef NCMPC_MINI
483 int
484 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
485 {
486 int i;
487 int retval = 0;
489 if (cp == NULL)
490 cp = cmds;
492 i=0;
493 while (cp[i].name) {
494 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
495 i++;
496 }
498 i=0;
499 while (cp[i].name) {
500 int j;
501 command_t cmd;
503 for(j=0; j<MAX_COMMAND_KEYS; j++)
504 if (cp[i].keys[j] &&
505 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
506 if (buf) {
507 g_snprintf(buf, bufsize,
508 _("Key %s assigned to %s and %s"),
509 key2str(cp[i].keys[j]),
510 get_key_command_name(cp[i].command),
511 get_key_command_name(cmd));
512 } else {
513 fprintf(stderr,
514 _("Key %s assigned to %s and %s"),
515 key2str(cp[i].keys[j]),
516 get_key_command_name(cp[i].command),
517 get_key_command_name(cmd));
518 fputc('\n', stderr);
519 }
520 cp[i].flags |= COMMAND_KEY_CONFLICT;
521 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
522 retval = -1;
523 }
524 i++;
525 }
526 return retval;
527 }
529 int
530 write_key_bindings(FILE *f, int flags)
531 {
532 int i,j;
534 if (flags & KEYDEF_WRITE_HEADER)
535 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
537 i = 0;
538 while (cmds[i].name && !ferror(f)) {
539 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
540 flags & KEYDEF_WRITE_ALL) {
541 fprintf(f, "## %s\n", cmds[i].description);
542 if (flags & KEYDEF_COMMENT_ALL)
543 fprintf(f, "#");
544 fprintf(f, "key %s = ", cmds[i].name);
545 for (j = 0; j < MAX_COMMAND_KEYS; j++) {
546 if (j && cmds[i].keys[j])
547 fprintf(f, ", ");
548 if (!j || cmds[i].keys[j]) {
549 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
550 isdigit(cmds[i].keys[j])))
551 fprintf(f, "\'%c\'", cmds[i].keys[j]);
552 else
553 fprintf(f, "%d", cmds[i].keys[j]);
554 }
555 }
556 fprintf(f,"\n\n");
557 }
558 i++;
559 }
561 return ferror(f);
562 }
564 #endif