Code

command.c: remove obsolete DEBUG_KEYS and DK()
[ncmpc.git] / src / command.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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 #define KEY_CTL(x) ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
37 #define BS   KEY_BACKSPACE
38 #define DEL  KEY_DC
39 #define UP   KEY_UP
40 #define DWN  KEY_DOWN
41 #define LEFT KEY_LEFT
42 #define RGHT KEY_RIGHT
43 #define HOME KEY_HOME
44 #define END  KEY_END
45 #define PGDN KEY_NPAGE
46 #define PGUP KEY_PPAGE
47 #define TAB  0x09
48 #define STAB 0x161
49 #define ESC  0x1B
50 #define RET  '\r'
51 #define F1   KEY_F(1)
52 #define F2   KEY_F(2)
53 #define F3   KEY_F(3)
54 #define F4   KEY_F(4)
55 #define F5   KEY_F(5)
56 #define F6   KEY_F(6)
57 #define F7   KEY_F(7)
58 #define F8   KEY_F(8)
59 #define C(x) KEY_CTL(x)
61 static command_definition_t cmds[] = {
62 #ifdef ENABLE_KEYDEF_SCREEN
63         { {'K', 0, 0 }, 0, CMD_SCREEN_KEYDEF, "screen-keyedit",
64           N_("Key configuration screen") },
65 #endif
66         { { 'q', 'Q', C('C') }, 0, CMD_QUIT, "quit",
67           N_("Quit") },
69         /* movement */
70         { { UP, 'k', 0 }, 0, CMD_LIST_PREVIOUS, "up",
71           N_("Move cursor up") },
72         { { DWN, 'j', 0 }, 0, CMD_LIST_NEXT, "down",
73           N_("Move cursor down") },
74         { { 'H', 0, 0 }, 0, CMD_LIST_TOP, "top",
75           N_("Move cursor to the top of screen") },
76         { { 'M', 0, 0 }, 0, CMD_LIST_MIDDLE, "middle",
77           N_("Move cursor to the middle of screen") },
78         { { 'L', 0, 0 }, 0, CMD_LIST_BOTTOM, "bottom",
79           N_("Move cursor to the bottom of screen") },
80         { { HOME, C('A'), 0 }, 0, CMD_LIST_FIRST, "home",
81           N_("Move cursor to the top of the list") },
82         { { END, C('E'), 0 }, 0, CMD_LIST_LAST, "end",
83           N_("Move cursor to the bottom of the list") },
84         { { PGUP, 0, 0 }, 0, CMD_LIST_PREVIOUS_PAGE, "pgup",
85           N_("Page up") },
86         { { PGDN, 0, 0 }, 0, CMD_LIST_NEXT_PAGE, "pgdn",
87           N_("Page down") },
88         { { 'v',  0, 0 }, 0, CMD_LIST_RANGE_SELECT, "range-select",
89           N_("Range selection") },
90         { { C('N'),  0, 0 }, 0, CMD_LIST_SCROLL_DOWN_LINE, "scroll-down-line",
91           N_("Scroll up one line") },
92         { { C('B'),  0, 0 }, 0, CMD_LIST_SCROLL_UP_LINE, "scroll-up-line",
93           N_("Scroll down one line") },
94         { { 'N',  0, 0 }, 0, CMD_LIST_SCROLL_DOWN_HALF, "scroll-down-half",
95           N_("Scroll up half a screen") },
96         { { 'B',  0, 0 }, 0, CMD_LIST_SCROLL_UP_HALF, "scroll-up-half",
97           N_("Scroll down half a screen") },
98         { { 'l', 0, 0 }, 0, CMD_SELECT_PLAYING, "select-playing",
99           N_("Select currently playing song") },
102         /* basic screens */
103         { { '1', F1, 'h' }, 0, CMD_SCREEN_HELP, "screen-help",
104           N_("Help screen") },
105         { { '2', F2, 0 }, 0, CMD_SCREEN_PLAY, "screen-playlist",
106           N_("Playlist screen") },
107         { { '3', F3, 0 }, 0, CMD_SCREEN_FILE, "screen-browse",
108           N_("Browse screen") },
111         /* player commands */
112         { { RET, 0, 0 }, 0, CMD_PLAY, "play",
113           N_("Play/Enter directory") },
114         { { 'P', 0, 0 }, 0, CMD_PAUSE,"pause",
115           N_("Pause") },
116         { { 's', BS, 0 }, 0, CMD_STOP, "stop",
117           N_("Stop") },
118         { { 'o', 0, 0 }, 0, CMD_CROP, "crop",
119           N_("Crop") },
120         { { '>', 0, 0 }, 0, CMD_TRACK_NEXT, "next",
121           N_("Next track") },
122         { { '<', 0, 0 }, 0, CMD_TRACK_PREVIOUS, "prev",
123           N_("Previous track") },
124         { { 'f', 0, 0 }, 0, CMD_SEEK_FORWARD, "seek-forward",
125           N_("Seek forward") },
126         { { 'b', 0, 0 }, 0, CMD_SEEK_BACKWARD, "seek-backward",
127           N_("Seek backward") },
128         { { '+', RGHT, 0 }, 0, CMD_VOLUME_UP, "volume-up",
129           N_("Increase volume") },
130         { { '-', LEFT, 0 }, 0, CMD_VOLUME_DOWN, "volume-down",
131           N_("Decrease volume") },
132         { { ' ', 0, 0 }, 0, CMD_SELECT, "select",
133           N_("Select/deselect song in playlist") },
134         { { 't', 0, 0 }, 0, CMD_SELECT_ALL, "select_all",
135           N_("Select all listed items") },
136         { { DEL, 'd', 0 }, 0, CMD_DELETE, "delete",
137           N_("Delete song from playlist") },
138         { { 'Z', 0, 0 }, 0, CMD_SHUFFLE, "shuffle",
139           N_("Shuffle playlist") },
140         { { 'c', 0, 0 }, 0, CMD_CLEAR, "clear",
141           N_("Clear playlist") },
142         { { 'r', 0, 0 }, 0, CMD_REPEAT, "repeat",
143           N_("Toggle repeat mode") },
144         { { 'z', 0, 0 }, 0, CMD_RANDOM, "random",
145           N_("Toggle random mode") },
146         { { 'y', 0, 0 }, 0, CMD_SINGLE, "single",
147           N_("Toggle single mode") },
148         { { 'C', 0, 0 }, 0, CMD_CONSUME, "consume",
149           N_("Toggle consume mode") },
150         { { 'x', 0, 0 }, 0, CMD_CROSSFADE, "crossfade",
151           N_("Toggle crossfade mode") },
152         { { C('U'), 0, 0 }, 0, CMD_DB_UPDATE, "db-update",
153           N_("Start a music database update") },
154         { { 'S', 0, 0 }, 0, CMD_SAVE_PLAYLIST, "save",
155           N_("Save playlist") },
156         { { 'a', 0, 0 }, 0, CMD_ADD, "add",
157           N_("Add url/file to playlist") },
159         { { '!', 0, 0 }, 0, CMD_GO_ROOT_DIRECTORY, "go-root-directory",
160           N_("Go to root directory") },
161         { { '"', 0, 0 }, 0, CMD_GO_PARENT_DIRECTORY, "go-parent-directory",
162           N_("Go to parent directory") },
164         { { 'G', 0, 0 }, 0, CMD_LOCATE, "locate",
165           N_("Locate song in browser") },
167         /* lists */
168         { { C('K'), 0, 0 }, 0, CMD_LIST_MOVE_UP, "move-up",
169           N_("Move item up") },
170         { { C('J'), 0, 0 }, 0, CMD_LIST_MOVE_DOWN, "move-down",
171           N_("Move item down") },
172         { { C('L'), 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") },
192         { { '`', 0, 0 }, 0, CMD_SCREEN_SWAP, "screen-swap",
193           N_("Swap to most recent screen") },
196         /* find */
197         { { '/', 0, 0 }, 0, CMD_LIST_FIND, "find",
198           N_("Forward find") },
199         { { 'n', 0, 0 }, 0, CMD_LIST_FIND_NEXT, "find-next",
200           N_("Forward find next") },
201         { { '?', 0, 0 }, 0, CMD_LIST_RFIND, "rfind",
202           N_("Backward find") },
203         { { 'p', 0, 0 }, 0, CMD_LIST_RFIND_NEXT, "rfind-next",
204           N_("Backward find previous") },
205         { { '.', 0, 0 }, 0, CMD_LIST_JUMP, "jump",
206                 /* translators: this queries the user for a string
207                  * and jumps directly (while the user is typing)
208                  * to the entry which begins with this string */
209           N_("Jump to") },
212         /* extra screens */
213 #ifdef ENABLE_ARTIST_SCREEN
214         { {'4', F4, 0 }, 0, CMD_SCREEN_ARTIST, "screen-artist",
215           N_("Artist screen") },
216 #endif
217 #ifdef ENABLE_SEARCH_SCREEN
218         { {'5', F5, 0 }, 0, CMD_SCREEN_SEARCH, "screen-search",
219           N_("Search screen") },
220         { {'m', 0, 0 }, 0, CMD_SEARCH_MODE, "search-mode",
221           N_("Change search mode") },
222 #endif
223 #ifdef ENABLE_SONG_SCREEN
224         { { 'i', 0, 0 }, 0, CMD_SCREEN_SONG, "view",
225           N_("View the selected and the currently playing song") },
226 #endif
227 #ifdef ENABLE_LYRICS_SCREEN
228         { {'7', F7, 0 }, 0, CMD_SCREEN_LYRICS, "screen-lyrics",
229           N_("Lyrics screen") },
230         { {ESC, 0, 0 }, 0, CMD_INTERRUPT, "lyrics-interrupt",
231           /* translators: interrupt the current background action,
232              e.g. stop loading lyrics from the internet */
233           N_("Interrupt action") },
234         { {'u', 0, 0 }, 0, CMD_LYRICS_UPDATE, "lyrics-update",
235           N_("Update Lyrics") },
236         /* this command may move out of #ifdef ENABLE_LYRICS_SCREEN
237            at some point */
238         { {'e', 0, 0 }, 0, CMD_EDIT, "edit",
239           N_("Edit the current item") },
240 #endif
242 #ifdef ENABLE_OUTPUTS_SCREEN
243         { {'8', F8, 0 }, 0, CMD_SCREEN_OUTPUTS, "screen-outputs",
244           N_("Outputs screen") },
245 #endif
248         { { -1, -1, -1 }, 0, CMD_NONE, NULL, NULL }
249 };
251 #ifdef ENABLE_KEYDEF_SCREEN
252 command_definition_t *
253 get_command_definitions(void)
255         return cmds;
257 #endif
259 const char *
260 key2str(int key)
262         static char buf[32];
263         int i;
265         buf[0] = 0;
266         switch(key) {
267         case 0:
268                 return _("Undefined");
269         case ' ':
270                 return _("Space");
271         case RET:
272                 return _("Enter");
273         case BS:
274                 return _("Backspace");
275         case DEL:
276                 return _("Delete");
277         case UP:
278                 return _("Up");
279         case DWN:
280                 return _("Down");
281         case LEFT:
282                 return _("Left");
283         case RGHT:
284                 return _("Right");
285         case HOME:
286                 return _("Home");
287         case END:
288                 return _("End");
289         case PGDN:
290                 return _("PageDown");
291         case PGUP:
292                 return _("PageUp");
293         case TAB:
294                 return _("Tab");
295         case STAB:
296                 return _("Shift+Tab");
297         case ESC:
298                 return _("Esc");
299         case KEY_IC:
300                 return _("Insert");
301         default:
302                 for (i = 0; i <= 63; i++)
303                         if (key == KEY_F(i)) {
304                                 g_snprintf(buf, 32, _("F%d"), i );
305                                 return buf;
306                         }
307                 if (!(key & ~037))
308                         g_snprintf(buf, 32, _("Ctrl-%c"), 'A'+(key & 037)-1 );
309                 else if ((key & ~037) == 224)
310                         g_snprintf(buf, 32, _("Alt-%c"), 'A'+(key & 037)-1 );
311                 else if (key > 32 && key < 256)
312                         g_snprintf(buf, 32, "%c", key);
313                 else
314                         g_snprintf(buf, 32, "0x%03X", key);
315         }
317         return buf;
320 void
321 command_dump_keys(void)
323         for (int i = 0; cmds[i].description; i++)
324                 if (cmds[i].command != CMD_NONE)
325                         printf(" %20s : %s\n",
326                                get_key_names(cmds[i].command, true),
327                                cmds[i].name);
330 #ifndef NCMPC_MINI
332 static void
333 set_key_flags(command_definition_t *cp, command_t command, int flags)
335         for (int i = 0; cp[i].name; i++) {
336                 if (cp[i].command == command) {
337                         cp[i].flags |= flags;
338                         break;
339                 }
340         }
343 #endif
345 const char *
346 get_key_names(command_t command, bool all)
348         for (int i = 0; cmds[i].description; i++) {
349                 if (cmds[i].command == command) {
350                         static char keystr[80];
352                         g_strlcpy(keystr, key2str(cmds[i].keys[0]), sizeof(keystr));
353                         if (!all)
354                                 return keystr;
356                         for (int j = 1; j < MAX_COMMAND_KEYS &&
357                                         cmds[i].keys[j] > 0; j++) {
358                                 g_strlcat(keystr, " ", sizeof(keystr));
359                                 g_strlcat(keystr, key2str(cmds[i].keys[j]), sizeof(keystr));
360                         }
361                         return keystr;
362                 }
363         }
364         return NULL;
367 const char *
368 get_key_description(command_t command)
370         for (int i = 0; cmds[i].description; i++)
371                 if (cmds[i].command == command)
372                         return _(cmds[i].description);
374         return NULL;
377 const char *
378 get_key_command_name(command_t command)
380         for (int i = 0; cmds[i].name; i++)
381                 if (cmds[i].command == command)
382                         return cmds[i].name;
384         return NULL;
387 command_t
388 get_key_command_from_name(char *name)
390         for (int i = 0; cmds[i].name; i++)
391                 if (strcmp(name, cmds[i].name) == 0)
392                         return cmds[i].command;
394         return CMD_NONE;
397 command_t
398 find_key_command(int key, command_definition_t *c)
400         assert(key != 0);
401         assert(c != NULL);
403         for (int i = 0; c[i].name; i++) {
404                 for (int j = 0; j < MAX_COMMAND_KEYS; j++)
405                         if (c[i].keys[j] == key)
406                                 return c[i].command;
407         }
409         return CMD_NONE;
412 command_t
413 get_key_command(int key)
415         return find_key_command(key, cmds);
418 command_t
419 get_keyboard_command(void)
421         int key;
423         key = wgetch(stdscr);
424         if (key == ERR)
425                 return CMD_NONE;
427 #ifdef HAVE_GETMOUSE
428         if (key == KEY_MOUSE)
429                 return CMD_MOUSE_EVENT;
430 #endif
432         return get_key_command(key);
435 int
436 assign_keys(command_t command, int keys[MAX_COMMAND_KEYS])
438         for (int i = 0; cmds[i].name; i++) {
439                 if (cmds[i].command == command) {
440                         memcpy(cmds[i].keys, keys, sizeof(int)*MAX_COMMAND_KEYS);
441 #ifndef NCMPC_MINI
442                         cmds[i].flags |= COMMAND_KEY_MODIFIED;
443 #endif
444                         return 0;
445                 }
446         }
448         return -1;
451 #ifndef NCMPC_MINI
453 int
454 check_key_bindings(command_definition_t *cp, char *buf, size_t bufsize)
456         int i;
457         int retval = 0;
459         if (cp == NULL)
460                 cp = cmds;
462         for (i = 0; cp[i].name; i++)
463                 cp[i].flags &= ~COMMAND_KEY_CONFLICT;
465         for (i = 0; cp[i].name; i++) {
466                 int j;
467                 command_t cmd;
469                 for(j=0; j<MAX_COMMAND_KEYS; j++) {
470                         if (cp[i].keys[j] &&
471                             (cmd = find_key_command(cp[i].keys[j],cp)) != cp[i].command) {
472                                 if (buf) {
473                                         g_snprintf(buf, bufsize,
474                                                    _("Key %s assigned to %s and %s"),
475                                                    key2str(cp[i].keys[j]),
476                                                    get_key_command_name(cp[i].command),
477                                                    get_key_command_name(cmd));
478                                 } else {
479                                         fprintf(stderr,
480                                                 _("Key %s assigned to %s and %s"),
481                                                 key2str(cp[i].keys[j]),
482                                                 get_key_command_name(cp[i].command),
483                                                 get_key_command_name(cmd));
484                                         fputc('\n', stderr);
485                                 }
486                                 cp[i].flags |= COMMAND_KEY_CONFLICT;
487                                 set_key_flags(cp, cmd, COMMAND_KEY_CONFLICT);
488                                 retval = -1;
489                         }
490                 }
491         }
493         return retval;
496 int
497 write_key_bindings(FILE *f, int flags)
499         if (flags & KEYDEF_WRITE_HEADER)
500                 fprintf(f, "## Key bindings for ncmpc (generated by ncmpc)\n\n");
502         for (int i = 0; cmds[i].name && !ferror(f); i++) {
503                 if (cmds[i].flags & COMMAND_KEY_MODIFIED ||
504                     flags & KEYDEF_WRITE_ALL) {
505                         fprintf(f, "## %s\n", cmds[i].description);
506                         if (flags & KEYDEF_COMMENT_ALL)
507                                 fprintf(f, "#");
508                         fprintf(f, "key %s = ", cmds[i].name);
509                         for (int j = 0; j < MAX_COMMAND_KEYS; j++) {
510                                 if (j && cmds[i].keys[j])
511                                         fprintf(f, ",  ");
512                                 if (!j || cmds[i].keys[j]) {
513                                         if (cmds[i].keys[j]<256 && (isalpha(cmds[i].keys[j]) ||
514                                                                     isdigit(cmds[i].keys[j])))
515                                                 fprintf(f, "\'%c\'", cmds[i].keys[j]);
516                                         else
517                                                 fprintf(f, "%d", cmds[i].keys[j]);
518                                 }
519                         }
520                         fprintf(f,"\n\n");
521                 }
522         }
524         return ferror(f);
527 #endif /* NCMPC_MINI */