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