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 /* movment */
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") },
101 /* basic screens */
102 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
103 N_("Help screen") },
104 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
105 N_("Playlist screen") },
106 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
107 N_("Browse screen") },
110 /* player commands */
111 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
112 N_("Play/Enter directory") },
113 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
114 N_("Pause") },
115 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
116 N_("Stop") },
117 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
118 N_("Crop") },
119 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
120 N_("Next track") },
121 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
122 N_("Previous track") },
123 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
124 N_("Seek forward") },
125 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
126 N_("Seek backward") },
127 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
128 N_("Increase volume") },
129 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
130 N_("Decrease volume") },
131 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
132 N_("Select/deselect song in playlist") },
133 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
134 N_("Select all listed items") },
135 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
136 N_("Delete song from playlist") },
137 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
138 N_("Shuffle playlist") },
139 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
140 N_("Clear playlist") },
141 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
142 N_("Toggle repeat mode") },
143 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
144 N_("Toggle random mode") },
145 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
146 N_("Toggle crossfade mode") },
147 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
148 N_("Start a music database update") },
149 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
150 N_("Save playlist") },
151 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
152 N_("Add url/file to playlist") },
154 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
155 N_("Go to root directory") },
156 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
157 N_("Go to parent directory") },
159 #ifdef ENABLE_SONG_SCREEN
160 { { 'i', 0, 0 }, 0, CMD_VIEW, "view",
161 N_("View the song") },
162 #endif
164 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
165 N_("Locate song in browser") },
167 /* lists */
168 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
169 N_("Move item up") },
170 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
171 N_("Move item down") },
172 { { 12, 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") },
194 /* find */
195 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
196 N_("Forward find") },
197 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
198 N_("Forward find next") },
199 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
200 N_("Backward find") },
201 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
202 N_("Backward find previous") },
203 { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
204 /* translators: this queries the user for a string
205 * and jumps directly (while the user is typing)
206 * to the entry which begins with this string */
207 N_("Jump to") },
210 /* extra screens */
211 #ifdef ENABLE_ARTIST_SCREEN
212 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
213 N_("Artist screen") },
214 #endif
215 #ifdef ENABLE_SEARCH_SCREEN
216 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
217 N_("Search screen") },
218 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
219 N_("Change search mode") },
220 #endif
222 #ifdef ENABLE_LYRICS_SCREEN
223 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
224 N_("Lyrics screen") },
225 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
226 /* translators: interrupt the current background action,
227 e.g. stop loading lyrics from the internet */
228 N_("Interrupt action") },
229 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
230 N_("Update Lyrics") },
231 #endif
233 #ifdef ENABLE_OUTPUTS_SCREEN
234 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
235 N_("Outputs screen") },
236 #endif
239 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
240 };
242 #ifdef ENABLE_KEYDEF_SCREEN
243 command_definition_t *
244 get_command_definitions(void)
245 {
246 return cmds;
247 }
248 #endif
250 const char *
251 key2str(int key)
252 {
253 static char buf[32];
254 int i;
256 buf[0] = 0;
257 switch(key) {
258 case 0:
259 return _("Undefined");
260 case ' ':
261 return _("Space");
262 case 13:
263 return _("Enter");
264 case BS:
265 return _("Backspace");
266 case DEL:
267 return _("Delete");
268 case UP:
269 return _("Up");
270 case DWN:
271 return _("Down");
272 case LEFT:
273 return _("Left");
274 case RGHT:
275 return _("Right");
276 case HOME:
277 return _("Home");
278 case END:
279 return _("End");
280 case PGDN:
281 return _("PageDown");
282 case PGUP:
283 return _("PageUp");
284 case TAB:
285 return _("Tab");
286 case STAB:
287 return _("Shift+Tab");
288 case ESC:
289 return _("Esc");
290 case KEY_IC:
291 return _("Insert");
292 default:
293 for (i = 0; i <= 63; i++)
294 if (key == KEY_F(i)) {
295 g_snprintf(buf, 32, "F%d", i );
296 return buf;
297 }
298 if (!(key & ~037))
299 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
300 else if ((key & ~037) == 224)
301 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
302 else if (key > 32 && key < 256)
303 g_snprintf(buf, 32, "%c", key);
304 else
305 g_snprintf(buf, 32, "0x%03X", key);
306 }
308 return buf;
309 }
311 void
312 command_dump_keys(void)
313 {
314 int i;
316 i = 0;
317 while (cmds[i].description) {
318 if (cmds[i].command != CMD_NONE)
319 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
320 i++;
321 }
322 }
324 #ifndef NCMPC_MINI
326 static int
327 set_key_flags(command_definition_t *cp, command_t command, int flags)
328 {
329 int i;
331 i = 0;
332 while (cp[i].name) {
333 if (cp[i].command == command) {
334 cp[i].flags |= flags;
335 return 0;
336 }
337 i++;
338 }
340 return 1;
341 }
343 #endif
345 const char *
346 get_key_names(command_t command, int all)
347 {
348 int i;
350 i=0;
351 while (cmds[i].description) {
352 if (cmds[i].command == command) {
353 int j;
354 static char keystr[80];
356 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
357 if (!all)
358 return keystr;
359 j=1;
360 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
361 g_strlcat(keystr, " ", sizeof(keystr));
362 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
363 j++;
364 }
365 return keystr;
366 }
367 i++;
368 }
369 return NULL;
370 }
372 const char *
373 get_key_description(command_t command)
374 {
375 int i;
377 i=0;
378 while (cmds[i].description) {
379 if (cmds[i].command == command)
380 return _(cmds[i].description);
381 i++;
382 }
384 return NULL;
385 }
387 const char *
388 get_key_command_name(command_t command)
389 {
390 int i;
392 i=0;
393 while (cmds[i].name) {
394 if (cmds[i].command == command)
395 return cmds[i].name;
396 i++;
397 }
398 return NULL;
399 }
401 command_t
402 get_key_command_from_name(char *name)
403 {
404 int i;
406 i=0;
407 while (cmds[i].name) {
408 if (strcmp(name, cmds[i].name) == 0)
409 return cmds[i].command;
410 i++;
411 }
413 return CMD_NONE;
414 }
416 command_t
417 find_key_command(int key, command_definition_t *c)
418 {
419 int i;
421 i=0;
422 while (key && c && c[i].name) {
423 if (c[i].keys[0] == key ||
424 c[i].keys[1] == key ||
425 c[i].keys[2] == key)
426 return c[i].command;
427 i++;
428 }
430 return CMD_NONE;
431 }
433 command_t
434 get_key_command(int key)
435 {
436 return find_key_command(key, cmds);
437 }
439 command_t
440 get_keyboard_command(void)
441 {
442 int key;
444 key = wgetch(stdscr);
445 if (key == ERR)
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 int
457 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
458 {
459 int i;
461 i=0;
462 while (cmds[i].name) {
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 i++;
471 }
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 i=0;
487 while (cp[i].name) {
488 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
489 i++;
490 }
492 i=0;
493 while (cp[i].name) {
494 int j;
495 command_t cmd;
497 for(j=0; j<MAX_COMMAND_KEYS; j++)
498 if (cp[i].keys[j] &&
499 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
500 if (buf) {
501 g_snprintf(buf, bufsize,
502 _("Key %s assigned to %s and %s"),
503 key2str(cp[i].keys[j]),
504 get_key_command_name(cp[i].command),
505 get_key_command_name(cmd));
506 } else {
507 fprintf(stderr,
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 fputc('\n', stderr);
513 }
514 cp[i].flags |= COMMAND_KEY_CONFLICT;
515 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
516 retval = -1;
517 }
518 i++;
519 }
520 return retval;
521 }
523 int
524 write_key_bindings(FILE *f, int flags)
525 {
526 int i,j;
528 if (flags & KEYDEF_WRITE_HEADER)
529 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
531 i = 0;
532 while (cmds[i].name && !ferror(f)) {
533 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
534 flags & KEYDEF_WRITE_ALL) {
535 fprintf(f, "## %s\n", cmds[i].description);
536 if (flags & KEYDEF_COMMENT_ALL)
537 fprintf(f, "#");
538 fprintf(f, "key %s = ", cmds[i].name);
539 for (j = 0; j < MAX_COMMAND_KEYS; j++) {
540 if (j && cmds[i].keys[j])
541 fprintf(f, ", ");
542 if (!j || cmds[i].keys[j]) {
543 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
544 isdigit(cmds[i].keys[j])))
545 fprintf(f, "\'%c\'", cmds[i].keys[j]);
546 else
547 fprintf(f, "%d", cmds[i].keys[j]);
548 }
549 }
550 fprintf(f,"\n\n");
551 }
552 i++;
553 }
555 return ferror(f);
556 }
558 #endif