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 { { HOME, 0x01, 0 }, 0, CMD_LIST_FIRST, "home",
80 N_("Home") },
81 { { END, 0x05, 0 }, 0, CMD_LIST_LAST, "end",
82 N_("End") },
83 { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
84 N_("Page up") },
85 { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
86 N_("Page down") },
89 /* basic screens */
90 { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
91 N_("Help screen") },
92 { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
93 N_("Playlist screen") },
94 { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
95 N_("Browse screen") },
98 /* player commands */
99 { { 13, 0, 0 }, 0, CMD_PLAY, "play",
100 N_("Play/Enter directory") },
101 { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
102 N_("Pause") },
103 { { 's', BS, 0 }, 0, CMD_STOP, "stop",
104 N_("Stop") },
105 { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
106 N_("Crop") },
107 { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
108 N_("Next track") },
109 { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
110 N_("Previous track") },
111 { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
112 N_("Seek forward") },
113 { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
114 N_("Seek backward") },
115 { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
116 N_("Increase volume") },
117 { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
118 N_("Decrease volume") },
119 { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
120 N_("Select/deselect song in playlist") },
121 { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
122 N_("Select all listed items") },
123 { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
124 N_("Delete song from playlist") },
125 { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
126 N_("Shuffle playlist") },
127 { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
128 N_("Clear playlist") },
129 { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
130 N_("Toggle repeat mode") },
131 { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
132 N_("Toggle random mode") },
133 { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
134 N_("Toggle crossfade mode") },
135 { { 21, 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
136 N_("Start a music database update") },
137 { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
138 N_("Save playlist") },
139 { { 'a', 0, 0 }, 0, CMD_ADD, "add",
140 N_("Add url/file to playlist") },
142 { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
143 N_("Go to root directory") },
144 { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
145 N_("Go to parent directory") },
147 #ifdef ENABLE_SONG_SCREEN
148 { { 'i', 0, 0 }, 0, CMD_VIEW, "view",
149 N_("View the song") },
150 #endif
152 { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
153 N_("Locate song in browser") },
155 /* lists */
156 { { 11, 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
157 N_("Move item up") },
158 { { 10, 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
159 N_("Move item down") },
160 { { 12, 0, 0 }, 0, CMD_SCREEN_UPDATE, "update",
161 N_("Refresh screen") },
164 /* ncmpc options */
165 { { 'w', 0, 0 }, 0, CMD_TOGGLE_FIND_WRAP, "wrap-mode",
166 /* translators: toggle between wrapping and non-wrapping
167 search */
168 N_("Toggle find mode") },
169 { { 'U', 0, 0 }, 0, CMD_TOGGLE_AUTOCENTER, "autocenter-mode",
170 /* translators: the auto center mode always centers the song
171 currently being played */
172 N_("Toggle auto center mode") },
175 /* change screen */
176 { { TAB, 0, 0 }, 0, CMD_SCREEN_NEXT, "screen-next",
177 N_("Next screen") },
178 { { STAB, 0, 0 }, 0, CMD_SCREEN_PREVIOUS, "screen-prev",
179 N_("Previous screen") },
182 /* find */
183 { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
184 N_("Forward find") },
185 { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
186 N_("Forward find next") },
187 { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
188 N_("Backward find") },
189 { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
190 N_("Backward find previous") },
193 /* extra screens */
194 #ifdef ENABLE_ARTIST_SCREEN
195 { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
196 N_("Artist screen") },
197 #endif
198 #ifdef ENABLE_SEARCH_SCREEN
199 { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
200 N_("Search screen") },
201 { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
202 N_("Change search mode") },
203 #endif
205 #ifdef ENABLE_LYRICS_SCREEN
206 { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
207 N_("Lyrics screen") },
208 { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
209 /* translators: interrupt the current background action,
210 e.g. stop loading lyrics from the internet */
211 N_("Interrupt action") },
212 { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
213 N_("Update Lyrics") },
214 #endif
216 #ifdef ENABLE_OUTPUTS_SCREEN
217 { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
218 N_("Outputs screen") },
219 #endif
222 { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
223 };
225 #ifdef ENABLE_KEYDEF_SCREEN
226 command_definition_t *
227 get_command_definitions(void)
228 {
229 return cmds;
230 }
231 #endif
233 const char *
234 key2str(int key)
235 {
236 static char buf[32];
237 int i;
239 buf[0] = 0;
240 switch(key) {
241 case 0:
242 return _("Undefined");
243 case ' ':
244 return _("Space");
245 case 13:
246 return _("Enter");
247 case BS:
248 return _("Backspace");
249 case DEL:
250 return _("Delete");
251 case UP:
252 return _("Up");
253 case DWN:
254 return _("Down");
255 case LEFT:
256 return _("Left");
257 case RGHT:
258 return _("Right");
259 case HOME:
260 return _("Home");
261 case END:
262 return _("End");
263 case PGDN:
264 return _("PageDown");
265 case PGUP:
266 return _("PageUp");
267 case TAB:
268 return _("Tab");
269 case STAB:
270 return _("Shift+Tab");
271 case ESC:
272 return _("Esc");
273 case KEY_IC:
274 return _("Insert");
275 default:
276 for (i = 0; i <= 63; i++)
277 if (key == KEY_F(i)) {
278 g_snprintf(buf, 32, "F%d", i );
279 return buf;
280 }
281 if (!(key & ~037))
282 g_snprintf(buf, 32, "Ctrl-%c", 'A'+(key & 037)-1 );
283 else if ((key & ~037) == 224)
284 g_snprintf(buf, 32, "Alt-%c", 'A'+(key & 037)-1 );
285 else if (key > 32 && key < 256)
286 g_snprintf(buf, 32, "%c", key);
287 else
288 g_snprintf(buf, 32, "0x%03X", key);
289 }
291 return buf;
292 }
294 void
295 command_dump_keys(void)
296 {
297 int i;
299 i = 0;
300 while (cmds[i].description) {
301 if (cmds[i].command != CMD_NONE)
302 printf(" %20s : %s\n", get_key_names(cmds[i].command,1),cmds[i].name);
303 i++;
304 }
305 }
307 #ifndef NCMPC_MINI
309 static int
310 set_key_flags(command_definition_t *cp, command_t command, int flags)
311 {
312 int i;
314 i = 0;
315 while (cp[i].name) {
316 if (cp[i].command == command) {
317 cp[i].flags |= flags;
318 return 0;
319 }
320 i++;
321 }
323 return 1;
324 }
326 #endif
328 const char *
329 get_key_names(command_t command, int all)
330 {
331 int i;
333 i=0;
334 while (cmds[i].description) {
335 if (cmds[i].command == command) {
336 int j;
337 static char keystr[80];
339 g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
340 if (!all)
341 return keystr;
342 j=1;
343 while (j < MAX_COMMAND_KEYS && cmds[i].keys[j] > 0) {
344 g_strlcat(keystr, " ", sizeof(keystr));
345 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
346 j++;
347 }
348 return keystr;
349 }
350 i++;
351 }
352 return NULL;
353 }
355 const char *
356 get_key_description(command_t command)
357 {
358 int i;
360 i=0;
361 while (cmds[i].description) {
362 if (cmds[i].command == command)
363 return _(cmds[i].description);
364 i++;
365 }
367 return NULL;
368 }
370 const char *
371 get_key_command_name(command_t command)
372 {
373 int i;
375 i=0;
376 while (cmds[i].name) {
377 if (cmds[i].command == command)
378 return cmds[i].name;
379 i++;
380 }
381 return NULL;
382 }
384 command_t
385 get_key_command_from_name(char *name)
386 {
387 int i;
389 i=0;
390 while (cmds[i].name) {
391 if (strcmp(name, cmds[i].name) == 0)
392 return cmds[i].command;
393 i++;
394 }
396 return CMD_NONE;
397 }
399 command_t
400 find_key_command(int key, command_definition_t *c)
401 {
402 int i;
404 i=0;
405 while (key && c && c[i].name) {
406 if (c[i].keys[0] == key ||
407 c[i].keys[1] == key ||
408 c[i].keys[2] == key)
409 return c[i].command;
410 i++;
411 }
413 return CMD_NONE;
414 }
416 command_t
417 get_key_command(int key)
418 {
419 return find_key_command(key, cmds);
420 }
422 command_t
423 get_keyboard_command(void)
424 {
425 int key;
427 key = wgetch(stdscr);
428 if (key == ERR)
429 return CMD_NONE;
431 #ifdef HAVE_GETMOUSE
432 if (key == KEY_MOUSE)
433 return CMD_MOUSE_EVENT;
434 #endif
436 return get_key_command(key);
437 }
439 int
440 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
441 {
442 int i;
444 i=0;
445 while (cmds[i].name) {
446 if (cmds[i].command == command) {
447 memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
448 #ifndef NCMPC_MINI
449 cmds[i].flags |= COMMAND_KEY_MODIFIED;
450 #endif
451 return 0;
452 }
453 i++;
454 }
455 return -1;
456 }
458 #ifndef NCMPC_MINI
460 int
461 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
462 {
463 int i;
464 int retval = 0;
466 if (cp == NULL)
467 cp = cmds;
469 i=0;
470 while (cp[i].name) {
471 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
472 i++;
473 }
475 i=0;
476 while (cp[i].name) {
477 int j;
478 command_t cmd;
480 for(j=0; j<MAX_COMMAND_KEYS; j++)
481 if (cp[i].keys[j] &&
482 (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
483 if (buf) {
484 g_snprintf(buf, bufsize,
485 _("Key %s assigned to %s and %s"),
486 key2str(cp[i].keys[j]),
487 get_key_command_name(cp[i].command),
488 get_key_command_name(cmd));
489 } else {
490 fprintf(stderr,
491 _("Key %s assigned to %s and %s"),
492 key2str(cp[i].keys[j]),
493 get_key_command_name(cp[i].command),
494 get_key_command_name(cmd));
495 fputc('\n', stderr);
496 }
497 cp[i].flags |= COMMAND_KEY_CONFLICT;
498 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
499 retval = -1;
500 }
501 i++;
502 }
503 return retval;
504 }
506 int
507 write_key_bindings(FILE *f, int flags)
508 {
509 int i,j;
511 if (flags & KEYDEF_WRITE_HEADER)
512 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
514 i = 0;
515 while (cmds[i].name && !ferror(f)) {
516 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
517 flags & KEYDEF_WRITE_ALL) {
518 fprintf(f, "## %s\n", cmds[i].description);
519 if (flags & KEYDEF_COMMENT_ALL)
520 fprintf(f, "#");
521 fprintf(f, "key %s = ", cmds[i].name);
522 for (j = 0; j < MAX_COMMAND_KEYS; j++) {
523 if (j && cmds[i].keys[j])
524 fprintf(f, ", ");
525 if (!j || cmds[i].keys[j]) {
526 if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
527 isdigit(cmds[i].keys[j])))
528 fprintf(f, "\'%c\'", cmds[i].keys[j]);
529 else
530 fprintf(f, "%d", cmds[i].keys[j]);
531 }
532 }
533 fprintf(f,"\n\n");
534 }
535 i++;
536 }
538 return ferror(f);
539 }
541 #endif